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