[llvm-commits] [llvm] r53393 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/2008-07-09-SubAndError.ll

Nick Lewycky nicholas at mxc.ca
Wed Jul 9 22:51:40 PDT 2008


Author: nicholas
Date: Thu Jul 10 00:51:40 2008
New Revision: 53393

URL: http://llvm.org/viewvc/llvm-project?rev=53393&view=rev
Log:
Fix overzealous optimization. Thanks to Duncan Sands for pointing out my error!

Added:
    llvm/trunk/test/Transforms/InstCombine/2008-07-09-SubAndError.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=53393&r1=53392&r2=53393&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Thu Jul 10 00:51:40 2008
@@ -3462,16 +3462,21 @@
         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:

Added: llvm/trunk/test/Transforms/InstCombine/2008-07-09-SubAndError.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/2008-07-09-SubAndError.ll?rev=53393&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/2008-07-09-SubAndError.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/2008-07-09-SubAndError.ll Thu Jul 10 00:51:40 2008
@@ -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
+}





More information about the llvm-commits mailing list