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: 169173 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp917462vqx; Thu, 23 Nov 2023 21:33:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IFv48RVoZa2XfCH7DdoNTCE3i5oUZHp9Qm33qFVm4k0raQ6Oa81dxycpR49m9wvbrXs0QNv X-Received: by 2002:a05:622a:18d:b0:41c:c8f2:58ce with SMTP id s13-20020a05622a018d00b0041cc8f258cemr2273196qtw.27.1700804004334; Thu, 23 Nov 2023 21:33:24 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700804004; cv=pass; d=google.com; s=arc-20160816; b=GuPl6ouCPkrifzBA/xkzkxL5r1a0ElrZm63cYTdm4N+4Xba5IWubAVIDkwbKfB7Fl3 2qOPU6+GiFmcVm0b8o9WqMGBPM91ZAFCa/SbgIfQpU4PuhSofD2LJlyIvHS8dHNEkSXi FriiGHxW5Bhq/Sq9KGMGbydToTeUCeIo2CuyEN8baIb53jergb5SN7ktY+XKI00+WS7v Dkw4rgLGjaaSc1iNo8buLSPFKKx09aMQDsG+7n25qQq+vuFHc/u96YUdbdTSqBfJW7/c IyLwgy1PMDLNPUi3eLd1fEatppX81REiCHy0Nn8omOZ9AyzI3j6pkCSqUWg7WfD4gx59 JQFg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=RnrqpfHDfM1cTS8D7goRuVAPwfKO8ufOtalmpixDkGU=; fh=yo6BNnqzP2M8LCaF92IYtu4ugi2zwWrwBw8U+R+PAs4=; b=IWAx+Ss0+qZeDj54yKb+Sfrx+0R47xW4ufnvx8G0CV8mtyBvii2javDaWs8X9fvuHN GNeNJuV7uMt6LH7V5ZtPCx9yAXjKagnh1UXkGYH7PsDsJvvsW9svpHtLkZ8eTM3/0H7l Q8aKOP6GfIV8IW2DNo82J4mvCdgX7PHNoFnjBqovGmnvdy5kQ6N6dNbBMOsYtpMjNvXK wLOL2bjflYriFqdA+Nbzjl5IL/r9KaGvLW8YG/fFqcRHlD/XbgknH1r/PEuADWMa0OBp 1av2L6wZ6RRe9bjTzA0nCbSBMJpT1/0Z1MSIqR++/rChE2ReH+Caip9ibhiLQPXgYdhD YuqQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@xry111.site header.s=default header.b=hhg4zeBs; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=xry111.site Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id r1-20020ac85c81000000b004236f70dff2si2638108qta.385.2023.11.23.21.33.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 21:33:24 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@xry111.site header.s=default header.b=hhg4zeBs; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=xry111.site Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1A72A3858D28 for ; Fri, 24 Nov 2023 05:33:24 +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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783422259543842689 X-GMAIL-MSGID: 1783422259543842689 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" } } */