Skip to content

Commit 142c3bd

Browse files
committed
[simplify-cfg] Enable some simple opts during ownerships on br, cond_br that do not involve objects directly.
Just to reduce the size of the CFG.
1 parent 6c25573 commit 142c3bd

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,6 +2657,8 @@ bool SimplifyCFG::simplifyBlocks() {
26572657
for (auto &BB : Fn)
26582658
addToWorklist(&BB);
26592659

2660+
bool hasOwnership = Fn.hasOwnership();
2661+
26602662
// Iteratively simplify while there is still work to do.
26612663
while (SILBasicBlock *BB = popWorklist()) {
26622664
// If the block is dead, remove it.
@@ -2674,6 +2676,11 @@ bool SimplifyCFG::simplifyBlocks() {
26742676
Changed = true;
26752677
continue;
26762678
}
2679+
2680+
// We do not jump thread at all now.
2681+
if (hasOwnership)
2682+
continue;
2683+
26772684
// If this unconditional branch has BBArgs, check to see if duplicating
26782685
// the destination would allow it to be simplified. This is a simple form
26792686
// of jump threading.
@@ -2686,10 +2693,14 @@ bool SimplifyCFG::simplifyBlocks() {
26862693
Changed |= simplifyCondBrBlock(cast<CondBranchInst>(TI));
26872694
break;
26882695
case TermKind::SwitchValueInst:
2696+
if (hasOwnership)
2697+
continue;
26892698
// FIXME: Optimize for known switch values.
26902699
Changed |= simplifySwitchValueBlock(cast<SwitchValueInst>(TI));
26912700
break;
26922701
case TermKind::SwitchEnumInst: {
2702+
if (hasOwnership)
2703+
continue;
26932704
auto *SEI = cast<SwitchEnumInst>(TI);
26942705
if (simplifySwitchEnumBlock(SEI)) {
26952706
Changed = true;
@@ -2705,31 +2716,49 @@ bool SimplifyCFG::simplifyBlocks() {
27052716
Changed |= simplifyUnreachableBlock(cast<UnreachableInst>(TI));
27062717
break;
27072718
case TermKind::CheckedCastBranchInst:
2719+
if (hasOwnership)
2720+
continue;
27082721
Changed |= simplifyCheckedCastBranchBlock(cast<CheckedCastBranchInst>(TI));
27092722
break;
27102723
case TermKind::CheckedCastValueBranchInst:
2724+
if (hasOwnership)
2725+
continue;
27112726
Changed |= simplifyCheckedCastValueBranchBlock(
27122727
cast<CheckedCastValueBranchInst>(TI));
27132728
break;
27142729
case TermKind::CheckedCastAddrBranchInst:
2730+
if (hasOwnership)
2731+
continue;
27152732
Changed |= simplifyCheckedCastAddrBranchBlock(cast<CheckedCastAddrBranchInst>(TI));
27162733
break;
27172734
case TermKind::TryApplyInst:
2735+
if (hasOwnership)
2736+
continue;
27182737
Changed |= simplifyTryApplyBlock(cast<TryApplyInst>(TI));
27192738
break;
27202739
case TermKind::SwitchEnumAddrInst:
2740+
if (hasOwnership)
2741+
continue;
27212742
Changed |= simplifyTermWithIdenticalDestBlocks(BB);
27222743
break;
27232744
case TermKind::ThrowInst:
27242745
case TermKind::DynamicMethodBranchInst:
27252746
case TermKind::ReturnInst:
27262747
case TermKind::UnwindInst:
27272748
case TermKind::YieldInst:
2749+
if (hasOwnership)
2750+
continue;
27282751
break;
27292752
case TermKind::AwaitAsyncContinuationInst:
2753+
if (hasOwnership)
2754+
continue;
27302755
// TODO(async): Simplify AwaitAsyncContinuationInst
27312756
break;
27322757
}
2758+
2759+
if (hasOwnership)
2760+
continue;
2761+
27332762
// If the block has a cond_fail, try to move it to the predecessors.
27342763
Changed |= tryMoveCondFailToPreds(BB);
27352764

@@ -3170,9 +3199,16 @@ bool SimplifyCFG::run() {
31703199
// First remove any block not reachable from the entry.
31713200
bool Changed = removeUnreachableBlocks(Fn);
31723201

3173-
// If we have ownership bail. We jus4t want to remove unreachable blocks.
3174-
if (Fn.hasOwnership())
3202+
// If we have ownership bail. We just want to remove unreachable blocks and
3203+
// simplify.
3204+
if (Fn.hasOwnership()) {
3205+
DT = nullptr;
3206+
if (simplifyBlocks()) {
3207+
removeUnreachableBlocks(Fn);
3208+
Changed = true;
3209+
}
31753210
return Changed;
3211+
}
31763212

31773213
// Find the set of loop headers. We don't want to jump-thread through headers.
31783214
findLoopHeaders();
@@ -3900,6 +3936,7 @@ bool SimplifyCFG::simplifyProgramTerminationBlock(SILBasicBlock *BB) {
39003936
#include "swift/AST/ReferenceStorage.def"
39013937
case SILInstructionKind::StrongReleaseInst:
39023938
case SILInstructionKind::ReleaseValueInst:
3939+
case SILInstructionKind::DestroyValueInst:
39033940
case SILInstructionKind::DestroyAddrInst:
39043941
break;
39053942
default:
@@ -3942,8 +3979,6 @@ class JumpThreadSimplifyCFGPass : public SILFunctionTransform {
39423979
public:
39433980
void run() override {
39443981
// FIXME: Handle ownership.
3945-
if (getFunction()->hasOwnership())
3946-
return;
39473982
if (SimplifyCFG(*getFunction(), *this, getOptions().VerifyAll,
39483983
/*EnableJumpThread=*/true)
39493984
.run())

lib/SILOptimizer/Utils/BasicBlockOptUtils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ void swift::clearBlockBody(SILBasicBlock *bb) {
4444

4545
for (SILArgument *arg : bb->getArguments()) {
4646
arg->replaceAllUsesWithUndef();
47+
// To appease the ownership verifier, just set to None.
48+
arg->setOwnershipKind(OwnershipKind::None);
4749
}
4850

4951
// Instructions in the dead block may be used by other dead blocks. Replace

0 commit comments

Comments
 (0)