diff options
author | Jason Merrill <jason@redhat.com> | 2022-09-19 19:08:10 +0200 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2022-09-29 13:48:20 -0400 |
commit | 32b2eb59fb904926eff77811e08766a8ae6804a7 (patch) | |
tree | b5d170cf847014cde68feb6c197515e3d9fdec99 /gcc | |
parent | af9034827e8f06f10767064e9fc7443b94e08184 (diff) | |
download | gcc-32b2eb59fb904926eff77811e08766a8ae6804a7.zip gcc-32b2eb59fb904926eff77811e08766a8ae6804a7.tar.gz gcc-32b2eb59fb904926eff77811e08766a8ae6804a7.tar.bz2 |
c++: reduce temporaries in ?:
When the sides of ?: are class prvalues, we wrap the COND_EXPR in a
TARGET_EXPR so that both sides will initialize the same temporary. But in
this case we were stripping the outer TARGET_EXPR and conditionally creating
different temporaries, unnecessarily using extra stack. The
recently added TARGET_EXPR_NO_ELIDE flag avoids this.
gcc/cp/ChangeLog:
* call.cc (build_conditional_expr): Set TARGET_EXPR_NO_ELIDE on the
outer TARGET_EXPR.
gcc/testsuite/ChangeLog:
* g++.dg/tree-ssa/cond-temp1.C: New test.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/call.cc | 8 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C | 16 |
2 files changed, 23 insertions, 1 deletions
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 3506b0f..9fad3cb 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -6009,7 +6009,13 @@ build_conditional_expr (const op_location_t &loc, but now we sometimes wrap them in NOP_EXPRs so the test would fail. */ if (CLASS_TYPE_P (TREE_TYPE (result))) - result = get_target_expr (result, complain); + { + result = get_target_expr (result, complain); + /* Tell gimplify_modify_expr_rhs not to strip this in + assignment context: we want both arms to initialize + the same temporary. */ + TARGET_EXPR_NO_ELIDE (result) = true; + } /* If this expression is an rvalue, but might be mistaken for an lvalue, we must add a NON_LVALUE_EXPR. */ result = rvalue (result); diff --git a/gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C b/gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C new file mode 100644 index 0000000..b156358 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C @@ -0,0 +1,16 @@ +// Test that the ?: only creates one temporary. +// { dg-additional-options "-fdump-tree-gimple" } +// { dg-final { scan-tree-dump-times "struct A" 2 "gimple" } } + +struct A +{ + int i; + A(int); +}; + +bool b; +int main() +{ + A a = 1; + a = b ? A(2) : A(3); +} |