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

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 23 02:06:10 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])) {
----------------
david-arm wrote:

Does this work for loops with early exits?

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


More information about the llvm-commits mailing list