Start using the nicer terminator auto-insertion API
[oota-llvm.git] / lib / Transforms / Utils / BreakCriticalEdges.cpp
index 8f8f0eea2dae7e5e6f3c79d08a41aabb516c597f..0b597102d87617f00ca903ee6b629ae9317810c4 100644 (file)
 // inserting a dummy basic block.  This pass may be "required" by passes that
 // cannot deal with critical edges.  For this usage, the structure type is
 // forward declared.  This pass obviously invalidates the CFG, but can update
-// forward dominator (set, immediate dominators, and tree) information.
+// forward dominator (set, immediate dominators, tree, and frontier)
+// information.
 //
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/Scalar.h"
+#include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Analysis/Dominators.h"
 #include "llvm/Function.h"
 #include "llvm/iTerminators.h"
@@ -23,6 +25,8 @@
 #include "llvm/Support/CFG.h"
 #include "Support/Statistic.h"
 
+namespace llvm {
+
 namespace {
   Statistic<> NumBroken("break-crit-edges", "Number of blocks inserted");
 
@@ -48,6 +52,27 @@ namespace {
 const PassInfo *BreakCriticalEdgesID = X.getPassInfo();
 Pass *createBreakCriticalEdgesPass() { return new BreakCriticalEdges(); }
 
+// runOnFunction - Loop over all of the edges in the CFG, breaking critical
+// edges as they are found.
+//
+bool BreakCriticalEdges::runOnFunction(Function &F) {
+  bool Changed = false;
+  for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
+    TerminatorInst *TI = I->getTerminator();
+    if (TI->getNumSuccessors() > 1)
+      for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
+        if (SplitCriticalEdge(TI, i, this)) {
+          ++NumBroken;
+          Changed = true;
+        }
+  }
+
+  return Changed;
+}
+
+//===----------------------------------------------------------------------===//
+//    Implementation of the external critical edge manipulation functions
+//===----------------------------------------------------------------------===//
 
 // isCriticalEdge - Return true if the specified edge is a critical edge.
 // Critical edges are edges from a block with multiple successors to a block
@@ -66,13 +91,14 @@ bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) {
   return I != E;
 }
 
-// SplitCriticalEdge - Insert a new node node to split the critical edge.  This
-// will update DominatorSet, ImmediateDominator and DominatorTree information if
-// it is available, thus calling this pass will not invalidate either of them.
+// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
+// split the critical edge.  This will update DominatorSet, ImmediateDominator,
+// DominatorTree, and DominatorFrontier information if it is available, thus
+// calling this pass will not invalidate either of them.  This returns true if
+// the edge was split, false otherwise.
 //
-void SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
-  assert(isCriticalEdge(TI, SuccNum) &&
-         "Cannot break a critical edge, if it isn't a critical edge");
+bool SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
+  if (!isCriticalEdge(TI, SuccNum)) return false;
   BasicBlock *TIBB = TI->getParent();
   BasicBlock *DestBB = TI->getSuccessor(SuccNum);
 
@@ -80,8 +106,7 @@ void SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
   BasicBlock *NewBB = new BasicBlock(TIBB->getName() + "." +
                                      DestBB->getName() + "_crit_edge");
   // Create our unconditional branch...
-  BranchInst *BI = new BranchInst(DestBB);
-  NewBB->getInstList().push_back(BI);
+  new BranchInst(DestBB, 0, 0, NewBB);
   
   // Branch to the new block, breaking the edge...
   TI->setSuccessor(SuccNum, NewBB);
@@ -100,7 +125,7 @@ void SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
   }
 
   // If we don't have a pass object, we can't update anything...
-  if (P == 0) return;
+  if (P == 0) return true;
 
   // Now update analysis information.  These are the analyses that we are
   // currently capable of updating...
@@ -142,23 +167,7 @@ void SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
     NewDFSet.insert(DestBB);
     DF->addBasicBlock(NewBB, NewDFSet);
   }
+  return true;
 }
 
-// runOnFunction - Loop over all of the edges in the CFG, breaking critical
-// edges as they are found.
-//
-bool BreakCriticalEdges::runOnFunction(Function &F) {
-  bool Changed = false;
-  for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
-    TerminatorInst *TI = I->getTerminator();
-    if (TI->getNumSuccessors() > 1)
-      for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
-        if (isCriticalEdge(TI, i)) {
-          SplitCriticalEdge(TI, i, this);
-          ++NumBroken;
-          Changed = true;
-        }
-  }
-
-  return Changed;
-}
+} // End llvm namespace