From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 18D50385559E; Tue, 28 Mar 2023 20:14:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 18D50385559E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680034460; bh=Z7jXg+hLqyaEhPvIVQ9zGe4ab+xAXsMY7og68+p6DqM=; h=From:To:Subject:Date:From; b=UBJXRA8A42QAU+KbYdNfHVI8GU2J42IW231R6LAEe6dzFuP1jxGUWbn9iPzGJUC78 RtTAqnfepc5+nfX13rvmC+22lmN88T1F6yrWFEfZcfowwu+dnPRN0CFPVRgJSCfppJ sUvsIofWeDr9cS1jHr+16l747rgfDbh8Ux+v4Vdo= 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 r13-6915] 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/master X-Git-Oldrev: cf19ef9eca82b01dd0a3a0a8e4c8bcec236eb2d9 X-Git-Newrev: bf78b43873b0b7e8f9a430df38749b8b61f9c9b8 Message-Id: <20230328201420.18D50385559E@sourceware.org> Date: Tue, 28 Mar 2023 20:14:20 +0000 (GMT) List-Id: https://gcc.gnu.org/g:bf78b43873b0b7e8f9a430df38749b8b61f9c9b8 commit r13-6915-gbf78b43873b0b7e8f9a430df38749b8b61f9c9b8 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. 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 1b8ebca7dad..5d040e2897d 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