[llvm] [LoopPeel] Remove known trip count restriction when peeling last. (PR #140792)
Philip Reames via llvm-commits
llvm-commits at lists.llvm.org
Wed May 21 07:24:53 PDT 2025
================
@@ -1080,11 +1086,40 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, bool PeelLast, LoopInfo *LI,
for (PHINode &P : Exit->phis())
ExitValues[&P] = P.getIncomingValueForBlock(Latch);
+ const SCEV *BTC = SE->getBackedgeTakenCount(L);
+
InsertTop = SplitEdge(Latch, Exit, &DT, LI);
InsertBot = SplitBlock(InsertTop, InsertTop->getTerminator(), &DT, LI);
InsertTop->setName(Exit->getName() + ".peel.begin");
InsertBot->setName(Exit->getName() + ".peel.next");
+ NewPreHeader = nullptr;
+
+ // If the original loop may only execute a single iteration we need to
+ // insert a trip count check and skip the peeled loop if necessary.
+ if (!SE->isKnownPredicate(CmpInst::ICMP_UGT, BTC,
+ SE->getZero(BTC->getType()))) {
+ NewPreHeader = SplitEdge(PreHeader, Header, &DT, LI);
+ SCEVExpander Expander(*SE, Latch->getDataLayout(), "loop-peel");
+
+ BranchInst *PreHeaderBR = cast<BranchInst>(PreHeader->getTerminator());
+ Value *BTCValue =
+ Expander.expandCodeFor(BTC, BTC->getType(), PreHeaderBR);
+ IRBuilder<> B(PreHeaderBR);
+ Value *Cond =
+ B.CreateICmpNE(BTCValue, ConstantInt::get(BTCValue->getType(), 0));
+ B.CreateCondBr(Cond, NewPreHeader, InsertTop);
+ PreHeaderBR->eraseFromParent();
+
+ // PreHeader now dominates InsertTop.
+ DT.changeImmediateDominator(InsertTop, PreHeader);
+
+ // If we branch from PreHeader to InsertTop, we are guaranteed to execute
+ // the peeled iteration, so the exit values from the original loop are
+ // dead. Use poison for them.
+ for (auto &PN : InsertTop->phis())
----------------
preames wrote:
Wait, don't these need to the be the starting values for the loops IVs? If we branch directly to the peeled iteration, don't we need to have the phis start with the original start values?
Note the comment is a tad confusing. The loop exit values *are* poison, but the incoming value isn't coming from the loop body, it's coming from the preheader. Unless I'm getting confused from the variable naming here?
https://github.com/llvm/llvm-project/pull/140792
More information about the llvm-commits
mailing list