Skip to content

Commit 2425c69

Browse files
committed
Remove getSpeculative in favour of getSymbolicMax
* I've rewritten the loop variant of BackedgeTakenInfo::getSymbolicMax to be more consistent with BackedgeTakenInfo::getExact so that it now also accepts predicates. * I've changed getPredicatedBackedgeTakenCount to use getSymbolicMax, although we still require the latch block to have an exact exit-not-taken count.
1 parent 813d693 commit 2425c69

File tree

2 files changed

+63
-96
lines changed

2 files changed

+63
-96
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -893,9 +893,10 @@ class ScalarEvolution {
893893
/// SCEV predicates to Predicates that are required to be true in order for
894894
/// the answer to be correct. Predicates can be checked with run-time
895895
/// checks and can be used to perform loop versioning. If \p Speculative is
896-
/// true, this will attempt to return the speculative backedge count for loops
897-
/// with early exits. However, this is only possible if we can formulate an
898-
/// exact expression for the backedge count from the latch block.
896+
/// true, this will attempt to return the speculative - a restricted variant
897+
/// of the symbolic maximum - backedge count for loops with early exits.
898+
/// However, this is only possible if we can formulate an exact expression for
899+
/// the backedge count from the latch block.
899900
const SCEV *getPredicatedBackedgeTakenCount(
900901
const Loop *L, SmallVector<const SCEVPredicate *, 4> &Predicates,
901902
bool Speculative = false);
@@ -1492,10 +1493,6 @@ class ScalarEvolution {
14921493
/// the loop.
14931494
bool IsComplete = false;
14941495

1495-
/// Expression indicating the least maximum backedge-taken count of the loop
1496-
/// that is known, or a SCEVCouldNotCompute. Lazily computed on first query.
1497-
const SCEV *SymbolicMax = nullptr;
1498-
14991496
/// True iff the backedge is taken either exactly Max or zero times.
15001497
bool MaxOrZero = false;
15011498

@@ -1544,16 +1541,6 @@ class ScalarEvolution {
15441541
const SCEV *getExact(const Loop *L, ScalarEvolution *SE,
15451542
SmallVector<const SCEVPredicate *, 4> *Predicates = nullptr) const;
15461543

1547-
/// Similar to the above, except we permit unknown exit counts from
1548-
/// non-latch exit blocks. Any such early exit blocks must dominate the
1549-
/// latch and so the returned expression represents the speculative, or
1550-
/// maximum possible, *backedge-taken* count of the loop. If there is no
1551-
/// exact exit count for the latch this function returns
1552-
/// SCEVCouldNotCompute.
1553-
const SCEV *getSpeculative(
1554-
const Loop *L, ScalarEvolution *SE,
1555-
SmallVector<const SCEVPredicate *, 4> *Predicates = nullptr) const;
1556-
15571544
/// Return the number of times this loop exit may fall through to the back
15581545
/// edge, or SCEVCouldNotCompute. The loop is guaranteed not to exit via
15591546
/// this block before this number of iterations, but may exit via another
@@ -1573,7 +1560,9 @@ class ScalarEvolution {
15731560
ScalarEvolution *SE) const;
15741561

15751562
/// Get the symbolic max backedge taken count for the loop.
1576-
const SCEV *getSymbolicMax(const Loop *L, ScalarEvolution *SE);
1563+
const SCEV *getSymbolicMax(
1564+
const Loop *L, ScalarEvolution *SE,
1565+
SmallVector<const SCEVPredicate *, 4> *Predicates = nullptr) const;
15771566

15781567
/// Get the symbolic max backedge taken count for the particular loop exit.
15791568
const SCEV *getSymbolicMax(const BasicBlock *ExitingBlock,
@@ -1582,6 +1571,10 @@ class ScalarEvolution {
15821571
/// Return true if the number of times this backedge is taken is either the
15831572
/// value returned by getConstantMax or zero.
15841573
bool isConstantMaxOrZero(ScalarEvolution *SE) const;
1574+
1575+
/// Return true if we have an exact exit-not-taken count for the exiting
1576+
/// block.
1577+
bool hasExact(const BasicBlock *ExitingBlock, ScalarEvolution *SE) const;
15851578
};
15861579

15871580
/// Cache the backedge-taken count of the loops for this function as they
@@ -1785,11 +1778,6 @@ class ScalarEvolution {
17851778
ExitLimit computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
17861779
bool AllowPredicates = false);
17871780

1788-
/// Return a symbolic upper bound for the backedge taken count of the loop.
1789-
/// This is more general than getConstantMaxBackedgeTakenCount as it returns
1790-
/// an arbitrary expression as opposed to only constants.
1791-
const SCEV *computeSymbolicMaxBackedgeTakenCount(const Loop *L);
1792-
17931781
// Helper functions for computeExitLimitFromCond to avoid exponential time
17941782
// complexity.
17951783

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 52 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -8279,10 +8279,14 @@ const SCEV *ScalarEvolution::getExitCount(const Loop *L,
82798279
const 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

82888292
const 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.
86638628
const SCEV *
86648629
ScalarEvolution::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+
86738647
const 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

87158718
bool 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

Comments
 (0)