summaryrefslogtreecommitdiff
path: root/lib/Transforms/Utils/SimplifyCFG.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2005-08-03 00:29:26 +0000
committerChris Lattner <sabre@nondot.org>2005-08-03 00:29:26 +0000
commit3b3efc77972e654c472cee8a3075e74a7d5f0cc8 (patch)
tree344e44d70d66e34edbec7e4bd7784657318281ea /lib/Transforms/Utils/SimplifyCFG.cpp
parentd423b8b6ca3d2d21a8aa07a877d63e5dc45abc70 (diff)
downloadllvm-3b3efc77972e654c472cee8a3075e74a7d5f0cc8.tar.gz
llvm-3b3efc77972e654c472cee8a3075e74a7d5f0cc8.tar.bz2
llvm-3b3efc77972e654c472cee8a3075e74a7d5f0cc8.tar.xz
Refactor code out of PropagatePredecessorsForPHIs, turning it into a pure function with no side-effects
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@22612 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Utils/SimplifyCFG.cpp')
-rw-r--r--lib/Transforms/Utils/SimplifyCFG.cpp73
1 files changed, 36 insertions, 37 deletions
diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp
index 3eaae8eacc..abe229d7ed 100644
--- a/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -67,55 +67,25 @@ static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
}
}
-// PropagatePredecessorsForPHIs - This gets "Succ" ready to have the
-// predecessors from "BB". This is a little tricky because "Succ" has PHI
-// nodes, which need to have extra slots added to them to hold the merge edges
-// from BB's predecessors, and BB itself might have had PHI nodes in it. This
-// function returns true (failure) if the Succ BB already has a predecessor that
-// is a predecessor of BB and incoming PHI arguments would not be discernible.
+// CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an
+// almost-empty BB ending in an unconditional branch to Succ, into succ.
//
// Assumption: Succ is the single successor for BB.
//
-static bool PropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
+static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
if (!isa<PHINode>(Succ->front()))
- return false; // We can make the transformation, no problem.
-
- // If there is more than one predecessor, and there are PHI nodes in
- // the successor, then we need to add incoming edges for the PHI nodes
- //
- const std::vector<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB));
+ return true; // We can make the transformation, no problem.
// Check to see if one of the predecessors of BB is already a predecessor of
// Succ. If so, we cannot do the transformation if there are any PHI nodes
// with incompatible values coming in from the two edges!
//
if (!SafeToMergeTerminators(BB->getTerminator(), Succ->getTerminator()))
- return true; // Cannot merge.
+ return false; // Cannot merge.
- // Loop over all of the PHI nodes in the successor BB.
- for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
- PHINode *PN = cast<PHINode>(I);
- Value *OldVal = PN->removeIncomingValue(BB, false);
- assert(OldVal && "No entry in PHI for Pred BB!");
-
- // If this incoming value is one of the PHI nodes in BB, the new entries in
- // the PHI node are the entries from the old PHI.
- if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
- PHINode *OldValPN = cast<PHINode>(OldVal);
- for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i)
- PN->addIncoming(OldValPN->getIncomingValue(i),
- OldValPN->getIncomingBlock(i));
- } else {
- for (std::vector<BasicBlock*>::const_iterator PredI = BBPreds.begin(),
- End = BBPreds.end(); PredI != End; ++PredI) {
- // Add an incoming value for each of the new incoming values...
- PN->addIncoming(OldVal, *PredI);
- }
- }
- }
- return false;
+ return true;
}
/// TryToSimplifyUncondBranchFromEmptyBlock - BB contains an unconditional
@@ -128,10 +98,39 @@ static bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
// if this transformation fails (returns true) then we cannot do this
// transformation!
//
- if (PropagatePredecessorsForPHIs(BB, Succ)) return false;
+ if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false;
DEBUG(std::cerr << "Killing Trivial BB: \n" << *BB);
+ if (isa<PHINode>(Succ->begin())) {
+ // If there is more than one pred of succ, and there are PHI nodes in
+ // the successor, then we need to add incoming edges for the PHI nodes
+ //
+ const std::vector<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB));
+
+ // Loop over all of the PHI nodes in the successor of BB.
+ for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
+ PHINode *PN = cast<PHINode>(I);
+ Value *OldVal = PN->removeIncomingValue(BB, false);
+ assert(OldVal && "No entry in PHI for Pred BB!");
+
+ // If this incoming value is one of the PHI nodes in BB, the new entries in
+ // the PHI node are the entries from the old PHI.
+ if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
+ PHINode *OldValPN = cast<PHINode>(OldVal);
+ for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i)
+ PN->addIncoming(OldValPN->getIncomingValue(i),
+ OldValPN->getIncomingBlock(i));
+ } else {
+ for (std::vector<BasicBlock*>::const_iterator PredI = BBPreds.begin(),
+ End = BBPreds.end(); PredI != End; ++PredI) {
+ // Add an incoming value for each of the new incoming values...
+ PN->addIncoming(OldVal, *PredI);
+ }
+ }
+ }
+ }
+
if (isa<PHINode>(&BB->front())) {
std::vector<BasicBlock*>
OldSuccPreds(pred_begin(Succ), pred_end(Succ));