I am testing the following patch for an optimization regression where
a loop made dead by final value replacement was made used again by
DOM 20 passes later.  The real issue here is that we do not get rid
of dead loops until very late so this patch makes sure to do that.
We could schedule it later (but better no later than unrolling
as that might expose a pretty inefficient way of removing a dead loop).

Bootstrap and regtest running on x86_64-unknown-linux-gnu.

Richard.

2016-11-24  Richard Biener  <rguent...@suse.de>

        PR tree-optimization/78343
        * passes.def: Add CD-DCE pass after loop splitting.
        * tree-ssa-dce.c (find_obviously_necessary_stmts): Move
        SCEV init/finalize ...
        (perform_tree_ssa_dce): ... here.  Deal with being
        executed inside the loop pipeline in aggressive mode.

        * gcc.dg/tree-ssa/sccp-2.c: New testcase.

Index: gcc/passes.def
===================================================================
--- gcc/passes.def      (revision 242827)
+++ gcc/passes.def      (working copy)
@@ -271,6 +271,9 @@ along with GCC; see the file COPYING3.
          NEXT_PASS (pass_tree_unswitch);
          NEXT_PASS (pass_scev_cprop);
          NEXT_PASS (pass_loop_split);
+         /* All unswitching, final value replacement and splitting can expose
+            empty loops.  Remove them now.  */
+         NEXT_PASS (pass_cd_dce);
          NEXT_PASS (pass_record_bounds);
          NEXT_PASS (pass_loop_distribution);
          NEXT_PASS (pass_copy_prop);
Index: gcc/tree-ssa-dce.c
===================================================================
--- gcc/tree-ssa-dce.c  (revision 242827)
+++ gcc/tree-ssa-dce.c  (working copy)
@@ -400,7 +400,6 @@ find_obviously_necessary_stmts (bool agg
   if (aggressive)
     {
       struct loop *loop;
-      scev_initialize ();
       if (mark_irreducible_loops ())
        FOR_EACH_BB_FN (bb, cfun)
          {
@@ -423,7 +422,6 @@ find_obviously_necessary_stmts (bool agg
              fprintf (dump_file, "can not prove finiteness of loop %i\n", 
loop->num);
            mark_control_dependent_edges_necessary (loop->latch, false);
          }
-      scev_finalize ();
     }
 }
 
@@ -1567,9 +1565,13 @@ perform_tree_ssa_dce (bool aggressive)
   /* Preheaders are needed for SCEV to work.
      Simple lateches and recorded exits improve chances that loop will
      proved to be finite in testcases such as in loop-15.c and loop-24.c  */
-  if (aggressive)
-    loop_optimizer_init (LOOPS_NORMAL
-                        | LOOPS_HAVE_RECORDED_EXITS);
+  bool in_loop_pipeline = scev_initialized_p ();
+  if (aggressive && ! in_loop_pipeline)
+    {
+      scev_initialize ();
+      loop_optimizer_init (LOOPS_NORMAL
+                          | LOOPS_HAVE_RECORDED_EXITS);
+    }
 
   tree_dce_init (aggressive);
 
@@ -1588,8 +1590,11 @@ perform_tree_ssa_dce (bool aggressive)
 
   find_obviously_necessary_stmts (aggressive);
 
-  if (aggressive)
-    loop_optimizer_finalize ();
+  if (aggressive && ! in_loop_pipeline)
+    {
+      loop_optimizer_finalize ();
+      scev_finalize ();
+    }
 
   longest_chain = 0;
   total_chain = 0;
@@ -1623,7 +1628,7 @@ perform_tree_ssa_dce (bool aggressive)
   if (something_changed)
     {
       free_numbers_of_iterations_estimates (cfun);
-      if (scev_initialized_p ())
+      if (in_loop_pipeline)
        scev_reset ();
       return TODO_update_ssa | TODO_cleanup_cfg;
     }
Index: gcc/testsuite/gcc.dg/tree-ssa/sccp-2.c
===================================================================
--- gcc/testsuite/gcc.dg/tree-ssa/sccp-2.c      (revision 0)
+++ gcc/testsuite/gcc.dg/tree-ssa/sccp-2.c      (working copy)
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+unsigned int
+test(unsigned int quant)
+{
+  unsigned int sum = 0;
+  for (unsigned int i = 0; i < quant; ++i)
+    sum += quant;
+  return sum;
+}
+
+/* A single basic-block should remain (computing and
+   returning quant * quant).  */
+/* { dg-final { scan-tree-dump-times "bb" 1 "optimized" } } */

Reply via email to