[llvm] [VPlan] Refactor FindLastSelect matching to use m_Specific(PhiR) (NFC). (PR #190547)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 5 12:35:06 PDT 2026


https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/190547

Match the select operands directly against PhiR using m_Specific, binding only the non-phi IV expression. This replaces the generic TrueVal/FalseVal matching followed by an assert and conditional extraction.

Split off from approved
https://github.com/llvm/llvm-project/pull/183911/ as suggested.

>From 17b0198017a619fa844f4967f5a7ed392982539a Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Sun, 5 Apr 2026 14:57:38 +0100
Subject: [PATCH] [VPlan] Refactor FindLastSelect matching to use
 m_Specific(PhiR) (NFC).

Match the select operands directly against PhiR using m_Specific, binding
only the non-phi IV expression. This replaces the generic TrueVal/FalseVal
matching followed by an assert and conditional extraction.

Split off from approved
https://github.com/llvm/llvm-project/pull/183911/ as suggested.
---
 .../Transforms/Vectorize/VPlanPatternMatch.h  |  5 +++
 .../Transforms/Vectorize/VPlanTransforms.cpp  | 32 ++++++++++---------
 2 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h b/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h
index 86fc518e3a7e0..2ce012c5a9f10 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h
@@ -272,6 +272,11 @@ inline bind_ty<VPValue> m_VPValue(VPValue *&V) { return V; }
 /// Match a VPIRValue.
 inline bind_ty<VPIRValue> m_VPIRValue(VPIRValue *&V) { return V; }
 
+/// Match a VPSingleDefRecipe, capturing if we match.
+inline bind_ty<VPSingleDefRecipe> m_VPSingleDefRecipe(VPSingleDefRecipe *&V) {
+  return V;
+}
+
 /// Match a VPInstruction, capturing if we match.
 inline bind_ty<VPInstruction> m_VPInstruction(VPInstruction *&V) { return V; }
 
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 7bf80f8d2616a..207099422905d 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -5751,24 +5751,26 @@ void VPlanTransforms::optimizeFindIVReductions(VPlan &Plan,
     // If there's a header mask, the backedge select will not be the find-last
     // select.
     VPValue *BackedgeVal = PhiR->getBackedgeValue();
-    VPValue *FindLastSelect = BackedgeVal;
-    if (HeaderMask && !match(BackedgeVal, m_Select(m_Specific(HeaderMask),
-                                                   m_VPValue(FindLastSelect),
-                                                   m_Specific(PhiR))))
+    auto *FindLastSelect = cast<VPSingleDefRecipe>(BackedgeVal);
+    if (HeaderMask &&
+        !match(BackedgeVal,
+               m_Select(m_Specific(HeaderMask),
+                        m_VPSingleDefRecipe(FindLastSelect), m_Specific(PhiR))))
       llvm_unreachable("expected header mask select");
 
-    // Get the IV from the find-last select of the reduction phi.
-    // The find-last select should be a select between the phi and the IV.
-    VPValue *Cond, *TrueVal, *FalseVal;
-    if (!match(FindLastSelect, m_Select(m_VPValue(Cond), m_VPValue(TrueVal),
-                                        m_VPValue(FalseVal))))
+    // Get the find-last expression from the find-last select of the reduction
+    // phi. The find-last select should be a select between the phi and the
+    // find-last expression.
+    VPValue *Cond, *FindLastExpression;
+    if (!match(FindLastSelect, m_Select(m_VPValue(Cond), m_Specific(PhiR),
+                                        m_VPValue(FindLastExpression))) &&
+        !match(FindLastSelect,
+               m_Select(m_VPValue(Cond), m_VPValue(FindLastExpression),
+                        m_Specific(PhiR))))
       continue;
 
-    // The non-phi operand of the select is the IV.
-    assert(is_contained(FindLastSelect->getDefiningRecipe()->operands(), PhiR));
-    VPValue *IV = TrueVal == PhiR ? FalseVal : TrueVal;
-
-    const SCEV *IVSCEV = vputils::getSCEVExprForVPValue(IV, PSE, &L);
+    const SCEV *IVSCEV =
+        vputils::getSCEVExprForVPValue(FindLastExpression, PSE, &L);
     const SCEV *Step;
     if (!match(IVSCEV, m_scev_AffineAddRec(m_SCEV(), m_SCEV(Step))))
       continue;
@@ -5833,7 +5835,7 @@ void VPlanTransforms::optimizeFindIVReductions(VPlan &Plan,
 
       VPBuilder LoopBuilder(BackedgeVal->getDefiningRecipe());
       VPValue *AnyOfCond = Cond;
-      if (TrueVal == PhiR)
+      if (FindLastSelect->getOperand(1) == PhiR)
         AnyOfCond = LoopBuilder.createNot(Cond);
       VPValue *OrVal = LoopBuilder.createOr(AnyOfPhi, AnyOfCond);
       AnyOfPhi->setOperand(1, OrVal);



More information about the llvm-commits mailing list