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