[llvm] [InstCombine] Avoid folding `select(umin(X, Y), X)` with non-constant mask (PR #143020)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 5 12:44:44 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;
+ }
+ }
+ }
+ }
----------------
nikic wrote:
The fold you are actually interested in is located at https://github.com/llvm/llvm-project/blob/a19889c3952ed70d93aa40e36e38404e531f1ce1/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp#L3870-L3879 If you want to add extra checks, they should be added there or inside FoldOpIntoSelect.
Though I'm not entirely sure precisely what we should be restricting.
https://github.com/llvm/llvm-project/pull/143020
More information about the llvm-commits
mailing list