PassManager.cpp revision 4d9a186b10dacf1d702ef32c2638e9016d8f483b
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// PMDataManager 204 205/// PMDataManager provides the common place to manage the analysis data 206/// used by pass managers. 207class PMDataManager { 208 209public: 210 211 PMDataManager(int D) : TPM(NULL), Depth(D) { 212 initializeAnalysisInfo(); 213 } 214 215 /// Return true IFF pass P's required analysis set does not required new 216 /// manager. 217 bool manageablePass(Pass *P); 218 219 Pass *getAnalysisPass(AnalysisID AID) const { 220 221 std::map<AnalysisID, Pass*>::const_iterator I = 222 AvailableAnalysis.find(AID); 223 224 if (I != AvailableAnalysis.end()) 225 return NULL; 226 else 227 return I->second; 228 } 229 230 /// Augment AvailableAnalysis by adding analysis made available by pass P. 231 void recordAvailableAnalysis(Pass *P); 232 233 /// Remove Analysis that is not preserved by the pass 234 void removeNotPreservedAnalysis(Pass *P); 235 236 /// Remove dead passes 237 void removeDeadPasses(Pass *P); 238 239 /// Add pass P into the PassVector. Update 240 /// AvailableAnalysis appropriately if ProcessAnalysis is true. 241 void addPassToManager (Pass *P, bool ProcessAnalysis = true); 242 243 /// Initialize available analysis information. 244 void initializeAnalysisInfo() { 245 ForcedLastUses.clear(); 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 /// Populate RequiredPasses with the analysis pass that are required by 256 /// pass P. 257 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses, 258 Pass *P); 259 260 /// All Required analyses should be available to the pass as it runs! Here 261 /// we fill in the AnalysisImpls member of the pass so that it can 262 /// successfully use the getAnalysis() method to retrieve the 263 /// implementations it needs. 264 void initializeAnalysisImpl(Pass *P); 265 266 inline std::vector<Pass *>::iterator passVectorBegin() { 267 return PassVector.begin(); 268 } 269 270 inline std::vector<Pass *>::iterator passVectorEnd() { 271 return PassVector.end(); 272 } 273 274 // Access toplevel manager 275 PMTopLevelManager *getTopLevelManager() { return TPM; } 276 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; } 277 278 unsigned getDepth() { return Depth; } 279 280protected: 281 282 // Collection of pass whose last user asked this manager to claim 283 // last use. If a FunctionPass F is the last user of ModulePass info M 284 // then the F's manager, not F, records itself as a last user of M. 285 std::vector<Pass *> ForcedLastUses; 286 287 // Top level manager. 288 // TODO : Make it a reference. 289 PMTopLevelManager *TPM; 290 291private: 292 // Set of available Analysis. This information is used while scheduling 293 // pass. If a pass requires an analysis which is not not available then 294 // equired analysis pass is scheduled to run before the pass itself is 295 // scheduled to run. 296 std::map<AnalysisID, Pass*> AvailableAnalysis; 297 298 // Collection of pass that are managed by this manager 299 std::vector<Pass *> PassVector; 300 301 unsigned Depth; 302}; 303 304/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the 305/// pass together and sequence them to process one basic block before 306/// processing next basic block. 307class BasicBlockPassManager_New : public PMDataManager, 308 public FunctionPass { 309 310public: 311 BasicBlockPassManager_New(int D) : PMDataManager(D) { } 312 313 /// Add a pass into a passmanager queue. 314 bool addPass(Pass *p); 315 316 /// Execute all of the passes scheduled for execution. Keep track of 317 /// whether any of the passes modifies the function, and if so, return true. 318 bool runOnFunction(Function &F); 319 320 /// Return true IFF AnalysisID AID is currently available. 321 /// TODO : Replace this method with getAnalysisPass() 322 Pass *getAnalysisPassFromManager(AnalysisID AID); 323 324 /// Pass Manager itself does not invalidate any analysis info. 325 void getAnalysisUsage(AnalysisUsage &Info) const { 326 Info.setPreservesAll(); 327 } 328 329 bool doInitialization(Module &M); 330 bool doInitialization(Function &F); 331 bool doFinalization(Module &M); 332 bool doFinalization(Function &F); 333 334}; 335 336/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers. 337/// It batches all function passes and basic block pass managers together and 338/// sequence them to process one function at a time before processing next 339/// function. 340class FunctionPassManagerImpl_New : public ModulePass, 341 public PMDataManager, 342 public PMTopLevelManager { 343public: 344 FunctionPassManagerImpl_New(ModuleProvider *P, int D) : 345 PMDataManager(D) { /* TODO */ } 346 FunctionPassManagerImpl_New(int D) : PMDataManager(D) { 347 activeBBPassManager = NULL; 348 } 349 ~FunctionPassManagerImpl_New() { /* TODO */ }; 350 351 inline void addTopLevelPass(Pass *P) { 352 addPass(P); 353 } 354 355 /// add - Add a pass to the queue of passes to run. This passes 356 /// ownership of the Pass to the PassManager. When the 357 /// PassManager_X is destroyed, the pass will be destroyed as well, so 358 /// there is no need to delete the pass. (TODO delete passes.) 359 /// This implies that all passes MUST be allocated with 'new'. 360 void add(Pass *P) { 361 schedulePass(P, this); 362 } 363 364 /// Add pass into the pass manager queue. 365 bool addPass(Pass *P); 366 367 /// Execute all of the passes scheduled for execution. Keep 368 /// track of whether any of the passes modifies the function, and if 369 /// so, return true. 370 bool runOnModule(Module &M); 371 bool runOnFunction(Function &F); 372 373 /// Return true IFF AnalysisID AID is currently available. 374 /// TODO : Replace this method with getAnalysisPass() 375 Pass *getAnalysisPassFromManager(AnalysisID AID); 376 377 /// doInitialization - Run all of the initializers for the function passes. 378 /// 379 bool doInitialization(Module &M); 380 381 /// doFinalization - Run all of the initializers for the function passes. 382 /// 383 bool doFinalization(Module &M); 384 385 /// Pass Manager itself does not invalidate any analysis info. 386 void getAnalysisUsage(AnalysisUsage &Info) const { 387 Info.setPreservesAll(); 388 } 389 390private: 391 // Active Pass Managers 392 BasicBlockPassManager_New *activeBBPassManager; 393}; 394 395/// ModulePassManager_New manages ModulePasses and function pass managers. 396/// It batches all Module passes passes and function pass managers together and 397/// sequence them to process one module. 398class ModulePassManager_New : public Pass, 399 public PMDataManager { 400 401public: 402 ModulePassManager_New(int D) : PMDataManager(D) { 403 activeFunctionPassManager = NULL; 404 } 405 406 /// Add a pass into a passmanager queue. 407 bool addPass(Pass *p); 408 409 /// run - Execute all of the passes scheduled for execution. Keep track of 410 /// whether any of the passes modifies the module, and if so, return true. 411 bool runOnModule(Module &M); 412 413 /// Return true IFF AnalysisID AID is currently available. 414 /// TODO : Replace this method with getAnalysisPass() 415 Pass *getAnalysisPassFromManager(AnalysisID AID); 416 417 /// Pass Manager itself does not invalidate any analysis info. 418 void getAnalysisUsage(AnalysisUsage &Info) const { 419 Info.setPreservesAll(); 420 } 421 422private: 423 // Active Pass Manager 424 FunctionPassManagerImpl_New *activeFunctionPassManager; 425}; 426 427/// PassManager_New manages ModulePassManagers 428class PassManagerImpl_New : public Pass, 429 public PMDataManager, 430 public PMTopLevelManager { 431 432public: 433 434 PassManagerImpl_New(int D) : PMDataManager(D) {} 435 436 /// add - Add a pass to the queue of passes to run. This passes ownership of 437 /// the Pass to the PassManager. When the PassManager is destroyed, the pass 438 /// will be destroyed as well, so there is no need to delete the pass. This 439 /// implies that all passes MUST be allocated with 'new'. 440 void add(Pass *P) { 441 schedulePass(P, this); 442 } 443 444 /// run - Execute all of the passes scheduled for execution. Keep track of 445 /// whether any of the passes modifies the module, and if so, return true. 446 bool run(Module &M); 447 448 /// Return true IFF AnalysisID AID is currently available. 449 /// TODO : Replace this method with getAnalysisPass() 450 Pass *getAnalysisPassFromManager(AnalysisID AID); 451 452 /// Pass Manager itself does not invalidate any analysis info. 453 void getAnalysisUsage(AnalysisUsage &Info) const { 454 Info.setPreservesAll(); 455 } 456 457 inline void addTopLevelPass(Pass *P) { 458 addPass(P); 459 } 460 461private: 462 463 /// Add a pass into a passmanager queue. 464 bool addPass(Pass *p); 465 466 // Collection of pass managers 467 std::vector<ModulePassManager_New *> PassManagers; 468 469 // Active Pass Manager 470 ModulePassManager_New *activeManager; 471}; 472 473} // End of llvm namespace 474 475//===----------------------------------------------------------------------===// 476// PMDataManager implementation 477 478/// Return true IFF pass P's required analysis set does not required new 479/// manager. 480bool PMDataManager::manageablePass(Pass *P) { 481 482 // TODO 483 // If this pass is not preserving information that is required by a 484 // pass maintained by higher level pass manager then do not insert 485 // this pass into current manager. Use new manager. For example, 486 // For example, If FunctionPass F is not preserving ModulePass Info M1 487 // that is used by another ModulePass M2 then do not insert F in 488 // current function pass manager. 489 return true; 490} 491 492/// Augement AvailableAnalysis by adding analysis made available by pass P. 493void PMDataManager::recordAvailableAnalysis(Pass *P) { 494 495 if (const PassInfo *PI = P->getPassInfo()) { 496 AvailableAnalysis[PI] = P; 497 498 //This pass is the current implementation of all of the interfaces it 499 //implements as well. 500 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); 501 for (unsigned i = 0, e = II.size(); i != e; ++i) 502 AvailableAnalysis[II[i]] = P; 503 } 504} 505 506/// Remove Analyss not preserved by Pass P 507void PMDataManager::removeNotPreservedAnalysis(Pass *P) { 508 AnalysisUsage AnUsage; 509 P->getAnalysisUsage(AnUsage); 510 511 if (AnUsage.getPreservesAll()) 512 return; 513 514 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); 515 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), 516 E = AvailableAnalysis.end(); I != E; ++I ) { 517 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) == 518 PreservedSet.end()) { 519 // Remove this analysis 520 std::map<AnalysisID, Pass*>::iterator J = I++; 521 AvailableAnalysis.erase(J); 522 } 523 } 524} 525 526/// Remove analysis passes that are not used any longer 527void PMDataManager::removeDeadPasses(Pass *P) { 528 529 std::vector<Pass *> DeadPasses; 530 TPM->collectLastUses(DeadPasses, P); 531 532 for (std::vector<Pass *>::iterator I = DeadPasses.begin(), 533 E = DeadPasses.end(); I != E; ++I) { 534 (*I)->releaseMemory(); 535 536 std::map<AnalysisID, Pass*>::iterator Pos = 537 AvailableAnalysis.find((*I)->getPassInfo()); 538 539 // It is possible that pass is already removed from the AvailableAnalysis 540 if (Pos != AvailableAnalysis.end()) 541 AvailableAnalysis.erase(Pos); 542 } 543} 544 545/// Add pass P into the PassVector. Update 546/// AvailableAnalysis appropriately if ProcessAnalysis is true. 547void PMDataManager::addPassToManager(Pass *P, 548 bool ProcessAnalysis) { 549 550 if (ProcessAnalysis) { 551 552 // At the moment, this pass is the last user of all required passes. 553 std::vector<Pass *> LastUses; 554 std::vector<Pass *> RequiredPasses; 555 unsigned PDepth = this->getDepth(); 556 557 collectRequiredAnalysisPasses(RequiredPasses, P); 558 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(), 559 E = RequiredPasses.end(); I != E; ++I) { 560 Pass *PRequired = *I; 561 unsigned RDepth = 0; 562 //FIXME: RDepth = PRequired->getResolver()->getDepth(); 563 if (PDepth == RDepth) 564 LastUses.push_back(PRequired); 565 else if (PDepth > RDepth) { 566 // Let the parent claim responsibility of last use 567 ForcedLastUses.push_back(PRequired); 568 } else { 569 // Note : This feature is not yet implemented 570 assert (0 && 571 "Unable to handle Pass that requires lower level Analysis pass"); 572 } 573 } 574 575 if (!LastUses.empty()) 576 TPM->setLastUser(LastUses, P); 577 578 // Take a note of analysis required and made available by this pass. 579 // Remove the analysis not preserved by this pass 580 initializeAnalysisImpl(P); 581 removeNotPreservedAnalysis(P); 582 recordAvailableAnalysis(P); 583 } 584 585 // Add pass 586 PassVector.push_back(P); 587} 588 589/// Populate RequiredPasses with the analysis pass that are required by 590/// pass P. 591void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP, 592 Pass *P) { 593 AnalysisUsage AnUsage; 594 P->getAnalysisUsage(AnUsage); 595 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); 596 for (std::vector<AnalysisID>::const_iterator 597 I = RequiredSet.begin(), E = RequiredSet.end(); 598 I != E; ++I) { 599 Pass *AnalysisPass = NULL; //FIXME findAnalysisPass(*I,true); 600 assert (AnalysisPass && "Analysis pass is not available"); 601 RP.push_back(AnalysisPass); 602 } 603} 604 605// All Required analyses should be available to the pass as it runs! Here 606// we fill in the AnalysisImpls member of the pass so that it can 607// successfully use the getAnalysis() method to retrieve the 608// implementations it needs. 609// 610void PMDataManager::initializeAnalysisImpl(Pass *P) { 611 AnalysisUsage AnUsage; 612 P->getAnalysisUsage(AnUsage); 613 614 for (std::vector<const PassInfo *>::const_iterator 615 I = AnUsage.getRequiredSet().begin(), 616 E = AnUsage.getRequiredSet().end(); I != E; ++I) { 617 Pass *Impl = getAnalysisPass(*I); 618 if (Impl == 0) 619 assert(0 && "Analysis used but not available!"); 620 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl)); 621 } 622} 623 624//===----------------------------------------------------------------------===// 625// BasicBlockPassManager_New implementation 626 627/// Add pass P into PassVector and return true. If this pass is not 628/// manageable by this manager then return false. 629bool 630BasicBlockPassManager_New::addPass(Pass *P) { 631 632 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); 633 if (!BP) 634 return false; 635 636 // If this pass does not preserve anlysis that is used by other passes 637 // managed by this manager than it is not a suiable pass for this manager. 638 if (!manageablePass(P)) 639 return false; 640 641 addPassToManager (BP); 642 643 return true; 644} 645 646/// Execute all of the passes scheduled for execution by invoking 647/// runOnBasicBlock method. Keep track of whether any of the passes modifies 648/// the function, and if so, return true. 649bool 650BasicBlockPassManager_New::runOnFunction(Function &F) { 651 652 bool Changed = doInitialization(F); 653 initializeAnalysisInfo(); 654 655 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) 656 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 657 e = passVectorEnd(); itr != e; ++itr) { 658 Pass *P = *itr; 659 660 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); 661 Changed |= BP->runOnBasicBlock(*I); 662 removeNotPreservedAnalysis(P); 663 recordAvailableAnalysis(P); 664 removeDeadPasses(P); 665 } 666 return Changed | doFinalization(F); 667} 668 669/// Return true IFF AnalysisID AID is currently available. 670/// TODO : Replace this method with getAnalysisPass() 671Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) { 672 return getAnalysisPass(AID); 673} 674 675// Implement doInitialization and doFinalization 676inline bool BasicBlockPassManager_New::doInitialization(Module &M) { 677 bool Changed = false; 678 679 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 680 e = passVectorEnd(); itr != e; ++itr) { 681 Pass *P = *itr; 682 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); 683 Changed |= BP->doInitialization(M); 684 } 685 686 return Changed; 687} 688 689inline bool BasicBlockPassManager_New::doFinalization(Module &M) { 690 bool Changed = false; 691 692 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 693 e = passVectorEnd(); itr != e; ++itr) { 694 Pass *P = *itr; 695 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); 696 Changed |= BP->doFinalization(M); 697 } 698 699 return Changed; 700} 701 702inline bool BasicBlockPassManager_New::doInitialization(Function &F) { 703 bool Changed = false; 704 705 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 706 e = passVectorEnd(); itr != e; ++itr) { 707 Pass *P = *itr; 708 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); 709 Changed |= BP->doInitialization(F); 710 } 711 712 return Changed; 713} 714 715inline bool BasicBlockPassManager_New::doFinalization(Function &F) { 716 bool Changed = false; 717 718 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 719 e = passVectorEnd(); itr != e; ++itr) { 720 Pass *P = *itr; 721 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); 722 Changed |= BP->doFinalization(F); 723 } 724 725 return Changed; 726} 727 728 729//===----------------------------------------------------------------------===// 730// FunctionPassManager_New implementation 731 732/// Create new Function pass manager 733FunctionPassManager_New::FunctionPassManager_New() { 734 FPM = new FunctionPassManagerImpl_New(0); 735} 736 737FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) { 738 FPM = new FunctionPassManagerImpl_New(0); 739 MP = P; 740} 741 742/// add - Add a pass to the queue of passes to run. This passes 743/// ownership of the Pass to the PassManager. When the 744/// PassManager_X is destroyed, the pass will be destroyed as well, so 745/// there is no need to delete the pass. (TODO delete passes.) 746/// This implies that all passes MUST be allocated with 'new'. 747void FunctionPassManager_New::add(Pass *P) { 748 FPM->add(P); 749} 750 751/// Execute all of the passes scheduled for execution. Keep 752/// track of whether any of the passes modifies the function, and if 753/// so, return true. 754bool FunctionPassManager_New::runOnModule(Module &M) { 755 return FPM->runOnModule(M); 756} 757 758/// run - Execute all of the passes scheduled for execution. Keep 759/// track of whether any of the passes modifies the function, and if 760/// so, return true. 761/// 762bool FunctionPassManager_New::run(Function &F) { 763 std::string errstr; 764 if (MP->materializeFunction(&F, &errstr)) { 765 cerr << "Error reading bytecode file: " << errstr << "\n"; 766 abort(); 767 } 768 return FPM->runOnFunction(F); 769} 770 771 772/// doInitialization - Run all of the initializers for the function passes. 773/// 774bool FunctionPassManager_New::doInitialization() { 775 return FPM->doInitialization(*MP->getModule()); 776} 777 778/// doFinalization - Run all of the initializers for the function passes. 779/// 780bool FunctionPassManager_New::doFinalization() { 781 return FPM->doFinalization(*MP->getModule()); 782} 783 784//===----------------------------------------------------------------------===// 785// FunctionPassManagerImpl_New implementation 786 787/// Add pass P into the pass manager queue. If P is a BasicBlockPass then 788/// either use it into active basic block pass manager or create new basic 789/// block pass manager to handle pass P. 790bool 791FunctionPassManagerImpl_New::addPass(Pass *P) { 792 793 // If P is a BasicBlockPass then use BasicBlockPassManager_New. 794 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) { 795 796 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) { 797 798 // If active manager exists then clear its analysis info. 799 if (activeBBPassManager) 800 activeBBPassManager->initializeAnalysisInfo(); 801 802 // Create and add new manager 803 activeBBPassManager = 804 new BasicBlockPassManager_New(getDepth() + 1); 805 addPassToManager(activeBBPassManager, false); 806 807 // Add pass into new manager. This time it must succeed. 808 if (!activeBBPassManager->addPass(BP)) 809 assert(0 && "Unable to add Pass"); 810 } 811 812 if (!ForcedLastUses.empty()) 813 TPM->setLastUser(ForcedLastUses, this); 814 815 return true; 816 } 817 818 FunctionPass *FP = dynamic_cast<FunctionPass *>(P); 819 if (!FP) 820 return false; 821 822 // If this pass does not preserve anlysis that is used by other passes 823 // managed by this manager than it is not a suiable pass for this manager. 824 if (!manageablePass(P)) 825 return false; 826 827 addPassToManager (FP); 828 829 // If active manager exists then clear its analysis info. 830 if (activeBBPassManager) { 831 activeBBPassManager->initializeAnalysisInfo(); 832 activeBBPassManager = NULL; 833 } 834 835 return true; 836} 837 838/// Execute all of the passes scheduled for execution by invoking 839/// runOnFunction method. Keep track of whether any of the passes modifies 840/// the function, and if so, return true. 841bool FunctionPassManagerImpl_New::runOnModule(Module &M) { 842 843 bool Changed = doInitialization(M); 844 initializeAnalysisInfo(); 845 846 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 847 this->runOnFunction(*I); 848 849 return Changed | doFinalization(M); 850} 851 852/// Execute all of the passes scheduled for execution by invoking 853/// runOnFunction method. Keep track of whether any of the passes modifies 854/// the function, and if so, return true. 855bool FunctionPassManagerImpl_New::runOnFunction(Function &F) { 856 857 bool Changed = false; 858 initializeAnalysisInfo(); 859 860 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 861 e = passVectorEnd(); itr != e; ++itr) { 862 Pass *P = *itr; 863 864 FunctionPass *FP = dynamic_cast<FunctionPass*>(P); 865 Changed |= FP->runOnFunction(F); 866 removeNotPreservedAnalysis(P); 867 recordAvailableAnalysis(P); 868 removeDeadPasses(P); 869 } 870 return Changed; 871} 872 873 874/// Return true IFF AnalysisID AID is currently available. 875/// TODO : Replace this method with getAnalysisPass() 876Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) { 877 878 Pass *P = getAnalysisPass(AID); 879 if (P) 880 return P; 881 882 if (activeBBPassManager && 883 activeBBPassManager->getAnalysisPass(AID) != 0) 884 return activeBBPassManager->getAnalysisPass(AID); 885 886 // TODO : Check inactive managers 887 return NULL; 888} 889 890inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) { 891 bool Changed = false; 892 893 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 894 e = passVectorEnd(); itr != e; ++itr) { 895 Pass *P = *itr; 896 897 FunctionPass *FP = dynamic_cast<FunctionPass*>(P); 898 Changed |= FP->doInitialization(M); 899 } 900 901 return Changed; 902} 903 904inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) { 905 bool Changed = false; 906 907 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 908 e = passVectorEnd(); itr != e; ++itr) { 909 Pass *P = *itr; 910 911 FunctionPass *FP = dynamic_cast<FunctionPass*>(P); 912 Changed |= FP->doFinalization(M); 913 } 914 915 916 return Changed; 917} 918 919//===----------------------------------------------------------------------===// 920// ModulePassManager implementation 921 922/// Add P into pass vector if it is manageble. If P is a FunctionPass 923/// then use FunctionPassManagerImpl_New to manage it. Return false if P 924/// is not manageable by this manager. 925bool 926ModulePassManager_New::addPass(Pass *P) { 927 928 // If P is FunctionPass then use function pass maanager. 929 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) { 930 931 if (!activeFunctionPassManager 932 || !activeFunctionPassManager->addPass(P)) { 933 934 // If active manager exists then clear its analysis info. 935 if (activeFunctionPassManager) 936 activeFunctionPassManager->initializeAnalysisInfo(); 937 938 // Create and add new manager 939 activeFunctionPassManager = 940 new FunctionPassManagerImpl_New(getDepth() + 1); 941 addPassToManager(activeFunctionPassManager, false); 942 943 // Add pass into new manager. This time it must succeed. 944 if (!activeFunctionPassManager->addPass(FP)) 945 assert(0 && "Unable to add pass"); 946 } 947 948 if (!ForcedLastUses.empty()) 949 TPM->setLastUser(ForcedLastUses, this); 950 951 return true; 952 } 953 954 ModulePass *MP = dynamic_cast<ModulePass *>(P); 955 if (!MP) 956 return false; 957 958 // If this pass does not preserve anlysis that is used by other passes 959 // managed by this manager than it is not a suiable pass for this manager. 960 if (!manageablePass(P)) 961 return false; 962 963 addPassToManager(MP); 964 // If active manager exists then clear its analysis info. 965 if (activeFunctionPassManager) { 966 activeFunctionPassManager->initializeAnalysisInfo(); 967 activeFunctionPassManager = NULL; 968 } 969 970 return true; 971} 972 973 974/// Execute all of the passes scheduled for execution by invoking 975/// runOnModule method. Keep track of whether any of the passes modifies 976/// the module, and if so, return true. 977bool 978ModulePassManager_New::runOnModule(Module &M) { 979 bool Changed = false; 980 initializeAnalysisInfo(); 981 982 for (std::vector<Pass *>::iterator itr = passVectorBegin(), 983 e = passVectorEnd(); itr != e; ++itr) { 984 Pass *P = *itr; 985 986 ModulePass *MP = dynamic_cast<ModulePass*>(P); 987 Changed |= MP->runOnModule(M); 988 removeNotPreservedAnalysis(P); 989 recordAvailableAnalysis(P); 990 removeDeadPasses(P); 991 } 992 return Changed; 993} 994 995/// Return true IFF AnalysisID AID is currently available. 996/// TODO : Replace this method with getAnalysisPass() 997Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) { 998 999 Pass *P = getAnalysisPass(AID); 1000 if (P) 1001 return P; 1002 1003 if (activeFunctionPassManager && 1004 activeFunctionPassManager->getAnalysisPass(AID) != 0) 1005 return activeFunctionPassManager->getAnalysisPass(AID); 1006 1007 // TODO : Check inactive managers 1008 return NULL; 1009} 1010 1011//===----------------------------------------------------------------------===// 1012// PassManagerImpl implementation 1013 1014/// Return true IFF AnalysisID AID is currently available. 1015/// TODO : Replace this method with getAnalysisPass() 1016Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) { 1017 1018 Pass *P = NULL; 1019 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), 1020 e = PassManagers.end(); !P && itr != e; ++itr) 1021 P = (*itr)->getAnalysisPassFromManager(AID); 1022 return P; 1023} 1024 1025// PassManager_New implementation 1026/// Add P into active pass manager or use new module pass manager to 1027/// manage it. 1028bool PassManagerImpl_New::addPass(Pass *P) { 1029 1030 if (!activeManager || !activeManager->addPass(P)) { 1031 activeManager = new ModulePassManager_New(getDepth() + 1); 1032 PassManagers.push_back(activeManager); 1033 return activeManager->addPass(P); 1034 } 1035 return true; 1036} 1037 1038/// run - Execute all of the passes scheduled for execution. Keep track of 1039/// whether any of the passes modifies the module, and if so, return true. 1040bool PassManagerImpl_New::run(Module &M) { 1041 1042 bool Changed = false; 1043 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), 1044 e = PassManagers.end(); itr != e; ++itr) { 1045 ModulePassManager_New *pm = *itr; 1046 Changed |= pm->runOnModule(M); 1047 } 1048 return Changed; 1049} 1050 1051//===----------------------------------------------------------------------===// 1052// PassManager implementation 1053 1054/// Create new pass manager 1055PassManager_New::PassManager_New() { 1056 PM = new PassManagerImpl_New(0); 1057} 1058 1059/// add - Add a pass to the queue of passes to run. This passes ownership of 1060/// the Pass to the PassManager. When the PassManager is destroyed, the pass 1061/// will be destroyed as well, so there is no need to delete the pass. This 1062/// implies that all passes MUST be allocated with 'new'. 1063void 1064PassManager_New::add(Pass *P) { 1065 PM->add(P); 1066} 1067 1068/// run - Execute all of the passes scheduled for execution. Keep track of 1069/// whether any of the passes modifies the module, and if so, return true. 1070bool 1071PassManager_New::run(Module &M) { 1072 return PM->run(M); 1073} 1074 1075