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

            Bug ID: 121344
           Summary: New __builtin_clz(0) when compiling with -Os
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kristerw at gcc dot gnu.org
  Target Milestone: ---

Compiling the following on x86_64 with -Os introduces new undefined behavior.

int
foo (unsigned char b)
{
  int c = 0;
  while (b) {
      b >>= 1;
      c++;
  }
  return c;
}

The issue is that the loop has been transformed to use __builtin_clz, which is
UB when called with 0.

Interestingly, -O1, -O2, and -O3 also transform this to use __builtin_clz, but
they correctly protect the code with:
  if (b_3(D) != 0)

So the compiler knows how to handle it correctly, but is failing to do so with
-Os.

Reply via email to