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

    <tr>
        <th>Summary</th>
        <td>
            armv7-a clang spills registers to unallocated stack slots in __attribute__((naked)) function
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    Hi!

Compile the following code with `-O0 -fPIC -march=armv7-a -mthumb`:
```
void * this_ptr;
void (*call_enter_ptr)(void * this_);

__attribute__((naked))
void raw_call_enter() {
    asm volatile(
        "ldr r0, %1\n"
        "blx %0\n"
        : : "r"(call_enter_ptr), "m"(this_ptr) : "r0", "r1"
    );

    asm volatile(
        "bx lr"
    );
}
```

Output:
```
raw_call_enter():
        ldr     r0, .LCPI0_0
.LPC0_0:
        add     r0, pc
        ldr     r0, [r0]
        ldr     r0, [r0]
        ldr     r1, .LCPI0_1
.LPC0_1:
        add     r1, pc
        ldr     r1, [r1]
        str     r0, [sp, #4]                    @ 4-byte Spill
        str     r1, [sp]                        @ 4-byte Spill
        ldr     r2, [sp, #4]                    @ 4-byte Reload
        ldr     r3, [sp]                        @ 4-byte Reload
        ldr     r0, [r3]
        blx     r2

        bx      lr
.LCPI0_0:
.Ltmp1:
        .long   call_enter_ptr(GOT_PREL)-((.LPC0_0+4)-.Ltmp1)
.LCPI0_1:
.Ltmp2:
        .long   this_ptr(GOT_PREL)-((.LPC0_1+4)-.Ltmp2)
this_ptr:
        .long   0

call_enter_ptr:
        .long   0
```

armv7-a clang 11.0.1
https://godbolt.org/z/rK8KW8von
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJylVVtvmzAU_jXkxQrCJkDywEObrFvVSI22SXtExjgJq8HINmm7X79jLrmT7mKBwT7H3_l8LnYqs_f4S-4Q7HgLx7tr-7ksqlxwZLYcraUQ8jUvN4jJjKPX3GyRE3rjZw-N16vHORoXVLGt4y-oKnbRmMKE2dZFCkqO3wHa__ZphjuZZ8ghd2Ag10lllOPfn0imIGRUiISXhqtGg8xg-nShnesXtn2SUGNUntaGJ0kDMy3pC8-a1bMjE4q-JgcDjeYMOVEHhqBRXaCdFNSAI6x8L7DNIURkCinPIXMYBNgJ5iVMXmil4s3Kvety_659CVFWSKZX9mzxSdGK9-6yZLuFXiNqtBQ-MXHpnj_ZV_qGhBrGiRZXI9r2z7WpajMU9Ws-3-v2DKxbbWtd6y7nq0cv6QDc5WpuB-draJYdranYLUQnuIefYPEfOviIGT5mhoeZ4ZvMcG8VX1jV5oyZrtpo-xPQRVeaM_HQZJy-G46-QRWLATx8wLuO8zHWnj_5B25fuZA0GwD0_5LcTbB9TP0L79ry7HZwViaN9K1DUn2Uu2zsw-wuTVFdRt0VEo5LhM6refr5-Xuy-vppCYk_bk-nPqXJ_cROdoD9UbVPshODZNDg4XwYNIVPTJG9qcNJPIB9UuhnO_tozbWzor8tmKCgi7HruV0xbY2ptMUkD_BsZJZKYVypNjD6Ba96mj79gKugHGWxn838GR2Z3Agen0Jqm7AaKb7JNRDVyEhUl0BcMmp4BpVA2QvSQhqN8hLdvjrQui6ZycFmrUR8xhCuxDp1mYRT-kGIXf8ZV0r-5MzAMNe65hp-gjAIw9E2njBKZxT6EJPpGocUk4gxzJgfRgHx0pGgKRc6hrSFpB3lMfEI8SIcYp9EfuRGWRquw4zx2SxlxKdQC7yguXCtYeurkYobDmm90SAU4AN9EFKt803JeY9Pa7OVKn7BgVnXo4Zt3FD9DYc_HUg">