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