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