Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 1 | //===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements LoopPass and LPPassManager. All loop optimization |
| 11 | // and transformation passes are derived from LoopPass. LPPassManager is |
| 12 | // responsible for managing LoopPasses. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/Analysis/LoopPass.h" |
| 17 | using namespace llvm; |
| 18 | |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | // LPPassManager |
| 21 | // |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 22 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 23 | char LPPassManager::ID = 0; |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 24 | /// LPPassManager manages FPPassManagers and CalLGraphSCCPasses. |
| 25 | |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 26 | LPPassManager::LPPassManager(int Depth) |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 27 | : FunctionPass(&ID), PMDataManager(Depth) { |
Devang Patel | 8ded585 | 2007-02-23 00:16:44 +0000 | [diff] [blame] | 28 | skipThisLoop = false; |
| 29 | redoThisLoop = false; |
Devang Patel | 7a9a069 | 2007-03-06 18:38:33 +0000 | [diff] [blame] | 30 | LI = NULL; |
| 31 | CurrentLoop = NULL; |
Devang Patel | d0e6e33 | 2007-02-22 23:30:07 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Dan Gohman | e6acf36 | 2008-07-11 22:51:32 +0000 | [diff] [blame] | 34 | /// Delete loop from the loop queue and loop hierarchy (LoopInfo). |
Devang Patel | 5afdc7d | 2007-02-23 00:10:16 +0000 | [diff] [blame] | 35 | void LPPassManager::deleteLoopFromQueue(Loop *L) { |
Devang Patel | 7a9a069 | 2007-03-06 18:38:33 +0000 | [diff] [blame] | 36 | |
| 37 | if (Loop *ParentLoop = L->getParentLoop()) { // Not a top-level loop. |
| 38 | // Reparent all of the blocks in this loop. Since BBLoop had a parent, |
| 39 | // they are now all in it. |
| 40 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 41 | I != E; ++I) |
| 42 | if (LI->getLoopFor(*I) == L) // Don't change blocks in subloops. |
| 43 | LI->changeLoopFor(*I, ParentLoop); |
| 44 | |
| 45 | // Remove the loop from its parent loop. |
| 46 | for (Loop::iterator I = ParentLoop->begin(), E = ParentLoop->end();; |
| 47 | ++I) { |
| 48 | assert(I != E && "Couldn't find loop"); |
| 49 | if (*I == L) { |
| 50 | ParentLoop->removeChildLoop(I); |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Move all subloops into the parent loop. |
Dan Gohman | cb406c2 | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 56 | while (!L->empty()) |
Devang Patel | 7a9a069 | 2007-03-06 18:38:33 +0000 | [diff] [blame] | 57 | ParentLoop->addChildLoop(L->removeChildLoop(L->end()-1)); |
| 58 | } else { |
| 59 | // Reparent all of the blocks in this loop. Since BBLoop had no parent, |
| 60 | // they no longer in a loop at all. |
| 61 | |
| 62 | for (unsigned i = 0; i != L->getBlocks().size(); ++i) { |
| 63 | // Don't change blocks in subloops. |
| 64 | if (LI->getLoopFor(L->getBlocks()[i]) == L) { |
| 65 | LI->removeBlock(L->getBlocks()[i]); |
| 66 | --i; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Remove the loop from the top-level LoopInfo object. |
| 71 | for (LoopInfo::iterator I = LI->begin(), E = LI->end();; ++I) { |
| 72 | assert(I != E && "Couldn't find loop"); |
| 73 | if (*I == L) { |
| 74 | LI->removeLoop(I); |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Move all of the subloops to the top-level. |
Dan Gohman | cb406c2 | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 80 | while (!L->empty()) |
Devang Patel | 7a9a069 | 2007-03-06 18:38:33 +0000 | [diff] [blame] | 81 | LI->addTopLevelLoop(L->removeChildLoop(L->end()-1)); |
| 82 | } |
| 83 | |
| 84 | delete L; |
| 85 | |
| 86 | // If L is current loop then skip rest of the passes and let |
| 87 | // runOnFunction remove L from LQ. Otherwise, remove L from LQ now |
| 88 | // and continue applying other passes on CurrentLoop. |
| 89 | if (CurrentLoop == L) { |
| 90 | skipThisLoop = true; |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | for (std::deque<Loop *>::iterator I = LQ.begin(), |
| 95 | E = LQ.end(); I != E; ++I) { |
| 96 | if (*I == L) { |
| 97 | LQ.erase(I); |
| 98 | break; |
| 99 | } |
| 100 | } |
Devang Patel | 5afdc7d | 2007-02-23 00:10:16 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Devang Patel | a885c06 | 2007-03-06 19:00:02 +0000 | [diff] [blame] | 103 | // Inset loop into loop nest (LoopInfo) and loop queue (LQ). |
| 104 | void LPPassManager::insertLoop(Loop *L, Loop *ParentLoop) { |
| 105 | |
| 106 | assert (CurrentLoop != L && "Cannot insert CurrentLoop"); |
| 107 | |
| 108 | // Insert into loop nest |
| 109 | if (ParentLoop) |
| 110 | ParentLoop->addChildLoop(L); |
| 111 | else |
| 112 | LI->addTopLevelLoop(L); |
| 113 | |
| 114 | // Insert L into loop queue |
| 115 | if (L == CurrentLoop) |
| 116 | redoLoop(L); |
| 117 | else if (!ParentLoop) |
| 118 | // This is top level loop. |
| 119 | LQ.push_front(L); |
| 120 | else { |
| 121 | // Insert L after ParentLoop |
| 122 | for (std::deque<Loop *>::iterator I = LQ.begin(), |
| 123 | E = LQ.end(); I != E; ++I) { |
| 124 | if (*I == ParentLoop) { |
| 125 | // deque does not support insert after. |
| 126 | ++I; |
| 127 | LQ.insert(I, 1, L); |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
Devang Patel | 8ded585 | 2007-02-23 00:16:44 +0000 | [diff] [blame] | 134 | // Reoptimize this loop. LPPassManager will re-insert this loop into the |
| 135 | // queue. This allows LoopPass to change loop nest for the loop. This |
| 136 | // utility may send LPPassManager into infinite loops so use caution. |
| 137 | void LPPassManager::redoLoop(Loop *L) { |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 138 | assert (CurrentLoop == L && "Can redo only CurrentLoop"); |
Devang Patel | 8ded585 | 2007-02-23 00:16:44 +0000 | [diff] [blame] | 139 | redoThisLoop = true; |
| 140 | } |
| 141 | |
Devang Patel | c7e49c0 | 2007-07-31 08:00:57 +0000 | [diff] [blame] | 142 | /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for |
| 143 | /// all loop passes. |
| 144 | void LPPassManager::cloneBasicBlockSimpleAnalysis(BasicBlock *From, |
| 145 | BasicBlock *To, Loop *L) { |
| 146 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 147 | Pass *P = getContainedPass(Index); |
| 148 | LoopPass *LP = dynamic_cast<LoopPass *>(P); |
| 149 | LP->cloneBasicBlockAnalysis(From, To, L); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes. |
| 154 | void LPPassManager::deleteSimpleAnalysisValue(Value *V, Loop *L) { |
Devang Patel | 575ec80 | 2009-03-25 23:57:48 +0000 | [diff] [blame^] | 155 | if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
| 156 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; |
| 157 | ++BI) { |
| 158 | Instruction &I = *BI; |
| 159 | deleteSimpleAnalysisValue(&I, L); |
| 160 | } |
| 161 | } |
Devang Patel | c7e49c0 | 2007-07-31 08:00:57 +0000 | [diff] [blame] | 162 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 163 | Pass *P = getContainedPass(Index); |
| 164 | LoopPass *LP = dynamic_cast<LoopPass *>(P); |
| 165 | LP->deleteAnalysisValue(V, L); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 170 | // Recurse through all subloops and all loops into LQ. |
Devang Patel | 3015972 | 2007-03-06 02:30:46 +0000 | [diff] [blame] | 171 | static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) { |
Devang Patel | 622adea | 2007-03-06 19:50:49 +0000 | [diff] [blame] | 172 | LQ.push_back(L); |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 173 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 174 | addLoopIntoQueue(*I, LQ); |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Devang Patel | c37177e | 2007-03-06 19:11:25 +0000 | [diff] [blame] | 177 | /// Pass Manager itself does not invalidate any analysis info. |
| 178 | void LPPassManager::getAnalysisUsage(AnalysisUsage &Info) const { |
| 179 | // LPPassManager needs LoopInfo. In the long term LoopInfo class will |
| 180 | // become part of LPPassManager. |
| 181 | Info.addRequired<LoopInfo>(); |
| 182 | Info.setPreservesAll(); |
| 183 | } |
| 184 | |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 185 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 186 | /// whether any of the passes modifies the function, and if so, return true. |
| 187 | bool LPPassManager::runOnFunction(Function &F) { |
Devang Patel | c37177e | 2007-03-06 19:11:25 +0000 | [diff] [blame] | 188 | LI = &getAnalysis<LoopInfo>(); |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 189 | bool Changed = false; |
| 190 | |
Devang Patel | 70c09c5 | 2008-07-03 07:02:30 +0000 | [diff] [blame] | 191 | // Collect inherited analysis from Module level pass manager. |
| 192 | populateInheritedAnalysis(TPM->activeStack); |
| 193 | |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 194 | // Populate Loop Queue |
Devang Patel | c37177e | 2007-03-06 19:11:25 +0000 | [diff] [blame] | 195 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 196 | addLoopIntoQueue(*I, LQ); |
| 197 | |
Devang Patel | a5057d0 | 2007-03-06 16:59:03 +0000 | [diff] [blame] | 198 | // Initialization |
| 199 | for (std::deque<Loop *>::const_iterator I = LQ.begin(), E = LQ.end(); |
| 200 | I != E; ++I) { |
| 201 | Loop *L = *I; |
| 202 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 203 | Pass *P = getContainedPass(Index); |
| 204 | LoopPass *LP = dynamic_cast<LoopPass *>(P); |
| 205 | if (LP) |
| 206 | Changed |= LP->doInitialization(L, *this); |
| 207 | } |
| 208 | } |
| 209 | |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 210 | // Walk Loops |
Devang Patel | 3015972 | 2007-03-06 02:30:46 +0000 | [diff] [blame] | 211 | while (!LQ.empty()) { |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 212 | |
Devang Patel | 7a9a069 | 2007-03-06 18:38:33 +0000 | [diff] [blame] | 213 | CurrentLoop = LQ.back(); |
Devang Patel | 5afdc7d | 2007-02-23 00:10:16 +0000 | [diff] [blame] | 214 | skipThisLoop = false; |
Devang Patel | 8ded585 | 2007-02-23 00:16:44 +0000 | [diff] [blame] | 215 | redoThisLoop = false; |
Devang Patel | 5afdc7d | 2007-02-23 00:10:16 +0000 | [diff] [blame] | 216 | |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 217 | // Run all passes on current SCC |
| 218 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 219 | Pass *P = getContainedPass(Index); |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 220 | |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 221 | dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG, ""); |
Chris Lattner | 0dabb7e | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 222 | dumpRequiredSet(P); |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 223 | |
| 224 | initializeAnalysisImpl(P); |
| 225 | |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 226 | LoopPass *LP = dynamic_cast<LoopPass *>(P); |
Chris Lattner | d6f1658 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 227 | { |
| 228 | PassManagerPrettyStackEntry X(LP, *CurrentLoop->getHeader()); |
| 229 | StartPassTimer(P); |
| 230 | assert(LP && "Invalid LPPassManager member"); |
| 231 | Changed |= LP->runOnLoop(CurrentLoop, *this); |
| 232 | StopPassTimer(P); |
| 233 | } |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 234 | |
| 235 | if (Changed) |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 236 | dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG, ""); |
Chris Lattner | 0dabb7e | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 237 | dumpPreservedSet(P); |
Devang Patel | 58e0ef1 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 238 | |
| 239 | verifyPreservedAnalysis(LP); |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 240 | removeNotPreservedAnalysis(P); |
| 241 | recordAvailableAnalysis(P); |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 242 | removeDeadPasses(P, "", ON_LOOP_MSG); |
Devang Patel | 5afdc7d | 2007-02-23 00:10:16 +0000 | [diff] [blame] | 243 | |
Devang Patel | 844a3d1 | 2008-07-01 19:50:56 +0000 | [diff] [blame] | 244 | // If dominator information is available then verify the info if requested. |
Devang Patel | 5b57e72 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 245 | verifyDomInfo(*LP, F); |
| 246 | |
Devang Patel | 5afdc7d | 2007-02-23 00:10:16 +0000 | [diff] [blame] | 247 | if (skipThisLoop) |
| 248 | // Do not run other passes on this loop. |
| 249 | break; |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 250 | } |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 251 | |
| 252 | // Pop the loop from queue after running all passes. |
Devang Patel | 3015972 | 2007-03-06 02:30:46 +0000 | [diff] [blame] | 253 | LQ.pop_back(); |
Devang Patel | 8ded585 | 2007-02-23 00:16:44 +0000 | [diff] [blame] | 254 | |
| 255 | if (redoThisLoop) |
Devang Patel | 7a9a069 | 2007-03-06 18:38:33 +0000 | [diff] [blame] | 256 | LQ.push_back(CurrentLoop); |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 257 | } |
Devang Patel | a5057d0 | 2007-03-06 16:59:03 +0000 | [diff] [blame] | 258 | |
| 259 | // Finalization |
| 260 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 261 | Pass *P = getContainedPass(Index); |
| 262 | LoopPass *LP = dynamic_cast <LoopPass *>(P); |
| 263 | if (LP) |
| 264 | Changed |= LP->doFinalization(); |
| 265 | } |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 266 | |
| 267 | return Changed; |
| 268 | } |
| 269 | |
Dan Gohman | 189c635 | 2009-02-17 19:41:26 +0000 | [diff] [blame] | 270 | /// Print passes managed by this manager |
| 271 | void LPPassManager::dumpPassStructure(unsigned Offset) { |
| 272 | llvm::cerr << std::string(Offset*2, ' ') << "Loop Pass Manager\n"; |
| 273 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 274 | Pass *P = getContainedPass(Index); |
| 275 | P->dumpPassStructure(Offset + 1); |
| 276 | dumpLastUses(P, Offset+1); |
| 277 | } |
| 278 | } |
| 279 | |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 280 | |
Devang Patel | bfd5905 | 2007-02-23 00:36:57 +0000 | [diff] [blame] | 281 | //===----------------------------------------------------------------------===// |
| 282 | // LoopPass |
| 283 | |
Devang Patel | 22033be | 2007-03-06 17:59:37 +0000 | [diff] [blame] | 284 | // Check if this pass is suitable for the current LPPassManager, if |
| 285 | // available. This pass P is not suitable for a LPPassManager if P |
| 286 | // is not preserving higher level analysis info used by other |
| 287 | // LPPassManager passes. In such case, pop LPPassManager from the |
| 288 | // stack. This will force assignPassManager() to create new |
| 289 | // LPPassManger as expected. |
| 290 | void LoopPass::preparePassManager(PMStack &PMS) { |
| 291 | |
| 292 | // Find LPPassManager |
Duncan Sands | 20d824b | 2007-07-19 09:42:01 +0000 | [diff] [blame] | 293 | while (!PMS.empty() && |
| 294 | PMS.top()->getPassManagerType() > PMT_LoopPassManager) |
| 295 | PMS.pop(); |
Devang Patel | 22033be | 2007-03-06 17:59:37 +0000 | [diff] [blame] | 296 | |
| 297 | LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top()); |
| 298 | |
| 299 | // If this pass is destroying high level information that is used |
| 300 | // by other passes that are managed by LPM then do not insert |
| 301 | // this pass in current LPM. Use new LPPassManager. |
| 302 | if (LPPM && !LPPM->preserveHigherLevelAnalysis(this)) |
| 303 | PMS.pop(); |
| 304 | } |
| 305 | |
Devang Patel | bfd5905 | 2007-02-23 00:36:57 +0000 | [diff] [blame] | 306 | /// Assign pass manager to manage this pass. |
| 307 | void LoopPass::assignPassManager(PMStack &PMS, |
| 308 | PassManagerType PreferredType) { |
| 309 | // Find LPPassManager |
Duncan Sands | 20d824b | 2007-07-19 09:42:01 +0000 | [diff] [blame] | 310 | while (!PMS.empty() && |
| 311 | PMS.top()->getPassManagerType() > PMT_LoopPassManager) |
| 312 | PMS.pop(); |
Devang Patel | bfd5905 | 2007-02-23 00:36:57 +0000 | [diff] [blame] | 313 | |
| 314 | LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top()); |
| 315 | |
| 316 | // Create new Loop Pass Manager if it does not exist. |
| 317 | if (!LPPM) { |
| 318 | |
| 319 | assert (!PMS.empty() && "Unable to create Loop Pass Manager"); |
| 320 | PMDataManager *PMD = PMS.top(); |
| 321 | |
| 322 | // [1] Create new Call Graph Pass Manager |
| 323 | LPPM = new LPPassManager(PMD->getDepth() + 1); |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 324 | LPPM->populateInheritedAnalysis(PMS); |
Devang Patel | bfd5905 | 2007-02-23 00:36:57 +0000 | [diff] [blame] | 325 | |
| 326 | // [2] Set up new manager's top level manager |
| 327 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 328 | TPM->addIndirectPassManager(LPPM); |
| 329 | |
| 330 | // [3] Assign manager to manage this new manager. This may create |
| 331 | // and push new managers into PMS |
| 332 | Pass *P = dynamic_cast<Pass *>(LPPM); |
Devang Patel | c37177e | 2007-03-06 19:11:25 +0000 | [diff] [blame] | 333 | TPM->schedulePass(P); |
Devang Patel | bfd5905 | 2007-02-23 00:36:57 +0000 | [diff] [blame] | 334 | |
| 335 | // [4] Push new manager into PMS |
| 336 | PMS.push(LPPM); |
| 337 | } |
| 338 | |
| 339 | LPPM->add(this); |
| 340 | } |