[llvm] [InstCombine] Preserve signbit semantics of NaN with fold to fabs (PR #136648)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 23 04:46:55 PDT 2025


================
@@ -2793,7 +2793,11 @@ static Instruction *foldSelectWithFCmpToFabs(SelectInst &SI,
 
     // fold (X <= +/-0.0) ? (0.0 - X) : X to fabs(X), when 'Swap' is false
     // fold (X >  +/-0.0) ? X : (0.0 - X) to fabs(X), when 'Swap' is true
-    if (match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(X)))) {
+    // Note: We require "nnan" for this fold because fcmp ignores the signbit
+    //       of NAN, but IEEE-754 specifies the signbit of NAN values with
+    //       fneg/fabs operations.
+    if (cast<FPMathOperator>(CondVal)->hasNoNaNs() &&
+        match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(X)))) {
----------------
dtcxzyw wrote:

> Can't you equivalently check the nnan on the fsub?

fsub may not be selected. https://alive2.llvm.org/ce/z/yyffzh


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


More information about the llvm-commits mailing list