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>
---
 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 538a0588d33..0130a809e30 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;
-- 
2.43.0

Reply via email to