Skip to content

Commit 57352d7

Browse files
committed
[AddressLowering] Handle select_enum.
1 parent ce43690 commit 57352d7

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2751,6 +2751,21 @@ class UseRewriter : SILInstructionVisitor<UseRewriter> {
27512751

27522752
void emitExtract(SingleValueInstruction *extractInst);
27532753

2754+
void visitSelectEnumInst(SelectEnumInst *sei) {
2755+
SmallVector<std::pair<EnumElementDecl *, SILValue>> caseValues;
2756+
for (unsigned index = 0, count = sei->getNumCases(); index < count;
2757+
++index) {
2758+
caseValues.push_back(sei->getCase(index));
2759+
}
2760+
2761+
SILValue opAddr = addrMat.materializeAddress(use->get());
2762+
SILValue addr =
2763+
builder.createSelectEnumAddr(sei->getLoc(), opAddr, sei->getType(),
2764+
sei->getDefaultResult(), caseValues);
2765+
sei->replaceAllUsesWith(addr);
2766+
pass.deleter.forceDelete(sei);
2767+
}
2768+
27542769
// Extract from an opaque struct.
27552770
void visitStructExtractInst(StructExtractInst *extractInst);
27562771

test/SILOptimizer/address_lowering.sil

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,23 @@ exit_error(%47 : @owned $any Error):
13401340
throw %47 : $any Error
13411341
}
13421342

1343+
// CHECK-LABEL: sil [ossa] @selecteroo : $@convention(thin) <T> () -> () {
1344+
// CHECK: [[ADDR:%[^,]+]] = alloc_stack $Optional<T>
1345+
// CHECK: apply undef<T>([[ADDR]]) : $@convention(thin) <τ_0_0> () -> @out Optional<τ_0_0>
1346+
// CHECK: select_enum_addr [[ADDR]] : $*Optional<T>, case #Optional.some!enumelt: {{%[^,]+}}, default {{%[^,]+}} : $Builtin.Int1
1347+
// CHECK: dealloc_stack [[ADDR]] : $*Optional<T>
1348+
// CHECK-LABEL: } // end sil function 'selecteroo'
1349+
sil [ossa] @selecteroo : $@convention(thin) <T> () -> () {
1350+
bb0:
1351+
%11 = apply undef<T>() : $@convention(thin) <τ_0_0> () -> @out Optional<τ_0_0>
1352+
%12 = integer_literal $Builtin.Int1, -1
1353+
%13 = integer_literal $Builtin.Int1, 0
1354+
%14 = select_enum %11 : $Optional<T>, case #Optional.some!enumelt: %12, default %13 : $Builtin.Int1
1355+
destroy_value %11 : $Optional<T>
1356+
%retval = tuple ()
1357+
return %retval : $()
1358+
} // end sil function '$ss17FixedWidthIntegerPsEyxSgSScfC'
1359+
13431360
sil hidden [ossa] @testBeginApplyDeadYield : $@convention(thin) <T> (@guaranteed TestGeneric<T>) -> () {
13441361
bb0(%0 : @guaranteed $TestGeneric<T>):
13451362
%2 = class_method %0 : $TestGeneric<T>, #TestGeneric.borrowedGeneric!read : <T> (TestGeneric<T>) -> () -> (), $@yield_once @convention(method) <τ_0_0> (@guaranteed TestGeneric<τ_0_0>) -> @yields @in_guaranteed τ_0_0

0 commit comments

Comments
 (0)