https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129165
Bug ID: 129165
Summary: [14/15/16/17 Regression] wrong code at -O0 and above:
`A < 0 ? <sign bit of A> : 0` -> `A & <sign bit>`
applied when A is unsigned (`(unsigned long
long)(unsigned)(0ULL > (unsigned long long)(signed
char)a ? 128u : 0)` folded to `a & 128`)
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: haoxintu at gmail dot com
Target Milestone: ---
Hi,
$cat repro.c
__attribute__((noinline)) unsigned long long f(unsigned a)
{
/* (unsigned long long)0 > x is false for every x, so f returns 0 */
return (unsigned long long)(unsigned)(0ULL > (unsigned long long)(signed
char)a ? 128u : 0);
}
int main(void)
{
if (f(0x80))
__builtin_abort();
return 0;
}
$ gcc -O0 repro.c && ./a.out # Aborted (also -O1, -O2, -O3, -Os, and with
-fwrapv)
$ clang -O0 repro.c && ./a.out # ok
$ g++ -O0 repro.c && ./a.out # ok
`0ULL > x` is an unsigned comparison, so it is false for every `x` and `f`
returns 0. The conversion of `a` to `signed char` is implementation-defined
(GCC: modulo 2^8), so there is no undefined behaviour; clang with
`-fsanitize=address,undefined` reports nothing.
Godbolt (GCC trunk aborts, Clang trunk is correct):
https://godbolt.org/z/a98Wc6brr
**Checked Versions: Seems not to be a recent regression. gcc 5.x to trunk
(17.0.0 20261129) all abort.
Thanks.