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