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