https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107087
--- Comment #10 from Jonathan Wakely <redi at gcc dot gnu.org> --- I wonder if some other hints about properties of the empty rep would help codegen: --- a/libstdc++-v3/include/bits/cow_string.h +++ b/libstdc++-v3/include/bits/cow_string.h @@ -204,6 +204,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION bool _M_is_leaked() const _GLIBCXX_NOEXCEPT { +#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 && __OPTIMIZE__ + if (_S_empty_rep()._M_refcount != 0) + __builtin_unreachable(); +#endif + #if defined(__GTHREADS) // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose, // so we need to use an atomic load. However, _M_is_leaked @@ -218,6 +223,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION bool _M_is_shared() const _GLIBCXX_NOEXCEPT { +#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 && __OPTIMIZE__ + if (_S_empty_rep()._M_refcount != 0) + __builtin_unreachable(); +#endif + #if defined(__GTHREADS) // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose, // so we need to use an atomic load. Another thread can drop last @@ -907,17 +917,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