aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2022-12-07 11:40:53 -0500
committerJason Merrill <jason@redhat.com>2023-06-02 10:54:39 -0400
commit4d935f52b0d5c00fcc154461b87415ebd8791a94 (patch)
treeb796b6682e064ac3c397b0769f549b9b52c7c976 /gcc/cp
parent99566c0c6b8802ba1d0baeebba3581563cf8a09f (diff)
downloadgcc-4d935f52b0d5c00fcc154461b87415ebd8791a94.zip
gcc-4d935f52b0d5c00fcc154461b87415ebd8791a94.tar.gz
gcc-4d935f52b0d5c00fcc154461b87415ebd8791a94.tar.bz2
c++: make initializer_list array static again [PR110070]
After the maybe_init_list_as_* patches, I noticed that we were putting the array of strings into .rodata, but then memcpying it into an automatic array, which is pointless; we should be able to use it directly. This doesn't happen automatically because TREE_ADDRESSABLE is set (since r12-657 for PR100464), and so gimplify_init_constructor won't promote the variable to static. Theoretically we could do escape analysis to recognize that the address, though taken, never leaves the function; that would allow promotion when we're only using the address for indexing within the function, as in initlist-opt2.C. But this would be a new pass. And in initlist-opt1.C, we're passing the array address to another function, so it definitely escapes; it's only safe in this case because it's calling a standard library function that we know only uses it for indexing. So, a flag seems needed. I first thought to put the flag on the TARGET_EXPR, but the VAR_DECL seems more appropriate. In a previous revision of the patch I called this flag DECL_NOT_OBSERVABLE, but I think DECL_MERGEABLE is a better name, especially if we're going to apply it to the backing array of initializer_list, which is observable. I then also check it in places that check for -fmerge-all-constants, so that multiple equivalent initializer-lists can also be combined. And then it seemed to make sense for [[no_unique_address]] to have this meaning for user-written variables. I think the note in [dcl.init.list]/6 intended to allow this kind of merging for initializer_lists, but it didn't actually work; for an explicit array with the same initializer, if the address escapes the program could tell whether the same variable in two frames have the same address. P2752 is trying to correct this defect, so I'm going to assume that this is the intent. PR c++/110070 PR c++/105838 gcc/ChangeLog: * tree.h (DECL_MERGEABLE): New. * tree-core.h (struct tree_decl_common): Mention it. * gimplify.cc (gimplify_init_constructor): Check it. * cgraph.cc (symtab_node::address_can_be_compared_p): Likewise. * varasm.cc (categorize_decl_for_section): Likewise. gcc/cp/ChangeLog: * call.cc (maybe_init_list_as_array): Set DECL_MERGEABLE. (convert_like_internal) [ck_list]: Set it. (set_up_extended_ref_temp): Copy it. * tree.cc (handle_no_unique_addr_attribute): Set it. gcc/testsuite/ChangeLog: * g++.dg/tree-ssa/initlist-opt1.C: Check for static array. * g++.dg/tree-ssa/initlist-opt2.C: Likewise. * g++.dg/tree-ssa/initlist-opt4.C: New test. * g++.dg/opt/icf1.C: New test. * g++.dg/opt/icf2.C: New test. * g++.dg/opt/icf3.C: New test. * g++.dg/tree-ssa/array-temp1.C: Revert r12-657 change.
Diffstat (limited to 'gcc/cp')
-rw-r--r--gcc/cp/call.cc15
-rw-r--r--gcc/cp/tree.cc9
2 files changed, 20 insertions, 4 deletions
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 2736f55..5d504e5 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -4274,7 +4274,9 @@ maybe_init_list_as_array (tree elttype, tree init)
init_elttype = cp_build_qualified_type (init_elttype, TYPE_QUAL_CONST);
tree arr = build_array_of_n_type (init_elttype, CONSTRUCTOR_NELTS (init));
- return finish_compound_literal (arr, init, tf_none);
+ arr = finish_compound_literal (arr, init, tf_none);
+ DECL_MERGEABLE (TARGET_EXPR_SLOT (arr)) = true;
+ return arr;
}
/* If we were going to call e.g. vector(initializer_list<string>) starting
@@ -8558,6 +8560,8 @@ convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
(elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
array = build_array_of_n_type (elttype, len);
array = finish_compound_literal (array, new_ctor, complain);
+ /* This is dubious now, should be blessed by P2752. */
+ DECL_MERGEABLE (TARGET_EXPR_SLOT (array)) = true;
/* Take the address explicitly rather than via decay_conversion
to avoid the error about taking the address of a temporary. */
array = cp_build_addr_expr (array, complain);
@@ -13571,8 +13575,13 @@ set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
VAR. */
if (TREE_CODE (expr) != TARGET_EXPR)
expr = get_target_expr (expr);
- else if (TREE_ADDRESSABLE (expr))
- TREE_ADDRESSABLE (var) = 1;
+ else
+ {
+ if (TREE_ADDRESSABLE (expr))
+ TREE_ADDRESSABLE (var) = 1;
+ if (DECL_MERGEABLE (TARGET_EXPR_SLOT (expr)))
+ DECL_MERGEABLE (var) = true;
+ }
if (TREE_CODE (decl) == FIELD_DECL
&& extra_warnings && !warning_suppressed_p (decl))
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index 19dfb3e..4d5e3f5 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -5045,7 +5045,14 @@ handle_no_unique_addr_attribute (tree* node,
int /*flags*/,
bool* no_add_attrs)
{
- if (TREE_CODE (*node) != FIELD_DECL)
+ if (TREE_CODE (*node) == VAR_DECL)
+ {
+ DECL_MERGEABLE (*node) = true;
+ if (pedantic)
+ warning (OPT_Wattributes, "%qE attribute can only be applied to "
+ "non-static data members", name);
+ }
+ else if (TREE_CODE (*node) != FIELD_DECL)
{
warning (OPT_Wattributes, "%qE attribute can only be applied to "
"non-static data members", name);