Skip to content

Commit 9e9f7b2

Browse files
committed
[NFC] CLF: Compute reachable blocks only.
Don't form a set of unreachable blocks, we only need to check whether any given block is unreachable, which can be done via `ReachableBlocks::isVisited`.
1 parent add0c5a commit 9e9f7b2

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

lib/SILOptimizer/Mandatory/ClosureLifetimeFixup.cpp

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,7 @@ static SILValue tryRewriteToPartialApplyStack(
587587
ConvertEscapeToNoEscapeInst *cvt, SILInstruction *closureUser,
588588
DominanceAnalysis *dominanceAnalysis, InstructionDeleter &deleter,
589589
llvm::DenseMap<SILInstruction *, SILInstruction *> &memoized,
590-
llvm::DenseSet<SILBasicBlock *> &unreachableBlocks,
591-
const bool &modifiedCFG) {
590+
ReachableBlocks const &reachableBlocks, const bool &modifiedCFG) {
592591

593592
auto *origPA = dyn_cast<PartialApplyInst>(skipConvert(cvt->getOperand()));
594593
if (!origPA)
@@ -973,7 +972,7 @@ static SILValue tryRewriteToPartialApplyStack(
973972
// Don't run insertDeallocOfCapturedArguments if newPA is in an unreachable
974973
// block insertDeallocOfCapturedArguments will run code that computes the DF
975974
// for newPA that will loop infinitely.
976-
if (unreachableBlocks.count(newPA->getParent()))
975+
if (!reachableBlocks.isReachable(newPA->getParent()))
977976
return closureOp;
978977

979978
auto getAddressToDealloc = [&](SILValue argAddress) -> SILValue {
@@ -995,8 +994,8 @@ static bool tryExtendLifetimeToLastUse(
995994
ConvertEscapeToNoEscapeInst *cvt, DominanceAnalysis *dominanceAnalysis,
996995
DeadEndBlocksAnalysis *deadEndBlocksAnalysis,
997996
llvm::DenseMap<SILInstruction *, SILInstruction *> &memoized,
998-
llvm::DenseSet<SILBasicBlock *> &unreachableBlocks,
999-
InstructionDeleter &deleter, const bool &modifiedCFG) {
997+
ReachableBlocks const &reachableBlocks, InstructionDeleter &deleter,
998+
const bool &modifiedCFG) {
1000999
// If there is a single user, this is simple: extend the
10011000
// lifetime of the operand until the use ends.
10021001
auto *singleUser = lookThroughRebastractionUsers(cvt, memoized);
@@ -1019,7 +1018,7 @@ static bool tryExtendLifetimeToLastUse(
10191018

10201019
if (SILValue closureOp = tryRewriteToPartialApplyStack(
10211020
cvt, singleUser, dominanceAnalysis, deleter, memoized,
1022-
unreachableBlocks, /*const*/ modifiedCFG)) {
1021+
reachableBlocks, /*const*/ modifiedCFG)) {
10231022
if (endAsyncLet) {
10241023
// Add the closure as a second operand to the endAsyncLet builtin.
10251024
// This ensures that the closure arguments are kept alive until the
@@ -1428,22 +1427,6 @@ static bool fixupCopyBlockWithoutEscaping(CopyBlockWithoutEscapingInst *cb,
14281427
return true;
14291428
}
14301429

1431-
static void computeUnreachableBlocks(
1432-
llvm::DenseSet<SILBasicBlock*> &unreachableBlocks,
1433-
SILFunction &fn) {
1434-
1435-
ReachableBlocks isReachable(&fn);
1436-
llvm::DenseSet<SILBasicBlock *> reachable;
1437-
isReachable.visit([&] (SILBasicBlock *block) -> bool {
1438-
reachable.insert(block);
1439-
return true;
1440-
});
1441-
for (auto &block : fn) {
1442-
if (!reachable.count(&block))
1443-
unreachableBlocks.insert(&block);
1444-
}
1445-
}
1446-
14471430
static bool fixupClosureLifetimes(SILFunction &fn,
14481431
DominanceAnalysis *dominanceAnalysis,
14491432
DeadEndBlocksAnalysis *deadEndBlocksAnalysis,
@@ -1454,8 +1437,8 @@ static bool fixupClosureLifetimes(SILFunction &fn,
14541437
// queries.
14551438
llvm::DenseMap<SILInstruction *, SILInstruction *> memoizedQueries;
14561439

1457-
llvm::DenseSet<SILBasicBlock *> unreachableBlocks;
1458-
computeUnreachableBlocks(unreachableBlocks, fn);
1440+
ReachableBlocks reachableBlocks(&fn);
1441+
reachableBlocks.compute();
14591442

14601443
for (auto &block : fn) {
14611444
SILSSAUpdater updater;
@@ -1485,7 +1468,7 @@ static bool fixupClosureLifetimes(SILFunction &fn,
14851468

14861469
if (tryExtendLifetimeToLastUse(cvt, dominanceAnalysis,
14871470
deadEndBlocksAnalysis, memoizedQueries,
1488-
unreachableBlocks, updater.getDeleter(),
1471+
reachableBlocks, updater.getDeleter(),
14891472
/*const*/ modifiedCFG)) {
14901473
changed = true;
14911474
checkStackNesting = true;

0 commit comments

Comments
 (0)