https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102666
Bug ID: 102666
Summary: Wrong loop optimization for unsigned types
Product: gcc
Version: 9.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: davidsondfgl at gmail dot com
Target Milestone: ---
The code below produces incorrect result when any optimizations (-O1/-O2/-O3)
are enabled:
$ cat unsigned-bug.c
int main(void) {
for (unsigned a = 0, b = 0; a < 6; a += 1, b += 2)
if (b < a)
return 1;
return 0;
}
$ gcc -Wall -Wextra unsigned-bug.c; ./a.out; echo $?
0
$ gcc -Wall -Wextra unsigned-bug.c -O1; ./a.out; echo $?
1
$ gcc -Wall -Wextra unsigned-bug.c -O2; ./a.out; echo $?
1
$ gcc -Wall -Wextra unsigned-bug.c -O3; ./a.out; echo $?
1
This affects all releases as of 9.1, including the master branch (commit
2b30143).
A bisect between v9.1 and v8.3 indicates that this may have been introduced in
r266171, as I can revert this commit (a81e2c6) in all releases up to v11.2 and
get the expected behavior.
It is also worthwhile to note that this only occurs for unsigned types.