From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id 723503858D20 for ; Mon, 14 Mar 2022 19:25:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 723503858D20 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=0w1eDB/chXEJH7cN1KL/lDOt48Hq2I4dzSiYF8J0NX8=; b=ap/qIEUTMxveesge09XaweYgCJ 5jvQVT3eSnMhpaNW5SPnMdFJ9Usa7lSD4vkTzmFgRNrOqk43cGhauLOPu0kNiXBOdPn2vQSYg4AyL n8l2+qwPqAgDgVtivsNIV43Cgf78kpPNexviQpPd4/4zztp5mMBOjjimpnXT+ZBBghrH+GwkZYq5D 6LFozWA1xlAlrQvPbW05OjRVOxX7vtDb+KvR2Jk5RKv9PZeNu0Xv9Sz3CDI2qYx8mYd9+ZMB7IgGE el4bzJuVd7/z2g95UvvEOq7Lyq3voiwALNUYcSnWVTB9ODOhaU/t2wkIM+0DrNcuKeqMy4IhtXUYu bfjQ5+3A==; Received: from host86-186-213-42.range86-186.btcentralplus.com ([86.186.213.42]:51366 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nTqK8-0002EF-1R for gcc-patches@gcc.gnu.org; Mon, 14 Mar 2022 15:25:44 -0400 From: "Roger Sayle" To: Subject: [PATCH] Ignore (possible) signed zeros in operands of FP comparisons. Date: Mon, 14 Mar 2022 19:25:42 -0000 Message-ID: <001c01d837d9$4c8bf060$e5a3d120$@nextmovesoftware.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_001D_01D837D9.4C8E3A50" X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adg32IOJF8ZrDY/3SOeLorGxAWbwYw== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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: Mon, 14 Mar 2022 19:25:46 -0000 This is a multipart message in MIME format. ------=_NextPart_000_001D_01D837D9.4C8E3A50 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit I've been wondering about the possible performance/missed-optimization impact of my patch for PR middle-end/98420 and similar IEEE correctness fixes that disable constant folding optimizations when worrying about -0.0. In the common situation where the floating point result is used by a FP comparison, there's no distinction between +0.0 and -0.0, so some HONOR_SIGNED_ZEROS optimizations that we'd usually disable, are safe. Consider the following interesting example: int foo(int x, double y) { return (x * 0.0) < y; } Although we know that x (when converted to double) can't be NaN or Inf, we still worry that for negative values of x that (x * 0.0) may be -0.0 and so perform the multiplication at run-time. But in this case, the result of the comparison (-0.0 < y) will be exactly the same as (+0.0 < y) for any y, hence the above may be safely constant folded to "0.0 < y" avoiding the multiplication at run-time. This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check with no new failures, and allows GCC to continue to optimize cases that we optimized in GCC 11 (without regard to correctness). Ok for mainline? 2022-03-14 Roger Sayle gcc/ChangeLog * match.pd (X CMP (Y-Y) -> X CMP 0.0): New transformation. (X CMP (Y * 0.0) -> X CMP 0.0): Likewise. (X CMP X -> true): Test tree_expr_maybe_nan_p instead of HONOR_NANS. (X LTGT X -> false): Enable if X is not tree_expr_maybe_nan_p, as this can't trap/signal. gcc/testsuite/ChangeLog * gcc.dg/fold-compare-9.c: New test case. Thanks in advance, Roger -- ------=_NextPart_000_001D_01D837D9.4C8E3A50 Content-Type: text/plain; name="patchrm3.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="patchrm3.txt" diff --git a/gcc/match.pd b/gcc/match.pd=0A= index 8b44b5c..f3f7865 100644=0A= --- a/gcc/match.pd=0A= +++ b/gcc/match.pd=0A= @@ -4671,6 +4671,35 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)=0A= (if (single_use (@2))=0A= (cmp @0 @1)))))=0A= =0A= +/* Floating point comparisons can ignore signed zeros. */=0A= +(for cmp (tcc_comparison)=0A= + /* Simplify (X - X) CMP Y even with -frounding-math. */=0A= + (simplify=0A= + (cmp (minus @0 @0) @1)=0A= + (if (FLOAT_TYPE_P (TREE_TYPE (@0))=0A= + && !tree_expr_maybe_nan_p (@0)=0A= + && !tree_expr_maybe_infinite_p (@0))=0A= + (cmp { build_zero_cst (TREE_TYPE (@0)); } @1)))=0A= + /* Simplify X CMP (Y - Y) even with -frounding-math. */=0A= + (simplify=0A= + (cmp @0 (minus @1 @1))=0A= + (if (FLOAT_TYPE_P (TREE_TYPE (@1))=0A= + && !tree_expr_maybe_nan_p (@1)=0A= + && !tree_expr_maybe_infinite_p (@1))=0A= + (cmp @0 { build_zero_cst (TREE_TYPE (@1)); })))=0A= + /* Simplify (X * 0.0) CMP Y. */=0A= + (simplify=0A= + (cmp (mult @0 real_zerop@1) @2)=0A= + (if (!tree_expr_maybe_nan_p (@0)=0A= + && !tree_expr_maybe_infinite_p (@0))=0A= + (cmp @1 @2)))=0A= + /* Simplify X CMP (Y * 0.0). */=0A= + (simplify=0A= + (cmp @0 (mult @1 real_zerop@2))=0A= + (if (!tree_expr_maybe_nan_p (@1)=0A= + && !tree_expr_maybe_infinite_p (@0))=0A= + (cmp @0 @2))))=0A= +=0A= /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and=0A= (x >=3D 0) ^ (y >=3D 0) to (x ^ y) < 0. */=0A= (for cmp (lt ge)=0A= @@ -4743,7 +4772,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)=0A= (simplify=0A= (cmp @0 @0)=0A= (if (! FLOAT_TYPE_P (TREE_TYPE (@0))=0A= - || ! HONOR_NANS (@0))=0A= + || ! tree_expr_maybe_nan_p (@0))=0A= { constant_boolean_node (true, type); }=0A= (if (cmp !=3D EQ_EXPR=0A= /* With -ftrapping-math conversion to EQ loses an exception. */=0A= @@ -4755,7 +4784,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)=0A= (cmp @0 @0)=0A= (if (cmp !=3D NE_EXPR=0A= || ! FLOAT_TYPE_P (TREE_TYPE (@0))=0A= - || ! HONOR_NANS (@0))=0A= + || ! tree_expr_maybe_nan_p (@0))=0A= { constant_boolean_node (false, type); })))=0A= (for cmp (unle unge uneq)=0A= (simplify=0A= @@ -4767,7 +4796,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)=0A= (unordered @0 @0)))=0A= (simplify=0A= (ltgt @0 @0)=0A= - (if (!flag_trapping_math)=0A= + (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))=0A= { constant_boolean_node (false, type); }))=0A= =0A= /* x =3D=3D ~x -> false */=0A= diff --git a/gcc/testsuite/gcc.dg/fold-compare-9.c = b/gcc/testsuite/gcc.dg/fold-compare-9.c=0A= new file mode 100644=0A= index 0000000..e56f63d=0A= --- /dev/null=0A= +++ b/gcc/testsuite/gcc.dg/fold-compare-9.c=0A= @@ -0,0 +1,8 @@=0A= +/* { dg-do compile } */=0A= +/* { dg-options "-O2 -fdump-tree-optimized" } */=0A= +=0A= +int foo(int x, double y) {=0A= + return (x * 0.0) < y;=0A= +}=0A= +=0A= +/* { dg-final { scan-tree-dump " y_\[0-9\]\\(D\\) > 0.0" "optimized" } = } */=0A= ------=_NextPart_000_001D_01D837D9.4C8E3A50--