- Do not expose Critical Edge breaking mechanics outside the BCE pass, thus
[oota-llvm.git] / lib / Transforms / Utils / BreakCriticalEdges.cpp
1 //===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
2 //
3 // BreakCriticalEdges pass - Break all of the critical edges in the CFG by
4 // inserting a dummy basic block.  This pass may be "required" by passes that
5 // cannot deal with critical edges.  For this usage, the structure type is
6 // forward declared.  This pass obviously invalidates the CFG, but can update
7 // forward dominator (set, immediate dominators, and tree) information.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #include "llvm/Transforms/Scalar.h"
12 #include "llvm/Analysis/Dominators.h"
13 #include "llvm/Function.h"
14 #include "llvm/iTerminators.h"
15 #include "llvm/iPHINode.h"
16 #include "llvm/Support/CFG.h"
17 #include "Support/StatisticReporter.h"
18
19 namespace {
20   Statistic<> NumBroken("break-crit-edges\t- Number of blocks inserted");
21
22   struct BreakCriticalEdges : public FunctionPass {
23     virtual bool runOnFunction(Function &F);
24     
25     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
26       AU.addPreserved<DominatorSet>();
27       AU.addPreserved<ImmediateDominators>();
28       AU.addPreserved<DominatorTree>();
29     }
30   };
31
32   RegisterOpt<BreakCriticalEdges> X("break-crit-edges",
33                                     "Break critical edges in CFG");
34 }
35
36 // Publically exposed interface to pass...
37 const PassInfo *BreakCriticalEdgesID = X.getPassInfo();
38 Pass *createBreakCriticalEdgesPass() { return new BreakCriticalEdges(); }
39
40
41 // isCriticalEdge - Return true if the specified edge is a critical edge.
42 // Critical edges are edges from a block with multiple successors to a block
43 // with multiple predecessors.
44 //
45 static bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) {
46   assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
47   assert (TI->getNumSuccessors() > 1);
48
49   const BasicBlock *Dest = TI->getSuccessor(SuccNum);
50   pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest);
51
52   // If there is more than one predecessor, this is a critical edge...
53   assert(I != E && "No preds, but we have an edge to the block?");
54   ++I;        // Skip one edge due to the incoming arc from TI.
55   return I != E;
56 }
57
58 // SplitCriticalEdge - Insert a new node node to split the critical edge.  This
59 // will update DominatorSet, ImmediateDominator and DominatorTree information if
60 // it is available, thus calling this pass will not invalidate either of them.
61 //
62 static void SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
63   assert(isCriticalEdge(TI, SuccNum) &&
64          "Cannot break a critical edge, if it isn't a critical edge");
65   BasicBlock *TIBB = TI->getParent();
66
67   // Create a new basic block, linking it into the CFG.
68   BasicBlock *NewBB = new BasicBlock(TIBB->getName()+"_crit_edge");
69   BasicBlock *DestBB = TI->getSuccessor(SuccNum);
70   // Create our unconditional branch...
71   BranchInst *BI = new BranchInst(DestBB);
72   NewBB->getInstList().push_back(BI);
73   
74   // Branch to the new block, breaking the edge...
75   TI->setSuccessor(SuccNum, NewBB);
76
77   // Insert the block into the function... right after the block TI lives in.
78   Function &F = *TIBB->getParent();
79   F.getBasicBlockList().insert(TIBB->getNext(), NewBB);
80
81   // If there are any PHI nodes in DestBB, we need to update them so that they
82   // merge incoming values from NewBB instead of from TIBB.
83   //
84   for (BasicBlock::iterator I = DestBB->begin();
85        PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
86     // We no longer enter through TIBB, now we come in through NewBB.
87     PN->replaceUsesOfWith(TIBB, NewBB);
88   }
89
90   // Now if we have a pass object, update analysis information.  Currently we
91   // update DominatorSet and DominatorTree information if it's available.
92   //
93   if (P) {
94     // Should we update DominatorSet information?
95     if (DominatorSet *DS = P->getAnalysisToUpdate<DominatorSet>()) {
96       // The blocks that dominate the new one are the blocks that dominate TIBB
97       // plus the new block itself.
98       DominatorSet::DomSetType DomSet = DS->getDominators(TIBB);
99       DomSet.insert(NewBB);  // A block always dominates itself.
100       DS->addBasicBlock(NewBB, DomSet);
101     }
102
103     // Should we update ImmdediateDominator information?
104     if (ImmediateDominators *ID =
105         P->getAnalysisToUpdate<ImmediateDominators>()) {
106       // TIBB is the new immediate dominator for NewBB.  NewBB doesn't dominate
107       // anything.
108       ID->addNewBlock(NewBB, TIBB);
109     }
110
111     // Should we update DominatorTree information?
112     if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) {
113       DominatorTree::Node *TINode = DT->getNode(TIBB);
114
115       // The new block is not the immediate dominator for any other nodes, but
116       // TINode is the immediate dominator for the new node.
117       //
118       DT->createNewNode(NewBB, TINode);
119     }
120   }
121 }
122
123 // runOnFunction - Loop over all of the edges in the CFG, breaking critical
124 // edges as they are found.
125 //
126 bool BreakCriticalEdges::runOnFunction(Function &F) {
127   bool Changed = false;
128   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
129     TerminatorInst *TI = I->getTerminator();
130     if (TI->getNumSuccessors() > 1)
131       for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
132         if (isCriticalEdge(TI, i)) {
133           SplitCriticalEdge(TI, i, this);
134           ++NumBroken;
135           Changed = true;
136         }
137   }
138
139   return Changed;
140 }