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