Fold (a < 8) && (b < 8) into (a|b) < 8 for unsigned less or greater than.
authorNick Lewycky <nicholas@mxc.ca>
Wed, 9 Jul 2008 07:29:11 +0000 (07:29 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Wed, 9 Jul 2008 07:29:11 +0000 (07:29 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@53282 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/InstructionCombining.cpp
test/Transforms/InstCombine/2008-07-08-AndICmp.ll [new file with mode: 0644]

index 27af3c397a1685f2cd85a99314e13fb15636c889..8d35bb5af03eb2e70e03c81304feaf87ef6b82f9 100644 (file)
@@ -3594,6 +3594,22 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
     }
   }
   
+  
+  { // (icmp ugt/ult A, C) & (icmp B, C) --> (icmp (A|B), C)
+    // where C is a power of 2
+    Value *A, *B;
+    ConstantInt *C1, *C2;
+    ICmpInst::Predicate LHSCC, RHSCC;
+    if (match(&I, m_And(m_ICmp(LHSCC, m_Value(A), m_ConstantInt(C1)),
+                        m_ICmp(RHSCC, m_Value(B), m_ConstantInt(C2)))))
+      if (C1 == C2 && LHSCC == RHSCC && C1->getValue().isPowerOf2() &&
+          (LHSCC == ICmpInst::ICMP_ULT || LHSCC == ICmpInst::ICMP_UGT)) {
+        Instruction *NewOr = BinaryOperator::CreateOr(A, B);
+        InsertNewInstBefore(NewOr, I);
+        return new ICmpInst(LHSCC, NewOr, C1);
+      }
+  }
+
   if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) {
     // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
     if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
diff --git a/test/Transforms/InstCombine/2008-07-08-AndICmp.ll b/test/Transforms/InstCombine/2008-07-08-AndICmp.ll
new file mode 100644 (file)
index 0000000..c600241
--- /dev/null
@@ -0,0 +1,10 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep icmp | count 1
+; PR2330
+
+define i1 @foo(i32 %a, i32 %b) nounwind {
+entry:
+       icmp ult i32 %a, 8              ; <i1>:0 [#uses=1]
+       icmp ult i32 %b, 8              ; <i1>:1 [#uses=1]
+       and i1 %1, %0           ; <i1>:2 [#uses=1]
+       ret i1 %2
+}