LLVM API Documentation
00001 //===- PassManagerT.h - Container for Passes --------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the PassManagerT class. This class is used to hold, 00011 // maintain, and optimize execution of Pass's. The PassManager class ensures 00012 // that analysis results are available before a pass runs, and that Pass's are 00013 // destroyed when the PassManager is destroyed. 00014 // 00015 // The PassManagerT template is instantiated three times to do its job. The 00016 // public PassManager class is a Pimpl around the PassManagerT<Module> interface 00017 // to avoid having all of the PassManager clients being exposed to the 00018 // implementation details herein. 00019 // 00020 //===----------------------------------------------------------------------===// 00021 00022 #ifndef LLVM_PASSMANAGER_T_H 00023 #define LLVM_PASSMANAGER_T_H 00024 00025 #include "llvm/Pass.h" 00026 #include "llvm/Support/CommandLine.h" 00027 #include "llvm/Support/LeakDetector.h" 00028 #include "llvm/Support/Timer.h" 00029 #include <algorithm> 00030 #include <iostream> 00031 00032 namespace llvm { 00033 00034 //===----------------------------------------------------------------------===// 00035 // Pass debugging information. Often it is useful to find out what pass is 00036 // running when a crash occurs in a utility. When this library is compiled with 00037 // debugging on, a command line option (--debug-pass) is enabled that causes the 00038 // pass name to be printed before it executes. 00039 // 00040 00041 // Different debug levels that can be enabled... 00042 enum PassDebugLevel { 00043 None, Arguments, Structure, Executions, Details 00044 }; 00045 00046 static cl::opt<enum PassDebugLevel> 00047 PassDebugging("debug-pass", cl::Hidden, 00048 cl::desc("Print PassManager debugging information"), 00049 cl::values( 00050 clEnumVal(None , "disable debug output"), 00051 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"), 00052 clEnumVal(Structure , "print pass structure before run()"), 00053 clEnumVal(Executions, "print pass name before it is executed"), 00054 clEnumVal(Details , "print pass details when it is executed"), 00055 clEnumValEnd)); 00056 00057 //===----------------------------------------------------------------------===// 00058 // PMDebug class - a set of debugging functions, that are not to be 00059 // instantiated by the template. 00060 // 00061 struct PMDebug { 00062 static void PerformPassStartupStuff(Pass *P) { 00063 // If debugging is enabled, print out argument information... 00064 if (PassDebugging >= Arguments) { 00065 std::cerr << "Pass Arguments: "; 00066 PrintArgumentInformation(P); 00067 std::cerr << "\n"; 00068 00069 // Print the pass execution structure 00070 if (PassDebugging >= Structure) 00071 P->dumpPassStructure(); 00072 } 00073 } 00074 00075 static void PrintArgumentInformation(const Pass *P); 00076 static void PrintPassInformation(unsigned,const char*,Pass *, Module *); 00077 static void PrintPassInformation(unsigned,const char*,Pass *, Function *); 00078 static void PrintPassInformation(unsigned,const char*,Pass *, BasicBlock *); 00079 static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P, 00080 const std::vector<AnalysisID> &); 00081 }; 00082 00083 00084 //===----------------------------------------------------------------------===// 00085 // TimingInfo Class - This class is used to calculate information about the 00086 // amount of time each pass takes to execute. This only happens when 00087 // -time-passes is enabled on the command line. 00088 // 00089 00090 class TimingInfo { 00091 std::map<Pass*, Timer> TimingData; 00092 TimerGroup TG; 00093 00094 // Private ctor, must use 'create' member 00095 TimingInfo() : TG("... Pass execution timing report ...") {} 00096 public: 00097 // TimingDtor - Print out information about timing information 00098 ~TimingInfo() { 00099 // Delete all of the timers... 00100 TimingData.clear(); 00101 // TimerGroup is deleted next, printing the report. 00102 } 00103 00104 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer 00105 // to a non null value (if the -time-passes option is enabled) or it leaves it 00106 // null. It may be called multiple times. 00107 static void createTheTimeInfo(); 00108 00109 void passStarted(Pass *P) { 00110 if (dynamic_cast<AnalysisResolver*>(P)) return; 00111 std::map<Pass*, Timer>::iterator I = TimingData.find(P); 00112 if (I == TimingData.end()) 00113 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first; 00114 I->second.startTimer(); 00115 } 00116 void passEnded(Pass *P) { 00117 if (dynamic_cast<AnalysisResolver*>(P)) return; 00118 std::map<Pass*, Timer>::iterator I = TimingData.find(P); 00119 assert (I != TimingData.end() && "passStarted/passEnded not nested right!"); 00120 I->second.stopTimer(); 00121 } 00122 }; 00123 00124 static TimingInfo *TheTimeInfo; 00125 00126 //===----------------------------------------------------------------------===// 00127 // Declare the PassManagerTraits which will be specialized... 00128 // 00129 template<class UnitType> class PassManagerTraits; // Do not define. 00130 00131 00132 //===----------------------------------------------------------------------===// 00133 // PassManagerT - Container object for passes. The PassManagerT destructor 00134 // deletes all passes contained inside of the PassManagerT, so you shouldn't 00135 // delete passes manually, and all passes should be dynamically allocated. 00136 // 00137 template<typename UnitType> 00138 class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{ 00139 typedef PassManagerTraits<UnitType> Traits; 00140 typedef typename Traits::PassClass PassClass; 00141 typedef typename Traits::SubPassClass SubPassClass; 00142 typedef typename Traits::BatcherClass BatcherClass; 00143 typedef typename Traits::ParentClass ParentClass; 00144 00145 #ifndef _MSC_VER 00146 friend class PassManagerTraits<UnitType>::PassClass; 00147 friend class PassManagerTraits<UnitType>::SubPassClass; 00148 #else 00149 friend PassClass; 00150 friend SubPassClass; 00151 #endif 00152 friend class PassManagerTraits<UnitType>; 00153 friend class ImmutablePass; 00154 00155 std::vector<PassClass*> Passes; // List of passes to run 00156 std::vector<ImmutablePass*> ImmutablePasses; // List of immutable passes 00157 00158 // The parent of this pass manager... 00159 ParentClass * const Parent; 00160 00161 // The current batcher if one is in use, or null 00162 BatcherClass *Batcher; 00163 00164 // CurrentAnalyses - As the passes are being run, this map contains the 00165 // analyses that are available to the current pass for use. This is accessed 00166 // through the getAnalysis() function in this class and in Pass. 00167 // 00168 std::map<AnalysisID, Pass*> CurrentAnalyses; 00169 00170 // LastUseOf - This map keeps track of the last usage in our pipeline of a 00171 // particular pass. When executing passes, the memory for .first is free'd 00172 // after .second is run. 00173 // 00174 std::map<Pass*, Pass*> LastUseOf; 00175 00176 public: 00177 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {} 00178 ~PassManagerT() { 00179 // Delete all of the contained passes... 00180 for (typename std::vector<PassClass*>::iterator 00181 I = Passes.begin(), E = Passes.end(); I != E; ++I) 00182 delete *I; 00183 00184 for (std::vector<ImmutablePass*>::iterator 00185 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) 00186 delete *I; 00187 } 00188 00189 // run - Run all of the queued passes on the specified module in an optimal 00190 // way. 00191 virtual bool runOnUnit(UnitType *M) { 00192 bool MadeChanges = false; 00193 closeBatcher(); 00194 CurrentAnalyses.clear(); 00195 00196 TimingInfo::createTheTimeInfo(); 00197 00198 // Add any immutable passes to the CurrentAnalyses set... 00199 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { 00200 ImmutablePass *IPass = ImmutablePasses[i]; 00201 if (const PassInfo *PI = IPass->getPassInfo()) { 00202 CurrentAnalyses[PI] = IPass; 00203 00204 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); 00205 for (unsigned i = 0, e = II.size(); i != e; ++i) 00206 CurrentAnalyses[II[i]] = IPass; 00207 } 00208 } 00209 00210 // LastUserOf - This contains the inverted LastUseOfMap... 00211 std::map<Pass *, std::vector<Pass*> > LastUserOf; 00212 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(), 00213 E = LastUseOf.end(); I != E; ++I) 00214 LastUserOf[I->second].push_back(I->first); 00215 00216 // Output debug information... 00217 if (Parent == 0) PMDebug::PerformPassStartupStuff(this); 00218 00219 // Run all of the passes 00220 for (unsigned i = 0, e = Passes.size(); i < e; ++i) { 00221 PassClass *P = Passes[i]; 00222 00223 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P, M); 00224 00225 // Get information about what analyses the pass uses... 00226 AnalysisUsage AnUsage; 00227 P->getAnalysisUsage(AnUsage); 00228 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P, 00229 AnUsage.getRequiredSet()); 00230 00231 // All Required analyses should be available to the pass as it runs! Here 00232 // we fill in the AnalysisImpls member of the pass so that it can 00233 // successfully use the getAnalysis() method to retrieve the 00234 // implementations it needs. 00235 // 00236 P->AnalysisImpls.clear(); 00237 P->AnalysisImpls.reserve(AnUsage.getRequiredSet().size()); 00238 for (std::vector<const PassInfo *>::const_iterator 00239 I = AnUsage.getRequiredSet().begin(), 00240 E = AnUsage.getRequiredSet().end(); I != E; ++I) { 00241 Pass *Impl = getAnalysisOrNullUp(*I); 00242 if (Impl == 0) { 00243 std::cerr << "Analysis '" << (*I)->getPassName() 00244 << "' used but not available!"; 00245 assert(0 && "Analysis used but not available!"); 00246 } else if (PassDebugging == Details) { 00247 if ((*I)->getPassName() != std::string(Impl->getPassName())) 00248 std::cerr << " Interface '" << (*I)->getPassName() 00249 << "' implemented by '" << Impl->getPassName() << "'\n"; 00250 } 00251 P->AnalysisImpls.push_back(std::make_pair(*I, Impl)); 00252 } 00253 00254 // Run the sub pass! 00255 if (TheTimeInfo) TheTimeInfo->passStarted(P); 00256 bool Changed = runPass(P, M); 00257 if (TheTimeInfo) TheTimeInfo->passEnded(P); 00258 MadeChanges |= Changed; 00259 00260 // Check for memory leaks by the pass... 00261 LeakDetector::checkForGarbage(std::string("after running pass '") + 00262 P->getPassName() + "'"); 00263 00264 if (Changed) 00265 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P, M); 00266 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P, 00267 AnUsage.getPreservedSet()); 00268 00269 00270 // Erase all analyses not in the preserved set... 00271 if (!AnUsage.getPreservesAll()) { 00272 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); 00273 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(), 00274 E = CurrentAnalyses.end(); I != E; ) 00275 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) != 00276 PreservedSet.end()) 00277 ++I; // This analysis is preserved, leave it in the available set... 00278 else { 00279 if (!dynamic_cast<ImmutablePass*>(I->second)) { 00280 std::map<AnalysisID, Pass*>::iterator J = I++; 00281 CurrentAnalyses.erase(J); // Analysis not preserved! 00282 } else { 00283 ++I; 00284 } 00285 } 00286 } 00287 00288 // Add the current pass to the set of passes that have been run, and are 00289 // thus available to users. 00290 // 00291 if (const PassInfo *PI = P->getPassInfo()) { 00292 CurrentAnalyses[PI] = P; 00293 00294 // This pass is the current implementation of all of the interfaces it 00295 // implements as well. 00296 // 00297 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); 00298 for (unsigned i = 0, e = II.size(); i != e; ++i) 00299 CurrentAnalyses[II[i]] = P; 00300 } 00301 00302 // Free memory for any passes that we are the last use of... 00303 std::vector<Pass*> &DeadPass = LastUserOf[P]; 00304 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end(); 00305 I != E; ++I) { 00306 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I, M); 00307 (*I)->releaseMemory(); 00308 } 00309 00310 // Make sure to remove dead passes from the CurrentAnalyses list... 00311 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(); 00312 I != CurrentAnalyses.end(); ) { 00313 std::vector<Pass*>::iterator DPI = std::find(DeadPass.begin(), 00314 DeadPass.end(), I->second); 00315 if (DPI != DeadPass.end()) { // This pass is dead now... remove it 00316 std::map<AnalysisID, Pass*>::iterator IDead = I++; 00317 CurrentAnalyses.erase(IDead); 00318 } else { 00319 ++I; // Move on to the next element... 00320 } 00321 } 00322 } 00323 00324 return MadeChanges; 00325 } 00326 00327 // dumpPassStructure - Implement the -debug-passes=PassStructure option 00328 virtual void dumpPassStructure(unsigned Offset = 0) { 00329 // Print out the immutable passes... 00330 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) 00331 ImmutablePasses[i]->dumpPassStructure(0); 00332 00333 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName() 00334 << " Pass Manager\n"; 00335 for (typename std::vector<PassClass*>::iterator 00336 I = Passes.begin(), E = Passes.end(); I != E; ++I) { 00337 PassClass *P = *I; 00338 P->dumpPassStructure(Offset+1); 00339 00340 // Loop through and see which classes are destroyed after this one... 00341 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(), 00342 E = LastUseOf.end(); I != E; ++I) { 00343 if (P == I->second) { 00344 std::cerr << "--" << std::string(Offset*2, ' '); 00345 I->first->dumpPassStructure(0); 00346 } 00347 } 00348 } 00349 } 00350 00351 Pass *getImmutablePassOrNull(const PassInfo *ID) const { 00352 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { 00353 const PassInfo *IPID = ImmutablePasses[i]->getPassInfo(); 00354 if (IPID == ID) 00355 return ImmutablePasses[i]; 00356 00357 // This pass is the current implementation of all of the interfaces it 00358 // implements as well. 00359 // 00360 const std::vector<const PassInfo*> &II = 00361 IPID->getInterfacesImplemented(); 00362 for (unsigned j = 0, e = II.size(); j != e; ++j) 00363 if (II[j] == ID) return ImmutablePasses[i]; 00364 } 00365 return 0; 00366 } 00367 00368 Pass *getAnalysisOrNullDown(const PassInfo *ID) const { 00369 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID); 00370 00371 if (I != CurrentAnalyses.end()) 00372 return I->second; // Found it. 00373 00374 if (Pass *P = getImmutablePassOrNull(ID)) 00375 return P; 00376 00377 if (Batcher) 00378 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID); 00379 return 0; 00380 } 00381 00382 Pass *getAnalysisOrNullUp(const PassInfo *ID) const { 00383 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID); 00384 if (I != CurrentAnalyses.end()) 00385 return I->second; // Found it. 00386 00387 if (Parent) // Try scanning... 00388 return Parent->getAnalysisOrNullUp(ID); 00389 else if (!ImmutablePasses.empty()) 00390 return getImmutablePassOrNull(ID); 00391 return 0; 00392 } 00393 00394 // markPassUsed - Inform higher level pass managers (and ourselves) 00395 // that these analyses are being used by this pass. This is used to 00396 // make sure that analyses are not free'd before we have to use 00397 // them... 00398 // 00399 void markPassUsed(const PassInfo *P, Pass *User) { 00400 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(P); 00401 00402 if (I != CurrentAnalyses.end()) { 00403 LastUseOf[I->second] = User; // Local pass, extend the lifetime 00404 00405 // Prolong live range of analyses that are needed after an analysis pass 00406 // is destroyed, for querying by subsequent passes 00407 AnalysisUsage AnUsage; 00408 I->second->getAnalysisUsage(AnUsage); 00409 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet(); 00410 for (std::vector<AnalysisID>::const_iterator i = IDs.begin(), 00411 e = IDs.end(); i != e; ++i) 00412 markPassUsed(*i, User); 00413 00414 } else { 00415 // Pass not in current available set, must be a higher level pass 00416 // available to us, propagate to parent pass manager... We tell the 00417 // parent that we (the passmanager) are using the analysis so that it 00418 // frees the analysis AFTER this pass manager runs. 00419 // 00420 if (Parent) { 00421 Parent->markPassUsed(P, this); 00422 } else { 00423 assert(getAnalysisOrNullUp(P) && 00424 dynamic_cast<ImmutablePass*>(getAnalysisOrNullUp(P)) && 00425 "Pass available but not found! " 00426 "Perhaps this is a module pass requiring a function pass?"); 00427 } 00428 } 00429 } 00430 00431 // Return the number of parent PassManagers that exist 00432 virtual unsigned getDepth() const { 00433 if (Parent == 0) return 0; 00434 return 1 + Parent->getDepth(); 00435 } 00436 00437 virtual unsigned getNumContainedPasses() const { return Passes.size(); } 00438 virtual const Pass *getContainedPass(unsigned N) const { 00439 assert(N < Passes.size() && "Pass number out of range!"); 00440 return Passes[N]; 00441 } 00442 00443 // add - Add a pass to the queue of passes to run. This gives ownership of 00444 // the Pass to the PassManager. When the PassManager is destroyed, the pass 00445 // will be destroyed as well, so there is no need to delete the pass. This 00446 // implies that all passes MUST be new'd. 00447 // 00448 void add(PassClass *P) { 00449 // Get information about what analyses the pass uses... 00450 AnalysisUsage AnUsage; 00451 P->getAnalysisUsage(AnUsage); 00452 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet(); 00453 00454 // Loop over all of the analyses used by this pass, 00455 for (std::vector<AnalysisID>::const_iterator I = Required.begin(), 00456 E = Required.end(); I != E; ++I) { 00457 if (getAnalysisOrNullDown(*I) == 0) { 00458 Pass *AP = (*I)->createPass(); 00459 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (AP)) { add(IP); } 00460 else if (PassClass *RP = dynamic_cast<PassClass *> (AP)) { add(RP); } 00461 else { assert (0 && "Wrong kind of pass for this PassManager"); } 00462 } 00463 } 00464 00465 // Tell the pass to add itself to this PassManager... the way it does so 00466 // depends on the class of the pass, and is critical to laying out passes in 00467 // an optimal order.. 00468 // 00469 P->addToPassManager(this, AnUsage); 00470 } 00471 00472 // add - H4x0r an ImmutablePass into a PassManager that might not be 00473 // expecting one. 00474 // 00475 void add(ImmutablePass *P) { 00476 // Get information about what analyses the pass uses... 00477 AnalysisUsage AnUsage; 00478 P->getAnalysisUsage(AnUsage); 00479 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet(); 00480 00481 // Loop over all of the analyses used by this pass, 00482 for (std::vector<AnalysisID>::const_iterator I = Required.begin(), 00483 E = Required.end(); I != E; ++I) { 00484 if (getAnalysisOrNullDown(*I) == 0) { 00485 Pass *AP = (*I)->createPass(); 00486 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (AP)) add(IP); 00487 else if (PassClass *RP = dynamic_cast<PassClass *> (AP)) add(RP); 00488 else assert (0 && "Wrong kind of pass for this PassManager"); 00489 } 00490 } 00491 00492 // Add the ImmutablePass to this PassManager. 00493 addPass(P, AnUsage); 00494 } 00495 00496 private: 00497 // addPass - These functions are used to implement the subclass specific 00498 // behaviors present in PassManager. Basically the add(Pass*) method ends up 00499 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of 00500 // Pass override it specifically so that they can reflect the type 00501 // information inherent in "this" back to the PassManager. 00502 // 00503 // For generic Pass subclasses (which are interprocedural passes), we simply 00504 // add the pass to the end of the pass list and terminate any accumulation of 00505 // FunctionPass's that are present. 00506 // 00507 void addPass(PassClass *P, AnalysisUsage &AnUsage) { 00508 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); 00509 00510 // FIXME: If this pass being added isn't killed by any of the passes in the 00511 // batcher class then we can reorder to pass to execute before the batcher 00512 // does, which will potentially allow us to batch more passes! 00513 // 00514 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet(); 00515 if (Batcher /*&& ProvidedSet.empty()*/) 00516 closeBatcher(); // This pass cannot be batched! 00517 00518 // Set the Resolver instance variable in the Pass so that it knows where to 00519 // find this object... 00520 // 00521 setAnalysisResolver(P, this); 00522 Passes.push_back(P); 00523 00524 // Inform higher level pass managers (and ourselves) that these analyses are 00525 // being used by this pass. This is used to make sure that analyses are not 00526 // free'd before we have to use them... 00527 // 00528 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(), 00529 E = RequiredSet.end(); I != E; ++I) 00530 markPassUsed(*I, P); // Mark *I as used by P 00531 00532 // Erase all analyses not in the preserved set... 00533 if (!AnUsage.getPreservesAll()) { 00534 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); 00535 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(), 00536 E = CurrentAnalyses.end(); I != E; ) { 00537 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) == 00538 PreservedSet.end()) { // Analysis not preserved! 00539 CurrentAnalyses.erase(I); // Remove from available analyses 00540 I = CurrentAnalyses.begin(); 00541 } else { 00542 ++I; 00543 } 00544 } 00545 } 00546 00547 // Add this pass to the currently available set... 00548 if (const PassInfo *PI = P->getPassInfo()) { 00549 CurrentAnalyses[PI] = P; 00550 00551 // This pass is the current implementation of all of the interfaces it 00552 // implements as well. 00553 // 00554 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); 00555 for (unsigned i = 0, e = II.size(); i != e; ++i) 00556 CurrentAnalyses[II[i]] = P; 00557 } 00558 00559 // For now assume that our results are never used... 00560 LastUseOf[P] = P; 00561 } 00562 00563 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's 00564 // together in a BatcherClass object so that all of the analyses are run 00565 // together a function at a time. 00566 // 00567 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) { 00568 if (Batcher == 0) // If we don't have a batcher yet, make one now. 00569 Batcher = new BatcherClass(this); 00570 // The Batcher will queue the passes up 00571 MP->addToPassManager(Batcher, AnUsage); 00572 } 00573 00574 // closeBatcher - Terminate the batcher that is being worked on. 00575 void closeBatcher() { 00576 if (Batcher) { 00577 Passes.push_back(Batcher); 00578 Batcher = 0; 00579 } 00580 } 00581 00582 public: 00583 // When an ImmutablePass is added, it gets added to the top level pass 00584 // manager. 00585 void addPass(ImmutablePass *IP, AnalysisUsage &AU) { 00586 if (Parent) { // Make sure this request goes to the top level passmanager... 00587 Parent->addPass(IP, AU); 00588 return; 00589 } 00590 00591 // Set the Resolver instance variable in the Pass so that it knows where to 00592 // find this object... 00593 // 00594 setAnalysisResolver(IP, this); 00595 ImmutablePasses.push_back(IP); 00596 00597 // All Required analyses should be available to the pass as it initializes! 00598 // Here we fill in the AnalysisImpls member of the pass so that it can 00599 // successfully use the getAnalysis() method to retrieve the implementations 00600 // it needs. 00601 // 00602 IP->AnalysisImpls.clear(); 00603 IP->AnalysisImpls.reserve(AU.getRequiredSet().size()); 00604 for (std::vector<const PassInfo *>::const_iterator 00605 I = AU.getRequiredSet().begin(), 00606 E = AU.getRequiredSet().end(); I != E; ++I) { 00607 Pass *Impl = getAnalysisOrNullUp(*I); 00608 if (Impl == 0) { 00609 std::cerr << "Analysis '" << (*I)->getPassName() 00610 << "' used but not available!"; 00611 assert(0 && "Analysis used but not available!"); 00612 } else if (PassDebugging == Details) { 00613 if ((*I)->getPassName() != std::string(Impl->getPassName())) 00614 std::cerr << " Interface '" << (*I)->getPassName() 00615 << "' implemented by '" << Impl->getPassName() << "'\n"; 00616 } 00617 IP->AnalysisImpls.push_back(std::make_pair(*I, Impl)); 00618 } 00619 00620 // Initialize the immutable pass... 00621 IP->initializePass(); 00622 } 00623 }; 00624 00625 00626 00627 //===----------------------------------------------------------------------===// 00628 // PassManagerTraits<BasicBlock> Specialization 00629 // 00630 // This pass manager is used to group together all of the BasicBlockPass's 00631 // into a single unit. 00632 // 00633 template<> class PassManagerTraits<BasicBlock> : public BasicBlockPass { 00634 public: 00635 // PassClass - The type of passes tracked by this PassManager 00636 typedef BasicBlockPass PassClass; 00637 00638 // SubPassClass - The types of classes that should be collated together 00639 // This is impossible to match, so BasicBlock instantiations of PassManagerT 00640 // do not collate. 00641 // 00642 typedef PassManagerT<Module> SubPassClass; 00643 00644 // BatcherClass - The type to use for collation of subtypes... This class is 00645 // never instantiated for the PassManager<BasicBlock>, but it must be an 00646 // instance of PassClass to typecheck. 00647 // 00648 typedef PassClass BatcherClass; 00649 00650 // ParentClass - The type of the parent PassManager... 00651 typedef PassManagerT<Function> ParentClass; 00652 00653 // PMType - The type of the passmanager that subclasses this class 00654 typedef PassManagerT<BasicBlock> PMType; 00655 00656 // runPass - Specify how the pass should be run on the UnitType 00657 static bool runPass(PassClass *P, BasicBlock *M) { 00658 // todo, init and finalize 00659 return P->runOnBasicBlock(*M); 00660 } 00661 00662 // getPMName() - Return the name of the unit the PassManager operates on for 00663 // debugging. 00664 const char *getPMName() const { return "BasicBlock"; } 00665 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; } 00666 00667 // Implement the BasicBlockPass interface... 00668 virtual bool doInitialization(Module &M); 00669 virtual bool doInitialization(Function &F); 00670 virtual bool runOnBasicBlock(BasicBlock &BB); 00671 virtual bool doFinalization(Function &F); 00672 virtual bool doFinalization(Module &M); 00673 00674 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00675 AU.setPreservesAll(); 00676 } 00677 }; 00678 00679 00680 00681 //===----------------------------------------------------------------------===// 00682 // PassManagerTraits<Function> Specialization 00683 // 00684 // This pass manager is used to group together all of the FunctionPass's 00685 // into a single unit. 00686 // 00687 template<> class PassManagerTraits<Function> : public FunctionPass { 00688 public: 00689 // PassClass - The type of passes tracked by this PassManager 00690 typedef FunctionPass PassClass; 00691 00692 // SubPassClass - The types of classes that should be collated together 00693 typedef BasicBlockPass SubPassClass; 00694 00695 // BatcherClass - The type to use for collation of subtypes... 00696 typedef PassManagerT<BasicBlock> BatcherClass; 00697 00698 // ParentClass - The type of the parent PassManager... 00699 typedef PassManagerT<Module> ParentClass; 00700 00701 // PMType - The type of the passmanager that subclasses this class 00702 typedef PassManagerT<Function> PMType; 00703 00704 // runPass - Specify how the pass should be run on the UnitType 00705 static bool runPass(PassClass *P, Function *F) { 00706 return P->runOnFunction(*F); 00707 } 00708 00709 // getPMName() - Return the name of the unit the PassManager operates on for 00710 // debugging. 00711 const char *getPMName() const { return "Function"; } 00712 virtual const char *getPassName() const { return "Function Pass Manager"; } 00713 00714 // Implement the FunctionPass interface... 00715 virtual bool doInitialization(Module &M); 00716 virtual bool runOnFunction(Function &F); 00717 virtual bool doFinalization(Module &M); 00718 00719 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00720 AU.setPreservesAll(); 00721 } 00722 }; 00723 00724 00725 00726 //===----------------------------------------------------------------------===// 00727 // PassManagerTraits<Module> Specialization 00728 // 00729 // This is the top level PassManager implementation that holds generic passes. 00730 // 00731 template<> class PassManagerTraits<Module> : public ModulePass { 00732 public: 00733 // PassClass - The type of passes tracked by this PassManager 00734 typedef ModulePass PassClass; 00735 00736 // SubPassClass - The types of classes that should be collated together 00737 typedef FunctionPass SubPassClass; 00738 00739 // BatcherClass - The type to use for collation of subtypes... 00740 typedef PassManagerT<Function> BatcherClass; 00741 00742 // ParentClass - The type of the parent PassManager... 00743 typedef AnalysisResolver ParentClass; 00744 00745 // runPass - Specify how the pass should be run on the UnitType 00746 static bool runPass(PassClass *P, Module *M) { return P->runOnModule(*M); } 00747 00748 // getPMName() - Return the name of the unit the PassManager operates on for 00749 // debugging. 00750 const char *getPMName() const { return "Module"; } 00751 virtual const char *getPassName() const { return "Module Pass Manager"; } 00752 00753 // runOnModule - Implement the PassManager interface. 00754 bool runOnModule(Module &M) { 00755 return ((PassManagerT<Module>*)this)->runOnUnit(&M); 00756 } 00757 }; 00758 00759 00760 00761 //===----------------------------------------------------------------------===// 00762 // PassManagerTraits Method Implementations 00763 // 00764 00765 // PassManagerTraits<BasicBlock> Implementations 00766 // 00767 inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) { 00768 bool Changed = false; 00769 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) 00770 ((PMType*)this)->Passes[i]->doInitialization(M); 00771 return Changed; 00772 } 00773 00774 inline bool PassManagerTraits<BasicBlock>::doInitialization(Function &F) { 00775 bool Changed = false; 00776 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) 00777 ((PMType*)this)->Passes[i]->doInitialization(F); 00778 return Changed; 00779 } 00780 00781 inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) { 00782 return ((PMType*)this)->runOnUnit(&BB); 00783 } 00784 00785 inline bool PassManagerTraits<BasicBlock>::doFinalization(Function &F) { 00786 bool Changed = false; 00787 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) 00788 ((PMType*)this)->Passes[i]->doFinalization(F); 00789 return Changed; 00790 } 00791 00792 inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) { 00793 bool Changed = false; 00794 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) 00795 ((PMType*)this)->Passes[i]->doFinalization(M); 00796 return Changed; 00797 } 00798 00799 00800 // PassManagerTraits<Function> Implementations 00801 // 00802 inline bool PassManagerTraits<Function>::doInitialization(Module &M) { 00803 bool Changed = false; 00804 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) 00805 ((PMType*)this)->Passes[i]->doInitialization(M); 00806 return Changed; 00807 } 00808 00809 inline bool PassManagerTraits<Function>::runOnFunction(Function &F) { 00810 return ((PMType*)this)->runOnUnit(&F); 00811 } 00812 00813 inline bool PassManagerTraits<Function>::doFinalization(Module &M) { 00814 bool Changed = false; 00815 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) 00816 ((PMType*)this)->Passes[i]->doFinalization(M); 00817 return Changed; 00818 } 00819 00820 } // End llvm namespace 00821 00822 #endif