- Rename AnalysisUsage::preservesAll to getPreservesAll & preservesCFG to
[oota-llvm.git] / lib / Transforms / Utils / SimplifyCFG.cpp
index 426d18ec9ff6fb19ced92da947fb701d09230822..6b642360d6cf0c841e14c04d9c9ba6b226d20d78 100644 (file)
@@ -1,13 +1,6 @@
 //===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===//
 //
-// SimplifyCFG - This function is used to do simplification of a CFG.  For
-// example, it adjusts branches to branches to eliminate the extra hop, it
-// eliminates unreachable basic blocks, and does other "peephole" optimization
-// of the CFG.  It returns true if a modification was made, and returns an 
-// iterator that designates the first element remaining after the block that
-// was deleted.
-//
-// WARNING:  The entry node of a function may not be simplified.
+// Peephole optimize the CFG.
 //
 //===----------------------------------------------------------------------===//
 
@@ -28,7 +21,9 @@
 //
 static bool PropogatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
   assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
-  assert(isa<PHINode>(Succ->front()) && "Only works on PHId BBs!");
+
+  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
@@ -39,15 +34,14 @@ static bool PropogatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
   // Succ.  If so, we cannot do the transformation!
   //
   for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
-       PI != PE; ++PI) {
+       PI != PE; ++PI)
     if (find(BBPreds.begin(), BBPreds.end(), *PI) != BBPreds.end())
       return true;
-  }
 
   // Loop over all of the PHI nodes in the successor BB
   for (BasicBlock::iterator I = Succ->begin();
        PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
-    Value *OldVal = PN->removeIncomingValue(BB);
+    Value *OldVal = PN->removeIncomingValue(BB, false);
     assert(OldVal && "No entry in PHI for Pred BB!");
 
     for (std::vector<BasicBlock*>::const_iterator PredI = BBPreds.begin(), 
@@ -118,11 +112,8 @@ bool SimplifyCFG(BasicBlock *BB) {
         // Be careful though, if this transformation fails (returns true) then
         // we cannot do this transformation!
         //
-       if (!isa<PHINode>(Succ->front()) ||
-            !PropogatePredecessorsForPHIs(BB, Succ)) {
-
+       if (!PropogatePredecessorsForPHIs(BB, Succ)) {
           //cerr << "Killing Trivial BB: \n" << BB;
-
           BB->replaceAllUsesWith(Succ);
           std::string OldName = BB->getName();
 
@@ -143,7 +134,7 @@ bool SimplifyCFG(BasicBlock *BB) {
   // pred, and if there is only one distinct successor of the predecessor, and
   // if there are no PHI nodes.
   //
-  if (!isa<PHINode>(BB->front()) && !BB->hasConstantReferences()) {
+  if (!BB->hasConstantReferences()) {
     pred_iterator PI(pred_begin(BB)), PE(pred_end(BB));
     BasicBlock *OnlyPred = *PI++;
     for (; PI != PE; ++PI)  // Search all predecessors, see if they are all same
@@ -168,6 +159,17 @@ bool SimplifyCFG(BasicBlock *BB) {
       //cerr << "Merging: " << BB << "into: " << OnlyPred;
       TerminatorInst *Term = OnlyPred->getTerminator();
 
+      // Resolve any PHI nodes at the start of the block.  They are all
+      // guaranteed to have exactly one entry if they exist, unless there are
+      // multiple duplicate (but guaranteed to be equal) entries for the
+      // incoming edges.  This occurs when there are multiple edges from
+      // OnlyPred to OnlySucc.
+      //
+      while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
+        PN->replaceAllUsesWith(PN->getIncomingValue(0));
+        BB->getInstList().pop_front();  // Delete the phi node...
+      }
+
       // Delete the unconditional branch from the predecessor...
       OnlyPred->getInstList().pop_back();