Fix a ValueTracking rule: RHS means operand 1, not 0. Add a simple
authorDan Gohman <gohman@apple.com>
Tue, 24 Feb 2009 02:00:40 +0000 (02:00 +0000)
committerDan Gohman <gohman@apple.com>
Tue, 24 Feb 2009 02:00:40 +0000 (02:00 +0000)
ashr instcombine to help expose this code. And apply the fix to
SelectionDAG's copy of this code too.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@65364 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ValueTracking.cpp
lib/CodeGen/SelectionDAG/SelectionDAG.cpp
lib/Transforms/Scalar/InstructionCombining.cpp
test/Transforms/InstCombine/ashr-nop.ll [new file with mode: 0644]

index 7ac138b90ef7cbfd8eda4a1c02a15be20d85c7a4..20fa69ea24f59c3238eb406a10a559983fa903b0 100644 (file)
@@ -625,7 +625,7 @@ unsigned llvm::ComputeNumSignBits(Value *V, TargetData *TD, unsigned Depth) {
     if (Tmp == 1) return 1;  // Early out.
       
     // Special case decrementing a value (ADD X, -1):
-    if (ConstantInt *CRHS = dyn_cast<ConstantInt>(U->getOperand(0)))
+    if (ConstantInt *CRHS = dyn_cast<ConstantInt>(U->getOperand(1)))
       if (CRHS->isAllOnesValue()) {
         APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
         APInt Mask = APInt::getAllOnesValue(TyBits);
index 66356f5f9afa79b51b3155c90e3464bca17ea5d2..358c9703a802bca916cd3e3b6e7fc6285644bb2a 100644 (file)
@@ -2008,7 +2008,7 @@ unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
     if (Tmp == 1) return 1;  // Early out.
 
     // Special case decrementing a value (ADD X, -1):
-    if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
+    if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
       if (CRHS->isAllOnesValue()) {
         APInt KnownZero, KnownOne;
         APInt Mask = APInt::getAllOnesValue(VTBits);
index 7f2cab96ec5550993c0d12cc8d794a152f99841f..62554a5e91fa056c81f88e3dfa5b2821c89c308a 100644 (file)
@@ -7031,7 +7031,12 @@ Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
       MaskedValueIsZero(Op0,
                       APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())))
     return BinaryOperator::CreateLShr(Op0, I.getOperand(1));
-  
+
+  // Arithmetic shifting an all-sign-bit value is a no-op.
+  unsigned NumSignBits = ComputeNumSignBits(Op0);
+  if (NumSignBits == Op0->getType()->getPrimitiveSizeInBits())
+    return ReplaceInstUsesWith(I, Op0);
+
   return 0;
 }
 
diff --git a/test/Transforms/InstCombine/ashr-nop.ll b/test/Transforms/InstCombine/ashr-nop.ll
new file mode 100644 (file)
index 0000000..bb0da34
--- /dev/null
@@ -0,0 +1,8 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep ashr
+
+define i32 @foo(i32 %x) {
+  %o = and i32 %x, 1
+  %n = add i32 %o, -1
+  %t = ashr i32 %n, 17
+  ret i32 %t
+}