This patch addresses PR middle-end/98420, which is inappropriate constant
folding of x - x to 0.0 (in match.pd) when -frounding-math is specified.
Specifically, x - x may be -0.0 with FE_DOWNWARD as the rounding mode.
To summarize, the desired IEEE behaviour, x - x for floating point x,
(1) can't be folded to 0.0 by default, due to the possibility of NaN or Inf
(2) can be folded to 0.0 with -ffinite-math-only
(3) can't be folded to 0.0 with -ffinite-math-only -frounding-math
(4) can be folded with -ffinite-math-only -frounding-math -fno-signed-zeros
2022-03-12 Roger Sayle <roger@nextmovesoftware.com>
gcc/ChangeLog
PR middle-end/98420
* match.pd (minus @0 @0): Additional checks for -fno-rounding-math
(the defaut) or -fno-signed-zeros.
gcc/testsuite/ChangeLog
PR middle-end/98420
* gcc.dg/pr98420.c: New test case.
/* Simplify x - x.
This is unsafe for certain floats even in non-IEEE formats.
In IEEE, it is unsafe because it does wrong for NaNs.
+ PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
Also note that operand_equal_p is always false if an operand
is volatile. */
(simplify
(minus @0 @0)
(if (!FLOAT_TYPE_P (type)
|| (!tree_expr_maybe_nan_p (@0)
- && !tree_expr_maybe_infinite_p (@0)))
+ && !tree_expr_maybe_infinite_p (@0)
+ && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
+ || !HONOR_SIGNED_ZEROS (type))))
{ build_zero_cst (type); }))
(simplify
(pointer_diff @@0 @0)
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O2 -ffinite-math-only -frounding-math -fdump-tree-optimized" } */
+double foo (double a)
+{
+ return a - a;
+}
+
+/* { dg-final { scan-tree-dump " = a_\[0-9\]\\(D\\) - a_\[0-9\]\\(D\\);" "optimized" } } */