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