From patchwork Thu Nov 5 13:18:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiufu Guo X-Patchwork-Id: 1394968 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@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gcc.gnu.org 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=cgrN2+uQ; dkim-atps=neutral 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 4CRkcb0JKsz9sSs for ; Fri, 6 Nov 2020 00:18:52 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2D4C3384A02A; Thu, 5 Nov 2020 13:18:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2D4C3384A02A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1604582329; bh=esCQGSbDe0ejUFMiyeBfcn9CEhZiH1MUB4GJavgTBkA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=cgrN2+uQO8NEVv86l9EpistKlhuL558npKmbioudJAhNu6FWuKictR7MHrM99Q2OY 1+ewd7aGnS2Ch+P8OBN0o8pgSrh+gZXjkxW7CAzxROifW1h+4kVvRReaBGvaqYQLW0 I40EfExd7u03KypnIiprYcUehBvr/FrQwAJzWvoY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id EF4753861810 for ; Thu, 5 Nov 2020 13:18:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org EF4753861810 Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 0A5D2c5P103342; Thu, 5 Nov 2020 08:18:44 -0500 Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 34mfdg5rhk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 05 Nov 2020 08:18:44 -0500 Received: from m0098393.ppops.net (m0098393.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 0A5D35Ov110757; Thu, 5 Nov 2020 08:18:44 -0500 Received: from ppma01fra.de.ibm.com (46.49.7a9f.ip4.static.sl-reverse.com [159.122.73.70]) by mx0a-001b2d01.pphosted.com with ESMTP id 34mfdg5rg2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 05 Nov 2020 08:18:44 -0500 Received: from pps.filterd (ppma01fra.de.ibm.com [127.0.0.1]) by ppma01fra.de.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 0A5DHPv7004887; Thu, 5 Nov 2020 13:18:41 GMT Received: from b06avi18626390.portsmouth.uk.ibm.com (b06avi18626390.portsmouth.uk.ibm.com [9.149.26.192]) by ppma01fra.de.ibm.com with ESMTP id 34jbytsvy9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 05 Nov 2020 13:18:41 +0000 Received: from d06av26.portsmouth.uk.ibm.com (d06av26.portsmouth.uk.ibm.com [9.149.105.62]) by b06avi18626390.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 0A5DIcGb59179518 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 5 Nov 2020 13:18:38 GMT Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9D3F8AE056; Thu, 5 Nov 2020 13:18:38 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7C481AE053; Thu, 5 Nov 2020 13:18:37 +0000 (GMT) Received: from genoa.aus.stglabs.ibm.com (unknown [9.40.192.157]) by d06av26.portsmouth.uk.ibm.com (Postfix) with ESMTP; Thu, 5 Nov 2020 13:18:37 +0000 (GMT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] Clean up loop-closed PHIs at loopdone pass Date: Thu, 5 Nov 2020 21:18:36 +0800 Message-Id: <20201105131836.1043501-1-guojiufu@linux.ibm.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.312, 18.0.737 definitions=2020-11-05_07:2020-11-05, 2020-11-05 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 bulkscore=0 mlxlogscore=999 spamscore=0 mlxscore=0 lowpriorityscore=0 priorityscore=1501 impostorscore=0 clxscore=1015 phishscore=0 adultscore=0 suspectscore=3 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011050086 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: guojiufu via Gcc-patches From: Jiufu Guo Reply-To: guojiufu Cc: rguenther@suse.de, segher@kernel.crashing.org, wschmidt@linux.ibm.com, dje.gcc@gmail.com Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" In PR87473, there are discussions about loop-closed PHIs which are generated for loop optimization passes. It would be helpful to clean them up after loop optimization is done, then this may simplify some jobs of following passes. This patch introduces a cheaper way to propagate them out in pass_tree_loop_done. This patch passes bootstrap and regtest on ppc64le. Is this ok for trunk? gcc/ChangeLog 2020-10-05 Jiufu Guo * tree-ssa-loop.h (clean_up_loop_closed_phi): New declaration. * tree-ssa-loop.c (tree_ssa_loop_done): Call clean_up_loop_closed_phi. * tree-ssa-propagate.c (propagate_rhs_into_lhs): New function. gcc/testsuite/ChangeLog 2020-10-05 Jiufu Guo * gcc.dg/tree-ssa/loopclosedphi.c: New test. --- gcc/testsuite/gcc.dg/tree-ssa/loopclosedphi.c | 21 +++ gcc/tree-ssa-loop.c | 1 + gcc/tree-ssa-loop.h | 1 + gcc/tree-ssa-propagate.c | 120 ++++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/loopclosedphi.c 2.25.1 diff --git a/gcc/testsuite/gcc.dg/tree-ssa/loopclosedphi.c b/gcc/testsuite/gcc.dg/tree-ssa/loopclosedphi.c new file mode 100644 index 00000000000..d71b757fbca --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/loopclosedphi.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fno-tree-ch -w -fdump-tree-loopdone-details" } */ + +void +t6 (int qz, int wh) +{ + int jl = wh; + + while (1.0 * qz / wh < 1) + { + qz = wh * (wh + 2); + + while (wh < 1) + jl = 0; + } + + while (qz < 1) + qz = jl * wh; +} + +/* { dg-final { scan-tree-dump-times "Replacing" 2 "loopdone"} } */ diff --git a/gcc/tree-ssa-loop.c b/gcc/tree-ssa-loop.c index 5e8365d4e83..7d680b2f5d2 100644 --- a/gcc/tree-ssa-loop.c +++ b/gcc/tree-ssa-loop.c @@ -530,6 +530,7 @@ tree_ssa_loop_done (void) free_numbers_of_iterations_estimates (cfun); scev_finalize (); loop_optimizer_finalize (); + clean_up_loop_closed_phi (cfun); return 0; } diff --git a/gcc/tree-ssa-loop.h b/gcc/tree-ssa-loop.h index 9e35125e6e8..baa940b9d1e 100644 --- a/gcc/tree-ssa-loop.h +++ b/gcc/tree-ssa-loop.h @@ -67,6 +67,7 @@ public: extern bool for_each_index (tree *, bool (*) (tree, tree *, void *), void *); extern char *get_lsm_tmp_name (tree ref, unsigned n, const char *suffix = NULL); extern unsigned tree_num_loop_insns (class loop *, struct eni_weights *); +extern unsigned clean_up_loop_closed_phi (function *); /* Returns the loop of the statement STMT. */ diff --git a/gcc/tree-ssa-propagate.c b/gcc/tree-ssa-propagate.c index 87dbf55fab9..813143852b9 100644 --- a/gcc/tree-ssa-propagate.c +++ b/gcc/tree-ssa-propagate.c @@ -1549,4 +1549,123 @@ propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val) else gcc_unreachable (); } + +/* Propagate RHS into all uses of LHS (when possible). + + RHS and LHS are derived from STMT, which is passed in solely so + that we can remove it if propagation is successful. */ + +static bool +propagate_rhs_into_lhs (gphi *stmt, tree lhs, tree rhs) +{ + use_operand_p use_p; + imm_use_iterator iter; + gimple_stmt_iterator gsi; + gimple *use_stmt; + bool changed = false; + bool all = true; + + /* Dump details. */ + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, " Replacing '"); + print_generic_expr (dump_file, lhs, dump_flags); + fprintf (dump_file, "' with '"); + print_generic_expr (dump_file, rhs, dump_flags); + fprintf (dump_file, "'\n"); + } + + /* Walk over every use of LHS and try to replace the use with RHS. */ + FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs) + { + /* It is not safe to propagate into below stmts. */ + if (gimple_debug_bind_p (use_stmt) + || (gimple_code (use_stmt) == GIMPLE_ASM + && !may_propagate_copy_into_asm (lhs)) + || (TREE_CODE (rhs) == SSA_NAME + && SSA_NAME_DEF_STMT (rhs) == use_stmt)) + { + all = false; + continue; + } + + /* Dump details. */ + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, " Original statement:"); + print_gimple_stmt (dump_file, use_stmt, 0, dump_flags); + } + + /* Propagate the RHS into this use of the LHS. */ + FOR_EACH_IMM_USE_ON_STMT (use_p, iter) + propagate_value (use_p, rhs); + + /* Propagation may expose new operands to the renamer. */ + update_stmt (use_stmt); + + /* If variable index is replaced with a constant, then + update the invariant flag for ADDR_EXPRs. */ + if (gimple_assign_single_p (use_stmt) + && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ADDR_EXPR) + recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (use_stmt)); + + /* Dump details. */ + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, " Updated statement:"); + print_gimple_stmt (dump_file, use_stmt, 0, dump_flags); + } + + changed = true; + } + + /* Remove the degenerate PHI node. */ + if (all) + { + gsi = gsi_for_stmt (stmt); + remove_phi_node (&gsi, true); + } + + return changed; +} + +/* Check exits of each loop in FUN, walk over loop closed PHIs in + each exit basic block and propagate degenerate PHIs. */ + +unsigned +clean_up_loop_closed_phi (function *fun) +{ + unsigned i; + edge e; + gphi *phi; + tree rhs; + tree lhs; + gphi_iterator gsi; + struct loop *loop; + bool cfg_altered = false; + + /* Walk over loop in function. */ + FOR_EACH_LOOP_FN (fun, loop, 0) + { + /* Check each exit edge of loop. */ + auto_vec exits = get_loop_exit_edges (loop); + FOR_EACH_VEC_ELT (exits, i, e) + if (single_pred_p (e->dest)) + /* Walk over loop-closed PHIs. */ + for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi);) + { + phi = gsi.phi (); + rhs = degenerate_phi_result (phi); + lhs = gimple_phi_result (phi); + + /* Advance the iterator before stmt is removed. */ + gsi_next (&gsi); + + if (rhs && !virtual_operand_p (lhs) + && may_propagate_copy (lhs, rhs)) + cfg_altered |= propagate_rhs_into_lhs (phi, lhs, rhs); + } + } + + return cfg_altered; +} --