[llvm] [VPlan] Untie tail folding from optimizeInductionLiveOutUsers. NFC (PR #207984)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 23 08:49:55 PDT 2026


================
@@ -1012,31 +1012,55 @@ optimizeLatchExitInductionUser(VPlan &Plan, VPValue *Op,
 
 void VPlanTransforms::optimizeInductionLiveOutUsers(
     VPlan &Plan, PredicatedScalarEvolution &PSE) {
-  // Compute end values for all inductions.
+  // Compute the value inductions at two points:
+  // - when exiting the loop from the latch, at trip-count iterations.
+  // - when resuming at the scalar preheader, at vector trip-count iterations.
   VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion();
   auto *VectorPH = cast<VPBasicBlock>(VectorRegion->getSinglePredecessor());
   VPBuilder VectorPHBuilder(VectorPH, VectorPH->begin());
-  DenseMap<VPValue *, VPValue *> EndValues;
-  VPValue *ResumeTC =
-      Plan.hasTailFolded() ? Plan.getTripCount() : &Plan.getVectorTripCount();
+  DenseMap<VPValue *, VPValue *> LatchExitValues, ResumeValues;
   for (auto &Phi : VectorRegion->getEntryBasicBlock()->phis()) {
     auto *WideIV = dyn_cast<VPWidenInductionRecipe>(&Phi);
     if (!WideIV)
       continue;
-    if (VPValue *EndValue =
-            tryToComputeEndValueForInduction(WideIV, VectorPHBuilder, ResumeTC))
-      EndValues[WideIV] = EndValue;
+    if (VPValue *EndValue = tryToComputeEndValueForInduction(
+            WideIV, VectorPHBuilder, Plan.getTripCount()))
+      LatchExitValues[WideIV] = EndValue;
+    if (VPValue *EndValue = tryToComputeEndValueForInduction(
+            WideIV, VectorPHBuilder, &Plan.getVectorTripCount()))
+      ResumeValues[WideIV] = EndValue;
   }
 
   VPBasicBlock *MiddleVPBB = Plan.getMiddleBlock();
+  VPBasicBlock *LatchExitVPBB = nullptr;
+  // Try and find the latch exit from MiddleVPBB.
+  if (Plan.isExitBlock(MiddleVPBB->getSuccessors()[0])) {
+    LatchExitVPBB = cast<VPBasicBlock>(MiddleVPBB->getSuccessors()[0]);
+    // If we branch to the latch exit on TC == VTC, then the IVs at the latch
+    // exit are equal to the IVs at the scalar preheader. Reuse ResumeValues
+    // to avoid computing the same value in two different ways.
+    if (match(MiddleVPBB->getTerminator(),
----------------
lukel97 wrote:

I believe so, I just checked in ` llvm/test/Transforms/LoopVectorize/optimal-epilog-vectorization-liveout.ll` and the epilogue plan's middle blocks get picked up by this

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


More information about the llvm-commits mailing list