diff options
author | Jason Merrill <jason@redhat.com> | 2022-10-05 11:50:59 -0400 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2022-10-07 09:22:32 -0400 |
commit | d3e5465757c599ea64f611290b7793d3141a6b7c (patch) | |
tree | 42223c603e13701c6d5569b2960677af795ca609 /gcc/testsuite | |
parent | 89228e3985c5cdf6be58a3b5b1afcad91e9e3422 (diff) | |
download | gcc-d3e5465757c599ea64f611290b7793d3141a6b7c.zip gcc-d3e5465757c599ea64f611290b7793d3141a6b7c.tar.gz gcc-d3e5465757c599ea64f611290b7793d3141a6b7c.tar.bz2 |
gimplify: prevent some C++ temporary elision
In this testcase, we were optimizing away the temporary for f(), but
C++17 and above are clear that there is a temporary, and because its
destructor has visible side-effects we can't optimize it away under the
as-if rule. So disable this optimization for TREE_ADDRESSABLE type.
I moved the declaration of volatile_p after the call to
gimple_fold_indirect_ref_rhs to minimize indentation changes; I don't see
any way the value of that flag could be affected by the call.
gcc/ChangeLog:
* gimplify.cc (gimplify_modify_expr_rhs): Don't optimize
x = *(A*)&<expr> to x = <expr> for a TREE_ADDRESSABLE type.
gcc/testsuite/ChangeLog:
* g++.dg/init/elide9.C: New test.
Diffstat (limited to 'gcc/testsuite')
-rw-r--r-- | gcc/testsuite/g++.dg/init/elide9.C | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/gcc/testsuite/g++.dg/init/elide9.C b/gcc/testsuite/g++.dg/init/elide9.C new file mode 100644 index 0000000..810d60a --- /dev/null +++ b/gcc/testsuite/g++.dg/init/elide9.C @@ -0,0 +1,25 @@ +// The static_cast should prevent temporary elision. +// { dg-do run { target c++11 } } + +int d; +struct A +{ + int i; + A() { } + ~A() { ++d; } +}; + +A f() { return A(); } + +struct B +{ + A a; + B(): a(static_cast<A&&>(f())) {} +}; + +int main() +{ + { B b; } + if (d != 2) + return -1; +} |