From patchwork Sun Apr 30 21:13:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 88957 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2329285vqo; Sun, 30 Apr 2023 14:14:59 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4ybI8l1VP7+p2QRWDQGX8Zt5xJMQ/B61oMPq6SYOsqjYwr3eDdraZHHDScMIjuALPxHZOE X-Received: by 2002:a17:906:dc92:b0:94e:ec0f:455 with SMTP id cs18-20020a170906dc9200b0094eec0f0455mr14286384ejc.54.1682889299633; Sun, 30 Apr 2023 14:14:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682889299; cv=none; d=google.com; s=arc-20160816; b=JIg3B+A0RfQ04+KalJZbM6Cj+mjf9G8JbPPYqflaO5PCvMIj5Wj6LuQGxwvlERT1li R4sXMWUtwNeKNxk7cRMmcksx7RY/CUq6HxXwkwuKLktvsy+GZwpWQSXXWdQtjjKLf2S1 +lYMLwLO2/UQAC0bDeE47I0kWHimvp9MFFodCwyGZ31/nDVRf/BZeqNIzLAtK89NvLm4 jlaakIV30OJHSz4eVk2tIL9Kk4snNCvjHCRLd2i36r4ZNxfHxQ9pO9c1PBJiQ4XkK1s0 SZeJwfQL51gCQQWAjJ6ihqtDKDONuCNG5Ek0eoyKSULi58NykPRaAdHxL6CXJrpI5L26 mxdg== 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:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=lWjjlG/6KqjsxUSAPRUCQzOwKB7TzkK/Lfs+t/kgAaA=; b=aIAB+XlZYUnsZyTwEb3ufS4lXh69Qckr6Ea0MS55es/TKvqvpw6Af5vyMeqaW566D+ reyFCi33oSs8bHmjKo9/1gHdyOuMcrLW+1fzDl737/xlYy4kWMUhaLD2aRiwE8r3ykRb 3uQIl00wJfVZ84Fc7Q4Uu3xjLQjoXAG/LQZODxt8hb4q5kqNO5Yt4rO7QXbAOSHUSyS4 KkHHGxrux9OjJwctCDXh7G5v1P/Ar+tVFgbA1yXJo1WNn0iWaNWcBlx/fR/8iw8SZ+9I GtqM9lwE3NMdgM48y55bgiGMaY3hSam7erJRzf6dRNiKuKvJLCn3mZsrjmp8vsC9vZPM GOtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Qu4D5adB; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id lj8-20020a170907188800b0095ed7845fbcsi8508675ejc.900.2023.04.30.14.14.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Apr 2023 14:14:59 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Qu4D5adB; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 506FD3857708 for ; Sun, 30 Apr 2023 21:14:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 506FD3857708 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682889290; bh=lWjjlG/6KqjsxUSAPRUCQzOwKB7TzkK/Lfs+t/kgAaA=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Qu4D5adBjuEZEcY7C1RBFH8xmeqOTA7dYjr3IiEaEgJ4+uWnRe3odQRR5fGLa4sqT p9TMY72mjEhGHsULxO6RZgPEDQ8Sws6V3Upi22oZtSY1wqE+STnftHL421HRi20SWA G2t/DTOA4/0Pob+ixQIh4YYV1tdF3uatc1pjpvyw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by sourceware.org (Postfix) with ESMTPS id 6DF71385840E for ; Sun, 30 Apr 2023 21:14:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6DF71385840E Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 33UKr943009310 for ; Sun, 30 Apr 2023 14:14:03 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3q90krv1k8-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sun, 30 Apr 2023 14:14:03 -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.48; Sun, 30 Apr 2023 14:14:02 -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.48 via Frontend Transport; Sun, 30 Apr 2023 14:14:01 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id A9B393F70AC; Sun, 30 Apr 2023 14:14:01 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] PHIOPT: Improve replace_phi_edge_with_variable for diamond shapped bb Date: Sun, 30 Apr 2023 14:13:56 -0700 Message-ID: <20230430211356.762030-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: PuOU8GMX4MoOaufAVoJKaKEnFPhVQlDm X-Proofpoint-GUID: PuOU8GMX4MoOaufAVoJKaKEnFPhVQlDm 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-30_08,2023-04-27_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?1764637330380038729?= X-GMAIL-MSGID: =?utf-8?q?1764637330380038729?= While looking at differences between what minmax_replacement and match_simplify_replacement does. I noticed that they sometimes chose different edges to remove. I decided we should be able to do better and be able to remove both empty basic blocks in the case of match_simplify_replacement as that moves the statements. This also updates the testcases as now match_simplify_replacement will remove the unused MIN/MAX_EXPR and they were checking for those. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * tree-ssa-phiopt.cc (copy_phi_args): New function. (replace_phi_edge_with_variable): Handle diamond form bb with forwarder only empty blocks better. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/minmax-15.c: Update test. * gcc.dg/tree-ssa/minmax-16.c: Update test. * gcc.dg/tree-ssa/minmax-3.c: Update test. * gcc.dg/tree-ssa/minmax-4.c: Update test. * gcc.dg/tree-ssa/minmax-5.c: Update test. * gcc.dg/tree-ssa/minmax-8.c: Update test. --- gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c | 3 +- gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c | 9 ++-- gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c | 2 +- gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c | 2 +- gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c | 2 +- gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c | 2 +- gcc/tree-ssa-phiopt.cc | 51 ++++++++++++++++++++++- 7 files changed, 59 insertions(+), 12 deletions(-) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c index 8a39871c938..6731f91e6c3 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c @@ -30,5 +30,6 @@ main (void) return 0; } -/* { dg-final { scan-tree-dump-times "MIN_EXPR" 3 "phiopt1" } } */ +/* There should only be two MIN_EXPR left, the 3rd one was removed. */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "phiopt1" } } */ /* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "phiopt1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c index 623b12b3f74..094364e6424 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c @@ -25,11 +25,8 @@ main (void) return 0; } -/* After phiopt1, there really should be only 3 MIN_EXPR in the IR (including debug statements). - But the way phiopt does not cleanup the CFG all the time, the PHI might still reference the - alternative bb's moved statement. - Note in the end, we do dce the statement and other debug statements to end up with only 2 MIN_EXPR. - So check that too. */ -/* { dg-final { scan-tree-dump-times "MIN_EXPR" 4 "phiopt1" } } */ +/* After phiopt1, will be only 2 MIN_EXPR in the IR (including debug statements). */ +/* xk will only have the final result so the extra debug info does not change anything. */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "phiopt1" } } */ /* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "optimized" } } */ /* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "phiopt1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c index 2af10776346..521afe3e4d9 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c @@ -25,5 +25,5 @@ main (void) return 0; } -/* { dg-final { scan-tree-dump-times "MIN_EXPR" 3 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "phiopt1" } } */ /* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "phiopt1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c index 973f39bfed3..49e27185b5e 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c @@ -26,4 +26,4 @@ main (void) } /* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "phiopt1" } } */ -/* { dg-final { scan-tree-dump-times "MAX_EXPR" 3 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 2 "phiopt1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c index 34e4e720511..194c881cc98 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c @@ -25,5 +25,5 @@ main (void) return 0; } -/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "phiopt1" } } */ /* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c index 0160e573fef..d5cb53145ea 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c @@ -26,4 +26,4 @@ main (void) } /* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "phiopt1" } } */ -/* { dg-final { scan-tree-dump-times "MAX_EXPR" 2 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt1" } } */ diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index 65b3deea34a..311423efeb5 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -82,6 +82,25 @@ single_non_singleton_phi_for_edges (gimple_seq seq, edge e0, edge e1) return phi; } +/* For each PHI in BB, copy the argument associated with SRC_E to TGT_E. */ + +static void +copy_phi_args (basic_block bb, edge src_e, edge tgt_e) +{ + gphi_iterator gsi; + int src_indx = src_e->dest_idx; + + for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) + { + gphi *phi = gsi.phi (); + tree def = gimple_phi_arg_def (phi, src_indx); + location_t locus = gimple_phi_arg_location (phi, src_indx); + + add_phi_arg (phi, def, tgt_e, locus); + } +} + + /* Replace PHI node element whose edge is E in block BB with variable NEW. Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK is known to have two edges, one of which must reach BB). */ @@ -94,6 +113,7 @@ replace_phi_edge_with_variable (basic_block cond_block, basic_block bb = gimple_bb (phi); gimple_stmt_iterator gsi; tree phi_result = PHI_RESULT (phi); + bool deleteboth = false; /* Duplicate range info if they are the only things setting the target PHI. This is needed as later on, the new_tree will be replacing @@ -137,7 +157,14 @@ replace_phi_edge_with_variable (basic_block cond_block, keep_edge = EDGE_SUCC (cond_block, 1); } else if ((keep_edge = find_edge (cond_block, e->src))) - ; + { + basic_block bb1 = EDGE_SUCC (cond_block, 0)->dest; + basic_block bb2 = EDGE_SUCC (cond_block, 1)->dest; + if (single_pred_p (bb1) && single_pred_p (bb2) + && single_succ_p (bb1) && single_succ_p (bb2) + && empty_block_p (bb1) && empty_block_p (bb2)) + deleteboth = true; + } else gcc_unreachable (); @@ -148,6 +175,28 @@ replace_phi_edge_with_variable (basic_block cond_block, e->probability = profile_probability::always (); delete_basic_block (edge_to_remove->dest); + /* Eliminate the COND_EXPR at the end of COND_BLOCK. */ + gsi = gsi_last_bb (cond_block); + gsi_remove (&gsi, true); + } + else if (deleteboth) + { + basic_block bb1 = EDGE_SUCC (cond_block, 0)->dest; + basic_block bb2 = EDGE_SUCC (cond_block, 1)->dest; + + edge newedge = redirect_edge_and_branch (keep_edge, bb); + /* no new edge should be the same. */ + gcc_assert (newedge == keep_edge); + keep_edge->flags |= EDGE_FALLTHRU; + keep_edge->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE); + keep_edge->probability = profile_probability::always (); + /* Copy the edge's phi entry from the old one */ + copy_phi_args(bb, e, keep_edge); + + /* Delete the old 2 empty basic blocks */ + delete_basic_block (bb1); + delete_basic_block (bb2); + /* Eliminate the COND_EXPR at the end of COND_BLOCK. */ gsi = gsi_last_bb (cond_block); gsi_remove (&gsi, true);