[llvm] [SCEV] Rename SCEVFlags -> SCEVFlagsPair (NFC) (PR #226935)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 28 03:06:16 PDT 2026
https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/226935
>From 81cb4df64d4b47f612c2b05a34affb7a286e5f72 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Mon, 28 Sep 2026 10:22:23 +0100
Subject: [PATCH 1/2] [SCEV] Rename SCEVFlags -> SCEVFlagsPair (NFC)
Make it clear that it's a pair of no-wrap flags, and free up the
SCEVFlags name for renaming SCEVNoWrapFlags to it.
---
llvm/include/llvm/Analysis/ScalarEvolution.h | 38 +++++++--------
llvm/lib/Analysis/ScalarEvolution.cpp | 50 ++++++++++----------
2 files changed, 41 insertions(+), 47 deletions(-)
diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
index 16739d0a3e5cd..17b667e40312b 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -193,18 +193,14 @@ template <typename SCEVPtrT> SCEVUseT(SCEVPtrT) -> SCEVUseT<SCEVPtrT>;
using SCEVUse = SCEVUseT<const SCEV *>;
/// The no-wrap flags to apply when creating a SCEV expression, to the
-/// expression and use respectively.
-struct SCEVFlags {
- /// Flags applied directly to a SCEV expression, must be valid wherever the
- /// expression is valid.
- SCEVNoWrapFlags ExprFlags;
-
- /// Flags only applied to a SCEVUse.
- SCEVNoWrapFlags UseFlags;
-
- constexpr SCEVFlags(SCEVNoWrapFlags ExprFlags = SCEVNoWrapFlags::FlagNone,
- SCEVNoWrapFlags UseFlags = SCEVNoWrapFlags::FlagNone)
- : ExprFlags(ExprFlags), UseFlags(UseFlags) {}
+/// expression and use: the first component of the pair is applied directly to a
+/// SCEV expression, and the second component is only applied to a SCEVUse.
+struct SCEVFlagsPair : public std::pair<SCEVNoWrapFlags, SCEVNoWrapFlags> {
+ using BaseT = std::pair<SCEVNoWrapFlags, SCEVNoWrapFlags>;
+
+ constexpr SCEVFlagsPair(SCEVNoWrapFlags ExprFlags = SCEVNoWrapFlags::FlagNone,
+ SCEVNoWrapFlags UseFlags = SCEVNoWrapFlags::FlagNone)
+ : BaseT(ExprFlags, UseFlags) {}
};
/// Provide PointerLikeTypeTraits for SCEVUse, so it can be used with
@@ -760,26 +756,26 @@ class ScalarEvolution {
LLVM_ABI const SCEV *getAnyExtendExpr(SCEVUse Op, Type *Ty);
LLVM_ABI SCEVUse getAddExpr(SmallVectorImpl<SCEVUse> &Ops,
- SCEVFlags Flags = {}, unsigned Depth = 0);
- SCEVUse getAddExpr(SCEVUse LHS, SCEVUse RHS, SCEVFlags Flags = {},
+ SCEVFlagsPair Flags = {}, unsigned Depth = 0);
+ SCEVUse getAddExpr(SCEVUse LHS, SCEVUse RHS, SCEVFlagsPair Flags = {},
unsigned Depth = 0) {
SmallVector<SCEVUse, 2> Ops = {LHS, RHS};
return getAddExpr(Ops, Flags, Depth);
}
SCEVUse getAddExpr(SCEVUse Op0, SCEVUse Op1, SCEVUse Op2,
- SCEVFlags Flags = {}, unsigned Depth = 0) {
+ SCEVFlagsPair Flags = {}, unsigned Depth = 0) {
SmallVector<SCEVUse, 3> Ops = {Op0, Op1, Op2};
return getAddExpr(Ops, Flags, Depth);
}
LLVM_ABI SCEVUse getMulExpr(SmallVectorImpl<SCEVUse> &Ops,
- SCEVFlags Flags = {}, unsigned Depth = 0);
- SCEVUse getMulExpr(SCEVUse LHS, SCEVUse RHS, SCEVFlags Flags = {},
+ SCEVFlagsPair Flags = {}, unsigned Depth = 0);
+ SCEVUse getMulExpr(SCEVUse LHS, SCEVUse RHS, SCEVFlagsPair Flags = {},
unsigned Depth = 0) {
SmallVector<SCEVUse, 2> Ops = {LHS, RHS};
return getMulExpr(Ops, Flags, Depth);
}
SCEVUse getMulExpr(SCEVUse Op0, SCEVUse Op1, SCEVUse Op2,
- SCEVFlags Flags = {}, unsigned Depth = 0) {
+ SCEVFlagsPair Flags = {}, unsigned Depth = 0) {
SmallVector<SCEVUse, 3> Ops = {Op0, Op1, Op2};
return getMulExpr(Ops, Flags, Depth);
}
@@ -787,11 +783,11 @@ class ScalarEvolution {
LLVM_ABI const SCEV *getUDivExactExpr(SCEVUse LHS, SCEVUse RHS);
LLVM_ABI const SCEV *getURemExpr(SCEVUse LHS, SCEVUse RHS);
LLVM_ABI SCEVUse getAddRecExpr(SCEVUse Start, SCEVUse Step, const Loop *L,
- SCEVFlags Flags);
+ SCEVFlagsPair Flags);
LLVM_ABI SCEVUse getAddRecExpr(SmallVectorImpl<SCEVUse> &Operands,
- const Loop *L, SCEVFlags Flags);
+ const Loop *L, SCEVFlagsPair Flags);
SCEVUse getAddRecExpr(const SmallVectorImpl<SCEVUse> &Operands, const Loop *L,
- SCEVFlags Flags) {
+ SCEVFlagsPair Flags) {
SmallVector<SCEVUse, 4> NewOp(Operands.begin(), Operands.end());
return getAddRecExpr(NewOp, L, Flags);
}
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index c5e1af4bf060f..2746dc5850771 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -2515,10 +2515,10 @@ bool ScalarEvolution::isAvailableAtLoopEntry(const SCEV *S, const Loop *L) {
/// Get a canonical add expression, or something simpler if possible.
SCEVUse ScalarEvolution::getAddExpr(SmallVectorImpl<SCEVUse> &Ops,
- SCEVFlags Flags, unsigned Depth) {
- SCEV::NoWrapFlags OrigFlags = Flags.ExprFlags;
- SCEV::NoWrapFlags UseFlags = Flags.UseFlags;
- assert(!(OrigFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
+ SCEVFlagsPair Flags, unsigned Depth) {
+ SCEV::NoWrapFlags ExprFlags, UseFlags;
+ std::tie(ExprFlags, UseFlags) = Flags;
+ assert(!(ExprFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
"only nuw or nsw allowed");
assert(!(UseFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
"only nuw or nsw allowed");
@@ -2551,8 +2551,8 @@ SCEVUse ScalarEvolution::getAddExpr(SmallVectorImpl<SCEVUse> &Ops,
unsigned Idx = isa<SCEVConstant>(Ops[0]) ? 1 : 0;
// Delay expensive flag strengthening until necessary.
- auto ComputeFlags = [this, OrigFlags](ArrayRef<SCEVUse> Ops) {
- return StrengthenNoWrapFlags(this, scAddExpr, Ops, OrigFlags);
+ auto ComputeFlags = [this, ExprFlags](ArrayRef<SCEVUse> Ops) {
+ return StrengthenNoWrapFlags(this, scAddExpr, Ops, ExprFlags);
};
// Limit recursion calls depth.
@@ -2562,7 +2562,7 @@ SCEVUse ScalarEvolution::getAddExpr(SmallVectorImpl<SCEVUse> &Ops,
if (SCEV *S = findExistingSCEVInCache(scAddExpr, Ops)) {
// Don't strengthen flags if we have no new information.
SCEVAddExpr *Add = static_cast<SCEVAddExpr *>(S);
- if (Add->getNoWrapFlags(OrigFlags) != OrigFlags)
+ if (Add->getNoWrapFlags(ExprFlags) != ExprFlags)
Add->setNoWrapFlags(ComputeFlags(Ops));
return {S, UseFlags};
}
@@ -2589,7 +2589,7 @@ SCEVUse ScalarEvolution::getAddExpr(SmallVectorImpl<SCEVUse> &Ops,
FoundMatch = true;
}
if (FoundMatch)
- return getAddExpr(Ops, OrigFlags, Depth + 1);
+ return getAddExpr(Ops, ExprFlags, Depth + 1);
// Check for truncates. If all the operands are truncated from the same
// type, see if factoring out the truncate would permit the result to be
@@ -2728,7 +2728,7 @@ SCEVUse ScalarEvolution::getAddExpr(SmallVectorImpl<SCEVUse> &Ops,
// If the original flags and all inlined SCEVAddExprs are NUW, use the
// common NUW flag for expression after inlining. Other flags cannot be
// preserved, because they may depend on the original order of operations.
- SCEV::NoWrapFlags CommonFlags = maskFlags(OrigFlags, SCEV::FlagNUW);
+ SCEV::NoWrapFlags CommonFlags = maskFlags(ExprFlags, SCEV::FlagNUW);
while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
if (Ops.size() > AddOpsInlineThreshold ||
Add->getNumOperands() > AddOpsInlineThreshold)
@@ -3131,10 +3131,10 @@ static bool containsConstantInAddMulChain(const SCEV *StartExpr) {
/// Get a canonical multiply expression, or something simpler if possible.
SCEVUse ScalarEvolution::getMulExpr(SmallVectorImpl<SCEVUse> &Ops,
- SCEVFlags Flags, unsigned Depth) {
- SCEV::NoWrapFlags OrigFlags = Flags.ExprFlags;
- SCEV::NoWrapFlags UseFlags = Flags.UseFlags;
- assert(OrigFlags == maskFlags(OrigFlags, SCEV::FlagNUW | SCEV::FlagNSW) &&
+ SCEVFlagsPair Flags, unsigned Depth) {
+ SCEVNoWrapFlags ExprFlags, UseFlags;
+ std::tie(ExprFlags, UseFlags) = Flags;
+ assert(ExprFlags == maskFlags(ExprFlags, SCEV::FlagNUW | SCEV::FlagNSW) &&
"only nuw or nsw allowed");
assert(UseFlags == maskFlags(UseFlags, SCEV::FlagNUW | SCEV::FlagNSW) &&
"only nuw or nsw allowed");
@@ -3163,8 +3163,8 @@ SCEVUse ScalarEvolution::getMulExpr(SmallVectorImpl<SCEVUse> &Ops,
#endif
// Delay expensive flag strengthening until necessary.
- auto ComputeFlags = [this, OrigFlags](const ArrayRef<SCEVUse> Ops) {
- return StrengthenNoWrapFlags(this, scMulExpr, Ops, OrigFlags);
+ auto ComputeFlags = [this, ExprFlags](const ArrayRef<SCEVUse> Ops) {
+ return StrengthenNoWrapFlags(this, scMulExpr, Ops, ExprFlags);
};
// Limit recursion calls depth.
@@ -3174,7 +3174,7 @@ SCEVUse ScalarEvolution::getMulExpr(SmallVectorImpl<SCEVUse> &Ops,
if (SCEV *S = findExistingSCEVInCache(scMulExpr, Ops)) {
// Don't strengthen flags if we have no new information.
SCEVMulExpr *Mul = static_cast<SCEVMulExpr *>(S);
- if (Mul->getNoWrapFlags(OrigFlags) != OrigFlags)
+ if (Mul->getNoWrapFlags(ExprFlags) != ExprFlags)
Mul->setNoWrapFlags(ComputeFlags(Ops));
return {S, UseFlags};
}
@@ -3683,7 +3683,7 @@ const SCEV *ScalarEvolution::getUDivExactExpr(SCEVUse LHS, SCEVUse RHS) {
/// Get an add recurrence expression for the specified loop. Simplify the
/// expression as much as possible.
SCEVUse ScalarEvolution::getAddRecExpr(SCEVUse Start, SCEVUse Step,
- const Loop *L, SCEVFlags Flags) {
+ const Loop *L, SCEVFlagsPair Flags) {
SmallVector<SCEVUse, 4> Operands;
Operands.push_back(Start);
if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
@@ -3691,8 +3691,7 @@ SCEVUse ScalarEvolution::getAddRecExpr(SCEVUse Start, SCEVUse Step,
append_range(Operands, StepChrec->operands());
// The use flags describe the two-operand recurrence, not the flattened
// one built here, so drop them just like the expression's NUW/NSW.
- return getAddRecExpr(Operands, L,
- maskFlags(Flags.ExprFlags, SCEV::FlagNW));
+ return getAddRecExpr(Operands, L, maskFlags(Flags.first, SCEV::FlagNW));
}
Operands.push_back(Step);
@@ -3702,9 +3701,8 @@ SCEVUse ScalarEvolution::getAddRecExpr(SCEVUse Start, SCEVUse Step,
/// Get an add recurrence expression for the specified loop. Simplify the
/// expression as much as possible.
SCEVUse ScalarEvolution::getAddRecExpr(SmallVectorImpl<SCEVUse> &Operands,
- const Loop *L, SCEVFlags NWFlags) {
- SCEV::NoWrapFlags Flags = NWFlags.ExprFlags;
- SCEV::NoWrapFlags UseFlags = NWFlags.UseFlags;
+ const Loop *L, SCEVFlagsPair NWFlags) {
+ auto [ExprFlags, UseFlags] = NWFlags;
assert(!(UseFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
"only nuw or nsw allowed");
if (Operands.size() == 1) return Operands[0];
@@ -3735,7 +3733,7 @@ SCEVUse ScalarEvolution::getAddRecExpr(SmallVectorImpl<SCEVUse> &Operands,
// meaningful BE count at this point (and if we don't, we'd be stuck
// with a SCEVCouldNotCompute as the cached BE count).
- Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags);
+ ExprFlags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, ExprFlags);
// Canonicalize nested AddRecs in by nesting them in order of loop depth.
if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
@@ -3758,7 +3756,7 @@ SCEVUse ScalarEvolution::getAddRecExpr(SmallVectorImpl<SCEVUse> &Operands,
// The outer recurrence keeps its NW flag but only keeps NUW/NSW if the
// inner recurrence has the same property.
SCEV::NoWrapFlags OuterFlags =
- maskFlags(Flags, SCEV::FlagNW | NestedAR->getNoWrapFlags());
+ maskFlags(ExprFlags, SCEV::FlagNW | NestedAR->getNoWrapFlags());
NestedOperands[0] = getAddRecExpr(Operands, L, OuterFlags);
AllInvariant = all_of(NestedOperands, [&](const SCEV *Op) {
@@ -3771,7 +3769,7 @@ SCEVUse ScalarEvolution::getAddRecExpr(SmallVectorImpl<SCEVUse> &Operands,
// The inner recurrence keeps its NW flag but only keeps NUW/NSW if
// the outer recurrence has the same property.
SCEV::NoWrapFlags InnerFlags =
- maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | Flags);
+ maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | ExprFlags);
return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags);
}
}
@@ -3784,7 +3782,7 @@ SCEVUse ScalarEvolution::getAddRecExpr(SmallVectorImpl<SCEVUse> &Operands,
// already have one, otherwise create a new one.
assert((UseFlags == SCEV::FlagNone || equal(OrigOperands, Operands)) &&
"Tried to add SCEVUse flags after operands changed");
- return {getOrCreateAddRecExpr(Operands, L, Flags), UseFlags};
+ return {getOrCreateAddRecExpr(Operands, L, ExprFlags), UseFlags};
}
const SCEV *ScalarEvolution::getGEPExpr(GEPOperator *GEP,
>From b913d2bc1013f9835fbfc57fc5d2802bb3185556 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Mon, 28 Sep 2026 11:03:48 +0100
Subject: [PATCH 2/2] [SCEV] Keep named members of pair
---
llvm/include/llvm/Analysis/ScalarEvolution.h | 14 +++++++++-----
llvm/lib/Analysis/ScalarEvolution.cpp | 14 ++++++++------
2 files changed, 17 insertions(+), 11 deletions(-)
diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
index 17b667e40312b..28ae51ae850a8 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -193,14 +193,18 @@ template <typename SCEVPtrT> SCEVUseT(SCEVPtrT) -> SCEVUseT<SCEVPtrT>;
using SCEVUse = SCEVUseT<const SCEV *>;
/// The no-wrap flags to apply when creating a SCEV expression, to the
-/// expression and use: the first component of the pair is applied directly to a
-/// SCEV expression, and the second component is only applied to a SCEVUse.
-struct SCEVFlagsPair : public std::pair<SCEVNoWrapFlags, SCEVNoWrapFlags> {
- using BaseT = std::pair<SCEVNoWrapFlags, SCEVNoWrapFlags>;
+/// expression and use respectively.
+struct SCEVFlagsPair {
+ /// Flags applied directly to a SCEV expression, must be valid wherever the
+ /// expression is valid.
+ SCEVNoWrapFlags ExprFlags;
+
+ /// Flags only applied to a SCEVUse.
+ SCEVNoWrapFlags UseFlags;
constexpr SCEVFlagsPair(SCEVNoWrapFlags ExprFlags = SCEVNoWrapFlags::FlagNone,
SCEVNoWrapFlags UseFlags = SCEVNoWrapFlags::FlagNone)
- : BaseT(ExprFlags, UseFlags) {}
+ : ExprFlags(ExprFlags), UseFlags(UseFlags) {}
};
/// Provide PointerLikeTypeTraits for SCEVUse, so it can be used with
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 2746dc5850771..23a6322a665fb 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -2516,8 +2516,8 @@ bool ScalarEvolution::isAvailableAtLoopEntry(const SCEV *S, const Loop *L) {
/// Get a canonical add expression, or something simpler if possible.
SCEVUse ScalarEvolution::getAddExpr(SmallVectorImpl<SCEVUse> &Ops,
SCEVFlagsPair Flags, unsigned Depth) {
- SCEV::NoWrapFlags ExprFlags, UseFlags;
- std::tie(ExprFlags, UseFlags) = Flags;
+ SCEV::NoWrapFlags ExprFlags = Flags.ExprFlags;
+ SCEV::NoWrapFlags UseFlags = Flags.UseFlags;
assert(!(ExprFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
"only nuw or nsw allowed");
assert(!(UseFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
@@ -3132,8 +3132,8 @@ static bool containsConstantInAddMulChain(const SCEV *StartExpr) {
/// Get a canonical multiply expression, or something simpler if possible.
SCEVUse ScalarEvolution::getMulExpr(SmallVectorImpl<SCEVUse> &Ops,
SCEVFlagsPair Flags, unsigned Depth) {
- SCEVNoWrapFlags ExprFlags, UseFlags;
- std::tie(ExprFlags, UseFlags) = Flags;
+ SCEVNoWrapFlags ExprFlags = Flags.ExprFlags;
+ SCEVNoWrapFlags UseFlags = Flags.UseFlags;
assert(ExprFlags == maskFlags(ExprFlags, SCEV::FlagNUW | SCEV::FlagNSW) &&
"only nuw or nsw allowed");
assert(UseFlags == maskFlags(UseFlags, SCEV::FlagNUW | SCEV::FlagNSW) &&
@@ -3691,7 +3691,8 @@ SCEVUse ScalarEvolution::getAddRecExpr(SCEVUse Start, SCEVUse Step,
append_range(Operands, StepChrec->operands());
// The use flags describe the two-operand recurrence, not the flattened
// one built here, so drop them just like the expression's NUW/NSW.
- return getAddRecExpr(Operands, L, maskFlags(Flags.first, SCEV::FlagNW));
+ return getAddRecExpr(Operands, L,
+ maskFlags(Flags.ExprFlags, SCEV::FlagNW));
}
Operands.push_back(Step);
@@ -3702,7 +3703,8 @@ SCEVUse ScalarEvolution::getAddRecExpr(SCEVUse Start, SCEVUse Step,
/// expression as much as possible.
SCEVUse ScalarEvolution::getAddRecExpr(SmallVectorImpl<SCEVUse> &Operands,
const Loop *L, SCEVFlagsPair NWFlags) {
- auto [ExprFlags, UseFlags] = NWFlags;
+ SCEVNoWrapFlags ExprFlags = NWFlags.ExprFlags;
+ SCEVNoWrapFlags UseFlags = NWFlags.UseFlags;
assert(!(UseFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
"only nuw or nsw allowed");
if (Operands.size() == 1) return Operands[0];
More information about the llvm-commits
mailing list