public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rguenth 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:22:27 +0000	[thread overview]
Message-ID: <bug-105329-4-5wxourADXR@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

--- Comment #12 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #11)
> (In reply to Richard Biener from comment #10)
> > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104336#c1 contains a workaround
> > for libstdc++
> 
> Maybe
> 
>       _GLIBCXX20_CONSTEXPR
>       basic_string&
>       assign(const _CharT* __s)
>       {
>         __glibcxx_requires_string(__s);
>         return _M_replace(size_type(0), this->size(), __s,
>                           traits_type::length(__s));
>       }
> 
> isn't the most efficient way to assign a string (constant) looking at
> the complexity of _M_replace and its overlap checks (that we all expect
> to be optimized away but which are not).  In fact the _M_disjunct check
> isn't optimized because we cannot tell if the std::string storage
> overlaps the constant pool ...

Maybe

diff --git a/libstdc++-v3/include/bits/basic_string.h
b/libstdc++-v3/include/bits/basic_string.h
index c3fbc53953c..e83f3e8afa4 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -407,7 +407,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       bool
       _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
       {
-       return (less<const _CharT*>()(__s, _M_data())
+       return (__builtin_constant_p (__s)
+               || less<const _CharT*>()(__s, _M_data())
                || less<const _CharT*>()(_M_data() + this->size(), __s));
       }

but oddly we folds the __builtin_constant_p to false early
but we treat __builtin_constant_p ("foo") as true.  That's inconsistent in
fold_builtin_constant_p:

  /* If this expression has side effects, show we don't know it to be a
     constant.  Likewise if it's a pointer or aggregate type since in
     those case we only want literals, since those are only optimized
     when generating RTL, not later.
     And finally, if we are compiling an initializer, not code, we
     need to return a definite result now; there's not going to be any
     more optimization done.  */
  if (TREE_SIDE_EFFECTS (arg)
      || AGGREGATE_TYPE_P (TREE_TYPE (arg))
      || POINTER_TYPE_P (TREE_TYPE (arg))
      || cfun == 0
      || folding_initializer
      || force_folding_builtin_constant_p)
    return integer_zero_node;

fixing that (removing POINTER_TYPE_P) makes the workaround work (and
the generated code simplifies greatly).

  parent reply	other threads:[~2022-05-02 13:22 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 [this message]
2022-05-02 13:33 ` jakub at gcc dot gnu.org
2022-05-02 13:58 ` jakub at gcc dot gnu.org
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-5wxourADXR@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).