https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114937
Bug ID: 114937
Summary: [11 regression] -ftree-vrp optimizes out range check
before conditional increment
Product: gcc
Version: 11.4.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: mital at mitalashok dot co.uk
Target Milestone: ---
https://godbolt.org/z/fdMGWa4nq
int f(int x) {
const int y = x;
if (x != INT_MAX) {
++x;
}
return (x > y) ? x : 0;
}
int z = INT_MAX;
int main(void) {
// Prints INT_MIN when it should print 0
__builtin_printf("%d\n", f(z));
}
`f` is miscompiled at `-O1 -ftree-vrp` (also `-O2`) in GCC10/11 to return `x+1`
unconditionally.
The same happens with `if (x != INT_MIN) --x; return (x < y) ? x : 0;`.
This does not happen in GCC9 or GCC12+