From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [69.48.154.134]) by sourceware.org (Postfix) with ESMTPS id B1CBE3858C60 for ; Fri, 26 Apr 2024 08:17:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B1CBE3858C60 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B1CBE3858C60 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=69.48.154.134 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714119471; cv=none; b=SmRfT+XaY2azbrpKIgOl1hmhKqsjcmnFw7/KLOM+daucZEOFvD+JbznCUBJ0rWZlbqYj7tsz6QDobMa+95R15QQ6xYr/PC1TcqDu4ipejWNYJDDgsHihoi31EeHvizp3mMZHnnCpkxE4GBkpX9K5Bg3/p2RsCjuUiqnjx8G0smA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714119471; c=relaxed/simple; bh=h+ThsyAggX6ZPqVoxqXt86hAU/Dt7fcaDsj6hdbxNW4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QAv/62G1pmJXn0jH6ecgas8X9UaKXvSzcd2TnGz8yxD1Rm+rOI8lC727QV44K6rOKxQxTOFKyBw2lxR1jUtyJZiCtNg/DwTDtPl/s6eg2NWRvxi0kt+rajdk2AOFMaBjWN3mGGKIqHYq1ZAmc8+ES9pqbez2ZYtTDCGPcXF8YcE= ARC-Authentication-Results: i=1; server2.sourceware.org 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=VVHzWng7uuXn2iwtBEdubm0uyMi1lalIPl+nZBUpSL4=; b=cqvFCu8hn+TBr0L1J/nvPfWcA4 +w2gwpCF2XuJqAx7i4uda1JRQKnvPgZsChrGzZraJFokoiv3GRJeo1lfpzwZs1387v2HKZz7E9vCV Jt1SG4tQ6d1Sf2yoaJc3FBPEs6V5PLkDz5sj+zk9HwRhi34FZXF0wK8J0M2VzGnCF2loZdLx+VBUz RACQfDqsHrUWUpftvtXcYIHsgtACigcRoMWqLaXTBDP0H3RaYdZO+iYFMKY8RtzlOty0KhpiIonU/ jcTQMqWjtaJ71AnKwcWmlQPovuO9tEHlLAbl6dIGDq+aY8qRRYtnC1tVHeIgywP7xGJNKisksZOUY KuRTSIuQ==; Received: from [168.86.199.132] (port=49701 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1s0Gm1-006hOs-2U for gcc-patches@gcc.gnu.org; Fri, 26 Apr 2024 04:17:37 -0400 From: "Roger Sayle" To: Subject: [PATCH] PR middle-end/111701: signbit(x*x) vs -fsignaling-nans Date: Fri, 26 Apr 2024 09:17:35 +0100 Message-ID: <009201da97b2$325fc140$971f43c0$@nextmovesoftware.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0093_01DA97BA.94242940" X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdqXsMowkRIqLsS6Qou+WI/9513zEQ== 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.7 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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: This is a multipart message in MIME format. ------=_NextPart_000_0093_01DA97BA.94242940 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit This patch addresses PR middle-end/111701 where optimization of signbit(x*x) using tree_nonnegative_p incorrectly eliminates a floating point multiplication when the operands may potentially be signaling NaNs. The above bug fix also provides a solution or work-around to the tricky issue in PR middle-end/111701, that the results of IEEE operations on NaNs are specified to return a NaN result, but fail to (precisely) specify the exact NaN representation of this result. Hence for the operation "-NaN*-NaN" different hardware implementations (targets) return different results. Ultimately knowing what the resulting NaN "payload" of an operation is can only be known by executing that operation at run-time, and I'd suggest that GCC's -fsignaling-nans provides a mechanism for handling code that uses NaN representations for communication/signaling (which is a different but related concept to IEEE's sNaN). One nice thing about this patch, which may or may not be a P2 regression fix, is that it only affects (improves) code compiled with -fsignaling-nans so should be extremely safe even for this point in stage 3. This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check, both with and without --target_board=unix{-m32} with no new failures. Ok for mainline? 2024-04-26 Roger Sayle gcc/ChangeLog PR middle-end/111701 * fold-const.cc (tree_binary_nonnegative_warnv_p) : Split handling of floating point and integer types. For equal floating point operands, avoid optimization if the operand may be a signaling NaN. gcc/testsuite/ChangeLog PR middle-end/111701 * gcc.dg/pr111701-1.c: New test case. * gcc.dg/pr111701-2.c: Likewise. Thanks in advance, Roger -- ------=_NextPart_000_0093_01DA97BA.94242940 Content-Type: text/plain; name="patchnn2.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="patchnn2.txt" diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc=0A= index 7b26896..f7f174d 100644=0A= --- a/gcc/fold-const.cc=0A= +++ b/gcc/fold-const.cc=0A= @@ -15076,16 +15076,27 @@ tree_binary_nonnegative_warnv_p (enum = tree_code code, tree type, tree op0,=0A= break;=0A= =0A= case MULT_EXPR:=0A= - if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))=0A= + if (FLOAT_TYPE_P (type))=0A= {=0A= - /* x * x is always non-negative for floating point x=0A= - or without overflow. */=0A= + /* x * x is non-negative for floating point x except=0A= + that -NaN*-NaN may return -NaN. PR middle-end/111701. */=0A= + if (operand_equal_p (op0, op1, 0))=0A= + {=0A= + if (!tree_expr_maybe_signaling_nan_p (op0) || RECURSE (op0))=0A= + return true;=0A= + }=0A= + else if (RECURSE (op0) && RECURSE (op1))=0A= + return true;=0A= + }=0A= +=0A= + if (ANY_INTEGRAL_TYPE_P (type)=0A= + && TYPE_OVERFLOW_UNDEFINED (type))=0A= + {=0A= + /* x * x is always non-negative without overflow. */=0A= if (operand_equal_p (op0, op1, 0)=0A= || (RECURSE (op0) && RECURSE (op1)))=0A= {=0A= - if (ANY_INTEGRAL_TYPE_P (type)=0A= - && TYPE_OVERFLOW_UNDEFINED (type))=0A= - *strict_overflow_p =3D true;=0A= + *strict_overflow_p =3D true;=0A= return true;=0A= }=0A= }=0A= diff --git a/gcc/testsuite/gcc.dg/pr111701-1.c = b/gcc/testsuite/gcc.dg/pr111701-1.c=0A= new file mode 100644=0A= index 0000000..5cbfac2=0A= --- /dev/null=0A= +++ b/gcc/testsuite/gcc.dg/pr111701-1.c=0A= @@ -0,0 +1,14 @@=0A= +/* { dg-do compile } */=0A= +/* { dg-options "-O2 -fsignaling-nans -fdump-tree-optimized" } */=0A= +=0A= +int foo(double x)=0A= +{=0A= + return __builtin_signbit(x*x);=0A= +}=0A= +=0A= +int bar(float x)=0A= +{=0A= + return __builtin_signbit(x*x);=0A= +}=0A= +=0A= +/* { dg-final { scan-tree-dump-times " \\* " 2 "optimized" } } */=0A= diff --git a/gcc/testsuite/gcc.dg/pr111701-2.c = b/gcc/testsuite/gcc.dg/pr111701-2.c=0A= new file mode 100644=0A= index 0000000..f79c7ba=0A= --- /dev/null=0A= +++ b/gcc/testsuite/gcc.dg/pr111701-2.c=0A= @@ -0,0 +1,14 @@=0A= +/* { dg-do compile } */=0A= +/* { dg-options "-O2 -fdump-tree-optimized" } */=0A= +=0A= +int foo(double x)=0A= +{=0A= + return __builtin_signbit(x*x);=0A= +}=0A= +=0A= +int bar(float x)=0A= +{=0A= + return __builtin_signbit(x*x);=0A= +}=0A= +=0A= +/* { dg-final { scan-tree-dump-not " \\* " "optimized" } } */=0A= ------=_NextPart_000_0093_01DA97BA.94242940--