public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] PR middle-end/98420: Don't fold x - x to 0.0 with -frounding-math
@ 2022-03-10 23:31 Roger Sayle
  2022-03-11 11:16 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Roger Sayle @ 2022-03-10 23:31 UTC (permalink / raw)
  To: 'GCC Patches'

[-- Attachment #1: Type: text/plain, Size: 1122 bytes --]


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

Technically, this is a regression from GCC 4.1 (according to godbolt.org)
so hopefully this patch is suitable during stage4.

This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
and make -k check with no new failures.  Ok for mainline?


2022-03-10  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.


Thanks in advance,
Roger
--


[-- Attachment #2: patchrm.txt --]
[-- Type: text/plain, Size: 1235 bytes --]

diff --git a/gcc/match.pd b/gcc/match.pd
index 97399e5..3fe53d1 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -229,13 +229,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 /* 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)
+	  && (!flag_rounding_math || !HONOR_SIGNED_ZEROS (type))))
   { build_zero_cst (type); }))
 (simplify
  (pointer_diff @@0 @0)
diff --git a/gcc/testsuite/gcc.dg/pr98420.c b/gcc/testsuite/gcc.dg/pr98420.c
new file mode 100644
index 0000000..c289b84
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr98420.c
@@ -0,0 +1,8 @@
+/* { 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" } } */

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] PR middle-end/98420: Don't fold x - x to 0.0 with -frounding-math
  2022-03-10 23:31 [PATCH] PR middle-end/98420: Don't fold x - x to 0.0 with -frounding-math Roger Sayle
@ 2022-03-11 11:16 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2022-03-11 11:16 UTC (permalink / raw)
  To: Roger Sayle; +Cc: GCC Patches

On Fri, Mar 11, 2022 at 12:31 AM Roger Sayle <roger@nextmovesoftware.com> wrote:
>
>
> 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
>
> Technically, this is a regression from GCC 4.1 (according to godbolt.org)
> so hopefully this patch is suitable during stage4.
>
> This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
> and make -k check with no new failures.  Ok for mainline?

+         && !tree_expr_maybe_infinite_p (@0)
+         && (!flag_rounding_math || !HONOR_SIGNED_ZEROS (type))))
   { build_zero_cst (type); }))

HONOR_SIGN_DEPENDENT_ROUNDING (type) instead of flag_rounding_math?

OK with that change.

Richard.

>
> 2022-03-10  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.
>
>
> Thanks in advance,
> Roger
> --
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-03-11 11:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-10 23:31 [PATCH] PR middle-end/98420: Don't fold x - x to 0.0 with -frounding-math Roger Sayle
2022-03-11 11:16 ` Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).