[SCEV] Convert more interfaces to use SCEVUse (NFC). (#185045)

Convert more interfaces to SCEVUse, following up to
https://github.com/llvm/llvm-project/pull/91961.

PR: https://github.com/llvm/llvm-project/pull/185045
This commit is contained in:
Florian Hahn 2026-03-19 17:02:48 +00:00 committed by GitHub
parent 65f6a346a9
commit dfafee7a47
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 74 additions and 68 deletions

View file

@ -1200,13 +1200,12 @@ public:
/// so we can assert on that.
/// e. Return true if isLoopEntryGuardedByCond(Pred, E(LHS), E(RHS)) &&
/// isLoopBackedgeGuardedByCond(Pred, B(LHS), B(RHS))
LLVM_ABI bool isKnownViaInduction(CmpPredicate Pred, const SCEV *LHS,
const SCEV *RHS);
LLVM_ABI bool isKnownViaInduction(CmpPredicate Pred, SCEVUse LHS,
SCEVUse RHS);
/// Test if the given expression is known to satisfy the condition described
/// by Pred, LHS, and RHS.
LLVM_ABI bool isKnownPredicate(CmpPredicate Pred, const SCEV *LHS,
const SCEV *RHS);
LLVM_ABI bool isKnownPredicate(CmpPredicate Pred, SCEVUse LHS, SCEVUse RHS);
/// Check whether the condition described by Pred, LHS, and RHS is true or
/// false. If we know it, return the evaluation of this condition. If neither
@ -1255,6 +1254,7 @@ public:
/// one from a SCEVCouldNotCompute. No other types of SCEVs are allowed
/// as arguments and asserts enforce that internally.
/*implicit*/ LLVM_ABI ExitLimit(const SCEV *E);
/*implicit*/ ExitLimit(SCEVUse E) : ExitLimit((const SCEV *)E) {}
LLVM_ABI
ExitLimit(const SCEV *E, const SCEV *ConstantMaxNotTaken,
@ -1348,8 +1348,8 @@ public:
/// iff any changes were made. If the operands are provably equal or
/// unequal, LHS and RHS are set to the same value and Pred is set to either
/// ICMP_EQ or ICMP_NE.
LLVM_ABI bool SimplifyICmpOperands(CmpPredicate &Pred, const SCEV *&LHS,
const SCEV *&RHS, unsigned Depth = 0);
LLVM_ABI bool SimplifyICmpOperands(CmpPredicate &Pred, SCEVUse &LHS,
SCEVUse &RHS, unsigned Depth = 0);
/// Return the "disposition" of the given SCEV with respect to the given
/// loop.
@ -2029,8 +2029,7 @@ private:
/// return more precise results in some cases and is preferred when caller
/// has a materialized ICmp.
ExitLimit computeExitLimitFromICmp(const Loop *L, CmpPredicate Pred,
const SCEV *LHS, const SCEV *RHS,
bool IsSubExpr,
SCEVUse LHS, SCEVUse RHS, bool IsSubExpr,
bool AllowPredicates = false);
/// Compute the number of times the backedge of the specified loop will
@ -2110,11 +2109,9 @@ private:
/// whenever the given FoundCondValue value evaluates to true in given
/// Context. If Context is nullptr, then the found predicate is true
/// everywhere. LHS and FoundLHS must have same type width.
LLVM_ABI bool isImpliedCondBalancedTypes(CmpPredicate Pred, const SCEV *LHS,
const SCEV *RHS,
CmpPredicate FoundPred,
const SCEV *FoundLHS,
const SCEV *FoundRHS,
LLVM_ABI bool isImpliedCondBalancedTypes(CmpPredicate Pred, SCEVUse LHS,
SCEVUse RHS, CmpPredicate FoundPred,
SCEVUse FoundLHS, SCEVUse FoundRHS,
const Instruction *CtxI);
/// Test whether the condition described by Pred, LHS, and RHS is true
@ -2145,8 +2142,8 @@ private:
/// Test whether the condition described by Pred, LHS, and RHS is true.
/// Use only simple non-recursive types of checks, such as range analysis etc.
bool isKnownViaNonRecursiveReasoning(CmpPredicate Pred, const SCEV *LHS,
const SCEV *RHS);
bool isKnownViaNonRecursiveReasoning(CmpPredicate Pred, SCEVUse LHS,
SCEVUse RHS);
/// Test whether the condition described by Pred, LHS, and RHS is true
/// whenever the condition described by Pred, FoundLHS, and FoundRHS is
@ -2219,24 +2216,24 @@ private:
/// Test if the given expression is known to satisfy the condition described
/// by Pred and the known constant ranges of LHS and RHS.
bool isKnownPredicateViaConstantRanges(CmpPredicate Pred, const SCEV *LHS,
const SCEV *RHS);
bool isKnownPredicateViaConstantRanges(CmpPredicate Pred, SCEVUse LHS,
SCEVUse RHS);
/// Try to prove the condition described by "LHS Pred RHS" by ruling out
/// integer overflow.
///
/// For instance, this will return true for "A s< (A + C)<nsw>" if C is
/// positive.
bool isKnownPredicateViaNoOverflow(CmpPredicate Pred, const SCEV *LHS,
const SCEV *RHS);
bool isKnownPredicateViaNoOverflow(CmpPredicate Pred, SCEVUse LHS,
SCEVUse RHS);
/// Try to split Pred LHS RHS into logical conjunctions (and's) and try to
/// prove them individually.
bool isKnownPredicateViaSplitting(CmpPredicate Pred, const SCEV *LHS,
const SCEV *RHS);
bool isKnownPredicateViaSplitting(CmpPredicate Pred, SCEVUse LHS,
SCEVUse RHS);
/// Try to match the Expr as "(L + R)<Flags>".
bool splitBinaryAdd(const SCEV *Expr, const SCEV *&L, const SCEV *&R,
bool splitBinaryAdd(SCEVUse Expr, SCEVUse &L, SCEVUse &R,
SCEV::NoWrapFlags &Flags);
/// Forget predicated/non-predicated backedge taken counts for the given loop.

View file

@ -88,8 +88,20 @@ template <typename Class> struct bind_ty {
}
};
template <> struct bind_ty<SCEVUse> {
SCEVUse &VR;
bind_ty(SCEVUse &V) : VR(V) {}
template <typename ITy> bool match(ITy *V) const {
VR = V;
return true;
}
};
/// Match a SCEV, capturing it if we match.
inline bind_ty<const SCEV> m_SCEV(const SCEV *&V) { return V; }
inline bind_ty<SCEVUse> m_SCEV(SCEVUse &V) { return V; }
inline bind_ty<const SCEVConstant> m_SCEVConstant(const SCEVConstant *&V) {
return V;
}

View file

@ -9391,7 +9391,7 @@ ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
ExitCond->getOperand(1), L, OriginalPred);
}
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
const Loop *L, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS,
const Loop *L, CmpPredicate Pred, SCEVUse LHS, SCEVUse RHS,
bool ControlsOnlyExit, bool AllowPredicates) {
// Try to evaluate any dependencies out of the loop.
@ -9433,7 +9433,7 @@ ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
// because if it did, we'd have an infinite (undefined) loop.
// TODO: We can peel off any functions which are invertible *in L*. Loop
// invariant terms are effectively constants for our purposes here.
auto *InnerLHS = LHS;
SCEVUse InnerLHS = LHS;
if (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(LHS))
InnerLHS = ZExt->getOperand();
if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(InnerLHS);
@ -10945,7 +10945,7 @@ static bool HasSameValue(const SCEV *A, const SCEV *B) {
return false;
}
static bool MatchBinarySub(const SCEV *S, const SCEV *&LHS, const SCEV *&RHS) {
static bool MatchBinarySub(const SCEV *S, SCEVUse &LHS, SCEVUse &RHS) {
const SCEV *Op0, *Op1;
if (!match(S, m_scev_Add(m_SCEV(Op0), m_SCEV(Op1))))
return false;
@ -10960,8 +10960,8 @@ static bool MatchBinarySub(const SCEV *S, const SCEV *&LHS, const SCEV *&RHS) {
return false;
}
bool ScalarEvolution::SimplifyICmpOperands(CmpPredicate &Pred, const SCEV *&LHS,
const SCEV *&RHS, unsigned Depth) {
bool ScalarEvolution::SimplifyICmpOperands(CmpPredicate &Pred, SCEVUse &LHS,
SCEVUse &RHS, unsigned Depth) {
bool Changed = false;
// Simplifies ICMP to trivial true or false by turning it into '0 == 0' or
// '0 != 0'.
@ -11283,8 +11283,8 @@ ScalarEvolution::SplitIntoInitAndPostInc(const Loop *L, const SCEV *S) {
return { Start, PostInc };
}
bool ScalarEvolution::isKnownViaInduction(CmpPredicate Pred, const SCEV *LHS,
const SCEV *RHS) {
bool ScalarEvolution::isKnownViaInduction(CmpPredicate Pred, SCEVUse LHS,
SCEVUse RHS) {
// First collect all loops.
SmallPtrSet<const Loop *, 8> LoopsUsed;
getUsedLoops(LHS, LoopsUsed);
@ -11333,8 +11333,8 @@ bool ScalarEvolution::isKnownViaInduction(CmpPredicate Pred, const SCEV *LHS,
isLoopEntryGuardedByCond(MDL, Pred, SplitLHS.first, SplitRHS.first);
}
bool ScalarEvolution::isKnownPredicate(CmpPredicate Pred, const SCEV *LHS,
const SCEV *RHS) {
bool ScalarEvolution::isKnownPredicate(CmpPredicate Pred, SCEVUse LHS,
SCEVUse RHS) {
// Canonicalize the inputs first.
(void)SimplifyICmpOperands(Pred, LHS, RHS);
@ -11627,8 +11627,8 @@ ScalarEvolution::getLoopInvariantExitCondDuringFirstIterationsImpl(
}
bool ScalarEvolution::isKnownPredicateViaConstantRanges(CmpPredicate Pred,
const SCEV *LHS,
const SCEV *RHS) {
SCEVUse LHS,
SCEVUse RHS) {
if (HasSameValue(LHS, RHS))
return ICmpInst::isTrueWhenEqual(Pred);
@ -11654,17 +11654,16 @@ bool ScalarEvolution::isKnownPredicateViaConstantRanges(CmpPredicate Pred,
}
bool ScalarEvolution::isKnownPredicateViaNoOverflow(CmpPredicate Pred,
const SCEV *LHS,
const SCEV *RHS) {
SCEVUse LHS, SCEVUse RHS) {
// Match X to (A + C1)<ExpectedFlags> and Y to (A + C2)<ExpectedFlags>, where
// C1 and C2 are constant integers. If either X or Y are not add expressions,
// consider them as X + 0 and Y + 0 respectively. C1 and C2 are returned via
// OutC1 and OutC2.
auto MatchBinaryAddToConst = [this](const SCEV *X, const SCEV *Y,
APInt &OutC1, APInt &OutC2,
auto MatchBinaryAddToConst = [this](SCEVUse X, SCEVUse Y, APInt &OutC1,
APInt &OutC2,
SCEV::NoWrapFlags ExpectedFlags) {
const SCEV *XNonConstOp, *XConstOp;
const SCEV *YNonConstOp, *YConstOp;
SCEVUse XNonConstOp, XConstOp;
SCEVUse YNonConstOp, YConstOp;
SCEV::NoWrapFlags XFlagsPresent;
SCEV::NoWrapFlags YFlagsPresent;
@ -11754,8 +11753,7 @@ bool ScalarEvolution::isKnownPredicateViaNoOverflow(CmpPredicate Pred,
}
bool ScalarEvolution::isKnownPredicateViaSplitting(CmpPredicate Pred,
const SCEV *LHS,
const SCEV *RHS) {
SCEVUse LHS, SCEVUse RHS) {
if (Pred != ICmpInst::ICMP_ULT || ProvingSplitPredicate)
return false;
@ -12112,8 +12110,8 @@ bool ScalarEvolution::isImpliedCond(CmpPredicate Pred, const SCEV *LHS,
}
bool ScalarEvolution::isImpliedCondBalancedTypes(
CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, CmpPredicate FoundPred,
const SCEV *FoundLHS, const SCEV *FoundRHS, const Instruction *CtxI) {
CmpPredicate Pred, SCEVUse LHS, SCEVUse RHS, CmpPredicate FoundPred,
SCEVUse FoundLHS, SCEVUse FoundRHS, const Instruction *CtxI) {
assert(getTypeSizeInBits(LHS->getType()) ==
getTypeSizeInBits(FoundLHS->getType()) &&
"Types should be balanced!");
@ -12315,8 +12313,7 @@ bool ScalarEvolution::isImpliedCondBalancedTypes(
return false;
}
bool ScalarEvolution::splitBinaryAdd(const SCEV *Expr,
const SCEV *&L, const SCEV *&R,
bool ScalarEvolution::splitBinaryAdd(SCEVUse Expr, SCEVUse &L, SCEVUse &R,
SCEV::NoWrapFlags &Flags) {
if (!match(Expr, m_scev_Add(m_SCEV(L), m_SCEV(R))))
return false;
@ -13014,8 +13011,8 @@ static bool isKnownPredicateExtendIdiom(CmpPredicate Pred, const SCEV *LHS,
}
bool ScalarEvolution::isKnownViaNonRecursiveReasoning(CmpPredicate Pred,
const SCEV *LHS,
const SCEV *RHS) {
SCEVUse LHS,
SCEVUse RHS) {
return isKnownPredicateExtendIdiom(Pred, LHS, RHS) ||
isKnownPredicateViaConstantRanges(Pred, LHS, RHS) ||
IsKnownPredicateViaMinOrMax(*this, Pred, LHS, RHS) ||
@ -16178,7 +16175,7 @@ const SCEV *ScalarEvolution::LoopGuards::rewrite(const SCEV *Expr) const {
// Helper to check if S is a subtraction (A - B) where A != B, and if so,
// return UMax(S, 1).
auto RewriteSubtraction = [&](const SCEV *S) -> const SCEV * {
const SCEV *LHS, *RHS;
SCEVUse LHS, RHS;
if (MatchBinarySub(S, LHS, RHS)) {
if (LHS > RHS)
std::swap(LHS, RHS);

View file

@ -1789,8 +1789,8 @@ TEST_F(ScalarEvolutionsTest, SimplifyICmpOperands) {
{
CmpPredicate NewPred = ICmpInst::ICMP_SLT;
const SCEV *NewLHS = VSxA;
const SCEV *NewRHS = VSxB;
SCEVUse NewLHS = VSxA;
SCEVUse NewRHS = VSxB;
EXPECT_TRUE(SE.SimplifyICmpOperands(NewPred, NewLHS, NewRHS));
EXPECT_EQ(NewPred, ICmpInst::ICMP_SLT);
EXPECT_EQ(NewLHS, A);
@ -1799,8 +1799,8 @@ TEST_F(ScalarEvolutionsTest, SimplifyICmpOperands) {
{
CmpPredicate NewPred = ICmpInst::ICMP_ULT;
const SCEV *NewLHS = VSxA;
const SCEV *NewRHS = VSxB;
SCEVUse NewLHS = VSxA;
SCEVUse NewRHS = VSxB;
EXPECT_TRUE(SE.SimplifyICmpOperands(NewPred, NewLHS, NewRHS));
EXPECT_EQ(NewPred, ICmpInst::ICMP_ULT);
EXPECT_EQ(NewLHS, A);
@ -1809,8 +1809,8 @@ TEST_F(ScalarEvolutionsTest, SimplifyICmpOperands) {
{
CmpPredicate NewPred = ICmpInst::ICMP_EQ;
const SCEV *NewLHS = VSxA;
const SCEV *NewRHS = VSxB;
SCEVUse NewLHS = VSxA;
SCEVUse NewRHS = VSxB;
EXPECT_TRUE(SE.SimplifyICmpOperands(NewPred, NewLHS, NewRHS));
EXPECT_EQ(NewPred, ICmpInst::ICMP_EQ);
EXPECT_EQ(NewLHS, A);
@ -1827,8 +1827,8 @@ TEST_F(ScalarEvolutionsTest, SimplifyICmpOperands) {
isa<SCEVVScale>(cast<SCEVMulExpr>(CxVS)->getOperand(0)));
CmpPredicate NewPred = ICmpInst::ICMP_SLT;
const SCEV *NewLHS = VSxA;
const SCEV *NewRHS = CxVS;
SCEVUse NewLHS = VSxA;
SCEVUse NewRHS = CxVS;
EXPECT_TRUE(SE.SimplifyICmpOperands(NewPred, NewLHS, NewRHS));
EXPECT_EQ(NewPred, ICmpInst::ICMP_SLT);
EXPECT_EQ(NewLHS, A);
@ -1846,15 +1846,15 @@ TEST_F(ScalarEvolutionsTest, SimplifyICmpOperands) {
{
CmpPredicate NewPred = ICmpInst::ICMP_SLT;
const SCEV *NewLHS = VSxA;
const SCEV *NewRHS = VSxB;
SCEVUse NewLHS = VSxA;
SCEVUse NewRHS = VSxB;
EXPECT_FALSE(SE.SimplifyICmpOperands(NewPred, NewLHS, NewRHS));
}
{
CmpPredicate NewPred = ICmpInst::ICMP_ULT;
const SCEV *NewLHS = VSxA;
const SCEV *NewRHS = VSxB;
SCEVUse NewLHS = VSxA;
SCEVUse NewRHS = VSxB;
EXPECT_TRUE(SE.SimplifyICmpOperands(NewPred, NewLHS, NewRHS));
EXPECT_EQ(NewPred, ICmpInst::ICMP_ULT);
EXPECT_EQ(NewLHS, A);
@ -1863,8 +1863,8 @@ TEST_F(ScalarEvolutionsTest, SimplifyICmpOperands) {
{
CmpPredicate NewPred = ICmpInst::ICMP_EQ;
const SCEV *NewLHS = VSxA;
const SCEV *NewRHS = VSxB;
SCEVUse NewLHS = VSxA;
SCEVUse NewRHS = VSxB;
EXPECT_TRUE(SE.SimplifyICmpOperands(NewPred, NewLHS, NewRHS));
EXPECT_EQ(NewPred, ICmpInst::ICMP_EQ);
EXPECT_EQ(NewLHS, A);
@ -1882,22 +1882,22 @@ TEST_F(ScalarEvolutionsTest, SimplifyICmpOperands) {
{
CmpPredicate NewPred = ICmpInst::ICMP_SLT;
const SCEV *NewLHS = VSxA;
const SCEV *NewRHS = VSxB;
SCEVUse NewLHS = VSxA;
SCEVUse NewRHS = VSxB;
EXPECT_FALSE(SE.SimplifyICmpOperands(NewPred, NewLHS, NewRHS));
}
{
CmpPredicate NewPred = ICmpInst::ICMP_ULT;
const SCEV *NewLHS = VSxA;
const SCEV *NewRHS = VSxB;
SCEVUse NewLHS = VSxA;
SCEVUse NewRHS = VSxB;
EXPECT_FALSE(SE.SimplifyICmpOperands(NewPred, NewLHS, NewRHS));
}
{
CmpPredicate NewPred = ICmpInst::ICMP_EQ;
const SCEV *NewLHS = VSxA;
const SCEV *NewRHS = VSxB;
SCEVUse NewLHS = VSxA;
SCEVUse NewRHS = VSxB;
EXPECT_FALSE(SE.SimplifyICmpOperands(NewPred, NewLHS, NewRHS));
}
});