[llvm] [InstCombine] Fold `ctpop(X) == 1` into `ctpop(X) <u 2` if X is non-zero (PR #67268)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 24 10:36:34 PDT 2023


================
@@ -3412,6 +3412,15 @@ static Instruction *foldCtpopPow2Test(ICmpInst &I, IntrinsicInst *CtpopLhs,
                                       const SimplifyQuery &Q) {
   assert(CtpopLhs->getIntrinsicID() == Intrinsic::ctpop &&
          "Non-ctpop intrin in ctpop fold");
+
+  const ICmpInst::Predicate Pred = I.getPredicate();
+  // If we know X is non-zero, we can fold `ctpop(X) == 1` into `ctpop(X) <u 2`
+  // since the latter gives better codegen.
+  if (Pred == ICmpInst::ICMP_EQ && CRhs == 1 &&
----------------
nikic wrote:

You should handle the conjugate `ne` case as well.

https://github.com/llvm/llvm-project/pull/67268


More information about the llvm-commits mailing list