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