[llvm] [LoopUnroll] Rotate loop to make it countable for runtime unrolling (PR #146540)
Marek Sedláček via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 10 07:52:27 PDT 2025
================
@@ -484,8 +485,27 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
assert(ULO.Count > 0);
- // All these values should be taken only after peeling because they might have
- // changed.
+ if (ULO.Runtime && SE) {
+ BasicBlock *OrigHeader = L->getHeader();
+ BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
+ // Rotate loop if it makes it countable (for later unrolling)
+ if (BI && !BI->isUnconditional() &&
+ isa<SCEVCouldNotCompute>(SE->getExitCount(L, L->getLoopLatch())) &&
+ !isa<SCEVCouldNotCompute>(SE->getExitCount(L, OrigHeader))) {
+ LLVM_DEBUG(dbgs() << " Rotating loop to make the loop countable.\n");
+ SimplifyQuery SQ{OrigHeader->getDataLayout()};
+ SQ.TLI = nullptr;
+ SQ.DT = DT;
+ SQ.AC = AC;
+ llvm::LoopRotation(L, LI, TTI, AC, DT, SE, nullptr /*MemorySSAUpdater*/,
----------------
mark-sed wrote:
I have tested moving the rotation into the `UnrollRuntimeLoopRemainder` and the recalculating is way simpler than I first thought simply because a bunch of the calculations can be moved after the call to `UnrollRuntimeLoopRemainder` (e.g. the `ExitInfo`) and so pretty much only `LatchBlock` has to be recomputed.
https://github.com/llvm/llvm-project/pull/146540
More information about the llvm-commits
mailing list