> Am 18.08.2024 um 00:57 schrieb Andrew Pinski <quic_apin...@quicinc.com>:
>
> 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.
Ok
Richard
> 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_apin...@quicinc.com>
> ---
> gcc/gimple-fold.cc | 43 +++++++++++++++++++++++++++++++++-------
> gcc/gimple-fold.h | 4 ++--
> gcc/tree-ssa-forwprop.cc | 2 +-
> 3 files changed, 39 insertions(+), 10 deletions(-)
>
> diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc
> index 18d7a6b176d..0bec35d06f6 100644
> --- a/gcc/gimple-fold.cc
> +++ b/gcc/gimple-fold.cc
> @@ -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
> diff --git a/gcc/gimple-fold.h b/gcc/gimple-fold.h
> index 182561b8132..dc709d515a9 100644
> --- a/gcc/gimple-fold.h
> +++ b/gcc/gimple-fold.h
> @@ -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,
> diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc
> index 2e37642359c..9595555138c 100644
> --- a/gcc/tree-ssa-forwprop.cc
> +++ b/gcc/tree-ssa-forwprop.cc
> @@ -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);
> --
> 2.43.0
>