https://gcc.gnu.org/g:030186cabe8128e752619e101768cf8823a42c38

commit r15-2132-g030186cabe8128e752619e101768cf8823a42c38
Author: Roger Sayle <ro...@nextmovesoftware.com>
Date:   Thu Jul 18 08:27:36 2024 +0100

    Implement a -ftrapping-math/-fsignaling-nans TODO in match.pd.
    
    I've been investigating some (float)i == CST optimizations for match.pd,
    and noticed there's already a TODO comment in match.pd that's relatively
    easy to implement.  When CST is a NaN, we only need to worry about
    exceptions with flag_trapping_math, and equality/inequality tests for
    sNaN only behave differently to qNaN with -fsignaling-nans.  These
    issues are related to PR 57371 and PR 106805 in bugzilla.
    
    2024-07-18  Roger Sayle  <ro...@nextmovesoftware.com>
    
    gcc/ChangeLog
            * match.pd ((FTYPE) N CMP CST): Only worry about exceptions with
            flag_trapping_math, and about signaling NaNs with HONOR_SNANS.
    
    gcc/testsuite/ChangeLog
            * c-c++-common/pr57371-4.c: Update comment.
            * c-c++-common/pr57371-5.c: Add missing testcases from pr57371-4.c
            and update for -fno-signaling-nans -fno-trapping-math.

Diff:
---
 gcc/match.pd                           | 14 ++++++------
 gcc/testsuite/c-c++-common/pr57371-4.c |  4 +---
 gcc/testsuite/c-c++-common/pr57371-5.c | 42 +++++++++++++++++++++++++++++++---
 3 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 5cb399b87180..6818856991c6 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -6862,13 +6862,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
        tree itype = TREE_TYPE (@0);
        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
-       /* Be careful to preserve any potential exceptions due to
-         NaNs.  qNaNs are ok in == or != context.
-         TODO: relax under -fno-trapping-math or
-         -fno-signaling-nans.  */
-       bool exception_p
-         = real_isnan (cst) && (cst->signalling
-                               || (cmp != EQ_EXPR && cmp != NE_EXPR));
+       /* Be careful to preserve any potential exceptions due to NaNs.
+         qNaNs are ok in == or != context.  */
+       bool exception_p = real_isnan (cst)
+                         && flag_trapping_math
+                         && ((cmp != EQ_EXPR && cmp != NE_EXPR)
+                             || (cst->signalling
+                                 && HONOR_SNANS (TREE_TYPE (@1))));
      }
      /* TODO: allow non-fitting itype and SNaNs when
        -fno-trapping-math.  */
diff --git a/gcc/testsuite/c-c++-common/pr57371-4.c 
b/gcc/testsuite/c-c++-common/pr57371-4.c
index f43f7c22419a..b0e539de4b9f 100644
--- a/gcc/testsuite/c-c++-common/pr57371-4.c
+++ b/gcc/testsuite/c-c++-common/pr57371-4.c
@@ -2,9 +2,7 @@
 /* { dg-options "-O -fsignaling-nans -fdump-tree-original" } */
 
 /* We can not get rid of comparison in tests below because of
-   pending NaN exceptions.
-
-   TODO: avoid under -fno-trapping-math.  */
+   pending NaN exceptions.  */
 
 #define QNAN __builtin_nanf ("0")
 #define SNAN __builtin_nansf ("0")
diff --git a/gcc/testsuite/c-c++-common/pr57371-5.c 
b/gcc/testsuite/c-c++-common/pr57371-5.c
index 8e18b0a73138..77decbe5dff5 100644
--- a/gcc/testsuite/c-c++-common/pr57371-5.c
+++ b/gcc/testsuite/c-c++-common/pr57371-5.c
@@ -2,11 +2,10 @@
 /* { 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.  */
+   pending NaN exceptions.  */
 
 #define QNAN __builtin_nanf ("0")
+#define SNAN __builtin_nansf ("0")
 
 void nonfinite(unsigned short x) {
   {
@@ -33,6 +32,43 @@ void nonfinite(unsigned short x) {
     /* { dg-final { scan-tree-dump "nonfinite_4 = 0" "original" } } */
   }
 
+  {
+    volatile int nonfinite_5;
+    nonfinite_5 = (float) x > SNAN;
+    /* { dg-final { scan-tree-dump "nonfinite_5 = 0" "original" } } */
+  }
+
+  {
+    volatile int nonfinite_6;
+    nonfinite_6 = (float) x >= SNAN;
+    /* { dg-final { scan-tree-dump "nonfinite_6 = 0" "original" } } */
+  }
+
+  {
+    volatile int nonfinite_7;
+    nonfinite_7 = (float) x < SNAN;
+    /* { dg-final { scan-tree-dump "nonfinite_7 = 0" "original" } } */
+  }
+
+  {
+    volatile int nonfinite_8;
+    nonfinite_8 = (float) x <= SNAN;
+    /* { dg-final { scan-tree-dump "nonfinite_8 = 0" "original" } } */
+  }
+
+  {
+    volatile int nonfinite_9;
+    nonfinite_9 = (float) x == SNAN;
+    /* { dg-final { scan-tree-dump "nonfinite_9 = 0" "original" } } */
+  }
+
+  {
+    volatile int nonfinite_10;
+    nonfinite_10 = (float) x != SNAN;
+    /* { dg-final { scan-tree-dump "nonfinite_10 = 1" "original" } } *
+ */
+  }
+
   {
     volatile int nonfinite_11;
     nonfinite_11 = (float) x == QNAN;

Reply via email to