Skip to content

Commit 5604831

Browse files
committed
[sil-combine] Enable switch_value to be optimized in OSSA.
This routine never touches values with non-trivial ownership, so I just removed the early exit and updated the style.
1 parent 2413f5f commit 5604831

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -256,47 +256,46 @@ SILInstruction *SILCombiner::visitSelectEnumAddrInst(SelectEnumAddrInst *seai) {
256256
return eraseInstFromFunction(*seai);
257257
}
258258

259-
SILInstruction *SILCombiner::visitSwitchValueInst(SwitchValueInst *SVI) {
260-
if (SVI->getFunction()->hasOwnership())
259+
SILInstruction *SILCombiner::visitSwitchValueInst(SwitchValueInst *svi) {
260+
SILValue cond = svi->getOperand();
261+
BuiltinIntegerType *condTy = cond->getType().getAs<BuiltinIntegerType>();
262+
if (!condTy || !condTy->isFixedWidth(1))
261263
return nullptr;
262264

263-
SILValue Cond = SVI->getOperand();
264-
BuiltinIntegerType *CondTy = Cond->getType().getAs<BuiltinIntegerType>();
265-
if (!CondTy || !CondTy->isFixedWidth(1))
266-
return nullptr;
267-
268-
SILBasicBlock *FalseBB = nullptr;
269-
SILBasicBlock *TrueBB = nullptr;
270-
for (unsigned Idx = 0, Num = SVI->getNumCases(); Idx < Num; ++Idx) {
271-
auto Case = SVI->getCase(Idx);
272-
auto *CaseVal = dyn_cast<IntegerLiteralInst>(Case.first);
273-
if (!CaseVal)
265+
SILBasicBlock *falseBB = nullptr;
266+
SILBasicBlock *trueBB = nullptr;
267+
for (unsigned idx : range(svi->getNumCases())) {
268+
auto switchCase = svi->getCase(idx);
269+
auto *caseVal = dyn_cast<IntegerLiteralInst>(switchCase.first);
270+
if (!caseVal)
274271
return nullptr;
275-
SILBasicBlock *DestBB = Case.second;
276-
assert(DestBB->args_empty() &&
272+
SILBasicBlock *destBB = switchCase.second;
273+
assert(destBB->args_empty() &&
277274
"switch_value case destination cannot take arguments");
278-
if (CaseVal->getValue() == 0) {
279-
assert(!FalseBB && "double case value 0 in switch_value");
280-
FalseBB = DestBB;
275+
if (caseVal->getValue() == 0) {
276+
assert(!falseBB && "double case value 0 in switch_value");
277+
falseBB = destBB;
281278
} else {
282-
assert(!TrueBB && "double case value 1 in switch_value");
283-
TrueBB = DestBB;
279+
assert(!trueBB && "double case value 1 in switch_value");
280+
trueBB = destBB;
284281
}
285282
}
286-
if (SVI->hasDefault()) {
287-
assert(SVI->getDefaultBB()->args_empty() &&
283+
284+
if (svi->hasDefault()) {
285+
assert(svi->getDefaultBB()->args_empty() &&
288286
"switch_value default destination cannot take arguments");
289-
if (!FalseBB) {
290-
FalseBB = SVI->getDefaultBB();
291-
} else if (!TrueBB) {
292-
TrueBB = SVI->getDefaultBB();
287+
if (!falseBB) {
288+
falseBB = svi->getDefaultBB();
289+
} else if (!trueBB) {
290+
trueBB = svi->getDefaultBB();
293291
}
294292
}
295-
if (!FalseBB || !TrueBB)
293+
294+
if (!falseBB || !trueBB)
296295
return nullptr;
297296

298-
Builder.setCurrentDebugScope(SVI->getDebugScope());
299-
return Builder.createCondBranch(SVI->getLoc(), Cond, TrueBB, FalseBB);
297+
Builder.setCurrentDebugScope(svi->getDebugScope());
298+
return Builder.createCondBranch(svi->getLoc(), cond, trueBB, falseBB);
300299
}
301300

302301
namespace {

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3681,9 +3681,10 @@ bb3(%16 : $Int32): // Preds: bb1 bb2
36813681
return %16 : $Int32 // id: %17
36823682
}
36833683

3684-
// CHECK-LABEL: sil [ossa] @switch_value_to_cond_br_1
3685-
// XHECK: bb0({{.*}}):
3686-
// XHECK-NEXT: cond_br %0, bb1, bb2
3684+
// CHECK-LABEL: sil [ossa] @switch_value_to_cond_br_1 :
3685+
// CHECK: bb0({{.*}}):
3686+
// CHECK-NEXT: cond_br %0, bb1, bb2
3687+
// CHECK: } // end sil function 'switch_value_to_cond_br_1'
36873688
sil [ossa] @switch_value_to_cond_br_1 : $@convention(thin) (Builtin.Int1) -> Int32 {
36883689
bb0(%0 : $Builtin.Int1):
36893690
%t1 = integer_literal $Builtin.Int1, -1
@@ -3703,9 +3704,10 @@ bb3(%16 : $Int32): // Preds: bb1 bb2
37033704
return %16 : $Int32 // id: %17
37043705
}
37053706

3706-
// CHECK-LABEL: sil [ossa] @switch_value_to_cond_br_2
3707-
// XHECK: bb0({{.*}}):
3708-
// XHECK-NEXT: cond_br %0, bb2, bb1
3707+
// CHECK-LABEL: sil [ossa] @switch_value_to_cond_br_2 :
3708+
// CHECK: bb0({{.*}}):
3709+
// CHECK-NEXT: cond_br %0, bb2, bb1
3710+
// CHECK: } // end sil function 'switch_value_to_cond_br_2'
37093711
sil [ossa] @switch_value_to_cond_br_2 : $@convention(thin) (Builtin.Int1) -> Int32 {
37103712
bb0(%0 : $Builtin.Int1):
37113713
%f1 = integer_literal $Builtin.Int1, 0
@@ -3725,9 +3727,10 @@ bb3(%16 : $Int32): // Preds: bb1 bb2
37253727
return %16 : $Int32 // id: %17
37263728
}
37273729

3728-
// CHECK-LABEL: sil [ossa] @switch_value_to_cond_br_3
3729-
// XHECK: bb0({{.*}}):
3730-
// XHECK-NEXT: cond_br %0, bb1, bb2
3730+
// CHECK-LABEL: sil [ossa] @switch_value_to_cond_br_3 :
3731+
// CHECK: bb0({{.*}}):
3732+
// CHECK-NEXT: cond_br %0, bb1, bb2
3733+
// CHECK: } // end sil function 'switch_value_to_cond_br_3'
37313734
sil [ossa] @switch_value_to_cond_br_3 : $@convention(thin) (Builtin.Int1) -> Int32 {
37323735
bb0(%0 : $Builtin.Int1):
37333736
%t1 = integer_literal $Builtin.Int1, -1

0 commit comments

Comments
 (0)