From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Teach optimizer that empty COW strings are empty [PR107087]
Date: Fri, 31 Mar 2023 23:45:41 +0100 [thread overview]
Message-ID: <20230331224541.102599-1-jwakely@redhat.com> (raw)
Tested powerpc64le-linux. Pushed to trunk.
-- >8 --
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().
---
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
--
2.39.2
reply other threads:[~2023-03-31 22:45 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230331224541.102599-1-jwakely@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).