From patchwork Mon Apr 24 21:30:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 87168 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3016044vqo; Mon, 24 Apr 2023 14:32:14 -0700 (PDT) X-Google-Smtp-Source: AKy350YsIc73zZbHeTY+fjFocZKkiS/k+1Yvusbfdf3lDrFz95RPUeVQVkSVePuwGspiE3ePuVA2 X-Received: by 2002:aa7:d519:0:b0:501:fe46:480b with SMTP id y25-20020aa7d519000000b00501fe46480bmr13453201edq.15.1682371933996; Mon, 24 Apr 2023 14:32:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682371933; cv=none; d=google.com; s=arc-20160816; b=utrGyb2JSMhST2ufjaqivvrzBzq+C6jgZN2+kh8mP4328zQ7ibDETV8e4ZPW3rgPs+ 33lJr4Twz0or+022PuTCfQbPUlkvMIUoXS7920g/3Z37J+kHptXHfw9s9nuagAwbGV7q lSx4zz7wGvnh0Ii37wXjnHHbbfBkivZqK7zfN8DZ2HoH+bKyfNP9FIDRSQcJyvgN2Ufd 31a4hIsfd8lPRtdZGQfsrJLu3E4dglc4UWZCZnbkpHST3CFdAQlh9FYQL1PzJT3RDDm5 d1ml6bHX5WtqJ+YtA8J9eSvOgMbgpVBNtT6mQXzIUg0tbK7u2WlE0M2irfXMql5dFl32 2tTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=OGqr1we4K/C9CIFTaNskAIhmjFnSwYF9Hrj5NmTO44c=; b=jd2zYVjShIyGo1GCV5JA5YQvpy3elFTcMCHhE5HND7ZF4vpFXW+fxrNUqpJHpbvcA0 EQdQgy8zWHs6X5K25/o+FCgTxrR9xJ/Hh+ZblpoFYeTPpjSyxFrg2KhUkf/Nprwq8mbC GVvE0zwLmiz9LeUQpJmqhhc1ady/RCEn0ln+zoLa/aXAs+d/xXKQIsDilK8vQrU4lUZ6 DmF43CQTnokKX2uPqU9vZWFCREtK3YUIOjqrMeP+ggkVddywAJYDeXkxxYpOwY5M852T ycLCOG16E4YPsTWsP00Nxh34/ailgEMMs7HROeDdWhXyU9u/7/+osY0TGyM11tSNAsjc Dhdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=OX1A56st; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id z20-20020aa7d414000000b00506778b4f3csi8792775edq.375.2023.04.24.14.32.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Apr 2023 14:32:13 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=OX1A56st; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 44FE63853562 for ; Mon, 24 Apr 2023 21:31:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 44FE63853562 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682371875; bh=OGqr1we4K/C9CIFTaNskAIhmjFnSwYF9Hrj5NmTO44c=; h=To:CC:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=OX1A56stIKNKsZEp0etDbffafkBGLBo9sKQggHvP8GIpyih4ARsWfiZor0SdwVaHV txwezAtXp4iWrVpm4RgfieSqIhvAC6WiUPNI/zCxv5Cqw2NmgMm5vr3GMR6eZwqBnV ee3TeydyDpXfOYadQlg3BiANAm2UiajmoKpnwqHk= 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 2AF823858C50 for ; Mon, 24 Apr 2023 21:30:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2AF823858C50 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 33OKnSI1010728 for ; Mon, 24 Apr 2023 14:30:23 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3q4f3p7pyj-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Mon, 24 Apr 2023 14:30:23 -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.48; Mon, 24 Apr 2023 14:30:20 -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.48 via Frontend Transport; Mon, 24 Apr 2023 14:30:20 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 312223F70B5; Mon, 24 Apr 2023 14:30:19 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH 1/7] PHIOPT: Split out store elimination from phiopt Date: Mon, 24 Apr 2023 14:30:05 -0700 Message-ID: <20230424213011.528181-2-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230424213011.528181-1-apinski@marvell.com> References: <20230424213011.528181-1-apinski@marvell.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: w9vfLJ-NoCeWsvh-2X32o-rgn_LaUcyA X-Proofpoint-GUID: w9vfLJ-NoCeWsvh-2X32o-rgn_LaUcyA X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-04-24_11,2023-04-21_01,2023-02-09_01 X-Spam-Status: No, score=-14.5 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_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Andrew Pinski via Gcc-patches From: Andrew Pinski Reply-To: Andrew Pinski Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764094832881887146?= X-GMAIL-MSGID: =?utf-8?q?1764094832881887146?= Since the last cleanups, it made easier to see that we should split out the store elimination worker from tree_ssa_phiopt_worker function. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * tree-ssa-phiopt.cc (tree_ssa_phiopt_worker): Remove do_store_elim argument and split that part out to ... (store_elim_worker): This new function. (pass_cselim::execute): Call store_elim_worker. (pass_phiopt::execute): Update call to tree_ssa_phiopt_worker. --- gcc/tree-ssa-phiopt.cc | 180 ++++++++++++++++++++++++++++------------- 1 file changed, 126 insertions(+), 54 deletions(-) diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index 4a3ab8efb71..7f47b32576b 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -104,27 +104,19 @@ single_non_singleton_phi_for_edges (gimple_seq seq, edge e0, edge e1) return phi; } -/* The core routine of conditional store replacement and normal - phi optimizations. Both share much of the infrastructure in how - to match applicable basic block patterns. DO_STORE_ELIM is true - when we want to do conditional store replacement, false otherwise. +/* The core routine of phi optimizations. DO_HOIST_LOADS is true when we want to hoist adjacent loads out of diamond control flow patterns, false otherwise. */ static unsigned int -tree_ssa_phiopt_worker (bool do_store_elim, bool do_hoist_loads, bool early_p) +tree_ssa_phiopt_worker (bool do_hoist_loads, bool early_p) { basic_block bb; basic_block *bb_order; unsigned n, i; bool cfgchanged = false; - hash_set *nontrap = 0; calculate_dominance_info (CDI_DOMINATORS); - if (do_store_elim) - /* Calculate the set of non-trapping memory accesses. */ - nontrap = get_non_trapping (); - /* Search every basic block for COND_EXPR we may be able to optimize. We walk the blocks in order that guarantees that a block with @@ -148,7 +140,7 @@ tree_ssa_phiopt_worker (bool do_store_elim, bool do_hoist_loads, bool early_p) /* Check to see if the last statement is a GIMPLE_COND. */ gcond *cond_stmt = safe_dyn_cast (*gsi_last_bb (bb)); if (!cond_stmt) - continue; + continue; e1 = EDGE_SUCC (bb, 0); bb1 = e1->dest; @@ -158,12 +150,12 @@ tree_ssa_phiopt_worker (bool do_store_elim, bool do_hoist_loads, bool early_p) /* We cannot do the optimization on abnormal edges. */ if ((e1->flags & EDGE_ABNORMAL) != 0 || (e2->flags & EDGE_ABNORMAL) != 0) - continue; + continue; /* If either bb1's succ or bb2 or bb2's succ is non NULL. */ if (EDGE_COUNT (bb1->succs) == 0 || EDGE_COUNT (bb2->succs) == 0) - continue; + continue; /* Find the bb which is the fall through to the other. */ if (EDGE_SUCC (bb1, 0)->dest == bb2) @@ -192,39 +184,6 @@ tree_ssa_phiopt_worker (bool do_store_elim, bool do_hoist_loads, bool early_p) || (e1->flags & EDGE_FALLTHRU) == 0) continue; - if (do_store_elim) - { - if (diamond_p) - { - basic_block bb3 = e1->dest; - - /* Only handle sinking of store from 2 bbs only, - The middle bbs don't need to come from the - if always since we are sinking rather than - hoisting. */ - if (EDGE_COUNT (bb3->preds) != 2) - continue; - if (cond_if_else_store_replacement (bb1, bb2, bb3)) - cfgchanged = true; - continue; - } - - /* Also make sure that bb1 only have one predecessor and that it - is bb. */ - if (!single_pred_p (bb1) - || single_pred (bb1) != bb) - continue; - - /* bb1 is the middle block, bb2 the join block, bb the split block, - e1 the fallthrough edge from bb1 to bb2. We can't do the - optimization if the join block has more than two predecessors. */ - if (EDGE_COUNT (bb2->preds) > 2) - continue; - if (cond_store_replacement (bb1, bb2, e1, e2, nontrap)) - cfgchanged = true; - continue; - } - if (diamond_p) { basic_block bb3 = e1->dest; @@ -322,18 +281,132 @@ tree_ssa_phiopt_worker (bool do_store_elim, bool do_hoist_loads, bool early_p) free (bb_order); - if (do_store_elim) - delete nontrap; + if (cfgchanged) + return TODO_cleanup_cfg; + return 0; +} + +/* The core routine of conditional store replacement. */ +static unsigned int +store_elim_worker (void) +{ + basic_block bb; + basic_block *bb_order; + unsigned n, i; + bool cfgchanged = false; + hash_set *nontrap = 0; + + calculate_dominance_info (CDI_DOMINATORS); + + /* Calculate the set of non-trapping memory accesses. */ + nontrap = get_non_trapping (); + + /* Search every basic block for COND_EXPR we may be able to optimize. + + We walk the blocks in order that guarantees that a block with + a single predecessor is processed before the predecessor. + This ensures that we collapse inner ifs before visiting the + outer ones, and also that we do not try to visit a removed + block. */ + bb_order = single_pred_before_succ_order (); + n = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; + + for (i = 0; i < n; i++) + { + basic_block bb1, bb2; + edge e1, e2; + bool diamond_p = false; + + bb = bb_order[i]; + + /* Check to see if the last statement is a GIMPLE_COND. */ + gcond *cond_stmt = safe_dyn_cast (*gsi_last_bb (bb)); + if (!cond_stmt) + continue; + + e1 = EDGE_SUCC (bb, 0); + bb1 = e1->dest; + e2 = EDGE_SUCC (bb, 1); + bb2 = e2->dest; + + /* We cannot do the optimization on abnormal edges. */ + if ((e1->flags & EDGE_ABNORMAL) != 0 + || (e2->flags & EDGE_ABNORMAL) != 0) + continue; + + /* If either bb1's succ or bb2 or bb2's succ is non NULL. */ + if (EDGE_COUNT (bb1->succs) == 0 + || EDGE_COUNT (bb2->succs) == 0) + continue; + + /* Find the bb which is the fall through to the other. */ + if (EDGE_SUCC (bb1, 0)->dest == bb2) + ; + else if (EDGE_SUCC (bb2, 0)->dest == bb1) + { + std::swap (bb1, bb2); + std::swap (e1, e2); + } + else if (EDGE_SUCC (bb1, 0)->dest == EDGE_SUCC (bb2, 0)->dest + && single_succ_p (bb2)) + { + diamond_p = true; + e2 = EDGE_SUCC (bb2, 0); + /* Make sure bb2 is just a fall through. */ + if ((e2->flags & EDGE_FALLTHRU) == 0) + continue; + } + else + continue; + + e1 = EDGE_SUCC (bb1, 0); + + /* Make sure that bb1 is just a fall through. */ + if (!single_succ_p (bb1) + || (e1->flags & EDGE_FALLTHRU) == 0) + continue; + + if (diamond_p) + { + basic_block bb3 = e1->dest; + + /* Only handle sinking of store from 2 bbs only, + The middle bbs don't need to come from the + if always since we are sinking rather than + hoisting. */ + if (EDGE_COUNT (bb3->preds) != 2) + continue; + if (cond_if_else_store_replacement (bb1, bb2, bb3)) + cfgchanged = true; + continue; + } + + /* Also make sure that bb1 only have one predecessor and that it + is bb. */ + if (!single_pred_p (bb1) + || single_pred (bb1) != bb) + continue; + + /* bb1 is the middle block, bb2 the join block, bb the split block, + e1 the fallthrough edge from bb1 to bb2. We can't do the + optimization if the join block has more than two predecessors. */ + if (EDGE_COUNT (bb2->preds) > 2) + continue; + if (cond_store_replacement (bb1, bb2, e1, e2, nontrap)) + cfgchanged = true; + } + + free (bb_order); + + delete nontrap; /* If the CFG has changed, we should cleanup the CFG. */ - if (cfgchanged && do_store_elim) + if (cfgchanged) { /* In cond-store replacement we have added some loads on edges and new VOPS (as we moved the store, and created a load). */ gsi_commit_edge_inserts (); return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals; } - else if (cfgchanged) - return TODO_cleanup_cfg; return 0; } @@ -4257,8 +4330,7 @@ public: bool gate (function *) final override { return flag_ssa_phiopt; } unsigned int execute (function *) final override { - return tree_ssa_phiopt_worker (false, - !early_p ? gate_hoist_loads () : false, + return tree_ssa_phiopt_worker (!early_p ? gate_hoist_loads () : false, early_p); } @@ -4360,7 +4432,7 @@ pass_cselim::execute (function *) An interfacing issue of find_data_references_in_bb. */ loop_optimizer_init (LOOPS_NORMAL); scev_initialize (); - todo = tree_ssa_phiopt_worker (true, false, false); + todo = store_elim_worker (); scev_finalize (); loop_optimizer_finalize (); return todo;