Skip to content

Commit ae119a4

Browse files
committed
WIP boneheaded loop guard cache
1 parent aff98e4 commit ae119a4

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,8 @@ class ScalarEvolution {
16511651
/// function as they are computed.
16521652
DenseMap<const Loop *, BackedgeTakenInfo> PredicatedBackedgeTakenCounts;
16531653

1654+
DenseMap<const Loop *, LoopGuards> CachedLoopGuards;
1655+
16541656
/// Loops whose backedge taken counts directly use this non-constant SCEV.
16551657
DenseMap<const SCEV *, SmallPtrSet<PointerIntPair<const Loop *, 1, bool>, 4>>
16561658
BECountUsers;

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8417,6 +8417,7 @@ void ScalarEvolution::forgetAllLoops() {
84178417
// result.
84188418
BackedgeTakenCounts.clear();
84198419
PredicatedBackedgeTakenCounts.clear();
8420+
CachedLoopGuards.clear();
84208421
BECountUsers.clear();
84218422
LoopPropertiesCache.clear();
84228423
ConstantEvolutionLoopExitValue.clear();
@@ -10551,9 +10552,9 @@ ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
1055110552
if (!isLoopInvariant(Step, L))
1055210553
return getCouldNotCompute();
1055310554

10554-
LoopGuards Guards = LoopGuards::collect(L, *this);
10555+
// LoopGuards Guards = LoopGuards::collect(L, *this);
1055510556
// Specialize step for this loop so we get context sensitive facts below.
10556-
const SCEV *StepWLG = applyLoopGuards(Step, Guards);
10557+
const SCEV *StepWLG = applyLoopGuards(Step, L);
1055710558

1055810559
// For positive steps (counting up until unsigned overflow):
1055910560
// N = -Start/Step (as unsigned)
@@ -10570,7 +10571,7 @@ ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
1057010571
// N = Distance (as unsigned)
1057110572
if (StepC &&
1057210573
(StepC->getValue()->isOne() || StepC->getValue()->isMinusOne())) {
10573-
APInt MaxBECount = getUnsignedRangeMax(applyLoopGuards(Distance, Guards));
10574+
APInt MaxBECount = getUnsignedRangeMax(applyLoopGuards(Distance, L));
1057410575
MaxBECount = APIntOps::umin(MaxBECount, getUnsignedRangeMax(Distance));
1057510576

1057610577
// When a loop like "for (int i = 0; i != n; ++i) { /* body */ }" is rotated,
@@ -10611,7 +10612,7 @@ ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
1061110612
getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
1061210613
const SCEV *ConstantMax = getCouldNotCompute();
1061310614
if (Exact != getCouldNotCompute()) {
10614-
APInt MaxInt = getUnsignedRangeMax(applyLoopGuards(Exact, Guards));
10615+
APInt MaxInt = getUnsignedRangeMax(applyLoopGuards(Exact, L));
1061510616
ConstantMax =
1061610617
getConstant(APIntOps::umin(MaxInt, getUnsignedRangeMax(Exact)));
1061710618
}
@@ -10629,7 +10630,7 @@ ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
1062910630

1063010631
const SCEV *M = E;
1063110632
if (E != getCouldNotCompute()) {
10632-
APInt MaxWithGuards = getUnsignedRangeMax(applyLoopGuards(E, Guards));
10633+
APInt MaxWithGuards = getUnsignedRangeMax(applyLoopGuards(E, L));
1063310634
M = getConstant(APIntOps::umin(MaxWithGuards, getUnsignedRangeMax(E)));
1063410635
}
1063510636
auto *S = isa<SCEVCouldNotCompute>(E) ? M : E;
@@ -13674,6 +13675,7 @@ ScalarEvolution::~ScalarEvolution() {
1367413675
HasRecMap.clear();
1367513676
BackedgeTakenCounts.clear();
1367613677
PredicatedBackedgeTakenCounts.clear();
13678+
CachedLoopGuards.clear();
1367713679

1367813680
assert(PendingLoopPredicates.empty() && "isImpliedCond garbage");
1367913681
assert(PendingPhiRanges.empty() && "getRangeRef garbage");
@@ -15889,7 +15891,9 @@ const SCEV *ScalarEvolution::LoopGuards::rewrite(const SCEV *Expr) const {
1588915891
}
1589015892

1589115893
const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) {
15892-
return applyLoopGuards(Expr, LoopGuards::collect(L, *this));
15894+
if (!CachedLoopGuards.contains(L))
15895+
CachedLoopGuards.insert({L, LoopGuards::collect(L, *this)});
15896+
return applyLoopGuards(Expr, CachedLoopGuards.find(L)->second);
1589315897
}
1589415898

1589515899
const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr,

0 commit comments

Comments
 (0)