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" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 20 | namespace llvm { |
| 21 | |
| 22 | /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the |
| 23 | /// pass together and sequence them to process one basic block before |
| 24 | /// processing next basic block. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 25 | class BasicBlockPassManager_New : public CommonPassManagerImpl { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 26 | |
| 27 | public: |
| 28 | BasicBlockPassManager_New() { } |
| 29 | |
| 30 | /// Add a pass into a passmanager queue. |
| 31 | bool addPass(Pass *p); |
| 32 | |
| 33 | /// Execute all of the passes scheduled for execution. Keep track of |
| 34 | /// whether any of the passes modifies the function, and if so, return true. |
| 35 | bool runOnFunction(Function &F); |
| 36 | |
| 37 | private: |
| 38 | // Collection of pass that are managed by this manager |
| 39 | std::vector<Pass *> PassVector; |
| 40 | }; |
| 41 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 42 | /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers. |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 43 | /// It batches all function passes and basic block pass managers together and |
| 44 | /// sequence them to process one function at a time before processing next |
| 45 | /// function. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 46 | class FunctionPassManagerImpl_New : public CommonPassManagerImpl { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 47 | public: |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 48 | FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ } |
| 49 | FunctionPassManagerImpl_New() { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 50 | activeBBPassManager = NULL; |
| 51 | } |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 52 | ~FunctionPassManagerImpl_New() { /* TODO */ }; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 53 | |
| 54 | /// add - Add a pass to the queue of passes to run. This passes |
| 55 | /// ownership of the Pass to the PassManager. When the |
| 56 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 57 | /// there is no need to delete the pass. (TODO delete passes.) |
| 58 | /// This implies that all passes MUST be allocated with 'new'. |
| 59 | void add(Pass *P) { /* TODO*/ } |
| 60 | |
| 61 | /// Add pass into the pass manager queue. |
| 62 | bool addPass(Pass *P); |
| 63 | |
| 64 | /// Execute all of the passes scheduled for execution. Keep |
| 65 | /// track of whether any of the passes modifies the function, and if |
| 66 | /// so, return true. |
| 67 | bool runOnModule(Module &M); |
| 68 | |
| 69 | private: |
| 70 | // Collection of pass that are manged by this manager |
| 71 | std::vector<Pass *> PassVector; |
| 72 | |
| 73 | // Active Pass Managers |
| 74 | BasicBlockPassManager_New *activeBBPassManager; |
| 75 | }; |
| 76 | |
| 77 | /// ModulePassManager_New manages ModulePasses and function pass managers. |
| 78 | /// It batches all Module passes passes and function pass managers together and |
| 79 | /// sequence them to process one module. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 80 | class ModulePassManager_New : public CommonPassManagerImpl { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 81 | |
| 82 | public: |
| 83 | ModulePassManager_New() { activeFunctionPassManager = NULL; } |
| 84 | |
| 85 | /// Add a pass into a passmanager queue. |
| 86 | bool addPass(Pass *p); |
| 87 | |
| 88 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 89 | /// whether any of the passes modifies the module, and if so, return true. |
| 90 | bool runOnModule(Module &M); |
| 91 | |
| 92 | private: |
| 93 | // Collection of pass that are managed by this manager |
| 94 | std::vector<Pass *> PassVector; |
| 95 | |
| 96 | // Active Pass Manager |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 97 | FunctionPassManagerImpl_New *activeFunctionPassManager; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 100 | /// PassManager_New manages ModulePassManagers |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 101 | class PassManagerImpl_New : public CommonPassManagerImpl { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 102 | |
| 103 | public: |
| 104 | |
| 105 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 106 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 107 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 108 | /// implies that all passes MUST be allocated with 'new'. |
| 109 | void add(Pass *P); |
| 110 | |
| 111 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 112 | /// whether any of the passes modifies the module, and if so, return true. |
| 113 | bool run(Module &M); |
| 114 | |
| 115 | private: |
| 116 | |
| 117 | /// Add a pass into a passmanager queue. This is used by schedulePasses |
| 118 | bool addPass(Pass *p); |
| 119 | |
| 120 | /// Schedule all passes collected in pass queue using add(). Add all the |
| 121 | /// schedule passes into various manager's queue using addPass(). |
| 122 | void schedulePasses(); |
| 123 | |
| 124 | // Collection of pass managers |
| 125 | std::vector<ModulePassManager_New *> PassManagers; |
| 126 | |
| 127 | // Collection of pass that are not yet scheduled |
| 128 | std::vector<Pass *> PassVector; |
| 129 | |
| 130 | // Active Pass Manager |
| 131 | ModulePassManager_New *activeManager; |
| 132 | }; |
| 133 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 134 | } // End of llvm namespace |
| 135 | |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 136 | // CommonPassManagerImpl implementation |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 137 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 138 | /// 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] | 139 | /// manager. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 140 | bool CommonPassManagerImpl::manageablePass(Pass *P) { |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 141 | |
| 142 | AnalysisUsage AnUsage; |
| 143 | P->getAnalysisUsage(AnUsage); |
| 144 | |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 145 | // If this pass is not preserving information that is required by the other |
| 146 | // passes managed by this manager then use new manager |
| 147 | if (!AnUsage.getPreservesAll()) { |
| 148 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
| 149 | for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(), |
| 150 | E = RequiredAnalysis.end(); I != E; ++I) { |
| 151 | if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) == |
| 152 | PreservedSet.end()) |
| 153 | // This analysis is not preserved. Need new manager. |
| 154 | return false; |
| 155 | } |
| 156 | } |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 160 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 161 | bool CommonPassManagerImpl::analysisCurrentlyAvailable(AnalysisID AID) { |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 162 | |
| 163 | // TODO |
| 164 | return false; |
| 165 | } |
| 166 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame^] | 167 | /// Augment RequiredAnalysis by adding analysis required by pass P. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 168 | void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) { |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 169 | AnalysisUsage AnUsage; |
| 170 | P->getAnalysisUsage(AnUsage); |
| 171 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 172 | |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 173 | // FIXME: What about duplicates ? |
| 174 | RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(), RequiredSet.end()); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame^] | 177 | /// Augement AvailableAnalysis by adding analysis made available by pass P. |
| 178 | void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) { |
| 179 | |
| 180 | if (const PassInfo *PI = P->getPassInfo()) { |
| 181 | AvailableAnalysis.insert(PI); |
| 182 | |
| 183 | //TODO This pass is the current implementation of all of the interfaces it |
| 184 | //TODO implements as well. |
| 185 | //TODO |
| 186 | //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 187 | //TODO for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 188 | //TODO CurrentAnalyses[II[i]] = P; |
| 189 | } |
| 190 | } |
| 191 | |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 192 | /// Remove AnalysisID from the RequiredSet |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 193 | void CommonPassManagerImpl::removeAnalysis(AnalysisID AID) { |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 194 | |
| 195 | // TODO |
| 196 | } |
| 197 | |
| 198 | /// Remove Analyss not preserved by Pass P |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 199 | void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) { |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 200 | |
| 201 | // TODO |
| 202 | } |
| 203 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 204 | /// BasicBlockPassManager implementation |
| 205 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 206 | /// Add pass P into PassVector and return true. If this pass is not |
| 207 | /// manageable by this manager then return false. |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 208 | bool |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 209 | BasicBlockPassManager_New::addPass(Pass *P) { |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 210 | |
| 211 | BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); |
| 212 | if (!BP) |
| 213 | return false; |
| 214 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 215 | // If this pass does not preserve anlysis that is used by other passes |
| 216 | // 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] | 217 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 218 | return false; |
| 219 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame^] | 220 | // Take a note of analysis required and made available by this pass |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 221 | noteDownRequiredAnalysis(P); |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame^] | 222 | noteDownAvailableAnalysis(P); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 223 | |
| 224 | // Add pass |
| 225 | PassVector.push_back(BP); |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | /// Execute all of the passes scheduled for execution by invoking |
| 230 | /// runOnBasicBlock method. Keep track of whether any of the passes modifies |
| 231 | /// the function, and if so, return true. |
| 232 | bool |
| 233 | BasicBlockPassManager_New::runOnFunction(Function &F) { |
| 234 | |
| 235 | bool Changed = false; |
| 236 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
| 237 | for (std::vector<Pass *>::iterator itr = PassVector.begin(), |
| 238 | e = PassVector.end(); itr != e; ++itr) { |
| 239 | Pass *P = *itr; |
| 240 | BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); |
| 241 | Changed |= BP->runOnBasicBlock(*I); |
| 242 | } |
| 243 | return Changed; |
| 244 | } |
| 245 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 246 | // FunctionPassManager_New implementation |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 247 | /// Create new Function pass manager |
| 248 | FunctionPassManager_New::FunctionPassManager_New() { |
| 249 | FPM = new FunctionPassManagerImpl_New(); |
| 250 | } |
| 251 | |
| 252 | /// add - Add a pass to the queue of passes to run. This passes |
| 253 | /// ownership of the Pass to the PassManager. When the |
| 254 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 255 | /// there is no need to delete the pass. (TODO delete passes.) |
| 256 | /// This implies that all passes MUST be allocated with 'new'. |
| 257 | void |
| 258 | FunctionPassManager_New::add(Pass *P) { |
| 259 | FPM->add(P); |
| 260 | } |
| 261 | |
| 262 | /// Execute all of the passes scheduled for execution. Keep |
| 263 | /// track of whether any of the passes modifies the function, and if |
| 264 | /// so, return true. |
| 265 | bool |
| 266 | FunctionPassManager_New::runOnModule(Module &M) { |
| 267 | return FPM->runOnModule(M); |
| 268 | } |
| 269 | |
| 270 | // FunctionPassManagerImpl_New implementation |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 271 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 272 | // FunctionPassManager |
| 273 | |
| 274 | /// Add pass P into the pass manager queue. If P is a BasicBlockPass then |
| 275 | /// either use it into active basic block pass manager or create new basic |
| 276 | /// block pass manager to handle pass P. |
| 277 | bool |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 278 | FunctionPassManagerImpl_New::addPass(Pass *P) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 279 | |
| 280 | // If P is a BasicBlockPass then use BasicBlockPassManager_New. |
| 281 | if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) { |
| 282 | |
| 283 | if (!activeBBPassManager |
| 284 | || !activeBBPassManager->addPass(BP)) { |
| 285 | |
| 286 | activeBBPassManager = new BasicBlockPassManager_New(); |
| 287 | |
| 288 | PassVector.push_back(activeBBPassManager); |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 289 | if (!activeBBPassManager->addPass(BP)) |
| 290 | assert(0 && "Unable to add Pass"); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 291 | } |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | FunctionPass *FP = dynamic_cast<FunctionPass *>(P); |
| 296 | if (!FP) |
| 297 | return false; |
| 298 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 299 | // If this pass does not preserve anlysis that is used by other passes |
| 300 | // 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] | 301 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 302 | return false; |
| 303 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame^] | 304 | // Take a note of analysis required and made available by this pass |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 305 | noteDownRequiredAnalysis(P); |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame^] | 306 | noteDownAvailableAnalysis(P); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 307 | |
| 308 | PassVector.push_back(FP); |
| 309 | activeBBPassManager = NULL; |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | /// Execute all of the passes scheduled for execution by invoking |
| 314 | /// runOnFunction method. Keep track of whether any of the passes modifies |
| 315 | /// the function, and if so, return true. |
| 316 | bool |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 317 | FunctionPassManagerImpl_New::runOnModule(Module &M) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 318 | |
| 319 | bool Changed = false; |
| 320 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 321 | for (std::vector<Pass *>::iterator itr = PassVector.begin(), |
| 322 | e = PassVector.end(); itr != e; ++itr) { |
| 323 | Pass *P = *itr; |
| 324 | FunctionPass *FP = dynamic_cast<FunctionPass*>(P); |
| 325 | Changed |= FP->runOnFunction(*I); |
| 326 | } |
| 327 | return Changed; |
| 328 | } |
| 329 | |
| 330 | |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 331 | // ModulePassManager implementation |
| 332 | |
| 333 | /// 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] | 334 | /// then use FunctionPassManagerImpl_New to manage it. Return false if P |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 335 | /// is not manageable by this manager. |
| 336 | bool |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 337 | ModulePassManager_New::addPass(Pass *P) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 338 | |
| 339 | // If P is FunctionPass then use function pass maanager. |
| 340 | if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) { |
| 341 | |
| 342 | activeFunctionPassManager = NULL; |
| 343 | |
| 344 | if (!activeFunctionPassManager |
| 345 | || !activeFunctionPassManager->addPass(P)) { |
| 346 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 347 | activeFunctionPassManager = new FunctionPassManagerImpl_New(); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 348 | |
| 349 | PassVector.push_back(activeFunctionPassManager); |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 350 | if (!activeFunctionPassManager->addPass(FP)) |
| 351 | assert(0 && "Unable to add pass"); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 352 | } |
| 353 | return true; |
| 354 | } |
| 355 | |
| 356 | ModulePass *MP = dynamic_cast<ModulePass *>(P); |
| 357 | if (!MP) |
| 358 | return false; |
| 359 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 360 | // If this pass does not preserve anlysis that is used by other passes |
| 361 | // 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] | 362 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 363 | return false; |
| 364 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame^] | 365 | // Take a note of analysis required and made available by this pass |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 366 | noteDownRequiredAnalysis(P); |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame^] | 367 | noteDownAvailableAnalysis(P); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 368 | |
| 369 | PassVector.push_back(MP); |
| 370 | activeFunctionPassManager = NULL; |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | |
| 375 | /// Execute all of the passes scheduled for execution by invoking |
| 376 | /// runOnModule method. Keep track of whether any of the passes modifies |
| 377 | /// the module, and if so, return true. |
| 378 | bool |
| 379 | ModulePassManager_New::runOnModule(Module &M) { |
| 380 | bool Changed = false; |
| 381 | for (std::vector<Pass *>::iterator itr = PassVector.begin(), |
| 382 | e = PassVector.end(); itr != e; ++itr) { |
| 383 | Pass *P = *itr; |
| 384 | ModulePass *MP = dynamic_cast<ModulePass*>(P); |
| 385 | Changed |= MP->runOnModule(M); |
| 386 | } |
| 387 | return Changed; |
| 388 | } |
| 389 | |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 390 | /// Schedule all passes from the queue by adding them in their |
| 391 | /// respective manager's queue. |
| 392 | void |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 393 | PassManagerImpl_New::schedulePasses() { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 394 | /* TODO */ |
| 395 | } |
| 396 | |
| 397 | /// Add pass P to the queue of passes to run. |
| 398 | void |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 399 | PassManagerImpl_New::add(Pass *P) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 400 | /* TODO */ |
| 401 | } |
| 402 | |
| 403 | // PassManager_New implementation |
| 404 | /// Add P into active pass manager or use new module pass manager to |
| 405 | /// manage it. |
| 406 | bool |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 407 | PassManagerImpl_New::addPass(Pass *P) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 408 | |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 409 | if (!activeManager || !activeManager->addPass(P)) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 410 | activeManager = new ModulePassManager_New(); |
| 411 | PassManagers.push_back(activeManager); |
| 412 | } |
| 413 | |
| 414 | return activeManager->addPass(P); |
| 415 | } |
| 416 | |
| 417 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 418 | /// whether any of the passes modifies the module, and if so, return true. |
| 419 | bool |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 420 | PassManagerImpl_New::run(Module &M) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 421 | |
| 422 | schedulePasses(); |
| 423 | bool Changed = false; |
| 424 | for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), |
| 425 | e = PassManagers.end(); itr != e; ++itr) { |
| 426 | ModulePassManager_New *pm = *itr; |
| 427 | Changed |= pm->runOnModule(M); |
| 428 | } |
| 429 | return Changed; |
| 430 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 431 | |
| 432 | /// Create new pass manager |
| 433 | PassManager_New::PassManager_New() { |
| 434 | PM = new PassManagerImpl_New(); |
| 435 | } |
| 436 | |
| 437 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 438 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 439 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 440 | /// implies that all passes MUST be allocated with 'new'. |
| 441 | void |
| 442 | PassManager_New::add(Pass *P) { |
| 443 | PM->add(P); |
| 444 | } |
| 445 | |
| 446 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 447 | /// whether any of the passes modifies the module, and if so, return true. |
| 448 | bool |
| 449 | PassManager_New::run(Module &M) { |
| 450 | return PM->run(M); |
| 451 | } |
| 452 | |