]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Clean up loop-closed PHIs after loop finalize
authorguojiufu <guojiufu@linux.ibm.com>
Tue, 17 Nov 2020 11:49:03 +0000 (19:49 +0800)
committerguojiufu <guojiufu@linux.ibm.com>
Wed, 18 Nov 2020 05:29:14 +0000 (13:29 +0800)
This patch propagates loop-closed PHIs them out at
loop_optimizer_finalize.  For some cases, to clean up loop-closed PHIs
would save efforts of optimization passes after loopdone.

Thanks,
Jiufu Guo.

gcc/ChangeLog:
2020-10-18  Jiufu Guo   <guojiufu@linux.ibm.com>

* cfgloop.h (loop_optimizer_finalize): Add flag argument.
* loop-init.c (loop_optimizer_finalize): Call clean_up_loop_closed_phi.
* tree-cfgcleanup.h (clean_up_loop_closed_phi): New declare.
* tree-ssa-loop.c (tree_ssa_loop_done): Call loop_optimizer_finalize
with flag argument.
* tree-ssa-propagate.c (clean_up_loop_closed_phi): New function.

gcc/testsuite/ChangeLog:
2020-10-18  Jiufu Guo   <guojiufu@linux.ibm.com>

* gcc.dg/tree-ssa/loopclosedphi.c: New test.

gcc/cfgloop.h
gcc/loop-init.c
gcc/testsuite/gcc.dg/tree-ssa/loopclosedphi.c [new file with mode: 0644]
gcc/tree-cfgcleanup.h
gcc/tree-ssa-loop.c
gcc/tree-ssa-propagate.c

index d14689dc31fcd9922c39ed75c5118c6cf9e9b06d..438b1f779bb1e78d274a18642bf3ec216000a1ba 100644 (file)
@@ -824,7 +824,7 @@ extern void init_set_costs (void);
 
 /* Loop optimizer initialization.  */
 extern void loop_optimizer_init (unsigned);
-extern void loop_optimizer_finalize (function *);
+extern void loop_optimizer_finalize (function *, bool = false);
 inline void
 loop_optimizer_finalize ()
 {
index 401e5282907215a74abc44b6aac5c2a9d134508a..ac87dafef6eeac3fa1d19991e5f6556739dd9566 100644 (file)
@@ -33,6 +33,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-ssa-loop-niter.h"
 #include "loop-unroll.h"
 #include "tree-scalar-evolution.h"
+#include "tree-cfgcleanup.h"
 
 \f
 /* Apply FLAGS to the loop state.  */
@@ -133,13 +134,19 @@ loop_optimizer_init (unsigned flags)
 /* Finalize loop structures.  */
 
 void
-loop_optimizer_finalize (struct function *fn)
+loop_optimizer_finalize (struct function *fn, bool clean_loop_closed_phi)
 {
   class loop *loop;
   basic_block bb;
 
   timevar_push (TV_LOOP_FINI);
 
+  if (clean_loop_closed_phi && loops_state_satisfies_p (fn, LOOP_CLOSED_SSA))
+    {
+      clean_up_loop_closed_phi (fn);
+      loops_state_clear (fn, LOOP_CLOSED_SSA);
+    }
+
   if (loops_state_satisfies_p (fn, LOOPS_HAVE_RECORDED_EXITS))
     release_recorded_exits (fn);
 
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/loopclosedphi.c b/gcc/testsuite/gcc.dg/tree-ssa/loopclosedphi.c
new file mode 100644 (file)
index 0000000..d71b757
--- /dev/null
@@ -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"} } */
index 6ff6726bfe4557c100b5e4cc3ef7f2f4c5820254..9e368d63709eb878d392059f68efc7f9df3b797f 100644 (file)
@@ -26,5 +26,6 @@ extern bool cleanup_tree_cfg (unsigned = 0);
 extern bool fixup_noreturn_call (gimple *stmt);
 extern bool delete_unreachable_blocks_update_callgraph (cgraph_node *dst_node,
                                                        bool update_clones);
+extern unsigned clean_up_loop_closed_phi (function *);
 
 #endif /* GCC_TREE_CFGCLEANUP_H */
index 5e8365d4e8363bc10029383d2150ced806c4bc31..339a0c50bc8a3873b7cd6384c9c498de51a4f157 100644 (file)
@@ -529,7 +529,7 @@ tree_ssa_loop_done (void)
 {
   free_numbers_of_iterations_estimates (cfun);
   scev_finalize ();
-  loop_optimizer_finalize ();
+  loop_optimizer_finalize (cfun, true);
   return 0;
 }
 
index 87dbf55fab965b55d49155a51b89d59ac712cbc9..354057b48bf3f960a3c0e9345639af911de6875a 100644 (file)
@@ -1549,3 +1549,75 @@ propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
   else
     gcc_unreachable ();
 }
+
+/* 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;
+
+  /* Avoid possibly quadratic work when scanning for loop exits across
+   all loops of a nest.  */
+  if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
+    return 0;
+
+  /* Walk over loop in function.  */
+  FOR_EACH_LOOP_FN (fun, loop, 0)
+    {
+      /* Check each exit edege of loop.  */
+      auto_vec<edge> 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 = gimple_phi_arg_def (phi, 0);
+             lhs = gimple_phi_result (phi);
+
+             if (rhs && may_propagate_copy (lhs, rhs))
+               {
+                 /* 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");
+                   }
+
+                 use_operand_p use_p;
+                 imm_use_iterator iter;
+                 gimple *use_stmt;
+                 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
+                   {
+                     FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
+                       replace_exp (use_p, rhs);
+                     update_stmt (use_stmt);
+
+                     /* Update the invariant flag for ADDR_EXPR if replacing
+                        a variable index with a constant.  */
+                     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));
+                   }
+                 remove_phi_node (&gsi, true);
+               }
+             else
+               gsi_next (&gsi);
+           }
+    }
+
+  return 0;
+}