public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jakub at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/105329] [12/13 Regression] Bogus restrict warning when assigning 1-char string literal to std::string since r12-3347-g8af8abfbbace49e6
Date: Mon, 02 May 2022 13:58:50 +0000	[thread overview]
Message-ID: <bug-105329-4-mlCmQgxJI9@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-105329-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105329

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #14 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Here is an untested patch for the cold part of the function:
--- libstdc++-v3/include/bits/basic_string.h.jj 2022-01-21 22:48:42.220261654
+0100
+++ libstdc++-v3/include/bits/basic_string.h    2022-05-02 15:51:42.381923962
+0200
@@ -2504,6 +2504,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
                     _CharT __c);

+      __attribute__((__noinline__, __noclone__, __cold__)) void
+      _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s,
+                     const size_type __len2, const size_type __how_much);
+
       _GLIBCXX20_CONSTEXPR
       basic_string&
       _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
--- libstdc++-v3/include/bits/basic_string.tcc.jj       2022-01-11
22:31:41.482757256 +0100
+++ libstdc++-v3/include/bits/basic_string.tcc  2022-05-02 15:51:38.630975348
+0200
@@ -471,6 +471,37 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     }

   template<typename _CharT, typename _Traits, typename _Alloc>
+    __attribute__((__noinline__, __noclone__, __cold__)) void
+    basic_string<_CharT, _Traits, _Alloc>::
+    _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s,
+                   const size_type __len2, const size_type __how_much)
+    {
+      // Work in-place.
+      if (__len2 && __len2 <= __len1)
+       this->_S_move(__p, __s, __len2);
+      if (__how_much && __len1 != __len2)
+       this->_S_move(__p + __len2, __p + __len1, __how_much);
+      if (__len2 > __len1)
+       {
+         if (__s + __len2 <= __p + __len1)
+           this->_S_move(__p, __s, __len2);
+         else if (__s >= __p + __len1)
+           {
+             // Hint to middle end that __p and __s overlap
+             // (PR 98465).
+             const size_type __poff = (__s - __p) + (__len2 - __len1);
+             this->_S_copy(__p, __p + __poff, __len2);
+           }
+         else
+           {
+             const size_type __nleft = (__p + __len1) - __s;
+             this->_S_move(__p, __s, __nleft);
+             this->_S_copy(__p + __nleft, __p + __len2, __len2 - __nleft);
+           }
+       }
+    }
+
+  template<typename _CharT, typename _Traits, typename _Alloc>
     _GLIBCXX20_CONSTEXPR
     basic_string<_CharT, _Traits, _Alloc>&
     basic_string<_CharT, _Traits, _Alloc>::
@@ -500,7 +531,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
            }
          else
 #endif
-         if (_M_disjunct(__s))
+         if (__builtin_expect(_M_disjunct(__s),true))
            {
              if (__how_much && __len1 != __len2)
                this->_S_move(__p + __len2, __p + __len1, __how_much);
@@ -508,32 +539,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
                this->_S_copy(__p, __s, __len2);
            }
          else
-           {
-             // Work in-place.
-             if (__len2 && __len2 <= __len1)
-               this->_S_move(__p, __s, __len2);
-             if (__how_much && __len1 != __len2)
-               this->_S_move(__p + __len2, __p + __len1, __how_much);
-             if (__len2 > __len1)
-               {
-                 if (__s + __len2 <= __p + __len1)
-                   this->_S_move(__p, __s, __len2);
-                 else if (__s >= __p + __len1)
-                   {
-                     // Hint to middle end that __p and __s overlap
-                     // (PR 98465).
-                     const size_type __poff = (__s - __p) + (__len2 - __len1);
-                     this->_S_copy(__p, __p + __poff, __len2);
-                   }
-                 else
-                   {
-                     const size_type __nleft = (__p + __len1) - __s;
-                     this->_S_move(__p, __s, __nleft);
-                     this->_S_copy(__p + __nleft, __p + __len2,
-                                   __len2 - __nleft);
-                   }
-               }
-           }
+           _M_replace_cold(__p, __len1, __s, __len2, __how_much);
        }
       else
        this->_M_mutate(__pos, __len1, __s, __len2);
Note, on the #c0 testcase alone it actually results in larger generated code
(unless we arrange for it to be exported from libstdc++, for which it is quite
late for 12.1 right now), but say on:
#include <string>

void f (std::string& s)
{
  s = "5";
}

void g (std::string& s)
{
  s = "56";
}

void h (std::string& s)
{
  s = "6";
}
it is already smaller (note, obviously it doesn't need to be in the same TU).

  parent reply	other threads:[~2022-05-02 13:58 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-21  7:41 [Bug c++/105329] New: Bogus restrict warning when assigning 1-char string literal to std::string boris at kolpackov dot net
2022-04-21 11:15 ` [Bug tree-optimization/105329] " rguenth at gcc dot gnu.org
2022-04-21 20:50 ` amacleod at redhat dot com
2022-04-21 22:51 ` amacleod at redhat dot com
2022-04-22 10:31 ` aldyh at gcc dot gnu.org
2022-04-25 17:50 ` amacleod at redhat dot com
2022-04-25 22:25 ` mattias.ellert at physics dot uu.se
2022-04-26 15:04 ` [Bug tree-optimization/105329] [12 Regression] Bogus restrict warning when assigning 1-char string literal to std::string since r12-3347-g8af8abfbbace49e6 marxin at gcc dot gnu.org
2022-05-02 12:44 ` [Bug tree-optimization/105329] [12/13 " rguenth at gcc dot gnu.org
2022-05-02 12:45 ` rguenth at gcc dot gnu.org
2022-05-02 12:46 ` rguenth at gcc dot gnu.org
2022-05-02 12:57 ` rguenth at gcc dot gnu.org
2022-05-02 13:22 ` rguenth at gcc dot gnu.org
2022-05-02 13:33 ` jakub at gcc dot gnu.org
2022-05-02 13:58 ` jakub at gcc dot gnu.org [this message]
2022-05-02 16:04 ` jakub at gcc dot gnu.org
2022-05-02 19:43 ` jakub at gcc dot gnu.org
2022-05-02 22:08 ` redi at gcc dot gnu.org
2022-05-06  3:59 ` mattias.ellert at physics dot uu.se
2022-05-06  8:33 ` jakub at gcc dot gnu.org
2022-05-20  8:13 ` rguenth at gcc dot gnu.org
2022-07-26 12:05 ` jakub at gcc dot gnu.org
2022-09-12  9:37 ` cvs-commit at gcc dot gnu.org
2022-09-29 15:19 ` msebor at gcc dot gnu.org
2022-09-30 16:55 ` dan at stahlke dot org
2022-09-30 17:16 ` dan at stahlke dot org
2023-02-21 18:30 ` 49tbwddbqeazdawz at chyen dot cc
2023-02-21 19:28 ` redi at gcc dot gnu.org
2023-02-22 17:28 ` 49tbwddbqeazdawz at chyen dot cc
2023-02-22 17:52 ` redi at gcc dot gnu.org
2023-02-24 14:10 ` wielkiegie at gmail dot com
2023-05-08 12:24 ` [Bug tree-optimization/105329] [12/13/14 " rguenth at gcc dot gnu.org

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=bug-105329-4-mlCmQgxJI9@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).