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