Skip to content

Commit c0c02f9

Browse files
committed
Enable SimplifyCFG::simplifySwitchEnumOnObjcClassOptional
1 parent 8e7019e commit c0c02f9

File tree

4 files changed

+102
-424
lines changed

4 files changed

+102
-424
lines changed

lib/SIL/Utils/InstructionUtils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,9 @@ bool swift::onlyAffectsRefCount(SILInstruction *user) {
279279
switch (user->getKind()) {
280280
default:
281281
return false;
282-
case SILInstructionKind::AutoreleaseValueInst:
282+
case SILInstructionKind::CopyValueInst:
283283
case SILInstructionKind::DestroyValueInst:
284+
case SILInstructionKind::AutoreleaseValueInst:
284285
case SILInstructionKind::ReleaseValueInst:
285286
case SILInstructionKind::RetainValueInst:
286287
case SILInstructionKind::StrongReleaseInst:

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,9 @@ static bool containsOnlyObjMethodCallOnOptional(SILValue optionalValue,
18471847

18481848
// The branch should forward one of the objc_method call.
18491849
if (auto *br = dyn_cast<BranchInst>(inst)) {
1850-
if (br->getNumArgs() == 0 || br->getNumArgs() > 1)
1850+
if (br->getNumArgs() == 0)
1851+
continue;
1852+
if (br->getNumArgs() > 1)
18511853
return false;
18521854
auto branchArg = br->getArg(0);
18531855
if (std::find(objCApplies.begin(), objCApplies.end(), branchArg) ==
@@ -1895,6 +1897,9 @@ static bool onlyForwardsNone(SILBasicBlock *noneBB, SILBasicBlock *someBB,
18951897
continue;
18961898
}
18971899
if (auto *noneBranch = dyn_cast<BranchInst>(inst)) {
1900+
if (noneBranch->getNumArgs() == 0) {
1901+
continue;
1902+
}
18981903
if (noneBranch->getNumArgs() != 1 ||
18991904
(noneBranch->getArg(0) != SEI->getOperand() &&
19001905
noneBranch->getArg(0) != optionalNone))
@@ -2006,12 +2011,6 @@ static bool hasSameUltimateSuccessor(SILBasicBlock *noneBB, SILBasicBlock *someB
20062011
/// %4 = enum #Optional.none
20072012
/// br mergeBB(%4)
20082013
bool SimplifyCFG::simplifySwitchEnumOnObjcClassOptional(SwitchEnumInst *SEI) {
2009-
// TODO: OSSA; handle non-trivial enum case cleanup
2010-
// (simplify_switch_enum_objc.sil).
2011-
if (!EnableOSSARewriteTerminator && Fn.hasOwnership()) {
2012-
return false;
2013-
}
2014-
20152014
auto optional = SEI->getOperand();
20162015
auto optionalPayloadType = optional->getType().getOptionalObjectType();
20172016
if (!optionalPayloadType ||
@@ -2048,10 +2047,16 @@ bool SimplifyCFG::simplifySwitchEnumOnObjcClassOptional(SwitchEnumInst *SEI) {
20482047
optionalPayloadType);
20492048
optionalPayload->replaceAllUsesWith(payloadCast);
20502049
auto *switchBB = SEI->getParent();
2051-
if (someBB->getNumArguments())
2052-
Builder.createBranch(SEI->getLoc(), someBB, SILValue(payloadCast));
2053-
else
2050+
2051+
if (!someBB->args_empty()) {
2052+
assert(someBB->getNumArguments() == 1);
2053+
someBB->eraseArgument(0);
20542054
Builder.createBranch(SEI->getLoc(), someBB);
2055+
}
2056+
else {
2057+
assert(!Fn.hasOwnership());
2058+
Builder.createBranch(SEI->getLoc(), someBB);
2059+
}
20552060

20562061
SEI->eraseFromParent();
20572062
addToWorklist(switchBB);

lib/SILOptimizer/UtilityPasses/UnitTestRunner.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,19 @@ struct SimplifyCFGSimplifySwitchEnumBlock : UnitTest {
321321
}
322322
};
323323

324+
struct SimplifyCFGSwitchEnumOnObjcClassOptional : UnitTest {
325+
SimplifyCFGSwitchEnumOnObjcClassOptional(UnitTestRunner *pass) : UnitTest(pass) {}
326+
void invoke(Arguments &arguments) override {
327+
auto *passToRun = cast<SILFunctionTransform>(createSimplifyCFG());
328+
passToRun->injectPassManager(getPass()->getPassManager());
329+
passToRun->injectFunction(getFunction());
330+
SimplifyCFG(*getFunction(), *passToRun, /*VerifyAll=*/false,
331+
/*EnableJumpThread=*/false)
332+
.simplifySwitchEnumOnObjcClassOptional(
333+
cast<SwitchEnumInst>(arguments.takeInstruction()));
334+
}
335+
};
336+
324337
struct SimplifyCFGSimplifySwitchEnumUnreachableBlocks : UnitTest {
325338
SimplifyCFGSimplifySwitchEnumUnreachableBlocks(UnitTestRunner *pass)
326339
: UnitTest(pass) {}
@@ -481,6 +494,9 @@ void UnitTestRunner::withTest(StringRef name, Doit doit) {
481494
ADD_UNIT_TEST_SUBCLASS(
482495
"simplify-cfg-simplify-switch-enum-unreachable-blocks",
483496
SimplifyCFGSimplifySwitchEnumUnreachableBlocks)
497+
ADD_UNIT_TEST_SUBCLASS(
498+
"simplify-cfg-simplify-switch-enum-on-objc-class-optional",
499+
SimplifyCFGSwitchEnumOnObjcClassOptional)
484500
ADD_UNIT_TEST_SUBCLASS(
485501
"simplify-cfg-simplify-term-with-identical-dest-blocks",
486502
SimplifyCFGSimplifyTermWithIdenticalDestBlocks)

0 commit comments

Comments
 (0)