Chris Lattner | 55d4788 | 2003-10-12 21:44:18 +0000 | [diff] [blame] | 1 | //===- LoopSimplify.cpp - Loop Canonicalization Pass ----------------------===// |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 3 | // This pass performs several transformations to transform natural loops into a |
| 4 | // simpler form, which makes subsequent analyses and transformations simpler and |
| 5 | // more effective. |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 6 | // |
| 7 | // Loop pre-header insertion guarantees that there is a single, non-critical |
| 8 | // entry edge from outside of the loop to the loop header. This simplifies a |
| 9 | // number of analyses and transformations, such as LICM. |
| 10 | // |
| 11 | // Loop exit-block insertion guarantees that all exit blocks from the loop |
| 12 | // (blocks which are outside of the loop that have predecessors inside of the |
| 13 | // loop) are dominated by the loop header. This simplifies transformations such |
Chris Lattner | a34c477 | 2003-08-18 22:54:06 +0000 | [diff] [blame] | 14 | // as store-sinking that are built into LICM. |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 15 | // |
| 16 | // Note that the simplifycfg pass will clean up blocks which are split out but |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 17 | // end up being unnecessary, so usage of this pass should not pessimize |
| 18 | // generated code. |
| 19 | // |
| 20 | // This pass obviously modifies the CFG, but updates loop information and |
| 21 | // dominator information. |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | #include "llvm/Transforms/Scalar.h" |
| 26 | #include "llvm/Analysis/Dominators.h" |
| 27 | #include "llvm/Analysis/LoopInfo.h" |
| 28 | #include "llvm/Function.h" |
| 29 | #include "llvm/iTerminators.h" |
| 30 | #include "llvm/iPHINode.h" |
| 31 | #include "llvm/Constant.h" |
| 32 | #include "llvm/Support/CFG.h" |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 33 | #include "Support/SetOperations.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 34 | #include "Support/Statistic.h" |
Chris Lattner | 32a39c2 | 2003-02-28 03:07:54 +0000 | [diff] [blame] | 35 | #include "Support/DepthFirstIterator.h" |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 36 | |
| 37 | namespace { |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 38 | Statistic<> |
| 39 | NumInserted("loopsimplify", "Number of pre-header blocks inserted"); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 41 | struct LoopSimplify : public FunctionPass { |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 42 | virtual bool runOnFunction(Function &F); |
| 43 | |
| 44 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 45 | // We need loop information to identify the loops... |
| 46 | AU.addRequired<LoopInfo>(); |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 47 | AU.addRequired<DominatorSet>(); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 48 | |
| 49 | AU.addPreserved<LoopInfo>(); |
| 50 | AU.addPreserved<DominatorSet>(); |
| 51 | AU.addPreserved<ImmediateDominators>(); |
| 52 | AU.addPreserved<DominatorTree>(); |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 53 | AU.addPreserved<DominanceFrontier>(); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 54 | AU.addPreservedID(BreakCriticalEdgesID); // No crit edges added.... |
| 55 | } |
| 56 | private: |
| 57 | bool ProcessLoop(Loop *L); |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 58 | BasicBlock *SplitBlockPredecessors(BasicBlock *BB, const char *Suffix, |
| 59 | const std::vector<BasicBlock*> &Preds); |
| 60 | void RewriteLoopExitBlock(Loop *L, BasicBlock *Exit); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 61 | void InsertPreheaderForLoop(Loop *L); |
| 62 | }; |
| 63 | |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 64 | RegisterOpt<LoopSimplify> |
| 65 | X("loopsimplify", "Canonicalize natural loops", true); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // Publically exposed interface to pass... |
Chris Lattner | 72272a7 | 2003-10-12 21:52:28 +0000 | [diff] [blame^] | 69 | const PassInfo *LoopSimplifyID = X.getPassInfo(); |
| 70 | Pass *createLoopSimplifyPass() { return new LoopSimplify(); } |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 71 | |
| 72 | |
| 73 | /// runOnFunction - Run down all loops in the CFG (recursively, but we could do |
| 74 | /// it in any convenient order) inserting preheaders... |
| 75 | /// |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 76 | bool LoopSimplify::runOnFunction(Function &F) { |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 77 | bool Changed = false; |
| 78 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 79 | |
| 80 | for (unsigned i = 0, e = LI.getTopLevelLoops().size(); i != e; ++i) |
| 81 | Changed |= ProcessLoop(LI.getTopLevelLoops()[i]); |
| 82 | |
| 83 | return Changed; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /// ProcessLoop - Walk the loop structure in depth first order, ensuring that |
| 88 | /// all loops have preheaders. |
| 89 | /// |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 90 | bool LoopSimplify::ProcessLoop(Loop *L) { |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 91 | bool Changed = false; |
| 92 | |
| 93 | // Does the loop already have a preheader? If so, don't modify the loop... |
| 94 | if (L->getLoopPreheader() == 0) { |
| 95 | InsertPreheaderForLoop(L); |
| 96 | NumInserted++; |
| 97 | Changed = true; |
| 98 | } |
| 99 | |
Chris Lattner | a34c477 | 2003-08-18 22:54:06 +0000 | [diff] [blame] | 100 | // Regardless of whether or not we added a preheader to the loop we must |
| 101 | // guarantee that the preheader dominates all exit nodes. If there are any |
| 102 | // exit nodes not dominated, split them now. |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 103 | DominatorSet &DS = getAnalysis<DominatorSet>(); |
| 104 | BasicBlock *Header = L->getHeader(); |
| 105 | for (unsigned i = 0, e = L->getExitBlocks().size(); i != e; ++i) |
| 106 | if (!DS.dominates(Header, L->getExitBlocks()[i])) { |
| 107 | RewriteLoopExitBlock(L, L->getExitBlocks()[i]); |
Chris Lattner | 10b2b05 | 2003-02-27 22:31:07 +0000 | [diff] [blame] | 108 | assert(DS.dominates(Header, L->getExitBlocks()[i]) && |
| 109 | "RewriteLoopExitBlock failed?"); |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 110 | NumInserted++; |
| 111 | Changed = true; |
| 112 | } |
| 113 | |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 114 | const std::vector<Loop*> &SubLoops = L->getSubLoops(); |
| 115 | for (unsigned i = 0, e = SubLoops.size(); i != e; ++i) |
| 116 | Changed |= ProcessLoop(SubLoops[i]); |
| 117 | return Changed; |
| 118 | } |
| 119 | |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 120 | /// SplitBlockPredecessors - Split the specified block into two blocks. We want |
| 121 | /// to move the predecessors specified in the Preds list to point to the new |
| 122 | /// block, leaving the remaining predecessors pointing to BB. This method |
| 123 | /// updates the SSA PHINode's, but no other analyses. |
| 124 | /// |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 125 | BasicBlock *LoopSimplify::SplitBlockPredecessors(BasicBlock *BB, |
| 126 | const char *Suffix, |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 127 | const std::vector<BasicBlock*> &Preds) { |
| 128 | |
| 129 | // Create new basic block, insert right before the original block... |
| 130 | BasicBlock *NewBB = new BasicBlock(BB->getName()+Suffix, BB); |
| 131 | |
| 132 | // The preheader first gets an unconditional branch to the loop header... |
| 133 | BranchInst *BI = new BranchInst(BB); |
| 134 | NewBB->getInstList().push_back(BI); |
| 135 | |
| 136 | // For every PHI node in the block, insert a PHI node into NewBB where the |
| 137 | // incoming values from the out of loop edges are moved to NewBB. We have two |
| 138 | // possible cases here. If the loop is dead, we just insert dummy entries |
| 139 | // into the PHI nodes for the new edge. If the loop is not dead, we move the |
| 140 | // incoming edges in BB into new PHI nodes in NewBB. |
| 141 | // |
| 142 | if (!Preds.empty()) { // Is the loop not obviously dead? |
| 143 | for (BasicBlock::iterator I = BB->begin(); |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 144 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 145 | |
| 146 | // Create the new PHI node, insert it into NewBB at the end of the block |
| 147 | PHINode *NewPHI = new PHINode(PN->getType(), PN->getName()+".ph", BI); |
| 148 | |
| 149 | // Move all of the edges from blocks outside the loop to the new PHI |
| 150 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) { |
| 151 | Value *V = PN->removeIncomingValue(Preds[i]); |
| 152 | NewPHI->addIncoming(V, Preds[i]); |
| 153 | } |
| 154 | |
| 155 | // Add an incoming value to the PHI node in the loop for the preheader |
| 156 | // edge |
| 157 | PN->addIncoming(NewPHI, NewBB); |
| 158 | } |
| 159 | |
| 160 | // Now that the PHI nodes are updated, actually move the edges from |
| 161 | // Preds to point to NewBB instead of BB. |
| 162 | // |
| 163 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) { |
| 164 | TerminatorInst *TI = Preds[i]->getTerminator(); |
| 165 | for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) |
| 166 | if (TI->getSuccessor(s) == BB) |
| 167 | TI->setSuccessor(s, NewBB); |
| 168 | } |
| 169 | |
| 170 | } else { // Otherwise the loop is dead... |
| 171 | for (BasicBlock::iterator I = BB->begin(); |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 172 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 173 | // Insert dummy values as the incoming value... |
| 174 | PN->addIncoming(Constant::getNullValue(PN->getType()), NewBB); |
| 175 | } |
| 176 | return NewBB; |
| 177 | } |
| 178 | |
Chris Lattner | 0895025 | 2003-05-12 22:04:34 +0000 | [diff] [blame] | 179 | // ChangeExitBlock - This recursive function is used to change any exit blocks |
| 180 | // that use OldExit to use NewExit instead. This is recursive because children |
| 181 | // may need to be processed as well. |
| 182 | // |
| 183 | static void ChangeExitBlock(Loop *L, BasicBlock *OldExit, BasicBlock *NewExit) { |
| 184 | if (L->hasExitBlock(OldExit)) { |
| 185 | L->changeExitBlock(OldExit, NewExit); |
| 186 | const std::vector<Loop*> &SubLoops = L->getSubLoops(); |
| 187 | for (unsigned i = 0, e = SubLoops.size(); i != e; ++i) |
| 188 | ChangeExitBlock(SubLoops[i], OldExit, NewExit); |
| 189 | } |
| 190 | } |
| 191 | |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 192 | |
| 193 | /// InsertPreheaderForLoop - Once we discover that a loop doesn't have a |
| 194 | /// preheader, this method is called to insert one. This method has two phases: |
| 195 | /// preheader insertion and analysis updating. |
| 196 | /// |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 197 | void LoopSimplify::InsertPreheaderForLoop(Loop *L) { |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 198 | BasicBlock *Header = L->getHeader(); |
| 199 | |
| 200 | // Compute the set of predecessors of the loop that are not in the loop. |
| 201 | std::vector<BasicBlock*> OutsideBlocks; |
| 202 | for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header); |
| 203 | PI != PE; ++PI) |
| 204 | if (!L->contains(*PI)) // Coming in from outside the loop? |
| 205 | OutsideBlocks.push_back(*PI); // Keep track of it... |
| 206 | |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 207 | // Split out the loop pre-header |
| 208 | BasicBlock *NewBB = |
| 209 | SplitBlockPredecessors(Header, ".preheader", OutsideBlocks); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 211 | //===--------------------------------------------------------------------===// |
Misha Brukman | 8b2bd4e | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 212 | // Update analysis results now that we have performed the transformation |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 213 | // |
| 214 | |
| 215 | // We know that we have loop information to update... update it now. |
| 216 | if (Loop *Parent = L->getParentLoop()) |
| 217 | Parent->addBasicBlockToLoop(NewBB, getAnalysis<LoopInfo>()); |
Chris Lattner | f2d9f94 | 2003-02-27 22:48:57 +0000 | [diff] [blame] | 218 | |
| 219 | // If the header for the loop used to be an exit node for another loop, then |
| 220 | // we need to update this to know that the loop-preheader is now the exit |
| 221 | // node. Note that the only loop that could have our header as an exit node |
Chris Lattner | 0895025 | 2003-05-12 22:04:34 +0000 | [diff] [blame] | 222 | // is a sibling loop, ie, one with the same parent loop, or one if it's |
| 223 | // children. |
| 224 | // |
Chris Lattner | f2d9f94 | 2003-02-27 22:48:57 +0000 | [diff] [blame] | 225 | const std::vector<Loop*> *ParentSubLoops; |
| 226 | if (Loop *Parent = L->getParentLoop()) |
| 227 | ParentSubLoops = &Parent->getSubLoops(); |
| 228 | else // Must check top-level loops... |
| 229 | ParentSubLoops = &getAnalysis<LoopInfo>().getTopLevelLoops(); |
| 230 | |
Chris Lattner | 0895025 | 2003-05-12 22:04:34 +0000 | [diff] [blame] | 231 | // Loop over all sibling loops, performing the substitution (recursively to |
| 232 | // include child loops)... |
Chris Lattner | f2d9f94 | 2003-02-27 22:48:57 +0000 | [diff] [blame] | 233 | for (unsigned i = 0, e = ParentSubLoops->size(); i != e; ++i) |
Chris Lattner | 0895025 | 2003-05-12 22:04:34 +0000 | [diff] [blame] | 234 | ChangeExitBlock((*ParentSubLoops)[i], Header, NewBB); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 236 | DominatorSet &DS = getAnalysis<DominatorSet>(); // Update dominator info |
| 237 | { |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 238 | // The blocks that dominate NewBB are the blocks that dominate Header, |
| 239 | // minus Header, plus NewBB. |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 240 | DominatorSet::DomSetType DomSet = DS.getDominators(Header); |
Chris Lattner | 03a9e15 | 2002-09-29 21:41:38 +0000 | [diff] [blame] | 241 | DomSet.insert(NewBB); // We dominate ourself |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 242 | DomSet.erase(Header); // Header does not dominate us... |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 243 | DS.addBasicBlock(NewBB, DomSet); |
Chris Lattner | 03a9e15 | 2002-09-29 21:41:38 +0000 | [diff] [blame] | 244 | |
| 245 | // The newly created basic block dominates all nodes dominated by Header. |
| 246 | for (Function::iterator I = Header->getParent()->begin(), |
| 247 | E = Header->getParent()->end(); I != E; ++I) |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 248 | if (DS.dominates(Header, I)) |
| 249 | DS.addDominator(I, NewBB); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // Update immediate dominator information if we have it... |
| 253 | if (ImmediateDominators *ID = getAnalysisToUpdate<ImmediateDominators>()) { |
| 254 | // Whatever i-dominated the header node now immediately dominates NewBB |
| 255 | ID->addNewBlock(NewBB, ID->get(Header)); |
| 256 | |
| 257 | // The preheader now is the immediate dominator for the header node... |
| 258 | ID->setImmediateDominator(Header, NewBB); |
| 259 | } |
| 260 | |
| 261 | // Update DominatorTree information if it is active. |
| 262 | if (DominatorTree *DT = getAnalysisToUpdate<DominatorTree>()) { |
| 263 | // The immediate dominator of the preheader is the immediate dominator of |
| 264 | // the old header. |
| 265 | // |
| 266 | DominatorTree::Node *HeaderNode = DT->getNode(Header); |
Chris Lattner | 03a9e15 | 2002-09-29 21:41:38 +0000 | [diff] [blame] | 267 | DominatorTree::Node *PHNode = DT->createNewNode(NewBB, |
| 268 | HeaderNode->getIDom()); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 269 | |
| 270 | // Change the header node so that PNHode is the new immediate dominator |
| 271 | DT->changeImmediateDominator(HeaderNode, PHNode); |
| 272 | } |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 273 | |
| 274 | // Update dominance frontier information... |
| 275 | if (DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>()) { |
| 276 | // The DF(NewBB) is just (DF(Header)-Header), because NewBB dominates |
| 277 | // everything that Header does, and it strictly dominates Header in |
| 278 | // addition. |
| 279 | assert(DF->find(Header) != DF->end() && "Header node doesn't have DF set?"); |
| 280 | DominanceFrontier::DomSetType NewDFSet = DF->find(Header)->second; |
| 281 | NewDFSet.erase(Header); |
| 282 | DF->addBasicBlock(NewBB, NewDFSet); |
| 283 | |
| 284 | // Now we must loop over all of the dominance frontiers in the function, |
Misha Brukman | 4ace48e | 2003-09-09 21:54:45 +0000 | [diff] [blame] | 285 | // replacing occurrences of Header with NewBB in some cases. If a block |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 286 | // dominates a (now) predecessor of NewBB, but did not strictly dominate |
| 287 | // Header, it will have Header in it's DF set, but should now have NewBB in |
| 288 | // its set. |
| 289 | for (unsigned i = 0, e = OutsideBlocks.size(); i != e; ++i) { |
| 290 | // Get all of the dominators of the predecessor... |
| 291 | const DominatorSet::DomSetType &PredDoms = |
| 292 | DS.getDominators(OutsideBlocks[i]); |
| 293 | for (DominatorSet::DomSetType::const_iterator PDI = PredDoms.begin(), |
| 294 | PDE = PredDoms.end(); PDI != PDE; ++PDI) { |
| 295 | BasicBlock *PredDom = *PDI; |
| 296 | // If the loop header is in DF(PredDom), then PredDom didn't dominate |
| 297 | // the header but did dominate a predecessor outside of the loop. Now |
| 298 | // we change this entry to include the preheader in the DF instead of |
| 299 | // the header. |
| 300 | DominanceFrontier::iterator DFI = DF->find(PredDom); |
| 301 | assert(DFI != DF->end() && "No dominance frontier for node?"); |
| 302 | if (DFI->second.count(Header)) { |
| 303 | DF->removeFromFrontier(DFI, Header); |
| 304 | DF->addToFrontier(DFI, NewBB); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 311 | void LoopSimplify::RewriteLoopExitBlock(Loop *L, BasicBlock *Exit) { |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 312 | DominatorSet &DS = getAnalysis<DominatorSet>(); |
| 313 | assert(!DS.dominates(L->getHeader(), Exit) && |
| 314 | "Loop already dominates exit block??"); |
Chris Lattner | 10b2b05 | 2003-02-27 22:31:07 +0000 | [diff] [blame] | 315 | assert(std::find(L->getExitBlocks().begin(), L->getExitBlocks().end(), Exit) |
| 316 | != L->getExitBlocks().end() && "Not a current exit block!"); |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 317 | |
| 318 | std::vector<BasicBlock*> LoopBlocks; |
| 319 | for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit); I != E; ++I) |
| 320 | if (L->contains(*I)) |
| 321 | LoopBlocks.push_back(*I); |
| 322 | |
Chris Lattner | 10b2b05 | 2003-02-27 22:31:07 +0000 | [diff] [blame] | 323 | assert(!LoopBlocks.empty() && "No edges coming in from outside the loop?"); |
| 324 | BasicBlock *NewBB = SplitBlockPredecessors(Exit, ".loopexit", LoopBlocks); |
| 325 | |
Chris Lattner | 4e2fbfb | 2003-02-27 21:50:19 +0000 | [diff] [blame] | 326 | // Update Loop Information - we know that the new block will be in the parent |
| 327 | // loop of L. |
| 328 | if (Loop *Parent = L->getParentLoop()) |
| 329 | Parent->addBasicBlockToLoop(NewBB, getAnalysis<LoopInfo>()); |
Chris Lattner | 32a39c2 | 2003-02-28 03:07:54 +0000 | [diff] [blame] | 330 | |
| 331 | // Replace any instances of Exit with NewBB in this and any nested loops... |
| 332 | for (df_iterator<Loop*> I = df_begin(L), E = df_end(L); I != E; ++I) |
Chris Lattner | 49eb0e3 | 2003-02-28 16:54:17 +0000 | [diff] [blame] | 333 | if (I->hasExitBlock(Exit)) |
| 334 | I->changeExitBlock(Exit, NewBB); // Update exit block information |
Chris Lattner | 4e2fbfb | 2003-02-27 21:50:19 +0000 | [diff] [blame] | 335 | |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 336 | // Update dominator information... The blocks that dominate NewBB are the |
| 337 | // intersection of the dominators of predecessors, plus the block itself. |
| 338 | // The newly created basic block does not dominate anything except itself. |
| 339 | // |
| 340 | DominatorSet::DomSetType NewBBDomSet = DS.getDominators(LoopBlocks[0]); |
| 341 | for (unsigned i = 1, e = LoopBlocks.size(); i != e; ++i) |
| 342 | set_intersect(NewBBDomSet, DS.getDominators(LoopBlocks[i])); |
| 343 | NewBBDomSet.insert(NewBB); // All blocks dominate themselves... |
| 344 | DS.addBasicBlock(NewBB, NewBBDomSet); |
| 345 | |
| 346 | // Update immediate dominator information if we have it... |
| 347 | BasicBlock *NewBBIDom = 0; |
| 348 | if (ImmediateDominators *ID = getAnalysisToUpdate<ImmediateDominators>()) { |
| 349 | // This block does not strictly dominate anything, so it is not an immediate |
| 350 | // dominator. To find the immediate dominator of the new exit node, we |
| 351 | // trace up the immediate dominators of a predecessor until we find a basic |
| 352 | // block that dominates the exit block. |
| 353 | // |
| 354 | BasicBlock *Dom = LoopBlocks[0]; // Some random predecessor... |
| 355 | while (!NewBBDomSet.count(Dom)) { // Loop until we find a dominator... |
| 356 | assert(Dom != 0 && "No shared dominator found???"); |
| 357 | Dom = ID->get(Dom); |
| 358 | } |
| 359 | |
| 360 | // Set the immediate dominator now... |
| 361 | ID->addNewBlock(NewBB, Dom); |
| 362 | NewBBIDom = Dom; // Reuse this if calculating DominatorTree info... |
| 363 | } |
| 364 | |
| 365 | // Update DominatorTree information if it is active. |
| 366 | if (DominatorTree *DT = getAnalysisToUpdate<DominatorTree>()) { |
| 367 | // NewBB doesn't dominate anything, so just create a node and link it into |
| 368 | // its immediate dominator. If we don't have ImmediateDominator info |
| 369 | // around, calculate the idom as above. |
| 370 | DominatorTree::Node *NewBBIDomNode; |
| 371 | if (NewBBIDom) { |
| 372 | NewBBIDomNode = DT->getNode(NewBBIDom); |
| 373 | } else { |
| 374 | NewBBIDomNode = DT->getNode(LoopBlocks[0]); // Random pred |
Chris Lattner | bb9d03b | 2003-09-11 16:26:13 +0000 | [diff] [blame] | 375 | while (!NewBBDomSet.count(NewBBIDomNode->getBlock())) { |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 376 | NewBBIDomNode = NewBBIDomNode->getIDom(); |
| 377 | assert(NewBBIDomNode && "No shared dominator found??"); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // Create the new dominator tree node... |
| 382 | DT->createNewNode(NewBB, NewBBIDomNode); |
| 383 | } |
| 384 | |
| 385 | // Update dominance frontier information... |
| 386 | if (DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>()) { |
| 387 | // DF(NewBB) is {Exit} because NewBB does not strictly dominate Exit, but it |
| 388 | // does dominate itself (and there is an edge (NewBB -> Exit)). |
| 389 | DominanceFrontier::DomSetType NewDFSet; |
| 390 | NewDFSet.insert(Exit); |
| 391 | DF->addBasicBlock(NewBB, NewDFSet); |
| 392 | |
| 393 | // Now we must loop over all of the dominance frontiers in the function, |
Misha Brukman | 4ace48e | 2003-09-09 21:54:45 +0000 | [diff] [blame] | 394 | // replacing occurrences of Exit with NewBB in some cases. If a block |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 395 | // dominates a (now) predecessor of NewBB, but did not strictly dominate |
| 396 | // Exit, it will have Exit in it's DF set, but should now have NewBB in its |
| 397 | // set. |
| 398 | for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) { |
| 399 | // Get all of the dominators of the predecessor... |
| 400 | const DominatorSet::DomSetType &PredDoms =DS.getDominators(LoopBlocks[i]); |
| 401 | for (DominatorSet::DomSetType::const_iterator PDI = PredDoms.begin(), |
| 402 | PDE = PredDoms.end(); PDI != PDE; ++PDI) { |
| 403 | BasicBlock *PredDom = *PDI; |
| 404 | // Make sure to only rewrite blocks that are part of the loop... |
| 405 | if (L->contains(PredDom)) { |
| 406 | // If the exit node is in DF(PredDom), then PredDom didn't dominate |
| 407 | // Exit but did dominate a predecessor inside of the loop. Now we |
| 408 | // change this entry to include NewBB in the DF instead of Exit. |
| 409 | DominanceFrontier::iterator DFI = DF->find(PredDom); |
| 410 | assert(DFI != DF->end() && "No dominance frontier for node?"); |
| 411 | if (DFI->second.count(Exit)) { |
| 412 | DF->removeFromFrontier(DFI, Exit); |
| 413 | DF->addToFrontier(DFI, NewBB); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | } |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 418 | } |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 419 | } |