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