Skip to content

[silgen] Remove some unnecessary indentation from SILGenPattern::emitSwitchStmt #22720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 53 additions & 50 deletions lib/SILGen/SILGenPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2654,62 +2654,65 @@ void SILGenFunction::emitSwitchStmt(SwitchStmt *S) {
// statement.
JumpDest sharedDest = emission.getSharedCaseBlockDest(caseBlock);
Cleanups.emitBranchAndCleanups(sharedDest, caseBlock);
} else if (row.hasFallthroughTo() || caseBlock->getCaseLabelItems().size() > 1) {
JumpDest sharedDest =
emission.getSharedCaseBlockDest(caseBlock);

// Generate the arguments from this row's pattern in the case block's
// expected order, and keep those arguments from being cleaned up, as
// we're passing the +1 along to the shared case block dest. (The
// cleanups still happen, as they are threaded through here messily,
// but the explicit retains here counteract them, and then the
// retain/release pair gets optimized out.)
ArrayRef<CaseLabelItem> labelItems = caseBlock->getCaseLabelItems();
SmallVector<SILValue, 4> args;
SmallVector<VarDecl *, 4> expectedVarOrder;
SmallVector<VarDecl *, 4> vars;
labelItems[0].getPattern()->collectVariables(expectedVarOrder);
row.getCasePattern()->collectVariables(vars);

SILModule &M = F.getModule();
for (auto expected : expectedVarOrder) {
if (!expected->hasName())
return;
}

// If we don't have a fallthrough or a multi-pattern 'case', we can just
// emit the body inline and save some dead blocks. Emit the statement here.
if (!row.hasFallthroughTo() && caseBlock->getCaseLabelItems().size() == 1) {
emission.emitCaseBody(caseBlock);
return;
}

JumpDest sharedDest = emission.getSharedCaseBlockDest(caseBlock);

// Generate the arguments from this row's pattern in the case block's
// expected order, and keep those arguments from being cleaned up, as
// we're passing the +1 along to the shared case block dest. (The
// cleanups still happen, as they are threaded through here messily,
// but the explicit retains here counteract them, and then the
// retain/release pair gets optimized out.)
ArrayRef<CaseLabelItem> labelItems = caseBlock->getCaseLabelItems();
SmallVector<SILValue, 4> args;
SmallVector<VarDecl *, 4> expectedVarOrder;
SmallVector<VarDecl *, 4> vars;
labelItems[0].getPattern()->collectVariables(expectedVarOrder);
row.getCasePattern()->collectVariables(vars);

SILModule &M = F.getModule();
for (auto expected : expectedVarOrder) {
if (!expected->hasName())
continue;
for (auto *var : vars) {
if (!var->hasName() || var->getName() != expected->getName())
continue;
for (auto *var : vars) {
if (var->hasName() && var->getName() == expected->getName()) {
SILValue value = VarLocs[var].value;
SILType type = value->getType();

// If we have an address-only type, initialize the temporary
// allocation. We're not going to pass the address as a block
// argument.
if (type.isAddressOnly(M)) {
emission.emitAddressOnlyInitialization(expected, value);
break;
}

// If we have a loadable address, perform a load [copy].
if (type.isAddress()) {
value = B.emitLoadValueOperation(CurrentSILLoc, value,
LoadOwnershipQualifier::Copy);
args.push_back(value);
break;
}
SILValue value = VarLocs[var].value;
SILType type = value->getType();

value = B.emitCopyValueOperation(CurrentSILLoc, value);
args.push_back(value);
break;
}
// If we have an address-only type, initialize the temporary
// allocation. We're not going to pass the address as a block
// argument.
if (type.isAddressOnly(M)) {
emission.emitAddressOnlyInitialization(expected, value);
break;
}
}

Cleanups.emitBranchAndCleanups(sharedDest, caseBlock, args);
} else {
// However, if we don't have a fallthrough or a multi-pattern 'case', we
// can just emit the body inline and save some dead blocks.
// Emit the statement here.
emission.emitCaseBody(caseBlock);
// If we have a loadable address, perform a load [copy].
if (type.isAddress()) {
value = B.emitLoadValueOperation(CurrentSILLoc, value,
LoadOwnershipQualifier::Copy);
args.push_back(value);
break;
}

value = B.emitCopyValueOperation(CurrentSILLoc, value);
args.push_back(value);
break;
}
}

Cleanups.emitBranchAndCleanups(sharedDest, caseBlock, args);
};

PatternMatchEmission emission(*this, S, completionHandler);
Expand Down