Chandler Carruth | 7caea41 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 1 | //===- LegacyPassManager.cpp - LLVM Pass Infrastructure Implementation ----===// |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Chandler Carruth | 7caea41 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 9 | // This file implements the legacy LLVM Pass Manager infrastructure. |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Chandler Carruth | 7caea41 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 13 | #include "llvm/IR/LegacyPassManager.h" |
Florian Hahn | a4ffa3a | 2018-05-24 21:33:17 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/MapVector.h" |
James Henderson | 852f6fd | 2017-05-16 09:43:21 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Statistic.h" |
Jessica Paquette | e49374d | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 16 | #include "llvm/IR/DiagnosticInfo.h" |
Pavel Labath | ec534e6 | 2016-10-25 16:20:07 +0000 | [diff] [blame] | 17 | #include "llvm/IR/IRPrintingPasses.h" |
| 18 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 7caea41 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LegacyPassManagers.h" |
Chandler Carruth | 1b69ed8 | 2014-03-04 12:32:42 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LegacyPassNameParser.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Module.h" |
Fedor Sergeev | 4308311 | 2018-08-28 21:06:51 +0000 | [diff] [blame] | 22 | #include "llvm/IR/PassTimingInfo.h" |
Pavel Labath | ec534e6 | 2016-10-25 16:20:07 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Chrono.h" |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Error.h" |
Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ManagedStatic.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Mutex.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Timer.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
Jeff Cohen | b622c11 | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 32 | #include <algorithm> |
Weiming Zhao | 0f1762c | 2016-01-06 22:55:03 +0000 | [diff] [blame] | 33 | #include <unordered_set> |
Dan Gohman | 8c43e41 | 2007-10-03 19:04:09 +0000 | [diff] [blame] | 34 | using namespace llvm; |
Chandler Carruth | 7caea41 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 35 | using namespace llvm::legacy; |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 36 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 37 | // See PassManagers.h for Pass Manager infrastructure overview. |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 38 | |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | // Pass debugging information. Often it is useful to find out what pass is |
| 41 | // running when a crash occurs in a utility. When this library is compiled with |
| 42 | // debugging on, a command line option (--debug-pass) is enabled that causes the |
| 43 | // pass name to be printed before it executes. |
| 44 | // |
| 45 | |
Chandler Carruth | 7caea41 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 46 | namespace { |
Devang Patel | 03fb587 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 47 | // Different debug levels that can be enabled... |
| 48 | enum PassDebugLevel { |
Dmitri Gribenko | 3238fb7 | 2013-05-05 00:40:33 +0000 | [diff] [blame] | 49 | Disabled, Arguments, Structure, Executions, Details |
Devang Patel | 03fb587 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 50 | }; |
Chandler Carruth | 7caea41 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 51 | } |
Devang Patel | 03fb587 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 52 | |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 53 | static cl::opt<enum PassDebugLevel> |
| 54 | PassDebugging("debug-pass", cl::Hidden, |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 55 | cl::desc("Print PassManager debugging information"), |
| 56 | cl::values( |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 57 | clEnumVal(Disabled , "disable debug output"), |
| 58 | clEnumVal(Arguments , "print pass arguments to pass to 'opt'"), |
| 59 | clEnumVal(Structure , "print pass structure before run()"), |
| 60 | clEnumVal(Executions, "print pass name before it is executed"), |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 61 | clEnumVal(Details , "print pass details when it is executed"))); |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 62 | |
Chandler Carruth | 7caea41 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 63 | namespace { |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 64 | typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser> |
| 65 | PassOptionList; |
Chandler Carruth | 7caea41 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 66 | } |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 67 | |
| 68 | // Print IR out before/after specified passes. |
| 69 | static PassOptionList |
| 70 | PrintBefore("print-before", |
| 71 | llvm::cl::desc("Print IR before specified passes"), |
| 72 | cl::Hidden); |
| 73 | |
| 74 | static PassOptionList |
| 75 | PrintAfter("print-after", |
| 76 | llvm::cl::desc("Print IR after specified passes"), |
| 77 | cl::Hidden); |
| 78 | |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 79 | static cl::opt<bool> PrintBeforeAll("print-before-all", |
| 80 | llvm::cl::desc("Print IR before each pass"), |
| 81 | cl::init(false), cl::Hidden); |
| 82 | static cl::opt<bool> PrintAfterAll("print-after-all", |
| 83 | llvm::cl::desc("Print IR after each pass"), |
| 84 | cl::init(false), cl::Hidden); |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 85 | |
Fedor Sergeev | 94dca7c | 2017-12-01 17:42:46 +0000 | [diff] [blame] | 86 | static cl::opt<bool> |
| 87 | PrintModuleScope("print-module-scope", |
| 88 | cl::desc("When printing IR for print-[before|after]{-all} " |
| 89 | "always print a module IR"), |
Craig Topper | f5730c3 | 2018-04-01 21:54:26 +0000 | [diff] [blame] | 90 | cl::init(false), cl::Hidden); |
Fedor Sergeev | 94dca7c | 2017-12-01 17:42:46 +0000 | [diff] [blame] | 91 | |
Weiming Zhao | 0f1762c | 2016-01-06 22:55:03 +0000 | [diff] [blame] | 92 | static cl::list<std::string> |
| 93 | PrintFuncsList("filter-print-funcs", cl::value_desc("function names"), |
| 94 | cl::desc("Only print IR for functions whose name " |
| 95 | "match this for all print-[before|after][-all] " |
| 96 | "options"), |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 97 | cl::CommaSeparated, cl::Hidden); |
Weiming Zhao | 0f1762c | 2016-01-06 22:55:03 +0000 | [diff] [blame] | 98 | |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 99 | /// This is a helper to determine whether to print IR before or |
| 100 | /// after a pass. |
| 101 | |
Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 102 | bool llvm::shouldPrintBeforePass() { |
| 103 | return PrintBeforeAll || !PrintBefore.empty(); |
| 104 | } |
| 105 | |
| 106 | bool llvm::shouldPrintAfterPass() { |
| 107 | return PrintAfterAll || !PrintAfter.empty(); |
| 108 | } |
| 109 | |
| 110 | static bool ShouldPrintBeforeOrAfterPass(StringRef PassID, |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 111 | PassOptionList &PassesToPrint) { |
Chris Bieneman | 664294c | 2015-04-29 21:45:22 +0000 | [diff] [blame] | 112 | for (auto *PassInf : PassesToPrint) { |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 113 | if (PassInf) |
Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 114 | if (PassInf->getPassArgument() == PassID) { |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 115 | return true; |
| 116 | } |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 117 | } |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 118 | return false; |
| 119 | } |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 120 | |
Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 121 | bool llvm::shouldPrintBeforePass(StringRef PassID) { |
| 122 | return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PassID, PrintBefore); |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 123 | } |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 124 | |
Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 125 | bool llvm::shouldPrintAfterPass(StringRef PassID) { |
| 126 | return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PassID, PrintAfter); |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Fedor Sergeev | 94dca7c | 2017-12-01 17:42:46 +0000 | [diff] [blame] | 129 | bool llvm::forcePrintModuleIR() { return PrintModuleScope; } |
| 130 | |
Weiming Zhao | 0f1762c | 2016-01-06 22:55:03 +0000 | [diff] [blame] | 131 | bool llvm::isFunctionInPrintList(StringRef FunctionName) { |
| 132 | static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(), |
| 133 | PrintFuncsList.end()); |
| 134 | return PrintFuncNames.empty() || PrintFuncNames.count(FunctionName); |
| 135 | } |
Chris Lattner | d4d966f | 2009-09-15 05:03:04 +0000 | [diff] [blame] | 136 | /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions |
| 137 | /// or higher is specified. |
| 138 | bool PMDataManager::isPassDebuggingExecutionsOrMore() const { |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 139 | return PassDebugging >= Executions; |
Chris Lattner | d4d966f | 2009-09-15 05:03:04 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Jessica Paquette | a0aa5b3 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 142 | unsigned PMDataManager::initSizeRemarkInfo( |
| 143 | Module &M, StringMap<std::pair<unsigned, unsigned>> &FunctionToInstrCount) { |
Jessica Paquette | e49374d | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 144 | // Only calculate getInstructionCount if the size-info remark is requested. |
Jessica Paquette | a0aa5b3 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 145 | unsigned InstrCount = 0; |
| 146 | |
| 147 | // Collect instruction counts for every function. We'll use this to emit |
| 148 | // per-function size remarks later. |
| 149 | for (Function &F : M) { |
| 150 | unsigned FCount = F.getInstructionCount(); |
| 151 | |
| 152 | // Insert a record into FunctionToInstrCount keeping track of the current |
| 153 | // size of the function as the first member of a pair. Set the second |
| 154 | // member to 0; if the function is deleted by the pass, then when we get |
| 155 | // here, we'll be able to let the user know that F no longer contributes to |
| 156 | // the module. |
| 157 | FunctionToInstrCount[F.getName().str()] = |
| 158 | std::pair<unsigned, unsigned>(FCount, 0); |
| 159 | InstrCount += FCount; |
| 160 | } |
| 161 | return InstrCount; |
Jessica Paquette | e49374d | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 162 | } |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 163 | |
Jessica Paquette | a0aa5b3 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 164 | void PMDataManager::emitInstrCountChangedRemark( |
| 165 | Pass *P, Module &M, int64_t Delta, unsigned CountBefore, |
| 166 | StringMap<std::pair<unsigned, unsigned>> &FunctionToInstrCount, |
| 167 | Function *F) { |
Jessica Paquette | 397c05d | 2018-08-31 20:51:54 +0000 | [diff] [blame] | 168 | // If it's a pass manager, don't emit a remark. (This hinges on the assumption |
| 169 | // that the only passes that return non-null with getAsPMDataManager are pass |
| 170 | // managers.) The reason we have to do this is to avoid emitting remarks for |
| 171 | // CGSCC passes. |
| 172 | if (P->getAsPMDataManager()) |
| 173 | return; |
| 174 | |
Jessica Paquette | 31d2e5e | 2018-09-04 21:03:43 +0000 | [diff] [blame] | 175 | // Set to true if this isn't a module pass or CGSCC pass. |
| 176 | bool CouldOnlyImpactOneFunction = (F != nullptr); |
| 177 | |
Jessica Paquette | a0aa5b3 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 178 | // Helper lambda that updates the changes to the size of some function. |
| 179 | auto UpdateFunctionChanges = |
| 180 | [&FunctionToInstrCount](Function &MaybeChangedFn) { |
| 181 | // Update the total module count. |
| 182 | unsigned FnSize = MaybeChangedFn.getInstructionCount(); |
| 183 | auto It = FunctionToInstrCount.find(MaybeChangedFn.getName()); |
| 184 | |
| 185 | // If we created a new function, then we need to add it to the map and |
| 186 | // say that it changed from 0 instructions to FnSize. |
| 187 | if (It == FunctionToInstrCount.end()) { |
| 188 | FunctionToInstrCount[MaybeChangedFn.getName()] = |
| 189 | std::pair<unsigned, unsigned>(0, FnSize); |
| 190 | return; |
| 191 | } |
| 192 | // Insert the new function size into the second member of the pair. This |
| 193 | // tells us whether or not this function changed in size. |
| 194 | It->second.second = FnSize; |
| 195 | }; |
| 196 | |
| 197 | // We need to initially update all of the function sizes. |
| 198 | // If no function was passed in, then we're either a module pass or an |
| 199 | // CGSCC pass. |
| 200 | if (!CouldOnlyImpactOneFunction) |
| 201 | std::for_each(M.begin(), M.end(), UpdateFunctionChanges); |
| 202 | else |
| 203 | UpdateFunctionChanges(*F); |
| 204 | |
Jessica Paquette | 71e9778 | 2018-08-31 20:54:37 +0000 | [diff] [blame] | 205 | // Do we have a function we can use to emit a remark? |
Jessica Paquette | 31d2e5e | 2018-09-04 21:03:43 +0000 | [diff] [blame] | 206 | if (!CouldOnlyImpactOneFunction) { |
Jessica Paquette | 71e9778 | 2018-08-31 20:54:37 +0000 | [diff] [blame] | 207 | // We need a function containing at least one basic block in order to output |
| 208 | // remarks. Since it's possible that the first function in the module |
| 209 | // doesn't actually contain a basic block, we have to go and find one that's |
| 210 | // suitable for emitting remarks. |
| 211 | auto It = std::find_if(M.begin(), M.end(), |
| 212 | [](const Function &Fn) { return !Fn.empty(); }); |
Jessica Paquette | e49374d | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 213 | |
Jessica Paquette | 71e9778 | 2018-08-31 20:54:37 +0000 | [diff] [blame] | 214 | // Didn't find a function. Quit. |
| 215 | if (It == M.end()) |
| 216 | return; |
Jessica Paquette | e49374d | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 217 | |
Jessica Paquette | 71e9778 | 2018-08-31 20:54:37 +0000 | [diff] [blame] | 218 | // We found a function containing at least one basic block. |
| 219 | F = &*It; |
| 220 | } |
Jessica Paquette | 9a23c55 | 2018-08-31 20:20:57 +0000 | [diff] [blame] | 221 | int64_t CountAfter = static_cast<int64_t>(CountBefore) + Delta; |
Jessica Paquette | e49374d | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 222 | BasicBlock &BB = *F->begin(); |
| 223 | OptimizationRemarkAnalysis R("size-info", "IRSizeChange", |
| 224 | DiagnosticLocation(), &BB); |
| 225 | // FIXME: Move ore namespace to DiagnosticInfo so that we can use it. This |
| 226 | // would let us use NV instead of DiagnosticInfoOptimizationBase::Argument. |
| 227 | R << DiagnosticInfoOptimizationBase::Argument("Pass", P->getPassName()) |
| 228 | << ": IR instruction count changed from " |
| 229 | << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", CountBefore) |
| 230 | << " to " |
| 231 | << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", CountAfter) |
| 232 | << "; Delta: " |
| 233 | << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", Delta); |
| 234 | F->getContext().diagnose(R); // Not using ORE for layering reasons. |
Jessica Paquette | a0aa5b3 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 235 | |
| 236 | // Emit per-function size change remarks separately. |
| 237 | std::string PassName = P->getPassName().str(); |
| 238 | |
| 239 | // Helper lambda that emits a remark when the size of a function has changed. |
| 240 | auto EmitFunctionSizeChangedRemark = [&FunctionToInstrCount, &F, &BB, |
| 241 | &PassName](const std::string &Fname) { |
| 242 | unsigned FnCountBefore, FnCountAfter; |
| 243 | std::pair<unsigned, unsigned> &Change = FunctionToInstrCount[Fname]; |
| 244 | std::tie(FnCountBefore, FnCountAfter) = Change; |
| 245 | int64_t FnDelta = static_cast<int64_t>(FnCountAfter) - |
| 246 | static_cast<int64_t>(FnCountBefore); |
| 247 | |
| 248 | if (FnDelta == 0) |
| 249 | return; |
| 250 | |
| 251 | // FIXME: We shouldn't use BB for the location here. Unfortunately, because |
| 252 | // the function that we're looking at could have been deleted, we can't use |
| 253 | // it for the source location. We *want* remarks when a function is deleted |
| 254 | // though, so we're kind of stuck here as is. (This remark, along with the |
| 255 | // whole-module size change remarks really ought not to have source |
| 256 | // locations at all.) |
| 257 | OptimizationRemarkAnalysis FR("size-info", "FunctionIRSizeChange", |
| 258 | DiagnosticLocation(), &BB); |
| 259 | FR << DiagnosticInfoOptimizationBase::Argument("Pass", PassName) |
| 260 | << ": Function: " |
| 261 | << DiagnosticInfoOptimizationBase::Argument("Function", Fname) |
| 262 | << ": IR instruction count changed from " |
| 263 | << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", |
| 264 | FnCountBefore) |
| 265 | << " to " |
| 266 | << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", |
| 267 | FnCountAfter) |
| 268 | << "; Delta: " |
| 269 | << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", FnDelta); |
| 270 | F->getContext().diagnose(FR); |
| 271 | |
| 272 | // Update the function size. |
| 273 | Change.first = FnCountAfter; |
| 274 | }; |
| 275 | |
| 276 | // Are we looking at more than one function? If so, emit remarks for all of |
| 277 | // the functions in the module. Otherwise, only emit one remark. |
| 278 | if (!CouldOnlyImpactOneFunction) |
| 279 | std::for_each(FunctionToInstrCount.keys().begin(), |
| 280 | FunctionToInstrCount.keys().end(), |
| 281 | EmitFunctionSizeChangedRemark); |
| 282 | else |
| 283 | EmitFunctionSizeChangedRemark(F->getName().str()); |
Jessica Paquette | e49374d | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 284 | } |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 286 | void PassManagerPrettyStackEntry::print(raw_ostream &OS) const { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 287 | if (!V && !M) |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 288 | OS << "Releasing pass '"; |
| 289 | else |
| 290 | OS << "Running pass '"; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 291 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 292 | OS << P->getPassName() << "'"; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 294 | if (M) { |
| 295 | OS << " on module '" << M->getModuleIdentifier() << "'.\n"; |
| 296 | return; |
| 297 | } |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 298 | if (!V) { |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 299 | OS << '\n'; |
| 300 | return; |
| 301 | } |
| 302 | |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 303 | OS << " on "; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 304 | if (isa<Function>(V)) |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 305 | OS << "function"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 306 | else if (isa<BasicBlock>(V)) |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 307 | OS << "basic block"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 308 | else |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 309 | OS << "value"; |
| 310 | |
| 311 | OS << " '"; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 312 | V->printAsOperand(OS, /*PrintTy=*/false, M); |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 313 | OS << "'\n"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 317 | namespace { |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 318 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 319 | // BBPassManager |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 320 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 321 | /// BBPassManager manages BasicBlockPass. It batches all the |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 322 | /// pass together and sequence them to process one basic block before |
| 323 | /// processing next basic block. |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 324 | class BBPassManager : public PMDataManager, public FunctionPass { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 325 | |
| 326 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 327 | static char ID; |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 328 | explicit BBPassManager() |
| 329 | : PMDataManager(), FunctionPass(ID) {} |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 330 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 331 | /// Execute all of the passes scheduled for execution. Keep track of |
| 332 | /// whether any of the passes modifies the function, and if so, return true. |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 333 | bool runOnFunction(Function &F) override; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 334 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 335 | /// Pass Manager itself does not invalidate any analysis info. |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 336 | void getAnalysisUsage(AnalysisUsage &Info) const override { |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 337 | Info.setPreservesAll(); |
| 338 | } |
| 339 | |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 340 | bool doInitialization(Module &M) override; |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 341 | bool doInitialization(Function &F); |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 342 | bool doFinalization(Module &M) override; |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 343 | bool doFinalization(Function &F); |
| 344 | |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 345 | PMDataManager *getAsPMDataManager() override { return this; } |
| 346 | Pass *getAsPass() override { return this; } |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 347 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 348 | StringRef getPassName() const override { return "BasicBlock Pass Manager"; } |
Devang Patel | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 349 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 350 | // Print passes managed by this manager |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 351 | void dumpPassStructure(unsigned Offset) override { |
Eric Christopher | a13839f | 2014-02-26 23:27:16 +0000 | [diff] [blame] | 352 | dbgs().indent(Offset*2) << "BasicBlockPass Manager\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 353 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 354 | BasicBlockPass *BP = getContainedPass(Index); |
Dan Gohman | f71c521 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 355 | BP->dumpPassStructure(Offset + 1); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 356 | dumpLastUses(BP, Offset+1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 359 | |
| 360 | BasicBlockPass *getContainedPass(unsigned N) { |
Evan Cheng | 66dbd3f | 2012-11-13 02:56:38 +0000 | [diff] [blame] | 361 | assert(N < PassVector.size() && "Pass number out of range!"); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 362 | BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]); |
| 363 | return BP; |
| 364 | } |
Devang Patel | 3b3f899 | 2007-01-11 01:10:25 +0000 | [diff] [blame] | 365 | |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 366 | PassManagerType getPassManagerType() const override { |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 367 | return PMT_BasicBlockPassManager; |
Devang Patel | 3b3f899 | 2007-01-11 01:10:25 +0000 | [diff] [blame] | 368 | } |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 369 | }; |
| 370 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 371 | char BBPassManager::ID = 0; |
Chandler Carruth | 7caea41 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 372 | } // End anonymous namespace |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 373 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 374 | namespace llvm { |
Chandler Carruth | 7caea41 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 375 | namespace legacy { |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 376 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 377 | // FunctionPassManagerImpl |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 378 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 379 | /// FunctionPassManagerImpl manages FPPassManagers |
| 380 | class FunctionPassManagerImpl : public Pass, |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 381 | public PMDataManager, |
| 382 | public PMTopLevelManager { |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 383 | virtual void anchor(); |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 384 | private: |
| 385 | bool wasRun; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 386 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 387 | static char ID; |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 388 | explicit FunctionPassManagerImpl() : |
| 389 | Pass(PT_PassManager, ID), PMDataManager(), |
| 390 | PMTopLevelManager(new FPPassManager()), wasRun(false) {} |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 391 | |
Matthias Braun | 0880c60 | 2014-12-12 01:27:01 +0000 | [diff] [blame] | 392 | /// \copydoc FunctionPassManager::add() |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 393 | void add(Pass *P) { |
| 394 | schedulePass(P); |
| 395 | } |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 396 | |
| 397 | /// createPrinterPass - Get a function printer pass. |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 398 | Pass *createPrinterPass(raw_ostream &O, |
| 399 | const std::string &Banner) const override { |
Chandler Carruth | 9d80513 | 2014-01-12 11:30:46 +0000 | [diff] [blame] | 400 | return createPrintFunctionPass(O, Banner); |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 403 | // Prepare for running an on the fly pass, freeing memory if needed |
| 404 | // from a previous run. |
| 405 | void releaseMemoryOnTheFly(); |
| 406 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 407 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 408 | /// whether any of the passes modifies the module, and if so, return true. |
| 409 | bool run(Function &F); |
| 410 | |
| 411 | /// doInitialization - Run all of the initializers for the function passes. |
| 412 | /// |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 413 | bool doInitialization(Module &M) override; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 414 | |
Dan Gohman | e6656eb | 2007-07-30 14:51:13 +0000 | [diff] [blame] | 415 | /// doFinalization - Run all of the finalizers for the function passes. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 416 | /// |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 417 | bool doFinalization(Module &M) override; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 418 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 419 | |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 420 | PMDataManager *getAsPMDataManager() override { return this; } |
| 421 | Pass *getAsPass() override { return this; } |
| 422 | PassManagerType getTopLevelPassManagerType() override { |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 423 | return PMT_FunctionPassManager; |
| 424 | } |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 425 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 426 | /// Pass Manager itself does not invalidate any analysis info. |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 427 | void getAnalysisUsage(AnalysisUsage &Info) const override { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 428 | Info.setPreservesAll(); |
| 429 | } |
| 430 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 431 | FPPassManager *getContainedManager(unsigned N) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 432 | assert(N < PassManagers.size() && "Pass number out of range!"); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 433 | FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]); |
| 434 | return FP; |
| 435 | } |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 436 | }; |
| 437 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 438 | void FunctionPassManagerImpl::anchor() {} |
| 439 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 440 | char FunctionPassManagerImpl::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 441 | } // End of legacy namespace |
| 442 | } // End of llvm namespace |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 443 | |
Chandler Carruth | 7caea41 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 444 | namespace { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 445 | //===----------------------------------------------------------------------===// |
| 446 | // MPPassManager |
| 447 | // |
| 448 | /// MPPassManager manages ModulePasses and function pass managers. |
Dan Gohman | dfdf2c0 | 2008-03-11 16:18:48 +0000 | [diff] [blame] | 449 | /// It batches all Module passes and function pass managers together and |
| 450 | /// sequences them to process one module. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 451 | class MPPassManager : public Pass, public PMDataManager { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 452 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 453 | static char ID; |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 454 | explicit MPPassManager() : |
| 455 | Pass(PT_PassManager, ID), PMDataManager() { } |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 456 | |
| 457 | // Delete on the fly managers. |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 458 | ~MPPassManager() override { |
Yaron Keren | 4849aa3 | 2015-06-05 17:48:47 +0000 | [diff] [blame] | 459 | for (auto &OnTheFlyManager : OnTheFlyManagers) { |
| 460 | FunctionPassManagerImpl *FPP = OnTheFlyManager.second; |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 461 | delete FPP; |
| 462 | } |
| 463 | } |
| 464 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 465 | /// createPrinterPass - Get a module printer pass. |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 466 | Pass *createPrinterPass(raw_ostream &O, |
| 467 | const std::string &Banner) const override { |
Chandler Carruth | 9d80513 | 2014-01-12 11:30:46 +0000 | [diff] [blame] | 468 | return createPrintModulePass(O, Banner); |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 471 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 472 | /// whether any of the passes modifies the module, and if so, return true. |
| 473 | bool runOnModule(Module &M); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 474 | |
Pedro Artigas | e4348b0 | 2012-12-03 21:56:57 +0000 | [diff] [blame] | 475 | using llvm::Pass::doInitialization; |
| 476 | using llvm::Pass::doFinalization; |
| 477 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 478 | /// Pass Manager itself does not invalidate any analysis info. |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 479 | void getAnalysisUsage(AnalysisUsage &Info) const override { |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 480 | Info.setPreservesAll(); |
| 481 | } |
| 482 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 483 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 484 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 485 | /// through getAnalysis interface. |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 486 | void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override; |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 487 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 488 | /// Return function pass corresponding to PassInfo PI, that is |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 489 | /// required by module pass MP. Instantiate analysis pass, by using |
| 490 | /// its runOnFunction() for function F. |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 491 | Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override; |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 492 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 493 | StringRef getPassName() const override { return "Module Pass Manager"; } |
Devang Patel | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 494 | |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 495 | PMDataManager *getAsPMDataManager() override { return this; } |
| 496 | Pass *getAsPass() override { return this; } |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 497 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 498 | // Print passes managed by this manager |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 499 | void dumpPassStructure(unsigned Offset) override { |
Eric Christopher | a13839f | 2014-02-26 23:27:16 +0000 | [diff] [blame] | 500 | dbgs().indent(Offset*2) << "ModulePass Manager\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 501 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 502 | ModulePass *MP = getContainedPass(Index); |
Dan Gohman | f71c521 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 503 | MP->dumpPassStructure(Offset + 1); |
Florian Hahn | a4ffa3a | 2018-05-24 21:33:17 +0000 | [diff] [blame] | 504 | MapVector<Pass *, FunctionPassManagerImpl *>::const_iterator I = |
| 505 | OnTheFlyManagers.find(MP); |
Dan Gohman | 83ff184 | 2009-07-01 23:12:33 +0000 | [diff] [blame] | 506 | if (I != OnTheFlyManagers.end()) |
| 507 | I->second->dumpPassStructure(Offset + 2); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 508 | dumpLastUses(MP, Offset+1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 512 | ModulePass *getContainedPass(unsigned N) { |
Evan Cheng | 66dbd3f | 2012-11-13 02:56:38 +0000 | [diff] [blame] | 513 | assert(N < PassVector.size() && "Pass number out of range!"); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 514 | return static_cast<ModulePass *>(PassVector[N]); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 517 | PassManagerType getPassManagerType() const override { |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 518 | return PMT_ModulePassManager; |
Devang Patel | 28349ab | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 519 | } |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 520 | |
| 521 | private: |
| 522 | /// Collection of on the fly FPPassManagers. These managers manage |
| 523 | /// function passes that are required by module passes. |
Florian Hahn | a4ffa3a | 2018-05-24 21:33:17 +0000 | [diff] [blame] | 524 | MapVector<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 525 | }; |
| 526 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 527 | char MPPassManager::ID = 0; |
Chandler Carruth | 7caea41 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 528 | } // End anonymous namespace |
| 529 | |
| 530 | namespace llvm { |
| 531 | namespace legacy { |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 532 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 533 | // PassManagerImpl |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 534 | // |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 535 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 536 | /// PassManagerImpl manages MPPassManagers |
| 537 | class PassManagerImpl : public Pass, |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 538 | public PMDataManager, |
| 539 | public PMTopLevelManager { |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 540 | virtual void anchor(); |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 541 | |
| 542 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 543 | static char ID; |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 544 | explicit PassManagerImpl() : |
| 545 | Pass(PT_PassManager, ID), PMDataManager(), |
| 546 | PMTopLevelManager(new MPPassManager()) {} |
Devang Patel | 4c36e6b | 2006-12-07 23:24:58 +0000 | [diff] [blame] | 547 | |
Matthias Braun | 0880c60 | 2014-12-12 01:27:01 +0000 | [diff] [blame] | 548 | /// \copydoc PassManager::add() |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 549 | void add(Pass *P) { |
Devang Patel | df6c9ae | 2006-12-08 22:34:02 +0000 | [diff] [blame] | 550 | schedulePass(P); |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 551 | } |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 552 | |
| 553 | /// createPrinterPass - Get a module printer pass. |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 554 | Pass *createPrinterPass(raw_ostream &O, |
| 555 | const std::string &Banner) const override { |
Chandler Carruth | 9d80513 | 2014-01-12 11:30:46 +0000 | [diff] [blame] | 556 | return createPrintModulePass(O, Banner); |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 559 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 560 | /// whether any of the passes modifies the module, and if so, return true. |
| 561 | bool run(Module &M); |
| 562 | |
Pedro Artigas | e4348b0 | 2012-12-03 21:56:57 +0000 | [diff] [blame] | 563 | using llvm::Pass::doInitialization; |
| 564 | using llvm::Pass::doFinalization; |
| 565 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 566 | /// Pass Manager itself does not invalidate any analysis info. |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 567 | void getAnalysisUsage(AnalysisUsage &Info) const override { |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 568 | Info.setPreservesAll(); |
| 569 | } |
| 570 | |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 571 | PMDataManager *getAsPMDataManager() override { return this; } |
| 572 | Pass *getAsPass() override { return this; } |
| 573 | PassManagerType getTopLevelPassManagerType() override { |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 574 | return PMT_ModulePassManager; |
| 575 | } |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 576 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 577 | MPPassManager *getContainedManager(unsigned N) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 578 | assert(N < PassManagers.size() && "Pass number out of range!"); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 579 | MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]); |
| 580 | return MP; |
| 581 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 582 | }; |
| 583 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 584 | void PassManagerImpl::anchor() {} |
| 585 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 586 | char PassManagerImpl::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 587 | } // End of legacy namespace |
| 588 | } // End of llvm namespace |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 589 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 590 | //===----------------------------------------------------------------------===// |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 591 | // PMTopLevelManager implementation |
| 592 | |
Devang Patel | 4268fc0 | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 593 | /// Initialize top level manager. Create first pass manager. |
Dan Gohman | e85c619 | 2010-08-16 21:38:42 +0000 | [diff] [blame] | 594 | PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) { |
| 595 | PMDM->setTopLevelManager(this); |
| 596 | addPassManager(PMDM); |
| 597 | activeStack.push(PMDM); |
Devang Patel | 4268fc0 | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 600 | /// Set pass P as the last user of the given analysis passes. |
Dan Gohman | c8da21b | 2010-10-12 00:12:29 +0000 | [diff] [blame] | 601 | void |
Bill Wendling | ea857e1 | 2012-05-14 07:53:40 +0000 | [diff] [blame] | 602 | PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) { |
Tobias Grosser | f07426b | 2011-01-20 21:03:22 +0000 | [diff] [blame] | 603 | unsigned PDepth = 0; |
| 604 | if (P->getResolver()) |
| 605 | PDepth = P->getResolver()->getPMDataManager().getDepth(); |
| 606 | |
Yaron Keren | 4849aa3 | 2015-06-05 17:48:47 +0000 | [diff] [blame] | 607 | for (Pass *AP : AnalysisPasses) { |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 608 | LastUser[AP] = P; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 609 | |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 610 | if (P == AP) |
| 611 | continue; |
| 612 | |
Tobias Grosser | f07426b | 2011-01-20 21:03:22 +0000 | [diff] [blame] | 613 | // Update the last users of passes that are required transitive by AP. |
| 614 | AnalysisUsage *AnUsage = findAnalysisUsage(AP); |
| 615 | const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet(); |
| 616 | SmallVector<Pass *, 12> LastUses; |
| 617 | SmallVector<Pass *, 12> LastPMUses; |
Yaron Keren | 8300995 | 2016-04-28 14:49:44 +0000 | [diff] [blame] | 618 | for (AnalysisID ID : IDs) { |
| 619 | Pass *AnalysisPass = findAnalysisPass(ID); |
Tobias Grosser | f07426b | 2011-01-20 21:03:22 +0000 | [diff] [blame] | 620 | assert(AnalysisPass && "Expected analysis pass to exist."); |
| 621 | AnalysisResolver *AR = AnalysisPass->getResolver(); |
| 622 | assert(AR && "Expected analysis resolver to exist."); |
| 623 | unsigned APDepth = AR->getPMDataManager().getDepth(); |
| 624 | |
| 625 | if (PDepth == APDepth) |
| 626 | LastUses.push_back(AnalysisPass); |
| 627 | else if (PDepth > APDepth) |
| 628 | LastPMUses.push_back(AnalysisPass); |
| 629 | } |
| 630 | |
| 631 | setLastUser(LastUses, P); |
| 632 | |
| 633 | // If this pass has a corresponding pass manager, push higher level |
| 634 | // analysis to this pass manager. |
| 635 | if (P->getResolver()) |
| 636 | setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass()); |
| 637 | |
| 638 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 639 | // If AP is the last user of other passes then make P last user of |
| 640 | // such passes. |
Yaron Keren | bd87319 | 2016-10-02 19:21:41 +0000 | [diff] [blame] | 641 | for (auto LU : LastUser) { |
| 642 | if (LU.second == AP) |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 643 | // DenseMap iterator is not invalidated here because |
Tobias Grosser | f07426b | 2011-01-20 21:03:22 +0000 | [diff] [blame] | 644 | // this is just updating existing entries. |
Yaron Keren | bd87319 | 2016-10-02 19:21:41 +0000 | [diff] [blame] | 645 | LastUser[LU.first] = P; |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /// Collect passes whose last user is P |
Dan Gohman | 7224bce | 2010-10-12 00:11:18 +0000 | [diff] [blame] | 651 | void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses, |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 652 | Pass *P) { |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 653 | DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI = |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 654 | InversedLastUser.find(P); |
| 655 | if (DMI == InversedLastUser.end()) |
| 656 | return; |
| 657 | |
| 658 | SmallPtrSet<Pass *, 8> &LU = DMI->second; |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 659 | for (Pass *LUP : LU) { |
| 660 | LastUses.push_back(LUP); |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 665 | AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 666 | AnalysisUsage *AnUsage = nullptr; |
Philip Reames | e8aeaeb | 2015-12-04 20:05:04 +0000 | [diff] [blame] | 667 | auto DMI = AnUsageMap.find(P); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 668 | if (DMI != AnUsageMap.end()) |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 669 | AnUsage = DMI->second; |
| 670 | else { |
Philip Reames | e8aeaeb | 2015-12-04 20:05:04 +0000 | [diff] [blame] | 671 | // Look up the analysis usage from the pass instance (different instances |
| 672 | // of the same pass can produce different results), but unique the |
| 673 | // resulting object to reduce memory usage. This helps to greatly reduce |
| 674 | // memory usage when we have many instances of only a few pass types |
| 675 | // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set |
| 676 | // of dependencies. |
| 677 | AnalysisUsage AU; |
| 678 | P->getAnalysisUsage(AU); |
Bjorn Pettersson | aa02580 | 2018-07-03 12:39:52 +0000 | [diff] [blame] | 679 | |
Philip Reames | e8aeaeb | 2015-12-04 20:05:04 +0000 | [diff] [blame] | 680 | AUFoldingSetNode* Node = nullptr; |
| 681 | FoldingSetNodeID ID; |
| 682 | AUFoldingSetNode::Profile(ID, AU); |
| 683 | void *IP = nullptr; |
| 684 | if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP)) |
| 685 | Node = N; |
| 686 | else { |
Philip Reames | 000f77d | 2015-12-04 23:48:19 +0000 | [diff] [blame] | 687 | Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU); |
Philip Reames | e8aeaeb | 2015-12-04 20:05:04 +0000 | [diff] [blame] | 688 | UniqueAnalysisUsages.InsertNode(Node, IP); |
| 689 | } |
| 690 | assert(Node && "cached analysis usage must be non null"); |
| 691 | |
| 692 | AnUsageMap[P] = &Node->AU; |
Mandeep Singh Grang | 5e1697e | 2017-06-06 05:08:36 +0000 | [diff] [blame] | 693 | AnUsage = &Node->AU; |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 694 | } |
| 695 | return AnUsage; |
| 696 | } |
| 697 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 698 | /// Schedule pass P for execution. Make sure that passes required by |
| 699 | /// P are run before P is run. Update analysis info maintained by |
| 700 | /// the manager. Remove dead passes. This is a recursive function. |
| 701 | void PMTopLevelManager::schedulePass(Pass *P) { |
| 702 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 703 | // TODO : Allocate function manager for this pass, other wise required set |
| 704 | // may be inserted into previous function manager |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 705 | |
Devang Patel | d74ede7 | 2007-03-06 01:06:16 +0000 | [diff] [blame] | 706 | // Give pass a chance to prepare the stage. |
| 707 | P->preparePassManager(activeStack); |
| 708 | |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 709 | // If P is an analysis pass and it is available then do not |
| 710 | // generate the analysis again. Stale analysis info should not be |
| 711 | // available at this point. |
Chandler Carruth | 5b0d3e3 | 2015-01-28 09:47:21 +0000 | [diff] [blame] | 712 | const PassInfo *PI = findAnalysisPassInfo(P->getPassID()); |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 713 | if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) { |
Craig Topper | 4ee2841 | 2018-08-20 20:57:30 +0000 | [diff] [blame] | 714 | // Remove any cached AnalysisUsage information. |
| 715 | AnUsageMap.erase(P); |
Nuno Lopes | 0460bb2 | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 716 | delete P; |
Devang Patel | af75ab8 | 2008-03-19 00:48:41 +0000 | [diff] [blame] | 717 | return; |
Nuno Lopes | 0460bb2 | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 718 | } |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 719 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 720 | AnalysisUsage *AnUsage = findAnalysisUsage(P); |
| 721 | |
Devang Patel | fdee703 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 722 | bool checkAnalysis = true; |
| 723 | while (checkAnalysis) { |
| 724 | checkAnalysis = false; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 725 | |
Devang Patel | fdee703 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 726 | const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet(); |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 727 | for (const AnalysisID ID : RequiredSet) { |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 728 | |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 729 | Pass *AnalysisPass = findAnalysisPass(ID); |
Devang Patel | fdee703 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 730 | if (!AnalysisPass) { |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 731 | const PassInfo *PI = findAnalysisPassInfo(ID); |
Victor Oliveira | aa9ccee | 2012-07-18 19:59:29 +0000 | [diff] [blame] | 732 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 733 | if (!PI) { |
Victor Oliveira | aa9ccee | 2012-07-18 19:59:29 +0000 | [diff] [blame] | 734 | // Pass P is not in the global PassRegistry |
| 735 | dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n"; |
| 736 | dbgs() << "Verify if there is a pass dependency cycle." << "\n"; |
| 737 | dbgs() << "Required Passes:" << "\n"; |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 738 | for (const AnalysisID ID2 : RequiredSet) { |
| 739 | if (ID == ID2) |
| 740 | break; |
| 741 | Pass *AnalysisPass2 = findAnalysisPass(ID2); |
Victor Oliveira | aa9ccee | 2012-07-18 19:59:29 +0000 | [diff] [blame] | 742 | if (AnalysisPass2) { |
| 743 | dbgs() << "\t" << AnalysisPass2->getPassName() << "\n"; |
Craig Topper | 821d6af | 2013-02-06 06:50:38 +0000 | [diff] [blame] | 744 | } else { |
Victor Oliveira | aa9ccee | 2012-07-18 19:59:29 +0000 | [diff] [blame] | 745 | dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n"; |
| 746 | dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n"; |
| 747 | dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n"; |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
Andrew Trick | 6bbaf13 | 2011-06-03 00:48:58 +0000 | [diff] [blame] | 752 | assert(PI && "Expected required passes to be initialized"); |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 753 | AnalysisPass = PI->createPass(); |
Devang Patel | fdee703 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 754 | if (P->getPotentialPassManagerType () == |
| 755 | AnalysisPass->getPotentialPassManagerType()) |
| 756 | // Schedule analysis pass that is managed by the same pass manager. |
| 757 | schedulePass(AnalysisPass); |
| 758 | else if (P->getPotentialPassManagerType () > |
| 759 | AnalysisPass->getPotentialPassManagerType()) { |
| 760 | // Schedule analysis pass that is managed by a new manager. |
| 761 | schedulePass(AnalysisPass); |
Dan Gohman | 6304db3 | 2010-08-16 22:57:28 +0000 | [diff] [blame] | 762 | // Recheck analysis passes to ensure that required analyses that |
Devang Patel | fdee703 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 763 | // are already checked are still available. |
| 764 | checkAnalysis = true; |
Craig Topper | 821d6af | 2013-02-06 06:50:38 +0000 | [diff] [blame] | 765 | } else |
Chad Rosier | c83fa9e | 2015-03-20 15:45:14 +0000 | [diff] [blame] | 766 | // Do not schedule this analysis. Lower level analysis |
Devang Patel | fdee703 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 767 | // passes are run on the fly. |
| 768 | delete AnalysisPass; |
| 769 | } |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
| 773 | // Now all required passes are available. |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 774 | if (ImmutablePass *IP = P->getAsImmutablePass()) { |
| 775 | // P is a immutable pass and it will be managed by this |
| 776 | // top level manager. Set up analysis resolver to connect them. |
| 777 | PMDataManager *DM = getAsPMDataManager(); |
| 778 | AnalysisResolver *AR = new AnalysisResolver(*DM); |
| 779 | P->setResolver(AR); |
| 780 | DM->initializeAnalysisImpl(P); |
| 781 | addImmutablePass(IP); |
| 782 | DM->recordAvailableAnalysis(IP); |
| 783 | return; |
| 784 | } |
| 785 | |
Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 786 | if (PI && !PI->isAnalysis() && shouldPrintBeforePass(PI->getPassArgument())) { |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 787 | Pass *PP = P->createPrinterPass( |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 788 | dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str()); |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 789 | PP->assignPassManager(activeStack, getTopLevelPassManagerType()); |
| 790 | } |
| 791 | |
| 792 | // Add the requested pass to the best available pass manager. |
| 793 | P->assignPassManager(activeStack, getTopLevelPassManagerType()); |
| 794 | |
Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 795 | if (PI && !PI->isAnalysis() && shouldPrintAfterPass(PI->getPassArgument())) { |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 796 | Pass *PP = P->createPrinterPass( |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 797 | dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str()); |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 798 | PP->assignPassManager(activeStack, getTopLevelPassManagerType()); |
| 799 | } |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | /// Find the pass that implements Analysis AID. Search immutable |
| 803 | /// passes and all pass managers. If desired pass is not found |
| 804 | /// then return NULL. |
| 805 | Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { |
Chandler Carruth | b1e3a9a | 2015-09-10 02:31:42 +0000 | [diff] [blame] | 806 | // For immutable passes we have a direct mapping from ID to pass, so check |
| 807 | // that first. |
| 808 | if (Pass *P = ImmutablePassMap.lookup(AID)) |
| 809 | return P; |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 810 | |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 811 | // Check pass managers |
Yaron Keren | 4849aa3 | 2015-06-05 17:48:47 +0000 | [diff] [blame] | 812 | for (PMDataManager *PassManager : PassManagers) |
| 813 | if (Pass *P = PassManager->findAnalysisPass(AID, false)) |
Dan Gohman | 844dd0a | 2010-10-11 23:19:01 +0000 | [diff] [blame] | 814 | return P; |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 815 | |
| 816 | // Check other pass managers |
Yaron Keren | 4849aa3 | 2015-06-05 17:48:47 +0000 | [diff] [blame] | 817 | for (PMDataManager *IndirectPassManager : IndirectPassManagers) |
| 818 | if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false)) |
Dan Gohman | 844dd0a | 2010-10-11 23:19:01 +0000 | [diff] [blame] | 819 | return P; |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 820 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 821 | return nullptr; |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Chandler Carruth | 5b0d3e3 | 2015-01-28 09:47:21 +0000 | [diff] [blame] | 824 | const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const { |
| 825 | const PassInfo *&PI = AnalysisPassInfos[AID]; |
| 826 | if (!PI) |
| 827 | PI = PassRegistry::getPassRegistry()->getPassInfo(AID); |
| 828 | else |
| 829 | assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) && |
| 830 | "The pass info pointer changed for an analysis ID!"); |
| 831 | |
| 832 | return PI; |
| 833 | } |
| 834 | |
Chandler Carruth | b1e3a9a | 2015-09-10 02:31:42 +0000 | [diff] [blame] | 835 | void PMTopLevelManager::addImmutablePass(ImmutablePass *P) { |
| 836 | P->initializePass(); |
| 837 | ImmutablePasses.push_back(P); |
| 838 | |
| 839 | // Add this pass to the map from its analysis ID. We clobber any prior runs |
| 840 | // of the pass in the map so that the last one added is the one found when |
| 841 | // doing lookups. |
| 842 | AnalysisID AID = P->getPassID(); |
| 843 | ImmutablePassMap[AID] = P; |
| 844 | |
| 845 | // Also add any interfaces implemented by the immutable pass to the map for |
| 846 | // fast lookup. |
| 847 | const PassInfo *PassInf = findAnalysisPassInfo(AID); |
| 848 | assert(PassInf && "Expected all immutable passes to be initialized"); |
Chandler Carruth | 8727518 | 2015-09-10 04:22:36 +0000 | [diff] [blame] | 849 | for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented()) |
Chandler Carruth | b1e3a9a | 2015-09-10 02:31:42 +0000 | [diff] [blame] | 850 | ImmutablePassMap[ImmPI->getTypeInfo()] = P; |
| 851 | } |
| 852 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 853 | // Print passes managed by this top level manager. |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 854 | void PMTopLevelManager::dumpPasses() const { |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 855 | |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 856 | if (PassDebugging < Structure) |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 857 | return; |
| 858 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 859 | // Print out the immutable passes |
| 860 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { |
Dan Gohman | f71c521 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 861 | ImmutablePasses[i]->dumpPassStructure(0); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 862 | } |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 863 | |
Dan Gohman | f71c521 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 864 | // Every class that derives from PMDataManager also derives from Pass |
| 865 | // (sometimes indirectly), but there's no inheritance relationship |
| 866 | // between PMDataManager and Pass, so we have to getAsPass to get |
| 867 | // from a PMDataManager* to a Pass*. |
Yaron Keren | 4849aa3 | 2015-06-05 17:48:47 +0000 | [diff] [blame] | 868 | for (PMDataManager *Manager : PassManagers) |
| 869 | Manager->getAsPass()->dumpPassStructure(1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 872 | void PMTopLevelManager::dumpArguments() const { |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 873 | |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 874 | if (PassDebugging < Arguments) |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 875 | return; |
| 876 | |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 877 | dbgs() << "Pass Arguments: "; |
Yaron Keren | 8300995 | 2016-04-28 14:49:44 +0000 | [diff] [blame] | 878 | for (ImmutablePass *P : ImmutablePasses) |
| 879 | if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) { |
Andrew Trick | 6bbaf13 | 2011-06-03 00:48:58 +0000 | [diff] [blame] | 880 | assert(PI && "Expected all immutable passes to be initialized"); |
Dan Gohman | f51d06bb | 2010-11-11 16:32:17 +0000 | [diff] [blame] | 881 | if (!PI->isAnalysisGroup()) |
| 882 | dbgs() << " -" << PI->getPassArgument(); |
Andrew Trick | 6bbaf13 | 2011-06-03 00:48:58 +0000 | [diff] [blame] | 883 | } |
Yaron Keren | 8300995 | 2016-04-28 14:49:44 +0000 | [diff] [blame] | 884 | for (PMDataManager *PM : PassManagers) |
| 885 | PM->dumpPassArguments(); |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 886 | dbgs() << "\n"; |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 889 | void PMTopLevelManager::initializeAllAnalysisInfo() { |
Yaron Keren | 8300995 | 2016-04-28 14:49:44 +0000 | [diff] [blame] | 890 | for (PMDataManager *PM : PassManagers) |
| 891 | PM->initializeAnalysisInfo(); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 892 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 893 | // Initailize other pass managers |
Yaron Keren | 8300995 | 2016-04-28 14:49:44 +0000 | [diff] [blame] | 894 | for (PMDataManager *IPM : IndirectPassManagers) |
| 895 | IPM->initializeAnalysisInfo(); |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 896 | |
Yaron Keren | bd87319 | 2016-10-02 19:21:41 +0000 | [diff] [blame] | 897 | for (auto LU : LastUser) { |
| 898 | SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second]; |
| 899 | L.insert(LU.first); |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 900 | } |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 903 | /// Destructor |
| 904 | PMTopLevelManager::~PMTopLevelManager() { |
Yaron Keren | 8300995 | 2016-04-28 14:49:44 +0000 | [diff] [blame] | 905 | for (PMDataManager *PM : PassManagers) |
| 906 | delete PM; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 907 | |
Yaron Keren | 8300995 | 2016-04-28 14:49:44 +0000 | [diff] [blame] | 908 | for (ImmutablePass *P : ImmutablePasses) |
| 909 | delete P; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 912 | //===----------------------------------------------------------------------===// |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 913 | // PMDataManager implementation |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 914 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 915 | /// Augement AvailableAnalysis by adding analysis made available by pass P. |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 916 | void PMDataManager::recordAvailableAnalysis(Pass *P) { |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 917 | AnalysisID PI = P->getPassID(); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 918 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 919 | AvailableAnalysis[PI] = P; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 920 | |
Dan Gohman | b83d1b6 | 2010-08-12 23:46:28 +0000 | [diff] [blame] | 921 | assert(!AvailableAnalysis.empty()); |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 922 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 923 | // This pass is the current implementation of all of the interfaces it |
| 924 | // implements as well. |
Chandler Carruth | 5b0d3e3 | 2015-01-28 09:47:21 +0000 | [diff] [blame] | 925 | const PassInfo *PInf = TPM->findAnalysisPassInfo(PI); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 926 | if (!PInf) return; |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 927 | const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented(); |
Owen Anderson | 3183ef1 | 2010-07-20 16:55:05 +0000 | [diff] [blame] | 928 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 929 | AvailableAnalysis[II[i]->getTypeInfo()] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 932 | // Return true if P preserves high level analysis used by other |
| 933 | // passes managed by this manager |
| 934 | bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 935 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 936 | if (AnUsage->getPreservesAll()) |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 937 | return true; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 938 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 939 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Yaron Keren | bd87319 | 2016-10-02 19:21:41 +0000 | [diff] [blame] | 940 | for (Pass *P1 : HigherLevelAnalysis) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 941 | if (P1->getAsImmutablePass() == nullptr && |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 942 | !is_contained(PreservedSet, P1->getPassID())) |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 943 | return false; |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 944 | } |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 945 | |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 946 | return true; |
| 947 | } |
| 948 | |
Chris Lattner | 02eb94c | 2008-08-07 07:34:50 +0000 | [diff] [blame] | 949 | /// verifyPreservedAnalysis -- Verify analysis preserved by pass P. |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 950 | void PMDataManager::verifyPreservedAnalysis(Pass *P) { |
Chris Lattner | 02eb94c | 2008-08-07 07:34:50 +0000 | [diff] [blame] | 951 | // Don't do this unless assertions are enabled. |
| 952 | #ifdef NDEBUG |
| 953 | return; |
| 954 | #endif |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 955 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 956 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 957 | |
Devang Patel | ef43253 | 2007-07-19 05:36:09 +0000 | [diff] [blame] | 958 | // Verify preserved analysis |
Yaron Keren | bd87319 | 2016-10-02 19:21:41 +0000 | [diff] [blame] | 959 | for (AnalysisID AID : PreservedSet) { |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 960 | if (Pass *AP = findAnalysisPass(AID, true)) { |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 961 | TimeRegion PassTimer(getPassTimer(AP)); |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 962 | AP->verifyAnalysis(); |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 963 | } |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
Devang Patel | 67c79a4 | 2008-07-01 19:50:56 +0000 | [diff] [blame] | 967 | /// Remove Analysis not preserved by Pass P |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 968 | void PMDataManager::removeNotPreservedAnalysis(Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 969 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 970 | if (AnUsage->getPreservesAll()) |
Devang Patel | 2e169c3 | 2006-12-07 20:03:49 +0000 | [diff] [blame] | 971 | return; |
| 972 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 973 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Michael Ilseman | c33b6ac | 2013-02-26 01:31:59 +0000 | [diff] [blame] | 974 | for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), |
Devang Patel | be6bd55e | 2006-12-12 23:07:44 +0000 | [diff] [blame] | 975 | E = AvailableAnalysis.end(); I != E; ) { |
Michael Ilseman | c33b6ac | 2013-02-26 01:31:59 +0000 | [diff] [blame] | 976 | DenseMap<AnalysisID, Pass*>::iterator Info = I++; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 977 | if (Info->second->getAsImmutablePass() == nullptr && |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 978 | !is_contained(PreservedSet, Info->first)) { |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 979 | // Remove this analysis |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 980 | if (PassDebugging >= Details) { |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 981 | Pass *S = Info->second; |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 982 | dbgs() << " -- '" << P->getPassName() << "' is not preserving '"; |
| 983 | dbgs() << S->getPassName() << "'\n"; |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 984 | } |
Dan Gohman | 193e4c0 | 2008-11-06 21:57:17 +0000 | [diff] [blame] | 985 | AvailableAnalysis.erase(Info); |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 986 | } |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 987 | } |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 988 | |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 989 | // Check inherited analysis also. If P is not preserving analysis |
| 990 | // provided by parent manager then remove it here. |
| 991 | for (unsigned Index = 0; Index < PMT_Last; ++Index) { |
| 992 | |
| 993 | if (!InheritedAnalysis[Index]) |
| 994 | continue; |
| 995 | |
Michael Ilseman | c33b6ac | 2013-02-26 01:31:59 +0000 | [diff] [blame] | 996 | for (DenseMap<AnalysisID, Pass*>::iterator |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 997 | I = InheritedAnalysis[Index]->begin(), |
| 998 | E = InheritedAnalysis[Index]->end(); I != E; ) { |
Michael Ilseman | c33b6ac | 2013-02-26 01:31:59 +0000 | [diff] [blame] | 999 | DenseMap<AnalysisID, Pass *>::iterator Info = I++; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1000 | if (Info->second->getAsImmutablePass() == nullptr && |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 1001 | !is_contained(PreservedSet, Info->first)) { |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 1002 | // Remove this analysis |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 1003 | if (PassDebugging >= Details) { |
Andreas Neustifter | 4665141 | 2009-12-04 06:58:24 +0000 | [diff] [blame] | 1004 | Pass *S = Info->second; |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1005 | dbgs() << " -- '" << P->getPassName() << "' is not preserving '"; |
| 1006 | dbgs() << S->getPassName() << "'\n"; |
Andreas Neustifter | 4665141 | 2009-12-04 06:58:24 +0000 | [diff] [blame] | 1007 | } |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 1008 | InheritedAnalysis[Index]->erase(Info); |
Andreas Neustifter | 4665141 | 2009-12-04 06:58:24 +0000 | [diff] [blame] | 1009 | } |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 1010 | } |
| 1011 | } |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 1014 | /// Remove analysis passes that are not used any longer |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 1015 | void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg, |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1016 | enum PassDebuggingString DBG_STR) { |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 1017 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 1018 | SmallVector<Pass *, 12> DeadPasses; |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1019 | |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 1020 | // If this is a on the fly manager then it does not have TPM. |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1021 | if (!TPM) |
| 1022 | return; |
| 1023 | |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 1024 | TPM->collectLastUses(DeadPasses, P); |
| 1025 | |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 1026 | if (PassDebugging >= Details && !DeadPasses.empty()) { |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1027 | dbgs() << " -*- '" << P->getPassName(); |
| 1028 | dbgs() << "' is the last user of following pass instances."; |
| 1029 | dbgs() << " Free these instances\n"; |
Evan Cheng | 93af6ce | 2008-06-04 09:13:31 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Yaron Keren | bd87319 | 2016-10-02 19:21:41 +0000 | [diff] [blame] | 1032 | for (Pass *P : DeadPasses) |
| 1033 | freePass(P, Msg, DBG_STR); |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 1034 | } |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 1035 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 1036 | void PMDataManager::freePass(Pass *P, StringRef Msg, |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 1037 | enum PassDebuggingString DBG_STR) { |
| 1038 | dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg); |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 1039 | |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 1040 | { |
| 1041 | // If the pass crashes releasing memory, remember this. |
| 1042 | PassManagerPrettyStackEntry X(P); |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1043 | TimeRegion PassTimer(getPassTimer(P)); |
| 1044 | |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 1045 | P->releaseMemory(); |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1048 | AnalysisID PI = P->getPassID(); |
Chandler Carruth | 5b0d3e3 | 2015-01-28 09:47:21 +0000 | [diff] [blame] | 1049 | if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) { |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 1050 | // Remove the pass itself (if it is not already removed). |
| 1051 | AvailableAnalysis.erase(PI); |
| 1052 | |
| 1053 | // Remove all interfaces this pass implements, for which it is also |
| 1054 | // listed as the available implementation. |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1055 | const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented(); |
Owen Anderson | 3183ef1 | 2010-07-20 16:55:05 +0000 | [diff] [blame] | 1056 | for (unsigned i = 0, e = II.size(); i != e; ++i) { |
Michael Ilseman | c33b6ac | 2013-02-26 01:31:59 +0000 | [diff] [blame] | 1057 | DenseMap<AnalysisID, Pass*>::iterator Pos = |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1058 | AvailableAnalysis.find(II[i]->getTypeInfo()); |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 1059 | if (Pos != AvailableAnalysis.end() && Pos->second == P) |
Devang Patel | c3e3ca9 | 2008-10-06 20:36:36 +0000 | [diff] [blame] | 1060 | AvailableAnalysis.erase(Pos); |
Devang Patel | c3e3ca9 | 2008-10-06 20:36:36 +0000 | [diff] [blame] | 1061 | } |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 1062 | } |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1065 | /// Add pass P into the PassVector. Update |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 1066 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1067 | void PMDataManager::add(Pass *P, bool ProcessAnalysis) { |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 1068 | // This manager is going to manage pass P. Set up analysis resolver |
| 1069 | // to connect them. |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 1070 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 1071 | P->setResolver(AR); |
| 1072 | |
Devang Patel | ec2b9a7 | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 1073 | // If a FunctionPass F is the last user of ModulePass info M |
| 1074 | // then the F's manager, not F, records itself as a last user of M. |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 1075 | SmallVector<Pass *, 12> TransferLastUses; |
Devang Patel | ec2b9a7 | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 1076 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1077 | if (!ProcessAnalysis) { |
| 1078 | // Add pass |
| 1079 | PassVector.push_back(P); |
| 1080 | return; |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 1081 | } |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 1082 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1083 | // At the moment, this pass is the last user of all required passes. |
| 1084 | SmallVector<Pass *, 12> LastUses; |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1085 | SmallVector<Pass *, 8> UsedPasses; |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1086 | SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable; |
| 1087 | |
| 1088 | unsigned PDepth = this->getDepth(); |
| 1089 | |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1090 | collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P); |
| 1091 | for (Pass *PUsed : UsedPasses) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1092 | unsigned RDepth = 0; |
| 1093 | |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1094 | assert(PUsed->getResolver() && "Analysis Resolver is not set"); |
| 1095 | PMDataManager &DM = PUsed->getResolver()->getPMDataManager(); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1096 | RDepth = DM.getDepth(); |
| 1097 | |
| 1098 | if (PDepth == RDepth) |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1099 | LastUses.push_back(PUsed); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1100 | else if (PDepth > RDepth) { |
| 1101 | // Let the parent claim responsibility of last use |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1102 | TransferLastUses.push_back(PUsed); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1103 | // Keep track of higher level analysis used by this manager. |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1104 | HigherLevelAnalysis.push_back(PUsed); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1105 | } else |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1106 | llvm_unreachable("Unable to accommodate Used Pass"); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | // Set P as P's last user until someone starts using P. |
| 1110 | // However, if P is a Pass Manager then it does not need |
| 1111 | // to record its last user. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1112 | if (!P->getAsPMDataManager()) |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1113 | LastUses.push_back(P); |
| 1114 | TPM->setLastUser(LastUses, P); |
| 1115 | |
| 1116 | if (!TransferLastUses.empty()) { |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 1117 | Pass *My_PM = getAsPass(); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1118 | TPM->setLastUser(TransferLastUses, My_PM); |
| 1119 | TransferLastUses.clear(); |
| 1120 | } |
| 1121 | |
Dan Gohman | 6304db3 | 2010-08-16 22:57:28 +0000 | [diff] [blame] | 1122 | // Now, take care of required analyses that are not available. |
Chandler Carruth | 2f02ea4 | 2015-08-18 18:41:53 +0000 | [diff] [blame] | 1123 | for (AnalysisID ID : ReqAnalysisNotAvailable) { |
| 1124 | const PassInfo *PI = TPM->findAnalysisPassInfo(ID); |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1125 | Pass *AnalysisPass = PI->createPass(); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1126 | this->addLowerLevelRequiredPass(P, AnalysisPass); |
| 1127 | } |
| 1128 | |
| 1129 | // Take a note of analysis required and made available by this pass. |
| 1130 | // Remove the analysis not preserved by this pass |
| 1131 | removeNotPreservedAnalysis(P); |
| 1132 | recordAvailableAnalysis(P); |
| 1133 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 1134 | // Add pass |
| 1135 | PassVector.push_back(P); |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1138 | |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1139 | /// Populate UP with analysis pass that are used or required by |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1140 | /// pass P and are available. Populate RP_NotAvail with analysis |
| 1141 | /// pass that are required by pass P but are not available. |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1142 | void PMDataManager::collectRequiredAndUsedAnalyses( |
| 1143 | SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail, |
| 1144 | Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 1145 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1146 | |
| 1147 | for (const auto &UsedID : AnUsage->getUsedSet()) |
| 1148 | if (Pass *AnalysisPass = findAnalysisPass(UsedID, true)) |
| 1149 | UP.push_back(AnalysisPass); |
| 1150 | |
Chandler Carruth | 2f02ea4 | 2015-08-18 18:41:53 +0000 | [diff] [blame] | 1151 | for (const auto &RequiredID : AnUsage->getRequiredSet()) |
| 1152 | if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true)) |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1153 | UP.push_back(AnalysisPass); |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1154 | else |
Chandler Carruth | 2f02ea4 | 2015-08-18 18:41:53 +0000 | [diff] [blame] | 1155 | RP_NotAvail.push_back(RequiredID); |
Devang Patel | f58183d | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 1156 | |
Chandler Carruth | 2f02ea4 | 2015-08-18 18:41:53 +0000 | [diff] [blame] | 1157 | for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet()) |
| 1158 | if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true)) |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1159 | UP.push_back(AnalysisPass); |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1160 | else |
Chandler Carruth | 2f02ea4 | 2015-08-18 18:41:53 +0000 | [diff] [blame] | 1161 | RP_NotAvail.push_back(RequiredID); |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 1164 | // All Required analyses should be available to the pass as it runs! Here |
| 1165 | // we fill in the AnalysisImpls member of the pass so that it can |
| 1166 | // successfully use the getAnalysis() method to retrieve the |
| 1167 | // implementations it needs. |
| 1168 | // |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 1169 | void PMDataManager::initializeAnalysisImpl(Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 1170 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 1171 | |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1172 | for (const AnalysisID ID : AnUsage->getRequiredSet()) { |
| 1173 | Pass *Impl = findAnalysisPass(ID, true); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1174 | if (!Impl) |
Devang Patel | 56a5c62 | 2007-04-16 20:44:16 +0000 | [diff] [blame] | 1175 | // This may be analysis pass that is initialized on the fly. |
| 1176 | // If that is not the case then it will raise an assert when it is used. |
| 1177 | continue; |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 1178 | AnalysisResolver *AR = P->getResolver(); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1179 | assert(AR && "Analysis Resolver is not set"); |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1180 | AR->addAnalysisImplsPair(ID, Impl); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 1181 | } |
| 1182 | } |
| 1183 | |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 1184 | /// Find the pass that implements Analysis AID. If desired pass is not found |
| 1185 | /// then return NULL. |
| 1186 | Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) { |
| 1187 | |
| 1188 | // Check if AvailableAnalysis map has one entry. |
Michael Ilseman | c33b6ac | 2013-02-26 01:31:59 +0000 | [diff] [blame] | 1189 | DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID); |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 1190 | |
| 1191 | if (I != AvailableAnalysis.end()) |
| 1192 | return I->second; |
| 1193 | |
| 1194 | // Search Parents through TopLevelManager |
| 1195 | if (SearchParent) |
| 1196 | return TPM->findAnalysisPass(AID); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1197 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1198 | return nullptr; |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1201 | // Print list of passes that are last used by P. |
| 1202 | void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{ |
| 1203 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 1204 | SmallVector<Pass *, 12> LUses; |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 1205 | |
| 1206 | // If this is a on the fly manager then it does not have TPM. |
| 1207 | if (!TPM) |
| 1208 | return; |
| 1209 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1210 | TPM->collectLastUses(LUses, P); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1211 | |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1212 | for (Pass *P : LUses) { |
Eric Christopher | a13839f | 2014-02-26 23:27:16 +0000 | [diff] [blame] | 1213 | dbgs() << "--" << std::string(Offset*2, ' '); |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1214 | P->dumpPassStructure(0); |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | void PMDataManager::dumpPassArguments() const { |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1219 | for (Pass *P : PassVector) { |
| 1220 | if (PMDataManager *PMD = P->getAsPMDataManager()) |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1221 | PMD->dumpPassArguments(); |
| 1222 | else |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1223 | if (const PassInfo *PI = |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1224 | TPM->findAnalysisPassInfo(P->getPassID())) |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1225 | if (!PI->isAnalysisGroup()) |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1226 | dbgs() << " -" << PI->getPassArgument(); |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1227 | } |
| 1228 | } |
| 1229 | |
Chris Lattner | dd6304f | 2007-08-10 06:17:04 +0000 | [diff] [blame] | 1230 | void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1, |
| 1231 | enum PassDebuggingString S2, |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 1232 | StringRef Msg) { |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 1233 | if (PassDebugging < Executions) |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1234 | return; |
Pavel Labath | ec534e6 | 2016-10-25 16:20:07 +0000 | [diff] [blame] | 1235 | dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this |
Chandler Carruth | 20c5693 | 2014-04-27 23:59:25 +0000 | [diff] [blame] | 1236 | << std::string(getDepth() * 2 + 1, ' '); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1237 | switch (S1) { |
| 1238 | case EXECUTION_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1239 | dbgs() << "Executing Pass '" << P->getPassName(); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1240 | break; |
| 1241 | case MODIFICATION_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1242 | dbgs() << "Made Modification '" << P->getPassName(); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1243 | break; |
| 1244 | case FREEING_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1245 | dbgs() << " Freeing Pass '" << P->getPassName(); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1246 | break; |
| 1247 | default: |
| 1248 | break; |
| 1249 | } |
| 1250 | switch (S2) { |
| 1251 | case ON_BASICBLOCK_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1252 | dbgs() << "' on BasicBlock '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1253 | break; |
| 1254 | case ON_FUNCTION_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1255 | dbgs() << "' on Function '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1256 | break; |
| 1257 | case ON_MODULE_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1258 | dbgs() << "' on Module '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1259 | break; |
Tobias Grosser | 23c8341 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 1260 | case ON_REGION_MSG: |
| 1261 | dbgs() << "' on Region '" << Msg << "'...\n"; |
| 1262 | break; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1263 | case ON_LOOP_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1264 | dbgs() << "' on Loop '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1265 | break; |
| 1266 | case ON_CG_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1267 | dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1268 | break; |
| 1269 | default: |
| 1270 | break; |
| 1271 | } |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1274 | void PMDataManager::dumpRequiredSet(const Pass *P) const { |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 1275 | if (PassDebugging < Details) |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1276 | return; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1277 | |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1278 | AnalysisUsage analysisUsage; |
| 1279 | P->getAnalysisUsage(analysisUsage); |
| 1280 | dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet()); |
| 1281 | } |
| 1282 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1283 | void PMDataManager::dumpPreservedSet(const Pass *P) const { |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 1284 | if (PassDebugging < Details) |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1285 | return; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1286 | |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1287 | AnalysisUsage analysisUsage; |
| 1288 | P->getAnalysisUsage(analysisUsage); |
| 1289 | dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet()); |
| 1290 | } |
| 1291 | |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1292 | void PMDataManager::dumpUsedSet(const Pass *P) const { |
| 1293 | if (PassDebugging < Details) |
| 1294 | return; |
| 1295 | |
| 1296 | AnalysisUsage analysisUsage; |
| 1297 | P->getAnalysisUsage(analysisUsage); |
| 1298 | dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet()); |
| 1299 | } |
| 1300 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 1301 | void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P, |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1302 | const AnalysisUsage::VectorType &Set) const { |
Andrew Trick | b5e1e6c | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 1303 | assert(PassDebugging >= Details); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1304 | if (Set.empty()) |
| 1305 | return; |
Roman Divacky | ad06cee | 2012-09-05 22:26:57 +0000 | [diff] [blame] | 1306 | dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1307 | for (unsigned i = 0; i != Set.size(); ++i) { |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1308 | if (i) dbgs() << ','; |
Chandler Carruth | 5b0d3e3 | 2015-01-28 09:47:21 +0000 | [diff] [blame] | 1309 | const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]); |
Andrew Trick | 6bbaf13 | 2011-06-03 00:48:58 +0000 | [diff] [blame] | 1310 | if (!PInf) { |
| 1311 | // Some preserved passes, such as AliasAnalysis, may not be initialized by |
| 1312 | // all drivers. |
| 1313 | dbgs() << " Uninitialized Pass"; |
| 1314 | continue; |
| 1315 | } |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1316 | dbgs() << ' ' << PInf->getPassName(); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1317 | } |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1318 | dbgs() << '\n'; |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1319 | } |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1320 | |
Devang Patel | 004937b | 2007-07-27 20:06:09 +0000 | [diff] [blame] | 1321 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 1322 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 1323 | /// through getAnalysis interface. |
| 1324 | /// This should be handled by specific pass manager. |
| 1325 | void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { |
| 1326 | if (TPM) { |
| 1327 | TPM->dumpArguments(); |
| 1328 | TPM->dumpPasses(); |
| 1329 | } |
Devang Patel | 8df7cc1 | 2008-02-02 01:43:30 +0000 | [diff] [blame] | 1330 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1331 | // Module Level pass may required Function Level analysis info |
| 1332 | // (e.g. dominator info). Pass manager uses on the fly function pass manager |
| 1333 | // to provide this on demand. In that case, in Pass manager terminology, |
Devang Patel | 8df7cc1 | 2008-02-02 01:43:30 +0000 | [diff] [blame] | 1334 | // module level pass is requiring lower level analysis info managed by |
| 1335 | // lower level pass manager. |
| 1336 | |
| 1337 | // When Pass manager is not able to order required analysis info, Pass manager |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1338 | // checks whether any lower level manager will be able to provide this |
Devang Patel | 8df7cc1 | 2008-02-02 01:43:30 +0000 | [diff] [blame] | 1339 | // analysis info on demand or not. |
Devang Patel | ab85d6b | 2008-06-03 01:20:02 +0000 | [diff] [blame] | 1340 | #ifndef NDEBUG |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1341 | dbgs() << "Unable to schedule '" << RequiredPass->getPassName(); |
| 1342 | dbgs() << "' required by '" << P->getPassName() << "'\n"; |
Devang Patel | ab85d6b | 2008-06-03 01:20:02 +0000 | [diff] [blame] | 1343 | #endif |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1344 | llvm_unreachable("Unable to schedule pass"); |
Devang Patel | 004937b | 2007-07-27 20:06:09 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1347 | Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 1348 | llvm_unreachable("Unable to find on the fly pass"); |
Dan Gohman | ffdee30 | 2010-06-21 18:46:45 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1351 | // Destructor |
| 1352 | PMDataManager::~PMDataManager() { |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1353 | for (Pass *P : PassVector) |
| 1354 | delete P; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1357 | //===----------------------------------------------------------------------===// |
| 1358 | // NOTE: Is this the right place to define this method ? |
Duncan Sands | 5a913d6 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 1359 | // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist. |
| 1360 | Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const { |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1361 | return PM.findAnalysisPass(ID, dir); |
| 1362 | } |
| 1363 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1364 | Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI, |
Devang Patel | 9294281 | 2007-04-16 20:56:24 +0000 | [diff] [blame] | 1365 | Function &F) { |
| 1366 | return PM.getOnTheFlyPass(P, AnalysisPI, F); |
| 1367 | } |
| 1368 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1369 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1370 | // BBPassManager implementation |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1371 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1372 | /// Execute all of the passes scheduled for execution by invoking |
| 1373 | /// runOnBasicBlock method. Keep track of whether any of the passes modifies |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1374 | /// the function, and if so, return true. |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1375 | bool BBPassManager::runOnFunction(Function &F) { |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1376 | if (F.isDeclaration()) |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1377 | return false; |
| 1378 | |
Devang Patel | e958559 | 2006-12-08 01:38:28 +0000 | [diff] [blame] | 1379 | bool Changed = doInitialization(F); |
Jessica Paquette | e49374d | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 1380 | Module &M = *F.getParent(); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 1381 | |
Jessica Paquette | 9eda13e | 2018-08-31 20:20:53 +0000 | [diff] [blame] | 1382 | unsigned InstrCount, BBSize = 0; |
Jessica Paquette | a0aa5b3 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1383 | StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount; |
Xin Tong | 023e25a | 2018-07-22 05:27:41 +0000 | [diff] [blame] | 1384 | bool EmitICRemark = M.shouldEmitInstrCountChangedRemark(); |
Jessica Paquette | 9eda13e | 2018-08-31 20:20:53 +0000 | [diff] [blame] | 1385 | if (EmitICRemark) |
Jessica Paquette | a0aa5b3 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1386 | InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount); |
Jessica Paquette | 9eda13e | 2018-08-31 20:20:53 +0000 | [diff] [blame] | 1387 | |
| 1388 | for (BasicBlock &BB : F) { |
| 1389 | // Collect the initial size of the basic block. |
| 1390 | if (EmitICRemark) |
| 1391 | BBSize = BB.size(); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1392 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1393 | BasicBlockPass *BP = getContainedPass(Index); |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1394 | bool LocalChanged = false; |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1395 | |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1396 | dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, BB.getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1397 | dumpRequiredSet(BP); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1398 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1399 | initializeAnalysisImpl(BP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1400 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1401 | { |
| 1402 | // If the pass crashes, remember this. |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1403 | PassManagerPrettyStackEntry X(BP, BB); |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1404 | TimeRegion PassTimer(getPassTimer(BP)); |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1405 | LocalChanged |= BP->runOnBasicBlock(BB); |
Jessica Paquette | 9eda13e | 2018-08-31 20:20:53 +0000 | [diff] [blame] | 1406 | if (EmitICRemark) { |
| 1407 | unsigned NewSize = BB.size(); |
| 1408 | // Update the size of the basic block, emit a remark, and update the |
| 1409 | // size of the module. |
| 1410 | if (NewSize != BBSize) { |
Jessica Paquette | 9eda13e | 2018-08-31 20:20:53 +0000 | [diff] [blame] | 1411 | int64_t Delta = |
| 1412 | static_cast<int64_t>(NewSize) - static_cast<int64_t>(BBSize); |
Jessica Paquette | a0aa5b3 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1413 | emitInstrCountChangedRemark(BP, M, Delta, InstrCount, |
| 1414 | FunctionToInstrCount, &F); |
Jessica Paquette | 9eda13e | 2018-08-31 20:20:53 +0000 | [diff] [blame] | 1415 | InstrCount = static_cast<int64_t>(InstrCount) + Delta; |
| 1416 | BBSize = NewSize; |
| 1417 | } |
| 1418 | } |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1419 | } |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1420 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1421 | Changed |= LocalChanged; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1422 | if (LocalChanged) |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 1423 | dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1424 | BB.getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1425 | dumpPreservedSet(BP); |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1426 | dumpUsedSet(BP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1427 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1428 | verifyPreservedAnalysis(BP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1429 | removeNotPreservedAnalysis(BP); |
| 1430 | recordAvailableAnalysis(BP); |
Florian Hahn | 03c3a1a | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1431 | removeDeadPasses(BP, BB.getName(), ON_BASICBLOCK_MSG); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1432 | } |
Jessica Paquette | 9eda13e | 2018-08-31 20:20:53 +0000 | [diff] [blame] | 1433 | } |
Chris Lattner | de2aa65 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 1434 | |
Bill Wendling | 6ce6d26 | 2009-12-25 13:50:18 +0000 | [diff] [blame] | 1435 | return doFinalization(F) || Changed; |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1438 | // Implement doInitialization and doFinalization |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1439 | bool BBPassManager::doInitialization(Module &M) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1440 | bool Changed = false; |
| 1441 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1442 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1443 | Changed |= getContainedPass(Index)->doInitialization(M); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1444 | |
| 1445 | return Changed; |
| 1446 | } |
| 1447 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1448 | bool BBPassManager::doFinalization(Module &M) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1449 | bool Changed = false; |
| 1450 | |
Pedro Artigas | 41b9884 | 2012-12-05 17:12:22 +0000 | [diff] [blame] | 1451 | for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index) |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1452 | Changed |= getContainedPass(Index)->doFinalization(M); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1453 | |
| 1454 | return Changed; |
| 1455 | } |
| 1456 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1457 | bool BBPassManager::doInitialization(Function &F) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1458 | bool Changed = false; |
| 1459 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1460 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1461 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1462 | Changed |= BP->doInitialization(F); |
| 1463 | } |
| 1464 | |
| 1465 | return Changed; |
| 1466 | } |
| 1467 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1468 | bool BBPassManager::doFinalization(Function &F) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1469 | bool Changed = false; |
| 1470 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1471 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1472 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1473 | Changed |= BP->doFinalization(F); |
| 1474 | } |
| 1475 | |
| 1476 | return Changed; |
| 1477 | } |
| 1478 | |
| 1479 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1480 | //===----------------------------------------------------------------------===// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1481 | // FunctionPassManager implementation |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1482 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1483 | /// Create new Function pass manager |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1484 | FunctionPassManager::FunctionPassManager(Module *m) : M(m) { |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1485 | FPM = new FunctionPassManagerImpl(); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1486 | // FPM is the top level manager. |
| 1487 | FPM->setTopLevelManager(FPM); |
Devang Patel | 1036b65 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1488 | |
Dan Gohman | 565df95 | 2008-03-13 02:08:36 +0000 | [diff] [blame] | 1489 | AnalysisResolver *AR = new AnalysisResolver(*FPM); |
Devang Patel | 1036b65 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1490 | FPM->setResolver(AR); |
Devang Patel | 1f65368 | 2006-12-08 18:57:16 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1493 | FunctionPassManager::~FunctionPassManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1494 | delete FPM; |
| 1495 | } |
| 1496 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1497 | void FunctionPassManager::add(Pass *P) { |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 1498 | FPM->add(P); |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1501 | /// run - Execute all of the passes scheduled for execution. Keep |
| 1502 | /// track of whether any of the passes modifies the function, and if |
| 1503 | /// so, return true. |
| 1504 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1505 | bool FunctionPassManager::run(Function &F) { |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 1506 | handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) { |
| 1507 | report_fatal_error("Error reading bitcode file: " + EIB.message()); |
| 1508 | }); |
Devang Patel | 272908d | 2006-12-08 22:57:48 +0000 | [diff] [blame] | 1509 | return FPM->run(F); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1513 | /// doInitialization - Run all of the initializers for the function passes. |
| 1514 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1515 | bool FunctionPassManager::doInitialization() { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1516 | return FPM->doInitialization(*M); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Dan Gohman | e6656eb | 2007-07-30 14:51:13 +0000 | [diff] [blame] | 1519 | /// doFinalization - Run all of the finalizers for the function passes. |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1520 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1521 | bool FunctionPassManager::doFinalization() { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1522 | return FPM->doFinalization(*M); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1525 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1526 | // FunctionPassManagerImpl implementation |
| 1527 | // |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1528 | bool FunctionPassManagerImpl::doInitialization(Module &M) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1529 | bool Changed = false; |
| 1530 | |
Dan Gohman | 05ebc8f | 2009-11-23 16:24:18 +0000 | [diff] [blame] | 1531 | dumpArguments(); |
| 1532 | dumpPasses(); |
| 1533 | |
Yaron Keren | 4849aa3 | 2015-06-05 17:48:47 +0000 | [diff] [blame] | 1534 | for (ImmutablePass *ImPass : getImmutablePasses()) |
| 1535 | Changed |= ImPass->doInitialization(M); |
Pedro Artigas | 41b9884 | 2012-12-05 17:12:22 +0000 | [diff] [blame] | 1536 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1537 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1538 | Changed |= getContainedManager(Index)->doInitialization(M); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1539 | |
| 1540 | return Changed; |
| 1541 | } |
| 1542 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1543 | bool FunctionPassManagerImpl::doFinalization(Module &M) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1544 | bool Changed = false; |
| 1545 | |
Pedro Artigas | 41b9884 | 2012-12-05 17:12:22 +0000 | [diff] [blame] | 1546 | for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index) |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1547 | Changed |= getContainedManager(Index)->doFinalization(M); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1548 | |
Yaron Keren | 4849aa3 | 2015-06-05 17:48:47 +0000 | [diff] [blame] | 1549 | for (ImmutablePass *ImPass : getImmutablePasses()) |
| 1550 | Changed |= ImPass->doFinalization(M); |
Pedro Artigas | 41b9884 | 2012-12-05 17:12:22 +0000 | [diff] [blame] | 1551 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1552 | return Changed; |
| 1553 | } |
| 1554 | |
Devang Patel | ec9c58f | 2009-04-01 22:34:41 +0000 | [diff] [blame] | 1555 | /// cleanup - After running all passes, clean up pass manager cache. |
| 1556 | void FPPassManager::cleanup() { |
| 1557 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1558 | FunctionPass *FP = getContainedPass(Index); |
| 1559 | AnalysisResolver *AR = FP->getResolver(); |
| 1560 | assert(AR && "Analysis Resolver is not set"); |
| 1561 | AR->clearAnalysisImpls(); |
| 1562 | } |
| 1563 | } |
| 1564 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1565 | void FunctionPassManagerImpl::releaseMemoryOnTheFly() { |
| 1566 | if (!wasRun) |
| 1567 | return; |
| 1568 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1569 | FPPassManager *FPPM = getContainedManager(Index); |
| 1570 | for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) { |
| 1571 | FPPM->getContainedPass(Index)->releaseMemory(); |
| 1572 | } |
| 1573 | } |
Torok Edwin | 896556e | 2009-06-29 21:05:10 +0000 | [diff] [blame] | 1574 | wasRun = false; |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1577 | // Execute all the passes managed by this top level manager. |
| 1578 | // Return true if any function is modified by a pass. |
| 1579 | bool FunctionPassManagerImpl::run(Function &F) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1580 | bool Changed = false; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1581 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 1582 | initializeAllAnalysisInfo(); |
Juergen Ributzka | 34390c7 | 2014-05-16 02:33:15 +0000 | [diff] [blame] | 1583 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
Serge Pavlov | ed5eb93 | 2017-01-15 10:23:18 +0000 | [diff] [blame] | 1584 | Changed |= getContainedManager(Index)->runOnFunction(F); |
Juergen Ributzka | 34390c7 | 2014-05-16 02:33:15 +0000 | [diff] [blame] | 1585 | F.getContext().yield(); |
| 1586 | } |
Devang Patel | ec9c58f | 2009-04-01 22:34:41 +0000 | [diff] [blame] | 1587 | |
| 1588 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1589 | getContainedManager(Index)->cleanup(); |
| 1590 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1591 | wasRun = true; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1592 | return Changed; |
| 1593 | } |
| 1594 | |
| 1595 | //===----------------------------------------------------------------------===// |
| 1596 | // FPPassManager implementation |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1597 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 1598 | char FPPassManager::ID = 0; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1599 | /// Print passes managed by this manager |
| 1600 | void FPPassManager::dumpPassStructure(unsigned Offset) { |
Benjamin Kramer | cc863b2 | 2011-10-16 16:30:34 +0000 | [diff] [blame] | 1601 | dbgs().indent(Offset*2) << "FunctionPass Manager\n"; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1602 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1603 | FunctionPass *FP = getContainedPass(Index); |
Dan Gohman | f71c521 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 1604 | FP->dumpPassStructure(Offset + 1); |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1605 | dumpLastUses(FP, Offset+1); |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1610 | /// Execute all of the passes scheduled for execution by invoking |
| 1611 | /// runOnFunction method. Keep track of whether any of the passes modifies |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1612 | /// the function, and if so, return true. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1613 | bool FPPassManager::runOnFunction(Function &F) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1614 | if (F.isDeclaration()) |
| 1615 | return false; |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1616 | |
| 1617 | bool Changed = false; |
Jessica Paquette | e49374d | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 1618 | Module &M = *F.getParent(); |
Devang Patel | cbbf291 | 2008-03-20 01:09:53 +0000 | [diff] [blame] | 1619 | // Collect inherited analysis from Module level pass manager. |
| 1620 | populateInheritedAnalysis(TPM->activeStack); |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1621 | |
Jessica Paquette | f2a202c | 2018-08-31 20:19:41 +0000 | [diff] [blame] | 1622 | unsigned InstrCount, FunctionSize = 0; |
Jessica Paquette | a0aa5b3 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1623 | StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount; |
Xin Tong | 023e25a | 2018-07-22 05:27:41 +0000 | [diff] [blame] | 1624 | bool EmitICRemark = M.shouldEmitInstrCountChangedRemark(); |
Jessica Paquette | f2a202c | 2018-08-31 20:19:41 +0000 | [diff] [blame] | 1625 | // Collect the initial size of the module. |
| 1626 | if (EmitICRemark) { |
Jessica Paquette | a0aa5b3 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1627 | InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount); |
Jessica Paquette | f2a202c | 2018-08-31 20:19:41 +0000 | [diff] [blame] | 1628 | FunctionSize = F.getInstructionCount(); |
| 1629 | } |
| 1630 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1631 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1632 | FunctionPass *FP = getContainedPass(Index); |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1633 | bool LocalChanged = false; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1634 | |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1635 | dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1636 | dumpRequiredSet(FP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1637 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1638 | initializeAnalysisImpl(FP); |
Eric Christopher | 3c0d516 | 2012-03-23 03:54:05 +0000 | [diff] [blame] | 1639 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1640 | { |
| 1641 | PassManagerPrettyStackEntry X(FP, F); |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1642 | TimeRegion PassTimer(getPassTimer(FP)); |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1643 | LocalChanged |= FP->runOnFunction(F); |
Jessica Paquette | f2a202c | 2018-08-31 20:19:41 +0000 | [diff] [blame] | 1644 | if (EmitICRemark) { |
| 1645 | unsigned NewSize = F.getInstructionCount(); |
| 1646 | |
| 1647 | // Update the size of the function, emit a remark, and update the size |
| 1648 | // of the module. |
| 1649 | if (NewSize != FunctionSize) { |
Jessica Paquette | f2a202c | 2018-08-31 20:19:41 +0000 | [diff] [blame] | 1650 | int64_t Delta = static_cast<int64_t>(NewSize) - |
| 1651 | static_cast<int64_t>(FunctionSize); |
Jessica Paquette | a0aa5b3 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1652 | emitInstrCountChangedRemark(FP, M, Delta, InstrCount, |
| 1653 | FunctionToInstrCount, &F); |
Jessica Paquette | f2a202c | 2018-08-31 20:19:41 +0000 | [diff] [blame] | 1654 | InstrCount = static_cast<int64_t>(InstrCount) + Delta; |
| 1655 | FunctionSize = NewSize; |
| 1656 | } |
| 1657 | } |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1658 | } |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1659 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1660 | Changed |= LocalChanged; |
| 1661 | if (LocalChanged) |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1662 | dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1663 | dumpPreservedSet(FP); |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1664 | dumpUsedSet(FP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1665 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1666 | verifyPreservedAnalysis(FP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1667 | removeNotPreservedAnalysis(FP); |
| 1668 | recordAvailableAnalysis(FP); |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1669 | removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1670 | } |
| 1671 | return Changed; |
| 1672 | } |
| 1673 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1674 | bool FPPassManager::runOnModule(Module &M) { |
Pedro Artigas | d6b092b | 2012-11-29 17:47:05 +0000 | [diff] [blame] | 1675 | bool Changed = false; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1676 | |
Serge Pavlov | ed5eb93 | 2017-01-15 10:23:18 +0000 | [diff] [blame] | 1677 | for (Function &F : M) |
Yaron Keren | 3b1e24b | 2015-06-05 14:15:07 +0000 | [diff] [blame] | 1678 | Changed |= runOnFunction(F); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1679 | |
Pedro Artigas | d6b092b | 2012-11-29 17:47:05 +0000 | [diff] [blame] | 1680 | return Changed; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1683 | bool FPPassManager::doInitialization(Module &M) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1684 | bool Changed = false; |
| 1685 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1686 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1687 | Changed |= getContainedPass(Index)->doInitialization(M); |
Andrew Trick | dc073ad | 2013-09-18 23:31:10 +0000 | [diff] [blame] | 1688 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1689 | return Changed; |
| 1690 | } |
| 1691 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1692 | bool FPPassManager::doFinalization(Module &M) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1693 | bool Changed = false; |
Pedro Artigas | 41b9884 | 2012-12-05 17:12:22 +0000 | [diff] [blame] | 1694 | |
| 1695 | for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index) |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1696 | Changed |= getContainedPass(Index)->doFinalization(M); |
Andrew Trick | dc073ad | 2013-09-18 23:31:10 +0000 | [diff] [blame] | 1697 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1698 | return Changed; |
| 1699 | } |
| 1700 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1701 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1702 | // MPPassManager implementation |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1703 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1704 | /// Execute all of the passes scheduled for execution by invoking |
| 1705 | /// runOnModule method. Keep track of whether any of the passes modifies |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1706 | /// the module, and if so, return true. |
| 1707 | bool |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1708 | MPPassManager::runOnModule(Module &M) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1709 | bool Changed = false; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 1710 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1711 | // Initialize on-the-fly passes |
Yaron Keren | 3b1e24b | 2015-06-05 14:15:07 +0000 | [diff] [blame] | 1712 | for (auto &OnTheFlyManager : OnTheFlyManagers) { |
| 1713 | FunctionPassManagerImpl *FPP = OnTheFlyManager.second; |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1714 | Changed |= FPP->doInitialization(M); |
| 1715 | } |
| 1716 | |
Pedro Artigas | d6b092b | 2012-11-29 17:47:05 +0000 | [diff] [blame] | 1717 | // Initialize module passes |
| 1718 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1719 | Changed |= getContainedPass(Index)->doInitialization(M); |
| 1720 | |
Jessica Paquette | 454d103 | 2018-08-31 20:20:55 +0000 | [diff] [blame] | 1721 | unsigned InstrCount, ModuleCount = 0; |
Jessica Paquette | a0aa5b3 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1722 | StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount; |
Xin Tong | 023e25a | 2018-07-22 05:27:41 +0000 | [diff] [blame] | 1723 | bool EmitICRemark = M.shouldEmitInstrCountChangedRemark(); |
Jessica Paquette | 454d103 | 2018-08-31 20:20:55 +0000 | [diff] [blame] | 1724 | // Collect the initial size of the module. |
| 1725 | if (EmitICRemark) { |
Jessica Paquette | a0aa5b3 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1726 | InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount); |
Jessica Paquette | 454d103 | 2018-08-31 20:20:55 +0000 | [diff] [blame] | 1727 | ModuleCount = InstrCount; |
| 1728 | } |
| 1729 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1730 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1731 | ModulePass *MP = getContainedPass(Index); |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1732 | bool LocalChanged = false; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1733 | |
Benjamin Kramer | dfcc285 | 2009-12-08 13:07:38 +0000 | [diff] [blame] | 1734 | dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1735 | dumpRequiredSet(MP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1736 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1737 | initializeAnalysisImpl(MP); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1738 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1739 | { |
| 1740 | PassManagerPrettyStackEntry X(MP, M); |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1741 | TimeRegion PassTimer(getPassTimer(MP)); |
| 1742 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1743 | LocalChanged |= MP->runOnModule(M); |
Jessica Paquette | 454d103 | 2018-08-31 20:20:55 +0000 | [diff] [blame] | 1744 | if (EmitICRemark) { |
| 1745 | // Update the size of the module. |
Jessica Paquette | 454d103 | 2018-08-31 20:20:55 +0000 | [diff] [blame] | 1746 | ModuleCount = M.getInstructionCount(); |
| 1747 | if (ModuleCount != InstrCount) { |
Jessica Paquette | 9a23c55 | 2018-08-31 20:20:57 +0000 | [diff] [blame] | 1748 | int64_t Delta = static_cast<int64_t>(ModuleCount) - |
| 1749 | static_cast<int64_t>(InstrCount); |
Jessica Paquette | a0aa5b3 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1750 | emitInstrCountChangedRemark(MP, M, Delta, InstrCount, |
| 1751 | FunctionToInstrCount); |
Jessica Paquette | a69696d | 2018-08-31 22:43:41 +0000 | [diff] [blame] | 1752 | InstrCount = ModuleCount; |
Jessica Paquette | 454d103 | 2018-08-31 20:20:55 +0000 | [diff] [blame] | 1753 | } |
| 1754 | } |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1755 | } |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1756 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1757 | Changed |= LocalChanged; |
| 1758 | if (LocalChanged) |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 1759 | dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG, |
Benjamin Kramer | dfcc285 | 2009-12-08 13:07:38 +0000 | [diff] [blame] | 1760 | M.getModuleIdentifier()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1761 | dumpPreservedSet(MP); |
Chandler Carruth | 44a1385 | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1762 | dumpUsedSet(MP); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1763 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1764 | verifyPreservedAnalysis(MP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1765 | removeNotPreservedAnalysis(MP); |
| 1766 | recordAvailableAnalysis(MP); |
Benjamin Kramer | dfcc285 | 2009-12-08 13:07:38 +0000 | [diff] [blame] | 1767 | removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1768 | } |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1769 | |
Pedro Artigas | d6b092b | 2012-11-29 17:47:05 +0000 | [diff] [blame] | 1770 | // Finalize module passes |
Pedro Artigas | 41b9884 | 2012-12-05 17:12:22 +0000 | [diff] [blame] | 1771 | for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index) |
Pedro Artigas | d6b092b | 2012-11-29 17:47:05 +0000 | [diff] [blame] | 1772 | Changed |= getContainedPass(Index)->doFinalization(M); |
| 1773 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1774 | // Finalize on-the-fly passes |
Yaron Keren | 3b1e24b | 2015-06-05 14:15:07 +0000 | [diff] [blame] | 1775 | for (auto &OnTheFlyManager : OnTheFlyManagers) { |
| 1776 | FunctionPassManagerImpl *FPP = OnTheFlyManager.second; |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1777 | // We don't know when is the last time an on-the-fly pass is run, |
| 1778 | // so we need to releaseMemory / finalize here |
| 1779 | FPP->releaseMemoryOnTheFly(); |
| 1780 | Changed |= FPP->doFinalization(M); |
| 1781 | } |
Andrew Trick | dc073ad | 2013-09-18 23:31:10 +0000 | [diff] [blame] | 1782 | |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1783 | return Changed; |
| 1784 | } |
| 1785 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1786 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 1787 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 1788 | /// through getAnalysis interface. |
| 1789 | void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1790 | assert(P->getPotentialPassManagerType() == PMT_ModulePassManager && |
| 1791 | "Unable to handle Pass that requires lower level Analysis pass"); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1792 | assert((P->getPotentialPassManagerType() < |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1793 | RequiredPass->getPotentialPassManagerType()) && |
| 1794 | "Unable to handle Pass that requires lower level Analysis pass"); |
Andrew Trick | 02066f2 | 2014-04-08 03:40:34 +0000 | [diff] [blame] | 1795 | if (!RequiredPass) |
| 1796 | return; |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1797 | |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1798 | FunctionPassManagerImpl *FPP = OnTheFlyManagers[P]; |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1799 | if (!FPP) { |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1800 | FPP = new FunctionPassManagerImpl(); |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1801 | // FPP is the top level manager. |
| 1802 | FPP->setTopLevelManager(FPP); |
| 1803 | |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1804 | OnTheFlyManagers[P] = FPP; |
| 1805 | } |
Chandler Carruth | 5b0d3e3 | 2015-01-28 09:47:21 +0000 | [diff] [blame] | 1806 | const PassInfo *RequiredPassPI = |
| 1807 | TPM->findAnalysisPassInfo(RequiredPass->getPassID()); |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1808 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1809 | Pass *FoundPass = nullptr; |
Andrew Trick | 02066f2 | 2014-04-08 03:40:34 +0000 | [diff] [blame] | 1810 | if (RequiredPassPI && RequiredPassPI->isAnalysis()) { |
| 1811 | FoundPass = |
| 1812 | ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID()); |
Devang Patel | 6eb3a6b | 2011-09-13 21:13:29 +0000 | [diff] [blame] | 1813 | } |
Andrew Trick | 02066f2 | 2014-04-08 03:40:34 +0000 | [diff] [blame] | 1814 | if (!FoundPass) { |
| 1815 | FoundPass = RequiredPass; |
| 1816 | // This should be guaranteed to add RequiredPass to the passmanager given |
Sylvestre Ledru | 469de19 | 2014-08-11 18:04:46 +0000 | [diff] [blame] | 1817 | // that we checked for an available analysis above. |
Andrew Trick | 02066f2 | 2014-04-08 03:40:34 +0000 | [diff] [blame] | 1818 | FPP->add(RequiredPass); |
| 1819 | } |
| 1820 | // Register P as the last user of FoundPass or RequiredPass. |
| 1821 | SmallVector<Pass *, 1> LU; |
| 1822 | LU.push_back(FoundPass); |
| 1823 | FPP->setLastUser(LU, P); |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1824 | } |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1825 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1826 | /// Return function pass corresponding to PassInfo PI, that is |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1827 | /// required by module pass MP. Instantiate analysis pass, by using |
| 1828 | /// its runOnFunction() for function F. |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1829 | Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){ |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1830 | FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP]; |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1831 | assert(FPP && "Unable to find on the fly pass"); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1832 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1833 | FPP->releaseMemoryOnTheFly(); |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1834 | FPP->run(F); |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1835 | return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI); |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1839 | //===----------------------------------------------------------------------===// |
| 1840 | // PassManagerImpl implementation |
Owen Anderson | 1aa2751 | 2012-11-15 00:14:15 +0000 | [diff] [blame] | 1841 | |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1842 | // |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1843 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1844 | /// whether any of the passes modifies the module, and if so, return true. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1845 | bool PassManagerImpl::run(Module &M) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1846 | bool Changed = false; |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1847 | |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 1848 | dumpArguments(); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1849 | dumpPasses(); |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 1850 | |
Yaron Keren | 3b1e24b | 2015-06-05 14:15:07 +0000 | [diff] [blame] | 1851 | for (ImmutablePass *ImPass : getImmutablePasses()) |
| 1852 | Changed |= ImPass->doInitialization(M); |
Pedro Artigas | 41b9884 | 2012-12-05 17:12:22 +0000 | [diff] [blame] | 1853 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 1854 | initializeAllAnalysisInfo(); |
Juergen Ributzka | 34390c7 | 2014-05-16 02:33:15 +0000 | [diff] [blame] | 1855 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
Serge Pavlov | ed5eb93 | 2017-01-15 10:23:18 +0000 | [diff] [blame] | 1856 | Changed |= getContainedManager(Index)->runOnModule(M); |
Juergen Ributzka | 34390c7 | 2014-05-16 02:33:15 +0000 | [diff] [blame] | 1857 | M.getContext().yield(); |
| 1858 | } |
Pedro Artigas | 41b9884 | 2012-12-05 17:12:22 +0000 | [diff] [blame] | 1859 | |
Yaron Keren | 3b1e24b | 2015-06-05 14:15:07 +0000 | [diff] [blame] | 1860 | for (ImmutablePass *ImPass : getImmutablePasses()) |
| 1861 | Changed |= ImPass->doFinalization(M); |
Pedro Artigas | 41b9884 | 2012-12-05 17:12:22 +0000 | [diff] [blame] | 1862 | |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1863 | return Changed; |
| 1864 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1865 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1866 | //===----------------------------------------------------------------------===// |
| 1867 | // PassManager implementation |
| 1868 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1869 | /// Create new pass manager |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1870 | PassManager::PassManager() { |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1871 | PM = new PassManagerImpl(); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1872 | // PM is the top level manager |
| 1873 | PM->setTopLevelManager(PM); |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1876 | PassManager::~PassManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1877 | delete PM; |
| 1878 | } |
| 1879 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1880 | void PassManager::add(Pass *P) { |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 1881 | PM->add(P); |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1885 | /// whether any of the passes modifies the module, and if so, return true. |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1886 | bool PassManager::run(Module &M) { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1887 | return PM->run(M); |
| 1888 | } |
| 1889 | |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1890 | //===----------------------------------------------------------------------===// |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1891 | // PMStack implementation |
| 1892 | // |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 1893 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1894 | // Pop Pass Manager from the stack and clear its analysis info. |
| 1895 | void PMStack::pop() { |
| 1896 | |
| 1897 | PMDataManager *Top = this->top(); |
| 1898 | Top->initializeAnalysisInfo(); |
| 1899 | |
| 1900 | S.pop_back(); |
| 1901 | } |
| 1902 | |
| 1903 | // Push PM on the stack and set its top level manager. |
Dan Gohman | 11eecd6 | 2008-03-13 01:21:31 +0000 | [diff] [blame] | 1904 | void PMStack::push(PMDataManager *PM) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1905 | assert(PM && "Unable to push. Pass Manager expected"); |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1906 | assert(PM->getDepth()==0 && "Pass Manager depth set too early"); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1907 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1908 | if (!this->empty()) { |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1909 | assert(PM->getPassManagerType() > this->top()->getPassManagerType() |
| 1910 | && "pushing bad pass manager to PMStack"); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1911 | PMTopLevelManager *TPM = this->top()->getTopLevelManager(); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1912 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1913 | assert(TPM && "Unable to find top level manager"); |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1914 | TPM->addIndirectPassManager(PM); |
| 1915 | PM->setTopLevelManager(TPM); |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1916 | PM->setDepth(this->top()->getDepth()+1); |
Craig Topper | 821d6af | 2013-02-06 06:50:38 +0000 | [diff] [blame] | 1917 | } else { |
Benjamin Kramer | 6bb5b3c | 2011-08-29 18:14:15 +0000 | [diff] [blame] | 1918 | assert((PM->getPassManagerType() == PMT_ModulePassManager |
| 1919 | || PM->getPassManagerType() == PMT_FunctionPassManager) |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1920 | && "pushing bad pass manager to PMStack"); |
| 1921 | PM->setDepth(1); |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1924 | S.push_back(PM); |
| 1925 | } |
| 1926 | |
| 1927 | // Dump content of the pass manager stack. |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 1928 | LLVM_DUMP_METHOD void PMStack::dump() const { |
Yaron Keren | 4849aa3 | 2015-06-05 17:48:47 +0000 | [diff] [blame] | 1929 | for (PMDataManager *Manager : S) |
| 1930 | dbgs() << Manager->getAsPass()->getPassName() << ' '; |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1931 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1932 | if (!S.empty()) |
Benjamin Kramer | 4dd515c | 2011-08-29 18:14:17 +0000 | [diff] [blame] | 1933 | dbgs() << '\n'; |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1936 | /// Find appropriate Module Pass Manager in the PM Stack and |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1937 | /// add self into that manager. |
| 1938 | void ModulePass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1939 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1940 | // Find Module Pass Manager |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1941 | while (!PMS.empty()) { |
Devang Patel | 23f8aa9 | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1942 | PassManagerType TopPMType = PMS.top()->getPassManagerType(); |
| 1943 | if (TopPMType == PreferredType) |
| 1944 | break; // We found desired pass manager |
| 1945 | else if (TopPMType > PMT_ModulePassManager) |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1946 | PMS.pop(); // Pop children pass managers |
Devang Patel | ac99eca | 2007-01-11 19:59:06 +0000 | [diff] [blame] | 1947 | else |
| 1948 | break; |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1949 | } |
Devang Patel | 18ff636 | 2008-09-09 21:38:40 +0000 | [diff] [blame] | 1950 | assert(!PMS.empty() && "Unable to find appropriate Pass Manager"); |
Devang Patel | 23f8aa9 | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1951 | PMS.top()->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1954 | /// Find appropriate Function Pass Manager or Call Graph Pass Manager |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1955 | /// in the PM Stack and add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1956 | void FunctionPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1957 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1958 | |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 1959 | // Find Function Pass Manager |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1960 | while (!PMS.empty()) { |
Devang Patel | ac99eca | 2007-01-11 19:59:06 +0000 | [diff] [blame] | 1961 | if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager) |
| 1962 | PMS.pop(); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1963 | else |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1964 | break; |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1965 | } |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1966 | |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1967 | // Create new Function Pass Manager if needed. |
| 1968 | FPPassManager *FPP; |
| 1969 | if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) { |
| 1970 | FPP = (FPPassManager *)PMS.top(); |
| 1971 | } else { |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1972 | assert(!PMS.empty() && "Unable to create Function Pass Manager"); |
| 1973 | PMDataManager *PMD = PMS.top(); |
| 1974 | |
| 1975 | // [1] Create new Function Pass Manager |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1976 | FPP = new FPPassManager(); |
Devang Patel | cbbf291 | 2008-03-20 01:09:53 +0000 | [diff] [blame] | 1977 | FPP->populateInheritedAnalysis(PMS); |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1978 | |
| 1979 | // [2] Set up new manager's top level manager |
| 1980 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 1981 | TPM->addIndirectPassManager(FPP); |
| 1982 | |
| 1983 | // [3] Assign manager to manage this new manager. This may create |
| 1984 | // and push new managers into PMS |
Devang Patel | a328690 | 2008-09-09 17:56:50 +0000 | [diff] [blame] | 1985 | FPP->assignPassManager(PMS, PMD->getPassManagerType()); |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1986 | |
| 1987 | // [4] Push new manager into PMS |
| 1988 | PMS.push(FPP); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1991 | // Assign FPP as the manager of this pass. |
| 1992 | FPP->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1995 | /// Find appropriate Basic Pass Manager or Call Graph Pass Manager |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1996 | /// in the PM Stack and add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1997 | void BasicBlockPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1998 | PassManagerType PreferredType) { |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1999 | BBPassManager *BBP; |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 2000 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 2001 | // Basic Pass Manager is a leaf pass manager. It does not handle |
| 2002 | // any other pass manager. |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 2003 | if (!PMS.empty() && |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 2004 | PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) { |
| 2005 | BBP = (BBPassManager *)PMS.top(); |
| 2006 | } else { |
| 2007 | // If leaf manager is not Basic Block Pass manager then create new |
| 2008 | // basic Block Pass manager. |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 2009 | assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager"); |
| 2010 | PMDataManager *PMD = PMS.top(); |
| 2011 | |
| 2012 | // [1] Create new Basic Block Manager |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 2013 | BBP = new BBPassManager(); |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 2014 | |
| 2015 | // [2] Set up new manager's top level manager |
| 2016 | // Basic Block Pass Manager does not live by itself |
| 2017 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 2018 | TPM->addIndirectPassManager(BBP); |
| 2019 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 2020 | // [3] Assign manager to manage this new manager. This may create |
| 2021 | // and push new managers into PMS |
David Greene | 103d4b4 | 2010-05-10 20:24:27 +0000 | [diff] [blame] | 2022 | BBP->assignPassManager(PMS, PreferredType); |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 2023 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 2024 | // [4] Push new manager into PMS |
| 2025 | PMS.push(BBP); |
| 2026 | } |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 2027 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 2028 | // Assign BBP as the manager of this pass. |
| 2029 | BBP->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Dan Gohman | d3a20c9 | 2008-03-11 16:41:42 +0000 | [diff] [blame] | 2032 | PassManagerBase::~PassManagerBase() {} |