[llvm] [LV] Vectorize selecting last IV of min/max element. (PR #141431)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 22 07:13:11 PDT 2025


================
@@ -813,3 +813,148 @@ bool VPlanTransforms::handleMaxMinNumReductionsWithoutFastMath(VPlan &Plan) {
   MiddleTerm->setOperand(0, NewCond);
   return true;
 }
+
+bool VPlanTransforms::legalizeUnclassifiedPhis(VPlan &Plan) {
+  using namespace VPlanPatternMatch;
+  for (auto &PhiR : make_early_inc_range(
+           Plan.getVectorLoopRegion()->getEntryBasicBlock()->phis())) {
+    if (!isa<VPWidenPHIRecipe>(&PhiR))
+      continue;
+
+    // Check if PhiR is a min/max reduction that has a user inside the loop
+    // outside the min/max reduction chain. The other user must be the compare
+    // of a FindLastIV reduction chain.
+    auto *MinMaxPhiR = cast<VPWidenPHIRecipe>(&PhiR);
+    auto *MinMaxOp = dyn_cast_or_null<VPSingleDefRecipe>(
+        MinMaxPhiR->getOperand(1)->getDefiningRecipe());
+    if (!MinMaxOp)
+      return false;
+
+    // The incoming value must be a min/max instrinsic.
+    // TODO: Also handle the select variant.
+    Intrinsic::ID ID = Intrinsic::not_intrinsic;
+    if (auto *WideInt = dyn_cast<VPWidenIntrinsicRecipe>(MinMaxOp))
+      ID = WideInt->getVectorIntrinsicID();
+    else {
----------------
fhahn wrote:

added, thanks

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


More information about the llvm-commits mailing list