diff options
author | Iain Buclaw <ibuclaw@gdcproject.org> | 2020-07-24 18:06:51 +0200 |
---|---|---|
committer | Iain Buclaw <ibuclaw@gdcproject.org> | 2020-08-26 10:03:56 +0200 |
commit | 87e36d9baf41a8642ca8687e846764e0828a088b (patch) | |
tree | 4f47a339a73c3f4f6adeba766237380e15558dc6 /gcc/d/d-frontend.cc | |
parent | 27e5d7c77218c0b5dd1a421a55234573e687e927 (diff) | |
download | gcc-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/d-frontend.cc')
-rw-r--r-- | gcc/d/d-frontend.cc | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/gcc/d/d-frontend.cc b/gcc/d/d-frontend.cc index 3b9fc1a..da34e90 100644 --- a/gcc/d/d-frontend.cc +++ b/gcc/d/d-frontend.cc @@ -143,12 +143,20 @@ Loc::equals (const Loc &loc) hidden pointer to the caller's stack. */ RET -retStyle (TypeFunction *) +retStyle (TypeFunction *tf) { /* Need the backend type to determine this, but this is called from the frontend before semantic processing is finished. An accurate value is not currently needed anyway. */ - return RETstack; + if (tf->isref) + return RETregs; + + Type *tn = tf->next->toBasetype (); + + if (tn->ty == Tstruct || tn->ty == Tsarray) + return RETstack; + + return RETregs; } /* Determine if function FD is a builtin one that we can evaluate in CTFE. */ |