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

            Bug ID: 129032
           Summary: [15/16/17 Regression] wrong code at -O2: if-conversion
                    hoists __builtin_ctz out of the zero check of
                    __builtin_ctzg, then vrp2 folds the select into ctz(x)
                    | (x == 0)
           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

short a;
char g[24];

int main(void) {
  for (int i = 0; i < 24; i++)
    g[i] = __builtin_ctzg((unsigned short)a, 16) / 10;   /* a == 0: ctzg gives
16, 16 / 10 == 1 */
  if (g[23] != 1)
    __builtin_abort();
  return 0;
}


$ gcc -O2 repro.c && ./a.out
Aborted
$ gcc -O0 repro.c && ./a.out              # ok (also -O1, -O3, -Os)
$ gcc -O2 -fno-tree-loop-vectorize repro.c && ./a.out     # ok
```

`__builtin_ctzg (x, 16)` is lowered to `x != 0 ? __builtin_ctz (x) : 16`, so
for a == 0 every element must be 16 / 10 == 1.


Reproduced in Godbolt (gcc trunk, -O2 vs -O0): https://godbolt.org/z/Kz7cjaa8M

## Checked versions
- gcc trunk 8264fc847f3 (2026-11-24): -O2 aborts; -O0, -O1, -O3, -Os ok
- gcc 16.0.1 20260315, gcc 15.2.0, gcc 15.1 (Godbolt): -O2 aborts

Thanks.

Reply via email to