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