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