From 1046c32de4956c3d706a2ff8683582fd21b8f360 Mon Sep 17 00:00:00 2001 From: Jan Hubicka Date: Wed, 27 Nov 2024 23:52:37 +0100 Subject: optimize basic_string Add __builtin_unreachable conditionls to declare value ranges of basic_string::length(). FIx max_size() to return actual max size using logic similar to std::vector. Aviod use of size() in empty() to save some compile time overhead. As disucced, max_size() change is technically ABI breaking, but hopefully this does not really matter in practice. Change of length() breaks empty-loop testcase where we now optimize the loop only after inlining, so template is updated to check cddce3 instead of cddce2. This is PR117764. With these chages we now optimize out unused strings as tested in string-1.C libstdc++-v3/ChangeLog: * include/bits/basic_string.h (basic_string::size(), basic_string::length(), basic_string::capacity()): Add __builtin_unreachable to declare value ranges. (basic_string::empty()): Implement directly (basic_string::max_size()): Account correctly the terminating 0 and limits implied by ptrdiff_t. gcc/testsuite/ChangeLog: * g++.dg/tree-ssa/empty-loop.C: xfail optimization at cddce2 and check it happens at cddce3. * g++.dg/tree-ssa/string-1.C: New test. --- gcc/testsuite/g++.dg/tree-ssa/empty-loop.C | 5 ++++- gcc/testsuite/g++.dg/tree-ssa/string-1.C | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/tree-ssa/string-1.C (limited to 'gcc') diff --git a/gcc/testsuite/g++.dg/tree-ssa/empty-loop.C b/gcc/testsuite/g++.dg/tree-ssa/empty-loop.C index ed4a603..b7e7e27 100644 --- a/gcc/testsuite/g++.dg/tree-ssa/empty-loop.C +++ b/gcc/testsuite/g++.dg/tree-ssa/empty-loop.C @@ -30,5 +30,8 @@ int foo (vector &v, list &l, set &s, map &m return 0; } -/* { dg-final { scan-tree-dump-not "if" "cddce2"} } */ +/* Adding __builtin_unreachable to std::string::size() prevents cddce2 from + eliminating the loop early, see PR117764. */ +/* { dg-final { scan-tree-dump-not "if" "cddce2" { xfail *-*-* } } } */ +/* { dg-final { scan-tree-dump-not "if" "cddce3"} } */ diff --git a/gcc/testsuite/g++.dg/tree-ssa/string-1.C b/gcc/testsuite/g++.dg/tree-ssa/string-1.C new file mode 100644 index 0000000..d38c23a --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/string-1.C @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -std=c++20 -fdump-tree-optimized" } */ +#include +std::string +test (std::string &a) +{ + return a; +} +/* { dg-final { scan-tree-dump-not "throw" "optimized" } } */ -- cgit v1.1