https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129047
Bug ID: 129047
Summary: [15/16/17 Regression] wrong code at -O2: CRC loop
replacement (-foptimize-crc) accepts a loop whose data
operand is shifted by 2 per iteration
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: 0599jiangyc at gmail dot com
Target Milestone: ---
The following code:
```c
/* GCC trunk: wrong code at -O2/-O3 (CRC loop replacement, -foptimize-crc).
* DATA is shifted by 2 per iteration, so this loop is not a CRC, but the CRC
pass
* replaces it with a table/CLMUL CRC that consumes DATA one bit per iteration.
* Correct at -O0/-O1/-Os, with -fno-optimize-crc, with GCC 14 and clang.
* Godbolt: x86-64 gcc (trunk), flags: -O2
* EXPECTED: prints 0xc3000, exit 0. BUGGY: prints 0xf3c000, exit 1.
*/
#include <stdio.h>
__attribute__((noipa)) unsigned f(unsigned short data, unsigned crc) {
for (int i = 0; i < 16; i++) {
if ((data ^ crc) & 1)
crc = (crc >> 1) ^ 0x82000000;
else
crc >>= 1;
data >>= 2;
}
return crc;
}
int main(void) {
unsigned r = f(0x00f0, 0);
printf("%#x\n", r);
return r != 0xc3000;
}
```
Resulted in this output:
```
$ gcc -O2 repro.c && ./a.out
0xf3c000
(gcc -O0/-O1/-Os, -O2 -fno-optimize-crc, GCC 14.2 and clang print 0xc3000)
```
Trunk: https://godbolt.org/z/cM8GMezdj (x86-64 gcc trunk, -O2, executed;
reproduces as of 2026-11-13; also GCC 15.2, not GCC 14.2)