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 | d0e6e33 | 2007-02-22 23:30:07 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Devang Patel | 5afdc7d | 2007-02-23 00:10:16 +0000 | [diff] [blame] | 29 | /// Delete loop from the loop queue. This is used by Loop pass to inform |
| 30 | /// Loop Pass Manager that it should skip rest of the passes for this loop. |
| 31 | void LPPassManager::deleteLoopFromQueue(Loop *L) { |
| 32 | // Do not pop loop from LQ here. It will be done by runOnFunction while loop. |
| 33 | skipThisLoop = true; |
| 34 | } |
| 35 | |
Devang Patel | 8ded585 | 2007-02-23 00:16:44 +0000 | [diff] [blame] | 36 | // Reoptimize this loop. LPPassManager will re-insert this loop into the |
| 37 | // queue. This allows LoopPass to change loop nest for the loop. This |
| 38 | // utility may send LPPassManager into infinite loops so use caution. |
| 39 | void LPPassManager::redoLoop(Loop *L) { |
| 40 | redoThisLoop = true; |
| 41 | } |
| 42 | |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 43 | // Recurse through all subloops and all loops into LQ. |
Devang Patel | 3015972 | 2007-03-06 02:30:46 +0000 | [diff] [blame^] | 44 | static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) { |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 45 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 46 | addLoopIntoQueue(*I, LQ); |
Devang Patel | 3015972 | 2007-03-06 02:30:46 +0000 | [diff] [blame^] | 47 | LQ.push_back(L); |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 50 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 51 | /// whether any of the passes modifies the function, and if so, return true. |
| 52 | bool LPPassManager::runOnFunction(Function &F) { |
| 53 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 54 | bool Changed = false; |
| 55 | |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 56 | // Populate Loop Queue |
| 57 | for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I) |
| 58 | addLoopIntoQueue(*I, LQ); |
| 59 | |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 60 | // Walk Loops |
Devang Patel | 3015972 | 2007-03-06 02:30:46 +0000 | [diff] [blame^] | 61 | while (!LQ.empty()) { |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 62 | |
Devang Patel | 3015972 | 2007-03-06 02:30:46 +0000 | [diff] [blame^] | 63 | Loop *L = LQ.back(); |
Devang Patel | 5afdc7d | 2007-02-23 00:10:16 +0000 | [diff] [blame] | 64 | skipThisLoop = false; |
Devang Patel | 8ded585 | 2007-02-23 00:16:44 +0000 | [diff] [blame] | 65 | redoThisLoop = false; |
Devang Patel | 5afdc7d | 2007-02-23 00:10:16 +0000 | [diff] [blame] | 66 | |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 67 | // Run all passes on current SCC |
| 68 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 69 | |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 70 | Pass *P = getContainedPass(Index); |
| 71 | AnalysisUsage AnUsage; |
| 72 | P->getAnalysisUsage(AnUsage); |
| 73 | |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 74 | dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG, ""); |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 75 | dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet()); |
| 76 | |
| 77 | initializeAnalysisImpl(P); |
| 78 | |
| 79 | StartPassTimer(P); |
| 80 | LoopPass *LP = dynamic_cast<LoopPass *>(P); |
| 81 | assert (LP && "Invalid LPPassManager member"); |
Devang Patel | 4b26463 | 2007-02-23 17:53:17 +0000 | [diff] [blame] | 82 | LP->runOnLoop(L, *this); |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 83 | StopPassTimer(P); |
| 84 | |
| 85 | if (Changed) |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 86 | dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG, ""); |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 87 | dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet()); |
| 88 | |
| 89 | removeNotPreservedAnalysis(P); |
| 90 | recordAvailableAnalysis(P); |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 91 | removeDeadPasses(P, "", ON_LOOP_MSG); |
Devang Patel | 5afdc7d | 2007-02-23 00:10:16 +0000 | [diff] [blame] | 92 | |
| 93 | if (skipThisLoop) |
| 94 | // Do not run other passes on this loop. |
| 95 | break; |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 96 | } |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 97 | |
| 98 | // Pop the loop from queue after running all passes. |
Devang Patel | 3015972 | 2007-03-06 02:30:46 +0000 | [diff] [blame^] | 99 | LQ.pop_back(); |
Devang Patel | 8ded585 | 2007-02-23 00:16:44 +0000 | [diff] [blame] | 100 | |
| 101 | if (redoThisLoop) |
Devang Patel | 3015972 | 2007-03-06 02:30:46 +0000 | [diff] [blame^] | 102 | LQ.push_back(L); |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | return Changed; |
| 106 | } |
| 107 | |
| 108 | |
Devang Patel | bfd5905 | 2007-02-23 00:36:57 +0000 | [diff] [blame] | 109 | //===----------------------------------------------------------------------===// |
| 110 | // LoopPass |
| 111 | |
| 112 | /// Assign pass manager to manage this pass. |
| 113 | void LoopPass::assignPassManager(PMStack &PMS, |
| 114 | PassManagerType PreferredType) { |
| 115 | // Find LPPassManager |
| 116 | while (!PMS.empty()) { |
| 117 | if (PMS.top()->getPassManagerType() > PMT_LoopPassManager) |
| 118 | PMS.pop(); |
| 119 | else; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top()); |
| 124 | |
| 125 | // Create new Loop Pass Manager if it does not exist. |
| 126 | if (!LPPM) { |
| 127 | |
| 128 | assert (!PMS.empty() && "Unable to create Loop Pass Manager"); |
| 129 | PMDataManager *PMD = PMS.top(); |
| 130 | |
| 131 | // [1] Create new Call Graph Pass Manager |
| 132 | LPPM = new LPPassManager(PMD->getDepth() + 1); |
| 133 | |
| 134 | // [2] Set up new manager's top level manager |
| 135 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 136 | TPM->addIndirectPassManager(LPPM); |
| 137 | |
| 138 | // [3] Assign manager to manage this new manager. This may create |
| 139 | // and push new managers into PMS |
| 140 | Pass *P = dynamic_cast<Pass *>(LPPM); |
| 141 | P->assignPassManager(PMS); |
| 142 | |
| 143 | // [4] Push new manager into PMS |
| 144 | PMS.push(LPPM); |
| 145 | } |
| 146 | |
| 147 | LPPM->add(this); |
| 148 | } |
| 149 | |