From patchwork Tue Feb 8 23:20:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1590147 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=nextmovesoftware.com header.i=@nextmovesoftware.com header.a=rsa-sha256 header.s=default header.b=SFXkLjuj; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JtfBV41lGz9s5B for ; Wed, 9 Feb 2022 10:20:32 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 340C33857C7A for ; Tue, 8 Feb 2022 23:20:30 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id 090D13858D3C for ; Tue, 8 Feb 2022 23:20:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 090D13858D3C 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=Ph5x1Mc9SfcSkwWu2FOMtyhsPhzU7KB/ejWj81npLp4=; b=SFXkLjujGgyyh7Jar5rB2HadAs JPTv23FEtCV2zSNttxZBz6viBObiKnGQdDZMaR2m5Z0Gpj2OzC/TclGry4fr7xis05Fts5Gs4tG3n yb0ni2A/R+9mG+ABKhoZtnGkYgW0IQ69LpHB87Bqi/fw4JWKYFTDtDoQdW5PkVgLy9z3LOrKj3J5k xQfNKIdS19weJv/xnEJIv29B0mYLu67vnmMJjZcIsBsBAOajKkZ+pXgpO+xy6MhNcIFHDzKKeNMFC thfn+bOTH623j84g/itcZUsa3pGVkAdg6ZTENPpu5eObVFtVkCFVzw/UaUpC6nQMiQ6NufSfPFN3s x97Ri8hw==; Received: from host86-160-23-130.range86-160.btcentralplus.com ([86.160.23.130]:50610 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 1nHZmI-0007qs-DY for gcc-patches@gcc.gnu.org; Tue, 08 Feb 2022 18:20:06 -0500 From: "Roger Sayle" To: Subject: [PATCH] PR tree-optimization/104420: Fix checks for constant folding X*0.0 Date: Tue, 8 Feb 2022 23:20:04 -0000 Message-ID: <007401d81d42$67e6dde0$37b499a0$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdgdQfVmrO+qTeQ9RqOqjx7l8PbuwQ== 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=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_BARRACUDACENTRAL, 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This patch resolves PR tree-optimization/104420, which is a P1 regression where, as observed by Jakub Jelinek, the conditions for constant folding x*0.0 are incorrect (following my patch for PR tree-optimization/96392). The multiplication x*0.0 may yield a negative zero result, -0.0, if X is negative (not just if x may be negative zero). Hence (without -ffast-math) (int)x*0.0 can't be optimized to 0.0, but (unsigned)x*0.0 can be constant folded. This adds a bunch of test cases to confirm the desired behaviour, and removes an incorrect test from gcc.dg/pr96392.c which checked for the wrong behaviour. This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check no new failures. Ok for mainline? 2022-02-08 Roger Sayle gcc/ChangeLog PR tree-optimization/104420 * match.pd (mult @0 real_zerop): Tweak conditions for constant folding X*0.0 (or X*-0.0) to HONOR_SIGNED_ZEROS when appropriate. gcc/testsuite/ChangeLog PR tree-optimization/104420 * gcc.dg/pr104420-1.c: New test case. * gcc.dg/pr104420-2.c: New test case. * gcc.dg/pr104420-3.c: New test case. * gcc.dg/pr104420-4.c: New test case. * gcc.dg/pr96392.c: Remove incorrect test. Thanks in advance (and sorry for the breakage/thinko). Roger diff --git a/gcc/match.pd b/gcc/match.pd index 7bbb801..4fe5909 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -262,8 +262,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (mult @0 real_zerop@1) (if (!tree_expr_maybe_nan_p (@0) && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0)) - && !tree_expr_maybe_real_minus_zero_p (@0) - && !tree_expr_maybe_real_minus_zero_p (@1)) + && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0))) @1)) /* In IEEE floating point, x*1 is not equivalent to x for snans. diff --git a/gcc/testsuite/gcc.dg/pr104420-1.c b/gcc/testsuite/gcc.dg/pr104420-1.c new file mode 100644 index 0000000..48385fa --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr104420-1.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-add-options ieee } */ + +double f(int a) +{ + return a * 0.0; +} + +/* { dg-final { scan-tree-dump " \\\* 0.0" "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/pr104420-2.c b/gcc/testsuite/gcc.dg/pr104420-2.c new file mode 100644 index 0000000..49d0189 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr104420-2.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-add-options ieee } */ + +double f(int a) +{ + return a * -0.0; +} + +/* { dg-final { scan-tree-dump " \\\* -0.0" "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/pr104420-3.c b/gcc/testsuite/gcc.dg/pr104420-3.c new file mode 100644 index 0000000..962dfff --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr104420-3.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-add-options ieee } */ + +double f(unsigned int a) +{ + return a * 0.0; +} + +/* { dg-final { scan-tree-dump "return 0.0" "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/pr104420-4.c b/gcc/testsuite/gcc.dg/pr104420-4.c new file mode 100644 index 0000000..95ed0cc --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr104420-4.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-add-options ieee } */ + +double f(unsigned int a) +{ + return a * -0.0; +} + +/* { dg-final { scan-tree-dump "return -0.0" "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/pr96392.c b/gcc/testsuite/gcc.dg/pr96392.c index 662bacb..fb7de21 100644 --- a/gcc/testsuite/gcc.dg/pr96392.c +++ b/gcc/testsuite/gcc.dg/pr96392.c @@ -12,11 +12,6 @@ double sub0(int x) return x - 0.0; } -double mult0(int x) -{ - return 0.0 * x; -} - double negate(int x) { return 0.0 - x; @@ -29,5 +24,4 @@ double subtract(int x) /* { dg-final { scan-tree-dump-not " \\+ " "optimized" } } */ /* { dg-final { scan-tree-dump-not " \\- " "optimized" } } */ -/* { dg-final { scan-tree-dump-not " \\* " "optimized" } } */