[llvm] r209903 - PR19753: Optimize comparisons with "ashr exact" of a constanst.

Rafael Espindola rafael.espindola at gmail.com
Fri May 30 08:54:32 PDT 2014


Author: rafael
Date: Fri May 30 10:54:32 2014
New Revision: 209903

URL: http://llvm.org/viewvc/llvm-project?rev=209903&view=rev
Log:
PR19753: Optimize comparisons with "ashr exact" of a constanst.

Patch by suyog sarda.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/test/Transforms/InstCombine/icmp.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=209903&r1=209902&r2=209903&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Fri May 30 10:54:32 2014
@@ -2439,6 +2439,23 @@ Instruction *InstCombiner::visitICmpInst
       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

Modified: llvm/trunk/test/Transforms/InstCombine/icmp.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp.ll?rev=209903&r1=209902&r2=209903&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/icmp.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/icmp.ll Fri May 30 10:54:32 2014
@@ -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
+}





More information about the llvm-commits mailing list