]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
match.pd: Don't fold nan < x etc. for -ftrapping-math [PR106805]
authorJakub Jelinek <jakub@redhat.com>
Mon, 5 Dec 2022 10:54:45 +0000 (11:54 +0100)
committerJakub Jelinek <jakub@redhat.com>
Mon, 5 Dec 2022 10:54:45 +0000 (11:54 +0100)
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.

gcc/match.pd
gcc/testsuite/c-c++-common/pr57371-4.c
gcc/testsuite/c-c++-common/pr57371-5.c [new file with mode: 0644]
gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-6.x
gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.c [new file with mode: 0644]
gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.x [new file with mode: 0644]

index 04d4c4c054ae4a162aa8a8160cfc354b84eb9b3b..f48cbd9b73baacf1c0368fb9a42728bbcc6adf2d 100644 (file)
@@ -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); })
index d938ecdd675c3eabea2fceb724525351e45b7f28..f43f7c22419a56661ebcb284fa648f478be1833f 100644 (file)
@@ -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 (file)
index 0000000..8e18b0a
--- /dev/null
@@ -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" } } */
+  }
+}
index e7c051d8fe066ea11f8bfc6c14c4b2dc37a4d8d0..6655a07ab3c4e2729f784aad6f8711aee3b499a5 100644 (file)
@@ -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 (file)
index 0000000..7172634
--- /dev/null
@@ -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 (file)
index 0000000..e7c051d
--- /dev/null
@@ -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