Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1 | //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 5 | // This file was developed by Devang Patel and is distributed under |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LLVM Pass Manager infrastructure. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | |
| 15 | #include "llvm/PassManager.h" |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 17 | #include "llvm/ModuleProvider.h" |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 18 | #include <vector> |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 19 | #include <map> |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 23 | namespace llvm { |
| 24 | |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 25 | /// CommonPassManagerImpl helps pass manager analysis required by |
| 26 | /// the managed passes. It provides methods to add/remove analysis |
| 27 | /// available and query if certain analysis is available or not. |
Devang Patel | 42add71 | 2006-11-15 01:11:27 +0000 | [diff] [blame] | 28 | class CommonPassManagerImpl { |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 29 | |
| 30 | public: |
| 31 | |
| 32 | /// Return true IFF pass P's required analysis set does not required new |
| 33 | /// manager. |
| 34 | bool manageablePass(Pass *P); |
| 35 | |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 36 | Pass *getAnalysisPass(AnalysisID AID) const { |
| 37 | |
| 38 | std::map<AnalysisID, Pass*>::const_iterator I = |
| 39 | AvailableAnalysis.find(AID); |
| 40 | |
| 41 | if (I != AvailableAnalysis.end()) |
| 42 | return NULL; |
| 43 | else |
| 44 | return I->second; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 45 | } |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 46 | |
| 47 | /// Augment RequiredAnalysis by adding analysis required by pass P. |
| 48 | void noteDownRequiredAnalysis(Pass *P); |
| 49 | |
| 50 | /// Augment AvailableAnalysis by adding analysis made available by pass P. |
| 51 | void noteDownAvailableAnalysis(Pass *P); |
| 52 | |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 53 | /// Remove Analysis that is not preserved by the pass |
| 54 | void removeNotPreservedAnalysis(Pass *P); |
| 55 | |
| 56 | /// Remove dead passes |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 57 | void removeDeadPasses(Pass *P); |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 58 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 59 | /// Add pass P into the PassVector. Update RequiredAnalysis and |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 60 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
| 61 | void addPassToManager (Pass *P, bool ProcessAnalysis = true); |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 62 | |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 63 | /// Clear analysis vectors RequiredAnalysis and AvailableAnalysis. |
| 64 | /// This is used before running passes managed by the manager. |
| 65 | void clearAnalysis() { |
| 66 | RequiredAnalysis.clear(); |
| 67 | AvailableAnalysis.clear(); |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 68 | LastUser.clear(); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 71 | // All Required analyses should be available to the pass as it runs! Here |
| 72 | // we fill in the AnalysisImpls member of the pass so that it can |
| 73 | // successfully use the getAnalysis() method to retrieve the |
| 74 | // implementations it needs. |
| 75 | // |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 76 | void initializeAnalysisImpl(Pass *P); |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 77 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 78 | inline std::vector<Pass *>::iterator passVectorBegin() { |
| 79 | return PassVector.begin(); |
| 80 | } |
| 81 | |
| 82 | inline std::vector<Pass *>::iterator passVectorEnd() { |
| 83 | return PassVector.end(); |
| 84 | } |
| 85 | |
Devang Patel | 4a3fa4f | 2006-11-15 01:48:14 +0000 | [diff] [blame] | 86 | inline void setLastUser(Pass *P, Pass *LU) { |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 87 | LastUser[P] = LU; |
| 88 | // TODO : Check if pass P is available. |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 89 | } |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 90 | |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 91 | private: |
Devang Patel | dafa4dd | 2006-11-14 00:03:04 +0000 | [diff] [blame] | 92 | // Analysis required by the passes managed by this manager. This information |
| 93 | // used while selecting pass manager during addPass. If a pass does not |
| 94 | // preserve any analysis required by other passes managed by current |
| 95 | // pass manager then new pass manager is used. |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 96 | std::vector<AnalysisID> RequiredAnalysis; |
| 97 | |
Devang Patel | dafa4dd | 2006-11-14 00:03:04 +0000 | [diff] [blame] | 98 | // Set of available Analysis. This information is used while scheduling |
| 99 | // pass. If a pass requires an analysis which is not not available then |
| 100 | // equired analysis pass is scheduled to run before the pass itself is |
| 101 | // scheduled to run. |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 102 | std::map<AnalysisID, Pass*> AvailableAnalysis; |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 103 | |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 104 | // Map to keep track of last user of the analysis pass. |
| 105 | // LastUser->second is the last user of Lastuser->first. |
| 106 | std::map<Pass *, Pass *> LastUser; |
| 107 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 108 | // Collection of pass that are managed by this manager |
| 109 | std::vector<Pass *> PassVector; |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 112 | /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the |
| 113 | /// pass together and sequence them to process one basic block before |
| 114 | /// processing next basic block. |
Devang Patel | 42add71 | 2006-11-15 01:11:27 +0000 | [diff] [blame] | 115 | class BasicBlockPassManager_New : public CommonPassManagerImpl, |
| 116 | public FunctionPass { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 117 | |
| 118 | public: |
| 119 | BasicBlockPassManager_New() { } |
| 120 | |
| 121 | /// Add a pass into a passmanager queue. |
| 122 | bool addPass(Pass *p); |
| 123 | |
| 124 | /// Execute all of the passes scheduled for execution. Keep track of |
| 125 | /// whether any of the passes modifies the function, and if so, return true. |
| 126 | bool runOnFunction(Function &F); |
| 127 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 128 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 129 | Pass *getAnalysisPassFromManager(AnalysisID AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 130 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 131 | private: |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 134 | /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers. |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 135 | /// It batches all function passes and basic block pass managers together and |
| 136 | /// sequence them to process one function at a time before processing next |
| 137 | /// function. |
Devang Patel | 42add71 | 2006-11-15 01:11:27 +0000 | [diff] [blame] | 138 | class FunctionPassManagerImpl_New : public CommonPassManagerImpl, |
| 139 | public ModulePass { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 140 | public: |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 141 | FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ } |
| 142 | FunctionPassManagerImpl_New() { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 143 | activeBBPassManager = NULL; |
| 144 | } |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 145 | ~FunctionPassManagerImpl_New() { /* TODO */ }; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 146 | |
| 147 | /// add - Add a pass to the queue of passes to run. This passes |
| 148 | /// ownership of the Pass to the PassManager. When the |
| 149 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 150 | /// there is no need to delete the pass. (TODO delete passes.) |
| 151 | /// This implies that all passes MUST be allocated with 'new'. |
| 152 | void add(Pass *P) { /* TODO*/ } |
| 153 | |
| 154 | /// Add pass into the pass manager queue. |
| 155 | bool addPass(Pass *P); |
| 156 | |
| 157 | /// Execute all of the passes scheduled for execution. Keep |
| 158 | /// track of whether any of the passes modifies the function, and if |
| 159 | /// so, return true. |
| 160 | bool runOnModule(Module &M); |
| 161 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 162 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 163 | Pass *getAnalysisPassFromManager(AnalysisID AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 164 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 165 | /// doInitialization - Run all of the initializers for the function passes. |
| 166 | /// |
| 167 | bool doInitialization(Module &M); |
| 168 | |
| 169 | /// doFinalization - Run all of the initializers for the function passes. |
| 170 | /// |
| 171 | bool doFinalization(Module &M); |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 172 | private: |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 173 | // Active Pass Managers |
| 174 | BasicBlockPassManager_New *activeBBPassManager; |
| 175 | }; |
| 176 | |
| 177 | /// ModulePassManager_New manages ModulePasses and function pass managers. |
| 178 | /// It batches all Module passes passes and function pass managers together and |
| 179 | /// sequence them to process one module. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 180 | class ModulePassManager_New : public CommonPassManagerImpl { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 181 | |
| 182 | public: |
| 183 | ModulePassManager_New() { activeFunctionPassManager = NULL; } |
| 184 | |
| 185 | /// Add a pass into a passmanager queue. |
| 186 | bool addPass(Pass *p); |
| 187 | |
| 188 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 189 | /// whether any of the passes modifies the module, and if so, return true. |
| 190 | bool runOnModule(Module &M); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 191 | |
| 192 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 193 | Pass *getAnalysisPassFromManager(AnalysisID AID); |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 194 | |
| 195 | private: |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 196 | // Active Pass Manager |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 197 | FunctionPassManagerImpl_New *activeFunctionPassManager; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 200 | /// PassManager_New manages ModulePassManagers |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 201 | class PassManagerImpl_New : public CommonPassManagerImpl { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 202 | |
| 203 | public: |
| 204 | |
| 205 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 206 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 207 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 208 | /// implies that all passes MUST be allocated with 'new'. |
| 209 | void add(Pass *P); |
| 210 | |
| 211 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 212 | /// whether any of the passes modifies the module, and if so, return true. |
| 213 | bool run(Module &M); |
| 214 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 215 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 216 | Pass *getAnalysisPassFromManager(AnalysisID AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 217 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 218 | private: |
| 219 | |
| 220 | /// Add a pass into a passmanager queue. This is used by schedulePasses |
| 221 | bool addPass(Pass *p); |
| 222 | |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 223 | /// Schedule pass P for execution. Make sure that passes required by |
| 224 | /// P are run before P is run. Update analysis info maintained by |
| 225 | /// the manager. Remove dead passes. This is a recursive function. |
| 226 | void schedulePass(Pass *P); |
| 227 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 228 | /// Schedule all passes collected in pass queue using add(). Add all the |
| 229 | /// schedule passes into various manager's queue using addPass(). |
| 230 | void schedulePasses(); |
| 231 | |
| 232 | // Collection of pass managers |
| 233 | std::vector<ModulePassManager_New *> PassManagers; |
| 234 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 235 | // Active Pass Manager |
| 236 | ModulePassManager_New *activeManager; |
| 237 | }; |
| 238 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 239 | } // End of llvm namespace |
| 240 | |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 241 | // CommonPassManagerImpl implementation |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 242 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 243 | /// Return true IFF pass P's required analysis set does not required new |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 244 | /// manager. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 245 | bool CommonPassManagerImpl::manageablePass(Pass *P) { |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 246 | |
| 247 | AnalysisUsage AnUsage; |
| 248 | P->getAnalysisUsage(AnUsage); |
| 249 | |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 250 | // If this pass is not preserving information that is required by the other |
| 251 | // passes managed by this manager then use new manager |
| 252 | if (!AnUsage.getPreservesAll()) { |
| 253 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
| 254 | for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(), |
| 255 | E = RequiredAnalysis.end(); I != E; ++I) { |
| 256 | if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) == |
| 257 | PreservedSet.end()) |
| 258 | // This analysis is not preserved. Need new manager. |
| 259 | return false; |
| 260 | } |
| 261 | } |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 262 | return true; |
| 263 | } |
| 264 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 265 | /// Augment RequiredAnalysis by adding analysis required by pass P. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 266 | void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) { |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 267 | AnalysisUsage AnUsage; |
| 268 | P->getAnalysisUsage(AnUsage); |
| 269 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 270 | |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 271 | // FIXME: What about duplicates ? |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 272 | RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(), |
| 273 | RequiredSet.end()); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 274 | |
| 275 | initializeAnalysisImpl(P); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 278 | /// Augement AvailableAnalysis by adding analysis made available by pass P. |
| 279 | void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 280 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 281 | if (const PassInfo *PI = P->getPassInfo()) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 282 | AvailableAnalysis[PI] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 283 | |
| 284 | //TODO This pass is the current implementation of all of the interfaces it |
| 285 | //TODO implements as well. |
| 286 | //TODO |
| 287 | //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 288 | //TODO for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 289 | //TODO CurrentAnalyses[II[i]] = P; |
| 290 | } |
| 291 | } |
| 292 | |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 293 | /// Remove Analyss not preserved by Pass P |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 294 | void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) { |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 295 | AnalysisUsage AnUsage; |
| 296 | P->getAnalysisUsage(AnUsage); |
| 297 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 298 | |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 299 | for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 300 | E = AvailableAnalysis.end(); I != E; ++I ) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 301 | if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) == |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 302 | PreservedSet.end()) { |
| 303 | // Remove this analysis |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 304 | std::map<AnalysisID, Pass*>::iterator J = I++; |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 305 | AvailableAnalysis.erase(J); |
| 306 | } |
| 307 | } |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 310 | /// Remove analysis passes that are not used any longer |
| 311 | void CommonPassManagerImpl::removeDeadPasses(Pass *P) { |
| 312 | |
| 313 | for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(), |
| 314 | E = LastUser.end(); I !=E; ++I) { |
| 315 | if (I->second == P) { |
| 316 | Pass *deadPass = I->first; |
| 317 | deadPass->releaseMemory(); |
| 318 | |
| 319 | std::map<AnalysisID, Pass*>::iterator Pos = |
| 320 | AvailableAnalysis.find(deadPass->getPassInfo()); |
| 321 | |
| 322 | assert (Pos != AvailableAnalysis.end() && |
| 323 | "Pass is not available"); |
| 324 | AvailableAnalysis.erase(Pos); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 329 | /// Add pass P into the PassVector. Update RequiredAnalysis and |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 330 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
| 331 | void CommonPassManagerImpl::addPassToManager (Pass *P, |
| 332 | bool ProcessAnalysis) { |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 333 | |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 334 | if (ProcessAnalysis) { |
| 335 | // Take a note of analysis required and made available by this pass |
| 336 | noteDownRequiredAnalysis(P); |
| 337 | noteDownAvailableAnalysis(P); |
| 338 | |
| 339 | // Remove the analysis not preserved by this pass |
| 340 | removeNotPreservedAnalysis(P); |
| 341 | } |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 342 | |
| 343 | // Add pass |
| 344 | PassVector.push_back(P); |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 347 | // All Required analyses should be available to the pass as it runs! Here |
| 348 | // we fill in the AnalysisImpls member of the pass so that it can |
| 349 | // successfully use the getAnalysis() method to retrieve the |
| 350 | // implementations it needs. |
| 351 | // |
| 352 | void CommonPassManagerImpl::initializeAnalysisImpl(Pass *P) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 353 | AnalysisUsage AnUsage; |
| 354 | P->getAnalysisUsage(AnUsage); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 355 | |
| 356 | for (std::vector<const PassInfo *>::const_iterator |
| 357 | I = AnUsage.getRequiredSet().begin(), |
| 358 | E = AnUsage.getRequiredSet().end(); I != E; ++I) { |
| 359 | Pass *Impl = getAnalysisPass(*I); |
| 360 | if (Impl == 0) |
| 361 | assert(0 && "Analysis used but not available!"); |
| 362 | // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl)); |
| 363 | } |
| 364 | } |
| 365 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 366 | /// BasicBlockPassManager implementation |
| 367 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 368 | /// Add pass P into PassVector and return true. If this pass is not |
| 369 | /// manageable by this manager then return false. |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 370 | bool |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 371 | BasicBlockPassManager_New::addPass(Pass *P) { |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 372 | |
| 373 | BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); |
| 374 | if (!BP) |
| 375 | return false; |
| 376 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 377 | // If this pass does not preserve anlysis that is used by other passes |
| 378 | // managed by this manager than it is not a suiable pass for this manager. |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 379 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 380 | return false; |
| 381 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 382 | addPassToManager (BP); |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 383 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 384 | return true; |
| 385 | } |
| 386 | |
| 387 | /// Execute all of the passes scheduled for execution by invoking |
| 388 | /// runOnBasicBlock method. Keep track of whether any of the passes modifies |
| 389 | /// the function, and if so, return true. |
| 390 | bool |
| 391 | BasicBlockPassManager_New::runOnFunction(Function &F) { |
| 392 | |
| 393 | bool Changed = false; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 394 | clearAnalysis(); |
| 395 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 396 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 397 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 398 | e = passVectorEnd(); itr != e; ++itr) { |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 399 | Pass *P = *itr; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 400 | |
| 401 | noteDownAvailableAnalysis(P); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 402 | BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); |
| 403 | Changed |= BP->runOnBasicBlock(*I); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 404 | removeNotPreservedAnalysis(P); |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 405 | removeDeadPasses(P); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 406 | } |
| 407 | return Changed; |
| 408 | } |
| 409 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 410 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 411 | Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) { |
| 412 | return getAnalysisPass(AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 415 | // FunctionPassManager_New implementation |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 416 | /// Create new Function pass manager |
| 417 | FunctionPassManager_New::FunctionPassManager_New() { |
| 418 | FPM = new FunctionPassManagerImpl_New(); |
| 419 | } |
| 420 | |
| 421 | /// add - Add a pass to the queue of passes to run. This passes |
| 422 | /// ownership of the Pass to the PassManager. When the |
| 423 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 424 | /// there is no need to delete the pass. (TODO delete passes.) |
| 425 | /// This implies that all passes MUST be allocated with 'new'. |
| 426 | void |
| 427 | FunctionPassManager_New::add(Pass *P) { |
| 428 | FPM->add(P); |
| 429 | } |
| 430 | |
| 431 | /// Execute all of the passes scheduled for execution. Keep |
| 432 | /// track of whether any of the passes modifies the function, and if |
| 433 | /// so, return true. |
| 434 | bool |
| 435 | FunctionPassManager_New::runOnModule(Module &M) { |
| 436 | return FPM->runOnModule(M); |
| 437 | } |
| 438 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 439 | /// doInitialization - Run all of the initializers for the function passes. |
| 440 | /// |
| 441 | bool FunctionPassManager_New::doInitialization() { |
| 442 | return FPM->doInitialization(*MP->getModule()); |
| 443 | } |
| 444 | |
| 445 | /// doFinalization - Run all of the initializers for the function passes. |
| 446 | /// |
| 447 | bool FunctionPassManager_New::doFinalization() { |
| 448 | return FPM->doFinalization(*MP->getModule()); |
| 449 | } |
| 450 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 451 | // FunctionPassManagerImpl_New implementation |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 452 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 453 | // FunctionPassManager |
| 454 | |
| 455 | /// Add pass P into the pass manager queue. If P is a BasicBlockPass then |
| 456 | /// either use it into active basic block pass manager or create new basic |
| 457 | /// block pass manager to handle pass P. |
| 458 | bool |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 459 | FunctionPassManagerImpl_New::addPass(Pass *P) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 460 | |
| 461 | // If P is a BasicBlockPass then use BasicBlockPassManager_New. |
| 462 | if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) { |
| 463 | |
| 464 | if (!activeBBPassManager |
| 465 | || !activeBBPassManager->addPass(BP)) { |
| 466 | |
| 467 | activeBBPassManager = new BasicBlockPassManager_New(); |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 468 | addPassToManager(activeBBPassManager, false); |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 469 | if (!activeBBPassManager->addPass(BP)) |
| 470 | assert(0 && "Unable to add Pass"); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 471 | } |
| 472 | return true; |
| 473 | } |
| 474 | |
| 475 | FunctionPass *FP = dynamic_cast<FunctionPass *>(P); |
| 476 | if (!FP) |
| 477 | return false; |
| 478 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 479 | // If this pass does not preserve anlysis that is used by other passes |
| 480 | // managed by this manager than it is not a suiable pass for this manager. |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 481 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 482 | return false; |
| 483 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 484 | addPassToManager (FP); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 485 | activeBBPassManager = NULL; |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | /// Execute all of the passes scheduled for execution by invoking |
| 490 | /// runOnFunction method. Keep track of whether any of the passes modifies |
| 491 | /// the function, and if so, return true. |
| 492 | bool |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 493 | FunctionPassManagerImpl_New::runOnModule(Module &M) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 494 | |
| 495 | bool Changed = false; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 496 | clearAnalysis(); |
| 497 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 498 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 499 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 500 | e = passVectorEnd(); itr != e; ++itr) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 501 | Pass *P = *itr; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 502 | |
| 503 | noteDownAvailableAnalysis(P); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 504 | FunctionPass *FP = dynamic_cast<FunctionPass*>(P); |
| 505 | Changed |= FP->runOnFunction(*I); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 506 | removeNotPreservedAnalysis(P); |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 507 | removeDeadPasses(P); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 508 | } |
| 509 | return Changed; |
| 510 | } |
| 511 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 512 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 513 | Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) { |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 514 | |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 515 | Pass *P = getAnalysisPass(AID); |
| 516 | if (P) |
| 517 | return P; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 518 | |
| 519 | if (activeBBPassManager && |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 520 | activeBBPassManager->getAnalysisPass(AID) != 0) |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 521 | return activeBBPassManager->getAnalysisPass(AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 522 | |
| 523 | // TODO : Check inactive managers |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 524 | return NULL; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 525 | } |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 526 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 527 | inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) { |
| 528 | bool Changed = false; |
| 529 | |
| 530 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 531 | e = passVectorEnd(); itr != e; ++itr) { |
| 532 | Pass *P = *itr; |
| 533 | |
| 534 | FunctionPass *FP = dynamic_cast<FunctionPass*>(P); |
| 535 | Changed |= FP->doInitialization(M); |
| 536 | } |
| 537 | |
| 538 | return Changed; |
| 539 | } |
| 540 | |
| 541 | inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) { |
| 542 | bool Changed = false; |
| 543 | |
| 544 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 545 | e = passVectorEnd(); itr != e; ++itr) { |
| 546 | Pass *P = *itr; |
| 547 | |
| 548 | FunctionPass *FP = dynamic_cast<FunctionPass*>(P); |
| 549 | Changed |= FP->doFinalization(M); |
| 550 | } |
| 551 | |
| 552 | |
| 553 | return Changed; |
| 554 | } |
| 555 | |
| 556 | |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 557 | // ModulePassManager implementation |
| 558 | |
| 559 | /// Add P into pass vector if it is manageble. If P is a FunctionPass |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 560 | /// then use FunctionPassManagerImpl_New to manage it. Return false if P |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 561 | /// is not manageable by this manager. |
| 562 | bool |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 563 | ModulePassManager_New::addPass(Pass *P) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 564 | |
| 565 | // If P is FunctionPass then use function pass maanager. |
| 566 | if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) { |
| 567 | |
| 568 | activeFunctionPassManager = NULL; |
| 569 | |
| 570 | if (!activeFunctionPassManager |
| 571 | || !activeFunctionPassManager->addPass(P)) { |
| 572 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 573 | activeFunctionPassManager = new FunctionPassManagerImpl_New(); |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 574 | addPassToManager(activeFunctionPassManager, false); |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 575 | if (!activeFunctionPassManager->addPass(FP)) |
| 576 | assert(0 && "Unable to add pass"); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 577 | } |
| 578 | return true; |
| 579 | } |
| 580 | |
| 581 | ModulePass *MP = dynamic_cast<ModulePass *>(P); |
| 582 | if (!MP) |
| 583 | return false; |
| 584 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 585 | // If this pass does not preserve anlysis that is used by other passes |
| 586 | // managed by this manager than it is not a suiable pass for this manager. |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 587 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 588 | return false; |
| 589 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 590 | addPassToManager(MP); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 591 | activeFunctionPassManager = NULL; |
| 592 | return true; |
| 593 | } |
| 594 | |
| 595 | |
| 596 | /// Execute all of the passes scheduled for execution by invoking |
| 597 | /// runOnModule method. Keep track of whether any of the passes modifies |
| 598 | /// the module, and if so, return true. |
| 599 | bool |
| 600 | ModulePassManager_New::runOnModule(Module &M) { |
| 601 | bool Changed = false; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 602 | clearAnalysis(); |
| 603 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 604 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 605 | e = passVectorEnd(); itr != e; ++itr) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 606 | Pass *P = *itr; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 607 | |
| 608 | noteDownAvailableAnalysis(P); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 609 | ModulePass *MP = dynamic_cast<ModulePass*>(P); |
| 610 | Changed |= MP->runOnModule(M); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 611 | removeNotPreservedAnalysis(P); |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 612 | removeDeadPasses(P); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 613 | } |
| 614 | return Changed; |
| 615 | } |
| 616 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 617 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 618 | Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) { |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 619 | |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 620 | |
| 621 | Pass *P = getAnalysisPass(AID); |
| 622 | if (P) |
| 623 | return P; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 624 | |
| 625 | if (activeFunctionPassManager && |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 626 | activeFunctionPassManager->getAnalysisPass(AID) != 0) |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 627 | return activeFunctionPassManager->getAnalysisPass(AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 628 | |
| 629 | // TODO : Check inactive managers |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 630 | return NULL; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 634 | Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) { |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 635 | |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 636 | Pass *P = NULL; |
Devang Patel | 7086844 | 2006-11-13 22:53:19 +0000 | [diff] [blame] | 637 | for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 638 | e = PassManagers.end(); !P && itr != e; ++itr) |
| 639 | P = (*itr)->getAnalysisPassFromManager(AID); |
| 640 | return P; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 643 | /// Schedule pass P for execution. Make sure that passes required by |
| 644 | /// P are run before P is run. Update analysis info maintained by |
| 645 | /// the manager. Remove dead passes. This is a recursive function. |
| 646 | void PassManagerImpl_New::schedulePass(Pass *P) { |
| 647 | |
| 648 | AnalysisUsage AnUsage; |
| 649 | P->getAnalysisUsage(AnUsage); |
| 650 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
| 651 | for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(), |
| 652 | E = RequiredSet.end(); I != E; ++I) { |
| 653 | |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 654 | Pass *AnalysisPass = getAnalysisPassFromManager(*I); |
| 655 | if (!AnalysisPass) { |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 656 | // Schedule this analysis run first. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 657 | AnalysisPass = (*I)->createPass(); |
| 658 | schedulePass(AnalysisPass); |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 659 | } |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 660 | setLastUser (AnalysisPass, P); |
Devang Patel | 4a3fa4f | 2006-11-15 01:48:14 +0000 | [diff] [blame] | 661 | |
| 662 | // Prolong live range of analyses that are needed after an analysis pass |
| 663 | // is destroyed, for querying by subsequent passes |
| 664 | const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet(); |
| 665 | for (std::vector<AnalysisID>::const_iterator I = IDs.begin(), |
| 666 | E = IDs.end(); I != E; ++I) { |
| 667 | Pass *AP = getAnalysisPassFromManager(*I); |
| 668 | assert (AP && "Analysis pass is not available"); |
| 669 | setLastUser(AP, P); |
| 670 | } |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 671 | } |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 672 | addPass(P); |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 675 | /// Schedule all passes from the queue by adding them in their |
| 676 | /// respective manager's queue. |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 677 | void PassManagerImpl_New::schedulePasses() { |
| 678 | for (std::vector<Pass *>::iterator I = passVectorBegin(), |
| 679 | E = passVectorEnd(); I != E; ++I) |
| 680 | schedulePass (*I); |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /// Add pass P to the queue of passes to run. |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 684 | void PassManagerImpl_New::add(Pass *P) { |
| 685 | // Do not process Analysis now. Analysis is process while scheduling |
| 686 | // the pass vector. |
Devang Patel | db789fb | 2006-11-11 02:06:21 +0000 | [diff] [blame] | 687 | addPassToManager(P, false); |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | // PassManager_New implementation |
| 691 | /// Add P into active pass manager or use new module pass manager to |
| 692 | /// manage it. |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 693 | bool PassManagerImpl_New::addPass(Pass *P) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 694 | |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 695 | if (!activeManager || !activeManager->addPass(P)) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 696 | activeManager = new ModulePassManager_New(); |
| 697 | PassManagers.push_back(activeManager); |
| 698 | } |
| 699 | |
| 700 | return activeManager->addPass(P); |
| 701 | } |
| 702 | |
| 703 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 704 | /// whether any of the passes modifies the module, and if so, return true. |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 705 | bool PassManagerImpl_New::run(Module &M) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 706 | |
| 707 | schedulePasses(); |
| 708 | bool Changed = false; |
| 709 | for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), |
| 710 | e = PassManagers.end(); itr != e; ++itr) { |
| 711 | ModulePassManager_New *pm = *itr; |
| 712 | Changed |= pm->runOnModule(M); |
| 713 | } |
| 714 | return Changed; |
| 715 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 716 | |
| 717 | /// Create new pass manager |
| 718 | PassManager_New::PassManager_New() { |
| 719 | PM = new PassManagerImpl_New(); |
| 720 | } |
| 721 | |
| 722 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 723 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 724 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 725 | /// implies that all passes MUST be allocated with 'new'. |
| 726 | void |
| 727 | PassManager_New::add(Pass *P) { |
| 728 | PM->add(P); |
| 729 | } |
| 730 | |
| 731 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 732 | /// whether any of the passes modifies the module, and if so, return true. |
| 733 | bool |
| 734 | PassManager_New::run(Module &M) { |
| 735 | return PM->run(M); |
| 736 | } |
| 737 | |