From patchwork Fri Nov 24 09:09:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 1868063 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=SoBM1kw0; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4Sc8NW6RJQz1yRy for ; Fri, 24 Nov 2023 20:11:05 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D42823858035 for ; Fri, 24 Nov 2023 09:11:02 +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 C9BBF385B532 for ; Fri, 24 Nov 2023 09:10:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C9BBF385B532 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 C9BBF385B532 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=1700817019; cv=none; b=Kq99eqJLrghkFNLvbuRoNUoK3GIv5Ru2HJ7vMXBfDhm9pw2lqFuZYKz91mCihy/o5rrx76wz7jaVGEJ94yDAYNB5sW8nJdfPDjgJEe4Myf5HUC9yuXf98IRjwnxjiGQAYuMGcl/Bh4eItoGQXO+XisyX34knkmODQ7NY9DoPqRM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700817019; c=relaxed/simple; bh=+P/36g8Zu44BWEGuaAkvE457Pq7sBjcQbg8u83P+tuc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=FsOxi45ERatkiAEs2x/H6wXo0+k7Frxlf/isbazvJ2hrlC0YiISemAlkEPJBGba/nvyVXuqv7wVRD6+y59vX+K6wFg22h8CD6FGdRLkyoCrH/wEvNjwrA3abDyQz21o5oG1/O4410Tmh2XNFT9Mbxqz4nz9u1Mnl1aXG0a7wsxI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1700817016; bh=+P/36g8Zu44BWEGuaAkvE457Pq7sBjcQbg8u83P+tuc=; h=From:To:Cc:Subject:Date:From; b=SoBM1kw0wVAD0Ku4HuN+QgNMgXPmnVdN3nJlhynuI4Hch64bSlbug2LMSzYo/q/Hb 3b6vv6i+D+tN7NVfuJ+C19sGcsfFJamHCRQ22S7BvknsiNhqsrclVs3do8qA5zukSd FsrhUFIKJLwpnsPj+cx13V/5/PpZZF6sSgP6glAw= 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 5DDE066B39; Fri, 24 Nov 2023 04:10:13 -0500 (EST) From: Xi Ruoyao To: gcc-patches@gcc.gnu.org Cc: Joseph Myers , Richard Biener , Xi Ruoyao Subject: [PATCH v2] Only allow (int)trunc(x) to (int)x simplification with -ffp-int-builtin-inexact [PR107723] Date: Fri, 24 Nov 2023 17:09:28 +0800 Message-ID: <20231124091002.194321-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 is in-effect. gcc/ChangeLog: PR middle-end/107723 * convert.cc (convert_to_integer_1) [case BUILT_IN_TRUNC]: Break early if !flag_fp_int_builtin_inexact and flag_trapping_math. gcc/testsuite/ChangeLog: PR middle-end/107723 * gcc.dg/torture/builtin-fp-int-inexact-trunc.c: New test. --- Change from v1: add flag_trapping_math into the condition. 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..f214b750188 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 && flag_trapping_math)) 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" } } */