[llvm-commits] [PATCH] [msan] Remove unreachable bb's
Evgeniy Stepanov
eugenis at google.com
Fri Dec 14 04:29:19 PST 2012
I've moved the code to BasicBlockUtils, in case we want to reuse it.
Hi kcc, chandlerc,
http://llvm-reviews.chandlerc.com/D208
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D208?vs=504&id=523#toc
Files:
include/llvm/Transforms/Utils/BasicBlockUtils.h
lib/Transforms/Instrumentation/MemorySanitizer.cpp
lib/Transforms/Scalar/SimplifyCFGPass.cpp
lib/Transforms/Utils/BasicBlockUtils.cpp
Index: include/llvm/Transforms/Utils/BasicBlockUtils.h
===================================================================
--- include/llvm/Transforms/Utils/BasicBlockUtils.h
+++ include/llvm/Transforms/Utils/BasicBlockUtils.h
@@ -228,6 +228,10 @@
TerminatorInst *SplitBlockAndInsertIfThen(Instruction *Cmp,
bool Unreachable, MDNode *BranchWeights = 0);
+/// RemoveUnreachableBlocks - Remove all blocks that can not be reached from the
+/// function's entry.
+void RemoveUnreachableBlocks(Function &F);
+
} // End llvm namespace
#endif
Index: lib/Transforms/Instrumentation/MemorySanitizer.cpp
===================================================================
--- lib/Transforms/Instrumentation/MemorySanitizer.cpp
+++ lib/Transforms/Instrumentation/MemorySanitizer.cpp
@@ -477,6 +477,9 @@
bool runOnFunction() {
MS.initializeCallbacks(*F.getParent());
if (!MS.TD) return false;
+
+ 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/Scalar/SimplifyCFGPass.cpp
===================================================================
--- lib/Transforms/Scalar/SimplifyCFGPass.cpp
+++ lib/Transforms/Scalar/SimplifyCFGPass.cpp
@@ -111,13 +111,11 @@
SmallVector<BasicBlock*, 128> Worklist;
Worklist.push_back(BB);
+ Reachable.insert(BB);
bool Changed = false;
do {
BB = Worklist.pop_back_val();
- if (!Reachable.insert(BB))
- continue;
-
// Do a quick scan of the basic block, turning any obviously unreachable
// instructions into LLVM unreachable insts. The instruction combining pass
// canonicalizes unreachable insts into stores to null or undef.
@@ -176,7 +174,8 @@
Changed |= ConstantFoldTerminator(BB, true);
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
- Worklist.push_back(*SI);
+ if (Reachable.insert(*SI))
+ Worklist.push_back(*SI);
} while (!Worklist.empty());
return Changed;
}
Index: lib/Transforms/Utils/BasicBlockUtils.cpp
===================================================================
--- lib/Transforms/Utils/BasicBlockUtils.cpp
+++ lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -726,3 +726,30 @@
ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
return CheckTerm;
}
+
+/// 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 = ++F.begin(); BB != F.end();)
+ if (!Reachable.count(BB)) {
+ if (!BB->use_empty())
+ BB->replaceAllUsesWith(UndefValue::get(BB->getType()));
+ DeleteDeadBlock(BB++);
+ } else
+ ++BB;
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D208.3.patch
Type: text/x-patch
Size: 3339 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20121214/972cda29/attachment.bin>
More information about the llvm-commits
mailing list