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