public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "barry.revzin at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/68350] std::uninitialized_copy overly restrictive for trivially_copyable types
Date: Wed, 23 Feb 2022 22:03:38 +0000	[thread overview]
Message-ID: <bug-68350-4-CND3KiWtCI@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-68350-4@http.gcc.gnu.org/bugzilla/>

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

Barry Revzin <barry.revzin at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |barry.revzin at gmail dot com

--- Comment #8 from Barry Revzin <barry.revzin at gmail dot com> ---
Here's an example:

struct A { int i; };
struct B { int i{}; };

Both are trivially copyable, A is additionally trivially default constructible
while B is not. This distinction is irrelevant in this case - we're copying,
and that does not involve default constructing an A or B (trivial or
otherwise).

But this means that __is_trivial(B) is false, so we don't go into std::copy, so
these do different things:

#include <memory>

struct A { int i; };
struct B { int i{}; };

void copy_a(A* f, A* l, A* out) {
    std::uninitialized_copy(f, l, out);
}

void copy_b(B* f, B* l, B* out) {
    std::uninitialized_copy(f, l, out);
}

emits (with g++ 11.2, -std=c++20 -O3, https://godbolt.org/z/eG9hsbcTE):

copy_a(A*, A*, A*):
        mov     r8, rdi
        mov     rdi, rdx
        cmp     r8, rsi
        je      .L1
        mov     rdx, rsi
        mov     rsi, r8
        sub     rdx, r8
        jmp     memmove
.L1:
        ret
copy_b(B*, B*, B*):
        cmp     rdi, rsi
        je      .L4
        sub     rsi, rdi
        xor     eax, eax
.L6:
        mov     ecx, DWORD PTR [rdi+rax]
        mov     DWORD PTR [rdx+rax], ecx
        add     rax, 4
        cmp     rax, rsi
        jne     .L6
.L4:
        ret

Whereas the copy_b case could also use memmove.

  parent reply	other threads:[~2022-02-23 22:03 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-68350-4@http.gcc.gnu.org/bugzilla/>
2020-04-09  9:31 ` redi at gcc dot gnu.org
2022-02-23 22:03 ` barry.revzin at gmail dot com [this message]
2022-02-24 12:19 ` redi at gcc dot gnu.org
2022-02-24 14:20 ` redi at gcc dot gnu.org
2022-02-24 14:45 ` redi at gcc dot gnu.org
2022-02-24 14:49 ` arthur.j.odwyer at gmail dot com
2022-02-25  9:43 ` redi at gcc dot gnu.org
2023-04-26  6:55 ` rguenth at gcc dot gnu.org
2023-07-27  9:21 ` rguenth at gcc dot gnu.org
2024-05-21  9:10 ` jakub 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-68350-4-CND3KiWtCI@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).