Skip to content

Commit a1462ef

Browse files
committed
SIL: Promote removeDeadBlock() from SILOptimizer to a method on SILBasicBlock
1 parent 231f5b5 commit a1462ef

File tree

6 files changed

+30
-42
lines changed

6 files changed

+30
-42
lines changed

include/swift/SIL/SILBasicBlock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ public SwiftObjectHeader {
174174
/// This method unlinks 'self' from the containing SILFunction and deletes it.
175175
void eraseFromParent();
176176

177+
/// Replaces usages of this block with 'undef's and then deletes it.
178+
void removeDeadBlock();
179+
177180
/// Remove all instructions of a SILGlobalVariable's static initializer block.
178181
void clearStaticInitializerBlock(SILModule &module);
179182

include/swift/SILOptimizer/Utils/BasicBlockOptUtils.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,6 @@ class NonErrorHandlingBlocks {
8383
}
8484
};
8585

86-
/// Remove all instructions in the body of \p bb in safe manner by using
87-
/// undef.
88-
void clearBlockBody(SILBasicBlock *bb);
89-
90-
/// Handle the mechanical aspects of removing an unreachable block.
91-
void removeDeadBlock(SILBasicBlock *bb);
92-
9386
/// Remove all unreachable blocks in a function.
9487
bool removeUnreachableBlocks(SILFunction &f);
9588

lib/SIL/IR/SILBasicBlock.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,29 @@ void SILBasicBlock::eraseFromParent() {
127127
getParent()->eraseBlock(this);
128128
}
129129

130+
/// Handle the mechanical aspects of removing an unreachable block.
131+
void SILBasicBlock::removeDeadBlock() {
132+
for (SILArgument *arg : getArguments()) {
133+
arg->replaceAllUsesWithUndef();
134+
// To appease the ownership verifier, just set to None.
135+
arg->setOwnershipKind(OwnershipKind::None);
136+
}
137+
138+
// Instructions in the dead block may be used by other dead blocks. Replace
139+
// any uses of them with undef values.
140+
while (!empty()) {
141+
// Grab the last instruction in the bb.
142+
auto *inst = &back();
143+
144+
// Replace any still-remaining uses with undef values and erase.
145+
inst->replaceAllUsesOfAllResultsWithUndef();
146+
inst->eraseFromParent();
147+
}
148+
149+
// Now that the bb is empty, eliminate it.
150+
eraseFromParent();
151+
}
152+
130153
void SILBasicBlock::cloneArgumentList(SILBasicBlock *Other) {
131154
assert(Other->isEntry() == isEntry() &&
132155
"Expected to both blocks to be entries or not");

lib/SILOptimizer/FunctionSignatureTransforms/ExistentialTransform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ void ExistentialTransform::populateThunkBody() {
380380
/// Remove original body of F.
381381
for (auto It = F->begin(), End = F->end(); It != End;) {
382382
auto *BB = &*It++;
383-
removeDeadBlock(BB);
383+
BB->removeDeadBlock();
384384
}
385385

386386
/// Create a basic block and the function arguments.

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ bool SimplifyCFG::removeIfDead(SILBasicBlock *BB) {
642642
addToWorklist(S);
643643

644644
LLVM_DEBUG(llvm::dbgs() << "remove dead bb" << BB->getDebugID() << '\n');
645-
removeDeadBlock(BB);
645+
BB->removeDeadBlock();
646646
++NumBlocksDeleted;
647647
return true;
648648
}
@@ -1782,7 +1782,7 @@ bool SimplifyCFG::simplifySwitchEnumUnreachableBlocks(SwitchEnumInst *SEI) {
17821782
addToWorklist(SEI->getParent());
17831783
SILBuilderWithScope(SEI).createUnreachable(SEI->getLoc());
17841784
for (auto &succ : SEI->getSuccessors()) {
1785-
removeDeadBlock(succ.getBB());
1785+
succ.getBB()->removeDeadBlock();
17861786
}
17871787
SEI->eraseFromParent();
17881788
return true;

lib/SILOptimizer/Utils/BasicBlockOptUtils.cpp

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,37 +67,6 @@ NonErrorHandlingBlocks::NonErrorHandlingBlocks(SILFunction *function)
6767
}
6868
}
6969

70-
/// Remove all instructions in the body of \p bb in safe manner by using
71-
/// undef.
72-
void swift::clearBlockBody(SILBasicBlock *bb) {
73-
74-
for (SILArgument *arg : bb->getArguments()) {
75-
arg->replaceAllUsesWithUndef();
76-
// To appease the ownership verifier, just set to None.
77-
arg->setOwnershipKind(OwnershipKind::None);
78-
}
79-
80-
// Instructions in the dead block may be used by other dead blocks. Replace
81-
// any uses of them with undef values.
82-
while (!bb->empty()) {
83-
// Grab the last instruction in the bb.
84-
auto *inst = &bb->back();
85-
86-
// Replace any still-remaining uses with undef values and erase.
87-
inst->replaceAllUsesOfAllResultsWithUndef();
88-
inst->eraseFromParent();
89-
}
90-
}
91-
92-
// Handle the mechanical aspects of removing an unreachable block.
93-
void swift::removeDeadBlock(SILBasicBlock *bb) {
94-
// Clear the body of bb.
95-
clearBlockBody(bb);
96-
97-
// Now that the bb is empty, eliminate it.
98-
bb->eraseFromParent();
99-
}
100-
10170
bool swift::removeUnreachableBlocks(SILFunction &f) {
10271
ReachableBlocks reachable(&f);
10372
// Visit all the blocks without doing any extra work.
@@ -109,7 +78,7 @@ bool swift::removeUnreachableBlocks(SILFunction &f) {
10978
for (auto ii = std::next(f.begin()), end = f.end(); ii != end;) {
11079
auto *bb = &*ii++;
11180
if (!reachable.isVisited(bb)) {
112-
removeDeadBlock(bb);
81+
bb->removeDeadBlock();
11382
changed = true;
11483
}
11584
}

0 commit comments

Comments
 (0)