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