From patchwork Thu Sep 9 10:08:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1526142 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: 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=pGW2sr3I; 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 ozlabs.org (Postfix) with ESMTPS id 4H4vpj2RsBz9t0J for ; Thu, 9 Sep 2021 20:08:27 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 17CF1384A889 for ; Thu, 9 Sep 2021 10:08:25 +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 B35803857403 for ; Thu, 9 Sep 2021 10:08:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B35803857403 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=u/1WA6O6jzXxUdOHwZXM8YFUpvcJ4ojmu8HH/XkU5B8=; b=pGW2sr3IRIt7nun+Yv73MJvpee O4LhG5pUT0/J6ybERC8QvC9uVjKntIiJKzHGUFEKsxfRaPMijJOkAdxpd5Uw8ITIiXU/8has5fo/m yruD2dCw5l0lD8rzVTmDW+RtFcPRjUWiUPoGBO2H6ywSREC3RMTJjJoFRsLEBmYmgWJ86PrhB9dOT yrKYFiBX8dinoRezNujQKxj4P96JKQlvqkhO5aceMQsgX0hbTQRKGv3SqNIts+8v8J1XjB0fmvq/U IrcLA1WXx1xO2BUTS2PfZ5SybgMJuCFvE7lx+EQ/C/NSwF/y231NI0ERG1uAM0SkLXIyD0J4G49VA Vp8M+P3Q==; Received: from [185.62.158.67] (port=57031 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 1mOGyb-0007G0-3R for gcc-patches@gcc.gnu.org; Thu, 09 Sep 2021 06:08:13 -0400 From: "Roger Sayle" To: "'GCC Patches'" Subject: [PATCH] More NEGATE_EXPR folding in match.pd Date: Thu, 9 Sep 2021 11:08:11 +0100 Message-ID: <002801d7a562$997998a0$cc6cc9e0$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdelYf/La9ta//4rS5ms0b04hKQIDg== 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.0 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.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" As observed by Jakub in comment #2 of PR 98865, the expression -(a>>63) is optimized in GENERIC but not in GIMPLE. Investigating further it turns out that this is one of a few transformations performed by fold_negate_expr in fold-const.c that aren't yet performed by match.pd. This patch moves/duplicates them there, and should be relatively safe as these transformations are already performed by the compiler, but just in different passes. Alas the one minor complication is that some of these transformations are only wins, if the intermediate result (of the multiplication or division) is only used once, to avoid duplication/performing them again. See gcc.dg/tree-ssa/ssa-free-88.c. Normally, this is the perfect usage of match's single_use (aka SSA's has_single_use). Alas, single_use is not always accurate in match.pd, as some passes will construct and simplify an expression/stmt before inserting it into GIMPLE, and folding during this process sees the temporary undercount from the data-flow. To solve this, this patch introduces a new single_use_is_op_p that double checks that the single_use has the expected tree_code/operation and skips the transformation if we can tell single_use might be invalid. A follow-up patch might be to investigate whether genmatch.c can be tweaked to use this new helper function to implement the :s qualifier when the enclosing context is/should be known, but that's overkill to just unblock Jakub and Andrew on 98865. This patch has been tested on x86_64-pc-linux-gnu with a "make bootstrap" and "make -k check" with no new failures. Ok for mainline? 2021-09-09 Roger Sayle gcc/ChangeLog * generic-match-head.c (single_use_is_op_p): New helper function. * gimple-match-head.c (single_use_is_op_p): New helper function. * match.pd (negation simplifications): Implement some negation folding transformations from fold-const.c's fold_negate_expr. gcc/testsuite/ChangeLog * gcc.dg/fold-negate-1.c: New test case. Roger --- /* { dg-do compile } */ /* { dg-options "-O2 -fdump-tree-optimized" } */ #define SHIFT ((8*__SIZEOF_INT__)-1) int test_rshift_1(int x) { int t = x >> SHIFT; return -t; } int test_rshift_2(int x) { unsigned int t = (unsigned int)x >> SHIFT; return -t; } int test_rshift_3(int x) { int t = (unsigned int)x >> SHIFT; return -t; } int test_rshift_4(int x) { unsigned int t = x >> SHIFT; return -t; } double test_mul_1(double x) { double t = -5.0 * x; return -t; } double test_mul_2(double x, double y) { double t1 = -x; double t2 = t1 * y; return -t2; } double test_rdiv_1(double x, double y) { double t1 = -x; double t2 = t1 / y; return -t2; } double test_rdiv_2(double x, double y) { double t1 = -y; double t2 = x / t1; return -t2; } /* { dg-final { scan-tree-dump-not " -" "optimized" } } */ diff --git a/gcc/generic-match-head.c b/gcc/generic-match-head.c index f426208..64115a2 100644 --- a/gcc/generic-match-head.c +++ b/gcc/generic-match-head.c @@ -63,6 +63,16 @@ single_use (tree t ATTRIBUTE_UNUSED) return true; } +/* Like single_use above, but confirm that the single use (if any) + is an expression of tree_code OP. For GENERIC, assume the worst + and postpone folding transformations until GIMPLE. */ + +static inline bool +single_use_is_op_p (tree t ATTRIBUTE_UNUSED, tree_code op ATTRIBUTE_UNUSED) +{ + return false; +} + /* Return true if math operations should be canonicalized, e.g. sqrt(sqrt(x)) -> pow(x, 0.25). */ diff --git a/gcc/gimple-match-head.c b/gcc/gimple-match-head.c index 7112c11..2026f96a 100644 --- a/gcc/gimple-match-head.c +++ b/gcc/gimple-match-head.c @@ -1141,6 +1141,22 @@ single_use (tree t) return TREE_CODE (t) != SSA_NAME || has_zero_uses (t) || has_single_use (t); } +/* Like single_use above, but confirm that the single use (if any) + is an expression of tree_code OP. */ + +static inline bool +single_use_is_op_p (tree t, tree_code op) +{ + use_operand_p use; + gimple *stmt; + + return TREE_CODE (t) != SSA_NAME + || has_zero_uses (t) + || (single_imm_use (t, &use, &stmt) + && is_gimple_assign (stmt) + && gimple_assign_rhs_code (stmt) == op); +} + /* Return true if math operations should be canonicalized, e.g. sqrt(sqrt(x)) -> pow(x, 0.25). */ diff --git a/gcc/match.pd b/gcc/match.pd index 008f775..091da6c 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1481,6 +1481,36 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (!FIXED_POINT_TYPE_P (type)) (plus @0 (negate @1)))) +/* Other simplifications of negation (c.f. fold_negate_expr_1). */ +(simplify + (negate (mult:c@0 @1 negate_expr_p@2)) + (if (! TYPE_UNSIGNED (type) + && ! HONOR_SIGN_DEPENDENT_ROUNDING (type) + && single_use_is_op_p (@0, NEGATE_EXPR)) + (mult @1 (negate @2)))) + +(simplify + (negate (rdiv@0 @1 negate_expr_p@2)) + (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type) + && single_use_is_op_p (@0, NEGATE_EXPR)) + (rdiv @1 (negate @2)))) + +(simplify + (negate (rdiv@0 negate_expr_p@1 @2)) + (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type) + && single_use_is_op_p (@0, NEGATE_EXPR)) + (rdiv (negate @1) @2))) + +/* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */ +(simplify + (negate (convert? (rshift @0 INTEGER_CST@1))) + (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) + && wi::to_wide (@1) == element_precision (type) - 1) + (with { tree stype = TREE_TYPE (@0); + tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype) + : unsigned_type_for (stype); } + (convert (rshift:ntype (convert:ntype @0) @1))))) + /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST)) when profitable. For bitwise binary operations apply operand conversions to the