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