This patch fixes incorrect debug values after web splitting in PR128020.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128020

The web pass renames ordinary uses and definitions, but leaves debug
uses referring to the original pseudo.  In the reproducer, the initial
zero remains in that pseudo while the loop increments are renamed.
Subsequent propagation therefore makes GDB report i = 0 at the loop
exit, although the actual value is 1.

Preserve the value of each split pseudo with debug uses in a debug
temporary.  Update the temporary at each definition with the register
selected for that web, and redirect the original debug uses to it.
Invalidate the temporary for partial, conditional and clobber definitions,
and before control-flow definitions.  Debug uses do not participate in
web merging, and the change does not introduce real pseudos.

Bootstrapped all default languages (C, C++, Fortran, Objective-C and LTO)
and ran all testsuites on x86_64-pc-linux-gnu with checking enabled and
multilib disabled.  Both the patched and baseline trees passed the
stage 2/3 comparison.  The baseline was revision
58f804554996fe057e5ca4fc814449640a6e816c, with the new regression test
also added for comparison.

The new test gives 27 PASS after the fix.  The full comparison has 10
FAIL -> PASS: nine GDB assertions in the new test and one in the existing
gcc.dg/guality/loop-1.c.  There are also two PASS -> FAIL results in
g++.dg/modules/bad-mapper-3.C with -std=c++20, caused by a hostname
resolution diagnostic variation.  Isolated reruns reproduce the same
resolution error in the baseline; identical C++20 commands give identical
diagnostics in both trees.  That test fails before reaching RTL.  No
patch-induced regressions were observed.

Both full make check runs return 2: the original DejaGNU FAIL totals are
198 before and 190 after.  Apart from the two mapper differences, the
remaining failures match.  The same 38 additional Automake self-test
failures occur in both trees: 37 in libbacktrace and one in MPFR tversion.
The MPFR failure is a GMP header/library version mismatch; relinking that
single test with the local GMP library makes it pass in both trees.

Additional testing on aarch64-conda-linux-gnu under QEMU user mode
confirmed the original reproducer and the new regression directly with
GDB: i = 1 after the fix, matching the register value, versus i = 0 with
the baseline.  On both targets, the original reproducer's .text is
unchanged; compare-debug and iteration-boundary checks also passed.
AArch64 testing did not include a full bootstrap or full testsuite run.

This patch was developed with AI assistance.

Is this OK for trunk?

      gcc/ChangeLog:

      PR rtl-optimization/128020
      * web.cc: Include cfgbuild.h.
      (pass_web::execute): Preserve debug values of split pseudos using
      debug temporaries.

      gcc/testsuite/ChangeLog:

      PR rtl-optimization/128020
      * gcc.dg/guality/pr128020.c: New test.

Regards,
Conglei Wang

---
diff --git a/gcc/web.cc b/gcc/web.cc
index 2aada36b619..c5f9a823861 100644
--- a/gcc/web.cc
+++ b/gcc/web.cc
@@ -37,6 +37,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "df.h"
 #include "insn-config.h"
 #include "recog.h"
+#include "cfgbuild.h"

 #include "tree-pass.h"

@@ -338,6 +339,8 @@ pass_web::execute (function *fun)
   basic_block bb;
   unsigned int uses_num = 0;
   rtx_insn *insn;
+  /* Mark split pseudos with const0_rtx until a debug use needs a temp.  */
+  rtx *debug_temps = XCNEWVEC (rtx, max);

   df_set_flags (DF_NO_HARD_REGS + DF_EQ_NOTES);
   df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
@@ -410,10 +413,55 @@ pass_web::execute (function *fun)
                                    use, used));
        FOR_EACH_INSN_INFO_DEF (def, insn_info)
          if (DF_REF_REGNO (def) >= FIRST_PSEUDO_REGISTER)
-           replace_ref (def, entry_register (def_entry + DF_REF_ID (def),
-                                   def, used));
+           {
+           rtx reg = entry_register (def_entry + DF_REF_ID (def),
+                               def, used);
+           if (reg != DF_REF_REAL_REG (def))
+             debug_temps[DF_REF_REGNO (def)] = const0_rtx;
+           replace_ref (def, reg);
+           }
      }

+  /* Preserve the value of split pseudos in debug temporaries.  Their
+     definitions follow the webs without making debug uses unite webs.  */
+  FOR_ALL_BB_FN (bb, fun)
+    FOR_BB_INSNS (bb, insn)
+      if (DEBUG_BIND_INSN_P (insn))
+     {
+       df_ref use;
+       FOR_EACH_INSN_USE (use, insn)
+         if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER
+           && debug_temps[DF_REF_REGNO (use)])
+           {
+           rtx &temp = debug_temps[DF_REF_REGNO (use)];
+           if (temp == const0_rtx)
+             temp = make_debug_expr_from_rtl (DF_REF_REAL_REG (use));
+           *DF_REF_REAL_LOC (use) = temp;
+           df_insn_rescan (insn);
+           }
+     }
+
+  for (unsigned int regno = FIRST_PSEUDO_REGISTER; regno < max; ++regno)
+    if (debug_temps[regno] && debug_temps[regno] != const0_rtx)
+      for (df_ref def = DF_REG_DEF_CHAIN (regno); def;
+        def = DF_REF_NEXT_REG (def))
+     {
+       insn = DF_REF_INSN (def);
+       rtx reg = *DF_REF_REAL_LOC (def);
+       if (DF_REF_FLAGS (def) & (DF_REF_CONDITIONAL | DF_REF_PARTIAL
+                          | DF_REF_MAY_CLOBBER | DF_REF_MUST_CLOBBER)
+           || control_flow_insn_p (insn))
+         reg = gen_rtx_UNKNOWN_VAR_LOC ();
+       rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (debug_temps[regno]),
+                          DEBUG_EXPR_TREE_DECL (debug_temps[regno]),
+                          reg, VAR_INIT_STATUS_INITIALIZED);
+       if (control_flow_insn_p (insn))
+         emit_debug_insn_before (bind, insn);
+       else
+         emit_debug_insn_after (bind, insn);
+     }
+
+  free (debug_temps);
   free (def_entry);
   free (use_entry);
   free (used);
diff --git a/gcc/testsuite/gcc.dg/guality/pr128020.c 
b/gcc/testsuite/gcc.dg/guality/pr128020.c
new file mode 100644
index 00000000000..8ae55fce57d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/guality/pr128020.c
@@ -0,0 +1,28 @@
+/* PR rtl-optimization/128020 */
+/* { dg-do run } */
+/* { dg-options "-O3 -g -funroll-loops -fno-tree-scev-cprop 
-fno-tree-vectorize" } */
+
+#include "../nop.h"
+
+void __attribute__((noipa))
+foo (int n)
+{
+  if (n == 0)
+    return;
+
+  int i = 0;
+  do
+    {
+      ++i;
+    }
+  while (i < n);
+
+  __asm__ volatile (NOP : : "g" (i) : "memory"); /* { dg-final { gdb-test . 
"i" "1" } } */
+}
+
+int
+main (void)
+{
+  foo (1);
+  return 0;
+}

Reply via email to