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