Skip to content

Commit 3e6fe70

Browse files
committed
SIL: use BasicBlockSet instead of SmallPtrSet in findJointPostDominatingSet
This allows putting the set as local variables into the function itself (where they are used) and removing them from JointPostDominanceSetComputer.
1 parent 2f890dc commit 3e6fe70

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

include/swift/SIL/BasicBlockUtils.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SWIFT_SIL_BASICBLOCKUTILS_H
1515

1616
#include "swift/SIL/SILValue.h"
17+
#include "swift/SIL/SILBitfield.h"
1718
#include "llvm/ADT/SetVector.h"
1819
#include "llvm/ADT/SmallPtrSet.h"
1920
#include "llvm/ADT/SmallVector.h"
@@ -98,13 +99,6 @@ struct JointPostDominanceSetComputer {
9899
/// The worklist that drives the algorithm.
99100
SmallVector<SILBasicBlock *, 32> worklist;
100101

101-
/// A set that guards our worklist. Any block before it is added to worklist
102-
/// should be checked against visitedBlocks.
103-
SmallPtrSet<SILBasicBlock *, 32> visitedBlocks;
104-
105-
/// The set of blocks where we begin our walk.
106-
SmallPtrSet<SILBasicBlock *, 8> initialBlocks;
107-
108102
/// A subset of our initial blocks that we found as a predecessor of another
109103
/// block along our walk.
110104
SmallVector<SILBasicBlock *, 8> reachableInputBlocks;
@@ -113,7 +107,7 @@ struct JointPostDominanceSetComputer {
113107
/// visited yet are placed in here. At the end of our worklist, any blocks
114108
/// that remain here are "leaking blocks" that together with our initial set
115109
/// would provide a jointly-postdominating set of our dominating value.
116-
SmallSetVector<SILBasicBlock *, 8> blocksThatLeakIfNeverVisited;
110+
SmallVector<SILBasicBlock *, 32> blocksThatLeakIfNeverVisited;
117111

118112
DeadEndBlocks &deadEndBlocks;
119113

@@ -122,8 +116,6 @@ struct JointPostDominanceSetComputer {
122116

123117
void clear() {
124118
worklist.clear();
125-
visitedBlocks.clear();
126-
initialBlocks.clear();
127119
reachableInputBlocks.clear();
128120
blocksThatLeakIfNeverVisited.clear();
129121
}

lib/SIL/Utils/BasicBlockUtils.cpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,17 @@ void JointPostDominanceSetComputer::findJointPostDominatingSet(
416416
// always clean up any resources that we use!
417417
SWIFT_DEFER { clear(); };
418418

419+
/// A set that guards our worklist. Any block before it is added to worklist
420+
/// should be checked against visitedBlocks.
421+
SILFunction *function = dominatingBlock->getParent();
422+
BasicBlockSet visitedBlocks(function);
423+
424+
/// The set of blocks where we begin our walk.
425+
BasicBlockSet initialBlocks(function);
426+
427+
/// True for blocks which are in blocksThatLeakIfNeverVisited.
428+
BasicBlockFlag isLeakingBlock(function);
429+
419430
// Otherwise, we need to compute our joint post dominating set. We do this by
420431
// performing a backwards walk up the CFG tracking back liveness until we find
421432
// our dominating block. As we walk up, we keep track of any successor blocks
@@ -429,7 +440,7 @@ void JointPostDominanceSetComputer::findJointPostDominatingSet(
429440

430441
// We require dominatedBlockSet to be a set and thus assert if we hit it to
431442
// flag user error to our caller.
432-
bool succeededInserting = visitedBlocks.insert(block).second;
443+
bool succeededInserting = visitedBlocks.insert(block);
433444
(void)succeededInserting;
434445
assert(succeededInserting &&
435446
"Repeat Elt: dominatedBlockSet should be a set?!");
@@ -441,19 +452,16 @@ void JointPostDominanceSetComputer::findJointPostDominatingSet(
441452
while (!worklist.empty()) {
442453
auto *block = worklist.pop_back_val();
443454

444-
// First remove block from blocksThatLeakIfNeverVisited if it is there since
445-
// we know that it isn't leaking since we are visiting it now.
446-
blocksThatLeakIfNeverVisited.remove(block);
447-
448455
// Then if our block is not one of our initial blocks, add the block's
449456
// successors to blocksThatLeakIfNeverVisited.
450-
if (!initialBlocks.count(block)) {
457+
if (!initialBlocks.contains(block)) {
451458
for (auto *succBlock : block->getSuccessorBlocks()) {
452-
if (visitedBlocks.count(succBlock))
459+
if (visitedBlocks.contains(succBlock))
453460
continue;
454461
if (deadEndBlocks.isDeadEnd(succBlock))
455462
continue;
456-
blocksThatLeakIfNeverVisited.insert(succBlock);
463+
blocksThatLeakIfNeverVisited.push_back(succBlock);
464+
isLeakingBlock.set(succBlock);
457465
}
458466
}
459467

@@ -465,25 +473,28 @@ void JointPostDominanceSetComputer::findJointPostDominatingSet(
465473
// our initial blocks (signaling a loop) and then add it to the worklist if
466474
// we haven't visited it already.
467475
for (auto *predBlock : block->getPredecessorBlocks()) {
468-
if (initialBlocks.count(predBlock)) {
476+
if (initialBlocks.contains(predBlock)) {
469477
reachableInputBlocks.push_back(predBlock);
470478
for (auto *succBlock : predBlock->getSuccessorBlocks()) {
471-
if (visitedBlocks.count(succBlock))
479+
if (visitedBlocks.contains(succBlock))
472480
continue;
473481
if (deadEndBlocks.isDeadEnd(succBlock))
474482
continue;
475-
blocksThatLeakIfNeverVisited.insert(succBlock);
483+
if (!isLeakingBlock.testAndSet(succBlock))
484+
blocksThatLeakIfNeverVisited.push_back(succBlock);
476485
}
477486
}
478-
if (visitedBlocks.insert(predBlock).second)
487+
if (visitedBlocks.insert(predBlock))
479488
worklist.push_back(predBlock);
480489
}
481490
}
482491

483-
// After our worklist has emptied, any blocks left in
492+
// After our worklist has emptied, any not visited blocks in
484493
// blocksThatLeakIfNeverVisited are "leaking blocks".
485-
for (auto *leakingBlock : blocksThatLeakIfNeverVisited)
486-
foundJointPostDomSetCompletionBlocks(leakingBlock);
494+
for (auto *leakingBlock : blocksThatLeakIfNeverVisited) {
495+
if (!visitedBlocks.contains(leakingBlock))
496+
foundJointPostDomSetCompletionBlocks(leakingBlock);
497+
}
487498

488499
// Then unique our list of reachable input blocks and pass them to our
489500
// callback.

0 commit comments

Comments
 (0)