[llvm] [LSR] Extend lsr-term-fold to multiple exits loop and support the operand of condition is PHINode (PR #96048)

Zhijin Zeng via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 20 00:45:49 PDT 2024


================
@@ -6830,15 +6830,18 @@ canFoldTermCondOfLoop(Loop *L, ScalarEvolution &SE, DominatorTree &DT,
     return std::nullopt;
   }
 
-  if (!SE.hasLoopInvariantBackedgeTakenCount(L)) {
-    LLVM_DEBUG(dbgs() << "Cannot fold on backedge that is loop variant\n");
-    return std::nullopt;
-  }
-
   BasicBlock *LoopLatch = L->getLoopLatch();
   BranchInst *BI = dyn_cast<BranchInst>(LoopLatch->getTerminator());
   if (!BI || BI->isUnconditional())
     return std::nullopt;
+
+  if (!SE.hasLoopInvariantBackedgeTakenCount(L)) {
+    if (isa<SCEVCouldNotCompute>(SE.getExitCount(L, BI->getParent()))) {
----------------
zengdage wrote:

Sorry, I update the description about it and invite it's owner to review. Based on my limited understanding, if the exit count of LoopLatch is computable and the condition just has one use, it's safe to fold it.

https://github.com/llvm/llvm-project/pull/96048


More information about the llvm-commits mailing list