merge a check into a place where it is simpler.
authorChris Lattner <sabre@nondot.org>
Sun, 16 Nov 2008 05:10:52 +0000 (05:10 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 16 Nov 2008 05:10:52 +0000 (05:10 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@59400 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/InstructionCombining.cpp

index 9447e4d208a423e9fa41eff39945fbe25ff5f20d..a8dcfa447da0430b26ee1f077a2447fca0ba97e1 100644 (file)
@@ -3556,15 +3556,28 @@ Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
 /// FoldAndOfICmps - Fold (icmp)&(icmp) if possible.
 Instruction *InstCombiner::FoldAndOfICmps(Instruction &I,
                                           ICmpInst *LHS, ICmpInst *RHS) {
-  Value *Val;
+  Value *Val, *Val2;
   ConstantInt *LHSCst, *RHSCst;
   ICmpInst::Predicate LHSCC, RHSCC;
   
-  // (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
+  // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
   if (!match(LHS, m_ICmp(LHSCC, m_Value(Val), m_ConstantInt(LHSCst))) ||
-      !match(RHS, m_ICmp(RHSCC, m_Specific(Val), m_ConstantInt(RHSCst))))
+      !match(RHS, m_ICmp(RHSCC, m_Value(Val2), m_ConstantInt(RHSCst))))
     return 0;
-    
+  
+  // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
+  // where C is a power of 2
+  if (LHSCst == RHSCst && LHSCC == RHSCC && LHSCC == ICmpInst::ICMP_ULT &&
+      LHSCst->getValue().isPowerOf2()) {
+    Instruction *NewOr = BinaryOperator::CreateOr(Val, Val2);
+    InsertNewInstBefore(NewOr, I);
+    return new ICmpInst(LHSCC, NewOr, LHSCst);
+  }
+  
+  // From here on, we only handle:
+  //    (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
+  if (Val != Val2) return 0;
+  
   // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
   if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
       RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
@@ -3931,22 +3944,6 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
     }
   }
   
-  { // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
-    // where C is a power of 2
-    Value *A, *B;
-    ConstantInt *C1, *C2;
-    ICmpInst::Predicate LHSCC = ICmpInst::BAD_ICMP_PREDICATE;
-    ICmpInst::Predicate RHSCC = ICmpInst::BAD_ICMP_PREDICATE;
-    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 && LHSCC == ICmpInst::ICMP_ULT &&
-          C1->getValue().isPowerOf2()) {
-        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)))