Skip to content

Commit 2413f5f

Browse files
committed
[sil-combine] Update select_enum_addr canonicalization to use OSSA.
1 parent 0a0f8cf commit 2413f5f

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -210,29 +210,25 @@ SILInstruction *SILCombiner::visitSwitchEnumAddrInst(SwitchEnumAddrInst *SEAI) {
210210
return eraseInstFromFunction(*SEAI);
211211
}
212212

213-
SILInstruction *SILCombiner::visitSelectEnumAddrInst(SelectEnumAddrInst *SEAI) {
214-
if (SEAI->getFunction()->hasOwnership())
215-
return nullptr;
216-
213+
SILInstruction *SILCombiner::visitSelectEnumAddrInst(SelectEnumAddrInst *seai) {
217214
// Canonicalize a select_enum_addr: if the default refers to exactly one case,
218215
// then replace the default with that case.
219-
Builder.setCurrentDebugScope(SEAI->getDebugScope());
220-
if (SEAI->hasDefault()) {
221-
NullablePtr<EnumElementDecl> elementDecl = SEAI->getUniqueCaseForDefault();
216+
Builder.setCurrentDebugScope(seai->getDebugScope());
217+
if (seai->hasDefault()) {
218+
NullablePtr<EnumElementDecl> elementDecl = seai->getUniqueCaseForDefault();
222219
if (elementDecl.isNonNull()) {
223220
// Construct a new instruction by copying all the case entries.
224-
SmallVector<std::pair<EnumElementDecl *, SILValue>, 4> CaseValues;
225-
for (int idx = 0, numIdcs = SEAI->getNumCases(); idx < numIdcs; ++idx) {
226-
CaseValues.push_back(SEAI->getCase(idx));
221+
SmallVector<std::pair<EnumElementDecl *, SILValue>, 4> caseValues;
222+
for (int idx = 0, numIdcs = seai->getNumCases(); idx < numIdcs; ++idx) {
223+
caseValues.push_back(seai->getCase(idx));
227224
}
228225
// Add the default-entry of the original instruction as case-entry.
229-
CaseValues.push_back(
230-
std::make_pair(elementDecl.get(), SEAI->getDefaultResult()));
226+
caseValues.push_back(
227+
std::make_pair(elementDecl.get(), seai->getDefaultResult()));
231228

232-
return Builder.createSelectEnumAddr(SEAI->getLoc(),
233-
SEAI->getEnumOperand(),
234-
SEAI->getType(), SILValue(),
235-
CaseValues);
229+
return Builder.createSelectEnumAddr(
230+
seai->getLoc(), seai->getEnumOperand(), seai->getType(), SILValue(),
231+
caseValues);
236232
}
237233
}
238234

@@ -241,20 +237,23 @@ SILInstruction *SILCombiner::visitSelectEnumAddrInst(SelectEnumAddrInst *SEAI) {
241237
// ->
242238
// %value = load %ptr
243239
// = select_enum %value
244-
SILType Ty = SEAI->getEnumOperand()->getType();
245-
if (!Ty.isLoadable(*SEAI->getFunction()))
240+
SILType ty = seai->getEnumOperand()->getType();
241+
if (!ty.isLoadable(*seai->getFunction()))
246242
return nullptr;
247243

248-
SmallVector<std::pair<EnumElementDecl*, SILValue>, 8> Cases;
249-
for (int i = 0, e = SEAI->getNumCases(); i < e; ++i)
250-
Cases.push_back(SEAI->getCase(i));
251-
252-
SILValue Default = SEAI->hasDefault() ? SEAI->getDefaultResult() : SILValue();
253-
LoadInst *EnumVal = Builder.createLoad(SEAI->getLoc(), SEAI->getEnumOperand(),
254-
LoadOwnershipQualifier::Unqualified);
255-
auto *I = Builder.createSelectEnum(SEAI->getLoc(), EnumVal, SEAI->getType(),
256-
Default, Cases);
257-
return I;
244+
SmallVector<std::pair<EnumElementDecl *, SILValue>, 8> cases;
245+
for (int i = 0, e = seai->getNumCases(); i < e; ++i)
246+
cases.push_back(seai->getCase(i));
247+
248+
SILValue defaultCase =
249+
seai->hasDefault() ? seai->getDefaultResult() : SILValue();
250+
auto enumVal =
251+
Builder.emitLoadBorrowOperation(seai->getLoc(), seai->getEnumOperand());
252+
auto *result = Builder.createSelectEnum(seai->getLoc(), enumVal,
253+
seai->getType(), defaultCase, cases);
254+
Builder.emitEndBorrowOperation(seai->getLoc(), enumVal);
255+
replaceInstUsesWith(*seai, result);
256+
return eraseInstFromFunction(*seai);
258257
}
259258

260259
SILInstruction *SILCombiner::visitSwitchValueInst(SwitchValueInst *SVI) {

test/SILOptimizer/sil_combine_enums_ossa.sil

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ bb2: // Preds: bb0
5454
unreachable // id: %23
5555
}
5656

57-
// CHECK-LABEL: sil [ossa] @eliminate_select_enum_addr
58-
// XHECK-NOT: select_enum_addr
59-
// XHECK: select_enum
60-
// XHECK: return
57+
// CHECK-LABEL: sil [ossa] @eliminate_select_enum_addr :
58+
// CHECK-NOT: select_enum_addr
59+
// CHECK: [[BORROWED_VAL:%.*]] = load_borrow
60+
// CHECK: select_enum [[BORROWED_VAL]]
61+
// CHECK: } // end sil function 'eliminate_select_enum_addr'
6162
sil [ossa] @eliminate_select_enum_addr : $@convention(thin) () -> Int {
6263
bb0:
6364
%0 = alloc_stack $Optional<SomeClass>
@@ -124,10 +125,9 @@ enum G<T> {
124125
case E2
125126
}
126127

127-
// CHECK-LABEL: sil [ossa] @canonicalize_select_enum_addr
128-
// XHECK: select_enum_addr {{.*}} case #G.E2!enumelt:
129-
// XHECK: return
130-
128+
// CHECK-LABEL: sil [ossa] @canonicalize_select_enum_addr :
129+
// CHECK: select_enum_addr {{.*}} case #G.E2!enumelt:
130+
// CHECK: } // end sil function 'canonicalize_select_enum_addr'
131131
sil [ossa] @canonicalize_select_enum_addr : $@convention(thin) <T> (@in G<T>) -> Int32 {
132132
bb0(%0 : $*G<T>):
133133
%2 = integer_literal $Builtin.Int32, 0

0 commit comments

Comments
 (0)