SILOptimizer: add basic block utilities ReachingReturnBlocks and NonErrorHandlingBlocks

* ReachingReturnBlocks: computes the set of blocks from which a path to the return-block exists (does not include paths to a throw-block)
* NonErrorHandlingBlocks: computes the set of blocks which are not used for error handling, i.e. not (exclusively) reachable from the error-block of a try_apply
This commit is contained in:
Erik Eckstein
2021-10-25 13:40:35 +02:00
parent ff2db93922
commit 094494e1ed
2 changed files with 56 additions and 0 deletions

View File

@@ -39,6 +39,33 @@ bool ReachableBlocks::visit(function_ref<bool(SILBasicBlock *)> visitor) {
return true;
}
ReachingReturnBlocks::ReachingReturnBlocks(SILFunction *function)
: worklist(function) {
for (SILBasicBlock &block : *function) {
if (isa<ReturnInst>(block.getTerminator()))
worklist.push(&block);
}
while (SILBasicBlock *block = worklist.pop()) {
for (SILBasicBlock *pred : block->getPredecessorBlocks()) {
worklist.pushIfNotVisited(pred);
}
}
}
NonErrorHandlingBlocks::NonErrorHandlingBlocks(SILFunction *function)
: worklist(function->getEntryBlock()) {
while (SILBasicBlock *block = worklist.pop()) {
if (auto ta = dyn_cast<TryApplyInst>(block->getTerminator())) {
worklist.pushIfNotVisited(ta->getNormalBB());
} else {
for (SILBasicBlock *succ : block->getSuccessorBlocks()) {
worklist.pushIfNotVisited(succ);
}
}
}
}
/// Remove all instructions in the body of \p bb in safe manner by using
/// undef.
void swift::clearBlockBody(SILBasicBlock *bb) {