Issue 244806
Summary Wrong code at -O1/-Os on `<12 x i64>` bitreverse + compare + bool-vector conversions (result depends on XMM register assignment)
Labels
Assignees
Reporter haoxintu
    ## Description

```c
typedef long c __attribute__((ext_vector_type(12)));
typedef _Bool d __attribute__((ext_vector_type(12)));
long e[] = {0, 1, 2, 3, 15, 015150151501515, 0, 15, 15, 5, 7, 9};
long f[12];
long long g[12];
long long *h = g;

int main (void)
{
  for (int j = 0; j < 12; j++)
    {
 f[j] = e[j];
      c a, b = 0;
      for (int i = 0; i < 12; i++)
 a[i] = f[4];
      c k = __builtin_convertvector (
 __builtin_convertvector (
 __builtin_elementwise_bitreverse (__builtin_shufflevector (a, b, 0, 5, 4, 1, 7, 3, 16, 22, 2, 7, 10, 4))
                        < (__builtin_convertvector (__builtin_convertvector (1 != (c){2, 2, 2036854775807, 2036854775807, 6, 7, 1, 7, 1, 1, 2, 1}, d), c) & a),
 d),
                c)
            & 1 + (c){3, 2, 6, 3, 0, 2036854775807, 2, 2036854775807, 7, 3, 7};
      for (int i = 0; i < 12; i++)
        h[i] = k[i];
    }
  /* last iteration (f[4] = 15): lane 11 = (bitreverse (15) < 0) & (1 + 0) == 1 */
  if (g[11] != 1)
 __builtin_abort ();
  return 0;
}
```

```
$ clang -O1 repro.c && ./a.out      # Aborted (also -Os); -O0, -O2, -O3 are fine
```

Lane 11 must be 1:

- `bitreverse(15) = 0xf000000000000000` is negative;
- the right-hand side of `<` is `0 & a` = 0;
- so the compare is true, the `_Bool` round trip gives 1, and `& (1 + 0)` keeps 1.

At -O1 it is 0.

With `-mllvm -opt-bisect-limit` the last skippable pass that changes the result is `lrshrink` on `main`. The only difference it makes in the assembly is a different choice of XMM registers (`xmm10`/`xmm11`/`xmm12` permuted). So the miscompile is probably in a later machine pass, or in lowering that depends on the register assignment, rather than in LiveRangeShrink itself.

Sanitizers (ASan, UBSan) are clean.

Reproduced in Godbolt (x86-64 clang assertions trunk, -O1): https://godbolt.org/z/MPETEGrxf

## Checked Versions

- Wrong at -O1/-Os with clang trunk 2012344e5da8 (2026-11-19); -O0/-O2/-O3 are fine.
- clang 21.1.8 is correct at -O1, so this is a regression.

Thanks.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to