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