Skip to content

Commit

Permalink
Try a more aggressive variant in toInPlaceConstruction()
Browse files Browse the repository at this point in the history
  • Loading branch information
kinke committed Aug 19, 2019
1 parent 54e9cd6 commit 782f7d8
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions gen/toir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2716,6 +2716,26 @@ bool basetypesAreEqualWithoutModifiers(Type *l, Type *r) {
}

bool toInPlaceConstruction(DLValue *lhs, Expression *rhs) {
// Is the rhs the init symbol of a zero-initialized struct?
// Then aggressively zero-out the lhs, without any type checks, e.g., allowing
// to initialize a `S[1]` lhs with a `S` rhs.
if (auto ve = rhs->isVarExp()) {
if (auto symdecl = ve->var->isSymbolDeclaration()) {
Type *t = symdecl->type->toBasetype();
if (auto ts = t->isTypeStruct()) {
// this is the static initializer for a struct (init symbol)
StructDeclaration *sd = ts->sym;
assert(sd);
DtoResolveStruct(sd);

if (sd->zeroInit) {
DtoMemSetZero(DtoLVal(lhs));
return true;
}
}
}
}

if (!basetypesAreEqualWithoutModifiers(lhs->type, rhs->type))
return false;

Expand Down Expand Up @@ -2763,31 +2783,6 @@ bool toInPlaceConstruction(DLValue *lhs, Expression *rhs) {
return true;
}

if (rhs->op == TOKvar) {
auto ve = static_cast<VarExp *>(rhs);
if (auto symdecl = ve->var->isSymbolDeclaration()) {
Type *t = symdecl->type->toBasetype();
if (auto ts = t->isTypeStruct()) {
// this is the static initializer for a struct (init symbol)
StructDeclaration *sd = ts->sym;
assert(sd);
DtoResolveStruct(sd);

LLValue *lhsLVal = DtoLVal(lhs);
if (sd->zeroInit) {
DtoMemSetZero(lhsLVal);
} else {
LLValue *initsym = getIrAggr(sd)->getInitSymbol();
initsym = DtoBitCast(initsym, DtoType(ve->type->pointerTo()));
assert(lhsLVal->getType() == initsym->getType());
DtoMemCpy(lhsLVal, initsym);
}

return true;
}
}
}

// static array literals too
Type *lhsBasetype = lhs->type->toBasetype();
if (rhs->op == TOKarrayliteral && lhsBasetype->ty == Tsarray) {
Expand Down

0 comments on commit 782f7d8

Please sign in to comment.