diff --git a/llvm/include/llvm/Analysis/ValueLattice.h b/llvm/include/llvm/Analysis/ValueLattice.h index 1b32fca506976..5ff9c4a6b0805 100644 --- a/llvm/include/llvm/Analysis/ValueLattice.h +++ b/llvm/include/llvm/Analysis/ValueLattice.h @@ -474,9 +474,11 @@ class ValueLatticeElement { const auto &CR = getConstantRange(); const auto &OtherCR = Other.getConstantRange(); - if (CR.icmp(Pred, OtherCR)) + if (ConstantRange::makeSatisfyingICmpRegion(Pred, OtherCR).contains(CR)) return ConstantInt::getTrue(Ty); - if (CR.icmp(CmpInst::getInversePredicate(Pred), OtherCR)) + if (ConstantRange::makeSatisfyingICmpRegion( + CmpInst::getInversePredicate(Pred), OtherCR) + .contains(CR)) return ConstantInt::getFalse(Ty); return nullptr; diff --git a/llvm/include/llvm/IR/ConstantRange.h b/llvm/include/llvm/IR/ConstantRange.h index 44b8c395c89e2..20e8e67436a43 100644 --- a/llvm/include/llvm/IR/ConstantRange.h +++ b/llvm/include/llvm/IR/ConstantRange.h @@ -124,10 +124,6 @@ class LLVM_NODISCARD ConstantRange { static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other); - /// Does the predicate \p Pred hold between ranges this and \p Other? - /// NOTE: false does not mean that inverse predicate holds! - bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const; - /// Produce the largest range containing all X such that "X BinOp Y" is /// guaranteed not to wrap (overflow) for *all* Y in Other. However, there may /// be *some* Y in Other for which additional X not contained in the result diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index b233a0f3eb2d0..a6d3ca64189d1 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -3451,10 +3451,13 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, auto LHS_CR = getConstantRangeFromMetadata( *LHS_Instr->getMetadata(LLVMContext::MD_range)); - if (LHS_CR.icmp(Pred, RHS_CR)) + auto Satisfied_CR = ConstantRange::makeSatisfyingICmpRegion(Pred, RHS_CR); + if (Satisfied_CR.contains(LHS_CR)) return ConstantInt::getTrue(RHS->getContext()); - if (LHS_CR.icmp(CmpInst::getInversePredicate(Pred), RHS_CR)) + auto InversedSatisfied_CR = ConstantRange::makeSatisfyingICmpRegion( + CmpInst::getInversePredicate(Pred), RHS_CR); + if (InversedSatisfied_CR.contains(LHS_CR)) return ConstantInt::getFalse(RHS->getContext()); } } diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index 4630c5562623c..a481c23c3d05c 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -9843,9 +9843,10 @@ bool ScalarEvolution::isKnownPredicateViaConstantRanges( // This code is split out from isKnownPredicate because it is called from // within isLoopEntryGuardedByCond. - auto CheckRanges = [&](const ConstantRange &RangeLHS, - const ConstantRange &RangeRHS) { - return RangeLHS.icmp(Pred, RangeRHS); + auto CheckRanges = + [&](const ConstantRange &RangeLHS, const ConstantRange &RangeRHS) { + return ConstantRange::makeSatisfyingICmpRegion(Pred, RangeRHS) + .contains(RangeLHS); }; // The check at the top of the function catches the case where the values are @@ -11147,9 +11148,12 @@ bool ScalarEvolution::isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred, // We can also compute the range of values for `LHS` that satisfy the // consequent, "`LHS` `Pred` `RHS`": const APInt &ConstRHS = cast(RHS)->getAPInt(); + ConstantRange SatisfyingLHSRange = + ConstantRange::makeSatisfyingICmpRegion(Pred, ConstRHS); + // The antecedent implies the consequent if every value of `LHS` that // satisfies the antecedent also satisfies the consequent. - return LHSRange.icmp(Pred, ConstRHS); + return SatisfyingLHSRange.contains(LHSRange); } bool ScalarEvolution::doesIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride, diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp index b38599fa7d982..4dbe1a1b902d4 100644 --- a/llvm/lib/IR/ConstantRange.cpp +++ b/llvm/lib/IR/ConstantRange.cpp @@ -181,11 +181,6 @@ bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred, return Success; } -bool ConstantRange::icmp(CmpInst::Predicate Pred, - const ConstantRange &Other) const { - return makeSatisfyingICmpRegion(Pred, Other).contains(*this); -} - /// Exact mul nuw region for single element RHS. static ConstantRange makeExactMulNUWRegion(const APInt &V) { unsigned BitWidth = V.getBitWidth(); diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp index 33ed2b4423a8a..867dd3118cea0 100644 --- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp +++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp @@ -7328,10 +7328,13 @@ struct AAValueConstantRangeFloating : AAValueConstantRangeImpl { auto AllowedRegion = ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange); + auto SatisfyingRegion = ConstantRange::makeSatisfyingICmpRegion( + CmpI->getPredicate(), RHSAARange); + if (AllowedRegion.intersectWith(LHSAARange).isEmptySet()) MustFalse = true; - if (LHSAARange.icmp(CmpI->getPredicate(), RHSAARange)) + if (SatisfyingRegion.contains(LHSAARange)) MustTrue = true; assert((!MustTrue || !MustFalse) && diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp index f8816e4d43df4..12362b9460f9c 100644 --- a/llvm/unittests/IR/ConstantRangeTest.cpp +++ b/llvm/unittests/IR/ConstantRangeTest.cpp @@ -6,10 +6,9 @@ // //===----------------------------------------------------------------------===// -#include "llvm/IR/ConstantRange.h" #include "llvm/ADT/BitVector.h" -#include "llvm/ADT/Sequence.h" #include "llvm/ADT/SmallBitVector.h" +#include "llvm/IR/ConstantRange.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Operator.h" #include "llvm/Support/KnownBits.h" @@ -1510,52 +1509,6 @@ TEST(ConstantRange, MakeSatisfyingICmpRegion) { ConstantRange(APInt(8, 4), APInt(8, -128))); } -static bool icmp(CmpInst::Predicate Pred, const APInt &LHS, const APInt &RHS) { - switch (Pred) { - case CmpInst::Predicate::ICMP_EQ: - return LHS.eq(RHS); - case CmpInst::Predicate::ICMP_NE: - return LHS.ne(RHS); - case CmpInst::Predicate::ICMP_UGT: - return LHS.ugt(RHS); - case CmpInst::Predicate::ICMP_UGE: - return LHS.uge(RHS); - case CmpInst::Predicate::ICMP_ULT: - return LHS.ult(RHS); - case CmpInst::Predicate::ICMP_ULE: - return LHS.ule(RHS); - case CmpInst::Predicate::ICMP_SGT: - return LHS.sgt(RHS); - case CmpInst::Predicate::ICMP_SGE: - return LHS.sge(RHS); - case CmpInst::Predicate::ICMP_SLT: - return LHS.slt(RHS); - case CmpInst::Predicate::ICMP_SLE: - return LHS.sle(RHS); - default: - llvm_unreachable("Not an ICmp predicate!"); - } -} - -void ICmpTestImpl(CmpInst::Predicate Pred) { - unsigned Bits = 4; - EnumerateTwoConstantRanges( - Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) { - bool Exhaustive = true; - ForeachNumInConstantRange(CR1, [&](const APInt &N1) { - ForeachNumInConstantRange( - CR2, [&](const APInt &N2) { Exhaustive &= icmp(Pred, N1, N2); }); - }); - EXPECT_EQ(CR1.icmp(Pred, CR2), Exhaustive); - }); -} - -TEST(ConstantRange, ICmp) { - for (auto Pred : seq(CmpInst::Predicate::FIRST_ICMP_PREDICATE, - 1 + CmpInst::Predicate::LAST_ICMP_PREDICATE)) - ICmpTestImpl((CmpInst::Predicate)Pred); -} - TEST(ConstantRange, MakeGuaranteedNoWrapRegion) { const int IntMin4Bits = 8; const int IntMax4Bits = 7;