Fix overzealous optimization. Thanks to Duncan Sands for pointing out my error!
authorNick Lewycky <nicholas@mxc.ca>
Thu, 10 Jul 2008 05:51:40 +0000 (05:51 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Thu, 10 Jul 2008 05:51:40 +0000 (05:51 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@53393 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/InstructionCombining.cpp
test/Transforms/InstCombine/2008-07-09-SubAndError.ll [new file with mode: 0644]

index c573f7f5312f68914d0914e079d31ea29b75180a..e0e3f49be1882ae6ac46a5e81b9a93b8fb4e7e00 100644 (file)
@@ -3462,16 +3462,21 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
           return BinaryOperator::CreateAnd(V, AndRHS);
 
-        // (A - N) & AndRHS -> -N & AndRHS where A & AndRHS == 0
-        if (Op0I->hasOneUse() && MaskedValueIsZero(Op0LHS, AndRHSMask)) {
+        // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS
+        // has 1's for all bits that the subtraction with A might affect.
+        if (Op0I->hasOneUse()) {
+          uint32_t BitWidth = AndRHSMask.getBitWidth();
+          uint32_t Zeros = AndRHSMask.countLeadingZeros();
+          APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros);
+
           ConstantInt *A = dyn_cast<ConstantInt>(Op0LHS);
-          if (!A || !A->isZero()) {
+          if (!(A && A->isZero()) &&               // avoid infinite recursion.
+              MaskedValueIsZero(Op0LHS, Mask)) {
             Instruction *NewNeg = BinaryOperator::CreateNeg(Op0RHS);
             InsertNewInstBefore(NewNeg, I);
             return BinaryOperator::CreateAnd(NewNeg, AndRHS);
           }
         }
-
         break;
 
       case Instruction::Shl:
diff --git a/test/Transforms/InstCombine/2008-07-09-SubAndError.ll b/test/Transforms/InstCombine/2008-07-09-SubAndError.ll
new file mode 100644 (file)
index 0000000..c5b9358
--- /dev/null
@@ -0,0 +1,9 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep {sub i32 0}
+; PR2330
+
+define i32 @foo(i32 %a) nounwind {
+entry:
+  %A = sub i32 5, %a
+  %B = and i32 %A, 2
+  ret i32 %B
+}