[llvm] [InstCombine] Avoid folding `select(umin(X, Y), X)` with non-constant mask (PR #143020)

Konstantin Bogdanov via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 5 12:59:38 PDT 2025


================
@@ -1654,6 +1654,29 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
   if (Value *FreedOp = getFreedOperand(&CI, &TLI))
     return visitFree(CI, FreedOp);
 
+  if (Function *F = CI.getCalledFunction()) {
+    if (F->getIntrinsicID() == Intrinsic::umin ||
+        F->getIntrinsicID() == Intrinsic::umax) {
+      for (Value *Arg : CI.args()) {
+        auto *SI = dyn_cast<SelectInst>(Arg);
+        if (!SI)
+          continue;
+
+        auto *TrueC = dyn_cast<Constant>(SI->getTrueValue());
+        auto *FalseC = dyn_cast<Constant>(SI->getFalseValue());
+
+        // Block only if the select is masking, e.g. select(cond, val, -1)
+        if ((TrueC && TrueC->isAllOnesValue()) ||
+            (FalseC && FalseC->isAllOnesValue())) {
+          LLVM_DEBUG(
+              dbgs()
+              << "InstCombine: skipping umin/umax folding for masked select\n");
+          return nullptr;
+        }
+      }
+    }
+  }
----------------
thevar1able wrote:

Maybe it'd be better to revert https://github.com/llvm/llvm-project/pull/116073?

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


More information about the llvm-commits mailing list