<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/59745>59745</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Bad code gen for simple swap
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
philnik777
</td>
</tr>
</table>
<pre>
```c++
struct Pair {
unsigned long long a;
unsigned long long b;
};
void swap(Pair& lhs, Pair& rhs) {
Pair tmp = lhs;
lhs = rhs;
rhs = tmp;
}
```
produces
```asm
swap(Pair&, Pair&):
movdqu xmm0, XMMWORD PTR [rdi]
movdqu xmm1, XMMWORD PTR [rsi]
movups XMMWORD PTR [rdi], xmm1
movups XMMWORD PTR [rsi], xmm0
ret
```
with GCC and
```asm
swap(Pair&, Pair&): # @swap(Pair&, Pair&)
movups xmm0, xmmword ptr [rdi]
movaps xmmword ptr [rsp - 24], xmm0
movups xmm0, xmmword ptr [rsi]
movups xmmword ptr [rdi], xmm0
movaps xmm0, xmmword ptr [rsp - 24]
movups xmmword ptr [rsi], xmm0
ret
```
with Clang ([Godbolt](https://godbolt.org/z/rKdG5hY9q)). I haven't benchmarked, but I can't imagine that storing the data on the stack and then loading it again is faster than using a second register.
For types with size <= 8 clang does the right thing AFAICT.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVN1u8ygQfRp8M2pE8P-FL5pErqpVtVVVaXcvscE2Wwwu4KTt06-wk01SJfl-LMsGZs7hcAYNtVa0ivMCxSsUbwI6uk6bYuiEVOItTdOg0uyzQAme3xqRlX_xBuF768xYO3imwgBK94sAo5o4GUit2vlDUXgrXP0fRunmOJ6-Wy0Y2B0dEMn8TogkIDuLyBoOU-On-amESZLrB0DhZso-bi87O62as1WzX3X9cKZlHhxOP08Ho9lYc_stSG2_t-VM7IlQRHIU7o8Fvd6y9xHgo--xz_n76emvP1828Pz6AiheGSZQvDnIm58TyPISxF6EjIOFK-xkPXP9FMSeQPA5xHB30amdcB08rNdAFftNt-Dyg0gIKMI3wRdPdbD7o-932jAYnLlhN91DzlLtAHdAoqtu_Gir0zKd5F6Qc42e3qQ_yrsm64KcX6_rWlLVAiIZilcPmlVauoko65wbrL_opESkbOfQQpsWkfILkdL8wR7i7p_83ReJ5At4hI5uuUIkdVBxVXc9NW-ceU3V6OARajoHRU9boTi4jjqwThuhWnAdB0YdBa2msXW0fvM3zs8USE2ZTxMOaEuFAmGhodZx42kUjNZHKVhea8XA8Fb44OK0B5XagPscuIXp5FZ8cUDh2jeMDOrJB6a5nbY3ou0cuM6z3pf3j-vXRcCKkOVhTgNeLJOUhElOcBR0RZJlDSZxxvKoyZK0qZuIMZY1aUhSRpomEAXBhCxJiHGIMQ4XCV8m2TJMsmWOs2qZoQjzngq5kHLbe48DYe3IizhPoziQtOLSTr2dEMV3MAURIb7Vm8Jj7qqxtSjCUlhnjyxOOMmLFWVQa8ah5QoabcCKfpB8asfBaGTxrdTCdWO1qHWPSOmp9r-7weh_ee0QKScBFpFyEvhfAAAA__8UOt_6">