https://bugs.llvm.org/show_bug.cgi?id=38103
Bug ID: 38103
Summary: Bad for loop copy optimization when using
-fno-builtin-memcpy -fno-builtin-memmove
Product: clang
Version: 6.0
Hardware: PC
OS: Linux
Status: NEW
Severity: enhancement
Priority: P
Component: C++
Assignee: unassignedclangb...@nondot.org
Reporter: gchate...@google.com
CC: dgre...@apple.com, llvm-bugs@lists.llvm.org
Straightforward copy using a for loop with known size at compile time leads to
very poor assembly.
> #include <cstddef>
>
> template <size_t kBlockSize>
> void Copy(char* __restrict dst, const char* __restrict src) {
> for (size_t i = 0; i < kBlockSize; ++i) dst[i] = src[i];
> }
>
> template void Copy<15>(char* __restrict dst, const char* __restrict src);
https://godbolt.org/g/YFq3o6
This can be mitigated by the introduction of a temporary buffer like so:
> template <size_t kBlockSize>
> void Copy(char* __restrict dst, const char* __restrict src) {
> char tmp[kBlockSize];
> for (size_t i = 0; i < kBlockSize; ++i) tmp[i] = src[i];
> for (size_t i = 0; i < kBlockSize; ++i) dst[i] = tmp[i];
> }
https://godbolt.org/g/48Dghk
It works up to 25B and produce bad code from 26B onwards.
Check the resulting code for 32B for instance: https://godbolt.org/g/jZwcrv
--
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs