LLVM doesn't make use of the return value of memcpy
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
`memcpy` always returns the destination pointer. This can be used to reduce the number of registers used if the destination pointer is used after a `memcpy`. However, Clang/LLVM currently doesn't make use of this AFAICT.
```c++
#include
auto test(int** dest, int* old_ptr, size_t old_cap, size_t size, size_t new_cap) {
auto new_ptr = ::operator new(new_cap * sizeof(int));
*dest = (int*)__builtin_memcpy(new_ptr, old_ptr, size * sizeof(int));
::operator delete(old_ptr, old_cap);
}
```
In the above example, there is an additional register used to save the `new_ptr` and store it after the call to `memcpy`. The return value of `memcpy` could be used instead though. GCC makes use of this and generates better code as a result: https://godbolt.org/z/jWvzWqnYo
Contributor guide
Research direction
Start with the C++ example in the issue and compare Clang/LLVM's generated code with the linked Godbolt GCC result. Trace the relevant memcpy lowering and code-generation path, then verify that the destination pointer returned by memcpy is reused and the unnecessary register or store is eliminated.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100