[PATCH] Patch for PR19753 InstCombine: constant comparison involving "ashr exact" not optimized well

suyog suyog.sarda at samsung.com
Fri May 30 08:25:48 PDT 2014


Hi Rafael,

Thanks for the review! 

I have made changes as suggested by you :
* FIXME/TODO about the lhsr
* The comment variable names update in the comment.
* A comment about the inexact div and log being handled in instsimplify

If this looks good to you, can you please commit this on my behalf. 

Thanks once again for reviews and help.

http://reviews.llvm.org/D3959

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

Index: lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2439,6 +2439,23 @@
       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: test/Transforms/InstCombine/icmp.ll
===================================================================
--- test/Transforms/InstCombine/icmp.ll
+++ test/Transforms/InstCombine/icmp.ll
@@ -1365,3 +1365,11 @@
   %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
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3959.9961.patch
Type: text/x-patch
Size: 1718 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140530/5376f040/attachment.bin>


More information about the llvm-commits mailing list