From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id A7C073858C2F; Mon, 5 Dec 2022 10:55:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A7C073858C2F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670237732; bh=3cWzigInr/3c/s8Y4UJbQkSIpvNB0tg7qzwHzK/iXgE=; h=From:To:Subject:Date:From; b=iJPF4HA1IOvUdIGEyL87vTM0bdRDmgi/InG36jB4FZ1RcXAwx0tLVDVodP3QJnXOC VLrKu/LyrGOw8tAuutNqDZDFMnpkULAvlsJU+xIZJArNkNKoDsrNhDV3jYf9yT6DNX ASrC5E6JGNJv5cyauULUkmV2D54UP8i7N6aR8JGs= MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-4493] match.pd: Don't fold nan < x etc. for -ftrapping-math [PR106805] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: 4500baaccb6e4d696e223c338bbdf7705c3646dd X-Git-Newrev: 109148dd16e4bcd50faee19c49082de69d0ba26e Message-Id: <20221205105532.A7C073858C2F@sourceware.org> Date: Mon, 5 Dec 2022 10:55:32 +0000 (GMT) List-Id: https://gcc.gnu.org/g:109148dd16e4bcd50faee19c49082de69d0ba26e commit r13-4493-g109148dd16e4bcd50faee19c49082de69d0ba26e Author: Jakub Jelinek 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 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 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