@@ -8417,6 +8417,7 @@ void ScalarEvolution::forgetAllLoops() {
8417
8417
// result.
8418
8418
BackedgeTakenCounts.clear();
8419
8419
PredicatedBackedgeTakenCounts.clear();
8420
+ CachedLoopGuards.clear();
8420
8421
BECountUsers.clear();
8421
8422
LoopPropertiesCache.clear();
8422
8423
ConstantEvolutionLoopExitValue.clear();
@@ -10551,9 +10552,9 @@ ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
10551
10552
if (!isLoopInvariant(Step, L))
10552
10553
return getCouldNotCompute();
10553
10554
10554
- LoopGuards Guards = LoopGuards::collect(L, *this);
10555
+ // LoopGuards Guards = LoopGuards::collect(L, *this);
10555
10556
// 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 );
10557
10558
10558
10559
// For positive steps (counting up until unsigned overflow):
10559
10560
// N = -Start/Step (as unsigned)
@@ -10570,7 +10571,7 @@ ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
10570
10571
// N = Distance (as unsigned)
10571
10572
if (StepC &&
10572
10573
(StepC->getValue()->isOne() || StepC->getValue()->isMinusOne())) {
10573
- APInt MaxBECount = getUnsignedRangeMax(applyLoopGuards(Distance, Guards ));
10574
+ APInt MaxBECount = getUnsignedRangeMax(applyLoopGuards(Distance, L ));
10574
10575
MaxBECount = APIntOps::umin(MaxBECount, getUnsignedRangeMax(Distance));
10575
10576
10576
10577
// 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,
10611
10612
getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
10612
10613
const SCEV *ConstantMax = getCouldNotCompute();
10613
10614
if (Exact != getCouldNotCompute()) {
10614
- APInt MaxInt = getUnsignedRangeMax(applyLoopGuards(Exact, Guards ));
10615
+ APInt MaxInt = getUnsignedRangeMax(applyLoopGuards(Exact, L ));
10615
10616
ConstantMax =
10616
10617
getConstant(APIntOps::umin(MaxInt, getUnsignedRangeMax(Exact)));
10617
10618
}
@@ -10629,7 +10630,7 @@ ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
10629
10630
10630
10631
const SCEV *M = E;
10631
10632
if (E != getCouldNotCompute()) {
10632
- APInt MaxWithGuards = getUnsignedRangeMax(applyLoopGuards(E, Guards ));
10633
+ APInt MaxWithGuards = getUnsignedRangeMax(applyLoopGuards(E, L ));
10633
10634
M = getConstant(APIntOps::umin(MaxWithGuards, getUnsignedRangeMax(E)));
10634
10635
}
10635
10636
auto *S = isa<SCEVCouldNotCompute>(E) ? M : E;
@@ -13674,6 +13675,7 @@ ScalarEvolution::~ScalarEvolution() {
13674
13675
HasRecMap.clear();
13675
13676
BackedgeTakenCounts.clear();
13676
13677
PredicatedBackedgeTakenCounts.clear();
13678
+ CachedLoopGuards.clear();
13677
13679
13678
13680
assert(PendingLoopPredicates.empty() && "isImpliedCond garbage");
13679
13681
assert(PendingPhiRanges.empty() && "getRangeRef garbage");
@@ -15889,7 +15891,9 @@ const SCEV *ScalarEvolution::LoopGuards::rewrite(const SCEV *Expr) const {
15889
15891
}
15890
15892
15891
15893
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);
15893
15897
}
15894
15898
15895
15899
const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr,
0 commit comments