From patchwork Tue Feb 1 05:10:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 50611 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 849FC3857C66 for ; Tue, 1 Feb 2022 05:11:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 849FC3857C66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1643692288; bh=yrPFHXWey1Xnpxp/ONbnVJl8jBRlQ7D77CtOydMbrZM=; h=Subject:To:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=IvfJD9Cbzp0MS/rhZJt6cFwirftg+3HpL2ec8NUk/wXnfcKq2MTFZ8iLupjRKqpOg mv58JpIzn1EM4Kvxq8uZMNg+fFURZx3TVS5dErixwZoFu2dXDMnyVxqYWk9khPETPr iqejw4Qj6lTxDPxCBsAl++bIDn255Fqj3P312rYA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mengyan1223.wang (mengyan1223.wang [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id 77A773858D20 for ; Tue, 1 Feb 2022 05:10:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 77A773858D20 Received: from localhost.localdomain (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@mengyan1223.wang) by mengyan1223.wang (Postfix) with ESMTPSA id 12DFC660FC; Tue, 1 Feb 2022 00:10:55 -0500 (EST) Message-ID: Subject: [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: Richard Biener Date: Tue, 01 Feb 2022 13:10:54 +0800 In-Reply-To: References: <8b49a9906a0d1019bd877bf526f71ab5321550fe.camel@mengyan1223.wang> User-Agent: Evolution 3.42.3 MIME-Version: 1.0 X-Spam-Status: No, score=-3037.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, JMQ_SPF_NEUTRAL, SPF_HELO_PASS, 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-Patchwork-Original-From: Xi Ruoyao via Gcc-patches From: Xi Ruoyao Reply-To: Xi Ruoyao Cc: GCC Patches , "Joseph S. Myers" Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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. 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); +}