LLVM API Documentation
00001 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the LLVM Pass Manager infrastructure. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 00015 #include "llvm/PassManagers.h" 00016 #include "llvm/Assembly/PrintModulePass.h" 00017 #include "llvm/Assembly/Writer.h" 00018 #include "llvm/IR/Module.h" 00019 #include "llvm/PassManager.h" 00020 #include "llvm/Support/CommandLine.h" 00021 #include "llvm/Support/Debug.h" 00022 #include "llvm/Support/ErrorHandling.h" 00023 #include "llvm/Support/ManagedStatic.h" 00024 #include "llvm/Support/Mutex.h" 00025 #include "llvm/Support/PassNameParser.h" 00026 #include "llvm/Support/Timer.h" 00027 #include "llvm/Support/raw_ostream.h" 00028 #include <algorithm> 00029 #include <map> 00030 using namespace llvm; 00031 00032 // See PassManagers.h for Pass Manager infrastructure overview. 00033 00034 namespace llvm { 00035 00036 //===----------------------------------------------------------------------===// 00037 // Pass debugging information. Often it is useful to find out what pass is 00038 // running when a crash occurs in a utility. When this library is compiled with 00039 // debugging on, a command line option (--debug-pass) is enabled that causes the 00040 // pass name to be printed before it executes. 00041 // 00042 00043 // Different debug levels that can be enabled... 00044 enum PassDebugLevel { 00045 Disabled, Arguments, Structure, Executions, Details 00046 }; 00047 00048 static cl::opt<enum PassDebugLevel> 00049 PassDebugging("debug-pass", cl::Hidden, 00050 cl::desc("Print PassManager debugging information"), 00051 cl::values( 00052 clEnumVal(Disabled , "disable debug output"), 00053 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"), 00054 clEnumVal(Structure , "print pass structure before run()"), 00055 clEnumVal(Executions, "print pass name before it is executed"), 00056 clEnumVal(Details , "print pass details when it is executed"), 00057 clEnumValEnd)); 00058 00059 typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser> 00060 PassOptionList; 00061 00062 // Print IR out before/after specified passes. 00063 static PassOptionList 00064 PrintBefore("print-before", 00065 llvm::cl::desc("Print IR before specified passes"), 00066 cl::Hidden); 00067 00068 static PassOptionList 00069 PrintAfter("print-after", 00070 llvm::cl::desc("Print IR after specified passes"), 00071 cl::Hidden); 00072 00073 static cl::opt<bool> 00074 PrintBeforeAll("print-before-all", 00075 llvm::cl::desc("Print IR before each pass"), 00076 cl::init(false)); 00077 static cl::opt<bool> 00078 PrintAfterAll("print-after-all", 00079 llvm::cl::desc("Print IR after each pass"), 00080 cl::init(false)); 00081 00082 /// This is a helper to determine whether to print IR before or 00083 /// after a pass. 00084 00085 static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI, 00086 PassOptionList &PassesToPrint) { 00087 for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) { 00088 const llvm::PassInfo *PassInf = PassesToPrint[i]; 00089 if (PassInf) 00090 if (PassInf->getPassArgument() == PI->getPassArgument()) { 00091 return true; 00092 } 00093 } 00094 return false; 00095 } 00096 00097 /// This is a utility to check whether a pass should have IR dumped 00098 /// before it. 00099 static bool ShouldPrintBeforePass(const PassInfo *PI) { 00100 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore); 00101 } 00102 00103 /// This is a utility to check whether a pass should have IR dumped 00104 /// after it. 00105 static bool ShouldPrintAfterPass(const PassInfo *PI) { 00106 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter); 00107 } 00108 00109 } // End of llvm namespace 00110 00111 /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions 00112 /// or higher is specified. 00113 bool PMDataManager::isPassDebuggingExecutionsOrMore() const { 00114 return PassDebugging >= Executions; 00115 } 00116 00117 00118 00119 00120 void PassManagerPrettyStackEntry::print(raw_ostream &OS) const { 00121 if (V == 0 && M == 0) 00122 OS << "Releasing pass '"; 00123 else 00124 OS << "Running pass '"; 00125 00126 OS << P->getPassName() << "'"; 00127 00128 if (M) { 00129 OS << " on module '" << M->getModuleIdentifier() << "'.\n"; 00130 return; 00131 } 00132 if (V == 0) { 00133 OS << '\n'; 00134 return; 00135 } 00136 00137 OS << " on "; 00138 if (isa<Function>(V)) 00139 OS << "function"; 00140 else if (isa<BasicBlock>(V)) 00141 OS << "basic block"; 00142 else 00143 OS << "value"; 00144 00145 OS << " '"; 00146 WriteAsOperand(OS, V, /*PrintTy=*/false, M); 00147 OS << "'\n"; 00148 } 00149 00150 00151 namespace { 00152 00153 //===----------------------------------------------------------------------===// 00154 // BBPassManager 00155 // 00156 /// BBPassManager manages BasicBlockPass. It batches all the 00157 /// pass together and sequence them to process one basic block before 00158 /// processing next basic block. 00159 class BBPassManager : public PMDataManager, public FunctionPass { 00160 00161 public: 00162 static char ID; 00163 explicit BBPassManager() 00164 : PMDataManager(), FunctionPass(ID) {} 00165 00166 /// Execute all of the passes scheduled for execution. Keep track of 00167 /// whether any of the passes modifies the function, and if so, return true. 00168 bool runOnFunction(Function &F); 00169 00170 /// Pass Manager itself does not invalidate any analysis info. 00171 void getAnalysisUsage(AnalysisUsage &Info) const { 00172 Info.setPreservesAll(); 00173 } 00174 00175 bool doInitialization(Module &M); 00176 bool doInitialization(Function &F); 00177 bool doFinalization(Module &M); 00178 bool doFinalization(Function &F); 00179 00180 virtual PMDataManager *getAsPMDataManager() { return this; } 00181 virtual Pass *getAsPass() { return this; } 00182 00183 virtual const char *getPassName() const { 00184 return "BasicBlock Pass Manager"; 00185 } 00186 00187 // Print passes managed by this manager 00188 void dumpPassStructure(unsigned Offset) { 00189 llvm::dbgs().indent(Offset*2) << "BasicBlockPass Manager\n"; 00190 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 00191 BasicBlockPass *BP = getContainedPass(Index); 00192 BP->dumpPassStructure(Offset + 1); 00193 dumpLastUses(BP, Offset+1); 00194 } 00195 } 00196 00197 BasicBlockPass *getContainedPass(unsigned N) { 00198 assert(N < PassVector.size() && "Pass number out of range!"); 00199 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]); 00200 return BP; 00201 } 00202 00203 virtual PassManagerType getPassManagerType() const { 00204 return PMT_BasicBlockPassManager; 00205 } 00206 }; 00207 00208 char BBPassManager::ID = 0; 00209 } 00210 00211 namespace llvm { 00212 00213 //===----------------------------------------------------------------------===// 00214 // FunctionPassManagerImpl 00215 // 00216 /// FunctionPassManagerImpl manages FPPassManagers 00217 class FunctionPassManagerImpl : public Pass, 00218 public PMDataManager, 00219 public PMTopLevelManager { 00220 virtual void anchor(); 00221 private: 00222 bool wasRun; 00223 public: 00224 static char ID; 00225 explicit FunctionPassManagerImpl() : 00226 Pass(PT_PassManager, ID), PMDataManager(), 00227 PMTopLevelManager(new FPPassManager()), wasRun(false) {} 00228 00229 /// add - Add a pass to the queue of passes to run. This passes ownership of 00230 /// the Pass to the PassManager. When the PassManager is destroyed, the pass 00231 /// will be destroyed as well, so there is no need to delete the pass. This 00232 /// implies that all passes MUST be allocated with 'new'. 00233 void add(Pass *P) { 00234 schedulePass(P); 00235 } 00236 00237 /// createPrinterPass - Get a function printer pass. 00238 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const { 00239 return createPrintFunctionPass(Banner, &O); 00240 } 00241 00242 // Prepare for running an on the fly pass, freeing memory if needed 00243 // from a previous run. 00244 void releaseMemoryOnTheFly(); 00245 00246 /// run - Execute all of the passes scheduled for execution. Keep track of 00247 /// whether any of the passes modifies the module, and if so, return true. 00248 bool run(Function &F); 00249 00250 /// doInitialization - Run all of the initializers for the function passes. 00251 /// 00252 bool doInitialization(Module &M); 00253 00254 /// doFinalization - Run all of the finalizers for the function passes. 00255 /// 00256 bool doFinalization(Module &M); 00257 00258 00259 virtual PMDataManager *getAsPMDataManager() { return this; } 00260 virtual Pass *getAsPass() { return this; } 00261 virtual PassManagerType getTopLevelPassManagerType() { 00262 return PMT_FunctionPassManager; 00263 } 00264 00265 /// Pass Manager itself does not invalidate any analysis info. 00266 void getAnalysisUsage(AnalysisUsage &Info) const { 00267 Info.setPreservesAll(); 00268 } 00269 00270 FPPassManager *getContainedManager(unsigned N) { 00271 assert(N < PassManagers.size() && "Pass number out of range!"); 00272 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]); 00273 return FP; 00274 } 00275 }; 00276 00277 void FunctionPassManagerImpl::anchor() {} 00278 00279 char FunctionPassManagerImpl::ID = 0; 00280 00281 //===----------------------------------------------------------------------===// 00282 // MPPassManager 00283 // 00284 /// MPPassManager manages ModulePasses and function pass managers. 00285 /// It batches all Module passes and function pass managers together and 00286 /// sequences them to process one module. 00287 class MPPassManager : public Pass, public PMDataManager { 00288 public: 00289 static char ID; 00290 explicit MPPassManager() : 00291 Pass(PT_PassManager, ID), PMDataManager() { } 00292 00293 // Delete on the fly managers. 00294 virtual ~MPPassManager() { 00295 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator 00296 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); 00297 I != E; ++I) { 00298 FunctionPassManagerImpl *FPP = I->second; 00299 delete FPP; 00300 } 00301 } 00302 00303 /// createPrinterPass - Get a module printer pass. 00304 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const { 00305 return createPrintModulePass(&O, false, Banner); 00306 } 00307 00308 /// run - Execute all of the passes scheduled for execution. Keep track of 00309 /// whether any of the passes modifies the module, and if so, return true. 00310 bool runOnModule(Module &M); 00311 00312 using llvm::Pass::doInitialization; 00313 using llvm::Pass::doFinalization; 00314 00315 /// doInitialization - Run all of the initializers for the module passes. 00316 /// 00317 bool doInitialization(); 00318 00319 /// doFinalization - Run all of the finalizers for the module passes. 00320 /// 00321 bool doFinalization(); 00322 00323 /// Pass Manager itself does not invalidate any analysis info. 00324 void getAnalysisUsage(AnalysisUsage &Info) const { 00325 Info.setPreservesAll(); 00326 } 00327 00328 /// Add RequiredPass into list of lower level passes required by pass P. 00329 /// RequiredPass is run on the fly by Pass Manager when P requests it 00330 /// through getAnalysis interface. 00331 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass); 00332 00333 /// Return function pass corresponding to PassInfo PI, that is 00334 /// required by module pass MP. Instantiate analysis pass, by using 00335 /// its runOnFunction() for function F. 00336 virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F); 00337 00338 virtual const char *getPassName() const { 00339 return "Module Pass Manager"; 00340 } 00341 00342 virtual PMDataManager *getAsPMDataManager() { return this; } 00343 virtual Pass *getAsPass() { return this; } 00344 00345 // Print passes managed by this manager 00346 void dumpPassStructure(unsigned Offset) { 00347 llvm::dbgs().indent(Offset*2) << "ModulePass Manager\n"; 00348 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 00349 ModulePass *MP = getContainedPass(Index); 00350 MP->dumpPassStructure(Offset + 1); 00351 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I = 00352 OnTheFlyManagers.find(MP); 00353 if (I != OnTheFlyManagers.end()) 00354 I->second->dumpPassStructure(Offset + 2); 00355 dumpLastUses(MP, Offset+1); 00356 } 00357 } 00358 00359 ModulePass *getContainedPass(unsigned N) { 00360 assert(N < PassVector.size() && "Pass number out of range!"); 00361 return static_cast<ModulePass *>(PassVector[N]); 00362 } 00363 00364 virtual PassManagerType getPassManagerType() const { 00365 return PMT_ModulePassManager; 00366 } 00367 00368 private: 00369 /// Collection of on the fly FPPassManagers. These managers manage 00370 /// function passes that are required by module passes. 00371 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers; 00372 }; 00373 00374 char MPPassManager::ID = 0; 00375 //===----------------------------------------------------------------------===// 00376 // PassManagerImpl 00377 // 00378 00379 /// PassManagerImpl manages MPPassManagers 00380 class PassManagerImpl : public Pass, 00381 public PMDataManager, 00382 public PMTopLevelManager { 00383 virtual void anchor(); 00384 00385 public: 00386 static char ID; 00387 explicit PassManagerImpl() : 00388 Pass(PT_PassManager, ID), PMDataManager(), 00389 PMTopLevelManager(new MPPassManager()) {} 00390 00391 /// add - Add a pass to the queue of passes to run. This passes ownership of 00392 /// the Pass to the PassManager. When the PassManager is destroyed, the pass 00393 /// will be destroyed as well, so there is no need to delete the pass. This 00394 /// implies that all passes MUST be allocated with 'new'. 00395 void add(Pass *P) { 00396 schedulePass(P); 00397 } 00398 00399 /// createPrinterPass - Get a module printer pass. 00400 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const { 00401 return createPrintModulePass(&O, false, Banner); 00402 } 00403 00404 /// run - Execute all of the passes scheduled for execution. Keep track of 00405 /// whether any of the passes modifies the module, and if so, return true. 00406 bool run(Module &M); 00407 00408 using llvm::Pass::doInitialization; 00409 using llvm::Pass::doFinalization; 00410 00411 /// doInitialization - Run all of the initializers for the module passes. 00412 /// 00413 bool doInitialization(); 00414 00415 /// doFinalization - Run all of the finalizers for the module passes. 00416 /// 00417 bool doFinalization(); 00418 00419 /// Pass Manager itself does not invalidate any analysis info. 00420 void getAnalysisUsage(AnalysisUsage &Info) const { 00421 Info.setPreservesAll(); 00422 } 00423 00424 virtual PMDataManager *getAsPMDataManager() { return this; } 00425 virtual Pass *getAsPass() { return this; } 00426 virtual PassManagerType getTopLevelPassManagerType() { 00427 return PMT_ModulePassManager; 00428 } 00429 00430 MPPassManager *getContainedManager(unsigned N) { 00431 assert(N < PassManagers.size() && "Pass number out of range!"); 00432 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]); 00433 return MP; 00434 } 00435 }; 00436 00437 void PassManagerImpl::anchor() {} 00438 00439 char PassManagerImpl::ID = 0; 00440 } // End of llvm namespace 00441 00442 namespace { 00443 00444 //===----------------------------------------------------------------------===// 00445 /// TimingInfo Class - This class is used to calculate information about the 00446 /// amount of time each pass takes to execute. This only happens when 00447 /// -time-passes is enabled on the command line. 00448 /// 00449 00450 static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex; 00451 00452 class TimingInfo { 00453 DenseMap<Pass*, Timer*> TimingData; 00454 TimerGroup TG; 00455 public: 00456 // Use 'create' member to get this. 00457 TimingInfo() : TG("... Pass execution timing report ...") {} 00458 00459 // TimingDtor - Print out information about timing information 00460 ~TimingInfo() { 00461 // Delete all of the timers, which accumulate their info into the 00462 // TimerGroup. 00463 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(), 00464 E = TimingData.end(); I != E; ++I) 00465 delete I->second; 00466 // TimerGroup is deleted next, printing the report. 00467 } 00468 00469 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer 00470 // to a non null value (if the -time-passes option is enabled) or it leaves it 00471 // null. It may be called multiple times. 00472 static void createTheTimeInfo(); 00473 00474 /// getPassTimer - Return the timer for the specified pass if it exists. 00475 Timer *getPassTimer(Pass *P) { 00476 if (P->getAsPMDataManager()) 00477 return 0; 00478 00479 sys::SmartScopedLock<true> Lock(*TimingInfoMutex); 00480 Timer *&T = TimingData[P]; 00481 if (T == 0) 00482 T = new Timer(P->getPassName(), TG); 00483 return T; 00484 } 00485 }; 00486 00487 } // End of anon namespace 00488 00489 static TimingInfo *TheTimeInfo; 00490 00491 //===----------------------------------------------------------------------===// 00492 // PMTopLevelManager implementation 00493 00494 /// Initialize top level manager. Create first pass manager. 00495 PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) { 00496 PMDM->setTopLevelManager(this); 00497 addPassManager(PMDM); 00498 activeStack.push(PMDM); 00499 } 00500 00501 /// Set pass P as the last user of the given analysis passes. 00502 void 00503 PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) { 00504 unsigned PDepth = 0; 00505 if (P->getResolver()) 00506 PDepth = P->getResolver()->getPMDataManager().getDepth(); 00507 00508 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(), 00509 E = AnalysisPasses.end(); I != E; ++I) { 00510 Pass *AP = *I; 00511 LastUser[AP] = P; 00512 00513 if (P == AP) 00514 continue; 00515 00516 // Update the last users of passes that are required transitive by AP. 00517 AnalysisUsage *AnUsage = findAnalysisUsage(AP); 00518 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet(); 00519 SmallVector<Pass *, 12> LastUses; 00520 SmallVector<Pass *, 12> LastPMUses; 00521 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(), 00522 E = IDs.end(); I != E; ++I) { 00523 Pass *AnalysisPass = findAnalysisPass(*I); 00524 assert(AnalysisPass && "Expected analysis pass to exist."); 00525 AnalysisResolver *AR = AnalysisPass->getResolver(); 00526 assert(AR && "Expected analysis resolver to exist."); 00527 unsigned APDepth = AR->getPMDataManager().getDepth(); 00528 00529 if (PDepth == APDepth) 00530 LastUses.push_back(AnalysisPass); 00531 else if (PDepth > APDepth) 00532 LastPMUses.push_back(AnalysisPass); 00533 } 00534 00535 setLastUser(LastUses, P); 00536 00537 // If this pass has a corresponding pass manager, push higher level 00538 // analysis to this pass manager. 00539 if (P->getResolver()) 00540 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass()); 00541 00542 00543 // If AP is the last user of other passes then make P last user of 00544 // such passes. 00545 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(), 00546 LUE = LastUser.end(); LUI != LUE; ++LUI) { 00547 if (LUI->second == AP) 00548 // DenseMap iterator is not invalidated here because 00549 // this is just updating existing entries. 00550 LastUser[LUI->first] = P; 00551 } 00552 } 00553 } 00554 00555 /// Collect passes whose last user is P 00556 void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses, 00557 Pass *P) { 00558 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI = 00559 InversedLastUser.find(P); 00560 if (DMI == InversedLastUser.end()) 00561 return; 00562 00563 SmallPtrSet<Pass *, 8> &LU = DMI->second; 00564 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(), 00565 E = LU.end(); I != E; ++I) { 00566 LastUses.push_back(*I); 00567 } 00568 00569 } 00570 00571 AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) { 00572 AnalysisUsage *AnUsage = NULL; 00573 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P); 00574 if (DMI != AnUsageMap.end()) 00575 AnUsage = DMI->second; 00576 else { 00577 AnUsage = new AnalysisUsage(); 00578 P->getAnalysisUsage(*AnUsage); 00579 AnUsageMap[P] = AnUsage; 00580 } 00581 return AnUsage; 00582 } 00583 00584 /// Schedule pass P for execution. Make sure that passes required by 00585 /// P are run before P is run. Update analysis info maintained by 00586 /// the manager. Remove dead passes. This is a recursive function. 00587 void PMTopLevelManager::schedulePass(Pass *P) { 00588 00589 // TODO : Allocate function manager for this pass, other wise required set 00590 // may be inserted into previous function manager 00591 00592 // Give pass a chance to prepare the stage. 00593 P->preparePassManager(activeStack); 00594 00595 // If P is an analysis pass and it is available then do not 00596 // generate the analysis again. Stale analysis info should not be 00597 // available at this point. 00598 const PassInfo *PI = 00599 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID()); 00600 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) { 00601 delete P; 00602 return; 00603 } 00604 00605 AnalysisUsage *AnUsage = findAnalysisUsage(P); 00606 00607 bool checkAnalysis = true; 00608 while (checkAnalysis) { 00609 checkAnalysis = false; 00610 00611 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet(); 00612 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(), 00613 E = RequiredSet.end(); I != E; ++I) { 00614 00615 Pass *AnalysisPass = findAnalysisPass(*I); 00616 if (!AnalysisPass) { 00617 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I); 00618 00619 if (PI == NULL) { 00620 // Pass P is not in the global PassRegistry 00621 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n"; 00622 dbgs() << "Verify if there is a pass dependency cycle." << "\n"; 00623 dbgs() << "Required Passes:" << "\n"; 00624 for (AnalysisUsage::VectorType::const_iterator I2 = RequiredSet.begin(), 00625 E = RequiredSet.end(); I2 != E && I2 != I; ++I2) { 00626 Pass *AnalysisPass2 = findAnalysisPass(*I2); 00627 if (AnalysisPass2) { 00628 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n"; 00629 } else { 00630 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n"; 00631 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n"; 00632 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n"; 00633 } 00634 } 00635 } 00636 00637 assert(PI && "Expected required passes to be initialized"); 00638 AnalysisPass = PI->createPass(); 00639 if (P->getPotentialPassManagerType () == 00640 AnalysisPass->getPotentialPassManagerType()) 00641 // Schedule analysis pass that is managed by the same pass manager. 00642 schedulePass(AnalysisPass); 00643 else if (P->getPotentialPassManagerType () > 00644 AnalysisPass->getPotentialPassManagerType()) { 00645 // Schedule analysis pass that is managed by a new manager. 00646 schedulePass(AnalysisPass); 00647 // Recheck analysis passes to ensure that required analyses that 00648 // are already checked are still available. 00649 checkAnalysis = true; 00650 } else 00651 // Do not schedule this analysis. Lower level analsyis 00652 // passes are run on the fly. 00653 delete AnalysisPass; 00654 } 00655 } 00656 } 00657 00658 // Now all required passes are available. 00659 if (ImmutablePass *IP = P->getAsImmutablePass()) { 00660 // P is a immutable pass and it will be managed by this 00661 // top level manager. Set up analysis resolver to connect them. 00662 PMDataManager *DM = getAsPMDataManager(); 00663 AnalysisResolver *AR = new AnalysisResolver(*DM); 00664 P->setResolver(AR); 00665 DM->initializeAnalysisImpl(P); 00666 addImmutablePass(IP); 00667 DM->recordAvailableAnalysis(IP); 00668 return; 00669 } 00670 00671 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) { 00672 Pass *PP = P->createPrinterPass( 00673 dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***"); 00674 PP->assignPassManager(activeStack, getTopLevelPassManagerType()); 00675 } 00676 00677 // Add the requested pass to the best available pass manager. 00678 P->assignPassManager(activeStack, getTopLevelPassManagerType()); 00679 00680 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) { 00681 Pass *PP = P->createPrinterPass( 00682 dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***"); 00683 PP->assignPassManager(activeStack, getTopLevelPassManagerType()); 00684 } 00685 } 00686 00687 /// Find the pass that implements Analysis AID. Search immutable 00688 /// passes and all pass managers. If desired pass is not found 00689 /// then return NULL. 00690 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { 00691 00692 // Check pass managers 00693 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(), 00694 E = PassManagers.end(); I != E; ++I) 00695 if (Pass *P = (*I)->findAnalysisPass(AID, false)) 00696 return P; 00697 00698 // Check other pass managers 00699 for (SmallVectorImpl<PMDataManager *>::iterator 00700 I = IndirectPassManagers.begin(), 00701 E = IndirectPassManagers.end(); I != E; ++I) 00702 if (Pass *P = (*I)->findAnalysisPass(AID, false)) 00703 return P; 00704 00705 // Check the immutable passes. Iterate in reverse order so that we find 00706 // the most recently registered passes first. 00707 for (SmallVector<ImmutablePass *, 8>::reverse_iterator I = 00708 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) { 00709 AnalysisID PI = (*I)->getPassID(); 00710 if (PI == AID) 00711 return *I; 00712 00713 // If Pass not found then check the interfaces implemented by Immutable Pass 00714 const PassInfo *PassInf = 00715 PassRegistry::getPassRegistry()->getPassInfo(PI); 00716 assert(PassInf && "Expected all immutable passes to be initialized"); 00717 const std::vector<const PassInfo*> &ImmPI = 00718 PassInf->getInterfacesImplemented(); 00719 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(), 00720 EE = ImmPI.end(); II != EE; ++II) { 00721 if ((*II)->getTypeInfo() == AID) 00722 return *I; 00723 } 00724 } 00725 00726 return 0; 00727 } 00728 00729 // Print passes managed by this top level manager. 00730 void PMTopLevelManager::dumpPasses() const { 00731 00732 if (PassDebugging < Structure) 00733 return; 00734 00735 // Print out the immutable passes 00736 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { 00737 ImmutablePasses[i]->dumpPassStructure(0); 00738 } 00739 00740 // Every class that derives from PMDataManager also derives from Pass 00741 // (sometimes indirectly), but there's no inheritance relationship 00742 // between PMDataManager and Pass, so we have to getAsPass to get 00743 // from a PMDataManager* to a Pass*. 00744 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(), 00745 E = PassManagers.end(); I != E; ++I) 00746 (*I)->getAsPass()->dumpPassStructure(1); 00747 } 00748 00749 void PMTopLevelManager::dumpArguments() const { 00750 00751 if (PassDebugging < Arguments) 00752 return; 00753 00754 dbgs() << "Pass Arguments: "; 00755 for (SmallVector<ImmutablePass *, 8>::const_iterator I = 00756 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) 00757 if (const PassInfo *PI = 00758 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) { 00759 assert(PI && "Expected all immutable passes to be initialized"); 00760 if (!PI->isAnalysisGroup()) 00761 dbgs() << " -" << PI->getPassArgument(); 00762 } 00763 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(), 00764 E = PassManagers.end(); I != E; ++I) 00765 (*I)->dumpPassArguments(); 00766 dbgs() << "\n"; 00767 } 00768 00769 void PMTopLevelManager::initializeAllAnalysisInfo() { 00770 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(), 00771 E = PassManagers.end(); I != E; ++I) 00772 (*I)->initializeAnalysisInfo(); 00773 00774 // Initailize other pass managers 00775 for (SmallVectorImpl<PMDataManager *>::iterator 00776 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end(); 00777 I != E; ++I) 00778 (*I)->initializeAnalysisInfo(); 00779 00780 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(), 00781 DME = LastUser.end(); DMI != DME; ++DMI) { 00782 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI = 00783 InversedLastUser.find(DMI->second); 00784 if (InvDMI != InversedLastUser.end()) { 00785 SmallPtrSet<Pass *, 8> &L = InvDMI->second; 00786 L.insert(DMI->first); 00787 } else { 00788 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first); 00789 InversedLastUser[DMI->second] = L; 00790 } 00791 } 00792 } 00793 00794 /// Destructor 00795 PMTopLevelManager::~PMTopLevelManager() { 00796 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(), 00797 E = PassManagers.end(); I != E; ++I) 00798 delete *I; 00799 00800 for (SmallVectorImpl<ImmutablePass *>::iterator 00801 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) 00802 delete *I; 00803 00804 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(), 00805 DME = AnUsageMap.end(); DMI != DME; ++DMI) 00806 delete DMI->second; 00807 } 00808 00809 //===----------------------------------------------------------------------===// 00810 // PMDataManager implementation 00811 00812 /// Augement AvailableAnalysis by adding analysis made available by pass P. 00813 void PMDataManager::recordAvailableAnalysis(Pass *P) { 00814 AnalysisID PI = P->getPassID(); 00815 00816 AvailableAnalysis[PI] = P; 00817 00818 assert(!AvailableAnalysis.empty()); 00819 00820 // This pass is the current implementation of all of the interfaces it 00821 // implements as well. 00822 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI); 00823 if (PInf == 0) return; 00824 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented(); 00825 for (unsigned i = 0, e = II.size(); i != e; ++i) 00826 AvailableAnalysis[II[i]->getTypeInfo()] = P; 00827 } 00828 00829 // Return true if P preserves high level analysis used by other 00830 // passes managed by this manager 00831 bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) { 00832 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); 00833 if (AnUsage->getPreservesAll()) 00834 return true; 00835 00836 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); 00837 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(), 00838 E = HigherLevelAnalysis.end(); I != E; ++I) { 00839 Pass *P1 = *I; 00840 if (P1->getAsImmutablePass() == 0 && 00841 std::find(PreservedSet.begin(), PreservedSet.end(), 00842 P1->getPassID()) == 00843 PreservedSet.end()) 00844 return false; 00845 } 00846 00847 return true; 00848 } 00849 00850 /// verifyPreservedAnalysis -- Verify analysis preserved by pass P. 00851 void PMDataManager::verifyPreservedAnalysis(Pass *P) { 00852 // Don't do this unless assertions are enabled. 00853 #ifdef NDEBUG 00854 return; 00855 #endif 00856 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); 00857 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); 00858 00859 // Verify preserved analysis 00860 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(), 00861 E = PreservedSet.end(); I != E; ++I) { 00862 AnalysisID AID = *I; 00863 if (Pass *AP = findAnalysisPass(AID, true)) { 00864 TimeRegion PassTimer(getPassTimer(AP)); 00865 AP->verifyAnalysis(); 00866 } 00867 } 00868 } 00869 00870 /// Remove Analysis not preserved by Pass P 00871 void PMDataManager::removeNotPreservedAnalysis(Pass *P) { 00872 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); 00873 if (AnUsage->getPreservesAll()) 00874 return; 00875 00876 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); 00877 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), 00878 E = AvailableAnalysis.end(); I != E; ) { 00879 DenseMap<AnalysisID, Pass*>::iterator Info = I++; 00880 if (Info->second->getAsImmutablePass() == 0 && 00881 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == 00882 PreservedSet.end()) { 00883 // Remove this analysis 00884 if (PassDebugging >= Details) { 00885 Pass *S = Info->second; 00886 dbgs() << " -- '" << P->getPassName() << "' is not preserving '"; 00887 dbgs() << S->getPassName() << "'\n"; 00888 } 00889 AvailableAnalysis.erase(Info); 00890 } 00891 } 00892 00893 // Check inherited analysis also. If P is not preserving analysis 00894 // provided by parent manager then remove it here. 00895 for (unsigned Index = 0; Index < PMT_Last; ++Index) { 00896 00897 if (!InheritedAnalysis[Index]) 00898 continue; 00899 00900 for (DenseMap<AnalysisID, Pass*>::iterator 00901 I = InheritedAnalysis[Index]->begin(), 00902 E = InheritedAnalysis[Index]->end(); I != E; ) { 00903 DenseMap<AnalysisID, Pass *>::iterator Info = I++; 00904 if (Info->second->getAsImmutablePass() == 0 && 00905 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == 00906 PreservedSet.end()) { 00907 // Remove this analysis 00908 if (PassDebugging >= Details) { 00909 Pass *S = Info->second; 00910 dbgs() << " -- '" << P->getPassName() << "' is not preserving '"; 00911 dbgs() << S->getPassName() << "'\n"; 00912 } 00913 InheritedAnalysis[Index]->erase(Info); 00914 } 00915 } 00916 } 00917 } 00918 00919 /// Remove analysis passes that are not used any longer 00920 void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg, 00921 enum PassDebuggingString DBG_STR) { 00922 00923 SmallVector<Pass *, 12> DeadPasses; 00924 00925 // If this is a on the fly manager then it does not have TPM. 00926 if (!TPM) 00927 return; 00928 00929 TPM->collectLastUses(DeadPasses, P); 00930 00931 if (PassDebugging >= Details && !DeadPasses.empty()) { 00932 dbgs() << " -*- '" << P->getPassName(); 00933 dbgs() << "' is the last user of following pass instances."; 00934 dbgs() << " Free these instances\n"; 00935 } 00936 00937 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(), 00938 E = DeadPasses.end(); I != E; ++I) 00939 freePass(*I, Msg, DBG_STR); 00940 } 00941 00942 void PMDataManager::freePass(Pass *P, StringRef Msg, 00943 enum PassDebuggingString DBG_STR) { 00944 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg); 00945 00946 { 00947 // If the pass crashes releasing memory, remember this. 00948 PassManagerPrettyStackEntry X(P); 00949 TimeRegion PassTimer(getPassTimer(P)); 00950 00951 P->releaseMemory(); 00952 } 00953 00954 AnalysisID PI = P->getPassID(); 00955 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) { 00956 // Remove the pass itself (if it is not already removed). 00957 AvailableAnalysis.erase(PI); 00958 00959 // Remove all interfaces this pass implements, for which it is also 00960 // listed as the available implementation. 00961 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented(); 00962 for (unsigned i = 0, e = II.size(); i != e; ++i) { 00963 DenseMap<AnalysisID, Pass*>::iterator Pos = 00964 AvailableAnalysis.find(II[i]->getTypeInfo()); 00965 if (Pos != AvailableAnalysis.end() && Pos->second == P) 00966 AvailableAnalysis.erase(Pos); 00967 } 00968 } 00969 } 00970 00971 /// Add pass P into the PassVector. Update 00972 /// AvailableAnalysis appropriately if ProcessAnalysis is true. 00973 void PMDataManager::add(Pass *P, bool ProcessAnalysis) { 00974 // This manager is going to manage pass P. Set up analysis resolver 00975 // to connect them. 00976 AnalysisResolver *AR = new AnalysisResolver(*this); 00977 P->setResolver(AR); 00978 00979 // If a FunctionPass F is the last user of ModulePass info M 00980 // then the F's manager, not F, records itself as a last user of M. 00981 SmallVector<Pass *, 12> TransferLastUses; 00982 00983 if (!ProcessAnalysis) { 00984 // Add pass 00985 PassVector.push_back(P); 00986 return; 00987 } 00988 00989 // At the moment, this pass is the last user of all required passes. 00990 SmallVector<Pass *, 12> LastUses; 00991 SmallVector<Pass *, 8> RequiredPasses; 00992 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable; 00993 00994 unsigned PDepth = this->getDepth(); 00995 00996 collectRequiredAnalysis(RequiredPasses, 00997 ReqAnalysisNotAvailable, P); 00998 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(), 00999 E = RequiredPasses.end(); I != E; ++I) { 01000 Pass *PRequired = *I; 01001 unsigned RDepth = 0; 01002 01003 assert(PRequired->getResolver() && "Analysis Resolver is not set"); 01004 PMDataManager &DM = PRequired->getResolver()->getPMDataManager(); 01005 RDepth = DM.getDepth(); 01006 01007 if (PDepth == RDepth) 01008 LastUses.push_back(PRequired); 01009 else if (PDepth > RDepth) { 01010 // Let the parent claim responsibility of last use 01011 TransferLastUses.push_back(PRequired); 01012 // Keep track of higher level analysis used by this manager. 01013 HigherLevelAnalysis.push_back(PRequired); 01014 } else 01015 llvm_unreachable("Unable to accommodate Required Pass"); 01016 } 01017 01018 // Set P as P's last user until someone starts using P. 01019 // However, if P is a Pass Manager then it does not need 01020 // to record its last user. 01021 if (P->getAsPMDataManager() == 0) 01022 LastUses.push_back(P); 01023 TPM->setLastUser(LastUses, P); 01024 01025 if (!TransferLastUses.empty()) { 01026 Pass *My_PM = getAsPass(); 01027 TPM->setLastUser(TransferLastUses, My_PM); 01028 TransferLastUses.clear(); 01029 } 01030 01031 // Now, take care of required analyses that are not available. 01032 for (SmallVectorImpl<AnalysisID>::iterator 01033 I = ReqAnalysisNotAvailable.begin(), 01034 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) { 01035 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I); 01036 Pass *AnalysisPass = PI->createPass(); 01037 this->addLowerLevelRequiredPass(P, AnalysisPass); 01038 } 01039 01040 // Take a note of analysis required and made available by this pass. 01041 // Remove the analysis not preserved by this pass 01042 removeNotPreservedAnalysis(P); 01043 recordAvailableAnalysis(P); 01044 01045 // Add pass 01046 PassVector.push_back(P); 01047 } 01048 01049 01050 /// Populate RP with analysis pass that are required by 01051 /// pass P and are available. Populate RP_NotAvail with analysis 01052 /// pass that are required by pass P but are not available. 01053 void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP, 01054 SmallVectorImpl<AnalysisID> &RP_NotAvail, 01055 Pass *P) { 01056 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); 01057 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet(); 01058 for (AnalysisUsage::VectorType::const_iterator 01059 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) { 01060 if (Pass *AnalysisPass = findAnalysisPass(*I, true)) 01061 RP.push_back(AnalysisPass); 01062 else 01063 RP_NotAvail.push_back(*I); 01064 } 01065 01066 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet(); 01067 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(), 01068 E = IDs.end(); I != E; ++I) { 01069 if (Pass *AnalysisPass = findAnalysisPass(*I, true)) 01070 RP.push_back(AnalysisPass); 01071 else 01072 RP_NotAvail.push_back(*I); 01073 } 01074 } 01075 01076 // All Required analyses should be available to the pass as it runs! Here 01077 // we fill in the AnalysisImpls member of the pass so that it can 01078 // successfully use the getAnalysis() method to retrieve the 01079 // implementations it needs. 01080 // 01081 void PMDataManager::initializeAnalysisImpl(Pass *P) { 01082 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); 01083 01084 for (AnalysisUsage::VectorType::const_iterator 01085 I = AnUsage->getRequiredSet().begin(), 01086 E = AnUsage->getRequiredSet().end(); I != E; ++I) { 01087 Pass *Impl = findAnalysisPass(*I, true); 01088 if (Impl == 0) 01089 // This may be analysis pass that is initialized on the fly. 01090 // If that is not the case then it will raise an assert when it is used. 01091 continue; 01092 AnalysisResolver *AR = P->getResolver(); 01093 assert(AR && "Analysis Resolver is not set"); 01094 AR->addAnalysisImplsPair(*I, Impl); 01095 } 01096 } 01097 01098 /// Find the pass that implements Analysis AID. If desired pass is not found 01099 /// then return NULL. 01100 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) { 01101 01102 // Check if AvailableAnalysis map has one entry. 01103 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID); 01104 01105 if (I != AvailableAnalysis.end()) 01106 return I->second; 01107 01108 // Search Parents through TopLevelManager 01109 if (SearchParent) 01110 return TPM->findAnalysisPass(AID); 01111 01112 return NULL; 01113 } 01114 01115 // Print list of passes that are last used by P. 01116 void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{ 01117 01118 SmallVector<Pass *, 12> LUses; 01119 01120 // If this is a on the fly manager then it does not have TPM. 01121 if (!TPM) 01122 return; 01123 01124 TPM->collectLastUses(LUses, P); 01125 01126 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(), 01127 E = LUses.end(); I != E; ++I) { 01128 llvm::dbgs() << "--" << std::string(Offset*2, ' '); 01129 (*I)->dumpPassStructure(0); 01130 } 01131 } 01132 01133 void PMDataManager::dumpPassArguments() const { 01134 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(), 01135 E = PassVector.end(); I != E; ++I) { 01136 if (PMDataManager *PMD = (*I)->getAsPMDataManager()) 01137 PMD->dumpPassArguments(); 01138 else 01139 if (const PassInfo *PI = 01140 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) 01141 if (!PI->isAnalysisGroup()) 01142 dbgs() << " -" << PI->getPassArgument(); 01143 } 01144 } 01145 01146 void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1, 01147 enum PassDebuggingString S2, 01148 StringRef Msg) { 01149 if (PassDebugging < Executions) 01150 return; 01151 dbgs() << (void*)this << std::string(getDepth()*2+1, ' '); 01152 switch (S1) { 01153 case EXECUTION_MSG: 01154 dbgs() << "Executing Pass '" << P->getPassName(); 01155 break; 01156 case MODIFICATION_MSG: 01157 dbgs() << "Made Modification '" << P->getPassName(); 01158 break; 01159 case FREEING_MSG: 01160 dbgs() << " Freeing Pass '" << P->getPassName(); 01161 break; 01162 default: 01163 break; 01164 } 01165 switch (S2) { 01166 case ON_BASICBLOCK_MSG: 01167 dbgs() << "' on BasicBlock '" << Msg << "'...\n"; 01168 break; 01169 case ON_FUNCTION_MSG: 01170 dbgs() << "' on Function '" << Msg << "'...\n"; 01171 break; 01172 case ON_MODULE_MSG: 01173 dbgs() << "' on Module '" << Msg << "'...\n"; 01174 break; 01175 case ON_REGION_MSG: 01176 dbgs() << "' on Region '" << Msg << "'...\n"; 01177 break; 01178 case ON_LOOP_MSG: 01179 dbgs() << "' on Loop '" << Msg << "'...\n"; 01180 break; 01181 case ON_CG_MSG: 01182 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n"; 01183 break; 01184 default: 01185 break; 01186 } 01187 } 01188 01189 void PMDataManager::dumpRequiredSet(const Pass *P) const { 01190 if (PassDebugging < Details) 01191 return; 01192 01193 AnalysisUsage analysisUsage; 01194 P->getAnalysisUsage(analysisUsage); 01195 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet()); 01196 } 01197 01198 void PMDataManager::dumpPreservedSet(const Pass *P) const { 01199 if (PassDebugging < Details) 01200 return; 01201 01202 AnalysisUsage analysisUsage; 01203 P->getAnalysisUsage(analysisUsage); 01204 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet()); 01205 } 01206 01207 void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P, 01208 const AnalysisUsage::VectorType &Set) const { 01209 assert(PassDebugging >= Details); 01210 if (Set.empty()) 01211 return; 01212 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:"; 01213 for (unsigned i = 0; i != Set.size(); ++i) { 01214 if (i) dbgs() << ','; 01215 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]); 01216 if (!PInf) { 01217 // Some preserved passes, such as AliasAnalysis, may not be initialized by 01218 // all drivers. 01219 dbgs() << " Uninitialized Pass"; 01220 continue; 01221 } 01222 dbgs() << ' ' << PInf->getPassName(); 01223 } 01224 dbgs() << '\n'; 01225 } 01226 01227 /// Add RequiredPass into list of lower level passes required by pass P. 01228 /// RequiredPass is run on the fly by Pass Manager when P requests it 01229 /// through getAnalysis interface. 01230 /// This should be handled by specific pass manager. 01231 void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { 01232 if (TPM) { 01233 TPM->dumpArguments(); 01234 TPM->dumpPasses(); 01235 } 01236 01237 // Module Level pass may required Function Level analysis info 01238 // (e.g. dominator info). Pass manager uses on the fly function pass manager 01239 // to provide this on demand. In that case, in Pass manager terminology, 01240 // module level pass is requiring lower level analysis info managed by 01241 // lower level pass manager. 01242 01243 // When Pass manager is not able to order required analysis info, Pass manager 01244 // checks whether any lower level manager will be able to provide this 01245 // analysis info on demand or not. 01246 #ifndef NDEBUG 01247 dbgs() << "Unable to schedule '" << RequiredPass->getPassName(); 01248 dbgs() << "' required by '" << P->getPassName() << "'\n"; 01249 #endif 01250 llvm_unreachable("Unable to schedule pass"); 01251 } 01252 01253 Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) { 01254 llvm_unreachable("Unable to find on the fly pass"); 01255 } 01256 01257 // Destructor 01258 PMDataManager::~PMDataManager() { 01259 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(), 01260 E = PassVector.end(); I != E; ++I) 01261 delete *I; 01262 } 01263 01264 //===----------------------------------------------------------------------===// 01265 // NOTE: Is this the right place to define this method ? 01266 // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist. 01267 Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const { 01268 return PM.findAnalysisPass(ID, dir); 01269 } 01270 01271 Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI, 01272 Function &F) { 01273 return PM.getOnTheFlyPass(P, AnalysisPI, F); 01274 } 01275 01276 //===----------------------------------------------------------------------===// 01277 // BBPassManager implementation 01278 01279 /// Execute all of the passes scheduled for execution by invoking 01280 /// runOnBasicBlock method. Keep track of whether any of the passes modifies 01281 /// the function, and if so, return true. 01282 bool BBPassManager::runOnFunction(Function &F) { 01283 if (F.isDeclaration()) 01284 return false; 01285 01286 bool Changed = doInitialization(F); 01287 01288 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) 01289 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 01290 BasicBlockPass *BP = getContainedPass(Index); 01291 bool LocalChanged = false; 01292 01293 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName()); 01294 dumpRequiredSet(BP); 01295 01296 initializeAnalysisImpl(BP); 01297 01298 { 01299 // If the pass crashes, remember this. 01300 PassManagerPrettyStackEntry X(BP, *I); 01301 TimeRegion PassTimer(getPassTimer(BP)); 01302 01303 LocalChanged |= BP->runOnBasicBlock(*I); 01304 } 01305 01306 Changed |= LocalChanged; 01307 if (LocalChanged) 01308 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, 01309 I->getName()); 01310 dumpPreservedSet(BP); 01311 01312 verifyPreservedAnalysis(BP); 01313 removeNotPreservedAnalysis(BP); 01314 recordAvailableAnalysis(BP); 01315 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG); 01316 } 01317 01318 return doFinalization(F) || Changed; 01319 } 01320 01321 // Implement doInitialization and doFinalization 01322 bool BBPassManager::doInitialization(Module &M) { 01323 bool Changed = false; 01324 01325 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) 01326 Changed |= getContainedPass(Index)->doInitialization(M); 01327 01328 return Changed; 01329 } 01330 01331 bool BBPassManager::doFinalization(Module &M) { 01332 bool Changed = false; 01333 01334 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index) 01335 Changed |= getContainedPass(Index)->doFinalization(M); 01336 01337 return Changed; 01338 } 01339 01340 bool BBPassManager::doInitialization(Function &F) { 01341 bool Changed = false; 01342 01343 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 01344 BasicBlockPass *BP = getContainedPass(Index); 01345 Changed |= BP->doInitialization(F); 01346 } 01347 01348 return Changed; 01349 } 01350 01351 bool BBPassManager::doFinalization(Function &F) { 01352 bool Changed = false; 01353 01354 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 01355 BasicBlockPass *BP = getContainedPass(Index); 01356 Changed |= BP->doFinalization(F); 01357 } 01358 01359 return Changed; 01360 } 01361 01362 01363 //===----------------------------------------------------------------------===// 01364 // FunctionPassManager implementation 01365 01366 /// Create new Function pass manager 01367 FunctionPassManager::FunctionPassManager(Module *m) : M(m) { 01368 FPM = new FunctionPassManagerImpl(); 01369 // FPM is the top level manager. 01370 FPM->setTopLevelManager(FPM); 01371 01372 AnalysisResolver *AR = new AnalysisResolver(*FPM); 01373 FPM->setResolver(AR); 01374 } 01375 01376 FunctionPassManager::~FunctionPassManager() { 01377 delete FPM; 01378 } 01379 01380 /// add - Add a pass to the queue of passes to run. This passes 01381 /// ownership of the Pass to the PassManager. When the 01382 /// PassManager_X is destroyed, the pass will be destroyed as well, so 01383 /// there is no need to delete the pass. (TODO delete passes.) 01384 /// This implies that all passes MUST be allocated with 'new'. 01385 void FunctionPassManager::add(Pass *P) { 01386 FPM->add(P); 01387 } 01388 01389 /// run - Execute all of the passes scheduled for execution. Keep 01390 /// track of whether any of the passes modifies the function, and if 01391 /// so, return true. 01392 /// 01393 bool FunctionPassManager::run(Function &F) { 01394 if (F.isMaterializable()) { 01395 std::string errstr; 01396 if (F.Materialize(&errstr)) 01397 report_fatal_error("Error reading bitcode file: " + Twine(errstr)); 01398 } 01399 return FPM->run(F); 01400 } 01401 01402 01403 /// doInitialization - Run all of the initializers for the function passes. 01404 /// 01405 bool FunctionPassManager::doInitialization() { 01406 return FPM->doInitialization(*M); 01407 } 01408 01409 /// doFinalization - Run all of the finalizers for the function passes. 01410 /// 01411 bool FunctionPassManager::doFinalization() { 01412 return FPM->doFinalization(*M); 01413 } 01414 01415 //===----------------------------------------------------------------------===// 01416 // FunctionPassManagerImpl implementation 01417 // 01418 bool FunctionPassManagerImpl::doInitialization(Module &M) { 01419 bool Changed = false; 01420 01421 dumpArguments(); 01422 dumpPasses(); 01423 01424 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses(); 01425 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(), 01426 E = IPV.end(); I != E; ++I) { 01427 Changed |= (*I)->doInitialization(M); 01428 } 01429 01430 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) 01431 Changed |= getContainedManager(Index)->doInitialization(M); 01432 01433 return Changed; 01434 } 01435 01436 bool FunctionPassManagerImpl::doFinalization(Module &M) { 01437 bool Changed = false; 01438 01439 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index) 01440 Changed |= getContainedManager(Index)->doFinalization(M); 01441 01442 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses(); 01443 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(), 01444 E = IPV.end(); I != E; ++I) { 01445 Changed |= (*I)->doFinalization(M); 01446 } 01447 01448 return Changed; 01449 } 01450 01451 /// cleanup - After running all passes, clean up pass manager cache. 01452 void FPPassManager::cleanup() { 01453 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 01454 FunctionPass *FP = getContainedPass(Index); 01455 AnalysisResolver *AR = FP->getResolver(); 01456 assert(AR && "Analysis Resolver is not set"); 01457 AR->clearAnalysisImpls(); 01458 } 01459 } 01460 01461 void FunctionPassManagerImpl::releaseMemoryOnTheFly() { 01462 if (!wasRun) 01463 return; 01464 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { 01465 FPPassManager *FPPM = getContainedManager(Index); 01466 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) { 01467 FPPM->getContainedPass(Index)->releaseMemory(); 01468 } 01469 } 01470 wasRun = false; 01471 } 01472 01473 // Execute all the passes managed by this top level manager. 01474 // Return true if any function is modified by a pass. 01475 bool FunctionPassManagerImpl::run(Function &F) { 01476 bool Changed = false; 01477 TimingInfo::createTheTimeInfo(); 01478 01479 initializeAllAnalysisInfo(); 01480 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) 01481 Changed |= getContainedManager(Index)->runOnFunction(F); 01482 01483 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) 01484 getContainedManager(Index)->cleanup(); 01485 01486 wasRun = true; 01487 return Changed; 01488 } 01489 01490 //===----------------------------------------------------------------------===// 01491 // FPPassManager implementation 01492 01493 char FPPassManager::ID = 0; 01494 /// Print passes managed by this manager 01495 void FPPassManager::dumpPassStructure(unsigned Offset) { 01496 dbgs().indent(Offset*2) << "FunctionPass Manager\n"; 01497 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 01498 FunctionPass *FP = getContainedPass(Index); 01499 FP->dumpPassStructure(Offset + 1); 01500 dumpLastUses(FP, Offset+1); 01501 } 01502 } 01503 01504 01505 /// Execute all of the passes scheduled for execution by invoking 01506 /// runOnFunction method. Keep track of whether any of the passes modifies 01507 /// the function, and if so, return true. 01508 bool FPPassManager::runOnFunction(Function &F) { 01509 if (F.isDeclaration()) 01510 return false; 01511 01512 bool Changed = false; 01513 01514 // Collect inherited analysis from Module level pass manager. 01515 populateInheritedAnalysis(TPM->activeStack); 01516 01517 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 01518 FunctionPass *FP = getContainedPass(Index); 01519 bool LocalChanged = false; 01520 01521 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName()); 01522 dumpRequiredSet(FP); 01523 01524 initializeAnalysisImpl(FP); 01525 01526 { 01527 PassManagerPrettyStackEntry X(FP, F); 01528 TimeRegion PassTimer(getPassTimer(FP)); 01529 01530 LocalChanged |= FP->runOnFunction(F); 01531 } 01532 01533 Changed |= LocalChanged; 01534 if (LocalChanged) 01535 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName()); 01536 dumpPreservedSet(FP); 01537 01538 verifyPreservedAnalysis(FP); 01539 removeNotPreservedAnalysis(FP); 01540 recordAvailableAnalysis(FP); 01541 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG); 01542 } 01543 return Changed; 01544 } 01545 01546 bool FPPassManager::runOnModule(Module &M) { 01547 bool Changed = false; 01548 01549 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 01550 Changed |= runOnFunction(*I); 01551 01552 return Changed; 01553 } 01554 01555 bool FPPassManager::doInitialization(Module &M) { 01556 bool Changed = false; 01557 01558 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) 01559 Changed |= getContainedPass(Index)->doInitialization(M); 01560 01561 return Changed; 01562 } 01563 01564 bool FPPassManager::doFinalization(Module &M) { 01565 bool Changed = false; 01566 01567 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index) 01568 Changed |= getContainedPass(Index)->doFinalization(M); 01569 01570 return Changed; 01571 } 01572 01573 //===----------------------------------------------------------------------===// 01574 // MPPassManager implementation 01575 01576 /// Execute all of the passes scheduled for execution by invoking 01577 /// runOnModule method. Keep track of whether any of the passes modifies 01578 /// the module, and if so, return true. 01579 bool 01580 MPPassManager::runOnModule(Module &M) { 01581 bool Changed = false; 01582 01583 // Initialize on-the-fly passes 01584 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator 01585 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); 01586 I != E; ++I) { 01587 FunctionPassManagerImpl *FPP = I->second; 01588 Changed |= FPP->doInitialization(M); 01589 } 01590 01591 // Initialize module passes 01592 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) 01593 Changed |= getContainedPass(Index)->doInitialization(M); 01594 01595 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 01596 ModulePass *MP = getContainedPass(Index); 01597 bool LocalChanged = false; 01598 01599 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier()); 01600 dumpRequiredSet(MP); 01601 01602 initializeAnalysisImpl(MP); 01603 01604 { 01605 PassManagerPrettyStackEntry X(MP, M); 01606 TimeRegion PassTimer(getPassTimer(MP)); 01607 01608 LocalChanged |= MP->runOnModule(M); 01609 } 01610 01611 Changed |= LocalChanged; 01612 if (LocalChanged) 01613 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG, 01614 M.getModuleIdentifier()); 01615 dumpPreservedSet(MP); 01616 01617 verifyPreservedAnalysis(MP); 01618 removeNotPreservedAnalysis(MP); 01619 recordAvailableAnalysis(MP); 01620 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG); 01621 } 01622 01623 // Finalize module passes 01624 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index) 01625 Changed |= getContainedPass(Index)->doFinalization(M); 01626 01627 // Finalize on-the-fly passes 01628 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator 01629 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); 01630 I != E; ++I) { 01631 FunctionPassManagerImpl *FPP = I->second; 01632 // We don't know when is the last time an on-the-fly pass is run, 01633 // so we need to releaseMemory / finalize here 01634 FPP->releaseMemoryOnTheFly(); 01635 Changed |= FPP->doFinalization(M); 01636 } 01637 01638 return Changed; 01639 } 01640 01641 /// Add RequiredPass into list of lower level passes required by pass P. 01642 /// RequiredPass is run on the fly by Pass Manager when P requests it 01643 /// through getAnalysis interface. 01644 void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { 01645 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager && 01646 "Unable to handle Pass that requires lower level Analysis pass"); 01647 assert((P->getPotentialPassManagerType() < 01648 RequiredPass->getPotentialPassManagerType()) && 01649 "Unable to handle Pass that requires lower level Analysis pass"); 01650 01651 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P]; 01652 if (!FPP) { 01653 FPP = new FunctionPassManagerImpl(); 01654 // FPP is the top level manager. 01655 FPP->setTopLevelManager(FPP); 01656 01657 OnTheFlyManagers[P] = FPP; 01658 } 01659 FPP->add(RequiredPass); 01660 01661 // Register P as the last user of RequiredPass. 01662 if (RequiredPass) { 01663 SmallVector<Pass *, 1> LU; 01664 LU.push_back(RequiredPass); 01665 FPP->setLastUser(LU, P); 01666 } 01667 } 01668 01669 /// Return function pass corresponding to PassInfo PI, that is 01670 /// required by module pass MP. Instantiate analysis pass, by using 01671 /// its runOnFunction() for function F. 01672 Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){ 01673 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP]; 01674 assert(FPP && "Unable to find on the fly pass"); 01675 01676 FPP->releaseMemoryOnTheFly(); 01677 FPP->run(F); 01678 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI); 01679 } 01680 01681 01682 //===----------------------------------------------------------------------===// 01683 // PassManagerImpl implementation 01684 01685 // 01686 /// run - Execute all of the passes scheduled for execution. Keep track of 01687 /// whether any of the passes modifies the module, and if so, return true. 01688 bool PassManagerImpl::run(Module &M) { 01689 bool Changed = false; 01690 TimingInfo::createTheTimeInfo(); 01691 01692 dumpArguments(); 01693 dumpPasses(); 01694 01695 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses(); 01696 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(), 01697 E = IPV.end(); I != E; ++I) { 01698 Changed |= (*I)->doInitialization(M); 01699 } 01700 01701 initializeAllAnalysisInfo(); 01702 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) 01703 Changed |= getContainedManager(Index)->runOnModule(M); 01704 01705 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(), 01706 E = IPV.end(); I != E; ++I) { 01707 Changed |= (*I)->doFinalization(M); 01708 } 01709 01710 return Changed; 01711 } 01712 01713 //===----------------------------------------------------------------------===// 01714 // PassManager implementation 01715 01716 /// Create new pass manager 01717 PassManager::PassManager() { 01718 PM = new PassManagerImpl(); 01719 // PM is the top level manager 01720 PM->setTopLevelManager(PM); 01721 } 01722 01723 PassManager::~PassManager() { 01724 delete PM; 01725 } 01726 01727 /// add - Add a pass to the queue of passes to run. This passes ownership of 01728 /// the Pass to the PassManager. When the PassManager is destroyed, the pass 01729 /// will be destroyed as well, so there is no need to delete the pass. This 01730 /// implies that all passes MUST be allocated with 'new'. 01731 void PassManager::add(Pass *P) { 01732 PM->add(P); 01733 } 01734 01735 /// run - Execute all of the passes scheduled for execution. Keep track of 01736 /// whether any of the passes modifies the module, and if so, return true. 01737 bool PassManager::run(Module &M) { 01738 return PM->run(M); 01739 } 01740 01741 //===----------------------------------------------------------------------===// 01742 // TimingInfo implementation 01743 01744 bool llvm::TimePassesIsEnabled = false; 01745 static cl::opt<bool,true> 01746 EnableTiming("time-passes", cl::location(TimePassesIsEnabled), 01747 cl::desc("Time each pass, printing elapsed time for each on exit")); 01748 01749 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to 01750 // a non null value (if the -time-passes option is enabled) or it leaves it 01751 // null. It may be called multiple times. 01752 void TimingInfo::createTheTimeInfo() { 01753 if (!TimePassesIsEnabled || TheTimeInfo) return; 01754 01755 // Constructed the first time this is called, iff -time-passes is enabled. 01756 // This guarantees that the object will be constructed before static globals, 01757 // thus it will be destroyed before them. 01758 static ManagedStatic<TimingInfo> TTI; 01759 TheTimeInfo = &*TTI; 01760 } 01761 01762 /// If TimingInfo is enabled then start pass timer. 01763 Timer *llvm::getPassTimer(Pass *P) { 01764 if (TheTimeInfo) 01765 return TheTimeInfo->getPassTimer(P); 01766 return 0; 01767 } 01768 01769 //===----------------------------------------------------------------------===// 01770 // PMStack implementation 01771 // 01772 01773 // Pop Pass Manager from the stack and clear its analysis info. 01774 void PMStack::pop() { 01775 01776 PMDataManager *Top = this->top(); 01777 Top->initializeAnalysisInfo(); 01778 01779 S.pop_back(); 01780 } 01781 01782 // Push PM on the stack and set its top level manager. 01783 void PMStack::push(PMDataManager *PM) { 01784 assert(PM && "Unable to push. Pass Manager expected"); 01785 assert(PM->getDepth()==0 && "Pass Manager depth set too early"); 01786 01787 if (!this->empty()) { 01788 assert(PM->getPassManagerType() > this->top()->getPassManagerType() 01789 && "pushing bad pass manager to PMStack"); 01790 PMTopLevelManager *TPM = this->top()->getTopLevelManager(); 01791 01792 assert(TPM && "Unable to find top level manager"); 01793 TPM->addIndirectPassManager(PM); 01794 PM->setTopLevelManager(TPM); 01795 PM->setDepth(this->top()->getDepth()+1); 01796 } else { 01797 assert((PM->getPassManagerType() == PMT_ModulePassManager 01798 || PM->getPassManagerType() == PMT_FunctionPassManager) 01799 && "pushing bad pass manager to PMStack"); 01800 PM->setDepth(1); 01801 } 01802 01803 S.push_back(PM); 01804 } 01805 01806 // Dump content of the pass manager stack. 01807 void PMStack::dump() const { 01808 for (std::vector<PMDataManager *>::const_iterator I = S.begin(), 01809 E = S.end(); I != E; ++I) 01810 dbgs() << (*I)->getAsPass()->getPassName() << ' '; 01811 01812 if (!S.empty()) 01813 dbgs() << '\n'; 01814 } 01815 01816 /// Find appropriate Module Pass Manager in the PM Stack and 01817 /// add self into that manager. 01818 void ModulePass::assignPassManager(PMStack &PMS, 01819 PassManagerType PreferredType) { 01820 // Find Module Pass Manager 01821 while (!PMS.empty()) { 01822 PassManagerType TopPMType = PMS.top()->getPassManagerType(); 01823 if (TopPMType == PreferredType) 01824 break; // We found desired pass manager 01825 else if (TopPMType > PMT_ModulePassManager) 01826 PMS.pop(); // Pop children pass managers 01827 else 01828 break; 01829 } 01830 assert(!PMS.empty() && "Unable to find appropriate Pass Manager"); 01831 PMS.top()->add(this); 01832 } 01833 01834 /// Find appropriate Function Pass Manager or Call Graph Pass Manager 01835 /// in the PM Stack and add self into that manager. 01836 void FunctionPass::assignPassManager(PMStack &PMS, 01837 PassManagerType PreferredType) { 01838 01839 // Find Function Pass Manager 01840 while (!PMS.empty()) { 01841 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager) 01842 PMS.pop(); 01843 else 01844 break; 01845 } 01846 01847 // Create new Function Pass Manager if needed. 01848 FPPassManager *FPP; 01849 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) { 01850 FPP = (FPPassManager *)PMS.top(); 01851 } else { 01852 assert(!PMS.empty() && "Unable to create Function Pass Manager"); 01853 PMDataManager *PMD = PMS.top(); 01854 01855 // [1] Create new Function Pass Manager 01856 FPP = new FPPassManager(); 01857 FPP->populateInheritedAnalysis(PMS); 01858 01859 // [2] Set up new manager's top level manager 01860 PMTopLevelManager *TPM = PMD->getTopLevelManager(); 01861 TPM->addIndirectPassManager(FPP); 01862 01863 // [3] Assign manager to manage this new manager. This may create 01864 // and push new managers into PMS 01865 FPP->assignPassManager(PMS, PMD->getPassManagerType()); 01866 01867 // [4] Push new manager into PMS 01868 PMS.push(FPP); 01869 } 01870 01871 // Assign FPP as the manager of this pass. 01872 FPP->add(this); 01873 } 01874 01875 /// Find appropriate Basic Pass Manager or Call Graph Pass Manager 01876 /// in the PM Stack and add self into that manager. 01877 void BasicBlockPass::assignPassManager(PMStack &PMS, 01878 PassManagerType PreferredType) { 01879 BBPassManager *BBP; 01880 01881 // Basic Pass Manager is a leaf pass manager. It does not handle 01882 // any other pass manager. 01883 if (!PMS.empty() && 01884 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) { 01885 BBP = (BBPassManager *)PMS.top(); 01886 } else { 01887 // If leaf manager is not Basic Block Pass manager then create new 01888 // basic Block Pass manager. 01889 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager"); 01890 PMDataManager *PMD = PMS.top(); 01891 01892 // [1] Create new Basic Block Manager 01893 BBP = new BBPassManager(); 01894 01895 // [2] Set up new manager's top level manager 01896 // Basic Block Pass Manager does not live by itself 01897 PMTopLevelManager *TPM = PMD->getTopLevelManager(); 01898 TPM->addIndirectPassManager(BBP); 01899 01900 // [3] Assign manager to manage this new manager. This may create 01901 // and push new managers into PMS 01902 BBP->assignPassManager(PMS, PreferredType); 01903 01904 // [4] Push new manager into PMS 01905 PMS.push(BBP); 01906 } 01907 01908 // Assign BBP as the manager of this pass. 01909 BBP->add(this); 01910 } 01911 01912 PassManagerBase::~PassManagerBase() {}