PassManager.cpp revision badc4fae531cc0fdaa8e58f481c3b1d23ea7dd56
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//===----------------------------------------------------------------------===// 90// PMTopLevelManager 91// 92/// PMTopLevelManager manages LastUser info and collects common APIs used by 93/// top level pass managers. 94class PMTopLevelManager { 95 96public: 97 98 inline std::vector<Pass *>::iterator passManagersBegin() { 99 return PassManagers.begin(); 100 } 101 102 inline std::vector<Pass *>::iterator passManagersEnd() { 103 return PassManagers.end(); 104 } 105 106 /// Schedule pass P for execution. Make sure that passes required by 107 /// P are run before P is run. Update analysis info maintained by 108 /// the manager. Remove dead passes. This is a recursive function. 109 void schedulePass(Pass *P, Pass *PM); 110 111 /// This is implemented by top level pass manager and used by 112 /// schedulePass() to add analysis info passes that are not available. 113 virtual void addTopLevelPass(Pass *P) = 0; 114 115 /// Set pass P as the last user of the given analysis passes. 116 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P); 117 118 /// Collect passes whose last user is P 119 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P); 120 121 virtual ~PMTopLevelManager() { 122 PassManagers.clear(); 123 } 124 125 /// Add immutable pass and initialize it. 126 inline void addImmutablePass(ImmutablePass *P) { 127 P->initializePass(); 128 ImmutablePasses.push_back(P); 129 } 130 131 inline std::vector<ImmutablePass *>& getImmutablePasses() { 132 return ImmutablePasses; 133 } 134 135private: 136 137 /// Collection of pass managers 138 std::vector<Pass *> PassManagers; 139 140 // Map to keep track of last user of the analysis pass. 141 // LastUser->second is the last user of Lastuser->first. 142 std::map<Pass *, Pass *> LastUser; 143 144 /// Immutable passes are managed by top level manager. 145 std::vector<ImmutablePass *> ImmutablePasses; 146}; 147 148/// Set pass P as the last user of the given analysis passes. 149void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses, 150 Pass *P) { 151 152 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(), 153 E = AnalysisPasses.end(); I != E; ++I) { 154 Pass *AP = *I; 155 LastUser[AP] = P; 156 // If AP is the last user of other passes then make P last user of 157 // such passes. 158 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(), 159 LUE = LastUser.end(); LUI != LUE; ++LUI) { 160 if (LUI->second == AP) 161 LastUser[LUI->first] = P; 162 } 163 } 164 165} 166 167/// Collect passes whose last user is P 168void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses, 169 Pass *P) { 170 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(), 171 LUE = LastUser.end(); LUI != LUE; ++LUI) 172 if (LUI->second == P) 173 LastUses.push_back(LUI->first); 174} 175 176/// Schedule pass P for execution. Make sure that passes required by 177/// P are run before P is run. Update analysis info maintained by 178/// the manager. Remove dead passes. This is a recursive function. 179void PMTopLevelManager::schedulePass(Pass *P, Pass *PM) { 180 181 // TODO : Allocate function manager for this pass, other wise required set 182 // may be inserted into previous function manager 183 184 AnalysisUsage AnUsage; 185 P->getAnalysisUsage(AnUsage); 186 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); 187 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(), 188 E = RequiredSet.end(); I != E; ++I) { 189 190 Pass *AnalysisPass = NULL; // FIXME PM->getAnalysisPass(*I, true); 191 if (!AnalysisPass) { 192 // Schedule this analysis run first. 193 AnalysisPass = (*I)->createPass(); 194 schedulePass(AnalysisPass, PM); 195 } 196 } 197 198 // Now all required passes are available. 199 addTopLevelPass(P); 200} 201 202 203//===----------------------------------------------------------------------===// 204// PMDataManager 205 206/// PMDataManager provides the common place to manage the analysis data 207/// used by pass managers. 208class PMDataManager { 209 210public: 211 212 PMDataManager() : TPM(NULL) { 213 initializeAnalysisInfo(); 214 } 215 216 /// Return true IFF pass P's required analysis set does not required new 217 /// manager. 218 bool manageablePass(Pass *P); 219 220 Pass *getAnalysisPass(AnalysisID AID) const { 221 222 std::map<AnalysisID, Pass*>::const_iterator I = 223 AvailableAnalysis.find(AID); 224 225 if (I != AvailableAnalysis.end()) 226 return NULL; 227 else 228 return I->second; 229 } 230 231 /// Augment AvailableAnalysis by adding analysis made available by pass P. 232 void recordAvailableAnalysis(Pass *P); 233 234 /// Remove Analysis that is not preserved by the pass 235 void removeNotPreservedAnalysis(Pass *P); 236 237 /// Remove dead passes 238 void removeDeadPasses(Pass *P); 239 240 /// Add pass P into the PassVector. Update 241 /// AvailableAnalysis appropriately if ProcessAnalysis is true. 242 void addPassToManager (Pass *P, bool ProcessAnalysis = true); 243 244 // Initialize available analysis information. 245 void initializeAnalysisInfo() { 246 AvailableAnalysis.clear(); 247 248 // Include immutable passes into AvailableAnalysis vector. 249 std::vector<ImmutablePass *> &ImmutablePasses = TPM->getImmutablePasses(); 250 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(), 251 E = ImmutablePasses.end(); I != E; ++I) 252 recordAvailableAnalysis(*I); 253 } 254 255 // All Required analyses should be available to the pass as it runs! Here 256 // we fill in the AnalysisImpls member of the pass so that it can 257 // successfully use the getAnalysis() method to retrieve the 258 // implementations it needs. 259 // 260 void initializeAnalysisImpl(Pass *P); 261 262 inline std::vector<Pass *>::iterator passVectorBegin() { 263 return PassVector.begin(); 264 } 265 266 inline std::vector<Pass *>::iterator passVectorEnd() { 267 return PassVector.end(); 268 } 269 270 // Access toplevel manager 271 PMTopLevelManager *getTopLevelManager() { return TPM; } 272 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; } 273 274private: 275 // Set of available Analysis. This information is used while scheduling 276 // pass. If a pass requires an analysis which is not not available then 277 // equired analysis pass is scheduled to run before the pass itself is 278 // scheduled to run. 279 std::map<AnalysisID, Pass*> AvailableAnalysis; 280 281 // Collection of pass that are managed by this manager 282 std::vector<Pass *> PassVector; 283 284 // Top level manager. 285 // TODO : Make it a reference. 286 PMTopLevelManager *TPM; 287}; 288 289/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the 290/// pass together and sequence them to process one basic block before 291/// processing next basic block. 292class BasicBlockPassManager_New : public PMDataManager, 293 public FunctionPass { 294 295public: 296 BasicBlockPassManager_New() { } 297 298 /// Add a pass into a passmanager queue. 299 bool addPass(Pass *p); 300 301 /// Execute all of the passes scheduled for execution. Keep track of 302 /// whether any of the passes modifies the function, and if so, return true. 303 bool runOnFunction(Function &F); 304 305 /// Return true IFF AnalysisID AID is currently available. 306 /// TODO : Replace this method with getAnalysisPass() 307 Pass *getAnalysisPassFromManager(AnalysisID AID); 308 309 /// Pass Manager itself does not invalidate any analysis info. 310 void getAnalysisUsage(AnalysisUsage &Info) const { 311 Info.setPreservesAll(); 312 } 313 314private: 315}; 316 317/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers. 318/// It batches all function passes and basic block pass managers together and 319/// sequence them to process one function at a time before processing next 320/// function. 321class FunctionPassManagerImpl_New : public ModulePass, 322 public PMDataManager, 323 public PMTopLevelManager { 324public: 325 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ } 326 FunctionPassManagerImpl_New() { 327 activeBBPassManager = NULL; 328 } 329 ~FunctionPassManagerImpl_New() { /* TODO */ }; 330 331 inline void addTopLevelPass(Pass *P) { 332 addPass(P); 333 } 334 335 /// add - Add a pass to the queue of passes to run. This passes 336 /// ownership of the Pass to the PassManager. When the 337 /// PassManager_X is destroyed, the pass will be destroyed as well, so 338 /// there is no need to delete the pass. (TODO delete passes.) 339 /// This implies that all passes MUST be allocated with 'new'. 340 void add(Pass *P) { 341 schedulePass(P, this); 342 } 343 344 /// Add pass into the pass manager queue. 345 bool addPass(Pass *P); 346 347 /// Execute all of the passes scheduled for execution. Keep 348 /// track of whether any of the passes modifies the function, and if 349 /// so, return true. 350 bool runOnModule(Module &M); 351 bool runOnFunction(Function &F); 352 353 /// Return true IFF AnalysisID AID is currently available. 354 /// TODO : Replace this method with getAnalysisPass() 355 Pass *getAnalysisPassFromManager(AnalysisID AID); 356 357 /// doInitialization - Run all of the initializers for the function passes. 358 /// 359 bool doInitialization(Module &M); 360 361 /// doFinalization - Run all of the initializers for the function passes. 362 /// 363 bool doFinalization(Module &M); 364 365 /// Pass Manager itself does not invalidate any analysis info. 366 void getAnalysisUsage(AnalysisUsage &Info) const { 367 Info.setPreservesAll(); 368 } 369 370private: 371 // Active Pass Managers 372 BasicBlockPassManager_New *activeBBPassManager; 373}; 374 375/// ModulePassManager_New manages ModulePasses and function pass managers. 376/// It batches all Module passes passes and function pass managers together and 377/// sequence them to process one module. 378class ModulePassManager_New : public PMDataManager { 379 380public: 381 ModulePassManager_New() { activeFunctionPassManager = NULL; } 382 383 /// Add a pass into a passmanager queue. 384 bool addPass(Pass *p); 385 386 /// run - Execute all of the passes scheduled for execution. Keep track of 387 /// whether any of the passes modifies the module, and if so, return true. 388 bool runOnModule(Module &M); 389 390 /// Return true IFF AnalysisID AID is currently available. 391 /// TODO : Replace this method with getAnalysisPass() 392 Pass *getAnalysisPassFromManager(AnalysisID AID); 393 394 /// Pass Manager itself does not invalidate any analysis info. 395 void getAnalysisUsage(AnalysisUsage &Info) const { 396 Info.setPreservesAll(); 397 } 398 399private: 400 // Active Pass Manager 401 FunctionPassManagerImpl_New *activeFunctionPassManager; 402}; 403 404/// PassManager_New manages ModulePassManagers 405class PassManagerImpl_New : public Pass, 406 public PMDataManager, 407 public PMTopLevelManager { 408 409public: 410 411 /// add - Add a pass to the queue of passes to run. This passes ownership of 412 /// the Pass to the PassManager. When the PassManager is destroyed, the pass 413 /// will be destroyed as well, so there is no need to delete the pass. This 414 /// implies that all passes MUST be allocated with 'new'. 415 void add(Pass *P) { 416 schedulePass(P, this); 417 } 418 419 /// run - Execute all of the passes scheduled for execution. Keep track of 420 /// whether any of the passes modifies the module, and if so, return true. 421 bool run(Module &M); 422 423 /// Return true IFF AnalysisID AID is currently available. 424 /// TODO : Replace this method with getAnalysisPass() 425 Pass *getAnalysisPassFromManager(AnalysisID AID); 426 427 /// Pass Manager itself does not invalidate any analysis info. 428 void getAnalysisUsage(AnalysisUsage &Info) const { 429 Info.setPreservesAll(); 430 } 431 432 inline void addTopLevelPass(Pass *P) { 433 addPass(P); 434 } 435 436private: 437 438 /// Add a pass into a passmanager queue. 439 bool addPass(Pass *p); 440 441 // Collection of pass managers 442 std::vector<ModulePassManager_New *> PassManagers; 443 444 // Active Pass Manager 445 ModulePassManager_New *activeManager; 446}; 447 448} // End of llvm namespace 449 450//===----------------------------------------------------------------------===// 451// PMDataManager implementation 452 453/// Return true IFF pass P's required analysis set does not required new 454/// manager. 455bool PMDataManager::manageablePass(Pass *P) { 456 457 // TODO 458 // If this pass is not preserving information that is required by a 459 // pass maintained by higher level pass manager then do not insert 460 // this pass into current manager. Use new manager. For example, 461 // For example, If FunctionPass F is not preserving ModulePass Info M1 462 // that is used by another ModulePass M2 then do not insert F in 463 // current function pass manager. 464 return true; 465} 466 467/// Augement AvailableAnalysis by adding analysis made available by pass P. 468void PMDataManager::recordAvailableAnalysis(Pass *P) { 469 470 if (const PassInfo *PI = P->getPassInfo()) { 471 AvailableAnalysis[PI] = P; 472 473 //This pass is the current implementation of all of the interfaces it 474 //implements as well. 475 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); 476 for (unsigned i = 0, e = II.size(); i != e; ++i) 477 AvailableAnalysis[II[i]] = P; 478 } 479} 480 481/// Remove Analyss not preserved by Pass P 482void PMDataManager::removeNotPreservedAnalysis(Pass *P) { 483 AnalysisUsage AnUsage; 484 P->getAnalysisUsage(AnUsage); 485 486 if (AnUsage.getPreservesAll()) 487 return; 488 489 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); 490 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), 491 E = AvailableAnalysis.end(); I != E; ++I ) { 492 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) == 493 PreservedSet.end()) { 494 // Remove this analysis 495 std::map<AnalysisID, Pass*>::iterator J = I++; 496 AvailableAnalysis.erase(J); 497 } 498 } 499} 500 501/// Remove analysis passes that are not used any longer 502void PMDataManager::removeDeadPasses(Pass *P) { 503 // TODO : reimplement 504} 505 506/// Add pass P into the PassVector. Update 507/// AvailableAnalysis appropriately if ProcessAnalysis is true. 508void PMDataManager::addPassToManager(Pass *P, 509 bool ProcessAnalysis) { 510 511 if (ProcessAnalysis) { 512 // Take a note of analysis required and made available by this pass. 513 // Remove the analysis not preserved by this pass 514 initializeAnalysisImpl(P); 515 removeNotPreservedAnalysis(P); 516 recordAvailableAnalysis(P); 517 } 518 519 // Add pass 520 PassVector.push_back(P); 521} 522 523// All Required analyses should be available to the pass as it runs! Here 524// we fill in the AnalysisImpls member of the pass so that it can 525// successfully use the getAnalysis() method to retrieve the 526// implementations it needs. 527// 528void PMDataManager::initializeAnalysisImpl(Pass *P) { 529 AnalysisUsage AnUsage; 530 P->getAnalysisUsage(AnUsage); 531 532 for (std::vector<const PassInfo *>::const_iterator 533 I = AnUsage.getRequiredSet().begin(), 534 E = AnUsage.getRequiredSet().end(); I != E; ++I) { 535 Pass *Impl = getAnalysisPass(*I); 536 if (Impl == 0) 537 assert(0 && "Analysis used but not available!"); 538 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl)); 539 } 540} 541 542//===----------------------------------------------------------------------===// 543// BasicBlockPassManager_New implementation 544 545/// Add pass P into PassVector and return true. If this pass is not 546/// manageable by this manager then return false. 547bool 548BasicBlockPassManager_New::addPass(Pass *P) { 549 550 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); 551 if (!BP) 552 return false; 553 554 // If this pass does not preserve anlysis that is used by other passes 555 // managed by this manager than it is not a suiable pass for this manager. 556 if (!manageablePass(P)) 557 return false; 558 559 addPassToManager (BP); 560 561 return true; 562} 563 564/// Execute all of the passes scheduled for execution by invoking 565/// runOnBasicBlock method. Keep track of whether any of the passes modifies 566/// the function, and if so, return true. 567bool 568BasicBlockPassManager_New::runOnFunction(Function &F) { 569 570 bool Changed = false; 571 initializeAnalysisInfo(); 572 573 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) 574 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 575 e = passVectorEnd(); itr != e; ++itr) { 576 Pass *P = *itr; 577 578 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); 579 Changed |= BP->runOnBasicBlock(*I); 580 removeNotPreservedAnalysis(P); 581 recordAvailableAnalysis(P); 582 removeDeadPasses(P); 583 } 584 return Changed; 585} 586 587/// Return true IFF AnalysisID AID is currently available. 588/// TODO : Replace this method with getAnalysisPass() 589Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) { 590 return getAnalysisPass(AID); 591} 592 593//===----------------------------------------------------------------------===// 594// FunctionPassManager_New implementation 595 596/// Create new Function pass manager 597FunctionPassManager_New::FunctionPassManager_New() { 598 FPM = new FunctionPassManagerImpl_New(); 599} 600 601/// add - Add a pass to the queue of passes to run. This passes 602/// ownership of the Pass to the PassManager. When the 603/// PassManager_X is destroyed, the pass will be destroyed as well, so 604/// there is no need to delete the pass. (TODO delete passes.) 605/// This implies that all passes MUST be allocated with 'new'. 606void FunctionPassManager_New::add(Pass *P) { 607 FPM->add(P); 608} 609 610/// Execute all of the passes scheduled for execution. Keep 611/// track of whether any of the passes modifies the function, and if 612/// so, return true. 613bool FunctionPassManager_New::runOnModule(Module &M) { 614 return FPM->runOnModule(M); 615} 616 617/// run - Execute all of the passes scheduled for execution. Keep 618/// track of whether any of the passes modifies the function, and if 619/// so, return true. 620/// 621bool FunctionPassManager_New::run(Function &F) { 622 std::string errstr; 623 if (MP->materializeFunction(&F, &errstr)) { 624 cerr << "Error reading bytecode file: " << errstr << "\n"; 625 abort(); 626 } 627 return FPM->runOnFunction(F); 628} 629 630 631/// doInitialization - Run all of the initializers for the function passes. 632/// 633bool FunctionPassManager_New::doInitialization() { 634 return FPM->doInitialization(*MP->getModule()); 635} 636 637/// doFinalization - Run all of the initializers for the function passes. 638/// 639bool FunctionPassManager_New::doFinalization() { 640 return FPM->doFinalization(*MP->getModule()); 641} 642 643//===----------------------------------------------------------------------===// 644// FunctionPassManagerImpl_New implementation 645 646/// Add pass P into the pass manager queue. If P is a BasicBlockPass then 647/// either use it into active basic block pass manager or create new basic 648/// block pass manager to handle pass P. 649bool 650FunctionPassManagerImpl_New::addPass(Pass *P) { 651 652 // If P is a BasicBlockPass then use BasicBlockPassManager_New. 653 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) { 654 655 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) { 656 657 // If active manager exists then clear its analysis info. 658 if (activeBBPassManager) 659 activeBBPassManager->initializeAnalysisInfo(); 660 661 // Create and add new manager 662 activeBBPassManager = new BasicBlockPassManager_New(); 663 addPassToManager(activeBBPassManager, false); 664 665 // Add pass into new manager. This time it must succeed. 666 if (!activeBBPassManager->addPass(BP)) 667 assert(0 && "Unable to add Pass"); 668 } 669 return true; 670 } 671 672 FunctionPass *FP = dynamic_cast<FunctionPass *>(P); 673 if (!FP) 674 return false; 675 676 // If this pass does not preserve anlysis that is used by other passes 677 // managed by this manager than it is not a suiable pass for this manager. 678 if (!manageablePass(P)) 679 return false; 680 681 addPassToManager (FP); 682 683 // If active manager exists then clear its analysis info. 684 if (activeBBPassManager) { 685 activeBBPassManager->initializeAnalysisInfo(); 686 activeBBPassManager = NULL; 687 } 688 689 return true; 690} 691 692/// Execute all of the passes scheduled for execution by invoking 693/// runOnFunction method. Keep track of whether any of the passes modifies 694/// the function, and if so, return true. 695bool FunctionPassManagerImpl_New::runOnModule(Module &M) { 696 697 bool Changed = false; 698 initializeAnalysisInfo(); 699 700 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 701 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 702 e = passVectorEnd(); itr != e; ++itr) { 703 Pass *P = *itr; 704 705 FunctionPass *FP = dynamic_cast<FunctionPass*>(P); 706 Changed |= FP->runOnFunction(*I); 707 removeNotPreservedAnalysis(P); 708 recordAvailableAnalysis(P); 709 removeDeadPasses(P); 710 } 711 return Changed; 712} 713 714/// Execute all of the passes scheduled for execution by invoking 715/// runOnFunction method. Keep track of whether any of the passes modifies 716/// the function, and if so, return true. 717bool FunctionPassManagerImpl_New::runOnFunction(Function &F) { 718 719 bool Changed = false; 720 initializeAnalysisInfo(); 721 722 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 723 e = passVectorEnd(); itr != e; ++itr) { 724 Pass *P = *itr; 725 726 FunctionPass *FP = dynamic_cast<FunctionPass*>(P); 727 Changed |= FP->runOnFunction(F); 728 removeNotPreservedAnalysis(P); 729 recordAvailableAnalysis(P); 730 removeDeadPasses(P); 731 } 732 return Changed; 733} 734 735 736/// Return true IFF AnalysisID AID is currently available. 737/// TODO : Replace this method with getAnalysisPass() 738Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) { 739 740 Pass *P = getAnalysisPass(AID); 741 if (P) 742 return P; 743 744 if (activeBBPassManager && 745 activeBBPassManager->getAnalysisPass(AID) != 0) 746 return activeBBPassManager->getAnalysisPass(AID); 747 748 // TODO : Check inactive managers 749 return NULL; 750} 751 752inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) { 753 bool Changed = false; 754 755 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 756 e = passVectorEnd(); itr != e; ++itr) { 757 Pass *P = *itr; 758 759 FunctionPass *FP = dynamic_cast<FunctionPass*>(P); 760 Changed |= FP->doInitialization(M); 761 } 762 763 return Changed; 764} 765 766inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) { 767 bool Changed = false; 768 769 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 770 e = passVectorEnd(); itr != e; ++itr) { 771 Pass *P = *itr; 772 773 FunctionPass *FP = dynamic_cast<FunctionPass*>(P); 774 Changed |= FP->doFinalization(M); 775 } 776 777 778 return Changed; 779} 780 781//===----------------------------------------------------------------------===// 782// ModulePassManager implementation 783 784/// Add P into pass vector if it is manageble. If P is a FunctionPass 785/// then use FunctionPassManagerImpl_New to manage it. Return false if P 786/// is not manageable by this manager. 787bool 788ModulePassManager_New::addPass(Pass *P) { 789 790 // If P is FunctionPass then use function pass maanager. 791 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) { 792 793 if (!activeFunctionPassManager 794 || !activeFunctionPassManager->addPass(P)) { 795 796 // If active manager exists then clear its analysis info. 797 if (activeFunctionPassManager) 798 activeFunctionPassManager->initializeAnalysisInfo(); 799 800 // Create and add new manager 801 activeFunctionPassManager = new FunctionPassManagerImpl_New(); 802 addPassToManager(activeFunctionPassManager, false); 803 804 // Add pass into new manager. This time it must succeed. 805 if (!activeFunctionPassManager->addPass(FP)) 806 assert(0 && "Unable to add pass"); 807 } 808 return true; 809 } 810 811 ModulePass *MP = dynamic_cast<ModulePass *>(P); 812 if (!MP) 813 return false; 814 815 // If this pass does not preserve anlysis that is used by other passes 816 // managed by this manager than it is not a suiable pass for this manager. 817 if (!manageablePass(P)) 818 return false; 819 820 addPassToManager(MP); 821 // If active manager exists then clear its analysis info. 822 if (activeFunctionPassManager) { 823 activeFunctionPassManager->initializeAnalysisInfo(); 824 activeFunctionPassManager = NULL; 825 } 826 827 return true; 828} 829 830 831/// Execute all of the passes scheduled for execution by invoking 832/// runOnModule method. Keep track of whether any of the passes modifies 833/// the module, and if so, return true. 834bool 835ModulePassManager_New::runOnModule(Module &M) { 836 bool Changed = false; 837 initializeAnalysisInfo(); 838 839 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 840 e = passVectorEnd(); itr != e; ++itr) { 841 Pass *P = *itr; 842 843 ModulePass *MP = dynamic_cast<ModulePass*>(P); 844 Changed |= MP->runOnModule(M); 845 removeNotPreservedAnalysis(P); 846 recordAvailableAnalysis(P); 847 removeDeadPasses(P); 848 } 849 return Changed; 850} 851 852/// Return true IFF AnalysisID AID is currently available. 853/// TODO : Replace this method with getAnalysisPass() 854Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) { 855 856 Pass *P = getAnalysisPass(AID); 857 if (P) 858 return P; 859 860 if (activeFunctionPassManager && 861 activeFunctionPassManager->getAnalysisPass(AID) != 0) 862 return activeFunctionPassManager->getAnalysisPass(AID); 863 864 // TODO : Check inactive managers 865 return NULL; 866} 867 868//===----------------------------------------------------------------------===// 869// PassManagerImpl implementation 870 871/// Return true IFF AnalysisID AID is currently available. 872/// TODO : Replace this method with getAnalysisPass() 873Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) { 874 875 Pass *P = NULL; 876 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), 877 e = PassManagers.end(); !P && itr != e; ++itr) 878 P = (*itr)->getAnalysisPassFromManager(AID); 879 return P; 880} 881 882// PassManager_New implementation 883/// Add P into active pass manager or use new module pass manager to 884/// manage it. 885bool PassManagerImpl_New::addPass(Pass *P) { 886 887 if (!activeManager || !activeManager->addPass(P)) { 888 activeManager = new ModulePassManager_New(); 889 PassManagers.push_back(activeManager); 890 return activeManager->addPass(P); 891 } 892 return true; 893} 894 895/// run - Execute all of the passes scheduled for execution. Keep track of 896/// whether any of the passes modifies the module, and if so, return true. 897bool PassManagerImpl_New::run(Module &M) { 898 899 bool Changed = false; 900 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), 901 e = PassManagers.end(); itr != e; ++itr) { 902 ModulePassManager_New *pm = *itr; 903 Changed |= pm->runOnModule(M); 904 } 905 return Changed; 906} 907 908//===----------------------------------------------------------------------===// 909// PassManager implementation 910 911/// Create new pass manager 912PassManager_New::PassManager_New() { 913 PM = new PassManagerImpl_New(); 914} 915 916/// add - Add a pass to the queue of passes to run. This passes ownership of 917/// the Pass to the PassManager. When the PassManager is destroyed, the pass 918/// will be destroyed as well, so there is no need to delete the pass. This 919/// implies that all passes MUST be allocated with 'new'. 920void 921PassManager_New::add(Pass *P) { 922 PM->add(P); 923} 924 925/// run - Execute all of the passes scheduled for execution. Keep track of 926/// whether any of the passes modifies the module, and if so, return true. 927bool 928PassManager_New::run(Module &M) { 929 return PM->run(M); 930} 931 932