On Mon, 9 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. > > Move the debug binds of the removed blocks before the condition and > reset them, as CFG cleanup does when it removes a forwarder block. The > binds stay in the condition block, so they do not reset the variables > on other paths into the merge block. phiopt2 now emits > "# DEBUG w => NULL" before the condition. > > Bootstrapped and tested on aarch64-none-linux-gnu and x86_64-linux.
LGTM, but please give Andrea the time to chime in. Richard. > gcc/ChangeLog: > > * tree-ssa-phiopt.cc (move_reset_debug_binds): New function. > (replace_phi_edge_with_variable): Call it for removed blocks. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/phi-opt-62.c: New test. > > Signed-off-by: Kyrylo Tkachov <[email protected]> > --- > v3 (patch 4/8): > https://gcc.gnu.org/pipermail/gcc-patches/2026-November/737302.html > 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 v3 (Richard Biener, > https://gcc.gnu.org/pipermail/gcc-patches/2026-November/737488.html): > - Reset the binds of the removed blocks, as v2 does, and do not emit > binds whose value the condition selects. > - Remove the test gcc.dg/guality/phiopt-1.c. > > Changes from v2: > - The reset binds go before the condition and not to the start of the > merge block, so they do not reset the variables on other paths into > the merge block. > > gcc/testsuite/gcc.dg/tree-ssa/phi-opt-62.c | 21 ++++++++++++++++ > gcc/tree-ssa-phiopt.cc | 29 +++++++++++++++++++++- > 2 files changed, 49 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-62.c > > 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 000000000000..28cb4a748239 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-62.c > @@ -0,0 +1,21 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -g -fdump-tree-phiopt2" } */ > + > +/* phiopt removes the conditional block, which binds W. The bind is > + moved before the condition and reset, so that W does not keep the > + value of A on the path through the block. */ > + > +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 => NULL" "phiopt2" } } */ > diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc > index 875f8e4af34f..f3777707201a 100644 > --- a/gcc/tree-ssa-phiopt.cc > +++ b/gcc/tree-ssa-phiopt.cc > @@ -86,6 +86,29 @@ single_non_singleton_phi_for_edges (gimple_seq seq, edge > e0, edge e1) > return phi; > } > > +/* Move the debug binds of BB, a basic block that is about to be removed, > + before the statement at GSI_TO and reset their values, so that the > + variables they bind do not keep an earlier value after it. */ > + > +static void > +move_reset_debug_binds (basic_block bb, gimple_stmt_iterator *gsi_to) > +{ > + if (!MAY_HAVE_DEBUG_BIND_STMTS) > + return; > + for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);) > + { > + gimple *stmt = gsi_stmt (gsi); > + if (gimple_debug_bind_p (stmt)) > + { > + gsi_move_before (&gsi, gsi_to); > + gimple_debug_bind_reset_value (stmt); > + update_stmt (stmt); > + } > + else > + gsi_next (&gsi); > + } > +} > + > /* 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,16 +197,20 @@ 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 (); > + gsi = gsi_last_bb (cond_block); > + move_reset_debug_binds (edge_to_remove->dest, &gsi); > delete_basic_block (edge_to_remove->dest); > > /* Eliminate the COND_EXPR at the end of COND_BLOCK. */ > - gsi = gsi_last_bb (cond_block); > gsi_remove (&gsi, true); > } > else if (deleteboth) > { > basic_block bb1 = EDGE_SUCC (cond_block, 0)->dest; > basic_block bb2 = EDGE_SUCC (cond_block, 1)->dest; > + gsi = gsi_last_bb (cond_block); > + move_reset_debug_binds (bb1, &gsi); > + move_reset_debug_binds (bb2, &gsi); > > 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)
