blob: 32840fdeddf799061b9ac2f08a2be2d892b6236b [file] [log] [blame]
Chandler Carruth08632732013-11-09 12:26:54 +00001//===- LegacyPassManager.cpp - LLVM Pass Infrastructure Implementation ----===//
Devang Patela9645c02006-11-07 21:31:57 +00002//
Chandler Carruthca6d7192019-01-19 08:50:56 +00003// 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
Devang Patela9645c02006-11-07 21:31:57 +00006//
7//===----------------------------------------------------------------------===//
8//
Chandler Carruth08632732013-11-09 12:26:54 +00009// This file implements the legacy LLVM Pass Manager infrastructure.
Devang Patela9645c02006-11-07 21:31:57 +000010//
11//===----------------------------------------------------------------------===//
12
Chandler Carruth08632732013-11-09 12:26:54 +000013#include "llvm/IR/LegacyPassManager.h"
Florian Hahn14a94142018-05-24 21:33:17 +000014#include "llvm/ADT/MapVector.h"
James Hendersonc67277e2017-05-16 09:43:21 +000015#include "llvm/ADT/Statistic.h"
Jessica Paquette46e868c2018-05-18 17:26:39 +000016#include "llvm/IR/DiagnosticInfo.h"
Pavel Labath64540e62016-10-25 16:20:07 +000017#include "llvm/IR/IRPrintingPasses.h"
18#include "llvm/IR/LLVMContext.h"
Chandler Carruth08632732013-11-09 12:26:54 +000019#include "llvm/IR/LegacyPassManagers.h"
Chandler Carruth877b5202014-03-04 12:32:42 +000020#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth49d0d3b2014-01-07 11:48:04 +000021#include "llvm/IR/Module.h"
Fedor Sergeev2dc7a042018-08-28 21:06:51 +000022#include "llvm/IR/PassTimingInfo.h"
Arthur Eubanks0a118522020-12-03 10:59:10 -080023#include "llvm/IR/PrintPasses.h"
serge-sans-paille055ea2a2020-08-27 17:31:54 +020024#include "llvm/IR/StructuralHash.h"
Pavel Labath64540e62016-10-25 16:20:07 +000025#include "llvm/Support/Chrono.h"
Devang Patel8b5b72e2006-12-13 20:03:48 +000026#include "llvm/Support/CommandLine.h"
David Greenee0081102010-01-05 01:30:02 +000027#include "llvm/Support/Debug.h"
Peter Collingbourne9c2be2d2016-11-09 17:49:19 +000028#include "llvm/Support/Error.h"
Torok Edwin681f5562009-07-08 18:01:40 +000029#include "llvm/Support/ErrorHandling.h"
Devang Patel75711122006-12-14 00:59:42 +000030#include "llvm/Support/ManagedStatic.h"
Michael J. Spenceraf423d52010-11-29 18:16:10 +000031#include "llvm/Support/Mutex.h"
Anton Afanasyevd506b67b2019-03-30 08:42:48 +000032#include "llvm/Support/TimeProfiler.h"
Chandler Carruth345b8272012-12-03 16:50:05 +000033#include "llvm/Support/Timer.h"
34#include "llvm/Support/raw_ostream.h"
Jeff Cohen18c735d2007-03-05 00:00:42 +000035#include <algorithm>
Weiming Zhao5938f0d2016-01-06 22:55:03 +000036#include <unordered_set>
Dan Gohman42831342007-10-03 19:04:09 +000037using namespace llvm;
Devang Patelff825b32006-12-15 19:39:30 +000038
Devang Pateld023bb42007-01-12 18:52:44 +000039// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patelff15e792006-12-07 18:23:30 +000040
Devang Patel8b5b72e2006-12-13 20:03:48 +000041//===----------------------------------------------------------------------===//
42// Pass debugging information. Often it is useful to find out what pass is
43// running when a crash occurs in a utility. When this library is compiled with
44// debugging on, a command line option (--debug-pass) is enabled that causes the
45// pass name to be printed before it executes.
46//
47
Chandler Carruth08632732013-11-09 12:26:54 +000048namespace {
Devang Patel50706632006-12-13 21:13:31 +000049// Different debug levels that can be enabled...
50enum PassDebugLevel {
Dmitri Gribenko353cc7c2013-05-05 00:40:33 +000051 Disabled, Arguments, Structure, Executions, Details
Devang Patel50706632006-12-13 21:13:31 +000052};
Arthur Eubanks0a118522020-12-03 10:59:10 -080053} // namespace
Devang Patel50706632006-12-13 21:13:31 +000054
Arthur Eubanks5687c522021-02-04 19:34:09 -080055static cl::opt<enum PassDebugLevel> PassDebugging(
56 "debug-pass", cl::Hidden,
57 cl::desc("Print legacy PassManager debugging information"),
58 cl::values(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")));
David Greenec63505d2010-04-02 23:17:14 +000063
Chris Lattnerbc72d152009-09-15 05:03:04 +000064/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
65/// or higher is specified.
66bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
Andrew Trick61cd3a62013-09-19 06:02:43 +000067 return PassDebugging >= Executions;
Chris Lattnerbc72d152009-09-15 05:03:04 +000068}
69
Jessica Paquette6c0520a2018-09-06 21:19:54 +000070unsigned PMDataManager::initSizeRemarkInfo(
71 Module &M, StringMap<std::pair<unsigned, unsigned>> &FunctionToInstrCount) {
Jessica Paquette46e868c2018-05-18 17:26:39 +000072 // Only calculate getInstructionCount if the size-info remark is requested.
Jessica Paquette6c0520a2018-09-06 21:19:54 +000073 unsigned InstrCount = 0;
74
75 // Collect instruction counts for every function. We'll use this to emit
76 // per-function size remarks later.
77 for (Function &F : M) {
78 unsigned FCount = F.getInstructionCount();
79
80 // Insert a record into FunctionToInstrCount keeping track of the current
81 // size of the function as the first member of a pair. Set the second
82 // member to 0; if the function is deleted by the pass, then when we get
83 // here, we'll be able to let the user know that F no longer contributes to
84 // the module.
85 FunctionToInstrCount[F.getName().str()] =
86 std::pair<unsigned, unsigned>(FCount, 0);
87 InstrCount += FCount;
88 }
89 return InstrCount;
Jessica Paquette46e868c2018-05-18 17:26:39 +000090}
Andrew Trick61cd3a62013-09-19 06:02:43 +000091
Jessica Paquette6c0520a2018-09-06 21:19:54 +000092void PMDataManager::emitInstrCountChangedRemark(
93 Pass *P, Module &M, int64_t Delta, unsigned CountBefore,
94 StringMap<std::pair<unsigned, unsigned>> &FunctionToInstrCount,
95 Function *F) {
Jessica Paquettee4a7fcd2018-08-31 20:51:54 +000096 // If it's a pass manager, don't emit a remark. (This hinges on the assumption
97 // that the only passes that return non-null with getAsPMDataManager are pass
98 // managers.) The reason we have to do this is to avoid emitting remarks for
99 // CGSCC passes.
100 if (P->getAsPMDataManager())
101 return;
102
Jessica Paquetteb9d21072018-09-04 21:03:43 +0000103 // Set to true if this isn't a module pass or CGSCC pass.
104 bool CouldOnlyImpactOneFunction = (F != nullptr);
105
Jessica Paquette6c0520a2018-09-06 21:19:54 +0000106 // Helper lambda that updates the changes to the size of some function.
107 auto UpdateFunctionChanges =
108 [&FunctionToInstrCount](Function &MaybeChangedFn) {
109 // Update the total module count.
110 unsigned FnSize = MaybeChangedFn.getInstructionCount();
111 auto It = FunctionToInstrCount.find(MaybeChangedFn.getName());
112
113 // If we created a new function, then we need to add it to the map and
114 // say that it changed from 0 instructions to FnSize.
115 if (It == FunctionToInstrCount.end()) {
116 FunctionToInstrCount[MaybeChangedFn.getName()] =
117 std::pair<unsigned, unsigned>(0, FnSize);
118 return;
119 }
120 // Insert the new function size into the second member of the pair. This
121 // tells us whether or not this function changed in size.
122 It->second.second = FnSize;
123 };
124
125 // We need to initially update all of the function sizes.
126 // If no function was passed in, then we're either a module pass or an
127 // CGSCC pass.
128 if (!CouldOnlyImpactOneFunction)
129 std::for_each(M.begin(), M.end(), UpdateFunctionChanges);
130 else
131 UpdateFunctionChanges(*F);
132
Jessica Paquette7be140c2018-08-31 20:54:37 +0000133 // Do we have a function we can use to emit a remark?
Jessica Paquetteb9d21072018-09-04 21:03:43 +0000134 if (!CouldOnlyImpactOneFunction) {
Jessica Paquette7be140c2018-08-31 20:54:37 +0000135 // We need a function containing at least one basic block in order to output
136 // remarks. Since it's possible that the first function in the module
137 // doesn't actually contain a basic block, we have to go and find one that's
138 // suitable for emitting remarks.
Kazu Hirataa526d462021-01-11 18:48:05 -0800139 auto It = llvm::find_if(M, [](const Function &Fn) { return !Fn.empty(); });
Jessica Paquette46e868c2018-05-18 17:26:39 +0000140
Jessica Paquette7be140c2018-08-31 20:54:37 +0000141 // Didn't find a function. Quit.
142 if (It == M.end())
143 return;
Jessica Paquette46e868c2018-05-18 17:26:39 +0000144
Jessica Paquette7be140c2018-08-31 20:54:37 +0000145 // We found a function containing at least one basic block.
146 F = &*It;
147 }
Jessica Paquettef4da24f2018-08-31 20:20:57 +0000148 int64_t CountAfter = static_cast<int64_t>(CountBefore) + Delta;
Jessica Paquette46e868c2018-05-18 17:26:39 +0000149 BasicBlock &BB = *F->begin();
150 OptimizationRemarkAnalysis R("size-info", "IRSizeChange",
151 DiagnosticLocation(), &BB);
152 // FIXME: Move ore namespace to DiagnosticInfo so that we can use it. This
153 // would let us use NV instead of DiagnosticInfoOptimizationBase::Argument.
154 R << DiagnosticInfoOptimizationBase::Argument("Pass", P->getPassName())
155 << ": IR instruction count changed from "
156 << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", CountBefore)
157 << " to "
158 << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", CountAfter)
159 << "; Delta: "
160 << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", Delta);
161 F->getContext().diagnose(R); // Not using ORE for layering reasons.
Jessica Paquette6c0520a2018-09-06 21:19:54 +0000162
163 // Emit per-function size change remarks separately.
164 std::string PassName = P->getPassName().str();
165
166 // Helper lambda that emits a remark when the size of a function has changed.
167 auto EmitFunctionSizeChangedRemark = [&FunctionToInstrCount, &F, &BB,
Benjamin Kramer5c9e53f2020-01-28 20:23:46 +0100168 &PassName](StringRef Fname) {
Jessica Paquette6c0520a2018-09-06 21:19:54 +0000169 unsigned FnCountBefore, FnCountAfter;
170 std::pair<unsigned, unsigned> &Change = FunctionToInstrCount[Fname];
171 std::tie(FnCountBefore, FnCountAfter) = Change;
172 int64_t FnDelta = static_cast<int64_t>(FnCountAfter) -
173 static_cast<int64_t>(FnCountBefore);
174
175 if (FnDelta == 0)
176 return;
177
178 // FIXME: We shouldn't use BB for the location here. Unfortunately, because
179 // the function that we're looking at could have been deleted, we can't use
180 // it for the source location. We *want* remarks when a function is deleted
181 // though, so we're kind of stuck here as is. (This remark, along with the
182 // whole-module size change remarks really ought not to have source
183 // locations at all.)
184 OptimizationRemarkAnalysis FR("size-info", "FunctionIRSizeChange",
185 DiagnosticLocation(), &BB);
186 FR << DiagnosticInfoOptimizationBase::Argument("Pass", PassName)
187 << ": Function: "
188 << DiagnosticInfoOptimizationBase::Argument("Function", Fname)
189 << ": IR instruction count changed from "
190 << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore",
191 FnCountBefore)
192 << " to "
193 << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter",
194 FnCountAfter)
195 << "; Delta: "
196 << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", FnDelta);
197 F->getContext().diagnose(FR);
198
199 // Update the function size.
200 Change.first = FnCountAfter;
201 };
202
203 // Are we looking at more than one function? If so, emit remarks for all of
204 // the functions in the module. Otherwise, only emit one remark.
205 if (!CouldOnlyImpactOneFunction)
206 std::for_each(FunctionToInstrCount.keys().begin(),
207 FunctionToInstrCount.keys().end(),
208 EmitFunctionSizeChangedRemark);
209 else
210 EmitFunctionSizeChangedRemark(F->getName().str());
Jessica Paquette46e868c2018-05-18 17:26:39 +0000211}
Andrew Trick61cd3a62013-09-19 06:02:43 +0000212
Chris Lattner223d1312009-03-06 06:45:05 +0000213void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
Craig Topperea285482014-04-09 06:08:46 +0000214 if (!V && !M)
Chris Lattner223d1312009-03-06 06:45:05 +0000215 OS << "Releasing pass '";
216 else
217 OS << "Running pass '";
Dan Gohman3d4563a2010-08-12 23:50:08 +0000218
Chris Lattner223d1312009-03-06 06:45:05 +0000219 OS << P->getPassName() << "'";
Dan Gohman3d4563a2010-08-12 23:50:08 +0000220
Chris Lattner223d1312009-03-06 06:45:05 +0000221 if (M) {
222 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
223 return;
224 }
Craig Topperea285482014-04-09 06:08:46 +0000225 if (!V) {
Chris Lattner223d1312009-03-06 06:45:05 +0000226 OS << '\n';
227 return;
228 }
229
Dan Gohmanfc719292009-03-10 18:47:59 +0000230 OS << " on ";
Chris Lattner223d1312009-03-06 06:45:05 +0000231 if (isa<Function>(V))
Dan Gohmanfc719292009-03-10 18:47:59 +0000232 OS << "function";
Chris Lattner223d1312009-03-06 06:45:05 +0000233 else if (isa<BasicBlock>(V))
Dan Gohmanfc719292009-03-10 18:47:59 +0000234 OS << "basic block";
Chris Lattner223d1312009-03-06 06:45:05 +0000235 else
Dan Gohmanfc719292009-03-10 18:47:59 +0000236 OS << "value";
237
238 OS << " '";
Rui Ueyama08343a22019-07-16 04:46:31 +0000239 V->printAsOperand(OS, /*PrintType=*/false, M);
Dan Gohmanfc719292009-03-10 18:47:59 +0000240 OS << "'\n";
Chris Lattner223d1312009-03-06 06:45:05 +0000241}
242
Devang Pateld023bb42007-01-12 18:52:44 +0000243namespace llvm {
Chandler Carruth08632732013-11-09 12:26:54 +0000244namespace legacy {
Arthur Eubanksaa1b43e2021-03-02 14:49:46 -0800245bool debugPassSpecified() { return PassDebugging != Disabled; }
246
Devang Patel9a5447b2006-12-12 22:47:13 +0000247//===----------------------------------------------------------------------===//
Devang Patel96abc762006-12-19 19:46:59 +0000248// FunctionPassManagerImpl
Devang Patel9a5447b2006-12-12 22:47:13 +0000249//
Devang Patel96abc762006-12-19 19:46:59 +0000250/// FunctionPassManagerImpl manages FPPassManagers
251class FunctionPassManagerImpl : public Pass,
Devang Patelafd19792007-01-11 22:15:30 +0000252 public PMDataManager,
253 public PMTopLevelManager {
David Blaikiecd738cc2011-12-20 02:50:00 +0000254 virtual void anchor();
Torok Edwincd0d8462009-06-29 18:49:09 +0000255private:
256 bool wasRun;
Devang Patel96abc762006-12-19 19:46:59 +0000257public:
Devang Patel4b409542007-05-03 01:11:54 +0000258 static char ID;
Andrew Trick87862d12011-08-29 17:07:00 +0000259 explicit FunctionPassManagerImpl() :
260 Pass(PT_PassManager, ID), PMDataManager(),
261 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel96abc762006-12-19 19:46:59 +0000262
Matthias Braun01450ac2014-12-12 01:27:01 +0000263 /// \copydoc FunctionPassManager::add()
Devang Patel96abc762006-12-19 19:46:59 +0000264 void add(Pass *P) {
265 schedulePass(P);
266 }
Dan Gohman3d4563a2010-08-12 23:50:08 +0000267
268 /// createPrinterPass - Get a function printer pass.
Craig Topper33a27982014-03-05 06:35:38 +0000269 Pass *createPrinterPass(raw_ostream &O,
270 const std::string &Banner) const override {
Chandler Carruth394cfcd2014-01-12 11:30:46 +0000271 return createPrintFunctionPass(O, Banner);
David Greenec63505d2010-04-02 23:17:14 +0000272 }
273
Torok Edwincd0d8462009-06-29 18:49:09 +0000274 // Prepare for running an on the fly pass, freeing memory if needed
275 // from a previous run.
276 void releaseMemoryOnTheFly();
277
Devang Patel96abc762006-12-19 19:46:59 +0000278 /// run - Execute all of the passes scheduled for execution. Keep track of
279 /// whether any of the passes modifies the module, and if so, return true.
280 bool run(Function &F);
281
282 /// doInitialization - Run all of the initializers for the function passes.
283 ///
Craig Topper33a27982014-03-05 06:35:38 +0000284 bool doInitialization(Module &M) override;
Dan Gohman3d4563a2010-08-12 23:50:08 +0000285
Dan Gohman31d7ca22007-07-30 14:51:13 +0000286 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel96abc762006-12-19 19:46:59 +0000287 ///
Craig Topper33a27982014-03-05 06:35:38 +0000288 bool doFinalization(Module &M) override;
Devang Patel96abc762006-12-19 19:46:59 +0000289
Dan Gohman3d4563a2010-08-12 23:50:08 +0000290
Craig Topper33a27982014-03-05 06:35:38 +0000291 PMDataManager *getAsPMDataManager() override { return this; }
292 Pass *getAsPass() override { return this; }
293 PassManagerType getTopLevelPassManagerType() override {
Andrew Trick5aeec472012-02-01 07:16:20 +0000294 return PMT_FunctionPassManager;
295 }
Chris Lattner8db741c2010-01-22 05:24:46 +0000296
Devang Patel96abc762006-12-19 19:46:59 +0000297 /// Pass Manager itself does not invalidate any analysis info.
Craig Topper33a27982014-03-05 06:35:38 +0000298 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel96abc762006-12-19 19:46:59 +0000299 Info.setPreservesAll();
300 }
301
Devang Patel96abc762006-12-19 19:46:59 +0000302 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner2072e9c2009-03-06 05:53:14 +0000303 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel96abc762006-12-19 19:46:59 +0000304 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
305 return FP;
306 }
evgeny5699b112019-11-01 14:43:51 +0300307
308 void dumpPassStructure(unsigned Offset) override {
309 for (unsigned I = 0; I < getNumContainedManagers(); ++I)
310 getContainedManager(I)->dumpPassStructure(Offset);
311 }
Devang Patel96abc762006-12-19 19:46:59 +0000312};
313
David Blaikiecd738cc2011-12-20 02:50:00 +0000314void FunctionPassManagerImpl::anchor() {}
315
Devang Patel4b409542007-05-03 01:11:54 +0000316char FunctionPassManagerImpl::ID = 0;
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -0700317
318//===----------------------------------------------------------------------===//
319// FunctionPassManagerImpl implementation
320//
321bool FunctionPassManagerImpl::doInitialization(Module &M) {
322 bool Changed = false;
323
324 dumpArguments();
325 dumpPasses();
326
327 for (ImmutablePass *ImPass : getImmutablePasses())
328 Changed |= ImPass->doInitialization(M);
329
330 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
331 Changed |= getContainedManager(Index)->doInitialization(M);
332
333 return Changed;
334}
335
336bool FunctionPassManagerImpl::doFinalization(Module &M) {
337 bool Changed = false;
338
339 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
340 Changed |= getContainedManager(Index)->doFinalization(M);
341
342 for (ImmutablePass *ImPass : getImmutablePasses())
343 Changed |= ImPass->doFinalization(M);
344
345 return Changed;
346}
347
348void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
349 if (!wasRun)
350 return;
351 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
352 FPPassManager *FPPM = getContainedManager(Index);
353 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
354 FPPM->getContainedPass(Index)->releaseMemory();
355 }
356 }
357 wasRun = false;
358}
359
360// Execute all the passes managed by this top level manager.
361// Return true if any function is modified by a pass.
362bool FunctionPassManagerImpl::run(Function &F) {
363 bool Changed = false;
364
365 initializeAllAnalysisInfo();
366 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
367 Changed |= getContainedManager(Index)->runOnFunction(F);
368 F.getContext().yield();
369 }
370
371 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
372 getContainedManager(Index)->cleanup();
373
374 wasRun = true;
375 return Changed;
376}
Simon Pilgrime2e161f2020-06-25 17:48:29 +0100377} // namespace legacy
378} // namespace llvm
Dan Gohman3d4563a2010-08-12 23:50:08 +0000379
Chandler Carruth08632732013-11-09 12:26:54 +0000380namespace {
Devang Patel96abc762006-12-19 19:46:59 +0000381//===----------------------------------------------------------------------===//
382// MPPassManager
383//
384/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmanf1877e32008-03-11 16:18:48 +0000385/// It batches all Module passes and function pass managers together and
386/// sequences them to process one module.
Devang Patel96abc762006-12-19 19:46:59 +0000387class MPPassManager : public Pass, public PMDataManager {
Devang Patelf1d1a202006-11-08 10:05:38 +0000388public:
Devang Patel4b409542007-05-03 01:11:54 +0000389 static char ID;
Andrew Trick87862d12011-08-29 17:07:00 +0000390 explicit MPPassManager() :
391 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel4bf9ac72007-04-16 20:39:59 +0000392
393 // Delete on the fly managers.
Alexander Kornienkoe4206332015-04-11 02:11:45 +0000394 ~MPPassManager() override {
Yaron Keren84b55422015-06-05 17:48:47 +0000395 for (auto &OnTheFlyManager : OnTheFlyManagers) {
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -0700396 legacy::FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Devang Patel4bf9ac72007-04-16 20:39:59 +0000397 delete FPP;
398 }
399 }
400
Dan Gohman3d4563a2010-08-12 23:50:08 +0000401 /// createPrinterPass - Get a module printer pass.
Craig Topper33a27982014-03-05 06:35:38 +0000402 Pass *createPrinterPass(raw_ostream &O,
403 const std::string &Banner) const override {
Chandler Carruth394cfcd2014-01-12 11:30:46 +0000404 return createPrintModulePass(O, Banner);
David Greenec63505d2010-04-02 23:17:14 +0000405 }
406
Devang Patelf1d1a202006-11-08 10:05:38 +0000407 /// run - Execute all of the passes scheduled for execution. Keep track of
408 /// whether any of the passes modifies the module, and if so, return true.
409 bool runOnModule(Module &M);
Devang Patel0ceb6c72006-11-13 22:40:09 +0000410
Pedro Artigasf9016b32012-12-03 21:56:57 +0000411 using llvm::Pass::doInitialization;
412 using llvm::Pass::doFinalization;
413
Devang Patel6114e602006-12-07 19:57:52 +0000414 /// Pass Manager itself does not invalidate any analysis info.
Craig Topper33a27982014-03-05 06:35:38 +0000415 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel6114e602006-12-07 19:57:52 +0000416 Info.setPreservesAll();
417 }
418
Devang Patel293bbe32007-04-16 20:12:57 +0000419 /// Add RequiredPass into list of lower level passes required by pass P.
420 /// RequiredPass is run on the fly by Pass Manager when P requests it
421 /// through getAnalysis interface.
Craig Topper33a27982014-03-05 06:35:38 +0000422 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patel293bbe32007-04-16 20:12:57 +0000423
Dan Gohman3d4563a2010-08-12 23:50:08 +0000424 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel654393f2007-04-16 20:27:05 +0000425 /// required by module pass MP. Instantiate analysis pass, by using
426 /// its runOnFunction() for function F.
serge-sans-pailleeb62e372020-06-26 13:07:31 +0200427 std::tuple<Pass *, bool> getOnTheFlyPass(Pass *MP, AnalysisID PI,
428 Function &F) override;
Devang Patel654393f2007-04-16 20:27:05 +0000429
Mehdi Amini0fba8cc2016-10-01 02:56:57 +0000430 StringRef getPassName() const override { return "Module Pass Manager"; }
Devang Patel9cf5faf2007-02-01 22:08:25 +0000431
Craig Topper33a27982014-03-05 06:35:38 +0000432 PMDataManager *getAsPMDataManager() override { return this; }
433 Pass *getAsPass() override { return this; }
Chris Lattner8db741c2010-01-22 05:24:46 +0000434
Devang Patel4799abe2006-12-12 23:34:33 +0000435 // Print passes managed by this manager
Craig Topper33a27982014-03-05 06:35:38 +0000436 void dumpPassStructure(unsigned Offset) override {
Eric Christopherf2fe0022014-02-26 23:27:16 +0000437 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patela8ecac42006-12-16 00:56:26 +0000438 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
439 ModulePass *MP = getContainedPass(Index);
Dan Gohman17cd5742010-08-19 01:29:07 +0000440 MP->dumpPassStructure(Offset + 1);
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -0700441 MapVector<Pass *, legacy::FunctionPassManagerImpl *>::const_iterator I =
Florian Hahn14a94142018-05-24 21:33:17 +0000442 OnTheFlyManagers.find(MP);
Dan Gohmanc3b67522009-07-01 23:12:33 +0000443 if (I != OnTheFlyManagers.end())
444 I->second->dumpPassStructure(Offset + 2);
Devang Patela8ecac42006-12-16 00:56:26 +0000445 dumpLastUses(MP, Offset+1);
Devang Patel4799abe2006-12-12 23:34:33 +0000446 }
447 }
448
Devang Patela8ecac42006-12-16 00:56:26 +0000449 ModulePass *getContainedPass(unsigned N) {
Evan Chengeea82f52012-11-13 02:56:38 +0000450 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner2072e9c2009-03-06 05:53:14 +0000451 return static_cast<ModulePass *>(PassVector[N]);
Devang Patela8ecac42006-12-16 00:56:26 +0000452 }
453
Craig Topper33a27982014-03-05 06:35:38 +0000454 PassManagerType getPassManagerType() const override {
Dan Gohman3d4563a2010-08-12 23:50:08 +0000455 return PMT_ModulePassManager;
Devang Patel58489442007-02-27 15:00:39 +0000456 }
Devang Patel654393f2007-04-16 20:27:05 +0000457
458 private:
459 /// Collection of on the fly FPPassManagers. These managers manage
460 /// function passes that are required by module passes.
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -0700461 MapVector<Pass *, legacy::FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelf1d1a202006-11-08 10:05:38 +0000462};
463
Devang Patel4b409542007-05-03 01:11:54 +0000464char MPPassManager::ID = 0;
Chandler Carruth08632732013-11-09 12:26:54 +0000465} // End anonymous namespace
466
467namespace llvm {
468namespace legacy {
Devang Patel9a5447b2006-12-12 22:47:13 +0000469//===----------------------------------------------------------------------===//
Devang Patel96abc762006-12-19 19:46:59 +0000470// PassManagerImpl
Devang Patel9a5447b2006-12-12 22:47:13 +0000471//
Devang Patel920f1e42007-05-01 21:15:47 +0000472
Devang Patel96abc762006-12-19 19:46:59 +0000473/// PassManagerImpl manages MPPassManagers
474class PassManagerImpl : public Pass,
Devang Patelafd19792007-01-11 22:15:30 +0000475 public PMDataManager,
476 public PMTopLevelManager {
David Blaikiecd738cc2011-12-20 02:50:00 +0000477 virtual void anchor();
Devang Patel5d72ec72006-11-08 10:29:57 +0000478
479public:
Devang Patel4b409542007-05-03 01:11:54 +0000480 static char ID;
Andrew Trick87862d12011-08-29 17:07:00 +0000481 explicit PassManagerImpl() :
482 Pass(PT_PassManager, ID), PMDataManager(),
483 PMTopLevelManager(new MPPassManager()) {}
Devang Patel3ab26702006-12-07 23:24:58 +0000484
Matthias Braun01450ac2014-12-12 01:27:01 +0000485 /// \copydoc PassManager::add()
Devang Patel8f425cd2006-12-07 21:32:57 +0000486 void add(Pass *P) {
Devang Patel19b9d402006-12-08 22:34:02 +0000487 schedulePass(P);
Devang Patel8f425cd2006-12-07 21:32:57 +0000488 }
Dan Gohman3d4563a2010-08-12 23:50:08 +0000489
490 /// createPrinterPass - Get a module printer pass.
Craig Topper33a27982014-03-05 06:35:38 +0000491 Pass *createPrinterPass(raw_ostream &O,
492 const std::string &Banner) const override {
Chandler Carruth394cfcd2014-01-12 11:30:46 +0000493 return createPrintModulePass(O, Banner);
David Greenec63505d2010-04-02 23:17:14 +0000494 }
495
Devang Patel5d72ec72006-11-08 10:29:57 +0000496 /// run - Execute all of the passes scheduled for execution. Keep track of
497 /// whether any of the passes modifies the module, and if so, return true.
498 bool run(Module &M);
499
Pedro Artigasf9016b32012-12-03 21:56:57 +0000500 using llvm::Pass::doInitialization;
501 using llvm::Pass::doFinalization;
502
Devang Patel6114e602006-12-07 19:57:52 +0000503 /// Pass Manager itself does not invalidate any analysis info.
Craig Topper33a27982014-03-05 06:35:38 +0000504 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel6114e602006-12-07 19:57:52 +0000505 Info.setPreservesAll();
506 }
507
Craig Topper33a27982014-03-05 06:35:38 +0000508 PMDataManager *getAsPMDataManager() override { return this; }
509 Pass *getAsPass() override { return this; }
510 PassManagerType getTopLevelPassManagerType() override {
Andrew Trick5aeec472012-02-01 07:16:20 +0000511 return PMT_ModulePassManager;
512 }
Chris Lattner8db741c2010-01-22 05:24:46 +0000513
Devang Patel96abc762006-12-19 19:46:59 +0000514 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner2072e9c2009-03-06 05:53:14 +0000515 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel96abc762006-12-19 19:46:59 +0000516 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
517 return MP;
518 }
Devang Patel5d72ec72006-11-08 10:29:57 +0000519};
520
David Blaikiecd738cc2011-12-20 02:50:00 +0000521void PassManagerImpl::anchor() {}
522
Devang Patel4b409542007-05-03 01:11:54 +0000523char PassManagerImpl::ID = 0;
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -0700524
525//===----------------------------------------------------------------------===//
526// PassManagerImpl implementation
527
528//
529/// run - Execute all of the passes scheduled for execution. Keep track of
530/// whether any of the passes modifies the module, and if so, return true.
531bool PassManagerImpl::run(Module &M) {
532 bool Changed = false;
533
534 dumpArguments();
535 dumpPasses();
536
537 for (ImmutablePass *ImPass : getImmutablePasses())
538 Changed |= ImPass->doInitialization(M);
539
540 initializeAllAnalysisInfo();
541 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
542 Changed |= getContainedManager(Index)->runOnModule(M);
543 M.getContext().yield();
544 }
545
546 for (ImmutablePass *ImPass : getImmutablePasses())
547 Changed |= ImPass->doFinalization(M);
548
549 return Changed;
550}
Simon Pilgrime2e161f2020-06-25 17:48:29 +0100551} // namespace legacy
552} // namespace llvm
Devang Patela825cd72007-01-29 23:10:37 +0000553
Devang Pateldfcb74b2006-12-07 19:39:39 +0000554//===----------------------------------------------------------------------===//
Devang Patel9c4869d2006-12-12 22:35:25 +0000555// PMTopLevelManager implementation
556
Devang Patel01e619b2007-01-16 02:00:38 +0000557/// Initialize top level manager. Create first pass manager.
Dan Gohmancb1f2fb2010-08-16 21:38:42 +0000558PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
559 PMDM->setTopLevelManager(this);
560 addPassManager(PMDM);
561 activeStack.push(PMDM);
Devang Patel01e619b2007-01-16 02:00:38 +0000562}
563
Devang Patel9c4869d2006-12-12 22:35:25 +0000564/// Set pass P as the last user of the given analysis passes.
Dan Gohman1600dea2010-10-12 00:12:29 +0000565void
Bill Wendling06f6f0c2012-05-14 07:53:40 +0000566PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosser196dae62011-01-20 21:03:22 +0000567 unsigned PDepth = 0;
568 if (P->getResolver())
569 PDepth = P->getResolver()->getPMDataManager().getDepth();
570
Yaron Keren84b55422015-06-05 17:48:47 +0000571 for (Pass *AP : AnalysisPasses) {
Jay Foad7cacc7b2020-11-27 17:32:01 +0000572 // Record P as the new last user of AP.
573 auto &LastUserOfAP = LastUser[AP];
574 if (LastUserOfAP)
575 InversedLastUser[LastUserOfAP].erase(AP);
576 LastUserOfAP = P;
577 InversedLastUser[P].insert(AP);
Dan Gohman3d4563a2010-08-12 23:50:08 +0000578
Devang Patel6e824ae2007-03-08 19:05:01 +0000579 if (P == AP)
580 continue;
581
Tobias Grosser196dae62011-01-20 21:03:22 +0000582 // Update the last users of passes that are required transitive by AP.
583 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
584 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
585 SmallVector<Pass *, 12> LastUses;
586 SmallVector<Pass *, 12> LastPMUses;
Yaron Keren73b78f72016-04-28 14:49:44 +0000587 for (AnalysisID ID : IDs) {
588 Pass *AnalysisPass = findAnalysisPass(ID);
Tobias Grosser196dae62011-01-20 21:03:22 +0000589 assert(AnalysisPass && "Expected analysis pass to exist.");
590 AnalysisResolver *AR = AnalysisPass->getResolver();
591 assert(AR && "Expected analysis resolver to exist.");
592 unsigned APDepth = AR->getPMDataManager().getDepth();
593
594 if (PDepth == APDepth)
595 LastUses.push_back(AnalysisPass);
596 else if (PDepth > APDepth)
597 LastPMUses.push_back(AnalysisPass);
598 }
599
600 setLastUser(LastUses, P);
601
602 // If this pass has a corresponding pass manager, push higher level
603 // analysis to this pass manager.
604 if (P->getResolver())
605 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
606
Devang Patel9c4869d2006-12-12 22:35:25 +0000607 // If AP is the last user of other passes then make P last user of
608 // such passes.
Jay Foad7cacc7b2020-11-27 17:32:01 +0000609 auto &LastUsedByAP = InversedLastUser[AP];
610 for (Pass *L : LastUsedByAP)
611 LastUser[L] = P;
612 InversedLastUser[P].insert(LastUsedByAP.begin(), LastUsedByAP.end());
613 LastUsedByAP.clear();
Devang Patel9c4869d2006-12-12 22:35:25 +0000614 }
Devang Patel9c4869d2006-12-12 22:35:25 +0000615}
616
617/// Collect passes whose last user is P
Dan Gohmancb3e7f02010-10-12 00:11:18 +0000618void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc50cf512008-08-12 00:26:16 +0000619 Pass *P) {
Jay Foadaeb45e32020-11-27 17:34:35 +0000620 auto DMI = InversedLastUser.find(P);
Devang Patelc50cf512008-08-12 00:26:16 +0000621 if (DMI == InversedLastUser.end())
622 return;
623
Jay Foadaeb45e32020-11-27 17:34:35 +0000624 auto &LU = DMI->second;
625 LastUses.append(LU.begin(), LU.end());
Devang Patel9c4869d2006-12-12 22:35:25 +0000626}
627
Devang Patel8b80f4c2008-08-11 21:13:39 +0000628AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperea285482014-04-09 06:08:46 +0000629 AnalysisUsage *AnUsage = nullptr;
Philip Reamese9042692015-12-04 20:05:04 +0000630 auto DMI = AnUsageMap.find(P);
Dan Gohman3d4563a2010-08-12 23:50:08 +0000631 if (DMI != AnUsageMap.end())
Devang Patel8b80f4c2008-08-11 21:13:39 +0000632 AnUsage = DMI->second;
633 else {
Philip Reamese9042692015-12-04 20:05:04 +0000634 // Look up the analysis usage from the pass instance (different instances
635 // of the same pass can produce different results), but unique the
636 // resulting object to reduce memory usage. This helps to greatly reduce
637 // memory usage when we have many instances of only a few pass types
638 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
639 // of dependencies.
640 AnalysisUsage AU;
641 P->getAnalysisUsage(AU);
Bjorn Pettersson9f4d8722018-07-03 12:39:52 +0000642
Philip Reamese9042692015-12-04 20:05:04 +0000643 AUFoldingSetNode* Node = nullptr;
644 FoldingSetNodeID ID;
645 AUFoldingSetNode::Profile(ID, AU);
646 void *IP = nullptr;
647 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
648 Node = N;
649 else {
Philip Reamescbfe24e2015-12-04 23:48:19 +0000650 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
Philip Reamese9042692015-12-04 20:05:04 +0000651 UniqueAnalysisUsages.InsertNode(Node, IP);
652 }
653 assert(Node && "cached analysis usage must be non null");
654
655 AnUsageMap[P] = &Node->AU;
Mandeep Singh Grang31affaa2017-06-06 05:08:36 +0000656 AnUsage = &Node->AU;
Devang Patel8b80f4c2008-08-11 21:13:39 +0000657 }
658 return AnUsage;
659}
660
Devang Patel9c4869d2006-12-12 22:35:25 +0000661/// Schedule pass P for execution. Make sure that passes required by
662/// P are run before P is run. Update analysis info maintained by
663/// the manager. Remove dead passes. This is a recursive function.
664void PMTopLevelManager::schedulePass(Pass *P) {
665
Devang Patelf02b2512007-01-16 21:43:18 +0000666 // TODO : Allocate function manager for this pass, other wise required set
667 // may be inserted into previous function manager
Devang Patel9c4869d2006-12-12 22:35:25 +0000668
Devang Patel7a8aee92007-03-06 01:06:16 +0000669 // Give pass a chance to prepare the stage.
670 P->preparePassManager(activeStack);
671
Devang Patela10b0b52008-03-18 00:39:19 +0000672 // If P is an analysis pass and it is available then do not
673 // generate the analysis again. Stale analysis info should not be
674 // available at this point.
Chandler Carruth9b3892c2015-01-28 09:47:21 +0000675 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Anderson140a03f2010-08-06 18:33:48 +0000676 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Craig Topperfe8f04a2018-08-20 20:57:30 +0000677 // Remove any cached AnalysisUsage information.
678 AnUsageMap.erase(P);
Nuno Lopes1a411d62008-11-04 23:03:58 +0000679 delete P;
Devang Patel75446db2008-03-19 00:48:41 +0000680 return;
Nuno Lopes1a411d62008-11-04 23:03:58 +0000681 }
Devang Patela10b0b52008-03-18 00:39:19 +0000682
Devang Patel8b80f4c2008-08-11 21:13:39 +0000683 AnalysisUsage *AnUsage = findAnalysisUsage(P);
684
Devang Patel1976d952008-08-14 23:07:48 +0000685 bool checkAnalysis = true;
686 while (checkAnalysis) {
687 checkAnalysis = false;
Dan Gohman3d4563a2010-08-12 23:50:08 +0000688
Devang Patel1976d952008-08-14 23:07:48 +0000689 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Florian Hahnfc3d9cb2017-07-13 10:52:00 +0000690 for (const AnalysisID ID : RequiredSet) {
Dan Gohman3d4563a2010-08-12 23:50:08 +0000691
Florian Hahnfc3d9cb2017-07-13 10:52:00 +0000692 Pass *AnalysisPass = findAnalysisPass(ID);
Devang Patel1976d952008-08-14 23:07:48 +0000693 if (!AnalysisPass) {
Florian Hahnfc3d9cb2017-07-13 10:52:00 +0000694 const PassInfo *PI = findAnalysisPassInfo(ID);
Victor Oliveira1508faa2012-07-18 19:59:29 +0000695
Craig Topperea285482014-04-09 06:08:46 +0000696 if (!PI) {
Victor Oliveira1508faa2012-07-18 19:59:29 +0000697 // Pass P is not in the global PassRegistry
698 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
699 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
700 dbgs() << "Required Passes:" << "\n";
Florian Hahnfc3d9cb2017-07-13 10:52:00 +0000701 for (const AnalysisID ID2 : RequiredSet) {
702 if (ID == ID2)
703 break;
704 Pass *AnalysisPass2 = findAnalysisPass(ID2);
Victor Oliveira1508faa2012-07-18 19:59:29 +0000705 if (AnalysisPass2) {
706 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper5d993c52013-02-06 06:50:38 +0000707 } else {
Victor Oliveira1508faa2012-07-18 19:59:29 +0000708 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
709 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
710 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
711 }
712 }
713 }
714
Andrew Trick46edcef2011-06-03 00:48:58 +0000715 assert(PI && "Expected required passes to be initialized");
Owen Anderson140a03f2010-08-06 18:33:48 +0000716 AnalysisPass = PI->createPass();
Devang Patel1976d952008-08-14 23:07:48 +0000717 if (P->getPotentialPassManagerType () ==
718 AnalysisPass->getPotentialPassManagerType())
719 // Schedule analysis pass that is managed by the same pass manager.
720 schedulePass(AnalysisPass);
721 else if (P->getPotentialPassManagerType () >
722 AnalysisPass->getPotentialPassManagerType()) {
723 // Schedule analysis pass that is managed by a new manager.
724 schedulePass(AnalysisPass);
Dan Gohman64a44872010-08-16 22:57:28 +0000725 // Recheck analysis passes to ensure that required analyses that
Devang Patel1976d952008-08-14 23:07:48 +0000726 // are already checked are still available.
727 checkAnalysis = true;
Craig Topper5d993c52013-02-06 06:50:38 +0000728 } else
Chad Rosier5f96aa82015-03-20 15:45:14 +0000729 // Do not schedule this analysis. Lower level analysis
Devang Patel1976d952008-08-14 23:07:48 +0000730 // passes are run on the fly.
731 delete AnalysisPass;
732 }
Devang Patel9c4869d2006-12-12 22:35:25 +0000733 }
734 }
735
736 // Now all required passes are available.
Andrew Trick5aeec472012-02-01 07:16:20 +0000737 if (ImmutablePass *IP = P->getAsImmutablePass()) {
738 // P is a immutable pass and it will be managed by this
739 // top level manager. Set up analysis resolver to connect them.
740 PMDataManager *DM = getAsPMDataManager();
741 AnalysisResolver *AR = new AnalysisResolver(*DM);
742 P->setResolver(AR);
743 DM->initializeAnalysisImpl(P);
744 addImmutablePass(IP);
745 DM->recordAvailableAnalysis(IP);
746 return;
747 }
748
Fedor Sergeevb8588f82018-09-24 16:08:15 +0000749 if (PI && !PI->isAnalysis() && shouldPrintBeforePass(PI->getPassArgument())) {
Nicolas Guillemoteab6fe942021-02-25 11:56:48 -0800750 Pass *PP =
751 P->createPrinterPass(dbgs(), ("*** IR Dump Before " + P->getPassName() +
752 " (" + PI->getPassArgument() + ") ***")
753 .str());
Andrew Trick5aeec472012-02-01 07:16:20 +0000754 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
755 }
756
757 // Add the requested pass to the best available pass manager.
758 P->assignPassManager(activeStack, getTopLevelPassManagerType());
759
Fedor Sergeevb8588f82018-09-24 16:08:15 +0000760 if (PI && !PI->isAnalysis() && shouldPrintAfterPass(PI->getPassArgument())) {
Nicolas Guillemoteab6fe942021-02-25 11:56:48 -0800761 Pass *PP =
762 P->createPrinterPass(dbgs(), ("*** IR Dump After " + P->getPassName() +
763 " (" + PI->getPassArgument() + ") ***")
764 .str());
Andrew Trick5aeec472012-02-01 07:16:20 +0000765 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
766 }
Devang Patel9c4869d2006-12-12 22:35:25 +0000767}
768
769/// Find the pass that implements Analysis AID. Search immutable
770/// passes and all pass managers. If desired pass is not found
771/// then return NULL.
772Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruth685efb32015-09-10 02:31:42 +0000773 // For immutable passes we have a direct mapping from ID to pass, so check
774 // that first.
775 if (Pass *P = ImmutablePassMap.lookup(AID))
776 return P;
Devang Patel9c4869d2006-12-12 22:35:25 +0000777
Devang Patel857dfd32006-12-12 22:50:05 +0000778 // Check pass managers
Yaron Keren84b55422015-06-05 17:48:47 +0000779 for (PMDataManager *PassManager : PassManagers)
780 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohmanbf588592010-10-11 23:19:01 +0000781 return P;
Devang Patel857dfd32006-12-12 22:50:05 +0000782
783 // Check other pass managers
Yaron Keren84b55422015-06-05 17:48:47 +0000784 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
785 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohmanbf588592010-10-11 23:19:01 +0000786 return P;
Devang Patel857dfd32006-12-12 22:50:05 +0000787
Craig Topperea285482014-04-09 06:08:46 +0000788 return nullptr;
Devang Patel9c4869d2006-12-12 22:35:25 +0000789}
790
Chandler Carruth9b3892c2015-01-28 09:47:21 +0000791const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
792 const PassInfo *&PI = AnalysisPassInfos[AID];
793 if (!PI)
794 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
795 else
796 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
797 "The pass info pointer changed for an analysis ID!");
798
799 return PI;
800}
801
Chandler Carruth685efb32015-09-10 02:31:42 +0000802void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
803 P->initializePass();
804 ImmutablePasses.push_back(P);
805
806 // Add this pass to the map from its analysis ID. We clobber any prior runs
807 // of the pass in the map so that the last one added is the one found when
808 // doing lookups.
809 AnalysisID AID = P->getPassID();
810 ImmutablePassMap[AID] = P;
811
812 // Also add any interfaces implemented by the immutable pass to the map for
813 // fast lookup.
814 const PassInfo *PassInf = findAnalysisPassInfo(AID);
815 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth80bd6db2015-09-10 04:22:36 +0000816 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruth685efb32015-09-10 02:31:42 +0000817 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
818}
819
Devang Patel4799abe2006-12-12 23:34:33 +0000820// Print passes managed by this top level manager.
Devang Patel5ac21a52006-12-15 20:13:01 +0000821void PMTopLevelManager::dumpPasses() const {
Devang Patel4799abe2006-12-12 23:34:33 +0000822
Andrew Trick61cd3a62013-09-19 06:02:43 +0000823 if (PassDebugging < Structure)
Devang Patel96abc762006-12-19 19:46:59 +0000824 return;
825
Devang Patel4799abe2006-12-12 23:34:33 +0000826 // Print out the immutable passes
827 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohman17cd5742010-08-19 01:29:07 +0000828 ImmutablePasses[i]->dumpPassStructure(0);
Devang Patel4799abe2006-12-12 23:34:33 +0000829 }
Dan Gohman3d4563a2010-08-12 23:50:08 +0000830
Dan Gohman17cd5742010-08-19 01:29:07 +0000831 // Every class that derives from PMDataManager also derives from Pass
832 // (sometimes indirectly), but there's no inheritance relationship
833 // between PMDataManager and Pass, so we have to getAsPass to get
834 // from a PMDataManager* to a Pass*.
Yaron Keren84b55422015-06-05 17:48:47 +0000835 for (PMDataManager *Manager : PassManagers)
836 Manager->getAsPass()->dumpPassStructure(1);
Devang Patel4799abe2006-12-12 23:34:33 +0000837}
838
Devang Patel5ac21a52006-12-15 20:13:01 +0000839void PMTopLevelManager::dumpArguments() const {
Devang Patel57f52ec2006-12-13 22:10:00 +0000840
Andrew Trick61cd3a62013-09-19 06:02:43 +0000841 if (PassDebugging < Arguments)
Devang Patel57f52ec2006-12-13 22:10:00 +0000842 return;
843
David Greenee0081102010-01-05 01:30:02 +0000844 dbgs() << "Pass Arguments: ";
Yaron Keren73b78f72016-04-28 14:49:44 +0000845 for (ImmutablePass *P : ImmutablePasses)
846 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trick46edcef2011-06-03 00:48:58 +0000847 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohman04355c02010-11-11 16:32:17 +0000848 if (!PI->isAnalysisGroup())
849 dbgs() << " -" << PI->getPassArgument();
Andrew Trick46edcef2011-06-03 00:48:58 +0000850 }
Yaron Keren73b78f72016-04-28 14:49:44 +0000851 for (PMDataManager *PM : PassManagers)
852 PM->dumpPassArguments();
David Greenee0081102010-01-05 01:30:02 +0000853 dbgs() << "\n";
Devang Patel57f52ec2006-12-13 22:10:00 +0000854}
855
Devang Patel5cf8b0a2006-12-21 00:16:50 +0000856void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren73b78f72016-04-28 14:49:44 +0000857 for (PMDataManager *PM : PassManagers)
858 PM->initializeAnalysisInfo();
Dan Gohman3d4563a2010-08-12 23:50:08 +0000859
Devang Patel5cf8b0a2006-12-21 00:16:50 +0000860 // Initailize other pass managers
Yaron Keren73b78f72016-04-28 14:49:44 +0000861 for (PMDataManager *IPM : IndirectPassManagers)
862 IPM->initializeAnalysisInfo();
Devang Patel5cf8b0a2006-12-21 00:16:50 +0000863}
864
Devang Pateld023bb42007-01-12 18:52:44 +0000865/// Destructor
866PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren73b78f72016-04-28 14:49:44 +0000867 for (PMDataManager *PM : PassManagers)
868 delete PM;
Dan Gohman3d4563a2010-08-12 23:50:08 +0000869
Yaron Keren73b78f72016-04-28 14:49:44 +0000870 for (ImmutablePass *P : ImmutablePasses)
871 delete P;
Devang Pateld023bb42007-01-12 18:52:44 +0000872}
873
Devang Patel9c4869d2006-12-12 22:35:25 +0000874//===----------------------------------------------------------------------===//
Devang Patel12532f92006-12-07 18:36:24 +0000875// PMDataManager implementation
Devang Patel30781bb2006-11-07 22:35:17 +0000876
Devang Patel77ffe132006-11-11 01:10:19 +0000877/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele7879fa2006-12-07 19:33:53 +0000878void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Anderson140a03f2010-08-06 18:33:48 +0000879 AnalysisID PI = P->getPassID();
Dan Gohman3d4563a2010-08-12 23:50:08 +0000880
Chris Lattner2072e9c2009-03-06 05:53:14 +0000881 AvailableAnalysis[PI] = P;
Dan Gohman3d4563a2010-08-12 23:50:08 +0000882
Dan Gohman069b1972010-08-12 23:46:28 +0000883 assert(!AvailableAnalysis.empty());
Devang Patel77ffe132006-11-11 01:10:19 +0000884
Dan Gohman3d4563a2010-08-12 23:50:08 +0000885 // This pass is the current implementation of all of the interfaces it
886 // implements as well.
Chandler Carruth9b3892c2015-01-28 09:47:21 +0000887 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperea285482014-04-09 06:08:46 +0000888 if (!PInf) return;
Owen Anderson140a03f2010-08-06 18:33:48 +0000889 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Andersonffe71b02010-07-20 16:55:05 +0000890 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Anderson140a03f2010-08-06 18:33:48 +0000891 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel77ffe132006-11-11 01:10:19 +0000892}
893
Devang Patele6be06a2007-03-06 17:52:53 +0000894// Return true if P preserves high level analysis used by other
895// passes managed by this manager
896bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patel8b80f4c2008-08-11 21:13:39 +0000897 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patel8b80f4c2008-08-11 21:13:39 +0000898 if (AnUsage->getPreservesAll())
Devang Patele6be06a2007-03-06 17:52:53 +0000899 return true;
Dan Gohman3d4563a2010-08-12 23:50:08 +0000900
Devang Patel8b80f4c2008-08-11 21:13:39 +0000901 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Keren697cb172016-10-02 19:21:41 +0000902 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperea285482014-04-09 06:08:46 +0000903 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer6d503892016-08-11 22:21:41 +0000904 !is_contained(PreservedSet, P1->getPassID()))
Devang Patel6e824ae2007-03-08 19:05:01 +0000905 return false;
Devang Patele6be06a2007-03-06 17:52:53 +0000906 }
Dan Gohman3d4563a2010-08-12 23:50:08 +0000907
Devang Patele6be06a2007-03-06 17:52:53 +0000908 return true;
909}
910
Chris Lattner092ad4c2008-08-07 07:34:50 +0000911/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patel667a8132007-07-19 18:02:32 +0000912void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner092ad4c2008-08-07 07:34:50 +0000913 // Don't do this unless assertions are enabled.
914#ifdef NDEBUG
915 return;
916#endif
Devang Patel8b80f4c2008-08-11 21:13:39 +0000917 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
918 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel30781bb2006-11-07 22:35:17 +0000919
Devang Patelf25ad212007-07-19 05:36:09 +0000920 // Verify preserved analysis
Yaron Keren697cb172016-10-02 19:21:41 +0000921 for (AnalysisID AID : PreservedSet) {
Dan Gohman79e98662009-09-28 00:27:48 +0000922 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattnerd7cf0bd2010-03-30 04:03:22 +0000923 TimeRegion PassTimer(getPassTimer(AP));
Devang Patel667a8132007-07-19 18:02:32 +0000924 AP->verifyAnalysis();
Dan Gohman79e98662009-09-28 00:27:48 +0000925 }
Devang Patelb8137592008-07-01 17:44:24 +0000926 }
927}
928
Devang Patela75450f2008-07-01 19:50:56 +0000929/// Remove Analysis not preserved by Pass P
Devang Patel667a8132007-07-19 18:02:32 +0000930void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel8b80f4c2008-08-11 21:13:39 +0000931 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
932 if (AnUsage->getPreservesAll())
Devang Patelbc45ee72006-12-07 20:03:49 +0000933 return;
934
Devang Patel8b80f4c2008-08-11 21:13:39 +0000935 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemand563bfe2013-02-26 01:31:59 +0000936 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Pateladc9a142006-12-12 23:07:44 +0000937 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemand563bfe2013-02-26 01:31:59 +0000938 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperea285482014-04-09 06:08:46 +0000939 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer6d503892016-08-11 22:21:41 +0000940 !is_contained(PreservedSet, Info->first)) {
Devang Pateld8771442006-11-11 01:24:55 +0000941 // Remove this analysis
Andrew Trick61cd3a62013-09-19 06:02:43 +0000942 if (PassDebugging >= Details) {
Devang Patel88e89b92008-06-03 01:02:16 +0000943 Pass *S = Info->second;
David Greenee0081102010-01-05 01:30:02 +0000944 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
945 dbgs() << S->getPassName() << "'\n";
Devang Patel88e89b92008-06-03 01:02:16 +0000946 }
Dan Gohman5b32efa2008-11-06 21:57:17 +0000947 AvailableAnalysis.erase(Info);
Devang Patel88e89b92008-06-03 01:02:16 +0000948 }
Devang Pateld8771442006-11-11 01:24:55 +0000949 }
Dan Gohman3d4563a2010-08-12 23:50:08 +0000950
Devang Patel89612512007-03-06 01:55:46 +0000951 // Check inherited analysis also. If P is not preserving analysis
952 // provided by parent manager then remove it here.
Kazu Hirata17bae6f2021-02-27 10:09:25 -0800953 for (DenseMap<AnalysisID, Pass *> *IA : InheritedAnalysis) {
954 if (!IA)
Devang Patel89612512007-03-06 01:55:46 +0000955 continue;
956
Kazu Hirata17bae6f2021-02-27 10:09:25 -0800957 for (DenseMap<AnalysisID, Pass *>::iterator I = IA->begin(),
958 E = IA->end();
959 I != E;) {
Michael Ilsemand563bfe2013-02-26 01:31:59 +0000960 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperea285482014-04-09 06:08:46 +0000961 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer6d503892016-08-11 22:21:41 +0000962 !is_contained(PreservedSet, Info->first)) {
Devang Patel89612512007-03-06 01:55:46 +0000963 // Remove this analysis
Andrew Trick61cd3a62013-09-19 06:02:43 +0000964 if (PassDebugging >= Details) {
Andreas Neustifterec6fa4f2009-12-04 06:58:24 +0000965 Pass *S = Info->second;
David Greenee0081102010-01-05 01:30:02 +0000966 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
967 dbgs() << S->getPassName() << "'\n";
Andreas Neustifterec6fa4f2009-12-04 06:58:24 +0000968 }
Kazu Hirata17bae6f2021-02-27 10:09:25 -0800969 IA->erase(Info);
Andreas Neustifterec6fa4f2009-12-04 06:58:24 +0000970 }
Devang Patel89612512007-03-06 01:55:46 +0000971 }
972 }
Devang Patel30781bb2006-11-07 22:35:17 +0000973}
974
Devang Patelcdd9e462006-11-14 03:05:08 +0000975/// Remove analysis passes that are not used any longer
Daniel Dunbarff88ed62009-11-06 10:58:06 +0000976void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel79979662007-03-05 20:01:30 +0000977 enum PassDebuggingString DBG_STR) {
Devang Patelbff68012006-12-08 00:37:52 +0000978
Devang Patelf5a14f22007-07-20 18:04:54 +0000979 SmallVector<Pass *, 12> DeadPasses;
Devang Patel654393f2007-04-16 20:27:05 +0000980
Devang Patel4bf9ac72007-04-16 20:39:59 +0000981 // If this is a on the fly manager then it does not have TPM.
Devang Patel654393f2007-04-16 20:27:05 +0000982 if (!TPM)
983 return;
984
Devang Patelbff68012006-12-08 00:37:52 +0000985 TPM->collectLastUses(DeadPasses, P);
986
Andrew Trick61cd3a62013-09-19 06:02:43 +0000987 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greenee0081102010-01-05 01:30:02 +0000988 dbgs() << " -*- '" << P->getPassName();
989 dbgs() << "' is the last user of following pass instances.";
990 dbgs() << " Free these instances\n";
Evan Cheng8424e512008-06-04 09:13:31 +0000991 }
992
Yaron Keren697cb172016-10-02 19:21:41 +0000993 for (Pass *P : DeadPasses)
994 freePass(P, Msg, DBG_STR);
Dan Gohman0ac7ac82009-09-27 23:38:27 +0000995}
Devang Patel0208f742006-12-13 23:50:44 +0000996
Daniel Dunbarff88ed62009-11-06 10:58:06 +0000997void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman0ac7ac82009-09-27 23:38:27 +0000998 enum PassDebuggingString DBG_STR) {
999 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel0208f742006-12-13 23:50:44 +00001000
Dan Gohman0ac7ac82009-09-27 23:38:27 +00001001 {
1002 // If the pass crashes releasing memory, remember this.
1003 PassManagerPrettyStackEntry X(P);
Chris Lattnerd7cf0bd2010-03-30 04:03:22 +00001004 TimeRegion PassTimer(getPassTimer(P));
1005
Dan Gohman0ac7ac82009-09-27 23:38:27 +00001006 P->releaseMemory();
Dan Gohman0ac7ac82009-09-27 23:38:27 +00001007 }
1008
Owen Anderson140a03f2010-08-06 18:33:48 +00001009 AnalysisID PI = P->getPassID();
Chandler Carruth9b3892c2015-01-28 09:47:21 +00001010 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman0ac7ac82009-09-27 23:38:27 +00001011 // Remove the pass itself (if it is not already removed).
1012 AvailableAnalysis.erase(PI);
1013
1014 // Remove all interfaces this pass implements, for which it is also
1015 // listed as the available implementation.
Owen Anderson140a03f2010-08-06 18:33:48 +00001016 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Andersonffe71b02010-07-20 16:55:05 +00001017 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemand563bfe2013-02-26 01:31:59 +00001018 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Anderson140a03f2010-08-06 18:33:48 +00001019 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman0ac7ac82009-09-27 23:38:27 +00001020 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc196cc02008-10-06 20:36:36 +00001021 AvailableAnalysis.erase(Pos);
Devang Patelc196cc02008-10-06 20:36:36 +00001022 }
Devang Patelbff68012006-12-08 00:37:52 +00001023 }
Devang Patelcdd9e462006-11-14 03:05:08 +00001024}
1025
Dan Gohman3d4563a2010-08-12 23:50:08 +00001026/// Add pass P into the PassVector. Update
Devang Patel62276112006-11-11 02:04:19 +00001027/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner2072e9c2009-03-06 05:53:14 +00001028void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Patelc5a52862006-12-08 23:53:00 +00001029 // This manager is going to manage pass P. Set up analysis resolver
1030 // to connect them.
Devang Patel59f1fd22007-01-05 22:47:07 +00001031 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patelc5a52862006-12-08 23:53:00 +00001032 P->setResolver(AR);
1033
Devang Patel5d83b232007-03-05 22:57:49 +00001034 // If a FunctionPass F is the last user of ModulePass info M
1035 // then the F's manager, not F, records itself as a last user of M.
Devang Patelf5a14f22007-07-20 18:04:54 +00001036 SmallVector<Pass *, 12> TransferLastUses;
Devang Patel5d83b232007-03-05 22:57:49 +00001037
Chris Lattner2072e9c2009-03-06 05:53:14 +00001038 if (!ProcessAnalysis) {
1039 // Add pass
1040 PassVector.push_back(P);
1041 return;
Devang Patel62276112006-11-11 02:04:19 +00001042 }
Devang Patel79504682006-11-11 01:51:02 +00001043
Chris Lattner2072e9c2009-03-06 05:53:14 +00001044 // At the moment, this pass is the last user of all required passes.
1045 SmallVector<Pass *, 12> LastUses;
Chandler Carrutha8d247f2015-08-19 03:02:12 +00001046 SmallVector<Pass *, 8> UsedPasses;
Chris Lattner2072e9c2009-03-06 05:53:14 +00001047 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
1048
1049 unsigned PDepth = this->getDepth();
1050
Chandler Carrutha8d247f2015-08-19 03:02:12 +00001051 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
1052 for (Pass *PUsed : UsedPasses) {
Chris Lattner2072e9c2009-03-06 05:53:14 +00001053 unsigned RDepth = 0;
1054
Chandler Carrutha8d247f2015-08-19 03:02:12 +00001055 assert(PUsed->getResolver() && "Analysis Resolver is not set");
1056 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattner2072e9c2009-03-06 05:53:14 +00001057 RDepth = DM.getDepth();
1058
1059 if (PDepth == RDepth)
Chandler Carrutha8d247f2015-08-19 03:02:12 +00001060 LastUses.push_back(PUsed);
Chris Lattner2072e9c2009-03-06 05:53:14 +00001061 else if (PDepth > RDepth) {
1062 // Let the parent claim responsibility of last use
Chandler Carrutha8d247f2015-08-19 03:02:12 +00001063 TransferLastUses.push_back(PUsed);
Chris Lattner2072e9c2009-03-06 05:53:14 +00001064 // Keep track of higher level analysis used by this manager.
Chandler Carrutha8d247f2015-08-19 03:02:12 +00001065 HigherLevelAnalysis.push_back(PUsed);
Dan Gohman3d4563a2010-08-12 23:50:08 +00001066 } else
Chandler Carrutha8d247f2015-08-19 03:02:12 +00001067 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattner2072e9c2009-03-06 05:53:14 +00001068 }
1069
1070 // Set P as P's last user until someone starts using P.
1071 // However, if P is a Pass Manager then it does not need
1072 // to record its last user.
Craig Topperea285482014-04-09 06:08:46 +00001073 if (!P->getAsPMDataManager())
Chris Lattner2072e9c2009-03-06 05:53:14 +00001074 LastUses.push_back(P);
1075 TPM->setLastUser(LastUses, P);
1076
1077 if (!TransferLastUses.empty()) {
Chris Lattner8db741c2010-01-22 05:24:46 +00001078 Pass *My_PM = getAsPass();
Chris Lattner2072e9c2009-03-06 05:53:14 +00001079 TPM->setLastUser(TransferLastUses, My_PM);
1080 TransferLastUses.clear();
1081 }
1082
Dan Gohman64a44872010-08-16 22:57:28 +00001083 // Now, take care of required analyses that are not available.
Chandler Carruthc390ac22015-08-18 18:41:53 +00001084 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1085 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Anderson140a03f2010-08-06 18:33:48 +00001086 Pass *AnalysisPass = PI->createPass();
Chris Lattner2072e9c2009-03-06 05:53:14 +00001087 this->addLowerLevelRequiredPass(P, AnalysisPass);
1088 }
1089
1090 // Take a note of analysis required and made available by this pass.
1091 // Remove the analysis not preserved by this pass
1092 removeNotPreservedAnalysis(P);
1093 recordAvailableAnalysis(P);
1094
Devang Patel79504682006-11-11 01:51:02 +00001095 // Add pass
1096 PassVector.push_back(P);
Devang Patel79504682006-11-11 01:51:02 +00001097}
1098
Devang Patel293bbe32007-04-16 20:12:57 +00001099
Chandler Carrutha8d247f2015-08-19 03:02:12 +00001100/// Populate UP with analysis pass that are used or required by
Devang Patel293bbe32007-04-16 20:12:57 +00001101/// pass P and are available. Populate RP_NotAvail with analysis
1102/// pass that are required by pass P but are not available.
Chandler Carrutha8d247f2015-08-19 03:02:12 +00001103void PMDataManager::collectRequiredAndUsedAnalyses(
1104 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1105 Pass *P) {
Devang Patel8b80f4c2008-08-11 21:13:39 +00001106 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carrutha8d247f2015-08-19 03:02:12 +00001107
1108 for (const auto &UsedID : AnUsage->getUsedSet())
1109 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1110 UP.push_back(AnalysisPass);
1111
Chandler Carruthc390ac22015-08-18 18:41:53 +00001112 for (const auto &RequiredID : AnUsage->getRequiredSet())
1113 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carrutha8d247f2015-08-19 03:02:12 +00001114 UP.push_back(AnalysisPass);
Devang Patel293bbe32007-04-16 20:12:57 +00001115 else
Chandler Carruthc390ac22015-08-18 18:41:53 +00001116 RP_NotAvail.push_back(RequiredID);
Devang Patele56c4582006-12-07 23:05:44 +00001117}
1118
Devang Patelf72b7282006-11-14 21:49:36 +00001119// All Required analyses should be available to the pass as it runs! Here
1120// we fill in the AnalysisImpls member of the pass so that it can
1121// successfully use the getAnalysis() method to retrieve the
1122// implementations it needs.
1123//
Devang Patel12532f92006-12-07 18:36:24 +00001124void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patel8b80f4c2008-08-11 21:13:39 +00001125 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1126
Florian Hahnfc3d9cb2017-07-13 10:52:00 +00001127 for (const AnalysisID ID : AnUsage->getRequiredSet()) {
1128 Pass *Impl = findAnalysisPass(ID, true);
Craig Topperea285482014-04-09 06:08:46 +00001129 if (!Impl)
Devang Patelaabe7842007-04-16 20:44:16 +00001130 // This may be analysis pass that is initialized on the fly.
1131 // If that is not the case then it will raise an assert when it is used.
1132 continue;
Devang Patel59f1fd22007-01-05 22:47:07 +00001133 AnalysisResolver *AR = P->getResolver();
Chris Lattner2072e9c2009-03-06 05:53:14 +00001134 assert(AR && "Analysis Resolver is not set");
Florian Hahnfc3d9cb2017-07-13 10:52:00 +00001135 AR->addAnalysisImplsPair(ID, Impl);
Devang Patelf72b7282006-11-14 21:49:36 +00001136 }
1137}
1138
Devang Patelcad76752006-12-08 22:30:11 +00001139/// Find the pass that implements Analysis AID. If desired pass is not found
1140/// then return NULL.
1141Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1142
1143 // Check if AvailableAnalysis map has one entry.
Michael Ilsemand563bfe2013-02-26 01:31:59 +00001144 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patelcad76752006-12-08 22:30:11 +00001145
1146 if (I != AvailableAnalysis.end())
1147 return I->second;
1148
1149 // Search Parents through TopLevelManager
1150 if (SearchParent)
1151 return TPM->findAnalysisPass(AID);
Dan Gohman3d4563a2010-08-12 23:50:08 +00001152
Craig Topperea285482014-04-09 06:08:46 +00001153 return nullptr;
Devang Patelcad76752006-12-08 22:30:11 +00001154}
1155
Devang Patel5ac21a52006-12-15 20:13:01 +00001156// Print list of passes that are last used by P.
1157void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
Jay Foad7cacc7b2020-11-27 17:32:01 +00001158 if (PassDebugging < Details)
1159 return;
Devang Patel5ac21a52006-12-15 20:13:01 +00001160
Devang Patelf5a14f22007-07-20 18:04:54 +00001161 SmallVector<Pass *, 12> LUses;
Devang Patel4bf9ac72007-04-16 20:39:59 +00001162
1163 // If this is a on the fly manager then it does not have TPM.
1164 if (!TPM)
1165 return;
1166
Devang Patel5ac21a52006-12-15 20:13:01 +00001167 TPM->collectLastUses(LUses, P);
Dan Gohman3d4563a2010-08-12 23:50:08 +00001168
Florian Hahnfc3d9cb2017-07-13 10:52:00 +00001169 for (Pass *P : LUses) {
Eric Christopherf2fe0022014-02-26 23:27:16 +00001170 dbgs() << "--" << std::string(Offset*2, ' ');
Florian Hahnfc3d9cb2017-07-13 10:52:00 +00001171 P->dumpPassStructure(0);
Devang Patel5ac21a52006-12-15 20:13:01 +00001172 }
1173}
1174
1175void PMDataManager::dumpPassArguments() const {
Florian Hahnfc3d9cb2017-07-13 10:52:00 +00001176 for (Pass *P : PassVector) {
1177 if (PMDataManager *PMD = P->getAsPMDataManager())
Devang Patel5ac21a52006-12-15 20:13:01 +00001178 PMD->dumpPassArguments();
1179 else
Owen Anderson140a03f2010-08-06 18:33:48 +00001180 if (const PassInfo *PI =
Florian Hahnfc3d9cb2017-07-13 10:52:00 +00001181 TPM->findAnalysisPassInfo(P->getPassID()))
Devang Patel5ac21a52006-12-15 20:13:01 +00001182 if (!PI->isAnalysisGroup())
David Greenee0081102010-01-05 01:30:02 +00001183 dbgs() << " -" << PI->getPassArgument();
Devang Patel5ac21a52006-12-15 20:13:01 +00001184 }
1185}
1186
Chris Lattnerb5c40f62007-08-10 06:17:04 +00001187void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1188 enum PassDebuggingString S2,
Daniel Dunbarff88ed62009-11-06 10:58:06 +00001189 StringRef Msg) {
Andrew Trick61cd3a62013-09-19 06:02:43 +00001190 if (PassDebugging < Executions)
Devang Patel5ac21a52006-12-15 20:13:01 +00001191 return;
Pavel Labath64540e62016-10-25 16:20:07 +00001192 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
Chandler Carruth91d1e0b2014-04-27 23:59:25 +00001193 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel79979662007-03-05 20:01:30 +00001194 switch (S1) {
1195 case EXECUTION_MSG:
David Greenee0081102010-01-05 01:30:02 +00001196 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel79979662007-03-05 20:01:30 +00001197 break;
1198 case MODIFICATION_MSG:
David Greenee0081102010-01-05 01:30:02 +00001199 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel79979662007-03-05 20:01:30 +00001200 break;
1201 case FREEING_MSG:
David Greenee0081102010-01-05 01:30:02 +00001202 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel79979662007-03-05 20:01:30 +00001203 break;
1204 default:
1205 break;
1206 }
1207 switch (S2) {
Devang Patel79979662007-03-05 20:01:30 +00001208 case ON_FUNCTION_MSG:
David Greenee0081102010-01-05 01:30:02 +00001209 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel79979662007-03-05 20:01:30 +00001210 break;
1211 case ON_MODULE_MSG:
David Greenee0081102010-01-05 01:30:02 +00001212 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel79979662007-03-05 20:01:30 +00001213 break;
Tobias Grosserc3206e42010-10-20 01:54:44 +00001214 case ON_REGION_MSG:
1215 dbgs() << "' on Region '" << Msg << "'...\n";
1216 break;
Devang Patel79979662007-03-05 20:01:30 +00001217 case ON_LOOP_MSG:
David Greenee0081102010-01-05 01:30:02 +00001218 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel79979662007-03-05 20:01:30 +00001219 break;
1220 case ON_CG_MSG:
David Greenee0081102010-01-05 01:30:02 +00001221 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel79979662007-03-05 20:01:30 +00001222 break;
1223 default:
1224 break;
1225 }
Devang Patel5ac21a52006-12-15 20:13:01 +00001226}
1227
Chris Lattner223d1312009-03-06 06:45:05 +00001228void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trick61cd3a62013-09-19 06:02:43 +00001229 if (PassDebugging < Details)
Chris Lattnerde371f32008-08-08 15:14:09 +00001230 return;
Dan Gohman3d4563a2010-08-12 23:50:08 +00001231
Chris Lattnerde371f32008-08-08 15:14:09 +00001232 AnalysisUsage analysisUsage;
1233 P->getAnalysisUsage(analysisUsage);
1234 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1235}
1236
Chris Lattner223d1312009-03-06 06:45:05 +00001237void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trick61cd3a62013-09-19 06:02:43 +00001238 if (PassDebugging < Details)
Chris Lattnerde371f32008-08-08 15:14:09 +00001239 return;
Dan Gohman3d4563a2010-08-12 23:50:08 +00001240
Chris Lattnerde371f32008-08-08 15:14:09 +00001241 AnalysisUsage analysisUsage;
1242 P->getAnalysisUsage(analysisUsage);
1243 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1244}
1245
Chandler Carrutha8d247f2015-08-19 03:02:12 +00001246void PMDataManager::dumpUsedSet(const Pass *P) const {
1247 if (PassDebugging < Details)
1248 return;
1249
1250 AnalysisUsage analysisUsage;
1251 P->getAnalysisUsage(analysisUsage);
1252 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1253}
1254
Daniel Dunbarff88ed62009-11-06 10:58:06 +00001255void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner223d1312009-03-06 06:45:05 +00001256 const AnalysisUsage::VectorType &Set) const {
Andrew Trick61cd3a62013-09-19 06:02:43 +00001257 assert(PassDebugging >= Details);
Chris Lattnerde371f32008-08-08 15:14:09 +00001258 if (Set.empty())
1259 return;
Roman Divacky9696b862012-09-05 22:26:57 +00001260 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner223d1312009-03-06 06:45:05 +00001261 for (unsigned i = 0; i != Set.size(); ++i) {
David Greenee0081102010-01-05 01:30:02 +00001262 if (i) dbgs() << ',';
Chandler Carruth9b3892c2015-01-28 09:47:21 +00001263 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick46edcef2011-06-03 00:48:58 +00001264 if (!PInf) {
1265 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1266 // all drivers.
1267 dbgs() << " Uninitialized Pass";
1268 continue;
1269 }
Owen Anderson140a03f2010-08-06 18:33:48 +00001270 dbgs() << ' ' << PInf->getPassName();
Chris Lattner223d1312009-03-06 06:45:05 +00001271 }
David Greenee0081102010-01-05 01:30:02 +00001272 dbgs() << '\n';
Devang Patel5ac21a52006-12-15 20:13:01 +00001273}
Devang Patel2d708df2006-12-08 23:28:54 +00001274
Devang Patel8ee0ca42007-07-27 20:06:09 +00001275/// Add RequiredPass into list of lower level passes required by pass P.
1276/// RequiredPass is run on the fly by Pass Manager when P requests it
1277/// through getAnalysis interface.
1278/// This should be handled by specific pass manager.
1279void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1280 if (TPM) {
1281 TPM->dumpArguments();
1282 TPM->dumpPasses();
1283 }
Devang Patelb507af72008-02-02 01:43:30 +00001284
Dan Gohman3d4563a2010-08-12 23:50:08 +00001285 // Module Level pass may required Function Level analysis info
1286 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1287 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patelb507af72008-02-02 01:43:30 +00001288 // module level pass is requiring lower level analysis info managed by
1289 // lower level pass manager.
1290
1291 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohman3d4563a2010-08-12 23:50:08 +00001292 // checks whether any lower level manager will be able to provide this
Devang Patelb507af72008-02-02 01:43:30 +00001293 // analysis info on demand or not.
Devang Patele278d4e2008-06-03 01:20:02 +00001294#ifndef NDEBUG
David Greenee0081102010-01-05 01:30:02 +00001295 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1296 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patele278d4e2008-06-03 01:20:02 +00001297#endif
Torok Edwind1295122009-07-14 16:55:14 +00001298 llvm_unreachable("Unable to schedule pass");
Devang Patel8ee0ca42007-07-27 20:06:09 +00001299}
1300
serge-sans-pailleeb62e372020-06-26 13:07:31 +02001301std::tuple<Pass *, bool> PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI,
1302 Function &F) {
Craig Topper4d04c042012-02-05 22:14:15 +00001303 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmandcc21202010-06-21 18:46:45 +00001304}
1305
Devang Pateld023bb42007-01-12 18:52:44 +00001306// Destructor
1307PMDataManager::~PMDataManager() {
Florian Hahnfc3d9cb2017-07-13 10:52:00 +00001308 for (Pass *P : PassVector)
1309 delete P;
Devang Pateld023bb42007-01-12 18:52:44 +00001310}
1311
Devang Patel2d708df2006-12-08 23:28:54 +00001312//===----------------------------------------------------------------------===//
1313// NOTE: Is this the right place to define this method ?
Duncan Sands210a8b62009-01-28 13:14:17 +00001314// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
Jay Foadd02b5542020-11-27 10:03:05 +00001315Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID) const {
1316 return PM.findAnalysisPass(ID, true);
Devang Patel2d708df2006-12-08 23:28:54 +00001317}
1318
serge-sans-pailleeb62e372020-06-26 13:07:31 +02001319std::tuple<Pass *, bool>
1320AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI, Function &F) {
Devang Patel3e741752007-04-16 20:56:24 +00001321 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1322}
1323
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -07001324namespace llvm {
1325namespace legacy {
1326
Devang Pateldfcb74b2006-12-07 19:39:39 +00001327//===----------------------------------------------------------------------===//
Devang Patel748205a2006-12-13 02:36:01 +00001328// FunctionPassManager implementation
Devang Pateldfcb74b2006-12-07 19:39:39 +00001329
Devang Pateldae40262006-11-08 10:44:40 +00001330/// Create new Function pass manager
Jeffrey Yasskin4415b4f2010-01-27 20:34:15 +00001331FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -07001332 FPM = new legacy::FunctionPassManagerImpl();
Devang Patel4e0114e2006-12-12 22:02:16 +00001333 // FPM is the top level manager.
1334 FPM->setTopLevelManager(FPM);
Devang Patela62bf2c2006-12-12 23:27:37 +00001335
Dan Gohman75194542008-03-13 02:08:36 +00001336 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patela62bf2c2006-12-12 23:27:37 +00001337 FPM->setResolver(AR);
Devang Patele1754842006-12-08 18:57:16 +00001338}
1339
Devang Patel748205a2006-12-13 02:36:01 +00001340FunctionPassManager::~FunctionPassManager() {
Devang Patel802d4b72006-12-13 00:09:23 +00001341 delete FPM;
1342}
1343
Dan Gohman3d4563a2010-08-12 23:50:08 +00001344void FunctionPassManager::add(Pass *P) {
Andrew Trick5aeec472012-02-01 07:16:20 +00001345 FPM->add(P);
Devang Pateldae40262006-11-08 10:44:40 +00001346}
1347
Devang Patel7b7bc092006-11-15 19:39:54 +00001348/// run - Execute all of the passes scheduled for execution. Keep
1349/// track of whether any of the passes modifies the function, and if
1350/// so, return true.
1351///
Devang Patel748205a2006-12-13 02:36:01 +00001352bool FunctionPassManager::run(Function &F) {
Peter Collingbourne9c2be2d2016-11-09 17:49:19 +00001353 handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
1354 report_fatal_error("Error reading bitcode file: " + EIB.message());
1355 });
Devang Patel2e9cf752006-12-08 22:57:48 +00001356 return FPM->run(F);
Devang Patel7b7bc092006-11-15 19:39:54 +00001357}
1358
1359
Devang Patel0ca2a202006-11-15 01:27:05 +00001360/// doInitialization - Run all of the initializers for the function passes.
1361///
Devang Patel748205a2006-12-13 02:36:01 +00001362bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin4415b4f2010-01-27 20:34:15 +00001363 return FPM->doInitialization(*M);
Devang Patel0ca2a202006-11-15 01:27:05 +00001364}
1365
Dan Gohman31d7ca22007-07-30 14:51:13 +00001366/// doFinalization - Run all of the finalizers for the function passes.
Devang Patel0ca2a202006-11-15 01:27:05 +00001367///
Devang Patel748205a2006-12-13 02:36:01 +00001368bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin4415b4f2010-01-27 20:34:15 +00001369 return FPM->doFinalization(*M);
Devang Patel0ca2a202006-11-15 01:27:05 +00001370}
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -07001371} // namespace legacy
1372} // namespace llvm
Devang Patel96abc762006-12-19 19:46:59 +00001373
Devang Pateldaec9362009-04-01 22:34:41 +00001374/// cleanup - After running all passes, clean up pass manager cache.
1375void FPPassManager::cleanup() {
1376 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1377 FunctionPass *FP = getContainedPass(Index);
1378 AnalysisResolver *AR = FP->getResolver();
1379 assert(AR && "Analysis Resolver is not set");
1380 AR->clearAnalysisImpls();
1381 }
1382}
1383
Devang Patel96abc762006-12-19 19:46:59 +00001384
1385//===----------------------------------------------------------------------===//
1386// FPPassManager implementation
Devang Patelfd883af2006-11-07 21:49:50 +00001387
Devang Patel4b409542007-05-03 01:11:54 +00001388char FPPassManager::ID = 0;
Devang Pateld023bb42007-01-12 18:52:44 +00001389/// Print passes managed by this manager
1390void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramerbc5ef892011-10-16 16:30:34 +00001391 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Pateld023bb42007-01-12 18:52:44 +00001392 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1393 FunctionPass *FP = getContainedPass(Index);
Dan Gohman17cd5742010-08-19 01:29:07 +00001394 FP->dumpPassStructure(Offset + 1);
Devang Pateld023bb42007-01-12 18:52:44 +00001395 dumpLastUses(FP, Offset+1);
1396 }
1397}
1398
Dan Gohman3d4563a2010-08-12 23:50:08 +00001399/// Execute all of the passes scheduled for execution by invoking
1400/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patelfd883af2006-11-07 21:49:50 +00001401/// the function, and if so, return true.
Devang Patel96abc762006-12-19 19:46:59 +00001402bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner2072e9c2009-03-06 05:53:14 +00001403 if (F.isDeclaration())
1404 return false;
Devang Patel7b7bc092006-11-15 19:39:54 +00001405
1406 bool Changed = false;
Jessica Paquette46e868c2018-05-18 17:26:39 +00001407 Module &M = *F.getParent();
Devang Patel370c62f2008-03-20 01:09:53 +00001408 // Collect inherited analysis from Module level pass manager.
1409 populateInheritedAnalysis(TPM->activeStack);
Devang Patel15db4ae2006-12-12 23:15:28 +00001410
Jessica Paquettee23d21a2018-08-31 20:19:41 +00001411 unsigned InstrCount, FunctionSize = 0;
Jessica Paquette6c0520a2018-09-06 21:19:54 +00001412 StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
Xin Tong38fec832018-07-22 05:27:41 +00001413 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Jessica Paquettee23d21a2018-08-31 20:19:41 +00001414 // Collect the initial size of the module.
1415 if (EmitICRemark) {
Jessica Paquette6c0520a2018-09-06 21:19:54 +00001416 InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
Jessica Paquettee23d21a2018-08-31 20:19:41 +00001417 FunctionSize = F.getInstructionCount();
1418 }
1419
Nico Webercc812522019-04-20 23:22:45 +00001420 llvm::TimeTraceScope FunctionScope("OptFunction", F.getName());
Anton Afanasyevd506b67b2019-03-30 08:42:48 +00001421
Devang Patela8ecac42006-12-16 00:56:26 +00001422 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1423 FunctionPass *FP = getContainedPass(Index);
Dan Gohmand1b283f22010-03-01 17:34:28 +00001424 bool LocalChanged = false;
Devang Patela8ecac42006-12-16 00:56:26 +00001425
Nico Webercc812522019-04-20 23:22:45 +00001426 llvm::TimeTraceScope PassScope("RunPass", FP->getPassName());
1427
Daniel Dunbarf71bde72009-07-26 07:49:05 +00001428 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattnerde371f32008-08-08 15:14:09 +00001429 dumpRequiredSet(FP);
Devang Patel968e03f2006-12-14 00:08:04 +00001430
Devang Patela8ecac42006-12-16 00:56:26 +00001431 initializeAnalysisImpl(FP);
Eric Christopherd2f088c2012-03-23 03:54:05 +00001432
Chris Lattner223d1312009-03-06 06:45:05 +00001433 {
1434 PassManagerPrettyStackEntry X(FP, F);
Chris Lattnerd7cf0bd2010-03-30 04:03:22 +00001435 TimeRegion PassTimer(getPassTimer(FP));
serge-sans-paille8a6423d2020-05-28 12:40:17 +02001436#ifdef EXPENSIVE_CHECKS
1437 uint64_t RefHash = StructuralHash(F);
1438#endif
Dan Gohmand1b283f22010-03-01 17:34:28 +00001439 LocalChanged |= FP->runOnFunction(F);
serge-sans-paille8a6423d2020-05-28 12:40:17 +02001440
Jon Roelofs067ff342020-07-27 12:34:36 -06001441#if defined(EXPENSIVE_CHECKS) && !defined(NDEBUG)
1442 if (!LocalChanged && (RefHash != StructuralHash(F))) {
1443 llvm::errs() << "Pass modifies its input and doesn't report it: "
1444 << FP->getPassName() << "\n";
serge-sans-paille055ea2a2020-08-27 17:31:54 +02001445 llvm_unreachable("Pass modifies its input and doesn't report it");
Jon Roelofs067ff342020-07-27 12:34:36 -06001446 }
serge-sans-paille8a6423d2020-05-28 12:40:17 +02001447#endif
1448
Jessica Paquettee23d21a2018-08-31 20:19:41 +00001449 if (EmitICRemark) {
1450 unsigned NewSize = F.getInstructionCount();
1451
1452 // Update the size of the function, emit a remark, and update the size
1453 // of the module.
1454 if (NewSize != FunctionSize) {
Jessica Paquettee23d21a2018-08-31 20:19:41 +00001455 int64_t Delta = static_cast<int64_t>(NewSize) -
1456 static_cast<int64_t>(FunctionSize);
Jessica Paquette6c0520a2018-09-06 21:19:54 +00001457 emitInstrCountChangedRemark(FP, M, Delta, InstrCount,
1458 FunctionToInstrCount, &F);
Jessica Paquettee23d21a2018-08-31 20:19:41 +00001459 InstrCount = static_cast<int64_t>(InstrCount) + Delta;
1460 FunctionSize = NewSize;
1461 }
1462 }
Chris Lattner223d1312009-03-06 06:45:05 +00001463 }
Devang Patel968e03f2006-12-14 00:08:04 +00001464
Dan Gohmand1b283f22010-03-01 17:34:28 +00001465 Changed |= LocalChanged;
1466 if (LocalChanged)
Daniel Dunbarf71bde72009-07-26 07:49:05 +00001467 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattnerde371f32008-08-08 15:14:09 +00001468 dumpPreservedSet(FP);
Chandler Carrutha8d247f2015-08-19 03:02:12 +00001469 dumpUsedSet(FP);
Devang Patel968e03f2006-12-14 00:08:04 +00001470
Devang Patel667a8132007-07-19 18:02:32 +00001471 verifyPreservedAnalysis(FP);
serge-sans-paille13f09312020-07-28 10:55:09 +02001472 if (LocalChanged)
1473 removeNotPreservedAnalysis(FP);
Devang Patela8ecac42006-12-16 00:56:26 +00001474 recordAvailableAnalysis(FP);
Daniel Dunbarf71bde72009-07-26 07:49:05 +00001475 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel7b7bc092006-11-15 19:39:54 +00001476 }
Anton Afanasyevd506b67b2019-03-30 08:42:48 +00001477
Devang Patel7b7bc092006-11-15 19:39:54 +00001478 return Changed;
1479}
1480
Devang Patel96abc762006-12-19 19:46:59 +00001481bool FPPassManager::runOnModule(Module &M) {
Pedro Artigas51543442012-11-29 17:47:05 +00001482 bool Changed = false;
Devang Patel96abc762006-12-19 19:46:59 +00001483
Serge Pavlovbef2f0b2017-01-15 10:23:18 +00001484 for (Function &F : M)
Yaron Kerenedf6a8c2015-06-05 14:15:07 +00001485 Changed |= runOnFunction(F);
Devang Patel96abc762006-12-19 19:46:59 +00001486
Pedro Artigas51543442012-11-29 17:47:05 +00001487 return Changed;
Devang Patel96abc762006-12-19 19:46:59 +00001488}
1489
Duncan Sands40b88a82009-02-13 09:42:34 +00001490bool FPPassManager::doInitialization(Module &M) {
Devang Patel0ca2a202006-11-15 01:27:05 +00001491 bool Changed = false;
1492
Chris Lattner223d1312009-03-06 06:45:05 +00001493 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1494 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trick1d36e072013-09-18 23:31:10 +00001495
Devang Patel0ca2a202006-11-15 01:27:05 +00001496 return Changed;
1497}
1498
Duncan Sands40b88a82009-02-13 09:42:34 +00001499bool FPPassManager::doFinalization(Module &M) {
Devang Patel0ca2a202006-11-15 01:27:05 +00001500 bool Changed = false;
Pedro Artigasbf632052012-12-05 17:12:22 +00001501
1502 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner223d1312009-03-06 06:45:05 +00001503 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trick1d36e072013-09-18 23:31:10 +00001504
Devang Patel0ca2a202006-11-15 01:27:05 +00001505 return Changed;
1506}
1507
Devang Pateldfcb74b2006-12-07 19:39:39 +00001508//===----------------------------------------------------------------------===//
Devang Patel96abc762006-12-19 19:46:59 +00001509// MPPassManager implementation
Devang Patel1d00fe82006-11-07 22:03:15 +00001510
Dan Gohman3d4563a2010-08-12 23:50:08 +00001511/// Execute all of the passes scheduled for execution by invoking
1512/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel1d00fe82006-11-07 22:03:15 +00001513/// the module, and if so, return true.
1514bool
Devang Patel96abc762006-12-19 19:46:59 +00001515MPPassManager::runOnModule(Module &M) {
Anton Afanasyevd506b67b2019-03-30 08:42:48 +00001516 llvm::TimeTraceScope TimeScope("OptModule", M.getName());
1517
Devang Patel1d00fe82006-11-07 22:03:15 +00001518 bool Changed = false;
Devang Patel0b05c922006-11-14 01:23:29 +00001519
Torok Edwincd0d8462009-06-29 18:49:09 +00001520 // Initialize on-the-fly passes
Yaron Kerenedf6a8c2015-06-05 14:15:07 +00001521 for (auto &OnTheFlyManager : OnTheFlyManagers) {
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -07001522 legacy::FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwincd0d8462009-06-29 18:49:09 +00001523 Changed |= FPP->doInitialization(M);
1524 }
1525
Pedro Artigas51543442012-11-29 17:47:05 +00001526 // Initialize module passes
1527 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1528 Changed |= getContainedPass(Index)->doInitialization(M);
1529
Fangrui Songa812c862019-07-12 15:18:29 +00001530 unsigned InstrCount;
Jessica Paquette6c0520a2018-09-06 21:19:54 +00001531 StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
Xin Tong38fec832018-07-22 05:27:41 +00001532 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Jessica Paquettee3204072018-08-31 20:20:55 +00001533 // Collect the initial size of the module.
Fangrui Songa812c862019-07-12 15:18:29 +00001534 if (EmitICRemark)
Jessica Paquette6c0520a2018-09-06 21:19:54 +00001535 InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
Jessica Paquettee3204072018-08-31 20:20:55 +00001536
Devang Patela8ecac42006-12-16 00:56:26 +00001537 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1538 ModulePass *MP = getContainedPass(Index);
Dan Gohmand1b283f22010-03-01 17:34:28 +00001539 bool LocalChanged = false;
Devang Patela8ecac42006-12-16 00:56:26 +00001540
Benjamin Kramer7de3ea22009-12-08 13:07:38 +00001541 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattnerde371f32008-08-08 15:14:09 +00001542 dumpRequiredSet(MP);
Devang Patel968e03f2006-12-14 00:08:04 +00001543
Devang Patela8ecac42006-12-16 00:56:26 +00001544 initializeAnalysisImpl(MP);
Devang Patel75711122006-12-14 00:59:42 +00001545
Chris Lattner223d1312009-03-06 06:45:05 +00001546 {
1547 PassManagerPrettyStackEntry X(MP, M);
Chris Lattnerd7cf0bd2010-03-30 04:03:22 +00001548 TimeRegion PassTimer(getPassTimer(MP));
1549
serge-sans-paille8a6423d2020-05-28 12:40:17 +02001550#ifdef EXPENSIVE_CHECKS
1551 uint64_t RefHash = StructuralHash(M);
1552#endif
1553
Dan Gohmand1b283f22010-03-01 17:34:28 +00001554 LocalChanged |= MP->runOnModule(M);
serge-sans-paille8a6423d2020-05-28 12:40:17 +02001555
1556#ifdef EXPENSIVE_CHECKS
1557 assert((LocalChanged || (RefHash == StructuralHash(M))) &&
1558 "Pass modifies its input and doesn't report it.");
1559#endif
1560
Jessica Paquettee3204072018-08-31 20:20:55 +00001561 if (EmitICRemark) {
1562 // Update the size of the module.
Fangrui Songa812c862019-07-12 15:18:29 +00001563 unsigned ModuleCount = M.getInstructionCount();
Jessica Paquettee3204072018-08-31 20:20:55 +00001564 if (ModuleCount != InstrCount) {
Jessica Paquettef4da24f2018-08-31 20:20:57 +00001565 int64_t Delta = static_cast<int64_t>(ModuleCount) -
1566 static_cast<int64_t>(InstrCount);
Jessica Paquette6c0520a2018-09-06 21:19:54 +00001567 emitInstrCountChangedRemark(MP, M, Delta, InstrCount,
1568 FunctionToInstrCount);
Jessica Paquetteebd00302018-08-31 22:43:41 +00001569 InstrCount = ModuleCount;
Jessica Paquettee3204072018-08-31 20:20:55 +00001570 }
1571 }
Chris Lattner223d1312009-03-06 06:45:05 +00001572 }
Devang Patel968e03f2006-12-14 00:08:04 +00001573
Dan Gohmand1b283f22010-03-01 17:34:28 +00001574 Changed |= LocalChanged;
1575 if (LocalChanged)
Dan Gohmanb0dee012008-01-29 12:09:55 +00001576 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramer7de3ea22009-12-08 13:07:38 +00001577 M.getModuleIdentifier());
Chris Lattnerde371f32008-08-08 15:14:09 +00001578 dumpPreservedSet(MP);
Chandler Carrutha8d247f2015-08-19 03:02:12 +00001579 dumpUsedSet(MP);
Dan Gohman3d4563a2010-08-12 23:50:08 +00001580
Devang Patel667a8132007-07-19 18:02:32 +00001581 verifyPreservedAnalysis(MP);
serge-sans-paille13f09312020-07-28 10:55:09 +02001582 if (LocalChanged)
1583 removeNotPreservedAnalysis(MP);
Devang Patela8ecac42006-12-16 00:56:26 +00001584 recordAvailableAnalysis(MP);
Benjamin Kramer7de3ea22009-12-08 13:07:38 +00001585 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel1d00fe82006-11-07 22:03:15 +00001586 }
Torok Edwincd0d8462009-06-29 18:49:09 +00001587
Pedro Artigas51543442012-11-29 17:47:05 +00001588 // Finalize module passes
Pedro Artigasbf632052012-12-05 17:12:22 +00001589 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigas51543442012-11-29 17:47:05 +00001590 Changed |= getContainedPass(Index)->doFinalization(M);
1591
Torok Edwincd0d8462009-06-29 18:49:09 +00001592 // Finalize on-the-fly passes
Yaron Kerenedf6a8c2015-06-05 14:15:07 +00001593 for (auto &OnTheFlyManager : OnTheFlyManagers) {
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -07001594 legacy::FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwincd0d8462009-06-29 18:49:09 +00001595 // We don't know when is the last time an on-the-fly pass is run,
1596 // so we need to releaseMemory / finalize here
1597 FPP->releaseMemoryOnTheFly();
1598 Changed |= FPP->doFinalization(M);
1599 }
Andrew Trick1d36e072013-09-18 23:31:10 +00001600
Devang Patel1d00fe82006-11-07 22:03:15 +00001601 return Changed;
1602}
1603
Devang Patel293bbe32007-04-16 20:12:57 +00001604/// Add RequiredPass into list of lower level passes required by pass P.
1605/// RequiredPass is run on the fly by Pass Manager when P requests it
1606/// through getAnalysis interface.
1607void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Dávid Bolvanský1b1444a2019-11-02 20:14:29 +01001608 assert(RequiredPass && "No required pass?");
Chris Lattner2072e9c2009-03-06 05:53:14 +00001609 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1610 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohman3d4563a2010-08-12 23:50:08 +00001611 assert((P->getPotentialPassManagerType() <
Chris Lattner2072e9c2009-03-06 05:53:14 +00001612 RequiredPass->getPotentialPassManagerType()) &&
1613 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patel293bbe32007-04-16 20:12:57 +00001614
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -07001615 legacy::FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel654393f2007-04-16 20:27:05 +00001616 if (!FPP) {
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -07001617 FPP = new legacy::FunctionPassManagerImpl();
Devang Patel3346d3d2007-04-26 17:50:19 +00001618 // FPP is the top level manager.
1619 FPP->setTopLevelManager(FPP);
1620
Devang Patel654393f2007-04-16 20:27:05 +00001621 OnTheFlyManagers[P] = FPP;
1622 }
Chandler Carruth9b3892c2015-01-28 09:47:21 +00001623 const PassInfo *RequiredPassPI =
1624 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel654393f2007-04-16 20:27:05 +00001625
Craig Topperea285482014-04-09 06:08:46 +00001626 Pass *FoundPass = nullptr;
Andrew Trick8e5110a2014-04-08 03:40:34 +00001627 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1628 FoundPass =
1629 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel3b01a8c2011-09-13 21:13:29 +00001630 }
Andrew Trick8e5110a2014-04-08 03:40:34 +00001631 if (!FoundPass) {
1632 FoundPass = RequiredPass;
1633 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru56671a12014-08-11 18:04:46 +00001634 // that we checked for an available analysis above.
Andrew Trick8e5110a2014-04-08 03:40:34 +00001635 FPP->add(RequiredPass);
1636 }
1637 // Register P as the last user of FoundPass or RequiredPass.
1638 SmallVector<Pass *, 1> LU;
1639 LU.push_back(FoundPass);
1640 FPP->setLastUser(LU, P);
Devang Patel293bbe32007-04-16 20:12:57 +00001641}
Devang Patel654393f2007-04-16 20:27:05 +00001642
Dan Gohman3d4563a2010-08-12 23:50:08 +00001643/// Return function pass corresponding to PassInfo PI, that is
Devang Patel654393f2007-04-16 20:27:05 +00001644/// required by module pass MP. Instantiate analysis pass, by using
1645/// its runOnFunction() for function F.
serge-sans-pailleeb62e372020-06-26 13:07:31 +02001646std::tuple<Pass *, bool> MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI,
1647 Function &F) {
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -07001648 legacy::FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner2072e9c2009-03-06 05:53:14 +00001649 assert(FPP && "Unable to find on the fly pass");
Dan Gohman3d4563a2010-08-12 23:50:08 +00001650
Torok Edwincd0d8462009-06-29 18:49:09 +00001651 FPP->releaseMemoryOnTheFly();
serge-sans-pailleeb62e372020-06-26 13:07:31 +02001652 bool Changed = FPP->run(F);
1653 return std::make_tuple(((PMTopLevelManager *)FPP)->findAnalysisPass(PI),
1654 Changed);
Devang Patel654393f2007-04-16 20:27:05 +00001655}
1656
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -07001657namespace llvm {
1658namespace legacy {
Devang Patel5d72ec72006-11-08 10:29:57 +00001659
Devang Pateldfcb74b2006-12-07 19:39:39 +00001660//===----------------------------------------------------------------------===//
1661// PassManager implementation
1662
Devang Patel5d72ec72006-11-08 10:29:57 +00001663/// Create new pass manager
Devang Patel748205a2006-12-13 02:36:01 +00001664PassManager::PassManager() {
Andrew Trick87862d12011-08-29 17:07:00 +00001665 PM = new PassManagerImpl();
Devang Patel4e0114e2006-12-12 22:02:16 +00001666 // PM is the top level manager
1667 PM->setTopLevelManager(PM);
Devang Patel5d72ec72006-11-08 10:29:57 +00001668}
1669
Devang Patel748205a2006-12-13 02:36:01 +00001670PassManager::~PassManager() {
Devang Patel802d4b72006-12-13 00:09:23 +00001671 delete PM;
1672}
1673
Chris Lattner2072e9c2009-03-06 05:53:14 +00001674void PassManager::add(Pass *P) {
Andrew Trick5aeec472012-02-01 07:16:20 +00001675 PM->add(P);
Devang Patel5d72ec72006-11-08 10:29:57 +00001676}
1677
1678/// run - Execute all of the passes scheduled for execution. Keep track of
1679/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner2072e9c2009-03-06 05:53:14 +00001680bool PassManager::run(Module &M) {
Devang Patel5d72ec72006-11-08 10:29:57 +00001681 return PM->run(M);
1682}
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -07001683} // namespace legacy
1684} // namespace llvm
Devang Patel5d72ec72006-11-08 10:29:57 +00001685
Devang Patel75711122006-12-14 00:59:42 +00001686//===----------------------------------------------------------------------===//
Devang Patel85c1e032007-01-08 19:29:38 +00001687// PMStack implementation
1688//
Devang Patelafd19792007-01-11 22:15:30 +00001689
Devang Patel85c1e032007-01-08 19:29:38 +00001690// Pop Pass Manager from the stack and clear its analysis info.
1691void PMStack::pop() {
1692
1693 PMDataManager *Top = this->top();
1694 Top->initializeAnalysisInfo();
1695
1696 S.pop_back();
1697}
1698
1699// Push PM on the stack and set its top level manager.
Dan Gohmanfba56192008-03-13 01:21:31 +00001700void PMStack::push(PMDataManager *PM) {
Chris Lattner2072e9c2009-03-06 05:53:14 +00001701 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick87862d12011-08-29 17:07:00 +00001702 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel85c1e032007-01-08 19:29:38 +00001703
Chris Lattner2072e9c2009-03-06 05:53:14 +00001704 if (!this->empty()) {
Andrew Trick87862d12011-08-29 17:07:00 +00001705 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1706 && "pushing bad pass manager to PMStack");
Chris Lattner2072e9c2009-03-06 05:53:14 +00001707 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel85c1e032007-01-08 19:29:38 +00001708
Chris Lattner2072e9c2009-03-06 05:53:14 +00001709 assert(TPM && "Unable to find top level manager");
Devang Patelcb680dc2007-01-11 00:19:00 +00001710 TPM->addIndirectPassManager(PM);
1711 PM->setTopLevelManager(TPM);
Andrew Trick87862d12011-08-29 17:07:00 +00001712 PM->setDepth(this->top()->getDepth()+1);
Craig Topper5d993c52013-02-06 06:50:38 +00001713 } else {
Benjamin Kramerac845ea2011-08-29 18:14:15 +00001714 assert((PM->getPassManagerType() == PMT_ModulePassManager
1715 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick87862d12011-08-29 17:07:00 +00001716 && "pushing bad pass manager to PMStack");
1717 PM->setDepth(1);
Devang Patelcb680dc2007-01-11 00:19:00 +00001718 }
1719
Devang Patelcb680dc2007-01-11 00:19:00 +00001720 S.push_back(PM);
1721}
1722
1723// Dump content of the pass manager stack.
Yaron Keren33afa9a2016-01-29 20:50:44 +00001724LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren84b55422015-06-05 17:48:47 +00001725 for (PMDataManager *Manager : S)
1726 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner2072e9c2009-03-06 05:53:14 +00001727
Devang Patelcb680dc2007-01-11 00:19:00 +00001728 if (!S.empty())
Benjamin Kramere7d5e442011-08-29 18:14:17 +00001729 dbgs() << '\n';
Devang Patel85c1e032007-01-08 19:29:38 +00001730}
1731
Devang Patel85c1e032007-01-08 19:29:38 +00001732/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohman3d4563a2010-08-12 23:50:08 +00001733/// add self into that manager.
1734void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikov861df192007-04-16 18:10:23 +00001735 PassManagerType PreferredType) {
Devang Patel85c1e032007-01-08 19:29:38 +00001736 // Find Module Pass Manager
Fangrui Song722ce852019-11-28 13:34:32 -08001737 PassManagerType T;
1738 while ((T = PMS.top()->getPassManagerType()) > PMT_ModulePassManager &&
1739 T != PreferredType)
1740 PMS.pop();
Devang Patel9fd256f2007-01-17 21:19:23 +00001741 PMS.top()->add(this);
Devang Patel85c1e032007-01-08 19:29:38 +00001742}
1743
Devang Patelf02b2512007-01-16 21:43:18 +00001744/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohman3d4563a2010-08-12 23:50:08 +00001745/// in the PM Stack and add self into that manager.
Devang Patel3a39e302007-01-17 20:30:17 +00001746void FunctionPass::assignPassManager(PMStack &PMS,
Fangrui Song9f0b0442019-11-28 14:00:12 -08001747 PassManagerType /*PreferredType*/) {
Andrew Trick5aeec472012-02-01 07:16:20 +00001748 // Find Function Pass Manager
Fangrui Song9f0b0442019-11-28 14:00:12 -08001749 PMDataManager *PM;
1750 while (PM = PMS.top(), PM->getPassManagerType() > PMT_FunctionPassManager)
Fangrui Song722ce852019-11-28 13:34:32 -08001751 PMS.pop();
Devang Patelf02b2512007-01-16 21:43:18 +00001752
Chris Lattner74028312010-01-22 05:37:10 +00001753 // Create new Function Pass Manager if needed.
Fangrui Song9f0b0442019-11-28 14:00:12 -08001754 if (PM->getPassManagerType() != PMT_FunctionPassManager) {
Devang Patelf02b2512007-01-16 21:43:18 +00001755 // [1] Create new Function Pass Manager
Fangrui Song9f0b0442019-11-28 14:00:12 -08001756 auto *FPP = new FPPassManager;
Devang Patel370c62f2008-03-20 01:09:53 +00001757 FPP->populateInheritedAnalysis(PMS);
Devang Patelf02b2512007-01-16 21:43:18 +00001758
1759 // [2] Set up new manager's top level manager
Fangrui Song9f0b0442019-11-28 14:00:12 -08001760 PM->getTopLevelManager()->addIndirectPassManager(FPP);
Devang Patelf02b2512007-01-16 21:43:18 +00001761
1762 // [3] Assign manager to manage this new manager. This may create
1763 // and push new managers into PMS
Fangrui Song9f0b0442019-11-28 14:00:12 -08001764 FPP->assignPassManager(PMS, PM->getPassManagerType());
Devang Patelf02b2512007-01-16 21:43:18 +00001765
1766 // [4] Push new manager into PMS
1767 PMS.push(FPP);
Fangrui Song9f0b0442019-11-28 14:00:12 -08001768 PM = FPP;
Devang Patel85c1e032007-01-08 19:29:38 +00001769 }
1770
Devang Patelf02b2512007-01-16 21:43:18 +00001771 // Assign FPP as the manager of this pass.
Fangrui Song9f0b0442019-11-28 14:00:12 -08001772 PM->add(this);
Devang Patel85c1e032007-01-08 19:29:38 +00001773}
1774
Arthur Eubanks59fa1bd2020-07-09 16:49:48 -07001775legacy::PassManagerBase::~PassManagerBase() {}