public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "msebor at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/95886] New: suboptimal memcpy with embedded zero bytes
Date: Wed, 24 Jun 2020 21:14:20 +0000	[thread overview]
Message-ID: <bug-95886-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 95886
           Summary: suboptimal memcpy with embedded zero bytes
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

While testing the fix for pr95189 I noticed that the memcpy expansion into
copy-by-pieces is less than optimal for sequences containing embedded null
bytes.  For example, in the test case below, the memcpy call in f() is expanded
into what looks like a more efficient sequence than the equivalent memcpy call
in g().  The only difference between the two is that the former copies a
sequence of non-zero bytes while among the bytes copied by the latter is a null
byte.  Clang emits the same code for g() as GCC does for f().

$ cat z.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout -o/dev/stdout
z.c
const char a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const char b[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };

void f (void *d)
{
  __builtin_memcpy (d, a, 9);   // optimal
}

void g (void *d)
{
  __builtin_memcpy (d, b, 9);   // suboptimal
}


        .file   "z.c"
        .text

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

f (void * d)
{
  <bb 2> [local count: 1073741824]:
  __builtin_memcpy (d_2(D), &a, 9); [tail call]
  return;

}


        .p2align 4
        .globl  f
        .type   f, @function
f:
.LFB0:
        .cfi_startproc
        movabsq $578437695752307201, %rax
        movb    $9, 8(%rdi)
        movq    %rax, (%rdi)
        ret
        .cfi_endproc
.LFE0:
        .size   f, .-f

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

g (void * d)
{
  <bb 2> [local count: 1073741824]:
  __builtin_memcpy (d_2(D), &b, 9); [tail call]
  return;

}


        .p2align 4
        .globl  g
        .type   g, @function
g:
.LFB1:
        .cfi_startproc
        movq    b(%rip), %rax
        movq    %rax, (%rdi)
        movzbl  b+8(%rip), %eax
        movb    %al, 8(%rdi)
        ret
        .cfi_endproc
.LFE1:
        .size   g, .-g
        .globl  b
        .section        .rodata
        .align 8
        .type   b, @object
        .size   b, 10
b:
        .string ""
        .string "\001\002\003\004\005\006\007\b"
        .globl  a
        .align 8
        .type   a, @object
        .size   a, 10
a:
        .string "\001\002\003\004\005\006\007\b\t"
        .ident  "GCC: (GNU) 10.1.1 20200527"
        .section        .note.GNU-stack,"",@progbits

             reply	other threads:[~2020-06-24 21:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-24 21:14 msebor at gcc dot gnu.org [this message]
2020-06-24 23:32 ` [Bug middle-end/95886] " msebor at gcc dot gnu.org
2020-06-24 23:36 ` msebor at gcc dot gnu.org
2020-07-01  0:26 ` msebor at gcc dot gnu.org
2020-07-20 18:09 ` cvs-commit at gcc dot gnu.org
2020-07-20 18:10 ` msebor at gcc dot gnu.org
2020-07-23 20:10 ` cvs-commit at gcc dot gnu.org
2020-07-24  2:28 ` cvs-commit at gcc dot gnu.org
2020-10-08 18:38 ` cvs-commit at gcc dot gnu.org
2020-10-16 11:37 ` cvs-commit at gcc dot gnu.org
2020-10-16 11:37 ` cvs-commit at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-95886-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).