[llvm-branch-commits] [llvm] [SCEV] Convert more interfaces to use SCEVUse (NFC). (PR #185045)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Mar 6 08:57:00 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-analysis
Author: Florian Hahn (fhahn)
<details>
<summary>Changes</summary>
Convert more interfaces to SCEVUse, following up to https://github.com/llvm/llvm-project/pull/91961.
---
Full diff: https://github.com/llvm/llvm-project/pull/185045.diff
3 Files Affected:
- (modified) llvm/include/llvm/Analysis/ScalarEvolution.h (+19-22)
- (modified) llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h (+12)
- (modified) llvm/lib/Analysis/ScalarEvolution.cpp (+23-26)
``````````diff
diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
index c59a652509e27..a871f6f050dfc 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -1198,13 +1198,12 @@ class ScalarEvolution {
/// 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
@@ -1253,6 +1252,7 @@ class ScalarEvolution {
/// 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,
@@ -1346,8 +1346,8 @@ class ScalarEvolution {
/// 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.
@@ -2033,8 +2033,7 @@ class ScalarEvolution {
/// 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
@@ -2114,11 +2113,9 @@ class ScalarEvolution {
/// 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
@@ -2149,8 +2146,8 @@ class ScalarEvolution {
/// 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
@@ -2223,24 +2220,24 @@ class ScalarEvolution {
/// 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.
diff --git a/llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h b/llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h
index 0b47357434782..398dc7da67abc 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h
@@ -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;
}
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 89dfb20bade47..03565e0dd5cd9 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -9318,7 +9318,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.
@@ -9360,7 +9360,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;
+ const SCEV *InnerLHS = LHS;
if (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(LHS))
InnerLHS = ZExt->getOperand();
if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(InnerLHS);
@@ -10872,7 +10872,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;
@@ -10887,8 +10887,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'.
@@ -11210,8 +11210,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);
@@ -11260,8 +11260,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);
@@ -11554,8 +11554,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);
@@ -11581,17 +11581,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;
@@ -11681,8 +11680,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;
@@ -12047,8 +12045,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!");
@@ -12250,8 +12248,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;
@@ -12947,8 +12944,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) ||
@@ -16113,7 +16110,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);
``````````
</details>
https://github.com/llvm/llvm-project/pull/185045
More information about the llvm-branch-commits
mailing list