summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/Utils/Local.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-11-27 07:43:12 +0000
committerChris Lattner <sabre@nondot.org>2008-11-27 07:43:12 +0000
commit99d6809ac1e5c158b4d74276fd452ca6cfc2a3d4 (patch)
tree161772dee2e555c2926284a7482d4fc6afbf4bad /llvm/lib/Transforms/Utils/Local.cpp
parent077eb6fcc2ac538676d5ac2444fe3cf967301164 (diff)
downloadbcm5719-llvm-99d6809ac1e5c158b4d74276fd452ca6cfc2a3d4.tar.gz
bcm5719-llvm-99d6809ac1e5c158b4d74276fd452ca6cfc2a3d4.zip
move MergeBasicBlockIntoOnlyPred to Transforms/Utils.
llvm-svn: 60162
Diffstat (limited to 'llvm/lib/Transforms/Utils/Local.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/Local.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index ed67c5eaaae..efb902267d9 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -200,3 +200,36 @@ bool llvm::dceInstruction(BasicBlock::iterator &BBI) {
}
return false;
}
+
+//===----------------------------------------------------------------------===//
+// Control Flow Graph Restructuring...
+//
+
+/// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its
+/// predecessor is known to have one successor (DestBB!). Eliminate the edge
+/// between them, moving the instructions in the predecessor into DestBB and
+/// deleting the predecessor block.
+///
+void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB) {
+ // If BB has single-entry PHI nodes, fold them.
+ while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
+ Value *NewVal = PN->getIncomingValue(0);
+ // Replace self referencing PHI with undef, it must be dead.
+ if (NewVal == PN) NewVal = UndefValue::get(PN->getType());
+ PN->replaceAllUsesWith(NewVal);
+ PN->eraseFromParent();
+ }
+
+ BasicBlock *PredBB = DestBB->getSinglePredecessor();
+ assert(PredBB && "Block doesn't have a single predecessor!");
+
+ // Splice all the instructions from PredBB to DestBB.
+ PredBB->getTerminator()->eraseFromParent();
+ DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList());
+
+ // Anything that branched to PredBB now branches to DestBB.
+ PredBB->replaceAllUsesWith(DestBB);
+
+ // Nuke BB.
+ PredBB->eraseFromParent();
+}
OpenPOWER on IntegriCloud