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

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 20 12:13:51 PST 2025


================
@@ -214,6 +214,51 @@ static bool checkOrderedReduction(RecurKind Kind, Instruction *ExactFPMathInst,
   return true;
 }
 
+/// Returns true if \p Phi is a min/max reduction matching \p Kind where \p Phi
+/// is used in the loop outside the reduction chain. This is common for
+static bool isMinMaxReductionWithLoopUsersOutsideReductionChain(
+    PHINode *Phi, RecurKind Kind, Loop *TheLoop, RecurrenceDescriptor &RedDes) {
+  BasicBlock *Latch = TheLoop->getLoopLatch();
+  if (!Latch)
+    return false;
+  assert(Phi->getNumIncomingValues() == 2 &&
+         "phi must have exactly 2 incoming values");
+  Value *Inc = Phi->getIncomingValueForBlock(Latch);
+  if (Phi->hasOneUse() ||
+      !RecurrenceDescriptor::isIntMinMaxRecurrenceKind(Kind))
+    return false;
+
+  Value *A, *B;
+  bool Matched = [&]() {
+    switch (Kind) {
+    case RecurKind::UMax:
+      return match(Inc, m_OneUse(m_UMax(m_Value(A), m_Value(B))));
+    case RecurKind::UMin:
+      return match(Inc, m_OneUse(m_UMin(m_Value(A), m_Value(B))));
+    case RecurKind::SMax:
+      return match(Inc, m_OneUse(m_SMax(m_Value(A), m_Value(B))));
+    case RecurKind::SMin:
+      return match(Inc, m_OneUse(m_SMin(m_Value(A), m_Value(B))));
+    default:
+      llvm_unreachable("all min/max kinds must be handled");
----------------
fhahn wrote:

This checks for completeness, all integer min/max should be handled, and unreachable here should make it slightly easier to catch missing cases together with the check above

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


More information about the llvm-commits mailing list