https://gcc.gnu.org/g:6e7cf1f7a5f59c033176fcd7074af53d01ad75ea

commit r17-4952-g6e7cf1f7a5f59c033176fcd7074af53d01ad75ea
Author: Roger Sayle <[email protected]>
Date:   Wed Oct 7 09:41:46 2026 +0100

    PR rtl-optimization/126521: Don't generate ABS in RTL ifcvt with NaNs.
    
    This patch addresses PR rtl-optimization/126521 where ifcvt transforms
    x < 0 ? -x : x (and x < 0 ? x : -x) into ABS (and FABS respectively).
    Unfortunately, these transformations are unsafe in the presence of NaNs.
    The simple fix is a one-line change to noce_try_abs.
    
    2026-10-07  Roger Sayle  <[email protected]>
    
    gcc/ChangeLog
            PR rtl-optimization/126521
            * ifcvt.cc (noce_try_abs): These conversions are unsafe in the
            presence of NaNs.
    
    gcc/testsuite/ChangeLog
            PR rtl-optimization/126521
            * gcc.target/i386/pr126521.c: New test case.

Diff:
---
 gcc/ifcvt.cc                             |  5 +++--
 gcc/testsuite/gcc.target/i386/pr126521.c | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/gcc/ifcvt.cc b/gcc/ifcvt.cc
index ab9d35699cba..fad341ead6cf 100644
--- a/gcc/ifcvt.cc
+++ b/gcc/ifcvt.cc
@@ -3166,8 +3166,9 @@ noce_try_abs (noce_if_info *if_info)
   if (!noce_simple_bbs (if_info))
     return false;
 
-  /* Reject modes with signed zeros.  */
-  if (HONOR_SIGNED_ZEROS (if_info->x))
+  /* Reject modes with signed zeros or NaNs.  */
+  if (HONOR_SIGNED_ZEROS (if_info->x)
+      || HONOR_NANS (if_info->x))
     return false;
 
   /* Recognize A and B as constituting an ABS or NABS.  The canonical
diff --git a/gcc/testsuite/gcc.target/i386/pr126521.c 
b/gcc/testsuite/gcc.target/i386/pr126521.c
new file mode 100644
index 000000000000..0b49bd36bb9d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr126521.c
@@ -0,0 +1,14 @@
+/* { dg-do compile { target { ! ia32 } } } */
+/* { dg-options "-O2 -fno-signed-zeros -fno-ssa-phiopt" } */
+
+double g (double a)
+{
+  double x;
+  if (a < 0.0)
+    x = a;
+  else
+    x = -a;
+  return x;
+}
+
+/* { dg-final { scan-assembler "cmpnlt" } } */

Reply via email to