On Tue, 3 Nov 2026, [email protected] wrote:

> From: Kyrylo Tkachov <[email protected]>
> 
> replace_phi_edge_with_variable deletes the middle blocks of a diamond or
> triangle together with their debug binds.  A variable that a removed
> block binds then keeps its earlier value after the condition, also on
> the path through the removed block, and a debugger shows a wrong value.
> For
> 
>   unsigned
>   f (unsigned m, unsigned a, unsigned b, _Bool c)
>   {
>     unsigned w = a;
>     unsigned r = m;
>     if (c)
>       {
>         w = b;
>         r = m | w;
>       }
>     return r;
>   }
> 
> phiopt2 removes the branch, and at the return w has the value of a also
> when c is true.
> 
> Before the condition is removed, emit a debug bind for each variable
> that the removed blocks bind.  The condition selects the value of the
> variable after the removed blocks on each path, as value_replacement and
> spaceship_replacement do for their debug binds.  On a path without a
> bind of the variable, the value is the one of its last bind in the
> condition block.  The variable is reset if a value is unknown, or is not
> a constant or an SSA name from outside the removed blocks.  The binds go
> to the end of the condition block, so they do not apply to other paths
> into the merge block.  phiopt2 now emits
> 
>   # DEBUG w => c_4(D) != 0 ? b_5(D) : a_2(D)
> 
> Bootstrapped and tested on aarch64-none-linux-gnu and x86_64-linux.
> 
> gcc/ChangeLog:
> 
>       * tree-ssa-phiopt.cc (emit_removed_debug_binds): New function.
>       (replace_phi_edge_with_variable): Call it for removed blocks.
> 
> gcc/testsuite/ChangeLog:
> 
>       * gcc.dg/guality/phiopt-1.c: New test.
>       * gcc.dg/tree-ssa/phi-opt-62.c: New test.
> 
> Signed-off-by: Kyrylo Tkachov <[email protected]>
> ---
> v2 (patch 5/9): 
> https://gcc.gnu.org/pipermail/gcc-patches/2026-October/735344.html
> v1 (patch 10/14) did not reach the list.
> 
> Changes from v2:
> - Instead of resetting the binds of the removed blocks in the merge
>   block, emit binds whose value the condition selects (Andrea Pinski).
> - The binds go to the end of the condition block, so they do not apply
>   to other paths into the merge block.
> - New test gcc.dg/guality/phiopt-1.c.
> 
> ---
>  gcc/testsuite/gcc.dg/guality/phiopt-1.c    |  31 +++++++
>  gcc/testsuite/gcc.dg/tree-ssa/phi-opt-62.c |  20 ++++
>  gcc/tree-ssa-phiopt.cc                     | 102 ++++++++++++++++++++-
>  3 files changed, 152 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/gcc.dg/guality/phiopt-1.c
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-62.c
> 
> diff --git a/gcc/testsuite/gcc.dg/guality/phiopt-1.c 
> b/gcc/testsuite/gcc.dg/guality/phiopt-1.c
> new file mode 100644
> index 00000000000..bb898da42cc
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/guality/phiopt-1.c
> @@ -0,0 +1,31 @@
> +/* { dg-do run } */
> +/* { dg-options "-g" } */
> +/* { dg-skip-if "phiopt does not run" { *-*-* } { "-Og" } { "" } } */
> +
> +#include "../nop.h"
> +
> +__attribute__((noipa)) unsigned
> +f (unsigned m, unsigned a, unsigned b, _Bool c)
> +{
> +  unsigned w = a;
> +  unsigned r = m;
> +  if (c)
> +    {
> +      w = b;
> +      r = m | w;
> +    }
> +  asm volatile (NOP : : "r" (r), "r" (a), "r" (b), "r" (c));
> +  return r;
> +}
> +
> +int
> +main (void)
> +{
> +  volatile unsigned m = 1, a = 2, b = 4;
> +  volatile _Bool c = 1;
> +  f (m, a, b, c);
> +  return 0;
> +}
> +
> +/* phiopt removes the branch.  W is B after it, as C is true.  */
> +/* { dg-final { gdb-test 17 "w" "4" } } */
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-62.c 
> b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-62.c
> new file mode 100644
> index 00000000000..28d25867e19
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-62.c
> @@ -0,0 +1,20 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -g -fdump-tree-phiopt2" } */
> +
> +/* phiopt removes the conditional block, which binds W.  W gets B or A
> +   after the condition, depending on C.  */
> +
> +unsigned
> +f (unsigned m, unsigned a, unsigned b, _Bool c)
> +{
> +  unsigned w = a;
> +  unsigned r = m;
> +  if (c)
> +    {
> +      w = b;
> +      r = m | w;
> +    }
> +  return r;
> +}
> +
> +/* { dg-final { scan-tree-dump {DEBUG w => c_\d+\(D\) != 0 \? b_\d+\(D\) : 
> a_\d+\(D\)} "phiopt2" } } */
> diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
> index 875f8e4af34..0daddb4ee2f 100644
> --- a/gcc/tree-ssa-phiopt.cc
> +++ b/gcc/tree-ssa-phiopt.cc
> @@ -86,6 +86,97 @@ single_non_singleton_phi_for_edges (gimple_seq seq, edge 
> e0, edge e1)
>    return phi;
>  }
>  
> +/* COND_BB ends in a condition that is about to be removed together with
> +   BB_TRUE and BB_FALSE, the blocks on its true and false edges, or NULL if
> +   the edge goes directly to the merge block.  For each variable that the
> +   debug binds of BB_TRUE or BB_FALSE bind, emit before the condition a
> +   bind to the value that the variable has after the removed block on each
> +   path, selected by the condition.  The value on a path without a bind of
> +   the variable is the one of its last bind in COND_BB.  Reset the variable
> +   if a value is unknown or is not a constant or an SSA name from outside
> +   the removed blocks.  */
> +
> +static void
> +emit_removed_debug_binds (basic_block cond_bb, basic_block bb_true,
> +                       basic_block bb_false)
> +{
> +  if (!MAY_HAVE_DEBUG_BIND_STMTS)
> +    return;
> +
> +  /* The values of the variables on the true and false paths, in the order
> +     of their first bind.  error_mark_node stands for no bind.  */
> +  hash_map<tree, std::pair<tree, tree> > vals;
> +  auto_vec<tree, 8> vars;
> +  basic_block arms[2] = { bb_true, bb_false };
> +  for (int i = 0; i < 2; i++)
> +    if (arms[i])
> +      for (gimple_stmt_iterator gsi = gsi_start_bb (arms[i]);
> +        !gsi_end_p (gsi); gsi_next (&gsi))
> +     {
> +       gimple *stmt = gsi_stmt (gsi);
> +       if (!gimple_debug_bind_p (stmt))
> +         continue;
> +       tree var = gimple_debug_bind_get_var (stmt);
> +       if (TREE_CODE (var) == DEBUG_EXPR_DECL)
> +         continue;
> +       bool existed;
> +       std::pair<tree, tree> &v = vals.get_or_insert (var, &existed);
> +       if (!existed)
> +         {
> +           v = std::make_pair (error_mark_node, error_mark_node);
> +           vars.safe_push (var);
> +         }
> +       tree val = gimple_debug_bind_get_value (stmt);
> +       if (val
> +           && !is_gimple_min_invariant (val)
> +           && (TREE_CODE (val) != SSA_NAME
> +               || gimple_bb (SSA_NAME_DEF_STMT (val)) == arms[i]))
> +         val = NULL_TREE;
> +       if (i == 0)
> +         v.first = val;
> +       else
> +         v.second = val;
> +     }
> +  if (vars.is_empty ())
> +    return;
> +
> +  for (gimple_stmt_iterator gsi = gsi_last_bb (cond_bb); !gsi_end_p (gsi);
> +       gsi_prev (&gsi))
> +    if (gimple_debug_bind_p (gsi_stmt (gsi)))
> +      if (std::pair<tree, tree> *v
> +         = vals.get (gimple_debug_bind_get_var (gsi_stmt (gsi))))
> +     {
> +       tree val = gimple_debug_bind_get_value (gsi_stmt (gsi));
> +       if (v->first == error_mark_node)
> +         v->first = val;
> +       if (v->second == error_mark_node)
> +         v->second = val;
> +     }
> +

I don't think this is correct - it assumes # DEBUG stmts are "complete",
but 

  # DEBUG i = 2;
  i_2 = 3;
  # DEBUG i = 4;

is valid and var-tracking would handle this correctly.  That said,
your original solution to reset vars is more correct IMO.

Richard.


> +  gimple_stmt_iterator gsi = gsi_last_bb (cond_bb);
> +  gcond *cond = as_a <gcond *> (gsi_stmt (gsi));
> +  for (tree var : vars)
> +    {
> +      std::pair<tree, tree> v = *vals.get (var);
> +      tree val = NULL_TREE;
> +      if (v.first && v.second
> +       && v.first != error_mark_node && v.second != error_mark_node)
> +     {
> +       if (operand_equal_p (v.first, v.second))
> +         val = unshare_expr (v.first);
> +       else if (useless_type_conversion_p (TREE_TYPE (v.first),
> +                                           TREE_TYPE (v.second)))
> +         val = build3 (COND_EXPR, TREE_TYPE (v.first),
> +                       build2 (gimple_cond_code (cond), boolean_type_node,
> +                               gimple_cond_lhs (cond),
> +                               gimple_cond_rhs (cond)),
> +                       unshare_expr (v.first), unshare_expr (v.second));
> +     }
> +      gsi_insert_before (&gsi, gimple_build_debug_bind (var, val, cond),
> +                      GSI_SAME_STMT);
> +    }
> +}
> +
>  /* Replace PHI node element whose edge is E in block BB with variable NEW.
>     Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
>     is known to have two edges, one of which must reach BB).  */
> @@ -174,7 +265,12 @@ replace_phi_edge_with_variable (basic_block cond_block,
>        e->flags |= EDGE_FALLTHRU;
>        e->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
>        e->probability = profile_probability::always ();
> -      delete_basic_block (edge_to_remove->dest);
> +      basic_block middle_bb = edge_to_remove->dest;
> +      if (edge_to_remove->flags & EDGE_TRUE_VALUE)
> +     emit_removed_debug_binds (cond_block, middle_bb, NULL);
> +      else
> +     emit_removed_debug_binds (cond_block, NULL, middle_bb);
> +      delete_basic_block (middle_bb);
>  
>        /* Eliminate the COND_EXPR at the end of COND_BLOCK.  */
>        gsi = gsi_last_bb (cond_block);
> @@ -184,6 +280,10 @@ 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 (EDGE_SUCC (cond_block, 0)->flags & EDGE_TRUE_VALUE)
> +     emit_removed_debug_binds (cond_block, bb1, bb2);
> +      else
> +     emit_removed_debug_binds (cond_block, bb2, bb1);
>  
>        edge newedge = redirect_edge_and_branch (keep_edge, bb);
>  
> 

-- 
Richard Biener <[email protected]>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri; (HRB 36809, AG Nuernberg)

Reply via email to