[InstCombine] remove zext-of-icmp fold that may conflict with other folds
authorSanjay Patel <spatel@rotateright.com>
Tue, 10 Jan 2023 14:54:54 +0000 (09:54 -0500)
committerSanjay Patel <spatel@rotateright.com>
Tue, 10 Jan 2023 15:23:21 +0000 (10:23 -0500)
This bit-hack transform would cause the new test to infinite loop
after 21d3871b7c90f85b3ae.

The deleted transform has existed for a very long time,
but the profitable parts appear to be handled by other
folds now. This fold could replace 2 instructions with
4 instructions, so it was always in danger of going
overboard.

No tests regress by removing the whole thing.

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
llvm/test/Transforms/InstCombine/zext.ll

index d9f8996..75ccb10 100644 (file)
@@ -1072,39 +1072,6 @@ Instruction *InstCombinerImpl::transformZExtICmp(ICmpInst *Cmp,
       Value *And1 = Builder.CreateAnd(Lshr, ConstantInt::get(X->getType(), 1));
       return replaceInstUsesWith(Zext, And1);
     }
-
-    // icmp ne A, B is equal to xor A, B when A and B only really have one bit.
-    // It is also profitable to transform icmp eq into not(xor(A, B)) because
-    // that may lead to additional simplifications.
-    if (IntegerType *ITy = dyn_cast<IntegerType>(Zext.getType())) {
-      Value *LHS = Cmp->getOperand(0);
-      Value *RHS = Cmp->getOperand(1);
-
-      KnownBits KnownLHS = computeKnownBits(LHS, 0, &Zext);
-      KnownBits KnownRHS = computeKnownBits(RHS, 0, &Zext);
-
-      if (KnownLHS == KnownRHS) {
-        APInt KnownBits = KnownLHS.Zero | KnownLHS.One;
-        APInt UnknownBit = ~KnownBits;
-        if (UnknownBit.countPopulation() == 1) {
-          Value *Result = Builder.CreateXor(LHS, RHS);
-
-          // Mask off any bits that are set and won't be shifted away.
-          if (KnownLHS.One.uge(UnknownBit))
-            Result = Builder.CreateAnd(Result,
-                                        ConstantInt::get(ITy, UnknownBit));
-
-          // Shift the bit we're testing down to the lsb.
-          Result = Builder.CreateLShr(
-               Result, ConstantInt::get(ITy, UnknownBit.countTrailingZeros()));
-
-          if (Cmp->getPredicate() == ICmpInst::ICMP_EQ)
-            Result = Builder.CreateXor(Result, ConstantInt::get(ITy, 1));
-          Result->takeName(Cmp);
-          return replaceInstUsesWith(Zext, Result);
-        }
-      }
-    }
   }
 
   return nullptr;
index c0275e9..d223c94 100644 (file)
@@ -685,3 +685,20 @@ define i16 @zext_icmp_eq0_pow2_use2(i32 %x) {
   %z = zext i1 %i to i16
   ret i16 %z
 }
+
+; This used to infinite loop.
+
+define i8 @zext_icmp_eq_pow2(i8 %y, i8 %x) {
+; CHECK-LABEL: @zext_icmp_eq_pow2(
+; CHECK-NEXT:    [[SHLX:%.*]] = shl i8 [[X:%.*]], 7
+; CHECK-NEXT:    [[SHLY:%.*]] = shl i8 -128, [[Y:%.*]]
+; CHECK-NEXT:    [[C:%.*]] = icmp eq i8 [[SHLX]], [[SHLY]]
+; CHECK-NEXT:    [[R:%.*]] = zext i1 [[C]] to i8
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %shlx = shl i8 %x, 7
+  %shly = shl i8 -128, %y
+  %c = icmp eq i8 %shlx, %shly
+  %r = zext i1 %c to i8
+  ret i8 %r
+}