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 | |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame^] | 163 | /// Schedule pass P for execution. Make sure that passes required by |
| 164 | /// P are run before P is run. Update analysis info maintained by |
| 165 | /// the manager. Remove dead passes. This is a recursive function. |
| 166 | void schedulePass(Pass *P); |
| 167 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 168 | /// Schedule all passes collected in pass queue using add(). Add all the |
| 169 | /// schedule passes into various manager's queue using addPass(). |
| 170 | void schedulePasses(); |
| 171 | |
| 172 | // Collection of pass managers |
| 173 | std::vector<ModulePassManager_New *> PassManagers; |
| 174 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 175 | // Active Pass Manager |
| 176 | ModulePassManager_New *activeManager; |
| 177 | }; |
| 178 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 179 | } // End of llvm namespace |
| 180 | |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 181 | // CommonPassManagerImpl implementation |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 182 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 183 | /// 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] | 184 | /// manager. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 185 | bool CommonPassManagerImpl::manageablePass(Pass *P) { |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 186 | |
| 187 | AnalysisUsage AnUsage; |
| 188 | P->getAnalysisUsage(AnUsage); |
| 189 | |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 190 | // If this pass is not preserving information that is required by the other |
| 191 | // passes managed by this manager then use new manager |
| 192 | if (!AnUsage.getPreservesAll()) { |
| 193 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
| 194 | for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(), |
| 195 | E = RequiredAnalysis.end(); I != E; ++I) { |
| 196 | if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) == |
| 197 | PreservedSet.end()) |
| 198 | // This analysis is not preserved. Need new manager. |
| 199 | return false; |
| 200 | } |
| 201 | } |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 202 | return true; |
| 203 | } |
| 204 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 205 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 206 | bool CommonPassManagerImpl::analysisCurrentlyAvailable(AnalysisID AID) { |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 207 | |
| 208 | // TODO |
| 209 | return false; |
| 210 | } |
| 211 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 212 | /// Augment RequiredAnalysis by adding analysis required by pass P. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 213 | void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) { |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 214 | AnalysisUsage AnUsage; |
| 215 | P->getAnalysisUsage(AnUsage); |
| 216 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 217 | |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 218 | // FIXME: What about duplicates ? |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 219 | RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(), |
| 220 | RequiredSet.end()); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 223 | /// Augement AvailableAnalysis by adding analysis made available by pass P. |
| 224 | void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) { |
| 225 | |
| 226 | if (const PassInfo *PI = P->getPassInfo()) { |
| 227 | AvailableAnalysis.insert(PI); |
| 228 | |
| 229 | //TODO This pass is the current implementation of all of the interfaces it |
| 230 | //TODO implements as well. |
| 231 | //TODO |
| 232 | //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 233 | //TODO for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 234 | //TODO CurrentAnalyses[II[i]] = P; |
| 235 | } |
| 236 | } |
| 237 | |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 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 ) { |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 246 | if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) == |
| 247 | PreservedSet.end()) { |
| 248 | // Remove this analysis |
| 249 | std::set<AnalysisID>::iterator J = I++; |
| 250 | AvailableAnalysis.erase(J); |
| 251 | } |
| 252 | } |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 255 | /// Add pass P into the PassVector. Update RequiredAnalysis and |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 256 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
| 257 | void CommonPassManagerImpl::addPassToManager (Pass *P, |
| 258 | bool ProcessAnalysis) { |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 259 | |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 260 | if (ProcessAnalysis) { |
| 261 | // Take a note of analysis required and made available by this pass |
| 262 | noteDownRequiredAnalysis(P); |
| 263 | noteDownAvailableAnalysis(P); |
| 264 | |
| 265 | // Remove the analysis not preserved by this pass |
| 266 | removeNotPreservedAnalysis(P); |
| 267 | } |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 268 | |
| 269 | // Add pass |
| 270 | PassVector.push_back(P); |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 273 | /// BasicBlockPassManager implementation |
| 274 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 275 | /// Add pass P into PassVector and return true. If this pass is not |
| 276 | /// manageable by this manager then return false. |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 277 | bool |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 278 | BasicBlockPassManager_New::addPass(Pass *P) { |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 279 | |
| 280 | BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); |
| 281 | if (!BP) |
| 282 | return false; |
| 283 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 284 | // If this pass does not preserve anlysis that is used by other passes |
| 285 | // 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] | 286 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 287 | return false; |
| 288 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 289 | addPassToManager (BP); |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 290 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 291 | return true; |
| 292 | } |
| 293 | |
| 294 | /// Execute all of the passes scheduled for execution by invoking |
| 295 | /// runOnBasicBlock method. Keep track of whether any of the passes modifies |
| 296 | /// the function, and if so, return true. |
| 297 | bool |
| 298 | BasicBlockPassManager_New::runOnFunction(Function &F) { |
| 299 | |
| 300 | bool Changed = false; |
| 301 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 302 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 303 | e = passVectorEnd(); itr != e; ++itr) { |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 304 | Pass *P = *itr; |
| 305 | BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); |
| 306 | Changed |= BP->runOnBasicBlock(*I); |
| 307 | } |
| 308 | return Changed; |
| 309 | } |
| 310 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 311 | // FunctionPassManager_New implementation |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 312 | /// Create new Function pass manager |
| 313 | FunctionPassManager_New::FunctionPassManager_New() { |
| 314 | FPM = new FunctionPassManagerImpl_New(); |
| 315 | } |
| 316 | |
| 317 | /// add - Add a pass to the queue of passes to run. This passes |
| 318 | /// ownership of the Pass to the PassManager. When the |
| 319 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 320 | /// there is no need to delete the pass. (TODO delete passes.) |
| 321 | /// This implies that all passes MUST be allocated with 'new'. |
| 322 | void |
| 323 | FunctionPassManager_New::add(Pass *P) { |
| 324 | FPM->add(P); |
| 325 | } |
| 326 | |
| 327 | /// Execute all of the passes scheduled for execution. Keep |
| 328 | /// track of whether any of the passes modifies the function, and if |
| 329 | /// so, return true. |
| 330 | bool |
| 331 | FunctionPassManager_New::runOnModule(Module &M) { |
| 332 | return FPM->runOnModule(M); |
| 333 | } |
| 334 | |
| 335 | // FunctionPassManagerImpl_New implementation |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 336 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 337 | // FunctionPassManager |
| 338 | |
| 339 | /// Add pass P into the pass manager queue. If P is a BasicBlockPass then |
| 340 | /// either use it into active basic block pass manager or create new basic |
| 341 | /// block pass manager to handle pass P. |
| 342 | bool |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 343 | FunctionPassManagerImpl_New::addPass(Pass *P) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 344 | |
| 345 | // If P is a BasicBlockPass then use BasicBlockPassManager_New. |
| 346 | if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) { |
| 347 | |
| 348 | if (!activeBBPassManager |
| 349 | || !activeBBPassManager->addPass(BP)) { |
| 350 | |
| 351 | activeBBPassManager = new BasicBlockPassManager_New(); |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 352 | addPassToManager(activeBBPassManager, false); |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 353 | if (!activeBBPassManager->addPass(BP)) |
| 354 | assert(0 && "Unable to add Pass"); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 355 | } |
| 356 | return true; |
| 357 | } |
| 358 | |
| 359 | FunctionPass *FP = dynamic_cast<FunctionPass *>(P); |
| 360 | if (!FP) |
| 361 | return false; |
| 362 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 363 | // If this pass does not preserve anlysis that is used by other passes |
| 364 | // 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] | 365 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 366 | return false; |
| 367 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 368 | addPassToManager (FP); |
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) |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 381 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 382 | e = passVectorEnd(); itr != e; ++itr) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 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 | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 408 | addPassToManager(activeFunctionPassManager, false); |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 409 | if (!activeFunctionPassManager->addPass(FP)) |
| 410 | assert(0 && "Unable to add pass"); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 411 | } |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | ModulePass *MP = dynamic_cast<ModulePass *>(P); |
| 416 | if (!MP) |
| 417 | return false; |
| 418 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 419 | // If this pass does not preserve anlysis that is used by other passes |
| 420 | // 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] | 421 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 422 | return false; |
| 423 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 424 | addPassToManager(MP); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 425 | activeFunctionPassManager = NULL; |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | /// Execute all of the passes scheduled for execution by invoking |
| 431 | /// runOnModule method. Keep track of whether any of the passes modifies |
| 432 | /// the module, and if so, return true. |
| 433 | bool |
| 434 | ModulePassManager_New::runOnModule(Module &M) { |
| 435 | bool Changed = false; |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 436 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 437 | e = passVectorEnd(); itr != e; ++itr) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 438 | Pass *P = *itr; |
| 439 | ModulePass *MP = dynamic_cast<ModulePass*>(P); |
| 440 | Changed |= MP->runOnModule(M); |
| 441 | } |
| 442 | return Changed; |
| 443 | } |
| 444 | |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame^] | 445 | /// Schedule pass P for execution. Make sure that passes required by |
| 446 | /// P are run before P is run. Update analysis info maintained by |
| 447 | /// the manager. Remove dead passes. This is a recursive function. |
| 448 | void PassManagerImpl_New::schedulePass(Pass *P) { |
| 449 | |
| 450 | AnalysisUsage AnUsage; |
| 451 | P->getAnalysisUsage(AnUsage); |
| 452 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
| 453 | for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(), |
| 454 | E = RequiredSet.end(); I != E; ++I) { |
| 455 | |
| 456 | // TODO Check if Analysis is currently available or not. |
| 457 | bool available = false; |
| 458 | if (!available) { |
| 459 | // Schedule this analysis run first. |
| 460 | Pass *AP = (*I)->createPass(); |
| 461 | schedulePass(AP); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | addPass(P); |
| 466 | |
| 467 | // TODO : Walk through all managers and remove not preserved analysis |
| 468 | // TODO : remove dead passes |
| 469 | } |
| 470 | |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 471 | /// Schedule all passes from the queue by adding them in their |
| 472 | /// respective manager's queue. |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame^] | 473 | void PassManagerImpl_New::schedulePasses() { |
| 474 | for (std::vector<Pass *>::iterator I = passVectorBegin(), |
| 475 | E = passVectorEnd(); I != E; ++I) |
| 476 | schedulePass (*I); |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /// Add pass P to the queue of passes to run. |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame^] | 480 | void PassManagerImpl_New::add(Pass *P) { |
| 481 | // Do not process Analysis now. Analysis is process while scheduling |
| 482 | // the pass vector. |
Devang Patel | db789fb | 2006-11-11 02:06:21 +0000 | [diff] [blame] | 483 | addPassToManager(P, false); |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | // PassManager_New implementation |
| 487 | /// Add P into active pass manager or use new module pass manager to |
| 488 | /// manage it. |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame^] | 489 | bool PassManagerImpl_New::addPass(Pass *P) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 490 | |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 491 | if (!activeManager || !activeManager->addPass(P)) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 492 | activeManager = new ModulePassManager_New(); |
| 493 | PassManagers.push_back(activeManager); |
| 494 | } |
| 495 | |
| 496 | return activeManager->addPass(P); |
| 497 | } |
| 498 | |
| 499 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 500 | /// whether any of the passes modifies the module, and if so, return true. |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame^] | 501 | bool PassManagerImpl_New::run(Module &M) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 502 | |
| 503 | schedulePasses(); |
| 504 | bool Changed = false; |
| 505 | for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), |
| 506 | e = PassManagers.end(); itr != e; ++itr) { |
| 507 | ModulePassManager_New *pm = *itr; |
| 508 | Changed |= pm->runOnModule(M); |
| 509 | } |
| 510 | return Changed; |
| 511 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 512 | |
| 513 | /// Create new pass manager |
| 514 | PassManager_New::PassManager_New() { |
| 515 | PM = new PassManagerImpl_New(); |
| 516 | } |
| 517 | |
| 518 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 519 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 520 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 521 | /// implies that all passes MUST be allocated with 'new'. |
| 522 | void |
| 523 | PassManager_New::add(Pass *P) { |
| 524 | PM->add(P); |
| 525 | } |
| 526 | |
| 527 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 528 | /// whether any of the passes modifies the module, and if so, return true. |
| 529 | bool |
| 530 | PassManager_New::run(Module &M) { |
| 531 | return PM->run(M); |
| 532 | } |
| 533 | |