Skip to content

Commit 3af8a11

Browse files
committed
[LoopDeletion] Separate logic in breakBackedgeIfNotTaken using symboic max trip count [nfc]
As mentioned in D108833, the logic for figuring out if a backedge is dead was somewhat interwoven with the SCEV based logic and the symbolic eval logic. This is my attempt at making the code easier to follow. Note that this is only NFC after the work done in 29fa37e. Thanks to Nikita for catching that case. Differential Revision: https://reviews.llvm.org/D108848
1 parent c7cbf1a commit 3af8a11

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

llvm/lib/Transforms/Scalar/LoopDeletion.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -393,19 +393,23 @@ breakBackedgeIfNotTaken(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
393393
if (!L->getLoopLatch())
394394
return LoopDeletionResult::Unmodified;
395395

396-
auto *BTCMax = SE.getConstantMaxBackedgeTakenCount(L);
397-
if (!BTCMax->isZero()) {
398-
auto *BTC = SE.getBackedgeTakenCount(L);
399-
if (!BTC->isZero()) {
400-
if (!isa<SCEVCouldNotCompute>(BTC) && SE.isKnownNonZero(BTC))
401-
return LoopDeletionResult::Unmodified;
402-
if (!canProveExitOnFirstIteration(L, DT, LI))
403-
return LoopDeletionResult::Unmodified;
404-
}
396+
auto *BTC = SE.getSymbolicMaxBackedgeTakenCount(L);
397+
if (BTC->isZero()) {
398+
// SCEV knows this backedge isn't taken!
399+
breakLoopBackedge(L, DT, SE, LI, MSSA);
400+
return LoopDeletionResult::Deleted;
405401
}
406402

407-
breakLoopBackedge(L, DT, SE, LI, MSSA);
408-
return LoopDeletionResult::Deleted;
403+
// If SCEV leaves open the possibility of a zero trip count, see if
404+
// symbolically evaluating the first iteration lets us prove the backedge
405+
// unreachable.
406+
if (isa<SCEVCouldNotCompute>(BTC) || !SE.isKnownNonZero(BTC))
407+
if (canProveExitOnFirstIteration(L, DT, LI)) {
408+
breakLoopBackedge(L, DT, SE, LI, MSSA);
409+
return LoopDeletionResult::Deleted;
410+
}
411+
412+
return LoopDeletionResult::Unmodified;
409413
}
410414

411415
/// Remove a loop if it is dead.

0 commit comments

Comments
 (0)