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