PassManager.cpp revision 2bb7d0647e5b3f79ad9b6e7efd0dd99f24a0ca69
1//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by Devang Patel and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the LLVM Pass Manager infrastructure. 11// 12//===----------------------------------------------------------------------===// 13 14 15#include "llvm/PassManagers.h" 16#include "llvm/Support/CommandLine.h" 17#include "llvm/Support/Timer.h" 18#include "llvm/Module.h" 19#include "llvm/ModuleProvider.h" 20#include "llvm/Support/Streams.h" 21#include "llvm/Support/ManagedStatic.h" 22#include <algorithm> 23#include <vector> 24#include <map> 25using namespace llvm; 26 27// See PassManagers.h for Pass Manager infrastructure overview. 28 29namespace llvm { 30 31//===----------------------------------------------------------------------===// 32// Pass debugging information. Often it is useful to find out what pass is 33// running when a crash occurs in a utility. When this library is compiled with 34// debugging on, a command line option (--debug-pass) is enabled that causes the 35// pass name to be printed before it executes. 36// 37 38// Different debug levels that can be enabled... 39enum PassDebugLevel { 40 None, Arguments, Structure, Executions, Details 41}; 42 43static cl::opt<enum PassDebugLevel> 44PassDebugging("debug-pass", cl::Hidden, 45 cl::desc("Print PassManager debugging information"), 46 cl::values( 47 clEnumVal(None , "disable debug output"), 48 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"), 49 clEnumVal(Structure , "print pass structure before run()"), 50 clEnumVal(Executions, "print pass name before it is executed"), 51 clEnumVal(Details , "print pass details when it is executed"), 52 clEnumValEnd)); 53} // End of llvm namespace 54 55namespace { 56 57//===----------------------------------------------------------------------===// 58// BBPassManager 59// 60/// BBPassManager manages BasicBlockPass. It batches all the 61/// pass together and sequence them to process one basic block before 62/// processing next basic block. 63class VISIBILITY_HIDDEN BBPassManager : public PMDataManager, 64 public FunctionPass { 65 66public: 67 static char ID; 68 BBPassManager(int Depth) 69 : PMDataManager(Depth), FunctionPass((intptr_t)&ID) {} 70 71 /// Execute all of the passes scheduled for execution. Keep track of 72 /// whether any of the passes modifies the function, and if so, return true. 73 bool runOnFunction(Function &F); 74 75 /// Pass Manager itself does not invalidate any analysis info. 76 void getAnalysisUsage(AnalysisUsage &Info) const { 77 Info.setPreservesAll(); 78 } 79 80 bool doInitialization(Module &M); 81 bool doInitialization(Function &F); 82 bool doFinalization(Module &M); 83 bool doFinalization(Function &F); 84 85 virtual const char *getPassName() const { 86 return "BasicBlock Pass Manager"; 87 } 88 89 // Print passes managed by this manager 90 void dumpPassStructure(unsigned Offset) { 91 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n"; 92 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 93 BasicBlockPass *BP = getContainedPass(Index); 94 BP->dumpPassStructure(Offset + 1); 95 dumpLastUses(BP, Offset+1); 96 } 97 } 98 99 BasicBlockPass *getContainedPass(unsigned N) { 100 assert ( N < PassVector.size() && "Pass number out of range!"); 101 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]); 102 return BP; 103 } 104 105 virtual PassManagerType getPassManagerType() const { 106 return PMT_BasicBlockPassManager; 107 } 108}; 109 110char BBPassManager::ID = 0; 111} 112 113namespace llvm { 114 115//===----------------------------------------------------------------------===// 116// FunctionPassManagerImpl 117// 118/// FunctionPassManagerImpl manages FPPassManagers 119class FunctionPassManagerImpl : public Pass, 120 public PMDataManager, 121 public PMTopLevelManager { 122public: 123 static char ID; 124 FunctionPassManagerImpl(int Depth) : 125 Pass((intptr_t)&ID), PMDataManager(Depth), 126 PMTopLevelManager(TLM_Function) { } 127 128 /// add - Add a pass to the queue of passes to run. This passes ownership of 129 /// the Pass to the PassManager. When the PassManager is destroyed, the pass 130 /// will be destroyed as well, so there is no need to delete the pass. This 131 /// implies that all passes MUST be allocated with 'new'. 132 void add(Pass *P) { 133 schedulePass(P); 134 } 135 136 /// run - Execute all of the passes scheduled for execution. Keep track of 137 /// whether any of the passes modifies the module, and if so, return true. 138 bool run(Function &F); 139 140 /// doInitialization - Run all of the initializers for the function passes. 141 /// 142 bool doInitialization(Module &M); 143 144 /// doFinalization - Run all of the finalizers for the function passes. 145 /// 146 bool doFinalization(Module &M); 147 148 /// Pass Manager itself does not invalidate any analysis info. 149 void getAnalysisUsage(AnalysisUsage &Info) const { 150 Info.setPreservesAll(); 151 } 152 153 inline void addTopLevelPass(Pass *P) { 154 155 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) { 156 157 // P is a immutable pass and it will be managed by this 158 // top level manager. Set up analysis resolver to connect them. 159 AnalysisResolver *AR = new AnalysisResolver(*this); 160 P->setResolver(AR); 161 initializeAnalysisImpl(P); 162 addImmutablePass(IP); 163 recordAvailableAnalysis(IP); 164 } else { 165 P->assignPassManager(activeStack); 166 } 167 168 } 169 170 FPPassManager *getContainedManager(unsigned N) { 171 assert ( N < PassManagers.size() && "Pass number out of range!"); 172 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]); 173 return FP; 174 } 175}; 176 177char FunctionPassManagerImpl::ID = 0; 178//===----------------------------------------------------------------------===// 179// MPPassManager 180// 181/// MPPassManager manages ModulePasses and function pass managers. 182/// It batches all Module passes passes and function pass managers together and 183/// sequence them to process one module. 184class MPPassManager : public Pass, public PMDataManager { 185 186public: 187 static char ID; 188 MPPassManager(int Depth) : Pass((intptr_t)&ID), PMDataManager(Depth) { } 189 190 // Delete on the fly managers. 191 virtual ~MPPassManager() { 192 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator 193 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); 194 I != E; ++I) { 195 FunctionPassManagerImpl *FPP = I->second; 196 delete FPP; 197 } 198 } 199 200 /// run - Execute all of the passes scheduled for execution. Keep track of 201 /// whether any of the passes modifies the module, and if so, return true. 202 bool runOnModule(Module &M); 203 204 /// Pass Manager itself does not invalidate any analysis info. 205 void getAnalysisUsage(AnalysisUsage &Info) const { 206 Info.setPreservesAll(); 207 } 208 209 /// Add RequiredPass into list of lower level passes required by pass P. 210 /// RequiredPass is run on the fly by Pass Manager when P requests it 211 /// through getAnalysis interface. 212 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass); 213 214 /// Return function pass corresponding to PassInfo PI, that is 215 /// required by module pass MP. Instantiate analysis pass, by using 216 /// its runOnFunction() for function F. 217 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F); 218 219 virtual const char *getPassName() const { 220 return "Module Pass Manager"; 221 } 222 223 // Print passes managed by this manager 224 void dumpPassStructure(unsigned Offset) { 225 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n"; 226 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 227 ModulePass *MP = getContainedPass(Index); 228 MP->dumpPassStructure(Offset + 1); 229 if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP]) 230 FPP->dumpPassStructure(Offset + 2); 231 dumpLastUses(MP, Offset+1); 232 } 233 } 234 235 ModulePass *getContainedPass(unsigned N) { 236 assert ( N < PassVector.size() && "Pass number out of range!"); 237 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]); 238 return MP; 239 } 240 241 virtual PassManagerType getPassManagerType() const { 242 return PMT_ModulePassManager; 243 } 244 245 private: 246 /// Collection of on the fly FPPassManagers. These managers manage 247 /// function passes that are required by module passes. 248 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers; 249}; 250 251char MPPassManager::ID = 0; 252//===----------------------------------------------------------------------===// 253// PassManagerImpl 254// 255 256/// PassManagerImpl manages MPPassManagers 257class PassManagerImpl : public Pass, 258 public PMDataManager, 259 public PMTopLevelManager { 260 261public: 262 static char ID; 263 PassManagerImpl(int Depth) : Pass((intptr_t)&ID), PMDataManager(Depth), 264 PMTopLevelManager(TLM_Pass) { } 265 266 /// add - Add a pass to the queue of passes to run. This passes ownership of 267 /// the Pass to the PassManager. When the PassManager is destroyed, the pass 268 /// will be destroyed as well, so there is no need to delete the pass. This 269 /// implies that all passes MUST be allocated with 'new'. 270 void add(Pass *P) { 271 schedulePass(P); 272 } 273 274 /// run - Execute all of the passes scheduled for execution. Keep track of 275 /// whether any of the passes modifies the module, and if so, return true. 276 bool run(Module &M); 277 278 /// Pass Manager itself does not invalidate any analysis info. 279 void getAnalysisUsage(AnalysisUsage &Info) const { 280 Info.setPreservesAll(); 281 } 282 283 inline void addTopLevelPass(Pass *P) { 284 285 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) { 286 287 // P is a immutable pass and it will be managed by this 288 // top level manager. Set up analysis resolver to connect them. 289 AnalysisResolver *AR = new AnalysisResolver(*this); 290 P->setResolver(AR); 291 initializeAnalysisImpl(P); 292 addImmutablePass(IP); 293 recordAvailableAnalysis(IP); 294 } else { 295 P->assignPassManager(activeStack); 296 } 297 298 } 299 300 MPPassManager *getContainedManager(unsigned N) { 301 assert ( N < PassManagers.size() && "Pass number out of range!"); 302 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]); 303 return MP; 304 } 305 306}; 307 308char PassManagerImpl::ID = 0; 309} // End of llvm namespace 310 311namespace { 312 313//===----------------------------------------------------------------------===// 314// TimingInfo Class - This class is used to calculate information about the 315// amount of time each pass takes to execute. This only happens when 316// -time-passes is enabled on the command line. 317// 318 319class VISIBILITY_HIDDEN TimingInfo { 320 std::map<Pass*, Timer> TimingData; 321 TimerGroup TG; 322 323public: 324 // Use 'create' member to get this. 325 TimingInfo() : TG("... Pass execution timing report ...") {} 326 327 // TimingDtor - Print out information about timing information 328 ~TimingInfo() { 329 // Delete all of the timers... 330 TimingData.clear(); 331 // TimerGroup is deleted next, printing the report. 332 } 333 334 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer 335 // to a non null value (if the -time-passes option is enabled) or it leaves it 336 // null. It may be called multiple times. 337 static void createTheTimeInfo(); 338 339 void passStarted(Pass *P) { 340 341 if (dynamic_cast<PMDataManager *>(P)) 342 return; 343 344 std::map<Pass*, Timer>::iterator I = TimingData.find(P); 345 if (I == TimingData.end()) 346 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first; 347 I->second.startTimer(); 348 } 349 void passEnded(Pass *P) { 350 351 if (dynamic_cast<PMDataManager *>(P)) 352 return; 353 354 std::map<Pass*, Timer>::iterator I = TimingData.find(P); 355 assert (I != TimingData.end() && "passStarted/passEnded not nested right!"); 356 I->second.stopTimer(); 357 } 358}; 359 360static TimingInfo *TheTimeInfo; 361 362} // End of anon namespace 363 364//===----------------------------------------------------------------------===// 365// PMTopLevelManager implementation 366 367/// Initialize top level manager. Create first pass manager. 368PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) { 369 370 if (t == TLM_Pass) { 371 MPPassManager *MPP = new MPPassManager(1); 372 MPP->setTopLevelManager(this); 373 addPassManager(MPP); 374 activeStack.push(MPP); 375 } 376 else if (t == TLM_Function) { 377 FPPassManager *FPP = new FPPassManager(1); 378 FPP->setTopLevelManager(this); 379 addPassManager(FPP); 380 activeStack.push(FPP); 381 } 382} 383 384/// Set pass P as the last user of the given analysis passes. 385void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, 386 Pass *P) { 387 388 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(), 389 E = AnalysisPasses.end(); I != E; ++I) { 390 Pass *AP = *I; 391 LastUser[AP] = P; 392 393 if (P == AP) 394 continue; 395 396 // If AP is the last user of other passes then make P last user of 397 // such passes. 398 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(), 399 LUE = LastUser.end(); LUI != LUE; ++LUI) { 400 if (LUI->second == AP) 401 LastUser[LUI->first] = P; 402 } 403 } 404} 405 406/// Collect passes whose last user is P 407void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses, 408 Pass *P) { 409 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(), 410 LUE = LastUser.end(); LUI != LUE; ++LUI) 411 if (LUI->second == P) 412 LastUses.push_back(LUI->first); 413} 414 415/// Schedule pass P for execution. Make sure that passes required by 416/// P are run before P is run. Update analysis info maintained by 417/// the manager. Remove dead passes. This is a recursive function. 418void PMTopLevelManager::schedulePass(Pass *P) { 419 420 // TODO : Allocate function manager for this pass, other wise required set 421 // may be inserted into previous function manager 422 423 // Give pass a chance to prepare the stage. 424 P->preparePassManager(activeStack); 425 426 AnalysisUsage AnUsage; 427 P->getAnalysisUsage(AnUsage); 428 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); 429 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(), 430 E = RequiredSet.end(); I != E; ++I) { 431 432 Pass *AnalysisPass = findAnalysisPass(*I); 433 if (!AnalysisPass) { 434 AnalysisPass = (*I)->createPass(); 435 // Schedule this analysis run first only if it is not a lower level 436 // analysis pass. Lower level analsyis passes are run on the fly. 437 if (P->getPotentialPassManagerType () >= 438 AnalysisPass->getPotentialPassManagerType()) 439 schedulePass(AnalysisPass); 440 else 441 delete AnalysisPass; 442 } 443 } 444 445 // Now all required passes are available. 446 addTopLevelPass(P); 447} 448 449/// Find the pass that implements Analysis AID. Search immutable 450/// passes and all pass managers. If desired pass is not found 451/// then return NULL. 452Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { 453 454 Pass *P = NULL; 455 // Check pass managers 456 for (std::vector<Pass *>::iterator I = PassManagers.begin(), 457 E = PassManagers.end(); P == NULL && I != E; ++I) { 458 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I); 459 assert(PMD && "This is not a PassManager"); 460 P = PMD->findAnalysisPass(AID, false); 461 } 462 463 // Check other pass managers 464 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(), 465 E = IndirectPassManagers.end(); P == NULL && I != E; ++I) 466 P = (*I)->findAnalysisPass(AID, false); 467 468 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(), 469 E = ImmutablePasses.end(); P == NULL && I != E; ++I) { 470 const PassInfo *PI = (*I)->getPassInfo(); 471 if (PI == AID) 472 P = *I; 473 474 // If Pass not found then check the interfaces implemented by Immutable Pass 475 if (!P) { 476 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented(); 477 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end()) 478 P = *I; 479 } 480 } 481 482 return P; 483} 484 485// Print passes managed by this top level manager. 486void PMTopLevelManager::dumpPasses() const { 487 488 if (PassDebugging < Structure) 489 return; 490 491 // Print out the immutable passes 492 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { 493 ImmutablePasses[i]->dumpPassStructure(0); 494 } 495 496 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(), 497 E = PassManagers.end(); I != E; ++I) 498 (*I)->dumpPassStructure(1); 499} 500 501void PMTopLevelManager::dumpArguments() const { 502 503 if (PassDebugging < Arguments) 504 return; 505 506 cerr << "Pass Arguments: "; 507 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(), 508 E = PassManagers.end(); I != E; ++I) { 509 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I); 510 assert(PMD && "This is not a PassManager"); 511 PMD->dumpPassArguments(); 512 } 513 cerr << "\n"; 514} 515 516void PMTopLevelManager::initializeAllAnalysisInfo() { 517 518 for (std::vector<Pass *>::iterator I = PassManagers.begin(), 519 E = PassManagers.end(); I != E; ++I) { 520 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I); 521 assert(PMD && "This is not a PassManager"); 522 PMD->initializeAnalysisInfo(); 523 } 524 525 // Initailize other pass managers 526 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(), 527 E = IndirectPassManagers.end(); I != E; ++I) 528 (*I)->initializeAnalysisInfo(); 529} 530 531/// Destructor 532PMTopLevelManager::~PMTopLevelManager() { 533 for (std::vector<Pass *>::iterator I = PassManagers.begin(), 534 E = PassManagers.end(); I != E; ++I) 535 delete *I; 536 537 for (std::vector<ImmutablePass *>::iterator 538 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) 539 delete *I; 540 541 PassManagers.clear(); 542} 543 544//===----------------------------------------------------------------------===// 545// PMDataManager implementation 546 547/// Return true IFF pass P's required analysis set does not required new 548/// manager. 549bool PMDataManager::manageablePass(Pass *P) { 550 551 // TODO 552 // If this pass is not preserving information that is required by a 553 // pass maintained by higher level pass manager then do not insert 554 // this pass into current manager. Use new manager. For example, 555 // For example, If FunctionPass F is not preserving ModulePass Info M1 556 // that is used by another ModulePass M2 then do not insert F in 557 // current function pass manager. 558 return true; 559} 560 561/// Augement AvailableAnalysis by adding analysis made available by pass P. 562void PMDataManager::recordAvailableAnalysis(Pass *P) { 563 564 if (const PassInfo *PI = P->getPassInfo()) { 565 AvailableAnalysis[PI] = P; 566 567 //This pass is the current implementation of all of the interfaces it 568 //implements as well. 569 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); 570 for (unsigned i = 0, e = II.size(); i != e; ++i) 571 AvailableAnalysis[II[i]] = P; 572 } 573} 574 575// Return true if P preserves high level analysis used by other 576// passes managed by this manager 577bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) { 578 579 AnalysisUsage AnUsage; 580 P->getAnalysisUsage(AnUsage); 581 582 if (AnUsage.getPreservesAll()) 583 return true; 584 585 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); 586 for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(), 587 E = HigherLevelAnalysis.end(); I != E; ++I) { 588 Pass *P1 = *I; 589 if (!dynamic_cast<ImmutablePass*>(P1) 590 && std::find(PreservedSet.begin(), PreservedSet.end(), P1->getPassInfo()) == 591 PreservedSet.end()) 592 return false; 593 } 594 595 return true; 596} 597 598/// verifyPreservedAnalysis -- Verify analysis presreved by pass P. 599void PMDataManager::verifyPreservedAnalysis(Pass *P) { 600 AnalysisUsage AnUsage; 601 P->getAnalysisUsage(AnUsage); 602 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); 603 604 // Verify preserved analysis 605 for (std::vector<AnalysisID>::const_iterator I = PreservedSet.begin(), 606 E = PreservedSet.end(); I != E; ++I) { 607 AnalysisID AID = *I; 608 Pass *AP = findAnalysisPass(AID, true); 609 if (AP) 610 AP->verifyAnalysis(); 611 } 612} 613 614/// Remove Analyss not preserved by Pass P 615void PMDataManager::removeNotPreservedAnalysis(Pass *P) { 616 AnalysisUsage AnUsage; 617 P->getAnalysisUsage(AnUsage); 618 if (AnUsage.getPreservesAll()) 619 return; 620 621 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); 622 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), 623 E = AvailableAnalysis.end(); I != E; ) { 624 std::map<AnalysisID, Pass*>::iterator Info = I++; 625 if (!dynamic_cast<ImmutablePass*>(Info->second) 626 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == 627 PreservedSet.end()) 628 // Remove this analysis 629 AvailableAnalysis.erase(Info); 630 } 631 632 // Check inherited analysis also. If P is not preserving analysis 633 // provided by parent manager then remove it here. 634 for (unsigned Index = 0; Index < PMT_Last; ++Index) { 635 636 if (!InheritedAnalysis[Index]) 637 continue; 638 639 for (std::map<AnalysisID, Pass*>::iterator 640 I = InheritedAnalysis[Index]->begin(), 641 E = InheritedAnalysis[Index]->end(); I != E; ) { 642 std::map<AnalysisID, Pass *>::iterator Info = I++; 643 if (!dynamic_cast<ImmutablePass*>(Info->second) 644 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == 645 PreservedSet.end()) 646 // Remove this analysis 647 InheritedAnalysis[Index]->erase(Info); 648 } 649 } 650 651} 652 653/// Remove analysis passes that are not used any longer 654void PMDataManager::removeDeadPasses(Pass *P, const char *Msg, 655 enum PassDebuggingString DBG_STR) { 656 657 SmallVector<Pass *, 12> DeadPasses; 658 659 // If this is a on the fly manager then it does not have TPM. 660 if (!TPM) 661 return; 662 663 TPM->collectLastUses(DeadPasses, P); 664 665 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(), 666 E = DeadPasses.end(); I != E; ++I) { 667 668 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg); 669 670 if (TheTimeInfo) TheTimeInfo->passStarted(*I); 671 (*I)->releaseMemory(); 672 if (TheTimeInfo) TheTimeInfo->passEnded(*I); 673 674 std::map<AnalysisID, Pass*>::iterator Pos = 675 AvailableAnalysis.find((*I)->getPassInfo()); 676 677 // It is possible that pass is already removed from the AvailableAnalysis 678 if (Pos != AvailableAnalysis.end()) 679 AvailableAnalysis.erase(Pos); 680 } 681} 682 683/// Add pass P into the PassVector. Update 684/// AvailableAnalysis appropriately if ProcessAnalysis is true. 685void PMDataManager::add(Pass *P, 686 bool ProcessAnalysis) { 687 688 // This manager is going to manage pass P. Set up analysis resolver 689 // to connect them. 690 AnalysisResolver *AR = new AnalysisResolver(*this); 691 P->setResolver(AR); 692 693 // If a FunctionPass F is the last user of ModulePass info M 694 // then the F's manager, not F, records itself as a last user of M. 695 SmallVector<Pass *, 12> TransferLastUses; 696 697 if (ProcessAnalysis) { 698 699 // At the moment, this pass is the last user of all required passes. 700 SmallVector<Pass *, 12> LastUses; 701 SmallVector<Pass *, 8> RequiredPasses; 702 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable; 703 704 unsigned PDepth = this->getDepth(); 705 706 collectRequiredAnalysis(RequiredPasses, 707 ReqAnalysisNotAvailable, P); 708 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(), 709 E = RequiredPasses.end(); I != E; ++I) { 710 Pass *PRequired = *I; 711 unsigned RDepth = 0; 712 713 PMDataManager &DM = PRequired->getResolver()->getPMDataManager(); 714 RDepth = DM.getDepth(); 715 716 if (PDepth == RDepth) 717 LastUses.push_back(PRequired); 718 else if (PDepth > RDepth) { 719 // Let the parent claim responsibility of last use 720 TransferLastUses.push_back(PRequired); 721 // Keep track of higher level analysis used by this manager. 722 HigherLevelAnalysis.push_back(PRequired); 723 } else 724 assert (0 && "Unable to accomodate Required Pass"); 725 } 726 727 // Set P as P's last user until someone starts using P. 728 // However, if P is a Pass Manager then it does not need 729 // to record its last user. 730 if (!dynamic_cast<PMDataManager *>(P)) 731 LastUses.push_back(P); 732 TPM->setLastUser(LastUses, P); 733 734 if (!TransferLastUses.empty()) { 735 Pass *My_PM = dynamic_cast<Pass *>(this); 736 TPM->setLastUser(TransferLastUses, My_PM); 737 TransferLastUses.clear(); 738 } 739 740 // Now, take care of required analysises that are not available. 741 for (SmallVector<AnalysisID, 8>::iterator 742 I = ReqAnalysisNotAvailable.begin(), 743 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) { 744 Pass *AnalysisPass = (*I)->createPass(); 745 this->addLowerLevelRequiredPass(P, AnalysisPass); 746 } 747 748 // Take a note of analysis required and made available by this pass. 749 // Remove the analysis not preserved by this pass 750 removeNotPreservedAnalysis(P); 751 recordAvailableAnalysis(P); 752 } 753 754 // Add pass 755 PassVector.push_back(P); 756} 757 758 759/// Populate RP with analysis pass that are required by 760/// pass P and are available. Populate RP_NotAvail with analysis 761/// pass that are required by pass P but are not available. 762void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP, 763 SmallVector<AnalysisID, 8> &RP_NotAvail, 764 Pass *P) { 765 AnalysisUsage AnUsage; 766 P->getAnalysisUsage(AnUsage); 767 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); 768 for (std::vector<AnalysisID>::const_iterator 769 I = RequiredSet.begin(), E = RequiredSet.end(); 770 I != E; ++I) { 771 AnalysisID AID = *I; 772 if (Pass *AnalysisPass = findAnalysisPass(*I, true)) 773 RP.push_back(AnalysisPass); 774 else 775 RP_NotAvail.push_back(AID); 776 } 777 778 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet(); 779 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(), 780 E = IDs.end(); I != E; ++I) { 781 AnalysisID AID = *I; 782 if (Pass *AnalysisPass = findAnalysisPass(*I, true)) 783 RP.push_back(AnalysisPass); 784 else 785 RP_NotAvail.push_back(AID); 786 } 787} 788 789// All Required analyses should be available to the pass as it runs! Here 790// we fill in the AnalysisImpls member of the pass so that it can 791// successfully use the getAnalysis() method to retrieve the 792// implementations it needs. 793// 794void PMDataManager::initializeAnalysisImpl(Pass *P) { 795 AnalysisUsage AnUsage; 796 P->getAnalysisUsage(AnUsage); 797 798 for (std::vector<const PassInfo *>::const_iterator 799 I = AnUsage.getRequiredSet().begin(), 800 E = AnUsage.getRequiredSet().end(); I != E; ++I) { 801 Pass *Impl = findAnalysisPass(*I, true); 802 if (Impl == 0) 803 // This may be analysis pass that is initialized on the fly. 804 // If that is not the case then it will raise an assert when it is used. 805 continue; 806 AnalysisResolver *AR = P->getResolver(); 807 AR->addAnalysisImplsPair(*I, Impl); 808 } 809} 810 811/// Find the pass that implements Analysis AID. If desired pass is not found 812/// then return NULL. 813Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) { 814 815 // Check if AvailableAnalysis map has one entry. 816 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID); 817 818 if (I != AvailableAnalysis.end()) 819 return I->second; 820 821 // Search Parents through TopLevelManager 822 if (SearchParent) 823 return TPM->findAnalysisPass(AID); 824 825 return NULL; 826} 827 828// Print list of passes that are last used by P. 829void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{ 830 831 SmallVector<Pass *, 12> LUses; 832 833 // If this is a on the fly manager then it does not have TPM. 834 if (!TPM) 835 return; 836 837 TPM->collectLastUses(LUses, P); 838 839 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(), 840 E = LUses.end(); I != E; ++I) { 841 llvm::cerr << "--" << std::string(Offset*2, ' '); 842 (*I)->dumpPassStructure(0); 843 } 844} 845 846void PMDataManager::dumpPassArguments() const { 847 for(std::vector<Pass *>::const_iterator I = PassVector.begin(), 848 E = PassVector.end(); I != E; ++I) { 849 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I)) 850 PMD->dumpPassArguments(); 851 else 852 if (const PassInfo *PI = (*I)->getPassInfo()) 853 if (!PI->isAnalysisGroup()) 854 cerr << " -" << PI->getPassArgument(); 855 } 856} 857 858void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1, 859 enum PassDebuggingString S2, 860 const char *Msg) { 861 if (PassDebugging < Executions) 862 return; 863 cerr << (void*)this << std::string(getDepth()*2+1, ' '); 864 switch (S1) { 865 case EXECUTION_MSG: 866 cerr << "Executing Pass '" << P->getPassName(); 867 break; 868 case MODIFICATION_MSG: 869 cerr << "Made Modification '" << P->getPassName(); 870 break; 871 case FREEING_MSG: 872 cerr << " Freeing Pass '" << P->getPassName(); 873 break; 874 default: 875 break; 876 } 877 switch (S2) { 878 case ON_BASICBLOCK_MSG: 879 cerr << "' on BasicBlock '" << Msg << "'...\n"; 880 break; 881 case ON_FUNCTION_MSG: 882 cerr << "' on Function '" << Msg << "'...\n"; 883 break; 884 case ON_MODULE_MSG: 885 cerr << "' on Module '" << Msg << "'...\n"; 886 break; 887 case ON_LOOP_MSG: 888 cerr << "' on Loop " << Msg << "'...\n"; 889 break; 890 case ON_CG_MSG: 891 cerr << "' on Call Graph " << Msg << "'...\n"; 892 break; 893 default: 894 break; 895 } 896} 897 898void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P, 899 const std::vector<AnalysisID> &Set) 900 const { 901 if (PassDebugging >= Details && !Set.empty()) { 902 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:"; 903 for (unsigned i = 0; i != Set.size(); ++i) { 904 if (i) cerr << ","; 905 cerr << " " << Set[i]->getPassName(); 906 } 907 cerr << "\n"; 908 } 909} 910 911/// Add RequiredPass into list of lower level passes required by pass P. 912/// RequiredPass is run on the fly by Pass Manager when P requests it 913/// through getAnalysis interface. 914/// This should be handled by specific pass manager. 915void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { 916 if (TPM) { 917 TPM->dumpArguments(); 918 TPM->dumpPasses(); 919 } 920 assert (0 && "Unable to handle Pass that requires lower level Analysis pass"); 921} 922 923// Destructor 924PMDataManager::~PMDataManager() { 925 926 for (std::vector<Pass *>::iterator I = PassVector.begin(), 927 E = PassVector.end(); I != E; ++I) 928 delete *I; 929 930 PassVector.clear(); 931} 932 933//===----------------------------------------------------------------------===// 934// NOTE: Is this the right place to define this method ? 935// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist 936Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const { 937 return PM.findAnalysisPass(ID, dir); 938} 939 940Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI, 941 Function &F) { 942 return PM.getOnTheFlyPass(P, AnalysisPI, F); 943} 944 945//===----------------------------------------------------------------------===// 946// BBPassManager implementation 947 948/// Execute all of the passes scheduled for execution by invoking 949/// runOnBasicBlock method. Keep track of whether any of the passes modifies 950/// the function, and if so, return true. 951bool 952BBPassManager::runOnFunction(Function &F) { 953 954 if (F.isDeclaration()) 955 return false; 956 957 bool Changed = doInitialization(F); 958 959 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) 960 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 961 BasicBlockPass *BP = getContainedPass(Index); 962 AnalysisUsage AnUsage; 963 BP->getAnalysisUsage(AnUsage); 964 965 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart()); 966 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet()); 967 968 initializeAnalysisImpl(BP); 969 970 if (TheTimeInfo) TheTimeInfo->passStarted(BP); 971 Changed |= BP->runOnBasicBlock(*I); 972 if (TheTimeInfo) TheTimeInfo->passEnded(BP); 973 974 if (Changed) 975 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, I->getNameStart()); 976 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet()); 977 978 verifyPreservedAnalysis(BP); 979 removeNotPreservedAnalysis(BP); 980 recordAvailableAnalysis(BP); 981 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG); 982 } 983 984 return Changed |= doFinalization(F); 985} 986 987// Implement doInitialization and doFinalization 988inline bool BBPassManager::doInitialization(Module &M) { 989 bool Changed = false; 990 991 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 992 BasicBlockPass *BP = getContainedPass(Index); 993 Changed |= BP->doInitialization(M); 994 } 995 996 return Changed; 997} 998 999inline bool BBPassManager::doFinalization(Module &M) { 1000 bool Changed = false; 1001 1002 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 1003 BasicBlockPass *BP = getContainedPass(Index); 1004 Changed |= BP->doFinalization(M); 1005 } 1006 1007 return Changed; 1008} 1009 1010inline bool BBPassManager::doInitialization(Function &F) { 1011 bool Changed = false; 1012 1013 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 1014 BasicBlockPass *BP = getContainedPass(Index); 1015 Changed |= BP->doInitialization(F); 1016 } 1017 1018 return Changed; 1019} 1020 1021inline bool BBPassManager::doFinalization(Function &F) { 1022 bool Changed = false; 1023 1024 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 1025 BasicBlockPass *BP = getContainedPass(Index); 1026 Changed |= BP->doFinalization(F); 1027 } 1028 1029 return Changed; 1030} 1031 1032 1033//===----------------------------------------------------------------------===// 1034// FunctionPassManager implementation 1035 1036/// Create new Function pass manager 1037FunctionPassManager::FunctionPassManager(ModuleProvider *P) { 1038 FPM = new FunctionPassManagerImpl(0); 1039 // FPM is the top level manager. 1040 FPM->setTopLevelManager(FPM); 1041 1042 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM); 1043 AnalysisResolver *AR = new AnalysisResolver(*PMD); 1044 FPM->setResolver(AR); 1045 1046 MP = P; 1047} 1048 1049FunctionPassManager::~FunctionPassManager() { 1050 delete FPM; 1051} 1052 1053/// add - Add a pass to the queue of passes to run. This passes 1054/// ownership of the Pass to the PassManager. When the 1055/// PassManager_X is destroyed, the pass will be destroyed as well, so 1056/// there is no need to delete the pass. (TODO delete passes.) 1057/// This implies that all passes MUST be allocated with 'new'. 1058void FunctionPassManager::add(Pass *P) { 1059 FPM->add(P); 1060} 1061 1062/// run - Execute all of the passes scheduled for execution. Keep 1063/// track of whether any of the passes modifies the function, and if 1064/// so, return true. 1065/// 1066bool FunctionPassManager::run(Function &F) { 1067 std::string errstr; 1068 if (MP->materializeFunction(&F, &errstr)) { 1069 cerr << "Error reading bitcode file: " << errstr << "\n"; 1070 abort(); 1071 } 1072 return FPM->run(F); 1073} 1074 1075 1076/// doInitialization - Run all of the initializers for the function passes. 1077/// 1078bool FunctionPassManager::doInitialization() { 1079 return FPM->doInitialization(*MP->getModule()); 1080} 1081 1082/// doFinalization - Run all of the finalizers for the function passes. 1083/// 1084bool FunctionPassManager::doFinalization() { 1085 return FPM->doFinalization(*MP->getModule()); 1086} 1087 1088//===----------------------------------------------------------------------===// 1089// FunctionPassManagerImpl implementation 1090// 1091inline bool FunctionPassManagerImpl::doInitialization(Module &M) { 1092 bool Changed = false; 1093 1094 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { 1095 FPPassManager *FP = getContainedManager(Index); 1096 Changed |= FP->doInitialization(M); 1097 } 1098 1099 return Changed; 1100} 1101 1102inline bool FunctionPassManagerImpl::doFinalization(Module &M) { 1103 bool Changed = false; 1104 1105 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { 1106 FPPassManager *FP = getContainedManager(Index); 1107 Changed |= FP->doFinalization(M); 1108 } 1109 1110 return Changed; 1111} 1112 1113// Execute all the passes managed by this top level manager. 1114// Return true if any function is modified by a pass. 1115bool FunctionPassManagerImpl::run(Function &F) { 1116 1117 bool Changed = false; 1118 1119 TimingInfo::createTheTimeInfo(); 1120 1121 dumpArguments(); 1122 dumpPasses(); 1123 1124 initializeAllAnalysisInfo(); 1125 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { 1126 FPPassManager *FP = getContainedManager(Index); 1127 Changed |= FP->runOnFunction(F); 1128 } 1129 return Changed; 1130} 1131 1132//===----------------------------------------------------------------------===// 1133// FPPassManager implementation 1134 1135char FPPassManager::ID = 0; 1136/// Print passes managed by this manager 1137void FPPassManager::dumpPassStructure(unsigned Offset) { 1138 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n"; 1139 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 1140 FunctionPass *FP = getContainedPass(Index); 1141 FP->dumpPassStructure(Offset + 1); 1142 dumpLastUses(FP, Offset+1); 1143 } 1144} 1145 1146 1147/// Execute all of the passes scheduled for execution by invoking 1148/// runOnFunction method. Keep track of whether any of the passes modifies 1149/// the function, and if so, return true. 1150bool FPPassManager::runOnFunction(Function &F) { 1151 1152 bool Changed = false; 1153 1154 if (F.isDeclaration()) 1155 return false; 1156 1157 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 1158 FunctionPass *FP = getContainedPass(Index); 1159 1160 AnalysisUsage AnUsage; 1161 FP->getAnalysisUsage(AnUsage); 1162 1163 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart()); 1164 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet()); 1165 1166 initializeAnalysisImpl(FP); 1167 1168 if (TheTimeInfo) TheTimeInfo->passStarted(FP); 1169 Changed |= FP->runOnFunction(F); 1170 if (TheTimeInfo) TheTimeInfo->passEnded(FP); 1171 1172 if (Changed) 1173 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart()); 1174 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet()); 1175 1176 verifyPreservedAnalysis(FP); 1177 removeNotPreservedAnalysis(FP); 1178 recordAvailableAnalysis(FP); 1179 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG); 1180 } 1181 return Changed; 1182} 1183 1184bool FPPassManager::runOnModule(Module &M) { 1185 1186 bool Changed = doInitialization(M); 1187 1188 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 1189 this->runOnFunction(*I); 1190 1191 return Changed |= doFinalization(M); 1192} 1193 1194inline bool FPPassManager::doInitialization(Module &M) { 1195 bool Changed = false; 1196 1197 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 1198 FunctionPass *FP = getContainedPass(Index); 1199 Changed |= FP->doInitialization(M); 1200 } 1201 1202 return Changed; 1203} 1204 1205inline bool FPPassManager::doFinalization(Module &M) { 1206 bool Changed = false; 1207 1208 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 1209 FunctionPass *FP = getContainedPass(Index); 1210 Changed |= FP->doFinalization(M); 1211 } 1212 1213 return Changed; 1214} 1215 1216//===----------------------------------------------------------------------===// 1217// MPPassManager implementation 1218 1219/// Execute all of the passes scheduled for execution by invoking 1220/// runOnModule method. Keep track of whether any of the passes modifies 1221/// the module, and if so, return true. 1222bool 1223MPPassManager::runOnModule(Module &M) { 1224 bool Changed = false; 1225 1226 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 1227 ModulePass *MP = getContainedPass(Index); 1228 1229 AnalysisUsage AnUsage; 1230 MP->getAnalysisUsage(AnUsage); 1231 1232 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier().c_str()); 1233 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet()); 1234 1235 initializeAnalysisImpl(MP); 1236 1237 if (TheTimeInfo) TheTimeInfo->passStarted(MP); 1238 Changed |= MP->runOnModule(M); 1239 if (TheTimeInfo) TheTimeInfo->passEnded(MP); 1240 1241 if (Changed) 1242 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG, M.getModuleIdentifier().c_str()); 1243 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet()); 1244 1245 verifyPreservedAnalysis(MP); 1246 removeNotPreservedAnalysis(MP); 1247 recordAvailableAnalysis(MP); 1248 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG); 1249 } 1250 return Changed; 1251} 1252 1253/// Add RequiredPass into list of lower level passes required by pass P. 1254/// RequiredPass is run on the fly by Pass Manager when P requests it 1255/// through getAnalysis interface. 1256void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { 1257 1258 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager 1259 && "Unable to handle Pass that requires lower level Analysis pass"); 1260 assert ((P->getPotentialPassManagerType() < 1261 RequiredPass->getPotentialPassManagerType()) 1262 && "Unable to handle Pass that requires lower level Analysis pass"); 1263 1264 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P]; 1265 if (!FPP) { 1266 FPP = new FunctionPassManagerImpl(0); 1267 // FPP is the top level manager. 1268 FPP->setTopLevelManager(FPP); 1269 1270 OnTheFlyManagers[P] = FPP; 1271 } 1272 FPP->add(RequiredPass); 1273 1274 // Register P as the last user of RequiredPass. 1275 SmallVector<Pass *, 12> LU; 1276 LU.push_back(RequiredPass); 1277 FPP->setLastUser(LU, P); 1278} 1279 1280/// Return function pass corresponding to PassInfo PI, that is 1281/// required by module pass MP. Instantiate analysis pass, by using 1282/// its runOnFunction() for function F. 1283Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, 1284 Function &F) { 1285 AnalysisID AID = PI; 1286 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP]; 1287 assert (FPP && "Unable to find on the fly pass"); 1288 1289 FPP->run(F); 1290 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID); 1291} 1292 1293 1294//===----------------------------------------------------------------------===// 1295// PassManagerImpl implementation 1296// 1297/// run - Execute all of the passes scheduled for execution. Keep track of 1298/// whether any of the passes modifies the module, and if so, return true. 1299bool PassManagerImpl::run(Module &M) { 1300 1301 bool Changed = false; 1302 1303 TimingInfo::createTheTimeInfo(); 1304 1305 dumpArguments(); 1306 dumpPasses(); 1307 1308 initializeAllAnalysisInfo(); 1309 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { 1310 MPPassManager *MP = getContainedManager(Index); 1311 Changed |= MP->runOnModule(M); 1312 } 1313 return Changed; 1314} 1315 1316//===----------------------------------------------------------------------===// 1317// PassManager implementation 1318 1319/// Create new pass manager 1320PassManager::PassManager() { 1321 PM = new PassManagerImpl(0); 1322 // PM is the top level manager 1323 PM->setTopLevelManager(PM); 1324} 1325 1326PassManager::~PassManager() { 1327 delete PM; 1328} 1329 1330/// add - Add a pass to the queue of passes to run. This passes ownership of 1331/// the Pass to the PassManager. When the PassManager is destroyed, the pass 1332/// will be destroyed as well, so there is no need to delete the pass. This 1333/// implies that all passes MUST be allocated with 'new'. 1334void 1335PassManager::add(Pass *P) { 1336 PM->add(P); 1337} 1338 1339/// run - Execute all of the passes scheduled for execution. Keep track of 1340/// whether any of the passes modifies the module, and if so, return true. 1341bool 1342PassManager::run(Module &M) { 1343 return PM->run(M); 1344} 1345 1346//===----------------------------------------------------------------------===// 1347// TimingInfo Class - This class is used to calculate information about the 1348// amount of time each pass takes to execute. This only happens with 1349// -time-passes is enabled on the command line. 1350// 1351bool llvm::TimePassesIsEnabled = false; 1352static cl::opt<bool,true> 1353EnableTiming("time-passes", cl::location(TimePassesIsEnabled), 1354 cl::desc("Time each pass, printing elapsed time for each on exit")); 1355 1356// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to 1357// a non null value (if the -time-passes option is enabled) or it leaves it 1358// null. It may be called multiple times. 1359void TimingInfo::createTheTimeInfo() { 1360 if (!TimePassesIsEnabled || TheTimeInfo) return; 1361 1362 // Constructed the first time this is called, iff -time-passes is enabled. 1363 // This guarantees that the object will be constructed before static globals, 1364 // thus it will be destroyed before them. 1365 static ManagedStatic<TimingInfo> TTI; 1366 TheTimeInfo = &*TTI; 1367} 1368 1369/// If TimingInfo is enabled then start pass timer. 1370void StartPassTimer(Pass *P) { 1371 if (TheTimeInfo) 1372 TheTimeInfo->passStarted(P); 1373} 1374 1375/// If TimingInfo is enabled then stop pass timer. 1376void StopPassTimer(Pass *P) { 1377 if (TheTimeInfo) 1378 TheTimeInfo->passEnded(P); 1379} 1380 1381//===----------------------------------------------------------------------===// 1382// PMStack implementation 1383// 1384 1385// Pop Pass Manager from the stack and clear its analysis info. 1386void PMStack::pop() { 1387 1388 PMDataManager *Top = this->top(); 1389 Top->initializeAnalysisInfo(); 1390 1391 S.pop_back(); 1392} 1393 1394// Push PM on the stack and set its top level manager. 1395void PMStack::push(Pass *P) { 1396 1397 PMDataManager *Top = NULL; 1398 PMDataManager *PM = dynamic_cast<PMDataManager *>(P); 1399 assert (PM && "Unable to push. Pass Manager expected"); 1400 1401 if (this->empty()) { 1402 Top = PM; 1403 } 1404 else { 1405 Top = this->top(); 1406 PMTopLevelManager *TPM = Top->getTopLevelManager(); 1407 1408 assert (TPM && "Unable to find top level manager"); 1409 TPM->addIndirectPassManager(PM); 1410 PM->setTopLevelManager(TPM); 1411 } 1412 1413 S.push_back(PM); 1414} 1415 1416// Dump content of the pass manager stack. 1417void PMStack::dump() { 1418 for(std::deque<PMDataManager *>::iterator I = S.begin(), 1419 E = S.end(); I != E; ++I) { 1420 Pass *P = dynamic_cast<Pass *>(*I); 1421 printf("%s ", P->getPassName()); 1422 } 1423 if (!S.empty()) 1424 printf("\n"); 1425} 1426 1427/// Find appropriate Module Pass Manager in the PM Stack and 1428/// add self into that manager. 1429void ModulePass::assignPassManager(PMStack &PMS, 1430 PassManagerType PreferredType) { 1431 1432 // Find Module Pass Manager 1433 while(!PMS.empty()) { 1434 PassManagerType TopPMType = PMS.top()->getPassManagerType(); 1435 if (TopPMType == PreferredType) 1436 break; // We found desired pass manager 1437 else if (TopPMType > PMT_ModulePassManager) 1438 PMS.pop(); // Pop children pass managers 1439 else 1440 break; 1441 } 1442 1443 PMS.top()->add(this); 1444} 1445 1446/// Find appropriate Function Pass Manager or Call Graph Pass Manager 1447/// in the PM Stack and add self into that manager. 1448void FunctionPass::assignPassManager(PMStack &PMS, 1449 PassManagerType PreferredType) { 1450 1451 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager) 1452 while(!PMS.empty()) { 1453 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager) 1454 PMS.pop(); 1455 else 1456 break; 1457 } 1458 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top()); 1459 1460 // Create new Function Pass Manager 1461 if (!FPP) { 1462 assert(!PMS.empty() && "Unable to create Function Pass Manager"); 1463 PMDataManager *PMD = PMS.top(); 1464 1465 // [1] Create new Function Pass Manager 1466 FPP = new FPPassManager(PMD->getDepth() + 1); 1467 1468 // [2] Set up new manager's top level manager 1469 PMTopLevelManager *TPM = PMD->getTopLevelManager(); 1470 TPM->addIndirectPassManager(FPP); 1471 1472 // [3] Assign manager to manage this new manager. This may create 1473 // and push new managers into PMS 1474 Pass *P = dynamic_cast<Pass *>(FPP); 1475 1476 // If Call Graph Pass Manager is active then use it to manage 1477 // this new Function Pass manager. 1478 if (PMD->getPassManagerType() == PMT_CallGraphPassManager) 1479 P->assignPassManager(PMS, PMT_CallGraphPassManager); 1480 else 1481 P->assignPassManager(PMS); 1482 1483 // [4] Push new manager into PMS 1484 PMS.push(FPP); 1485 } 1486 1487 // Assign FPP as the manager of this pass. 1488 FPP->add(this); 1489} 1490 1491/// Find appropriate Basic Pass Manager or Call Graph Pass Manager 1492/// in the PM Stack and add self into that manager. 1493void BasicBlockPass::assignPassManager(PMStack &PMS, 1494 PassManagerType PreferredType) { 1495 1496 BBPassManager *BBP = NULL; 1497 1498 // Basic Pass Manager is a leaf pass manager. It does not handle 1499 // any other pass manager. 1500 if (!PMS.empty()) 1501 BBP = dynamic_cast<BBPassManager *>(PMS.top()); 1502 1503 // If leaf manager is not Basic Block Pass manager then create new 1504 // basic Block Pass manager. 1505 1506 if (!BBP) { 1507 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager"); 1508 PMDataManager *PMD = PMS.top(); 1509 1510 // [1] Create new Basic Block Manager 1511 BBP = new BBPassManager(PMD->getDepth() + 1); 1512 1513 // [2] Set up new manager's top level manager 1514 // Basic Block Pass Manager does not live by itself 1515 PMTopLevelManager *TPM = PMD->getTopLevelManager(); 1516 TPM->addIndirectPassManager(BBP); 1517 1518 // [3] Assign manager to manage this new manager. This may create 1519 // and push new managers into PMS 1520 Pass *P = dynamic_cast<Pass *>(BBP); 1521 P->assignPassManager(PMS); 1522 1523 // [4] Push new manager into PMS 1524 PMS.push(BBP); 1525 } 1526 1527 // Assign BBP as the manager of this pass. 1528 BBP->add(this); 1529} 1530 1531 1532