<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/117332>117332</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Possible missed optimization when calling memcpy or memmove in a loop
</td>
</tr>
<tr>
<th>Labels</th>
<td>
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ldionne
</td>
</tr>
</table>
<pre>
I noticed that the following code did not optimize to a single memcpy, unlike I would expect:
```c++
template <class T>
void relocate_1(T *first, T *last, T *dest) {
for ( ; first != last; ++first, ++dest) {
std::memcpy((void*)dest, first, sizeof(T));
}
}
```
I would expect this to be equivalent to roughly:
```c++
template <class T>
void relocate_2(T *first, T *last, T *dest) {
auto n = last - first;
std::memcpy((void*)dest, first, n);
}
```
Is this a problem with e.g. the lack of knowledge that the `[first, last)` range is all valid? Note that both GCC and Clang fail to perform this optimization.
Godbolt: https://godbolt.org/z/zzdhcKPh4
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyslEGPqzYQxz-NuYw2goGE5MBhN9s8PVWq3uHdK4MHcHfwUGyS7n76ypBk86pKVatGDnhs5u-Zn-3R3tvOEVVq-6K2r4meQy9TxcaKc5TUYt6rr-Ak2IYMhF4HCD1BK8xysa6DRgyBsSZ-AzIGO9gPgiCgwVvXMcFAQzO-KzzC7Ni-EXyFi8xsgP4YqQkqf1bpq0pvz126tkbhS2zLaKBhZB0IVH5sWHsP31X-0zp3FmtgIpZGB_o1U7j_DgqfWzv5EFddLNYPhqFoHECVV3kAgFYmULgHlb_A4gsKM5W_wuKav8Aaz112Nf9WKv58MDGz_PmW_l7hPoaq8FnhYfU7wl3O2w-SNgav8BBbfhNU5esVzb1zY_QI7keqEHrr4zbUBPT7bM-ayYU4MMnc9fz-P2PH_4pdz0HAwQ00PF2R3NP_tyDdI7x_QOZXTBrGSWqmAS429ECbbrOcctbNG0gLb04uTKajzwsQlbafh2HN86B2KUzadQRRlRnOmq1R-Ql-kXD1riX08OV4BO0MHFm7DlptOW7NSFMr07AGdb1LOlhxm8eov4ipheO9gT6E0Uc6eFJ46taJjUydwtNH_H-Yvvn5W18kpsrNIT_ohKqszBH32zItkr6iOi3TrMkavd3poi1JF3nTEpY6PWCpKbEVplhkGWJW4B6LTbNL0_12W7ZZsc8O250qUhq05Q3zeYhrJ9b7maosK_McE9Y1sV_KC-JgvSfz9JiaQox1Z6qi-1M9d14VKVsf_KdgsIGp-ibe2zoWlEXlB0Bw6clBo5ljTVpPCsgUe4OcCawDDSwyJvPE1V-w2dDP9aaRQeEprnl9PY2T_BYLFJ6WjLzC0zWpc4V_BgAA__-LvZQs">