reorg: Ignore debug insns in delete_prior_computation [PR127795]

Message ID 20261009123807.60177-1-yqtian668@gmail.com
State Committed
Commit 084169bfcc44a1dd249ca73ca8a606fcd2034dad
Headers
Series reorg: Ignore debug insns in delete_prior_computation [PR127795] |

Commit Message

Yongqiang Tian Oct. 9, 2026, 12:38 p.m. UTC
  delete_prior_computation walks backward with prev_nonnote_insn while
deleting a dead computation.  A DEBUG_INSN therefore terminates the scan
and can make -g retain an instruction that is deleted with -g0.

Use prev_nonnote_nondebug_insn for both steps of the walk.  Add a SPARC
regression test that checks both -fcompare-debug and the absence of the
dead add.

Assisted-by: OpenAI Codex 5.6 Sol, co-reviewed by Claude and Yongqiang Tian.

	PR rtl-optimization/127795

gcc/ChangeLog:

	* reorg.cc (delete_prior_computation): Ignore debug insns in the
	backward scan.

gcc/testsuite/ChangeLog:

	* gcc.target/sparc/reorg-delete-prior-debug.c: New test.

Signed-off-by: Yongqiang Tian <yqtian668@gmail.com>
---
 gcc/reorg.cc                                  |  4 ++--
 .../sparc/reorg-delete-prior-debug.c          | 19 +++++++++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/sparc/reorg-delete-prior-debug.c
  

Comments

Jeff Law Oct. 9, 2026, 1:49 p.m. UTC | #1
On 10/9/26 6:38 AM, Yongqiang Tian wrote:
> delete_prior_computation walks backward with prev_nonnote_insn while
> deleting a dead computation.  A DEBUG_INSN therefore terminates the scan
> and can make -g retain an instruction that is deleted with -g0.
>
> Use prev_nonnote_nondebug_insn for both steps of the walk.  Add a SPARC
> regression test that checks both -fcompare-debug and the absence of the
> dead add.
>
> Assisted-by: OpenAI Codex 5.6 Sol, co-reviewed by Claude and Yongqiang Tian.
>
> 	PR rtl-optimization/127795
>
> gcc/ChangeLog:
>
> 	* reorg.cc (delete_prior_computation): Ignore debug insns in the
> 	backward scan.
>
> gcc/testsuite/ChangeLog:
>
> 	* gcc.target/sparc/reorg-delete-prior-debug.c: New test.
>
> Signed-off-by: Yongqiang Tian <yqtian668@gmail.com>
Thanks.  Given the patch to the compiler proper is trivial from a 
copyright standpoint it doesn't matter if it was LLM generated or not.

For future patches please note how the patch was tested.  Normally a 
patch to the generic part of the compiler would require a bootstrap and 
regression test on one of the primary targets such as x86 or aarch64.  
reorg.cc is pretty narrow and only used on a small number of targets and 
wouldn't be exercised during an x86 or aarch64 build.  A bootstrap and 
regression test on sparc would be best, but that may not be generally 
feasible.   I'm not set up for sparc testing (though there's no inherent 
reason why I couldn't set that up again....)

As a quick proxy, I tested this on crosses that use delay slots.  In 
particular, sh3, sh3eb, or1k, visium, microblaze, iq2000, fr30, cris, 
and arc with no regressions.   So I'm confident it's behaving 
reasonably.  hppa, sh4, sh4eb will give this deeper testing over the 
coming days.

Your other patch fixes similar problems elsewhere in reorg.cc, I'll look 
at that patch shortly.  LLM review indicates there are two similar kinds 
of instances that show up in reorg.c.  In particular the BARRIER checks 
in relax_delay_slots could potentially lead to a -fcompare-debug fail, 
but they'd probably need to be deeply evaluated to determine if a 
DEBUG_INSN could appear at that particular point in the IL.

I'll push this to the trunk momentarily.

Thanks!

jeff
  
Yongqiang Tian Oct. 9, 2026, 2:04 p.m. UTC | #2
On 10/9/26, Jeff Law wrote:
> For future patches please note how the patch was tested.

Thanks for testing it on the delay-slot crosses and for pushing it.

Sorry for leaving that out.  For this patch:

  - I completed a native x86_64-pc-linux-gnu bootstrap and full
    make -k check at master
    54e232d0d4752b419ed775bdad50d83798fa899d, both with and without
    the patch. After normalizing the build-directory path embedded in
    one module test name, both runs produced the same 541,492 result
    records. As you noted, this does not exercise reorg.cc.
  - sparc-elf cross: the new gcc.target/sparc/reorg-delete-prior-debug.c
    fails without the patch and passes with it.
  - contrib/check_GNU_style.sh is clean.

I will include the testing in future submissions, and for reorg.cc
changes I will add a run of the sparc-elf cross testsuite.

> In particular the BARRIER checks in relax_delay_slots could
> potentially lead to a -fcompare-debug fail, but they'd probably need
> to be deeply evaluated to determine if a DEBUG_INSN could appear at
> that particular point in the IL.

I also found those when auditing reorg.cc.  The prev_nonnote_insn
(label) BARRIER tests would only change if a DEBUG_INSN sat between a
BARRIER and the following CODE_LABEL, and I have not seen GCC put one
there. Thus I did not open a bug report for this. I can also take a second look
tomorrow.

Again, thanks a lot for looking at this.

Thanks,
Yongqiang

On Sat, 10 Oct 2026 at 00:51, Jeff Law <jeffrey.law@oss.qualcomm.com> wrote:
>
>
>
> On 10/9/26 6:38 AM, Yongqiang Tian wrote:
> > delete_prior_computation walks backward with prev_nonnote_insn while
> > deleting a dead computation.  A DEBUG_INSN therefore terminates the scan
> > and can make -g retain an instruction that is deleted with -g0.
> >
> > Use prev_nonnote_nondebug_insn for both steps of the walk.  Add a SPARC
> > regression test that checks both -fcompare-debug and the absence of the
> > dead add.
> >
> > Assisted-by: OpenAI Codex 5.6 Sol, co-reviewed by Claude and Yongqiang Tian.
> >
> >       PR rtl-optimization/127795
> >
> > gcc/ChangeLog:
> >
> >       * reorg.cc (delete_prior_computation): Ignore debug insns in the
> >       backward scan.
> >
> > gcc/testsuite/ChangeLog:
> >
> >       * gcc.target/sparc/reorg-delete-prior-debug.c: New test.
> >
> > Signed-off-by: Yongqiang Tian <yqtian668@gmail.com>
> Thanks.  Given the patch to the compiler proper is trivial from a
> copyright standpoint it doesn't matter if it was LLM generated or not.
>
> For future patches please note how the patch was tested.  Normally a
> patch to the generic part of the compiler would require a bootstrap and
> regression test on one of the primary targets such as x86 or aarch64.
> reorg.cc is pretty narrow and only used on a small number of targets and
> wouldn't be exercised during an x86 or aarch64 build.  A bootstrap and
> regression test on sparc would be best, but that may not be generally
> feasible.   I'm not set up for sparc testing (though there's no inherent
> reason why I couldn't set that up again....)
>
> As a quick proxy, I tested this on crosses that use delay slots.  In
> particular, sh3, sh3eb, or1k, visium, microblaze, iq2000, fr30, cris,
> and arc with no regressions.   So I'm confident it's behaving
> reasonably.  hppa, sh4, sh4eb will give this deeper testing over the
> coming days.
>
> Your other patch fixes similar problems elsewhere in reorg.cc, I'll look
> at that patch shortly.  LLM review indicates there are two similar kinds
> of instances that show up in reorg.c.  In particular the BARRIER checks
> in relax_delay_slots could potentially lead to a -fcompare-debug fail,
> but they'd probably need to be deeply evaluated to determine if a
> DEBUG_INSN could appear at that particular point in the IL.
>
> I'll push this to the trunk momentarily.
>
> Thanks!
>
> jeff
  

Patch

diff --git a/gcc/reorg.cc b/gcc/reorg.cc
index 76b3e1438..38aad68d7 100644
--- a/gcc/reorg.cc
+++ b/gcc/reorg.cc
@@ -2884,10 +2884,10 @@  delete_prior_computation (rtx note, rtx_insn *insn)
   rtx_insn *our_prev;
   rtx reg = XEXP (note, 0);
 
-  for (our_prev = prev_nonnote_insn (insn);
+  for (our_prev = prev_nonnote_nondebug_insn (insn);
        our_prev && (NONJUMP_INSN_P (our_prev)
 		    || CALL_P (our_prev));
-       our_prev = prev_nonnote_insn (our_prev))
+       our_prev = prev_nonnote_nondebug_insn (our_prev))
     {
       rtx pat = PATTERN (our_prev);
 
diff --git a/gcc/testsuite/gcc.target/sparc/reorg-delete-prior-debug.c b/gcc/testsuite/gcc.target/sparc/reorg-delete-prior-debug.c
new file mode 100644
index 000000000..b1992bd96
--- /dev/null
+++ b/gcc/testsuite/gcc.target/sparc/reorg-delete-prior-debug.c
@@ -0,0 +1,19 @@ 
+/* PR rtl-optimization/127795 */
+
+/* Verify that delay-slot reorganization deletes the dead add with and
+   without debug information.  -fno-tree-ter keeps the add separate, while
+   -fno-var-tracking keeps the debug marker until the dbr pass.  Falling off
+   the end of the function is required to expose the deletion path.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-tree-ter -fno-var-tracking -fcompare-debug" } */
+
+int
+foo (int a, int b)
+{
+  int e = a + b;
+  if (e >= 97)
+    return a;
+}
+
+/* { dg-final { scan-assembler-not "add\t%" } } */