https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82885
Bug ID: 82885
Summary: memcpy does not propagate aliasing knowledge
Product: gcc
Version: 8.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: antoshkka at gmail dot com
Target Milestone: ---
The following code:
void foo3(char* ch1, const char* ch2) {
__builtin_memcpy(ch1, ch2, 1024*1024);
ch1[0] = ch2[2];
ch1[1] = ch2[2];
}
Produces assembly
foo3(char*, char*):
push rbx
mov edx, 1048576
mov rbx, rsi
call memcpy
mov rcx, rax
movzx eax, BYTE PTR [rbx+2]
mov BYTE PTR [rcx], al
movzx eax, BYTE PTR [rbx+2] <== This could be removed
mov BYTE PTR [rcx+1], al
pop rbx
ret
memcpy works for non aliased data, otherwise it is UB. Knowledge that pointers
do not alias could be propagated further to remove reads from ch2.