blob: d5c7e2874825d1d6cd63178ba12739584b5dba26 [file] [log] [blame]
Chandler Carruth7caea412013-11-09 12:26:54 +00001//===- LegacyPassManager.cpp - LLVM Pass Infrastructure Implementation ----===//
Devang Patel6e5a1132006-11-07 21:31:57 +00002//
Chandler Carruth2946cd72019-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 Patel6e5a1132006-11-07 21:31:57 +00006//
7//===----------------------------------------------------------------------===//
8//
Chandler Carruth7caea412013-11-09 12:26:54 +00009// This file implements the legacy LLVM Pass Manager infrastructure.
Devang Patel6e5a1132006-11-07 21:31:57 +000010//
11//===----------------------------------------------------------------------===//
12
Chandler Carruth7caea412013-11-09 12:26:54 +000013#include "llvm/IR/LegacyPassManager.h"
Florian Hahna4ffa3a2018-05-24 21:33:17 +000014#include "llvm/ADT/MapVector.h"
James Henderson852f6fd2017-05-16 09:43:21 +000015#include "llvm/ADT/Statistic.h"
Jessica Paquettee49374d2018-05-18 17:26:39 +000016#include "llvm/IR/DiagnosticInfo.h"
Pavel Labathec534e62016-10-25 16:20:07 +000017#include "llvm/IR/IRPrintingPasses.h"
18#include "llvm/IR/LLVMContext.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000019#include "llvm/IR/LegacyPassManagers.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000020#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "llvm/IR/Module.h"
Fedor Sergeev43083112018-08-28 21:06:51 +000022#include "llvm/IR/PassTimingInfo.h"
Pavel Labathec534e62016-10-25 16:20:07 +000023#include "llvm/Support/Chrono.h"
Devang Patelf1567a52006-12-13 20:03:48 +000024#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000025#include "llvm/Support/Debug.h"
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +000026#include "llvm/Support/Error.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000027#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000028#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000029#include "llvm/Support/Mutex.h"
Anton Afanasyevd880de22019-03-30 08:42:48 +000030#include "llvm/Support/TimeProfiler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/Timer.h"
32#include "llvm/Support/raw_ostream.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000033#include <algorithm>
Weiming Zhao0f1762c2016-01-06 22:55:03 +000034#include <unordered_set>
Dan Gohman8c43e412007-10-03 19:04:09 +000035using namespace llvm;
Chandler Carruth7caea412013-11-09 12:26:54 +000036using namespace llvm::legacy;
Devang Patelffca9102006-12-15 19:39:30 +000037
Devang Patele7599552007-01-12 18:52:44 +000038// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000039
Devang Patelf1567a52006-12-13 20:03:48 +000040//===----------------------------------------------------------------------===//
41// Pass debugging information. Often it is useful to find out what pass is
42// running when a crash occurs in a utility. When this library is compiled with
43// debugging on, a command line option (--debug-pass) is enabled that causes the
44// pass name to be printed before it executes.
45//
46
Chandler Carruth7caea412013-11-09 12:26:54 +000047namespace {
Devang Patel03fb5872006-12-13 21:13:31 +000048// Different debug levels that can be enabled...
49enum PassDebugLevel {
Dmitri Gribenko3238fb72013-05-05 00:40:33 +000050 Disabled, Arguments, Structure, Executions, Details
Devang Patel03fb5872006-12-13 21:13:31 +000051};
Chandler Carruth7caea412013-11-09 12:26:54 +000052}
Devang Patel03fb5872006-12-13 21:13:31 +000053
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000054static cl::opt<enum PassDebugLevel>
55PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000056 cl::desc("Print PassManager debugging information"),
57 cl::values(
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000058 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"),
Mehdi Amini732afdd2016-10-08 19:41:06 +000062 clEnumVal(Details , "print pass details when it is executed")));
David Greene9b063df2010-04-02 23:17:14 +000063
Chandler Carruth7caea412013-11-09 12:26:54 +000064namespace {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000065typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
66PassOptionList;
Chandler Carruth7caea412013-11-09 12:26:54 +000067}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000068
69// Print IR out before/after specified passes.
70static PassOptionList
71PrintBefore("print-before",
72 llvm::cl::desc("Print IR before specified passes"),
73 cl::Hidden);
74
75static PassOptionList
76PrintAfter("print-after",
77 llvm::cl::desc("Print IR after specified passes"),
78 cl::Hidden);
79
Zachary Turner8065f0b2017-12-01 00:53:10 +000080static cl::opt<bool> PrintBeforeAll("print-before-all",
81 llvm::cl::desc("Print IR before each pass"),
82 cl::init(false), cl::Hidden);
83static cl::opt<bool> PrintAfterAll("print-after-all",
84 llvm::cl::desc("Print IR after each pass"),
85 cl::init(false), cl::Hidden);
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000086
Fedor Sergeev94dca7c2017-12-01 17:42:46 +000087static cl::opt<bool>
88 PrintModuleScope("print-module-scope",
89 cl::desc("When printing IR for print-[before|after]{-all} "
90 "always print a module IR"),
Craig Topperf5730c32018-04-01 21:54:26 +000091 cl::init(false), cl::Hidden);
Fedor Sergeev94dca7c2017-12-01 17:42:46 +000092
Weiming Zhao0f1762c2016-01-06 22:55:03 +000093static cl::list<std::string>
94 PrintFuncsList("filter-print-funcs", cl::value_desc("function names"),
95 cl::desc("Only print IR for functions whose name "
96 "match this for all print-[before|after][-all] "
97 "options"),
Zachary Turner8065f0b2017-12-01 00:53:10 +000098 cl::CommaSeparated, cl::Hidden);
Weiming Zhao0f1762c2016-01-06 22:55:03 +000099
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000100/// This is a helper to determine whether to print IR before or
101/// after a pass.
102
Fedor Sergeev662e5682018-09-24 16:08:15 +0000103bool llvm::shouldPrintBeforePass() {
104 return PrintBeforeAll || !PrintBefore.empty();
105}
106
107bool llvm::shouldPrintAfterPass() {
108 return PrintAfterAll || !PrintAfter.empty();
109}
110
111static bool ShouldPrintBeforeOrAfterPass(StringRef PassID,
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000112 PassOptionList &PassesToPrint) {
Chris Bieneman664294c2015-04-29 21:45:22 +0000113 for (auto *PassInf : PassesToPrint) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000114 if (PassInf)
Fedor Sergeev662e5682018-09-24 16:08:15 +0000115 if (PassInf->getPassArgument() == PassID) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000116 return true;
117 }
David Greene9b063df2010-04-02 23:17:14 +0000118 }
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000119 return false;
120}
Dan Gohmande6188a2010-08-12 23:50:08 +0000121
Fedor Sergeev662e5682018-09-24 16:08:15 +0000122bool llvm::shouldPrintBeforePass(StringRef PassID) {
123 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PassID, PrintBefore);
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000124}
David Greene9b063df2010-04-02 23:17:14 +0000125
Fedor Sergeev662e5682018-09-24 16:08:15 +0000126bool llvm::shouldPrintAfterPass(StringRef PassID) {
127 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PassID, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000128}
129
Fedor Sergeev94dca7c2017-12-01 17:42:46 +0000130bool llvm::forcePrintModuleIR() { return PrintModuleScope; }
131
Weiming Zhao0f1762c2016-01-06 22:55:03 +0000132bool llvm::isFunctionInPrintList(StringRef FunctionName) {
133 static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(),
134 PrintFuncsList.end());
Benjamin Krameradcd0262020-01-28 20:23:46 +0100135 return PrintFuncNames.empty() ||
136 PrintFuncNames.count(std::string(FunctionName));
Weiming Zhao0f1762c2016-01-06 22:55:03 +0000137}
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000138/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
139/// or higher is specified.
140bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000141 return PassDebugging >= Executions;
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000142}
143
Jessica Paquettea0aa5b32018-09-06 21:19:54 +0000144unsigned PMDataManager::initSizeRemarkInfo(
145 Module &M, StringMap<std::pair<unsigned, unsigned>> &FunctionToInstrCount) {
Jessica Paquettee49374d2018-05-18 17:26:39 +0000146 // Only calculate getInstructionCount if the size-info remark is requested.
Jessica Paquettea0aa5b32018-09-06 21:19:54 +0000147 unsigned InstrCount = 0;
148
149 // Collect instruction counts for every function. We'll use this to emit
150 // per-function size remarks later.
151 for (Function &F : M) {
152 unsigned FCount = F.getInstructionCount();
153
154 // Insert a record into FunctionToInstrCount keeping track of the current
155 // size of the function as the first member of a pair. Set the second
156 // member to 0; if the function is deleted by the pass, then when we get
157 // here, we'll be able to let the user know that F no longer contributes to
158 // the module.
159 FunctionToInstrCount[F.getName().str()] =
160 std::pair<unsigned, unsigned>(FCount, 0);
161 InstrCount += FCount;
162 }
163 return InstrCount;
Jessica Paquettee49374d2018-05-18 17:26:39 +0000164}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000165
Jessica Paquettea0aa5b32018-09-06 21:19:54 +0000166void PMDataManager::emitInstrCountChangedRemark(
167 Pass *P, Module &M, int64_t Delta, unsigned CountBefore,
168 StringMap<std::pair<unsigned, unsigned>> &FunctionToInstrCount,
169 Function *F) {
Jessica Paquette397c05d2018-08-31 20:51:54 +0000170 // If it's a pass manager, don't emit a remark. (This hinges on the assumption
171 // that the only passes that return non-null with getAsPMDataManager are pass
172 // managers.) The reason we have to do this is to avoid emitting remarks for
173 // CGSCC passes.
174 if (P->getAsPMDataManager())
175 return;
176
Jessica Paquette31d2e5e2018-09-04 21:03:43 +0000177 // Set to true if this isn't a module pass or CGSCC pass.
178 bool CouldOnlyImpactOneFunction = (F != nullptr);
179
Jessica Paquettea0aa5b32018-09-06 21:19:54 +0000180 // Helper lambda that updates the changes to the size of some function.
181 auto UpdateFunctionChanges =
182 [&FunctionToInstrCount](Function &MaybeChangedFn) {
183 // Update the total module count.
184 unsigned FnSize = MaybeChangedFn.getInstructionCount();
185 auto It = FunctionToInstrCount.find(MaybeChangedFn.getName());
186
187 // If we created a new function, then we need to add it to the map and
188 // say that it changed from 0 instructions to FnSize.
189 if (It == FunctionToInstrCount.end()) {
190 FunctionToInstrCount[MaybeChangedFn.getName()] =
191 std::pair<unsigned, unsigned>(0, FnSize);
192 return;
193 }
194 // Insert the new function size into the second member of the pair. This
195 // tells us whether or not this function changed in size.
196 It->second.second = FnSize;
197 };
198
199 // We need to initially update all of the function sizes.
200 // If no function was passed in, then we're either a module pass or an
201 // CGSCC pass.
202 if (!CouldOnlyImpactOneFunction)
203 std::for_each(M.begin(), M.end(), UpdateFunctionChanges);
204 else
205 UpdateFunctionChanges(*F);
206
Jessica Paquette71e97782018-08-31 20:54:37 +0000207 // Do we have a function we can use to emit a remark?
Jessica Paquette31d2e5e2018-09-04 21:03:43 +0000208 if (!CouldOnlyImpactOneFunction) {
Jessica Paquette71e97782018-08-31 20:54:37 +0000209 // We need a function containing at least one basic block in order to output
210 // remarks. Since it's possible that the first function in the module
211 // doesn't actually contain a basic block, we have to go and find one that's
212 // suitable for emitting remarks.
213 auto It = std::find_if(M.begin(), M.end(),
214 [](const Function &Fn) { return !Fn.empty(); });
Jessica Paquettee49374d2018-05-18 17:26:39 +0000215
Jessica Paquette71e97782018-08-31 20:54:37 +0000216 // Didn't find a function. Quit.
217 if (It == M.end())
218 return;
Jessica Paquettee49374d2018-05-18 17:26:39 +0000219
Jessica Paquette71e97782018-08-31 20:54:37 +0000220 // We found a function containing at least one basic block.
221 F = &*It;
222 }
Jessica Paquette9a23c552018-08-31 20:20:57 +0000223 int64_t CountAfter = static_cast<int64_t>(CountBefore) + Delta;
Jessica Paquettee49374d2018-05-18 17:26:39 +0000224 BasicBlock &BB = *F->begin();
225 OptimizationRemarkAnalysis R("size-info", "IRSizeChange",
226 DiagnosticLocation(), &BB);
227 // FIXME: Move ore namespace to DiagnosticInfo so that we can use it. This
228 // would let us use NV instead of DiagnosticInfoOptimizationBase::Argument.
229 R << DiagnosticInfoOptimizationBase::Argument("Pass", P->getPassName())
230 << ": IR instruction count changed from "
231 << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", CountBefore)
232 << " to "
233 << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", CountAfter)
234 << "; Delta: "
235 << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", Delta);
236 F->getContext().diagnose(R); // Not using ORE for layering reasons.
Jessica Paquettea0aa5b32018-09-06 21:19:54 +0000237
238 // Emit per-function size change remarks separately.
239 std::string PassName = P->getPassName().str();
240
241 // Helper lambda that emits a remark when the size of a function has changed.
242 auto EmitFunctionSizeChangedRemark = [&FunctionToInstrCount, &F, &BB,
Benjamin Krameradcd0262020-01-28 20:23:46 +0100243 &PassName](StringRef Fname) {
Jessica Paquettea0aa5b32018-09-06 21:19:54 +0000244 unsigned FnCountBefore, FnCountAfter;
245 std::pair<unsigned, unsigned> &Change = FunctionToInstrCount[Fname];
246 std::tie(FnCountBefore, FnCountAfter) = Change;
247 int64_t FnDelta = static_cast<int64_t>(FnCountAfter) -
248 static_cast<int64_t>(FnCountBefore);
249
250 if (FnDelta == 0)
251 return;
252
253 // FIXME: We shouldn't use BB for the location here. Unfortunately, because
254 // the function that we're looking at could have been deleted, we can't use
255 // it for the source location. We *want* remarks when a function is deleted
256 // though, so we're kind of stuck here as is. (This remark, along with the
257 // whole-module size change remarks really ought not to have source
258 // locations at all.)
259 OptimizationRemarkAnalysis FR("size-info", "FunctionIRSizeChange",
260 DiagnosticLocation(), &BB);
261 FR << DiagnosticInfoOptimizationBase::Argument("Pass", PassName)
262 << ": Function: "
263 << DiagnosticInfoOptimizationBase::Argument("Function", Fname)
264 << ": IR instruction count changed from "
265 << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore",
266 FnCountBefore)
267 << " to "
268 << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter",
269 FnCountAfter)
270 << "; Delta: "
271 << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", FnDelta);
272 F->getContext().diagnose(FR);
273
274 // Update the function size.
275 Change.first = FnCountAfter;
276 };
277
278 // Are we looking at more than one function? If so, emit remarks for all of
279 // the functions in the module. Otherwise, only emit one remark.
280 if (!CouldOnlyImpactOneFunction)
281 std::for_each(FunctionToInstrCount.keys().begin(),
282 FunctionToInstrCount.keys().end(),
283 EmitFunctionSizeChangedRemark);
284 else
285 EmitFunctionSizeChangedRemark(F->getName().str());
Jessica Paquettee49374d2018-05-18 17:26:39 +0000286}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000287
Chris Lattner4c1e9542009-03-06 06:45:05 +0000288void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
Craig Topperc6207612014-04-09 06:08:46 +0000289 if (!V && !M)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000290 OS << "Releasing pass '";
291 else
292 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000293
Chris Lattner4c1e9542009-03-06 06:45:05 +0000294 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000295
Chris Lattner4c1e9542009-03-06 06:45:05 +0000296 if (M) {
297 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
298 return;
299 }
Craig Topperc6207612014-04-09 06:08:46 +0000300 if (!V) {
Chris Lattner4c1e9542009-03-06 06:45:05 +0000301 OS << '\n';
302 return;
303 }
304
Dan Gohman79fc0e92009-03-10 18:47:59 +0000305 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000306 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000307 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000308 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000309 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000310 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000311 OS << "value";
312
313 OS << " '";
Rui Ueyama49a3ad22019-07-16 04:46:31 +0000314 V->printAsOperand(OS, /*PrintType=*/false, M);
Dan Gohman79fc0e92009-03-10 18:47:59 +0000315 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000316}
317
Devang Patele7599552007-01-12 18:52:44 +0000318namespace llvm {
Chandler Carruth7caea412013-11-09 12:26:54 +0000319namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000320//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000321// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000322//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000323/// FunctionPassManagerImpl manages FPPassManagers
324class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000325 public PMDataManager,
326 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000327 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000328private:
329 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000330public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000331 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000332 explicit FunctionPassManagerImpl() :
333 Pass(PT_PassManager, ID), PMDataManager(),
334 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000335
Matthias Braun0880c602014-12-12 01:27:01 +0000336 /// \copydoc FunctionPassManager::add()
Devang Patel67d6a5e2006-12-19 19:46:59 +0000337 void add(Pass *P) {
338 schedulePass(P);
339 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000340
341 /// createPrinterPass - Get a function printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000342 Pass *createPrinterPass(raw_ostream &O,
343 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000344 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000345 }
346
Torok Edwin24c78352009-06-29 18:49:09 +0000347 // Prepare for running an on the fly pass, freeing memory if needed
348 // from a previous run.
349 void releaseMemoryOnTheFly();
350
Devang Patel67d6a5e2006-12-19 19:46:59 +0000351 /// run - Execute all of the passes scheduled for execution. Keep track of
352 /// whether any of the passes modifies the module, and if so, return true.
353 bool run(Function &F);
354
355 /// doInitialization - Run all of the initializers for the function passes.
356 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000357 bool doInitialization(Module &M) override;
Dan Gohmande6188a2010-08-12 23:50:08 +0000358
Dan Gohmane6656eb2007-07-30 14:51:13 +0000359 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000360 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000361 bool doFinalization(Module &M) override;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000362
Dan Gohmande6188a2010-08-12 23:50:08 +0000363
Craig Topperf398d7c2014-03-05 06:35:38 +0000364 PMDataManager *getAsPMDataManager() override { return this; }
365 Pass *getAsPass() override { return this; }
366 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000367 return PMT_FunctionPassManager;
368 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000369
Devang Patel67d6a5e2006-12-19 19:46:59 +0000370 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000371 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000372 Info.setPreservesAll();
373 }
374
Devang Patel67d6a5e2006-12-19 19:46:59 +0000375 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000376 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000377 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
378 return FP;
379 }
evgeny87eac7e2019-11-01 14:43:51 +0300380
381 void dumpPassStructure(unsigned Offset) override {
382 for (unsigned I = 0; I < getNumContainedManagers(); ++I)
383 getContainedManager(I)->dumpPassStructure(Offset);
384 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000385};
386
David Blaikiea379b1812011-12-20 02:50:00 +0000387void FunctionPassManagerImpl::anchor() {}
388
Devang Patel8c78a0b2007-05-03 01:11:54 +0000389char FunctionPassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000390} // End of legacy namespace
391} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000392
Chandler Carruth7caea412013-11-09 12:26:54 +0000393namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000394//===----------------------------------------------------------------------===//
395// MPPassManager
396//
397/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000398/// It batches all Module passes and function pass managers together and
399/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000400class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000401public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000402 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000403 explicit MPPassManager() :
404 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000405
406 // Delete on the fly managers.
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000407 ~MPPassManager() override {
Yaron Keren4849aa32015-06-05 17:48:47 +0000408 for (auto &OnTheFlyManager : OnTheFlyManagers) {
409 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Devang Patel2ff44922007-04-16 20:39:59 +0000410 delete FPP;
411 }
412 }
413
Dan Gohmande6188a2010-08-12 23:50:08 +0000414 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000415 Pass *createPrinterPass(raw_ostream &O,
416 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000417 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000418 }
419
Devang Patelca58e352006-11-08 10:05:38 +0000420 /// run - Execute all of the passes scheduled for execution. Keep track of
421 /// whether any of the passes modifies the module, and if so, return true.
422 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000423
Pedro Artigase4348b02012-12-03 21:56:57 +0000424 using llvm::Pass::doInitialization;
425 using llvm::Pass::doFinalization;
426
Devang Patelf9d96b92006-12-07 19:57:52 +0000427 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000428 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000429 Info.setPreservesAll();
430 }
431
Devang Patele64d3052007-04-16 20:12:57 +0000432 /// Add RequiredPass into list of lower level passes required by pass P.
433 /// RequiredPass is run on the fly by Pass Manager when P requests it
434 /// through getAnalysis interface.
Craig Topperf398d7c2014-03-05 06:35:38 +0000435 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patele64d3052007-04-16 20:12:57 +0000436
Dan Gohmande6188a2010-08-12 23:50:08 +0000437 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000438 /// required by module pass MP. Instantiate analysis pass, by using
439 /// its runOnFunction() for function F.
Craig Topperf398d7c2014-03-05 06:35:38 +0000440 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000441
Mehdi Amini117296c2016-10-01 02:56:57 +0000442 StringRef getPassName() const override { return "Module Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000443
Craig Topperf398d7c2014-03-05 06:35:38 +0000444 PMDataManager *getAsPMDataManager() override { return this; }
445 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000446
Devang Pateleda56172006-12-12 23:34:33 +0000447 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000448 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000449 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000450 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
451 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000452 MP->dumpPassStructure(Offset + 1);
Florian Hahna4ffa3a2018-05-24 21:33:17 +0000453 MapVector<Pass *, FunctionPassManagerImpl *>::const_iterator I =
454 OnTheFlyManagers.find(MP);
Dan Gohman83ff1842009-07-01 23:12:33 +0000455 if (I != OnTheFlyManagers.end())
456 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000457 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000458 }
459 }
460
Devang Patelabfbe3b2006-12-16 00:56:26 +0000461 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000462 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000463 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000464 }
465
Craig Topperf398d7c2014-03-05 06:35:38 +0000466 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000467 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000468 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000469
470 private:
471 /// Collection of on the fly FPPassManagers. These managers manage
472 /// function passes that are required by module passes.
Florian Hahna4ffa3a2018-05-24 21:33:17 +0000473 MapVector<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000474};
475
Devang Patel8c78a0b2007-05-03 01:11:54 +0000476char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000477} // End anonymous namespace
478
479namespace llvm {
480namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000481//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000482// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000483//
Devang Patel09f162c2007-05-01 21:15:47 +0000484
Devang Patel67d6a5e2006-12-19 19:46:59 +0000485/// PassManagerImpl manages MPPassManagers
486class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000487 public PMDataManager,
488 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000489 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000490
491public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000492 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000493 explicit PassManagerImpl() :
494 Pass(PT_PassManager, ID), PMDataManager(),
495 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000496
Matthias Braun0880c602014-12-12 01:27:01 +0000497 /// \copydoc PassManager::add()
Devang Patel31217af2006-12-07 21:32:57 +0000498 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000499 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000500 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000501
502 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000503 Pass *createPrinterPass(raw_ostream &O,
504 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000505 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000506 }
507
Devang Patel376fefa2006-11-08 10:29:57 +0000508 /// run - Execute all of the passes scheduled for execution. Keep track of
509 /// whether any of the passes modifies the module, and if so, return true.
510 bool run(Module &M);
511
Pedro Artigase4348b02012-12-03 21:56:57 +0000512 using llvm::Pass::doInitialization;
513 using llvm::Pass::doFinalization;
514
Devang Patelf9d96b92006-12-07 19:57:52 +0000515 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000516 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000517 Info.setPreservesAll();
518 }
519
Craig Topperf398d7c2014-03-05 06:35:38 +0000520 PMDataManager *getAsPMDataManager() override { return this; }
521 Pass *getAsPass() override { return this; }
522 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000523 return PMT_ModulePassManager;
524 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000525
Devang Patel67d6a5e2006-12-19 19:46:59 +0000526 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000527 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000528 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
529 return MP;
530 }
Devang Patel376fefa2006-11-08 10:29:57 +0000531};
532
David Blaikiea379b1812011-12-20 02:50:00 +0000533void PassManagerImpl::anchor() {}
534
Devang Patel8c78a0b2007-05-03 01:11:54 +0000535char PassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000536} // End of legacy namespace
537} // End of llvm namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000538
Devang Patela1514cb2006-12-07 19:39:39 +0000539//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000540// PMTopLevelManager implementation
541
Devang Patel4268fc02007-01-16 02:00:38 +0000542/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000543PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
544 PMDM->setTopLevelManager(this);
545 addPassManager(PMDM);
546 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000547}
548
Devang Patelafb1f3622006-12-12 22:35:25 +0000549/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000550void
Bill Wendlingea857e12012-05-14 07:53:40 +0000551PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000552 unsigned PDepth = 0;
553 if (P->getResolver())
554 PDepth = P->getResolver()->getPMDataManager().getDepth();
555
Yaron Keren4849aa32015-06-05 17:48:47 +0000556 for (Pass *AP : AnalysisPasses) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000557 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000558
Devang Patel01919d22007-03-08 19:05:01 +0000559 if (P == AP)
560 continue;
561
Tobias Grosserf07426b2011-01-20 21:03:22 +0000562 // Update the last users of passes that are required transitive by AP.
563 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
564 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
565 SmallVector<Pass *, 12> LastUses;
566 SmallVector<Pass *, 12> LastPMUses;
Yaron Keren83009952016-04-28 14:49:44 +0000567 for (AnalysisID ID : IDs) {
568 Pass *AnalysisPass = findAnalysisPass(ID);
Tobias Grosserf07426b2011-01-20 21:03:22 +0000569 assert(AnalysisPass && "Expected analysis pass to exist.");
570 AnalysisResolver *AR = AnalysisPass->getResolver();
571 assert(AR && "Expected analysis resolver to exist.");
572 unsigned APDepth = AR->getPMDataManager().getDepth();
573
574 if (PDepth == APDepth)
575 LastUses.push_back(AnalysisPass);
576 else if (PDepth > APDepth)
577 LastPMUses.push_back(AnalysisPass);
578 }
579
580 setLastUser(LastUses, P);
581
582 // If this pass has a corresponding pass manager, push higher level
583 // analysis to this pass manager.
584 if (P->getResolver())
585 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
586
587
Devang Patelafb1f3622006-12-12 22:35:25 +0000588 // If AP is the last user of other passes then make P last user of
589 // such passes.
Yaron Kerenbd873192016-10-02 19:21:41 +0000590 for (auto LU : LastUser) {
591 if (LU.second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000592 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000593 // this is just updating existing entries.
Yaron Kerenbd873192016-10-02 19:21:41 +0000594 LastUser[LU.first] = P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000595 }
596 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000597}
598
599/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000600void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000601 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000602 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000603 InversedLastUser.find(P);
604 if (DMI == InversedLastUser.end())
605 return;
606
607 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper46276792014-08-24 23:23:06 +0000608 for (Pass *LUP : LU) {
609 LastUses.push_back(LUP);
Devang Patelc68a0b62008-08-12 00:26:16 +0000610 }
611
Devang Patelafb1f3622006-12-12 22:35:25 +0000612}
613
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000614AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperc6207612014-04-09 06:08:46 +0000615 AnalysisUsage *AnUsage = nullptr;
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000616 auto DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000617 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000618 AnUsage = DMI->second;
619 else {
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000620 // Look up the analysis usage from the pass instance (different instances
621 // of the same pass can produce different results), but unique the
622 // resulting object to reduce memory usage. This helps to greatly reduce
623 // memory usage when we have many instances of only a few pass types
624 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
625 // of dependencies.
626 AnalysisUsage AU;
627 P->getAnalysisUsage(AU);
Bjorn Petterssonaa025802018-07-03 12:39:52 +0000628
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000629 AUFoldingSetNode* Node = nullptr;
630 FoldingSetNodeID ID;
631 AUFoldingSetNode::Profile(ID, AU);
632 void *IP = nullptr;
633 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
634 Node = N;
635 else {
Philip Reames000f77d2015-12-04 23:48:19 +0000636 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000637 UniqueAnalysisUsages.InsertNode(Node, IP);
638 }
639 assert(Node && "cached analysis usage must be non null");
640
641 AnUsageMap[P] = &Node->AU;
Mandeep Singh Grang5e1697e2017-06-06 05:08:36 +0000642 AnUsage = &Node->AU;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000643 }
644 return AnUsage;
645}
646
Devang Patelafb1f3622006-12-12 22:35:25 +0000647/// Schedule pass P for execution. Make sure that passes required by
648/// P are run before P is run. Update analysis info maintained by
649/// the manager. Remove dead passes. This is a recursive function.
650void PMTopLevelManager::schedulePass(Pass *P) {
651
Devang Patel3312f752007-01-16 21:43:18 +0000652 // TODO : Allocate function manager for this pass, other wise required set
653 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000654
Devang Pateld74ede72007-03-06 01:06:16 +0000655 // Give pass a chance to prepare the stage.
656 P->preparePassManager(activeStack);
657
Devang Patel864970e2008-03-18 00:39:19 +0000658 // If P is an analysis pass and it is available then do not
659 // generate the analysis again. Stale analysis info should not be
660 // available at this point.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000661 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Andersona7aed182010-08-06 18:33:48 +0000662 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Craig Topper4ee28412018-08-20 20:57:30 +0000663 // Remove any cached AnalysisUsage information.
664 AnUsageMap.erase(P);
Nuno Lopes0460bb22008-11-04 23:03:58 +0000665 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000666 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000667 }
Devang Patel864970e2008-03-18 00:39:19 +0000668
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000669 AnalysisUsage *AnUsage = findAnalysisUsage(P);
670
Devang Patelfdee7032008-08-14 23:07:48 +0000671 bool checkAnalysis = true;
672 while (checkAnalysis) {
673 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000674
Devang Patelfdee7032008-08-14 23:07:48 +0000675 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000676 for (const AnalysisID ID : RequiredSet) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000677
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000678 Pass *AnalysisPass = findAnalysisPass(ID);
Devang Patelfdee7032008-08-14 23:07:48 +0000679 if (!AnalysisPass) {
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000680 const PassInfo *PI = findAnalysisPassInfo(ID);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000681
Craig Topperc6207612014-04-09 06:08:46 +0000682 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000683 // Pass P is not in the global PassRegistry
684 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
685 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
686 dbgs() << "Required Passes:" << "\n";
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000687 for (const AnalysisID ID2 : RequiredSet) {
688 if (ID == ID2)
689 break;
690 Pass *AnalysisPass2 = findAnalysisPass(ID2);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000691 if (AnalysisPass2) {
692 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000693 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000694 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
695 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
696 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
697 }
698 }
699 }
700
Andrew Trick6bbaf132011-06-03 00:48:58 +0000701 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000702 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000703 if (P->getPotentialPassManagerType () ==
704 AnalysisPass->getPotentialPassManagerType())
705 // Schedule analysis pass that is managed by the same pass manager.
706 schedulePass(AnalysisPass);
707 else if (P->getPotentialPassManagerType () >
708 AnalysisPass->getPotentialPassManagerType()) {
709 // Schedule analysis pass that is managed by a new manager.
710 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000711 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000712 // are already checked are still available.
713 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000714 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000715 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000716 // passes are run on the fly.
717 delete AnalysisPass;
718 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000719 }
720 }
721
722 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000723 if (ImmutablePass *IP = P->getAsImmutablePass()) {
724 // P is a immutable pass and it will be managed by this
725 // top level manager. Set up analysis resolver to connect them.
726 PMDataManager *DM = getAsPMDataManager();
727 AnalysisResolver *AR = new AnalysisResolver(*DM);
728 P->setResolver(AR);
729 DM->initializeAnalysisImpl(P);
730 addImmutablePass(IP);
731 DM->recordAvailableAnalysis(IP);
732 return;
733 }
734
Fedor Sergeev662e5682018-09-24 16:08:15 +0000735 if (PI && !PI->isAnalysis() && shouldPrintBeforePass(PI->getPassArgument())) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000736 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000737 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000738 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
739 }
740
741 // Add the requested pass to the best available pass manager.
742 P->assignPassManager(activeStack, getTopLevelPassManagerType());
743
Fedor Sergeev662e5682018-09-24 16:08:15 +0000744 if (PI && !PI->isAnalysis() && shouldPrintAfterPass(PI->getPassArgument())) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000745 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000746 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000747 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
748 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000749}
750
751/// Find the pass that implements Analysis AID. Search immutable
752/// passes and all pass managers. If desired pass is not found
753/// then return NULL.
754Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000755 // For immutable passes we have a direct mapping from ID to pass, so check
756 // that first.
757 if (Pass *P = ImmutablePassMap.lookup(AID))
758 return P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000759
Devang Patelcd6ba152006-12-12 22:50:05 +0000760 // Check pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000761 for (PMDataManager *PassManager : PassManagers)
762 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000763 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000764
765 // Check other pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000766 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
767 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000768 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000769
Craig Topperc6207612014-04-09 06:08:46 +0000770 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000771}
772
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000773const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
774 const PassInfo *&PI = AnalysisPassInfos[AID];
775 if (!PI)
776 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
777 else
778 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
779 "The pass info pointer changed for an analysis ID!");
780
781 return PI;
782}
783
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000784void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
785 P->initializePass();
786 ImmutablePasses.push_back(P);
787
788 // Add this pass to the map from its analysis ID. We clobber any prior runs
789 // of the pass in the map so that the last one added is the one found when
790 // doing lookups.
791 AnalysisID AID = P->getPassID();
792 ImmutablePassMap[AID] = P;
793
794 // Also add any interfaces implemented by the immutable pass to the map for
795 // fast lookup.
796 const PassInfo *PassInf = findAnalysisPassInfo(AID);
797 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth87275182015-09-10 04:22:36 +0000798 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000799 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
800}
801
Devang Pateleda56172006-12-12 23:34:33 +0000802// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000803void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000804
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000805 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000806 return;
807
Devang Pateleda56172006-12-12 23:34:33 +0000808 // Print out the immutable passes
809 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000810 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000811 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000812
Dan Gohmanf71c5212010-08-19 01:29:07 +0000813 // Every class that derives from PMDataManager also derives from Pass
814 // (sometimes indirectly), but there's no inheritance relationship
815 // between PMDataManager and Pass, so we have to getAsPass to get
816 // from a PMDataManager* to a Pass*.
Yaron Keren4849aa32015-06-05 17:48:47 +0000817 for (PMDataManager *Manager : PassManagers)
818 Manager->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000819}
820
Devang Patel991aeba2006-12-15 20:13:01 +0000821void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000822
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000823 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000824 return;
825
David Greene994e1bb2010-01-05 01:30:02 +0000826 dbgs() << "Pass Arguments: ";
Yaron Keren83009952016-04-28 14:49:44 +0000827 for (ImmutablePass *P : ImmutablePasses)
828 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000829 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000830 if (!PI->isAnalysisGroup())
831 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000832 }
Yaron Keren83009952016-04-28 14:49:44 +0000833 for (PMDataManager *PM : PassManagers)
834 PM->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000835 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000836}
837
Devang Patele3068402006-12-21 00:16:50 +0000838void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren83009952016-04-28 14:49:44 +0000839 for (PMDataManager *PM : PassManagers)
840 PM->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000841
Devang Patele3068402006-12-21 00:16:50 +0000842 // Initailize other pass managers
Yaron Keren83009952016-04-28 14:49:44 +0000843 for (PMDataManager *IPM : IndirectPassManagers)
844 IPM->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000845
Yaron Kerenbd873192016-10-02 19:21:41 +0000846 for (auto LU : LastUser) {
847 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
848 L.insert(LU.first);
Devang Patelc68a0b62008-08-12 00:26:16 +0000849 }
Devang Patele3068402006-12-21 00:16:50 +0000850}
851
Devang Patele7599552007-01-12 18:52:44 +0000852/// Destructor
853PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren83009952016-04-28 14:49:44 +0000854 for (PMDataManager *PM : PassManagers)
855 delete PM;
Dan Gohmande6188a2010-08-12 23:50:08 +0000856
Yaron Keren83009952016-04-28 14:49:44 +0000857 for (ImmutablePass *P : ImmutablePasses)
858 delete P;
Devang Patele7599552007-01-12 18:52:44 +0000859}
860
Devang Patelafb1f3622006-12-12 22:35:25 +0000861//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000862// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000863
Devang Patel643676c2006-11-11 01:10:19 +0000864/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000865void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000866 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000867
Chris Lattner60987362009-03-06 05:53:14 +0000868 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000869
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000870 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000871
Dan Gohmande6188a2010-08-12 23:50:08 +0000872 // This pass is the current implementation of all of the interfaces it
873 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000874 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000875 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000876 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000877 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000878 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000879}
880
Devang Patel9d9fc902007-03-06 17:52:53 +0000881// Return true if P preserves high level analysis used by other
882// passes managed by this manager
883bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000884 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000885 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000886 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000887
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000888 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Kerenbd873192016-10-02 19:21:41 +0000889 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperc6207612014-04-09 06:08:46 +0000890 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000891 !is_contained(PreservedSet, P1->getPassID()))
Devang Patel01919d22007-03-08 19:05:01 +0000892 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000893 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000894
Devang Patel9d9fc902007-03-06 17:52:53 +0000895 return true;
896}
897
Chris Lattner02eb94c2008-08-07 07:34:50 +0000898/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000899void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000900 // Don't do this unless assertions are enabled.
901#ifdef NDEBUG
902 return;
903#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000904 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
905 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000906
Devang Patelef432532007-07-19 05:36:09 +0000907 // Verify preserved analysis
Yaron Kerenbd873192016-10-02 19:21:41 +0000908 for (AnalysisID AID : PreservedSet) {
Dan Gohman4dbb3012009-09-28 00:27:48 +0000909 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000910 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000911 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000912 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000913 }
914}
915
Devang Patel67c79a42008-07-01 19:50:56 +0000916/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000917void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000918 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
919 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000920 return;
921
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000922 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000923 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000924 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000925 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000926 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000927 !is_contained(PreservedSet, Info->first)) {
Devang Patel349170f2006-11-11 01:24:55 +0000928 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000929 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000930 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000931 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
932 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000933 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000934 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000935 }
Devang Patel349170f2006-11-11 01:24:55 +0000936 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000937
Devang Patel42dd1e92007-03-06 01:55:46 +0000938 // Check inherited analysis also. If P is not preserving analysis
939 // provided by parent manager then remove it here.
940 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
941
942 if (!InheritedAnalysis[Index])
943 continue;
944
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000945 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000946 I = InheritedAnalysis[Index]->begin(),
947 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000948 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000949 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000950 !is_contained(PreservedSet, Info->first)) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000951 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000952 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000953 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000954 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
955 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000956 }
Devang Patel01919d22007-03-08 19:05:01 +0000957 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000958 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000959 }
960 }
Devang Patelf68a3492006-11-07 22:35:17 +0000961}
962
Devang Patelca189262006-11-14 03:05:08 +0000963/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000964void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000965 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000966
Devang Patel8adae862007-07-20 18:04:54 +0000967 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000968
Devang Patel2ff44922007-04-16 20:39:59 +0000969 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000970 if (!TPM)
971 return;
972
Devang Patel17ad0962006-12-08 00:37:52 +0000973 TPM->collectLastUses(DeadPasses, P);
974
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000975 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000976 dbgs() << " -*- '" << P->getPassName();
977 dbgs() << "' is the last user of following pass instances.";
978 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000979 }
980
Yaron Kerenbd873192016-10-02 19:21:41 +0000981 for (Pass *P : DeadPasses)
982 freePass(P, Msg, DBG_STR);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000983}
Devang Patel200d3052006-12-13 23:50:44 +0000984
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000985void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000986 enum PassDebuggingString DBG_STR) {
987 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000988
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000989 {
990 // If the pass crashes releasing memory, remember this.
991 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000992 TimeRegion PassTimer(getPassTimer(P));
993
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000994 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000995 }
996
Owen Andersona7aed182010-08-06 18:33:48 +0000997 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000998 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000999 // Remove the pass itself (if it is not already removed).
1000 AvailableAnalysis.erase(PI);
1001
1002 // Remove all interfaces this pass implements, for which it is also
1003 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +00001004 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +00001005 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001006 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +00001007 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001008 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +00001009 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +00001010 }
Devang Patel17ad0962006-12-08 00:37:52 +00001011 }
Devang Patelca189262006-11-14 03:05:08 +00001012}
1013
Dan Gohmande6188a2010-08-12 23:50:08 +00001014/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +00001015/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +00001016void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +00001017 // This manager is going to manage pass P. Set up analysis resolver
1018 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +00001019 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +00001020 P->setResolver(AR);
1021
Devang Patelec2b9a72007-03-05 22:57:49 +00001022 // If a FunctionPass F is the last user of ModulePass info M
1023 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +00001024 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +00001025
Chris Lattner60987362009-03-06 05:53:14 +00001026 if (!ProcessAnalysis) {
1027 // Add pass
1028 PassVector.push_back(P);
1029 return;
Devang Patel90b05e02006-11-11 02:04:19 +00001030 }
Devang Patel8cad70d2006-11-11 01:51:02 +00001031
Chris Lattner60987362009-03-06 05:53:14 +00001032 // At the moment, this pass is the last user of all required passes.
1033 SmallVector<Pass *, 12> LastUses;
Chandler Carruth44a13852015-08-19 03:02:12 +00001034 SmallVector<Pass *, 8> UsedPasses;
Chris Lattner60987362009-03-06 05:53:14 +00001035 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
1036
1037 unsigned PDepth = this->getDepth();
1038
Chandler Carruth44a13852015-08-19 03:02:12 +00001039 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
1040 for (Pass *PUsed : UsedPasses) {
Chris Lattner60987362009-03-06 05:53:14 +00001041 unsigned RDepth = 0;
1042
Chandler Carruth44a13852015-08-19 03:02:12 +00001043 assert(PUsed->getResolver() && "Analysis Resolver is not set");
1044 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattner60987362009-03-06 05:53:14 +00001045 RDepth = DM.getDepth();
1046
1047 if (PDepth == RDepth)
Chandler Carruth44a13852015-08-19 03:02:12 +00001048 LastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001049 else if (PDepth > RDepth) {
1050 // Let the parent claim responsibility of last use
Chandler Carruth44a13852015-08-19 03:02:12 +00001051 TransferLastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001052 // Keep track of higher level analysis used by this manager.
Chandler Carruth44a13852015-08-19 03:02:12 +00001053 HigherLevelAnalysis.push_back(PUsed);
Dan Gohmande6188a2010-08-12 23:50:08 +00001054 } else
Chandler Carruth44a13852015-08-19 03:02:12 +00001055 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001056 }
1057
1058 // Set P as P's last user until someone starts using P.
1059 // However, if P is a Pass Manager then it does not need
1060 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001061 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001062 LastUses.push_back(P);
1063 TPM->setLastUser(LastUses, P);
1064
1065 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001066 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001067 TPM->setLastUser(TransferLastUses, My_PM);
1068 TransferLastUses.clear();
1069 }
1070
Dan Gohman6304db32010-08-16 22:57:28 +00001071 // Now, take care of required analyses that are not available.
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001072 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1073 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Andersona7aed182010-08-06 18:33:48 +00001074 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001075 this->addLowerLevelRequiredPass(P, AnalysisPass);
1076 }
1077
1078 // Take a note of analysis required and made available by this pass.
1079 // Remove the analysis not preserved by this pass
1080 removeNotPreservedAnalysis(P);
1081 recordAvailableAnalysis(P);
1082
Devang Patel8cad70d2006-11-11 01:51:02 +00001083 // Add pass
1084 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001085}
1086
Devang Patele64d3052007-04-16 20:12:57 +00001087
Chandler Carruth44a13852015-08-19 03:02:12 +00001088/// Populate UP with analysis pass that are used or required by
Devang Patele64d3052007-04-16 20:12:57 +00001089/// pass P and are available. Populate RP_NotAvail with analysis
1090/// pass that are required by pass P but are not available.
Chandler Carruth44a13852015-08-19 03:02:12 +00001091void PMDataManager::collectRequiredAndUsedAnalyses(
1092 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1093 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001094 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth44a13852015-08-19 03:02:12 +00001095
1096 for (const auto &UsedID : AnUsage->getUsedSet())
1097 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1098 UP.push_back(AnalysisPass);
1099
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001100 for (const auto &RequiredID : AnUsage->getRequiredSet())
1101 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001102 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001103 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001104 RP_NotAvail.push_back(RequiredID);
Devang Patelf58183d2006-12-12 23:09:32 +00001105
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001106 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1107 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001108 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001109 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001110 RP_NotAvail.push_back(RequiredID);
Devang Patel1d6267c2006-12-07 23:05:44 +00001111}
1112
Devang Patel07f4f582006-11-14 21:49:36 +00001113// All Required analyses should be available to the pass as it runs! Here
1114// we fill in the AnalysisImpls member of the pass so that it can
1115// successfully use the getAnalysis() method to retrieve the
1116// implementations it needs.
1117//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001118void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001119 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1120
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001121 for (const AnalysisID ID : AnUsage->getRequiredSet()) {
1122 Pass *Impl = findAnalysisPass(ID, true);
Craig Topperc6207612014-04-09 06:08:46 +00001123 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001124 // This may be analysis pass that is initialized on the fly.
1125 // If that is not the case then it will raise an assert when it is used.
1126 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001127 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001128 assert(AR && "Analysis Resolver is not set");
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001129 AR->addAnalysisImplsPair(ID, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001130 }
1131}
1132
Devang Patel640c5bb2006-12-08 22:30:11 +00001133/// Find the pass that implements Analysis AID. If desired pass is not found
1134/// then return NULL.
1135Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1136
1137 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001138 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001139
1140 if (I != AvailableAnalysis.end())
1141 return I->second;
1142
1143 // Search Parents through TopLevelManager
1144 if (SearchParent)
1145 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001146
Craig Topperc6207612014-04-09 06:08:46 +00001147 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001148}
1149
Devang Patel991aeba2006-12-15 20:13:01 +00001150// Print list of passes that are last used by P.
1151void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1152
Devang Patel8adae862007-07-20 18:04:54 +00001153 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001154
1155 // If this is a on the fly manager then it does not have TPM.
1156 if (!TPM)
1157 return;
1158
Devang Patel991aeba2006-12-15 20:13:01 +00001159 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001160
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001161 for (Pass *P : LUses) {
Eric Christophera13839f2014-02-26 23:27:16 +00001162 dbgs() << "--" << std::string(Offset*2, ' ');
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001163 P->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001164 }
1165}
1166
1167void PMDataManager::dumpPassArguments() const {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001168 for (Pass *P : PassVector) {
1169 if (PMDataManager *PMD = P->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001170 PMD->dumpPassArguments();
1171 else
Owen Andersona7aed182010-08-06 18:33:48 +00001172 if (const PassInfo *PI =
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001173 TPM->findAnalysisPassInfo(P->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001174 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001175 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001176 }
1177}
1178
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001179void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1180 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001181 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001182 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001183 return;
Pavel Labathec534e62016-10-25 16:20:07 +00001184 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
Chandler Carruth20c56932014-04-27 23:59:25 +00001185 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001186 switch (S1) {
1187 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001188 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001189 break;
1190 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001191 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001192 break;
1193 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001194 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001195 break;
1196 default:
1197 break;
1198 }
1199 switch (S2) {
Devang Patel003a5592007-03-05 20:01:30 +00001200 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001201 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001202 break;
1203 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001204 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001205 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001206 case ON_REGION_MSG:
1207 dbgs() << "' on Region '" << Msg << "'...\n";
1208 break;
Devang Patel003a5592007-03-05 20:01:30 +00001209 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001210 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001211 break;
1212 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001213 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001214 break;
1215 default:
1216 break;
1217 }
Devang Patel991aeba2006-12-15 20:13:01 +00001218}
1219
Chris Lattner4c1e9542009-03-06 06:45:05 +00001220void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001221 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001222 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001223
Chris Lattner4c493d92008-08-08 15:14:09 +00001224 AnalysisUsage analysisUsage;
1225 P->getAnalysisUsage(analysisUsage);
1226 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1227}
1228
Chris Lattner4c1e9542009-03-06 06:45:05 +00001229void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001230 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001231 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001232
Chris Lattner4c493d92008-08-08 15:14:09 +00001233 AnalysisUsage analysisUsage;
1234 P->getAnalysisUsage(analysisUsage);
1235 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1236}
1237
Chandler Carruth44a13852015-08-19 03:02:12 +00001238void PMDataManager::dumpUsedSet(const Pass *P) const {
1239 if (PassDebugging < Details)
1240 return;
1241
1242 AnalysisUsage analysisUsage;
1243 P->getAnalysisUsage(analysisUsage);
1244 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1245}
1246
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001247void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001248 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001249 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001250 if (Set.empty())
1251 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001252 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001253 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001254 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001255 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001256 if (!PInf) {
1257 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1258 // all drivers.
1259 dbgs() << " Uninitialized Pass";
1260 continue;
1261 }
Owen Andersona7aed182010-08-06 18:33:48 +00001262 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001263 }
David Greene994e1bb2010-01-05 01:30:02 +00001264 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001265}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001266
Devang Patel004937b2007-07-27 20:06:09 +00001267/// Add RequiredPass into list of lower level passes required by pass P.
1268/// RequiredPass is run on the fly by Pass Manager when P requests it
1269/// through getAnalysis interface.
1270/// This should be handled by specific pass manager.
1271void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1272 if (TPM) {
1273 TPM->dumpArguments();
1274 TPM->dumpPasses();
1275 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001276
Dan Gohmande6188a2010-08-12 23:50:08 +00001277 // Module Level pass may required Function Level analysis info
1278 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1279 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001280 // module level pass is requiring lower level analysis info managed by
1281 // lower level pass manager.
1282
1283 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001284 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001285 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001286#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001287 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1288 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001289#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001290 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001291}
1292
Owen Andersona7aed182010-08-06 18:33:48 +00001293Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001294 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001295}
1296
Devang Patele7599552007-01-12 18:52:44 +00001297// Destructor
1298PMDataManager::~PMDataManager() {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001299 for (Pass *P : PassVector)
1300 delete P;
Devang Patele7599552007-01-12 18:52:44 +00001301}
1302
Devang Patel9bdf7d42006-12-08 23:28:54 +00001303//===----------------------------------------------------------------------===//
1304// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001305// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1306Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001307 return PM.findAnalysisPass(ID, dir);
1308}
1309
Dan Gohmande6188a2010-08-12 23:50:08 +00001310Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001311 Function &F) {
1312 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1313}
1314
Devang Patela1514cb2006-12-07 19:39:39 +00001315//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001316// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001317
Devang Patel4e12f862006-11-08 10:44:40 +00001318/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001319FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001320 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001321 // FPM is the top level manager.
1322 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001323
Dan Gohman565df952008-03-13 02:08:36 +00001324 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001325 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001326}
1327
Devang Patelb67904d2006-12-13 02:36:01 +00001328FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001329 delete FPM;
1330}
1331
Dan Gohmande6188a2010-08-12 23:50:08 +00001332void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001333 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001334}
1335
Devang Patel9f3083e2006-11-15 19:39:54 +00001336/// run - Execute all of the passes scheduled for execution. Keep
1337/// track of whether any of the passes modifies the function, and if
1338/// so, return true.
1339///
Devang Patelb67904d2006-12-13 02:36:01 +00001340bool FunctionPassManager::run(Function &F) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +00001341 handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
1342 report_fatal_error("Error reading bitcode file: " + EIB.message());
1343 });
Devang Patel272908d2006-12-08 22:57:48 +00001344 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001345}
1346
1347
Devang Patelff631ae2006-11-15 01:27:05 +00001348/// doInitialization - Run all of the initializers for the function passes.
1349///
Devang Patelb67904d2006-12-13 02:36:01 +00001350bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001351 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001352}
1353
Dan Gohmane6656eb2007-07-30 14:51:13 +00001354/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001355///
Devang Patelb67904d2006-12-13 02:36:01 +00001356bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001357 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001358}
1359
Devang Patela1514cb2006-12-07 19:39:39 +00001360//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001361// FunctionPassManagerImpl implementation
1362//
Duncan Sands51495602009-02-13 09:42:34 +00001363bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001364 bool Changed = false;
1365
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001366 dumpArguments();
1367 dumpPasses();
1368
Yaron Keren4849aa32015-06-05 17:48:47 +00001369 for (ImmutablePass *ImPass : getImmutablePasses())
1370 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001371
Chris Lattner4c1e9542009-03-06 06:45:05 +00001372 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1373 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001374
1375 return Changed;
1376}
1377
Duncan Sands51495602009-02-13 09:42:34 +00001378bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001379 bool Changed = false;
1380
Pedro Artigas41b98842012-12-05 17:12:22 +00001381 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001382 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001383
Yaron Keren4849aa32015-06-05 17:48:47 +00001384 for (ImmutablePass *ImPass : getImmutablePasses())
1385 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001386
Devang Patel67d6a5e2006-12-19 19:46:59 +00001387 return Changed;
1388}
1389
Devang Patelec9c58f2009-04-01 22:34:41 +00001390/// cleanup - After running all passes, clean up pass manager cache.
1391void FPPassManager::cleanup() {
1392 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1393 FunctionPass *FP = getContainedPass(Index);
1394 AnalysisResolver *AR = FP->getResolver();
1395 assert(AR && "Analysis Resolver is not set");
1396 AR->clearAnalysisImpls();
1397 }
1398}
1399
Torok Edwin24c78352009-06-29 18:49:09 +00001400void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1401 if (!wasRun)
1402 return;
1403 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1404 FPPassManager *FPPM = getContainedManager(Index);
1405 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1406 FPPM->getContainedPass(Index)->releaseMemory();
1407 }
1408 }
Torok Edwin896556e2009-06-29 21:05:10 +00001409 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001410}
1411
Devang Patel67d6a5e2006-12-19 19:46:59 +00001412// Execute all the passes managed by this top level manager.
1413// Return true if any function is modified by a pass.
1414bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001415 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001416
Devang Patele3068402006-12-21 00:16:50 +00001417 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001418 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001419 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001420 F.getContext().yield();
1421 }
Devang Patelec9c58f2009-04-01 22:34:41 +00001422
1423 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1424 getContainedManager(Index)->cleanup();
1425
Torok Edwin24c78352009-06-29 18:49:09 +00001426 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001427 return Changed;
1428}
1429
1430//===----------------------------------------------------------------------===//
1431// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001432
Devang Patel8c78a0b2007-05-03 01:11:54 +00001433char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001434/// Print passes managed by this manager
1435void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001436 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001437 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1438 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001439 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001440 dumpLastUses(FP, Offset+1);
1441 }
1442}
1443
1444
Dan Gohmande6188a2010-08-12 23:50:08 +00001445/// Execute all of the passes scheduled for execution by invoking
1446/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001447/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001448bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001449 if (F.isDeclaration())
1450 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001451
1452 bool Changed = false;
Jessica Paquettee49374d2018-05-18 17:26:39 +00001453 Module &M = *F.getParent();
Devang Patelcbbf2912008-03-20 01:09:53 +00001454 // Collect inherited analysis from Module level pass manager.
1455 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001456
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001457 unsigned InstrCount, FunctionSize = 0;
Jessica Paquettea0aa5b32018-09-06 21:19:54 +00001458 StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
Xin Tong023e25a2018-07-22 05:27:41 +00001459 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001460 // Collect the initial size of the module.
1461 if (EmitICRemark) {
Jessica Paquettea0aa5b32018-09-06 21:19:54 +00001462 InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001463 FunctionSize = F.getInstructionCount();
1464 }
1465
Nico Weberf2654b62019-04-20 23:22:45 +00001466 llvm::TimeTraceScope FunctionScope("OptFunction", F.getName());
Anton Afanasyevd880de22019-03-30 08:42:48 +00001467
Devang Patelabfbe3b2006-12-16 00:56:26 +00001468 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1469 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001470 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001471
Nico Weberf2654b62019-04-20 23:22:45 +00001472 llvm::TimeTraceScope PassScope("RunPass", FP->getPassName());
1473
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001474 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001475 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001476
Devang Patelabfbe3b2006-12-16 00:56:26 +00001477 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001478
Chris Lattner4c1e9542009-03-06 06:45:05 +00001479 {
1480 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001481 TimeRegion PassTimer(getPassTimer(FP));
Dan Gohman74b189f2010-03-01 17:34:28 +00001482 LocalChanged |= FP->runOnFunction(F);
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001483 if (EmitICRemark) {
1484 unsigned NewSize = F.getInstructionCount();
1485
1486 // Update the size of the function, emit a remark, and update the size
1487 // of the module.
1488 if (NewSize != FunctionSize) {
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001489 int64_t Delta = static_cast<int64_t>(NewSize) -
1490 static_cast<int64_t>(FunctionSize);
Jessica Paquettea0aa5b32018-09-06 21:19:54 +00001491 emitInstrCountChangedRemark(FP, M, Delta, InstrCount,
1492 FunctionToInstrCount, &F);
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001493 InstrCount = static_cast<int64_t>(InstrCount) + Delta;
1494 FunctionSize = NewSize;
1495 }
1496 }
Chris Lattner4c1e9542009-03-06 06:45:05 +00001497 }
Devang Patel93a197c2006-12-14 00:08:04 +00001498
Dan Gohman74b189f2010-03-01 17:34:28 +00001499 Changed |= LocalChanged;
1500 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001501 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001502 dumpPreservedSet(FP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001503 dumpUsedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001504
Devang Patela273d1c2007-07-19 18:02:32 +00001505 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001506 removeNotPreservedAnalysis(FP);
1507 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001508 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001509 }
Anton Afanasyevd880de22019-03-30 08:42:48 +00001510
Devang Patel9f3083e2006-11-15 19:39:54 +00001511 return Changed;
1512}
1513
Devang Patel67d6a5e2006-12-19 19:46:59 +00001514bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001515 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001516
Serge Pavloved5eb932017-01-15 10:23:18 +00001517 for (Function &F : M)
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001518 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001519
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001520 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001521}
1522
Duncan Sands51495602009-02-13 09:42:34 +00001523bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001524 bool Changed = false;
1525
Chris Lattner4c1e9542009-03-06 06:45:05 +00001526 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1527 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001528
Devang Patelff631ae2006-11-15 01:27:05 +00001529 return Changed;
1530}
1531
Duncan Sands51495602009-02-13 09:42:34 +00001532bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001533 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001534
1535 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001536 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001537
Devang Patelff631ae2006-11-15 01:27:05 +00001538 return Changed;
1539}
1540
Devang Patela1514cb2006-12-07 19:39:39 +00001541//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001542// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001543
Dan Gohmande6188a2010-08-12 23:50:08 +00001544/// Execute all of the passes scheduled for execution by invoking
1545/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001546/// the module, and if so, return true.
1547bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001548MPPassManager::runOnModule(Module &M) {
Anton Afanasyevd880de22019-03-30 08:42:48 +00001549 llvm::TimeTraceScope TimeScope("OptModule", M.getName());
1550
Devang Patel05e1a972006-11-07 22:03:15 +00001551 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001552
Torok Edwin24c78352009-06-29 18:49:09 +00001553 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001554 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1555 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001556 Changed |= FPP->doInitialization(M);
1557 }
1558
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001559 // Initialize module passes
1560 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1561 Changed |= getContainedPass(Index)->doInitialization(M);
1562
Fangrui Song38cd36402019-07-12 15:18:29 +00001563 unsigned InstrCount;
Jessica Paquettea0aa5b32018-09-06 21:19:54 +00001564 StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
Xin Tong023e25a2018-07-22 05:27:41 +00001565 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Jessica Paquette454d1032018-08-31 20:20:55 +00001566 // Collect the initial size of the module.
Fangrui Song38cd36402019-07-12 15:18:29 +00001567 if (EmitICRemark)
Jessica Paquettea0aa5b32018-09-06 21:19:54 +00001568 InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
Jessica Paquette454d1032018-08-31 20:20:55 +00001569
Devang Patelabfbe3b2006-12-16 00:56:26 +00001570 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1571 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001572 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001573
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001574 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001575 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001576
Devang Patelabfbe3b2006-12-16 00:56:26 +00001577 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001578
Chris Lattner4c1e9542009-03-06 06:45:05 +00001579 {
1580 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001581 TimeRegion PassTimer(getPassTimer(MP));
1582
Dan Gohman74b189f2010-03-01 17:34:28 +00001583 LocalChanged |= MP->runOnModule(M);
Jessica Paquette454d1032018-08-31 20:20:55 +00001584 if (EmitICRemark) {
1585 // Update the size of the module.
Fangrui Song38cd36402019-07-12 15:18:29 +00001586 unsigned ModuleCount = M.getInstructionCount();
Jessica Paquette454d1032018-08-31 20:20:55 +00001587 if (ModuleCount != InstrCount) {
Jessica Paquette9a23c552018-08-31 20:20:57 +00001588 int64_t Delta = static_cast<int64_t>(ModuleCount) -
1589 static_cast<int64_t>(InstrCount);
Jessica Paquettea0aa5b32018-09-06 21:19:54 +00001590 emitInstrCountChangedRemark(MP, M, Delta, InstrCount,
1591 FunctionToInstrCount);
Jessica Paquettea69696d2018-08-31 22:43:41 +00001592 InstrCount = ModuleCount;
Jessica Paquette454d1032018-08-31 20:20:55 +00001593 }
1594 }
Chris Lattner4c1e9542009-03-06 06:45:05 +00001595 }
Devang Patel93a197c2006-12-14 00:08:04 +00001596
Dan Gohman74b189f2010-03-01 17:34:28 +00001597 Changed |= LocalChanged;
1598 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001599 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001600 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001601 dumpPreservedSet(MP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001602 dumpUsedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001603
Devang Patela273d1c2007-07-19 18:02:32 +00001604 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001605 removeNotPreservedAnalysis(MP);
1606 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001607 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001608 }
Torok Edwin24c78352009-06-29 18:49:09 +00001609
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001610 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001611 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001612 Changed |= getContainedPass(Index)->doFinalization(M);
1613
Torok Edwin24c78352009-06-29 18:49:09 +00001614 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001615 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1616 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001617 // We don't know when is the last time an on-the-fly pass is run,
1618 // so we need to releaseMemory / finalize here
1619 FPP->releaseMemoryOnTheFly();
1620 Changed |= FPP->doFinalization(M);
1621 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001622
Devang Patel05e1a972006-11-07 22:03:15 +00001623 return Changed;
1624}
1625
Devang Patele64d3052007-04-16 20:12:57 +00001626/// Add RequiredPass into list of lower level passes required by pass P.
1627/// RequiredPass is run on the fly by Pass Manager when P requests it
1628/// through getAnalysis interface.
1629void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Dávid Bolvanský5ccad892019-11-02 20:14:29 +01001630 assert(RequiredPass && "No required pass?");
Chris Lattner60987362009-03-06 05:53:14 +00001631 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1632 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001633 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001634 RequiredPass->getPotentialPassManagerType()) &&
1635 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001636
Devang Patel68f72b12007-04-26 17:50:19 +00001637 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001638 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001639 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001640 // FPP is the top level manager.
1641 FPP->setTopLevelManager(FPP);
1642
Devang Patel69e9f6d2007-04-16 20:27:05 +00001643 OnTheFlyManagers[P] = FPP;
1644 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001645 const PassInfo *RequiredPassPI =
1646 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001647
Craig Topperc6207612014-04-09 06:08:46 +00001648 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001649 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1650 FoundPass =
1651 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001652 }
Andrew Trick02066f22014-04-08 03:40:34 +00001653 if (!FoundPass) {
1654 FoundPass = RequiredPass;
1655 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001656 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001657 FPP->add(RequiredPass);
1658 }
1659 // Register P as the last user of FoundPass or RequiredPass.
1660 SmallVector<Pass *, 1> LU;
1661 LU.push_back(FoundPass);
1662 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001663}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001664
Dan Gohmande6188a2010-08-12 23:50:08 +00001665/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001666/// required by module pass MP. Instantiate analysis pass, by using
1667/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001668Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001669 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001670 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001671
Torok Edwin24c78352009-06-29 18:49:09 +00001672 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001673 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001674 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001675}
1676
1677
Devang Patela1514cb2006-12-07 19:39:39 +00001678//===----------------------------------------------------------------------===//
1679// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001680
Devang Patelab97cf42006-12-13 00:09:23 +00001681//
Devang Patelc290c8a2006-11-07 22:23:34 +00001682/// run - Execute all of the passes scheduled for execution. Keep track of
1683/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001684bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001685 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001686
Devang Patelcfd70c42006-12-13 22:10:00 +00001687 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001688 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001689
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001690 for (ImmutablePass *ImPass : getImmutablePasses())
1691 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001692
Devang Patele3068402006-12-21 00:16:50 +00001693 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001694 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001695 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001696 M.getContext().yield();
1697 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001698
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001699 for (ImmutablePass *ImPass : getImmutablePasses())
1700 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001701
Devang Patelc290c8a2006-11-07 22:23:34 +00001702 return Changed;
1703}
Devang Patel376fefa2006-11-08 10:29:57 +00001704
Devang Patela1514cb2006-12-07 19:39:39 +00001705//===----------------------------------------------------------------------===//
1706// PassManager implementation
1707
Devang Patel376fefa2006-11-08 10:29:57 +00001708/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001709PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001710 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001711 // PM is the top level manager
1712 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001713}
1714
Devang Patelb67904d2006-12-13 02:36:01 +00001715PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001716 delete PM;
1717}
1718
Chris Lattner60987362009-03-06 05:53:14 +00001719void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001720 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001721}
1722
1723/// run - Execute all of the passes scheduled for execution. Keep track of
1724/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001725bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001726 return PM->run(M);
1727}
1728
Devang Patelb8817b92006-12-14 00:59:42 +00001729//===----------------------------------------------------------------------===//
Devang Patel1c56a632007-01-08 19:29:38 +00001730// PMStack implementation
1731//
Devang Patelad98d232007-01-11 22:15:30 +00001732
Devang Patel1c56a632007-01-08 19:29:38 +00001733// Pop Pass Manager from the stack and clear its analysis info.
1734void PMStack::pop() {
1735
1736 PMDataManager *Top = this->top();
1737 Top->initializeAnalysisInfo();
1738
1739 S.pop_back();
1740}
1741
1742// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001743void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001744 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001745 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001746
Chris Lattner60987362009-03-06 05:53:14 +00001747 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001748 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1749 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001750 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001751
Chris Lattner60987362009-03-06 05:53:14 +00001752 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001753 TPM->addIndirectPassManager(PM);
1754 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001755 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001756 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001757 assert((PM->getPassManagerType() == PMT_ModulePassManager
1758 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001759 && "pushing bad pass manager to PMStack");
1760 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001761 }
1762
Devang Patel15701b52007-01-11 00:19:00 +00001763 S.push_back(PM);
1764}
1765
1766// Dump content of the pass manager stack.
Yaron Kereneb2a2542016-01-29 20:50:44 +00001767LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren4849aa32015-06-05 17:48:47 +00001768 for (PMDataManager *Manager : S)
1769 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001770
Devang Patel15701b52007-01-11 00:19:00 +00001771 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001772 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001773}
1774
Devang Patel1c56a632007-01-08 19:29:38 +00001775/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001776/// add self into that manager.
1777void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001778 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001779 // Find Module Pass Manager
Fangrui Songa7abe6e2019-11-28 13:34:32 -08001780 PassManagerType T;
1781 while ((T = PMS.top()->getPassManagerType()) > PMT_ModulePassManager &&
1782 T != PreferredType)
1783 PMS.pop();
Devang Patel23f8aa92007-01-17 21:19:23 +00001784 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001785}
1786
Devang Patel3312f752007-01-16 21:43:18 +00001787/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001788/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001789void FunctionPass::assignPassManager(PMStack &PMS,
Fangrui Song4adddbd2019-11-28 14:00:12 -08001790 PassManagerType /*PreferredType*/) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001791 // Find Function Pass Manager
Fangrui Song4adddbd2019-11-28 14:00:12 -08001792 PMDataManager *PM;
1793 while (PM = PMS.top(), PM->getPassManagerType() > PMT_FunctionPassManager)
Fangrui Songa7abe6e2019-11-28 13:34:32 -08001794 PMS.pop();
Devang Patel3312f752007-01-16 21:43:18 +00001795
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001796 // Create new Function Pass Manager if needed.
Fangrui Song4adddbd2019-11-28 14:00:12 -08001797 if (PM->getPassManagerType() != PMT_FunctionPassManager) {
Devang Patel3312f752007-01-16 21:43:18 +00001798 // [1] Create new Function Pass Manager
Fangrui Song4adddbd2019-11-28 14:00:12 -08001799 auto *FPP = new FPPassManager;
Devang Patelcbbf2912008-03-20 01:09:53 +00001800 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001801
1802 // [2] Set up new manager's top level manager
Fangrui Song4adddbd2019-11-28 14:00:12 -08001803 PM->getTopLevelManager()->addIndirectPassManager(FPP);
Devang Patel3312f752007-01-16 21:43:18 +00001804
1805 // [3] Assign manager to manage this new manager. This may create
1806 // and push new managers into PMS
Fangrui Song4adddbd2019-11-28 14:00:12 -08001807 FPP->assignPassManager(PMS, PM->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001808
1809 // [4] Push new manager into PMS
1810 PMS.push(FPP);
Fangrui Song4adddbd2019-11-28 14:00:12 -08001811 PM = FPP;
Devang Patel1c56a632007-01-08 19:29:38 +00001812 }
1813
Devang Patel3312f752007-01-16 21:43:18 +00001814 // Assign FPP as the manager of this pass.
Fangrui Song4adddbd2019-11-28 14:00:12 -08001815 PM->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001816}
1817
Dan Gohmand3a20c92008-03-11 16:41:42 +00001818PassManagerBase::~PassManagerBase() {}