[PATCH] D68191: Simplify function llvm::removeUnreachableBlocks() to avoid (re-)computation.

Rodrigo Caetano Rocha via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 29 04:06:49 PDT 2019


rcorcs created this revision.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

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.


Repository:
  rL LLVM

https://reviews.llvm.org/D68191

Files:
  llvm/lib/Transforms/Utils/Local.cpp


Index: llvm/lib/Transforms/Utils/Local.cpp
===================================================================
--- llvm/lib/Transforms/Utils/Local.cpp
+++ llvm/lib/Transforms/Utils/Local.cpp
@@ -2228,7 +2228,8 @@
   SmallSetVector<BasicBlock *, 8> DeadBlockSet;
   for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ++I) {
     auto *BB = &*I;
-    if (Reachable.count(BB))
+    //skip reachable basic blocks
+    if (Reachable.find(BB)!=Reachable.end())
       continue;
     DeadBlockSet.insert(BB);
   }
@@ -2250,12 +2251,8 @@
       LVI->eraseBlock(BB);
     BB->dropAllReferences();
   }
-  for (Function::iterator I = ++F.begin(); I != F.end();) {
-    auto *BB = &*I;
-    if (Reachable.count(BB)) {
-      ++I;
-      continue;
-    }
+
+  for (auto *BB : DeadBlockSet) {
     if (DTU) {
       // Remove the terminator of BB to clear the successor list of BB.
       if (BB->getTerminator())
@@ -2263,9 +2260,8 @@
       new UnreachableInst(BB->getContext(), BB);
       assert(succ_empty(BB) && "The successor list of BB isn't empty before "
                                "applying corresponding DTU updates.");
-      ++I;
     } else {
-      I = F.getBasicBlockList().erase(I);
+      BB->eraseFromParent();
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68191.222318.patch
Type: text/x-patch
Size: 1248 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190929/9dbb44f7/attachment.bin>


More information about the llvm-commits mailing list