public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Xi Ruoyao <xry111@mengyan1223.wang>
To: Richard Biener <richard.guenther@gmail.com>
Cc: "Joseph S. Myers" <joseph@codesourcery.com>,
	GCC Patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH] fold-const: do not fold NaN result from non-NaN operands (was: Re: [PATCH] fold-const: do not fold 'inf/inf' with -ftrapping-math [PR95115])
Date: Tue, 01 Feb 2022 13:10:54 +0800	[thread overview]
Message-ID: <a426ae642b4d40ef37dffecedad462fd72a5fa61.camel@mengyan1223.wang> (raw)
In-Reply-To: <CAFiYyc133MUJnnSiwLnpVGn4ci-AJuyhVWQHENTOdCY_H_006w@mail.gmail.com>

Bootstrapped and tested on x86_64-linux-gnu.  Ok for trunk, gcc-11, and
gcc-10?  (I'd consider PR 95115 a 10/11/12 regression because GCC
10/11/12 miscompiles glibc acos/asin function, but not GCC 9.)

These operations should raise an invalid operation exception at runtime.
So they should not be folded during compilation unless -fno-trapping-math
is used.

gcc/
	PR middle-end/95115
	* fold-const.cc (const_binop): Do not fold NaN result from
	  non-NaN operands.

gcc/testsuite
	* gcc.dg/pr95115.c: New test.
---
 gcc/fold-const.cc              | 11 +++++++++++
 gcc/testsuite/gcc.dg/pr95115.c | 25 +++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr95115.c

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index b155611578d..8fc01cdfb77 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -1305,6 +1305,17 @@ const_binop (enum tree_code code, tree arg1, tree arg2)
       inexact = real_arithmetic (&value, code, &d1, &d2);
       real_convert (&result, mode, &value);
 
+      /* Don't constant fold this floating point operation if
+	 both operands are not NaN but the result is NaN, and
+	 flag_trapping_math.  Such operations should raise an
+	 invalid operation exception.  */
+      if (flag_trapping_math
+	  && MODE_HAS_NANS (mode)
+	  && REAL_VALUE_ISNAN (result)
+	  && !REAL_VALUE_ISNAN (d1)
+	  && !REAL_VALUE_ISNAN (d2))
+	return NULL_TREE;
+
       /* Don't constant fold this floating point operation if
 	 the result has overflowed and flag_trapping_math.  */
       if (flag_trapping_math
diff --git a/gcc/testsuite/gcc.dg/pr95115.c b/gcc/testsuite/gcc.dg/pr95115.c
new file mode 100644
index 00000000000..46a95dfb698
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr95115.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -ftrapping-math" } */
+/* { dg-add-options ieee } */
+/* { dg-require-effective-target fenv_exceptions } */
+
+#include <fenv.h>
+#include <stdlib.h>
+
+double
+x (void)
+{
+  double d = __builtin_inf ();
+  return d / d;
+}
+
+int
+main (void)
+{
+  double r = x ();
+  if (!__builtin_isnan (r))
+	abort ();
+  if (!fetestexcept (FE_INVALID))
+	abort ();
+  exit (0);
+}
-- 
2.35.1



  reply	other threads:[~2022-02-01  5:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-30 18:39 [PATCH] fold-const: do not fold 'inf/inf' with -ftrapping-math [PR95115] Xi Ruoyao
2022-01-31  9:35 ` Richard Biener
2022-01-31 10:07   ` Xi Ruoyao
2022-01-31 14:57     ` Richard Biener
2022-02-01  5:10       ` Xi Ruoyao [this message]
2022-02-01  7:19         ` [PATCH] fold-const: do not fold NaN result from non-NaN operands (was: Re: [PATCH] fold-const: do not fold 'inf/inf' with -ftrapping-math [PR95115]) Richard Biener
2022-01-31 23:32   ` [PATCH] fold-const: do not fold 'inf/inf' with -ftrapping-math [PR95115] Joseph Myers

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=a426ae642b4d40ef37dffecedad462fd72a5fa61.camel@mengyan1223.wang \
    --to=xry111@mengyan1223.wang \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=joseph@codesourcery.com \
    --cc=richard.guenther@gmail.com \
    /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).