aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-12-03 06:37:44 +0000
committerChris Lattner <sabre@nondot.org>2008-12-03 06:37:44 +0000
commitbcc904a67c2f77b6a619d8ec9370af02e3a5ce02 (patch)
treef2273d7c1ea04649b2e3747c44c1cefad576271d /llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
parent6a113d9ddb4a4a2d19b4796e3b0f53aab7ec2d5b (diff)
downloadllvm-bcc904a67c2f77b6a619d8ec9370af02e3a5ce02.zip
llvm-bcc904a67c2f77b6a619d8ec9370af02e3a5ce02.tar.gz
llvm-bcc904a67c2f77b6a619d8ec9370af02e3a5ce02.tar.bz2
Factor some code out of SimplifyCFG, forming a new
DeleteBlockIfDead method. llvm-svn: 60463
Diffstat (limited to 'llvm/lib/Transforms/Utils/BasicBlockUtils.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/BasicBlockUtils.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
index bd32a99..bfcf375 100644
--- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -24,6 +24,39 @@
#include <algorithm>
using namespace llvm;
+/// DeleteBlockIfDead - If the specified basic block is trivially dead (has no
+/// predecessors and not the entry block), delete it and return true. Otherwise
+/// return false.
+bool llvm::DeleteBlockIfDead(BasicBlock *BB) {
+ if (pred_begin(BB) != pred_end(BB) ||
+ BB == &BB->getParent()->getEntryBlock())
+ return false;
+
+ TerminatorInst *BBTerm = BB->getTerminator();
+
+ // Loop through all of our successors and make sure they know that one
+ // of their predecessors is going away.
+ for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i)
+ BBTerm->getSuccessor(i)->removePredecessor(BB);
+
+ // Zap all the instructions in the block.
+ while (!BB->empty()) {
+ Instruction &I = BB->back();
+ // If this instruction is used, replace uses with an arbitrary value.
+ // Because control flow can't get here, we don't care what we replace the
+ // value with. Note that since this block is unreachable, and all values
+ // contained within it must dominate their uses, that all uses will
+ // eventually be removed (they are themselves dead).
+ if (!I.use_empty())
+ I.replaceAllUsesWith(UndefValue::get(I.getType()));
+ BB->getInstList().pop_back();
+ }
+
+ // Zap the block!
+ BB->eraseFromParent();
+ return true;
+}
+
/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
/// if possible. The return value indicates success or failure.
bool llvm::MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P) {