From: Jakub Jelinek Date: Mon, 5 Dec 2022 10:54:45 +0000 (+0100) Subject: match.pd: Don't fold nan < x etc. for -ftrapping-math [PR106805] X-Git-Tag: basepoints/gcc-14~2714 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=109148dd16e4bcd50faee19c49082de69d0ba26e;p=thirdparty%2Fgcc.git 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 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 >/>=/ 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 000000000000..8e18b0a73138 --- /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 e7c051d8fe06..6655a07ab3c4 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 000000000000..71726346848c --- /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 000000000000..e7c051d8fe06 --- /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