]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
forwprop: Also dce from added statements from gimple_simplify
authorAndrew Pinski <quic_apinski@quicinc.com>
Sat, 17 Aug 2024 19:14:54 +0000 (12:14 -0700)
committerAndrew Pinski <quic_apinski@quicinc.com>
Sun, 18 Aug 2024 07:10:13 +0000 (00:10 -0700)
This extends r14-3982-g9ea74d235c7e78 to also include the newly added statements
since some of them might be dead too (due to the way match and simplify works).
This was noticed while working on adding a new match and simplify pattern where a
new statement that got added was not being used.

Bootstrapped and tested on x86_64-linux-gnu with no regressions.

gcc/ChangeLog:

* gimple-fold.cc (mark_lhs_in_seq_for_dce): New function.
(replace_stmt_with_simplification): Call mark_lhs_in_seq_for_dce
right before inserting the sequence.
(fold_stmt_1): Add dce_worklist argument, update call to
replace_stmt_with_simplification.
(fold_stmt): Add dce_worklist argument, update call to fold_stmt_1.
(fold_stmt_inplace): Update call to fold_stmt_1.
* gimple-fold.h (fold_stmt): Add bitmap argument.
* tree-ssa-forwprop.cc (pass_forwprop::execute): Update call to fold_stmt.

Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
gcc/gimple-fold.cc
gcc/gimple-fold.h
gcc/tree-ssa-forwprop.cc

index 18d7a6b176db7ee2d7cd68dcf418b596bbf0dee0..0bec35d06f66b9c8760ec11d1714620e15493067 100644 (file)
@@ -5914,6 +5914,24 @@ has_use_on_stmt (tree name, gimple *stmt)
   return false;
 }
 
+/* Add the lhs of each statement of SEQ to DCE_WORKLIST. */
+
+static void
+mark_lhs_in_seq_for_dce (bitmap dce_worklist, gimple_seq seq)
+{
+  if (!dce_worklist)
+    return;
+
+  for (gimple_stmt_iterator i = gsi_start (seq);
+       !gsi_end_p (i); gsi_next (&i))
+    {
+      gimple *stmt = gsi_stmt (i);
+      tree name = gimple_get_lhs (stmt);
+      if (name && TREE_CODE (name) == SSA_NAME)
+       bitmap_set_bit (dce_worklist, SSA_NAME_VERSION (name));
+    }
+}
+
 /* Worker for fold_stmt_1 dispatch to pattern based folding with
    gimple_simplify.
 
@@ -5924,7 +5942,8 @@ has_use_on_stmt (tree name, gimple *stmt)
 static bool
 replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
                                  gimple_match_op *res_op,
-                                 gimple_seq *seq, bool inplace)
+                                 gimple_seq *seq, bool inplace,
+                                 bitmap dce_worklist)
 {
   gimple *stmt = gsi_stmt (*gsi);
   tree *ops = res_op->ops;
@@ -5992,6 +6011,8 @@ replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
          print_gimple_stmt (dump_file, gsi_stmt (*gsi),
                             0, TDF_SLIM);
        }
+      // Mark the lhs of the new statements maybe for dce
+      mark_lhs_in_seq_for_dce (dce_worklist, *seq);
       gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
       return true;
     }
@@ -6015,6 +6036,8 @@ replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
              print_gimple_stmt (dump_file, gsi_stmt (*gsi),
                                 0, TDF_SLIM);
            }
+         // Mark the lhs of the new statements maybe for dce
+         mark_lhs_in_seq_for_dce (dce_worklist, *seq);
          gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
          return true;
        }
@@ -6032,6 +6055,8 @@ replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
            print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
          print_gimple_stmt (dump_file, gsi_stmt (*gsi), 0, TDF_SLIM);
        }
+      // Mark the lhs of the new statements maybe for dce
+      mark_lhs_in_seq_for_dce (dce_worklist, *seq);
       gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
       return true;
     }
@@ -6047,6 +6072,8 @@ replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
              fprintf (dump_file, "gimple_simplified to ");
              print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
            }
+         // Mark the lhs of the new statements maybe for dce
+         mark_lhs_in_seq_for_dce (dce_worklist, *seq);
          gsi_replace_with_seq_vops (gsi, *seq);
          return true;
        }
@@ -6214,7 +6241,8 @@ maybe_canonicalize_mem_ref_addr (tree *t, bool is_debug = false)
    distinguishes both cases.  */
 
 static bool
-fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree))
+fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree),
+            bitmap dce_worklist = nullptr)
 {
   bool changed = false;
   gimple *stmt = gsi_stmt (*gsi);
@@ -6382,7 +6410,8 @@ fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree))
       if (gimple_simplify (stmt, &res_op, inplace ? NULL : &seq,
                           valueize, valueize))
        {
-         if (replace_stmt_with_simplification (gsi, &res_op, &seq, inplace))
+         if (replace_stmt_with_simplification (gsi, &res_op, &seq, inplace,
+                                               dce_worklist))
            changed = true;
          else
            gimple_seq_discard (seq);
@@ -6537,15 +6566,15 @@ follow_all_ssa_edges (tree val)
    which can produce *&x = 0.  */
 
 bool
-fold_stmt (gimple_stmt_iterator *gsi)
+fold_stmt (gimple_stmt_iterator *gsi, bitmap dce_bitmap)
 {
-  return fold_stmt_1 (gsi, false, no_follow_ssa_edges);
+  return fold_stmt_1 (gsi, false, no_follow_ssa_edges, dce_bitmap);
 }
 
 bool
-fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree))
+fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree), bitmap dce_bitmap)
 {
-  return fold_stmt_1 (gsi, false, valueize);
+  return fold_stmt_1 (gsi, false, valueize, dce_bitmap);
 }
 
 /* Perform the minimal folding on statement *GSI.  Only operations like
index 182561b813292d0a63ed34a6422538034c0fb84d..dc709d515a98497a64b0d67ce33d209667fe24de 100644 (file)
@@ -29,8 +29,8 @@ struct c_strlen_data;
 extern bool get_range_strlen (tree, c_strlen_data *, unsigned eltsize);
 extern void gimplify_and_update_call_from_tree (gimple_stmt_iterator *, tree);
 extern bool update_gimple_call (gimple_stmt_iterator *, tree, int, ...);
-extern bool fold_stmt (gimple_stmt_iterator *);
-extern bool fold_stmt (gimple_stmt_iterator *, tree (*) (tree));
+extern bool fold_stmt (gimple_stmt_iterator *, bitmap = nullptr);
+extern bool fold_stmt (gimple_stmt_iterator *, tree (*) (tree), bitmap = nullptr);
 extern bool fold_stmt_inplace (gimple_stmt_iterator *);
 extern tree maybe_fold_and_comparisons (tree, enum tree_code, tree, tree,
                                        enum tree_code, tree, tree,
index 2e37642359c96b9e0909d80a5c72e36760be9110..9595555138cfde2adf3dc3d6de43ddcdd55f9251 100644 (file)
@@ -3954,7 +3954,7 @@ pass_forwprop::execute (function *fun)
                if (uses.space (1))
                  uses.quick_push (USE_FROM_PTR (usep));
 
-             if (fold_stmt (&gsi, fwprop_ssa_val))
+             if (fold_stmt (&gsi, fwprop_ssa_val, simple_dce_worklist))
                {
                  changed = true;
                  stmt = gsi_stmt (gsi);