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-7130] [PATCH] PR tree-optimization/104420: Fix checks for constant folding X*0.0
Date: Wed,  9 Feb 2022 14:24:00 +0000 (GMT)	[thread overview]
Message-ID: <20220209142400.EFB7E3858D28@sourceware.org> (raw)

https://gcc.gnu.org/g:2d3c477599b02b06e338acd5f5098ee7a3fe6176

commit r12-7130-g2d3c477599b02b06e338acd5f5098ee7a3fe6176
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Wed Feb 9 14:21:08 2022 +0000

    [PATCH] PR tree-optimization/104420: Fix checks for constant folding X*0.0
    
    This patch resolves PR tree-optimization/104420, which is a P1 regression
    where, as observed by Jakub Jelinek, the conditions for constant folding
    x*0.0 are incorrect (following my patch for PR tree-optimization/96392).
    The multiplication x*0.0 may yield a negative zero result, -0.0, if X is
    negative (not just if x may be negative zero).  Hence (without -ffast-math)
    (int)x*0.0 can't be optimized to 0.0, but (unsigned)x*0.0 can be constant
    folded.  This adds a bunch of test cases to confirm the desired behaviour,
    and removes an incorrect test from gcc.dg/pr96392.c which checked for the
    wrong behaviour.
    
    2022-02-09  Roger Sayle  <roger@nextmovesoftware.com>
    
    gcc/ChangeLog
            PR tree-optimization/104420
            * match.pd (mult @0 real_zerop): Tweak conditions for constant
            folding X*0.0 (or X*-0.0) to HONOR_SIGNED_ZEROS when appropriate.
    
    gcc/testsuite/ChangeLog
            PR tree-optimization/104420
            * gcc.dg/pr104420-1.c: New test case.
            * gcc.dg/pr104420-2.c: New test case.
            * gcc.dg/pr104420-3.c: New test case.
            * gcc.dg/pr104420-4.c: New test case.
            * gcc.dg/pr96392.c: Remove incorrect test.

Diff:
---
 gcc/match.pd                      |  3 +--
 gcc/testsuite/gcc.dg/pr104420-1.c | 10 ++++++++++
 gcc/testsuite/gcc.dg/pr104420-2.c | 10 ++++++++++
 gcc/testsuite/gcc.dg/pr104420-3.c | 10 ++++++++++
 gcc/testsuite/gcc.dg/pr104420-4.c | 10 ++++++++++
 gcc/testsuite/gcc.dg/pr96392.c    |  6 ------
 6 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 7bbb80172fc..4fe590983f3 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -262,8 +262,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (mult @0 real_zerop@1)
  (if (!tree_expr_maybe_nan_p (@0)
       && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
-      && !tree_expr_maybe_real_minus_zero_p (@0)
-      && !tree_expr_maybe_real_minus_zero_p (@1))
+      && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
   @1))
 
 /* In IEEE floating point, x*1 is not equivalent to x for snans.
diff --git a/gcc/testsuite/gcc.dg/pr104420-1.c b/gcc/testsuite/gcc.dg/pr104420-1.c
new file mode 100644
index 00000000000..48385fae0ba
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr104420-1.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-add-options ieee } */
+
+double f(int a)
+{
+  return a * 0.0;
+}
+
+/* { dg-final { scan-tree-dump " \\\* 0.0" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/pr104420-2.c b/gcc/testsuite/gcc.dg/pr104420-2.c
new file mode 100644
index 00000000000..49d01896722
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr104420-2.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-add-options ieee } */
+
+double f(int a)
+{
+  return a * -0.0;
+}
+
+/* { dg-final { scan-tree-dump " \\\* -0.0" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/pr104420-3.c b/gcc/testsuite/gcc.dg/pr104420-3.c
new file mode 100644
index 00000000000..962dfff9cd4
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr104420-3.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-add-options ieee } */
+
+double f(unsigned int a)
+{
+  return a * 0.0;
+}
+
+/* { dg-final { scan-tree-dump "return 0.0" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/pr104420-4.c b/gcc/testsuite/gcc.dg/pr104420-4.c
new file mode 100644
index 00000000000..95ed0cc18dc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr104420-4.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-add-options ieee } */
+
+double f(unsigned int a)
+{
+  return a * -0.0;
+}
+
+/* { dg-final { scan-tree-dump "return -0.0" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/pr96392.c b/gcc/testsuite/gcc.dg/pr96392.c
index 662bacb62bc..fb7de217f96 100644
--- a/gcc/testsuite/gcc.dg/pr96392.c
+++ b/gcc/testsuite/gcc.dg/pr96392.c
@@ -12,11 +12,6 @@ double sub0(int x)
   return x - 0.0;
 }
 
-double mult0(int x)
-{
-  return 0.0 * x;
-}
-
 double negate(int x)
 {
   return 0.0 - x;
@@ -29,5 +24,4 @@ double subtract(int x)
 
 /* { dg-final { scan-tree-dump-not " \\+ " "optimized" } } */
 /* { dg-final { scan-tree-dump-not " \\- " "optimized" } } */
-/* { dg-final { scan-tree-dump-not " \\* " "optimized" } } */


                 reply	other threads:[~2022-02-09 14:24 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=20220209142400.EFB7E3858D28@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).