[llvm] [ValueTracking] Fix incorrect FMF propagation from select to fcmp (PR #195787)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri May 15 06:41:03 PDT 2026


================
@@ -8922,14 +8921,21 @@ static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred,
   SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
   bool Ordered = false;
 
+  auto NaNFMFFor = [&](const Value *Op) {
+    FastMathFlags Res = CmpFMF;
+    if ((Op == TrueVal || Op == FalseVal) && FMF.noNaNs())
+      Res.setNoNaNs(true);
+    return Res;
+  };
+
   // When given one NaN and one non-NaN input:
   //   - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
   //   - A simple C99 (a < b ? a : b) construction will return 'b' (as the
   //     ordered comparison fails), which could be NaN or non-NaN.
   // so here we discover exactly what NaN behavior is required/accepted.
   if (CmpInst::isFPPredicate(Pred)) {
-    bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
-    bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
+    bool LHSSafe = isKnownNonNaN(CmpLHS, NaNFMFFor(CmpLHS));
+    bool RHSSafe = isKnownNonNaN(CmpRHS, NaNFMFFor(CmpRHS));
----------------
arsenm wrote:

Mutating the flags to pass them into isKnownNonNaN is confusing; that function just checks the flag. (I'm also wondering why we have isKnownNonNaN, it's a simpler version if isKnownNeverNaN?)

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


More information about the llvm-commits mailing list