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

commit r15-9209-gc89714d9df3ad8dd6b79946da85f5e276aba83db
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Fri Apr 4 20:53:19 2025 +0200

    profile: Another musttail fix [PR119618]
    
    As the following testcase shows, sometimes we can have debug stmts
    after a musttail call and profile.cc in that case would incorrectly
    allow the edge from that, causing musttail error and -fcompare-debug
    failure (because if there are no debug stmts after it, then musttail
    is found there and the edge is ignored).
    
    The following patch uses gsi_last_nondebug_bb instead of gsi_last_bb
    to find the musttail call.  And so that we don't uselessly skip over
    debug stmts at the end of many bbs, the patch limits it to
    cfun->has_musttail functions.
    
    2025-04-04  Jakub Jelinek  <ja...@redhat.com>
    
            PR gcov-profile/119618
            * profile.cc (branch_prob): Only check for musttail calls if
            cfun->has_musttail.  Use gsi_last_nondebug_bb instead of 
gsi_last_bb.
    
            * c-c++-common/pr119618.c: New test.

Diff:
---
 gcc/profile.cc                        |  5 +++--
 gcc/testsuite/c-c++-common/pr119618.c | 21 +++++++++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/gcc/profile.cc b/gcc/profile.cc
index 550c85b9742c..6234dd2d4e2d 100644
--- a/gcc/profile.cc
+++ b/gcc/profile.cc
@@ -1341,9 +1341,10 @@ branch_prob (bool thunk)
          ignored_edges++;
        }
       /* Ignore edges after musttail calls.  */
-      if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
+      if (cfun->has_musttail
+         && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
        {
-         gimple_stmt_iterator gsi = gsi_last_bb (e->src);
+         gimple_stmt_iterator gsi = gsi_last_nondebug_bb (e->src);
          gimple *stmt = gsi_stmt (gsi);
          if (stmt
              && is_gimple_call (stmt)
diff --git a/gcc/testsuite/c-c++-common/pr119618.c 
b/gcc/testsuite/c-c++-common/pr119618.c
new file mode 100644
index 000000000000..a56e6695cf5a
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/pr119618.c
@@ -0,0 +1,21 @@
+/* PR gcov-profile/119618 */
+/* { dg-do compile { target musttail } } */
+/* { dg-options "-fcompare-debug -fprofile-generate -O1" } */
+/* { dg-require-profiling "-fprofile-generate" } */
+
+struct S { char s; };
+int foo (void);
+int *(*fn) (void);
+
+int *
+bar (void)
+{
+  if (foo ())
+    return 0;
+  {
+    struct S s;
+    do
+      [[gnu::musttail]] return fn ();
+    while (0);
+  }
+}

Reply via email to