From patchwork Fri Nov 24 05:28:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 1868016 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=xry111.site header.i=@xry111.site header.a=rsa-sha256 header.s=default header.b=hhg4zeBs; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Sc3YG33Fpz1yR8 for ; Fri, 24 Nov 2023 16:33:22 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6C33A3858C62 for ; Fri, 24 Nov 2023 05:33:12 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id 2880D3858D1E for ; Fri, 24 Nov 2023 05:32:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2880D3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=xry111.site Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=xry111.site ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2880D3858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=89.208.246.23 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700803980; cv=none; b=VghUS4PBA8j5Z9JBG6RvaJqPwDr9QS76qz1D34a1HBypDZLsyjj75wi4sY533L5Q8aZSfxEpwc7SeCV06lg+7tQNMypnj8lrcmHIFGO63HcFphGSLvy+9JuO/6kx5TiQLq0ZZCvcmxDzWoHkIWDOyLCVs3BPbjX8aLFtK36NnNA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700803980; c=relaxed/simple; bh=u+sXDiBu8oO6/8WaVu+jT5m21TWJnEuUr0KogFYg5js=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=t8myFcz02KYJpMcnhNv/UlV66tAk4E+xQInCEAEaq98h0SkWUAxBPwFu14L7Pqgltci/STjmSdEX3bZlv9rWwkV3Z/mY4hs2meayFGJNj7AI00Sodb/pjpsDgqgANlVj/xodVrV01wBCQVYF4LcaKZNcQCcLIZfCgAF3V49j5Rs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1700803974; bh=u+sXDiBu8oO6/8WaVu+jT5m21TWJnEuUr0KogFYg5js=; h=From:To:Cc:Subject:Date:From; b=hhg4zeBsUqT9cbP8WHUjuBbSpVm+PF5RSiYfCfqkSS4Efia+PKFMII2jYkzBK0otF lBhrunSvoQMp5ePIeAqe/zQSJ1pHU7/8UQLYkAMGcF5nbUTV7suVnT/ro+l74qu9u2 T6qOdTfhnVb7OzuGm/VJH7h+0EKdREaE8HFYe5p0= Received: from stargazer.. (unknown [IPv6:240e:358:11dc:c000:dc73:854d:832e:2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id BCF6266B39; Fri, 24 Nov 2023 00:32:52 -0500 (EST) From: Xi Ruoyao To: gcc-patches@gcc.gnu.org Cc: Joseph Myers , Xi Ruoyao Subject: [PATCH] Only allow (int)trunc(x) to (int)x simplification with -ffp-int-builtin-inexact [PR107723] Date: Fri, 24 Nov 2023 13:28:22 +0800 Message-ID: <20231124053241.64194-1-xry111@xry111.site> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-9.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, LIKELY_SPAM_FROM, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org With -fno-fp-int-builtin-inexact, trunc is not allowed to raise FE_INEXACT and it should produce an integral result (if the input is not NaN or Inf). Thus FE_INEXACT should not be raised. But (int)x may raise FE_INEXACT when x is a non-integer, non-NaN, and non-Inf value. C23 recommends to do so in a footnote. Thus we should not simplify (int)trunc(x) to (int)x if -fno-fp-int-builtin-inexact. gcc/ChangeLog: PR middle-end/107723 * convert.cc (convert_to_integer_1) [case BUILT_IN_TRUNC]: Break early if !flag_fp_int_builtin_inexact. gcc/testsuite/ChangeLog: PR middle-end/107723 * gcc.dg/torture/builtin-fp-int-inexact-trunc.c: New test. --- Bootstrapped and regtested on x86_64-linux-gnu. Ok for trunk? gcc/convert.cc | 3 ++- .../gcc.dg/torture/builtin-fp-int-inexact-trunc.c | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/torture/builtin-fp-int-inexact-trunc.c diff --git a/gcc/convert.cc b/gcc/convert.cc index 46c8bcb31f8..31f63ea783b 100644 --- a/gcc/convert.cc +++ b/gcc/convert.cc @@ -591,7 +591,8 @@ convert_to_integer_1 (tree type, tree expr, bool dofold) CASE_FLT_FN (BUILT_IN_TRUNC): CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC): if (call_expr_nargs (s_expr) != 1 - || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (s_expr, 0)))) + || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (s_expr, 0))) + || !flag_fp_int_builtin_inexact) break; return convert_to_integer_1 (type, CALL_EXPR_ARG (s_expr, 0), dofold); diff --git a/gcc/testsuite/gcc.dg/torture/builtin-fp-int-inexact-trunc.c b/gcc/testsuite/gcc.dg/torture/builtin-fp-int-inexact-trunc.c new file mode 100644 index 00000000000..09731183621 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/builtin-fp-int-inexact-trunc.c @@ -0,0 +1,12 @@ +/* Test -fno-fp-int-builtin-inexact. */ +/* { dg-do compile } */ +/* { dg-options "-fno-fp-int-builtin-inexact -fdump-tree-original" } */ + +long +x (double y) +{ + return __builtin_trunc (y); +} + +/* Optimization should not discard the __builtin_trunc call. */ +/* { dg-final { scan-tree-dump "__builtin_trunc" "original" } } */