Skip to content

Commit 1fafa32

Browse files
authored
[SCEV] Avoid unnecessary call to getExitingBlock() in computeExitLimit() (#96188)
In `computeExitLimit()`, we use `getExitingBlock()` to check if loop has exactly one exiting block. Since `computeExitLimit()` is only used in `computeBackedgeTakenCount()`, and `getExitingBlocks()` is called to get all exiting blocks in `computeBackedgeTakenCount()`, we can simply check if loop has exactly one exiting block by checking if the number of exiting blocks equals 1 in `computeBackedgeTakenCount()` and pass it as an argument to `computeExitLimit()`. This change helps to improve the compile time for files containing large loops.
1 parent 1373f7c commit 1fafa32

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1766,7 +1766,7 @@ class ScalarEvolution {
17661766
/// this call will try to use a minimal set of SCEV predicates in order to
17671767
/// return an exact answer.
17681768
ExitLimit computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
1769-
bool AllowPredicates = false);
1769+
bool IsOnlyExit, bool AllowPredicates = false);
17701770

17711771
// Helper functions for computeExitLimitFromCond to avoid exponential time
17721772
// complexity.

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8780,6 +8780,7 @@ ScalarEvolution::computeBackedgeTakenCount(const Loop *L,
87808780
const SCEV *MustExitMaxBECount = nullptr;
87818781
const SCEV *MayExitMaxBECount = nullptr;
87828782
bool MustExitMaxOrZero = false;
8783+
bool IsOnlyExit = ExitingBlocks.size() == 1;
87838784

87848785
// Compute the ExitLimit for each loop exit. Use this to populate ExitCounts
87858786
// and compute maxBECount.
@@ -8797,7 +8798,7 @@ ScalarEvolution::computeBackedgeTakenCount(const Loop *L,
87978798
continue;
87988799
}
87998800

8800-
ExitLimit EL = computeExitLimit(L, ExitBB, AllowPredicates);
8801+
ExitLimit EL = computeExitLimit(L, ExitBB, IsOnlyExit, AllowPredicates);
88018802

88028803
assert((AllowPredicates || EL.Predicates.empty()) &&
88038804
"Predicated exit limit when predicates are not allowed!");
@@ -8872,15 +8873,14 @@ ScalarEvolution::computeBackedgeTakenCount(const Loop *L,
88728873

88738874
ScalarEvolution::ExitLimit
88748875
ScalarEvolution::computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
8875-
bool AllowPredicates) {
8876+
bool IsOnlyExit, bool AllowPredicates) {
88768877
assert(L->contains(ExitingBlock) && "Exit count for non-loop block?");
88778878
// If our exiting block does not dominate the latch, then its connection with
88788879
// loop's exit limit may be far from trivial.
88798880
const BasicBlock *Latch = L->getLoopLatch();
88808881
if (!Latch || !DT.dominates(ExitingBlock, Latch))
88818882
return getCouldNotCompute();
88828883

8883-
bool IsOnlyExit = (L->getExitingBlock() != nullptr);
88848884
Instruction *Term = ExitingBlock->getTerminator();
88858885
if (BranchInst *BI = dyn_cast<BranchInst>(Term)) {
88868886
assert(BI->isConditional() && "If unconditional, it can't be in loop!");
@@ -8904,8 +8904,7 @@ ScalarEvolution::computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
89048904
}
89058905
assert(Exit && "Exiting block must have at least one exit");
89068906
return computeExitLimitFromSingleExitSwitch(
8907-
L, SI, Exit,
8908-
/*ControlsOnlyExit=*/IsOnlyExit);
8907+
L, SI, Exit, /*ControlsOnlyExit=*/IsOnlyExit);
89098908
}
89108909

89118910
return getCouldNotCompute();

0 commit comments

Comments
 (0)