https://gcc.gnu.org/g:cbcf63f580131a77189ab0f81e3a553aec61c451

commit r17-6493-gcbcf63f580131a77189ab0f81e3a553aec61c451
Author: Kyrylo Tkachov <[email protected]>
Date:   Wed Nov 4 05:34:34 2026 -0800

    phiopt: Reset the debug binds of removed middle blocks
    
    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.
    
    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]>

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-62.c | 21 +++++++++++++++++++++
 gcc/tree-ssa-phiopt.cc                     | 29 ++++++++++++++++++++++++++++-
 2 files changed, 49 insertions(+), 1 deletion(-)

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 256f8eccffa1..4276d143ebb4 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -87,6 +87,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).  */
@@ -175,16 +198,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);

Reply via email to