[llvm] [InstCombine] Canonicalize nsz NaN-fallback float clamp to minnum/maxnum (PR #213690)
Jun Yeong Kim via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 2 16:54:56 PDT 2026
================
@@ -4586,6 +4582,111 @@ static bool isSelectZeroSignInsignificant(SelectInst &SI) {
return true;
}
+/// Canonicalize an `nsz` NaN-fallback float clamp to `minnum`/`maxnum`:
+///
+/// below_lo_or_nan = fcmp ult %x, LO
+/// below_hi = fcmp olt %x, HI
+/// upper = select nsz below_hi, %x, HI
+/// ordered = fcmp ord %x, LO
+/// lower_or_fb = select nsz ordered, LO, %fallback
+/// result = select nsz below_lo_or_nan, lower_or_fb, upper
+///
+/// =>
+///
+/// isnan = fcmp uno %x, %x
+/// upperV = call nsz @llvm.minnum(%x, HI)
+/// clamped = call nsz @llvm.maxnum(upperV, LO)
+/// result = select isnan, %fallback, clamped
+///
+/// The match is deliberately narrow: exact predicates/operands/bounds, `nsz` on
+/// every value-producing select, `denormal-fp-math = ieee`, and one-use
+/// intermediate nodes so the original tree is fully removed. `nnan`/`ninf` are
+/// never inferred; NaN is handled explicitly.
+static Value *foldClampWithNaNFallback(SelectInst &Outer,
+ InstCombinerImpl &IC) {
+ InstCombiner::BuilderTy &Builder = IC.Builder;
+ // Only handle scalar/vector float selects. FPMathOperator also covers selects
+ // of homogeneous FP aggregates (e.g. { float, float }), whose scalar type has
+ // no fltSemantics, so restrict to isFPOrFPVectorTy() before querying
+ // fast-math flags or FP semantics below.
+ if (!Outer.getType()->isFPOrFPVectorTy() || !Outer.hasNoSignedZeros())
----------------
junyeong0619 wrote:
Removed it.
https://github.com/llvm/llvm-project/pull/213690
More information about the llvm-commits
mailing list