Remove attribution from file headers, per discussion on llvmdev.
[oota-llvm.git] / lib / Transforms / Utils / BreakCriticalEdges.cpp
index 5253b609b8b5a86498f18e1d6470e57782e8b57e..78801db4cd43eb33cf124fdc2173b52870bc7cd4 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -34,11 +34,13 @@ STATISTIC(NumBroken, "Number of blocks inserted");
 
 namespace {
   struct VISIBILITY_HIDDEN BreakCriticalEdges : public FunctionPass {
+    static char ID; // Pass identification, replacement for typeid
+    BreakCriticalEdges() : FunctionPass((intptr_t)&ID) {}
+
     virtual bool runOnFunction(Function &F);
 
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
-      AU.addPreserved<ETForest>();
-      AU.addPreserved<ImmediateDominators>();
+      AU.addPreserved<DominatorTree>();
       AU.addPreserved<DominanceFrontier>();
       AU.addPreserved<LoopInfo>();
 
@@ -47,6 +49,7 @@ namespace {
     }
   };
 
+  char BreakCriticalEdges::ID = 0;
   RegisterPass<BreakCriticalEdges> X("break-crit-edges",
                                     "Break critical edges in CFG");
 }
@@ -106,11 +109,11 @@ bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
 }
 
 // SplitCriticalEdge - If this edge is a critical edge, insert a new node to
-// split the critical edge.  This will update ETForest, ImmediateDominator,
-// DominatorTree, and DominatorFrontier information if it is available, thus
-// calling this pass will not invalidate any of them.  This returns true if
-// the edge was split, false otherwise.  This ensures that all edges to that
-// dest go to one block instead of each going to a different block.
+// split the critical edge.  This will update DominatorTree, and DominatorFrontier 
+// information if it is available, thus calling this pass will not invalidate 
+// any of them.  This returns true if the edge was split, false otherwise. 
+// This ensures that all edges to that dest go to one block instead of each 
+// going to a different block.
 //
 bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P,
                              bool MergeIdenticalEdges) {
@@ -129,7 +132,8 @@ bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P,
 
   // Insert the block into the function... right after the block TI lives in.
   Function &F = *TIBB->getParent();
-  F.getBasicBlockList().insert(TIBB->getNext(), NewBB);
+  Function::iterator FBBI = TIBB;
+  F.getBasicBlockList().insert(++FBBI, NewBB);
   
   // If there are any PHI nodes in DestBB, we need to update them so that they
   // merge incoming values from NewBB instead of from TIBB.
@@ -176,66 +180,23 @@ bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P,
   
   bool NewBBDominatesDestBB = true;
   
-  // Update the forest?
-  if (ETForest *EF = P->getAnalysisToUpdate<ETForest>()) {
-    // NewBB is dominated by TIBB.
-    EF->addNewBlock(NewBB, TIBB);
-    
-    // If NewBBDominatesDestBB hasn't been computed yet, do so with EF.
-    if (!OtherPreds.empty()) {
-      while (!OtherPreds.empty() && NewBBDominatesDestBB) {
-        NewBBDominatesDestBB = EF->dominates(DestBB, OtherPreds.back());
-        OtherPreds.pop_back();
-      }
-      OtherPreds.clear();
-    }
-    
-    // If NewBBDominatesDestBB, then NewBB dominates DestBB, otherwise it
-    // doesn't dominate anything.
-    if (NewBBDominatesDestBB)
-      EF->setImmediateDominator(DestBB, NewBB);
-  }
-
-  // Should we update ImmediateDominator information?
-  if (ImmediateDominators *ID = P->getAnalysisToUpdate<ImmediateDominators>()) {
-    // Only do this if TIBB is reachable.
-    if (ID->get(TIBB) || &TIBB->getParent()->getEntryBlock() == TIBB) {
-      // TIBB is the new immediate dominator for NewBB.
-      ID->addNewBlock(NewBB, TIBB);
-      
-      // If NewBBDominatesDestBB hasn't been computed yet, do so with ID.
-      if (!OtherPreds.empty()) {
-        while (!OtherPreds.empty() && NewBBDominatesDestBB) {
-          NewBBDominatesDestBB = ID->dominates(DestBB, OtherPreds.back());
-          OtherPreds.pop_back();
-        }
-        OtherPreds.clear();
-      }
-      
-      // If NewBBDominatesDestBB, then NewBB dominates DestBB, otherwise it
-      // doesn't dominate anything.
-      if (NewBBDominatesDestBB)
-        ID->setImmediateDominator(DestBB, NewBB);
-    }
-  }
-  
   // Should we update DominatorTree information?
   if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) {
-    DominatorTree::Node *TINode = DT->getNode(TIBB);
+    DomTreeNode *TINode = DT->getNode(TIBB);
 
     // The new block is not the immediate dominator for any other nodes, but
     // TINode is the immediate dominator for the new node.
     //
     if (TINode) {       // Don't break unreachable code!
-      DominatorTree::Node *NewBBNode = DT->createNewNode(NewBB, TINode);
-      DominatorTree::Node *DestBBNode = 0;
+      DomTreeNode *NewBBNode = DT->addNewBlock(NewBB, TIBB);
+      DomTreeNode *DestBBNode = 0;
      
       // If NewBBDominatesDestBB hasn't been computed yet, do so with DT.
       if (!OtherPreds.empty()) {
         DestBBNode = DT->getNode(DestBB);
         while (!OtherPreds.empty() && NewBBDominatesDestBB) {
-          if (DominatorTree::Node *OPNode = DT->getNode(OtherPreds.back()))
-            NewBBDominatesDestBB = DestBBNode->dominates(OPNode);
+          if (DomTreeNode *OPNode = DT->getNode(OtherPreds.back()))
+            NewBBDominatesDestBB = DT->dominates(DestBBNode, OPNode);
           OtherPreds.pop_back();
         }
         OtherPreds.clear();
@@ -266,8 +227,12 @@ bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P,
     DominanceFrontier::DomSetType NewDFSet;
     if (NewBBDominatesDestBB) {
       DominanceFrontier::iterator I = DF->find(DestBB);
-      if (I != DF->end())
+      if (I != DF->end()) {
         DF->addBasicBlock(NewBB, I->second);
+        // However NewBB's frontier does not include DestBB.
+        DominanceFrontier::iterator NF = DF->find(NewBB);
+        DF->removeFromFrontier(NF, DestBB);
+      }
       else
         DF->addBasicBlock(NewBB, DominanceFrontier::DomSetType());
     } else {
@@ -285,13 +250,13 @@ bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P,
       if (Loop *DestLoop = LI->getLoopFor(DestBB)) {
         if (TIL == DestLoop) {
           // Both in the same loop, the NewBB joins loop.
-          DestLoop->addBasicBlockToLoop(NewBB, *LI);
+          DestLoop->addBasicBlockToLoop(NewBB, LI->getBase());
         } else if (TIL->contains(DestLoop->getHeader())) {
           // Edge from an outer loop to an inner loop.  Add to the outer loop.
-          TIL->addBasicBlockToLoop(NewBB, *LI);
+          TIL->addBasicBlockToLoop(NewBB, LI->getBase());
         } else if (DestLoop->contains(TIL->getHeader())) {
           // Edge from an inner loop to an outer loop.  Add to the outer loop.
-          DestLoop->addBasicBlockToLoop(NewBB, *LI);
+          DestLoop->addBasicBlockToLoop(NewBB, LI->getBase());
         } else {
           // Edge from two loops with no containment relation.  Because these
           // are natural loops, we know that the destination block must be the
@@ -300,7 +265,7 @@ bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P,
           assert(DestLoop->getHeader() == DestBB &&
                  "Should not create irreducible loops!");
           if (Loop *P = DestLoop->getParentLoop())
-            P->addBasicBlockToLoop(NewBB, *LI);
+            P->addBasicBlockToLoop(NewBB, LI->getBase());
         }
       }
   }