PassManager.cpp revision 4045af1175509c81d6a4708df233f0b1abe888fc
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/PassManager.h" 16#include "llvm/Module.h" 17#include "llvm/ModuleProvider.h" 18#include "llvm/Support/Streams.h" 19#include <vector> 20#include <map> 21using namespace llvm; 22 23//===----------------------------------------------------------------------===// 24// Overview: 25// The Pass Manager Infrastructure manages passes. It's responsibilities are: 26// 27// o Manage optimization pass execution order 28// o Make required Analysis information available before pass P is run 29// o Release memory occupied by dead passes 30// o If Analysis information is dirtied by a pass then regenerate Analysis 31// information before it is consumed by another pass. 32// 33// Pass Manager Infrastructure uses multipe pass managers. They are PassManager, 34// FunctionPassManager, ModulePassManager, BasicBlockPassManager. This class 35// hierarcy uses multiple inheritance but pass managers do not derive from 36// another pass manager. 37// 38// PassManager and FunctionPassManager are two top level pass manager that 39// represents the external interface of this entire pass manager infrastucture. 40// 41// Important classes : 42// 43// [o] class PMTopLevelManager; 44// 45// Two top level managers, PassManager and FunctionPassManager, derive from 46// PMTopLevelManager. PMTopLevelManager manages information used by top level 47// managers such as last user info. 48// 49// [o] class PMDataManager; 50// 51// PMDataManager manages information, e.g. list of available analysis info, 52// used by a pass manager to manage execution order of passes. It also provides 53// a place to implement common pass manager APIs. All pass managers derive from 54// PMDataManager. 55// 56// [o] class BasicBlockPassManager : public FunctionPass, public PMDataManager; 57// 58// BasicBlockPassManager manages BasicBlockPasses. 59// 60// [o] class FunctionPassManager; 61// 62// This is a external interface used by JIT to manage FunctionPasses. This 63// interface relies on FunctionPassManagerImpl to do all the tasks. 64// 65// [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager, 66// public PMTopLevelManager; 67// 68// FunctionPassManagerImpl is a top level manager. It manages FunctionPasses 69// and BasicBlockPassManagers. 70// 71// [o] class ModulePassManager : public Pass, public PMDataManager; 72// 73// ModulePassManager manages ModulePasses and FunctionPassManagerImpls. 74// 75// [o] class PassManager; 76// 77// This is a external interface used by various tools to manages passes. It 78// relies on PassManagerImpl to do all the tasks. 79// 80// [o] class PassManagerImpl : public Pass, public PMDataManager, 81// public PMDTopLevelManager 82// 83// PassManagerImpl is a top level pass manager responsible for managing 84// ModulePassManagers. 85//===----------------------------------------------------------------------===// 86 87namespace llvm { 88 89/// PMDataManager provides the common place to manage the analysis data 90/// used by pass managers. 91class PMDataManager { 92 93public: 94 95 /// Return true IFF pass P's required analysis set does not required new 96 /// manager. 97 bool manageablePass(Pass *P); 98 99 Pass *getAnalysisPass(AnalysisID AID) const { 100 101 std::map<AnalysisID, Pass*>::const_iterator I = 102 AvailableAnalysis.find(AID); 103 104 if (I != AvailableAnalysis.end()) 105 return NULL; 106 else 107 return I->second; 108 } 109 110 /// Augment AvailableAnalysis by adding analysis made available by pass P. 111 void noteDownAvailableAnalysis(Pass *P); 112 113 /// Remove Analysis that is not preserved by the pass 114 void removeNotPreservedAnalysis(Pass *P); 115 116 /// Remove dead passes 117 void removeDeadPasses(Pass *P); 118 119 /// Add pass P into the PassVector. Update 120 /// AvailableAnalysis appropriately if ProcessAnalysis is true. 121 void addPassToManager (Pass *P, bool ProcessAnalysis = true); 122 123 // Initialize available analysis information. 124 void initializeAnalysisInfo() { 125 AvailableAnalysis.clear(); 126 LastUser.clear(); 127 } 128 129 // All Required analyses should be available to the pass as it runs! Here 130 // we fill in the AnalysisImpls member of the pass so that it can 131 // successfully use the getAnalysis() method to retrieve the 132 // implementations it needs. 133 // 134 void initializeAnalysisImpl(Pass *P); 135 136 inline std::vector<Pass *>::iterator passVectorBegin() { 137 return PassVector.begin(); 138 } 139 140 inline std::vector<Pass *>::iterator passVectorEnd() { 141 return PassVector.end(); 142 } 143 144 inline void setLastUser(Pass *P, Pass *LU) { 145 LastUser[P] = LU; 146 // TODO : Check if pass P is available. 147 } 148 149private: 150 // Set of available Analysis. This information is used while scheduling 151 // pass. If a pass requires an analysis which is not not available then 152 // equired analysis pass is scheduled to run before the pass itself is 153 // scheduled to run. 154 std::map<AnalysisID, Pass*> AvailableAnalysis; 155 156 // Map to keep track of last user of the analysis pass. 157 // LastUser->second is the last user of Lastuser->first. 158 std::map<Pass *, Pass *> LastUser; 159 160 // Collection of pass that are managed by this manager 161 std::vector<Pass *> PassVector; 162}; 163 164/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the 165/// pass together and sequence them to process one basic block before 166/// processing next basic block. 167class BasicBlockPassManager_New : public PMDataManager, 168 public FunctionPass { 169 170public: 171 BasicBlockPassManager_New() { } 172 173 /// Add a pass into a passmanager queue. 174 bool addPass(Pass *p); 175 176 /// Execute all of the passes scheduled for execution. Keep track of 177 /// whether any of the passes modifies the function, and if so, return true. 178 bool runOnFunction(Function &F); 179 180 /// Return true IFF AnalysisID AID is currently available. 181 Pass *getAnalysisPassFromManager(AnalysisID AID); 182 183private: 184}; 185 186/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers. 187/// It batches all function passes and basic block pass managers together and 188/// sequence them to process one function at a time before processing next 189/// function. 190class FunctionPassManagerImpl_New : public PMDataManager, 191 public ModulePass { 192public: 193 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ } 194 FunctionPassManagerImpl_New() { 195 activeBBPassManager = NULL; 196 } 197 ~FunctionPassManagerImpl_New() { /* TODO */ }; 198 199 /// add - Add a pass to the queue of passes to run. This passes 200 /// ownership of the Pass to the PassManager. When the 201 /// PassManager_X is destroyed, the pass will be destroyed as well, so 202 /// there is no need to delete the pass. (TODO delete passes.) 203 /// This implies that all passes MUST be allocated with 'new'. 204 void add(Pass *P) { /* TODO*/ } 205 206 /// Add pass into the pass manager queue. 207 bool addPass(Pass *P); 208 209 /// Execute all of the passes scheduled for execution. Keep 210 /// track of whether any of the passes modifies the function, and if 211 /// so, return true. 212 bool runOnModule(Module &M); 213 bool runOnFunction(Function &F); 214 215 /// Return true IFF AnalysisID AID is currently available. 216 Pass *getAnalysisPassFromManager(AnalysisID AID); 217 218 /// doInitialization - Run all of the initializers for the function passes. 219 /// 220 bool doInitialization(Module &M); 221 222 /// doFinalization - Run all of the initializers for the function passes. 223 /// 224 bool doFinalization(Module &M); 225private: 226 // Active Pass Managers 227 BasicBlockPassManager_New *activeBBPassManager; 228}; 229 230/// ModulePassManager_New manages ModulePasses and function pass managers. 231/// It batches all Module passes passes and function pass managers together and 232/// sequence them to process one module. 233class ModulePassManager_New : public PMDataManager { 234 235public: 236 ModulePassManager_New() { activeFunctionPassManager = NULL; } 237 238 /// Add a pass into a passmanager queue. 239 bool addPass(Pass *p); 240 241 /// run - Execute all of the passes scheduled for execution. Keep track of 242 /// whether any of the passes modifies the module, and if so, return true. 243 bool runOnModule(Module &M); 244 245 /// Return true IFF AnalysisID AID is currently available. 246 Pass *getAnalysisPassFromManager(AnalysisID AID); 247 248private: 249 // Active Pass Manager 250 FunctionPassManagerImpl_New *activeFunctionPassManager; 251}; 252 253/// PassManager_New manages ModulePassManagers 254class PassManagerImpl_New : public PMDataManager { 255 256public: 257 258 /// add - Add a pass to the queue of passes to run. This passes ownership of 259 /// the Pass to the PassManager. When the PassManager is destroyed, the pass 260 /// will be destroyed as well, so there is no need to delete the pass. This 261 /// implies that all passes MUST be allocated with 'new'. 262 void add(Pass *P); 263 264 /// run - Execute all of the passes scheduled for execution. Keep track of 265 /// whether any of the passes modifies the module, and if so, return true. 266 bool run(Module &M); 267 268 /// Return true IFF AnalysisID AID is currently available. 269 Pass *getAnalysisPassFromManager(AnalysisID AID); 270 271private: 272 273 /// Add a pass into a passmanager queue. This is used by schedulePasses 274 bool addPass(Pass *p); 275 276 /// Schedule pass P for execution. Make sure that passes required by 277 /// P are run before P is run. Update analysis info maintained by 278 /// the manager. Remove dead passes. This is a recursive function. 279 void schedulePass(Pass *P); 280 281 /// Schedule all passes collected in pass queue using add(). Add all the 282 /// schedule passes into various manager's queue using addPass(). 283 void schedulePasses(); 284 285 // Collection of pass managers 286 std::vector<ModulePassManager_New *> PassManagers; 287 288 // Active Pass Manager 289 ModulePassManager_New *activeManager; 290}; 291 292} // End of llvm namespace 293 294// PMDataManager implementation 295 296/// Return true IFF pass P's required analysis set does not required new 297/// manager. 298bool PMDataManager::manageablePass(Pass *P) { 299 300 // TODO 301 // If this pass is not preserving information that is required by a 302 // pass maintained by higher level pass manager then do not insert 303 // this pass into current manager. Use new manager. For example, 304 // For example, If FunctionPass F is not preserving ModulePass Info M1 305 // that is used by another ModulePass M2 then do not insert F in 306 // current function pass manager. 307 return true; 308} 309 310/// Augement AvailableAnalysis by adding analysis made available by pass P. 311void PMDataManager::noteDownAvailableAnalysis(Pass *P) { 312 313 if (const PassInfo *PI = P->getPassInfo()) { 314 AvailableAnalysis[PI] = P; 315 316 //TODO This pass is the current implementation of all of the interfaces it 317 //TODO implements as well. 318 //TODO 319 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); 320 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i) 321 //TODO CurrentAnalyses[II[i]] = P; 322 } 323} 324 325/// Remove Analyss not preserved by Pass P 326void PMDataManager::removeNotPreservedAnalysis(Pass *P) { 327 AnalysisUsage AnUsage; 328 P->getAnalysisUsage(AnUsage); 329 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); 330 331 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), 332 E = AvailableAnalysis.end(); I != E; ++I ) { 333 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) == 334 PreservedSet.end()) { 335 // Remove this analysis 336 std::map<AnalysisID, Pass*>::iterator J = I++; 337 AvailableAnalysis.erase(J); 338 } 339 } 340} 341 342/// Remove analysis passes that are not used any longer 343void PMDataManager::removeDeadPasses(Pass *P) { 344 345 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(), 346 E = LastUser.end(); I !=E; ++I) { 347 if (I->second == P) { 348 Pass *deadPass = I->first; 349 deadPass->releaseMemory(); 350 351 std::map<AnalysisID, Pass*>::iterator Pos = 352 AvailableAnalysis.find(deadPass->getPassInfo()); 353 354 assert (Pos != AvailableAnalysis.end() && 355 "Pass is not available"); 356 AvailableAnalysis.erase(Pos); 357 } 358 } 359} 360 361/// Add pass P into the PassVector. Update 362/// AvailableAnalysis appropriately if ProcessAnalysis is true. 363void PMDataManager::addPassToManager (Pass *P, 364 bool ProcessAnalysis) { 365 366 if (ProcessAnalysis) { 367 // Take a note of analysis required and made available by this pass 368 initializeAnalysisImpl(P); 369 noteDownAvailableAnalysis(P); 370 371 // Remove the analysis not preserved by this pass 372 removeNotPreservedAnalysis(P); 373 } 374 375 // Add pass 376 PassVector.push_back(P); 377} 378 379// All Required analyses should be available to the pass as it runs! Here 380// we fill in the AnalysisImpls member of the pass so that it can 381// successfully use the getAnalysis() method to retrieve the 382// implementations it needs. 383// 384void PMDataManager::initializeAnalysisImpl(Pass *P) { 385 AnalysisUsage AnUsage; 386 P->getAnalysisUsage(AnUsage); 387 388 for (std::vector<const PassInfo *>::const_iterator 389 I = AnUsage.getRequiredSet().begin(), 390 E = AnUsage.getRequiredSet().end(); I != E; ++I) { 391 Pass *Impl = getAnalysisPass(*I); 392 if (Impl == 0) 393 assert(0 && "Analysis used but not available!"); 394 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl)); 395 } 396} 397 398/// BasicBlockPassManager implementation 399 400/// Add pass P into PassVector and return true. If this pass is not 401/// manageable by this manager then return false. 402bool 403BasicBlockPassManager_New::addPass(Pass *P) { 404 405 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); 406 if (!BP) 407 return false; 408 409 // If this pass does not preserve anlysis that is used by other passes 410 // managed by this manager than it is not a suiable pass for this manager. 411 if (!manageablePass(P)) 412 return false; 413 414 addPassToManager (BP); 415 416 return true; 417} 418 419/// Execute all of the passes scheduled for execution by invoking 420/// runOnBasicBlock method. Keep track of whether any of the passes modifies 421/// the function, and if so, return true. 422bool 423BasicBlockPassManager_New::runOnFunction(Function &F) { 424 425 bool Changed = false; 426 initializeAnalysisInfo(); 427 428 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) 429 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 430 e = passVectorEnd(); itr != e; ++itr) { 431 Pass *P = *itr; 432 433 noteDownAvailableAnalysis(P); 434 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); 435 Changed |= BP->runOnBasicBlock(*I); 436 removeNotPreservedAnalysis(P); 437 removeDeadPasses(P); 438 } 439 return Changed; 440} 441 442/// Return true IFF AnalysisID AID is currently available. 443Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) { 444 return getAnalysisPass(AID); 445} 446 447// FunctionPassManager_New implementation 448/// Create new Function pass manager 449FunctionPassManager_New::FunctionPassManager_New() { 450 FPM = new FunctionPassManagerImpl_New(); 451} 452 453/// add - Add a pass to the queue of passes to run. This passes 454/// ownership of the Pass to the PassManager. When the 455/// PassManager_X is destroyed, the pass will be destroyed as well, so 456/// there is no need to delete the pass. (TODO delete passes.) 457/// This implies that all passes MUST be allocated with 'new'. 458void FunctionPassManager_New::add(Pass *P) { 459 FPM->add(P); 460} 461 462/// Execute all of the passes scheduled for execution. Keep 463/// track of whether any of the passes modifies the function, and if 464/// so, return true. 465bool FunctionPassManager_New::runOnModule(Module &M) { 466 return FPM->runOnModule(M); 467} 468 469/// run - Execute all of the passes scheduled for execution. Keep 470/// track of whether any of the passes modifies the function, and if 471/// so, return true. 472/// 473bool FunctionPassManager_New::run(Function &F) { 474 std::string errstr; 475 if (MP->materializeFunction(&F, &errstr)) { 476 cerr << "Error reading bytecode file: " << errstr << "\n"; 477 abort(); 478 } 479 return FPM->runOnFunction(F); 480} 481 482 483/// doInitialization - Run all of the initializers for the function passes. 484/// 485bool FunctionPassManager_New::doInitialization() { 486 return FPM->doInitialization(*MP->getModule()); 487} 488 489/// doFinalization - Run all of the initializers for the function passes. 490/// 491bool FunctionPassManager_New::doFinalization() { 492 return FPM->doFinalization(*MP->getModule()); 493} 494 495// FunctionPassManagerImpl_New implementation 496 497// FunctionPassManager 498 499/// Add pass P into the pass manager queue. If P is a BasicBlockPass then 500/// either use it into active basic block pass manager or create new basic 501/// block pass manager to handle pass P. 502bool 503FunctionPassManagerImpl_New::addPass(Pass *P) { 504 505 // If P is a BasicBlockPass then use BasicBlockPassManager_New. 506 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) { 507 508 if (!activeBBPassManager 509 || !activeBBPassManager->addPass(BP)) { 510 511 activeBBPassManager = new BasicBlockPassManager_New(); 512 addPassToManager(activeBBPassManager, false); 513 if (!activeBBPassManager->addPass(BP)) 514 assert(0 && "Unable to add Pass"); 515 } 516 return true; 517 } 518 519 FunctionPass *FP = dynamic_cast<FunctionPass *>(P); 520 if (!FP) 521 return false; 522 523 // If this pass does not preserve anlysis that is used by other passes 524 // managed by this manager than it is not a suiable pass for this manager. 525 if (!manageablePass(P)) 526 return false; 527 528 addPassToManager (FP); 529 activeBBPassManager = NULL; 530 return true; 531} 532 533/// Execute all of the passes scheduled for execution by invoking 534/// runOnFunction method. Keep track of whether any of the passes modifies 535/// the function, and if so, return true. 536bool FunctionPassManagerImpl_New::runOnModule(Module &M) { 537 538 bool Changed = false; 539 initializeAnalysisInfo(); 540 541 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 542 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 543 e = passVectorEnd(); itr != e; ++itr) { 544 Pass *P = *itr; 545 546 noteDownAvailableAnalysis(P); 547 FunctionPass *FP = dynamic_cast<FunctionPass*>(P); 548 Changed |= FP->runOnFunction(*I); 549 removeNotPreservedAnalysis(P); 550 removeDeadPasses(P); 551 } 552 return Changed; 553} 554 555/// Execute all of the passes scheduled for execution by invoking 556/// runOnFunction method. Keep track of whether any of the passes modifies 557/// the function, and if so, return true. 558bool FunctionPassManagerImpl_New::runOnFunction(Function &F) { 559 560 bool Changed = false; 561 initializeAnalysisInfo(); 562 563 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 564 e = passVectorEnd(); itr != e; ++itr) { 565 Pass *P = *itr; 566 567 noteDownAvailableAnalysis(P); 568 FunctionPass *FP = dynamic_cast<FunctionPass*>(P); 569 Changed |= FP->runOnFunction(F); 570 removeNotPreservedAnalysis(P); 571 removeDeadPasses(P); 572 } 573 return Changed; 574} 575 576 577/// Return true IFF AnalysisID AID is currently available. 578Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) { 579 580 Pass *P = getAnalysisPass(AID); 581 if (P) 582 return P; 583 584 if (activeBBPassManager && 585 activeBBPassManager->getAnalysisPass(AID) != 0) 586 return activeBBPassManager->getAnalysisPass(AID); 587 588 // TODO : Check inactive managers 589 return NULL; 590} 591 592inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) { 593 bool Changed = false; 594 595 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 596 e = passVectorEnd(); itr != e; ++itr) { 597 Pass *P = *itr; 598 599 FunctionPass *FP = dynamic_cast<FunctionPass*>(P); 600 Changed |= FP->doInitialization(M); 601 } 602 603 return Changed; 604} 605 606inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) { 607 bool Changed = false; 608 609 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 610 e = passVectorEnd(); itr != e; ++itr) { 611 Pass *P = *itr; 612 613 FunctionPass *FP = dynamic_cast<FunctionPass*>(P); 614 Changed |= FP->doFinalization(M); 615 } 616 617 618 return Changed; 619} 620 621 622// ModulePassManager implementation 623 624/// Add P into pass vector if it is manageble. If P is a FunctionPass 625/// then use FunctionPassManagerImpl_New to manage it. Return false if P 626/// is not manageable by this manager. 627bool 628ModulePassManager_New::addPass(Pass *P) { 629 630 // If P is FunctionPass then use function pass maanager. 631 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) { 632 633 activeFunctionPassManager = NULL; 634 635 if (!activeFunctionPassManager 636 || !activeFunctionPassManager->addPass(P)) { 637 638 activeFunctionPassManager = new FunctionPassManagerImpl_New(); 639 addPassToManager(activeFunctionPassManager, false); 640 if (!activeFunctionPassManager->addPass(FP)) 641 assert(0 && "Unable to add pass"); 642 } 643 return true; 644 } 645 646 ModulePass *MP = dynamic_cast<ModulePass *>(P); 647 if (!MP) 648 return false; 649 650 // If this pass does not preserve anlysis that is used by other passes 651 // managed by this manager than it is not a suiable pass for this manager. 652 if (!manageablePass(P)) 653 return false; 654 655 addPassToManager(MP); 656 activeFunctionPassManager = NULL; 657 return true; 658} 659 660 661/// Execute all of the passes scheduled for execution by invoking 662/// runOnModule method. Keep track of whether any of the passes modifies 663/// the module, and if so, return true. 664bool 665ModulePassManager_New::runOnModule(Module &M) { 666 bool Changed = false; 667 initializeAnalysisInfo(); 668 669 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 670 e = passVectorEnd(); itr != e; ++itr) { 671 Pass *P = *itr; 672 673 noteDownAvailableAnalysis(P); 674 ModulePass *MP = dynamic_cast<ModulePass*>(P); 675 Changed |= MP->runOnModule(M); 676 removeNotPreservedAnalysis(P); 677 removeDeadPasses(P); 678 } 679 return Changed; 680} 681 682/// Return true IFF AnalysisID AID is currently available. 683Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) { 684 685 686 Pass *P = getAnalysisPass(AID); 687 if (P) 688 return P; 689 690 if (activeFunctionPassManager && 691 activeFunctionPassManager->getAnalysisPass(AID) != 0) 692 return activeFunctionPassManager->getAnalysisPass(AID); 693 694 // TODO : Check inactive managers 695 return NULL; 696} 697 698/// Return true IFF AnalysisID AID is currently available. 699Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) { 700 701 Pass *P = NULL; 702 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), 703 e = PassManagers.end(); !P && itr != e; ++itr) 704 P = (*itr)->getAnalysisPassFromManager(AID); 705 return P; 706} 707 708/// Schedule pass P for execution. Make sure that passes required by 709/// P are run before P is run. Update analysis info maintained by 710/// the manager. Remove dead passes. This is a recursive function. 711void PassManagerImpl_New::schedulePass(Pass *P) { 712 713 AnalysisUsage AnUsage; 714 P->getAnalysisUsage(AnUsage); 715 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); 716 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(), 717 E = RequiredSet.end(); I != E; ++I) { 718 719 Pass *AnalysisPass = getAnalysisPassFromManager(*I); 720 if (!AnalysisPass) { 721 // Schedule this analysis run first. 722 AnalysisPass = (*I)->createPass(); 723 schedulePass(AnalysisPass); 724 } 725 setLastUser (AnalysisPass, P); 726 727 // Prolong live range of analyses that are needed after an analysis pass 728 // is destroyed, for querying by subsequent passes 729 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet(); 730 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(), 731 E = IDs.end(); I != E; ++I) { 732 Pass *AP = getAnalysisPassFromManager(*I); 733 assert (AP && "Analysis pass is not available"); 734 setLastUser(AP, P); 735 } 736 } 737 addPass(P); 738} 739 740/// Schedule all passes from the queue by adding them in their 741/// respective manager's queue. 742void PassManagerImpl_New::schedulePasses() { 743 for (std::vector<Pass *>::iterator I = passVectorBegin(), 744 E = passVectorEnd(); I != E; ++I) 745 schedulePass (*I); 746} 747 748/// Add pass P to the queue of passes to run. 749void PassManagerImpl_New::add(Pass *P) { 750 // Do not process Analysis now. Analysis is process while scheduling 751 // the pass vector. 752 addPassToManager(P, false); 753} 754 755// PassManager_New implementation 756/// Add P into active pass manager or use new module pass manager to 757/// manage it. 758bool PassManagerImpl_New::addPass(Pass *P) { 759 760 if (!activeManager || !activeManager->addPass(P)) { 761 activeManager = new ModulePassManager_New(); 762 PassManagers.push_back(activeManager); 763 } 764 765 return activeManager->addPass(P); 766} 767 768/// run - Execute all of the passes scheduled for execution. Keep track of 769/// whether any of the passes modifies the module, and if so, return true. 770bool PassManagerImpl_New::run(Module &M) { 771 772 schedulePasses(); 773 bool Changed = false; 774 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), 775 e = PassManagers.end(); itr != e; ++itr) { 776 ModulePassManager_New *pm = *itr; 777 Changed |= pm->runOnModule(M); 778 } 779 return Changed; 780} 781 782/// Create new pass manager 783PassManager_New::PassManager_New() { 784 PM = new PassManagerImpl_New(); 785} 786 787/// add - Add a pass to the queue of passes to run. This passes ownership of 788/// the Pass to the PassManager. When the PassManager is destroyed, the pass 789/// will be destroyed as well, so there is no need to delete the pass. This 790/// implies that all passes MUST be allocated with 'new'. 791void 792PassManager_New::add(Pass *P) { 793 PM->add(P); 794} 795 796/// run - Execute all of the passes scheduled for execution. Keep track of 797/// whether any of the passes modifies the module, and if so, return true. 798bool 799PassManager_New::run(Module &M) { 800 return PM->run(M); 801} 802 803