[llvm] [LV] Fix handleFirstArgMinOrMax to use 0 as identity element for UMax/SMax (PR #192054)

Akash Agrawal via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 15 10:39:35 PDT 2026


================
@@ -1779,14 +1785,11 @@ static bool handleFirstArgMinOrMax(
     FinalCanIV = DerivedIVRecipe;
   }
 
-  // If the final min/max value matches its start value, the condition in the
-  // loop was always false, i.e. no induction value has been selected. If that's
-  // the case, set the result of the IV reduction to its start value.
-  VPValue *AlwaysFalse = Builder.createICmp(CmpInst::ICMP_EQ, MinOrMaxResult,
-                                            MinOrMaxPhiR->getStartValue());
-  VPValue *FinalIV = Builder.createSelect(
-      AlwaysFalse, FindIVSelect->getOperand(2), FinalCanIV);
-  FindIVSelect->replaceAllUsesWith(FinalIV);
+  // Replace the previous FindIV result with the new canonical IV result.
+  // No guard needed: when the condition is never true, FindLastIVPhiR is
+  // initialized with 0, so RawCanIV = 0 and FinalCanIV correctly returns the
+  // start value of the original IV.
+  FindIVSelect->replaceAllUsesWith(FinalCanIV);
----------------
akashagrwl wrote:

You're right that we need to return the original start value of the FindIV reduction when the condition is never true. Our fix does exactly this: when the condition is never true, `FindLastIVPhiR` stays at 0 (the canonical identity element), so `RawCanIV = 0`. For non-canonical IVs, `FinalCanIV = DerivedIV(start_value, 0, step) = start_value`, which is the start value of the FindLastIV phi (since the FindLastIV phi starts at the same value as the wide IV). For canonical IVs, `FinalCanIV = 0`, which is also the start value of the FindLastIV phi. So our fix correctly returns the start value of the FindIV reduction in all cases.


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


More information about the llvm-commits mailing list