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

            Bug ID: 129031
           Summary: [14/15/16/17 Regression]  wrong code at -O2 and -O3:
                    the vectorizer's over-widening pattern narrows the
                    negation from x / -1 to signed char, so (signed
                    char)(a[i] / -1) <= 0 becomes a[i] >= 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

signed char a[64], b[64];

int main(void) {
  for (int f = 0; f < 64; f++)
    a[f] = -128;
  for (int f = 0; f < 64; f++)
    b[f] = (signed char)(a[f] / -1) <= 0;   /* a[f] / -1 == 128, (signed
char)128 == -128, so 1 */
  if (b[0] != 1)
    __builtin_abort();
  return 0;
}

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

`a[f] / -1` is computed in int (128, no overflow), and the conversion of 128 to
signed char gives -128 (implementation-defined; GCC documents modulo
reduction), so every `b[f]` must be 1.


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

## Checked versions
- gcc trunk 8264fc847f3 (2026-11-24): -O2, -O3 abort; -O0, -O1, -Os ok
- gcc 16.0.1, gcc 15.2.0, gcc 14.1 : abort
- clang 21: ok

Thanks.

Reply via email to