[Bug tree-optimization/93538] equality of address of first member to address to enclosing object not folded

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Sun Feb 2 00:09:00 GMT 2020


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

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
(I noticed this while testing my fix for bug 93519.)

Here's a (strictly undefined) test case involving memcpy where the lack of
folding it prevents the call from being eliminated (it is ultimately eliminated
in RTL but it's supposed to be eliminated sooner):

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

void f (struct A *p)
{
  void *q = p->a;

  __builtin_memcpy (p, q, sizeof (struct A));
}
a.c: In function ‘f’:
a.c:7:3: warning: ‘__builtin_memcpy’ accessing 3 bytes at offsets 0 and 0
overlaps 3 bytes at offset 0 [-Wrestrict]
    7 |   __builtin_memcpy (p, q, sizeof (struct A));
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

f (struct A * p)
{
  void * q;

  <bb 2> [local count: 1073741824]:
  q_2 = &p_1(D)->a;
  __builtin_memcpy (p_1(D), q_2, 3); [tail call]
  return;

}

Another undefined test case involving strcpy that isn't folded even in RTL
(Clang eliminates the pointless copy):

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

void f (struct A *a)
{
  void *p = a;
  void *q = a->a;

  __builtin_strcpy ((char*)p, (char*)q);
}
        .file   "a.c"
        .text
a.c: In function ‘f’:
a.c:8:3: warning: ‘__builtin_strcpy’ accessing 1 byte at offsets 0 and 0
overlaps 1 byte at offset 0 [-Wrestrict]
    8 |   __builtin_strcpy ((char*)p, (char*)q);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

f (struct A * a)
{
  void * q;

  <bb 2> [local count: 1073741824]:
  q_2 = &a_1(D)->a;
  __builtin_strcpy (a_1(D), q_2); [tail call]
  return;

}


        .p2align 4
        .globl  f
        .type   f, @function
f:
.LFB0:
        .cfi_startproc
        movq    %rdi, %rsi
        jmp     strcpy
        .cfi_endproc
.LFE0:
        .size   f, .-f
        .ident  "GCC: (GNU) 10.0.1 20200131 (experimental)"
        .section        .note.GNU-stack,"",@progbits


More information about the Gcc-bugs mailing list