<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/56162>56162</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Loop-Idiom optimization into memcpy doesn't trigger for 16-byte struct with local temporary copy
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
He3lixxx
</td>
</tr>
</table>
<pre>
Given this code ([godbolt](https://godbolt.org/z/eqorzE7zE))
```c++
#include <cstdint>
#include <cstddef>
struct Data {
uint64_t a;
// uint64_t b;
};
void bar(Data* __restrict data_out, Data* __restrict data_in, uint64_t count_in) {
for(uint64_t i = 0; i < count_in; ++i) {
Data value = data_in[i];
*data_out++ = value;
}
}
```
clang optimizes the loop into a `memcpy` call. However, when uncommenting the second `uint64_t` member `b`, it doesn't do that anymore.
I've played around a bit with different struct sizes using
```c++
struct Data {
char data[SIZE];
};
```
and it seems the optimization is only applied for sizes 1, 2, 4 and 8, but not for higher powers of two (16, 32, 64) and not for sizes in-between.
When changing the type of `value` to `Data&` instead of `Data`, the loop is always optimized into a `memcpy` call, regardless of `sizeof(Data)` ([godbolt](https://godbolt.org/z/dTM1xeYMq)).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJydVE2P4ygQ_TXkgjqyceIkBx96Oj0zLe2cdqXVzqUFpmwzwuABnLT712-B89lS72ElEmPq69Wrh4WVU_VNHcDQ0ClPayuBErYl6y-tlcLqQNZ7fO9CGDwpHgn7iutkWlrX4ts7_uC3de_Pm_dnwnZxZXuSPZIym1dN2Je45lNWKFPrMRYqnmofpDKBFM-fWSU0V2v698GNdaB7Hjglm1NaSkfMU65eA-WkuBzOgK82cbGRzf66T_8HqyQV3GG_MTdhj_T11QGWU1hO4tGrHQNhT_QzszLReilW29GEdLi7BdrYWOLipbDTPc0QTNo-XcPwZGZOfchA5-YPXI-Qos_V1-i63t_0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