Chandler Carruth | 0863273 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 1 | //===- LegacyPassManager.cpp - LLVM Pass Infrastructure Implementation ----===// |
Devang Patel | a9645c0 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | ca6d719 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Devang Patel | a9645c0 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Chandler Carruth | 0863273 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 9 | // This file implements the legacy LLVM Pass Manager infrastructure. |
Devang Patel | a9645c0 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Chandler Carruth | 0863273 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 13 | #include "llvm/IR/LegacyPassManager.h" |
Florian Hahn | 14a9414 | 2018-05-24 21:33:17 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/MapVector.h" |
James Henderson | c67277e | 2017-05-16 09:43:21 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Statistic.h" |
Jessica Paquette | 46e868c | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 16 | #include "llvm/IR/DiagnosticInfo.h" |
Pavel Labath | 64540e6 | 2016-10-25 16:20:07 +0000 | [diff] [blame] | 17 | #include "llvm/IR/IRPrintingPasses.h" |
| 18 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 0863273 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LegacyPassManagers.h" |
Chandler Carruth | 877b520 | 2014-03-04 12:32:42 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LegacyPassNameParser.h" |
Chandler Carruth | 49d0d3b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Module.h" |
Fedor Sergeev | 2dc7a04 | 2018-08-28 21:06:51 +0000 | [diff] [blame] | 22 | #include "llvm/IR/PassTimingInfo.h" |
Arthur Eubanks | 0a11852 | 2020-12-03 10:59:10 -0800 | [diff] [blame] | 23 | #include "llvm/IR/PrintPasses.h" |
serge-sans-paille | 055ea2a | 2020-08-27 17:31:54 +0200 | [diff] [blame] | 24 | #include "llvm/IR/StructuralHash.h" |
Pavel Labath | 64540e6 | 2016-10-25 16:20:07 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Chrono.h" |
Devang Patel | 8b5b72e | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Peter Collingbourne | 9c2be2d | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Error.h" |
Torok Edwin | 681f556 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ErrorHandling.h" |
Devang Patel | 7571112 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ManagedStatic.h" |
Michael J. Spencer | af423d5 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Mutex.h" |
Anton Afanasyev | d506b67b | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 32 | #include "llvm/Support/TimeProfiler.h" |
Chandler Carruth | 345b827 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Timer.h" |
| 34 | #include "llvm/Support/raw_ostream.h" |
Jeff Cohen | 18c735d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Weiming Zhao | 5938f0d | 2016-01-06 22:55:03 +0000 | [diff] [blame] | 36 | #include <unordered_set> |
Dan Gohman | 4283134 | 2007-10-03 19:04:09 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Devang Patel | ff825b3 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 38 | |
Devang Patel | d023bb4 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 39 | // See PassManagers.h for Pass Manager infrastructure overview. |
Devang Patel | ff15e79 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 40 | |
Devang Patel | 8b5b72e | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | // Pass debugging information. Often it is useful to find out what pass is |
| 43 | // running when a crash occurs in a utility. When this library is compiled with |
| 44 | // debugging on, a command line option (--debug-pass) is enabled that causes the |
| 45 | // pass name to be printed before it executes. |
| 46 | // |
| 47 | |
Chandler Carruth | 0863273 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 48 | namespace { |
Devang Patel | 5070663 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 49 | // Different debug levels that can be enabled... |
| 50 | enum PassDebugLevel { |
Dmitri Gribenko | 353cc7c | 2013-05-05 00:40:33 +0000 | [diff] [blame] | 51 | Disabled, Arguments, Structure, Executions, Details |
Devang Patel | 5070663 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 52 | }; |
Arthur Eubanks | 0a11852 | 2020-12-03 10:59:10 -0800 | [diff] [blame] | 53 | } // namespace |
Devang Patel | 5070663 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 54 | |
Arthur Eubanks | 5687c52 | 2021-02-04 19:34:09 -0800 | [diff] [blame] | 55 | static cl::opt<enum PassDebugLevel> PassDebugging( |
| 56 | "debug-pass", cl::Hidden, |
| 57 | cl::desc("Print legacy PassManager debugging information"), |
| 58 | cl::values(clEnumVal(Disabled, "disable debug output"), |
| 59 | clEnumVal(Arguments, "print pass arguments to pass to 'opt'"), |
| 60 | clEnumVal(Structure, "print pass structure before run()"), |
| 61 | clEnumVal(Executions, "print pass name before it is executed"), |
| 62 | clEnumVal(Details, "print pass details when it is executed"))); |
David Greene | c63505d | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 63 | |
Chris Lattner | bc72d15 | 2009-09-15 05:03:04 +0000 | [diff] [blame] | 64 | /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions |
| 65 | /// or higher is specified. |
| 66 | bool PMDataManager::isPassDebuggingExecutionsOrMore() const { |
Andrew Trick | 61cd3a6 | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 67 | return PassDebugging >= Executions; |
Chris Lattner | bc72d15 | 2009-09-15 05:03:04 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Jessica Paquette | 6c0520a | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 70 | unsigned PMDataManager::initSizeRemarkInfo( |
| 71 | Module &M, StringMap<std::pair<unsigned, unsigned>> &FunctionToInstrCount) { |
Jessica Paquette | 46e868c | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 72 | // Only calculate getInstructionCount if the size-info remark is requested. |
Jessica Paquette | 6c0520a | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 73 | unsigned InstrCount = 0; |
| 74 | |
| 75 | // Collect instruction counts for every function. We'll use this to emit |
| 76 | // per-function size remarks later. |
| 77 | for (Function &F : M) { |
| 78 | unsigned FCount = F.getInstructionCount(); |
| 79 | |
| 80 | // Insert a record into FunctionToInstrCount keeping track of the current |
| 81 | // size of the function as the first member of a pair. Set the second |
| 82 | // member to 0; if the function is deleted by the pass, then when we get |
| 83 | // here, we'll be able to let the user know that F no longer contributes to |
| 84 | // the module. |
| 85 | FunctionToInstrCount[F.getName().str()] = |
| 86 | std::pair<unsigned, unsigned>(FCount, 0); |
| 87 | InstrCount += FCount; |
| 88 | } |
| 89 | return InstrCount; |
Jessica Paquette | 46e868c | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 90 | } |
Andrew Trick | 61cd3a6 | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 91 | |
Jessica Paquette | 6c0520a | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 92 | void PMDataManager::emitInstrCountChangedRemark( |
| 93 | Pass *P, Module &M, int64_t Delta, unsigned CountBefore, |
| 94 | StringMap<std::pair<unsigned, unsigned>> &FunctionToInstrCount, |
| 95 | Function *F) { |
Jessica Paquette | e4a7fcd | 2018-08-31 20:51:54 +0000 | [diff] [blame] | 96 | // If it's a pass manager, don't emit a remark. (This hinges on the assumption |
| 97 | // that the only passes that return non-null with getAsPMDataManager are pass |
| 98 | // managers.) The reason we have to do this is to avoid emitting remarks for |
| 99 | // CGSCC passes. |
| 100 | if (P->getAsPMDataManager()) |
| 101 | return; |
| 102 | |
Jessica Paquette | b9d2107 | 2018-09-04 21:03:43 +0000 | [diff] [blame] | 103 | // Set to true if this isn't a module pass or CGSCC pass. |
| 104 | bool CouldOnlyImpactOneFunction = (F != nullptr); |
| 105 | |
Jessica Paquette | 6c0520a | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 106 | // Helper lambda that updates the changes to the size of some function. |
| 107 | auto UpdateFunctionChanges = |
| 108 | [&FunctionToInstrCount](Function &MaybeChangedFn) { |
| 109 | // Update the total module count. |
| 110 | unsigned FnSize = MaybeChangedFn.getInstructionCount(); |
| 111 | auto It = FunctionToInstrCount.find(MaybeChangedFn.getName()); |
| 112 | |
| 113 | // If we created a new function, then we need to add it to the map and |
| 114 | // say that it changed from 0 instructions to FnSize. |
| 115 | if (It == FunctionToInstrCount.end()) { |
| 116 | FunctionToInstrCount[MaybeChangedFn.getName()] = |
| 117 | std::pair<unsigned, unsigned>(0, FnSize); |
| 118 | return; |
| 119 | } |
| 120 | // Insert the new function size into the second member of the pair. This |
| 121 | // tells us whether or not this function changed in size. |
| 122 | It->second.second = FnSize; |
| 123 | }; |
| 124 | |
| 125 | // We need to initially update all of the function sizes. |
| 126 | // If no function was passed in, then we're either a module pass or an |
| 127 | // CGSCC pass. |
| 128 | if (!CouldOnlyImpactOneFunction) |
| 129 | std::for_each(M.begin(), M.end(), UpdateFunctionChanges); |
| 130 | else |
| 131 | UpdateFunctionChanges(*F); |
| 132 | |
Jessica Paquette | 7be140c | 2018-08-31 20:54:37 +0000 | [diff] [blame] | 133 | // Do we have a function we can use to emit a remark? |
Jessica Paquette | b9d2107 | 2018-09-04 21:03:43 +0000 | [diff] [blame] | 134 | if (!CouldOnlyImpactOneFunction) { |
Jessica Paquette | 7be140c | 2018-08-31 20:54:37 +0000 | [diff] [blame] | 135 | // We need a function containing at least one basic block in order to output |
| 136 | // remarks. Since it's possible that the first function in the module |
| 137 | // doesn't actually contain a basic block, we have to go and find one that's |
| 138 | // suitable for emitting remarks. |
Kazu Hirata | a526d46 | 2021-01-11 18:48:05 -0800 | [diff] [blame] | 139 | auto It = llvm::find_if(M, [](const Function &Fn) { return !Fn.empty(); }); |
Jessica Paquette | 46e868c | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 140 | |
Jessica Paquette | 7be140c | 2018-08-31 20:54:37 +0000 | [diff] [blame] | 141 | // Didn't find a function. Quit. |
| 142 | if (It == M.end()) |
| 143 | return; |
Jessica Paquette | 46e868c | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 144 | |
Jessica Paquette | 7be140c | 2018-08-31 20:54:37 +0000 | [diff] [blame] | 145 | // We found a function containing at least one basic block. |
| 146 | F = &*It; |
| 147 | } |
Jessica Paquette | f4da24f | 2018-08-31 20:20:57 +0000 | [diff] [blame] | 148 | int64_t CountAfter = static_cast<int64_t>(CountBefore) + Delta; |
Jessica Paquette | 46e868c | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 149 | BasicBlock &BB = *F->begin(); |
| 150 | OptimizationRemarkAnalysis R("size-info", "IRSizeChange", |
| 151 | DiagnosticLocation(), &BB); |
| 152 | // FIXME: Move ore namespace to DiagnosticInfo so that we can use it. This |
| 153 | // would let us use NV instead of DiagnosticInfoOptimizationBase::Argument. |
| 154 | R << DiagnosticInfoOptimizationBase::Argument("Pass", P->getPassName()) |
| 155 | << ": IR instruction count changed from " |
| 156 | << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", CountBefore) |
| 157 | << " to " |
| 158 | << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", CountAfter) |
| 159 | << "; Delta: " |
| 160 | << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", Delta); |
| 161 | F->getContext().diagnose(R); // Not using ORE for layering reasons. |
Jessica Paquette | 6c0520a | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 162 | |
| 163 | // Emit per-function size change remarks separately. |
| 164 | std::string PassName = P->getPassName().str(); |
| 165 | |
| 166 | // Helper lambda that emits a remark when the size of a function has changed. |
| 167 | auto EmitFunctionSizeChangedRemark = [&FunctionToInstrCount, &F, &BB, |
Benjamin Kramer | 5c9e53f | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 168 | &PassName](StringRef Fname) { |
Jessica Paquette | 6c0520a | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 169 | unsigned FnCountBefore, FnCountAfter; |
| 170 | std::pair<unsigned, unsigned> &Change = FunctionToInstrCount[Fname]; |
| 171 | std::tie(FnCountBefore, FnCountAfter) = Change; |
| 172 | int64_t FnDelta = static_cast<int64_t>(FnCountAfter) - |
| 173 | static_cast<int64_t>(FnCountBefore); |
| 174 | |
| 175 | if (FnDelta == 0) |
| 176 | return; |
| 177 | |
| 178 | // FIXME: We shouldn't use BB for the location here. Unfortunately, because |
| 179 | // the function that we're looking at could have been deleted, we can't use |
| 180 | // it for the source location. We *want* remarks when a function is deleted |
| 181 | // though, so we're kind of stuck here as is. (This remark, along with the |
| 182 | // whole-module size change remarks really ought not to have source |
| 183 | // locations at all.) |
| 184 | OptimizationRemarkAnalysis FR("size-info", "FunctionIRSizeChange", |
| 185 | DiagnosticLocation(), &BB); |
| 186 | FR << DiagnosticInfoOptimizationBase::Argument("Pass", PassName) |
| 187 | << ": Function: " |
| 188 | << DiagnosticInfoOptimizationBase::Argument("Function", Fname) |
| 189 | << ": IR instruction count changed from " |
| 190 | << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", |
| 191 | FnCountBefore) |
| 192 | << " to " |
| 193 | << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", |
| 194 | FnCountAfter) |
| 195 | << "; Delta: " |
| 196 | << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", FnDelta); |
| 197 | F->getContext().diagnose(FR); |
| 198 | |
| 199 | // Update the function size. |
| 200 | Change.first = FnCountAfter; |
| 201 | }; |
| 202 | |
| 203 | // Are we looking at more than one function? If so, emit remarks for all of |
| 204 | // the functions in the module. Otherwise, only emit one remark. |
| 205 | if (!CouldOnlyImpactOneFunction) |
| 206 | std::for_each(FunctionToInstrCount.keys().begin(), |
| 207 | FunctionToInstrCount.keys().end(), |
| 208 | EmitFunctionSizeChangedRemark); |
| 209 | else |
| 210 | EmitFunctionSizeChangedRemark(F->getName().str()); |
Jessica Paquette | 46e868c | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 211 | } |
Andrew Trick | 61cd3a6 | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 213 | void PassManagerPrettyStackEntry::print(raw_ostream &OS) const { |
Craig Topper | ea28548 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 214 | if (!V && !M) |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 215 | OS << "Releasing pass '"; |
| 216 | else |
| 217 | OS << "Running pass '"; |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 219 | OS << P->getPassName() << "'"; |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 221 | if (M) { |
| 222 | OS << " on module '" << M->getModuleIdentifier() << "'.\n"; |
| 223 | return; |
| 224 | } |
Craig Topper | ea28548 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 225 | if (!V) { |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 226 | OS << '\n'; |
| 227 | return; |
| 228 | } |
| 229 | |
Dan Gohman | fc71929 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 230 | OS << " on "; |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 231 | if (isa<Function>(V)) |
Dan Gohman | fc71929 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 232 | OS << "function"; |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 233 | else if (isa<BasicBlock>(V)) |
Dan Gohman | fc71929 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 234 | OS << "basic block"; |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 235 | else |
Dan Gohman | fc71929 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 236 | OS << "value"; |
| 237 | |
| 238 | OS << " '"; |
Rui Ueyama | 08343a2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 239 | V->printAsOperand(OS, /*PrintType=*/false, M); |
Dan Gohman | fc71929 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 240 | OS << "'\n"; |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Devang Patel | d023bb4 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 243 | namespace llvm { |
Chandler Carruth | 0863273 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 244 | namespace legacy { |
Arthur Eubanks | aa1b43e | 2021-03-02 14:49:46 -0800 | [diff] [blame] | 245 | bool debugPassSpecified() { return PassDebugging != Disabled; } |
| 246 | |
Devang Patel | 9a5447b | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 247 | //===----------------------------------------------------------------------===// |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 248 | // FunctionPassManagerImpl |
Devang Patel | 9a5447b | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 249 | // |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 250 | /// FunctionPassManagerImpl manages FPPassManagers |
| 251 | class FunctionPassManagerImpl : public Pass, |
Devang Patel | afd1979 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 252 | public PMDataManager, |
| 253 | public PMTopLevelManager { |
David Blaikie | cd738cc | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 254 | virtual void anchor(); |
Torok Edwin | cd0d846 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 255 | private: |
| 256 | bool wasRun; |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 257 | public: |
Devang Patel | 4b40954 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 258 | static char ID; |
Andrew Trick | 87862d1 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 259 | explicit FunctionPassManagerImpl() : |
| 260 | Pass(PT_PassManager, ID), PMDataManager(), |
| 261 | PMTopLevelManager(new FPPassManager()), wasRun(false) {} |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 262 | |
Matthias Braun | 01450ac | 2014-12-12 01:27:01 +0000 | [diff] [blame] | 263 | /// \copydoc FunctionPassManager::add() |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 264 | void add(Pass *P) { |
| 265 | schedulePass(P); |
| 266 | } |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 267 | |
| 268 | /// createPrinterPass - Get a function printer pass. |
Craig Topper | 33a2798 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 269 | Pass *createPrinterPass(raw_ostream &O, |
| 270 | const std::string &Banner) const override { |
Chandler Carruth | 394cfcd | 2014-01-12 11:30:46 +0000 | [diff] [blame] | 271 | return createPrintFunctionPass(O, Banner); |
David Greene | c63505d | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Torok Edwin | cd0d846 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 274 | // Prepare for running an on the fly pass, freeing memory if needed |
| 275 | // from a previous run. |
| 276 | void releaseMemoryOnTheFly(); |
| 277 | |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 278 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 279 | /// whether any of the passes modifies the module, and if so, return true. |
| 280 | bool run(Function &F); |
| 281 | |
| 282 | /// doInitialization - Run all of the initializers for the function passes. |
| 283 | /// |
Craig Topper | 33a2798 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 284 | bool doInitialization(Module &M) override; |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 285 | |
Dan Gohman | 31d7ca2 | 2007-07-30 14:51:13 +0000 | [diff] [blame] | 286 | /// doFinalization - Run all of the finalizers for the function passes. |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 287 | /// |
Craig Topper | 33a2798 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 288 | bool doFinalization(Module &M) override; |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 289 | |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 290 | |
Craig Topper | 33a2798 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 291 | PMDataManager *getAsPMDataManager() override { return this; } |
| 292 | Pass *getAsPass() override { return this; } |
| 293 | PassManagerType getTopLevelPassManagerType() override { |
Andrew Trick | 5aeec47 | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 294 | return PMT_FunctionPassManager; |
| 295 | } |
Chris Lattner | 8db741c | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 296 | |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 297 | /// Pass Manager itself does not invalidate any analysis info. |
Craig Topper | 33a2798 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 298 | void getAnalysisUsage(AnalysisUsage &Info) const override { |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 299 | Info.setPreservesAll(); |
| 300 | } |
| 301 | |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 302 | FPPassManager *getContainedManager(unsigned N) { |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 303 | assert(N < PassManagers.size() && "Pass number out of range!"); |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 304 | FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]); |
| 305 | return FP; |
| 306 | } |
evgeny | 5699b11 | 2019-11-01 14:43:51 +0300 | [diff] [blame] | 307 | |
| 308 | void dumpPassStructure(unsigned Offset) override { |
| 309 | for (unsigned I = 0; I < getNumContainedManagers(); ++I) |
| 310 | getContainedManager(I)->dumpPassStructure(Offset); |
| 311 | } |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 312 | }; |
| 313 | |
David Blaikie | cd738cc | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 314 | void FunctionPassManagerImpl::anchor() {} |
| 315 | |
Devang Patel | 4b40954 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 316 | char FunctionPassManagerImpl::ID = 0; |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 317 | |
| 318 | //===----------------------------------------------------------------------===// |
| 319 | // FunctionPassManagerImpl implementation |
| 320 | // |
| 321 | bool FunctionPassManagerImpl::doInitialization(Module &M) { |
| 322 | bool Changed = false; |
| 323 | |
| 324 | dumpArguments(); |
| 325 | dumpPasses(); |
| 326 | |
| 327 | for (ImmutablePass *ImPass : getImmutablePasses()) |
| 328 | Changed |= ImPass->doInitialization(M); |
| 329 | |
| 330 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 331 | Changed |= getContainedManager(Index)->doInitialization(M); |
| 332 | |
| 333 | return Changed; |
| 334 | } |
| 335 | |
| 336 | bool FunctionPassManagerImpl::doFinalization(Module &M) { |
| 337 | bool Changed = false; |
| 338 | |
| 339 | for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index) |
| 340 | Changed |= getContainedManager(Index)->doFinalization(M); |
| 341 | |
| 342 | for (ImmutablePass *ImPass : getImmutablePasses()) |
| 343 | Changed |= ImPass->doFinalization(M); |
| 344 | |
| 345 | return Changed; |
| 346 | } |
| 347 | |
| 348 | void FunctionPassManagerImpl::releaseMemoryOnTheFly() { |
| 349 | if (!wasRun) |
| 350 | return; |
| 351 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 352 | FPPassManager *FPPM = getContainedManager(Index); |
| 353 | for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) { |
| 354 | FPPM->getContainedPass(Index)->releaseMemory(); |
| 355 | } |
| 356 | } |
| 357 | wasRun = false; |
| 358 | } |
| 359 | |
| 360 | // Execute all the passes managed by this top level manager. |
| 361 | // Return true if any function is modified by a pass. |
| 362 | bool FunctionPassManagerImpl::run(Function &F) { |
| 363 | bool Changed = false; |
| 364 | |
| 365 | initializeAllAnalysisInfo(); |
| 366 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 367 | Changed |= getContainedManager(Index)->runOnFunction(F); |
| 368 | F.getContext().yield(); |
| 369 | } |
| 370 | |
| 371 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 372 | getContainedManager(Index)->cleanup(); |
| 373 | |
| 374 | wasRun = true; |
| 375 | return Changed; |
| 376 | } |
Simon Pilgrim | e2e161f | 2020-06-25 17:48:29 +0100 | [diff] [blame] | 377 | } // namespace legacy |
| 378 | } // namespace llvm |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 379 | |
Chandler Carruth | 0863273 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 380 | namespace { |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 381 | //===----------------------------------------------------------------------===// |
| 382 | // MPPassManager |
| 383 | // |
| 384 | /// MPPassManager manages ModulePasses and function pass managers. |
Dan Gohman | f1877e3 | 2008-03-11 16:18:48 +0000 | [diff] [blame] | 385 | /// It batches all Module passes and function pass managers together and |
| 386 | /// sequences them to process one module. |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 387 | class MPPassManager : public Pass, public PMDataManager { |
Devang Patel | f1d1a20 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 388 | public: |
Devang Patel | 4b40954 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 389 | static char ID; |
Andrew Trick | 87862d1 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 390 | explicit MPPassManager() : |
| 391 | Pass(PT_PassManager, ID), PMDataManager() { } |
Devang Patel | 4bf9ac7 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 392 | |
| 393 | // Delete on the fly managers. |
Alexander Kornienko | e420633 | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 394 | ~MPPassManager() override { |
Yaron Keren | 84b5542 | 2015-06-05 17:48:47 +0000 | [diff] [blame] | 395 | for (auto &OnTheFlyManager : OnTheFlyManagers) { |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 396 | legacy::FunctionPassManagerImpl *FPP = OnTheFlyManager.second; |
Devang Patel | 4bf9ac7 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 397 | delete FPP; |
| 398 | } |
| 399 | } |
| 400 | |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 401 | /// createPrinterPass - Get a module printer pass. |
Craig Topper | 33a2798 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 402 | Pass *createPrinterPass(raw_ostream &O, |
| 403 | const std::string &Banner) const override { |
Chandler Carruth | 394cfcd | 2014-01-12 11:30:46 +0000 | [diff] [blame] | 404 | return createPrintModulePass(O, Banner); |
David Greene | c63505d | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Devang Patel | f1d1a20 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 407 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 408 | /// whether any of the passes modifies the module, and if so, return true. |
| 409 | bool runOnModule(Module &M); |
Devang Patel | 0ceb6c7 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 410 | |
Pedro Artigas | f9016b3 | 2012-12-03 21:56:57 +0000 | [diff] [blame] | 411 | using llvm::Pass::doInitialization; |
| 412 | using llvm::Pass::doFinalization; |
| 413 | |
Devang Patel | 6114e60 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 414 | /// Pass Manager itself does not invalidate any analysis info. |
Craig Topper | 33a2798 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 415 | void getAnalysisUsage(AnalysisUsage &Info) const override { |
Devang Patel | 6114e60 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 416 | Info.setPreservesAll(); |
| 417 | } |
| 418 | |
Devang Patel | 293bbe3 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 419 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 420 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 421 | /// through getAnalysis interface. |
Craig Topper | 33a2798 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 422 | void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override; |
Devang Patel | 293bbe3 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 423 | |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 424 | /// Return function pass corresponding to PassInfo PI, that is |
Devang Patel | 654393f | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 425 | /// required by module pass MP. Instantiate analysis pass, by using |
| 426 | /// its runOnFunction() for function F. |
serge-sans-paille | eb62e37 | 2020-06-26 13:07:31 +0200 | [diff] [blame] | 427 | std::tuple<Pass *, bool> getOnTheFlyPass(Pass *MP, AnalysisID PI, |
| 428 | Function &F) override; |
Devang Patel | 654393f | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 429 | |
Mehdi Amini | 0fba8cc | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 430 | StringRef getPassName() const override { return "Module Pass Manager"; } |
Devang Patel | 9cf5faf | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 431 | |
Craig Topper | 33a2798 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 432 | PMDataManager *getAsPMDataManager() override { return this; } |
| 433 | Pass *getAsPass() override { return this; } |
Chris Lattner | 8db741c | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 434 | |
Devang Patel | 4799abe | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 435 | // Print passes managed by this manager |
Craig Topper | 33a2798 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 436 | void dumpPassStructure(unsigned Offset) override { |
Eric Christopher | f2fe002 | 2014-02-26 23:27:16 +0000 | [diff] [blame] | 437 | dbgs().indent(Offset*2) << "ModulePass Manager\n"; |
Devang Patel | a8ecac4 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 438 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 439 | ModulePass *MP = getContainedPass(Index); |
Dan Gohman | 17cd574 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 440 | MP->dumpPassStructure(Offset + 1); |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 441 | MapVector<Pass *, legacy::FunctionPassManagerImpl *>::const_iterator I = |
Florian Hahn | 14a9414 | 2018-05-24 21:33:17 +0000 | [diff] [blame] | 442 | OnTheFlyManagers.find(MP); |
Dan Gohman | c3b6752 | 2009-07-01 23:12:33 +0000 | [diff] [blame] | 443 | if (I != OnTheFlyManagers.end()) |
| 444 | I->second->dumpPassStructure(Offset + 2); |
Devang Patel | a8ecac4 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 445 | dumpLastUses(MP, Offset+1); |
Devang Patel | 4799abe | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Devang Patel | a8ecac4 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 449 | ModulePass *getContainedPass(unsigned N) { |
Evan Cheng | eea82f5 | 2012-11-13 02:56:38 +0000 | [diff] [blame] | 450 | assert(N < PassVector.size() && "Pass number out of range!"); |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 451 | return static_cast<ModulePass *>(PassVector[N]); |
Devang Patel | a8ecac4 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Craig Topper | 33a2798 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 454 | PassManagerType getPassManagerType() const override { |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 455 | return PMT_ModulePassManager; |
Devang Patel | 5848944 | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 456 | } |
Devang Patel | 654393f | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 457 | |
| 458 | private: |
| 459 | /// Collection of on the fly FPPassManagers. These managers manage |
| 460 | /// function passes that are required by module passes. |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 461 | MapVector<Pass *, legacy::FunctionPassManagerImpl *> OnTheFlyManagers; |
Devang Patel | f1d1a20 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 462 | }; |
| 463 | |
Devang Patel | 4b40954 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 464 | char MPPassManager::ID = 0; |
Chandler Carruth | 0863273 | 2013-11-09 12:26:54 +0000 | [diff] [blame] | 465 | } // End anonymous namespace |
| 466 | |
| 467 | namespace llvm { |
| 468 | namespace legacy { |
Devang Patel | 9a5447b | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 469 | //===----------------------------------------------------------------------===// |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 470 | // PassManagerImpl |
Devang Patel | 9a5447b | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 471 | // |
Devang Patel | 920f1e4 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 472 | |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 473 | /// PassManagerImpl manages MPPassManagers |
| 474 | class PassManagerImpl : public Pass, |
Devang Patel | afd1979 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 475 | public PMDataManager, |
| 476 | public PMTopLevelManager { |
David Blaikie | cd738cc | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 477 | virtual void anchor(); |
Devang Patel | 5d72ec7 | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 478 | |
| 479 | public: |
Devang Patel | 4b40954 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 480 | static char ID; |
Andrew Trick | 87862d1 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 481 | explicit PassManagerImpl() : |
| 482 | Pass(PT_PassManager, ID), PMDataManager(), |
| 483 | PMTopLevelManager(new MPPassManager()) {} |
Devang Patel | 3ab2670 | 2006-12-07 23:24:58 +0000 | [diff] [blame] | 484 | |
Matthias Braun | 01450ac | 2014-12-12 01:27:01 +0000 | [diff] [blame] | 485 | /// \copydoc PassManager::add() |
Devang Patel | 8f425cd | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 486 | void add(Pass *P) { |
Devang Patel | 19b9d40 | 2006-12-08 22:34:02 +0000 | [diff] [blame] | 487 | schedulePass(P); |
Devang Patel | 8f425cd | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 488 | } |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 489 | |
| 490 | /// createPrinterPass - Get a module printer pass. |
Craig Topper | 33a2798 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 491 | Pass *createPrinterPass(raw_ostream &O, |
| 492 | const std::string &Banner) const override { |
Chandler Carruth | 394cfcd | 2014-01-12 11:30:46 +0000 | [diff] [blame] | 493 | return createPrintModulePass(O, Banner); |
David Greene | c63505d | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Devang Patel | 5d72ec7 | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 496 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 497 | /// whether any of the passes modifies the module, and if so, return true. |
| 498 | bool run(Module &M); |
| 499 | |
Pedro Artigas | f9016b3 | 2012-12-03 21:56:57 +0000 | [diff] [blame] | 500 | using llvm::Pass::doInitialization; |
| 501 | using llvm::Pass::doFinalization; |
| 502 | |
Devang Patel | 6114e60 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 503 | /// Pass Manager itself does not invalidate any analysis info. |
Craig Topper | 33a2798 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 504 | void getAnalysisUsage(AnalysisUsage &Info) const override { |
Devang Patel | 6114e60 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 505 | Info.setPreservesAll(); |
| 506 | } |
| 507 | |
Craig Topper | 33a2798 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 508 | PMDataManager *getAsPMDataManager() override { return this; } |
| 509 | Pass *getAsPass() override { return this; } |
| 510 | PassManagerType getTopLevelPassManagerType() override { |
Andrew Trick | 5aeec47 | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 511 | return PMT_ModulePassManager; |
| 512 | } |
Chris Lattner | 8db741c | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 513 | |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 514 | MPPassManager *getContainedManager(unsigned N) { |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 515 | assert(N < PassManagers.size() && "Pass number out of range!"); |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 516 | MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]); |
| 517 | return MP; |
| 518 | } |
Devang Patel | 5d72ec7 | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 519 | }; |
| 520 | |
David Blaikie | cd738cc | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 521 | void PassManagerImpl::anchor() {} |
| 522 | |
Devang Patel | 4b40954 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 523 | char PassManagerImpl::ID = 0; |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 524 | |
| 525 | //===----------------------------------------------------------------------===// |
| 526 | // PassManagerImpl implementation |
| 527 | |
| 528 | // |
| 529 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 530 | /// whether any of the passes modifies the module, and if so, return true. |
| 531 | bool PassManagerImpl::run(Module &M) { |
| 532 | bool Changed = false; |
| 533 | |
| 534 | dumpArguments(); |
| 535 | dumpPasses(); |
| 536 | |
| 537 | for (ImmutablePass *ImPass : getImmutablePasses()) |
| 538 | Changed |= ImPass->doInitialization(M); |
| 539 | |
| 540 | initializeAllAnalysisInfo(); |
| 541 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 542 | Changed |= getContainedManager(Index)->runOnModule(M); |
| 543 | M.getContext().yield(); |
| 544 | } |
| 545 | |
| 546 | for (ImmutablePass *ImPass : getImmutablePasses()) |
| 547 | Changed |= ImPass->doFinalization(M); |
| 548 | |
| 549 | return Changed; |
| 550 | } |
Simon Pilgrim | e2e161f | 2020-06-25 17:48:29 +0100 | [diff] [blame] | 551 | } // namespace legacy |
| 552 | } // namespace llvm |
Devang Patel | a825cd7 | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 553 | |
Devang Patel | dfcb74b | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 554 | //===----------------------------------------------------------------------===// |
Devang Patel | 9c4869d | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 555 | // PMTopLevelManager implementation |
| 556 | |
Devang Patel | 01e619b | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 557 | /// Initialize top level manager. Create first pass manager. |
Dan Gohman | cb1f2fb | 2010-08-16 21:38:42 +0000 | [diff] [blame] | 558 | PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) { |
| 559 | PMDM->setTopLevelManager(this); |
| 560 | addPassManager(PMDM); |
| 561 | activeStack.push(PMDM); |
Devang Patel | 01e619b | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Devang Patel | 9c4869d | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 564 | /// Set pass P as the last user of the given analysis passes. |
Dan Gohman | 1600dea | 2010-10-12 00:12:29 +0000 | [diff] [blame] | 565 | void |
Bill Wendling | 06f6f0c | 2012-05-14 07:53:40 +0000 | [diff] [blame] | 566 | PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) { |
Tobias Grosser | 196dae6 | 2011-01-20 21:03:22 +0000 | [diff] [blame] | 567 | unsigned PDepth = 0; |
| 568 | if (P->getResolver()) |
| 569 | PDepth = P->getResolver()->getPMDataManager().getDepth(); |
| 570 | |
Yaron Keren | 84b5542 | 2015-06-05 17:48:47 +0000 | [diff] [blame] | 571 | for (Pass *AP : AnalysisPasses) { |
Jay Foad | 7cacc7b | 2020-11-27 17:32:01 +0000 | [diff] [blame] | 572 | // Record P as the new last user of AP. |
| 573 | auto &LastUserOfAP = LastUser[AP]; |
| 574 | if (LastUserOfAP) |
| 575 | InversedLastUser[LastUserOfAP].erase(AP); |
| 576 | LastUserOfAP = P; |
| 577 | InversedLastUser[P].insert(AP); |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 578 | |
Devang Patel | 6e824ae | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 579 | if (P == AP) |
| 580 | continue; |
| 581 | |
Tobias Grosser | 196dae6 | 2011-01-20 21:03:22 +0000 | [diff] [blame] | 582 | // Update the last users of passes that are required transitive by AP. |
| 583 | AnalysisUsage *AnUsage = findAnalysisUsage(AP); |
| 584 | const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet(); |
| 585 | SmallVector<Pass *, 12> LastUses; |
| 586 | SmallVector<Pass *, 12> LastPMUses; |
Yaron Keren | 73b78f7 | 2016-04-28 14:49:44 +0000 | [diff] [blame] | 587 | for (AnalysisID ID : IDs) { |
| 588 | Pass *AnalysisPass = findAnalysisPass(ID); |
Tobias Grosser | 196dae6 | 2011-01-20 21:03:22 +0000 | [diff] [blame] | 589 | assert(AnalysisPass && "Expected analysis pass to exist."); |
| 590 | AnalysisResolver *AR = AnalysisPass->getResolver(); |
| 591 | assert(AR && "Expected analysis resolver to exist."); |
| 592 | unsigned APDepth = AR->getPMDataManager().getDepth(); |
| 593 | |
| 594 | if (PDepth == APDepth) |
| 595 | LastUses.push_back(AnalysisPass); |
| 596 | else if (PDepth > APDepth) |
| 597 | LastPMUses.push_back(AnalysisPass); |
| 598 | } |
| 599 | |
| 600 | setLastUser(LastUses, P); |
| 601 | |
| 602 | // If this pass has a corresponding pass manager, push higher level |
| 603 | // analysis to this pass manager. |
| 604 | if (P->getResolver()) |
| 605 | setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass()); |
| 606 | |
Devang Patel | 9c4869d | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 607 | // If AP is the last user of other passes then make P last user of |
| 608 | // such passes. |
Jay Foad | 7cacc7b | 2020-11-27 17:32:01 +0000 | [diff] [blame] | 609 | auto &LastUsedByAP = InversedLastUser[AP]; |
| 610 | for (Pass *L : LastUsedByAP) |
| 611 | LastUser[L] = P; |
| 612 | InversedLastUser[P].insert(LastUsedByAP.begin(), LastUsedByAP.end()); |
| 613 | LastUsedByAP.clear(); |
Devang Patel | 9c4869d | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 614 | } |
Devang Patel | 9c4869d | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /// Collect passes whose last user is P |
Dan Gohman | cb3e7f0 | 2010-10-12 00:11:18 +0000 | [diff] [blame] | 618 | void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses, |
Devang Patel | c50cf51 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 619 | Pass *P) { |
Jay Foad | aeb45e3 | 2020-11-27 17:34:35 +0000 | [diff] [blame] | 620 | auto DMI = InversedLastUser.find(P); |
Devang Patel | c50cf51 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 621 | if (DMI == InversedLastUser.end()) |
| 622 | return; |
| 623 | |
Jay Foad | aeb45e3 | 2020-11-27 17:34:35 +0000 | [diff] [blame] | 624 | auto &LU = DMI->second; |
| 625 | LastUses.append(LU.begin(), LU.end()); |
Devang Patel | 9c4869d | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Devang Patel | 8b80f4c | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 628 | AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) { |
Craig Topper | ea28548 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 629 | AnalysisUsage *AnUsage = nullptr; |
Philip Reames | e904269 | 2015-12-04 20:05:04 +0000 | [diff] [blame] | 630 | auto DMI = AnUsageMap.find(P); |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 631 | if (DMI != AnUsageMap.end()) |
Devang Patel | 8b80f4c | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 632 | AnUsage = DMI->second; |
| 633 | else { |
Philip Reames | e904269 | 2015-12-04 20:05:04 +0000 | [diff] [blame] | 634 | // Look up the analysis usage from the pass instance (different instances |
| 635 | // of the same pass can produce different results), but unique the |
| 636 | // resulting object to reduce memory usage. This helps to greatly reduce |
| 637 | // memory usage when we have many instances of only a few pass types |
| 638 | // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set |
| 639 | // of dependencies. |
| 640 | AnalysisUsage AU; |
| 641 | P->getAnalysisUsage(AU); |
Bjorn Pettersson | 9f4d872 | 2018-07-03 12:39:52 +0000 | [diff] [blame] | 642 | |
Philip Reames | e904269 | 2015-12-04 20:05:04 +0000 | [diff] [blame] | 643 | AUFoldingSetNode* Node = nullptr; |
| 644 | FoldingSetNodeID ID; |
| 645 | AUFoldingSetNode::Profile(ID, AU); |
| 646 | void *IP = nullptr; |
| 647 | if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP)) |
| 648 | Node = N; |
| 649 | else { |
Philip Reames | cbfe24e | 2015-12-04 23:48:19 +0000 | [diff] [blame] | 650 | Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU); |
Philip Reames | e904269 | 2015-12-04 20:05:04 +0000 | [diff] [blame] | 651 | UniqueAnalysisUsages.InsertNode(Node, IP); |
| 652 | } |
| 653 | assert(Node && "cached analysis usage must be non null"); |
| 654 | |
| 655 | AnUsageMap[P] = &Node->AU; |
Mandeep Singh Grang | 31affaa | 2017-06-06 05:08:36 +0000 | [diff] [blame] | 656 | AnUsage = &Node->AU; |
Devang Patel | 8b80f4c | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 657 | } |
| 658 | return AnUsage; |
| 659 | } |
| 660 | |
Devang Patel | 9c4869d | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 661 | /// Schedule pass P for execution. Make sure that passes required by |
| 662 | /// P are run before P is run. Update analysis info maintained by |
| 663 | /// the manager. Remove dead passes. This is a recursive function. |
| 664 | void PMTopLevelManager::schedulePass(Pass *P) { |
| 665 | |
Devang Patel | f02b251 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 666 | // TODO : Allocate function manager for this pass, other wise required set |
| 667 | // may be inserted into previous function manager |
Devang Patel | 9c4869d | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 668 | |
Devang Patel | 7a8aee9 | 2007-03-06 01:06:16 +0000 | [diff] [blame] | 669 | // Give pass a chance to prepare the stage. |
| 670 | P->preparePassManager(activeStack); |
| 671 | |
Devang Patel | a10b0b5 | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 672 | // If P is an analysis pass and it is available then do not |
| 673 | // generate the analysis again. Stale analysis info should not be |
| 674 | // available at this point. |
Chandler Carruth | 9b3892c | 2015-01-28 09:47:21 +0000 | [diff] [blame] | 675 | const PassInfo *PI = findAnalysisPassInfo(P->getPassID()); |
Owen Anderson | 140a03f | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 676 | if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) { |
Craig Topper | fe8f04a | 2018-08-20 20:57:30 +0000 | [diff] [blame] | 677 | // Remove any cached AnalysisUsage information. |
| 678 | AnUsageMap.erase(P); |
Nuno Lopes | 1a411d6 | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 679 | delete P; |
Devang Patel | 75446db | 2008-03-19 00:48:41 +0000 | [diff] [blame] | 680 | return; |
Nuno Lopes | 1a411d6 | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 681 | } |
Devang Patel | a10b0b5 | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 682 | |
Devang Patel | 8b80f4c | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 683 | AnalysisUsage *AnUsage = findAnalysisUsage(P); |
| 684 | |
Devang Patel | 1976d95 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 685 | bool checkAnalysis = true; |
| 686 | while (checkAnalysis) { |
| 687 | checkAnalysis = false; |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 688 | |
Devang Patel | 1976d95 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 689 | const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet(); |
Florian Hahn | fc3d9cb | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 690 | for (const AnalysisID ID : RequiredSet) { |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 691 | |
Florian Hahn | fc3d9cb | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 692 | Pass *AnalysisPass = findAnalysisPass(ID); |
Devang Patel | 1976d95 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 693 | if (!AnalysisPass) { |
Florian Hahn | fc3d9cb | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 694 | const PassInfo *PI = findAnalysisPassInfo(ID); |
Victor Oliveira | 1508faa | 2012-07-18 19:59:29 +0000 | [diff] [blame] | 695 | |
Craig Topper | ea28548 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 696 | if (!PI) { |
Victor Oliveira | 1508faa | 2012-07-18 19:59:29 +0000 | [diff] [blame] | 697 | // Pass P is not in the global PassRegistry |
| 698 | dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n"; |
| 699 | dbgs() << "Verify if there is a pass dependency cycle." << "\n"; |
| 700 | dbgs() << "Required Passes:" << "\n"; |
Florian Hahn | fc3d9cb | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 701 | for (const AnalysisID ID2 : RequiredSet) { |
| 702 | if (ID == ID2) |
| 703 | break; |
| 704 | Pass *AnalysisPass2 = findAnalysisPass(ID2); |
Victor Oliveira | 1508faa | 2012-07-18 19:59:29 +0000 | [diff] [blame] | 705 | if (AnalysisPass2) { |
| 706 | dbgs() << "\t" << AnalysisPass2->getPassName() << "\n"; |
Craig Topper | 5d993c5 | 2013-02-06 06:50:38 +0000 | [diff] [blame] | 707 | } else { |
Victor Oliveira | 1508faa | 2012-07-18 19:59:29 +0000 | [diff] [blame] | 708 | dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n"; |
| 709 | dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n"; |
| 710 | dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n"; |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | |
Andrew Trick | 46edcef | 2011-06-03 00:48:58 +0000 | [diff] [blame] | 715 | assert(PI && "Expected required passes to be initialized"); |
Owen Anderson | 140a03f | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 716 | AnalysisPass = PI->createPass(); |
Devang Patel | 1976d95 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 717 | if (P->getPotentialPassManagerType () == |
| 718 | AnalysisPass->getPotentialPassManagerType()) |
| 719 | // Schedule analysis pass that is managed by the same pass manager. |
| 720 | schedulePass(AnalysisPass); |
| 721 | else if (P->getPotentialPassManagerType () > |
| 722 | AnalysisPass->getPotentialPassManagerType()) { |
| 723 | // Schedule analysis pass that is managed by a new manager. |
| 724 | schedulePass(AnalysisPass); |
Dan Gohman | 64a4487 | 2010-08-16 22:57:28 +0000 | [diff] [blame] | 725 | // Recheck analysis passes to ensure that required analyses that |
Devang Patel | 1976d95 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 726 | // are already checked are still available. |
| 727 | checkAnalysis = true; |
Craig Topper | 5d993c5 | 2013-02-06 06:50:38 +0000 | [diff] [blame] | 728 | } else |
Chad Rosier | 5f96aa8 | 2015-03-20 15:45:14 +0000 | [diff] [blame] | 729 | // Do not schedule this analysis. Lower level analysis |
Devang Patel | 1976d95 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 730 | // passes are run on the fly. |
| 731 | delete AnalysisPass; |
| 732 | } |
Devang Patel | 9c4869d | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
| 736 | // Now all required passes are available. |
Andrew Trick | 5aeec47 | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 737 | if (ImmutablePass *IP = P->getAsImmutablePass()) { |
| 738 | // P is a immutable pass and it will be managed by this |
| 739 | // top level manager. Set up analysis resolver to connect them. |
| 740 | PMDataManager *DM = getAsPMDataManager(); |
| 741 | AnalysisResolver *AR = new AnalysisResolver(*DM); |
| 742 | P->setResolver(AR); |
| 743 | DM->initializeAnalysisImpl(P); |
| 744 | addImmutablePass(IP); |
| 745 | DM->recordAvailableAnalysis(IP); |
| 746 | return; |
| 747 | } |
| 748 | |
Fedor Sergeev | b8588f8 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 749 | if (PI && !PI->isAnalysis() && shouldPrintBeforePass(PI->getPassArgument())) { |
Nicolas Guillemot | eab6fe94 | 2021-02-25 11:56:48 -0800 | [diff] [blame] | 750 | Pass *PP = |
| 751 | P->createPrinterPass(dbgs(), ("*** IR Dump Before " + P->getPassName() + |
| 752 | " (" + PI->getPassArgument() + ") ***") |
| 753 | .str()); |
Andrew Trick | 5aeec47 | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 754 | PP->assignPassManager(activeStack, getTopLevelPassManagerType()); |
| 755 | } |
| 756 | |
| 757 | // Add the requested pass to the best available pass manager. |
| 758 | P->assignPassManager(activeStack, getTopLevelPassManagerType()); |
| 759 | |
Fedor Sergeev | b8588f8 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 760 | if (PI && !PI->isAnalysis() && shouldPrintAfterPass(PI->getPassArgument())) { |
Nicolas Guillemot | eab6fe94 | 2021-02-25 11:56:48 -0800 | [diff] [blame] | 761 | Pass *PP = |
| 762 | P->createPrinterPass(dbgs(), ("*** IR Dump After " + P->getPassName() + |
| 763 | " (" + PI->getPassArgument() + ") ***") |
| 764 | .str()); |
Andrew Trick | 5aeec47 | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 765 | PP->assignPassManager(activeStack, getTopLevelPassManagerType()); |
| 766 | } |
Devang Patel | 9c4869d | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /// Find the pass that implements Analysis AID. Search immutable |
| 770 | /// passes and all pass managers. If desired pass is not found |
| 771 | /// then return NULL. |
| 772 | Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { |
Chandler Carruth | 685efb3 | 2015-09-10 02:31:42 +0000 | [diff] [blame] | 773 | // For immutable passes we have a direct mapping from ID to pass, so check |
| 774 | // that first. |
| 775 | if (Pass *P = ImmutablePassMap.lookup(AID)) |
| 776 | return P; |
Devang Patel | 9c4869d | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 777 | |
Devang Patel | 857dfd3 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 778 | // Check pass managers |
Yaron Keren | 84b5542 | 2015-06-05 17:48:47 +0000 | [diff] [blame] | 779 | for (PMDataManager *PassManager : PassManagers) |
| 780 | if (Pass *P = PassManager->findAnalysisPass(AID, false)) |
Dan Gohman | bf58859 | 2010-10-11 23:19:01 +0000 | [diff] [blame] | 781 | return P; |
Devang Patel | 857dfd3 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 782 | |
| 783 | // Check other pass managers |
Yaron Keren | 84b5542 | 2015-06-05 17:48:47 +0000 | [diff] [blame] | 784 | for (PMDataManager *IndirectPassManager : IndirectPassManagers) |
| 785 | if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false)) |
Dan Gohman | bf58859 | 2010-10-11 23:19:01 +0000 | [diff] [blame] | 786 | return P; |
Devang Patel | 857dfd3 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 787 | |
Craig Topper | ea28548 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 788 | return nullptr; |
Devang Patel | 9c4869d | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Chandler Carruth | 9b3892c | 2015-01-28 09:47:21 +0000 | [diff] [blame] | 791 | const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const { |
| 792 | const PassInfo *&PI = AnalysisPassInfos[AID]; |
| 793 | if (!PI) |
| 794 | PI = PassRegistry::getPassRegistry()->getPassInfo(AID); |
| 795 | else |
| 796 | assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) && |
| 797 | "The pass info pointer changed for an analysis ID!"); |
| 798 | |
| 799 | return PI; |
| 800 | } |
| 801 | |
Chandler Carruth | 685efb3 | 2015-09-10 02:31:42 +0000 | [diff] [blame] | 802 | void PMTopLevelManager::addImmutablePass(ImmutablePass *P) { |
| 803 | P->initializePass(); |
| 804 | ImmutablePasses.push_back(P); |
| 805 | |
| 806 | // Add this pass to the map from its analysis ID. We clobber any prior runs |
| 807 | // of the pass in the map so that the last one added is the one found when |
| 808 | // doing lookups. |
| 809 | AnalysisID AID = P->getPassID(); |
| 810 | ImmutablePassMap[AID] = P; |
| 811 | |
| 812 | // Also add any interfaces implemented by the immutable pass to the map for |
| 813 | // fast lookup. |
| 814 | const PassInfo *PassInf = findAnalysisPassInfo(AID); |
| 815 | assert(PassInf && "Expected all immutable passes to be initialized"); |
Chandler Carruth | 80bd6db | 2015-09-10 04:22:36 +0000 | [diff] [blame] | 816 | for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented()) |
Chandler Carruth | 685efb3 | 2015-09-10 02:31:42 +0000 | [diff] [blame] | 817 | ImmutablePassMap[ImmPI->getTypeInfo()] = P; |
| 818 | } |
| 819 | |
Devang Patel | 4799abe | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 820 | // Print passes managed by this top level manager. |
Devang Patel | 5ac21a5 | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 821 | void PMTopLevelManager::dumpPasses() const { |
Devang Patel | 4799abe | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 822 | |
Andrew Trick | 61cd3a6 | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 823 | if (PassDebugging < Structure) |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 824 | return; |
| 825 | |
Devang Patel | 4799abe | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 826 | // Print out the immutable passes |
| 827 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { |
Dan Gohman | 17cd574 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 828 | ImmutablePasses[i]->dumpPassStructure(0); |
Devang Patel | 4799abe | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 829 | } |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 830 | |
Dan Gohman | 17cd574 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 831 | // Every class that derives from PMDataManager also derives from Pass |
| 832 | // (sometimes indirectly), but there's no inheritance relationship |
| 833 | // between PMDataManager and Pass, so we have to getAsPass to get |
| 834 | // from a PMDataManager* to a Pass*. |
Yaron Keren | 84b5542 | 2015-06-05 17:48:47 +0000 | [diff] [blame] | 835 | for (PMDataManager *Manager : PassManagers) |
| 836 | Manager->getAsPass()->dumpPassStructure(1); |
Devang Patel | 4799abe | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Devang Patel | 5ac21a5 | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 839 | void PMTopLevelManager::dumpArguments() const { |
Devang Patel | 57f52ec | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 840 | |
Andrew Trick | 61cd3a6 | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 841 | if (PassDebugging < Arguments) |
Devang Patel | 57f52ec | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 842 | return; |
| 843 | |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 844 | dbgs() << "Pass Arguments: "; |
Yaron Keren | 73b78f7 | 2016-04-28 14:49:44 +0000 | [diff] [blame] | 845 | for (ImmutablePass *P : ImmutablePasses) |
| 846 | if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) { |
Andrew Trick | 46edcef | 2011-06-03 00:48:58 +0000 | [diff] [blame] | 847 | assert(PI && "Expected all immutable passes to be initialized"); |
Dan Gohman | 04355c0 | 2010-11-11 16:32:17 +0000 | [diff] [blame] | 848 | if (!PI->isAnalysisGroup()) |
| 849 | dbgs() << " -" << PI->getPassArgument(); |
Andrew Trick | 46edcef | 2011-06-03 00:48:58 +0000 | [diff] [blame] | 850 | } |
Yaron Keren | 73b78f7 | 2016-04-28 14:49:44 +0000 | [diff] [blame] | 851 | for (PMDataManager *PM : PassManagers) |
| 852 | PM->dumpPassArguments(); |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 853 | dbgs() << "\n"; |
Devang Patel | 57f52ec | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Devang Patel | 5cf8b0a | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 856 | void PMTopLevelManager::initializeAllAnalysisInfo() { |
Yaron Keren | 73b78f7 | 2016-04-28 14:49:44 +0000 | [diff] [blame] | 857 | for (PMDataManager *PM : PassManagers) |
| 858 | PM->initializeAnalysisInfo(); |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 859 | |
Devang Patel | 5cf8b0a | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 860 | // Initailize other pass managers |
Yaron Keren | 73b78f7 | 2016-04-28 14:49:44 +0000 | [diff] [blame] | 861 | for (PMDataManager *IPM : IndirectPassManagers) |
| 862 | IPM->initializeAnalysisInfo(); |
Devang Patel | 5cf8b0a | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Devang Patel | d023bb4 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 865 | /// Destructor |
| 866 | PMTopLevelManager::~PMTopLevelManager() { |
Yaron Keren | 73b78f7 | 2016-04-28 14:49:44 +0000 | [diff] [blame] | 867 | for (PMDataManager *PM : PassManagers) |
| 868 | delete PM; |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 869 | |
Yaron Keren | 73b78f7 | 2016-04-28 14:49:44 +0000 | [diff] [blame] | 870 | for (ImmutablePass *P : ImmutablePasses) |
| 871 | delete P; |
Devang Patel | d023bb4 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Devang Patel | 9c4869d | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 874 | //===----------------------------------------------------------------------===// |
Devang Patel | 12532f9 | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 875 | // PMDataManager implementation |
Devang Patel | 30781bb | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 876 | |
Devang Patel | 77ffe13 | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 877 | /// Augement AvailableAnalysis by adding analysis made available by pass P. |
Devang Patel | e7879fa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 878 | void PMDataManager::recordAvailableAnalysis(Pass *P) { |
Owen Anderson | 140a03f | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 879 | AnalysisID PI = P->getPassID(); |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 880 | |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 881 | AvailableAnalysis[PI] = P; |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 882 | |
Dan Gohman | 069b197 | 2010-08-12 23:46:28 +0000 | [diff] [blame] | 883 | assert(!AvailableAnalysis.empty()); |
Devang Patel | 77ffe13 | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 884 | |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 885 | // This pass is the current implementation of all of the interfaces it |
| 886 | // implements as well. |
Chandler Carruth | 9b3892c | 2015-01-28 09:47:21 +0000 | [diff] [blame] | 887 | const PassInfo *PInf = TPM->findAnalysisPassInfo(PI); |
Craig Topper | ea28548 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 888 | if (!PInf) return; |
Owen Anderson | 140a03f | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 889 | const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented(); |
Owen Anderson | ffe71b0 | 2010-07-20 16:55:05 +0000 | [diff] [blame] | 890 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
Owen Anderson | 140a03f | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 891 | AvailableAnalysis[II[i]->getTypeInfo()] = P; |
Devang Patel | 77ffe13 | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Devang Patel | e6be06a | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 894 | // Return true if P preserves high level analysis used by other |
| 895 | // passes managed by this manager |
| 896 | bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) { |
Devang Patel | 8b80f4c | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 897 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
Devang Patel | 8b80f4c | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 898 | if (AnUsage->getPreservesAll()) |
Devang Patel | e6be06a | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 899 | return true; |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 900 | |
Devang Patel | 8b80f4c | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 901 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Yaron Keren | 697cb17 | 2016-10-02 19:21:41 +0000 | [diff] [blame] | 902 | for (Pass *P1 : HigherLevelAnalysis) { |
Craig Topper | ea28548 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 903 | if (P1->getAsImmutablePass() == nullptr && |
David Majnemer | 6d50389 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 904 | !is_contained(PreservedSet, P1->getPassID())) |
Devang Patel | 6e824ae | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 905 | return false; |
Devang Patel | e6be06a | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 906 | } |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 907 | |
Devang Patel | e6be06a | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 908 | return true; |
| 909 | } |
| 910 | |
Chris Lattner | 092ad4c | 2008-08-07 07:34:50 +0000 | [diff] [blame] | 911 | /// verifyPreservedAnalysis -- Verify analysis preserved by pass P. |
Devang Patel | 667a813 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 912 | void PMDataManager::verifyPreservedAnalysis(Pass *P) { |
Chris Lattner | 092ad4c | 2008-08-07 07:34:50 +0000 | [diff] [blame] | 913 | // Don't do this unless assertions are enabled. |
| 914 | #ifdef NDEBUG |
| 915 | return; |
| 916 | #endif |
Devang Patel | 8b80f4c | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 917 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 918 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Devang Patel | 30781bb | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 919 | |
Devang Patel | f25ad21 | 2007-07-19 05:36:09 +0000 | [diff] [blame] | 920 | // Verify preserved analysis |
Yaron Keren | 697cb17 | 2016-10-02 19:21:41 +0000 | [diff] [blame] | 921 | for (AnalysisID AID : PreservedSet) { |
Dan Gohman | 79e9866 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 922 | if (Pass *AP = findAnalysisPass(AID, true)) { |
Chris Lattner | d7cf0bd | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 923 | TimeRegion PassTimer(getPassTimer(AP)); |
Devang Patel | 667a813 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 924 | AP->verifyAnalysis(); |
Dan Gohman | 79e9866 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 925 | } |
Devang Patel | b813759 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | |
Devang Patel | a75450f | 2008-07-01 19:50:56 +0000 | [diff] [blame] | 929 | /// Remove Analysis not preserved by Pass P |
Devang Patel | 667a813 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 930 | void PMDataManager::removeNotPreservedAnalysis(Pass *P) { |
Devang Patel | 8b80f4c | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 931 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 932 | if (AnUsage->getPreservesAll()) |
Devang Patel | bc45ee7 | 2006-12-07 20:03:49 +0000 | [diff] [blame] | 933 | return; |
| 934 | |
Devang Patel | 8b80f4c | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 935 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Michael Ilseman | d563bfe | 2013-02-26 01:31:59 +0000 | [diff] [blame] | 936 | for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), |
Devang Patel | adc9a14 | 2006-12-12 23:07:44 +0000 | [diff] [blame] | 937 | E = AvailableAnalysis.end(); I != E; ) { |
Michael Ilseman | d563bfe | 2013-02-26 01:31:59 +0000 | [diff] [blame] | 938 | DenseMap<AnalysisID, Pass*>::iterator Info = I++; |
Craig Topper | ea28548 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 939 | if (Info->second->getAsImmutablePass() == nullptr && |
David Majnemer | 6d50389 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 940 | !is_contained(PreservedSet, Info->first)) { |
Devang Patel | d877144 | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 941 | // Remove this analysis |
Andrew Trick | 61cd3a6 | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 942 | if (PassDebugging >= Details) { |
Devang Patel | 88e89b9 | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 943 | Pass *S = Info->second; |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 944 | dbgs() << " -- '" << P->getPassName() << "' is not preserving '"; |
| 945 | dbgs() << S->getPassName() << "'\n"; |
Devang Patel | 88e89b9 | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 946 | } |
Dan Gohman | 5b32efa | 2008-11-06 21:57:17 +0000 | [diff] [blame] | 947 | AvailableAnalysis.erase(Info); |
Devang Patel | 88e89b9 | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 948 | } |
Devang Patel | d877144 | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 949 | } |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 950 | |
Devang Patel | 8961251 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 951 | // Check inherited analysis also. If P is not preserving analysis |
| 952 | // provided by parent manager then remove it here. |
Kazu Hirata | 17bae6f | 2021-02-27 10:09:25 -0800 | [diff] [blame] | 953 | for (DenseMap<AnalysisID, Pass *> *IA : InheritedAnalysis) { |
| 954 | if (!IA) |
Devang Patel | 8961251 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 955 | continue; |
| 956 | |
Kazu Hirata | 17bae6f | 2021-02-27 10:09:25 -0800 | [diff] [blame] | 957 | for (DenseMap<AnalysisID, Pass *>::iterator I = IA->begin(), |
| 958 | E = IA->end(); |
| 959 | I != E;) { |
Michael Ilseman | d563bfe | 2013-02-26 01:31:59 +0000 | [diff] [blame] | 960 | DenseMap<AnalysisID, Pass *>::iterator Info = I++; |
Craig Topper | ea28548 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 961 | if (Info->second->getAsImmutablePass() == nullptr && |
David Majnemer | 6d50389 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 962 | !is_contained(PreservedSet, Info->first)) { |
Devang Patel | 8961251 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 963 | // Remove this analysis |
Andrew Trick | 61cd3a6 | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 964 | if (PassDebugging >= Details) { |
Andreas Neustifter | ec6fa4f | 2009-12-04 06:58:24 +0000 | [diff] [blame] | 965 | Pass *S = Info->second; |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 966 | dbgs() << " -- '" << P->getPassName() << "' is not preserving '"; |
| 967 | dbgs() << S->getPassName() << "'\n"; |
Andreas Neustifter | ec6fa4f | 2009-12-04 06:58:24 +0000 | [diff] [blame] | 968 | } |
Kazu Hirata | 17bae6f | 2021-02-27 10:09:25 -0800 | [diff] [blame] | 969 | IA->erase(Info); |
Andreas Neustifter | ec6fa4f | 2009-12-04 06:58:24 +0000 | [diff] [blame] | 970 | } |
Devang Patel | 8961251 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 971 | } |
| 972 | } |
Devang Patel | 30781bb | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Devang Patel | cdd9e46 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 975 | /// Remove analysis passes that are not used any longer |
Daniel Dunbar | ff88ed6 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 976 | void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg, |
Devang Patel | 7997966 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 977 | enum PassDebuggingString DBG_STR) { |
Devang Patel | bff6801 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 978 | |
Devang Patel | f5a14f2 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 979 | SmallVector<Pass *, 12> DeadPasses; |
Devang Patel | 654393f | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 980 | |
Devang Patel | 4bf9ac7 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 981 | // If this is a on the fly manager then it does not have TPM. |
Devang Patel | 654393f | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 982 | if (!TPM) |
| 983 | return; |
| 984 | |
Devang Patel | bff6801 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 985 | TPM->collectLastUses(DeadPasses, P); |
| 986 | |
Andrew Trick | 61cd3a6 | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 987 | if (PassDebugging >= Details && !DeadPasses.empty()) { |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 988 | dbgs() << " -*- '" << P->getPassName(); |
| 989 | dbgs() << "' is the last user of following pass instances."; |
| 990 | dbgs() << " Free these instances\n"; |
Evan Cheng | 8424e51 | 2008-06-04 09:13:31 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Yaron Keren | 697cb17 | 2016-10-02 19:21:41 +0000 | [diff] [blame] | 993 | for (Pass *P : DeadPasses) |
| 994 | freePass(P, Msg, DBG_STR); |
Dan Gohman | 0ac7ac8 | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 995 | } |
Devang Patel | 0208f74 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 996 | |
Daniel Dunbar | ff88ed6 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 997 | void PMDataManager::freePass(Pass *P, StringRef Msg, |
Dan Gohman | 0ac7ac8 | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 998 | enum PassDebuggingString DBG_STR) { |
| 999 | dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg); |
Devang Patel | 0208f74 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 1000 | |
Dan Gohman | 0ac7ac8 | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 1001 | { |
| 1002 | // If the pass crashes releasing memory, remember this. |
| 1003 | PassManagerPrettyStackEntry X(P); |
Chris Lattner | d7cf0bd | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1004 | TimeRegion PassTimer(getPassTimer(P)); |
| 1005 | |
Dan Gohman | 0ac7ac8 | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 1006 | P->releaseMemory(); |
Dan Gohman | 0ac7ac8 | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Owen Anderson | 140a03f | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1009 | AnalysisID PI = P->getPassID(); |
Chandler Carruth | 9b3892c | 2015-01-28 09:47:21 +0000 | [diff] [blame] | 1010 | if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) { |
Dan Gohman | 0ac7ac8 | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 1011 | // Remove the pass itself (if it is not already removed). |
| 1012 | AvailableAnalysis.erase(PI); |
| 1013 | |
| 1014 | // Remove all interfaces this pass implements, for which it is also |
| 1015 | // listed as the available implementation. |
Owen Anderson | 140a03f | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1016 | const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented(); |
Owen Anderson | ffe71b0 | 2010-07-20 16:55:05 +0000 | [diff] [blame] | 1017 | for (unsigned i = 0, e = II.size(); i != e; ++i) { |
Michael Ilseman | d563bfe | 2013-02-26 01:31:59 +0000 | [diff] [blame] | 1018 | DenseMap<AnalysisID, Pass*>::iterator Pos = |
Owen Anderson | 140a03f | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1019 | AvailableAnalysis.find(II[i]->getTypeInfo()); |
Dan Gohman | 0ac7ac8 | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 1020 | if (Pos != AvailableAnalysis.end() && Pos->second == P) |
Devang Patel | c196cc0 | 2008-10-06 20:36:36 +0000 | [diff] [blame] | 1021 | AvailableAnalysis.erase(Pos); |
Devang Patel | c196cc0 | 2008-10-06 20:36:36 +0000 | [diff] [blame] | 1022 | } |
Devang Patel | bff6801 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 1023 | } |
Devang Patel | cdd9e46 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1026 | /// Add pass P into the PassVector. Update |
Devang Patel | 6227611 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 1027 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1028 | void PMDataManager::add(Pass *P, bool ProcessAnalysis) { |
Devang Patel | c5a5286 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 1029 | // This manager is going to manage pass P. Set up analysis resolver |
| 1030 | // to connect them. |
Devang Patel | 59f1fd2 | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 1031 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | c5a5286 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 1032 | P->setResolver(AR); |
| 1033 | |
Devang Patel | 5d83b23 | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 1034 | // If a FunctionPass F is the last user of ModulePass info M |
| 1035 | // then the F's manager, not F, records itself as a last user of M. |
Devang Patel | f5a14f2 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 1036 | SmallVector<Pass *, 12> TransferLastUses; |
Devang Patel | 5d83b23 | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 1037 | |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1038 | if (!ProcessAnalysis) { |
| 1039 | // Add pass |
| 1040 | PassVector.push_back(P); |
| 1041 | return; |
Devang Patel | 6227611 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 1042 | } |
Devang Patel | 7950468 | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 1043 | |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1044 | // At the moment, this pass is the last user of all required passes. |
| 1045 | SmallVector<Pass *, 12> LastUses; |
Chandler Carruth | a8d247f | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1046 | SmallVector<Pass *, 8> UsedPasses; |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1047 | SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable; |
| 1048 | |
| 1049 | unsigned PDepth = this->getDepth(); |
| 1050 | |
Chandler Carruth | a8d247f | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1051 | collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P); |
| 1052 | for (Pass *PUsed : UsedPasses) { |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1053 | unsigned RDepth = 0; |
| 1054 | |
Chandler Carruth | a8d247f | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1055 | assert(PUsed->getResolver() && "Analysis Resolver is not set"); |
| 1056 | PMDataManager &DM = PUsed->getResolver()->getPMDataManager(); |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1057 | RDepth = DM.getDepth(); |
| 1058 | |
| 1059 | if (PDepth == RDepth) |
Chandler Carruth | a8d247f | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1060 | LastUses.push_back(PUsed); |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1061 | else if (PDepth > RDepth) { |
| 1062 | // Let the parent claim responsibility of last use |
Chandler Carruth | a8d247f | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1063 | TransferLastUses.push_back(PUsed); |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1064 | // Keep track of higher level analysis used by this manager. |
Chandler Carruth | a8d247f | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1065 | HigherLevelAnalysis.push_back(PUsed); |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1066 | } else |
Chandler Carruth | a8d247f | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1067 | llvm_unreachable("Unable to accommodate Used Pass"); |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | // Set P as P's last user until someone starts using P. |
| 1071 | // However, if P is a Pass Manager then it does not need |
| 1072 | // to record its last user. |
Craig Topper | ea28548 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1073 | if (!P->getAsPMDataManager()) |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1074 | LastUses.push_back(P); |
| 1075 | TPM->setLastUser(LastUses, P); |
| 1076 | |
| 1077 | if (!TransferLastUses.empty()) { |
Chris Lattner | 8db741c | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 1078 | Pass *My_PM = getAsPass(); |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1079 | TPM->setLastUser(TransferLastUses, My_PM); |
| 1080 | TransferLastUses.clear(); |
| 1081 | } |
| 1082 | |
Dan Gohman | 64a4487 | 2010-08-16 22:57:28 +0000 | [diff] [blame] | 1083 | // Now, take care of required analyses that are not available. |
Chandler Carruth | c390ac2 | 2015-08-18 18:41:53 +0000 | [diff] [blame] | 1084 | for (AnalysisID ID : ReqAnalysisNotAvailable) { |
| 1085 | const PassInfo *PI = TPM->findAnalysisPassInfo(ID); |
Owen Anderson | 140a03f | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1086 | Pass *AnalysisPass = PI->createPass(); |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1087 | this->addLowerLevelRequiredPass(P, AnalysisPass); |
| 1088 | } |
| 1089 | |
| 1090 | // Take a note of analysis required and made available by this pass. |
| 1091 | // Remove the analysis not preserved by this pass |
| 1092 | removeNotPreservedAnalysis(P); |
| 1093 | recordAvailableAnalysis(P); |
| 1094 | |
Devang Patel | 7950468 | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 1095 | // Add pass |
| 1096 | PassVector.push_back(P); |
Devang Patel | 7950468 | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Devang Patel | 293bbe3 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1099 | |
Chandler Carruth | a8d247f | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1100 | /// Populate UP with analysis pass that are used or required by |
Devang Patel | 293bbe3 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1101 | /// pass P and are available. Populate RP_NotAvail with analysis |
| 1102 | /// pass that are required by pass P but are not available. |
Chandler Carruth | a8d247f | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1103 | void PMDataManager::collectRequiredAndUsedAnalyses( |
| 1104 | SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail, |
| 1105 | Pass *P) { |
Devang Patel | 8b80f4c | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 1106 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
Chandler Carruth | a8d247f | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1107 | |
| 1108 | for (const auto &UsedID : AnUsage->getUsedSet()) |
| 1109 | if (Pass *AnalysisPass = findAnalysisPass(UsedID, true)) |
| 1110 | UP.push_back(AnalysisPass); |
| 1111 | |
Chandler Carruth | c390ac2 | 2015-08-18 18:41:53 +0000 | [diff] [blame] | 1112 | for (const auto &RequiredID : AnUsage->getRequiredSet()) |
| 1113 | if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true)) |
Chandler Carruth | a8d247f | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1114 | UP.push_back(AnalysisPass); |
Devang Patel | 293bbe3 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1115 | else |
Chandler Carruth | c390ac2 | 2015-08-18 18:41:53 +0000 | [diff] [blame] | 1116 | RP_NotAvail.push_back(RequiredID); |
Devang Patel | e56c458 | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Devang Patel | f72b728 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 1119 | // All Required analyses should be available to the pass as it runs! Here |
| 1120 | // we fill in the AnalysisImpls member of the pass so that it can |
| 1121 | // successfully use the getAnalysis() method to retrieve the |
| 1122 | // implementations it needs. |
| 1123 | // |
Devang Patel | 12532f9 | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 1124 | void PMDataManager::initializeAnalysisImpl(Pass *P) { |
Devang Patel | 8b80f4c | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 1125 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 1126 | |
Florian Hahn | fc3d9cb | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1127 | for (const AnalysisID ID : AnUsage->getRequiredSet()) { |
| 1128 | Pass *Impl = findAnalysisPass(ID, true); |
Craig Topper | ea28548 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1129 | if (!Impl) |
Devang Patel | aabe784 | 2007-04-16 20:44:16 +0000 | [diff] [blame] | 1130 | // This may be analysis pass that is initialized on the fly. |
| 1131 | // If that is not the case then it will raise an assert when it is used. |
| 1132 | continue; |
Devang Patel | 59f1fd2 | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 1133 | AnalysisResolver *AR = P->getResolver(); |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1134 | assert(AR && "Analysis Resolver is not set"); |
Florian Hahn | fc3d9cb | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1135 | AR->addAnalysisImplsPair(ID, Impl); |
Devang Patel | f72b728 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
Devang Patel | cad7675 | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 1139 | /// Find the pass that implements Analysis AID. If desired pass is not found |
| 1140 | /// then return NULL. |
| 1141 | Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) { |
| 1142 | |
| 1143 | // Check if AvailableAnalysis map has one entry. |
Michael Ilseman | d563bfe | 2013-02-26 01:31:59 +0000 | [diff] [blame] | 1144 | DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID); |
Devang Patel | cad7675 | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 1145 | |
| 1146 | if (I != AvailableAnalysis.end()) |
| 1147 | return I->second; |
| 1148 | |
| 1149 | // Search Parents through TopLevelManager |
| 1150 | if (SearchParent) |
| 1151 | return TPM->findAnalysisPass(AID); |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1152 | |
Craig Topper | ea28548 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1153 | return nullptr; |
Devang Patel | cad7675 | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Devang Patel | 5ac21a5 | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1156 | // Print list of passes that are last used by P. |
| 1157 | void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{ |
Jay Foad | 7cacc7b | 2020-11-27 17:32:01 +0000 | [diff] [blame] | 1158 | if (PassDebugging < Details) |
| 1159 | return; |
Devang Patel | 5ac21a5 | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1160 | |
Devang Patel | f5a14f2 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 1161 | SmallVector<Pass *, 12> LUses; |
Devang Patel | 4bf9ac7 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 1162 | |
| 1163 | // If this is a on the fly manager then it does not have TPM. |
| 1164 | if (!TPM) |
| 1165 | return; |
| 1166 | |
Devang Patel | 5ac21a5 | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1167 | TPM->collectLastUses(LUses, P); |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1168 | |
Florian Hahn | fc3d9cb | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1169 | for (Pass *P : LUses) { |
Eric Christopher | f2fe002 | 2014-02-26 23:27:16 +0000 | [diff] [blame] | 1170 | dbgs() << "--" << std::string(Offset*2, ' '); |
Florian Hahn | fc3d9cb | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1171 | P->dumpPassStructure(0); |
Devang Patel | 5ac21a5 | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | void PMDataManager::dumpPassArguments() const { |
Florian Hahn | fc3d9cb | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1176 | for (Pass *P : PassVector) { |
| 1177 | if (PMDataManager *PMD = P->getAsPMDataManager()) |
Devang Patel | 5ac21a5 | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1178 | PMD->dumpPassArguments(); |
| 1179 | else |
Owen Anderson | 140a03f | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1180 | if (const PassInfo *PI = |
Florian Hahn | fc3d9cb | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1181 | TPM->findAnalysisPassInfo(P->getPassID())) |
Devang Patel | 5ac21a5 | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1182 | if (!PI->isAnalysisGroup()) |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1183 | dbgs() << " -" << PI->getPassArgument(); |
Devang Patel | 5ac21a5 | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | |
Chris Lattner | b5c40f6 | 2007-08-10 06:17:04 +0000 | [diff] [blame] | 1187 | void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1, |
| 1188 | enum PassDebuggingString S2, |
Daniel Dunbar | ff88ed6 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 1189 | StringRef Msg) { |
Andrew Trick | 61cd3a6 | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 1190 | if (PassDebugging < Executions) |
Devang Patel | 5ac21a5 | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1191 | return; |
Pavel Labath | 64540e6 | 2016-10-25 16:20:07 +0000 | [diff] [blame] | 1192 | dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this |
Chandler Carruth | 91d1e0b | 2014-04-27 23:59:25 +0000 | [diff] [blame] | 1193 | << std::string(getDepth() * 2 + 1, ' '); |
Devang Patel | 7997966 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1194 | switch (S1) { |
| 1195 | case EXECUTION_MSG: |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1196 | dbgs() << "Executing Pass '" << P->getPassName(); |
Devang Patel | 7997966 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1197 | break; |
| 1198 | case MODIFICATION_MSG: |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1199 | dbgs() << "Made Modification '" << P->getPassName(); |
Devang Patel | 7997966 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1200 | break; |
| 1201 | case FREEING_MSG: |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1202 | dbgs() << " Freeing Pass '" << P->getPassName(); |
Devang Patel | 7997966 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1203 | break; |
| 1204 | default: |
| 1205 | break; |
| 1206 | } |
| 1207 | switch (S2) { |
Devang Patel | 7997966 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1208 | case ON_FUNCTION_MSG: |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1209 | dbgs() << "' on Function '" << Msg << "'...\n"; |
Devang Patel | 7997966 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1210 | break; |
| 1211 | case ON_MODULE_MSG: |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1212 | dbgs() << "' on Module '" << Msg << "'...\n"; |
Devang Patel | 7997966 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1213 | break; |
Tobias Grosser | c3206e4 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 1214 | case ON_REGION_MSG: |
| 1215 | dbgs() << "' on Region '" << Msg << "'...\n"; |
| 1216 | break; |
Devang Patel | 7997966 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1217 | case ON_LOOP_MSG: |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1218 | dbgs() << "' on Loop '" << Msg << "'...\n"; |
Devang Patel | 7997966 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1219 | break; |
| 1220 | case ON_CG_MSG: |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1221 | dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n"; |
Devang Patel | 7997966 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1222 | break; |
| 1223 | default: |
| 1224 | break; |
| 1225 | } |
Devang Patel | 5ac21a5 | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1228 | void PMDataManager::dumpRequiredSet(const Pass *P) const { |
Andrew Trick | 61cd3a6 | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 1229 | if (PassDebugging < Details) |
Chris Lattner | de371f3 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1230 | return; |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1231 | |
Chris Lattner | de371f3 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1232 | AnalysisUsage analysisUsage; |
| 1233 | P->getAnalysisUsage(analysisUsage); |
| 1234 | dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet()); |
| 1235 | } |
| 1236 | |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1237 | void PMDataManager::dumpPreservedSet(const Pass *P) const { |
Andrew Trick | 61cd3a6 | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 1238 | if (PassDebugging < Details) |
Chris Lattner | de371f3 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1239 | return; |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1240 | |
Chris Lattner | de371f3 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1241 | AnalysisUsage analysisUsage; |
| 1242 | P->getAnalysisUsage(analysisUsage); |
| 1243 | dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet()); |
| 1244 | } |
| 1245 | |
Chandler Carruth | a8d247f | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1246 | void PMDataManager::dumpUsedSet(const Pass *P) const { |
| 1247 | if (PassDebugging < Details) |
| 1248 | return; |
| 1249 | |
| 1250 | AnalysisUsage analysisUsage; |
| 1251 | P->getAnalysisUsage(analysisUsage); |
| 1252 | dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet()); |
| 1253 | } |
| 1254 | |
Daniel Dunbar | ff88ed6 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 1255 | void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P, |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1256 | const AnalysisUsage::VectorType &Set) const { |
Andrew Trick | 61cd3a6 | 2013-09-19 06:02:43 +0000 | [diff] [blame] | 1257 | assert(PassDebugging >= Details); |
Chris Lattner | de371f3 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1258 | if (Set.empty()) |
| 1259 | return; |
Roman Divacky | 9696b86 | 2012-09-05 22:26:57 +0000 | [diff] [blame] | 1260 | dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:"; |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1261 | for (unsigned i = 0; i != Set.size(); ++i) { |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1262 | if (i) dbgs() << ','; |
Chandler Carruth | 9b3892c | 2015-01-28 09:47:21 +0000 | [diff] [blame] | 1263 | const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]); |
Andrew Trick | 46edcef | 2011-06-03 00:48:58 +0000 | [diff] [blame] | 1264 | if (!PInf) { |
| 1265 | // Some preserved passes, such as AliasAnalysis, may not be initialized by |
| 1266 | // all drivers. |
| 1267 | dbgs() << " Uninitialized Pass"; |
| 1268 | continue; |
| 1269 | } |
Owen Anderson | 140a03f | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1270 | dbgs() << ' ' << PInf->getPassName(); |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1271 | } |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1272 | dbgs() << '\n'; |
Devang Patel | 5ac21a5 | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1273 | } |
Devang Patel | 2d708df | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1274 | |
Devang Patel | 8ee0ca4 | 2007-07-27 20:06:09 +0000 | [diff] [blame] | 1275 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 1276 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 1277 | /// through getAnalysis interface. |
| 1278 | /// This should be handled by specific pass manager. |
| 1279 | void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { |
| 1280 | if (TPM) { |
| 1281 | TPM->dumpArguments(); |
| 1282 | TPM->dumpPasses(); |
| 1283 | } |
Devang Patel | b507af7 | 2008-02-02 01:43:30 +0000 | [diff] [blame] | 1284 | |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1285 | // Module Level pass may required Function Level analysis info |
| 1286 | // (e.g. dominator info). Pass manager uses on the fly function pass manager |
| 1287 | // to provide this on demand. In that case, in Pass manager terminology, |
Devang Patel | b507af7 | 2008-02-02 01:43:30 +0000 | [diff] [blame] | 1288 | // module level pass is requiring lower level analysis info managed by |
| 1289 | // lower level pass manager. |
| 1290 | |
| 1291 | // When Pass manager is not able to order required analysis info, Pass manager |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1292 | // checks whether any lower level manager will be able to provide this |
Devang Patel | b507af7 | 2008-02-02 01:43:30 +0000 | [diff] [blame] | 1293 | // analysis info on demand or not. |
Devang Patel | e278d4e | 2008-06-03 01:20:02 +0000 | [diff] [blame] | 1294 | #ifndef NDEBUG |
David Greene | e008110 | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1295 | dbgs() << "Unable to schedule '" << RequiredPass->getPassName(); |
| 1296 | dbgs() << "' required by '" << P->getPassName() << "'\n"; |
Devang Patel | e278d4e | 2008-06-03 01:20:02 +0000 | [diff] [blame] | 1297 | #endif |
Torok Edwin | d129512 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1298 | llvm_unreachable("Unable to schedule pass"); |
Devang Patel | 8ee0ca4 | 2007-07-27 20:06:09 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
serge-sans-paille | eb62e37 | 2020-06-26 13:07:31 +0200 | [diff] [blame] | 1301 | std::tuple<Pass *, bool> PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, |
| 1302 | Function &F) { |
Craig Topper | 4d04c04 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 1303 | llvm_unreachable("Unable to find on the fly pass"); |
Dan Gohman | dcc2120 | 2010-06-21 18:46:45 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Devang Patel | d023bb4 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1306 | // Destructor |
| 1307 | PMDataManager::~PMDataManager() { |
Florian Hahn | fc3d9cb | 2017-07-13 10:52:00 +0000 | [diff] [blame] | 1308 | for (Pass *P : PassVector) |
| 1309 | delete P; |
Devang Patel | d023bb4 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Devang Patel | 2d708df | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1312 | //===----------------------------------------------------------------------===// |
| 1313 | // NOTE: Is this the right place to define this method ? |
Duncan Sands | 210a8b6 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 1314 | // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist. |
Jay Foad | d02b554 | 2020-11-27 10:03:05 +0000 | [diff] [blame] | 1315 | Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID) const { |
| 1316 | return PM.findAnalysisPass(ID, true); |
Devang Patel | 2d708df | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
serge-sans-paille | eb62e37 | 2020-06-26 13:07:31 +0200 | [diff] [blame] | 1319 | std::tuple<Pass *, bool> |
| 1320 | AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI, Function &F) { |
Devang Patel | 3e74175 | 2007-04-16 20:56:24 +0000 | [diff] [blame] | 1321 | return PM.getOnTheFlyPass(P, AnalysisPI, F); |
| 1322 | } |
| 1323 | |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 1324 | namespace llvm { |
| 1325 | namespace legacy { |
| 1326 | |
Devang Patel | dfcb74b | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1327 | //===----------------------------------------------------------------------===// |
Devang Patel | 748205a | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1328 | // FunctionPassManager implementation |
Devang Patel | dfcb74b | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1329 | |
Devang Patel | dae4026 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1330 | /// Create new Function pass manager |
Jeffrey Yasskin | 4415b4f | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1331 | FunctionPassManager::FunctionPassManager(Module *m) : M(m) { |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 1332 | FPM = new legacy::FunctionPassManagerImpl(); |
Devang Patel | 4e0114e | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1333 | // FPM is the top level manager. |
| 1334 | FPM->setTopLevelManager(FPM); |
Devang Patel | a62bf2c | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1335 | |
Dan Gohman | 7519454 | 2008-03-13 02:08:36 +0000 | [diff] [blame] | 1336 | AnalysisResolver *AR = new AnalysisResolver(*FPM); |
Devang Patel | a62bf2c | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1337 | FPM->setResolver(AR); |
Devang Patel | e175484 | 2006-12-08 18:57:16 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Devang Patel | 748205a | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1340 | FunctionPassManager::~FunctionPassManager() { |
Devang Patel | 802d4b7 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1341 | delete FPM; |
| 1342 | } |
| 1343 | |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1344 | void FunctionPassManager::add(Pass *P) { |
Andrew Trick | 5aeec47 | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 1345 | FPM->add(P); |
Devang Patel | dae4026 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Devang Patel | 7b7bc09 | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1348 | /// run - Execute all of the passes scheduled for execution. Keep |
| 1349 | /// track of whether any of the passes modifies the function, and if |
| 1350 | /// so, return true. |
| 1351 | /// |
Devang Patel | 748205a | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1352 | bool FunctionPassManager::run(Function &F) { |
Peter Collingbourne | 9c2be2d | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 1353 | handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) { |
| 1354 | report_fatal_error("Error reading bitcode file: " + EIB.message()); |
| 1355 | }); |
Devang Patel | 2e9cf75 | 2006-12-08 22:57:48 +0000 | [diff] [blame] | 1356 | return FPM->run(F); |
Devang Patel | 7b7bc09 | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | |
Devang Patel | 0ca2a20 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1360 | /// doInitialization - Run all of the initializers for the function passes. |
| 1361 | /// |
Devang Patel | 748205a | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1362 | bool FunctionPassManager::doInitialization() { |
Jeffrey Yasskin | 4415b4f | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1363 | return FPM->doInitialization(*M); |
Devang Patel | 0ca2a20 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Dan Gohman | 31d7ca2 | 2007-07-30 14:51:13 +0000 | [diff] [blame] | 1366 | /// doFinalization - Run all of the finalizers for the function passes. |
Devang Patel | 0ca2a20 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1367 | /// |
Devang Patel | 748205a | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1368 | bool FunctionPassManager::doFinalization() { |
Jeffrey Yasskin | 4415b4f | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1369 | return FPM->doFinalization(*M); |
Devang Patel | 0ca2a20 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1370 | } |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 1371 | } // namespace legacy |
| 1372 | } // namespace llvm |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1373 | |
Devang Patel | daec936 | 2009-04-01 22:34:41 +0000 | [diff] [blame] | 1374 | /// cleanup - After running all passes, clean up pass manager cache. |
| 1375 | void FPPassManager::cleanup() { |
| 1376 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1377 | FunctionPass *FP = getContainedPass(Index); |
| 1378 | AnalysisResolver *AR = FP->getResolver(); |
| 1379 | assert(AR && "Analysis Resolver is not set"); |
| 1380 | AR->clearAnalysisImpls(); |
| 1381 | } |
| 1382 | } |
| 1383 | |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1384 | |
| 1385 | //===----------------------------------------------------------------------===// |
| 1386 | // FPPassManager implementation |
Devang Patel | fd883af | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1387 | |
Devang Patel | 4b40954 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 1388 | char FPPassManager::ID = 0; |
Devang Patel | d023bb4 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1389 | /// Print passes managed by this manager |
| 1390 | void FPPassManager::dumpPassStructure(unsigned Offset) { |
Benjamin Kramer | bc5ef89 | 2011-10-16 16:30:34 +0000 | [diff] [blame] | 1391 | dbgs().indent(Offset*2) << "FunctionPass Manager\n"; |
Devang Patel | d023bb4 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1392 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1393 | FunctionPass *FP = getContainedPass(Index); |
Dan Gohman | 17cd574 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 1394 | FP->dumpPassStructure(Offset + 1); |
Devang Patel | d023bb4 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1395 | dumpLastUses(FP, Offset+1); |
| 1396 | } |
| 1397 | } |
| 1398 | |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1399 | /// Execute all of the passes scheduled for execution by invoking |
| 1400 | /// runOnFunction method. Keep track of whether any of the passes modifies |
Devang Patel | fd883af | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1401 | /// the function, and if so, return true. |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1402 | bool FPPassManager::runOnFunction(Function &F) { |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1403 | if (F.isDeclaration()) |
| 1404 | return false; |
Devang Patel | 7b7bc09 | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1405 | |
| 1406 | bool Changed = false; |
Jessica Paquette | 46e868c | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 1407 | Module &M = *F.getParent(); |
Devang Patel | 370c62f | 2008-03-20 01:09:53 +0000 | [diff] [blame] | 1408 | // Collect inherited analysis from Module level pass manager. |
| 1409 | populateInheritedAnalysis(TPM->activeStack); |
Devang Patel | 15db4ae | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1410 | |
Jessica Paquette | e23d21a | 2018-08-31 20:19:41 +0000 | [diff] [blame] | 1411 | unsigned InstrCount, FunctionSize = 0; |
Jessica Paquette | 6c0520a | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1412 | StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount; |
Xin Tong | 38fec83 | 2018-07-22 05:27:41 +0000 | [diff] [blame] | 1413 | bool EmitICRemark = M.shouldEmitInstrCountChangedRemark(); |
Jessica Paquette | e23d21a | 2018-08-31 20:19:41 +0000 | [diff] [blame] | 1414 | // Collect the initial size of the module. |
| 1415 | if (EmitICRemark) { |
Jessica Paquette | 6c0520a | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1416 | InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount); |
Jessica Paquette | e23d21a | 2018-08-31 20:19:41 +0000 | [diff] [blame] | 1417 | FunctionSize = F.getInstructionCount(); |
| 1418 | } |
| 1419 | |
Nico Weber | cc81252 | 2019-04-20 23:22:45 +0000 | [diff] [blame] | 1420 | llvm::TimeTraceScope FunctionScope("OptFunction", F.getName()); |
Anton Afanasyev | d506b67b | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 1421 | |
Devang Patel | a8ecac4 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1422 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1423 | FunctionPass *FP = getContainedPass(Index); |
Dan Gohman | d1b283f2 | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1424 | bool LocalChanged = false; |
Devang Patel | a8ecac4 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1425 | |
Nico Weber | cc81252 | 2019-04-20 23:22:45 +0000 | [diff] [blame] | 1426 | llvm::TimeTraceScope PassScope("RunPass", FP->getPassName()); |
| 1427 | |
Daniel Dunbar | f71bde7 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1428 | dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName()); |
Chris Lattner | de371f3 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1429 | dumpRequiredSet(FP); |
Devang Patel | 968e03f | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1430 | |
Devang Patel | a8ecac4 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1431 | initializeAnalysisImpl(FP); |
Eric Christopher | d2f088c | 2012-03-23 03:54:05 +0000 | [diff] [blame] | 1432 | |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1433 | { |
| 1434 | PassManagerPrettyStackEntry X(FP, F); |
Chris Lattner | d7cf0bd | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1435 | TimeRegion PassTimer(getPassTimer(FP)); |
serge-sans-paille | 8a6423d | 2020-05-28 12:40:17 +0200 | [diff] [blame] | 1436 | #ifdef EXPENSIVE_CHECKS |
| 1437 | uint64_t RefHash = StructuralHash(F); |
| 1438 | #endif |
Dan Gohman | d1b283f2 | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1439 | LocalChanged |= FP->runOnFunction(F); |
serge-sans-paille | 8a6423d | 2020-05-28 12:40:17 +0200 | [diff] [blame] | 1440 | |
Jon Roelofs | 067ff34 | 2020-07-27 12:34:36 -0600 | [diff] [blame] | 1441 | #if defined(EXPENSIVE_CHECKS) && !defined(NDEBUG) |
| 1442 | if (!LocalChanged && (RefHash != StructuralHash(F))) { |
| 1443 | llvm::errs() << "Pass modifies its input and doesn't report it: " |
| 1444 | << FP->getPassName() << "\n"; |
serge-sans-paille | 055ea2a | 2020-08-27 17:31:54 +0200 | [diff] [blame] | 1445 | llvm_unreachable("Pass modifies its input and doesn't report it"); |
Jon Roelofs | 067ff34 | 2020-07-27 12:34:36 -0600 | [diff] [blame] | 1446 | } |
serge-sans-paille | 8a6423d | 2020-05-28 12:40:17 +0200 | [diff] [blame] | 1447 | #endif |
| 1448 | |
Jessica Paquette | e23d21a | 2018-08-31 20:19:41 +0000 | [diff] [blame] | 1449 | if (EmitICRemark) { |
| 1450 | unsigned NewSize = F.getInstructionCount(); |
| 1451 | |
| 1452 | // Update the size of the function, emit a remark, and update the size |
| 1453 | // of the module. |
| 1454 | if (NewSize != FunctionSize) { |
Jessica Paquette | e23d21a | 2018-08-31 20:19:41 +0000 | [diff] [blame] | 1455 | int64_t Delta = static_cast<int64_t>(NewSize) - |
| 1456 | static_cast<int64_t>(FunctionSize); |
Jessica Paquette | 6c0520a | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1457 | emitInstrCountChangedRemark(FP, M, Delta, InstrCount, |
| 1458 | FunctionToInstrCount, &F); |
Jessica Paquette | e23d21a | 2018-08-31 20:19:41 +0000 | [diff] [blame] | 1459 | InstrCount = static_cast<int64_t>(InstrCount) + Delta; |
| 1460 | FunctionSize = NewSize; |
| 1461 | } |
| 1462 | } |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1463 | } |
Devang Patel | 968e03f | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1464 | |
Dan Gohman | d1b283f2 | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1465 | Changed |= LocalChanged; |
| 1466 | if (LocalChanged) |
Daniel Dunbar | f71bde7 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1467 | dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName()); |
Chris Lattner | de371f3 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1468 | dumpPreservedSet(FP); |
Chandler Carruth | a8d247f | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1469 | dumpUsedSet(FP); |
Devang Patel | 968e03f | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1470 | |
Devang Patel | 667a813 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1471 | verifyPreservedAnalysis(FP); |
serge-sans-paille | 13f0931 | 2020-07-28 10:55:09 +0200 | [diff] [blame] | 1472 | if (LocalChanged) |
| 1473 | removeNotPreservedAnalysis(FP); |
Devang Patel | a8ecac4 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1474 | recordAvailableAnalysis(FP); |
Daniel Dunbar | f71bde7 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1475 | removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG); |
Devang Patel | 7b7bc09 | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1476 | } |
Anton Afanasyev | d506b67b | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 1477 | |
Devang Patel | 7b7bc09 | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1478 | return Changed; |
| 1479 | } |
| 1480 | |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1481 | bool FPPassManager::runOnModule(Module &M) { |
Pedro Artigas | 5154344 | 2012-11-29 17:47:05 +0000 | [diff] [blame] | 1482 | bool Changed = false; |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1483 | |
Serge Pavlov | bef2f0b | 2017-01-15 10:23:18 +0000 | [diff] [blame] | 1484 | for (Function &F : M) |
Yaron Keren | edf6a8c | 2015-06-05 14:15:07 +0000 | [diff] [blame] | 1485 | Changed |= runOnFunction(F); |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1486 | |
Pedro Artigas | 5154344 | 2012-11-29 17:47:05 +0000 | [diff] [blame] | 1487 | return Changed; |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Duncan Sands | 40b88a8 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1490 | bool FPPassManager::doInitialization(Module &M) { |
Devang Patel | 0ca2a20 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1491 | bool Changed = false; |
| 1492 | |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1493 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1494 | Changed |= getContainedPass(Index)->doInitialization(M); |
Andrew Trick | 1d36e07 | 2013-09-18 23:31:10 +0000 | [diff] [blame] | 1495 | |
Devang Patel | 0ca2a20 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1496 | return Changed; |
| 1497 | } |
| 1498 | |
Duncan Sands | 40b88a8 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1499 | bool FPPassManager::doFinalization(Module &M) { |
Devang Patel | 0ca2a20 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1500 | bool Changed = false; |
Pedro Artigas | bf63205 | 2012-12-05 17:12:22 +0000 | [diff] [blame] | 1501 | |
| 1502 | for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index) |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1503 | Changed |= getContainedPass(Index)->doFinalization(M); |
Andrew Trick | 1d36e07 | 2013-09-18 23:31:10 +0000 | [diff] [blame] | 1504 | |
Devang Patel | 0ca2a20 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1505 | return Changed; |
| 1506 | } |
| 1507 | |
Devang Patel | dfcb74b | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1508 | //===----------------------------------------------------------------------===// |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1509 | // MPPassManager implementation |
Devang Patel | 1d00fe8 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1510 | |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1511 | /// Execute all of the passes scheduled for execution by invoking |
| 1512 | /// runOnModule method. Keep track of whether any of the passes modifies |
Devang Patel | 1d00fe8 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1513 | /// the module, and if so, return true. |
| 1514 | bool |
Devang Patel | 96abc76 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1515 | MPPassManager::runOnModule(Module &M) { |
Anton Afanasyev | d506b67b | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 1516 | llvm::TimeTraceScope TimeScope("OptModule", M.getName()); |
| 1517 | |
Devang Patel | 1d00fe8 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1518 | bool Changed = false; |
Devang Patel | 0b05c92 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 1519 | |
Torok Edwin | cd0d846 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1520 | // Initialize on-the-fly passes |
Yaron Keren | edf6a8c | 2015-06-05 14:15:07 +0000 | [diff] [blame] | 1521 | for (auto &OnTheFlyManager : OnTheFlyManagers) { |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 1522 | legacy::FunctionPassManagerImpl *FPP = OnTheFlyManager.second; |
Torok Edwin | cd0d846 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1523 | Changed |= FPP->doInitialization(M); |
| 1524 | } |
| 1525 | |
Pedro Artigas | 5154344 | 2012-11-29 17:47:05 +0000 | [diff] [blame] | 1526 | // Initialize module passes |
| 1527 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1528 | Changed |= getContainedPass(Index)->doInitialization(M); |
| 1529 | |
Fangrui Song | a812c86 | 2019-07-12 15:18:29 +0000 | [diff] [blame] | 1530 | unsigned InstrCount; |
Jessica Paquette | 6c0520a | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1531 | StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount; |
Xin Tong | 38fec83 | 2018-07-22 05:27:41 +0000 | [diff] [blame] | 1532 | bool EmitICRemark = M.shouldEmitInstrCountChangedRemark(); |
Jessica Paquette | e320407 | 2018-08-31 20:20:55 +0000 | [diff] [blame] | 1533 | // Collect the initial size of the module. |
Fangrui Song | a812c86 | 2019-07-12 15:18:29 +0000 | [diff] [blame] | 1534 | if (EmitICRemark) |
Jessica Paquette | 6c0520a | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1535 | InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount); |
Jessica Paquette | e320407 | 2018-08-31 20:20:55 +0000 | [diff] [blame] | 1536 | |
Devang Patel | a8ecac4 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1537 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1538 | ModulePass *MP = getContainedPass(Index); |
Dan Gohman | d1b283f2 | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1539 | bool LocalChanged = false; |
Devang Patel | a8ecac4 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1540 | |
Benjamin Kramer | 7de3ea2 | 2009-12-08 13:07:38 +0000 | [diff] [blame] | 1541 | dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier()); |
Chris Lattner | de371f3 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1542 | dumpRequiredSet(MP); |
Devang Patel | 968e03f | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1543 | |
Devang Patel | a8ecac4 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1544 | initializeAnalysisImpl(MP); |
Devang Patel | 7571112 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1545 | |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1546 | { |
| 1547 | PassManagerPrettyStackEntry X(MP, M); |
Chris Lattner | d7cf0bd | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1548 | TimeRegion PassTimer(getPassTimer(MP)); |
| 1549 | |
serge-sans-paille | 8a6423d | 2020-05-28 12:40:17 +0200 | [diff] [blame] | 1550 | #ifdef EXPENSIVE_CHECKS |
| 1551 | uint64_t RefHash = StructuralHash(M); |
| 1552 | #endif |
| 1553 | |
Dan Gohman | d1b283f2 | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1554 | LocalChanged |= MP->runOnModule(M); |
serge-sans-paille | 8a6423d | 2020-05-28 12:40:17 +0200 | [diff] [blame] | 1555 | |
| 1556 | #ifdef EXPENSIVE_CHECKS |
| 1557 | assert((LocalChanged || (RefHash == StructuralHash(M))) && |
| 1558 | "Pass modifies its input and doesn't report it."); |
| 1559 | #endif |
| 1560 | |
Jessica Paquette | e320407 | 2018-08-31 20:20:55 +0000 | [diff] [blame] | 1561 | if (EmitICRemark) { |
| 1562 | // Update the size of the module. |
Fangrui Song | a812c86 | 2019-07-12 15:18:29 +0000 | [diff] [blame] | 1563 | unsigned ModuleCount = M.getInstructionCount(); |
Jessica Paquette | e320407 | 2018-08-31 20:20:55 +0000 | [diff] [blame] | 1564 | if (ModuleCount != InstrCount) { |
Jessica Paquette | f4da24f | 2018-08-31 20:20:57 +0000 | [diff] [blame] | 1565 | int64_t Delta = static_cast<int64_t>(ModuleCount) - |
| 1566 | static_cast<int64_t>(InstrCount); |
Jessica Paquette | 6c0520a | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 1567 | emitInstrCountChangedRemark(MP, M, Delta, InstrCount, |
| 1568 | FunctionToInstrCount); |
Jessica Paquette | ebd0030 | 2018-08-31 22:43:41 +0000 | [diff] [blame] | 1569 | InstrCount = ModuleCount; |
Jessica Paquette | e320407 | 2018-08-31 20:20:55 +0000 | [diff] [blame] | 1570 | } |
| 1571 | } |
Chris Lattner | 223d131 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1572 | } |
Devang Patel | 968e03f | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1573 | |
Dan Gohman | d1b283f2 | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1574 | Changed |= LocalChanged; |
| 1575 | if (LocalChanged) |
Dan Gohman | b0dee01 | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 1576 | dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG, |
Benjamin Kramer | 7de3ea2 | 2009-12-08 13:07:38 +0000 | [diff] [blame] | 1577 | M.getModuleIdentifier()); |
Chris Lattner | de371f3 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1578 | dumpPreservedSet(MP); |
Chandler Carruth | a8d247f | 2015-08-19 03:02:12 +0000 | [diff] [blame] | 1579 | dumpUsedSet(MP); |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1580 | |
Devang Patel | 667a813 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1581 | verifyPreservedAnalysis(MP); |
serge-sans-paille | 13f0931 | 2020-07-28 10:55:09 +0200 | [diff] [blame] | 1582 | if (LocalChanged) |
| 1583 | removeNotPreservedAnalysis(MP); |
Devang Patel | a8ecac4 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1584 | recordAvailableAnalysis(MP); |
Benjamin Kramer | 7de3ea2 | 2009-12-08 13:07:38 +0000 | [diff] [blame] | 1585 | removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG); |
Devang Patel | 1d00fe8 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1586 | } |
Torok Edwin | cd0d846 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1587 | |
Pedro Artigas | 5154344 | 2012-11-29 17:47:05 +0000 | [diff] [blame] | 1588 | // Finalize module passes |
Pedro Artigas | bf63205 | 2012-12-05 17:12:22 +0000 | [diff] [blame] | 1589 | for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index) |
Pedro Artigas | 5154344 | 2012-11-29 17:47:05 +0000 | [diff] [blame] | 1590 | Changed |= getContainedPass(Index)->doFinalization(M); |
| 1591 | |
Torok Edwin | cd0d846 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1592 | // Finalize on-the-fly passes |
Yaron Keren | edf6a8c | 2015-06-05 14:15:07 +0000 | [diff] [blame] | 1593 | for (auto &OnTheFlyManager : OnTheFlyManagers) { |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 1594 | legacy::FunctionPassManagerImpl *FPP = OnTheFlyManager.second; |
Torok Edwin | cd0d846 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1595 | // We don't know when is the last time an on-the-fly pass is run, |
| 1596 | // so we need to releaseMemory / finalize here |
| 1597 | FPP->releaseMemoryOnTheFly(); |
| 1598 | Changed |= FPP->doFinalization(M); |
| 1599 | } |
Andrew Trick | 1d36e07 | 2013-09-18 23:31:10 +0000 | [diff] [blame] | 1600 | |
Devang Patel | 1d00fe8 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1601 | return Changed; |
| 1602 | } |
| 1603 | |
Devang Patel | 293bbe3 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1604 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 1605 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 1606 | /// through getAnalysis interface. |
| 1607 | void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { |
Dávid Bolvanský | 1b1444a | 2019-11-02 20:14:29 +0100 | [diff] [blame] | 1608 | assert(RequiredPass && "No required pass?"); |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1609 | assert(P->getPotentialPassManagerType() == PMT_ModulePassManager && |
| 1610 | "Unable to handle Pass that requires lower level Analysis pass"); |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1611 | assert((P->getPotentialPassManagerType() < |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1612 | RequiredPass->getPotentialPassManagerType()) && |
| 1613 | "Unable to handle Pass that requires lower level Analysis pass"); |
Devang Patel | 293bbe3 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1614 | |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 1615 | legacy::FunctionPassManagerImpl *FPP = OnTheFlyManagers[P]; |
Devang Patel | 654393f | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1616 | if (!FPP) { |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 1617 | FPP = new legacy::FunctionPassManagerImpl(); |
Devang Patel | 3346d3d | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1618 | // FPP is the top level manager. |
| 1619 | FPP->setTopLevelManager(FPP); |
| 1620 | |
Devang Patel | 654393f | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1621 | OnTheFlyManagers[P] = FPP; |
| 1622 | } |
Chandler Carruth | 9b3892c | 2015-01-28 09:47:21 +0000 | [diff] [blame] | 1623 | const PassInfo *RequiredPassPI = |
| 1624 | TPM->findAnalysisPassInfo(RequiredPass->getPassID()); |
Devang Patel | 654393f | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1625 | |
Craig Topper | ea28548 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1626 | Pass *FoundPass = nullptr; |
Andrew Trick | 8e5110a | 2014-04-08 03:40:34 +0000 | [diff] [blame] | 1627 | if (RequiredPassPI && RequiredPassPI->isAnalysis()) { |
| 1628 | FoundPass = |
| 1629 | ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID()); |
Devang Patel | 3b01a8c | 2011-09-13 21:13:29 +0000 | [diff] [blame] | 1630 | } |
Andrew Trick | 8e5110a | 2014-04-08 03:40:34 +0000 | [diff] [blame] | 1631 | if (!FoundPass) { |
| 1632 | FoundPass = RequiredPass; |
| 1633 | // This should be guaranteed to add RequiredPass to the passmanager given |
Sylvestre Ledru | 56671a1 | 2014-08-11 18:04:46 +0000 | [diff] [blame] | 1634 | // that we checked for an available analysis above. |
Andrew Trick | 8e5110a | 2014-04-08 03:40:34 +0000 | [diff] [blame] | 1635 | FPP->add(RequiredPass); |
| 1636 | } |
| 1637 | // Register P as the last user of FoundPass or RequiredPass. |
| 1638 | SmallVector<Pass *, 1> LU; |
| 1639 | LU.push_back(FoundPass); |
| 1640 | FPP->setLastUser(LU, P); |
Devang Patel | 293bbe3 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1641 | } |
Devang Patel | 654393f | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1642 | |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1643 | /// Return function pass corresponding to PassInfo PI, that is |
Devang Patel | 654393f | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1644 | /// required by module pass MP. Instantiate analysis pass, by using |
| 1645 | /// its runOnFunction() for function F. |
serge-sans-paille | eb62e37 | 2020-06-26 13:07:31 +0200 | [diff] [blame] | 1646 | std::tuple<Pass *, bool> MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, |
| 1647 | Function &F) { |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 1648 | legacy::FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP]; |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1649 | assert(FPP && "Unable to find on the fly pass"); |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1650 | |
Torok Edwin | cd0d846 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1651 | FPP->releaseMemoryOnTheFly(); |
serge-sans-paille | eb62e37 | 2020-06-26 13:07:31 +0200 | [diff] [blame] | 1652 | bool Changed = FPP->run(F); |
| 1653 | return std::make_tuple(((PMTopLevelManager *)FPP)->findAnalysisPass(PI), |
| 1654 | Changed); |
Devang Patel | 654393f | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 1657 | namespace llvm { |
| 1658 | namespace legacy { |
Devang Patel | 5d72ec7 | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1659 | |
Devang Patel | dfcb74b | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1660 | //===----------------------------------------------------------------------===// |
| 1661 | // PassManager implementation |
| 1662 | |
Devang Patel | 5d72ec7 | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1663 | /// Create new pass manager |
Devang Patel | 748205a | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1664 | PassManager::PassManager() { |
Andrew Trick | 87862d1 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1665 | PM = new PassManagerImpl(); |
Devang Patel | 4e0114e | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1666 | // PM is the top level manager |
| 1667 | PM->setTopLevelManager(PM); |
Devang Patel | 5d72ec7 | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Devang Patel | 748205a | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1670 | PassManager::~PassManager() { |
Devang Patel | 802d4b7 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1671 | delete PM; |
| 1672 | } |
| 1673 | |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1674 | void PassManager::add(Pass *P) { |
Andrew Trick | 5aeec47 | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 1675 | PM->add(P); |
Devang Patel | 5d72ec7 | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1679 | /// whether any of the passes modifies the module, and if so, return true. |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1680 | bool PassManager::run(Module &M) { |
Devang Patel | 5d72ec7 | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1681 | return PM->run(M); |
| 1682 | } |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 1683 | } // namespace legacy |
| 1684 | } // namespace llvm |
Devang Patel | 5d72ec7 | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1685 | |
Devang Patel | 7571112 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1686 | //===----------------------------------------------------------------------===// |
Devang Patel | 85c1e03 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1687 | // PMStack implementation |
| 1688 | // |
Devang Patel | afd1979 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 1689 | |
Devang Patel | 85c1e03 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1690 | // Pop Pass Manager from the stack and clear its analysis info. |
| 1691 | void PMStack::pop() { |
| 1692 | |
| 1693 | PMDataManager *Top = this->top(); |
| 1694 | Top->initializeAnalysisInfo(); |
| 1695 | |
| 1696 | S.pop_back(); |
| 1697 | } |
| 1698 | |
| 1699 | // Push PM on the stack and set its top level manager. |
Dan Gohman | fba5619 | 2008-03-13 01:21:31 +0000 | [diff] [blame] | 1700 | void PMStack::push(PMDataManager *PM) { |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1701 | assert(PM && "Unable to push. Pass Manager expected"); |
Andrew Trick | 87862d1 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1702 | assert(PM->getDepth()==0 && "Pass Manager depth set too early"); |
Devang Patel | 85c1e03 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1703 | |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1704 | if (!this->empty()) { |
Andrew Trick | 87862d1 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1705 | assert(PM->getPassManagerType() > this->top()->getPassManagerType() |
| 1706 | && "pushing bad pass manager to PMStack"); |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1707 | PMTopLevelManager *TPM = this->top()->getTopLevelManager(); |
Devang Patel | 85c1e03 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1708 | |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1709 | assert(TPM && "Unable to find top level manager"); |
Devang Patel | cb680dc | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1710 | TPM->addIndirectPassManager(PM); |
| 1711 | PM->setTopLevelManager(TPM); |
Andrew Trick | 87862d1 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1712 | PM->setDepth(this->top()->getDepth()+1); |
Craig Topper | 5d993c5 | 2013-02-06 06:50:38 +0000 | [diff] [blame] | 1713 | } else { |
Benjamin Kramer | ac845ea | 2011-08-29 18:14:15 +0000 | [diff] [blame] | 1714 | assert((PM->getPassManagerType() == PMT_ModulePassManager |
| 1715 | || PM->getPassManagerType() == PMT_FunctionPassManager) |
Andrew Trick | 87862d1 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1716 | && "pushing bad pass manager to PMStack"); |
| 1717 | PM->setDepth(1); |
Devang Patel | cb680dc | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Devang Patel | cb680dc | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1720 | S.push_back(PM); |
| 1721 | } |
| 1722 | |
| 1723 | // Dump content of the pass manager stack. |
Yaron Keren | 33afa9a | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 1724 | LLVM_DUMP_METHOD void PMStack::dump() const { |
Yaron Keren | 84b5542 | 2015-06-05 17:48:47 +0000 | [diff] [blame] | 1725 | for (PMDataManager *Manager : S) |
| 1726 | dbgs() << Manager->getAsPass()->getPassName() << ' '; |
Chris Lattner | 2072e9c | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1727 | |
Devang Patel | cb680dc | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1728 | if (!S.empty()) |
Benjamin Kramer | e7d5e44 | 2011-08-29 18:14:17 +0000 | [diff] [blame] | 1729 | dbgs() << '\n'; |
Devang Patel | 85c1e03 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Devang Patel | 85c1e03 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1732 | /// Find appropriate Module Pass Manager in the PM Stack and |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1733 | /// add self into that manager. |
| 1734 | void ModulePass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | 861df19 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1735 | PassManagerType PreferredType) { |
Devang Patel | 85c1e03 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1736 | // Find Module Pass Manager |
Fangrui Song | 722ce85 | 2019-11-28 13:34:32 -0800 | [diff] [blame] | 1737 | PassManagerType T; |
| 1738 | while ((T = PMS.top()->getPassManagerType()) > PMT_ModulePassManager && |
| 1739 | T != PreferredType) |
| 1740 | PMS.pop(); |
Devang Patel | 9fd256f | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1741 | PMS.top()->add(this); |
Devang Patel | 85c1e03 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Devang Patel | f02b251 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1744 | /// Find appropriate Function Pass Manager or Call Graph Pass Manager |
Dan Gohman | 3d4563a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1745 | /// in the PM Stack and add self into that manager. |
Devang Patel | 3a39e30 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1746 | void FunctionPass::assignPassManager(PMStack &PMS, |
Fangrui Song | 9f0b044 | 2019-11-28 14:00:12 -0800 | [diff] [blame] | 1747 | PassManagerType /*PreferredType*/) { |
Andrew Trick | 5aeec47 | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 1748 | // Find Function Pass Manager |
Fangrui Song | 9f0b044 | 2019-11-28 14:00:12 -0800 | [diff] [blame] | 1749 | PMDataManager *PM; |
| 1750 | while (PM = PMS.top(), PM->getPassManagerType() > PMT_FunctionPassManager) |
Fangrui Song | 722ce85 | 2019-11-28 13:34:32 -0800 | [diff] [blame] | 1751 | PMS.pop(); |
Devang Patel | f02b251 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1752 | |
Chris Lattner | 7402831 | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1753 | // Create new Function Pass Manager if needed. |
Fangrui Song | 9f0b044 | 2019-11-28 14:00:12 -0800 | [diff] [blame] | 1754 | if (PM->getPassManagerType() != PMT_FunctionPassManager) { |
Devang Patel | f02b251 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1755 | // [1] Create new Function Pass Manager |
Fangrui Song | 9f0b044 | 2019-11-28 14:00:12 -0800 | [diff] [blame] | 1756 | auto *FPP = new FPPassManager; |
Devang Patel | 370c62f | 2008-03-20 01:09:53 +0000 | [diff] [blame] | 1757 | FPP->populateInheritedAnalysis(PMS); |
Devang Patel | f02b251 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1758 | |
| 1759 | // [2] Set up new manager's top level manager |
Fangrui Song | 9f0b044 | 2019-11-28 14:00:12 -0800 | [diff] [blame] | 1760 | PM->getTopLevelManager()->addIndirectPassManager(FPP); |
Devang Patel | f02b251 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1761 | |
| 1762 | // [3] Assign manager to manage this new manager. This may create |
| 1763 | // and push new managers into PMS |
Fangrui Song | 9f0b044 | 2019-11-28 14:00:12 -0800 | [diff] [blame] | 1764 | FPP->assignPassManager(PMS, PM->getPassManagerType()); |
Devang Patel | f02b251 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1765 | |
| 1766 | // [4] Push new manager into PMS |
| 1767 | PMS.push(FPP); |
Fangrui Song | 9f0b044 | 2019-11-28 14:00:12 -0800 | [diff] [blame] | 1768 | PM = FPP; |
Devang Patel | 85c1e03 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
Devang Patel | f02b251 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1771 | // Assign FPP as the manager of this pass. |
Fangrui Song | 9f0b044 | 2019-11-28 14:00:12 -0800 | [diff] [blame] | 1772 | PM->add(this); |
Devang Patel | 85c1e03 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Arthur Eubanks | 59fa1bd | 2020-07-09 16:49:48 -0700 | [diff] [blame] | 1775 | legacy::PassManagerBase::~PassManagerBase() {} |