Chris Lattner | 67a9801 | 2003-10-12 21:44:18 +0000 | [diff] [blame] | 1 | //===- LoopSimplify.cpp - Loop Canonicalization Pass ----------------------===// |
John Criswell | b576c94 | 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 | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | ee2c50c | 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 | dbf3cd7 | 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 |
Chris Lattner | 66ea98e | 2003-12-10 17:20:35 +0000 | [diff] [blame] | 20 | // loop) only have predecessors from inside of the loop (and are thus dominated |
| 21 | // by the loop header). This simplifies transformations such as store-sinking |
| 22 | // that are built into LICM. |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 23 | // |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 24 | // This pass also guarantees that loops will have exactly one backedge. |
| 25 | // |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 26 | // Note that the simplifycfg pass will clean up blocks which are split out but |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 27 | // end up being unnecessary, so usage of this pass should not pessimize |
| 28 | // generated code. |
| 29 | // |
| 30 | // This pass obviously modifies the CFG, but updates loop information and |
| 31 | // dominator information. |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 32 | // |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 2ef703e | 2004-03-14 03:59:22 +0000 | [diff] [blame] | 36 | #include "llvm/Constant.h" |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 37 | #include "llvm/iTerminators.h" |
| 38 | #include "llvm/iPHINode.h" |
Chris Lattner | 2ef703e | 2004-03-14 03:59:22 +0000 | [diff] [blame] | 39 | #include "llvm/Function.h" |
| 40 | #include "llvm/Type.h" |
Chris Lattner | 0f98e75 | 2003-12-19 06:27:08 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/Dominators.h" |
| 42 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 43 | #include "llvm/Support/CFG.h" |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 44 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 45 | #include "Support/SetOperations.h" |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 46 | #include "Support/Statistic.h" |
Chris Lattner | 74cd04e | 2003-02-28 03:07:54 +0000 | [diff] [blame] | 47 | #include "Support/DepthFirstIterator.h" |
Chris Lattner | 66ea98e | 2003-12-10 17:20:35 +0000 | [diff] [blame] | 48 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 50 | namespace { |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 51 | Statistic<> |
Chris Lattner | 66ea98e | 2003-12-10 17:20:35 +0000 | [diff] [blame] | 52 | NumInserted("loopsimplify", "Number of pre-header or exit blocks inserted"); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 53 | Statistic<> |
| 54 | NumNested("loopsimplify", "Number of nested loops split out"); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 55 | |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 56 | struct LoopSimplify : public FunctionPass { |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 57 | virtual bool runOnFunction(Function &F); |
| 58 | |
| 59 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 60 | // We need loop information to identify the loops... |
| 61 | AU.addRequired<LoopInfo>(); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 62 | AU.addRequired<DominatorSet>(); |
Chris Lattner | 786c564 | 2004-03-13 22:01:26 +0000 | [diff] [blame] | 63 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 64 | |
| 65 | AU.addPreserved<LoopInfo>(); |
| 66 | AU.addPreserved<DominatorSet>(); |
| 67 | AU.addPreserved<ImmediateDominators>(); |
| 68 | AU.addPreserved<DominatorTree>(); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 69 | AU.addPreserved<DominanceFrontier>(); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 70 | AU.addPreservedID(BreakCriticalEdgesID); // No crit edges added.... |
| 71 | } |
| 72 | private: |
| 73 | bool ProcessLoop(Loop *L); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 74 | BasicBlock *SplitBlockPredecessors(BasicBlock *BB, const char *Suffix, |
| 75 | const std::vector<BasicBlock*> &Preds); |
| 76 | void RewriteLoopExitBlock(Loop *L, BasicBlock *Exit); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 77 | void InsertPreheaderForLoop(Loop *L); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 78 | Loop *SeparateNestedLoop(Loop *L); |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 79 | void InsertUniqueBackedgeBlock(Loop *L); |
| 80 | |
| 81 | void UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB, |
| 82 | std::vector<BasicBlock*> &PredBlocks); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 85 | RegisterOpt<LoopSimplify> |
| 86 | X("loopsimplify", "Canonicalize natural loops", true); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Publically exposed interface to pass... |
Chris Lattner | 66ea98e | 2003-12-10 17:20:35 +0000 | [diff] [blame] | 90 | const PassInfo *llvm::LoopSimplifyID = X.getPassInfo(); |
| 91 | Pass *llvm::createLoopSimplifyPass() { return new LoopSimplify(); } |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 93 | /// runOnFunction - Run down all loops in the CFG (recursively, but we could do |
| 94 | /// it in any convenient order) inserting preheaders... |
| 95 | /// |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 96 | bool LoopSimplify::runOnFunction(Function &F) { |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 97 | bool Changed = false; |
| 98 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 99 | |
Chris Lattner | 329c1c6 | 2004-01-08 00:09:44 +0000 | [diff] [blame] | 100 | for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I) |
| 101 | Changed |= ProcessLoop(*I); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 102 | |
| 103 | return Changed; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /// ProcessLoop - Walk the loop structure in depth first order, ensuring that |
| 108 | /// all loops have preheaders. |
| 109 | /// |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 110 | bool LoopSimplify::ProcessLoop(Loop *L) { |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 111 | bool Changed = false; |
| 112 | |
Chris Lattner | 2ef703e | 2004-03-14 03:59:22 +0000 | [diff] [blame] | 113 | // Check to see that no blocks (other than the header) in the loop have |
| 114 | // predecessors that are not in the loop. This is not valid for natural |
| 115 | // loops, but can occur if the blocks are unreachable. Since they are |
| 116 | // unreachable we can just shamelessly destroy their terminators to make them |
| 117 | // not branch into the loop! |
| 118 | assert(L->getBlocks()[0] == L->getHeader() && |
| 119 | "Header isn't first block in loop?"); |
| 120 | for (unsigned i = 1, e = L->getBlocks().size(); i != e; ++i) { |
| 121 | BasicBlock *LoopBB = L->getBlocks()[i]; |
| 122 | Retry: |
| 123 | for (pred_iterator PI = pred_begin(LoopBB), E = pred_end(LoopBB); |
| 124 | PI != E; ++PI) |
| 125 | if (!L->contains(*PI)) { |
| 126 | // This predecessor is not in the loop. Kill its terminator! |
| 127 | BasicBlock *DeadBlock = *PI; |
| 128 | for (succ_iterator SI = succ_begin(DeadBlock), E = succ_end(DeadBlock); |
| 129 | SI != E; ++SI) |
| 130 | (*SI)->removePredecessor(DeadBlock); // Remove PHI node entries |
| 131 | |
| 132 | // Delete the dead terminator. |
| 133 | DeadBlock->getInstList().pop_back(); |
| 134 | |
| 135 | Value *RetVal = 0; |
| 136 | if (LoopBB->getParent()->getReturnType() != Type::VoidTy) |
| 137 | RetVal = Constant::getNullValue(LoopBB->getParent()->getReturnType()); |
| 138 | new ReturnInst(RetVal, DeadBlock); |
| 139 | goto Retry; // We just invalidated the pred_iterator. Retry. |
| 140 | } |
| 141 | } |
| 142 | |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 143 | // Does the loop already have a preheader? If so, don't modify the loop... |
| 144 | if (L->getLoopPreheader() == 0) { |
| 145 | InsertPreheaderForLoop(L); |
| 146 | NumInserted++; |
| 147 | Changed = true; |
| 148 | } |
| 149 | |
Chris Lattner | 66ea98e | 2003-12-10 17:20:35 +0000 | [diff] [blame] | 150 | // Next, check to make sure that all exit nodes of the loop only have |
| 151 | // predecessors that are inside of the loop. This check guarantees that the |
| 152 | // loop preheader/header will dominate the exit blocks. If the exit block has |
| 153 | // predecessors from outside of the loop, split the edge now. |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame^] | 154 | std::vector<BasicBlock*> ExitBlocks; |
| 155 | L->getExitBlocks(ExitBlocks); |
| 156 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
| 157 | BasicBlock *ExitBlock = ExitBlocks[i]; |
Chris Lattner | 66ea98e | 2003-12-10 17:20:35 +0000 | [diff] [blame] | 158 | for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock); |
| 159 | PI != PE; ++PI) |
| 160 | if (!L->contains(*PI)) { |
| 161 | RewriteLoopExitBlock(L, ExitBlock); |
| 162 | NumInserted++; |
| 163 | Changed = true; |
| 164 | break; |
| 165 | } |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 168 | // If the header has more than two predecessors at this point (from the |
| 169 | // preheader and from multiple backedges), we must adjust the loop. |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 170 | if (L->getNumBackEdges() != 1) { |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 171 | // If this is really a nested loop, rip it out into a child loop. |
| 172 | if (Loop *NL = SeparateNestedLoop(L)) { |
| 173 | ++NumNested; |
| 174 | // This is a big restructuring change, reprocess the whole loop. |
| 175 | ProcessLoop(NL); |
| 176 | return true; |
| 177 | } |
| 178 | |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 179 | InsertUniqueBackedgeBlock(L); |
| 180 | NumInserted++; |
| 181 | Changed = true; |
| 182 | } |
| 183 | |
Chris Lattner | 329c1c6 | 2004-01-08 00:09:44 +0000 | [diff] [blame] | 184 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 185 | Changed |= ProcessLoop(*I); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 186 | return Changed; |
| 187 | } |
| 188 | |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 189 | /// SplitBlockPredecessors - Split the specified block into two blocks. We want |
| 190 | /// to move the predecessors specified in the Preds list to point to the new |
| 191 | /// block, leaving the remaining predecessors pointing to BB. This method |
| 192 | /// updates the SSA PHINode's, but no other analyses. |
| 193 | /// |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 194 | BasicBlock *LoopSimplify::SplitBlockPredecessors(BasicBlock *BB, |
| 195 | const char *Suffix, |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 196 | const std::vector<BasicBlock*> &Preds) { |
| 197 | |
| 198 | // Create new basic block, insert right before the original block... |
Chris Lattner | c24a076 | 2004-02-04 03:58:28 +0000 | [diff] [blame] | 199 | BasicBlock *NewBB = new BasicBlock(BB->getName()+Suffix, BB->getParent(), BB); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 200 | |
| 201 | // The preheader first gets an unconditional branch to the loop header... |
Chris Lattner | 108e4ab | 2003-11-21 16:52:05 +0000 | [diff] [blame] | 202 | BranchInst *BI = new BranchInst(BB, NewBB); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 203 | |
| 204 | // For every PHI node in the block, insert a PHI node into NewBB where the |
| 205 | // incoming values from the out of loop edges are moved to NewBB. We have two |
| 206 | // possible cases here. If the loop is dead, we just insert dummy entries |
| 207 | // into the PHI nodes for the new edge. If the loop is not dead, we move the |
| 208 | // incoming edges in BB into new PHI nodes in NewBB. |
| 209 | // |
| 210 | if (!Preds.empty()) { // Is the loop not obviously dead? |
Chris Lattner | 0f98e75 | 2003-12-19 06:27:08 +0000 | [diff] [blame] | 211 | // Check to see if the values being merged into the new block need PHI |
| 212 | // nodes. If so, insert them. |
| 213 | for (BasicBlock::iterator I = BB->begin(); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 214 | PHINode *PN = dyn_cast<PHINode>(I); ) { |
| 215 | ++I; |
| 216 | |
Chris Lattner | 0f98e75 | 2003-12-19 06:27:08 +0000 | [diff] [blame] | 217 | // Check to see if all of the values coming in are the same. If so, we |
| 218 | // don't need to create a new PHI node. |
| 219 | Value *InVal = PN->getIncomingValueForBlock(Preds[0]); |
| 220 | for (unsigned i = 1, e = Preds.size(); i != e; ++i) |
| 221 | if (InVal != PN->getIncomingValueForBlock(Preds[i])) { |
| 222 | InVal = 0; |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | // If the values coming into the block are not the same, we need a PHI. |
| 227 | if (InVal == 0) { |
Chris Lattner | 010ba10 | 2003-12-09 23:12:55 +0000 | [diff] [blame] | 228 | // Create the new PHI node, insert it into NewBB at the end of the block |
| 229 | PHINode *NewPHI = new PHINode(PN->getType(), PN->getName()+".ph", BI); |
| 230 | |
| 231 | // Move all of the edges from blocks outside the loop to the new PHI |
| 232 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) { |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 233 | Value *V = PN->removeIncomingValue(Preds[i], false); |
Chris Lattner | 010ba10 | 2003-12-09 23:12:55 +0000 | [diff] [blame] | 234 | NewPHI->addIncoming(V, Preds[i]); |
| 235 | } |
Chris Lattner | 0f98e75 | 2003-12-19 06:27:08 +0000 | [diff] [blame] | 236 | InVal = NewPHI; |
| 237 | } else { |
| 238 | // Remove all of the edges coming into the PHI nodes from outside of the |
| 239 | // block. |
| 240 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) |
| 241 | PN->removeIncomingValue(Preds[i], false); |
Chris Lattner | 010ba10 | 2003-12-09 23:12:55 +0000 | [diff] [blame] | 242 | } |
Chris Lattner | 0f98e75 | 2003-12-19 06:27:08 +0000 | [diff] [blame] | 243 | |
| 244 | // Add an incoming value to the PHI node in the loop for the preheader |
| 245 | // edge. |
| 246 | PN->addIncoming(InVal, NewBB); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 247 | |
| 248 | // Can we eliminate this phi node now? |
| 249 | if (Value *V = hasConstantValue(PN)) { |
| 250 | PN->replaceAllUsesWith(V); |
| 251 | BB->getInstList().erase(PN); |
| 252 | } |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // Now that the PHI nodes are updated, actually move the edges from |
| 256 | // Preds to point to NewBB instead of BB. |
| 257 | // |
| 258 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) { |
| 259 | TerminatorInst *TI = Preds[i]->getTerminator(); |
| 260 | for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) |
| 261 | if (TI->getSuccessor(s) == BB) |
| 262 | TI->setSuccessor(s, NewBB); |
| 263 | } |
| 264 | |
| 265 | } else { // Otherwise the loop is dead... |
| 266 | for (BasicBlock::iterator I = BB->begin(); |
Chris Lattner | e408e25 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 267 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 268 | // Insert dummy values as the incoming value... |
| 269 | PN->addIncoming(Constant::getNullValue(PN->getType()), NewBB); |
| 270 | } |
| 271 | return NewBB; |
| 272 | } |
| 273 | |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 274 | /// InsertPreheaderForLoop - Once we discover that a loop doesn't have a |
| 275 | /// preheader, this method is called to insert one. This method has two phases: |
| 276 | /// preheader insertion and analysis updating. |
| 277 | /// |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 278 | void LoopSimplify::InsertPreheaderForLoop(Loop *L) { |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 279 | BasicBlock *Header = L->getHeader(); |
| 280 | |
| 281 | // Compute the set of predecessors of the loop that are not in the loop. |
| 282 | std::vector<BasicBlock*> OutsideBlocks; |
| 283 | for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header); |
| 284 | PI != PE; ++PI) |
| 285 | if (!L->contains(*PI)) // Coming in from outside the loop? |
| 286 | OutsideBlocks.push_back(*PI); // Keep track of it... |
| 287 | |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 288 | // Split out the loop pre-header |
| 289 | BasicBlock *NewBB = |
| 290 | SplitBlockPredecessors(Header, ".preheader", OutsideBlocks); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 291 | |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 292 | //===--------------------------------------------------------------------===// |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 293 | // Update analysis results now that we have performed the transformation |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 294 | // |
| 295 | |
| 296 | // We know that we have loop information to update... update it now. |
| 297 | if (Loop *Parent = L->getParentLoop()) |
| 298 | Parent->addBasicBlockToLoop(NewBB, getAnalysis<LoopInfo>()); |
Chris Lattner | 9f879cf | 2003-02-27 22:48:57 +0000 | [diff] [blame] | 299 | |
| 300 | // If the header for the loop used to be an exit node for another loop, then |
| 301 | // we need to update this to know that the loop-preheader is now the exit |
| 302 | // node. Note that the only loop that could have our header as an exit node |
Chris Lattner | 8f6396e | 2003-05-12 22:04:34 +0000 | [diff] [blame] | 303 | // is a sibling loop, ie, one with the same parent loop, or one if it's |
| 304 | // children. |
| 305 | // |
Chris Lattner | 329c1c6 | 2004-01-08 00:09:44 +0000 | [diff] [blame] | 306 | LoopInfo::iterator ParentLoops, ParentLoopsE; |
| 307 | if (Loop *Parent = L->getParentLoop()) { |
| 308 | ParentLoops = Parent->begin(); |
| 309 | ParentLoopsE = Parent->end(); |
| 310 | } else { // Must check top-level loops... |
| 311 | ParentLoops = getAnalysis<LoopInfo>().begin(); |
| 312 | ParentLoopsE = getAnalysis<LoopInfo>().end(); |
| 313 | } |
Chris Lattner | 9f879cf | 2003-02-27 22:48:57 +0000 | [diff] [blame] | 314 | |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 315 | DominatorSet &DS = getAnalysis<DominatorSet>(); // Update dominator info |
Chris Lattner | 786c564 | 2004-03-13 22:01:26 +0000 | [diff] [blame] | 316 | DominatorTree &DT = getAnalysis<DominatorTree>(); |
Chris Lattner | 85ebd54 | 2004-03-16 06:00:15 +0000 | [diff] [blame] | 317 | |
| 318 | |
| 319 | // Update the dominator tree information. |
| 320 | // The immediate dominator of the preheader is the immediate dominator of |
| 321 | // the old header. |
| 322 | DominatorTree::Node *PHDomTreeNode = |
| 323 | DT.createNewNode(NewBB, DT.getNode(Header)->getIDom()); |
| 324 | |
| 325 | // Change the header node so that PNHode is the new immediate dominator |
| 326 | DT.changeImmediateDominator(DT.getNode(Header), PHDomTreeNode); |
Chris Lattner | 786c564 | 2004-03-13 22:01:26 +0000 | [diff] [blame] | 327 | |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 328 | { |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 329 | // The blocks that dominate NewBB are the blocks that dominate Header, |
| 330 | // minus Header, plus NewBB. |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 331 | DominatorSet::DomSetType DomSet = DS.getDominators(Header); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 332 | DomSet.erase(Header); // Header does not dominate us... |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 333 | DS.addBasicBlock(NewBB, DomSet); |
Chris Lattner | 4d01892 | 2002-09-29 21:41:38 +0000 | [diff] [blame] | 334 | |
| 335 | // The newly created basic block dominates all nodes dominated by Header. |
Chris Lattner | 85ebd54 | 2004-03-16 06:00:15 +0000 | [diff] [blame] | 336 | for (df_iterator<DominatorTree::Node*> DFI = df_begin(PHDomTreeNode), |
| 337 | E = df_end(PHDomTreeNode); DFI != E; ++DFI) |
| 338 | DS.addDominator((*DFI)->getBlock(), NewBB); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // Update immediate dominator information if we have it... |
| 342 | if (ImmediateDominators *ID = getAnalysisToUpdate<ImmediateDominators>()) { |
| 343 | // Whatever i-dominated the header node now immediately dominates NewBB |
| 344 | ID->addNewBlock(NewBB, ID->get(Header)); |
| 345 | |
| 346 | // The preheader now is the immediate dominator for the header node... |
| 347 | ID->setImmediateDominator(Header, NewBB); |
| 348 | } |
| 349 | |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 350 | // Update dominance frontier information... |
| 351 | if (DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>()) { |
| 352 | // The DF(NewBB) is just (DF(Header)-Header), because NewBB dominates |
| 353 | // everything that Header does, and it strictly dominates Header in |
| 354 | // addition. |
| 355 | assert(DF->find(Header) != DF->end() && "Header node doesn't have DF set?"); |
| 356 | DominanceFrontier::DomSetType NewDFSet = DF->find(Header)->second; |
| 357 | NewDFSet.erase(Header); |
| 358 | DF->addBasicBlock(NewBB, NewDFSet); |
| 359 | |
| 360 | // Now we must loop over all of the dominance frontiers in the function, |
Misha Brukman | dfa5f83 | 2003-09-09 21:54:45 +0000 | [diff] [blame] | 361 | // replacing occurrences of Header with NewBB in some cases. If a block |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 362 | // dominates a (now) predecessor of NewBB, but did not strictly dominate |
| 363 | // Header, it will have Header in it's DF set, but should now have NewBB in |
| 364 | // its set. |
| 365 | for (unsigned i = 0, e = OutsideBlocks.size(); i != e; ++i) { |
| 366 | // Get all of the dominators of the predecessor... |
| 367 | const DominatorSet::DomSetType &PredDoms = |
| 368 | DS.getDominators(OutsideBlocks[i]); |
| 369 | for (DominatorSet::DomSetType::const_iterator PDI = PredDoms.begin(), |
| 370 | PDE = PredDoms.end(); PDI != PDE; ++PDI) { |
| 371 | BasicBlock *PredDom = *PDI; |
| 372 | // If the loop header is in DF(PredDom), then PredDom didn't dominate |
| 373 | // the header but did dominate a predecessor outside of the loop. Now |
| 374 | // we change this entry to include the preheader in the DF instead of |
| 375 | // the header. |
| 376 | DominanceFrontier::iterator DFI = DF->find(PredDom); |
| 377 | assert(DFI != DF->end() && "No dominance frontier for node?"); |
| 378 | if (DFI->second.count(Header)) { |
| 379 | DF->removeFromFrontier(DFI, Header); |
| 380 | DF->addToFrontier(DFI, NewBB); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 387 | /// RewriteLoopExitBlock - Ensure that the loop preheader dominates all exit |
| 388 | /// blocks. This method is used to split exit blocks that have predecessors |
| 389 | /// outside of the loop. |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 390 | void LoopSimplify::RewriteLoopExitBlock(Loop *L, BasicBlock *Exit) { |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 391 | DominatorSet &DS = getAnalysis<DominatorSet>(); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 392 | |
| 393 | std::vector<BasicBlock*> LoopBlocks; |
| 394 | for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit); I != E; ++I) |
| 395 | if (L->contains(*I)) |
| 396 | LoopBlocks.push_back(*I); |
| 397 | |
Chris Lattner | 7e7ad49 | 2003-02-27 22:31:07 +0000 | [diff] [blame] | 398 | assert(!LoopBlocks.empty() && "No edges coming in from outside the loop?"); |
| 399 | BasicBlock *NewBB = SplitBlockPredecessors(Exit, ".loopexit", LoopBlocks); |
| 400 | |
Chris Lattner | 69269ac | 2003-02-27 21:50:19 +0000 | [diff] [blame] | 401 | // Update Loop Information - we know that the new block will be in the parent |
| 402 | // loop of L. |
| 403 | if (Loop *Parent = L->getParentLoop()) |
| 404 | Parent->addBasicBlockToLoop(NewBB, getAnalysis<LoopInfo>()); |
Chris Lattner | 74cd04e | 2003-02-28 03:07:54 +0000 | [diff] [blame] | 405 | |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 406 | // Update dominator information (set, immdom, domtree, and domfrontier) |
| 407 | UpdateDomInfoForRevectoredPreds(NewBB, LoopBlocks); |
| 408 | } |
| 409 | |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 410 | /// AddBlockAndPredsToSet - Add the specified block, and all of its |
| 411 | /// predecessors, to the specified set, if it's not already in there. Stop |
| 412 | /// predecessor traversal when we reach StopBlock. |
| 413 | static void AddBlockAndPredsToSet(BasicBlock *BB, BasicBlock *StopBlock, |
| 414 | std::set<BasicBlock*> &Blocks) { |
| 415 | if (!Blocks.insert(BB).second) return; // already processed. |
| 416 | if (BB == StopBlock) return; // Stop here! |
| 417 | |
| 418 | for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) |
| 419 | AddBlockAndPredsToSet(*I, StopBlock, Blocks); |
| 420 | } |
| 421 | |
Chris Lattner | 1f62f82 | 2004-04-13 15:21:18 +0000 | [diff] [blame] | 422 | /// FindPHIToPartitionLoops - The first part of loop-nestification is to find a |
| 423 | /// PHI node that tells us how to partition the loops. |
| 424 | static PHINode *FindPHIToPartitionLoops(Loop *L) { |
| 425 | for (BasicBlock::iterator I = L->getHeader()->begin(); |
| 426 | PHINode *PN = dyn_cast<PHINode>(I); ) { |
| 427 | ++I; |
| 428 | if (Value *V = hasConstantValue(PN)) { |
| 429 | // This is a degenerate PHI already, don't modify it! |
| 430 | PN->replaceAllUsesWith(V); |
| 431 | PN->getParent()->getInstList().erase(PN); |
| 432 | } else { |
| 433 | // Scan this PHI node looking for a use of the PHI node by itself. |
| 434 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 435 | if (PN->getIncomingValue(i) == PN && |
| 436 | L->contains(PN->getIncomingBlock(i))) |
| 437 | // We found something tasty to remove. |
| 438 | return PN; |
| 439 | } |
| 440 | } |
| 441 | return 0; |
| 442 | } |
| 443 | |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 444 | /// SeparateNestedLoop - If this loop has multiple backedges, try to pull one of |
| 445 | /// them out into a nested loop. This is important for code that looks like |
| 446 | /// this: |
| 447 | /// |
| 448 | /// Loop: |
| 449 | /// ... |
| 450 | /// br cond, Loop, Next |
| 451 | /// ... |
| 452 | /// br cond2, Loop, Out |
| 453 | /// |
| 454 | /// To identify this common case, we look at the PHI nodes in the header of the |
| 455 | /// loop. PHI nodes with unchanging values on one backedge correspond to values |
| 456 | /// that change in the "outer" loop, but not in the "inner" loop. |
| 457 | /// |
| 458 | /// If we are able to separate out a loop, return the new outer loop that was |
| 459 | /// created. |
| 460 | /// |
| 461 | Loop *LoopSimplify::SeparateNestedLoop(Loop *L) { |
Chris Lattner | 1f62f82 | 2004-04-13 15:21:18 +0000 | [diff] [blame] | 462 | PHINode *PN = FindPHIToPartitionLoops(L); |
| 463 | if (PN == 0) return 0; // No known way to partition. |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 464 | |
Chris Lattner | 1f62f82 | 2004-04-13 15:21:18 +0000 | [diff] [blame] | 465 | // Pull out all predecessors that have varying values in the loop. This |
| 466 | // handles the case when a PHI node has multiple instances of itself as |
| 467 | // arguments. |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 468 | std::vector<BasicBlock*> OuterLoopPreds; |
Chris Lattner | 1f62f82 | 2004-04-13 15:21:18 +0000 | [diff] [blame] | 469 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 470 | if (PN->getIncomingValue(i) != PN || |
| 471 | !L->contains(PN->getIncomingBlock(i))) |
| 472 | OuterLoopPreds.push_back(PN->getIncomingBlock(i)); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 473 | |
Chris Lattner | 4b66242 | 2004-04-13 16:23:25 +0000 | [diff] [blame] | 474 | BasicBlock *Header = L->getHeader(); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 475 | BasicBlock *NewBB = SplitBlockPredecessors(Header, ".outer", OuterLoopPreds); |
| 476 | |
| 477 | // Update dominator information (set, immdom, domtree, and domfrontier) |
| 478 | UpdateDomInfoForRevectoredPreds(NewBB, OuterLoopPreds); |
| 479 | |
| 480 | // Create the new outer loop. |
| 481 | Loop *NewOuter = new Loop(); |
| 482 | |
| 483 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 484 | |
| 485 | // Change the parent loop to use the outer loop as its child now. |
| 486 | if (Loop *Parent = L->getParentLoop()) |
| 487 | Parent->replaceChildLoopWith(L, NewOuter); |
| 488 | else |
| 489 | LI.changeTopLevelLoop(L, NewOuter); |
| 490 | |
| 491 | // This block is going to be our new header block: add it to this loop and all |
| 492 | // parent loops. |
| 493 | NewOuter->addBasicBlockToLoop(NewBB, getAnalysis<LoopInfo>()); |
| 494 | |
| 495 | // L is now a subloop of our outer loop. |
| 496 | NewOuter->addChildLoop(L); |
| 497 | |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 498 | for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) |
| 499 | NewOuter->addBlockEntry(L->getBlocks()[i]); |
| 500 | |
| 501 | // Determine which blocks should stay in L and which should be moved out to |
| 502 | // the Outer loop now. |
| 503 | DominatorSet &DS = getAnalysis<DominatorSet>(); |
| 504 | std::set<BasicBlock*> BlocksInL; |
| 505 | for (pred_iterator PI = pred_begin(Header), E = pred_end(Header); PI!=E; ++PI) |
| 506 | if (DS.dominates(Header, *PI)) |
| 507 | AddBlockAndPredsToSet(*PI, Header, BlocksInL); |
| 508 | |
| 509 | |
| 510 | // Scan all of the loop children of L, moving them to OuterLoop if they are |
| 511 | // not part of the inner loop. |
| 512 | for (Loop::iterator I = L->begin(); I != L->end(); ) |
| 513 | if (BlocksInL.count((*I)->getHeader())) |
| 514 | ++I; // Loop remains in L |
| 515 | else |
| 516 | NewOuter->addChildLoop(L->removeChildLoop(I)); |
| 517 | |
| 518 | // Now that we know which blocks are in L and which need to be moved to |
| 519 | // OuterLoop, move any blocks that need it. |
| 520 | for (unsigned i = 0; i != L->getBlocks().size(); ++i) { |
| 521 | BasicBlock *BB = L->getBlocks()[i]; |
| 522 | if (!BlocksInL.count(BB)) { |
| 523 | // Move this block to the parent, updating the exit blocks sets |
| 524 | L->removeBlockFromLoop(BB); |
| 525 | if (LI[BB] == L) |
| 526 | LI.changeLoopFor(BB, NewOuter); |
| 527 | --i; |
| 528 | } |
| 529 | } |
| 530 | |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 531 | return NewOuter; |
| 532 | } |
| 533 | |
| 534 | |
| 535 | |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 536 | /// InsertUniqueBackedgeBlock - This method is called when the specified loop |
| 537 | /// has more than one backedge in it. If this occurs, revector all of these |
| 538 | /// backedges to target a new basic block and have that block branch to the loop |
| 539 | /// header. This ensures that loops have exactly one backedge. |
| 540 | /// |
| 541 | void LoopSimplify::InsertUniqueBackedgeBlock(Loop *L) { |
| 542 | assert(L->getNumBackEdges() > 1 && "Must have > 1 backedge!"); |
| 543 | |
| 544 | // Get information about the loop |
| 545 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 546 | BasicBlock *Header = L->getHeader(); |
| 547 | Function *F = Header->getParent(); |
| 548 | |
| 549 | // Figure out which basic blocks contain back-edges to the loop header. |
| 550 | std::vector<BasicBlock*> BackedgeBlocks; |
| 551 | for (pred_iterator I = pred_begin(Header), E = pred_end(Header); I != E; ++I) |
| 552 | if (*I != Preheader) BackedgeBlocks.push_back(*I); |
| 553 | |
| 554 | // Create and insert the new backedge block... |
| 555 | BasicBlock *BEBlock = new BasicBlock(Header->getName()+".backedge", F); |
Chris Lattner | 108e4ab | 2003-11-21 16:52:05 +0000 | [diff] [blame] | 556 | BranchInst *BETerminator = new BranchInst(Header, BEBlock); |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 557 | |
| 558 | // Move the new backedge block to right after the last backedge block. |
| 559 | Function::iterator InsertPos = BackedgeBlocks.back(); ++InsertPos; |
| 560 | F->getBasicBlockList().splice(InsertPos, F->getBasicBlockList(), BEBlock); |
| 561 | |
| 562 | // Now that the block has been inserted into the function, create PHI nodes in |
| 563 | // the backedge block which correspond to any PHI nodes in the header block. |
| 564 | for (BasicBlock::iterator I = Header->begin(); |
| 565 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 566 | PHINode *NewPN = new PHINode(PN->getType(), PN->getName()+".be", |
| 567 | BETerminator); |
| 568 | NewPN->op_reserve(2*BackedgeBlocks.size()); |
| 569 | |
| 570 | // Loop over the PHI node, moving all entries except the one for the |
| 571 | // preheader over to the new PHI node. |
| 572 | unsigned PreheaderIdx = ~0U; |
| 573 | bool HasUniqueIncomingValue = true; |
| 574 | Value *UniqueValue = 0; |
| 575 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 576 | BasicBlock *IBB = PN->getIncomingBlock(i); |
| 577 | Value *IV = PN->getIncomingValue(i); |
| 578 | if (IBB == Preheader) { |
| 579 | PreheaderIdx = i; |
| 580 | } else { |
| 581 | NewPN->addIncoming(IV, IBB); |
| 582 | if (HasUniqueIncomingValue) { |
| 583 | if (UniqueValue == 0) |
| 584 | UniqueValue = IV; |
| 585 | else if (UniqueValue != IV) |
| 586 | HasUniqueIncomingValue = false; |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | // Delete all of the incoming values from the old PN except the preheader's |
| 592 | assert(PreheaderIdx != ~0U && "PHI has no preheader entry??"); |
| 593 | if (PreheaderIdx != 0) { |
| 594 | PN->setIncomingValue(0, PN->getIncomingValue(PreheaderIdx)); |
| 595 | PN->setIncomingBlock(0, PN->getIncomingBlock(PreheaderIdx)); |
| 596 | } |
| 597 | PN->op_erase(PN->op_begin()+2, PN->op_end()); |
| 598 | |
| 599 | // Finally, add the newly constructed PHI node as the entry for the BEBlock. |
| 600 | PN->addIncoming(NewPN, BEBlock); |
| 601 | |
| 602 | // As an optimization, if all incoming values in the new PhiNode (which is a |
| 603 | // subset of the incoming values of the old PHI node) have the same value, |
| 604 | // eliminate the PHI Node. |
| 605 | if (HasUniqueIncomingValue) { |
| 606 | NewPN->replaceAllUsesWith(UniqueValue); |
| 607 | BEBlock->getInstList().erase(NewPN); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | // Now that all of the PHI nodes have been inserted and adjusted, modify the |
| 612 | // backedge blocks to just to the BEBlock instead of the header. |
| 613 | for (unsigned i = 0, e = BackedgeBlocks.size(); i != e; ++i) { |
| 614 | TerminatorInst *TI = BackedgeBlocks[i]->getTerminator(); |
| 615 | for (unsigned Op = 0, e = TI->getNumSuccessors(); Op != e; ++Op) |
| 616 | if (TI->getSuccessor(Op) == Header) |
| 617 | TI->setSuccessor(Op, BEBlock); |
| 618 | } |
| 619 | |
| 620 | //===--- Update all analyses which we must preserve now -----------------===// |
| 621 | |
| 622 | // Update Loop Information - we know that this block is now in the current |
| 623 | // loop and all parent loops. |
| 624 | L->addBasicBlockToLoop(BEBlock, getAnalysis<LoopInfo>()); |
| 625 | |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 626 | // Update dominator information (set, immdom, domtree, and domfrontier) |
| 627 | UpdateDomInfoForRevectoredPreds(BEBlock, BackedgeBlocks); |
| 628 | } |
| 629 | |
| 630 | /// UpdateDomInfoForRevectoredPreds - This method is used to update the four |
| 631 | /// different kinds of dominator information (dominator sets, immediate |
| 632 | /// dominators, dominator trees, and dominance frontiers) after a new block has |
| 633 | /// been added to the CFG. |
| 634 | /// |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 635 | /// This only supports the case when an existing block (known as "NewBBSucc"), |
| 636 | /// had some of its predecessors factored into a new basic block. This |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 637 | /// transformation inserts a new basic block ("NewBB"), with a single |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 638 | /// unconditional branch to NewBBSucc, and moves some predecessors of |
| 639 | /// "NewBBSucc" to now branch to NewBB. These predecessors are listed in |
| 640 | /// PredBlocks, even though they are the same as |
| 641 | /// pred_begin(NewBB)/pred_end(NewBB). |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 642 | /// |
| 643 | void LoopSimplify::UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB, |
| 644 | std::vector<BasicBlock*> &PredBlocks) { |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 645 | assert(!PredBlocks.empty() && "No predblocks??"); |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 646 | assert(succ_begin(NewBB) != succ_end(NewBB) && |
| 647 | ++succ_begin(NewBB) == succ_end(NewBB) && |
| 648 | "NewBB should have a single successor!"); |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 649 | BasicBlock *NewBBSucc = *succ_begin(NewBB); |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 650 | DominatorSet &DS = getAnalysis<DominatorSet>(); |
| 651 | |
Chris Lattner | 4f303bd | 2004-04-01 19:06:07 +0000 | [diff] [blame] | 652 | // Update dominator information... The blocks that dominate NewBB are the |
| 653 | // intersection of the dominators of predecessors, plus the block itself. |
| 654 | // |
| 655 | DominatorSet::DomSetType NewBBDomSet = DS.getDominators(PredBlocks[0]); |
| 656 | for (unsigned i = 1, e = PredBlocks.size(); i != e; ++i) |
| 657 | set_intersect(NewBBDomSet, DS.getDominators(PredBlocks[i])); |
| 658 | NewBBDomSet.insert(NewBB); // All blocks dominate themselves... |
| 659 | DS.addBasicBlock(NewBB, NewBBDomSet); |
| 660 | |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 661 | // The newly inserted basic block will dominate existing basic blocks iff the |
| 662 | // PredBlocks dominate all of the non-pred blocks. If all predblocks dominate |
| 663 | // the non-pred blocks, then they all must be the same block! |
Chris Lattner | 4f303bd | 2004-04-01 19:06:07 +0000 | [diff] [blame] | 664 | // |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 665 | bool NewBBDominatesNewBBSucc = true; |
| 666 | { |
| 667 | BasicBlock *OnePred = PredBlocks[0]; |
| 668 | for (unsigned i = 1, e = PredBlocks.size(); i != e; ++i) |
| 669 | if (PredBlocks[i] != OnePred) { |
| 670 | NewBBDominatesNewBBSucc = false; |
| 671 | break; |
| 672 | } |
| 673 | |
| 674 | if (NewBBDominatesNewBBSucc) |
| 675 | for (pred_iterator PI = pred_begin(NewBBSucc), E = pred_end(NewBBSucc); |
| 676 | PI != E; ++PI) |
Chris Lattner | 99dcc1d | 2004-02-05 23:20:59 +0000 | [diff] [blame] | 677 | if (*PI != NewBB && !DS.dominates(NewBBSucc, *PI)) { |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 678 | NewBBDominatesNewBBSucc = false; |
| 679 | break; |
| 680 | } |
| 681 | } |
| 682 | |
Chris Lattner | 4f303bd | 2004-04-01 19:06:07 +0000 | [diff] [blame] | 683 | // The other scenario where the new block can dominate its successors are when |
| 684 | // all predecessors of NewBBSucc that are not NewBB are dominated by NewBBSucc |
| 685 | // already. |
| 686 | if (!NewBBDominatesNewBBSucc) { |
| 687 | NewBBDominatesNewBBSucc = true; |
| 688 | for (pred_iterator PI = pred_begin(NewBBSucc), E = pred_end(NewBBSucc); |
| 689 | PI != E; ++PI) |
| 690 | if (*PI != NewBB && !DS.dominates(NewBBSucc, *PI)) { |
| 691 | NewBBDominatesNewBBSucc = false; |
| 692 | break; |
| 693 | } |
| 694 | } |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 695 | |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 696 | // If NewBB dominates some blocks, then it will dominate all blocks that |
Chris Lattner | 3e0b870 | 2004-02-05 22:33:26 +0000 | [diff] [blame] | 697 | // NewBBSucc does. |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 698 | if (NewBBDominatesNewBBSucc) { |
| 699 | BasicBlock *PredBlock = PredBlocks[0]; |
| 700 | Function *F = NewBB->getParent(); |
| 701 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
Chris Lattner | 3e0b870 | 2004-02-05 22:33:26 +0000 | [diff] [blame] | 702 | if (DS.dominates(NewBBSucc, I)) |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 703 | DS.addDominator(I, NewBB); |
| 704 | } |
| 705 | |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 706 | // Update immediate dominator information if we have it... |
| 707 | BasicBlock *NewBBIDom = 0; |
| 708 | if (ImmediateDominators *ID = getAnalysisToUpdate<ImmediateDominators>()) { |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 709 | // To find the immediate dominator of the new exit node, we trace up the |
| 710 | // immediate dominators of a predecessor until we find a basic block that |
| 711 | // dominates the exit block. |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 712 | // |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 713 | BasicBlock *Dom = PredBlocks[0]; // Some random predecessor... |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 714 | while (!NewBBDomSet.count(Dom)) { // Loop until we find a dominator... |
| 715 | assert(Dom != 0 && "No shared dominator found???"); |
| 716 | Dom = ID->get(Dom); |
| 717 | } |
| 718 | |
| 719 | // Set the immediate dominator now... |
| 720 | ID->addNewBlock(NewBB, Dom); |
| 721 | NewBBIDom = Dom; // Reuse this if calculating DominatorTree info... |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 722 | |
| 723 | // If NewBB strictly dominates other blocks, we need to update their idom's |
| 724 | // now. The only block that need adjustment is the NewBBSucc block, whose |
| 725 | // idom should currently be set to PredBlocks[0]. |
Chris Lattner | 4edf6c0 | 2004-04-01 19:21:46 +0000 | [diff] [blame] | 726 | if (NewBBDominatesNewBBSucc) |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 727 | ID->setImmediateDominator(NewBBSucc, NewBB); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | // Update DominatorTree information if it is active. |
| 731 | if (DominatorTree *DT = getAnalysisToUpdate<DominatorTree>()) { |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 732 | // If we don't have ImmediateDominator info around, calculate the idom as |
| 733 | // above. |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 734 | DominatorTree::Node *NewBBIDomNode; |
| 735 | if (NewBBIDom) { |
| 736 | NewBBIDomNode = DT->getNode(NewBBIDom); |
| 737 | } else { |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 738 | NewBBIDomNode = DT->getNode(PredBlocks[0]); // Random pred |
Chris Lattner | c444a42 | 2003-09-11 16:26:13 +0000 | [diff] [blame] | 739 | while (!NewBBDomSet.count(NewBBIDomNode->getBlock())) { |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 740 | NewBBIDomNode = NewBBIDomNode->getIDom(); |
| 741 | assert(NewBBIDomNode && "No shared dominator found??"); |
| 742 | } |
| 743 | } |
| 744 | |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 745 | // Create the new dominator tree node... and set the idom of NewBB. |
| 746 | DominatorTree::Node *NewBBNode = DT->createNewNode(NewBB, NewBBIDomNode); |
| 747 | |
| 748 | // If NewBB strictly dominates other blocks, then it is now the immediate |
| 749 | // dominator of NewBBSucc. Update the dominator tree as appropriate. |
| 750 | if (NewBBDominatesNewBBSucc) { |
| 751 | DominatorTree::Node *NewBBSuccNode = DT->getNode(NewBBSucc); |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 752 | DT->changeImmediateDominator(NewBBSuccNode, NewBBNode); |
| 753 | } |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | // Update dominance frontier information... |
| 757 | if (DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>()) { |
Chris Lattner | 4b66242 | 2004-04-13 16:23:25 +0000 | [diff] [blame] | 758 | // If NewBB dominates NewBBSucc, then DF(NewBB) is now going to be the |
| 759 | // DF(PredBlocks[0]) without the stuff that the new block does not dominate |
| 760 | // a predecessor of. |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 761 | if (NewBBDominatesNewBBSucc) { |
| 762 | DominanceFrontier::iterator DFI = DF->find(PredBlocks[0]); |
| 763 | if (DFI != DF->end()) { |
| 764 | DominanceFrontier::DomSetType Set = DFI->second; |
| 765 | // Filter out stuff in Set that we do not dominate a predecessor of. |
| 766 | for (DominanceFrontier::DomSetType::iterator SetI = Set.begin(), |
| 767 | E = Set.end(); SetI != E;) { |
| 768 | bool DominatesPred = false; |
| 769 | for (pred_iterator PI = pred_begin(*SetI), E = pred_end(*SetI); |
| 770 | PI != E; ++PI) |
| 771 | if (DS.dominates(NewBB, *PI)) |
| 772 | DominatesPred = true; |
| 773 | if (!DominatesPred) |
| 774 | Set.erase(SetI++); |
| 775 | else |
| 776 | ++SetI; |
| 777 | } |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 778 | |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 779 | DF->addBasicBlock(NewBB, Set); |
| 780 | } |
| 781 | |
| 782 | } else { |
| 783 | // DF(NewBB) is {NewBBSucc} because NewBB does not strictly dominate |
| 784 | // NewBBSucc, but it does dominate itself (and there is an edge (NewBB -> |
| 785 | // NewBBSucc)). NewBBSucc is the single successor of NewBB. |
| 786 | DominanceFrontier::DomSetType NewDFSet; |
| 787 | NewDFSet.insert(NewBBSucc); |
| 788 | DF->addBasicBlock(NewBB, NewDFSet); |
Chris Lattner | 4b66242 | 2004-04-13 16:23:25 +0000 | [diff] [blame] | 789 | } |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 790 | |
Chris Lattner | 4b66242 | 2004-04-13 16:23:25 +0000 | [diff] [blame] | 791 | // Now we must loop over all of the dominance frontiers in the function, |
| 792 | // replacing occurrences of NewBBSucc with NewBB in some cases. All |
| 793 | // blocks that dominate a block in PredBlocks and contained NewBBSucc in |
| 794 | // their dominance frontier must be updated to contain NewBB instead. |
| 795 | // |
| 796 | for (unsigned i = 0, e = PredBlocks.size(); i != e; ++i) { |
| 797 | BasicBlock *Pred = PredBlocks[i]; |
| 798 | // Get all of the dominators of the predecessor... |
| 799 | const DominatorSet::DomSetType &PredDoms = DS.getDominators(Pred); |
| 800 | for (DominatorSet::DomSetType::const_iterator PDI = PredDoms.begin(), |
| 801 | PDE = PredDoms.end(); PDI != PDE; ++PDI) { |
| 802 | BasicBlock *PredDom = *PDI; |
| 803 | |
| 804 | // If the NewBBSucc node is in DF(PredDom), then PredDom didn't |
| 805 | // dominate NewBBSucc but did dominate a predecessor of it. Now we |
| 806 | // change this entry to include NewBB in the DF instead of NewBBSucc. |
| 807 | DominanceFrontier::iterator DFI = DF->find(PredDom); |
| 808 | assert(DFI != DF->end() && "No dominance frontier for node?"); |
| 809 | if (DFI->second.count(NewBBSucc)) { |
| 810 | // If NewBBSucc should not stay in our dominator frontier, remove it. |
| 811 | // We remove it unless there is a predecessor of NewBBSucc that we |
| 812 | // dominate, but we don't strictly dominate NewBBSucc. |
| 813 | bool ShouldRemove = true; |
| 814 | if (PredDom == NewBBSucc || !DS.dominates(PredDom, NewBBSucc)) { |
| 815 | // Okay, we know that PredDom does not strictly dominate NewBBSucc. |
| 816 | // Check to see if it dominates any predecessors of NewBBSucc. |
| 817 | for (pred_iterator PI = pred_begin(NewBBSucc), |
| 818 | E = pred_end(NewBBSucc); PI != E; ++PI) |
| 819 | if (DS.dominates(PredDom, *PI)) { |
| 820 | ShouldRemove = false; |
| 821 | break; |
| 822 | } |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 823 | } |
Chris Lattner | 4b66242 | 2004-04-13 16:23:25 +0000 | [diff] [blame] | 824 | |
| 825 | if (ShouldRemove) |
| 826 | DF->removeFromFrontier(DFI, NewBBSucc); |
| 827 | DF->addToFrontier(DFI, NewBB); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | } |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 831 | } |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 832 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 833 | |