@@ -8279,10 +8279,14 @@ const SCEV *ScalarEvolution::getExitCount(const Loop *L,
82798279const SCEV *ScalarEvolution::getPredicatedBackedgeTakenCount(
82808280 const Loop *L, SmallVector<const SCEVPredicate *, 4> &Preds,
82818281 bool Speculative) {
8282- if (Speculative)
8283- return getPredicatedBackedgeTakenInfo(L).getSpeculative(L, this, &Preds);
8284- else
8285- return getPredicatedBackedgeTakenInfo(L).getExact(L, this, &Preds);
8282+ const BackedgeTakenInfo &BTI = getPredicatedBackedgeTakenInfo(L);
8283+ if (Speculative) {
8284+ const BasicBlock *Latch = L->getLoopLatch();
8285+ if (!Latch || !BTI.hasExact(Latch, this))
8286+ return getCouldNotCompute();
8287+ return BTI.getSymbolicMax(L, this, &Preds);
8288+ } else
8289+ return BTI.getExact(L, this, &Preds);
82868290}
82878291
82888292const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L,
@@ -8620,45 +8624,6 @@ void ScalarEvolution::BackedgeTakenInfo::getCountableExitingBlocks(
86208624 return;
86218625}
86228626
8623- const SCEV *ScalarEvolution::BackedgeTakenInfo::getSpeculative(
8624- const Loop *L, ScalarEvolution *SE,
8625- SmallVector<const SCEVPredicate *, 4> *Preds) const {
8626- // All exiting blocks we have collected must dominate the only backedge.
8627- const BasicBlock *Latch = L->getLoopLatch();
8628- if (!Latch || !hasAnyInfo())
8629- return SE->getCouldNotCompute();
8630-
8631- // All exiting blocks we have gathered dominate loop's latch, so speculative
8632- // trip count is simply a minimum out of all these calculated exit counts.
8633- SmallVector<const SCEV *, 2> Ops;
8634- bool FoundLatch = false;
8635- for (const auto &ENT : ExitNotTaken) {
8636- const SCEV *BECount = ENT.ExactNotTaken;
8637- if (BECount == SE->getCouldNotCompute())
8638- continue;
8639-
8640- assert(SE->DT.dominates(ENT.ExitingBlock, Latch) &&
8641- "We should only have known counts for exiting blocks that dominate "
8642- "latch!");
8643- Ops.push_back(BECount);
8644- if (Preds)
8645- for (const auto *P : ENT.Predicates)
8646- Preds->push_back(P);
8647- assert((Preds || ENT.hasAlwaysTruePredicate()) &&
8648- "Predicate should be always true!");
8649- if (ENT.ExitingBlock == Latch)
8650- FoundLatch = true;
8651- }
8652-
8653- if (!FoundLatch)
8654- return SE->getCouldNotCompute();
8655-
8656- // If an earlier exit exits on the first iteration (exit count zero), then
8657- // a later poison exit count should not propagate into the result. This are
8658- // exactly the semantics provided by umin_seq.
8659- return SE->getUMinFromMismatchedTypes(Ops, /* Sequential */ true);
8660- }
8661-
86628627/// Get the exact not taken count for this loop exit.
86638628const SCEV *
86648629ScalarEvolution::BackedgeTakenInfo::getExact(const BasicBlock *ExitingBlock,
@@ -8670,6 +8635,15 @@ ScalarEvolution::BackedgeTakenInfo::getExact(const BasicBlock *ExitingBlock,
86708635 return SE->getCouldNotCompute();
86718636}
86728637
8638+ bool ScalarEvolution::BackedgeTakenInfo::hasExact(
8639+ const BasicBlock *ExitingBlock, ScalarEvolution *SE) const {
8640+ for (const auto &ENT : ExitNotTaken)
8641+ if (ENT.ExitingBlock == ExitingBlock)
8642+ return ENT.ExactNotTaken != SE->getCouldNotCompute();
8643+
8644+ return false;
8645+ }
8646+
86738647const SCEV *ScalarEvolution::BackedgeTakenInfo::getConstantMax(
86748648 const BasicBlock *ExitingBlock, ScalarEvolution *SE) const {
86758649 for (const auto &ENT : ExitNotTaken)
@@ -8704,12 +8678,41 @@ ScalarEvolution::BackedgeTakenInfo::getConstantMax(ScalarEvolution *SE) const {
87048678 return getConstantMax();
87058679}
87068680
8707- const SCEV *
8708- ScalarEvolution::BackedgeTakenInfo::getSymbolicMax(const Loop *L,
8709- ScalarEvolution *SE) {
8710- if (!SymbolicMax)
8711- SymbolicMax = SE->computeSymbolicMaxBackedgeTakenCount(L);
8712- return SymbolicMax;
8681+ const SCEV *ScalarEvolution::BackedgeTakenInfo::getSymbolicMax(
8682+ const Loop *L, ScalarEvolution *SE,
8683+ SmallVector<const SCEVPredicate *, 4> *Preds) const {
8684+ // If any exits were not computable, the loop is not computable.
8685+ if (ExitNotTaken.empty())
8686+ return SE->getCouldNotCompute();
8687+
8688+ const BasicBlock *Latch = L->getLoopLatch();
8689+ // All exiting blocks we have collected must dominate the only backedge.
8690+ if (!Latch)
8691+ return SE->getCouldNotCompute();
8692+
8693+ // Form an expression for the maximum exit count possible for this loop. We
8694+ // merge the max and exact information to approximate a version of
8695+ // getConstantMaxBackedgeTakenCount which isn't restricted to just constants.
8696+ SmallVector<const SCEV *, 4> ExitCounts;
8697+ for (const auto &ENT : ExitNotTaken) {
8698+ const SCEV *ExitCount = ENT.SymbolicMaxNotTaken;
8699+ if (ExitCount == SE->getCouldNotCompute())
8700+ continue;
8701+
8702+ assert(SE->DT.dominates(ENT.ExitingBlock, Latch) &&
8703+ "We should only have known counts for exiting blocks that dominate "
8704+ "the latch!");
8705+ ExitCounts.push_back(ExitCount);
8706+ if (Preds)
8707+ for (const auto *P : ENT.Predicates)
8708+ Preds->push_back(P);
8709+ assert((Preds || ENT.hasAlwaysTruePredicate()) &&
8710+ "Predicate should be always true!");
8711+ }
8712+
8713+ if (ExitCounts.empty())
8714+ return SE->getCouldNotCompute();
8715+ return SE->getUMinFromMismatchedTypes(ExitCounts, /*Sequential*/ true);
87138716}
87148717
87158718bool ScalarEvolution::BackedgeTakenInfo::isConstantMaxOrZero(
@@ -15024,30 +15027,6 @@ bool ScalarEvolution::matchURem(const SCEV *Expr, const SCEV *&LHS,
1502415027 return false;
1502515028}
1502615029
15027- const SCEV *
15028- ScalarEvolution::computeSymbolicMaxBackedgeTakenCount(const Loop *L) {
15029- SmallVector<BasicBlock*, 16> ExitingBlocks;
15030- L->getExitingBlocks(ExitingBlocks);
15031-
15032- // Form an expression for the maximum exit count possible for this loop. We
15033- // merge the max and exact information to approximate a version of
15034- // getConstantMaxBackedgeTakenCount which isn't restricted to just constants.
15035- SmallVector<const SCEV*, 4> ExitCounts;
15036- for (BasicBlock *ExitingBB : ExitingBlocks) {
15037- const SCEV *ExitCount =
15038- getExitCount(L, ExitingBB, ScalarEvolution::SymbolicMaximum);
15039- if (!isa<SCEVCouldNotCompute>(ExitCount)) {
15040- assert(DT.dominates(ExitingBB, L->getLoopLatch()) &&
15041- "We should only have known counts for exiting blocks that "
15042- "dominate latch!");
15043- ExitCounts.push_back(ExitCount);
15044- }
15045- }
15046- if (ExitCounts.empty())
15047- return getCouldNotCompute();
15048- return getUMinFromMismatchedTypes(ExitCounts, /*Sequential*/ true);
15049- }
15050-
1505115030/// A rewriter to replace SCEV expressions in Map with the corresponding entry
1505215031/// in the map. It skips AddRecExpr because we cannot guarantee that the
1505315032/// replacement is loop invariant in the loop of the AddRec.
0 commit comments