https://gcc.gnu.org/g:24949e640307f91a831e0fb699fea85fb9276a09
commit r15-5843-g24949e640307f91a831e0fb699fea85fb9276a09 Author: Andrew Pinski <quic_apin...@quicinc.com> Date: Sat Nov 30 13:12:13 2024 -0800 gimple-lim: Reuse boolean var when moving PHI While looking into PR 117859, I noticed that LIM sometimes would produce `bool_var2 = bool_var1 != 0` instead of just using bool_var2. This patch allows LIM to reuse bool_var1 in the place where bool_var2 was going to be used. Bootstrapped and tested on x86_64-linux-gnu. gcc/ChangeLog: * tree-ssa-loop-im.cc (move_computations_worker): While moving phi, reuse the lhs of the conditional if it is a boolean type. Signed-off-by: Andrew Pinski <quic_apin...@quicinc.com> Diff: --- gcc/tree-ssa-loop-im.cc | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/gcc/tree-ssa-loop-im.cc b/gcc/tree-ssa-loop-im.cc index 538a0588d332..0130a809e30f 100644 --- a/gcc/tree-ssa-loop-im.cc +++ b/gcc/tree-ssa-loop-im.cc @@ -1304,11 +1304,22 @@ move_computations_worker (basic_block bb) edges of COND. */ extract_true_false_args_from_phi (dom, stmt, &arg0, &arg1); gcc_assert (arg0 && arg1); - t = make_ssa_name (boolean_type_node); - new_stmt = gimple_build_assign (t, gimple_cond_code (cond), - gimple_cond_lhs (cond), - gimple_cond_rhs (cond)); - gsi_insert_on_edge (loop_preheader_edge (level), new_stmt); + /* For `bool_val != 0`, reuse bool_val. */ + if (gimple_cond_code (cond) == NE_EXPR + && integer_zerop (gimple_cond_rhs (cond)) + && types_compatible_p (TREE_TYPE (gimple_cond_lhs (cond)), + boolean_type_node)) + { + t = gimple_cond_lhs (cond); + } + else + { + t = make_ssa_name (boolean_type_node); + new_stmt = gimple_build_assign (t, gimple_cond_code (cond), + gimple_cond_lhs (cond), + gimple_cond_rhs (cond)); + gsi_insert_on_edge (loop_preheader_edge (level), new_stmt); + } new_stmt = gimple_build_assign (gimple_phi_result (stmt), COND_EXPR, t, arg0, arg1); todo |= TODO_cleanup_cfg;