Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1 | //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LLVM Pass Manager infrastructure. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 15 | #include "llvm/PassManagers.h" |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 16 | #include "llvm/Assembly/PrintModulePass.h" |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 17 | #include "llvm/Assembly/Writer.h" |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CommandLine.h" |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Timer.h" |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 21 | #include "llvm/Module.h" |
Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ManagedStatic.h" |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 24 | #include "llvm/Support/PassNameParser.h" |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Owen Anderson | 0dd39fd | 2009-06-17 21:28:54 +0000 | [diff] [blame] | 26 | #include "llvm/System/Mutex.h" |
Owen Anderson | 7d42b95 | 2009-06-18 16:54:52 +0000 | [diff] [blame] | 27 | #include "llvm/System/Threading.h" |
Gordon Henriksen | 878114b | 2008-03-16 04:20:44 +0000 | [diff] [blame] | 28 | #include "llvm-c/Core.h" |
Jeff Cohen | b622c11 | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Duncan Sands | 26ff6f9 | 2008-10-08 07:23:46 +0000 | [diff] [blame] | 30 | #include <cstdio> |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 31 | #include <map> |
Dan Gohman | 8c43e41 | 2007-10-03 19:04:09 +0000 | [diff] [blame] | 32 | using namespace llvm; |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 33 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 34 | // See PassManagers.h for Pass Manager infrastructure overview. |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 35 | |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 36 | namespace llvm { |
| 37 | |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | // Pass debugging information. Often it is useful to find out what pass is |
| 40 | // running when a crash occurs in a utility. When this library is compiled with |
| 41 | // debugging on, a command line option (--debug-pass) is enabled that causes the |
| 42 | // pass name to be printed before it executes. |
| 43 | // |
| 44 | |
Devang Patel | 03fb587 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 45 | // Different debug levels that can be enabled... |
| 46 | enum PassDebugLevel { |
| 47 | None, Arguments, Structure, Executions, Details |
| 48 | }; |
| 49 | |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 50 | static cl::opt<enum PassDebugLevel> |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 51 | PassDebugging("debug-pass", cl::Hidden, |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 52 | cl::desc("Print PassManager debugging information"), |
| 53 | cl::values( |
Devang Patel | 03fb587 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 54 | clEnumVal(None , "disable debug output"), |
| 55 | clEnumVal(Arguments , "print pass arguments to pass to 'opt'"), |
| 56 | clEnumVal(Structure , "print pass structure before run()"), |
| 57 | clEnumVal(Executions, "print pass name before it is executed"), |
| 58 | clEnumVal(Details , "print pass details when it is executed"), |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 59 | clEnumValEnd)); |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 60 | |
| 61 | typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser> |
| 62 | PassOptionList; |
| 63 | |
| 64 | // Print IR out before/after specified passes. |
| 65 | static PassOptionList |
| 66 | PrintBefore("print-before", |
| 67 | llvm::cl::desc("Print IR before specified passes")); |
| 68 | |
| 69 | static PassOptionList |
| 70 | PrintAfter("print-after", |
| 71 | llvm::cl::desc("Print IR after specified passes")); |
| 72 | |
| 73 | static cl::opt<bool> |
| 74 | PrintBeforeAll("print-before-all", |
| 75 | llvm::cl::desc("Print IR before each pass"), |
| 76 | cl::init(false)); |
| 77 | static cl::opt<bool> |
| 78 | PrintAfterAll("print-after-all", |
| 79 | llvm::cl::desc("Print IR after each pass"), |
| 80 | cl::init(false)); |
| 81 | |
| 82 | /// This is a helper to determine whether to print IR before or |
| 83 | /// after a pass. |
| 84 | |
| 85 | static bool ShouldPrintBeforeOrAfterPass(Pass *P, |
| 86 | PassOptionList &PassesToPrint) { |
| 87 | for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) { |
| 88 | const llvm::PassInfo *PassInf = PassesToPrint[i]; |
| 89 | if (PassInf && P->getPassInfo()) |
| 90 | if (PassInf->getPassArgument() == |
| 91 | P->getPassInfo()->getPassArgument()) { |
| 92 | return true; |
| 93 | } |
| 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /// This is a utility to check whether a pass should have IR dumped |
| 100 | /// before it. |
| 101 | static bool ShouldPrintBeforePass(Pass *P) { |
| 102 | return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(P, PrintBefore); |
| 103 | } |
| 104 | |
| 105 | /// This is a utility to check whether a pass should have IR dumped |
| 106 | /// after it. |
| 107 | static bool ShouldPrintAfterPass(Pass *P) { |
| 108 | return PrintAfterAll || ShouldPrintBeforeOrAfterPass(P, PrintAfter); |
| 109 | } |
| 110 | |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 111 | } // End of llvm namespace |
| 112 | |
Chris Lattner | d4d966f | 2009-09-15 05:03:04 +0000 | [diff] [blame] | 113 | /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions |
| 114 | /// or higher is specified. |
| 115 | bool PMDataManager::isPassDebuggingExecutionsOrMore() const { |
| 116 | return PassDebugging >= Executions; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 122 | void PassManagerPrettyStackEntry::print(raw_ostream &OS) const { |
| 123 | if (V == 0 && M == 0) |
| 124 | OS << "Releasing pass '"; |
| 125 | else |
| 126 | OS << "Running pass '"; |
| 127 | |
| 128 | OS << P->getPassName() << "'"; |
| 129 | |
| 130 | if (M) { |
| 131 | OS << " on module '" << M->getModuleIdentifier() << "'.\n"; |
| 132 | return; |
| 133 | } |
| 134 | if (V == 0) { |
| 135 | OS << '\n'; |
| 136 | return; |
| 137 | } |
| 138 | |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 139 | OS << " on "; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 140 | if (isa<Function>(V)) |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 141 | OS << "function"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 142 | else if (isa<BasicBlock>(V)) |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 143 | OS << "basic block"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 144 | else |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 145 | OS << "value"; |
| 146 | |
| 147 | OS << " '"; |
| 148 | WriteAsOperand(OS, V, /*PrintTy=*/false, M); |
| 149 | OS << "'\n"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 153 | namespace { |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 154 | |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 155 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 156 | // BBPassManager |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 157 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 158 | /// BBPassManager manages BasicBlockPass. It batches all the |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 159 | /// pass together and sequence them to process one basic block before |
| 160 | /// processing next basic block. |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 161 | class BBPassManager : public PMDataManager, public FunctionPass { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 162 | |
| 163 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 164 | static char ID; |
Dan Gohman | 13ab93e | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 165 | explicit BBPassManager(int Depth) |
Dan Gohman | a79db30 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 166 | : PMDataManager(Depth), FunctionPass(&ID) {} |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 167 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 168 | /// Execute all of the passes scheduled for execution. Keep track of |
| 169 | /// whether any of the passes modifies the function, and if so, return true. |
| 170 | bool runOnFunction(Function &F); |
| 171 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 172 | /// Pass Manager itself does not invalidate any analysis info. |
| 173 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 174 | Info.setPreservesAll(); |
| 175 | } |
| 176 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 177 | bool doInitialization(Module &M); |
| 178 | bool doInitialization(Function &F); |
| 179 | bool doFinalization(Module &M); |
| 180 | bool doFinalization(Function &F); |
| 181 | |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 182 | virtual PMDataManager *getAsPMDataManager() { return this; } |
| 183 | virtual Pass *getAsPass() { return this; } |
| 184 | |
Devang Patel | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 185 | virtual const char *getPassName() const { |
Dan Gohman | 1e9860a | 2008-03-13 01:58:48 +0000 | [diff] [blame] | 186 | return "BasicBlock Pass Manager"; |
Devang Patel | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 189 | // Print passes managed by this manager |
| 190 | void dumpPassStructure(unsigned Offset) { |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 191 | llvm::dbgs() << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 192 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 193 | BasicBlockPass *BP = getContainedPass(Index); |
| 194 | BP->dumpPassStructure(Offset + 1); |
| 195 | dumpLastUses(BP, Offset+1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 198 | |
| 199 | BasicBlockPass *getContainedPass(unsigned N) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 200 | assert(N < PassVector.size() && "Pass number out of range!"); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 201 | BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]); |
| 202 | return BP; |
| 203 | } |
Devang Patel | 3b3f899 | 2007-01-11 01:10:25 +0000 | [diff] [blame] | 204 | |
Devang Patel | 28349ab | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 205 | virtual PassManagerType getPassManagerType() const { |
Devang Patel | 3b3f899 | 2007-01-11 01:10:25 +0000 | [diff] [blame] | 206 | return PMT_BasicBlockPassManager; |
| 207 | } |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 210 | char BBPassManager::ID = 0; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 211 | } |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 212 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 213 | namespace llvm { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 214 | |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 215 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 216 | // FunctionPassManagerImpl |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 217 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 218 | /// FunctionPassManagerImpl manages FPPassManagers |
| 219 | class FunctionPassManagerImpl : public Pass, |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 220 | public PMDataManager, |
| 221 | public PMTopLevelManager { |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 222 | private: |
| 223 | bool wasRun; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 224 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 225 | static char ID; |
Dan Gohman | 13ab93e | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 226 | explicit FunctionPassManagerImpl(int Depth) : |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 227 | Pass(PT_PassManager, &ID), PMDataManager(Depth), |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 228 | PMTopLevelManager(TLM_Function), wasRun(false) { } |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 229 | |
| 230 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 231 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 232 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 233 | /// implies that all passes MUST be allocated with 'new'. |
| 234 | void add(Pass *P) { |
| 235 | schedulePass(P); |
| 236 | } |
| 237 | |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 238 | /// createPrinterPass - Get a function printer pass. |
| 239 | Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const { |
| 240 | return createPrintFunctionPass(Banner, &O); |
| 241 | } |
| 242 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 243 | // Prepare for running an on the fly pass, freeing memory if needed |
| 244 | // from a previous run. |
| 245 | void releaseMemoryOnTheFly(); |
| 246 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 247 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 248 | /// whether any of the passes modifies the module, and if so, return true. |
| 249 | bool run(Function &F); |
| 250 | |
| 251 | /// doInitialization - Run all of the initializers for the function passes. |
| 252 | /// |
| 253 | bool doInitialization(Module &M); |
| 254 | |
Dan Gohman | e6656eb | 2007-07-30 14:51:13 +0000 | [diff] [blame] | 255 | /// doFinalization - Run all of the finalizers for the function passes. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 256 | /// |
| 257 | bool doFinalization(Module &M); |
| 258 | |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 259 | |
| 260 | virtual PMDataManager *getAsPMDataManager() { return this; } |
| 261 | virtual Pass *getAsPass() { return this; } |
| 262 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 263 | /// Pass Manager itself does not invalidate any analysis info. |
| 264 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 265 | Info.setPreservesAll(); |
| 266 | } |
| 267 | |
| 268 | inline void addTopLevelPass(Pass *P) { |
Chris Lattner | 21889d7 | 2010-01-22 04:55:08 +0000 | [diff] [blame] | 269 | if (ImmutablePass *IP = P->getAsImmutablePass()) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 270 | // P is a immutable pass and it will be managed by this |
| 271 | // top level manager. Set up analysis resolver to connect them. |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 272 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 273 | P->setResolver(AR); |
| 274 | initializeAnalysisImpl(P); |
| 275 | addImmutablePass(IP); |
| 276 | recordAvailableAnalysis(IP); |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 277 | } else { |
David Greene | 103d4b4 | 2010-05-10 20:24:27 +0000 | [diff] [blame^] | 278 | P->assignPassManager(activeStack, PMT_FunctionPassManager); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 279 | } |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 280 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | FPPassManager *getContainedManager(unsigned N) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 284 | assert(N < PassManagers.size() && "Pass number out of range!"); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 285 | FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]); |
| 286 | return FP; |
| 287 | } |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 290 | char FunctionPassManagerImpl::ID = 0; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 291 | //===----------------------------------------------------------------------===// |
| 292 | // MPPassManager |
| 293 | // |
| 294 | /// MPPassManager manages ModulePasses and function pass managers. |
Dan Gohman | dfdf2c0 | 2008-03-11 16:18:48 +0000 | [diff] [blame] | 295 | /// It batches all Module passes and function pass managers together and |
| 296 | /// sequences them to process one module. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 297 | class MPPassManager : public Pass, public PMDataManager { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 298 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 299 | static char ID; |
Dan Gohman | 13ab93e | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 300 | explicit MPPassManager(int Depth) : |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 301 | Pass(PT_PassManager, &ID), PMDataManager(Depth) { } |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 302 | |
| 303 | // Delete on the fly managers. |
| 304 | virtual ~MPPassManager() { |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 305 | for (std::map<Pass *, FunctionPassManagerImpl *>::iterator |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 306 | I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); |
| 307 | I != E; ++I) { |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 308 | FunctionPassManagerImpl *FPP = I->second; |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 309 | delete FPP; |
| 310 | } |
| 311 | } |
| 312 | |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 313 | /// createPrinterPass - Get a module printer pass. |
| 314 | Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const { |
| 315 | return createPrintModulePass(&O, false, Banner); |
| 316 | } |
| 317 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 318 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 319 | /// whether any of the passes modifies the module, and if so, return true. |
| 320 | bool runOnModule(Module &M); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 321 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 322 | /// Pass Manager itself does not invalidate any analysis info. |
| 323 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 324 | Info.setPreservesAll(); |
| 325 | } |
| 326 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 327 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 328 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 329 | /// through getAnalysis interface. |
| 330 | virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass); |
| 331 | |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 332 | /// Return function pass corresponding to PassInfo PI, that is |
| 333 | /// required by module pass MP. Instantiate analysis pass, by using |
| 334 | /// its runOnFunction() for function F. |
| 335 | virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F); |
| 336 | |
Devang Patel | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 337 | virtual const char *getPassName() const { |
| 338 | return "Module Pass Manager"; |
| 339 | } |
| 340 | |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 341 | virtual PMDataManager *getAsPMDataManager() { return this; } |
| 342 | virtual Pass *getAsPass() { return this; } |
| 343 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 344 | // Print passes managed by this manager |
| 345 | void dumpPassStructure(unsigned Offset) { |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 346 | llvm::dbgs() << std::string(Offset*2, ' ') << "ModulePass Manager\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 347 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 348 | ModulePass *MP = getContainedPass(Index); |
| 349 | MP->dumpPassStructure(Offset + 1); |
Dan Gohman | 83ff184 | 2009-07-01 23:12:33 +0000 | [diff] [blame] | 350 | std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I = |
| 351 | OnTheFlyManagers.find(MP); |
| 352 | if (I != OnTheFlyManagers.end()) |
| 353 | I->second->dumpPassStructure(Offset + 2); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 354 | dumpLastUses(MP, Offset+1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 358 | ModulePass *getContainedPass(unsigned N) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 359 | assert(N < PassVector.size() && "Pass number out of range!"); |
| 360 | return static_cast<ModulePass *>(PassVector[N]); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Devang Patel | 28349ab | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 363 | virtual PassManagerType getPassManagerType() const { |
| 364 | return PMT_ModulePassManager; |
| 365 | } |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 366 | |
| 367 | private: |
| 368 | /// Collection of on the fly FPPassManagers. These managers manage |
| 369 | /// function passes that are required by module passes. |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 370 | std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 371 | }; |
| 372 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 373 | char MPPassManager::ID = 0; |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 374 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 375 | // PassManagerImpl |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 376 | // |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 377 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 378 | /// PassManagerImpl manages MPPassManagers |
| 379 | class PassManagerImpl : public Pass, |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 380 | public PMDataManager, |
| 381 | public PMTopLevelManager { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 382 | |
| 383 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 384 | static char ID; |
Dan Gohman | 13ab93e | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 385 | explicit PassManagerImpl(int Depth) : |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 386 | Pass(PT_PassManager, &ID), PMDataManager(Depth), |
| 387 | PMTopLevelManager(TLM_Pass) { } |
Devang Patel | 4c36e6b | 2006-12-07 23:24:58 +0000 | [diff] [blame] | 388 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 389 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 390 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 391 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 392 | /// implies that all passes MUST be allocated with 'new'. |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 393 | void add(Pass *P) { |
Devang Patel | df6c9ae | 2006-12-08 22:34:02 +0000 | [diff] [blame] | 394 | schedulePass(P); |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 395 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 396 | |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 397 | /// createPrinterPass - Get a module printer pass. |
| 398 | Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const { |
| 399 | return createPrintModulePass(&O, false, Banner); |
| 400 | } |
| 401 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 402 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 403 | /// whether any of the passes modifies the module, and if so, return true. |
| 404 | bool run(Module &M); |
| 405 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 406 | /// Pass Manager itself does not invalidate any analysis info. |
| 407 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 408 | Info.setPreservesAll(); |
| 409 | } |
| 410 | |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 411 | inline void addTopLevelPass(Pass *P) { |
Chris Lattner | 21889d7 | 2010-01-22 04:55:08 +0000 | [diff] [blame] | 412 | if (ImmutablePass *IP = P->getAsImmutablePass()) { |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 413 | // P is a immutable pass and it will be managed by this |
| 414 | // top level manager. Set up analysis resolver to connect them. |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 415 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 416 | P->setResolver(AR); |
Devang Patel | 9525754 | 2006-12-12 22:21:37 +0000 | [diff] [blame] | 417 | initializeAnalysisImpl(P); |
Devang Patel | fa971cd | 2006-12-08 23:57:43 +0000 | [diff] [blame] | 418 | addImmutablePass(IP); |
Devang Patel | 9525754 | 2006-12-12 22:21:37 +0000 | [diff] [blame] | 419 | recordAvailableAnalysis(IP); |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 420 | } else { |
David Greene | 103d4b4 | 2010-05-10 20:24:27 +0000 | [diff] [blame^] | 421 | P->assignPassManager(activeStack, PMT_ModulePassManager); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 422 | } |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 425 | virtual PMDataManager *getAsPMDataManager() { return this; } |
| 426 | virtual Pass *getAsPass() { return this; } |
| 427 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 428 | MPPassManager *getContainedManager(unsigned N) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 429 | assert(N < PassManagers.size() && "Pass number out of range!"); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 430 | MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]); |
| 431 | return MP; |
| 432 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 433 | }; |
| 434 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 435 | char PassManagerImpl::ID = 0; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 436 | } // End of llvm namespace |
| 437 | |
| 438 | namespace { |
| 439 | |
| 440 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 441 | /// TimingInfo Class - This class is used to calculate information about the |
| 442 | /// amount of time each pass takes to execute. This only happens when |
| 443 | /// -time-passes is enabled on the command line. |
| 444 | /// |
Owen Anderson | 0dd39fd | 2009-06-17 21:28:54 +0000 | [diff] [blame] | 445 | |
Owen Anderson | 5a6960f | 2009-06-18 20:51:00 +0000 | [diff] [blame] | 446 | static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex; |
Owen Anderson | 0dd39fd | 2009-06-17 21:28:54 +0000 | [diff] [blame] | 447 | |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 448 | class TimingInfo { |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 449 | DenseMap<Pass*, Timer*> TimingData; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 450 | TimerGroup TG; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 451 | public: |
| 452 | // Use 'create' member to get this. |
| 453 | TimingInfo() : TG("... Pass execution timing report ...") {} |
| 454 | |
| 455 | // TimingDtor - Print out information about timing information |
| 456 | ~TimingInfo() { |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 457 | // Delete all of the timers, which accumulate their info into the |
| 458 | // TimerGroup. |
| 459 | for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(), |
| 460 | E = TimingData.end(); I != E; ++I) |
| 461 | delete I->second; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 462 | // TimerGroup is deleted next, printing the report. |
| 463 | } |
| 464 | |
| 465 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer |
| 466 | // to a non null value (if the -time-passes option is enabled) or it leaves it |
| 467 | // null. It may be called multiple times. |
| 468 | static void createTheTimeInfo(); |
| 469 | |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 470 | /// getPassTimer - Return the timer for the specified pass if it exists. |
| 471 | Timer *getPassTimer(Pass *P) { |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 472 | if (P->getAsPMDataManager()) |
Dan Gohman | 277e767 | 2009-09-28 00:07:05 +0000 | [diff] [blame] | 473 | return 0; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 474 | |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 475 | sys::SmartScopedLock<true> Lock(*TimingInfoMutex); |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 476 | Timer *&T = TimingData[P]; |
| 477 | if (T == 0) |
| 478 | T = new Timer(P->getPassName(), TG); |
Chris Lattner | ec8ef9b | 2010-03-30 03:57:00 +0000 | [diff] [blame] | 479 | return T; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 480 | } |
| 481 | }; |
| 482 | |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 483 | } // End of anon namespace |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 484 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 485 | static TimingInfo *TheTimeInfo; |
| 486 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 487 | //===----------------------------------------------------------------------===// |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 488 | // PMTopLevelManager implementation |
| 489 | |
Devang Patel | 4268fc0 | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 490 | /// Initialize top level manager. Create first pass manager. |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 491 | PMTopLevelManager::PMTopLevelManager(enum TopLevelManagerType t) { |
Devang Patel | 4268fc0 | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 492 | if (t == TLM_Pass) { |
| 493 | MPPassManager *MPP = new MPPassManager(1); |
| 494 | MPP->setTopLevelManager(this); |
| 495 | addPassManager(MPP); |
| 496 | activeStack.push(MPP); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 497 | } else if (t == TLM_Function) { |
Devang Patel | 4268fc0 | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 498 | FPPassManager *FPP = new FPPassManager(1); |
| 499 | FPP->setTopLevelManager(this); |
| 500 | addPassManager(FPP); |
| 501 | activeStack.push(FPP); |
| 502 | } |
| 503 | } |
| 504 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 505 | /// Set pass P as the last user of the given analysis passes. |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 506 | void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 507 | Pass *P) { |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 508 | for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(), |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 509 | E = AnalysisPasses.end(); I != E; ++I) { |
| 510 | Pass *AP = *I; |
| 511 | LastUser[AP] = P; |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 512 | |
| 513 | if (P == AP) |
| 514 | continue; |
| 515 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 516 | // If AP is the last user of other passes then make P last user of |
| 517 | // such passes. |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 518 | for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(), |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 519 | LUE = LastUser.end(); LUI != LUE; ++LUI) { |
| 520 | if (LUI->second == AP) |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 521 | // DenseMap iterator is not invalidated here because |
| 522 | // this is just updating exisitng entry. |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 523 | LastUser[LUI->first] = P; |
| 524 | } |
| 525 | } |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /// Collect passes whose last user is P |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 529 | void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses, |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 530 | Pass *P) { |
| 531 | DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI = |
| 532 | InversedLastUser.find(P); |
| 533 | if (DMI == InversedLastUser.end()) |
| 534 | return; |
| 535 | |
| 536 | SmallPtrSet<Pass *, 8> &LU = DMI->second; |
| 537 | for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(), |
| 538 | E = LU.end(); I != E; ++I) { |
| 539 | LastUses.push_back(*I); |
| 540 | } |
| 541 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 544 | AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) { |
| 545 | AnalysisUsage *AnUsage = NULL; |
| 546 | DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P); |
| 547 | if (DMI != AnUsageMap.end()) |
| 548 | AnUsage = DMI->second; |
| 549 | else { |
| 550 | AnUsage = new AnalysisUsage(); |
| 551 | P->getAnalysisUsage(*AnUsage); |
| 552 | AnUsageMap[P] = AnUsage; |
| 553 | } |
| 554 | return AnUsage; |
| 555 | } |
| 556 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 557 | /// Schedule pass P for execution. Make sure that passes required by |
| 558 | /// P are run before P is run. Update analysis info maintained by |
| 559 | /// the manager. Remove dead passes. This is a recursive function. |
| 560 | void PMTopLevelManager::schedulePass(Pass *P) { |
| 561 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 562 | // TODO : Allocate function manager for this pass, other wise required set |
| 563 | // may be inserted into previous function manager |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 564 | |
Devang Patel | d74ede7 | 2007-03-06 01:06:16 +0000 | [diff] [blame] | 565 | // Give pass a chance to prepare the stage. |
| 566 | P->preparePassManager(activeStack); |
| 567 | |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 568 | // If P is an analysis pass and it is available then do not |
| 569 | // generate the analysis again. Stale analysis info should not be |
| 570 | // available at this point. |
Devang Patel | 718da66 | 2008-03-19 21:56:59 +0000 | [diff] [blame] | 571 | if (P->getPassInfo() && |
Nuno Lopes | 0460bb2 | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 572 | P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo())) { |
| 573 | delete P; |
Devang Patel | af75ab8 | 2008-03-19 00:48:41 +0000 | [diff] [blame] | 574 | return; |
Nuno Lopes | 0460bb2 | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 575 | } |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 576 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 577 | AnalysisUsage *AnUsage = findAnalysisUsage(P); |
| 578 | |
Devang Patel | fdee703 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 579 | bool checkAnalysis = true; |
| 580 | while (checkAnalysis) { |
| 581 | checkAnalysis = false; |
| 582 | |
| 583 | const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet(); |
| 584 | for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(), |
| 585 | E = RequiredSet.end(); I != E; ++I) { |
| 586 | |
| 587 | Pass *AnalysisPass = findAnalysisPass(*I); |
| 588 | if (!AnalysisPass) { |
| 589 | AnalysisPass = (*I)->createPass(); |
| 590 | if (P->getPotentialPassManagerType () == |
| 591 | AnalysisPass->getPotentialPassManagerType()) |
| 592 | // Schedule analysis pass that is managed by the same pass manager. |
| 593 | schedulePass(AnalysisPass); |
| 594 | else if (P->getPotentialPassManagerType () > |
| 595 | AnalysisPass->getPotentialPassManagerType()) { |
| 596 | // Schedule analysis pass that is managed by a new manager. |
| 597 | schedulePass(AnalysisPass); |
| 598 | // Recheck analysis passes to ensure that required analysises that |
| 599 | // are already checked are still available. |
| 600 | checkAnalysis = true; |
| 601 | } |
| 602 | else |
| 603 | // Do not schedule this analysis. Lower level analsyis |
| 604 | // passes are run on the fly. |
| 605 | delete AnalysisPass; |
| 606 | } |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
| 610 | // Now all required passes are available. |
| 611 | addTopLevelPass(P); |
| 612 | } |
| 613 | |
| 614 | /// Find the pass that implements Analysis AID. Search immutable |
| 615 | /// passes and all pass managers. If desired pass is not found |
| 616 | /// then return NULL. |
| 617 | Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { |
| 618 | |
| 619 | Pass *P = NULL; |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 620 | // Check pass managers |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 621 | for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(), |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 622 | E = PassManagers.end(); P == NULL && I != E; ++I) { |
Dan Gohman | 73caf5f | 2008-03-13 01:48:32 +0000 | [diff] [blame] | 623 | PMDataManager *PMD = *I; |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 624 | P = PMD->findAnalysisPass(AID, false); |
| 625 | } |
| 626 | |
| 627 | // Check other pass managers |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 628 | for (SmallVector<PMDataManager *, 8>::iterator |
| 629 | I = IndirectPassManagers.begin(), |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 630 | E = IndirectPassManagers.end(); P == NULL && I != E; ++I) |
| 631 | P = (*I)->findAnalysisPass(AID, false); |
| 632 | |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 633 | for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(), |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 634 | E = ImmutablePasses.end(); P == NULL && I != E; ++I) { |
| 635 | const PassInfo *PI = (*I)->getPassInfo(); |
| 636 | if (PI == AID) |
| 637 | P = *I; |
| 638 | |
| 639 | // If Pass not found then check the interfaces implemented by Immutable Pass |
| 640 | if (!P) { |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 641 | const std::vector<const PassInfo*> &ImmPI = |
| 642 | PI->getInterfacesImplemented(); |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 643 | if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end()) |
| 644 | P = *I; |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 648 | return P; |
| 649 | } |
| 650 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 651 | // Print passes managed by this top level manager. |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 652 | void PMTopLevelManager::dumpPasses() const { |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 653 | |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 654 | if (PassDebugging < Structure) |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 655 | return; |
| 656 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 657 | // Print out the immutable passes |
| 658 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { |
| 659 | ImmutablePasses[i]->dumpPassStructure(0); |
| 660 | } |
| 661 | |
Dan Gohman | 73caf5f | 2008-03-13 01:48:32 +0000 | [diff] [blame] | 662 | // Every class that derives from PMDataManager also derives from Pass |
| 663 | // (sometimes indirectly), but there's no inheritance relationship |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 664 | // between PMDataManager and Pass, so we have to getAsPass to get |
Dan Gohman | 73caf5f | 2008-03-13 01:48:32 +0000 | [diff] [blame] | 665 | // from a PMDataManager* to a Pass*. |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 666 | for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(), |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 667 | E = PassManagers.end(); I != E; ++I) |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 668 | (*I)->getAsPass()->dumpPassStructure(1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 671 | void PMTopLevelManager::dumpArguments() const { |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 672 | |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 673 | if (PassDebugging < Arguments) |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 674 | return; |
| 675 | |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 676 | dbgs() << "Pass Arguments: "; |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 677 | for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(), |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 678 | E = PassManagers.end(); I != E; ++I) |
| 679 | (*I)->dumpPassArguments(); |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 680 | dbgs() << "\n"; |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 683 | void PMTopLevelManager::initializeAllAnalysisInfo() { |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 684 | for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(), |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 685 | E = PassManagers.end(); I != E; ++I) |
| 686 | (*I)->initializeAnalysisInfo(); |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 687 | |
| 688 | // Initailize other pass managers |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 689 | for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(), |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 690 | E = IndirectPassManagers.end(); I != E; ++I) |
| 691 | (*I)->initializeAnalysisInfo(); |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 692 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 693 | for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(), |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 694 | DME = LastUser.end(); DMI != DME; ++DMI) { |
| 695 | DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI = |
| 696 | InversedLastUser.find(DMI->second); |
| 697 | if (InvDMI != InversedLastUser.end()) { |
| 698 | SmallPtrSet<Pass *, 8> &L = InvDMI->second; |
| 699 | L.insert(DMI->first); |
| 700 | } else { |
| 701 | SmallPtrSet<Pass *, 8> L; L.insert(DMI->first); |
| 702 | InversedLastUser[DMI->second] = L; |
| 703 | } |
| 704 | } |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 707 | /// Destructor |
| 708 | PMTopLevelManager::~PMTopLevelManager() { |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 709 | for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(), |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 710 | E = PassManagers.end(); I != E; ++I) |
| 711 | delete *I; |
| 712 | |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 713 | for (SmallVector<ImmutablePass *, 8>::iterator |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 714 | I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) |
| 715 | delete *I; |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 716 | |
| 717 | for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(), |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 718 | DME = AnUsageMap.end(); DMI != DME; ++DMI) |
| 719 | delete DMI->second; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 722 | //===----------------------------------------------------------------------===// |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 723 | // PMDataManager implementation |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 724 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 725 | /// Augement AvailableAnalysis by adding analysis made available by pass P. |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 726 | void PMDataManager::recordAvailableAnalysis(Pass *P) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 727 | const PassInfo *PI = P->getPassInfo(); |
| 728 | if (PI == 0) return; |
| 729 | |
| 730 | AvailableAnalysis[PI] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 731 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 732 | //This pass is the current implementation of all of the interfaces it |
| 733 | //implements as well. |
| 734 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 735 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 736 | AvailableAnalysis[II[i]] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 739 | // Return true if P preserves high level analysis used by other |
| 740 | // passes managed by this manager |
| 741 | bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 742 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 743 | if (AnUsage->getPreservesAll()) |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 744 | return true; |
| 745 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 746 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 747 | for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(), |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 748 | E = HigherLevelAnalysis.end(); I != E; ++I) { |
| 749 | Pass *P1 = *I; |
Chris Lattner | 21889d7 | 2010-01-22 04:55:08 +0000 | [diff] [blame] | 750 | if (P1->getAsImmutablePass() == 0 && |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 751 | std::find(PreservedSet.begin(), PreservedSet.end(), |
| 752 | P1->getPassInfo()) == |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 753 | PreservedSet.end()) |
| 754 | return false; |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | return true; |
| 758 | } |
| 759 | |
Chris Lattner | 02eb94c | 2008-08-07 07:34:50 +0000 | [diff] [blame] | 760 | /// verifyPreservedAnalysis -- Verify analysis preserved by pass P. |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 761 | void PMDataManager::verifyPreservedAnalysis(Pass *P) { |
Chris Lattner | 02eb94c | 2008-08-07 07:34:50 +0000 | [diff] [blame] | 762 | // Don't do this unless assertions are enabled. |
| 763 | #ifdef NDEBUG |
| 764 | return; |
| 765 | #endif |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 766 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 767 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 768 | |
Devang Patel | ef43253 | 2007-07-19 05:36:09 +0000 | [diff] [blame] | 769 | // Verify preserved analysis |
Chris Lattner | cbd160f | 2008-08-08 05:33:04 +0000 | [diff] [blame] | 770 | for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(), |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 771 | E = PreservedSet.end(); I != E; ++I) { |
| 772 | AnalysisID AID = *I; |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 773 | if (Pass *AP = findAnalysisPass(AID, true)) { |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 774 | TimeRegion PassTimer(getPassTimer(AP)); |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 775 | AP->verifyAnalysis(); |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 776 | } |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
Devang Patel | 67c79a4 | 2008-07-01 19:50:56 +0000 | [diff] [blame] | 780 | /// Remove Analysis not preserved by Pass P |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 781 | void PMDataManager::removeNotPreservedAnalysis(Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 782 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 783 | if (AnUsage->getPreservesAll()) |
Devang Patel | 2e169c3 | 2006-12-07 20:03:49 +0000 | [diff] [blame] | 784 | return; |
| 785 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 786 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 787 | for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), |
Devang Patel | be6bd55e | 2006-12-12 23:07:44 +0000 | [diff] [blame] | 788 | E = AvailableAnalysis.end(); I != E; ) { |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 789 | std::map<AnalysisID, Pass*>::iterator Info = I++; |
Chris Lattner | 21889d7 | 2010-01-22 04:55:08 +0000 | [diff] [blame] | 790 | if (Info->second->getAsImmutablePass() == 0 && |
| 791 | std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 792 | PreservedSet.end()) { |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 793 | // Remove this analysis |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 794 | if (PassDebugging >= Details) { |
| 795 | Pass *S = Info->second; |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 796 | dbgs() << " -- '" << P->getPassName() << "' is not preserving '"; |
| 797 | dbgs() << S->getPassName() << "'\n"; |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 798 | } |
Dan Gohman | 193e4c0 | 2008-11-06 21:57:17 +0000 | [diff] [blame] | 799 | AvailableAnalysis.erase(Info); |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 800 | } |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 801 | } |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 802 | |
| 803 | // Check inherited analysis also. If P is not preserving analysis |
| 804 | // provided by parent manager then remove it here. |
| 805 | for (unsigned Index = 0; Index < PMT_Last; ++Index) { |
| 806 | |
| 807 | if (!InheritedAnalysis[Index]) |
| 808 | continue; |
| 809 | |
| 810 | for (std::map<AnalysisID, Pass*>::iterator |
| 811 | I = InheritedAnalysis[Index]->begin(), |
| 812 | E = InheritedAnalysis[Index]->end(); I != E; ) { |
| 813 | std::map<AnalysisID, Pass *>::iterator Info = I++; |
Chris Lattner | 21889d7 | 2010-01-22 04:55:08 +0000 | [diff] [blame] | 814 | if (Info->second->getAsImmutablePass() == 0 && |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 815 | std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == |
Andreas Neustifter | 4665141 | 2009-12-04 06:58:24 +0000 | [diff] [blame] | 816 | PreservedSet.end()) { |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 817 | // Remove this analysis |
Andreas Neustifter | 4665141 | 2009-12-04 06:58:24 +0000 | [diff] [blame] | 818 | if (PassDebugging >= Details) { |
| 819 | Pass *S = Info->second; |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 820 | dbgs() << " -- '" << P->getPassName() << "' is not preserving '"; |
| 821 | dbgs() << S->getPassName() << "'\n"; |
Andreas Neustifter | 4665141 | 2009-12-04 06:58:24 +0000 | [diff] [blame] | 822 | } |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 823 | InheritedAnalysis[Index]->erase(Info); |
Andreas Neustifter | 4665141 | 2009-12-04 06:58:24 +0000 | [diff] [blame] | 824 | } |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 825 | } |
| 826 | } |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 829 | /// Remove analysis passes that are not used any longer |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 830 | void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg, |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 831 | enum PassDebuggingString DBG_STR) { |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 832 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 833 | SmallVector<Pass *, 12> DeadPasses; |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 834 | |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 835 | // If this is a on the fly manager then it does not have TPM. |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 836 | if (!TPM) |
| 837 | return; |
| 838 | |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 839 | TPM->collectLastUses(DeadPasses, P); |
| 840 | |
Devang Patel | 656a917 | 2008-06-06 17:50:36 +0000 | [diff] [blame] | 841 | if (PassDebugging >= Details && !DeadPasses.empty()) { |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 842 | dbgs() << " -*- '" << P->getPassName(); |
| 843 | dbgs() << "' is the last user of following pass instances."; |
| 844 | dbgs() << " Free these instances\n"; |
Evan Cheng | 93af6ce | 2008-06-04 09:13:31 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 847 | for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(), |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 848 | E = DeadPasses.end(); I != E; ++I) |
| 849 | freePass(*I, Msg, DBG_STR); |
| 850 | } |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 851 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 852 | void PMDataManager::freePass(Pass *P, StringRef Msg, |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 853 | enum PassDebuggingString DBG_STR) { |
| 854 | dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg); |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 855 | |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 856 | { |
| 857 | // If the pass crashes releasing memory, remember this. |
| 858 | PassManagerPrettyStackEntry X(P); |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 859 | TimeRegion PassTimer(getPassTimer(P)); |
| 860 | |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 861 | P->releaseMemory(); |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | if (const PassInfo *PI = P->getPassInfo()) { |
| 865 | // Remove the pass itself (if it is not already removed). |
| 866 | AvailableAnalysis.erase(PI); |
| 867 | |
| 868 | // Remove all interfaces this pass implements, for which it is also |
| 869 | // listed as the available implementation. |
| 870 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 871 | for (unsigned i = 0, e = II.size(); i != e; ++i) { |
Devang Patel | c3e3ca9 | 2008-10-06 20:36:36 +0000 | [diff] [blame] | 872 | std::map<AnalysisID, Pass*>::iterator Pos = |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 873 | AvailableAnalysis.find(II[i]); |
| 874 | if (Pos != AvailableAnalysis.end() && Pos->second == P) |
Devang Patel | c3e3ca9 | 2008-10-06 20:36:36 +0000 | [diff] [blame] | 875 | AvailableAnalysis.erase(Pos); |
Devang Patel | c3e3ca9 | 2008-10-06 20:36:36 +0000 | [diff] [blame] | 876 | } |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 877 | } |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Devang Patel | 8f677ce | 2006-12-07 18:47:25 +0000 | [diff] [blame] | 880 | /// Add pass P into the PassVector. Update |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 881 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 882 | void PMDataManager::add(Pass *P, bool ProcessAnalysis) { |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 883 | // This manager is going to manage pass P. Set up analysis resolver |
| 884 | // to connect them. |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 885 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 886 | P->setResolver(AR); |
| 887 | |
Devang Patel | ec2b9a7 | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 888 | // If a FunctionPass F is the last user of ModulePass info M |
| 889 | // then the F's manager, not F, records itself as a last user of M. |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 890 | SmallVector<Pass *, 12> TransferLastUses; |
Devang Patel | ec2b9a7 | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 891 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 892 | if (!ProcessAnalysis) { |
| 893 | // Add pass |
| 894 | PassVector.push_back(P); |
| 895 | return; |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 896 | } |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 897 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 898 | // At the moment, this pass is the last user of all required passes. |
| 899 | SmallVector<Pass *, 12> LastUses; |
| 900 | SmallVector<Pass *, 8> RequiredPasses; |
| 901 | SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable; |
| 902 | |
| 903 | unsigned PDepth = this->getDepth(); |
| 904 | |
| 905 | collectRequiredAnalysis(RequiredPasses, |
| 906 | ReqAnalysisNotAvailable, P); |
| 907 | for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(), |
| 908 | E = RequiredPasses.end(); I != E; ++I) { |
| 909 | Pass *PRequired = *I; |
| 910 | unsigned RDepth = 0; |
| 911 | |
| 912 | assert(PRequired->getResolver() && "Analysis Resolver is not set"); |
| 913 | PMDataManager &DM = PRequired->getResolver()->getPMDataManager(); |
| 914 | RDepth = DM.getDepth(); |
| 915 | |
| 916 | if (PDepth == RDepth) |
| 917 | LastUses.push_back(PRequired); |
| 918 | else if (PDepth > RDepth) { |
| 919 | // Let the parent claim responsibility of last use |
| 920 | TransferLastUses.push_back(PRequired); |
| 921 | // Keep track of higher level analysis used by this manager. |
| 922 | HigherLevelAnalysis.push_back(PRequired); |
| 923 | } else |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 924 | llvm_unreachable("Unable to accomodate Required Pass"); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | // Set P as P's last user until someone starts using P. |
| 928 | // However, if P is a Pass Manager then it does not need |
| 929 | // to record its last user. |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 930 | if (P->getAsPMDataManager() == 0) |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 931 | LastUses.push_back(P); |
| 932 | TPM->setLastUser(LastUses, P); |
| 933 | |
| 934 | if (!TransferLastUses.empty()) { |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 935 | Pass *My_PM = getAsPass(); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 936 | TPM->setLastUser(TransferLastUses, My_PM); |
| 937 | TransferLastUses.clear(); |
| 938 | } |
| 939 | |
| 940 | // Now, take care of required analysises that are not available. |
| 941 | for (SmallVector<AnalysisID, 8>::iterator |
| 942 | I = ReqAnalysisNotAvailable.begin(), |
| 943 | E = ReqAnalysisNotAvailable.end() ;I != E; ++I) { |
| 944 | Pass *AnalysisPass = (*I)->createPass(); |
| 945 | this->addLowerLevelRequiredPass(P, AnalysisPass); |
| 946 | } |
| 947 | |
| 948 | // Take a note of analysis required and made available by this pass. |
| 949 | // Remove the analysis not preserved by this pass |
| 950 | removeNotPreservedAnalysis(P); |
| 951 | recordAvailableAnalysis(P); |
| 952 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 953 | // Add pass |
| 954 | PassVector.push_back(P); |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 957 | |
| 958 | /// Populate RP with analysis pass that are required by |
| 959 | /// pass P and are available. Populate RP_NotAvail with analysis |
| 960 | /// pass that are required by pass P but are not available. |
| 961 | void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP, |
| 962 | SmallVector<AnalysisID, 8> &RP_NotAvail, |
| 963 | Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 964 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 965 | const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet(); |
Chris Lattner | cbd160f | 2008-08-08 05:33:04 +0000 | [diff] [blame] | 966 | for (AnalysisUsage::VectorType::const_iterator |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 967 | I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) { |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 968 | if (Pass *AnalysisPass = findAnalysisPass(*I, true)) |
| 969 | RP.push_back(AnalysisPass); |
| 970 | else |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 971 | RP_NotAvail.push_back(*I); |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 972 | } |
Devang Patel | f58183d | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 973 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 974 | const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet(); |
Chris Lattner | cbd160f | 2008-08-08 05:33:04 +0000 | [diff] [blame] | 975 | for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(), |
Devang Patel | f58183d | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 976 | E = IDs.end(); I != E; ++I) { |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 977 | if (Pass *AnalysisPass = findAnalysisPass(*I, true)) |
| 978 | RP.push_back(AnalysisPass); |
| 979 | else |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 980 | RP_NotAvail.push_back(*I); |
Devang Patel | f58183d | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 981 | } |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 984 | // All Required analyses should be available to the pass as it runs! Here |
| 985 | // we fill in the AnalysisImpls member of the pass so that it can |
| 986 | // successfully use the getAnalysis() method to retrieve the |
| 987 | // implementations it needs. |
| 988 | // |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 989 | void PMDataManager::initializeAnalysisImpl(Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 990 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 991 | |
Chris Lattner | cbd160f | 2008-08-08 05:33:04 +0000 | [diff] [blame] | 992 | for (AnalysisUsage::VectorType::const_iterator |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 993 | I = AnUsage->getRequiredSet().begin(), |
| 994 | E = AnUsage->getRequiredSet().end(); I != E; ++I) { |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 995 | Pass *Impl = findAnalysisPass(*I, true); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 996 | if (Impl == 0) |
Devang Patel | 56a5c62 | 2007-04-16 20:44:16 +0000 | [diff] [blame] | 997 | // This may be analysis pass that is initialized on the fly. |
| 998 | // If that is not the case then it will raise an assert when it is used. |
| 999 | continue; |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 1000 | AnalysisResolver *AR = P->getResolver(); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1001 | assert(AR && "Analysis Resolver is not set"); |
Devang Patel | 984698a | 2006-12-09 01:11:34 +0000 | [diff] [blame] | 1002 | AR->addAnalysisImplsPair(*I, Impl); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 1006 | /// Find the pass that implements Analysis AID. If desired pass is not found |
| 1007 | /// then return NULL. |
| 1008 | Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) { |
| 1009 | |
| 1010 | // Check if AvailableAnalysis map has one entry. |
| 1011 | std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID); |
| 1012 | |
| 1013 | if (I != AvailableAnalysis.end()) |
| 1014 | return I->second; |
| 1015 | |
| 1016 | // Search Parents through TopLevelManager |
| 1017 | if (SearchParent) |
| 1018 | return TPM->findAnalysisPass(AID); |
| 1019 | |
Devang Patel | 9d759b8 | 2006-12-09 00:09:12 +0000 | [diff] [blame] | 1020 | return NULL; |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1023 | // Print list of passes that are last used by P. |
| 1024 | void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{ |
| 1025 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 1026 | SmallVector<Pass *, 12> LUses; |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 1027 | |
| 1028 | // If this is a on the fly manager then it does not have TPM. |
| 1029 | if (!TPM) |
| 1030 | return; |
| 1031 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1032 | TPM->collectLastUses(LUses, P); |
| 1033 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 1034 | for (SmallVector<Pass *, 12>::iterator I = LUses.begin(), |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1035 | E = LUses.end(); I != E; ++I) { |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1036 | llvm::dbgs() << "--" << std::string(Offset*2, ' '); |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1037 | (*I)->dumpPassStructure(0); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | void PMDataManager::dumpPassArguments() const { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1042 | for (SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(), |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1043 | E = PassVector.end(); I != E; ++I) { |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 1044 | if (PMDataManager *PMD = (*I)->getAsPMDataManager()) |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1045 | PMD->dumpPassArguments(); |
| 1046 | else |
| 1047 | if (const PassInfo *PI = (*I)->getPassInfo()) |
| 1048 | if (!PI->isAnalysisGroup()) |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1049 | dbgs() << " -" << PI->getPassArgument(); |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | |
Chris Lattner | dd6304f | 2007-08-10 06:17:04 +0000 | [diff] [blame] | 1053 | void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1, |
| 1054 | enum PassDebuggingString S2, |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 1055 | StringRef Msg) { |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 1056 | if (PassDebugging < Executions) |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1057 | return; |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1058 | dbgs() << (void*)this << std::string(getDepth()*2+1, ' '); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1059 | switch (S1) { |
| 1060 | case EXECUTION_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1061 | dbgs() << "Executing Pass '" << P->getPassName(); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1062 | break; |
| 1063 | case MODIFICATION_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1064 | dbgs() << "Made Modification '" << P->getPassName(); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1065 | break; |
| 1066 | case FREEING_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1067 | dbgs() << " Freeing Pass '" << P->getPassName(); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1068 | break; |
| 1069 | default: |
| 1070 | break; |
| 1071 | } |
| 1072 | switch (S2) { |
| 1073 | case ON_BASICBLOCK_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1074 | dbgs() << "' on BasicBlock '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1075 | break; |
| 1076 | case ON_FUNCTION_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1077 | dbgs() << "' on Function '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1078 | break; |
| 1079 | case ON_MODULE_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1080 | dbgs() << "' on Module '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1081 | break; |
| 1082 | case ON_LOOP_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1083 | dbgs() << "' on Loop '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1084 | break; |
| 1085 | case ON_CG_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1086 | dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1087 | break; |
| 1088 | default: |
| 1089 | break; |
| 1090 | } |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1093 | void PMDataManager::dumpRequiredSet(const Pass *P) const { |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1094 | if (PassDebugging < Details) |
| 1095 | return; |
| 1096 | |
| 1097 | AnalysisUsage analysisUsage; |
| 1098 | P->getAnalysisUsage(analysisUsage); |
| 1099 | dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet()); |
| 1100 | } |
| 1101 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1102 | void PMDataManager::dumpPreservedSet(const Pass *P) const { |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1103 | if (PassDebugging < Details) |
| 1104 | return; |
| 1105 | |
| 1106 | AnalysisUsage analysisUsage; |
| 1107 | P->getAnalysisUsage(analysisUsage); |
| 1108 | dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet()); |
| 1109 | } |
| 1110 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 1111 | void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P, |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1112 | const AnalysisUsage::VectorType &Set) const { |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1113 | assert(PassDebugging >= Details); |
| 1114 | if (Set.empty()) |
| 1115 | return; |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1116 | dbgs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1117 | for (unsigned i = 0; i != Set.size(); ++i) { |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1118 | if (i) dbgs() << ','; |
| 1119 | dbgs() << ' ' << Set[i]->getPassName(); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1120 | } |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1121 | dbgs() << '\n'; |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1122 | } |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1123 | |
Devang Patel | 004937b | 2007-07-27 20:06:09 +0000 | [diff] [blame] | 1124 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 1125 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 1126 | /// through getAnalysis interface. |
| 1127 | /// This should be handled by specific pass manager. |
| 1128 | void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { |
| 1129 | if (TPM) { |
| 1130 | TPM->dumpArguments(); |
| 1131 | TPM->dumpPasses(); |
| 1132 | } |
Devang Patel | 8df7cc1 | 2008-02-02 01:43:30 +0000 | [diff] [blame] | 1133 | |
| 1134 | // Module Level pass may required Function Level analysis info |
| 1135 | // (e.g. dominator info). Pass manager uses on the fly function pass manager |
| 1136 | // to provide this on demand. In that case, in Pass manager terminology, |
| 1137 | // module level pass is requiring lower level analysis info managed by |
| 1138 | // lower level pass manager. |
| 1139 | |
| 1140 | // When Pass manager is not able to order required analysis info, Pass manager |
| 1141 | // checks whether any lower level manager will be able to provide this |
| 1142 | // analysis info on demand or not. |
Devang Patel | ab85d6b | 2008-06-03 01:20:02 +0000 | [diff] [blame] | 1143 | #ifndef NDEBUG |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1144 | dbgs() << "Unable to schedule '" << RequiredPass->getPassName(); |
| 1145 | dbgs() << "' required by '" << P->getPassName() << "'\n"; |
Devang Patel | ab85d6b | 2008-06-03 01:20:02 +0000 | [diff] [blame] | 1146 | #endif |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1147 | llvm_unreachable("Unable to schedule pass"); |
Devang Patel | 004937b | 2007-07-27 20:06:09 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1150 | // Destructor |
| 1151 | PMDataManager::~PMDataManager() { |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 1152 | for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(), |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1153 | E = PassVector.end(); I != E; ++I) |
| 1154 | delete *I; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1157 | //===----------------------------------------------------------------------===// |
| 1158 | // NOTE: Is this the right place to define this method ? |
Duncan Sands | 5a913d6 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 1159 | // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist. |
| 1160 | Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const { |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1161 | return PM.findAnalysisPass(ID, dir); |
| 1162 | } |
| 1163 | |
Devang Patel | 9294281 | 2007-04-16 20:56:24 +0000 | [diff] [blame] | 1164 | Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI, |
| 1165 | Function &F) { |
| 1166 | return PM.getOnTheFlyPass(P, AnalysisPI, F); |
| 1167 | } |
| 1168 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1169 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1170 | // BBPassManager implementation |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1171 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1172 | /// Execute all of the passes scheduled for execution by invoking |
| 1173 | /// runOnBasicBlock method. Keep track of whether any of the passes modifies |
| 1174 | /// the function, and if so, return true. |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1175 | bool BBPassManager::runOnFunction(Function &F) { |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1176 | if (F.isDeclaration()) |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1177 | return false; |
| 1178 | |
Devang Patel | e958559 | 2006-12-08 01:38:28 +0000 | [diff] [blame] | 1179 | bool Changed = doInitialization(F); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 1180 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1181 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1182 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1183 | BasicBlockPass *BP = getContainedPass(Index); |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1184 | bool LocalChanged = false; |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1185 | |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1186 | dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1187 | dumpRequiredSet(BP); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1188 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1189 | initializeAnalysisImpl(BP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1190 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1191 | { |
| 1192 | // If the pass crashes, remember this. |
| 1193 | PassManagerPrettyStackEntry X(BP, *I); |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1194 | TimeRegion PassTimer(getPassTimer(BP)); |
| 1195 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1196 | LocalChanged |= BP->runOnBasicBlock(*I); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1197 | } |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1198 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1199 | Changed |= LocalChanged; |
| 1200 | if (LocalChanged) |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 1201 | dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1202 | I->getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1203 | dumpPreservedSet(BP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1204 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1205 | verifyPreservedAnalysis(BP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1206 | removeNotPreservedAnalysis(BP); |
| 1207 | recordAvailableAnalysis(BP); |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1208 | removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1209 | } |
Chris Lattner | de2aa65 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 1210 | |
Bill Wendling | 6ce6d26 | 2009-12-25 13:50:18 +0000 | [diff] [blame] | 1211 | return doFinalization(F) || Changed; |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1214 | // Implement doInitialization and doFinalization |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1215 | bool BBPassManager::doInitialization(Module &M) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1216 | bool Changed = false; |
| 1217 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1218 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1219 | Changed |= getContainedPass(Index)->doInitialization(M); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1220 | |
| 1221 | return Changed; |
| 1222 | } |
| 1223 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1224 | bool BBPassManager::doFinalization(Module &M) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1225 | bool Changed = false; |
| 1226 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1227 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1228 | Changed |= getContainedPass(Index)->doFinalization(M); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1229 | |
| 1230 | return Changed; |
| 1231 | } |
| 1232 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1233 | bool BBPassManager::doInitialization(Function &F) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1234 | bool Changed = false; |
| 1235 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1236 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1237 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1238 | Changed |= BP->doInitialization(F); |
| 1239 | } |
| 1240 | |
| 1241 | return Changed; |
| 1242 | } |
| 1243 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1244 | bool BBPassManager::doFinalization(Function &F) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1245 | bool Changed = false; |
| 1246 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1247 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1248 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1249 | Changed |= BP->doFinalization(F); |
| 1250 | } |
| 1251 | |
| 1252 | return Changed; |
| 1253 | } |
| 1254 | |
| 1255 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1256 | //===----------------------------------------------------------------------===// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1257 | // FunctionPassManager implementation |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1258 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1259 | /// Create new Function pass manager |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1260 | FunctionPassManager::FunctionPassManager(Module *m) : M(m) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1261 | FPM = new FunctionPassManagerImpl(0); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1262 | // FPM is the top level manager. |
| 1263 | FPM->setTopLevelManager(FPM); |
Devang Patel | 1036b65 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1264 | |
Dan Gohman | 565df95 | 2008-03-13 02:08:36 +0000 | [diff] [blame] | 1265 | AnalysisResolver *AR = new AnalysisResolver(*FPM); |
Devang Patel | 1036b65 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1266 | FPM->setResolver(AR); |
Devang Patel | 1f65368 | 2006-12-08 18:57:16 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1269 | FunctionPassManager::~FunctionPassManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1270 | delete FPM; |
| 1271 | } |
| 1272 | |
David Greene | 103d4b4 | 2010-05-10 20:24:27 +0000 | [diff] [blame^] | 1273 | /// addImpl - Add a pass to the queue of passes to run, without |
| 1274 | /// checking whether to add a printer pass. |
| 1275 | void FunctionPassManager::addImpl(Pass *P) { |
| 1276 | FPM->add(P); |
| 1277 | } |
| 1278 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1279 | /// add - Add a pass to the queue of passes to run. This passes |
| 1280 | /// ownership of the Pass to the PassManager. When the |
| 1281 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 1282 | /// there is no need to delete the pass. (TODO delete passes.) |
| 1283 | /// This implies that all passes MUST be allocated with 'new'. |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1284 | void FunctionPassManager::add(Pass *P) { |
David Greene | 103d4b4 | 2010-05-10 20:24:27 +0000 | [diff] [blame^] | 1285 | // If this is a not a function pass, don't add a printer for it. |
| 1286 | if (P->getPassKind() == PT_Function) |
| 1287 | if (ShouldPrintBeforePass(P)) |
| 1288 | addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ") |
| 1289 | + P->getPassName() + " ***")); |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 1290 | |
David Greene | 103d4b4 | 2010-05-10 20:24:27 +0000 | [diff] [blame^] | 1291 | addImpl(P); |
| 1292 | |
| 1293 | if (P->getPassKind() == PT_Function) |
| 1294 | if (ShouldPrintAfterPass(P)) |
| 1295 | addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ") |
| 1296 | + P->getPassName() + " ***")); |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1299 | /// run - Execute all of the passes scheduled for execution. Keep |
| 1300 | /// track of whether any of the passes modifies the function, and if |
| 1301 | /// so, return true. |
| 1302 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1303 | bool FunctionPassManager::run(Function &F) { |
Nick Lewycky | 94e168f | 2010-02-15 21:27:56 +0000 | [diff] [blame] | 1304 | if (F.isMaterializable()) { |
| 1305 | std::string errstr; |
Chris Lattner | b6166b3 | 2010-04-07 22:41:29 +0000 | [diff] [blame] | 1306 | if (F.Materialize(&errstr)) |
Benjamin Kramer | a676926 | 2010-04-08 10:44:28 +0000 | [diff] [blame] | 1307 | report_fatal_error("Error reading bitcode file: " + Twine(errstr)); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1308 | } |
Devang Patel | 272908d | 2006-12-08 22:57:48 +0000 | [diff] [blame] | 1309 | return FPM->run(F); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1313 | /// doInitialization - Run all of the initializers for the function passes. |
| 1314 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1315 | bool FunctionPassManager::doInitialization() { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1316 | return FPM->doInitialization(*M); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Dan Gohman | e6656eb | 2007-07-30 14:51:13 +0000 | [diff] [blame] | 1319 | /// doFinalization - Run all of the finalizers for the function passes. |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1320 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1321 | bool FunctionPassManager::doFinalization() { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1322 | return FPM->doFinalization(*M); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1325 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1326 | // FunctionPassManagerImpl implementation |
| 1327 | // |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1328 | bool FunctionPassManagerImpl::doInitialization(Module &M) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1329 | bool Changed = false; |
| 1330 | |
Dan Gohman | 05ebc8f | 2009-11-23 16:24:18 +0000 | [diff] [blame] | 1331 | dumpArguments(); |
| 1332 | dumpPasses(); |
| 1333 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1334 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1335 | Changed |= getContainedManager(Index)->doInitialization(M); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1336 | |
| 1337 | return Changed; |
| 1338 | } |
| 1339 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1340 | bool FunctionPassManagerImpl::doFinalization(Module &M) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1341 | bool Changed = false; |
| 1342 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1343 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1344 | Changed |= getContainedManager(Index)->doFinalization(M); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1345 | |
| 1346 | return Changed; |
| 1347 | } |
| 1348 | |
Devang Patel | ec9c58f | 2009-04-01 22:34:41 +0000 | [diff] [blame] | 1349 | /// cleanup - After running all passes, clean up pass manager cache. |
| 1350 | void FPPassManager::cleanup() { |
| 1351 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1352 | FunctionPass *FP = getContainedPass(Index); |
| 1353 | AnalysisResolver *AR = FP->getResolver(); |
| 1354 | assert(AR && "Analysis Resolver is not set"); |
| 1355 | AR->clearAnalysisImpls(); |
| 1356 | } |
| 1357 | } |
| 1358 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1359 | void FunctionPassManagerImpl::releaseMemoryOnTheFly() { |
| 1360 | if (!wasRun) |
| 1361 | return; |
| 1362 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1363 | FPPassManager *FPPM = getContainedManager(Index); |
| 1364 | for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) { |
| 1365 | FPPM->getContainedPass(Index)->releaseMemory(); |
| 1366 | } |
| 1367 | } |
Torok Edwin | 896556e | 2009-06-29 21:05:10 +0000 | [diff] [blame] | 1368 | wasRun = false; |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1371 | // Execute all the passes managed by this top level manager. |
| 1372 | // Return true if any function is modified by a pass. |
| 1373 | bool FunctionPassManagerImpl::run(Function &F) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1374 | bool Changed = false; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1375 | TimingInfo::createTheTimeInfo(); |
| 1376 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 1377 | initializeAllAnalysisInfo(); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1378 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1379 | Changed |= getContainedManager(Index)->runOnFunction(F); |
Devang Patel | ec9c58f | 2009-04-01 22:34:41 +0000 | [diff] [blame] | 1380 | |
| 1381 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1382 | getContainedManager(Index)->cleanup(); |
| 1383 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1384 | wasRun = true; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1385 | return Changed; |
| 1386 | } |
| 1387 | |
| 1388 | //===----------------------------------------------------------------------===// |
| 1389 | // FPPassManager implementation |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1390 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 1391 | char FPPassManager::ID = 0; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1392 | /// Print passes managed by this manager |
| 1393 | void FPPassManager::dumpPassStructure(unsigned Offset) { |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1394 | llvm::dbgs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n"; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1395 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1396 | FunctionPass *FP = getContainedPass(Index); |
| 1397 | FP->dumpPassStructure(Offset + 1); |
| 1398 | dumpLastUses(FP, Offset+1); |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1403 | /// Execute all of the passes scheduled for execution by invoking |
| 1404 | /// runOnFunction method. Keep track of whether any of the passes modifies |
| 1405 | /// the function, and if so, return true. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1406 | bool FPPassManager::runOnFunction(Function &F) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1407 | if (F.isDeclaration()) |
| 1408 | return false; |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1409 | |
| 1410 | bool Changed = false; |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1411 | |
Devang Patel | cbbf291 | 2008-03-20 01:09:53 +0000 | [diff] [blame] | 1412 | // Collect inherited analysis from Module level pass manager. |
| 1413 | populateInheritedAnalysis(TPM->activeStack); |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1414 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1415 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1416 | FunctionPass *FP = getContainedPass(Index); |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1417 | bool LocalChanged = false; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1418 | |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1419 | dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1420 | dumpRequiredSet(FP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1421 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1422 | initializeAnalysisImpl(FP); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1423 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1424 | { |
| 1425 | PassManagerPrettyStackEntry X(FP, F); |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1426 | TimeRegion PassTimer(getPassTimer(FP)); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1427 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1428 | LocalChanged |= FP->runOnFunction(F); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1429 | } |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1430 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1431 | Changed |= LocalChanged; |
| 1432 | if (LocalChanged) |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1433 | dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1434 | dumpPreservedSet(FP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1435 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1436 | verifyPreservedAnalysis(FP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1437 | removeNotPreservedAnalysis(FP); |
| 1438 | recordAvailableAnalysis(FP); |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1439 | removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1440 | } |
| 1441 | return Changed; |
| 1442 | } |
| 1443 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1444 | bool FPPassManager::runOnModule(Module &M) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1445 | bool Changed = doInitialization(M); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1446 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1447 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 1448 | runOnFunction(*I); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1449 | |
Bill Wendling | 6ce6d26 | 2009-12-25 13:50:18 +0000 | [diff] [blame] | 1450 | return doFinalization(M) || Changed; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1453 | bool FPPassManager::doInitialization(Module &M) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1454 | bool Changed = false; |
| 1455 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1456 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1457 | Changed |= getContainedPass(Index)->doInitialization(M); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1458 | |
| 1459 | return Changed; |
| 1460 | } |
| 1461 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1462 | bool FPPassManager::doFinalization(Module &M) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1463 | bool Changed = false; |
| 1464 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1465 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1466 | Changed |= getContainedPass(Index)->doFinalization(M); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1467 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1468 | return Changed; |
| 1469 | } |
| 1470 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1471 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1472 | // MPPassManager implementation |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1473 | |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1474 | /// Execute all of the passes scheduled for execution by invoking |
| 1475 | /// runOnModule method. Keep track of whether any of the passes modifies |
| 1476 | /// the module, and if so, return true. |
| 1477 | bool |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1478 | MPPassManager::runOnModule(Module &M) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1479 | bool Changed = false; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 1480 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1481 | // Initialize on-the-fly passes |
| 1482 | for (std::map<Pass *, FunctionPassManagerImpl *>::iterator |
| 1483 | I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); |
| 1484 | I != E; ++I) { |
| 1485 | FunctionPassManagerImpl *FPP = I->second; |
| 1486 | Changed |= FPP->doInitialization(M); |
| 1487 | } |
| 1488 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1489 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1490 | ModulePass *MP = getContainedPass(Index); |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1491 | bool LocalChanged = false; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1492 | |
Benjamin Kramer | dfcc285 | 2009-12-08 13:07:38 +0000 | [diff] [blame] | 1493 | dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1494 | dumpRequiredSet(MP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1495 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1496 | initializeAnalysisImpl(MP); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1497 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1498 | { |
| 1499 | PassManagerPrettyStackEntry X(MP, M); |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1500 | TimeRegion PassTimer(getPassTimer(MP)); |
| 1501 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1502 | LocalChanged |= MP->runOnModule(M); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1503 | } |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1504 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1505 | Changed |= LocalChanged; |
| 1506 | if (LocalChanged) |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 1507 | dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG, |
Benjamin Kramer | dfcc285 | 2009-12-08 13:07:38 +0000 | [diff] [blame] | 1508 | M.getModuleIdentifier()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1509 | dumpPreservedSet(MP); |
Chris Lattner | 02eb94c | 2008-08-07 07:34:50 +0000 | [diff] [blame] | 1510 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1511 | verifyPreservedAnalysis(MP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1512 | removeNotPreservedAnalysis(MP); |
| 1513 | recordAvailableAnalysis(MP); |
Benjamin Kramer | dfcc285 | 2009-12-08 13:07:38 +0000 | [diff] [blame] | 1514 | removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1515 | } |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1516 | |
| 1517 | // Finalize on-the-fly passes |
| 1518 | for (std::map<Pass *, FunctionPassManagerImpl *>::iterator |
| 1519 | I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); |
| 1520 | I != E; ++I) { |
| 1521 | FunctionPassManagerImpl *FPP = I->second; |
| 1522 | // We don't know when is the last time an on-the-fly pass is run, |
| 1523 | // so we need to releaseMemory / finalize here |
| 1524 | FPP->releaseMemoryOnTheFly(); |
| 1525 | Changed |= FPP->doFinalization(M); |
| 1526 | } |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1527 | return Changed; |
| 1528 | } |
| 1529 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1530 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 1531 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 1532 | /// through getAnalysis interface. |
| 1533 | void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1534 | assert(P->getPotentialPassManagerType() == PMT_ModulePassManager && |
| 1535 | "Unable to handle Pass that requires lower level Analysis pass"); |
| 1536 | assert((P->getPotentialPassManagerType() < |
| 1537 | RequiredPass->getPotentialPassManagerType()) && |
| 1538 | "Unable to handle Pass that requires lower level Analysis pass"); |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1539 | |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1540 | FunctionPassManagerImpl *FPP = OnTheFlyManagers[P]; |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1541 | if (!FPP) { |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1542 | FPP = new FunctionPassManagerImpl(0); |
| 1543 | // FPP is the top level manager. |
| 1544 | FPP->setTopLevelManager(FPP); |
| 1545 | |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1546 | OnTheFlyManagers[P] = FPP; |
| 1547 | } |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1548 | FPP->add(RequiredPass); |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1549 | |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1550 | // Register P as the last user of RequiredPass. |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 1551 | SmallVector<Pass *, 12> LU; |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1552 | LU.push_back(RequiredPass); |
| 1553 | FPP->setLastUser(LU, P); |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1554 | } |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1555 | |
| 1556 | /// Return function pass corresponding to PassInfo PI, that is |
| 1557 | /// required by module pass MP. Instantiate analysis pass, by using |
| 1558 | /// its runOnFunction() for function F. |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1559 | Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F){ |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1560 | FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP]; |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1561 | assert(FPP && "Unable to find on the fly pass"); |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1562 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1563 | FPP->releaseMemoryOnTheFly(); |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1564 | FPP->run(F); |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1565 | return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI); |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1569 | //===----------------------------------------------------------------------===// |
| 1570 | // PassManagerImpl implementation |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1571 | // |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1572 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1573 | /// whether any of the passes modifies the module, and if so, return true. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1574 | bool PassManagerImpl::run(Module &M) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1575 | bool Changed = false; |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1576 | TimingInfo::createTheTimeInfo(); |
| 1577 | |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 1578 | dumpArguments(); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1579 | dumpPasses(); |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 1580 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 1581 | initializeAllAnalysisInfo(); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1582 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1583 | Changed |= getContainedManager(Index)->runOnModule(M); |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1584 | return Changed; |
| 1585 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1586 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1587 | //===----------------------------------------------------------------------===// |
| 1588 | // PassManager implementation |
| 1589 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1590 | /// Create new pass manager |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1591 | PassManager::PassManager() { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1592 | PM = new PassManagerImpl(0); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1593 | // PM is the top level manager |
| 1594 | PM->setTopLevelManager(PM); |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1597 | PassManager::~PassManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1598 | delete PM; |
| 1599 | } |
| 1600 | |
David Greene | 103d4b4 | 2010-05-10 20:24:27 +0000 | [diff] [blame^] | 1601 | /// addImpl - Add a pass to the queue of passes to run, without |
| 1602 | /// checking whether to add a printer pass. |
| 1603 | void PassManager::addImpl(Pass *P) { |
| 1604 | PM->add(P); |
| 1605 | } |
| 1606 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1607 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 1608 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 1609 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 1610 | /// implies that all passes MUST be allocated with 'new'. |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1611 | void PassManager::add(Pass *P) { |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 1612 | if (ShouldPrintBeforePass(P)) |
David Greene | 103d4b4 | 2010-05-10 20:24:27 +0000 | [diff] [blame^] | 1613 | addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ") |
| 1614 | + P->getPassName() + " ***")); |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 1615 | |
David Greene | 103d4b4 | 2010-05-10 20:24:27 +0000 | [diff] [blame^] | 1616 | addImpl(P); |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 1617 | |
| 1618 | if (ShouldPrintAfterPass(P)) |
David Greene | 103d4b4 | 2010-05-10 20:24:27 +0000 | [diff] [blame^] | 1619 | addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ") |
| 1620 | + P->getPassName() + " ***")); |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1624 | /// whether any of the passes modifies the module, and if so, return true. |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1625 | bool PassManager::run(Module &M) { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1626 | return PM->run(M); |
| 1627 | } |
| 1628 | |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1629 | //===----------------------------------------------------------------------===// |
| 1630 | // TimingInfo Class - This class is used to calculate information about the |
| 1631 | // amount of time each pass takes to execute. This only happens with |
| 1632 | // -time-passes is enabled on the command line. |
| 1633 | // |
| 1634 | bool llvm::TimePassesIsEnabled = false; |
| 1635 | static cl::opt<bool,true> |
| 1636 | EnableTiming("time-passes", cl::location(TimePassesIsEnabled), |
| 1637 | cl::desc("Time each pass, printing elapsed time for each on exit")); |
| 1638 | |
| 1639 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to |
| 1640 | // a non null value (if the -time-passes option is enabled) or it leaves it |
| 1641 | // null. It may be called multiple times. |
| 1642 | void TimingInfo::createTheTimeInfo() { |
| 1643 | if (!TimePassesIsEnabled || TheTimeInfo) return; |
| 1644 | |
| 1645 | // Constructed the first time this is called, iff -time-passes is enabled. |
| 1646 | // This guarantees that the object will be constructed before static globals, |
| 1647 | // thus it will be destroyed before them. |
| 1648 | static ManagedStatic<TimingInfo> TTI; |
| 1649 | TheTimeInfo = &*TTI; |
| 1650 | } |
| 1651 | |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 1652 | /// If TimingInfo is enabled then start pass timer. |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1653 | Timer *llvm::getPassTimer(Pass *P) { |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 1654 | if (TheTimeInfo) |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1655 | return TheTimeInfo->getPassTimer(P); |
Dan Gohman | 277e767 | 2009-09-28 00:07:05 +0000 | [diff] [blame] | 1656 | return 0; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1659 | //===----------------------------------------------------------------------===// |
| 1660 | // PMStack implementation |
| 1661 | // |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 1662 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1663 | // Pop Pass Manager from the stack and clear its analysis info. |
| 1664 | void PMStack::pop() { |
| 1665 | |
| 1666 | PMDataManager *Top = this->top(); |
| 1667 | Top->initializeAnalysisInfo(); |
| 1668 | |
| 1669 | S.pop_back(); |
| 1670 | } |
| 1671 | |
| 1672 | // Push PM on the stack and set its top level manager. |
Dan Gohman | 11eecd6 | 2008-03-13 01:21:31 +0000 | [diff] [blame] | 1673 | void PMStack::push(PMDataManager *PM) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1674 | assert(PM && "Unable to push. Pass Manager expected"); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1676 | if (!this->empty()) { |
| 1677 | PMTopLevelManager *TPM = this->top()->getTopLevelManager(); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1678 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1679 | assert(TPM && "Unable to find top level manager"); |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1680 | TPM->addIndirectPassManager(PM); |
| 1681 | PM->setTopLevelManager(TPM); |
| 1682 | } |
| 1683 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1684 | S.push_back(PM); |
| 1685 | } |
| 1686 | |
| 1687 | // Dump content of the pass manager stack. |
| 1688 | void PMStack::dump() { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1689 | for (std::deque<PMDataManager *>::iterator I = S.begin(), |
| 1690 | E = S.end(); I != E; ++I) |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 1691 | printf("%s ", (*I)->getAsPass()->getPassName()); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1692 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1693 | if (!S.empty()) |
Chris Lattner | de2aa65 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 1694 | printf("\n"); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1697 | /// Find appropriate Module Pass Manager in the PM Stack and |
| 1698 | /// add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1699 | void ModulePass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1700 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1701 | // Find Module Pass Manager |
| 1702 | while(!PMS.empty()) { |
Devang Patel | 23f8aa9 | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1703 | PassManagerType TopPMType = PMS.top()->getPassManagerType(); |
| 1704 | if (TopPMType == PreferredType) |
| 1705 | break; // We found desired pass manager |
| 1706 | else if (TopPMType > PMT_ModulePassManager) |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1707 | PMS.pop(); // Pop children pass managers |
Devang Patel | ac99eca | 2007-01-11 19:59:06 +0000 | [diff] [blame] | 1708 | else |
| 1709 | break; |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1710 | } |
Devang Patel | 18ff636 | 2008-09-09 21:38:40 +0000 | [diff] [blame] | 1711 | assert(!PMS.empty() && "Unable to find appropriate Pass Manager"); |
Devang Patel | 23f8aa9 | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1712 | PMS.top()->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1715 | /// Find appropriate Function Pass Manager or Call Graph Pass Manager |
| 1716 | /// in the PM Stack and add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1717 | void FunctionPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1718 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1719 | |
Devang Patel | a328690 | 2008-09-09 17:56:50 +0000 | [diff] [blame] | 1720 | // Find Module Pass Manager |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1721 | while (!PMS.empty()) { |
Devang Patel | ac99eca | 2007-01-11 19:59:06 +0000 | [diff] [blame] | 1722 | if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager) |
| 1723 | PMS.pop(); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1724 | else |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1725 | break; |
| 1726 | } |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1727 | |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1728 | // Create new Function Pass Manager if needed. |
| 1729 | FPPassManager *FPP; |
| 1730 | if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) { |
| 1731 | FPP = (FPPassManager *)PMS.top(); |
| 1732 | } else { |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1733 | assert(!PMS.empty() && "Unable to create Function Pass Manager"); |
| 1734 | PMDataManager *PMD = PMS.top(); |
| 1735 | |
| 1736 | // [1] Create new Function Pass Manager |
| 1737 | FPP = new FPPassManager(PMD->getDepth() + 1); |
Devang Patel | cbbf291 | 2008-03-20 01:09:53 +0000 | [diff] [blame] | 1738 | FPP->populateInheritedAnalysis(PMS); |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1739 | |
| 1740 | // [2] Set up new manager's top level manager |
| 1741 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 1742 | TPM->addIndirectPassManager(FPP); |
| 1743 | |
| 1744 | // [3] Assign manager to manage this new manager. This may create |
| 1745 | // and push new managers into PMS |
Devang Patel | a328690 | 2008-09-09 17:56:50 +0000 | [diff] [blame] | 1746 | FPP->assignPassManager(PMS, PMD->getPassManagerType()); |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1747 | |
| 1748 | // [4] Push new manager into PMS |
| 1749 | PMS.push(FPP); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1752 | // Assign FPP as the manager of this pass. |
| 1753 | FPP->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1756 | /// Find appropriate Basic Pass Manager or Call Graph Pass Manager |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1757 | /// in the PM Stack and add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1758 | void BasicBlockPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1759 | PassManagerType PreferredType) { |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1760 | BBPassManager *BBP; |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1761 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1762 | // Basic Pass Manager is a leaf pass manager. It does not handle |
| 1763 | // any other pass manager. |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1764 | if (!PMS.empty() && |
| 1765 | PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) { |
| 1766 | BBP = (BBPassManager *)PMS.top(); |
| 1767 | } else { |
| 1768 | // If leaf manager is not Basic Block Pass manager then create new |
| 1769 | // basic Block Pass manager. |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1770 | assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager"); |
| 1771 | PMDataManager *PMD = PMS.top(); |
| 1772 | |
| 1773 | // [1] Create new Basic Block Manager |
| 1774 | BBP = new BBPassManager(PMD->getDepth() + 1); |
| 1775 | |
| 1776 | // [2] Set up new manager's top level manager |
| 1777 | // Basic Block Pass Manager does not live by itself |
| 1778 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 1779 | TPM->addIndirectPassManager(BBP); |
| 1780 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1781 | // [3] Assign manager to manage this new manager. This may create |
| 1782 | // and push new managers into PMS |
David Greene | 103d4b4 | 2010-05-10 20:24:27 +0000 | [diff] [blame^] | 1783 | BBP->assignPassManager(PMS, PreferredType); |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1784 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1785 | // [4] Push new manager into PMS |
| 1786 | PMS.push(BBP); |
| 1787 | } |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1788 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1789 | // Assign BBP as the manager of this pass. |
| 1790 | BBP->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Dan Gohman | d3a20c9 | 2008-03-11 16:41:42 +0000 | [diff] [blame] | 1793 | PassManagerBase::~PassManagerBase() {} |
Gordon Henriksen | 878114b | 2008-03-16 04:20:44 +0000 | [diff] [blame] | 1794 | |
| 1795 | /*===-- C Bindings --------------------------------------------------------===*/ |
| 1796 | |
| 1797 | LLVMPassManagerRef LLVMCreatePassManager() { |
| 1798 | return wrap(new PassManager()); |
| 1799 | } |
| 1800 | |
Erick Tryzelaar | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 1801 | LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) { |
| 1802 | return wrap(new FunctionPassManager(unwrap(M))); |
| 1803 | } |
| 1804 | |
Gordon Henriksen | 878114b | 2008-03-16 04:20:44 +0000 | [diff] [blame] | 1805 | LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) { |
Erick Tryzelaar | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 1806 | return LLVMCreateFunctionPassManagerForModule( |
| 1807 | reinterpret_cast<LLVMModuleRef>(P)); |
Gordon Henriksen | 878114b | 2008-03-16 04:20:44 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Chris Lattner | 25963c6 | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 1810 | LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) { |
Gordon Henriksen | 878114b | 2008-03-16 04:20:44 +0000 | [diff] [blame] | 1811 | return unwrap<PassManager>(PM)->run(*unwrap(M)); |
| 1812 | } |
| 1813 | |
Chris Lattner | 25963c6 | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 1814 | LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) { |
Gordon Henriksen | 878114b | 2008-03-16 04:20:44 +0000 | [diff] [blame] | 1815 | return unwrap<FunctionPassManager>(FPM)->doInitialization(); |
| 1816 | } |
| 1817 | |
Chris Lattner | 25963c6 | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 1818 | LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) { |
Gordon Henriksen | 878114b | 2008-03-16 04:20:44 +0000 | [diff] [blame] | 1819 | return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F)); |
| 1820 | } |
| 1821 | |
Chris Lattner | 25963c6 | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 1822 | LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) { |
Gordon Henriksen | 878114b | 2008-03-16 04:20:44 +0000 | [diff] [blame] | 1823 | return unwrap<FunctionPassManager>(FPM)->doFinalization(); |
| 1824 | } |
| 1825 | |
| 1826 | void LLVMDisposePassManager(LLVMPassManagerRef PM) { |
| 1827 | delete unwrap(PM); |
| 1828 | } |