public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "yinyuefengyi at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug gcov-profile/93680] [GCOV] "do-while" structure in case statement leads to incorrect code coverage
Date: Wed, 01 Mar 2023 02:33:35 +0000	[thread overview]
Message-ID: <bug-93680-4-i0u0nY6ukM@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-93680-4@http.gcc.gnu.org/bugzilla/>

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

Xionghu Luo (luoxhu at gcc dot gnu.org) <yinyuefengyi at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yinyuefengyi at gmail dot com

--- Comment #4 from Xionghu Luo (luoxhu at gcc dot gnu.org) <yinyuefengyi at gmail dot com> ---
Check the difference of the two switch cases, both called split_edge to
generate empty latch bb after the loop:

profile.cc:
              /* Edge with goto locus might get wrong coverage info unless
                 it is the only edge out of BB.
                 Don't do that when the locuses match, so
                 if (blah) goto something;
                 is not computed twice.  */
              if (last
                  && gimple_has_location (last)
                  && !RESERVED_LOCATION_P (e->goto_locus)
                  && !single_succ_p (bb)
                  && (LOCATION_FILE (e->goto_locus)
                      != LOCATION_FILE (gimple_location (last))
                      || (LOCATION_LINE (e->goto_locus)
                          != LOCATION_LINE (gimple_location (last)))))
                {
                  basic_block new_bb = split_edge (e);
                  edge ne = single_succ_edge (new_bb);
                  ne->goto_locus = e->goto_locus;
                }

but the second case failed to find a edge from dest_prev to dest if edge_in
forms a self loop (edge_in->src == edge_in->dest)


  <bb 2> :
  p_6 = 0;
  q_7 = 0;
  switch (s_8(D)) <default: <L6> [INV], case 0: <L0> [INV], case 1: <L3> [INV]>

  <bb 3> :
  # n_1 = PHI <n_9(D)(2), n_14(4)>
  # p_3 = PHI <p_6(2), p_13(4)>
<L0>:
  p_13 = p_3 + 1;
  n_14 = n_1 + -1;
  if (n_14 != 0)
    goto <bb 4>; [INV]
  else
    goto <bb 5>; [INV]

  <bb 4> :
  goto <bb 3>; [100.00%]

  <bb 5> :
  _15 = p_13;
  goto <bb 10>; [INV]

  <bb 6> :

  <bb 7> :
  # n_2 = PHI <n_9(D)(2), n_11(6)>
  # p_4 = PHI <p_6(2), p_10(6)>
<L3>:
  p_10 = p_4 + 1;
  n_11 = n_2 + -1;
  if (n_11 != 0)
    goto <bb 6>; [INV]
  else
    goto <bb 8>; [INV]

  <bb 8> :
  _12 = p_10;
  goto <bb 10>; [INV]


Note the two loops have different latch bb location.



So add the check like this for self loop to return loop bb itself as after_bb?

diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc
index a9fcc7fd050..6fa1d83d366 100644
--- a/gcc/tree-cfg.cc
+++ b/gcc/tree-cfg.cc
@@ -3009,7 +3009,7 @@ split_edge_bb_loc (edge edge_in)
   if (dest_prev)
     {
       edge e = find_edge (dest_prev, dest);
-      if (e && !(e->flags & EDGE_COMPLEX))
+      if ((e && !(e->flags & EDGE_COMPLEX)) || edge_in->src == edge_in->dest)
        return edge_in->src;
     }
   return dest_prev;

With the fix, small.c.069i.profile:

  <bb 2> :
  p_6 = 0;
  q_7 = 0;
  switch (s_8(D)) <default: <L6> [INV], case 0: <L0> [INV], case 1: <L3> [INV]>

  <bb 3> :
  # n_1 = PHI <n_9(D)(2), n_14(4)>
  # p_3 = PHI <p_6(2), p_13(4)>
<L0>:
  p_13 = p_3 + 1;
  n_14 = n_1 + -1;
  if (n_14 != 0)
    goto <bb 4>; [INV]
  else
    goto <bb 5>; [INV]

  <bb 4> :
  goto <bb 3>; [100.00%]

  <bb 5> :
  _15 = p_13;
  goto <bb 10>; [INV]

  <bb 6> :
  # n_2 = PHI <n_9(D)(2), n_11(7)>
  # p_4 = PHI <p_6(2), p_10(7)>
<L3>:
  p_10 = p_4 + 1;
  n_11 = n_2 + -1;
  if (n_11 != 0)
    goto <bb 7>; [INV]
  else
    goto <bb 8>; [INV]

  <bb 7> :
  goto <bb 6>; [100.00%]

  <bb 8> :
  _12 = p_10;
  goto <bb 10>; [INV]



cat small.c.gcov:
        -:    0:Source:small.c
        -:    0:Graph:small.gcno
        -:    0:Data:small.gcda
        -:    0:Runs:1
        2:    1:int f(int s, int n)
        -:    2:{
        2:    3:  int p = 0;
        2:    4:  int q = 0;
        -:    5:
        2:    6:  switch (s)
        -:    7:    {
        5:    8:    case 0:
        5:    9:      do { p++; } while (--n);
        1:   10:      return p;
        -:   11:
        5:   12:    case 1:
        5:   13:      do { p++; } while (--n);
        1:   14:      return p;
        -:   15:    }
        -:   16:
    #####:   17:  return 0;
        -:   18:}
        -:   19:
        1:   20:int main() { f(0, 5); f(1, 5);}


Is this reasonable Fix? If so I could cook a patch and send it to maillist for
review...

  parent reply	other threads:[~2023-03-01  2:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-93680-4@http.gcc.gnu.org/bugzilla/>
2020-11-25  8:22 ` sunil.kumar3 at ltts dot com
2022-09-08 11:18 ` yangyibiao at nju dot edu.cn
2023-03-01  2:33 ` yinyuefengyi at gmail dot com [this message]
2023-05-06  1:40 ` yinyuefengyi at gmail dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-93680-4-i0u0nY6ukM@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).