@@ -8279,10 +8279,14 @@ const SCEV *ScalarEvolution::getExitCount(const Loop *L,
8279
8279
const SCEV *ScalarEvolution::getPredicatedBackedgeTakenCount(
8280
8280
const Loop *L, SmallVector<const SCEVPredicate *, 4> &Preds,
8281
8281
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);
8286
8290
}
8287
8291
8288
8292
const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L,
@@ -8620,45 +8624,6 @@ void ScalarEvolution::BackedgeTakenInfo::getCountableExitingBlocks(
8620
8624
return;
8621
8625
}
8622
8626
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
-
8662
8627
/// Get the exact not taken count for this loop exit.
8663
8628
const SCEV *
8664
8629
ScalarEvolution::BackedgeTakenInfo::getExact(const BasicBlock *ExitingBlock,
@@ -8670,6 +8635,15 @@ ScalarEvolution::BackedgeTakenInfo::getExact(const BasicBlock *ExitingBlock,
8670
8635
return SE->getCouldNotCompute();
8671
8636
}
8672
8637
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
+
8673
8647
const SCEV *ScalarEvolution::BackedgeTakenInfo::getConstantMax(
8674
8648
const BasicBlock *ExitingBlock, ScalarEvolution *SE) const {
8675
8649
for (const auto &ENT : ExitNotTaken)
@@ -8704,12 +8678,41 @@ ScalarEvolution::BackedgeTakenInfo::getConstantMax(ScalarEvolution *SE) const {
8704
8678
return getConstantMax();
8705
8679
}
8706
8680
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);
8713
8716
}
8714
8717
8715
8718
bool ScalarEvolution::BackedgeTakenInfo::isConstantMaxOrZero(
@@ -15024,30 +15027,6 @@ bool ScalarEvolution::matchURem(const SCEV *Expr, const SCEV *&LHS,
15024
15027
return false;
15025
15028
}
15026
15029
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
-
15051
15030
/// A rewriter to replace SCEV expressions in Map with the corresponding entry
15052
15031
/// in the map. It skips AddRecExpr because we cannot guarantee that the
15053
15032
/// replacement is loop invariant in the loop of the AddRec.
0 commit comments