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

Evgeniy Stepanov eugenis at google.com
Wed Dec 12 01:40:19 PST 2012


  tests++

  Sorry, should have done it before.

Hi kcc, chandlerc,

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

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

Files:
  llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
  llvm/test/Instrumentation/MemorySanitizer/unreachable.ll

Index: llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
+++ llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
@@ -473,10 +473,46 @@
     DEBUG(dbgs() << "DONE:\n" << F);
   }
 
+  /// \brief Remove unreachable basic blocks from the function.
+  ///
+  /// 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.
+  void removeUnreachableBlocks() {
+    SmallPtrSet<BasicBlock*, 128> Reachable;
+    SmallVector<BasicBlock*, 128> Worklist;
+    Worklist.push_back(&F.getEntryBlock());
+    do {
+      BasicBlock *BB = Worklist.pop_back_val();
+      if (!Reachable.insert(BB))
+        continue;
+      for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
+        Worklist.push_back(*SI);
+    } while (!Worklist.empty());
+
+    if (Reachable.size() == F.size())
+      return;
+
+    assert(Reachable.size() < F.size());
+    for (Function::iterator BB = ++F.begin(); BB != F.end();)
+      if (!Reachable.count(BB)) {
+        for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
+          if (Reachable.count(*SI))
+            (*SI)->removePredecessor(BB);
+        BB->dropAllReferences();
+        BB = F.getBasicBlockList().erase(BB);
+      } else
+        ++BB;
+  }
+
   /// \brief Add MemorySanitizer instrumentation to a function.
   bool runOnFunction() {
     MS.initializeCallbacks(*F.getParent());
     if (!MS.TD) return false;
+
+    removeUnreachableBlocks();
+
     // 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: llvm/test/Instrumentation/MemorySanitizer/unreachable.ll
===================================================================
--- /dev/null
+++ llvm/test/Instrumentation/MemorySanitizer/unreachable.ll
@@ -0,0 +1,23 @@
+; RUN: opt < %s -msan -S | FileCheck %s
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+
+; Test that MemorySanitizer correctly handles unreachable blocks.
+
+define i32 @Func(i32* %p) nounwind uwtable {
+entry:
+  br label %exit
+
+unreachable:
+  %x = load i32* %p
+  br label %exit
+
+exit:
+  %z = phi i32 [ 42, %entry ], [ %x, %unreachable ]
+  ret i32 %z
+}
+
+; CHECK: @Func
+; CHECK: store i32 0, {{.*}} @__msan_retval_tls
+; CHECK: ret i32 42
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D208.2.patch
Type: text/x-patch
Size: 2863 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20121212/4c3f2067/attachment.bin>


More information about the llvm-commits mailing list