public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rguenth at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/106892] Wrong code at -O3 on x86_64-linux-gnu
Date: Fri, 09 Sep 2022 08:15:14 +0000	[thread overview]
Message-ID: <bug-106892-4-vGCKb3EpkB@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-106892-4@http.gcc.gnu.org/bugzilla/>

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |NEW
                 CC|                            |rguenth at gcc dot gnu.org
           Keywords|                            |wrong-code

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Li Shaohua from comment #3)
> Yes, I reduced it too much. Here is the new one with return value in g()
> function.
> 
> a, b, c, d, e;
> f[8];
> g() {
>   while (a)
>     a >>= 4;
>   return 0;
> }
> h(i) {
>   if (i >= '0')
>     return i - '0';
> }
> j(i) {
>   b = 2;
>   for (; g() <= 7; b++)
>     if (i) {
>       for (; e <= 7; e++) {
>         c = 1;
>         for (; c <= 7; c++) {
>           d = h(b + 48);
>           f[-d + 4] ^= 3;
>         }
>       }
>       return;
>     }
> }
> main() {
>   j(1);
>   printf("%d\n", f[2]);
> }

When I apply the same "fix" to h() (add a return 0 or __builtin_unreachable ())
the code works again.

Note I think the missing return stmt isn't reached at runtime.

Disabling either unswitching or loop splitting makes the issue go away,
enabling both ontop of -O2 doesn't trigger it.

We early inline h into j where h looks like

int h (int i)
{
  int _3;

  <bb 2> :
  if (i_1(D) > 47)
    goto <bb 3>; [INV]
  else
    goto <bb 4>; [INV]

  <bb 3> :
  _3 = i_1(D) + -48;
  // predicted unlikely by early return (on trees) predictor.
  return _3;

  <bb 4> :
  return;

}

which results in

  b.2_1 = b;
  _2 = b.2_1 + 48;
  _3 = h (_2);
  d = _3;

becoming

  b.2_1 = b;
  _2 = b.2_1 + 48;
  if (_2 > 47)
    goto <bb 6>; [34.00%]
  else
    goto <bb 7>; [66.00%]

  <bb 6> :
  _30 = _2 + -48;
  _43 = _30;

  <bb 7> :
  # _39 = PHI <_43(6), _37(5)>
  _3 = _39;
  d = _3;

note that the return; case results in _37 to be used which is completely
random.

That said, our handling of an unreachable missing return in the IL is
likely counter productive.

Cleaned up testcase:

int a, b, c, d, e;
int f[8];
int g() {
  while (a)
    a >>= 4;
  return 0;
}
int h(int i) {
  if (i >= '0')
    return i - '0';
  //__builtin_unreachable ();
}
void j(int i) {
  b = 2;
  for (; g() <= 7; b++)
    if (i) {
      for (; e <= 7; e++) {
        c = 1;
        for (; c <= 7; c++) {
          d = h(b + 48);
          f[-d + 4] ^= 3;
        }
      }
      return;
    }
}
int main() {
  j(1);
  if (f[2] != 0)
    __builtin_abort ();
}

  parent reply	other threads:[~2022-09-09  8:15 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-08 21:37 [Bug c/106892] New: " shaohua.li at inf dot ethz.ch
2022-09-08 22:02 ` [Bug c/106892] " jakub at gcc dot gnu.org
2022-09-09  7:40 ` rguenth at gcc dot gnu.org
2022-09-09  7:53 ` shaohua.li at inf dot ethz.ch
2022-09-09  8:15 ` rguenth at gcc dot gnu.org [this message]
2022-09-09  8:20 ` marxin at gcc dot gnu.org
2022-09-09  8:20 ` jakub at gcc dot gnu.org
2022-09-09  8:31 ` [Bug c/106892] [11/12/13 Regression] Wrong code at -O3 on x86_64-linux-gnu since r11-963-g80d6f89e78fc3b77 rguenth at gcc dot gnu.org
2022-09-09  8:32 ` [Bug middle-end/106892] " rguenth at gcc dot gnu.org
2022-09-09  9:05 ` rguenth at gcc dot gnu.org
2022-09-09  9:31 ` rguenth at gcc dot gnu.org
2022-09-09 10:04 ` rguenth at gcc dot gnu.org
2022-09-09 12:43 ` rguenth at gcc dot gnu.org
2022-09-10 13:14 ` [Bug middle-end/106892] [11/12 " mikpelinux at gmail dot com
2022-09-12  7:53 ` marxin at gcc dot gnu.org
2022-10-11 12:06 ` cvs-commit at gcc dot gnu.org
2022-10-17 13:28 ` [Bug middle-end/106892] [11 " cvs-commit at gcc dot gnu.org
2022-10-17 13:29 ` rguenth at gcc dot gnu.org

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-106892-4-vGCKb3EpkB@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).