https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104521
Bug ID: 104521
Summary: wrong code at -O2 and -O3 on x86_64-linux-gnu
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: zhendong.su at inf dot ethz.ch
Target Milestone: ---
It appears to be a recent regression and is very likely related to PR 104519.
But opposite to PR 104519, it doesn't reproduce at -Os, and instead reproduces
at -O2 and -O3. At -Os, there is a spurious UB warning though.
[611] % gcctk -v
Using built-in specs.
COLLECT_GCC=gcctk
COLLECT_LTO_WRAPPER=/local/suz-local/software/local/gcc-trunk/libexec/gcc/x86_64-pc-linux-gnu/12.0.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-trunk/configure --disable-bootstrap
--prefix=/local/suz-local/software/local/gcc-trunk --enable-languages=c,c++
--disable-werror --enable-multilib --with-system-zlib
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 12.0.1 20220213 (experimental) [master r12-7216-g7e204bd2f18] (GCC)
[612] %
[612] % gcctk -Os small.c; ./a.out
small.c: In function ‘main’:
small.c:6:30: warning: iteration 42 invokes undefined behavior
[-Waggressive-loop-optimizations]
6 | for (c = 0; c != -4; c -= 3) {
| ^~
small.c:6:21: note: within this loop
6 | for (c = 0; c != -4; c -= 3) {
| ~~^~~~~
[613] %
[613] % gcctk -O2 small.c
[614] % timeout -s 9 5 ./a.out
Killed
[615] %
[615] % cat small.c
char a, b, c;
int main() {
unsigned char d = 1;
while (1) {
if (c >= a) {
for (c = 0; c != -4; c -= 3) {
while (!d)
b = 0;
continue;
}
}
d = ~a;
if (!d)
continue;
return 0;
}
}