https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127984

            Bug ID: 127984
           Summary: wrong code at -O2 and -O3 for a bit-reflected CRC loop
                    of 24 iterations
           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: kyuwoncho18 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

__attribute__((noipa)) unsigned f(unsigned crc) {
  for (int i = 0; i < 24; i++) {
    if (crc & 1) crc = (crc >> 1) ^ 0xEDB88320; else crc >>= 1;
  }
  return crc;
}
int main(void) {
  if (f(0xFFFFFFFF) != 0xBE26ED)
    __builtin_abort();
}

godbolt: https://godbolt.org/z/9os41K4Mb

With -O0, -O1 or -Os, f returns 0x00be26ed (24 shift steps) and the program
exits normally, but with -O2 or -O3 it returns 0xdebb20e3, the value after 32
steps, and the program aborts. -fno-optimize-crc avoids it.

Disclaimer: This test case was generated by a fuzzer, and the report was
written with LLM assistance.

Reply via email to