Skip to content

Commit befd05e

Browse files
committed
Optimize lhs = structliteral.ctor(args)
Fixes runnable/sdtor:2842 which tests this for return expressions (in a function using sret).
1 parent 980c562 commit befd05e

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

gen/toir.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,16 +2723,34 @@ bool toInPlaceConstruction(DLValue *lhs, Expression *rhs) {
27232723
rhs = castSource;
27242724
}
27252725

2726-
// Direct construction by rhs call via sret?
2727-
// E.g., `T v = foo();` if the callee `T foo()` uses sret.
2728-
// In this case, pass `&v` as hidden sret argument, i.e., let `foo()`
2729-
// construct the return value directly into the lhs lvalue.
27302726
if (rhs->op == TOKcall) {
27312727
auto ce = static_cast<CallExp *>(rhs);
2728+
2729+
// Direct construction by rhs call via sret?
2730+
// E.g., `T v = foo();` if the callee `T foo()` uses sret.
2731+
// In this case, pass `&v` as hidden sret argument, i.e., let `foo()`
2732+
// construct the return value directly into the lhs lvalue.
27322733
if (DtoIsReturnInArg(ce)) {
27332734
ToElemVisitor::call(gIR, ce, DtoLVal(lhs));
27342735
return true;
27352736
}
2737+
2738+
// DMD issue 17457: detect structliteral.ctor(args)
2739+
if (ce->e1->op == TOKdotvar) {
2740+
auto dve = static_cast<DotVarExp *>(ce->e1);
2741+
auto fd = dve->var->isFuncDeclaration();
2742+
if (fd && fd->isCtorDeclaration() && dve->e1->op == TOKstructliteral) {
2743+
// emit the struct literal directly into the lhs lvalue...
2744+
auto sle = static_cast<StructLiteralExp *>(dve->e1);
2745+
auto lval = DtoLVal(lhs);
2746+
ToElemVisitor::emitStructLiteral(sle, lval);
2747+
// ... and invoke the ctor directly on it
2748+
DtoDeclareFunction(fd);
2749+
auto fnval = new DFuncValue(fd, DtoCallee(fd), lval);
2750+
DtoCallFunction(ce->loc, ce->type, fnval, ce->arguments);
2751+
return true;
2752+
}
2753+
}
27362754
}
27372755

27382756
// emit struct literals directly into the lhs lvalue

0 commit comments

Comments
 (0)