[PATCH] D108848: [LoopDeletion] Separate logic in breakBackedgeIfNotTaken using symboic max trip count [nfc]

Philip Reames via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 27 14:40:29 PDT 2021


reames created this revision.
reames added reviewers: nikic, fhahn, mkazantsev, lebedev.ri.
Herald added subscribers: javed.absar, bollu, hiraditya, mcrosier.
reames requested review of this revision.
Herald added a project: LLVM.

As mentioned in D108833 <https://reviews.llvm.org/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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D108848

Files:
  llvm/lib/Transforms/Scalar/LoopDeletion.cpp


Index: llvm/lib/Transforms/Scalar/LoopDeletion.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LoopDeletion.cpp
+++ llvm/lib/Transforms/Scalar/LoopDeletion.cpp
@@ -393,19 +393,23 @@
   if (!L->getLoopLatch())
     return LoopDeletionResult::Unmodified;
 
-  auto *BTCMax = SE.getConstantMaxBackedgeTakenCount(L);
-  if (!BTCMax->isZero()) {
-    auto *BTC = SE.getBackedgeTakenCount(L);
-    if (!BTC->isZero()) {
-      if (!isa<SCEVCouldNotCompute>(BTC) && SE.isKnownNonZero(BTC))
-        return LoopDeletionResult::Unmodified;
-      if (!canProveExitOnFirstIteration(L, DT, LI))
-        return LoopDeletionResult::Unmodified;
-    }
+  auto *BTC = SE.getSymbolicMaxBackedgeTakenCount(L);
+  if (BTC->isZero()) {
+    // SCEV knows this backedge isn't taken!
+    breakLoopBackedge(L, DT, SE, LI, MSSA);
+    return LoopDeletionResult::Deleted;
   }
 
-  breakLoopBackedge(L, DT, SE, LI, MSSA);
-  return LoopDeletionResult::Deleted;
+  // If SCEV leaves open the possibility of a zero trip count, see if
+  // symbolically evaluating the first iteration lets us prove the backedge
+  // unreachable.
+  if (isa<SCEVCouldNotCompute>(BTC) || !SE.isKnownNonZero(BTC))
+    if (canProveExitOnFirstIteration(L, DT, LI)) {
+      breakLoopBackedge(L, DT, SE, LI, MSSA);
+      return LoopDeletionResult::Deleted;
+    }
+
+  return LoopDeletionResult::Unmodified;
 }
 
 /// Remove a loop if it is dead.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108848.369191.patch
Type: text/x-patch
Size: 1458 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210827/64f9bb13/attachment.bin>


More information about the llvm-commits mailing list