https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128904
Bug ID: 128904
Summary: [17 Regression] wrong code at -O2: if-conversion
speculates __builtin_ctz on a possibly-zero argument,
then VRP uses the ctz range to fold the select
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]] unsigned f (unsigned a, unsigned c)
{
unsigned short acc = a;
c &= 3;
for (int i = 0; i < 31; i++)
acc |= __builtin_ctzg (c, -1) & 1;
return acc;
}
int main (void)
{
/* c == 0: __builtin_ctzg (0, -1) is -1, and -1 & 1 == 1, so f returns 0 | 1
== 1. */
if (f (0, 0) != 1)
__builtin_abort ();
return 0;
}
$ gcc -O2 repro.c && ./a.out # Aborted (f returns 33)
$ gcc -O0 / -O1 / -O3 / -Os # ok
$ gcc -O2 -fno-tree-vectorize # ok
If-conversion speculates __builtin_ctz(c) outside the c != 0 guard. VRP2 then
assumes its result is in [0, 1] and folds the select into ctz(c) | (c == 0).
For c == 0, the generated code yields 33 instead of 1.
Reproduced in Godbolt: https://godbolt.org/z/jdT9c9roq
Thanks.