Bug 110286 - Wrong code at -O2 on x86_64-linux-gnu (nondeterministic output)
Summary: Wrong code at -O2 on x86_64-linux-gnu (nondeterministic output)
Status: RESOLVED DUPLICATE of bug 110228
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 14.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-06-16 14:11 UTC by Shaohua Li
Modified: 2023-06-16 22:22 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Shaohua Li 2023-06-16 14:11:35 UTC
For the following code, gcc trunk at -O2 emits wrong code. If you run the compiled code multiple times, you will observe different outputs.

$ cat a.c
int printf(const char *, ...);
char a;
int b;
short c;
short(d)(short e) { return e + c; }
int f;
char g;
char g_0;
int h;
void i() {
  for (; h; f++)
    switch (g_0)
    case '"':
    case '\\':
      ++g;
}
short j;
static int aa;
int k;
const int *l;
const int **m = &l;
short n;
signed o;
int p = 84134051;
unsigned r = 1;
int ag;
static char ah() {
  int ai = 0;
  if (r)
  aj : {
    i();
    short *ak = &n;
    ag = 78003764;
    *m = &ag;
    *ak = 1;
    for (; *l + p - 162153904 + k; k++)
      for (; p - 168260671 > 3; ai++) {
        *ak = o && 5;
        if (a) {
          int *al = &aa;
          short *am = &j;
          *al &= b;
          *am = 0;
        }
      }
    if (d((*ak)--))
      ;
    else if (*l)
      goto aj;
  }
    return 0;
}
int main() {
  ah();
  printf("%d\n", n);
}
$
$ gcc-tk -O0 a.c && ./a.out
0
$ gcc-tk -O2 a.c
$ ./a.out
518
$ ./a.out
514
$
$ gcc-tk -v
Using built-in specs.
COLLECT_GCC=gcc-tk
COLLECT_LTO_WRAPPER=/zdata/shaoli/compilers/ccbuilder-compilers/gcc-27612ce352921d74086102d9ad4d3e011923f837/libexec/gcc/x86_64-pc-linux-gnu/14.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --disable-multilib --disable-bootstrap --enable-languages=c,c++ --prefix=/zdata/shaoli/compilers/ccbuilder-compilers/gcc-27612ce352921d74086102d9ad4d3e011923f837
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 14.0.0 20230616 (experimental) (GCC) 
$
Comment 1 Andrew Pinski 2023-06-16 22:17:00 UTC
phiopt match-simplify trying:
	n_lsm_flag.36_58 != 0 ? n_lsm.35_15 : 1
Matching expression match.pd:1990, gimple-match-5.cc:23
Matching expression match.pd:1990, gimple-match-5.cc:23
Matching expression match.pd:1990, gimple-match-5.cc:23
Matching expression match.pd:1947, gimple-match-7.cc:20
Matching expression match.pd:2479, gimple-match-4.cc:35
Matching expression match.pd:2482, gimple-match-3.cc:66
Matching expression match.pd:2489, gimple-match-2.cc:58
Applying pattern match.pd:6445, gimple-match-4.cc:12746
Applying pattern match.pd:1378, gimple-match-2.cc:7699
Applying pattern match.pd:1885, gimple-match-3.cc:2770
Applying pattern match.pd:4745, gimple-match-7.cc:15371
Folded into the sequence:
_112 = (short int) n_lsm_flag.36_58;
_85 = ~n_lsm_flag.36_58;
_34 = (short int) _85;
_87 = n_lsm.35_15 | _34;
Comment 2 Andrew Pinski 2023-06-16 22:18:47 UTC
Dup of bug 110228.

*** This bug has been marked as a duplicate of bug 110228 ***
Comment 3 Andrew Pinski 2023-06-16 22:22:02 UTC
In this case we have:
  # RANGE [irange] short int [0, 1] NONZERO 0x1
  # n_lsm.35_15 = PHI <_10(9), n_lsm.35_45(12)>
...
  # RANGE [irange] short int [0, 1] NONZERO 0x1
  # n_lsm.35_45 = PHI <n_lsm.35_15(11), _69(D)(6)>

  # RANGE [irange] short int [0, 1] NONZERO 0x1
  _10 = (short intD.17) _9;

But the range of _69(D) is really undefined ....

There is nothing phiopt/match is doing incorrectly due to the above, rather it is ranger that might be considering the wrong/incorrect information for uninitialized variables ...