public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Roger Sayle <sayle@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-7621] PR middle-end/98420: Don't fold x - x to 0.0 with -frounding-math
Date: Sat, 12 Mar 2022 09:23:25 +0000 (GMT)	[thread overview]
Message-ID: <20220312092325.1CCB53858C27@sourceware.org> (raw)

https://gcc.gnu.org/g:72c243017dee611bf3c32ea7bfad6ac538021aaf

commit r12-7621-g72c243017dee611bf3c32ea7bfad6ac538021aaf
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Sat Mar 12 09:20:52 2022 +0000

    PR middle-end/98420: Don't fold x - x to 0.0 with -frounding-math
    
    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.

Diff:
---
 gcc/match.pd                   | 5 ++++-
 gcc/testsuite/gcc.dg/pr98420.c | 8 ++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 97399e580a4..8b44b5cc92c 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -229,13 +229,16 @@ 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)
+	  && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
+	      || !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 00000000000..c289b845d63
--- /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" } } */


                 reply	other threads:[~2022-03-12  9:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220312092325.1CCB53858C27@sourceware.org \
    --to=sayle@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).