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