Index: include/llvm/Analysis/ValueTracking.h =================================================================== --- include/llvm/Analysis/ValueTracking.h +++ include/llvm/Analysis/ValueTracking.h @@ -422,6 +422,16 @@ SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp = nullptr); + /// Return true if RHS is known to be implied by LHS. A & B must be i1 + /// (boolean) values or a vector of such values. Note that the truth table for + /// implication is the same as <=u on i1 values (but not <=s!). The truth + /// table for both is: + /// | T | F (B) + /// T | T | F + /// F | T | T + /// (A) + bool isImpliedCondition(Value *LHS, Value *RHS); + } // end namespace llvm #endif Index: lib/Analysis/InstructionSimplify.cpp =================================================================== --- lib/Analysis/InstructionSimplify.cpp +++ lib/Analysis/InstructionSimplify.cpp @@ -2128,54 +2128,6 @@ return nullptr; } -/// Return true if B is known to be implied by A. A & B must be i1 (boolean) -/// values or a vector of such values. Note that the truth table for -/// implication is the same as <=u on i1 values (but not <=s!). The truth -/// table for both is: -/// | T | F (B) -/// T | T | F -/// F | T | T -/// (A) -static bool implies(Value *A, Value *B) { - assert(A->getType() == B->getType() && "mismatched type"); - Type *OpTy = A->getType(); - assert(OpTy->getScalarType()->isIntegerTy(1)); - - // A ==> A by definition - if (A == B) return true; - - if (OpTy->isVectorTy()) - // TODO: extending the code below to handle vectors - return false; - assert(OpTy->isIntegerTy(1) && "implied by above"); - - ICmpInst::Predicate APred, BPred; - Value *I; - Value *L; - ConstantInt *CI; - // i +_{nsw} C_{>0} i isNegative() && - match(B, m_ICmp(BPred, m_Specific(I), m_Specific(L))) && - BPred == ICmpInst::ICMP_SLT) - return true; - - // i +_{nuw} C_{>0} i isNegative() && - match(B, m_ICmp(BPred, m_Specific(I), m_Specific(L))) && - BPred == ICmpInst::ICMP_ULT) - return true; - - return false; -} - static ConstantRange GetConstantRangeFromMetadata(MDNode *Ranges, uint32_t BitWidth) { const unsigned NumRanges = Ranges->getNumOperands() / 2; assert(NumRanges >= 1); @@ -2247,7 +2199,7 @@ // X >=u 1 -> X if (match(RHS, m_One())) return LHS; - if (implies(RHS, LHS)) + if (isImpliedCondition(RHS, LHS)) return getTrue(ITy); break; case ICmpInst::ICMP_SLT: @@ -2261,7 +2213,7 @@ return LHS; break; case ICmpInst::ICMP_ULE: - if (implies(LHS, RHS)) + if (isImpliedCondition(LHS, RHS)) return getTrue(ITy); break; } Index: lib/Analysis/ValueTracking.cpp =================================================================== --- lib/Analysis/ValueTracking.cpp +++ lib/Analysis/ValueTracking.cpp @@ -3908,3 +3908,43 @@ return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS); } + +bool llvm::isImpliedCondition(Value *LHS, Value *RHS) { + assert(LHS->getType() == RHS->getType() && "mismatched type"); + Type *OpTy = LHS->getType(); + assert(OpTy->getScalarType()->isIntegerTy(1)); + + // LHS ==> RHS by definition + if (LHS == RHS) return true; + + if (OpTy->isVectorTy()) + // TODO: extending the code below to handle vectors + return false; + assert(OpTy->isIntegerTy(1) && "implied by above"); + + ICmpInst::Predicate APred, BPred; + Value *I; + Value *L; + ConstantInt *CI; + // i +_{nsw} C_{>0} i isNegative() && + match(RHS, m_ICmp(BPred, m_Specific(I), m_Specific(L))) && + BPred == ICmpInst::ICMP_SLT) + return true; + + // i +_{nuw} C_{>0} i isNegative() && + match(RHS, m_ICmp(BPred, m_Specific(I), m_Specific(L))) && + BPred == ICmpInst::ICMP_ULT) + return true; + + return false; +}