<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/56891>56891</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Backend][X86] Incorrect output + missed optimization for function marked as clang::musttail
</td>
</tr>
<tr>
<th>Labels</th>
<td>
bug,
backend:X86,
invalid-code-generation,
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
huangjd
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
huangjd
</td>
</tr>
</table>
<pre>
As of commit 4bd9d98bc2be3548418dc67accc662cbeb10a2e1
When compiling this source code
```
struct A {
int a[5];
};
extern int G(A a);
int F(A a) {
[[clang::musttail]] return G(a);
}
```
with `-O3 -target x86_64-linux`, clang generates the following assembly (only showing relevant part)
```
_Z1F1A: # @_Z1F1A
.cfi_startproc
# %bb.0: # %entry
movl 24(%rsp), %eax
movl %eax, 16(%rsp)
movaps 8(%rsp), %xmm0
movups %xmm0, (%rsp)
movaps %xmm0, 8(%rsp)
movl %eax, 24(%rsp)
jmp _Z1G1A@PLT # TAILCALL
.Lfunc_end0:
```
`movups %xmm0, (%rsp)` overwrites the return address of caller `F`, which will result in a crash.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyNVEtv2zAM_jX2RUggy4_YBx-SBikKBNgOBTbsUkgyE6uV7UCSk3S_fpTjtEmRtjMMPchPJD-CpOiq13JuSbchsmsa5UgiqqIqciGZgDhN8iTKK5nNuJQyy5gUICLKGUQBXQZ0flp_1dD69zulVbslrlaW2K43ElBaAbnEBhkd_-FqnemlI3MSzBYnCcFPtY7wIF2kQboM4lERzC7OwwpHB6Yd0PcBy-f4hhUfMF65elNeu0EP-EvN220Qz_FveuscV9q7TZfEgOvRvrd9bRkjuUXmtB6UqwnKJj9iMnHcbMGRY549ZckE09MfPZzdkcEt2UILhjuwmDUgm07r7uBzyK2FRuhXgr67FndbnxQGNOw5ktpx43xQtwJ5-hOtIk-IfPcFLCZBQscHI4tiKjfqyWLsbmc6OYo9kqVCTOl_GB7R0Drz-ma26fYaN5YgKVQau_MEMBceyY8fcaMU9VF29eICx3cWD_kNg8emoZfIfkCeFQPmK5MXwPwz5HWQH2idcc_NDldM8D0mOKE_14-fZ-xx_rC-m6_Xp8fT9aZv5RO0lc_4FxWH128JZpR0ezAHo87FNlY3ryoD9jQCuNZgfO2uxiI91ErW5KC0RrjttcNmI5xIw209DaGMsrRI8jQp8rAq46qICx465TSU2FgLLl8w-KGZFr_zzLfUQys7YwBbvuvdrncY5II0Cqu9It3OqUb95U51LbaCIZ7-cGm4eUEAt-RGs4a90WXtHNJHOcNmX22xBXsxxZGEF633522C5fyMzvGKLnuweEizvIjCusyyWYyTDQffJs5mVcFFNdvEVCQyTmJGeai5AG09sYAx0W9xPWWZiZFoPPcs38Sq3XOtqomfgZOx0ZHNO-DEe3LJ2yvTZahKRhmjOY0pjaJ4NoUZFAJkImSU5TRKsJKgQfJTT2vamW1oyoEhBmZRqZV19l2J40RtW4Ax-rrHLD5XozPeu7oz5SgMh8yUQ1r-AdmsuDQ">