From patchwork Tue Sep 3 17:50:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1980251 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=quicinc.com header.i=@quicinc.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=aH9a1tB7; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WytTX5lFvz1yZ9 for ; Wed, 4 Sep 2024 03:51:12 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5EE893864819 for ; Tue, 3 Sep 2024 17:51:10 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by sourceware.org (Postfix) with ESMTPS id 6F88E3858402 for ; Tue, 3 Sep 2024 17:50:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6F88E3858402 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=quicinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6F88E3858402 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725385849; cv=none; b=frEIQeohVZ606s03xw8MgB5r3wJAvqTM1nMShInNNT+CCXbVQuSx828tp8faJUHe7FMKvFhQzrjwuqPEeVR/HTQ87POPNPDFJ+ffOCp6UEdrFS4ALGIuu7yxr/RlrGXzg5RIE5FBiRvKZx3Ove83ASr31jA37pIA4ViEY5A3DmI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725385849; c=relaxed/simple; bh=4alUvi0kpVWReSwp89pV+9jrdR14D6075cE9ogU8dpE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=P41ASgGi0AK2zez4PyV26X+GGB+y1mi8fLdp8IlV3xvfkF5NuA7NWYCekF5Ozbmli/BakZHH4PYsgcBQJ8nj+DXNjpzPt73fhH+D3XTeoAt+pmrZccttg+K9PmxZ47eP5kMvawWenizIi1VaJuVTO84zRwlfB/gnUULlsCgGHJ8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279865.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 483BAohl001693 for ; Tue, 3 Sep 2024 17:50:44 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:message-id :mime-version:subject:to; s=qcppdkim1; bh=wTtQC05xPk0lYZ1pQhfvCj UfS1BaP/JdhH4ZQzqmS7c=; b=aH9a1tB7q5kLC4kGRFAmpwHhTql7OQKjkREU6I q+LFtKd6Z4PaNfj36J6n9lXIBbyXBVa6aAhz4mgllDdDtZMbm1PCWjYhpvVAiGpD ipeB3ZSv+IJHM0PMr4j90hkB4HWjLh8OtmBdKqAl57FVCO3oW+kZfCZbrGdWCAOm e6bSPP8rul0boqc1uijHnGfNZsdRwiw4bExh3ERa6zngzFAmISvvDPmZEq63tYpD gYrmwNnnKJoIaRD+iikvpHvoqmaBEgbkw8t8KNj+MLudQ37aouvM7XY19IFXEf6s 7wo/giftdjrjP69NBQV9Ie/O8gKqhnrVN3dnEkA9wD1S4sfw== Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 41bt4rgc2v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 03 Sep 2024 17:50:44 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA01.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 483HohGn022090 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 3 Sep 2024 17:50:43 GMT Received: from hu-apinski-lv.qualcomm.com (10.49.16.6) by nasanex01c.na.qualcomm.com (10.45.79.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Tue, 3 Sep 2024 10:50:43 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] split-path: Improve ifcvt heurstic for split path [PR112402] Date: Tue, 3 Sep 2024 10:50:31 -0700 Message-ID: <20240903175031.2682032-1-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01a.na.qualcomm.com (10.47.209.196) To nasanex01c.na.qualcomm.com (10.45.79.139) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: miMZn4ngFNIg3bfp0rBVP_2DrhIf9fxB X-Proofpoint-ORIG-GUID: miMZn4ngFNIg3bfp0rBVP_2DrhIf9fxB X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-03_05,2024-09-03_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 phishscore=0 bulkscore=0 malwarescore=0 impostorscore=0 mlxlogscore=957 priorityscore=1501 spamscore=0 mlxscore=0 clxscore=1015 adultscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2407110000 definitions=main-2409030143 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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.30 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 This simplifies the heurstic for split path to see if the join bb is a ifcvt candidate. For the predecessors bbs need either to be empty or only have one statement in them which could be a decent ifcvt candidate. The previous heurstics would miss that: ``` if (a) goto B else goto C; B: goto C; C: c = PHI ``` Would be a decent ifcvt candidate. And would also miss: ``` if (a) goto B else goto C; B: d = f + 1; goto C; C: c = PHI ``` Also since currently the max number of cmovs being able to produced is 3, we should only assume `<= 3` phis can be ifcvt candidates. The testcase changes for split-path-6.c is that lookharder function is a true ifcvt case where we would get cmov as expected; it looks like it was not a candidate when the heurstic was added but became one later on. pr88797.C is now rejected via it being an ifcvt candidate rather than being about DCE/const prop. The rest of the testsuite changes are just slight change in the dump, removing the "*diamnond" part as it was removed from the print. Bootstrapped and tested on x86_64. PR tree-optimization/112402 gcc/ChangeLog: * gimple-ssa-split-paths.cc (poor_ifcvt_pred): New function. (is_feasible_trace): Remove old heurstics for ifcvt cases. For num_stmts <=1 for both pred check poor_ifcvt_pred on both pred. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/split-path-11.c: Update scan. * gcc.dg/tree-ssa/split-path-2.c: Update scan. * gcc.dg/tree-ssa/split-path-5.c: Update scan. * gcc.dg/tree-ssa/split-path-6.c: Update scan. * g++.dg/tree-ssa/pr88797.C: Update scan. * gcc.dg/tree-ssa/split-path-13.c: New test. Signed-off-by: Andrew Pinski --- gcc/gimple-ssa-split-paths.cc | 172 ++++++------------ gcc/testsuite/g++.dg/tree-ssa/pr88797.C | 2 +- gcc/testsuite/gcc.dg/tree-ssa/split-path-11.c | 2 +- gcc/testsuite/gcc.dg/tree-ssa/split-path-13.c | 26 +++ gcc/testsuite/gcc.dg/tree-ssa/split-path-2.c | 2 +- gcc/testsuite/gcc.dg/tree-ssa/split-path-5.c | 2 +- gcc/testsuite/gcc.dg/tree-ssa/split-path-6.c | 4 +- 7 files changed, 88 insertions(+), 122 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/split-path-13.c diff --git a/gcc/gimple-ssa-split-paths.cc b/gcc/gimple-ssa-split-paths.cc index 81a5d1dee5b..32b5c445760 100644 --- a/gcc/gimple-ssa-split-paths.cc +++ b/gcc/gimple-ssa-split-paths.cc @@ -35,6 +35,7 @@ along with GCC; see the file COPYING3. If not see #include "tree-phinodes.h" #include "ssa-iterators.h" #include "fold-const.h" +#include "cfghooks.h" /* Given LATCH, the latch block in a loop, see if the shape of the path reaching LATCH is suitable for being split by duplication. @@ -141,6 +142,40 @@ poor_ifcvt_candidate_code (enum tree_code code) || code == CALL_EXPR); } +/* Return TRUE if PRED of BB is an poor ifcvt candidate. */ +static bool +poor_ifcvt_pred (basic_block pred, basic_block bb) +{ + /* If the edge count of the pred is not 1, then + this is the predecessor from the if rather + than middle one. */ + if (EDGE_COUNT (pred->succs) != 1) + return false; + + /* Empty middle bb are never a poor ifcvt candidate. */ + if (empty_block_p (pred)) + return false; + /* If BB's predecessors are single statement blocks where + the output of that statement feed the same PHI in BB, + it an ifcvt candidate. */ + gimple *stmt = last_and_only_stmt (pred); + if (!stmt || gimple_code (stmt) != GIMPLE_ASSIGN) + return true; + tree_code code = gimple_assign_rhs_code (stmt); + if (poor_ifcvt_candidate_code (code)) + return true; + tree lhs = gimple_assign_lhs (stmt); + gimple_stmt_iterator gsi; + for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) + { + gimple *phi = gsi_stmt (gsi); + if (gimple_phi_arg_def (phi, 0) == lhs + || gimple_phi_arg_def (phi, 1) == lhs) + return false; + } + return true; +} + /* Return TRUE if BB is a reasonable block to duplicate by examining its size, false otherwise. BB will always be a loop latch block. @@ -181,127 +216,30 @@ is_feasible_trace (basic_block bb) } /* This is meant to catch cases that are likely opportunities for - if-conversion. Essentially we look for the case where - BB's predecessors are both single statement blocks where - the output of that statement feed the same PHI in BB. */ - if (num_stmts_in_pred1 == 1 && num_stmts_in_pred2 == 1) - { - gimple *stmt1 = last_and_only_stmt (pred1); - gimple *stmt2 = last_and_only_stmt (pred2); - - if (stmt1 && stmt2 - && gimple_code (stmt1) == GIMPLE_ASSIGN - && gimple_code (stmt2) == GIMPLE_ASSIGN) - { - enum tree_code code1 = gimple_assign_rhs_code (stmt1); - enum tree_code code2 = gimple_assign_rhs_code (stmt2); - - if (!poor_ifcvt_candidate_code (code1) - && !poor_ifcvt_candidate_code (code2)) - { - tree lhs1 = gimple_assign_lhs (stmt1); - tree lhs2 = gimple_assign_lhs (stmt2); - gimple_stmt_iterator gsi; - for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) - { - gimple *phi = gsi_stmt (gsi); - if ((gimple_phi_arg_def (phi, 0) == lhs1 - && gimple_phi_arg_def (phi, 1) == lhs2) - || (gimple_phi_arg_def (phi, 1) == lhs1 - && gimple_phi_arg_def (phi, 0) == lhs2)) - { - if (dump_file && (dump_flags & TDF_DETAILS)) - fprintf (dump_file, - "Block %d appears to be a join point for " - "if-convertable diamond.\n", - bb->index); - return false; - } - } - } - } - } - - /* Canonicalize the form. */ - if (num_stmts_in_pred1 == 0 && num_stmts_in_pred2 == 1) - { - std::swap (pred1, pred2); - std::swap (num_stmts_in_pred1, num_stmts_in_pred2); - } - - /* Another variant. This one is half-diamond. */ - if (num_stmts_in_pred1 == 1 && num_stmts_in_pred2 == 0 - && dominated_by_p (CDI_DOMINATORS, pred1, pred2)) + if-conversion. */ + if (num_stmts_in_pred1 <= 1 && num_stmts_in_pred2 <= 1) { - gimple *stmt1 = last_and_only_stmt (pred1); - - /* The only statement in PRED1 must be an assignment that is - not a good candidate for if-conversion. This may need some - generalization. */ - if (stmt1 && gimple_code (stmt1) == GIMPLE_ASSIGN) + int num_phis = 0; + /* The max number of PHIs that should be considered for an ifcvt + candidate. */ + const int max_num_phis = 3; + for (gphi_iterator si = gsi_start_phis (bb); ! gsi_end_p (si); + gsi_next (&si)) { - enum tree_code code1 = gimple_assign_rhs_code (stmt1); - - if (!poor_ifcvt_candidate_code (code1)) - { - tree lhs1 = gimple_assign_lhs (stmt1); - tree rhs1 = gimple_assign_rhs1 (stmt1); - - gimple_stmt_iterator gsi; - for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) - { - gimple *phi = gsi_stmt (gsi); - if ((gimple_phi_arg_def (phi, 0) == lhs1 - && gimple_phi_arg_def (phi, 1) == rhs1) - || (gimple_phi_arg_def (phi, 1) == lhs1 - && gimple_phi_arg_def (phi, 0) == rhs1)) - { - if (dump_file && (dump_flags & TDF_DETAILS)) - fprintf (dump_file, - "Block %d appears to be a join point for " - "if-convertable half-diamond.\n", - bb->index); - return false; - } - } - } + num_phis++; + if (num_phis > max_num_phis) + break; } - } - - /* Canonicalize the form. */ - if (single_pred_p (pred1) && single_pred (pred1) == pred2 - && num_stmts_in_pred1 == 0) - std::swap (pred1, pred2); - - /* This is meant to catch another kind of cases that are likely opportunities - for if-conversion. After canonicalizing, PRED2 must be an empty block and - PRED1 must be the only predecessor of PRED2. Moreover, PRED1 is supposed - to end with a cond_stmt which has the same args with the PHI in BB. */ - if (single_pred_p (pred2) && single_pred (pred2) == pred1 - && num_stmts_in_pred2 == 0) - { - if (gcond *cond_stmt = dyn_cast (*gsi_last_bb (pred1))) + if (num_phis <= max_num_phis + && !poor_ifcvt_pred (pred1, bb) + && !poor_ifcvt_pred (pred2, bb)) { - tree lhs = gimple_cond_lhs (cond_stmt); - tree rhs = gimple_cond_rhs (cond_stmt); - - gimple_stmt_iterator gsi; - for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) - { - gimple *phi = gsi_stmt (gsi); - if ((operand_equal_p (gimple_phi_arg_def (phi, 0), lhs) - && operand_equal_p (gimple_phi_arg_def (phi, 1), rhs)) - || (operand_equal_p (gimple_phi_arg_def (phi, 0), rhs) - && (operand_equal_p (gimple_phi_arg_def (phi, 1), lhs)))) - { - if (dump_file && (dump_flags & TDF_DETAILS)) - fprintf (dump_file, - "Block %d appears to be optimized to a join " - "point for if-convertable half-diamond.\n", - bb->index); - return false; - } - } + if (dump_file && (dump_flags & TDF_DETAILS)) + fprintf (dump_file, + "Block %d appears to be a join point for " + "if-convertable bbs.\n", + bb->index); + return false; } } diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr88797.C b/gcc/testsuite/g++.dg/tree-ssa/pr88797.C index 75391d6c049..df1df89fa67 100644 --- a/gcc/testsuite/g++.dg/tree-ssa/pr88797.C +++ b/gcc/testsuite/g++.dg/tree-ssa/pr88797.C @@ -12,5 +12,5 @@ void test_f(unsigned x, unsigned y) { } /* { dg-final { scan-tree-dump-not "Duplicating join block" "split-paths" } } */ -/* { dg-final { scan-tree-dump-times "Block . is a join that does not expose" 1 "split-paths" } } */ +/* { dg-final { scan-tree-dump-times "appears to be a join point for if-convertable bbs." 1 "split-paths" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/split-path-11.c b/gcc/testsuite/gcc.dg/tree-ssa/split-path-11.c index 66f57d92edb..6c15c16151b 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/split-path-11.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/split-path-11.c @@ -11,4 +11,4 @@ void foo(unsigned long long *M) } } -/* { dg-final { scan-tree-dump-times "join point for if-convertable half-diamond" 1 "split-paths" } } */ +/* { dg-final { scan-tree-dump-times "join point for if-convertable" 1 "split-paths" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/split-path-13.c b/gcc/testsuite/gcc.dg/tree-ssa/split-path-13.c new file mode 100644 index 00000000000..8a24972d050 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/split-path-13.c @@ -0,0 +1,26 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fsplit-paths -fdump-tree-split-paths-details " } */ +/* PR tree-optimization/112402 */ +/* This is similar to split-path-2.c but instead of the add + being inside both sides, we have a constant. */ + +int +foo(signed char *p, int n) +{ + int s = 0; + int i; + + for (i = 0; i < n; i++) { + int t; + if (p[i] >= 0) + t = 1; + else + t = -1; + s += t; + } + + return s; +} + +/* { dg-final { scan-tree-dump "appears to be a join point for if-convertable" "split-paths" } } */ + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/split-path-2.c b/gcc/testsuite/gcc.dg/tree-ssa/split-path-2.c index 8f503f236a6..73c21635388 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/split-path-2.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/split-path-2.c @@ -17,5 +17,5 @@ foo(signed char *p, int n) return s; } -/* { dg-final { scan-tree-dump "appears to be a join point for if-convertable diamond" "split-paths" } } */ +/* { dg-final { scan-tree-dump "appears to be a join point for if-convertable" "split-paths" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/split-path-5.c b/gcc/testsuite/gcc.dg/tree-ssa/split-path-5.c index 88c3a55b968..317a55f158a 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/split-path-5.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/split-path-5.c @@ -41,4 +41,4 @@ bmhi_init (const signed char *pattern) } } -/* { dg-final { scan-tree-dump-times "join point for if-convertable half-diamond" 1 "split-paths" } } */ +/* { dg-final { scan-tree-dump-times "join point for if-convertable" 1 "split-paths" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/split-path-6.c b/gcc/testsuite/gcc.dg/tree-ssa/split-path-6.c index 5f5dd157601..71e6362b10c 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/split-path-6.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/split-path-6.c @@ -57,6 +57,8 @@ oof (void) } } + +/* lookharder becomes an ifcvt'd/cmov. */ void lookharder (char *string) { @@ -73,4 +75,4 @@ lookharder (char *string) } } -/* { dg-final { scan-tree-dump-times "Duplicating join block" 3 "split-paths" } } */ +/* { dg-final { scan-tree-dump-times "Duplicating join block" 2 "split-paths" } } */