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/Statistic.h" 18 19 namespace { 20 Statistic<> NumBroken("break-crit-edges", "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 AU.addPreserved<DominanceFrontier>(); 30 AU.addPreservedID(LoopPreheadersID); // No preheaders deleted. 31 } 32 }; 33 34 RegisterOpt<BreakCriticalEdges> X("break-crit-edges", 35 "Break critical edges in CFG"); 36 } 37 38 // Publically exposed interface to pass... 39 const PassInfo *BreakCriticalEdgesID = X.getPassInfo(); 40 Pass *createBreakCriticalEdgesPass() { return new BreakCriticalEdges(); } 41 42 43 // isCriticalEdge - Return true if the specified edge is a critical edge. 44 // Critical edges are edges from a block with multiple successors to a block 45 // with multiple predecessors. 46 // 47 bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) { 48 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!"); 49 if (TI->getNumSuccessors() == 1) return false; 50 51 const BasicBlock *Dest = TI->getSuccessor(SuccNum); 52 pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest); 53 54 // If there is more than one predecessor, this is a critical edge... 55 assert(I != E && "No preds, but we have an edge to the block?"); 56 ++I; // Skip one edge due to the incoming arc from TI. 57 return I != E; 58 } 59 60 // SplitCriticalEdge - Insert a new node node to split the critical edge. This 61 // will update DominatorSet, ImmediateDominator and DominatorTree information if 62 // it is available, thus calling this pass will not invalidate either of them. 63 // 64 void SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) { 65 assert(isCriticalEdge(TI, SuccNum) && 66 "Cannot break a critical edge, if it isn't a critical edge"); 67 BasicBlock *TIBB = TI->getParent(); 68 BasicBlock *DestBB = TI->getSuccessor(SuccNum); 69 70 // Create a new basic block, linking it into the CFG. 71 BasicBlock *NewBB = new BasicBlock(TIBB->getName() + "." + 72 DestBB->getName() + "_crit_edge"); 73 // Create our unconditional branch... 74 BranchInst *BI = new BranchInst(DestBB); 75 NewBB->getInstList().push_back(BI); 76 77 // Branch to the new block, breaking the edge... 78 TI->setSuccessor(SuccNum, NewBB); 79 80 // Insert the block into the function... right after the block TI lives in. 81 Function &F = *TIBB->getParent(); 82 F.getBasicBlockList().insert(TIBB->getNext(), NewBB); 83 84 // If there are any PHI nodes in DestBB, we need to update them so that they 85 // merge incoming values from NewBB instead of from TIBB. 86 // 87 for (BasicBlock::iterator I = DestBB->begin(); 88 PHINode *PN = dyn_cast<PHINode>(I); ++I) { 89 // We no longer enter through TIBB, now we come in through NewBB. 90 PN->replaceUsesOfWith(TIBB, NewBB); 91 } 92 93 // If we don't have a pass object, we can't update anything... 94 if (P == 0) return; 95 96 // Now update analysis information. These are the analyses that we are 97 // currently capable of updating... 98 // 99 100 // Should we update DominatorSet information? 101 if (DominatorSet *DS = P->getAnalysisToUpdate<DominatorSet>()) { 102 // The blocks that dominate the new one are the blocks that dominate TIBB 103 // plus the new block itself. 104 DominatorSet::DomSetType DomSet = DS->getDominators(TIBB); 105 DomSet.insert(NewBB); // A block always dominates itself. 106 DS->addBasicBlock(NewBB, DomSet); 107 } 108 109 // Should we update ImmdediateDominator information? 110 if (ImmediateDominators *ID = P->getAnalysisToUpdate<ImmediateDominators>()) { 111 // TIBB is the new immediate dominator for NewBB. NewBB doesn't dominate 112 // anything. 113 ID->addNewBlock(NewBB, TIBB); 114 } 115 116 // Should we update DominatorTree information? 117 if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) { 118 DominatorTree::Node *TINode = DT->getNode(TIBB); 119 120 // The new block is not the immediate dominator for any other nodes, but 121 // TINode is the immediate dominator for the new node. 122 // 123 if (TINode) // Don't break unreachable code! 124 DT->createNewNode(NewBB, TINode); 125 } 126 127 // Should we update DominanceFrontier information? 128 if (DominanceFrontier *DF = P->getAnalysisToUpdate<DominanceFrontier>()) { 129 // Since the new block is dominated by its only predecessor TIBB, 130 // it cannot be in any block's dominance frontier. Its dominance 131 // frontier is {DestBB}. 132 DominanceFrontier::DomSetType NewDFSet; 133 NewDFSet.insert(DestBB); 134 DF->addBasicBlock(NewBB, NewDFSet); 135 } 136 } 137 138 // runOnFunction - Loop over all of the edges in the CFG, breaking critical 139 // edges as they are found. 140 // 141 bool BreakCriticalEdges::runOnFunction(Function &F) { 142 bool Changed = false; 143 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { 144 TerminatorInst *TI = I->getTerminator(); 145 if (TI->getNumSuccessors() > 1) 146 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 147 if (isCriticalEdge(TI, i)) { 148 SplitCriticalEdge(TI, i, this); 149 ++NumBroken; 150 Changed = true; 151 } 152 } 153 154 return Changed; 155 } 156