https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128908
Bug ID: 128908
Summary: [14/15/16/17 Regression] wrong code at -O1 and above:
reverse range of PLUS_EXPR ignores that the other
operand may be NaN (b * 0.0 with b = -inf)
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: haoxintu at gmail dot com
Target Milestone: ---
Hi,
$cat repro.c
[[gnu::noipa]] int f (float a, float b)
{
if (b * 0.0f + a > 0)
return 1;
return (a > 0 ? (unsigned) a : 0) != 0;
}
int main (void)
{
/* b = -inf: b * 0 is NaN, so the condition is false; then a > 0 and
(unsigned) a == 1, so f must return 1. */
if (f (1, -__builtin_inff ()) != 1)
__builtin_abort ();
return 0;
}
$ gcc -O0 repro.c && ./a.out # ok
$ gcc -O1 repro.c && ./a.out # Aborted (also -O2, -O3, -Os)
At -O1, the optimized dump of `f` keeps only `if (b * 0.0 + a > 0.0)`; the `a >
0 ? (unsigned) a : 0` arm is gone, and the false edge returns 0.
## Checked Versions
- Wrong at -O1/-O2/-O3/-Os with GCC 13, 14, 15, 16 and trunk (17.0.0 20261119);
- -O0 with GCC, all levels with gcc 11/12 and clang are correct.
Reproduced in Godbolt: https://godbolt.org/z/xxYT9zY9a
Thanks.