From patchwork Fri Jul 9 05:33:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 1502905 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=RSR+jIcR; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4GLhhN5y1Rz9sWS for ; Fri, 9 Jul 2021 15:35:32 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5AF0939890AE for ; Fri, 9 Jul 2021 05:35:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5AF0939890AE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1625808930; bh=NFSkCfpl8ynm7cf92TkcqMj82OVzykRV4Ejy5vmhVaI=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=RSR+jIcR9yK/EIGbklxo2a508+kl4vYuhwV+ShonTmaAa9tGdkzfCUjdWONcIimYq 8Lp7Q/V9rhWxXDuIwkl6roEUmD/qQP3jcfajForBvFN3zN5UfsFlkWGEtEOX0Xk6HN 47wS6CiAJwd8oW3iLfzNCY8H5RJXJVlCEct0Pv+g= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 7BB04385AC24 for ; Fri, 9 Jul 2021 05:33:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7BB04385AC24 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 1695Tavs027672 for ; Thu, 8 Jul 2021 22:33:08 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com with ESMTP id 39ny0gbyjk-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Thu, 08 Jul 2021 22:33:08 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Thu, 8 Jul 2021 22:33:06 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.18 via Frontend Transport; Thu, 8 Jul 2021 22:33:05 -0700 Received: from linux.wrightpinski.org.com (unknown [10.69.242.197]) by maili.marvell.com (Postfix) with ESMTP id C91303F7080; Thu, 8 Jul 2021 22:33:05 -0700 (PDT) To: Subject: [PATCH 1/2] Improve early simplify and match for phiopt Date: Thu, 8 Jul 2021 22:33:00 -0700 Message-ID: <1625808781-21156-2-git-send-email-apinski@marvell.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1625808781-21156-1-git-send-email-apinski@marvell.com> References: <1625808781-21156-1-git-send-email-apinski@marvell.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: vlg3Bw6cNY-pRALIFpYPh5rwTDV1CnFR X-Proofpoint-GUID: vlg3Bw6cNY-pRALIFpYPh5rwTDV1CnFR X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391, 18.0.790 definitions=2021-07-09_01:2021-07-09, 2021-07-09 signatures=0 X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, 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: , X-Patchwork-Original-From: apinski--- via Gcc-patches From: "Li, Pan2 via Gcc-patches" Reply-To: apinski@marvell.com Cc: Andrew Pinski Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" From: Andrew Pinski Previously the idea was gimple_simplify_phiopt would call resimplify with a NULL sequence but that sometimes fails even if there was only one statement produced. The cases where it fails is when there are two simplifications happen. In the case of the min/max production, the first simplifcation produces: (convert (min @1 @2)) And then the convert is removed by a second one. The Min statement will be in the sequence while the op will be a SSA name. This was rejected before as could not produce something in the sequence. So this patch changes the way resimplify is called to always passing a pointer to the sequence and then decide based on if op is a SSA_NAME or not. OK? Bootstrapped and tested on x86_64-linux-gnu. gcc/ChangeLog: * tree-ssa-phiopt.c (phiopt_early_allow): Change arguments to take sequence and gimple_match_op. Accept the case where op is a SSA_NAME and one statement in the sequence. Also allow constants. (gimple_simplify_phiopt): Always pass a sequence to resimplify. Update call to phiopt_early_allow. Discard the sequence if not used. --- gcc/tree-ssa-phiopt.c | 62 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/gcc/tree-ssa-phiopt.c b/gcc/tree-ssa-phiopt.c index 8b60ee81082..7a98b7afdf1 100644 --- a/gcc/tree-ssa-phiopt.c +++ b/gcc/tree-ssa-phiopt.c @@ -812,11 +812,33 @@ two_value_replacement (basic_block cond_bb, basic_block middle_bb, return true; } -/* Return TRUE if CODE should be allowed during early phiopt. - Currently this is to allow MIN/MAX and ABS/NEGATE. */ +/* Return TRUE if SEQ/OP pair should be allowed during early phiopt. + Currently this is to allow MIN/MAX and ABS/NEGATE and constants. */ static bool -phiopt_early_allow (enum tree_code code) +phiopt_early_allow (gimple_seq &seq, gimple_match_op &op) { + /* Don't allow functions. */ + if (!op.code.is_tree_code ()) + return false; + tree_code code = (tree_code)op.code; + + /* For non-empty sequence, only allow one statement. */ + if (!gimple_seq_empty_p (seq)) + { + /* Check to make sure op was already a SSA_NAME. */ + if (code != SSA_NAME) + return false; + if (!gimple_seq_singleton_p (seq)) + return false; + gimple *stmt = gimple_seq_first_stmt (seq); + /* Only allow assignments. */ + if (!is_gimple_assign (stmt)) + return false; + if (gimple_assign_lhs (stmt) != op.ops[0]) + return false; + code = gimple_assign_rhs_code (stmt); + } + switch (code) { case MIN_EXPR: @@ -826,6 +848,11 @@ phiopt_early_allow (enum tree_code code) case NEGATE_EXPR: case SSA_NAME: return true; + case INTEGER_CST: + case REAL_CST: + case VECTOR_CST: + case FIXED_CST: + return true; default: return false; } @@ -844,6 +871,7 @@ gimple_simplify_phiopt (bool early_p, tree type, gimple *comp_stmt, gimple_seq *seq) { tree result; + gimple_seq seq1 = NULL; enum tree_code comp_code = gimple_cond_code (comp_stmt); location_t loc = gimple_location (comp_stmt); tree cmp0 = gimple_cond_lhs (comp_stmt); @@ -858,18 +886,23 @@ gimple_simplify_phiopt (bool early_p, tree type, gimple *comp_stmt, gimple_match_op op (gimple_match_cond::UNCOND, COND_EXPR, type, cond, arg0, arg1); - if (op.resimplify (early_p ? NULL : seq, follow_all_ssa_edges)) + if (op.resimplify (&seq1, follow_all_ssa_edges)) { /* Early we want only to allow some generated tree codes. */ if (!early_p - || op.code.is_tree_code () - || phiopt_early_allow ((tree_code)op.code)) + || phiopt_early_allow (seq1, op)) { - result = maybe_push_res_to_seq (&op, seq); + result = maybe_push_res_to_seq (&op, &seq1); if (result) - return result; + { + gimple_seq_add_seq_without_update (seq, seq1); + return result; + } } } + gimple_seq_discard (seq1); + seq1 = NULL; + /* Try the inverted comparison, that is !COMP ? ARG1 : ARG0. */ comp_code = invert_tree_comparison (comp_code, HONOR_NANS (cmp0)); @@ -882,18 +915,21 @@ gimple_simplify_phiopt (bool early_p, tree type, gimple *comp_stmt, gimple_match_op op1 (gimple_match_cond::UNCOND, COND_EXPR, type, cond, arg1, arg0); - if (op1.resimplify (early_p ? NULL : seq, follow_all_ssa_edges)) + if (op1.resimplify (&seq1, follow_all_ssa_edges)) { /* Early we want only to allow some generated tree codes. */ if (!early_p - || op1.code.is_tree_code () - || phiopt_early_allow ((tree_code)op1.code)) + || phiopt_early_allow (seq1, op1)) { - result = maybe_push_res_to_seq (&op1, seq); + result = maybe_push_res_to_seq (&op1, &seq1); if (result) - return result; + { + gimple_seq_add_seq_without_update (seq, seq1); + return result; + } } } + gimple_seq_discard (seq1); return NULL; } From patchwork Fri Jul 9 05:33:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 1502903 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=X2NauxFt; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.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 4GLhgj54Y8z9sCD for ; Fri, 9 Jul 2021 15:34:56 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 91493385DC08 for ; Fri, 9 Jul 2021 05:34:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 91493385DC08 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1625808892; bh=wRNAa2u6kXqBPyMpq52Lc2CrqLdLYayMpaZx5wrD998=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=X2NauxFtxU4jwgLBkS6gqCDeKxvqqr4MfFZFzTbG8+MQ2kd29qbkKjEdiS1tTukmj q8KvMzqLZW4QqAI3toa3gSw6yKOqmKSYTjSMfSni9/UIAFmDxzYJ8BCY0yuy0Q/Mms KcTipoGNZfj8PAr65uxfSAlpcULt3x37ZgqxPGns= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 3AF45385B804 for ; Fri, 9 Jul 2021 05:33:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3AF45385B804 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 1695Tavt027672 for ; Thu, 8 Jul 2021 22:33:09 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com with ESMTP id 39ny0gbyjk-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Thu, 08 Jul 2021 22:33:08 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Thu, 8 Jul 2021 22:33:06 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.18 via Frontend Transport; Thu, 8 Jul 2021 22:33:06 -0700 Received: from linux.wrightpinski.org.com (unknown [10.69.242.197]) by maili.marvell.com (Postfix) with ESMTP id 320CF3F7082; Thu, 8 Jul 2021 22:33:06 -0700 (PDT) To: Subject: [PATCH 2/2] [PHIOPT/MATCH] Remove the statement to move if not used Date: Thu, 8 Jul 2021 22:33:01 -0700 Message-ID: <1625808781-21156-3-git-send-email-apinski@marvell.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1625808781-21156-1-git-send-email-apinski@marvell.com> References: <1625808781-21156-1-git-send-email-apinski@marvell.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: akkxm7aAcnHcov4Q4kNIol1PEHQBE4nE X-Proofpoint-GUID: akkxm7aAcnHcov4Q4kNIol1PEHQBE4nE X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391, 18.0.790 definitions=2021-07-09_01:2021-07-09, 2021-07-09 signatures=0 X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, 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: , X-Patchwork-Original-From: apinski--- via Gcc-patches From: "Li, Pan2 via Gcc-patches" Reply-To: apinski@marvell.com Cc: Andrew Pinski Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" From: Andrew Pinski Instead of waiting for DCE to remove the unused statement, and maybe optimize another conditional, it is better if we don't move the statement and have the statement removed. gcc/ChangeLog: * tree-ssa-phiopt.c (used_in_seq): New function. (match_simplify_replacement): Don't move the statement if not used in sequence. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr96928-1.c: Update to similar as pr96928.c. --- gcc/testsuite/gcc.dg/tree-ssa/pr96928-1.c | 5 ++++- gcc/tree-ssa-phiopt.c | 24 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr96928-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr96928-1.c index 2e86620da11..9e505ac9900 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/pr96928-1.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr96928-1.c @@ -2,7 +2,10 @@ /* { dg-do compile } */ /* { dg-options "-O2 -fdump-tree-phiopt2 -fdump-tree-optimized" } */ /* { dg-final { scan-tree-dump-times " = a_\[0-9]*\\\(D\\\) >> " 5 "phiopt2" } } */ -/* { dg-final { scan-tree-dump-times " = ~c_\[0-9]*\\\(D\\\);" 1 "phiopt2" } } */ +/* The following check is done at optimized because a ^ (~b) is rewritten as ~(a^b) + and in the case of match.pd optimizing these ?:, the ~ is moved out already + by the time we get to phiopt2. */ +/* { dg-final { scan-tree-dump-times "c_\[0-9]*\\\(D\\\) \\\^" 1 "optimized" } } */ /* { dg-final { scan-tree-dump-times " = ~" 1 "optimized" } } */ /* { dg-final { scan-tree-dump-times " = \[abc_0-9\\\(\\\)D]* \\\^ " 5 "phiopt2" } } */ /* { dg-final { scan-tree-dump-not "a < 0" "phiopt2" } } */ diff --git a/gcc/tree-ssa-phiopt.c b/gcc/tree-ssa-phiopt.c index 7a98b7afdf1..a237df02153 100644 --- a/gcc/tree-ssa-phiopt.c +++ b/gcc/tree-ssa-phiopt.c @@ -934,6 +934,26 @@ gimple_simplify_phiopt (bool early_p, tree type, gimple *comp_stmt, return NULL; } +/* Return true if the lhs of STMT is used in the SEQ sequence + of statements. */ +static bool +used_in_seq (gimple *stmt, gimple_seq seq) +{ + tree lhs = gimple_assign_lhs (stmt); + for (auto gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next_nondebug (&gsi)) + { + use_operand_p use_p; + ssa_op_iter iter; + gimple *stmt1 = gsi_stmt (gsi); + FOR_EACH_SSA_USE_OPERAND (use_p, stmt1, iter, SSA_OP_USE) + { + if (USE_FROM_PTR (use_p) == lhs) + return true; + } + } + return false; +} + /* The function match_simplify_replacement does the main work of doing the replacement using match and simplify. Return true if the replacement is done. Otherwise return false. @@ -1020,7 +1040,9 @@ match_simplify_replacement (basic_block cond_bb, basic_block middle_bb, return false; gsi = gsi_last_bb (cond_bb); - if (stmt_to_move) + if (stmt_to_move + && (gimple_assign_lhs (stmt_to_move) == result + || used_in_seq (stmt_to_move, seq))) { if (dump_file && (dump_flags & TDF_DETAILS)) {