Teach InstCombine to fold "(shr exact X, Y) == 0" --> X == 0, fixing #1 from
authorNick Lewycky <nicholas@mxc.ca>
Mon, 28 Feb 2011 08:31:40 +0000 (08:31 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Mon, 28 Feb 2011 08:31:40 +0000 (08:31 +0000)
PR9343.

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

lib/Transforms/InstCombine/InstCombineCompares.cpp
test/Transforms/InstCombine/icmp.ll

index 7c67bf61b6ccc1a451c30cfac5939c535f753d18..79c5d88c6315891de26b46c346ad9315a418d654 100644 (file)
@@ -1289,13 +1289,21 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
   }
     
   case Instruction::LShr:         // (icmp pred (shr X, ShAmt), CI)
-  case Instruction::AShr:
-    // Only handle equality comparisons of shift-by-constant.
-    if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)))
-      if (Instruction *Res = FoldICmpShrCst(ICI, cast<BinaryOperator>(LHSI),
-                                            ShAmt))
+  case Instruction::AShr: {
+    // Handle equality comparisons of shift-by-constant.
+    BinaryOperator *BO = cast<BinaryOperator>(LHSI);
+    if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
+      if (Instruction *Res = FoldICmpShrCst(ICI, BO, ShAmt))
         return Res;
+    }
+
+    // Handle exact shr's.
+    if (ICI.isEquality() && BO->isExact() && BO->hasOneUse()) {
+      if (RHSV.isMinValue())
+        return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), RHS);
+    }
     break;
+  }
     
   case Instruction::SDiv:
   case Instruction::UDiv:
index c11dea5b8fa1dafa34f08302845413c60a98a1fe..f6a18faeb33d40d0a8b2ae32d2bc94f2df557743 100644 (file)
@@ -387,3 +387,20 @@ define i1 @test39(i31 %X, i32 %Y) {
   %C = icmp slt i32 %B, 0
   ret i1 %C
 }
+
+; PR9343 #1
+; CHECK: test40
+; CHECK %B = icmp eq i32 %X, 0
+define i1 @test40(i32 %X, i32 %Y) {
+  %A = ashr exact i32 %X, %Y
+  %B = icmp eq i32 %A, 0
+  ret i1 %B
+}
+
+; CHECK: test41
+; CHECK %B = icmp ne i32 %X, 0
+define i1 @test41(i32 %X, i32 %Y) {
+  %A = lshr exact i32 %X, %Y
+  %B = icmp ne i32 %A, 0
+  ret i1 %B
+}