Chris Lattner | d76efa0 | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 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" |
Chris Lattner | d76efa0 | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 12 | #include "llvm/Analysis/Dominators.h" |
| 13 | #include "llvm/Function.h" |
Chris Lattner | eb0456c | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 14 | #include "llvm/iTerminators.h" |
| 15 | #include "llvm/iPHINode.h" |
| 16 | #include "llvm/Support/CFG.h" |
Chris Lattner | d76efa0 | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 17 | #include "Support/StatisticReporter.h" |
| 18 | |
Chris Lattner | 6de302b | 2002-09-24 15:43:12 +0000 | [diff] [blame] | 19 | namespace { |
| 20 | Statistic<> NumBroken("break-crit-edges\t- Number of blocks inserted"); |
Chris Lattner | d76efa0 | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 6de302b | 2002-09-24 15:43:12 +0000 | [diff] [blame] | 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>(); |
Chris Lattner | c178d94 | 2002-09-26 16:18:51 +0000 | [diff] [blame] | 29 | AU.addPreservedID(LoopPreheadersID); // No preheaders deleted. |
Chris Lattner | 6de302b | 2002-09-24 15:43:12 +0000 | [diff] [blame] | 30 | } |
Chris Lattner | c178d94 | 2002-09-26 16:18:51 +0000 | [diff] [blame] | 31 | private: |
| 32 | void SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum); |
Chris Lattner | 6de302b | 2002-09-24 15:43:12 +0000 | [diff] [blame] | 33 | }; |
Chris Lattner | d76efa0 | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 6de302b | 2002-09-24 15:43:12 +0000 | [diff] [blame] | 35 | RegisterOpt<BreakCriticalEdges> X("break-crit-edges", |
| 36 | "Break critical edges in CFG"); |
| 37 | } |
Chris Lattner | d76efa0 | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 38 | |
Chris Lattner | eb0456c | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 39 | // Publically exposed interface to pass... |
Chris Lattner | 6de302b | 2002-09-24 15:43:12 +0000 | [diff] [blame] | 40 | const PassInfo *BreakCriticalEdgesID = X.getPassInfo(); |
Chris Lattner | d76efa0 | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 41 | Pass *createBreakCriticalEdgesPass() { return new BreakCriticalEdges(); } |
| 42 | |
Chris Lattner | eb0456c | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 43 | |
| 44 | // isCriticalEdge - Return true if the specified edge is a critical edge. |
| 45 | // Critical edges are edges from a block with multiple successors to a block |
| 46 | // with multiple predecessors. |
| 47 | // |
| 48 | static bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) { |
| 49 | assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!"); |
| 50 | assert (TI->getNumSuccessors() > 1); |
| 51 | |
| 52 | const BasicBlock *Dest = TI->getSuccessor(SuccNum); |
| 53 | pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest); |
| 54 | |
| 55 | // If there is more than one predecessor, this is a critical edge... |
| 56 | assert(I != E && "No preds, but we have an edge to the block?"); |
| 57 | ++I; // Skip one edge due to the incoming arc from TI. |
| 58 | return I != E; |
| 59 | } |
| 60 | |
| 61 | // SplitCriticalEdge - Insert a new node node to split the critical edge. This |
| 62 | // will update DominatorSet, ImmediateDominator and DominatorTree information if |
| 63 | // it is available, thus calling this pass will not invalidate either of them. |
| 64 | // |
Chris Lattner | c178d94 | 2002-09-26 16:18:51 +0000 | [diff] [blame] | 65 | void BreakCriticalEdges::SplitCriticalEdge(TerminatorInst *TI,unsigned SuccNum){ |
Chris Lattner | eb0456c | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 66 | assert(isCriticalEdge(TI, SuccNum) && |
| 67 | "Cannot break a critical edge, if it isn't a critical edge"); |
| 68 | BasicBlock *TIBB = TI->getParent(); |
| 69 | |
| 70 | // Create a new basic block, linking it into the CFG. |
| 71 | BasicBlock *NewBB = new BasicBlock(TIBB->getName()+"_crit_edge"); |
| 72 | BasicBlock *DestBB = TI->getSuccessor(SuccNum); |
| 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 | |
Chris Lattner | c178d94 | 2002-09-26 16:18:51 +0000 | [diff] [blame] | 93 | // Now update analysis information. These are the analyses that we are |
| 94 | // currently capable of updating... |
Chris Lattner | eb0456c | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 95 | // |
Chris Lattner | eb0456c | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 96 | |
Chris Lattner | c178d94 | 2002-09-26 16:18:51 +0000 | [diff] [blame] | 97 | // Should we update DominatorSet information? |
| 98 | if (DominatorSet *DS = getAnalysisToUpdate<DominatorSet>()) { |
| 99 | // The blocks that dominate the new one are the blocks that dominate TIBB |
| 100 | // plus the new block itself. |
| 101 | DominatorSet::DomSetType DomSet = DS->getDominators(TIBB); |
| 102 | DomSet.insert(NewBB); // A block always dominates itself. |
| 103 | DS->addBasicBlock(NewBB, DomSet); |
| 104 | } |
Chris Lattner | eb0456c | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 105 | |
Chris Lattner | c178d94 | 2002-09-26 16:18:51 +0000 | [diff] [blame] | 106 | // Should we update ImmdediateDominator information? |
| 107 | if (ImmediateDominators *ID = getAnalysisToUpdate<ImmediateDominators>()) { |
| 108 | // TIBB is the new immediate dominator for NewBB. NewBB doesn't dominate |
| 109 | // anything. |
| 110 | ID->addNewBlock(NewBB, TIBB); |
| 111 | } |
| 112 | |
| 113 | // Should we update DominatorTree information? |
| 114 | if (DominatorTree *DT = getAnalysisToUpdate<DominatorTree>()) { |
| 115 | DominatorTree::Node *TINode = DT->getNode(TIBB); |
| 116 | |
| 117 | // The new block is not the immediate dominator for any other nodes, but |
| 118 | // TINode is the immediate dominator for the new node. |
| 119 | // |
| 120 | DT->createNewNode(NewBB, TINode); |
Chris Lattner | eb0456c | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Chris Lattner | d76efa0 | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 124 | // runOnFunction - Loop over all of the edges in the CFG, breaking critical |
| 125 | // edges as they are found. |
| 126 | // |
| 127 | bool BreakCriticalEdges::runOnFunction(Function &F) { |
| 128 | bool Changed = false; |
| 129 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 130 | TerminatorInst *TI = I->getTerminator(); |
Chris Lattner | eb0456c | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 131 | if (TI->getNumSuccessors() > 1) |
| 132 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 133 | if (isCriticalEdge(TI, i)) { |
Chris Lattner | c178d94 | 2002-09-26 16:18:51 +0000 | [diff] [blame] | 134 | SplitCriticalEdge(TI, i); |
Chris Lattner | eb0456c | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 135 | ++NumBroken; |
| 136 | Changed = true; |
| 137 | } |
Chris Lattner | d76efa0 | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | return Changed; |
| 141 | } |