[llvm-commits] [PATCH] [msan] Remove unreachable bb's

Evgeniy Stepanov eugenis at google.com
Mon Dec 17 07:25:15 PST 2012


  PTAL.
  The new removeUnreachableBlocks (it's second part) looks almost entirely like DeleteDeadBlock - the only difference is it does not removePredecessor from other unreachable blocks.

Hi kcc, chandlerc,

http://llvm-reviews.chandlerc.com/D208

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D208?vs=523&id=530#toc

Files:
  lib/Transforms/Instrumentation/MemorySanitizer.cpp
  lib/Transforms/Utils/Local.cpp
  include/llvm/Transforms/Utils/Local.h

Index: lib/Transforms/Instrumentation/MemorySanitizer.cpp
===================================================================
--- lib/Transforms/Instrumentation/MemorySanitizer.cpp
+++ lib/Transforms/Instrumentation/MemorySanitizer.cpp
@@ -67,6 +67,7 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
+#include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Transforms/Utils/ModuleUtils.h"
 #include "llvm/Type.h"
 
@@ -478,6 +479,13 @@
   bool runOnFunction() {
     MS.initializeCallbacks(*F.getParent());
     if (!MS.TD) return false;
+
+    // In the presence of unreachable blocks, we may see Phi nodes with
+    // incoming nodes from such blocks. Since InstVisitor skips unreachable
+    // blocks, such nodes will not have any shadow value associated with them.
+    // It's easier to remove unreachable blocks than deal with missing shadow.
+    removeUnreachableBlocks(F);
+
     // Iterate all BBs in depth-first order and create shadow instructions
     // for all instructions (where applicable).
     // For PHI nodes we create dummy shadow PHIs which will be finalized later.
Index: lib/Transforms/Utils/Local.cpp
===================================================================
--- lib/Transforms/Utils/Local.cpp
+++ lib/Transforms/Utils/Local.cpp
@@ -15,6 +15,7 @@
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Analysis/Dominators.h"
 #include "llvm/Analysis/InstructionSimplify.h"
 #include "llvm/Analysis/MemoryBuiltins.h"
@@ -963,3 +964,45 @@
   DDI->eraseFromParent();
   return true;
 }
+
+/// RemoveUnreachableBlocks - Remove all blocks that can not be reached from the
+/// function's entry.
+void llvm::removeUnreachableBlocks(Function &F) {
+  SmallPtrSet<BasicBlock*, 128> Reachable;
+  SmallVector<BasicBlock*, 128> Worklist;
+  Worklist.push_back(&F.getEntryBlock());
+  Reachable.insert(&F.getEntryBlock());
+  do {
+    BasicBlock *BB = Worklist.pop_back_val();
+    for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
+      if (Reachable.insert(*SI))
+        Worklist.push_back(*SI);
+  } while (!Worklist.empty());
+
+  if (Reachable.size() == F.size())
+    return;
+
+  assert(Reachable.size() < F.size());
+  for (Function::iterator BB = llvm::next(F.begin()), E = F.end();
+       BB != E; ++BB) {
+    if (Reachable.count(BB))
+      continue;
+
+    // Remove BB as predecessor of all its reachable successors.
+    // Unreachable successors don't matter as they'll soon be removed, too.
+    for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
+      if (Reachable.count(*SI))
+        (*SI)->removePredecessor(BB);
+
+    // Zap all instructions in BB.
+    while (!BB->empty()) {
+      Instruction &I = BB->back();
+      if (!I.use_empty())
+        I.replaceAllUsesWith(UndefValue::get(I.getType()));
+      BB->getInstList().pop_back();
+    }
+
+    --BB;
+    llvm::next(BB)->eraseFromParent();
+  }
+}
Index: include/llvm/Transforms/Utils/Local.h
===================================================================
--- include/llvm/Transforms/Utils/Local.h
+++ include/llvm/Transforms/Utils/Local.h
@@ -257,6 +257,10 @@
 bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
                                 DIBuilder &Builder);
 
+/// \\brief - Remove all blocks that can not be reached from the function's
+/// entry.
+void removeUnreachableBlocks(Function &F);
+
 } // End llvm namespace
 
 #endif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D208.4.patch
Type: text/x-patch
Size: 3611 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20121217/7070a4a0/attachment.bin>


More information about the llvm-commits mailing list