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