From: Jason Merrill <jason@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [pushed] c++: build initializer_list<string> in a loop [PR105838]
Date: Mon, 12 Jun 2023 04:56:17 -0400 [thread overview]
Message-ID: <20230612085617.3908962-1-jason@redhat.com> (raw)
Tested x86_64-pc-linux-gnu, applying to trunk.
-- 8< --
I previously applied this change in r13-4565 but reverted it due to
PR108071. That PR was then fixed by r13-4712, but I didn't re-apply this
change then because we weren't making the array static; since r14-1500 for
PR110070 we now make the initializer array static, so let's bring this back.
In situations where the maybe_init_list_as_range optimization isn't viable,
we can build an initializer_list<string> with a loop over a constant array
of string literals.
This is represented using a VEC_INIT_EXPR, which required adjusting a couple
of places that expected the initializer array to have the same type as the
target array and fixing build_vec_init not to undo our efforts.
PR c++/105838
gcc/cp/ChangeLog:
* call.cc (convert_like_internal) [ck_list]: Use
maybe_init_list_as_array.
* constexpr.cc (cxx_eval_vec_init_1): Init might have
a different type.
* tree.cc (build_vec_init_elt): Likewise.
* init.cc (build_vec_init): Handle from_array from a
TARGET_EXPR. Retain TARGET_EXPR of a different type.
gcc/testsuite/ChangeLog:
* g++.dg/tree-ssa/initlist-opt5.C: New test.
---
gcc/cp/call.cc | 11 ++++++++-
gcc/cp/constexpr.cc | 6 ++---
gcc/cp/init.cc | 13 ++++++++--
gcc/cp/tree.cc | 2 --
gcc/testsuite/g++.dg/tree-ssa/initlist-opt5.C | 24 +++++++++++++++++++
5 files changed, 48 insertions(+), 8 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/tree-ssa/initlist-opt5.C
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 354773f00c6..68cf878308e 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -8541,7 +8541,16 @@ convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
unsigned len = CONSTRUCTOR_NELTS (expr);
tree array;
- if (len)
+ if (tree init = maybe_init_list_as_array (elttype, expr))
+ {
+ elttype = cp_build_qualified_type
+ (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
+ array = build_array_of_n_type (elttype, len);
+ array = build_vec_init_expr (array, init, complain);
+ array = get_target_expr (array);
+ array = cp_build_addr_expr (array, complain);
+ }
+ else if (len)
{
tree val; unsigned ix;
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 8f7f0b7d325..bbecf86d58b 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -5240,12 +5240,12 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
else
{
/* Copying an element. */
- gcc_assert (same_type_ignoring_top_level_qualifiers_p
- (atype, TREE_TYPE (init)));
eltinit = cp_build_array_ref (input_location, init, idx, complain);
if (!lvalue_p (init))
eltinit = move (eltinit);
- eltinit = force_rvalue (eltinit, complain);
+ eltinit = (perform_implicit_conversion_flags
+ (elttype, eltinit, complain,
+ LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
non_constant_p, overflow_p);
}
diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
index 6ccda365b04..af6e30f511e 100644
--- a/gcc/cp/init.cc
+++ b/gcc/cp/init.cc
@@ -4475,7 +4475,9 @@ build_vec_init (tree base, tree maxindex, tree init,
/* Look through the TARGET_EXPR around a compound literal. */
if (init && TREE_CODE (init) == TARGET_EXPR
&& TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR
- && from_array != 2)
+ && from_array != 2
+ && (same_type_ignoring_top_level_qualifiers_p
+ (TREE_TYPE (init), atype)))
init = TARGET_EXPR_INITIAL (init);
if (tree vi = get_vec_init_expr (init))
@@ -4601,7 +4603,14 @@ build_vec_init (tree base, tree maxindex, tree init,
{
if (lvalue_kind (init) & clk_rvalueref)
xvalue = true;
- base2 = decay_conversion (init, complain);
+ if (TREE_CODE (init) == TARGET_EXPR)
+ {
+ /* Avoid error in decay_conversion. */
+ base2 = decay_conversion (TARGET_EXPR_SLOT (init), complain);
+ base2 = cp_build_compound_expr (init, base2, tf_none);
+ }
+ else
+ base2 = decay_conversion (init, complain);
if (base2 == error_mark_node)
return error_mark_node;
itype = TREE_TYPE (base2);
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index 4d5e3f53c5e..751c9adeb62 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -785,8 +785,6 @@ build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
releasing_vec argvec;
if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
{
- gcc_assert (same_type_ignoring_top_level_qualifiers_p
- (type, TREE_TYPE (init)));
tree init_type = strip_array_types (TREE_TYPE (init));
tree dummy = build_dummy_object (init_type);
if (!lvalue_p (init))
diff --git a/gcc/testsuite/g++.dg/tree-ssa/initlist-opt5.C b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt5.C
new file mode 100644
index 00000000000..fc928bb5405
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt5.C
@@ -0,0 +1,24 @@
+// PR c++/105838
+// { dg-additional-options -fdump-tree-gimple }
+// { dg-do compile { target c++11 } }
+
+// Check that we call the basic_string constructor once (and define it once).
+// { dg-final { scan-tree-dump-times {>::basic_string} 2 "gimple" } }
+
+#include <string>
+
+void g (const void *);
+
+void f (const char *p)
+{
+ std::initializer_list<std::string> lst = {
+ "aahing", "aaliis", "aarrgh", "abacas", "abacus", "abakas", "abamps", "abands", "abased", "abaser", "abases", "abasia",
+ "abated", "abater", "abates", "abatis", "abator", "abattu", "abayas", "abbacy", "abbess", "abbeys", "abbots", "abcees",
+ "abdabs", "abduce", "abduct", "abears", "abeigh", "abeles", "abelia", "abends", "abhors", "abided", "abider", "abides",
+ "abject", "abjure", "ablate", "ablaut", "ablaze", "ablest", "ablets", "abling", "ablins", "abloom", "ablush", "abmhos",
+ "aboard", "aboded", "abodes", "abohms", "abolla", "abomas", "aboral", "abords", "aborne", "aborts", "abound", "abouts",
+ "aboves", "abrade", "abraid", "abrash", "abrays", "abrazo", "abrege", "abrins", "abroad", "abrupt", "abseil", "absent",
+ };
+
+ g(&lst);
+}
base-commit: 20c85207abd543c76818032632a5f1b3aea5495b
--
2.31.1
next reply other threads:[~2023-06-12 8:56 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-12 8:56 Jason Merrill [this message]
-- strict thread matches above, loose matches on Subject: below --
2022-12-08 18:45 Jason Merrill
2022-12-13 14:07 ` Stephan Bergmann
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=20230612085617.3908962-1-jason@redhat.com \
--to=jason@redhat.com \
--cc=gcc-patches@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).