c++: Only do maybe_init_list_as_range optimization if !processing_template_decl [PR108047]
Checks
Commit Message
Hi!
The last testcase in this patch ICEs, because
maybe_init_list_as_range -> maybe_init_list_as_array
calls maybe_constant_init in:
/* Don't do this if the conversion would be constant. */
first = maybe_constant_init (first);
if (TREE_CONSTANT (first))
return NULL_TREE;
but maybe_constant_init shouldn't be called when processing_template_decl.
While we could replace that call with fold_non_dependent_init, my limited
understanding is that this is an optimization and even if we don't optimize
it when processing_template_decl, build_user_type_conversion_1 will be
called again during instantiation with !processing_template_decl if it is
every instantiated and we can do the optimization only then.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
Or do you want fold_non_dependent_init instead?
2023-01-09 Jakub Jelinek <jakub@redhat.com>
PR c++/105838
PR c++/108047
PR c++/108266
* call.cc (maybe_init_list_as_range): Always return NULL_TREE if
processing_template_decl.
* g++.dg/tree-ssa/initlist-opt2.C: New test.
* g++.dg/tree-ssa/initlist-opt3.C: New test.
Jakub
Comments
On 1/9/23 05:19, Jakub Jelinek wrote:
> Hi!
>
> The last testcase in this patch ICEs, because
> maybe_init_list_as_range -> maybe_init_list_as_array
> calls maybe_constant_init in:
> /* Don't do this if the conversion would be constant. */
> first = maybe_constant_init (first);
> if (TREE_CONSTANT (first))
> return NULL_TREE;
> but maybe_constant_init shouldn't be called when processing_template_decl.
> While we could replace that call with fold_non_dependent_init, my limited
> understanding is that this is an optimization and even if we don't optimize
> it when processing_template_decl, build_user_type_conversion_1 will be
> called again during instantiation with !processing_template_decl if it is
> every instantiated and we can do the optimization only then.
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
OK.
> Or do you want fold_non_dependent_init instead?
>
> 2023-01-09 Jakub Jelinek <jakub@redhat.com>
>
> PR c++/105838
> PR c++/108047
> PR c++/108266
> * call.cc (maybe_init_list_as_range): Always return NULL_TREE if
> processing_template_decl.
>
> * g++.dg/tree-ssa/initlist-opt2.C: New test.
> * g++.dg/tree-ssa/initlist-opt3.C: New test.
>
> --- gcc/cp/call.cc.jj 2022-12-15 09:24:44.265935297 +0100
> +++ gcc/cp/call.cc 2023-01-06 11:24:44.837270905 +0100
> @@ -4285,7 +4285,8 @@ maybe_init_list_as_array (tree elttype,
> static tree
> maybe_init_list_as_range (tree fn, tree expr)
> {
> - if (BRACE_ENCLOSED_INITIALIZER_P (expr)
> + if (!processing_template_decl
> + && BRACE_ENCLOSED_INITIALIZER_P (expr)
> && is_list_ctor (fn)
> && decl_in_std_namespace_p (fn))
> {
> --- gcc/testsuite/g++.dg/tree-ssa/initlist-opt2.C.jj 2023-01-06 11:53:13.160432870 +0100
> +++ gcc/testsuite/g++.dg/tree-ssa/initlist-opt2.C 2023-01-06 11:53:44.561976302 +0100
> @@ -0,0 +1,31 @@
> +// PR c++/105838
> +// { dg-additional-options -fdump-tree-gimple }
> +// { dg-do compile { target c++11 } }
> +
> +// Test that we do range-initialization from const char *.
> +// { dg-final { scan-tree-dump {_M_range_initialize<const char\* const\*>} "gimple" } }
> +
> +#include <string>
> +#include <vector>
> +
> +void g (const void *);
> +
> +template <int N>
> +void f (const char *p)
> +{
> + std::vector<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);
> +}
> +
> +void h (const char *p)
> +{
> + f<0> (p);
> +}
> --- gcc/testsuite/g++.dg/tree-ssa/initlist-opt3.C.jj 2023-01-06 11:56:36.981469370 +0100
> +++ gcc/testsuite/g++.dg/tree-ssa/initlist-opt3.C 2023-01-06 11:56:09.984861898 +0100
> @@ -0,0 +1,21 @@
> +// PR c++/108266
> +// { dg-do compile { target c++11 } }
> +
> +#include <initializer_list>
> +#include <vector>
> +
> +struct S { S (const char *); };
> +void bar (std::vector<S>);
> +
> +template <int N>
> +void
> +foo ()
> +{
> + bar ({"", ""});
> +}
> +
> +void
> +baz ()
> +{
> + foo<0> ();
> +}
>
> Jakub
>
@@ -4285,7 +4285,8 @@ maybe_init_list_as_array (tree elttype,
static tree
maybe_init_list_as_range (tree fn, tree expr)
{
- if (BRACE_ENCLOSED_INITIALIZER_P (expr)
+ if (!processing_template_decl
+ && BRACE_ENCLOSED_INITIALIZER_P (expr)
&& is_list_ctor (fn)
&& decl_in_std_namespace_p (fn))
{
@@ -0,0 +1,31 @@
+// PR c++/105838
+// { dg-additional-options -fdump-tree-gimple }
+// { dg-do compile { target c++11 } }
+
+// Test that we do range-initialization from const char *.
+// { dg-final { scan-tree-dump {_M_range_initialize<const char\* const\*>} "gimple" } }
+
+#include <string>
+#include <vector>
+
+void g (const void *);
+
+template <int N>
+void f (const char *p)
+{
+ std::vector<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);
+}
+
+void h (const char *p)
+{
+ f<0> (p);
+}
@@ -0,0 +1,21 @@
+// PR c++/108266
+// { dg-do compile { target c++11 } }
+
+#include <initializer_list>
+#include <vector>
+
+struct S { S (const char *); };
+void bar (std::vector<S>);
+
+template <int N>
+void
+foo ()
+{
+ bar ({"", ""});
+}
+
+void
+baz ()
+{
+ foo<0> ();
+}