https://gcc.gnu.org/g:aac5c57ee167230cea466064951daf06e42197b9

commit r15-5361-gaac5c57ee167230cea466064951daf06e42197b9
Author: Jan Hubicka <hubi...@ucw.cz>
Date:   Sun Nov 17 01:21:04 2024 +0100

    Add __builtion_unreachable to vector::size(), vector::capacity()
    
    This patch makes it clear that vector sizes and capacities are not
    negative.  With recent change to ipa-fnsummary this should not affect
    inlining and improves codegen of some vector manipulation functions.
    
    I tested clang build.  Looking for throw_bad calls there are only 3
    called considerably often (bad_allloc, bad_array_new_length and
    function_callv).
    The patch seems to reduce bad_alloc and bad_array_new_length calls
    considerably:
    
    bad_alloc 380->147
    bad_array_new_length 832->128
    
    libstdc++-v3/ChangeLog:
    
            PR tree-optimization/109442
            * include/bits/stl_vector.h: (vector::size(),
            vector::capacity()): Add __builtin_unreachable call to announce
            that size and capacity are non-negative.
    
    gcc/testsuite/ChangeLog:
    
            PR tree-optimization/109442
            * g++.dg/tree-ssa/pr109442.C: New test.

Diff:
---
 gcc/testsuite/g++.dg/tree-ssa/pr109442.C |  2 +-
 libstdc++-v3/include/bits/stl_vector.h   | 14 +++++++++++---
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr109442.C 
b/gcc/testsuite/g++.dg/tree-ssa/pr109442.C
index ec40c470c8dd..ea5800aa1340 100644
--- a/gcc/testsuite/g++.dg/tree-ssa/pr109442.C
+++ b/gcc/testsuite/g++.dg/tree-ssa/pr109442.C
@@ -1,5 +1,5 @@
 // { dg-do compile { target c++11 } }
-// { dg-options "-O1 -fdump-tree-optimized" }
+// { dg-options "-O2 -fdump-tree-optimized" }
 #include <vector>
 #define T int
 T vat1(std::vector<T> v1) {
diff --git a/libstdc++-v3/include/bits/stl_vector.h 
b/libstdc++-v3/include/bits/stl_vector.h
index df48ba3377fa..3ece16b2facd 100644
--- a/libstdc++-v3/include/bits/stl_vector.h
+++ b/libstdc++-v3/include/bits/stl_vector.h
@@ -1114,7 +1114,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
       size_type
       size() const _GLIBCXX_NOEXCEPT
-      { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
+      {
+       ptrdiff_t __dif = this->_M_impl._M_finish - this->_M_impl._M_start;
+       if (__dif < 0)
+          __builtin_unreachable ();
+       return size_type(__dif);
+      }
 
       /**  Returns the size() of the largest possible %vector.  */
       _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
@@ -1201,8 +1206,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       size_type
       capacity() const _GLIBCXX_NOEXCEPT
       {
-       return size_type(this->_M_impl._M_end_of_storage
-                          - this->_M_impl._M_start);
+       ptrdiff_t __dif = this->_M_impl._M_end_of_storage
+                         - this->_M_impl._M_start;
+       if (__dif < 0)
+          __builtin_unreachable ();
+       return size_type(__dif);
       }
 
       /**

Reply via email to