https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120841
Bug ID: 120841
Summary: gcc prefer non-volatile register produces sub optimal
code
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: rockeet at gmail dot com
Target Milestone: ---
void foo(char* buf, const char* src, size_t n) {
memcpy(buf, src, n);
memcpy(buf + n, &n, sizeof(n));
}
Both gcc and g++ produces(https://godbolt.org/z/TzW4jvToe):
push rbx
mov rbx, rdx
call memcpy
mov QWORD PTR [rax+rbx], rbx
pop rbx
ret
The optimal does not need non-volatile register:
push rdx
call memcpy
pop rdx
mov QWORD PTR [rax+rdx], rdx
ret