https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96966
Bug ID: 96966 Summary: redundant memcpy not eliminated after pointer subtraction Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- The second call to memcpy and mempcpy, respectively, is redundant in each of the functions below and can be eliminated. GCC doesn't notice the redundancy and emits both pairs of copies. Clang emits just one call to memcpy but it too fails to eliminate the redundant call to mempcpy. $ cat z.c && gcc -O2 -S -o /dev/stdout z.c extern char a[32]; void f (const void *s) { char *p = (char*)__builtin_memcpy (a, s, 16) + 16; __builtin_memcpy (p - 16, s, 16); } void g (const void *s) { char *p = (char*)__builtin_mempcpy (a, s, 16); __builtin_mempcpy (p - 16, s, 16); } .file "z.c" .text .p2align 4 .globl f .type f, @function f: .LFB0: .cfi_startproc movdqu (%rdi), %xmm0 movups %xmm0, a(%rip) movdqu (%rdi), %xmm1 movups %xmm1, a(%rip) ret .cfi_endproc .LFE0: .size f, .-f .p2align 4 .globl g .type g, @function g: .LFB3: .cfi_startproc movdqu (%rdi), %xmm0 movups %xmm0, a(%rip) movdqu (%rdi), %xmm1 movups %xmm1, a(%rip) ret .cfi_endproc .LFE3: .size g, .-g .ident "GCC: (GNU) 11.0.0 20200902 (experimental)" .section .note.GNU-stack,"",@progbits