Skip to content

Commit f3905cd

Browse files
committed
[SCEV] Introduce SCEVUse, use it instead of const SCEV * (NFCI) (WIP).
This patch introduces SCEVUse, which is a tagged pointer containing the used const SCEV *, plus extra bits to store NUW/NSW flags that are only valid at the specific use. This was suggested by @nikic as an alternative to #90742. This patch just updates most SCEV infrastructure to operate on SCEVUse instead of const SCEV *. It does not introduce any code that makes use of the use-specific flags yet which I'll share as follow-ups. Note that this should be NFC, but currently there's at least one case where it is not (turn-to-invariant.ll), which I'll investigate once we agree on the overall direction. This PR at the moment also contains a commit that updates various SCEV clients to use `const SCEV *` instead of `const auto *`, to prepare for this patch. This reduces the number of changes needed, as SCEVUse will automatically convert to `const SCEV *`. This is a safe default, as it just drops the use-specific flags for the expression (it will not drop any use-specific flags for any of its operands though). This probably SCEVUse could probably also be used to address mis-compiles due to equivalent AddRecs modulo flags result in an AddRec with incorrect flags for some uses of some phis, e.g. the one #80430 attempted to fix Compile-time impact: stage1-O3: +0.06% stage1-ReleaseThinLTO: +0.07% stage1-ReleaseLTO-g: +0.07% stage2-O3: +0.11% https://llvm-compile-time-tracker.com/compare.php?from=ce055843e2be9643bd58764783a7bb69f6db8c9a&to=8c7f4e9e154ebc4862c4e2716cedc3c688352d7c&stat=instructions:u !fixup use raw pointer (const SCEV * + lower bits) for AddPointer. !fix formatting !fixup fix build failures after rebase, address comments !fixup isCanonical/getCanonical Simp
1 parent 4e828f8 commit f3905cd

File tree

13 files changed

+2018
-1724
lines changed

13 files changed

+2018
-1724
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 486 additions & 363 deletions
Large diffs are not rendered by default.

llvm/include/llvm/Analysis/ScalarEvolutionExpressions.h

Lines changed: 154 additions & 133 deletions
Large diffs are not rendered by default.

llvm/lib/Analysis/DependenceAnalysis.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,10 +1250,12 @@ bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
12501250
if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
12511251
LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound);
12521252
LLVM_DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n");
1253-
const SCEV *AbsDelta =
1254-
SE->isKnownNonNegative(Delta) ? Delta : SE->getNegativeSCEV(Delta);
1255-
const SCEV *AbsCoeff =
1256-
SE->isKnownNonNegative(Coeff) ? Coeff : SE->getNegativeSCEV(Coeff);
1253+
const SCEV *AbsDelta = SE->isKnownNonNegative(Delta)
1254+
? Delta
1255+
: SE->getNegativeSCEV(Delta).getPointer();
1256+
const SCEV *AbsCoeff = SE->isKnownNonNegative(Coeff)
1257+
? Coeff
1258+
: SE->getNegativeSCEV(Coeff).getPointer();
12571259
const SCEV *Product = SE->getMulExpr(UpperBound, AbsCoeff);
12581260
if (isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product)) {
12591261
// Distance greater than trip count - no dependence
@@ -1791,8 +1793,9 @@ bool DependenceInfo::weakZeroSrcSIVtest(const SCEV *DstCoeff,
17911793
const SCEV *AbsCoeff =
17921794
SE->isKnownNegative(ConstCoeff) ?
17931795
SE->getNegativeSCEV(ConstCoeff) : ConstCoeff;
1794-
const SCEV *NewDelta =
1795-
SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1796+
const SCEV *NewDelta = SE->isKnownNegative(ConstCoeff)
1797+
? SE->getNegativeSCEV(Delta).getPointer()
1798+
: Delta;
17961799

17971800
// check that Delta/SrcCoeff < iteration count
17981801
// really check NewDelta < count*AbsCoeff
@@ -1900,8 +1903,9 @@ bool DependenceInfo::weakZeroDstSIVtest(const SCEV *SrcCoeff,
19001903
const SCEV *AbsCoeff =
19011904
SE->isKnownNegative(ConstCoeff) ?
19021905
SE->getNegativeSCEV(ConstCoeff) : ConstCoeff;
1903-
const SCEV *NewDelta =
1904-
SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1906+
const SCEV *NewDelta = SE->isKnownNegative(ConstCoeff)
1907+
? SE->getNegativeSCEV(Delta).getPointer()
1908+
: Delta;
19051909

19061910
// check that Delta/SrcCoeff < iteration count
19071911
// really check NewDelta < count*AbsCoeff

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1520,7 +1520,7 @@ bool InductionDescriptor::isInductionPHI(
15201520
return false;
15211521

15221522
// Check that the PHI is consecutive.
1523-
const SCEV *PhiScev = Expr ? Expr : SE->getSCEV(Phi);
1523+
const SCEV *PhiScev = Expr ? Expr : SE->getSCEV(Phi).getPointer();
15241524
const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
15251525

15261526
if (!AR) {

llvm/lib/Analysis/LoopCacheAnalysis.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,8 @@ bool IndexedReference::isConsecutive(const Loop &L, const SCEV *&Stride,
500500
SE.getNoopOrSignExtend(ElemSize, WiderType));
501501
const SCEV *CacheLineSize = SE.getConstant(Stride->getType(), CLS);
502502

503-
Stride = SE.isKnownNegative(Stride) ? SE.getNegativeSCEV(Stride) : Stride;
503+
Stride = SE.isKnownNegative(Stride) ? SE.getNegativeSCEV(Stride).getPointer()
504+
: Stride;
504505
return SE.isKnownPredicate(ICmpInst::ICMP_ULT, Stride, CacheLineSize);
505506
}
506507

0 commit comments

Comments
 (0)