https://gcc.gnu.org/g:44aa5e2291a46c9306eb52ca357d29a94a7d2090

commit r17-5546-g44aa5e2291a46c9306eb52ca357d29a94a7d2090
Author: Andrea Pinski <[email protected]>
Date:   Thu Oct 22 16:53:26 2026 -0700

    PHIOPT: Improve replace_phi_edge_with_variable for diamond shapped bb 
(again)
    
    This improves on top of r14-478-gd256dc28f0eadd.  Instead of checking if
    the two middle bbs are empty, we can check if the new phi entry is depends
    on either branch instead.  Since we move the statements in
    match_simplify_replacement, this does not make a difference.  This will
    make a difference when we support multiple stmts though. As we might not 
remove
    the middles bb even though we could.  I don't have a testcase for that
    but a LLM based review of allowing more than one stmt to be phiopt marked
    as a possibility of wrong code/ICE (neither is true due to the way the code
    is structed as there would be no uses of those statements left behind as we
    would have replaced the phi entry for that case).
    This helps the next patch which is the `allowing more than one stmt to be 
phiopt`.
    
    gcc/ChangeLog:
    
            * tree-ssa-phiopt.cc (replace_phi_edge_with_variable): Handle 
non-empty
            middle bbs for diamond case.
            (match_simplify_replacement): Mark phi args to be maybe dce'd also.
    
    Signed-off-by: Andrea Pinski <[email protected]>

Diff:
---
 gcc/tree-ssa-phiopt.cc | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index a8c3a94a3f96..53256d1f7879 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -145,9 +145,15 @@ replace_phi_edge_with_variable (basic_block cond_block,
     {
       basic_block bb1 = EDGE_SUCC (cond_block, 0)->dest;
       basic_block bb2 = EDGE_SUCC (cond_block, 1)->dest;
+      // If the new phi entry does not refer to either of
+      // the middle bb, then either middle bbs can be deleted
+      // as long as the middle bbs only reach the phi bb and only
+      // reachable from the conditional bb.
       if (single_pred_p (bb1) && single_pred_p (bb2)
          && single_succ_p (bb1) && single_succ_p (bb2)
-         && empty_block_p (bb1) && empty_block_p (bb2))
+         && (TREE_CODE (new_tree) != SSA_NAME
+             || (gimple_bb (SSA_NAME_DEF_STMT (new_tree)) != bb1
+                 && gimple_bb (SSA_NAME_DEF_STMT (new_tree)) != bb2)))
        deleteboth = true;
     }
   else
@@ -1250,6 +1256,12 @@ match_simplify_replacement (basic_block cond_bb, 
basic_block middle_bb,
       && !SSA_NAME_IS_DEFAULT_DEF (gimple_cond_rhs (stmt)))
     bitmap_set_bit (exprs_maybe_dce,
                    SSA_NAME_VERSION (gimple_cond_rhs (stmt)));
+  if (TREE_CODE (arg_true) == SSA_NAME
+      && !SSA_NAME_IS_DEFAULT_DEF (arg_true))
+    bitmap_set_bit (exprs_maybe_dce, SSA_NAME_VERSION (arg_true));
+  if (TREE_CODE (arg_false) == SSA_NAME
+      && !SSA_NAME_IS_DEFAULT_DEF (arg_false))
+    bitmap_set_bit (exprs_maybe_dce, SSA_NAME_VERSION (arg_false));
 
   gsi = gsi_last_bb (cond_bb);
   /* Insert the sequence generated from gimple_simplify_phiopt.  */

Reply via email to