From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by sourceware.org (Postfix) with ESMTPS id DCCD43857C62 for ; Tue, 1 Feb 2022 07:19:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DCCD43857C62 Received: by mail-ed1-x536.google.com with SMTP id m11so32194880edi.13 for ; Mon, 31 Jan 2022 23:19:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Tnw0FJEJ5uED9kda8ExcBZ6ExEPAJxhjPGxBp3uVs/U=; b=oHq3xpsnpdG3I1iNJQIUy4kThm70Bv68Um67UgDxshKVD+0I+HW+l4XGux3E7KW4Ax hgAVje0uL4nA395/njDRPbBGT7Lc+1mPFnBH6K4frDqD0CgYqCv55ZN+/Jz7vLbnmiHq zw4SLDdAEFGv/w2XY3ONCVMQbTUxc1euzn/bvn2MkvvsUNhSvCN44GdaVFFcPP463o4n +Nh63H20kRsdy83jNo+LU+a238ebjabQEIVjqyTmzlyN10B+dG6Tl8O+GOcp0nUxQime 8WYFmymz6ftMgNnlf0N54bdbFis3fxy0kwtd04eRaeLBNlzL5q7vfo6EhEccJz2+EVGU 2hxw== X-Gm-Message-State: AOAM530kRbLv+F9MEllLTtdeIvymPT7EAN+kJQ24vIj719tWx0Z2VvCu aNGSv/mNOGrjDbv6AWfhh63ZSZz3C3QamiozuBpm133T X-Google-Smtp-Source: ABdhPJzi6Rljh6Xz1IENnPHT8c37TrsS/hLN6GzE7pbzn3SXZKWDTBduS/L7dJyTgOCpmZzEyr0g0wnPHACbIW+aHKQ= X-Received: by 2002:a05:6402:1774:: with SMTP id da20mr23811929edb.372.1643699973956; Mon, 31 Jan 2022 23:19:33 -0800 (PST) MIME-Version: 1.0 References: <8b49a9906a0d1019bd877bf526f71ab5321550fe.camel@mengyan1223.wang> In-Reply-To: From: Richard Biener Date: Tue, 1 Feb 2022 08:19:23 +0100 Message-ID: Subject: Re: [PATCH] fold-const: do not fold NaN result from non-NaN operands (was: Re: [PATCH] fold-const: do not fold 'inf/inf' with -ftrapping-math [PR95115]) To: Xi Ruoyao Cc: "Joseph S. Myers" , GCC Patches Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 01 Feb 2022 07:19:36 -0000 On Tue, Feb 1, 2022 at 6:10 AM Xi Ruoyao wrote: > > Bootstrapped and tested on x86_64-linux-gnu. Ok for trunk, gcc-11, and > gcc-10? (I'd consider PR 95115 a 10/11/12 regression because GCC > 10/11/12 miscompiles glibc acos/asin function, but not GCC 9.) > > These operations should raise an invalid operation exception at runtime. > So they should not be folded during compilation unless -fno-trapping-math > is used. OK for trunk and branches after a while. Thanks, Richard. > 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. > --- > gcc/fold-const.cc | 11 +++++++++++ > gcc/testsuite/gcc.dg/pr95115.c | 25 +++++++++++++++++++++++++ > 2 files changed, 36 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/pr95115.c > > 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); > +} > -- > 2.35.1 > >