https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93539

            Bug ID: 93539
           Summary: memmove over self with result of string function not
                    eliminated
           Product: gcc
           Version: 10.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: ---

While experimenting with the test case from pr3538 I noticed that GCC doesn't
take full advantage of the fact that functions like strcpy return their first
argument.  In the test case below only the memmove call in the first function
is eliminated.  The one in the second one is not (and never has been).  Clang
eliminates both as expected (and always has).

$ cat a.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout -o/dev/stdout
a.c
struct A { char a[10]; };

void f (struct A *p)
{
  __builtin_strcpy (p->a, "abcd");
  char *q = p->a;
  __builtin_memmove (q, p->a, sizeof p->a);   // eliminated (good)
}

void g (struct A *p)
{
  char *q = __builtin_strcpy (p->a, "abcd");
  __builtin_memmove (q, p->a, sizeof p->a);   // not eliminated
}

        .file   "a.c"
        .text

;; Function f (f, funcdef_no=0, decl_uid=1932, cgraph_uid=1, symbol_order=0)

f (struct A * p)
{
  char[10] * _1;

  <bb 2> [local count: 1073741824]:
  _1 = &p_2(D)->a;
  __builtin_memcpy (_1, "abcd", 5); [tail call]
  return;

}


        .p2align 4
        .globl  f
        .type   f, @function
f:
.LFB0:
        .cfi_startproc
        movl    $1684234849, (%rdi)
        movb    $0, 4(%rdi)
        ret
        .cfi_endproc
.LFE0:
        .size   f, .-f

;; Function g (g, funcdef_no=1, decl_uid=1936, cgraph_uid=2, symbol_order=1)

g (struct A * p)
{
  char * q;
  char[10] * _1;

  <bb 2> [local count: 1073741824]:
  _1 = &p_2(D)->a;
  q_5 = __builtin_memcpy (_1, "abcd", 5);
  __builtin_memmove (q_5, _1, 10); [tail call]
  return;

}


        .p2align 4
        .globl  g
        .type   g, @function
g:
.LFB1:
        .cfi_startproc
        movl    $1684234849, (%rdi)
        movl    $10, %edx
        movq    %rdi, %rsi
        movb    $0, 4(%rdi)
        jmp     memmove
        .cfi_endproc
.LFE1:
        .size   g, .-g
        .ident  "GCC: (GNU) 10.0.1 20200131 (experimental)"
        .section        .note.GNU-stack,"",@progbits

Reply via email to