gimple_can_merge_blocks_p checks the final statement of a block for the
compiler-inserted nop that marks a coverage-suppression boundary.  A trailing
DEBUG_BEGIN_STMT hides that nop under -g, allows the blocks to be merged, and
changes the gcov CFG checksum even though the generated code is unchanged.

Use gsi_last_nondebug_bb so debug markers do not affect the merge decision.
Add a focused regression test.

The test case was found by a non-AI automated testing tool.
Assisted-by: OpenAI Codex 5.6 Sol.

Bootstrapped and regression tested on x86_64-pc-linux-gnu.  The focused
test fails before the change and passes after it.

        PR gcov-profile/128346

gcc/ChangeLog:

        * tree-cfg.cc (gimple_can_merge_blocks_p): Ignore debug statements
        when examining the end of a block.

gcc/testsuite/ChangeLog:

        * gcc.misc-tests/gcov-45.c: New test.

Signed-off-by: Yongqiang Tian <[email protected]>
---
 gcc/testsuite/gcc.misc-tests/gcov-45.c | 19 +++++++++++++++++++
 gcc/tree-cfg.cc                        |  2 +-
 2 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.misc-tests/gcov-45.c

diff --git a/gcc/testsuite/gcc.misc-tests/gcov-45.c 
b/gcc/testsuite/gcc.misc-tests/gcov-45.c
new file mode 100644
index 000000000..c7b223048
--- /dev/null
+++ b/gcc/testsuite/gcc.misc-tests/gcov-45.c
@@ -0,0 +1,19 @@
+/* PR gcov-profile/128346 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -g -fno-thread-jumps -fprofile-arcs 
-fdump-tree-cfg-details" } */
+
+int bar (int);
+
+#pragma GCC suppress_coverage begin
+int
+foo (int a)
+{
+  a;
+#pragma GCC suppress_coverage end
+  if (a > 3)
+    a = bar (a);
+  return bar (a);
+}
+
+/* A debug marker must not hide the coverage-suppression nop.  */
+/* { dg-final { scan-tree-dump-not "Merging blocks 2 and 3" "cfg" } } */
diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc
index ba1d4a125..93a830f0e 100644
--- a/gcc/tree-cfg.cc
+++ b/gcc/tree-cfg.cc
@@ -1874,7 +1874,7 @@ gimple_can_merge_blocks_p (basic_block a, basic_block b)
 
   /* If A ends by a statement causing exceptions or something similar, we
      cannot merge the blocks.  */
-  stmt = *gsi_last_bb (a);
+  stmt = *gsi_last_nondebug_bb (a);
   if (stmt && stmt_ends_bb_p (stmt))
     return false;
 
-- 
2.34.1

Reply via email to