From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7856) id 66B673858403; Tue, 1 Feb 2022 10:21:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 66B673858403 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Xi Ruoyao To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-6959] fold-const: do not fold NaN result from non-NaN operands [PR95115] X-Act-Checkin: gcc X-Git-Author: Xi Ruoyao X-Git-Refname: refs/heads/master X-Git-Oldrev: d43fbc7d3f7621e1c8f153c1471d2a5cd20bfdc8 X-Git-Newrev: 34afa19d29c5bf0b0f504e4d0aca4e9a8bc82c5c Message-Id: <20220201102145.66B673858403@sourceware.org> Date: Tue, 1 Feb 2022 10:21:45 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 01 Feb 2022 10:21:45 -0000 https://gcc.gnu.org/g:34afa19d29c5bf0b0f504e4d0aca4e9a8bc82c5c commit r12-6959-g34afa19d29c5bf0b0f504e4d0aca4e9a8bc82c5c Author: Xi Ruoyao Date: Mon Jan 31 01:15:20 2022 +0800 fold-const: do not fold NaN result from non-NaN operands [PR95115] These operations should raise an invalid operation exception at runtime. So they should not be folded during compilation unless -fno-trapping-math is used. gcc/ PR middle-end/95115 * fold-const.cc (const_binop): Do not fold NaN result from non-NaN operands. gcc/testsuite * gcc.dg/pr95115.c: New test. Diff: --- gcc/fold-const.cc | 11 +++++++++++ gcc/testsuite/gcc.dg/pr95115.c | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index b155611578d..8fc01cdfb77 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -1305,6 +1305,17 @@ const_binop (enum tree_code code, tree arg1, tree arg2) inexact = real_arithmetic (&value, code, &d1, &d2); real_convert (&result, mode, &value); + /* Don't constant fold this floating point operation if + both operands are not NaN but the result is NaN, and + flag_trapping_math. Such operations should raise an + invalid operation exception. */ + if (flag_trapping_math + && MODE_HAS_NANS (mode) + && REAL_VALUE_ISNAN (result) + && !REAL_VALUE_ISNAN (d1) + && !REAL_VALUE_ISNAN (d2)) + return NULL_TREE; + /* Don't constant fold this floating point operation if the result has overflowed and flag_trapping_math. */ if (flag_trapping_math diff --git a/gcc/testsuite/gcc.dg/pr95115.c b/gcc/testsuite/gcc.dg/pr95115.c new file mode 100644 index 00000000000..46a95dfb698 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr95115.c @@ -0,0 +1,25 @@ +/* { dg-do run } */ +/* { dg-options "-O2 -ftrapping-math" } */ +/* { dg-add-options ieee } */ +/* { dg-require-effective-target fenv_exceptions } */ + +#include +#include + +double +x (void) +{ + double d = __builtin_inf (); + return d / d; +} + +int +main (void) +{ + double r = x (); + if (!__builtin_isnan (r)) + abort (); + if (!fetestexcept (FE_INVALID)) + abort (); + exit (0); +}