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