[llvm] [SelectionDAG] Fix isKnownNeverNaN OR-logic for FMINNUM/FMAXNUM with SNaN (PR #204736)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 26 03:01:24 PDT 2026


================
@@ -6423,10 +6423,17 @@ bool SelectionDAG::isKnownNeverNaN(SDValue Op, const APInt &DemandedElts,
   case ISD::FMAXNUM:
   case ISD::FMINIMUMNUM:
   case ISD::FMAXIMUMNUM: {
-    // Only one needs to be known not-nan, since it will be returned if the
-    // other ends up being one.
-    return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
-           isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
+    if (!SNaN)
+      // The result is a NaN only if both operands are NaN, so it is never NaN
+      // if either operand is known to never be NaN.
+      return isKnownNeverNaN(Op.getOperand(0), DemandedElts, false,
+                             Depth + 1) ||
+             isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1);
+    // These do not quiet a signaling NaN: if either operand may be a signaling
+    // NaN it can be returned unchanged, so both operands must be known to never
+    // be a signaling NaN.
+    return isKnownNeverNaN(Op.getOperand(0), DemandedElts, true, Depth + 1) &&
+           isKnownNeverNaN(Op.getOperand(1), DemandedElts, true, Depth + 1);
----------------
jayfoad wrote:

I think this works, but it could be made more precise.

The result is not snan if:
- both operands are not snan (which is what you check here), or
- either operand is not nan (which is what you check above but _only_ in the !SNaN case)

So I would suggest restructuring this code as:
```
  if (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) ||
      isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1))
    return true;
  if (SNan &&
      isKnownNeverNaN(Op.getOperand(0), DemandedElts, true, Depth + 1) &&
      isKnownNeverNaN(Op.getOperand(1), DemandedElts, true, Depth + 1))
    return true;
  return false;
```
Unless that's going to be too expensive in terms of compile time?

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


More information about the llvm-commits mailing list