[llvm] [VPlan] Expand SCEV min/max via scalar intrinsic VPInstructions. (PR #207842)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 13 02:01:45 PDT 2026


================
@@ -842,6 +842,43 @@ VPValue *VPSCEVExpander::tryToExpand(const SCEV *S) {
     }
     return Builder.createScalarCast(Opcode, Op, S->getType(), DL);
   }
+  case scUMaxExpr:
+  case scSMaxExpr:
+  case scUMinExpr:
+  case scSMinExpr: {
+    auto *MinMax = cast<SCEVMinMaxExpr>(S);
+    Intrinsic::ID IntrinsicID;
+    switch (S->getSCEVType()) {
+    case scUMaxExpr:
+      IntrinsicID = Intrinsic::umax;
+      break;
+    case scSMaxExpr:
+      IntrinsicID = Intrinsic::smax;
+      break;
+    case scUMinExpr:
+      IntrinsicID = Intrinsic::umin;
+      break;
+    case scSMinExpr:
+      IntrinsicID = Intrinsic::smin;
+      break;
+    default:
+      llvm_unreachable("not a min/max SCEV");
+    }
+    // Chain operands in reverse order matching SCEVExpander's expansion of
+    // min/max expressions.
+    SmallVector<VPValue *, 2> Ops;
+    for (const SCEVUse &Op : reverse(MinMax->operands())) {
+      VPValue *OpV = tryToExpand(Op);
+      if (!OpV)
+        return nullptr;
+      Ops.push_back(OpV);
+    }
+    Type *Ty = MinMax->getType();
+    VPValue *Result = Ops.front();
+    for (VPValue *Op : drop_begin(Ops))
+      Result = Builder.createScalarIntrinsic(IntrinsicID, {Result, Op}, Ty, DL);
----------------
fhahn wrote:

done thanks

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


More information about the llvm-commits mailing list