[llvm] [VPlan] Model first memory runtime checks as VPlan recipes. (PR #221483)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 22 15:47:51 PDT 2026


================
@@ -1077,9 +1078,31 @@ VPValue *VPSCEVExpander::expand(const SCEV *S) {
       Ops.push_back(OpV);
     }
     VPValue *Result = Ops.front();
-    for (VPValue *Op : drop_begin(Ops))
-      Result = Builder.createScalarIntrinsic(IntrinsicID, {Result, Op},
-                                             ResultTy, DL);
+    for (VPValue *Op : drop_begin(Ops)) {
+      if (ResultTy->isPointerTy()) {
+        // The min/max intrinsics don't support pointer operands, so expand
+        // pointer-typed min/max as cmp + select, matching SCEVExpander.
+        VPValue *Cmp = Builder.createICmp(
+            MinMaxIntrinsic::getPredicate(IntrinsicID), Result, Op, DL);
+        VPInstruction *Sel = Builder.createSelect(Cmp, Result, Op, DL);
+        Function *F =
+            Builder.getPlan().getScalarHeader()->getIRBasicBlock()->getParent();
+        std::optional<uint64_t> EC = F->getEntryCount();
+        if (EC && *EC > 0) {
+          MDBuilder MDB(SE.getContext());
+          Sel->setMetadata(
+              LLVMContext::MD_prof,
+              MDNode::get(SE.getContext(),
+                          {MDB.createString(
+                               MDProfLabels::UnknownBranchWeightsMarker),
+                           MDB.createString("scev-expander")}));
+        }
+        Result = Sel;
----------------
artagnon wrote:

```suggestion
        Result = Builder.createSelect(Cmp, Result, Op, DL);
        Function *F =
            Builder.getPlan().getScalarHeader()->getIRBasicBlock()->getParent();
        std::optional<uint64_t> EC = F->getEntryCount();
        if (EC && *EC > 0) {
          MDBuilder MDB(SE.getContext());
          Result->setMetadata(
              LLVMContext::MD_prof,
              MDNode::get(SE.getContext(),
                          {MDB.createString(
                               MDProfLabels::UnknownBranchWeightsMarker),
                           MDB.createString("scev-expander")}));
        }
```

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


More information about the llvm-commits mailing list