Skip to content

[SCEV] Avoid unnecessary call to getExitingBlock() in computeExitLimit() #96188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llvm/include/llvm/Analysis/ScalarEvolution.h
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,7 @@ class ScalarEvolution {
/// this call will try to use a minimal set of SCEV predicates in order to
/// return an exact answer.
ExitLimit computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
bool AllowPredicates = false);
bool IsOnlyExit, bool AllowPredicates = false);

// Helper functions for computeExitLimitFromCond to avoid exponential time
// complexity.
Expand Down
9 changes: 4 additions & 5 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8775,6 +8775,7 @@ ScalarEvolution::computeBackedgeTakenCount(const Loop *L,
const SCEV *MustExitMaxBECount = nullptr;
const SCEV *MayExitMaxBECount = nullptr;
bool MustExitMaxOrZero = false;
bool IsOnlyExit = ExitingBlocks.size() == 1;

// Compute the ExitLimit for each loop exit. Use this to populate ExitCounts
// and compute maxBECount.
Expand All @@ -8792,7 +8793,7 @@ ScalarEvolution::computeBackedgeTakenCount(const Loop *L,
continue;
}

ExitLimit EL = computeExitLimit(L, ExitBB, AllowPredicates);
ExitLimit EL = computeExitLimit(L, ExitBB, IsOnlyExit, AllowPredicates);

assert((AllowPredicates || EL.Predicates.empty()) &&
"Predicated exit limit when predicates are not allowed!");
Expand Down Expand Up @@ -8867,15 +8868,14 @@ ScalarEvolution::computeBackedgeTakenCount(const Loop *L,

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

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

return getCouldNotCompute();
Expand Down
Loading