[llvm] [InstCombine] Fold zext-of-icmp with no shift (PR #68503)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 9 04:15:07 PDT 2023


================
@@ -904,37 +904,34 @@ Instruction *InstCombinerImpl::transformZExtICmp(ICmpInst *Cmp,
     // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
     // zext (X != 0) to i32 --> X        iff X has only the low bit set.
     // zext (X != 0) to i32 --> X>>1     iff X has only the 2nd bit set.
-    if (Op1CV->isZero() && Cmp->isEquality() &&
-        (Cmp->getOperand(0)->getType() == Zext.getType() ||
-         Cmp->getPredicate() == ICmpInst::ICMP_NE)) {
-      // If Op1C some other power of two, convert:
-      KnownBits Known = computeKnownBits(Cmp->getOperand(0), 0, &Zext);
 
-      // Exactly 1 possible 1? But not the high-bit because that is
-      // canonicalized to this form.
-      APInt KnownZeroMask(~Known.Zero);
-      if (KnownZeroMask.isPowerOf2() &&
-          (Zext.getType()->getScalarSizeInBits() !=
-           KnownZeroMask.logBase2() + 1)) {
-        uint32_t ShAmt = KnownZeroMask.logBase2();
-        Value *In = Cmp->getOperand(0);
-        if (ShAmt) {
-          // Perform a logical shr by shiftamt.
-          // Insert the shift to put the result in the low bit.
-          In = Builder.CreateLShr(In, ConstantInt::get(In->getType(), ShAmt),
-                                  In->getName() + ".lobit");
-        }
+    // Exactly 1 possible 1? But not the high-bit because that is
+    // canonicalized to this form.
+    KnownBits Known = computeKnownBits(Cmp->getOperand(0), 0, &Zext);
----------------
vfdff wrote:

Apply your suggestion, thanks @nikic 

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


More information about the llvm-commits mailing list