Skip to content

Commit 9e969d2

Browse files
committed
[silgen] Add helper methods to SwitchEnumBuilder for creating .Some, .None cases.
I also used it to clean up a few uses of SwitchEnumBuilder.
1 parent af19e54 commit 9e969d2

File tree

4 files changed

+57
-28
lines changed

4 files changed

+57
-28
lines changed

lib/SILGen/SILGenConvert.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,8 @@ SILGenFunction::emitOptionalToOptional(SILLocation loc,
391391
finalResult = B.createOwnedPHIArgument(resultTL.getLoweredType());
392392
}
393393

394-
SEBuilder.addCase(
395-
getASTContext().getOptionalSomeDecl(), isPresentBB, contBB,
396-
[&](ManagedValue input, SwitchCaseFullExpr &&scope) {
394+
SEBuilder.addOptionalSomeCase(
395+
isPresentBB, contBB, [&](ManagedValue input, SwitchCaseFullExpr &&scope) {
397396
// If we have an address only type, we want to match the old behavior of
398397
// transforming the underlying type instead of the optional type. This
399398
// ensures that we use the more efficient non-generic code paths when
@@ -420,8 +419,8 @@ SILGenFunction::emitOptionalToOptional(SILLocation loc,
420419
return scope.exitAndBranch(loc);
421420
});
422421

423-
SEBuilder.addCase(
424-
getASTContext().getOptionalNoneDecl(), isNotPresentBB, contBB,
422+
SEBuilder.addOptionalNoneCase(
423+
isNotPresentBB, contBB,
425424
[&](ManagedValue input, SwitchCaseFullExpr &&scope) {
426425
if (!(resultTL.isAddressOnly() && silConv.useLoweredAddresses())) {
427426
SILValue none =

lib/SILGen/SILGenExpr.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4253,23 +4253,23 @@ static ManagedValue flattenOptional(SILGenFunction &SGF, SILLocation loc,
42534253

42544254
SwitchEnumBuilder SEB(SGF.B, loc, optVal);
42554255

4256-
auto *someDecl = SGF.getASTContext().getOptionalSomeDecl();
4257-
SEB.addCase(someDecl, isPresentBB, contBB, [&](ManagedValue input,
4258-
SwitchCaseFullExpr &&scope) {
4259-
if (resultTL.isAddressOnly()) {
4260-
SILValue addr =
4261-
addrOnlyResultBuf->getAddressForInPlaceInitialization(SGF, loc);
4262-
input = SGF.B.createUncheckedTakeEnumDataAddr(
4263-
loc, input, someDecl, input.getType().getOptionalObjectType());
4264-
SGF.B.createCopyAddr(loc, input.getValue(), addr, IsNotTake,
4265-
IsInitialization);
4266-
scope.exitAndBranch(loc);
4267-
return;
4268-
}
4269-
scope.exitAndBranch(loc, input.forward(SGF));
4270-
});
4271-
SEB.addCase(
4272-
SGF.getASTContext().getOptionalNoneDecl(), isNotPresentBB, contBB,
4256+
SEB.addOptionalSomeCase(
4257+
isPresentBB, contBB, [&](ManagedValue input, SwitchCaseFullExpr &&scope) {
4258+
if (resultTL.isAddressOnly()) {
4259+
SILValue addr =
4260+
addrOnlyResultBuf->getAddressForInPlaceInitialization(SGF, loc);
4261+
auto *someDecl = SGF.getASTContext().getOptionalSomeDecl();
4262+
input = SGF.B.createUncheckedTakeEnumDataAddr(
4263+
loc, input, someDecl, input.getType().getOptionalObjectType());
4264+
SGF.B.createCopyAddr(loc, input.getValue(), addr, IsNotTake,
4265+
IsInitialization);
4266+
scope.exitAndBranch(loc);
4267+
return;
4268+
}
4269+
scope.exitAndBranch(loc, input.forward(SGF));
4270+
});
4271+
SEB.addOptionalNoneCase(
4272+
isNotPresentBB, contBB,
42734273
[&](ManagedValue input, SwitchCaseFullExpr &&scope) {
42744274
if (resultTL.isAddressOnly()) {
42754275
SILValue addr =

lib/SILGen/SILGenStmt.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -816,9 +816,8 @@ void StmtEmitter::visitForEachStmt(ForEachStmt *S) {
816816
SILBasicBlock *failExitingBlock = createBasicBlock();
817817
SwitchEnumBuilder switchEnumBuilder(SGF.B, S, nextBufOrValue);
818818

819-
switchEnumBuilder.addCase(
820-
SGF.getASTContext().getOptionalSomeDecl(), createBasicBlock(),
821-
loopDest.getBlock(),
819+
switchEnumBuilder.addOptionalSomeCase(
820+
createBasicBlock(), loopDest.getBlock(),
822821
[&](ManagedValue inputValue, SwitchCaseFullExpr &&scope) {
823822
SGF.emitProfilerIncrement(S->getBody());
824823

@@ -881,9 +880,8 @@ void StmtEmitter::visitForEachStmt(ForEachStmt *S) {
881880
// We add loop fail block, just to be defensive about intermediate
882881
// transformations performing cleanups at scope.exit(). We still jump to the
883882
// contBlock.
884-
switchEnumBuilder.addCase(
885-
SGF.getASTContext().getOptionalNoneDecl(), createBasicBlock(),
886-
failExitingBlock,
883+
switchEnumBuilder.addOptionalNoneCase(
884+
createBasicBlock(), failExitingBlock,
887885
[&](ManagedValue inputValue, SwitchCaseFullExpr &&scope) {
888886
assert(!inputValue && "None should not be passed an argument!");
889887
scope.exitAndBranch(S);

lib/SILGen/SwitchEnumBuilder.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,38 @@ class SwitchEnumBuilder {
121121
caseDataArray.emplace_back(decl, caseBlock, contBlock, handle, count);
122122
}
123123

124+
void addOptionalSomeCase(SILBasicBlock *caseBlock) {
125+
auto *decl = getSGF().getASTContext().getOptionalSomeDecl();
126+
caseDataArray.emplace_back(
127+
decl, caseBlock, nullptr,
128+
[](ManagedValue mv, SwitchCaseFullExpr &&expr) { expr.exit(); },
129+
ProfileCounter());
130+
}
131+
132+
void addOptionalNoneCase(SILBasicBlock *caseBlock) {
133+
auto *decl = getSGF().getASTContext().getOptionalNoneDecl();
134+
caseDataArray.emplace_back(
135+
decl, caseBlock, nullptr,
136+
[](ManagedValue mv, SwitchCaseFullExpr &&expr) { expr.exit(); },
137+
ProfileCounter());
138+
}
139+
140+
void addOptionalSomeCase(SILBasicBlock *caseBlock,
141+
NullablePtr<SILBasicBlock> contBlock,
142+
NormalCaseHandler handle,
143+
ProfileCounter count = ProfileCounter()) {
144+
auto *decl = getSGF().getASTContext().getOptionalSomeDecl();
145+
caseDataArray.emplace_back(decl, caseBlock, contBlock, handle, count);
146+
}
147+
148+
void addOptionalNoneCase(SILBasicBlock *caseBlock,
149+
NullablePtr<SILBasicBlock> contBlock,
150+
NormalCaseHandler handle,
151+
ProfileCounter count = ProfileCounter()) {
152+
auto *decl = getSGF().getASTContext().getOptionalNoneDecl();
153+
caseDataArray.emplace_back(decl, caseBlock, contBlock, handle, count);
154+
}
155+
124156
void emit() &&;
125157

126158
private:

0 commit comments

Comments
 (0)