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