Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 1 | //===- llvm/PassManager.h - Container for Passes -----------------*- C++ -*--=// |
| 2 | // |
| 3 | // This file defines the PassManager class. This class is used to hold, |
| 4 | // maintain, and optimize execution of Pass's. The PassManager class ensures |
| 5 | // that analysis results are available before a pass runs, and that Pass's are |
| 6 | // destroyed when the PassManager is destroyed. |
| 7 | // |
| 8 | // The PassManagerT template is instantiated three times to do its job. |
| 9 | // |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #ifndef LLVM_PASSMANAGER_H |
| 13 | #define LLVM_PASSMANAGER_H |
| 14 | |
| 15 | #include "llvm/Pass.h" |
| 16 | #include <string> |
| 17 | |
| 18 | // PassManager - Top level PassManagerT instantiation intended to be used. |
| 19 | typedef PassManagerT<Module> PassManager; |
| 20 | |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // PMDebug class - a set of debugging functions that are enabled when compiling |
| 24 | // with -g on. If compiling at -O, all functions are inlined noops. |
| 25 | // |
| 26 | struct PMDebug { |
| 27 | #ifdef NDEBUG |
| 28 | inline static void PrintPassStructure(Pass *) {} |
| 29 | inline static void PrintPassInformation(unsigned,const char*,Pass*,Value*) {} |
| 30 | inline static void PrintAnalysisSetInfo(unsigned,const char*, |
| 31 | const Pass::AnalysisSet &) {} |
| 32 | #else |
| 33 | // If compiled in debug mode, these functions can be enabled by setting |
| 34 | // -debug-pass on the command line of the tool being used. |
| 35 | // |
| 36 | static void PrintPassStructure(Pass *P); |
| 37 | static void PrintPassInformation(unsigned,const char*,Pass *, Value *); |
| 38 | static void PrintAnalysisSetInfo(unsigned,const char*,const Pass::AnalysisSet&); |
| 39 | #endif |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | // Declare the PassManagerTraits which will be specialized... |
| 46 | // |
| 47 | template<class UnitType> class PassManagerTraits; // Do not define. |
| 48 | |
| 49 | |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | // PassManagerT - Container object for passes. The PassManagerT destructor |
| 52 | // deletes all passes contained inside of the PassManagerT, so you shouldn't |
| 53 | // delete passes manually, and all passes should be dynamically allocated. |
| 54 | // |
| 55 | template<typename UnitType> |
| 56 | class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{ |
| 57 | typedef typename PassManagerTraits<UnitType>::PassClass PassClass; |
| 58 | typedef typename PassManagerTraits<UnitType>::SubPassClass SubPassClass; |
| 59 | typedef typename PassManagerTraits<UnitType>::BatcherClass BatcherClass; |
| 60 | typedef typename PassManagerTraits<UnitType>::ParentClass ParentClass; |
| 61 | typedef PassManagerTraits<UnitType> Traits; |
| 62 | |
| 63 | friend typename PassManagerTraits<UnitType>::PassClass; |
| 64 | friend typename PassManagerTraits<UnitType>::SubPassClass; |
| 65 | friend class PassManagerTraits<UnitType>; |
| 66 | |
| 67 | std::vector<PassClass*> Passes; // List of pass's to run |
| 68 | |
| 69 | // The parent of this pass manager... |
| 70 | const ParentClass *Parent; |
| 71 | |
| 72 | // The current batcher if one is in use, or null |
| 73 | BatcherClass *Batcher; |
| 74 | |
| 75 | // CurrentAnalyses - As the passes are being run, this map contains the |
| 76 | // analyses that are available to the current pass for use. This is accessed |
| 77 | // through the getAnalysis() function in this class and in Pass. |
| 78 | // |
| 79 | std::map<AnalysisID, Pass*> CurrentAnalyses; |
| 80 | |
| 81 | public: |
| 82 | PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {} |
| 83 | ~PassManagerT() { |
| 84 | // Delete all of the contained passes... |
| 85 | for (std::vector<PassClass*>::iterator I = Passes.begin(), E = Passes.end(); |
| 86 | I != E; ++I) |
| 87 | delete *I; |
| 88 | } |
| 89 | |
| 90 | // run - Run all of the queued passes on the specified module in an optimal |
| 91 | // way. |
| 92 | virtual bool runOnUnit(UnitType *M) { |
| 93 | bool MadeChanges = false; |
| 94 | closeBatcher(); |
| 95 | CurrentAnalyses.clear(); |
| 96 | |
| 97 | // Output debug information... |
| 98 | if (Parent == 0) PMDebug::PrintPassStructure(this); |
| 99 | |
| 100 | // Run all of the passes |
| 101 | for (unsigned i = 0, e = Passes.size(); i < e; ++i) { |
| 102 | PassClass *P = Passes[i]; |
| 103 | |
| 104 | PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P, (Value*)M); |
| 105 | |
| 106 | // Get information about what analyses the pass uses... |
| 107 | std::vector<AnalysisID> Required, Destroyed, Provided; |
| 108 | P->getAnalysisUsageInfo(Required, Destroyed, Provided); |
| 109 | |
| 110 | PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", Required); |
| 111 | |
| 112 | #ifndef NDEBUG |
| 113 | // All Required analyses should be available to the pass as it runs! |
| 114 | for (Pass::AnalysisSet::iterator I = Required.begin(), |
| 115 | E = Required.end(); I != E; ++I) { |
| 116 | assert(getAnalysisOrNullUp(*I) && "Analysis used but not available!"); |
| 117 | } |
| 118 | #endif |
| 119 | |
| 120 | // Run the sub pass! |
| 121 | MadeChanges |= Traits::runPass(P, M); |
| 122 | |
| 123 | PMDebug::PrintAnalysisSetInfo(getDepth(), "Destroyed", Destroyed); |
| 124 | PMDebug::PrintAnalysisSetInfo(getDepth(), "Provided", Provided); |
| 125 | |
| 126 | // Erase all analyses in the destroyed set... |
| 127 | for (Pass::AnalysisSet::iterator I = Destroyed.begin(), |
| 128 | E = Destroyed.end(); I != E; ++I) |
| 129 | CurrentAnalyses.erase(*I); |
| 130 | |
| 131 | // Add all analyses in the provided set... |
| 132 | for (Pass::AnalysisSet::iterator I = Provided.begin(), |
| 133 | E = Provided.end(); I != E; ++I) |
| 134 | CurrentAnalyses[*I] = P; |
| 135 | } |
| 136 | return MadeChanges; |
| 137 | } |
| 138 | |
| 139 | // add - Add a pass to the queue of passes to run. This passes ownership of |
| 140 | // the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 141 | // will be destroyed as well, so there is no need to delete the pass. Also, |
| 142 | // all passes MUST be new'd. |
| 143 | // |
| 144 | void add(PassClass *P) { |
| 145 | // Get information about what analyses the pass uses... |
| 146 | std::vector<AnalysisID> Required, Destroyed, Provided; |
| 147 | P->getAnalysisUsageInfo(Required, Destroyed, Provided); |
| 148 | |
| 149 | // Loop over all of the analyses used by this pass, |
| 150 | for (std::vector<AnalysisID>::iterator I = Required.begin(), |
| 151 | E = Required.end(); I != E; ++I) { |
| 152 | if (getAnalysisOrNullDown(*I) == 0) |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame^] | 153 | add((PassClass*)I->createPass()); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Tell the pass to add itself to this PassManager... the way it does so |
| 157 | // depends on the class of the pass, and is critical to laying out passes in |
| 158 | // an optimal order.. |
| 159 | // |
| 160 | P->addToPassManager(this, Destroyed, Provided); |
| 161 | } |
| 162 | |
| 163 | #ifndef NDEBUG |
| 164 | // dumpPassStructure - Implement the -debug-passes=PassStructure option |
| 165 | virtual void dumpPassStructure(unsigned Offset = 0) { |
| 166 | std::cerr << std::string(Offset*2, ' ') << "Pass Manager\n"; |
| 167 | for (std::vector<PassClass*>::iterator I = Passes.begin(), E = Passes.end(); |
| 168 | I != E; ++I) |
| 169 | (*I)->dumpPassStructure(Offset+1); |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | public: |
| 174 | Pass *getAnalysisOrNullDown(AnalysisID ID) { |
| 175 | std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.find(ID); |
| 176 | if (I == CurrentAnalyses.end()) { |
| 177 | if (Batcher) |
| 178 | return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID); |
| 179 | return 0; |
| 180 | } |
| 181 | return I->second; |
| 182 | } |
| 183 | |
| 184 | Pass *getAnalysisOrNullUp(AnalysisID ID) { |
| 185 | std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.find(ID); |
| 186 | if (I == CurrentAnalyses.end()) { |
| 187 | if (Parent) |
| 188 | return ((AnalysisResolver*)Parent)->getAnalysisOrNullUp(ID); |
| 189 | return 0; |
| 190 | } |
| 191 | return I->second; |
| 192 | } |
| 193 | |
| 194 | virtual unsigned getDepth() const { |
| 195 | if (Parent == 0) return 0; |
| 196 | return 1 + ((AnalysisResolver*)Parent)->getDepth(); |
| 197 | } |
| 198 | |
| 199 | private: |
| 200 | |
| 201 | // addPass - These functions are used to implement the subclass specific |
| 202 | // behaviors present in PassManager. Basically the add(Pass*) method ends up |
| 203 | // reflecting its behavior into a Pass::addToPassManager call. Subclasses of |
| 204 | // Pass override it specifically so that they can reflect the type |
| 205 | // information inherent in "this" back to the PassManager. |
| 206 | // |
| 207 | // For generic Pass subclasses (which are interprocedural passes), we simply |
| 208 | // add the pass to the end of the pass list and terminate any accumulation of |
| 209 | // MethodPasses that are present. |
| 210 | // |
| 211 | void addPass(PassClass *P, Pass::AnalysisSet &Destroyed, |
| 212 | Pass::AnalysisSet &Provided) { |
| 213 | // Providers are analysis classes which are forbidden to modify the module |
| 214 | // they are operating on, so they are allowed to be reordered to before the |
| 215 | // batcher... |
| 216 | // |
| 217 | if (Batcher && Provided.empty()) |
| 218 | closeBatcher(); // This pass cannot be batched! |
| 219 | |
| 220 | // Set the Resolver instance variable in the Pass so that it knows where to |
| 221 | // find this object... |
| 222 | // |
| 223 | setAnalysisResolver(P, this); |
| 224 | Passes.push_back(P); |
| 225 | |
| 226 | // Erase all analyses in the destroyed set... |
| 227 | for (std::vector<AnalysisID>::iterator I = Destroyed.begin(), |
| 228 | E = Destroyed.end(); I != E; ++I) |
| 229 | CurrentAnalyses.erase(*I); |
| 230 | |
| 231 | // Add all analyses in the provided set... |
| 232 | for (std::vector<AnalysisID>::iterator I = Provided.begin(), |
| 233 | E = Provided.end(); I != E; ++I) |
| 234 | CurrentAnalyses[*I] = P; |
| 235 | } |
| 236 | |
| 237 | // For MethodPass subclasses, we must be sure to batch the MethodPasses |
| 238 | // together in a MethodPassBatcher object so that all of the analyses are run |
| 239 | // together a method at a time. |
| 240 | // |
| 241 | void addPass(SubPassClass *MP, Pass::AnalysisSet &Destroyed, |
| 242 | Pass::AnalysisSet &Provided) { |
| 243 | if (Batcher == 0) // If we don't have a batcher yet, make one now. |
| 244 | Batcher = new BatcherClass(this); |
| 245 | // The Batcher will queue them passes up |
| 246 | MP->addToPassManager(Batcher, Destroyed, Provided); |
| 247 | } |
| 248 | |
| 249 | // closeBatcher - Terminate the batcher that is being worked on. |
| 250 | void closeBatcher() { |
| 251 | if (Batcher) { |
| 252 | Passes.push_back(Batcher); |
| 253 | Batcher = 0; |
| 254 | } |
| 255 | } |
| 256 | }; |
| 257 | |
| 258 | |
| 259 | |
| 260 | //===----------------------------------------------------------------------===// |
| 261 | // PassManagerTraits<BasicBlock> Specialization |
| 262 | // |
| 263 | // This pass manager is used to group together all of the BasicBlockPass's |
| 264 | // into a single unit. |
| 265 | // |
| 266 | template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass { |
| 267 | // PassClass - The type of passes tracked by this PassManager |
| 268 | typedef BasicBlockPass PassClass; |
| 269 | |
| 270 | // SubPassClass - The types of classes that should be collated together |
| 271 | // This is impossible to match, so BasicBlock instantiations of PassManagerT |
| 272 | // do not collate. |
| 273 | // |
| 274 | typedef PassManagerT<Module> SubPassClass; |
| 275 | |
| 276 | // BatcherClass - The type to use for collation of subtypes... This class is |
| 277 | // never instantiated for the PassManager<BasicBlock>, but it must be an |
| 278 | // instance of PassClass to typecheck. |
| 279 | // |
| 280 | typedef PassClass BatcherClass; |
| 281 | |
| 282 | // ParentClass - The type of the parent PassManager... |
| 283 | typedef PassManagerT<Method> ParentClass; |
| 284 | |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame^] | 285 | // PMType - The type of the passmanager that subclasses this class |
| 286 | typedef PassManagerT<BasicBlock> PMType; |
| 287 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 288 | // runPass - Specify how the pass should be run on the UnitType |
| 289 | static bool runPass(PassClass *P, BasicBlock *M) { |
| 290 | // todo, init and finalize |
| 291 | return P->runOnBasicBlock(M); |
| 292 | } |
| 293 | |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame^] | 294 | // Implement the BasicBlockPass interface... |
| 295 | virtual bool doInitialization(Module *M); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 296 | virtual bool runOnBasicBlock(BasicBlock *BB); |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame^] | 297 | virtual bool doFinalization(Module *M); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | |
| 301 | |
| 302 | //===----------------------------------------------------------------------===// |
| 303 | // PassManagerTraits<Method> Specialization |
| 304 | // |
| 305 | // This pass manager is used to group together all of the MethodPass's |
| 306 | // into a single unit. |
| 307 | // |
| 308 | template<> struct PassManagerTraits<Method> : public MethodPass { |
| 309 | // PassClass - The type of passes tracked by this PassManager |
| 310 | typedef MethodPass PassClass; |
| 311 | |
| 312 | // SubPassClass - The types of classes that should be collated together |
| 313 | typedef BasicBlockPass SubPassClass; |
| 314 | |
| 315 | // BatcherClass - The type to use for collation of subtypes... |
| 316 | typedef PassManagerT<BasicBlock> BatcherClass; |
| 317 | |
| 318 | // ParentClass - The type of the parent PassManager... |
| 319 | typedef PassManagerT<Module> ParentClass; |
| 320 | |
| 321 | // PMType - The type of the passmanager that subclasses this class |
| 322 | typedef PassManagerT<Method> PMType; |
| 323 | |
| 324 | // runPass - Specify how the pass should be run on the UnitType |
| 325 | static bool runPass(PassClass *P, Method *M) { |
| 326 | return P->runOnMethod(M); |
| 327 | } |
| 328 | |
| 329 | // Implement the MethodPass interface... |
| 330 | virtual bool doInitialization(Module *M); |
| 331 | virtual bool runOnMethod(Method *M); |
| 332 | virtual bool doFinalization(Module *M); |
| 333 | }; |
| 334 | |
| 335 | |
| 336 | |
| 337 | //===----------------------------------------------------------------------===// |
| 338 | // PassManagerTraits<Module> Specialization |
| 339 | // |
| 340 | // This is the top level PassManager implementation that holds generic passes. |
| 341 | // |
| 342 | template<> struct PassManagerTraits<Module> : public Pass { |
| 343 | // PassClass - The type of passes tracked by this PassManager |
| 344 | typedef Pass PassClass; |
| 345 | |
| 346 | // SubPassClass - The types of classes that should be collated together |
| 347 | typedef MethodPass SubPassClass; |
| 348 | |
| 349 | // BatcherClass - The type to use for collation of subtypes... |
| 350 | typedef PassManagerT<Method> BatcherClass; |
| 351 | |
| 352 | // ParentClass - The type of the parent PassManager... |
| 353 | typedef void ParentClass; |
| 354 | |
| 355 | // runPass - Specify how the pass should be run on the UnitType |
| 356 | static bool runPass(PassClass *P, Module *M) { return P->run(M); } |
| 357 | |
| 358 | // run - Implement the Pass interface... |
| 359 | virtual bool run(Module *M) { |
| 360 | return ((PassManagerT<Module>*)this)->runOnUnit(M); |
| 361 | } |
| 362 | }; |
| 363 | |
| 364 | |
| 365 | |
| 366 | //===----------------------------------------------------------------------===// |
| 367 | // PassManagerTraits Method Implementations |
| 368 | // |
| 369 | |
| 370 | // PassManagerTraits<BasicBlock> Implementations |
| 371 | // |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame^] | 372 | inline bool PassManagerTraits<BasicBlock>::doInitialization(Module *M) { |
| 373 | bool Changed = false; |
| 374 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 375 | ((PMType*)this)->Passes[i]->doInitialization(M); |
| 376 | return Changed; |
| 377 | } |
| 378 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 379 | inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock *BB) { |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame^] | 380 | return ((PMType*)this)->runOnUnit(BB); |
| 381 | } |
| 382 | |
| 383 | inline bool PassManagerTraits<BasicBlock>::doFinalization(Module *M) { |
| 384 | bool Changed = false; |
| 385 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 386 | ((PMType*)this)->Passes[i]->doFinalization(M); |
| 387 | return Changed; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | |
| 391 | // PassManagerTraits<Method> Implementations |
| 392 | // |
| 393 | inline bool PassManagerTraits<Method>::doInitialization(Module *M) { |
| 394 | bool Changed = false; |
| 395 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 396 | ((PMType*)this)->Passes[i]->doInitialization(M); |
| 397 | return Changed; |
| 398 | } |
| 399 | |
| 400 | inline bool PassManagerTraits<Method>::runOnMethod(Method *M) { |
| 401 | return ((PMType*)this)->runOnUnit(M); |
| 402 | } |
| 403 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 404 | inline bool PassManagerTraits<Method>::doFinalization(Module *M) { |
| 405 | bool Changed = false; |
| 406 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 407 | ((PMType*)this)->Passes[i]->doFinalization(M); |
| 408 | return Changed; |
| 409 | } |
| 410 | |
| 411 | #endif |