https://gcc.gnu.org/g:252d2c0abfcc7633fe63ecacbc3cd535b2a58dc2

commit r17-5949-g252d2c0abfcc7633fe63ecacbc3cd535b2a58dc2
Author: Roger Sayle <[email protected]>
Date:   Wed Nov 4 18:15:19 2026 +0000

    middle-end: PR 12800[47]: x - x may be -0.0 with FE_DOWNWARD rounding.
    
    This patch addresses PR middle-end/128004 and PR rtl-optimization/128007
    which is effectively the same signed-zero and rounding-math corner case
    present in fold-const.cc and simplify-rtx.cc respectively.  With
    -frounding-math and FE_DOWNWARD rounding, when x is finite (and not NaN),
    x - x is not +0.0 but -0.0 when honoring signed zeros (the default).
    
    2026-11-04  Roger Sayle  <[email protected]>
    
    gcc/ChangeLog
            PR middle-end/128004
            PR rtl-optimization/128007
            * fold-const.cc (const_binop): Don't fold subtractions when
            the exact result is known to be zero with -frounding-math when
            we honor signed zeros.  The result is -0.0 with FE_DOWNWARD.
            * simplify-rtx.cc (simplify_binary_operation_1): Likewise, don't
            simplify x-x to +0.0 in the RTL optimizers in the same conditions.
            (simplify_const_binary_operation): Likewise, don't fold RTL
            subtractions when the exact result is zero in the same conditions.
    
    gcc/testsuite/ChangeLog
            PR middle-end/128004
            PR rtl-optimization/128007
            * gcc.dg/pr128004.c: New test case.
            * gcc.dg/pr128007.c: Likewise.

Diff:
---
 gcc/fold-const.cc               |  8 ++++++--
 gcc/simplify-rtx.cc             | 20 ++++++++++++++------
 gcc/testsuite/gcc.dg/pr128004.c | 18 ++++++++++++++++++
 gcc/testsuite/gcc.dg/pr128007.c | 18 ++++++++++++++++++
 4 files changed, 56 insertions(+), 8 deletions(-)

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 047c7dcada4d..a37026e1be76 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -1506,10 +1506,14 @@ const_binop (enum tree_code code, tree arg1, tree arg2)
       /* Don't constant fold this floating point operation if the
         result may dependent upon the run-time rounding mode and
         flag_rounding_math is set, or if GCC's software emulation
-        is unable to accurately represent the result.  */
+        is unable to accurately represent the result.  When the
+        result is known to be exactly zero, rounding may affect
+        the sign for modes that support signed zeros.  */
       if ((flag_rounding_math
           || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
-         && (inexact || !real_identical (&result, &value)))
+         && (inexact
+             || !real_identical (&result, &value)
+             || (real_iszero (&value) && HONOR_SIGNED_ZEROS (mode))))
        return NULL_TREE;
 
       t = build_real (type, result);
diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc
index 3ad51cec7139..e979d4c29641 100644
--- a/gcc/simplify-rtx.cc
+++ b/gcc/simplify-rtx.cc
@@ -3524,12 +3524,16 @@ simplify_context::simplify_binary_operation_1 (rtx_code 
code,
       break;
 
     case MINUS:
-      /* We can't assume x-x is 0 even with non-IEEE floating point,
-        but since it is zero except in very strange circumstances, we
-        will treat it as zero with -ffinite-math-only.  */
+      /* We can't assume x-x is 0 even with non-IEEE floating point.
+        This is unsafe if x is NaN or Inf, and with FE_DOWNWARD as the
+        rounding mode x - x may be -0.0.  c.f. PR middle-end/98420.  */
       if (rtx_equal_p (trueop0, trueop1)
          && ! side_effects_p (op0)
-         && (!FLOAT_MODE_P (mode) || !HONOR_NANS (mode)))
+         && (!FLOAT_MODE_P (mode)
+             || (!HONOR_NANS (mode)
+                 && !HONOR_INFINITIES (mode)
+                 && (!HONOR_SIGN_DEPENDENT_ROUNDING (mode)
+                     || !HONOR_SIGNED_ZEROS (mode)))))
        return CONST0_RTX (mode);
 
       /* Change subtraction from zero into negation.  (0 - x) is the
@@ -6072,11 +6076,15 @@ simplify_const_binary_operation (enum rtx_code code, 
machine_mode mode,
          /* Don't constant fold this floating point operation if the
             result may dependent upon the run-time rounding mode and
             flag_rounding_math is set, or if GCC's software emulation
-            is unable to accurately represent the result.  */
+            is unable to accurately represent the result. When the
+            result is known to be exactly zero, rounding may affect
+            the sign for modes that support signed zeros.  */
 
          if ((flag_rounding_math
               || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
-             && (inexact || !real_identical (&result, &value)))
+             && (inexact
+                 || !real_identical (&result, &value)
+                 || (real_iszero (&value) && HONOR_SIGNED_ZEROS (mode))))
            return NULL_RTX;
 
          return const_double_from_real_value (result, mode);
diff --git a/gcc/testsuite/gcc.dg/pr128004.c b/gcc/testsuite/gcc.dg/pr128004.c
new file mode 100644
index 000000000000..8666837a0fea
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr128004.c
@@ -0,0 +1,18 @@
+/* PR middle-end/128004 */
+/* { dg-do run } */
+/* { dg-require-effective-target fenv } */
+/* { dg-require-effective-target hard_float } */
+/* { dg-options "-O2 -frounding-math" } */
+
+#include <fenv.h>
+
+int main(void) {
+#ifdef FE_DOWNWARD
+  double a = 1.0, b = -1.0;
+  fesetround(FE_DOWNWARD);
+  double r = a + b;
+  if (!__builtin_signbit(r))
+    __builtin_abort();
+#endif
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/pr128007.c b/gcc/testsuite/gcc.dg/pr128007.c
new file mode 100644
index 000000000000..c134f6e16260
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr128007.c
@@ -0,0 +1,18 @@
+/* PR rtl-optimization/128007 */
+/* { dg-do run } */
+/* { dg-require-effective-target fenv } */
+/* { dg-require-effective-target hard_float } */
+/* { dg-options "-O2 -frounding-math -ffinite-math-only" } */
+
+#include <fenv.h>
+
+__attribute__((noipa)) double f(double x) { return x - x; }
+
+int main(void) {
+#ifdef FE_DOWNWARD
+  fesetround(FE_DOWNWARD);
+  if (!__builtin_signbit(f(3.0)))
+    __builtin_abort();
+#endif
+  return 0;
+}

Reply via email to