aboutsummaryrefslogtreecommitdiff
path: root/gcc/d/decl.cc
diff options
context:
space:
mode:
authorIain Buclaw <ibuclaw@gdcproject.org>2020-07-24 18:06:51 +0200
committerIain Buclaw <ibuclaw@gdcproject.org>2020-08-26 10:03:56 +0200
commit87e36d9baf41a8642ca8687e846764e0828a088b (patch)
tree4f47a339a73c3f4f6adeba766237380e15558dc6 /gcc/d/decl.cc
parent27e5d7c77218c0b5dd1a421a55234573e687e927 (diff)
downloadgcc-87e36d9baf41a8642ca8687e846764e0828a088b.zip
gcc-87e36d9baf41a8642ca8687e846764e0828a088b.tar.gz
gcc-87e36d9baf41a8642ca8687e846764e0828a088b.tar.bz2
d: Fix no RVO when returning struct literals initialized with constructor.
Backports a change from upstream dmd that moves front-end NRVO checking from ReturnStatement semantic to the end of FuncDeclaration semantic. In the codegen, retStyle has been partially implemented so that only structs and static arrays return RETstack. This isn't accurate, but don't need to be for the purposes of semantic analysis. If a function either has TREE_ADDRESSABLE or must return in memory, then DECL_RESULT is set as the shidden field for the function. This is used in the codegen pass for ReturnStatement where it is now detected whether a function is returning a struct literal or a constructor function, then the DECL_RESULT is used to directly construct the return value, instead of doing so via temporaries. Reviewed-on: https://github.com/dlang/dmd/pull/11622 gcc/d/ChangeLog: PR d/96156 * d-frontend.cc (retStyle): Only return RETstack for struct and static array types. * decl.cc (DeclVisitor::visit (FuncDeclaration *)): Use NRVO return for all TREE_ADDRESSABLE types. Set shidden to the RESULT_DECL. * expr.cc (ExprVisitor::visit (CallExp *)): Force TARGET_EXPR if the 'this' pointer reference is a CONSTRUCTOR. (ExprVisitor::visit (StructLiteralExp *)): Generate assignment to the symbol to initialize with literal. * toir.cc (IRVisitor::visit (ReturnStatement *)): Detect returning struct literals and write directly into the RESULT_DECL. * dmd/MERGE: Merge upstream dmd fe5f388d8. gcc/testsuite/ChangeLog: PR d/96156 * gdc.dg/pr96156.d: New test.
Diffstat (limited to 'gcc/d/decl.cc')
-rw-r--r--gcc/d/decl.cc25
1 files changed, 8 insertions, 17 deletions
diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc
index 295f780..008a2bb 100644
--- a/gcc/d/decl.cc
+++ b/gcc/d/decl.cc
@@ -944,20 +944,11 @@ public:
Implemented by overriding all the RETURN_EXPRs and replacing all
occurrences of VAR with the RESULT_DECL for the function.
This is only worth doing for functions that can return in memory. */
- if (d->nrvo_can)
- {
- tree restype = TREE_TYPE (DECL_RESULT (fndecl));
-
- if (!AGGREGATE_TYPE_P (restype))
- d->nrvo_can = 0;
- else
- d->nrvo_can = aggregate_value_p (restype, fndecl);
- }
+ tree resdecl = DECL_RESULT (fndecl);
- if (d->nrvo_can)
+ if (TREE_ADDRESSABLE (TREE_TYPE (resdecl))
+ || aggregate_value_p (TREE_TYPE (resdecl), fndecl))
{
- tree resdecl = DECL_RESULT (fndecl);
-
/* Return non-trivial structs by invisible reference. */
if (TREE_ADDRESSABLE (TREE_TYPE (resdecl)))
{
@@ -965,9 +956,12 @@ public:
DECL_BY_REFERENCE (resdecl) = 1;
TREE_ADDRESSABLE (resdecl) = 0;
relayout_decl (resdecl);
+ d->shidden = build_deref (resdecl);
}
+ else
+ d->shidden = resdecl;
- if (d->nrvo_var)
+ if (d->nrvo_can && d->nrvo_var)
{
tree var = get_symbol_decl (d->nrvo_var);
@@ -976,12 +970,9 @@ public:
/* Don't forget that we take its address. */
TREE_ADDRESSABLE (var) = 1;
- if (DECL_BY_REFERENCE (resdecl))
- resdecl = build_deref (resdecl);
-
SET_DECL_VALUE_EXPR (var, resdecl);
DECL_HAS_VALUE_EXPR_P (var) = 1;
- SET_DECL_LANG_NRVO (var, resdecl);
+ SET_DECL_LANG_NRVO (var, d->shidden);
}
}