[llvm] [DAGCombiner] Fold smax(X, -1) to or(X, ashr(X, BW-1)) for code size (PR #206242)
Aayush Shrivastava via llvm-commits
llvm-commits at lists.llvm.org
Sat Jun 27 11:44:40 PDT 2026
iamaayushrivastava wrote:
> WAIT!
>
> if (CC == ISD::SETGT && isAllOnesOrAllOnesSplat(CondC) && isAllOnesOrAllOnesSplat(C2)) { // i32 X > -1 ? C1 : -1 --> (X >>s 31) | C1 SDValue ShAmtC = DAG.getConstant(X.getScalarValueSizeInBits() - 1, DL, VT); SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, X, ShAmtC); return DAG.getNode(ISD::OR, DL, VT, Sra, C1); }
>
> exists though in dagcombiner.
Thanks for the pointer! That's `foldSelectOfConstantsUsingSra`, but it bails out early on line 12705 if either branch is not a constant (`isConstantOrConstantVector`). For `smax(X, -1)`, the true branch is X itself (a variable), so it's never reached. Our fold in `visitIMINMAX` handles the variable-true-branch case directly on the `SMAX` node, which that function doesn't cover. The two patterns are complementary, not overlapping.
https://github.com/llvm/llvm-project/pull/206242
More information about the llvm-commits
mailing list