1 //===- LegacyPassManager.cpp - LLVM Pass Infrastructure Implementation ----===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the legacy LLVM Pass Manager infrastructure. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/IR/LegacyPassManager.h" 14 #include "llvm/ADT/MapVector.h" 15 #include "llvm/ADT/Statistic.h" 16 #include "llvm/IR/DiagnosticInfo.h" 17 #include "llvm/IR/IRPrintingPasses.h" 18 #include "llvm/IR/LLVMContext.h" 19 #include "llvm/IR/LegacyPassManagers.h" 20 #include "llvm/IR/LegacyPassNameParser.h" 21 #include "llvm/IR/Module.h" 22 #include "llvm/IR/PassTimingInfo.h" 23 #include "llvm/Support/Chrono.h" 24 #include "llvm/Support/CommandLine.h" 25 #include "llvm/Support/Debug.h" 26 #include "llvm/Support/Error.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include "llvm/Support/ManagedStatic.h" 29 #include "llvm/Support/Mutex.h" 30 #include "llvm/Support/TimeProfiler.h" 31 #include "llvm/Support/Timer.h" 32 #include "llvm/Support/raw_ostream.h" 33 #include <algorithm> 34 #include <unordered_set> 35 using namespace llvm; 36 using namespace llvm::legacy; 37 38 // See PassManagers.h for Pass Manager infrastructure overview. 39 40 //===----------------------------------------------------------------------===// 41 // Pass debugging information. Often it is useful to find out what pass is 42 // running when a crash occurs in a utility. When this library is compiled with 43 // debugging on, a command line option (--debug-pass) is enabled that causes the 44 // pass name to be printed before it executes. 45 // 46 47 namespace { 48 // Different debug levels that can be enabled... 49 enum PassDebugLevel { 50 Disabled, Arguments, Structure, Executions, Details 51 }; 52 } 53 54 static cl::opt<enum PassDebugLevel> 55 PassDebugging("debug-pass", cl::Hidden, 56 cl::desc("Print PassManager debugging information"), 57 cl::values( 58 clEnumVal(Disabled , "disable debug output"), 59 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"), 60 clEnumVal(Structure , "print pass structure before run()"), 61 clEnumVal(Executions, "print pass name before it is executed"), 62 clEnumVal(Details , "print pass details when it is executed"))); 63 64 namespace { 65 typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser> 66 PassOptionList; 67 } 68 69 // Print IR out before/after specified passes. 70 static PassOptionList 71 PrintBefore("print-before", 72 llvm::cl::desc("Print IR before specified passes"), 73 cl::Hidden); 74 75 static PassOptionList 76 PrintAfter("print-after", 77 llvm::cl::desc("Print IR after specified passes"), 78 cl::Hidden); 79 80 static cl::opt<bool> PrintBeforeAll("print-before-all", 81 llvm::cl::desc("Print IR before each pass"), 82 cl::init(false), cl::Hidden); 83 static cl::opt<bool> PrintAfterAll("print-after-all", 84 llvm::cl::desc("Print IR after each pass"), 85 cl::init(false), cl::Hidden); 86 87 static cl::opt<bool> 88 PrintModuleScope("print-module-scope", 89 cl::desc("When printing IR for print-[before|after]{-all} " 90 "always print a module IR"), 91 cl::init(false), cl::Hidden); 92 93 static cl::list<std::string> 94 PrintFuncsList("filter-print-funcs", cl::value_desc("function names"), 95 cl::desc("Only print IR for functions whose name " 96 "match this for all print-[before|after][-all] " 97 "options"), 98 cl::CommaSeparated, cl::Hidden); 99 100 /// This is a helper to determine whether to print IR before or 101 /// after a pass. 102 103 bool llvm::shouldPrintBeforePass() { 104 return PrintBeforeAll || !PrintBefore.empty(); 105 } 106 107 bool llvm::shouldPrintAfterPass() { 108 return PrintAfterAll || !PrintAfter.empty(); 109 } 110 111 static bool ShouldPrintBeforeOrAfterPass(StringRef PassID, 112 PassOptionList &PassesToPrint) { 113 for (auto *PassInf : PassesToPrint) { 114 if (PassInf) 115 if (PassInf->getPassArgument() == PassID) { 116 return true; 117 } 118 } 119 return false; 120 } 121 122 bool llvm::shouldPrintBeforePass(StringRef PassID) { 123 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PassID, PrintBefore); 124 } 125 126 bool llvm::shouldPrintAfterPass(StringRef PassID) { 127 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PassID, PrintAfter); 128 } 129 130 bool llvm::forcePrintModuleIR() { return PrintModuleScope; } 131 132 bool llvm::isFunctionInPrintList(StringRef FunctionName) { 133 static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(), 134 PrintFuncsList.end()); 135 return PrintFuncNames.empty() || PrintFuncNames.count(FunctionName); 136 } 137 /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions 138 /// or higher is specified. 139 bool PMDataManager::isPassDebuggingExecutionsOrMore() const { 140 return PassDebugging >= Executions; 141 } 142 143 unsigned PMDataManager::initSizeRemarkInfo( 144 Module &M, StringMap<std::pair<unsigned, unsigned>> &FunctionToInstrCount) { 145 // Only calculate getInstructionCount if the size-info remark is requested. 146 unsigned InstrCount = 0; 147 148 // Collect instruction counts for every function. We'll use this to emit 149 // per-function size remarks later. 150 for (Function &F : M) { 151 unsigned FCount = F.getInstructionCount(); 152 153 // Insert a record into FunctionToInstrCount keeping track of the current 154 // size of the function as the first member of a pair. Set the second 155 // member to 0; if the function is deleted by the pass, then when we get 156 // here, we'll be able to let the user know that F no longer contributes to 157 // the module. 158 FunctionToInstrCount[F.getName().str()] = 159 std::pair<unsigned, unsigned>(FCount, 0); 160 InstrCount += FCount; 161 } 162 return InstrCount; 163 } 164 165 void PMDataManager::emitInstrCountChangedRemark( 166 Pass *P, Module &M, int64_t Delta, unsigned CountBefore, 167 StringMap<std::pair<unsigned, unsigned>> &FunctionToInstrCount, 168 Function *F) { 169 // If it's a pass manager, don't emit a remark. (This hinges on the assumption 170 // that the only passes that return non-null with getAsPMDataManager are pass 171 // managers.) The reason we have to do this is to avoid emitting remarks for 172 // CGSCC passes. 173 if (P->getAsPMDataManager()) 174 return; 175 176 // Set to true if this isn't a module pass or CGSCC pass. 177 bool CouldOnlyImpactOneFunction = (F != nullptr); 178 179 // Helper lambda that updates the changes to the size of some function. 180 auto UpdateFunctionChanges = 181 [&FunctionToInstrCount](Function &MaybeChangedFn) { 182 // Update the total module count. 183 unsigned FnSize = MaybeChangedFn.getInstructionCount(); 184 auto It = FunctionToInstrCount.find(MaybeChangedFn.getName()); 185 186 // If we created a new function, then we need to add it to the map and 187 // say that it changed from 0 instructions to FnSize. 188 if (It == FunctionToInstrCount.end()) { 189 FunctionToInstrCount[MaybeChangedFn.getName()] = 190 std::pair<unsigned, unsigned>(0, FnSize); 191 return; 192 } 193 // Insert the new function size into the second member of the pair. This 194 // tells us whether or not this function changed in size. 195 It->second.second = FnSize; 196 }; 197 198 // We need to initially update all of the function sizes. 199 // If no function was passed in, then we're either a module pass or an 200 // CGSCC pass. 201 if (!CouldOnlyImpactOneFunction) 202 std::for_each(M.begin(), M.end(), UpdateFunctionChanges); 203 else 204 UpdateFunctionChanges(*F); 205 206 // Do we have a function we can use to emit a remark? 207 if (!CouldOnlyImpactOneFunction) { 208 // We need a function containing at least one basic block in order to output 209 // remarks. Since it's possible that the first function in the module 210 // doesn't actually contain a basic block, we have to go and find one that's 211 // suitable for emitting remarks. 212 auto It = std::find_if(M.begin(), M.end(), 213 [](const Function &Fn) { return !Fn.empty(); }); 214 215 // Didn't find a function. Quit. 216 if (It == M.end()) 217 return; 218 219 // We found a function containing at least one basic block. 220 F = &*It; 221 } 222 int64_t CountAfter = static_cast<int64_t>(CountBefore) + Delta; 223 BasicBlock &BB = *F->begin(); 224 OptimizationRemarkAnalysis R("size-info", "IRSizeChange", 225 DiagnosticLocation(), &BB); 226 // FIXME: Move ore namespace to DiagnosticInfo so that we can use it. This 227 // would let us use NV instead of DiagnosticInfoOptimizationBase::Argument. 228 R << DiagnosticInfoOptimizationBase::Argument("Pass", P->getPassName()) 229 << ": IR instruction count changed from " 230 << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", CountBefore) 231 << " to " 232 << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", CountAfter) 233 << "; Delta: " 234 << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", Delta); 235 F->getContext().diagnose(R); // Not using ORE for layering reasons. 236 237 // Emit per-function size change remarks separately. 238 std::string PassName = P->getPassName().str(); 239 240 // Helper lambda that emits a remark when the size of a function has changed. 241 auto EmitFunctionSizeChangedRemark = [&FunctionToInstrCount, &F, &BB, 242 &PassName](const std::string &Fname) { 243 unsigned FnCountBefore, FnCountAfter; 244 std::pair<unsigned, unsigned> &Change = FunctionToInstrCount[Fname]; 245 std::tie(FnCountBefore, FnCountAfter) = Change; 246 int64_t FnDelta = static_cast<int64_t>(FnCountAfter) - 247 static_cast<int64_t>(FnCountBefore); 248 249 if (FnDelta == 0) 250 return; 251 252 // FIXME: We shouldn't use BB for the location here. Unfortunately, because 253 // the function that we're looking at could have been deleted, we can't use 254 // it for the source location. We *want* remarks when a function is deleted 255 // though, so we're kind of stuck here as is. (This remark, along with the 256 // whole-module size change remarks really ought not to have source 257 // locations at all.) 258 OptimizationRemarkAnalysis FR("size-info", "FunctionIRSizeChange", 259 DiagnosticLocation(), &BB); 260 FR << DiagnosticInfoOptimizationBase::Argument("Pass", PassName) 261 << ": Function: " 262 << DiagnosticInfoOptimizationBase::Argument("Function", Fname) 263 << ": IR instruction count changed from " 264 << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", 265 FnCountBefore) 266 << " to " 267 << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", 268 FnCountAfter) 269 << "; Delta: " 270 << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", FnDelta); 271 F->getContext().diagnose(FR); 272 273 // Update the function size. 274 Change.first = FnCountAfter; 275 }; 276 277 // Are we looking at more than one function? If so, emit remarks for all of 278 // the functions in the module. Otherwise, only emit one remark. 279 if (!CouldOnlyImpactOneFunction) 280 std::for_each(FunctionToInstrCount.keys().begin(), 281 FunctionToInstrCount.keys().end(), 282 EmitFunctionSizeChangedRemark); 283 else 284 EmitFunctionSizeChangedRemark(F->getName().str()); 285 } 286 287 void PassManagerPrettyStackEntry::print(raw_ostream &OS) const { 288 if (!V && !M) 289 OS << "Releasing pass '"; 290 else 291 OS << "Running pass '"; 292 293 OS << P->getPassName() << "'"; 294 295 if (M) { 296 OS << " on module '" << M->getModuleIdentifier() << "'.\n"; 297 return; 298 } 299 if (!V) { 300 OS << '\n'; 301 return; 302 } 303 304 OS << " on "; 305 if (isa<Function>(V)) 306 OS << "function"; 307 else if (isa<BasicBlock>(V)) 308 OS << "basic block"; 309 else 310 OS << "value"; 311 312 OS << " '"; 313 V->printAsOperand(OS, /*PrintType=*/false, M); 314 OS << "'\n"; 315 } 316 317 namespace llvm { 318 namespace legacy { 319 //===----------------------------------------------------------------------===// 320 // FunctionPassManagerImpl 321 // 322 /// FunctionPassManagerImpl manages FPPassManagers 323 class FunctionPassManagerImpl : public Pass, 324 public PMDataManager, 325 public PMTopLevelManager { 326 virtual void anchor(); 327 private: 328 bool wasRun; 329 public: 330 static char ID; 331 explicit FunctionPassManagerImpl() : 332 Pass(PT_PassManager, ID), PMDataManager(), 333 PMTopLevelManager(new FPPassManager()), wasRun(false) {} 334 335 /// \copydoc FunctionPassManager::add() 336 void add(Pass *P) { 337 schedulePass(P); 338 } 339 340 /// createPrinterPass - Get a function printer pass. 341 Pass *createPrinterPass(raw_ostream &O, 342 const std::string &Banner) const override { 343 return createPrintFunctionPass(O, Banner); 344 } 345 346 // Prepare for running an on the fly pass, freeing memory if needed 347 // from a previous run. 348 void releaseMemoryOnTheFly(); 349 350 /// run - Execute all of the passes scheduled for execution. Keep track of 351 /// whether any of the passes modifies the module, and if so, return true. 352 bool run(Function &F); 353 354 /// doInitialization - Run all of the initializers for the function passes. 355 /// 356 bool doInitialization(Module &M) override; 357 358 /// doFinalization - Run all of the finalizers for the function passes. 359 /// 360 bool doFinalization(Module &M) override; 361 362 363 PMDataManager *getAsPMDataManager() override { return this; } 364 Pass *getAsPass() override { return this; } 365 PassManagerType getTopLevelPassManagerType() override { 366 return PMT_FunctionPassManager; 367 } 368 369 /// Pass Manager itself does not invalidate any analysis info. 370 void getAnalysisUsage(AnalysisUsage &Info) const override { 371 Info.setPreservesAll(); 372 } 373 374 FPPassManager *getContainedManager(unsigned N) { 375 assert(N < PassManagers.size() && "Pass number out of range!"); 376 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]); 377 return FP; 378 } 379 }; 380 381 void FunctionPassManagerImpl::anchor() {} 382 383 char FunctionPassManagerImpl::ID = 0; 384 } // End of legacy namespace 385 } // End of llvm namespace 386 387 namespace { 388 //===----------------------------------------------------------------------===// 389 // MPPassManager 390 // 391 /// MPPassManager manages ModulePasses and function pass managers. 392 /// It batches all Module passes and function pass managers together and 393 /// sequences them to process one module. 394 class MPPassManager : public Pass, public PMDataManager { 395 public: 396 static char ID; 397 explicit MPPassManager() : 398 Pass(PT_PassManager, ID), PMDataManager() { } 399 400 // Delete on the fly managers. 401 ~MPPassManager() override { 402 for (auto &OnTheFlyManager : OnTheFlyManagers) { 403 FunctionPassManagerImpl *FPP = OnTheFlyManager.second; 404 delete FPP; 405 } 406 } 407 408 /// createPrinterPass - Get a module printer pass. 409 Pass *createPrinterPass(raw_ostream &O, 410 const std::string &Banner) const override { 411 return createPrintModulePass(O, Banner); 412 } 413 414 /// run - Execute all of the passes scheduled for execution. Keep track of 415 /// whether any of the passes modifies the module, and if so, return true. 416 bool runOnModule(Module &M); 417 418 using llvm::Pass::doInitialization; 419 using llvm::Pass::doFinalization; 420 421 /// Pass Manager itself does not invalidate any analysis info. 422 void getAnalysisUsage(AnalysisUsage &Info) const override { 423 Info.setPreservesAll(); 424 } 425 426 /// Add RequiredPass into list of lower level passes required by pass P. 427 /// RequiredPass is run on the fly by Pass Manager when P requests it 428 /// through getAnalysis interface. 429 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override; 430 431 /// Return function pass corresponding to PassInfo PI, that is 432 /// required by module pass MP. Instantiate analysis pass, by using 433 /// its runOnFunction() for function F. 434 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override; 435 436 StringRef getPassName() const override { return "Module Pass Manager"; } 437 438 PMDataManager *getAsPMDataManager() override { return this; } 439 Pass *getAsPass() override { return this; } 440 441 // Print passes managed by this manager 442 void dumpPassStructure(unsigned Offset) override { 443 dbgs().indent(Offset*2) << "ModulePass Manager\n"; 444 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 445 ModulePass *MP = getContainedPass(Index); 446 MP->dumpPassStructure(Offset + 1); 447 MapVector<Pass *, FunctionPassManagerImpl *>::const_iterator I = 448 OnTheFlyManagers.find(MP); 449 if (I != OnTheFlyManagers.end()) 450 I->second->dumpPassStructure(Offset + 2); 451 dumpLastUses(MP, Offset+1); 452 } 453 } 454 455 ModulePass *getContainedPass(unsigned N) { 456 assert(N < PassVector.size() && "Pass number out of range!"); 457 return static_cast<ModulePass *>(PassVector[N]); 458 } 459 460 PassManagerType getPassManagerType() const override { 461 return PMT_ModulePassManager; 462 } 463 464 private: 465 /// Collection of on the fly FPPassManagers. These managers manage 466 /// function passes that are required by module passes. 467 MapVector<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers; 468 }; 469 470 char MPPassManager::ID = 0; 471 } // End anonymous namespace 472 473 namespace llvm { 474 namespace legacy { 475 //===----------------------------------------------------------------------===// 476 // PassManagerImpl 477 // 478 479 /// PassManagerImpl manages MPPassManagers 480 class PassManagerImpl : public Pass, 481 public PMDataManager, 482 public PMTopLevelManager { 483 virtual void anchor(); 484 485 public: 486 static char ID; 487 explicit PassManagerImpl() : 488 Pass(PT_PassManager, ID), PMDataManager(), 489 PMTopLevelManager(new MPPassManager()) {} 490 491 /// \copydoc PassManager::add() 492 void add(Pass *P) { 493 schedulePass(P); 494 } 495 496 /// createPrinterPass - Get a module printer pass. 497 Pass *createPrinterPass(raw_ostream &O, 498 const std::string &Banner) const override { 499 return createPrintModulePass(O, Banner); 500 } 501 502 /// run - Execute all of the passes scheduled for execution. Keep track of 503 /// whether any of the passes modifies the module, and if so, return true. 504 bool run(Module &M); 505 506 using llvm::Pass::doInitialization; 507 using llvm::Pass::doFinalization; 508 509 /// Pass Manager itself does not invalidate any analysis info. 510 void getAnalysisUsage(AnalysisUsage &Info) const override { 511 Info.setPreservesAll(); 512 } 513 514 PMDataManager *getAsPMDataManager() override { return this; } 515 Pass *getAsPass() override { return this; } 516 PassManagerType getTopLevelPassManagerType() override { 517 return PMT_ModulePassManager; 518 } 519 520 MPPassManager *getContainedManager(unsigned N) { 521 assert(N < PassManagers.size() && "Pass number out of range!"); 522 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]); 523 return MP; 524 } 525 }; 526 527 void PassManagerImpl::anchor() {} 528 529 char PassManagerImpl::ID = 0; 530 } // End of legacy namespace 531 } // End of llvm namespace 532 533 //===----------------------------------------------------------------------===// 534 // PMTopLevelManager implementation 535 536 /// Initialize top level manager. Create first pass manager. 537 PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) { 538 PMDM->setTopLevelManager(this); 539 addPassManager(PMDM); 540 activeStack.push(PMDM); 541 } 542 543 /// Set pass P as the last user of the given analysis passes. 544 void 545 PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) { 546 unsigned PDepth = 0; 547 if (P->getResolver()) 548 PDepth = P->getResolver()->getPMDataManager().getDepth(); 549 550 for (Pass *AP : AnalysisPasses) { 551 LastUser[AP] = P; 552 553 if (P == AP) 554 continue; 555 556 // Update the last users of passes that are required transitive by AP. 557 AnalysisUsage *AnUsage = findAnalysisUsage(AP); 558 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet(); 559 SmallVector<Pass *, 12> LastUses; 560 SmallVector<Pass *, 12> LastPMUses; 561 for (AnalysisID ID : IDs) { 562 Pass *AnalysisPass = findAnalysisPass(ID); 563 assert(AnalysisPass && "Expected analysis pass to exist."); 564 AnalysisResolver *AR = AnalysisPass->getResolver(); 565 assert(AR && "Expected analysis resolver to exist."); 566 unsigned APDepth = AR->getPMDataManager().getDepth(); 567 568 if (PDepth == APDepth) 569 LastUses.push_back(AnalysisPass); 570 else if (PDepth > APDepth) 571 LastPMUses.push_back(AnalysisPass); 572 } 573 574 setLastUser(LastUses, P); 575 576 // If this pass has a corresponding pass manager, push higher level 577 // analysis to this pass manager. 578 if (P->getResolver()) 579 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass()); 580 581 582 // If AP is the last user of other passes then make P last user of 583 // such passes. 584 for (auto LU : LastUser) { 585 if (LU.second == AP) 586 // DenseMap iterator is not invalidated here because 587 // this is just updating existing entries. 588 LastUser[LU.first] = P; 589 } 590 } 591 } 592 593 /// Collect passes whose last user is P 594 void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses, 595 Pass *P) { 596 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI = 597 InversedLastUser.find(P); 598 if (DMI == InversedLastUser.end()) 599 return; 600 601 SmallPtrSet<Pass *, 8> &LU = DMI->second; 602 for (Pass *LUP : LU) { 603 LastUses.push_back(LUP); 604 } 605 606 } 607 608 AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) { 609 AnalysisUsage *AnUsage = nullptr; 610 auto DMI = AnUsageMap.find(P); 611 if (DMI != AnUsageMap.end()) 612 AnUsage = DMI->second; 613 else { 614 // Look up the analysis usage from the pass instance (different instances 615 // of the same pass can produce different results), but unique the 616 // resulting object to reduce memory usage. This helps to greatly reduce 617 // memory usage when we have many instances of only a few pass types 618 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set 619 // of dependencies. 620 AnalysisUsage AU; 621 P->getAnalysisUsage(AU); 622 623 AUFoldingSetNode* Node = nullptr; 624 FoldingSetNodeID ID; 625 AUFoldingSetNode::Profile(ID, AU); 626 void *IP = nullptr; 627 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP)) 628 Node = N; 629 else { 630 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU); 631 UniqueAnalysisUsages.InsertNode(Node, IP); 632 } 633 assert(Node && "cached analysis usage must be non null"); 634 635 AnUsageMap[P] = &Node->AU; 636 AnUsage = &Node->AU; 637 } 638 return AnUsage; 639 } 640 641 /// Schedule pass P for execution. Make sure that passes required by 642 /// P are run before P is run. Update analysis info maintained by 643 /// the manager. Remove dead passes. This is a recursive function. 644 void PMTopLevelManager::schedulePass(Pass *P) { 645 646 // TODO : Allocate function manager for this pass, other wise required set 647 // may be inserted into previous function manager 648 649 // Give pass a chance to prepare the stage. 650 P->preparePassManager(activeStack); 651 652 // If P is an analysis pass and it is available then do not 653 // generate the analysis again. Stale analysis info should not be 654 // available at this point. 655 const PassInfo *PI = findAnalysisPassInfo(P->getPassID()); 656 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) { 657 // Remove any cached AnalysisUsage information. 658 AnUsageMap.erase(P); 659 delete P; 660 return; 661 } 662 663 AnalysisUsage *AnUsage = findAnalysisUsage(P); 664 665 bool checkAnalysis = true; 666 while (checkAnalysis) { 667 checkAnalysis = false; 668 669 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet(); 670 for (const AnalysisID ID : RequiredSet) { 671 672 Pass *AnalysisPass = findAnalysisPass(ID); 673 if (!AnalysisPass) { 674 const PassInfo *PI = findAnalysisPassInfo(ID); 675 676 if (!PI) { 677 // Pass P is not in the global PassRegistry 678 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n"; 679 dbgs() << "Verify if there is a pass dependency cycle." << "\n"; 680 dbgs() << "Required Passes:" << "\n"; 681 for (const AnalysisID ID2 : RequiredSet) { 682 if (ID == ID2) 683 break; 684 Pass *AnalysisPass2 = findAnalysisPass(ID2); 685 if (AnalysisPass2) { 686 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n"; 687 } else { 688 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n"; 689 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n"; 690 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n"; 691 } 692 } 693 } 694 695 assert(PI && "Expected required passes to be initialized"); 696 AnalysisPass = PI->createPass(); 697 if (P->getPotentialPassManagerType () == 698 AnalysisPass->getPotentialPassManagerType()) 699 // Schedule analysis pass that is managed by the same pass manager. 700 schedulePass(AnalysisPass); 701 else if (P->getPotentialPassManagerType () > 702 AnalysisPass->getPotentialPassManagerType()) { 703 // Schedule analysis pass that is managed by a new manager. 704 schedulePass(AnalysisPass); 705 // Recheck analysis passes to ensure that required analyses that 706 // are already checked are still available. 707 checkAnalysis = true; 708 } else 709 // Do not schedule this analysis. Lower level analysis 710 // passes are run on the fly. 711 delete AnalysisPass; 712 } 713 } 714 } 715 716 // Now all required passes are available. 717 if (ImmutablePass *IP = P->getAsImmutablePass()) { 718 // P is a immutable pass and it will be managed by this 719 // top level manager. Set up analysis resolver to connect them. 720 PMDataManager *DM = getAsPMDataManager(); 721 AnalysisResolver *AR = new AnalysisResolver(*DM); 722 P->setResolver(AR); 723 DM->initializeAnalysisImpl(P); 724 addImmutablePass(IP); 725 DM->recordAvailableAnalysis(IP); 726 return; 727 } 728 729 if (PI && !PI->isAnalysis() && shouldPrintBeforePass(PI->getPassArgument())) { 730 Pass *PP = P->createPrinterPass( 731 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str()); 732 PP->assignPassManager(activeStack, getTopLevelPassManagerType()); 733 } 734 735 // Add the requested pass to the best available pass manager. 736 P->assignPassManager(activeStack, getTopLevelPassManagerType()); 737 738 if (PI && !PI->isAnalysis() && shouldPrintAfterPass(PI->getPassArgument())) { 739 Pass *PP = P->createPrinterPass( 740 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str()); 741 PP->assignPassManager(activeStack, getTopLevelPassManagerType()); 742 } 743 } 744 745 /// Find the pass that implements Analysis AID. Search immutable 746 /// passes and all pass managers. If desired pass is not found 747 /// then return NULL. 748 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { 749 // For immutable passes we have a direct mapping from ID to pass, so check 750 // that first. 751 if (Pass *P = ImmutablePassMap.lookup(AID)) 752 return P; 753 754 // Check pass managers 755 for (PMDataManager *PassManager : PassManagers) 756 if (Pass *P = PassManager->findAnalysisPass(AID, false)) 757 return P; 758 759 // Check other pass managers 760 for (PMDataManager *IndirectPassManager : IndirectPassManagers) 761 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false)) 762 return P; 763 764 return nullptr; 765 } 766 767 const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const { 768 const PassInfo *&PI = AnalysisPassInfos[AID]; 769 if (!PI) 770 PI = PassRegistry::getPassRegistry()->getPassInfo(AID); 771 else 772 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) && 773 "The pass info pointer changed for an analysis ID!"); 774 775 return PI; 776 } 777 778 void PMTopLevelManager::addImmutablePass(ImmutablePass *P) { 779 P->initializePass(); 780 ImmutablePasses.push_back(P); 781 782 // Add this pass to the map from its analysis ID. We clobber any prior runs 783 // of the pass in the map so that the last one added is the one found when 784 // doing lookups. 785 AnalysisID AID = P->getPassID(); 786 ImmutablePassMap[AID] = P; 787 788 // Also add any interfaces implemented by the immutable pass to the map for 789 // fast lookup. 790 const PassInfo *PassInf = findAnalysisPassInfo(AID); 791 assert(PassInf && "Expected all immutable passes to be initialized"); 792 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented()) 793 ImmutablePassMap[ImmPI->getTypeInfo()] = P; 794 } 795 796 // Print passes managed by this top level manager. 797 void PMTopLevelManager::dumpPasses() const { 798 799 if (PassDebugging < Structure) 800 return; 801 802 // Print out the immutable passes 803 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { 804 ImmutablePasses[i]->dumpPassStructure(0); 805 } 806 807 // Every class that derives from PMDataManager also derives from Pass 808 // (sometimes indirectly), but there's no inheritance relationship 809 // between PMDataManager and Pass, so we have to getAsPass to get 810 // from a PMDataManager* to a Pass*. 811 for (PMDataManager *Manager : PassManagers) 812 Manager->getAsPass()->dumpPassStructure(1); 813 } 814 815 void PMTopLevelManager::dumpArguments() const { 816 817 if (PassDebugging < Arguments) 818 return; 819 820 dbgs() << "Pass Arguments: "; 821 for (ImmutablePass *P : ImmutablePasses) 822 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) { 823 assert(PI && "Expected all immutable passes to be initialized"); 824 if (!PI->isAnalysisGroup()) 825 dbgs() << " -" << PI->getPassArgument(); 826 } 827 for (PMDataManager *PM : PassManagers) 828 PM->dumpPassArguments(); 829 dbgs() << "\n"; 830 } 831 832 void PMTopLevelManager::initializeAllAnalysisInfo() { 833 for (PMDataManager *PM : PassManagers) 834 PM->initializeAnalysisInfo(); 835 836 // Initailize other pass managers 837 for (PMDataManager *IPM : IndirectPassManagers) 838 IPM->initializeAnalysisInfo(); 839 840 for (auto LU : LastUser) { 841 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second]; 842 L.insert(LU.first); 843 } 844 } 845 846 /// Destructor 847 PMTopLevelManager::~PMTopLevelManager() { 848 for (PMDataManager *PM : PassManagers) 849 delete PM; 850 851 for (ImmutablePass *P : ImmutablePasses) 852 delete P; 853 } 854 855 //===----------------------------------------------------------------------===// 856 // PMDataManager implementation 857 858 /// Augement AvailableAnalysis by adding analysis made available by pass P. 859 void PMDataManager::recordAvailableAnalysis(Pass *P) { 860 AnalysisID PI = P->getPassID(); 861 862 AvailableAnalysis[PI] = P; 863 864 assert(!AvailableAnalysis.empty()); 865 866 // This pass is the current implementation of all of the interfaces it 867 // implements as well. 868 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI); 869 if (!PInf) return; 870 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented(); 871 for (unsigned i = 0, e = II.size(); i != e; ++i) 872 AvailableAnalysis[II[i]->getTypeInfo()] = P; 873 } 874 875 // Return true if P preserves high level analysis used by other 876 // passes managed by this manager 877 bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) { 878 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); 879 if (AnUsage->getPreservesAll()) 880 return true; 881 882 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); 883 for (Pass *P1 : HigherLevelAnalysis) { 884 if (P1->getAsImmutablePass() == nullptr && 885 !is_contained(PreservedSet, P1->getPassID())) 886 return false; 887 } 888 889 return true; 890 } 891 892 /// verifyPreservedAnalysis -- Verify analysis preserved by pass P. 893 void PMDataManager::verifyPreservedAnalysis(Pass *P) { 894 // Don't do this unless assertions are enabled. 895 #ifdef NDEBUG 896 return; 897 #endif 898 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); 899 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); 900 901 // Verify preserved analysis 902 for (AnalysisID AID : PreservedSet) { 903 if (Pass *AP = findAnalysisPass(AID, true)) { 904 TimeRegion PassTimer(getPassTimer(AP)); 905 AP->verifyAnalysis(); 906 } 907 } 908 } 909 910 /// Remove Analysis not preserved by Pass P 911 void PMDataManager::removeNotPreservedAnalysis(Pass *P) { 912 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); 913 if (AnUsage->getPreservesAll()) 914 return; 915 916 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); 917 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), 918 E = AvailableAnalysis.end(); I != E; ) { 919 DenseMap<AnalysisID, Pass*>::iterator Info = I++; 920 if (Info->second->getAsImmutablePass() == nullptr && 921 !is_contained(PreservedSet, Info->first)) { 922 // Remove this analysis 923 if (PassDebugging >= Details) { 924 Pass *S = Info->second; 925 dbgs() << " -- '" << P->getPassName() << "' is not preserving '"; 926 dbgs() << S->getPassName() << "'\n"; 927 } 928 AvailableAnalysis.erase(Info); 929 } 930 } 931 932 // Check inherited analysis also. If P is not preserving analysis 933 // provided by parent manager then remove it here. 934 for (unsigned Index = 0; Index < PMT_Last; ++Index) { 935 936 if (!InheritedAnalysis[Index]) 937 continue; 938 939 for (DenseMap<AnalysisID, Pass*>::iterator 940 I = InheritedAnalysis[Index]->begin(), 941 E = InheritedAnalysis[Index]->end(); I != E; ) { 942 DenseMap<AnalysisID, Pass *>::iterator Info = I++; 943 if (Info->second->getAsImmutablePass() == nullptr && 944 !is_contained(PreservedSet, Info->first)) { 945 // Remove this analysis 946 if (PassDebugging >= Details) { 947 Pass *S = Info->second; 948 dbgs() << " -- '" << P->getPassName() << "' is not preserving '"; 949 dbgs() << S->getPassName() << "'\n"; 950 } 951 InheritedAnalysis[Index]->erase(Info); 952 } 953 } 954 } 955 } 956 957 /// Remove analysis passes that are not used any longer 958 void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg, 959 enum PassDebuggingString DBG_STR) { 960 961 SmallVector<Pass *, 12> DeadPasses; 962 963 // If this is a on the fly manager then it does not have TPM. 964 if (!TPM) 965 return; 966 967 TPM->collectLastUses(DeadPasses, P); 968 969 if (PassDebugging >= Details && !DeadPasses.empty()) { 970 dbgs() << " -*- '" << P->getPassName(); 971 dbgs() << "' is the last user of following pass instances."; 972 dbgs() << " Free these instances\n"; 973 } 974 975 for (Pass *P : DeadPasses) 976 freePass(P, Msg, DBG_STR); 977 } 978 979 void PMDataManager::freePass(Pass *P, StringRef Msg, 980 enum PassDebuggingString DBG_STR) { 981 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg); 982 983 { 984 // If the pass crashes releasing memory, remember this. 985 PassManagerPrettyStackEntry X(P); 986 TimeRegion PassTimer(getPassTimer(P)); 987 988 P->releaseMemory(); 989 } 990 991 AnalysisID PI = P->getPassID(); 992 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) { 993 // Remove the pass itself (if it is not already removed). 994 AvailableAnalysis.erase(PI); 995 996 // Remove all interfaces this pass implements, for which it is also 997 // listed as the available implementation. 998 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented(); 999 for (unsigned i = 0, e = II.size(); i != e; ++i) { 1000 DenseMap<AnalysisID, Pass*>::iterator Pos = 1001 AvailableAnalysis.find(II[i]->getTypeInfo()); 1002 if (Pos != AvailableAnalysis.end() && Pos->second == P) 1003 AvailableAnalysis.erase(Pos); 1004 } 1005 } 1006 } 1007 1008 /// Add pass P into the PassVector. Update 1009 /// AvailableAnalysis appropriately if ProcessAnalysis is true. 1010 void PMDataManager::add(Pass *P, bool ProcessAnalysis) { 1011 // This manager is going to manage pass P. Set up analysis resolver 1012 // to connect them. 1013 AnalysisResolver *AR = new AnalysisResolver(*this); 1014 P->setResolver(AR); 1015 1016 // If a FunctionPass F is the last user of ModulePass info M 1017 // then the F's manager, not F, records itself as a last user of M. 1018 SmallVector<Pass *, 12> TransferLastUses; 1019 1020 if (!ProcessAnalysis) { 1021 // Add pass 1022 PassVector.push_back(P); 1023 return; 1024 } 1025 1026 // At the moment, this pass is the last user of all required passes. 1027 SmallVector<Pass *, 12> LastUses; 1028 SmallVector<Pass *, 8> UsedPasses; 1029 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable; 1030 1031 unsigned PDepth = this->getDepth(); 1032 1033 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P); 1034 for (Pass *PUsed : UsedPasses) { 1035 unsigned RDepth = 0; 1036 1037 assert(PUsed->getResolver() && "Analysis Resolver is not set"); 1038 PMDataManager &DM = PUsed->getResolver()->getPMDataManager(); 1039 RDepth = DM.getDepth(); 1040 1041 if (PDepth == RDepth) 1042 LastUses.push_back(PUsed); 1043 else if (PDepth > RDepth) { 1044 // Let the parent claim responsibility of last use 1045 TransferLastUses.push_back(PUsed); 1046 // Keep track of higher level analysis used by this manager. 1047 HigherLevelAnalysis.push_back(PUsed); 1048 } else 1049 llvm_unreachable("Unable to accommodate Used Pass"); 1050 } 1051 1052 // Set P as P's last user until someone starts using P. 1053 // However, if P is a Pass Manager then it does not need 1054 // to record its last user. 1055 if (!P->getAsPMDataManager()) 1056 LastUses.push_back(P); 1057 TPM->setLastUser(LastUses, P); 1058 1059 if (!TransferLastUses.empty()) { 1060 Pass *My_PM = getAsPass(); 1061 TPM->setLastUser(TransferLastUses, My_PM); 1062 TransferLastUses.clear(); 1063 } 1064 1065 // Now, take care of required analyses that are not available. 1066 for (AnalysisID ID : ReqAnalysisNotAvailable) { 1067 const PassInfo *PI = TPM->findAnalysisPassInfo(ID); 1068 Pass *AnalysisPass = PI->createPass(); 1069 this->addLowerLevelRequiredPass(P, AnalysisPass); 1070 } 1071 1072 // Take a note of analysis required and made available by this pass. 1073 // Remove the analysis not preserved by this pass 1074 removeNotPreservedAnalysis(P); 1075 recordAvailableAnalysis(P); 1076 1077 // Add pass 1078 PassVector.push_back(P); 1079 } 1080 1081 1082 /// Populate UP with analysis pass that are used or required by 1083 /// pass P and are available. Populate RP_NotAvail with analysis 1084 /// pass that are required by pass P but are not available. 1085 void PMDataManager::collectRequiredAndUsedAnalyses( 1086 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail, 1087 Pass *P) { 1088 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); 1089 1090 for (const auto &UsedID : AnUsage->getUsedSet()) 1091 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true)) 1092 UP.push_back(AnalysisPass); 1093 1094 for (const auto &RequiredID : AnUsage->getRequiredSet()) 1095 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true)) 1096 UP.push_back(AnalysisPass); 1097 else 1098 RP_NotAvail.push_back(RequiredID); 1099 1100 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet()) 1101 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true)) 1102 UP.push_back(AnalysisPass); 1103 else 1104 RP_NotAvail.push_back(RequiredID); 1105 } 1106 1107 // All Required analyses should be available to the pass as it runs! Here 1108 // we fill in the AnalysisImpls member of the pass so that it can 1109 // successfully use the getAnalysis() method to retrieve the 1110 // implementations it needs. 1111 // 1112 void PMDataManager::initializeAnalysisImpl(Pass *P) { 1113 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); 1114 1115 for (const AnalysisID ID : AnUsage->getRequiredSet()) { 1116 Pass *Impl = findAnalysisPass(ID, true); 1117 if (!Impl) 1118 // This may be analysis pass that is initialized on the fly. 1119 // If that is not the case then it will raise an assert when it is used. 1120 continue; 1121 AnalysisResolver *AR = P->getResolver(); 1122 assert(AR && "Analysis Resolver is not set"); 1123 AR->addAnalysisImplsPair(ID, Impl); 1124 } 1125 } 1126 1127 /// Find the pass that implements Analysis AID. If desired pass is not found 1128 /// then return NULL. 1129 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) { 1130 1131 // Check if AvailableAnalysis map has one entry. 1132 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID); 1133 1134 if (I != AvailableAnalysis.end()) 1135 return I->second; 1136 1137 // Search Parents through TopLevelManager 1138 if (SearchParent) 1139 return TPM->findAnalysisPass(AID); 1140 1141 return nullptr; 1142 } 1143 1144 // Print list of passes that are last used by P. 1145 void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{ 1146 1147 SmallVector<Pass *, 12> LUses; 1148 1149 // If this is a on the fly manager then it does not have TPM. 1150 if (!TPM) 1151 return; 1152 1153 TPM->collectLastUses(LUses, P); 1154 1155 for (Pass *P : LUses) { 1156 dbgs() << "--" << std::string(Offset*2, ' '); 1157 P->dumpPassStructure(0); 1158 } 1159 } 1160 1161 void PMDataManager::dumpPassArguments() const { 1162 for (Pass *P : PassVector) { 1163 if (PMDataManager *PMD = P->getAsPMDataManager()) 1164 PMD->dumpPassArguments(); 1165 else 1166 if (const PassInfo *PI = 1167 TPM->findAnalysisPassInfo(P->getPassID())) 1168 if (!PI->isAnalysisGroup()) 1169 dbgs() << " -" << PI->getPassArgument(); 1170 } 1171 } 1172 1173 void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1, 1174 enum PassDebuggingString S2, 1175 StringRef Msg) { 1176 if (PassDebugging < Executions) 1177 return; 1178 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this 1179 << std::string(getDepth() * 2 + 1, ' '); 1180 switch (S1) { 1181 case EXECUTION_MSG: 1182 dbgs() << "Executing Pass '" << P->getPassName(); 1183 break; 1184 case MODIFICATION_MSG: 1185 dbgs() << "Made Modification '" << P->getPassName(); 1186 break; 1187 case FREEING_MSG: 1188 dbgs() << " Freeing Pass '" << P->getPassName(); 1189 break; 1190 default: 1191 break; 1192 } 1193 switch (S2) { 1194 case ON_FUNCTION_MSG: 1195 dbgs() << "' on Function '" << Msg << "'...\n"; 1196 break; 1197 case ON_MODULE_MSG: 1198 dbgs() << "' on Module '" << Msg << "'...\n"; 1199 break; 1200 case ON_REGION_MSG: 1201 dbgs() << "' on Region '" << Msg << "'...\n"; 1202 break; 1203 case ON_LOOP_MSG: 1204 dbgs() << "' on Loop '" << Msg << "'...\n"; 1205 break; 1206 case ON_CG_MSG: 1207 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n"; 1208 break; 1209 default: 1210 break; 1211 } 1212 } 1213 1214 void PMDataManager::dumpRequiredSet(const Pass *P) const { 1215 if (PassDebugging < Details) 1216 return; 1217 1218 AnalysisUsage analysisUsage; 1219 P->getAnalysisUsage(analysisUsage); 1220 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet()); 1221 } 1222 1223 void PMDataManager::dumpPreservedSet(const Pass *P) const { 1224 if (PassDebugging < Details) 1225 return; 1226 1227 AnalysisUsage analysisUsage; 1228 P->getAnalysisUsage(analysisUsage); 1229 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet()); 1230 } 1231 1232 void PMDataManager::dumpUsedSet(const Pass *P) const { 1233 if (PassDebugging < Details) 1234 return; 1235 1236 AnalysisUsage analysisUsage; 1237 P->getAnalysisUsage(analysisUsage); 1238 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet()); 1239 } 1240 1241 void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P, 1242 const AnalysisUsage::VectorType &Set) const { 1243 assert(PassDebugging >= Details); 1244 if (Set.empty()) 1245 return; 1246 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:"; 1247 for (unsigned i = 0; i != Set.size(); ++i) { 1248 if (i) dbgs() << ','; 1249 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]); 1250 if (!PInf) { 1251 // Some preserved passes, such as AliasAnalysis, may not be initialized by 1252 // all drivers. 1253 dbgs() << " Uninitialized Pass"; 1254 continue; 1255 } 1256 dbgs() << ' ' << PInf->getPassName(); 1257 } 1258 dbgs() << '\n'; 1259 } 1260 1261 /// Add RequiredPass into list of lower level passes required by pass P. 1262 /// RequiredPass is run on the fly by Pass Manager when P requests it 1263 /// through getAnalysis interface. 1264 /// This should be handled by specific pass manager. 1265 void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { 1266 if (TPM) { 1267 TPM->dumpArguments(); 1268 TPM->dumpPasses(); 1269 } 1270 1271 // Module Level pass may required Function Level analysis info 1272 // (e.g. dominator info). Pass manager uses on the fly function pass manager 1273 // to provide this on demand. In that case, in Pass manager terminology, 1274 // module level pass is requiring lower level analysis info managed by 1275 // lower level pass manager. 1276 1277 // When Pass manager is not able to order required analysis info, Pass manager 1278 // checks whether any lower level manager will be able to provide this 1279 // analysis info on demand or not. 1280 #ifndef NDEBUG 1281 dbgs() << "Unable to schedule '" << RequiredPass->getPassName(); 1282 dbgs() << "' required by '" << P->getPassName() << "'\n"; 1283 #endif 1284 llvm_unreachable("Unable to schedule pass"); 1285 } 1286 1287 Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) { 1288 llvm_unreachable("Unable to find on the fly pass"); 1289 } 1290 1291 // Destructor 1292 PMDataManager::~PMDataManager() { 1293 for (Pass *P : PassVector) 1294 delete P; 1295 } 1296 1297 //===----------------------------------------------------------------------===// 1298 // NOTE: Is this the right place to define this method ? 1299 // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist. 1300 Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const { 1301 return PM.findAnalysisPass(ID, dir); 1302 } 1303 1304 Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI, 1305 Function &F) { 1306 return PM.getOnTheFlyPass(P, AnalysisPI, F); 1307 } 1308 1309 //===----------------------------------------------------------------------===// 1310 // FunctionPassManager implementation 1311 1312 /// Create new Function pass manager 1313 FunctionPassManager::FunctionPassManager(Module *m) : M(m) { 1314 FPM = new FunctionPassManagerImpl(); 1315 // FPM is the top level manager. 1316 FPM->setTopLevelManager(FPM); 1317 1318 AnalysisResolver *AR = new AnalysisResolver(*FPM); 1319 FPM->setResolver(AR); 1320 } 1321 1322 FunctionPassManager::~FunctionPassManager() { 1323 delete FPM; 1324 } 1325 1326 void FunctionPassManager::add(Pass *P) { 1327 FPM->add(P); 1328 } 1329 1330 /// run - Execute all of the passes scheduled for execution. Keep 1331 /// track of whether any of the passes modifies the function, and if 1332 /// so, return true. 1333 /// 1334 bool FunctionPassManager::run(Function &F) { 1335 handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) { 1336 report_fatal_error("Error reading bitcode file: " + EIB.message()); 1337 }); 1338 return FPM->run(F); 1339 } 1340 1341 1342 /// doInitialization - Run all of the initializers for the function passes. 1343 /// 1344 bool FunctionPassManager::doInitialization() { 1345 return FPM->doInitialization(*M); 1346 } 1347 1348 /// doFinalization - Run all of the finalizers for the function passes. 1349 /// 1350 bool FunctionPassManager::doFinalization() { 1351 return FPM->doFinalization(*M); 1352 } 1353 1354 //===----------------------------------------------------------------------===// 1355 // FunctionPassManagerImpl implementation 1356 // 1357 bool FunctionPassManagerImpl::doInitialization(Module &M) { 1358 bool Changed = false; 1359 1360 dumpArguments(); 1361 dumpPasses(); 1362 1363 for (ImmutablePass *ImPass : getImmutablePasses()) 1364 Changed |= ImPass->doInitialization(M); 1365 1366 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) 1367 Changed |= getContainedManager(Index)->doInitialization(M); 1368 1369 return Changed; 1370 } 1371 1372 bool FunctionPassManagerImpl::doFinalization(Module &M) { 1373 bool Changed = false; 1374 1375 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index) 1376 Changed |= getContainedManager(Index)->doFinalization(M); 1377 1378 for (ImmutablePass *ImPass : getImmutablePasses()) 1379 Changed |= ImPass->doFinalization(M); 1380 1381 return Changed; 1382 } 1383 1384 /// cleanup - After running all passes, clean up pass manager cache. 1385 void FPPassManager::cleanup() { 1386 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 1387 FunctionPass *FP = getContainedPass(Index); 1388 AnalysisResolver *AR = FP->getResolver(); 1389 assert(AR && "Analysis Resolver is not set"); 1390 AR->clearAnalysisImpls(); 1391 } 1392 } 1393 1394 void FunctionPassManagerImpl::releaseMemoryOnTheFly() { 1395 if (!wasRun) 1396 return; 1397 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { 1398 FPPassManager *FPPM = getContainedManager(Index); 1399 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) { 1400 FPPM->getContainedPass(Index)->releaseMemory(); 1401 } 1402 } 1403 wasRun = false; 1404 } 1405 1406 // Execute all the passes managed by this top level manager. 1407 // Return true if any function is modified by a pass. 1408 bool FunctionPassManagerImpl::run(Function &F) { 1409 bool Changed = false; 1410 1411 initializeAllAnalysisInfo(); 1412 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { 1413 Changed |= getContainedManager(Index)->runOnFunction(F); 1414 F.getContext().yield(); 1415 } 1416 1417 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) 1418 getContainedManager(Index)->cleanup(); 1419 1420 wasRun = true; 1421 return Changed; 1422 } 1423 1424 //===----------------------------------------------------------------------===// 1425 // FPPassManager implementation 1426 1427 char FPPassManager::ID = 0; 1428 /// Print passes managed by this manager 1429 void FPPassManager::dumpPassStructure(unsigned Offset) { 1430 dbgs().indent(Offset*2) << "FunctionPass Manager\n"; 1431 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 1432 FunctionPass *FP = getContainedPass(Index); 1433 FP->dumpPassStructure(Offset + 1); 1434 dumpLastUses(FP, Offset+1); 1435 } 1436 } 1437 1438 1439 /// Execute all of the passes scheduled for execution by invoking 1440 /// runOnFunction method. Keep track of whether any of the passes modifies 1441 /// the function, and if so, return true. 1442 bool FPPassManager::runOnFunction(Function &F) { 1443 if (F.isDeclaration()) 1444 return false; 1445 1446 bool Changed = false; 1447 Module &M = *F.getParent(); 1448 // Collect inherited analysis from Module level pass manager. 1449 populateInheritedAnalysis(TPM->activeStack); 1450 1451 unsigned InstrCount, FunctionSize = 0; 1452 StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount; 1453 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark(); 1454 // Collect the initial size of the module. 1455 if (EmitICRemark) { 1456 InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount); 1457 FunctionSize = F.getInstructionCount(); 1458 } 1459 1460 llvm::TimeTraceScope FunctionScope("OptFunction", F.getName()); 1461 1462 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 1463 FunctionPass *FP = getContainedPass(Index); 1464 bool LocalChanged = false; 1465 1466 llvm::TimeTraceScope PassScope("RunPass", FP->getPassName()); 1467 1468 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName()); 1469 dumpRequiredSet(FP); 1470 1471 initializeAnalysisImpl(FP); 1472 1473 { 1474 PassManagerPrettyStackEntry X(FP, F); 1475 TimeRegion PassTimer(getPassTimer(FP)); 1476 LocalChanged |= FP->runOnFunction(F); 1477 if (EmitICRemark) { 1478 unsigned NewSize = F.getInstructionCount(); 1479 1480 // Update the size of the function, emit a remark, and update the size 1481 // of the module. 1482 if (NewSize != FunctionSize) { 1483 int64_t Delta = static_cast<int64_t>(NewSize) - 1484 static_cast<int64_t>(FunctionSize); 1485 emitInstrCountChangedRemark(FP, M, Delta, InstrCount, 1486 FunctionToInstrCount, &F); 1487 InstrCount = static_cast<int64_t>(InstrCount) + Delta; 1488 FunctionSize = NewSize; 1489 } 1490 } 1491 } 1492 1493 Changed |= LocalChanged; 1494 if (LocalChanged) 1495 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName()); 1496 dumpPreservedSet(FP); 1497 dumpUsedSet(FP); 1498 1499 verifyPreservedAnalysis(FP); 1500 removeNotPreservedAnalysis(FP); 1501 recordAvailableAnalysis(FP); 1502 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG); 1503 } 1504 1505 return Changed; 1506 } 1507 1508 bool FPPassManager::runOnModule(Module &M) { 1509 bool Changed = false; 1510 1511 for (Function &F : M) 1512 Changed |= runOnFunction(F); 1513 1514 return Changed; 1515 } 1516 1517 bool FPPassManager::doInitialization(Module &M) { 1518 bool Changed = false; 1519 1520 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) 1521 Changed |= getContainedPass(Index)->doInitialization(M); 1522 1523 return Changed; 1524 } 1525 1526 bool FPPassManager::doFinalization(Module &M) { 1527 bool Changed = false; 1528 1529 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index) 1530 Changed |= getContainedPass(Index)->doFinalization(M); 1531 1532 return Changed; 1533 } 1534 1535 //===----------------------------------------------------------------------===// 1536 // MPPassManager implementation 1537 1538 /// Execute all of the passes scheduled for execution by invoking 1539 /// runOnModule method. Keep track of whether any of the passes modifies 1540 /// the module, and if so, return true. 1541 bool 1542 MPPassManager::runOnModule(Module &M) { 1543 llvm::TimeTraceScope TimeScope("OptModule", M.getName()); 1544 1545 bool Changed = false; 1546 1547 // Initialize on-the-fly passes 1548 for (auto &OnTheFlyManager : OnTheFlyManagers) { 1549 FunctionPassManagerImpl *FPP = OnTheFlyManager.second; 1550 Changed |= FPP->doInitialization(M); 1551 } 1552 1553 // Initialize module passes 1554 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) 1555 Changed |= getContainedPass(Index)->doInitialization(M); 1556 1557 unsigned InstrCount; 1558 StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount; 1559 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark(); 1560 // Collect the initial size of the module. 1561 if (EmitICRemark) 1562 InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount); 1563 1564 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 1565 ModulePass *MP = getContainedPass(Index); 1566 bool LocalChanged = false; 1567 1568 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier()); 1569 dumpRequiredSet(MP); 1570 1571 initializeAnalysisImpl(MP); 1572 1573 { 1574 PassManagerPrettyStackEntry X(MP, M); 1575 TimeRegion PassTimer(getPassTimer(MP)); 1576 1577 LocalChanged |= MP->runOnModule(M); 1578 if (EmitICRemark) { 1579 // Update the size of the module. 1580 unsigned ModuleCount = M.getInstructionCount(); 1581 if (ModuleCount != InstrCount) { 1582 int64_t Delta = static_cast<int64_t>(ModuleCount) - 1583 static_cast<int64_t>(InstrCount); 1584 emitInstrCountChangedRemark(MP, M, Delta, InstrCount, 1585 FunctionToInstrCount); 1586 InstrCount = ModuleCount; 1587 } 1588 } 1589 } 1590 1591 Changed |= LocalChanged; 1592 if (LocalChanged) 1593 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG, 1594 M.getModuleIdentifier()); 1595 dumpPreservedSet(MP); 1596 dumpUsedSet(MP); 1597 1598 verifyPreservedAnalysis(MP); 1599 removeNotPreservedAnalysis(MP); 1600 recordAvailableAnalysis(MP); 1601 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG); 1602 } 1603 1604 // Finalize module passes 1605 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index) 1606 Changed |= getContainedPass(Index)->doFinalization(M); 1607 1608 // Finalize on-the-fly passes 1609 for (auto &OnTheFlyManager : OnTheFlyManagers) { 1610 FunctionPassManagerImpl *FPP = OnTheFlyManager.second; 1611 // We don't know when is the last time an on-the-fly pass is run, 1612 // so we need to releaseMemory / finalize here 1613 FPP->releaseMemoryOnTheFly(); 1614 Changed |= FPP->doFinalization(M); 1615 } 1616 1617 return Changed; 1618 } 1619 1620 /// Add RequiredPass into list of lower level passes required by pass P. 1621 /// RequiredPass is run on the fly by Pass Manager when P requests it 1622 /// through getAnalysis interface. 1623 void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { 1624 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager && 1625 "Unable to handle Pass that requires lower level Analysis pass"); 1626 assert((P->getPotentialPassManagerType() < 1627 RequiredPass->getPotentialPassManagerType()) && 1628 "Unable to handle Pass that requires lower level Analysis pass"); 1629 if (!RequiredPass) 1630 return; 1631 1632 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P]; 1633 if (!FPP) { 1634 FPP = new FunctionPassManagerImpl(); 1635 // FPP is the top level manager. 1636 FPP->setTopLevelManager(FPP); 1637 1638 OnTheFlyManagers[P] = FPP; 1639 } 1640 const PassInfo *RequiredPassPI = 1641 TPM->findAnalysisPassInfo(RequiredPass->getPassID()); 1642 1643 Pass *FoundPass = nullptr; 1644 if (RequiredPassPI && RequiredPassPI->isAnalysis()) { 1645 FoundPass = 1646 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID()); 1647 } 1648 if (!FoundPass) { 1649 FoundPass = RequiredPass; 1650 // This should be guaranteed to add RequiredPass to the passmanager given 1651 // that we checked for an available analysis above. 1652 FPP->add(RequiredPass); 1653 } 1654 // Register P as the last user of FoundPass or RequiredPass. 1655 SmallVector<Pass *, 1> LU; 1656 LU.push_back(FoundPass); 1657 FPP->setLastUser(LU, P); 1658 } 1659 1660 /// Return function pass corresponding to PassInfo PI, that is 1661 /// required by module pass MP. Instantiate analysis pass, by using 1662 /// its runOnFunction() for function F. 1663 Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){ 1664 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP]; 1665 assert(FPP && "Unable to find on the fly pass"); 1666 1667 FPP->releaseMemoryOnTheFly(); 1668 FPP->run(F); 1669 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI); 1670 } 1671 1672 1673 //===----------------------------------------------------------------------===// 1674 // PassManagerImpl implementation 1675 1676 // 1677 /// run - Execute all of the passes scheduled for execution. Keep track of 1678 /// whether any of the passes modifies the module, and if so, return true. 1679 bool PassManagerImpl::run(Module &M) { 1680 bool Changed = false; 1681 1682 dumpArguments(); 1683 dumpPasses(); 1684 1685 for (ImmutablePass *ImPass : getImmutablePasses()) 1686 Changed |= ImPass->doInitialization(M); 1687 1688 initializeAllAnalysisInfo(); 1689 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { 1690 Changed |= getContainedManager(Index)->runOnModule(M); 1691 M.getContext().yield(); 1692 } 1693 1694 for (ImmutablePass *ImPass : getImmutablePasses()) 1695 Changed |= ImPass->doFinalization(M); 1696 1697 return Changed; 1698 } 1699 1700 //===----------------------------------------------------------------------===// 1701 // PassManager implementation 1702 1703 /// Create new pass manager 1704 PassManager::PassManager() { 1705 PM = new PassManagerImpl(); 1706 // PM is the top level manager 1707 PM->setTopLevelManager(PM); 1708 } 1709 1710 PassManager::~PassManager() { 1711 delete PM; 1712 } 1713 1714 void PassManager::add(Pass *P) { 1715 PM->add(P); 1716 } 1717 1718 /// run - Execute all of the passes scheduled for execution. Keep track of 1719 /// whether any of the passes modifies the module, and if so, return true. 1720 bool PassManager::run(Module &M) { 1721 return PM->run(M); 1722 } 1723 1724 //===----------------------------------------------------------------------===// 1725 // PMStack implementation 1726 // 1727 1728 // Pop Pass Manager from the stack and clear its analysis info. 1729 void PMStack::pop() { 1730 1731 PMDataManager *Top = this->top(); 1732 Top->initializeAnalysisInfo(); 1733 1734 S.pop_back(); 1735 } 1736 1737 // Push PM on the stack and set its top level manager. 1738 void PMStack::push(PMDataManager *PM) { 1739 assert(PM && "Unable to push. Pass Manager expected"); 1740 assert(PM->getDepth()==0 && "Pass Manager depth set too early"); 1741 1742 if (!this->empty()) { 1743 assert(PM->getPassManagerType() > this->top()->getPassManagerType() 1744 && "pushing bad pass manager to PMStack"); 1745 PMTopLevelManager *TPM = this->top()->getTopLevelManager(); 1746 1747 assert(TPM && "Unable to find top level manager"); 1748 TPM->addIndirectPassManager(PM); 1749 PM->setTopLevelManager(TPM); 1750 PM->setDepth(this->top()->getDepth()+1); 1751 } else { 1752 assert((PM->getPassManagerType() == PMT_ModulePassManager 1753 || PM->getPassManagerType() == PMT_FunctionPassManager) 1754 && "pushing bad pass manager to PMStack"); 1755 PM->setDepth(1); 1756 } 1757 1758 S.push_back(PM); 1759 } 1760 1761 // Dump content of the pass manager stack. 1762 LLVM_DUMP_METHOD void PMStack::dump() const { 1763 for (PMDataManager *Manager : S) 1764 dbgs() << Manager->getAsPass()->getPassName() << ' '; 1765 1766 if (!S.empty()) 1767 dbgs() << '\n'; 1768 } 1769 1770 /// Find appropriate Module Pass Manager in the PM Stack and 1771 /// add self into that manager. 1772 void ModulePass::assignPassManager(PMStack &PMS, 1773 PassManagerType PreferredType) { 1774 // Find Module Pass Manager 1775 while (!PMS.empty()) { 1776 PassManagerType TopPMType = PMS.top()->getPassManagerType(); 1777 if (TopPMType == PreferredType) 1778 break; // We found desired pass manager 1779 else if (TopPMType > PMT_ModulePassManager) 1780 PMS.pop(); // Pop children pass managers 1781 else 1782 break; 1783 } 1784 assert(!PMS.empty() && "Unable to find appropriate Pass Manager"); 1785 PMS.top()->add(this); 1786 } 1787 1788 /// Find appropriate Function Pass Manager or Call Graph Pass Manager 1789 /// in the PM Stack and add self into that manager. 1790 void FunctionPass::assignPassManager(PMStack &PMS, 1791 PassManagerType PreferredType) { 1792 1793 // Find Function Pass Manager 1794 while (!PMS.empty()) { 1795 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager) 1796 PMS.pop(); 1797 else 1798 break; 1799 } 1800 1801 // Create new Function Pass Manager if needed. 1802 FPPassManager *FPP; 1803 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) { 1804 FPP = (FPPassManager *)PMS.top(); 1805 } else { 1806 assert(!PMS.empty() && "Unable to create Function Pass Manager"); 1807 PMDataManager *PMD = PMS.top(); 1808 1809 // [1] Create new Function Pass Manager 1810 FPP = new FPPassManager(); 1811 FPP->populateInheritedAnalysis(PMS); 1812 1813 // [2] Set up new manager's top level manager 1814 PMTopLevelManager *TPM = PMD->getTopLevelManager(); 1815 TPM->addIndirectPassManager(FPP); 1816 1817 // [3] Assign manager to manage this new manager. This may create 1818 // and push new managers into PMS 1819 FPP->assignPassManager(PMS, PMD->getPassManagerType()); 1820 1821 // [4] Push new manager into PMS 1822 PMS.push(FPP); 1823 } 1824 1825 // Assign FPP as the manager of this pass. 1826 FPP->add(this); 1827 } 1828 1829 PassManagerBase::~PassManagerBase() {} 1830