[PATCH] [InstCombine] Constant comparison involving "lshr exact"

Rahul Jain rahul1.jain at samsung.com
Mon Jun 2 03:24:27 PDT 2014


Hi rafael, majnemer, bkramer,


Hi all,

This is a follow up patch for http://llvm.org/viewvc/llvm-project?view=revision&revision=209903.
Added support to optimize comparisons with "lshr exact" of a constant
similar to "ashr exact".  

I could have merged the code for both, but didnt seem to make much of a difference,
so handled after the code to handle "ashr exact".

Please help in reviewing the same.

Thanks,
Rahul

http://reviews.llvm.org/D3987

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

Index: test/Transforms/InstCombine/icmp.ll
===================================================================
--- test/Transforms/InstCombine/icmp.ll
+++ test/Transforms/InstCombine/icmp.ll
@@ -1373,3 +1373,11 @@
   %cmp = icmp eq i32 %shr, -15
   ret i1 %cmp
 }
+
+; CHECK-LABEL: @exact_lhsr
+; CHECK-NEXT: icmp eq i32 %a, 3
+define i1 @exact_lhsr(i32 %a) {
+  %shr = lshr exact i32 80, %a
+  %cmp = icmp eq i32 %shr, 10
+  ret i1 %cmp
+}
Index: lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2444,17 +2444,28 @@
     // 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;
+      ConstantInt *CI2 = nullptr;
       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));
       }
     }
+    
+    // Handle the case for lhsr similar to above transformation.
+    {
+      ConstantInt *CI2 = nullptr;
+      if (match(Op0, m_LShr(m_ConstantInt(CI2), m_Value(A))) &&
+          (cast<BinaryOperator>(Op0)->isExact())) {
+        APInt Quotient = CI2->getValue().udiv(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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3987.10009.patch
Type: text/x-patch
Size: 1986 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140602/726e7df3/attachment.bin>


More information about the llvm-commits mailing list