From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5D6613858C83; Thu, 20 Apr 2023 13:50:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5D6613858C83 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681998637; bh=kz9xs4AGjU6J5g7XJQ4hGJPKbQ1HTYFy1W5Wlz4RllI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=sMWunHqow09LNFyNNHHkHD5JZ2+CbQ1tI0LNh9ywER0vq5qqq5K0aNOPj8qJ08vT2 DwsN+7y7a2/F4WvcPkjgItg4EKEe5qhL720NuEvubU2bhonEMT91lJ+DMwSxCm8mCo /VM/JY/yHBUyMUtfd78RriwEDWIVF1Fe5xxMs68Q= From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/108846] std::copy, std::copy_n and std::copy_backward on potentially overlapping subobjects Date: Thu, 20 Apr 2023 13:50:35 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 13.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: redi at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D108846 --- Comment #22 from Jonathan Wakely --- (In reply to Jonathan Wakely from comment #16) > const ptrdiff_t _Num =3D __last - __first; > - if (_Num) > + if (__builtin_expect(_Num > 1, true)) > __builtin_memmove(__result, __first, sizeof(_Tp) * _Num); > + else if (_Num =3D=3D 1) Richi suggested that we could avoid these runtime branches (which hurt optimization, see PR 109445) if we knew how many bytes of tail padding there are in _Tp, e.g., __builtin_memmove(__result, __first, sizeof(_Tp) * _Num - __padding_bytes= ); This would mean we copy whole objects for the first Num - 1 elements, and t= hen only copy the non-padding bytes for the last element. This would be conform= ing, because there's no guarantee that padding bits are copied by assignment any= way, and std::copy is specified in terms of assignment. When copying a single base-class object (the subject of this bug) the last element is the only element, so we solve the problem. We don't have a built-in to tell us the number of padding bytes, but we mig= ht be able to use something like this: template constexpr size_t potentially_overlapping_tail_padding() { if constexpr (is_final::value) return 0; else { struct D1 : T { char c1; }; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Winvalid-offsetof" return sizeof(T) - offsetof(D1, c1); #pragma GCC diagnostic pop } } For pre-C++17 we would need to use tag dispatching and __is_final instead of if-constexpr, but that's doable. For pre-C++14 it can't be constexpr, but that's OK too.=