Chris Lattner | 55d4788 | 2003-10-12 21:44:18 +0000 | [diff] [blame] | 1 | //===- LoopSimplify.cpp - Loop Canonicalization 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 | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 10 | // This pass performs several transformations to transform natural loops into a |
| 11 | // simpler form, which makes subsequent analyses and transformations simpler and |
| 12 | // more effective. |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 13 | // |
| 14 | // Loop pre-header insertion guarantees that there is a single, non-critical |
| 15 | // entry edge from outside of the loop to the loop header. This simplifies a |
| 16 | // number of analyses and transformations, such as LICM. |
| 17 | // |
| 18 | // Loop exit-block insertion guarantees that all exit blocks from the loop |
| 19 | // (blocks which are outside of the loop that have predecessors inside of the |
| 20 | // loop) are dominated by the loop header. This simplifies transformations such |
Chris Lattner | a34c477 | 2003-08-18 22:54:06 +0000 | [diff] [blame] | 21 | // as store-sinking that are built into LICM. |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 22 | // |
Chris Lattner | c4622a6 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 23 | // This pass also guarantees that loops will have exactly one backedge. |
| 24 | // |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 25 | // 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] | 26 | // end up being unnecessary, so usage of this pass should not pessimize |
| 27 | // generated code. |
| 28 | // |
| 29 | // This pass obviously modifies the CFG, but updates loop information and |
| 30 | // dominator information. |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 31 | // |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | #include "llvm/Transforms/Scalar.h" |
| 35 | #include "llvm/Analysis/Dominators.h" |
| 36 | #include "llvm/Analysis/LoopInfo.h" |
| 37 | #include "llvm/Function.h" |
| 38 | #include "llvm/iTerminators.h" |
| 39 | #include "llvm/iPHINode.h" |
| 40 | #include "llvm/Constant.h" |
| 41 | #include "llvm/Support/CFG.h" |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 42 | #include "Support/SetOperations.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 43 | #include "Support/Statistic.h" |
Chris Lattner | 32a39c2 | 2003-02-28 03:07:54 +0000 | [diff] [blame] | 44 | #include "Support/DepthFirstIterator.h" |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 45 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame^] | 46 | namespace llvm { |
| 47 | |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 48 | namespace { |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 49 | Statistic<> |
| 50 | NumInserted("loopsimplify", "Number of pre-header blocks inserted"); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 52 | struct LoopSimplify : public FunctionPass { |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 53 | virtual bool runOnFunction(Function &F); |
| 54 | |
| 55 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 56 | // We need loop information to identify the loops... |
| 57 | AU.addRequired<LoopInfo>(); |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 58 | AU.addRequired<DominatorSet>(); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 59 | |
| 60 | AU.addPreserved<LoopInfo>(); |
| 61 | AU.addPreserved<DominatorSet>(); |
| 62 | AU.addPreserved<ImmediateDominators>(); |
| 63 | AU.addPreserved<DominatorTree>(); |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 64 | AU.addPreserved<DominanceFrontier>(); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 65 | AU.addPreservedID(BreakCriticalEdgesID); // No crit edges added.... |
| 66 | } |
| 67 | private: |
| 68 | bool ProcessLoop(Loop *L); |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 69 | BasicBlock *SplitBlockPredecessors(BasicBlock *BB, const char *Suffix, |
| 70 | const std::vector<BasicBlock*> &Preds); |
| 71 | void RewriteLoopExitBlock(Loop *L, BasicBlock *Exit); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 72 | void InsertPreheaderForLoop(Loop *L); |
Chris Lattner | c4622a6 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 73 | void InsertUniqueBackedgeBlock(Loop *L); |
| 74 | |
| 75 | void UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB, |
| 76 | std::vector<BasicBlock*> &PredBlocks); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 79 | RegisterOpt<LoopSimplify> |
| 80 | X("loopsimplify", "Canonicalize natural loops", true); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // Publically exposed interface to pass... |
Chris Lattner | 72272a7 | 2003-10-12 21:52:28 +0000 | [diff] [blame] | 84 | const PassInfo *LoopSimplifyID = X.getPassInfo(); |
| 85 | Pass *createLoopSimplifyPass() { return new LoopSimplify(); } |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 87 | /// runOnFunction - Run down all loops in the CFG (recursively, but we could do |
| 88 | /// it in any convenient order) inserting preheaders... |
| 89 | /// |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 90 | bool LoopSimplify::runOnFunction(Function &F) { |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 91 | bool Changed = false; |
| 92 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 93 | |
| 94 | for (unsigned i = 0, e = LI.getTopLevelLoops().size(); i != e; ++i) |
| 95 | Changed |= ProcessLoop(LI.getTopLevelLoops()[i]); |
| 96 | |
| 97 | return Changed; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /// ProcessLoop - Walk the loop structure in depth first order, ensuring that |
| 102 | /// all loops have preheaders. |
| 103 | /// |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 104 | bool LoopSimplify::ProcessLoop(Loop *L) { |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 105 | bool Changed = false; |
| 106 | |
| 107 | // Does the loop already have a preheader? If so, don't modify the loop... |
| 108 | if (L->getLoopPreheader() == 0) { |
| 109 | InsertPreheaderForLoop(L); |
| 110 | NumInserted++; |
| 111 | Changed = true; |
| 112 | } |
| 113 | |
Chris Lattner | a34c477 | 2003-08-18 22:54:06 +0000 | [diff] [blame] | 114 | // Regardless of whether or not we added a preheader to the loop we must |
| 115 | // guarantee that the preheader dominates all exit nodes. If there are any |
| 116 | // exit nodes not dominated, split them now. |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 117 | DominatorSet &DS = getAnalysis<DominatorSet>(); |
| 118 | BasicBlock *Header = L->getHeader(); |
| 119 | for (unsigned i = 0, e = L->getExitBlocks().size(); i != e; ++i) |
| 120 | if (!DS.dominates(Header, L->getExitBlocks()[i])) { |
| 121 | RewriteLoopExitBlock(L, L->getExitBlocks()[i]); |
Chris Lattner | 10b2b05 | 2003-02-27 22:31:07 +0000 | [diff] [blame] | 122 | assert(DS.dominates(Header, L->getExitBlocks()[i]) && |
| 123 | "RewriteLoopExitBlock failed?"); |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 124 | NumInserted++; |
| 125 | Changed = true; |
| 126 | } |
| 127 | |
Chris Lattner | c4622a6 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 128 | // The preheader may have more than two predecessors at this point (from the |
| 129 | // preheader and from the backedges). To simplify the loop more, insert an |
| 130 | // extra back-edge block in the loop so that there is exactly one backedge. |
| 131 | if (L->getNumBackEdges() != 1) { |
| 132 | InsertUniqueBackedgeBlock(L); |
| 133 | NumInserted++; |
| 134 | Changed = true; |
| 135 | } |
| 136 | |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 137 | const std::vector<Loop*> &SubLoops = L->getSubLoops(); |
| 138 | for (unsigned i = 0, e = SubLoops.size(); i != e; ++i) |
| 139 | Changed |= ProcessLoop(SubLoops[i]); |
| 140 | return Changed; |
| 141 | } |
| 142 | |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 143 | /// SplitBlockPredecessors - Split the specified block into two blocks. We want |
| 144 | /// to move the predecessors specified in the Preds list to point to the new |
| 145 | /// block, leaving the remaining predecessors pointing to BB. This method |
| 146 | /// updates the SSA PHINode's, but no other analyses. |
| 147 | /// |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 148 | BasicBlock *LoopSimplify::SplitBlockPredecessors(BasicBlock *BB, |
| 149 | const char *Suffix, |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 150 | const std::vector<BasicBlock*> &Preds) { |
| 151 | |
| 152 | // Create new basic block, insert right before the original block... |
| 153 | BasicBlock *NewBB = new BasicBlock(BB->getName()+Suffix, BB); |
| 154 | |
| 155 | // The preheader first gets an unconditional branch to the loop header... |
| 156 | BranchInst *BI = new BranchInst(BB); |
| 157 | NewBB->getInstList().push_back(BI); |
| 158 | |
| 159 | // For every PHI node in the block, insert a PHI node into NewBB where the |
| 160 | // incoming values from the out of loop edges are moved to NewBB. We have two |
| 161 | // possible cases here. If the loop is dead, we just insert dummy entries |
| 162 | // into the PHI nodes for the new edge. If the loop is not dead, we move the |
| 163 | // incoming edges in BB into new PHI nodes in NewBB. |
| 164 | // |
| 165 | if (!Preds.empty()) { // Is the loop not obviously dead? |
| 166 | for (BasicBlock::iterator I = BB->begin(); |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 167 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 168 | |
| 169 | // Create the new PHI node, insert it into NewBB at the end of the block |
| 170 | PHINode *NewPHI = new PHINode(PN->getType(), PN->getName()+".ph", BI); |
| 171 | |
| 172 | // Move all of the edges from blocks outside the loop to the new PHI |
| 173 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) { |
| 174 | Value *V = PN->removeIncomingValue(Preds[i]); |
| 175 | NewPHI->addIncoming(V, Preds[i]); |
| 176 | } |
| 177 | |
| 178 | // Add an incoming value to the PHI node in the loop for the preheader |
| 179 | // edge |
| 180 | PN->addIncoming(NewPHI, NewBB); |
| 181 | } |
| 182 | |
| 183 | // Now that the PHI nodes are updated, actually move the edges from |
| 184 | // Preds to point to NewBB instead of BB. |
| 185 | // |
| 186 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) { |
| 187 | TerminatorInst *TI = Preds[i]->getTerminator(); |
| 188 | for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) |
| 189 | if (TI->getSuccessor(s) == BB) |
| 190 | TI->setSuccessor(s, NewBB); |
| 191 | } |
| 192 | |
| 193 | } else { // Otherwise the loop is dead... |
| 194 | for (BasicBlock::iterator I = BB->begin(); |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 195 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 196 | // Insert dummy values as the incoming value... |
| 197 | PN->addIncoming(Constant::getNullValue(PN->getType()), NewBB); |
| 198 | } |
| 199 | return NewBB; |
| 200 | } |
| 201 | |
Chris Lattner | 0895025 | 2003-05-12 22:04:34 +0000 | [diff] [blame] | 202 | // ChangeExitBlock - This recursive function is used to change any exit blocks |
| 203 | // that use OldExit to use NewExit instead. This is recursive because children |
| 204 | // may need to be processed as well. |
| 205 | // |
| 206 | static void ChangeExitBlock(Loop *L, BasicBlock *OldExit, BasicBlock *NewExit) { |
| 207 | if (L->hasExitBlock(OldExit)) { |
| 208 | L->changeExitBlock(OldExit, NewExit); |
| 209 | const std::vector<Loop*> &SubLoops = L->getSubLoops(); |
| 210 | for (unsigned i = 0, e = SubLoops.size(); i != e; ++i) |
| 211 | ChangeExitBlock(SubLoops[i], OldExit, NewExit); |
| 212 | } |
| 213 | } |
| 214 | |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 215 | |
| 216 | /// InsertPreheaderForLoop - Once we discover that a loop doesn't have a |
| 217 | /// preheader, this method is called to insert one. This method has two phases: |
| 218 | /// preheader insertion and analysis updating. |
| 219 | /// |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 220 | void LoopSimplify::InsertPreheaderForLoop(Loop *L) { |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 221 | BasicBlock *Header = L->getHeader(); |
| 222 | |
| 223 | // Compute the set of predecessors of the loop that are not in the loop. |
| 224 | std::vector<BasicBlock*> OutsideBlocks; |
| 225 | for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header); |
| 226 | PI != PE; ++PI) |
| 227 | if (!L->contains(*PI)) // Coming in from outside the loop? |
| 228 | OutsideBlocks.push_back(*PI); // Keep track of it... |
| 229 | |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 230 | // Split out the loop pre-header |
| 231 | BasicBlock *NewBB = |
| 232 | SplitBlockPredecessors(Header, ".preheader", OutsideBlocks); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 233 | |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 234 | //===--------------------------------------------------------------------===// |
Misha Brukman | 8b2bd4e | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 235 | // Update analysis results now that we have performed the transformation |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 236 | // |
| 237 | |
| 238 | // We know that we have loop information to update... update it now. |
| 239 | if (Loop *Parent = L->getParentLoop()) |
| 240 | Parent->addBasicBlockToLoop(NewBB, getAnalysis<LoopInfo>()); |
Chris Lattner | f2d9f94 | 2003-02-27 22:48:57 +0000 | [diff] [blame] | 241 | |
| 242 | // If the header for the loop used to be an exit node for another loop, then |
| 243 | // we need to update this to know that the loop-preheader is now the exit |
| 244 | // 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] | 245 | // is a sibling loop, ie, one with the same parent loop, or one if it's |
| 246 | // children. |
| 247 | // |
Chris Lattner | f2d9f94 | 2003-02-27 22:48:57 +0000 | [diff] [blame] | 248 | const std::vector<Loop*> *ParentSubLoops; |
| 249 | if (Loop *Parent = L->getParentLoop()) |
| 250 | ParentSubLoops = &Parent->getSubLoops(); |
| 251 | else // Must check top-level loops... |
| 252 | ParentSubLoops = &getAnalysis<LoopInfo>().getTopLevelLoops(); |
| 253 | |
Chris Lattner | 0895025 | 2003-05-12 22:04:34 +0000 | [diff] [blame] | 254 | // Loop over all sibling loops, performing the substitution (recursively to |
| 255 | // include child loops)... |
Chris Lattner | f2d9f94 | 2003-02-27 22:48:57 +0000 | [diff] [blame] | 256 | for (unsigned i = 0, e = ParentSubLoops->size(); i != e; ++i) |
Chris Lattner | 0895025 | 2003-05-12 22:04:34 +0000 | [diff] [blame] | 257 | ChangeExitBlock((*ParentSubLoops)[i], Header, NewBB); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 259 | DominatorSet &DS = getAnalysis<DominatorSet>(); // Update dominator info |
| 260 | { |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 261 | // The blocks that dominate NewBB are the blocks that dominate Header, |
| 262 | // minus Header, plus NewBB. |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 263 | DominatorSet::DomSetType DomSet = DS.getDominators(Header); |
Chris Lattner | 03a9e15 | 2002-09-29 21:41:38 +0000 | [diff] [blame] | 264 | DomSet.insert(NewBB); // We dominate ourself |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 265 | DomSet.erase(Header); // Header does not dominate us... |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 266 | DS.addBasicBlock(NewBB, DomSet); |
Chris Lattner | 03a9e15 | 2002-09-29 21:41:38 +0000 | [diff] [blame] | 267 | |
| 268 | // The newly created basic block dominates all nodes dominated by Header. |
| 269 | for (Function::iterator I = Header->getParent()->begin(), |
| 270 | E = Header->getParent()->end(); I != E; ++I) |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 271 | if (DS.dominates(Header, I)) |
| 272 | DS.addDominator(I, NewBB); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // Update immediate dominator information if we have it... |
| 276 | if (ImmediateDominators *ID = getAnalysisToUpdate<ImmediateDominators>()) { |
| 277 | // Whatever i-dominated the header node now immediately dominates NewBB |
| 278 | ID->addNewBlock(NewBB, ID->get(Header)); |
| 279 | |
| 280 | // The preheader now is the immediate dominator for the header node... |
| 281 | ID->setImmediateDominator(Header, NewBB); |
| 282 | } |
| 283 | |
| 284 | // Update DominatorTree information if it is active. |
| 285 | if (DominatorTree *DT = getAnalysisToUpdate<DominatorTree>()) { |
| 286 | // The immediate dominator of the preheader is the immediate dominator of |
| 287 | // the old header. |
| 288 | // |
| 289 | DominatorTree::Node *HeaderNode = DT->getNode(Header); |
Chris Lattner | 03a9e15 | 2002-09-29 21:41:38 +0000 | [diff] [blame] | 290 | DominatorTree::Node *PHNode = DT->createNewNode(NewBB, |
| 291 | HeaderNode->getIDom()); |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 292 | |
| 293 | // Change the header node so that PNHode is the new immediate dominator |
| 294 | DT->changeImmediateDominator(HeaderNode, PHNode); |
| 295 | } |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 296 | |
| 297 | // Update dominance frontier information... |
| 298 | if (DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>()) { |
| 299 | // The DF(NewBB) is just (DF(Header)-Header), because NewBB dominates |
| 300 | // everything that Header does, and it strictly dominates Header in |
| 301 | // addition. |
| 302 | assert(DF->find(Header) != DF->end() && "Header node doesn't have DF set?"); |
| 303 | DominanceFrontier::DomSetType NewDFSet = DF->find(Header)->second; |
| 304 | NewDFSet.erase(Header); |
| 305 | DF->addBasicBlock(NewBB, NewDFSet); |
| 306 | |
| 307 | // 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] | 308 | // replacing occurrences of Header with NewBB in some cases. If a block |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 309 | // dominates a (now) predecessor of NewBB, but did not strictly dominate |
| 310 | // Header, it will have Header in it's DF set, but should now have NewBB in |
| 311 | // its set. |
| 312 | for (unsigned i = 0, e = OutsideBlocks.size(); i != e; ++i) { |
| 313 | // Get all of the dominators of the predecessor... |
| 314 | const DominatorSet::DomSetType &PredDoms = |
| 315 | DS.getDominators(OutsideBlocks[i]); |
| 316 | for (DominatorSet::DomSetType::const_iterator PDI = PredDoms.begin(), |
| 317 | PDE = PredDoms.end(); PDI != PDE; ++PDI) { |
| 318 | BasicBlock *PredDom = *PDI; |
| 319 | // If the loop header is in DF(PredDom), then PredDom didn't dominate |
| 320 | // the header but did dominate a predecessor outside of the loop. Now |
| 321 | // we change this entry to include the preheader in the DF instead of |
| 322 | // the header. |
| 323 | DominanceFrontier::iterator DFI = DF->find(PredDom); |
| 324 | assert(DFI != DF->end() && "No dominance frontier for node?"); |
| 325 | if (DFI->second.count(Header)) { |
| 326 | DF->removeFromFrontier(DFI, Header); |
| 327 | DF->addToFrontier(DFI, NewBB); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
Chris Lattner | 154e4d5 | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 334 | void LoopSimplify::RewriteLoopExitBlock(Loop *L, BasicBlock *Exit) { |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 335 | DominatorSet &DS = getAnalysis<DominatorSet>(); |
| 336 | assert(!DS.dominates(L->getHeader(), Exit) && |
| 337 | "Loop already dominates exit block??"); |
Chris Lattner | 10b2b05 | 2003-02-27 22:31:07 +0000 | [diff] [blame] | 338 | assert(std::find(L->getExitBlocks().begin(), L->getExitBlocks().end(), Exit) |
| 339 | != L->getExitBlocks().end() && "Not a current exit block!"); |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 340 | |
| 341 | std::vector<BasicBlock*> LoopBlocks; |
| 342 | for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit); I != E; ++I) |
| 343 | if (L->contains(*I)) |
| 344 | LoopBlocks.push_back(*I); |
| 345 | |
Chris Lattner | 10b2b05 | 2003-02-27 22:31:07 +0000 | [diff] [blame] | 346 | assert(!LoopBlocks.empty() && "No edges coming in from outside the loop?"); |
| 347 | BasicBlock *NewBB = SplitBlockPredecessors(Exit, ".loopexit", LoopBlocks); |
| 348 | |
Chris Lattner | 4e2fbfb | 2003-02-27 21:50:19 +0000 | [diff] [blame] | 349 | // Update Loop Information - we know that the new block will be in the parent |
| 350 | // loop of L. |
| 351 | if (Loop *Parent = L->getParentLoop()) |
| 352 | Parent->addBasicBlockToLoop(NewBB, getAnalysis<LoopInfo>()); |
Chris Lattner | 32a39c2 | 2003-02-28 03:07:54 +0000 | [diff] [blame] | 353 | |
| 354 | // Replace any instances of Exit with NewBB in this and any nested loops... |
| 355 | 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] | 356 | if (I->hasExitBlock(Exit)) |
| 357 | I->changeExitBlock(Exit, NewBB); // Update exit block information |
Chris Lattner | 4e2fbfb | 2003-02-27 21:50:19 +0000 | [diff] [blame] | 358 | |
Chris Lattner | c4622a6 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 359 | // Update dominator information (set, immdom, domtree, and domfrontier) |
| 360 | UpdateDomInfoForRevectoredPreds(NewBB, LoopBlocks); |
| 361 | } |
| 362 | |
| 363 | /// InsertUniqueBackedgeBlock - This method is called when the specified loop |
| 364 | /// has more than one backedge in it. If this occurs, revector all of these |
| 365 | /// backedges to target a new basic block and have that block branch to the loop |
| 366 | /// header. This ensures that loops have exactly one backedge. |
| 367 | /// |
| 368 | void LoopSimplify::InsertUniqueBackedgeBlock(Loop *L) { |
| 369 | assert(L->getNumBackEdges() > 1 && "Must have > 1 backedge!"); |
| 370 | |
| 371 | // Get information about the loop |
| 372 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 373 | BasicBlock *Header = L->getHeader(); |
| 374 | Function *F = Header->getParent(); |
| 375 | |
| 376 | // Figure out which basic blocks contain back-edges to the loop header. |
| 377 | std::vector<BasicBlock*> BackedgeBlocks; |
| 378 | for (pred_iterator I = pred_begin(Header), E = pred_end(Header); I != E; ++I) |
| 379 | if (*I != Preheader) BackedgeBlocks.push_back(*I); |
| 380 | |
| 381 | // Create and insert the new backedge block... |
| 382 | BasicBlock *BEBlock = new BasicBlock(Header->getName()+".backedge", F); |
| 383 | Instruction *BETerminator = new BranchInst(Header); |
| 384 | BEBlock->getInstList().push_back(BETerminator); |
| 385 | |
| 386 | // Move the new backedge block to right after the last backedge block. |
| 387 | Function::iterator InsertPos = BackedgeBlocks.back(); ++InsertPos; |
| 388 | F->getBasicBlockList().splice(InsertPos, F->getBasicBlockList(), BEBlock); |
| 389 | |
| 390 | // Now that the block has been inserted into the function, create PHI nodes in |
| 391 | // the backedge block which correspond to any PHI nodes in the header block. |
| 392 | for (BasicBlock::iterator I = Header->begin(); |
| 393 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 394 | PHINode *NewPN = new PHINode(PN->getType(), PN->getName()+".be", |
| 395 | BETerminator); |
| 396 | NewPN->op_reserve(2*BackedgeBlocks.size()); |
| 397 | |
| 398 | // Loop over the PHI node, moving all entries except the one for the |
| 399 | // preheader over to the new PHI node. |
| 400 | unsigned PreheaderIdx = ~0U; |
| 401 | bool HasUniqueIncomingValue = true; |
| 402 | Value *UniqueValue = 0; |
| 403 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 404 | BasicBlock *IBB = PN->getIncomingBlock(i); |
| 405 | Value *IV = PN->getIncomingValue(i); |
| 406 | if (IBB == Preheader) { |
| 407 | PreheaderIdx = i; |
| 408 | } else { |
| 409 | NewPN->addIncoming(IV, IBB); |
| 410 | if (HasUniqueIncomingValue) { |
| 411 | if (UniqueValue == 0) |
| 412 | UniqueValue = IV; |
| 413 | else if (UniqueValue != IV) |
| 414 | HasUniqueIncomingValue = false; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // Delete all of the incoming values from the old PN except the preheader's |
| 420 | assert(PreheaderIdx != ~0U && "PHI has no preheader entry??"); |
| 421 | if (PreheaderIdx != 0) { |
| 422 | PN->setIncomingValue(0, PN->getIncomingValue(PreheaderIdx)); |
| 423 | PN->setIncomingBlock(0, PN->getIncomingBlock(PreheaderIdx)); |
| 424 | } |
| 425 | PN->op_erase(PN->op_begin()+2, PN->op_end()); |
| 426 | |
| 427 | // Finally, add the newly constructed PHI node as the entry for the BEBlock. |
| 428 | PN->addIncoming(NewPN, BEBlock); |
| 429 | |
| 430 | // As an optimization, if all incoming values in the new PhiNode (which is a |
| 431 | // subset of the incoming values of the old PHI node) have the same value, |
| 432 | // eliminate the PHI Node. |
| 433 | if (HasUniqueIncomingValue) { |
| 434 | NewPN->replaceAllUsesWith(UniqueValue); |
| 435 | BEBlock->getInstList().erase(NewPN); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // Now that all of the PHI nodes have been inserted and adjusted, modify the |
| 440 | // backedge blocks to just to the BEBlock instead of the header. |
| 441 | for (unsigned i = 0, e = BackedgeBlocks.size(); i != e; ++i) { |
| 442 | TerminatorInst *TI = BackedgeBlocks[i]->getTerminator(); |
| 443 | for (unsigned Op = 0, e = TI->getNumSuccessors(); Op != e; ++Op) |
| 444 | if (TI->getSuccessor(Op) == Header) |
| 445 | TI->setSuccessor(Op, BEBlock); |
| 446 | } |
| 447 | |
| 448 | //===--- Update all analyses which we must preserve now -----------------===// |
| 449 | |
| 450 | // Update Loop Information - we know that this block is now in the current |
| 451 | // loop and all parent loops. |
| 452 | L->addBasicBlockToLoop(BEBlock, getAnalysis<LoopInfo>()); |
| 453 | |
| 454 | // Replace any instances of Exit with NewBB in this and any nested loops... |
| 455 | for (df_iterator<Loop*> I = df_begin(L), E = df_end(L); I != E; ++I) |
| 456 | if (I->hasExitBlock(Header)) |
| 457 | I->changeExitBlock(Header, BEBlock); // Update exit block information |
| 458 | |
| 459 | // Update dominator information (set, immdom, domtree, and domfrontier) |
| 460 | UpdateDomInfoForRevectoredPreds(BEBlock, BackedgeBlocks); |
| 461 | } |
| 462 | |
| 463 | /// UpdateDomInfoForRevectoredPreds - This method is used to update the four |
| 464 | /// different kinds of dominator information (dominator sets, immediate |
| 465 | /// dominators, dominator trees, and dominance frontiers) after a new block has |
| 466 | /// been added to the CFG. |
| 467 | /// |
| 468 | /// This only supports the case when an existing block (known as "Exit"), had |
| 469 | /// some of its predecessors factored into a new basic block. This |
| 470 | /// transformation inserts a new basic block ("NewBB"), with a single |
| 471 | /// unconditional branch to Exit, and moves some predecessors of "Exit" to now |
| 472 | /// branch to NewBB. These predecessors are listed in PredBlocks, even though |
| 473 | /// they are the same as pred_begin(NewBB)/pred_end(NewBB). |
| 474 | /// |
| 475 | void LoopSimplify::UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB, |
| 476 | std::vector<BasicBlock*> &PredBlocks) { |
| 477 | assert(succ_begin(NewBB) != succ_end(NewBB) && |
| 478 | ++succ_begin(NewBB) == succ_end(NewBB) && |
| 479 | "NewBB should have a single successor!"); |
| 480 | DominatorSet &DS = getAnalysis<DominatorSet>(); |
| 481 | |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 482 | // Update dominator information... The blocks that dominate NewBB are the |
| 483 | // intersection of the dominators of predecessors, plus the block itself. |
| 484 | // The newly created basic block does not dominate anything except itself. |
| 485 | // |
Chris Lattner | c4622a6 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 486 | DominatorSet::DomSetType NewBBDomSet = DS.getDominators(PredBlocks[0]); |
| 487 | for (unsigned i = 1, e = PredBlocks.size(); i != e; ++i) |
| 488 | set_intersect(NewBBDomSet, DS.getDominators(PredBlocks[i])); |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 489 | NewBBDomSet.insert(NewBB); // All blocks dominate themselves... |
| 490 | DS.addBasicBlock(NewBB, NewBBDomSet); |
| 491 | |
| 492 | // Update immediate dominator information if we have it... |
| 493 | BasicBlock *NewBBIDom = 0; |
| 494 | if (ImmediateDominators *ID = getAnalysisToUpdate<ImmediateDominators>()) { |
| 495 | // This block does not strictly dominate anything, so it is not an immediate |
| 496 | // dominator. To find the immediate dominator of the new exit node, we |
| 497 | // trace up the immediate dominators of a predecessor until we find a basic |
| 498 | // block that dominates the exit block. |
| 499 | // |
Chris Lattner | c4622a6 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 500 | BasicBlock *Dom = PredBlocks[0]; // Some random predecessor... |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 501 | while (!NewBBDomSet.count(Dom)) { // Loop until we find a dominator... |
| 502 | assert(Dom != 0 && "No shared dominator found???"); |
| 503 | Dom = ID->get(Dom); |
| 504 | } |
| 505 | |
| 506 | // Set the immediate dominator now... |
| 507 | ID->addNewBlock(NewBB, Dom); |
| 508 | NewBBIDom = Dom; // Reuse this if calculating DominatorTree info... |
| 509 | } |
| 510 | |
| 511 | // Update DominatorTree information if it is active. |
| 512 | if (DominatorTree *DT = getAnalysisToUpdate<DominatorTree>()) { |
| 513 | // NewBB doesn't dominate anything, so just create a node and link it into |
| 514 | // its immediate dominator. If we don't have ImmediateDominator info |
| 515 | // around, calculate the idom as above. |
| 516 | DominatorTree::Node *NewBBIDomNode; |
| 517 | if (NewBBIDom) { |
| 518 | NewBBIDomNode = DT->getNode(NewBBIDom); |
| 519 | } else { |
Chris Lattner | c4622a6 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 520 | NewBBIDomNode = DT->getNode(PredBlocks[0]); // Random pred |
Chris Lattner | bb9d03b | 2003-09-11 16:26:13 +0000 | [diff] [blame] | 521 | while (!NewBBDomSet.count(NewBBIDomNode->getBlock())) { |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 522 | NewBBIDomNode = NewBBIDomNode->getIDom(); |
| 523 | assert(NewBBIDomNode && "No shared dominator found??"); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // Create the new dominator tree node... |
| 528 | DT->createNewNode(NewBB, NewBBIDomNode); |
| 529 | } |
| 530 | |
| 531 | // Update dominance frontier information... |
| 532 | if (DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>()) { |
| 533 | // DF(NewBB) is {Exit} because NewBB does not strictly dominate Exit, but it |
Chris Lattner | c4622a6 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 534 | // does dominate itself (and there is an edge (NewBB -> Exit)). Exit is the |
| 535 | // single successor of NewBB. |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 536 | DominanceFrontier::DomSetType NewDFSet; |
Chris Lattner | c4622a6 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 537 | BasicBlock *Exit = *succ_begin(NewBB); |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 538 | NewDFSet.insert(Exit); |
| 539 | DF->addBasicBlock(NewBB, NewDFSet); |
| 540 | |
| 541 | // Now we must loop over all of the dominance frontiers in the function, |
Chris Lattner | c4622a6 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 542 | // replacing occurrences of Exit with NewBB in some cases. All blocks that |
| 543 | // dominate a block in PredBlocks and contained Exit in their dominance |
| 544 | // frontier must be updated to contain NewBB instead. This only occurs if |
| 545 | // there is more than one block in PredBlocks. |
| 546 | // |
| 547 | if (PredBlocks.size() > 1) { |
| 548 | for (unsigned i = 0, e = PredBlocks.size(); i != e; ++i) { |
| 549 | BasicBlock *Pred = PredBlocks[i]; |
| 550 | // Get all of the dominators of the predecessor... |
| 551 | const DominatorSet::DomSetType &PredDoms = DS.getDominators(Pred); |
| 552 | for (DominatorSet::DomSetType::const_iterator PDI = PredDoms.begin(), |
| 553 | PDE = PredDoms.end(); PDI != PDE; ++PDI) { |
| 554 | BasicBlock *PredDom = *PDI; |
| 555 | |
| 556 | // If the Exit node is in DF(PredDom), then PredDom didn't dominate |
| 557 | // Exit but did dominate a predecessor of it. Now we change this |
| 558 | // entry to include NewBB in the DF instead of Exit. |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 559 | DominanceFrontier::iterator DFI = DF->find(PredDom); |
| 560 | assert(DFI != DF->end() && "No dominance frontier for node?"); |
| 561 | if (DFI->second.count(Exit)) { |
| 562 | DF->removeFromFrontier(DFI, Exit); |
| 563 | DF->addToFrontier(DFI, NewBB); |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | } |
Chris Lattner | 650096a | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 568 | } |
Chris Lattner | 61992f6 | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 569 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame^] | 570 | |
| 571 | } // End llvm namespace |