Issue 117332
Summary Possible missed optimization when calling memcpy or memmove in a loop
Labels missed-optimization
Assignees
Reporter ldionne
    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
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to