diff options
author | Jason Merrill <jason@redhat.com> | 2022-12-07 11:40:53 -0500 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2023-06-02 10:54:39 -0400 |
commit | 4d935f52b0d5c00fcc154461b87415ebd8791a94 (patch) | |
tree | b796b6682e064ac3c397b0769f549b9b52c7c976 /gcc/testsuite/g++.dg | |
parent | 99566c0c6b8802ba1d0baeebba3581563cf8a09f (diff) | |
download | gcc-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/testsuite/g++.dg')
-rw-r--r-- | gcc/testsuite/g++.dg/opt/icf1.C | 16 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/opt/icf2.C | 17 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/opt/icf3.C | 17 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/tree-ssa/array-temp1.C | 6 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/tree-ssa/initlist-opt1.C | 1 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/tree-ssa/initlist-opt2.C | 2 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/tree-ssa/initlist-opt4.C | 13 |
7 files changed, 72 insertions, 0 deletions
diff --git a/gcc/testsuite/g++.dg/opt/icf1.C b/gcc/testsuite/g++.dg/opt/icf1.C new file mode 100644 index 0000000..fbb275e --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/icf1.C @@ -0,0 +1,16 @@ +// Test that -fipa-icf combines i and j. +// { dg-do run { target c++11 } } +// { dg-options -fipa-icf } + +[[no_unique_address]] extern const int i[] = { 1,2,3 }; +[[no_unique_address]] extern const int j[] = { 1,2,3 }; + +[[gnu::noipa]] void f (const void *a, const void *b) +{ + if (a != b) __builtin_abort(); +} + +int main() +{ + f (&i, &j); +} diff --git a/gcc/testsuite/g++.dg/opt/icf2.C b/gcc/testsuite/g++.dg/opt/icf2.C new file mode 100644 index 0000000..1ad48f6 --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/icf2.C @@ -0,0 +1,17 @@ +// Test that -fipa-icf combines the backing arrays for a and b. +// { dg-do run { target c++11 } } +// { dg-options -fipa-icf } + +#include <initializer_list> + +[[gnu::noipa]] void f (const void *a, const void *b) +{ + if (a != b) __builtin_abort(); +} + +int main() +{ + auto a = { 1, 2 }; + auto b = { 1, 2 }; + f (a.begin(), b.begin()); +} diff --git a/gcc/testsuite/g++.dg/opt/icf3.C b/gcc/testsuite/g++.dg/opt/icf3.C new file mode 100644 index 0000000..8b86982 --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/icf3.C @@ -0,0 +1,17 @@ +// Test that -fipa-icf combines the backing arrays for a and b. +// { dg-do run { target c++11 } } +// { dg-options -fipa-icf } + +#include <initializer_list> + +template <class T> +[[gnu::noipa]] void f (std::initializer_list<T> a, + std::initializer_list<T> b) +{ + if (a.begin() != b.begin()) __builtin_abort(); +} + +int main() +{ + f ({1,2}, {1,2}); +} diff --git a/gcc/testsuite/g++.dg/tree-ssa/array-temp1.C b/gcc/testsuite/g++.dg/tree-ssa/array-temp1.C index 3df7aad..97c2e05 100644 --- a/gcc/testsuite/g++.dg/tree-ssa/array-temp1.C +++ b/gcc/testsuite/g++.dg/tree-ssa/array-temp1.C @@ -13,3 +13,9 @@ int f() using AR = const int[]; return AR{ 1,42,3,4,5,6,7,8,9,0 }[5]; } + +int g() +{ + std::initializer_list<int> a = {1,42,3}; + return a.begin()[0]; +} diff --git a/gcc/testsuite/g++.dg/tree-ssa/initlist-opt1.C b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt1.C index 053317b..b1d2d25 100644 --- a/gcc/testsuite/g++.dg/tree-ssa/initlist-opt1.C +++ b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt1.C @@ -4,6 +4,7 @@ // Test that we do range-initialization from const char *. // { dg-final { scan-tree-dump {_M_range_initialize<const char\* const\*>} "gimple" } } +// { dg-final { scan-tree-dump {static const char.*72} "gimple" } } #include <string> #include <vector> diff --git a/gcc/testsuite/g++.dg/tree-ssa/initlist-opt2.C b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt2.C index c20713a..1e9ac73 100644 --- a/gcc/testsuite/g++.dg/tree-ssa/initlist-opt2.C +++ b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt2.C @@ -4,6 +4,8 @@ // Test that we do range-initialization from const char *. // { dg-final { scan-tree-dump {_M_range_initialize<const char\* const\*>} "gimple" } } +// And that the backing array is static. +// { dg-final { scan-tree-dump {static const char.*72} "gimple" } } #include <string> #include <vector> diff --git a/gcc/testsuite/g++.dg/tree-ssa/initlist-opt4.C b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt4.C new file mode 100644 index 0000000..16e2e53 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt4.C @@ -0,0 +1,13 @@ +// PR c++/110070 +// { dg-additional-options -fdump-tree-gimple } +// { dg-do compile { target c++11 } } + +// { dg-final { scan-tree-dump {static const int [^\n]*\[4\] = } "gimple" } } + +#include <initializer_list> +extern void ext(int); +void foo() +{ + for (int i: {1,2,4,6}) + ext(i); +} |