Skip to content

Commit 763bfed

Browse files
committed
SILOptimizer: use the BasicBlockFlag utility in ReachableBlocks
1 parent 65976fd commit 763bfed

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

include/swift/SILOptimizer/Utils/BasicBlockOptUtils.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define SWIFT_SILOPTIMIZER_UTILS_BASICBLOCKOPTUTILS_H
2424

2525
#include "swift/SIL/SILBasicBlock.h"
26+
#include "swift/SIL/SILBitfield.h"
2627
#include "swift/SIL/SILCloner.h"
2728
#include "swift/SIL/SILInstruction.h"
2829
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
@@ -35,20 +36,22 @@ class SILLoopInfo;
3536

3637
/// Compute the set of reachable blocks.
3738
class ReachableBlocks {
38-
SmallPtrSet<SILBasicBlock *, 32> visited;
39+
BasicBlockSet visited;
3940

4041
public:
42+
ReachableBlocks(SILFunction *function) : visited(function) {}
43+
4144
/// Invoke \p visitor for each reachable block in \p f in worklist order (at
4245
/// least one predecessor has been visited--defs are always visited before
4346
/// uses except for phi-type block args). The \p visitor takes a block
4447
/// argument, which is already marked visited, and must return true to
4548
/// continue visiting blocks.
4649
///
4750
/// Returns true if all reachable blocks were visited.
48-
bool visit(SILFunction *f, function_ref<bool(SILBasicBlock *)> visitor);
51+
bool visit(function_ref<bool(SILBasicBlock *)> visitor);
4952

5053
/// Return true if \p bb has been visited.
51-
bool isVisited(SILBasicBlock *bb) const { return visited.count(bb); }
54+
bool isVisited(SILBasicBlock *bb) const { return visited.contains(bb); }
5255
};
5356

5457
/// Remove all instructions in the body of \p bb in safe manner by using

lib/SILOptimizer/Analysis/EscapeAnalysis.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,8 +1619,8 @@ void EscapeAnalysis::ConnectionGraph::verify() const {
16191619
// Verify that all pointer nodes are still mapped, otherwise the process of
16201620
// merging nodes may have lost information. Only visit reachable blocks,
16211621
// because the graph builder only mapped values from reachable blocks.
1622-
ReachableBlocks reachable;
1623-
reachable.visit(F, [this](SILBasicBlock *bb) {
1622+
ReachableBlocks reachable(F);
1623+
reachable.visit([this](SILBasicBlock *bb) {
16241624
for (auto &i : *bb) {
16251625
if (isNonWritableMemoryAddress(&i))
16261626
continue;
@@ -1748,8 +1748,8 @@ void EscapeAnalysis::buildConnectionGraph(FunctionInfo *FInfo,
17481748
assert(ConGraph->isEmpty());
17491749

17501750
// Visit the blocks in dominance order.
1751-
ReachableBlocks reachable;
1752-
reachable.visit(ConGraph->F, [&](SILBasicBlock *bb) {
1751+
ReachableBlocks reachable(ConGraph->F);
1752+
reachable.visit([&](SILBasicBlock *bb) {
17531753
// Create edges for the instructions.
17541754
for (auto &i : *bb) {
17551755
analyzeInstruction(&i, FInfo, BottomUpOrder, RecursionDepth);

lib/SILOptimizer/Utils/BasicBlockOptUtils.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@ using namespace swift;
1919

2020
/// Invoke \p visitor for each reachable block in \p f in worklist order (at
2121
/// least one predecessor has been visited).
22-
bool ReachableBlocks::visit(SILFunction *f,
23-
function_ref<bool(SILBasicBlock *)> visitor) {
24-
assert(visited.empty() && "blocks already visited");
25-
22+
bool ReachableBlocks::visit(function_ref<bool(SILBasicBlock *)> visitor) {
2623
// Walk over the CFG, starting at the entry block, until all reachable blocks
2724
// are visited.
28-
SILBasicBlock *entryBB = f->getEntryBlock();
25+
SILBasicBlock *entryBB = visited.getFunction()->getEntryBlock();
2926
SmallVector<SILBasicBlock *, 8> worklist = {entryBB};
3027
visited.insert(entryBB);
3128
while (!worklist.empty()) {
@@ -34,7 +31,7 @@ bool ReachableBlocks::visit(SILFunction *f,
3431
return false;
3532

3633
for (auto &succ : bb->getSuccessors()) {
37-
if (visited.insert(succ).second)
34+
if (visited.insert(succ))
3835
worklist.push_back(succ);
3936
}
4037
}
@@ -71,9 +68,9 @@ void swift::removeDeadBlock(SILBasicBlock *bb) {
7168
}
7269

7370
bool swift::removeUnreachableBlocks(SILFunction &f) {
74-
ReachableBlocks reachable;
71+
ReachableBlocks reachable(&f);
7572
// Visit all the blocks without doing any extra work.
76-
reachable.visit(&f, [](SILBasicBlock *) { return true; });
73+
reachable.visit([](SILBasicBlock *) { return true; });
7774

7875
// Remove the blocks we never reached. Assume the entry block is visited.
7976
// Reachable's visited set contains dangling pointers during this loop.

0 commit comments

Comments
 (0)