[llvm] 3b1125b - [SCEV] Add canonical SCEV pointer and construct canonical SCEVs (NFC) (#188858)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 1 04:43:46 PDT 2026
Author: Florian Hahn
Date: 2026-04-01T11:43:40Z
New Revision: 3b1125b1edf997e261ebafdf31b1828f5c458ebb
URL: https://github.com/llvm/llvm-project/commit/3b1125b1edf997e261ebafdf31b1828f5c458ebb
DIFF: https://github.com/llvm/llvm-project/commit/3b1125b1edf997e261ebafdf31b1828f5c458ebb.diff
LOG: [SCEV] Add canonical SCEV pointer and construct canonical SCEVs (NFC) (#188858)
Add a canonical SCEV pointer to SCEV, to support comparing SCEVs for
equality, even with different use-specific flags.
Currently this should be NFC, as nothing yet sets use flags.
Compile-time impact:
https://llvm-compile-time-tracker.com/compare.php?from=13f1fd006243f756417c3ae992342c0674e3f04e&to=e7f46bcf8bef62c619380fbcccbe6073300b69fe&stat=instructions:u
+0.03% - +0.05% on stage1 configs
-0.03% surprisingly for stage2-O3
PR: https://github.com/llvm/llvm-project/pull/188858
Added:
Modified:
llvm/include/llvm/Analysis/ScalarEvolution.h
llvm/lib/Analysis/ScalarEvolution.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
index 9a97ca218b3d3..f20b73f9f358c 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -80,6 +80,13 @@ struct SCEVUse : PointerIntPair<const SCEV *, 2> {
void *getRawPointer() const { return getOpaqueValue(); }
+ /// Returns true of the SCEVUse is canonical, i.e. no SCEVUse flags set in any
+ /// operands.
+ bool isCanonical() const { return getCanonical() == getRawPointer(); }
+
+ /// Return the canonical SCEV for this SCEVUse.
+ const SCEV *getCanonical() const;
+
unsigned getFlags() const { return getInt(); }
bool operator==(const SCEVUse &RHS) const {
@@ -159,6 +166,10 @@ class SCEV : public FoldingSetNode {
/// miscellaneous information.
unsigned short SubclassData = 0;
+ /// Pointer to the canonical version of the SCEV, i.e. one where all operands
+ /// have no SCEVUse flags.
+ const SCEV *CanonicalSCEV = nullptr;
+
public:
/// NoWrapFlags are bitfield indices into SubclassData.
///
@@ -249,6 +260,16 @@ class SCEV : public FoldingSetNode {
/// This method is used for debugging.
LLVM_ABI void dump() const;
+
+ /// Compute and set the canonical SCEV, by constructing a SCEV with the same
+ /// operands, but all SCEVUse flags dropped.
+ LLVM_ABI void computeAndSetCanonical(ScalarEvolution &SE);
+
+ /// Return the canonical SCEV.
+ LLVM_ABI const SCEV *getCanonical() const {
+ assert(CanonicalSCEV && "canonical SCEV not yet computed");
+ return CanonicalSCEV;
+ }
};
// Specialize FoldingSetTrait for SCEV to avoid needing to compute
@@ -271,6 +292,11 @@ inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) {
return OS;
}
+inline raw_ostream &operator<<(raw_ostream &OS, SCEVUse U) {
+ U.print(OS);
+ return OS;
+}
+
/// An object of this class is returned by queries that could not be answered.
/// For example, if you ask for the number of iterations of a linked-list
/// traversal loop, you will get one of these. None of the standard SCEV
@@ -2629,6 +2655,10 @@ template <> struct DenseMapInfo<ScalarEvolution::FoldID> {
}
};
+inline const SCEV *SCEVUse::getCanonical() const {
+ return getPointer()->getCanonical();
+}
+
} // end namespace llvm
#endif // LLVM_ANALYSIS_SCALAREVOLUTION_H
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index d99488121baba..73dc298e66e6f 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -258,6 +258,84 @@ static cl::opt<bool> UseContextForNoWrapFlagInference(
// SCEV class definitions
//===----------------------------------------------------------------------===//
+void SCEV::computeAndSetCanonical(ScalarEvolution &SE) {
+ // Leaf nodes are always their own canonical.
+ switch (getSCEVType()) {
+ case scConstant:
+ case scVScale:
+ case scUnknown:
+ CanonicalSCEV = this;
+ return;
+ default:
+ break;
+ }
+
+ // For all other expressions, check whether any immediate operand has a
+ //
diff erent canonical. Since operands are always created before their parent,
+ // their canonical pointers are already set — no recursion needed.
+ bool Changed = false;
+ SmallVector<SCEVUse, 4> CanonOps;
+ for (SCEVUse Op : operands()) {
+ CanonOps.push_back(Op->getCanonical());
+ Changed |= CanonOps.back() != Op.getPointer();
+ }
+
+ if (!Changed) {
+ CanonicalSCEV = this;
+ return;
+ }
+
+ auto *NAry = dyn_cast<SCEVNAryExpr>(this);
+ SCEV::NoWrapFlags Flags = NAry ? NAry->getNoWrapFlags() : SCEV::FlagAnyWrap;
+ switch (getSCEVType()) {
+ case scPtrToAddr:
+ CanonicalSCEV = SE.getPtrToAddrExpr(CanonOps[0]);
+ return;
+ case scPtrToInt:
+ CanonicalSCEV = SE.getPtrToIntExpr(CanonOps[0], getType());
+ return;
+ case scTruncate:
+ CanonicalSCEV = SE.getTruncateExpr(CanonOps[0], getType());
+ return;
+ case scZeroExtend:
+ CanonicalSCEV = SE.getZeroExtendExpr(CanonOps[0], getType());
+ return;
+ case scSignExtend:
+ CanonicalSCEV = SE.getSignExtendExpr(CanonOps[0], getType());
+ return;
+ case scUDivExpr:
+ CanonicalSCEV = SE.getUDivExpr(CanonOps[0], CanonOps[1]);
+ return;
+ case scAddExpr:
+ CanonicalSCEV = SE.getAddExpr(CanonOps, Flags);
+ return;
+ case scMulExpr:
+ CanonicalSCEV = SE.getMulExpr(CanonOps, Flags);
+ return;
+ case scAddRecExpr:
+ CanonicalSCEV = SE.getAddRecExpr(
+ CanonOps, cast<SCEVAddRecExpr>(this)->getLoop(), Flags);
+ return;
+ case scSMaxExpr:
+ CanonicalSCEV = SE.getSMaxExpr(CanonOps);
+ return;
+ case scUMaxExpr:
+ CanonicalSCEV = SE.getUMaxExpr(CanonOps);
+ return;
+ case scSMinExpr:
+ CanonicalSCEV = SE.getSMinExpr(CanonOps);
+ return;
+ case scUMinExpr:
+ CanonicalSCEV = SE.getUMinExpr(CanonOps);
+ return;
+ case scSequentialUMinExpr:
+ CanonicalSCEV = SE.getUMinExpr(CanonOps, /*Sequential=*/true);
+ return;
+ default:
+ llvm_unreachable("Unknown SCEV type");
+ }
+}
+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void SCEVUse::dump() const {
print(dbgs());
@@ -495,6 +573,7 @@ const SCEV *ScalarEvolution::getConstant(ConstantInt *V) {
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V);
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
return S;
}
@@ -520,6 +599,7 @@ const SCEV *ScalarEvolution::getVScale(Type *Ty) {
return S;
SCEV *S = new (SCEVAllocator) SCEVVScale(ID.Intern(SCEVAllocator), Ty);
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
return S;
}
@@ -1133,6 +1213,7 @@ const SCEV *ScalarEvolution::getLosslessPtrToIntExpr(const SCEV *Op) {
SCEV *S = new (SCEVAllocator)
SCEVPtrToIntExpr(ID.Intern(SCEVAllocator), U, IntPtrTy);
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
registerUser(S, U);
return static_cast<const SCEV *>(S);
});
@@ -1166,6 +1247,7 @@ const SCEV *ScalarEvolution::getPtrToAddrExpr(const SCEV *Op) {
SCEV *S = new (SCEVAllocator)
SCEVPtrToAddrExpr(ID.Intern(SCEVAllocator), U, Ty);
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
registerUser(S, U);
return static_cast<const SCEV *>(S);
});
@@ -1222,6 +1304,7 @@ const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, Type *Ty,
SCEV *S =
new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator), Op, Ty);
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
registerUser(S, Op);
return S;
}
@@ -1275,6 +1358,7 @@ const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, Type *Ty,
SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator),
Op, Ty);
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
registerUser(S, Op);
return S;
}
@@ -1645,6 +1729,7 @@ const SCEV *ScalarEvolution::getZeroExtendExprImpl(const SCEV *Op, Type *Ty,
SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
Op, Ty);
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
registerUser(S, Op);
return S;
}
@@ -1929,6 +2014,7 @@ const SCEV *ScalarEvolution::getZeroExtendExprImpl(const SCEV *Op, Type *Ty,
SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
Op, Ty);
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
registerUser(S, Op);
return S;
}
@@ -1985,6 +2071,7 @@ const SCEV *ScalarEvolution::getSignExtendExprImpl(const SCEV *Op, Type *Ty,
SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
Op, Ty);
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
registerUser(S, Op);
return S;
}
@@ -2191,6 +2278,7 @@ const SCEV *ScalarEvolution::getSignExtendExprImpl(const SCEV *Op, Type *Ty,
SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
Op, Ty);
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
registerUser(S, Op);
return S;
}
@@ -3035,6 +3123,7 @@ const SCEV *ScalarEvolution::getOrCreateAddExpr(ArrayRef<SCEVUse> Ops,
S = new (SCEVAllocator)
SCEVAddExpr(ID.Intern(SCEVAllocator), O, Ops.size());
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
registerUser(S, Ops);
}
S->setNoWrapFlags(Flags);
@@ -3058,6 +3147,7 @@ const SCEV *ScalarEvolution::getOrCreateAddRecExpr(ArrayRef<SCEVUse> Ops,
S = new (SCEVAllocator)
SCEVAddRecExpr(ID.Intern(SCEVAllocator), O, Ops.size(), L);
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
LoopUsers[L].push_back(S);
registerUser(S, Ops);
}
@@ -3080,6 +3170,7 @@ const SCEV *ScalarEvolution::getOrCreateMulExpr(ArrayRef<SCEVUse> Ops,
S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator),
O, Ops.size());
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
registerUser(S, Ops);
}
S->setNoWrapFlags(Flags);
@@ -3647,6 +3738,7 @@ const SCEV *ScalarEvolution::getUDivExpr(SCEVUse LHS, SCEVUse RHS) {
SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator),
LHS, RHS);
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
registerUser(S, ArrayRef<SCEVUse>({LHS, RHS}));
return S;
}
@@ -4047,6 +4139,7 @@ const SCEV *ScalarEvolution::getMinMaxExpr(SCEVTypes Kind,
SCEVMinMaxExpr(ID.Intern(SCEVAllocator), Kind, O, Ops.size());
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
registerUser(S, Ops);
return S;
}
@@ -4437,6 +4530,7 @@ ScalarEvolution::getSequentialMinMaxExpr(SCEVTypes Kind,
SCEVSequentialMinMaxExpr(ID.Intern(SCEVAllocator), Kind, O, Ops.size());
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
registerUser(S, Ops);
return S;
}
@@ -4527,6 +4621,7 @@ const SCEV *ScalarEvolution::getUnknown(Value *V) {
FirstUnknown);
FirstUnknown = cast<SCEVUnknown>(S);
UniqueSCEVs.InsertNode(S, IP);
+ S->computeAndSetCanonical(*this);
return S;
}
More information about the llvm-commits
mailing list