PR19753: Optimize comparisons with "ashr exact" of a constanst.
authorRafael Espindola <rafael.espindola@gmail.com>
Fri, 30 May 2014 15:54:32 +0000 (15:54 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Fri, 30 May 2014 15:54:32 +0000 (15:54 +0000)
Patch by suyog sarda.

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

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

index 02e8bf10135fad84bb955a20f8a0f5d692b63f48..79cd1fba85fe59c720bd93e6656e39fc61ff0ddb 100644 (file)
@@ -2439,6 +2439,23 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
       return new ICmpInst(I.getPredicate(), A, B);
     }
 
+    // PR19753:
+    // (icmp (ashr exact const2, A), const1) -> icmp A, Log2(const2/const1)
+    // Cases where const1 doesn't divide const2 exactly or Quotient is not
+    // exact of log2 are handled by SimplifyICmpInst call above where we
+    // return false.
+    // TODO: Handle this for lshr exact with udiv.
+    {
+      ConstantInt *CI2;
+      if (match(Op0, m_AShr(m_ConstantInt(CI2), m_Value(A))) &&
+          (cast<BinaryOperator>(Op0)->isExact())) {
+        APInt Quotient = CI2->getValue().sdiv(CI->getValue());
+        unsigned shift = Quotient.logBase2();
+        return new ICmpInst(I.getPredicate(), A,
+                            ConstantInt::get(A->getType(), shift));
+      }
+    }
+
     // If we have an icmp le or icmp ge instruction, turn it into the
     // appropriate icmp lt or icmp gt instruction.  This allows us to rely on
     // them being folded in the code below.  The SimplifyICmpInst code has
index f45897cd3eec0500be1599f63dc7b169663748c4..fd7514d9e17fc05c70dac057fe7e7aedae3491fd 100644 (file)
@@ -1365,3 +1365,11 @@ define i1 @icmp_neg_cst_slt(i32 %a) {
   %2 = icmp slt i32 %1, -10
   ret i1 %2
 }
+
+; CHECK-LABEL: @exact_ashr_eq_false
+; CHECK-NEXT: icmp eq i32 %a, 1
+define i1 @exact_ashr_eq_false(i32 %a) {
+  %shr = ashr exact i32 -30, %a
+  %cmp = icmp eq i32 %shr, -15
+  ret i1 %cmp
+}