Skip to content

Commit 0ddab71

Browse files
committed
[gardening] Reduce indentation of SILGenLValue::visitRec by inverting an if condition.
The issue here is that the way the code was structured caused a large conditional block at the beginning of the function that always exited early and then a short unconditional block. This commit inverts the if statement so that the short unconditional block is now conditional and the large conditional block is unconditional, reducing the overall indentation for the majority of the function.
1 parent 970f020 commit 0ddab71

File tree

1 file changed

+66
-63
lines changed

1 file changed

+66
-63
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 66 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,75 +1939,78 @@ LValue SILGenFunction::emitLValue(Expr *e, AccessKind accessKind) {
19391939

19401940
LValue SILGenLValue::visitRec(Expr *e, AccessKind accessKind,
19411941
AbstractionPattern orig) {
1942-
// Non-lvalue types (references, values, metatypes, etc) form the root of a
1943-
// logical l-value.
1944-
if (!e->getType()->is<LValueType>() && !e->getType()->is<InOutType>()) {
1945-
// Decide if we can evaluate this expression at +0 for the rest of the
1946-
// lvalue.
1947-
SGFContext Ctx;
1948-
ManagedValue rv;
1949-
1950-
// Calls through opaque protocols can be done with +0 rvalues. This allows
1951-
// us to avoid materializing copies of existentials.
1952-
if (SGF.SGM.Types.isIndirectPlusZeroSelfParameter(e->getType()))
1953-
Ctx = SGFContext::AllowGuaranteedPlusZero;
1954-
else if (auto *DRE = dyn_cast<DeclRefExpr>(e)) {
1955-
// Any reference to "self" can be done at +0 so long as it is a direct
1956-
// access, since we know it is guaranteed.
1957-
// TODO: it would be great to factor this even lower into SILGen to the
1958-
// point where we can see that the parameter is +0 guaranteed. Note that
1959-
// this handles the case in initializers where there is actually a stack
1960-
// allocation for it as well.
1961-
if (isa<ParamDecl>(DRE->getDecl()) &&
1962-
DRE->getDecl()->getFullName() == SGF.getASTContext().Id_self &&
1963-
DRE->getDecl()->isImplicit()) {
1964-
Ctx = SGFContext::AllowGuaranteedPlusZero;
1965-
if (SGF.SelfInitDelegationState != SILGenFunction::NormalSelf) {
1966-
// This needs to be inlined since there is a Formal Evaluation Scope
1967-
// in emitRValueForDecl that causing any borrow for this LValue to be
1968-
// popped too soon.
1969-
auto *vd = cast<ParamDecl>(DRE->getDecl());
1970-
ManagedValue selfLValue = SGF.emitLValueForDecl(
1971-
DRE, vd, DRE->getType()->getCanonicalType(), AccessKind::Read,
1972-
DRE->getAccessSemantics());
1973-
rv = SGF.emitRValueForSelfInDelegationInit(
1974-
e, DRE->getType()->getCanonicalType(),
1975-
selfLValue.getLValueAddress(), Ctx)
1976-
.getScalarValue();
1977-
}
1978-
} else if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
1979-
// All let values are guaranteed to be held alive across their lifetime,
1980-
// and won't change once initialized. Any loaded value is good for the
1981-
// duration of this expression evaluation.
1982-
if (VD->isLet())
1983-
Ctx = SGFContext::AllowGuaranteedPlusZero;
1984-
}
1942+
// First see if we have an lvalue type. If we do, then quickly handle that and
1943+
// return.
1944+
if (e->getType()->is<LValueType>() || e->getType()->is<InOutType>()) {
1945+
auto lv = visit(e, accessKind);
1946+
// If necessary, handle reabstraction with a SubstToOrigComponent that
1947+
// handles
1948+
// writeback in the original representation.
1949+
if (orig.isValid()) {
1950+
auto &origTL = SGF.getTypeLowering(orig, e->getType()->getRValueType());
1951+
if (lv.getTypeOfRValue() != origTL.getLoweredType().getObjectType())
1952+
lv.addSubstToOrigComponent(orig,
1953+
origTL.getLoweredType().getObjectType());
19851954
}
1955+
return lv;
1956+
}
19861957

1987-
if (!rv) {
1988-
// For an rvalue base, apply the reabstraction (if any) eagerly, since
1989-
// there's no need for writeback.
1990-
if (orig.isValid())
1991-
rv = SGF.emitRValueAsOrig(e, orig,
1992-
SGF.getTypeLowering(orig, e->getType()->getRValueType()));
1993-
else
1994-
rv = SGF.emitRValueAsSingleValue(e, Ctx);
1958+
// Otherwise we have a non-lvalue type (references, values, metatypes,
1959+
// etc). These act as the root of a logical lvalue.
1960+
SGFContext Ctx;
1961+
ManagedValue rv;
1962+
1963+
// Calls through opaque protocols can be done with +0 rvalues. This allows
1964+
// us to avoid materializing copies of existentials.
1965+
if (SGF.SGM.Types.isIndirectPlusZeroSelfParameter(e->getType()))
1966+
Ctx = SGFContext::AllowGuaranteedPlusZero;
1967+
else if (auto *DRE = dyn_cast<DeclRefExpr>(e)) {
1968+
// Any reference to "self" can be done at +0 so long as it is a direct
1969+
// access, since we know it is guaranteed.
1970+
// TODO: it would be great to factor this even lower into SILGen to the
1971+
// point where we can see that the parameter is +0 guaranteed. Note that
1972+
// this handles the case in initializers where there is actually a stack
1973+
// allocation for it as well.
1974+
if (isa<ParamDecl>(DRE->getDecl()) &&
1975+
DRE->getDecl()->getFullName() == SGF.getASTContext().Id_self &&
1976+
DRE->getDecl()->isImplicit()) {
1977+
Ctx = SGFContext::AllowGuaranteedPlusZero;
1978+
if (SGF.SelfInitDelegationState != SILGenFunction::NormalSelf) {
1979+
// This needs to be inlined since there is a Formal Evaluation Scope
1980+
// in emitRValueForDecl that causing any borrow for this LValue to be
1981+
// popped too soon.
1982+
auto *vd = cast<ParamDecl>(DRE->getDecl());
1983+
ManagedValue selfLValue =
1984+
SGF.emitLValueForDecl(DRE, vd, DRE->getType()->getCanonicalType(),
1985+
AccessKind::Read, DRE->getAccessSemantics());
1986+
rv = SGF.emitRValueForSelfInDelegationInit(
1987+
e, DRE->getType()->getCanonicalType(),
1988+
selfLValue.getLValueAddress(), Ctx)
1989+
.getScalarValue();
1990+
}
1991+
} else if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
1992+
// All let values are guaranteed to be held alive across their lifetime,
1993+
// and won't change once initialized. Any loaded value is good for the
1994+
// duration of this expression evaluation.
1995+
if (VD->isLet())
1996+
Ctx = SGFContext::AllowGuaranteedPlusZero;
19951997
}
1996-
CanType formalType = getSubstFormalRValueType(e);
1997-
auto typeData = getValueTypeData(formalType, rv.getValue());
1998-
LValue lv;
1999-
lv.add<ValueComponent>(rv, None, typeData, /*isRValue=*/true);
2000-
return lv;
20011998
}
20021999

2003-
auto lv = visit(e, accessKind);
2004-
// If necessary, handle reabstraction with a SubstToOrigComponent that handles
2005-
// writeback in the original representation.
2006-
if (orig.isValid()) {
2007-
auto &origTL = SGF.getTypeLowering(orig, e->getType()->getRValueType());
2008-
if (lv.getTypeOfRValue() != origTL.getLoweredType().getObjectType())
2009-
lv.addSubstToOrigComponent(orig, origTL.getLoweredType().getObjectType());
2000+
if (!rv) {
2001+
// For an rvalue base, apply the reabstraction (if any) eagerly, since
2002+
// there's no need for writeback.
2003+
if (orig.isValid())
2004+
rv = SGF.emitRValueAsOrig(
2005+
e, orig, SGF.getTypeLowering(orig, e->getType()->getRValueType()));
2006+
else
2007+
rv = SGF.emitRValueAsSingleValue(e, Ctx);
20102008
}
2009+
2010+
CanType formalType = getSubstFormalRValueType(e);
2011+
auto typeData = getValueTypeData(formalType, rv.getValue());
2012+
LValue lv;
2013+
lv.add<ValueComponent>(rv, None, typeData, /*isRValue=*/true);
20112014
return lv;
20122015
}
20132016

0 commit comments

Comments
 (0)