Skip to content

Commit cd2fbfc

Browse files
committed
[silgen] Use early exits and invert an if condition to reduce indentation.
1 parent 17e64f5 commit cd2fbfc

File tree

1 file changed

+50
-48
lines changed

1 file changed

+50
-48
lines changed

lib/SILGen/SILGenPattern.cpp

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,62 +2654,64 @@ void SILGenFunction::emitSwitchStmt(SwitchStmt *S) {
26542654
// statement.
26552655
JumpDest sharedDest = emission.getSharedCaseBlockDest(caseBlock);
26562656
Cleanups.emitBranchAndCleanups(sharedDest, caseBlock);
2657-
} else if (row.hasFallthroughTo() || caseBlock->getCaseLabelItems().size() > 1) {
2658-
JumpDest sharedDest =
2659-
emission.getSharedCaseBlockDest(caseBlock);
2660-
2661-
// Generate the arguments from this row's pattern in the case block's
2662-
// expected order, and keep those arguments from being cleaned up, as
2663-
// we're passing the +1 along to the shared case block dest. (The
2664-
// cleanups still happen, as they are threaded through here messily,
2665-
// but the explicit retains here counteract them, and then the
2666-
// retain/release pair gets optimized out.)
2667-
ArrayRef<CaseLabelItem> labelItems = caseBlock->getCaseLabelItems();
2668-
SmallVector<SILValue, 4> args;
2669-
SmallVector<VarDecl *, 4> expectedVarOrder;
2670-
SmallVector<VarDecl *, 4> vars;
2671-
labelItems[0].getPattern()->collectVariables(expectedVarOrder);
2672-
row.getCasePattern()->collectVariables(vars);
2673-
2674-
SILModule &M = F.getModule();
2675-
for (auto expected : expectedVarOrder) {
2676-
if (!expected->hasName())
2677-
continue;
2678-
for (auto *var : vars) {
2679-
if (var->hasName() && var->getName() == expected->getName()) {
2680-
SILValue value = VarLocs[var].value;
2681-
SILType type = value->getType();
2682-
2683-
// If we have an address-only type, initialize the temporary
2684-
// allocation. We're not going to pass the address as a block
2685-
// argument.
2686-
if (type.isAddressOnly(M)) {
2687-
emission.emitAddressOnlyInitialization(expected, value);
2688-
break;
2689-
}
2657+
return;
2658+
}
26902659

2691-
// If we have a loadable address, perform a load [copy].
2692-
if (type.isAddress()) {
2693-
value = B.emitLoadValueOperation(CurrentSILLoc, value,
2694-
LoadOwnershipQualifier::Copy);
2695-
args.push_back(value);
2696-
break;
2697-
}
2660+
// If we don't have a fallthrough or a multi-pattern 'case', we can just
2661+
// emit the body inline and save some dead blocks. Emit the statement here.
2662+
if (!row.hasFallthroughTo() && caseBlock->getCaseLabelItems().size() == 1) {
2663+
emission.emitCaseBody(caseBlock);
2664+
return;
2665+
}
2666+
2667+
JumpDest sharedDest = emission.getSharedCaseBlockDest(caseBlock);
2668+
2669+
// Generate the arguments from this row's pattern in the case block's
2670+
// expected order, and keep those arguments from being cleaned up, as
2671+
// we're passing the +1 along to the shared case block dest. (The
2672+
// cleanups still happen, as they are threaded through here messily,
2673+
// but the explicit retains here counteract them, and then the
2674+
// retain/release pair gets optimized out.)
2675+
ArrayRef<CaseLabelItem> labelItems = caseBlock->getCaseLabelItems();
2676+
SmallVector<SILValue, 4> args;
2677+
SmallVector<VarDecl *, 4> expectedVarOrder;
2678+
SmallVector<VarDecl *, 4> vars;
2679+
labelItems[0].getPattern()->collectVariables(expectedVarOrder);
2680+
row.getCasePattern()->collectVariables(vars);
26982681

2699-
value = B.emitCopyValueOperation(CurrentSILLoc, value);
2682+
SILModule &M = F.getModule();
2683+
for (auto expected : expectedVarOrder) {
2684+
if (!expected->hasName())
2685+
continue;
2686+
for (auto *var : vars) {
2687+
if (var->hasName() && var->getName() == expected->getName()) {
2688+
SILValue value = VarLocs[var].value;
2689+
SILType type = value->getType();
2690+
2691+
// If we have an address-only type, initialize the temporary
2692+
// allocation. We're not going to pass the address as a block
2693+
// argument.
2694+
if (type.isAddressOnly(M)) {
2695+
emission.emitAddressOnlyInitialization(expected, value);
2696+
break;
2697+
}
2698+
2699+
// If we have a loadable address, perform a load [copy].
2700+
if (type.isAddress()) {
2701+
value = B.emitLoadValueOperation(CurrentSILLoc, value,
2702+
LoadOwnershipQualifier::Copy);
27002703
args.push_back(value);
27012704
break;
27022705
}
2706+
2707+
value = B.emitCopyValueOperation(CurrentSILLoc, value);
2708+
args.push_back(value);
2709+
break;
27032710
}
27042711
}
2705-
2706-
Cleanups.emitBranchAndCleanups(sharedDest, caseBlock, args);
2707-
} else {
2708-
// However, if we don't have a fallthrough or a multi-pattern 'case', we
2709-
// can just emit the body inline and save some dead blocks.
2710-
// Emit the statement here.
2711-
emission.emitCaseBody(caseBlock);
27122712
}
2713+
2714+
Cleanups.emitBranchAndCleanups(sharedDest, caseBlock, args);
27132715
};
27142716

27152717
PatternMatchEmission emission(*this, S, completionHandler);

0 commit comments

Comments
 (0)