[middle-end] PR 12800[47]: x - x may be -0.0 with FE_DOWNWARD rounding.
Checks
| Context |
Check |
Description |
| linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_gcc_build--master-arm |
fail
|
Patch failed to apply
|
Commit Message
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).
This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
and make -k check, both with and without --target_board=unix{-m32}
with no new failures. Ok for mainline?
2026-11-04 Roger Sayle <roger@nextmovesoftware.com>
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.
Thanks in advance,
Roger
--
Comments
On Wed, Nov 4, 2026 at 3:50 PM Roger Sayle <roger@nextmovesoftware.com> wrote:
>
> 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).
>
> This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
> and make -k check, both with and without --target_board=unix{-m32}
> with no new failures. Ok for mainline?
OK.
Thanks,
Richard.
>
> 2026-11-04 Roger Sayle <roger@nextmovesoftware.com>
>
> 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.
>
> Thanks in advance,
> Roger
> --
>
@@ -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);
@@ -3473,12 +3473,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
@@ -6014,11 +6018,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);
new file mode 100644
@@ -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;
+}
new file mode 100644
@@ -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;
+}