From: Andrew Pinski <apin...@marvell.com>

Instead of putting a full blow DCE after execute_fixup_cfg, it makes sense
to try to remove the defining statement for the store that is being removed.
Using simple_dce_from_worklist makes this easier, just mark the ssa_name on
the rhs side of the store (if it was one) in a bitmap and then call
simple_dce_from_worklist at the end.

gcc.dg/pr36902.c needed to be changed such that the static array was no
longer a static array but a global array.  This is because this new dce
will remove the load as it is dead. I also filed PR 102864 for the warning
on dead loads.

gcc/ChangeLog:

        * tree-cfg.c (maybe_remove_writeonly_store): Add dce_ssa_names argument.
        Mark the ssa-name of the rhs as one to be removed.
        (execute_fixup_cfg): Update call to maybe_remove_writeonly_store.
        Call simple_dce_from_worklist at the end to a simple dce.

gcc/testsuite/ChangeLog:

        * gcc.dg/pr36902.c: Move buf to be a non-static variable.
---
 gcc/testsuite/gcc.dg/pr36902.c |  5 +----
 gcc/tree-cfg.c                 | 18 ++++++++++++++++--
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/pr36902.c b/gcc/testsuite/gcc.dg/pr36902.c
index 7dafc9ac171..365a26e26b7 100644
--- a/gcc/testsuite/gcc.dg/pr36902.c
+++ b/gcc/testsuite/gcc.dg/pr36902.c
@@ -24,10 +24,9 @@ struct {
   unsigned char pcr_select[4];
 } sel;
 
+unsigned char buf[64];
 int bar(void)
 {
-  static unsigned char buf[64];
-
   sel.size_of_select = 3;
   foo(buf, sel.pcr_select, sel.size_of_select);
 
@@ -52,8 +51,6 @@ foo2(unsigned char * to, const unsigned char * from, int n)
 
 int baz(void)
 {
-  static unsigned char buf[64];
-
   sel.size_of_select = 5;
   foo2(buf, sel.pcr_select, sel.size_of_select);
 
diff --git a/gcc/tree-cfg.c b/gcc/tree-cfg.c
index dbbf6beb6e4..b3a27bcd17c 100644
--- a/gcc/tree-cfg.c
+++ b/gcc/tree-cfg.c
@@ -54,6 +54,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "value-prof.h"
 #include "tree-inline.h"
 #include "tree-ssa-live.h"
+#include "tree-ssa-dce.h"
 #include "omp-general.h"
 #include "omp-expand.h"
 #include "tree-cfgcleanup.h"
@@ -9669,7 +9670,8 @@ make_pass_warn_unused_result (gcc::context *ctxt)
 /* Maybe Remove stores to variables we marked write-only.
    Return true if a store was removed. */
 static bool
-maybe_remove_writeonly_store (gimple_stmt_iterator &gsi, gimple *stmt)
+maybe_remove_writeonly_store (gimple_stmt_iterator &gsi, gimple *stmt,
+                             bitmap dce_ssa_names)
 {
   /* Keep access when store has side effect, i.e. in case when source
      is volatile.  */  
@@ -9692,6 +9694,15 @@ maybe_remove_writeonly_store (gimple_stmt_iterator &gsi, 
gimple *stmt)
       print_gimple_stmt (dump_file, stmt, 0,
                         TDF_VOPS|TDF_MEMSYMS);
     }
+
+  /* Mark ssa name defining to be checked for simple dce. */
+  if (gimple_assign_single_p (stmt))
+    {
+      tree rhs = gimple_assign_rhs1 (stmt);
+      if (TREE_CODE (rhs) == SSA_NAME
+         && !SSA_NAME_IS_DEFAULT_DEF (rhs))
+       bitmap_set_bit (dce_ssa_names, SSA_NAME_VERSION (rhs));
+    }
   unlink_stmt_vdef (stmt);
   gsi_remove (&gsi, true);
   release_defs (stmt);
@@ -9714,6 +9725,7 @@ execute_fixup_cfg (void)
   profile_count num = node->count;
   profile_count den = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
   bool scale = num.initialized_p () && !(num == den);
+  auto_bitmap dce_ssa_names;
 
   if (scale)
     {
@@ -9754,7 +9766,7 @@ execute_fixup_cfg (void)
             }
 
          /* Remove stores to variables we marked write-only. */
-         if (maybe_remove_writeonly_store (gsi, stmt))
+         if (maybe_remove_writeonly_store (gsi, stmt, dce_ssa_names))
            {
              todo |= TODO_update_ssa | TODO_cleanup_cfg;
              continue;
@@ -9820,6 +9832,8 @@ execute_fixup_cfg (void)
       && (todo & TODO_cleanup_cfg))
     loops_state_set (LOOPS_NEED_FIXUP);
 
+  simple_dce_from_worklist (dce_ssa_names);
+
   return todo;
 }
 
-- 
2.17.1

Reply via email to