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