From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id BC62A3858D28; Fri, 31 Mar 2023 22:45:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BC62A3858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680302730; bh=2Yfy1dM9eWtFGklCjfOhXmnznjg3A/xWFtZrC/jazpo=; h=From:To:Subject:Date:From; b=HNZ2RJ8+r2wJqwu7OAcu65N/j95A+oB2JahjECsmG6tqRhrHXnAjgYdTLoiRVUllR O+gG9yoMsiRwghcDSJFdYWk7Q74Htw7XJhB3vG6xmvJEA4TuNi9nfCqojBZqzRhW8B Nqu/vTQZypmja2CG9lHVWaUGd9vUyu+9xiNcCCk4= 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-6962] libstdc++: Teach optimizer that empty COW strings are empty [PR107087] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 92f02e754ca2fbcd56dbd7b3949147d50bab99a0 X-Git-Newrev: 4969dcd2b7a94ce6c0d07225b21b5f3c040a4902 Message-Id: <20230331224530.BC62A3858D28@sourceware.org> Date: Fri, 31 Mar 2023 22:45:30 +0000 (GMT) List-Id: https://gcc.gnu.org/g:4969dcd2b7a94ce6c0d07225b21b5f3c040a4902 commit r13-6962-g4969dcd2b7a94ce6c0d07225b21b5f3c040a4902 Author: Jonathan Wakely Date: Fri Mar 31 13:44:04 2023 +0100 libstdc++: Teach optimizer that empty COW strings are empty [PR107087] The compiler doesn't know about the invariant that the _S_empty_rep() object is immutable and so _M_length and _M_refcount are always zero. This means that we get warnings about writing possibly-non-zero length strings into buffers that can't hold them. If we teach the compiler that the empty rep is always zero length, it knows it can be copied into any buffer. For Stage 1 we might want to also consider adding this to capacity(): if (_S_empty_rep()._M_capacity != 0) __builtin_unreachable(); And this to _Rep::_M_is_leaked() and _Rep::_M_is_shared(): if (_S_empty_rep()._M_refcount != 0) __builtin_unreachable(); libstdc++-v3/ChangeLog: PR tree-optimization/107087 * include/bits/cow_string.h (basic_string::size()): Add optimizer hint that _S_empty_rep()._M_length is always zero. (basic_string::length()): Call size(). Diff: --- libstdc++-v3/include/bits/cow_string.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/bits/cow_string.h b/libstdc++-v3/include/bits/cow_string.h index 1ee84e60678..b6024365d4f 100644 --- a/libstdc++-v3/include/bits/cow_string.h +++ b/libstdc++-v3/include/bits/cow_string.h @@ -907,17 +907,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION public: // Capacity: + /// Returns the number of characters in the string, not including any /// null-termination. size_type size() const _GLIBCXX_NOEXCEPT - { return _M_rep()->_M_length; } + { +#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 && __OPTIMIZE__ + if (_S_empty_rep()._M_length != 0) + __builtin_unreachable(); +#endif + return _M_rep()->_M_length; + } /// Returns the number of characters in the string, not including any /// null-termination. size_type length() const _GLIBCXX_NOEXCEPT - { return _M_rep()->_M_length; } + { return size(); } /// Returns the size() of the largest possible %string. size_type