@@ -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
1589115893const 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
1589515899const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr,
0 commit comments