From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id C543F38515D7; Tue, 28 Mar 2023 23:33:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C543F38515D7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680046433; bh=OpZN0LPUMhSUHyZWUgD7z6woDSZWsEh9DUWYKol6HU0=; h=From:To:Subject:Date:From; b=QBJTRJCRTPJYtwzy1szTzMxx7ynCEHfS0L3zy70Sv8ZLNWx7aq0vxK2GqnvULhAUC 17m4u+FXJ7XRQB/6bNguVxqjRJpEfJiqsAu8ZjJjUYoy5aVZcsJIs4ZvlLBQZehSnm hL5Ba8Pv8cWSYZ9YX6O168DslDM1t3t07snM6ZJM= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-9330] libstdc++: Tell GCC what basic_string::_M_is_local() means [PR109299] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 1729d03652a700342f23c2fa6f249d9dec5e9e02 X-Git-Newrev: 00ac6fa3f2a54fb9cc17b7b7f51eae3c6bf7a1bd Message-Id: <20230328233353.C543F38515D7@sourceware.org> Date: Tue, 28 Mar 2023 23:33:53 +0000 (GMT) List-Id: https://gcc.gnu.org/g:00ac6fa3f2a54fb9cc17b7b7f51eae3c6bf7a1bd commit r12-9330-g00ac6fa3f2a54fb9cc17b7b7f51eae3c6bf7a1bd Author: Jonathan Wakely Date: Tue Mar 28 10:50:40 2023 +0100 libstdc++: Tell GCC what basic_string::_M_is_local() means [PR109299] This avoids a bogus warning about overflowing a buffer, because GCC can't tell that we don't copy into the buffer unless it fits. By adding a __builtin_unreachable() hint we inform the compiler about the invariant that the buffer is only used when it's big enough. This can also improve codegen, by eliminating dead code that GCC couldn't tell was unreachable. libstdc++-v3/ChangeLog: PR libstdc++/109299 * include/bits/basic_string.h (basic_string::_M_is_local()): Add hint for compiler that local strings fit in the local buffer. (cherry picked from commit bf78b43873b0b7e8f9a430df38749b8b61f9c9b8) Diff: --- libstdc++-v3/include/bits/basic_string.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index f1a0e6984a1..1dbe7c2415c 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -271,7 +271,15 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 _GLIBCXX20_CONSTEXPR bool _M_is_local() const - { return _M_data() == _M_local_data(); } + { + if (_M_data() == _M_local_data()) + { + if (_M_string_length > _S_local_capacity) + __builtin_unreachable(); + return true; + } + return false; + } // Create & Destroy _GLIBCXX20_CONSTEXPR