public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-4493] match.pd: Don't fold nan < x etc. for -ftrapping-math [PR106805]
@ 2022-12-05 10:55 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2022-12-05 10:55 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:109148dd16e4bcd50faee19c49082de69d0ba26e

commit r13-4493-g109148dd16e4bcd50faee19c49082de69d0ba26e
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Dec 5 11:54:45 2022 +0100

    match.pd: Don't fold nan < x etc. for -ftrapping-math [PR106805]
    
    As reported in the PR, the following pr106805.c testcase is miscompiled
    with the default -ftrapping-math, because we fold all the comparisons into
    constants and don't raise any exceptions.
    
    The match.pd pattern handles just simple comparisons, from those
    EQ/NE are quiet and don't raise exceptions on anything but sNaN, while
    GT/GE/LT/LE are signaling and do raise exceptions even on qNaN.
    
    fold_relational_const handles this IMHO correctly:
          /* Handle the cases where either operand is a NaN.  */
          if (real_isnan (c0) || real_isnan (c1))
            {
              switch (code)
                {
                case EQ_EXPR:
                case ORDERED_EXPR:
                  result = 0;
                  break;
    
                case NE_EXPR:
                case UNORDERED_EXPR:
                case UNLT_EXPR:
                case UNLE_EXPR:
                case UNGT_EXPR:
                case UNGE_EXPR:
                case UNEQ_EXPR:
                  result = 1;
                  break;
    
                case LT_EXPR:
                case LE_EXPR:
                case GT_EXPR:
                case GE_EXPR:
                case LTGT_EXPR:
                  if (flag_trapping_math)
                    return NULL_TREE;
                  result = 0;
                  break;
    
                default:
                  gcc_unreachable ();
                }
    
              return constant_boolean_node (result, type);
            }
    by folding the signaling comparisons only if -fno-trapping-math.
    The following patch does the same in match.pd.
    
    Unfortunately the pr106805.c testcase still fails, but no longer because of
    match.pd, but on the trunk because of the still unresolved ranger problems
    (same issue as for fold-overflow-1.c etc.) and on 12 branch (and presumably
    trunk too) somewhere during expansion the comparisons are also expanded
    into constants (which is ok for -fno-trapping-math, but not ok with that).
    
    Though, I think the patch is a small step in the direction, so I'd like
    to commit this patch without the gcc.dg/pr106805.c testcase for now.
    
    2022-12-05  Jakub Jelinek  <jakub@redhat.com>
    
            PR middle-end/106805
            * match.pd (cmp @0 REAL_CST@1): Don't optimize x cmp NaN
            or NaN cmp x to false/true for cmp >/>=/</<= if -ftrapping-math.
    
            * c-c++-common/pr57371-4.c: Revert 2021-09-19 changes.
            * c-c++-common/pr57371-5.c: New test.
            * gcc.c-torture/execute/ieee/fp-cmp-6.x: Add -fno-trapping-math.
            * gcc.c-torture/execute/ieee/fp-cmp-9.c: New test.
            * gcc.c-torture/execute/ieee/fp-cmp-9.x: New file.

Diff:
---
 gcc/match.pd                                       |  2 +
 gcc/testsuite/c-c++-common/pr57371-4.c             |  8 ++--
 gcc/testsuite/c-c++-common/pr57371-5.c             | 47 ++++++++++++++++++++++
 .../gcc.c-torture/execute/ieee/fp-cmp-6.x          |  1 +
 .../gcc.c-torture/execute/ieee/fp-cmp-9.c          | 31 ++++++++++++++
 .../gcc.c-torture/execute/ieee/fp-cmp-9.x          | 16 ++++++++
 6 files changed, 101 insertions(+), 4 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 04d4c4c054a..f48cbd9b73b 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5146,12 +5146,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
    /* x != NaN is always true, other ops are always false.  */
    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
+	&& (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
 	&& !tree_expr_signaling_nan_p (@1)
 	&& !tree_expr_maybe_signaling_nan_p (@0))
     { constant_boolean_node (cmp == NE_EXPR, type); })
    /* NaN != y is always true, other ops are always false.  */
    (if (TREE_CODE (@0) == REAL_CST
 	&& REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
+	&& (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
         && !tree_expr_signaling_nan_p (@0)
         && !tree_expr_signaling_nan_p (@1))
     { constant_boolean_node (cmp == NE_EXPR, type); })
diff --git a/gcc/testsuite/c-c++-common/pr57371-4.c b/gcc/testsuite/c-c++-common/pr57371-4.c
index d938ecdd675..f43f7c22419 100644
--- a/gcc/testsuite/c-c++-common/pr57371-4.c
+++ b/gcc/testsuite/c-c++-common/pr57371-4.c
@@ -13,25 +13,25 @@ void nonfinite(unsigned short x) {
   {
     volatile int nonfinite_1;
     nonfinite_1 = (float) x > QNAN;
-    /* { dg-final { scan-tree-dump "nonfinite_1 = 0" "original" } } */
+    /* { dg-final { scan-tree-dump "nonfinite_1 = \\(float\\)" "original" } } */
   }
 
   {
     volatile int nonfinite_2;
     nonfinite_2 = (float) x >= QNAN;
-    /* { dg-final { scan-tree-dump "nonfinite_2 = 0" "original" } } */
+    /* { dg-final { scan-tree-dump "nonfinite_2 = \\(float\\)" "original" } } */
   }
 
   {
     volatile int nonfinite_3;
     nonfinite_3 = (float) x < QNAN;
-    /* { dg-final { scan-tree-dump "nonfinite_3 = 0" "original" } } */
+    /* { dg-final { scan-tree-dump "nonfinite_3 = \\(float\\)" "original" } } */
   }
 
   {
     volatile int nonfinite_4;
     nonfinite_4 = (float) x <= QNAN;
-    /* { dg-final { scan-tree-dump "nonfinite_4 = 0" "original" } } */
+    /* { dg-final { scan-tree-dump "nonfinite_4 = \\(float\\)" "original" } } */
   }
 
   {
diff --git a/gcc/testsuite/c-c++-common/pr57371-5.c b/gcc/testsuite/c-c++-common/pr57371-5.c
new file mode 100644
index 00000000000..8e18b0a7313
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/pr57371-5.c
@@ -0,0 +1,47 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fno-signaling-nans -fno-trapping-math -fdump-tree-original" } */
+
+/* We can not get rid of comparison in tests below because of
+   pending NaN exceptions.
+
+   TODO: avoid under -fno-trapping-math.  */
+
+#define QNAN __builtin_nanf ("0")
+
+void nonfinite(unsigned short x) {
+  {
+    volatile int nonfinite_1;
+    nonfinite_1 = (float) x > QNAN;
+    /* { dg-final { scan-tree-dump "nonfinite_1 = 0" "original" } } */
+  }
+
+  {
+    volatile int nonfinite_2;
+    nonfinite_2 = (float) x >= QNAN;
+    /* { dg-final { scan-tree-dump "nonfinite_2 = 0" "original" } } */
+  }
+
+  {
+    volatile int nonfinite_3;
+    nonfinite_3 = (float) x < QNAN;
+    /* { dg-final { scan-tree-dump "nonfinite_3 = 0" "original" } } */
+  }
+
+  {
+    volatile int nonfinite_4;
+    nonfinite_4 = (float) x <= QNAN;
+    /* { dg-final { scan-tree-dump "nonfinite_4 = 0" "original" } } */
+  }
+
+  {
+    volatile int nonfinite_11;
+    nonfinite_11 = (float) x == QNAN;
+    /* { dg-final { scan-tree-dump "nonfinite_11 = 0" "original" } } */
+  }
+
+  {
+    volatile int nonfinite_12;
+    nonfinite_12 = (float) x != QNAN;
+    /* { dg-final { scan-tree-dump "nonfinite_12 = 1" "original" } } */
+  }
+}
diff --git a/gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-6.x b/gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-6.x
index e7c051d8fe0..6655a07ab3c 100644
--- a/gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-6.x
+++ b/gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-6.x
@@ -1,3 +1,4 @@
+lappend additional_flags "-fno-trapping-math"
 # The ARM VxWorks kernel uses an external floating-point library in
 # which routines like __ledf2 are just aliases for __cmpdf2.  These
 # routines therefore don't handle NaNs correctly.
diff --git a/gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.c b/gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.c
new file mode 100644
index 00000000000..71726346848
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.c
@@ -0,0 +1,31 @@
+
+const double dnan = 1.0/0.0 - 1.0/0.0;
+double x = 1.0;
+
+extern void link_error (void);
+extern void abort (void);
+
+main ()
+{
+#if ! defined (__vax__) && ! defined (_CRAY)
+  /* NaN is an IEEE unordered operand.  All these test should be false.  */
+  if (dnan == dnan)
+    link_error ();
+  if (dnan != x)
+    x = 1.0;
+  else
+    link_error ();
+
+  if (dnan == x)
+    link_error ();
+#endif
+  exit (0);
+}
+
+#ifndef __OPTIMIZE__
+void link_error (void)
+{
+  abort ();
+}
+#endif
+
diff --git a/gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.x b/gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.x
new file mode 100644
index 00000000000..e7c051d8fe0
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.x
@@ -0,0 +1,16 @@
+# The ARM VxWorks kernel uses an external floating-point library in
+# which routines like __ledf2 are just aliases for __cmpdf2.  These
+# routines therefore don't handle NaNs correctly.
+if [istarget "arm*-*-vxworks*"] {
+    set torture_eval_before_execute {
+	global compiler_conditional_xfail_data
+	set compiler_conditional_xfail_data {
+	    "The ARM kernel uses a flawed floating-point library."
+	    { "*-*-*" }
+	    { "-O0" }
+	    { "-mrtp" }
+	}
+    }
+}
+
+return 0

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-12-05 10:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-05 10:55 [gcc r13-4493] match.pd: Don't fold nan < x etc. for -ftrapping-math [PR106805] Jakub Jelinek

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).