[llvm-commits] [PATCH] [msan] Remove unreachable bb's
Evgeniy Stepanov
eugenis at google.com
Wed Dec 19 00:42:53 PST 2012
Changed the return type to bool - this seems to be commonly used for this type of functions.
OK to commit?
Hi kcc, chandlerc,
http://llvm-reviews.chandlerc.com/D208
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D208?vs=530&id=543#toc
Files:
include/llvm/Transforms/Utils/Local.h
lib/Transforms/Utils/Local.cpp
lib/Transforms/Instrumentation/MemorySanitizer.cpp
test/Instrumentation/MemorySanitizer/unreachable.ll
Index: include/llvm/Transforms/Utils/Local.h
===================================================================
--- include/llvm/Transforms/Utils/Local.h
+++ include/llvm/Transforms/Utils/Local.h
@@ -257,6 +257,12 @@
bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
DIBuilder &Builder);
+/// \\brief - Remove all blocks that can not be reached from the function's
+/// entry.
+///
+/// Returns true if any basic block was removed.
+bool removeUnreachableBlocks(Function &F);
+
} // End llvm namespace
#endif
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,46 @@
DDI->eraseFromParent();
return true;
}
+
+/// RemoveUnreachableBlocks - Remove all blocks that can not be reached from the
+/// function's entry.
+bool 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 false;
+
+ 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();
+ }
+ return true;
+}
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: test/Instrumentation/MemorySanitizer/unreachable.ll
===================================================================
--- test/Instrumentation/MemorySanitizer/unreachable.ll
+++ 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.5.patch
Type: text/x-patch
Size: 4565 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20121219/5570a15e/attachment.bin>
More information about the llvm-commits
mailing list