Hi!

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.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

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.

--- gcc/profile.cc.jj   2025-04-03 20:51:20.744542139 +0200
+++ gcc/profile.cc      2025-04-04 10:05:33.120871284 +0200
@@ -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)
--- gcc/testsuite/c-c++-common/pr119618.c.jj    2025-04-04 10:09:21.179905955 
+0200
+++ gcc/testsuite/c-c++-common/pr119618.c       2025-04-04 10:10:50.924919610 
+0200
@@ -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);
+  }
+}

        Jakub

Reply via email to