[llvm] [VPlan] Optimize FindLast of (binop %IV, live-in) by sinking. (PR #183911)

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 31 05:22:41 PDT 2026


================
@@ -5783,16 +5823,34 @@ void VPlanTransforms::optimizeFindIVReductions(VPlan &Plan,
         BackedgeVal,
         match_fn(m_VPInstruction<VPInstruction::ComputeReductionResult>())));
 
+    // If IV is an expression, create a new select that tracks the underlying
+    // IV directly and update the backedge value.
+    if (ExpressionIV) {
+      auto *SelectR = CondSelect->getDefiningRecipe();
+      VPBuilder LoopBuilder(SelectR);
+      VPValue *SelectCond = Cond;
+      if (TrueVal == PhiR)
+        SelectCond = LoopBuilder.createNot(Cond);
+      CondSelect = LoopBuilder.createSelect(SelectCond, ExpressionIV, PhiR,
+                                            SelectR->getDebugLoc());
+    }
+
+    // Create the reduction result in the middle block using sentinel directly.
     RecurKind MinMaxKind =
         UseMax ? (UseSigned ? RecurKind::SMax : RecurKind::UMax)
                : (UseSigned ? RecurKind::SMin : RecurKind::UMin);
     VPIRFlags Flags(MinMaxKind, /*IsOrdered=*/false, /*IsInLoop=*/false,
                     FastMathFlags());
     DebugLoc ExitDL = RdxResult->getDebugLoc();
     VPBuilder MiddleBuilder(RdxResult);
-    VPValue *ReducedIV =
-        MiddleBuilder.createNaryOp(VPInstruction::ComputeReductionResult,
-                                   RdxResult->getOperand(0), Flags, ExitDL);
+    VPValue *ReducedIV = MiddleBuilder.createNaryOp(
+        VPInstruction::ComputeReductionResult, CondSelect, Flags, ExitDL);
+
+    // If IV was an expression, sink the expression to the middle block.
----------------
ayalz wrote:

missing a commit?

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


More information about the llvm-commits mailing list