<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/60375>60375</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            clang 16.0.0-rc1 regression: Illegal Instruction in `__init_libc` for musl on aarch64
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            bug,
            backend:AArch64,
            regression
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          andrewrk
      </td>
    </tr>
</table>

<pre>
    When compiling musl libc with clang 15.0.7, the machine code looks like this:

```
000000000040befc <__init_libc>:
  40befc: d10603ff      sub     sp, sp, #0x180
  40bf00: a9174ff4      stp     x20, x19, [sp, #0x170]
  40bf04: aa0103f3      mov     x19, x1
  40bf08: aa0003f4      mov     x20, x0
  40bf0c: 9100c3e0      add     x0, sp, #0x30
  40bf10: 2a1f03e1      mov     w1, wzr
  40bf14: 52802602      mov     w2, #0x130
 40bf18: a9167bfd       stp     x29, x30, [sp, #0x160]
  40bf1c: 94000ea9      bl      0x40f9c0 <memset>
  40bf20: d00000c8      adrp    x8, 0x425000 <Thread.LinuxThreadImpl.tls_thread_id+0x424ff8>
  40bf24: f941a508      ldr     x8, [x8, #0x348]
  40bf28: f9000114      str     x20, [x8]
 40bf2c: f8408688       ldr     x8, [x20], #0x8
```

With clang 16.0.0-rc1, the machine code looks like this:

```
000000000040be5c <__init_libc>:
  40be5c: d105c3ff      sub     sp, sp, #0x170
  40be60: d00000ca      adrp    x10, 0x425000 <Thread.LinuxThreadImpl.tls_thread_id+0x424ff8>
  40be64: a9167bfd      stp     x29, x30, [sp, #0x160]
  40be68: 52802608      mov     w8, #0x130
 40be6c: 9100c3e9       add     x9, sp, #0x30
  40be70: f9416d4a      ldr     x10, [x10, #0x2d8]
=>40be74: 19df0509           setp    [x9]!, x8!, xzr
  40be78: 19df4509      setm    [x9]!, x8!, xzr
  40be7c: 19df8509      sete    [x9]!, x8!, xzr
  40be80: f9000140      str     x0, [x10]
 40be84: f8408408       ldr     x8, [x0], #0x8
  40be88: b5ffffe8      cbnz    x8, 0x40be84 <__init_libc+0x28>
```

At the `=>`, Illegal Instruction occurs. I have discovered this bug by running the zig behavior tests. I do not yet have a reduction that is limited to LLVM IR, but here are the steps to reproduce that I do have:

```
git clone https://github.com/ziglang/zig/
cd zig
git checkout llvm16
mkdir build
cd build
cmake .. -DCMAKE_PREFIX_PATH="$LLVM_16_PREFIX"
make install
stage3/bin/zig test ../test/behavior.zig -lc -target aarch64-linux-musl --test-cmd qemu-aarch64 --test-cmd-bin  -I../test -femit-bin=test
```

Output:

```
$ stage3/bin/zig test ../test/behavior.zig -lc -target aarch64-linux-musl --test-cmd qemu-aarch64 --test-cmd-bin  -I../test -femit-bin=test
error: the following test command crashed:
qemu-aarch64 ./test
$ qemu-aarch64 ./test
Illegal instruction (core dumped)
```

Observed with qemu-aarch64 version 7.1.0. I have also observed Illegal Instruction occurring on real aarch64 hardware.

I will attempt to work on reduction steps for this bug report that involve only C code or LLVM IR, but this is what information I have so far.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMV0tv4zgS_jXypWChRD0sHXxIJx1ssD3YwWCwszeDIksW15TopSg73b9-QcpWZCPJPtCHEQKFIvV9xXp8JZoPg9r3RNso_xLlTys-utbYLe-lpbM9rGojv2__aKkHYbqj0qrfQzcOGrSqBZyVa0Fo3u8hyWOMNxF7BNcSdFy0qicQRhJoYw4DaHUgcK0aovQhwqcIr_cCL3_hEecrw5oaAVH6uNupXrmdNxmlX2c8wPRKlD6ATLDAtGkgwmoYa38_-s1M94il-JqUuMA1iB7Hq2STNU0G_vIod4ywemXoYa9JFdD5lyXNBqP86YYpC0wcE0yb9MrUmZNnmihekxtAeQEgpk22ePli9nafwb8qQRQp4ZWdS-kBeOdjegNNgouMJw2mlNxu7Jx40PmHvQEET3JWIiuQ3QHYWwhmMwFUXgJZbOpG3gdycj_Fd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