Skip to content

Commit 167b052

Browse files
committed
[Local] Simplify function removeUnreachableBlocks() to avoid (re-)computation.
Two small changes in llvm::removeUnreachableBlocks() to avoid unnecessary (re-)computation. First, replace the use of count() with find(), which has better time complexity. Second, because we have already computed the set of dead blocks, replace the second loop over all basic blocks to a loop only over the already computed dead blocks. This simplifies the loop and avoids recomputation. Patch by Rodrigo Caetano Rocha <[email protected]> Reviewers: efriedma, spatel, fhahn, xbolva00 Reviewed By: fhahn, xbolva00 Differential Revision: https://reviews.llvm.org/D68191 llvm-svn: 373429
1 parent 1c57143 commit 167b052

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,22 +2215,22 @@ void llvm::removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU) {
22152215
bool llvm::removeUnreachableBlocks(Function &F, LazyValueInfo *LVI,
22162216
DomTreeUpdater *DTU,
22172217
MemorySSAUpdater *MSSAU) {
2218-
SmallPtrSet<BasicBlock*, 16> Reachable;
2218+
SmallPtrSet<BasicBlock *, 16> Reachable;
22192219
bool Changed = markAliveBlocks(F, Reachable, DTU);
22202220

22212221
// If there are unreachable blocks in the CFG...
22222222
if (Reachable.size() == F.size())
22232223
return Changed;
22242224

22252225
assert(Reachable.size() < F.size());
2226-
NumRemoved += F.size()-Reachable.size();
2226+
NumRemoved += F.size() - Reachable.size();
22272227

22282228
SmallSetVector<BasicBlock *, 8> DeadBlockSet;
2229-
for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ++I) {
2230-
auto *BB = &*I;
2231-
if (Reachable.count(BB))
2229+
for (BasicBlock &BB : F) {
2230+
// Skip reachable basic blocks
2231+
if (Reachable.find(&BB) != Reachable.end())
22322232
continue;
2233-
DeadBlockSet.insert(BB);
2233+
DeadBlockSet.insert(&BB);
22342234
}
22352235

22362236
if (MSSAU)
@@ -2249,23 +2249,13 @@ bool llvm::removeUnreachableBlocks(Function &F, LazyValueInfo *LVI,
22492249
if (LVI)
22502250
LVI->eraseBlock(BB);
22512251
BB->dropAllReferences();
2252-
}
2253-
for (Function::iterator I = ++F.begin(); I != F.end();) {
2254-
auto *BB = &*I;
2255-
if (Reachable.count(BB)) {
2256-
++I;
2257-
continue;
2258-
}
22592252
if (DTU) {
22602253
// Remove the terminator of BB to clear the successor list of BB.
22612254
if (BB->getTerminator())
22622255
BB->getInstList().pop_back();
22632256
new UnreachableInst(BB->getContext(), BB);
22642257
assert(succ_empty(BB) && "The successor list of BB isn't empty before "
22652258
"applying corresponding DTU updates.");
2266-
++I;
2267-
} else {
2268-
I = F.getBasicBlockList().erase(I);
22692259
}
22702260
}
22712261

@@ -2281,7 +2271,11 @@ bool llvm::removeUnreachableBlocks(Function &F, LazyValueInfo *LVI,
22812271
}
22822272
if (!Deleted)
22832273
return false;
2274+
} else {
2275+
for (auto *BB : DeadBlockSet)
2276+
BB->eraseFromParent();
22842277
}
2278+
22852279
return true;
22862280
}
22872281

0 commit comments

Comments
 (0)