Devang Patel | 55fd43f | 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 | 4ee451d | 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 | 55fd43f | 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 | ab7752c | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 15 | #include "llvm/PassManagers.h" |
Devang Patel | 45dc02d | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CommandLine.h" |
Devang Patel | c874eb5 | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Timer.h" |
Devang Patel | 55fd43f | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Devang Patel | 3799f97 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 19 | #include "llvm/ModuleProvider.h" |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Streams.h" |
Devang Patel | 8e58a1b | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ManagedStatic.h" |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Devang Patel | 85d344b | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 23 | #include <vector> |
Devang Patel | b899eed | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 24 | #include <map> |
Dan Gohman | 2bb7d06 | 2007-10-03 19:04:09 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Devang Patel | c2ff962 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 26 | |
Devang Patel | ab7752c | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 27 | // See PassManagers.h for Pass Manager infrastructure overview. |
Devang Patel | e77242c | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 28 | |
Devang Patel | 45dc02d | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 29 | namespace llvm { |
| 30 | |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | // Pass debugging information. Often it is useful to find out what pass is |
| 33 | // running when a crash occurs in a utility. When this library is compiled with |
| 34 | // debugging on, a command line option (--debug-pass) is enabled that causes the |
| 35 | // pass name to be printed before it executes. |
| 36 | // |
| 37 | |
Devang Patel | e8ff1ce | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 38 | // Different debug levels that can be enabled... |
| 39 | enum PassDebugLevel { |
| 40 | None, Arguments, Structure, Executions, Details |
| 41 | }; |
| 42 | |
Devang Patel | 45dc02d | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 43 | static cl::opt<enum PassDebugLevel> |
Devang Patel | 2642694 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 44 | PassDebugging("debug-pass", cl::Hidden, |
Devang Patel | 45dc02d | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 45 | cl::desc("Print PassManager debugging information"), |
| 46 | cl::values( |
Devang Patel | e8ff1ce | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 47 | clEnumVal(None , "disable debug output"), |
| 48 | clEnumVal(Arguments , "print pass arguments to pass to 'opt'"), |
| 49 | clEnumVal(Structure , "print pass structure before run()"), |
| 50 | clEnumVal(Executions, "print pass name before it is executed"), |
| 51 | clEnumVal(Details , "print pass details when it is executed"), |
Devang Patel | 45dc02d | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 52 | clEnumValEnd)); |
| 53 | } // End of llvm namespace |
| 54 | |
Devang Patel | c2ff962 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 55 | namespace { |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 56 | |
Devang Patel | 3f5d2b5 | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 57 | //===----------------------------------------------------------------------===// |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 58 | // BBPassManager |
Devang Patel | 7e601a7 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 59 | // |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 60 | /// BBPassManager manages BasicBlockPass. It batches all the |
Devang Patel | c67c938 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 61 | /// pass together and sequence them to process one basic block before |
| 62 | /// processing next basic block. |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 63 | class VISIBILITY_HIDDEN BBPassManager : public PMDataManager, |
| 64 | public FunctionPass { |
Devang Patel | c67c938 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 65 | |
| 66 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 67 | static char ID; |
Dan Gohman | cdf2b3b | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 68 | explicit BBPassManager(int Depth) |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 69 | : PMDataManager(Depth), FunctionPass((intptr_t)&ID) {} |
Devang Patel | c67c938 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 70 | |
Devang Patel | c67c938 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 71 | /// Execute all of the passes scheduled for execution. Keep track of |
| 72 | /// whether any of the passes modifies the function, and if so, return true. |
| 73 | bool runOnFunction(Function &F); |
| 74 | |
Devang Patel | 66d72e1 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 75 | /// Pass Manager itself does not invalidate any analysis info. |
| 76 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 77 | Info.setPreservesAll(); |
| 78 | } |
| 79 | |
Devang Patel | 964e45e | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 80 | bool doInitialization(Module &M); |
| 81 | bool doInitialization(Function &F); |
| 82 | bool doFinalization(Module &M); |
| 83 | bool doFinalization(Function &F); |
| 84 | |
Devang Patel | e27ae7e | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 85 | virtual const char *getPassName() const { |
| 86 | return "BasicBlock Pass Manager"; |
| 87 | } |
| 88 | |
Devang Patel | ebc0922 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 89 | // Print passes managed by this manager |
| 90 | void dumpPassStructure(unsigned Offset) { |
Devang Patel | c2ff962 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 91 | llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n"; |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 92 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 93 | BasicBlockPass *BP = getContainedPass(Index); |
| 94 | BP->dumpPassStructure(Offset + 1); |
| 95 | dumpLastUses(BP, Offset+1); |
Devang Patel | ebc0922 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 98 | |
| 99 | BasicBlockPass *getContainedPass(unsigned N) { |
| 100 | assert ( N < PassVector.size() && "Pass number out of range!"); |
| 101 | BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]); |
| 102 | return BP; |
| 103 | } |
Devang Patel | 25919cb | 2007-01-11 01:10:25 +0000 | [diff] [blame] | 104 | |
Devang Patel | 84da80d | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 105 | virtual PassManagerType getPassManagerType() const { |
Devang Patel | 25919cb | 2007-01-11 01:10:25 +0000 | [diff] [blame] | 106 | return PMT_BasicBlockPassManager; |
| 107 | } |
Devang Patel | c67c938 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 110 | char BBPassManager::ID = 0; |
Devang Patel | ab7752c | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 111 | } |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 112 | |
Devang Patel | ab7752c | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 113 | namespace llvm { |
Devang Patel | c67c938 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 114 | |
Devang Patel | 7e601a7 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 115 | //===----------------------------------------------------------------------===// |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 116 | // FunctionPassManagerImpl |
Devang Patel | 7e601a7 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 117 | // |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 118 | /// FunctionPassManagerImpl manages FPPassManagers |
| 119 | class FunctionPassManagerImpl : public Pass, |
Devang Patel | 36bcb82 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 120 | public PMDataManager, |
| 121 | public PMTopLevelManager { |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 122 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 123 | static char ID; |
Dan Gohman | cdf2b3b | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 124 | explicit FunctionPassManagerImpl(int Depth) : |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 125 | Pass((intptr_t)&ID), PMDataManager(Depth), |
| 126 | PMTopLevelManager(TLM_Function) { } |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 127 | |
| 128 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 129 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 130 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 131 | /// implies that all passes MUST be allocated with 'new'. |
| 132 | void add(Pass *P) { |
| 133 | schedulePass(P); |
| 134 | } |
| 135 | |
| 136 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 137 | /// whether any of the passes modifies the module, and if so, return true. |
| 138 | bool run(Function &F); |
| 139 | |
| 140 | /// doInitialization - Run all of the initializers for the function passes. |
| 141 | /// |
| 142 | bool doInitialization(Module &M); |
| 143 | |
Dan Gohman | 209ee18 | 2007-07-30 14:51:13 +0000 | [diff] [blame] | 144 | /// doFinalization - Run all of the finalizers for the function passes. |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 145 | /// |
| 146 | bool doFinalization(Module &M); |
| 147 | |
| 148 | /// Pass Manager itself does not invalidate any analysis info. |
| 149 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 150 | Info.setPreservesAll(); |
| 151 | } |
| 152 | |
| 153 | inline void addTopLevelPass(Pass *P) { |
| 154 | |
| 155 | if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) { |
| 156 | |
| 157 | // P is a immutable pass and it will be managed by this |
| 158 | // top level manager. Set up analysis resolver to connect them. |
Devang Patel | cde53d3 | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 159 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 160 | P->setResolver(AR); |
| 161 | initializeAnalysisImpl(P); |
| 162 | addImmutablePass(IP); |
| 163 | recordAvailableAnalysis(IP); |
Devang Patel | a0dd987 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 164 | } else { |
Devang Patel | a0dd987 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 165 | P->assignPassManager(activeStack); |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 166 | } |
Devang Patel | a0dd987 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 167 | |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | FPPassManager *getContainedManager(unsigned N) { |
| 171 | assert ( N < PassManagers.size() && "Pass number out of range!"); |
| 172 | FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]); |
| 173 | return FP; |
| 174 | } |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 177 | char FunctionPassManagerImpl::ID = 0; |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 178 | //===----------------------------------------------------------------------===// |
| 179 | // MPPassManager |
| 180 | // |
| 181 | /// MPPassManager manages ModulePasses and function pass managers. |
Devang Patel | c67c938 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 182 | /// It batches all Module passes passes and function pass managers together and |
| 183 | /// sequence them to process one module. |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 184 | class MPPassManager : public Pass, public PMDataManager { |
Devang Patel | c67c938 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 185 | |
| 186 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 187 | static char ID; |
Dan Gohman | cdf2b3b | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 188 | explicit MPPassManager(int Depth) : |
| 189 | Pass((intptr_t)&ID), PMDataManager(Depth) { } |
Devang Patel | 693941b | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 190 | |
| 191 | // Delete on the fly managers. |
| 192 | virtual ~MPPassManager() { |
Devang Patel | dfa1ec3 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 193 | for (std::map<Pass *, FunctionPassManagerImpl *>::iterator |
Devang Patel | 693941b | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 194 | I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); |
| 195 | I != E; ++I) { |
Devang Patel | dfa1ec3 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 196 | FunctionPassManagerImpl *FPP = I->second; |
Devang Patel | 693941b | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 197 | delete FPP; |
| 198 | } |
| 199 | } |
| 200 | |
Devang Patel | c67c938 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 201 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 202 | /// whether any of the passes modifies the module, and if so, return true. |
| 203 | bool runOnModule(Module &M); |
Devang Patel | be6d515 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 204 | |
Devang Patel | 66d72e1 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 205 | /// Pass Manager itself does not invalidate any analysis info. |
| 206 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 207 | Info.setPreservesAll(); |
| 208 | } |
| 209 | |
Devang Patel | 569a6fd | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 210 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 211 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 212 | /// through getAnalysis interface. |
| 213 | virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass); |
| 214 | |
Devang Patel | 0ed8df3 | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 215 | /// Return function pass corresponding to PassInfo PI, that is |
| 216 | /// required by module pass MP. Instantiate analysis pass, by using |
| 217 | /// its runOnFunction() for function F. |
| 218 | virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F); |
| 219 | |
Devang Patel | e27ae7e | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 220 | virtual const char *getPassName() const { |
| 221 | return "Module Pass Manager"; |
| 222 | } |
| 223 | |
Devang Patel | ebc0922 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 224 | // Print passes managed by this manager |
| 225 | void dumpPassStructure(unsigned Offset) { |
| 226 | llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n"; |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 227 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 228 | ModulePass *MP = getContainedPass(Index); |
| 229 | MP->dumpPassStructure(Offset + 1); |
Devang Patel | dfa1ec3 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 230 | if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP]) |
Devang Patel | 693941b | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 231 | FPP->dumpPassStructure(Offset + 2); |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 232 | dumpLastUses(MP, Offset+1); |
Devang Patel | ebc0922 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 236 | ModulePass *getContainedPass(unsigned N) { |
| 237 | assert ( N < PassVector.size() && "Pass number out of range!"); |
| 238 | ModulePass *MP = static_cast<ModulePass *>(PassVector[N]); |
| 239 | return MP; |
| 240 | } |
| 241 | |
Devang Patel | 84da80d | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 242 | virtual PassManagerType getPassManagerType() const { |
| 243 | return PMT_ModulePassManager; |
| 244 | } |
Devang Patel | 0ed8df3 | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 245 | |
| 246 | private: |
| 247 | /// Collection of on the fly FPPassManagers. These managers manage |
| 248 | /// function passes that are required by module passes. |
Devang Patel | dfa1ec3 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 249 | std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers; |
Devang Patel | c67c938 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 250 | }; |
| 251 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 252 | char MPPassManager::ID = 0; |
Devang Patel | 7e601a7 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 253 | //===----------------------------------------------------------------------===// |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 254 | // PassManagerImpl |
Devang Patel | 7e601a7 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 255 | // |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 256 | |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 257 | /// PassManagerImpl manages MPPassManagers |
| 258 | class PassManagerImpl : public Pass, |
Devang Patel | 36bcb82 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 259 | public PMDataManager, |
| 260 | public PMTopLevelManager { |
Devang Patel | 5a39b2e | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 261 | |
| 262 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 263 | static char ID; |
Dan Gohman | cdf2b3b | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 264 | explicit PassManagerImpl(int Depth) : |
| 265 | Pass((intptr_t)&ID), PMDataManager(Depth), |
| 266 | PMTopLevelManager(TLM_Pass) { } |
Devang Patel | f72d29c | 2006-12-07 23:24:58 +0000 | [diff] [blame] | 267 | |
Devang Patel | 5a39b2e | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 268 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 269 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 270 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 271 | /// implies that all passes MUST be allocated with 'new'. |
Devang Patel | 877bfbb | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 272 | void add(Pass *P) { |
Devang Patel | e61b747 | 2006-12-08 22:34:02 +0000 | [diff] [blame] | 273 | schedulePass(P); |
Devang Patel | 877bfbb | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 274 | } |
Devang Patel | 5a39b2e | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 275 | |
| 276 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 277 | /// whether any of the passes modifies the module, and if so, return true. |
| 278 | bool run(Module &M); |
| 279 | |
Devang Patel | 66d72e1 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 280 | /// Pass Manager itself does not invalidate any analysis info. |
| 281 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 282 | Info.setPreservesAll(); |
| 283 | } |
| 284 | |
Devang Patel | eb0d613 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 285 | inline void addTopLevelPass(Pass *P) { |
Devang Patel | 145e83d | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 286 | |
Devang Patel | c9a6293 | 2006-12-08 23:57:43 +0000 | [diff] [blame] | 287 | if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) { |
Devang Patel | 145e83d | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 288 | |
| 289 | // P is a immutable pass and it will be managed by this |
| 290 | // top level manager. Set up analysis resolver to connect them. |
Devang Patel | cde53d3 | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 291 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | 145e83d | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 292 | P->setResolver(AR); |
Devang Patel | 689c683 | 2006-12-12 22:21:37 +0000 | [diff] [blame] | 293 | initializeAnalysisImpl(P); |
Devang Patel | c9a6293 | 2006-12-08 23:57:43 +0000 | [diff] [blame] | 294 | addImmutablePass(IP); |
Devang Patel | 689c683 | 2006-12-12 22:21:37 +0000 | [diff] [blame] | 295 | recordAvailableAnalysis(IP); |
Devang Patel | a0dd987 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 296 | } else { |
Devang Patel | a0dd987 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 297 | P->assignPassManager(activeStack); |
Devang Patel | 145e83d | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 298 | } |
Devang Patel | a0dd987 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 299 | |
Devang Patel | eb0d613 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 302 | MPPassManager *getContainedManager(unsigned N) { |
| 303 | assert ( N < PassManagers.size() && "Pass number out of range!"); |
| 304 | MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]); |
| 305 | return MP; |
| 306 | } |
| 307 | |
Devang Patel | 5a39b2e | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 310 | char PassManagerImpl::ID = 0; |
Devang Patel | c874eb5 | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 311 | } // End of llvm namespace |
| 312 | |
| 313 | namespace { |
| 314 | |
| 315 | //===----------------------------------------------------------------------===// |
| 316 | // TimingInfo Class - This class is used to calculate information about the |
| 317 | // amount of time each pass takes to execute. This only happens when |
| 318 | // -time-passes is enabled on the command line. |
| 319 | // |
| 320 | |
| 321 | class VISIBILITY_HIDDEN TimingInfo { |
| 322 | std::map<Pass*, Timer> TimingData; |
| 323 | TimerGroup TG; |
| 324 | |
| 325 | public: |
| 326 | // Use 'create' member to get this. |
| 327 | TimingInfo() : TG("... Pass execution timing report ...") {} |
| 328 | |
| 329 | // TimingDtor - Print out information about timing information |
| 330 | ~TimingInfo() { |
| 331 | // Delete all of the timers... |
| 332 | TimingData.clear(); |
| 333 | // TimerGroup is deleted next, printing the report. |
| 334 | } |
| 335 | |
| 336 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer |
| 337 | // to a non null value (if the -time-passes option is enabled) or it leaves it |
| 338 | // null. It may be called multiple times. |
| 339 | static void createTheTimeInfo(); |
| 340 | |
| 341 | void passStarted(Pass *P) { |
| 342 | |
| 343 | if (dynamic_cast<PMDataManager *>(P)) |
| 344 | return; |
| 345 | |
| 346 | std::map<Pass*, Timer>::iterator I = TimingData.find(P); |
| 347 | if (I == TimingData.end()) |
| 348 | I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first; |
| 349 | I->second.startTimer(); |
| 350 | } |
| 351 | void passEnded(Pass *P) { |
| 352 | |
| 353 | if (dynamic_cast<PMDataManager *>(P)) |
| 354 | return; |
| 355 | |
| 356 | std::map<Pass*, Timer>::iterator I = TimingData.find(P); |
| 357 | assert (I != TimingData.end() && "passStarted/passEnded not nested right!"); |
| 358 | I->second.stopTimer(); |
| 359 | } |
| 360 | }; |
| 361 | |
Devang Patel | 8e58a1b | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 362 | static TimingInfo *TheTimeInfo; |
| 363 | |
Devang Patel | c874eb5 | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 364 | } // End of anon namespace |
Devang Patel | c67c938 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 365 | |
Devang Patel | 06e8656 | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 366 | //===----------------------------------------------------------------------===// |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 367 | // PMTopLevelManager implementation |
| 368 | |
Devang Patel | 8f3f3d1 | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 369 | /// Initialize top level manager. Create first pass manager. |
| 370 | PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) { |
| 371 | |
| 372 | if (t == TLM_Pass) { |
| 373 | MPPassManager *MPP = new MPPassManager(1); |
| 374 | MPP->setTopLevelManager(this); |
| 375 | addPassManager(MPP); |
| 376 | activeStack.push(MPP); |
| 377 | } |
| 378 | else if (t == TLM_Function) { |
| 379 | FPPassManager *FPP = new FPPassManager(1); |
| 380 | FPP->setTopLevelManager(this); |
| 381 | addPassManager(FPP); |
| 382 | activeStack.push(FPP); |
| 383 | } |
| 384 | } |
| 385 | |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 386 | /// Set pass P as the last user of the given analysis passes. |
Devang Patel | edbef38 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 387 | void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 388 | Pass *P) { |
| 389 | |
Devang Patel | edbef38 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 390 | for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(), |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 391 | E = AnalysisPasses.end(); I != E; ++I) { |
| 392 | Pass *AP = *I; |
| 393 | LastUser[AP] = P; |
Devang Patel | d46825c | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 394 | |
| 395 | if (P == AP) |
| 396 | continue; |
| 397 | |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 398 | // If AP is the last user of other passes then make P last user of |
| 399 | // such passes. |
| 400 | for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(), |
| 401 | LUE = LastUser.end(); LUI != LUE; ++LUI) { |
| 402 | if (LUI->second == AP) |
| 403 | LastUser[LUI->first] = P; |
| 404 | } |
| 405 | } |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /// Collect passes whose last user is P |
Devang Patel | edbef38 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 409 | void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses, |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 410 | Pass *P) { |
Chris Lattner | 63925c8 | 2007-02-17 23:14:24 +0000 | [diff] [blame] | 411 | for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(), |
| 412 | LUE = LastUser.end(); LUI != LUE; ++LUI) |
| 413 | if (LUI->second == P) |
| 414 | LastUses.push_back(LUI->first); |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /// Schedule pass P for execution. Make sure that passes required by |
| 418 | /// P are run before P is run. Update analysis info maintained by |
| 419 | /// the manager. Remove dead passes. This is a recursive function. |
| 420 | void PMTopLevelManager::schedulePass(Pass *P) { |
| 421 | |
Devang Patel | 9d133e1 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 422 | // TODO : Allocate function manager for this pass, other wise required set |
| 423 | // may be inserted into previous function manager |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 424 | |
Devang Patel | 22a1cf9 | 2007-03-06 01:06:16 +0000 | [diff] [blame] | 425 | // Give pass a chance to prepare the stage. |
| 426 | P->preparePassManager(activeStack); |
| 427 | |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 428 | AnalysisUsage AnUsage; |
| 429 | P->getAnalysisUsage(AnUsage); |
| 430 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
| 431 | for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(), |
| 432 | E = RequiredSet.end(); I != E; ++I) { |
| 433 | |
| 434 | Pass *AnalysisPass = findAnalysisPass(*I); |
| 435 | if (!AnalysisPass) { |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 436 | AnalysisPass = (*I)->createPass(); |
Devang Patel | 569a6fd | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 437 | // Schedule this analysis run first only if it is not a lower level |
| 438 | // analysis pass. Lower level analsyis passes are run on the fly. |
| 439 | if (P->getPotentialPassManagerType () >= |
| 440 | AnalysisPass->getPotentialPassManagerType()) |
| 441 | schedulePass(AnalysisPass); |
| 442 | else |
| 443 | delete AnalysisPass; |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | // Now all required passes are available. |
| 448 | addTopLevelPass(P); |
| 449 | } |
| 450 | |
| 451 | /// Find the pass that implements Analysis AID. Search immutable |
| 452 | /// passes and all pass managers. If desired pass is not found |
| 453 | /// then return NULL. |
| 454 | Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { |
| 455 | |
| 456 | Pass *P = NULL; |
Devang Patel | d0fa16c | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 457 | // Check pass managers |
| 458 | for (std::vector<Pass *>::iterator I = PassManagers.begin(), |
| 459 | E = PassManagers.end(); P == NULL && I != E; ++I) { |
| 460 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I); |
| 461 | assert(PMD && "This is not a PassManager"); |
| 462 | P = PMD->findAnalysisPass(AID, false); |
| 463 | } |
| 464 | |
| 465 | // Check other pass managers |
| 466 | for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(), |
| 467 | E = IndirectPassManagers.end(); P == NULL && I != E; ++I) |
| 468 | P = (*I)->findAnalysisPass(AID, false); |
| 469 | |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 470 | for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(), |
| 471 | E = ImmutablePasses.end(); P == NULL && I != E; ++I) { |
| 472 | const PassInfo *PI = (*I)->getPassInfo(); |
| 473 | if (PI == AID) |
| 474 | P = *I; |
| 475 | |
| 476 | // If Pass not found then check the interfaces implemented by Immutable Pass |
| 477 | if (!P) { |
Devang Patel | 1a80386 | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 478 | const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented(); |
| 479 | if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end()) |
| 480 | P = *I; |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 484 | return P; |
| 485 | } |
| 486 | |
Devang Patel | ebc0922 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 487 | // Print passes managed by this top level manager. |
Devang Patel | a52035a | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 488 | void PMTopLevelManager::dumpPasses() const { |
Devang Patel | ebc0922 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 489 | |
Devang Patel | 2642694 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 490 | if (PassDebugging < Structure) |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 491 | return; |
| 492 | |
Devang Patel | ebc0922 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 493 | // Print out the immutable passes |
| 494 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { |
| 495 | ImmutablePasses[i]->dumpPassStructure(0); |
| 496 | } |
| 497 | |
Devang Patel | a52035a | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 498 | for (std::vector<Pass *>::const_iterator I = PassManagers.begin(), |
Devang Patel | ebc0922 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 499 | E = PassManagers.end(); I != E; ++I) |
| 500 | (*I)->dumpPassStructure(1); |
Devang Patel | ebc0922 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Devang Patel | a52035a | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 503 | void PMTopLevelManager::dumpArguments() const { |
Devang Patel | c32cf54 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 504 | |
Devang Patel | 2642694 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 505 | if (PassDebugging < Arguments) |
Devang Patel | c32cf54 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 506 | return; |
| 507 | |
| 508 | cerr << "Pass Arguments: "; |
Devang Patel | a52035a | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 509 | for (std::vector<Pass *>::const_iterator I = PassManagers.begin(), |
Devang Patel | c32cf54 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 510 | E = PassManagers.end(); I != E; ++I) { |
| 511 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I); |
| 512 | assert(PMD && "This is not a PassManager"); |
| 513 | PMD->dumpPassArguments(); |
| 514 | } |
| 515 | cerr << "\n"; |
| 516 | } |
| 517 | |
Devang Patel | 1336a6b | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 518 | void PMTopLevelManager::initializeAllAnalysisInfo() { |
| 519 | |
| 520 | for (std::vector<Pass *>::iterator I = PassManagers.begin(), |
| 521 | E = PassManagers.end(); I != E; ++I) { |
| 522 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I); |
| 523 | assert(PMD && "This is not a PassManager"); |
| 524 | PMD->initializeAnalysisInfo(); |
| 525 | } |
| 526 | |
| 527 | // Initailize other pass managers |
| 528 | for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(), |
| 529 | E = IndirectPassManagers.end(); I != E; ++I) |
| 530 | (*I)->initializeAnalysisInfo(); |
| 531 | } |
| 532 | |
Devang Patel | ab7752c | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 533 | /// Destructor |
| 534 | PMTopLevelManager::~PMTopLevelManager() { |
| 535 | for (std::vector<Pass *>::iterator I = PassManagers.begin(), |
| 536 | E = PassManagers.end(); I != E; ++I) |
| 537 | delete *I; |
| 538 | |
| 539 | for (std::vector<ImmutablePass *>::iterator |
| 540 | I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) |
| 541 | delete *I; |
| 542 | |
| 543 | PassManagers.clear(); |
| 544 | } |
| 545 | |
Devang Patel | 1b8d015 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 546 | //===----------------------------------------------------------------------===// |
Devang Patel | 419f0e9 | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 547 | // PMDataManager implementation |
Devang Patel | 889739c | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 548 | |
Devang Patel | 2c5d185 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 549 | /// Return true IFF pass P's required analysis set does not required new |
Devang Patel | 889739c | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 550 | /// manager. |
Devang Patel | 419f0e9 | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 551 | bool PMDataManager::manageablePass(Pass *P) { |
Devang Patel | 889739c | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 552 | |
Devang Patel | 4045af1 | 2006-12-07 18:47:25 +0000 | [diff] [blame] | 553 | // TODO |
| 554 | // If this pass is not preserving information that is required by a |
| 555 | // pass maintained by higher level pass manager then do not insert |
| 556 | // this pass into current manager. Use new manager. For example, |
| 557 | // For example, If FunctionPass F is not preserving ModulePass Info M1 |
| 558 | // that is used by another ModulePass M2 then do not insert F in |
| 559 | // current function pass manager. |
Devang Patel | 889739c | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 560 | return true; |
| 561 | } |
| 562 | |
Devang Patel | b852616 | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 563 | /// Augement AvailableAnalysis by adding analysis made available by pass P. |
Devang Patel | f32b4dd | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 564 | void PMDataManager::recordAvailableAnalysis(Pass *P) { |
Devang Patel | b899eed | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 565 | |
Devang Patel | b852616 | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 566 | if (const PassInfo *PI = P->getPassInfo()) { |
Devang Patel | b899eed | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 567 | AvailableAnalysis[PI] = P; |
Devang Patel | b852616 | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 568 | |
Devang Patel | f32b4dd | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 569 | //This pass is the current implementation of all of the interfaces it |
| 570 | //implements as well. |
| 571 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 572 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 573 | AvailableAnalysis[II[i]] = P; |
Devang Patel | b852616 | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
Devang Patel | 7b65dd9 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 577 | // Return true if P preserves high level analysis used by other |
| 578 | // passes managed by this manager |
| 579 | bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) { |
| 580 | |
| 581 | AnalysisUsage AnUsage; |
| 582 | P->getAnalysisUsage(AnUsage); |
| 583 | |
| 584 | if (AnUsage.getPreservesAll()) |
| 585 | return true; |
| 586 | |
| 587 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
| 588 | for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(), |
| 589 | E = HigherLevelAnalysis.end(); I != E; ++I) { |
| 590 | Pass *P1 = *I; |
Devang Patel | d46825c | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 591 | if (!dynamic_cast<ImmutablePass*>(P1) |
| 592 | && std::find(PreservedSet.begin(), PreservedSet.end(), P1->getPassInfo()) == |
| 593 | PreservedSet.end()) |
| 594 | return false; |
Devang Patel | 7b65dd9 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | return true; |
| 598 | } |
| 599 | |
Devang Patel | 58e0ef1 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 600 | /// verifyPreservedAnalysis -- Verify analysis presreved by pass P. |
| 601 | void PMDataManager::verifyPreservedAnalysis(Pass *P) { |
Devang Patel | 14d6581 | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 602 | AnalysisUsage AnUsage; |
| 603 | P->getAnalysisUsage(AnUsage); |
Devang Patel | 9750b5d | 2007-07-19 05:36:09 +0000 | [diff] [blame] | 604 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
Devang Patel | 889739c | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 605 | |
Devang Patel | 9750b5d | 2007-07-19 05:36:09 +0000 | [diff] [blame] | 606 | // Verify preserved analysis |
Devang Patel | 58e0ef1 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 607 | for (std::vector<AnalysisID>::const_iterator I = PreservedSet.begin(), |
| 608 | E = PreservedSet.end(); I != E; ++I) { |
| 609 | AnalysisID AID = *I; |
| 610 | Pass *AP = findAnalysisPass(AID, true); |
| 611 | if (AP) |
| 612 | AP->verifyAnalysis(); |
Devang Patel | 9750b5d | 2007-07-19 05:36:09 +0000 | [diff] [blame] | 613 | } |
Devang Patel | 58e0ef1 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /// Remove Analyss not preserved by Pass P |
| 617 | void PMDataManager::removeNotPreservedAnalysis(Pass *P) { |
| 618 | AnalysisUsage AnUsage; |
| 619 | P->getAnalysisUsage(AnUsage); |
Devang Patel | 04b4e05 | 2006-12-07 20:03:49 +0000 | [diff] [blame] | 620 | if (AnUsage.getPreservesAll()) |
| 621 | return; |
| 622 | |
Devang Patel | 58e0ef1 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 623 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
Devang Patel | b899eed | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 624 | for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), |
Devang Patel | 54e247d | 2006-12-12 23:07:44 +0000 | [diff] [blame] | 625 | E = AvailableAnalysis.end(); I != E; ) { |
Devang Patel | 1a80386 | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 626 | std::map<AnalysisID, Pass*>::iterator Info = I++; |
Devang Patel | d46825c | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 627 | if (!dynamic_cast<ImmutablePass*>(Info->second) |
| 628 | && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == |
| 629 | PreservedSet.end()) |
Devang Patel | 14d6581 | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 630 | // Remove this analysis |
Devang Patel | d46825c | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 631 | AvailableAnalysis.erase(Info); |
Devang Patel | 14d6581 | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 632 | } |
Devang Patel | fe61390 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 633 | |
| 634 | // Check inherited analysis also. If P is not preserving analysis |
| 635 | // provided by parent manager then remove it here. |
| 636 | for (unsigned Index = 0; Index < PMT_Last; ++Index) { |
| 637 | |
| 638 | if (!InheritedAnalysis[Index]) |
| 639 | continue; |
| 640 | |
| 641 | for (std::map<AnalysisID, Pass*>::iterator |
| 642 | I = InheritedAnalysis[Index]->begin(), |
| 643 | E = InheritedAnalysis[Index]->end(); I != E; ) { |
| 644 | std::map<AnalysisID, Pass *>::iterator Info = I++; |
Devang Patel | d46825c | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 645 | if (!dynamic_cast<ImmutablePass*>(Info->second) |
| 646 | && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == |
| 647 | PreservedSet.end()) |
Devang Patel | fe61390 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 648 | // Remove this analysis |
Devang Patel | d46825c | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 649 | InheritedAnalysis[Index]->erase(Info); |
Devang Patel | fe61390 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
Devang Patel | 889739c | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Devang Patel | df1a10e | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 655 | /// Remove analysis passes that are not used any longer |
Devang Patel | 6b4af74 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 656 | void PMDataManager::removeDeadPasses(Pass *P, const char *Msg, |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 657 | enum PassDebuggingString DBG_STR) { |
Devang Patel | f9a60ae | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 658 | |
Devang Patel | edbef38 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 659 | SmallVector<Pass *, 12> DeadPasses; |
Devang Patel | 0ed8df3 | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 660 | |
Devang Patel | 693941b | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 661 | // If this is a on the fly manager then it does not have TPM. |
Devang Patel | 0ed8df3 | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 662 | if (!TPM) |
| 663 | return; |
| 664 | |
Devang Patel | f9a60ae | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 665 | TPM->collectLastUses(DeadPasses, P); |
| 666 | |
Devang Patel | edbef38 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 667 | for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(), |
Devang Patel | f9a60ae | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 668 | E = DeadPasses.end(); I != E; ++I) { |
Devang Patel | 4eeea77 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 669 | |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 670 | dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg); |
Devang Patel | 4eeea77 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 671 | |
Devang Patel | 55d5ac7 | 2007-03-05 18:20:51 +0000 | [diff] [blame] | 672 | if (TheTimeInfo) TheTimeInfo->passStarted(*I); |
Devang Patel | f9a60ae | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 673 | (*I)->releaseMemory(); |
Devang Patel | 55d5ac7 | 2007-03-05 18:20:51 +0000 | [diff] [blame] | 674 | if (TheTimeInfo) TheTimeInfo->passEnded(*I); |
Devang Patel | 8e58a1b | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 675 | |
Devang Patel | f9a60ae | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 676 | std::map<AnalysisID, Pass*>::iterator Pos = |
| 677 | AvailableAnalysis.find((*I)->getPassInfo()); |
| 678 | |
Devang Patel | 964e45e | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 679 | // It is possible that pass is already removed from the AvailableAnalysis |
Devang Patel | f9a60ae | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 680 | if (Pos != AvailableAnalysis.end()) |
| 681 | AvailableAnalysis.erase(Pos); |
| 682 | } |
Devang Patel | df1a10e | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Devang Patel | 4045af1 | 2006-12-07 18:47:25 +0000 | [diff] [blame] | 685 | /// Add pass P into the PassVector. Update |
Devang Patel | 893a5a6 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 686 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
Devang Patel | e24e0e1 | 2007-01-12 20:07:16 +0000 | [diff] [blame] | 687 | void PMDataManager::add(Pass *P, |
Devang Patel | 66eeb49 | 2007-01-15 23:06:56 +0000 | [diff] [blame] | 688 | bool ProcessAnalysis) { |
Devang Patel | e253385 | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 689 | |
Devang Patel | 145e83d | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 690 | // This manager is going to manage pass P. Set up analysis resolver |
| 691 | // to connect them. |
Devang Patel | cde53d3 | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 692 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | 145e83d | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 693 | P->setResolver(AR); |
| 694 | |
Devang Patel | cf5fb2b | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 695 | // If a FunctionPass F is the last user of ModulePass info M |
| 696 | // then the F's manager, not F, records itself as a last user of M. |
Devang Patel | edbef38 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 697 | SmallVector<Pass *, 12> TransferLastUses; |
Devang Patel | cf5fb2b | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 698 | |
Devang Patel | 893a5a6 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 699 | if (ProcessAnalysis) { |
Devang Patel | e166340 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 700 | |
| 701 | // At the moment, this pass is the last user of all required passes. |
Devang Patel | edbef38 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 702 | SmallVector<Pass *, 12> LastUses; |
Devang Patel | 569a6fd | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 703 | SmallVector<Pass *, 8> RequiredPasses; |
| 704 | SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable; |
| 705 | |
Devang Patel | e166340 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 706 | unsigned PDepth = this->getDepth(); |
| 707 | |
Devang Patel | 569a6fd | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 708 | collectRequiredAnalysis(RequiredPasses, |
| 709 | ReqAnalysisNotAvailable, P); |
| 710 | for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(), |
Devang Patel | e166340 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 711 | E = RequiredPasses.end(); I != E; ++I) { |
| 712 | Pass *PRequired = *I; |
| 713 | unsigned RDepth = 0; |
Devang Patel | cd520b1 | 2006-12-09 00:07:38 +0000 | [diff] [blame] | 714 | |
| 715 | PMDataManager &DM = PRequired->getResolver()->getPMDataManager(); |
| 716 | RDepth = DM.getDepth(); |
| 717 | |
Devang Patel | e166340 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 718 | if (PDepth == RDepth) |
| 719 | LastUses.push_back(PRequired); |
| 720 | else if (PDepth > RDepth) { |
| 721 | // Let the parent claim responsibility of last use |
Devang Patel | ef89c55 | 2006-12-15 00:08:26 +0000 | [diff] [blame] | 722 | TransferLastUses.push_back(PRequired); |
Devang Patel | 7b65dd9 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 723 | // Keep track of higher level analysis used by this manager. |
| 724 | HigherLevelAnalysis.push_back(PRequired); |
Devang Patel | 569a6fd | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 725 | } else |
| 726 | assert (0 && "Unable to accomodate Required Pass"); |
| 727 | } |
| 728 | |
Devang Patel | 3f5ecd7 | 2007-01-15 20:31:54 +0000 | [diff] [blame] | 729 | // Set P as P's last user until someone starts using P. |
| 730 | // However, if P is a Pass Manager then it does not need |
| 731 | // to record its last user. |
Devang Patel | 8df8709 | 2007-01-16 22:38:10 +0000 | [diff] [blame] | 732 | if (!dynamic_cast<PMDataManager *>(P)) |
Devang Patel | 3f5ecd7 | 2007-01-15 20:31:54 +0000 | [diff] [blame] | 733 | LastUses.push_back(P); |
Devang Patel | 8df8709 | 2007-01-16 22:38:10 +0000 | [diff] [blame] | 734 | TPM->setLastUser(LastUses, P); |
Devang Patel | e166340 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 735 | |
Devang Patel | cf5fb2b | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 736 | if (!TransferLastUses.empty()) { |
| 737 | Pass *My_PM = dynamic_cast<Pass *>(this); |
| 738 | TPM->setLastUser(TransferLastUses, My_PM); |
| 739 | TransferLastUses.clear(); |
| 740 | } |
| 741 | |
Devang Patel | 0ed8df3 | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 742 | // Now, take care of required analysises that are not available. |
| 743 | for (SmallVector<AnalysisID, 8>::iterator |
| 744 | I = ReqAnalysisNotAvailable.begin(), |
| 745 | E = ReqAnalysisNotAvailable.end() ;I != E; ++I) { |
| 746 | Pass *AnalysisPass = (*I)->createPass(); |
| 747 | this->addLowerLevelRequiredPass(P, AnalysisPass); |
| 748 | } |
| 749 | |
Devang Patel | 0ac961d | 2006-12-07 22:09:36 +0000 | [diff] [blame] | 750 | // Take a note of analysis required and made available by this pass. |
Devang Patel | 893a5a6 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 751 | // Remove the analysis not preserved by this pass |
| 752 | removeNotPreservedAnalysis(P); |
Devang Patel | 0ac961d | 2006-12-07 22:09:36 +0000 | [diff] [blame] | 753 | recordAvailableAnalysis(P); |
Devang Patel | 893a5a6 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 754 | } |
Devang Patel | e253385 | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 755 | |
| 756 | // Add pass |
| 757 | PassVector.push_back(P); |
Devang Patel | e253385 | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Devang Patel | 569a6fd | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 760 | |
| 761 | /// Populate RP with analysis pass that are required by |
| 762 | /// pass P and are available. Populate RP_NotAvail with analysis |
| 763 | /// pass that are required by pass P but are not available. |
| 764 | void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP, |
| 765 | SmallVector<AnalysisID, 8> &RP_NotAvail, |
| 766 | Pass *P) { |
Devang Patel | c17bbb6 | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 767 | AnalysisUsage AnUsage; |
| 768 | P->getAnalysisUsage(AnUsage); |
| 769 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
| 770 | for (std::vector<AnalysisID>::const_iterator |
| 771 | I = RequiredSet.begin(), E = RequiredSet.end(); |
| 772 | I != E; ++I) { |
Devang Patel | 569a6fd | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 773 | AnalysisID AID = *I; |
| 774 | if (Pass *AnalysisPass = findAnalysisPass(*I, true)) |
| 775 | RP.push_back(AnalysisPass); |
| 776 | else |
| 777 | RP_NotAvail.push_back(AID); |
Devang Patel | c17bbb6 | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 778 | } |
Devang Patel | 27aaab2 | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 779 | |
| 780 | const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet(); |
| 781 | for (std::vector<AnalysisID>::const_iterator I = IDs.begin(), |
| 782 | E = IDs.end(); I != E; ++I) { |
Devang Patel | 569a6fd | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 783 | AnalysisID AID = *I; |
| 784 | if (Pass *AnalysisPass = findAnalysisPass(*I, true)) |
| 785 | RP.push_back(AnalysisPass); |
| 786 | else |
| 787 | RP_NotAvail.push_back(AID); |
Devang Patel | 27aaab2 | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 788 | } |
Devang Patel | c17bbb6 | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Devang Patel | 2f42ed6 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 791 | // All Required analyses should be available to the pass as it runs! Here |
| 792 | // we fill in the AnalysisImpls member of the pass so that it can |
| 793 | // successfully use the getAnalysis() method to retrieve the |
| 794 | // implementations it needs. |
| 795 | // |
Devang Patel | 419f0e9 | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 796 | void PMDataManager::initializeAnalysisImpl(Pass *P) { |
Devang Patel | 3799f97 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 797 | AnalysisUsage AnUsage; |
| 798 | P->getAnalysisUsage(AnUsage); |
Devang Patel | 2f42ed6 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 799 | |
| 800 | for (std::vector<const PassInfo *>::const_iterator |
| 801 | I = AnUsage.getRequiredSet().begin(), |
| 802 | E = AnUsage.getRequiredSet().end(); I != E; ++I) { |
Devang Patel | 69867b5 | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 803 | Pass *Impl = findAnalysisPass(*I, true); |
Devang Patel | 2f42ed6 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 804 | if (Impl == 0) |
Devang Patel | f4bd76a | 2007-04-16 20:44:16 +0000 | [diff] [blame] | 805 | // This may be analysis pass that is initialized on the fly. |
| 806 | // If that is not the case then it will raise an assert when it is used. |
| 807 | continue; |
Devang Patel | cde53d3 | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 808 | AnalysisResolver *AR = P->getResolver(); |
Devang Patel | 298fead | 2006-12-09 01:11:34 +0000 | [diff] [blame] | 809 | AR->addAnalysisImplsPair(*I, Impl); |
Devang Patel | 2f42ed6 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
Devang Patel | 69867b5 | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 813 | /// Find the pass that implements Analysis AID. If desired pass is not found |
| 814 | /// then return NULL. |
| 815 | Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) { |
| 816 | |
| 817 | // Check if AvailableAnalysis map has one entry. |
| 818 | std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID); |
| 819 | |
| 820 | if (I != AvailableAnalysis.end()) |
| 821 | return I->second; |
| 822 | |
| 823 | // Search Parents through TopLevelManager |
| 824 | if (SearchParent) |
| 825 | return TPM->findAnalysisPass(AID); |
| 826 | |
Devang Patel | 5b640e7 | 2006-12-09 00:09:12 +0000 | [diff] [blame] | 827 | return NULL; |
Devang Patel | 69867b5 | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Devang Patel | a52035a | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 830 | // Print list of passes that are last used by P. |
| 831 | void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{ |
| 832 | |
Devang Patel | edbef38 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 833 | SmallVector<Pass *, 12> LUses; |
Devang Patel | 693941b | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 834 | |
| 835 | // If this is a on the fly manager then it does not have TPM. |
| 836 | if (!TPM) |
| 837 | return; |
| 838 | |
Devang Patel | a52035a | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 839 | TPM->collectLastUses(LUses, P); |
| 840 | |
Devang Patel | edbef38 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 841 | for (SmallVector<Pass *, 12>::iterator I = LUses.begin(), |
Devang Patel | a52035a | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 842 | E = LUses.end(); I != E; ++I) { |
| 843 | llvm::cerr << "--" << std::string(Offset*2, ' '); |
| 844 | (*I)->dumpPassStructure(0); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | void PMDataManager::dumpPassArguments() const { |
| 849 | for(std::vector<Pass *>::const_iterator I = PassVector.begin(), |
| 850 | E = PassVector.end(); I != E; ++I) { |
| 851 | if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I)) |
| 852 | PMD->dumpPassArguments(); |
| 853 | else |
| 854 | if (const PassInfo *PI = (*I)->getPassInfo()) |
| 855 | if (!PI->isAnalysisGroup()) |
| 856 | cerr << " -" << PI->getPassArgument(); |
| 857 | } |
| 858 | } |
| 859 | |
Chris Lattner | 417efc8 | 2007-08-10 06:17:04 +0000 | [diff] [blame] | 860 | void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1, |
| 861 | enum PassDebuggingString S2, |
Devang Patel | 6b4af74 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 862 | const char *Msg) { |
Devang Patel | 2642694 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 863 | if (PassDebugging < Executions) |
Devang Patel | a52035a | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 864 | return; |
| 865 | cerr << (void*)this << std::string(getDepth()*2+1, ' '); |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 866 | switch (S1) { |
| 867 | case EXECUTION_MSG: |
| 868 | cerr << "Executing Pass '" << P->getPassName(); |
| 869 | break; |
| 870 | case MODIFICATION_MSG: |
Devang Patel | 7d97bf1 | 2007-06-18 21:32:29 +0000 | [diff] [blame] | 871 | cerr << "Made Modification '" << P->getPassName(); |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 872 | break; |
| 873 | case FREEING_MSG: |
| 874 | cerr << " Freeing Pass '" << P->getPassName(); |
| 875 | break; |
| 876 | default: |
| 877 | break; |
| 878 | } |
| 879 | switch (S2) { |
| 880 | case ON_BASICBLOCK_MSG: |
Devang Patel | 7d97bf1 | 2007-06-18 21:32:29 +0000 | [diff] [blame] | 881 | cerr << "' on BasicBlock '" << Msg << "'...\n"; |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 882 | break; |
| 883 | case ON_FUNCTION_MSG: |
Devang Patel | 7d97bf1 | 2007-06-18 21:32:29 +0000 | [diff] [blame] | 884 | cerr << "' on Function '" << Msg << "'...\n"; |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 885 | break; |
| 886 | case ON_MODULE_MSG: |
Devang Patel | 7d97bf1 | 2007-06-18 21:32:29 +0000 | [diff] [blame] | 887 | cerr << "' on Module '" << Msg << "'...\n"; |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 888 | break; |
| 889 | case ON_LOOP_MSG: |
Devang Patel | 7d97bf1 | 2007-06-18 21:32:29 +0000 | [diff] [blame] | 890 | cerr << "' on Loop " << Msg << "'...\n"; |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 891 | break; |
| 892 | case ON_CG_MSG: |
Devang Patel | 7d97bf1 | 2007-06-18 21:32:29 +0000 | [diff] [blame] | 893 | cerr << "' on Call Graph " << Msg << "'...\n"; |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 894 | break; |
| 895 | default: |
| 896 | break; |
| 897 | } |
Devang Patel | a52035a | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P, |
| 901 | const std::vector<AnalysisID> &Set) |
| 902 | const { |
Devang Patel | 2642694 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 903 | if (PassDebugging >= Details && !Set.empty()) { |
Devang Patel | a52035a | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 904 | cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:"; |
| 905 | for (unsigned i = 0; i != Set.size(); ++i) { |
| 906 | if (i) cerr << ","; |
| 907 | cerr << " " << Set[i]->getPassName(); |
| 908 | } |
| 909 | cerr << "\n"; |
| 910 | } |
| 911 | } |
Devang Patel | f3dc6d9 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 912 | |
Devang Patel | 19fe8f9 | 2007-07-27 20:06:09 +0000 | [diff] [blame] | 913 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 914 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 915 | /// through getAnalysis interface. |
| 916 | /// This should be handled by specific pass manager. |
| 917 | void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { |
| 918 | if (TPM) { |
| 919 | TPM->dumpArguments(); |
| 920 | TPM->dumpPasses(); |
| 921 | } |
| 922 | assert (0 && "Unable to handle Pass that requires lower level Analysis pass"); |
| 923 | } |
| 924 | |
Devang Patel | ab7752c | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 925 | // Destructor |
| 926 | PMDataManager::~PMDataManager() { |
| 927 | |
| 928 | for (std::vector<Pass *>::iterator I = PassVector.begin(), |
| 929 | E = PassVector.end(); I != E; ++I) |
| 930 | delete *I; |
| 931 | |
| 932 | PassVector.clear(); |
| 933 | } |
| 934 | |
Devang Patel | f3dc6d9 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 935 | //===----------------------------------------------------------------------===// |
| 936 | // NOTE: Is this the right place to define this method ? |
| 937 | // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist |
Devang Patel | cde53d3 | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 938 | Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const { |
Devang Patel | f3dc6d9 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 939 | return PM.findAnalysisPass(ID, dir); |
| 940 | } |
| 941 | |
Devang Patel | 6b1df0e | 2007-04-16 20:56:24 +0000 | [diff] [blame] | 942 | Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI, |
| 943 | Function &F) { |
| 944 | return PM.getOnTheFlyPass(P, AnalysisPI, F); |
| 945 | } |
| 946 | |
Devang Patel | 06e8656 | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 947 | //===----------------------------------------------------------------------===// |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 948 | // BBPassManager implementation |
Devang Patel | 55fd43f | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 949 | |
Devang Patel | 55fd43f | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 950 | /// Execute all of the passes scheduled for execution by invoking |
| 951 | /// runOnBasicBlock method. Keep track of whether any of the passes modifies |
| 952 | /// the function, and if so, return true. |
| 953 | bool |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 954 | BBPassManager::runOnFunction(Function &F) { |
Devang Patel | 55fd43f | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 955 | |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 956 | if (F.isDeclaration()) |
Devang Patel | 1fbe2c9 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 957 | return false; |
| 958 | |
Devang Patel | 3b14fbe | 2006-12-08 01:38:28 +0000 | [diff] [blame] | 959 | bool Changed = doInitialization(F); |
Devang Patel | c1d6e1f | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 960 | |
Devang Patel | 55fd43f | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 961 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 962 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 963 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 017b5d9 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 964 | AnalysisUsage AnUsage; |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 965 | BP->getAnalysisUsage(AnUsage); |
Devang Patel | 017b5d9 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 966 | |
Devang Patel | 6b4af74 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 967 | dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart()); |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 968 | dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet()); |
Devang Patel | 017b5d9 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 969 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 970 | initializeAnalysisImpl(BP); |
Devang Patel | 693a74e | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 971 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 972 | if (TheTimeInfo) TheTimeInfo->passStarted(BP); |
Devang Patel | 55fd43f | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 973 | Changed |= BP->runOnBasicBlock(*I); |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 974 | if (TheTimeInfo) TheTimeInfo->passEnded(BP); |
Devang Patel | 693a74e | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 975 | |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 976 | if (Changed) |
Devang Patel | 6b4af74 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 977 | dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, I->getNameStart()); |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 978 | dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet()); |
Devang Patel | 693a74e | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 979 | |
Devang Patel | 58e0ef1 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 980 | verifyPreservedAnalysis(BP); |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 981 | removeNotPreservedAnalysis(BP); |
| 982 | recordAvailableAnalysis(BP); |
Devang Patel | 6b4af74 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 983 | removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG); |
Devang Patel | 55fd43f | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 984 | } |
Chris Lattner | fc23bc7 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 985 | |
Devang Patel | 1a80386 | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 986 | return Changed |= doFinalization(F); |
Devang Patel | 55fd43f | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Devang Patel | 964e45e | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 989 | // Implement doInitialization and doFinalization |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 990 | inline bool BBPassManager::doInitialization(Module &M) { |
Devang Patel | 964e45e | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 991 | bool Changed = false; |
| 992 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 993 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 994 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 964e45e | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 995 | Changed |= BP->doInitialization(M); |
| 996 | } |
| 997 | |
| 998 | return Changed; |
| 999 | } |
| 1000 | |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1001 | inline bool BBPassManager::doFinalization(Module &M) { |
Devang Patel | 964e45e | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1002 | bool Changed = false; |
| 1003 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1004 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1005 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 964e45e | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1006 | Changed |= BP->doFinalization(M); |
| 1007 | } |
| 1008 | |
| 1009 | return Changed; |
| 1010 | } |
| 1011 | |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1012 | inline bool BBPassManager::doInitialization(Function &F) { |
Devang Patel | 964e45e | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1013 | bool Changed = false; |
| 1014 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1015 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1016 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 964e45e | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1017 | Changed |= BP->doInitialization(F); |
| 1018 | } |
| 1019 | |
| 1020 | return Changed; |
| 1021 | } |
| 1022 | |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1023 | inline bool BBPassManager::doFinalization(Function &F) { |
Devang Patel | 964e45e | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1024 | bool Changed = false; |
| 1025 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1026 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1027 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 964e45e | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1028 | Changed |= BP->doFinalization(F); |
| 1029 | } |
| 1030 | |
| 1031 | return Changed; |
| 1032 | } |
| 1033 | |
| 1034 | |
Devang Patel | 06e8656 | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1035 | //===----------------------------------------------------------------------===// |
Devang Patel | 3162691 | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1036 | // FunctionPassManager implementation |
Devang Patel | 06e8656 | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1037 | |
Devang Patel | c63592b | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1038 | /// Create new Function pass manager |
Devang Patel | 3162691 | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1039 | FunctionPassManager::FunctionPassManager(ModuleProvider *P) { |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1040 | FPM = new FunctionPassManagerImpl(0); |
Devang Patel | dff33ef | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1041 | // FPM is the top level manager. |
| 1042 | FPM->setTopLevelManager(FPM); |
Devang Patel | b920bd8 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1043 | |
| 1044 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM); |
Devang Patel | cde53d3 | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 1045 | AnalysisResolver *AR = new AnalysisResolver(*PMD); |
Devang Patel | b920bd8 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1046 | FPM->setResolver(AR); |
| 1047 | |
Devang Patel | cc132cd | 2006-12-08 18:57:16 +0000 | [diff] [blame] | 1048 | MP = P; |
| 1049 | } |
| 1050 | |
Devang Patel | 3162691 | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1051 | FunctionPassManager::~FunctionPassManager() { |
Devang Patel | 37a6f79 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1052 | delete FPM; |
| 1053 | } |
| 1054 | |
Devang Patel | c63592b | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1055 | /// add - Add a pass to the queue of passes to run. This passes |
| 1056 | /// ownership of the Pass to the PassManager. When the |
| 1057 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 1058 | /// there is no need to delete the pass. (TODO delete passes.) |
| 1059 | /// This implies that all passes MUST be allocated with 'new'. |
Devang Patel | 3162691 | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1060 | void FunctionPassManager::add(Pass *P) { |
Devang Patel | c63592b | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1061 | FPM->add(P); |
| 1062 | } |
| 1063 | |
Devang Patel | 214ca23 | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1064 | /// run - Execute all of the passes scheduled for execution. Keep |
| 1065 | /// track of whether any of the passes modifies the function, and if |
| 1066 | /// so, return true. |
| 1067 | /// |
Devang Patel | 3162691 | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1068 | bool FunctionPassManager::run(Function &F) { |
Devang Patel | 214ca23 | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1069 | std::string errstr; |
| 1070 | if (MP->materializeFunction(&F, &errstr)) { |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 1071 | cerr << "Error reading bitcode file: " << errstr << "\n"; |
Devang Patel | 214ca23 | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1072 | abort(); |
| 1073 | } |
Devang Patel | c475692 | 2006-12-08 22:57:48 +0000 | [diff] [blame] | 1074 | return FPM->run(F); |
Devang Patel | 214ca23 | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | |
Devang Patel | 3799f97 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1078 | /// doInitialization - Run all of the initializers for the function passes. |
| 1079 | /// |
Devang Patel | 3162691 | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1080 | bool FunctionPassManager::doInitialization() { |
Devang Patel | 3799f97 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1081 | return FPM->doInitialization(*MP->getModule()); |
| 1082 | } |
| 1083 | |
Dan Gohman | 209ee18 | 2007-07-30 14:51:13 +0000 | [diff] [blame] | 1084 | /// doFinalization - Run all of the finalizers for the function passes. |
Devang Patel | 3799f97 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1085 | /// |
Devang Patel | 3162691 | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1086 | bool FunctionPassManager::doFinalization() { |
Devang Patel | 3799f97 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1087 | return FPM->doFinalization(*MP->getModule()); |
| 1088 | } |
| 1089 | |
Devang Patel | 06e8656 | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1090 | //===----------------------------------------------------------------------===// |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1091 | // FunctionPassManagerImpl implementation |
| 1092 | // |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1093 | inline bool FunctionPassManagerImpl::doInitialization(Module &M) { |
| 1094 | bool Changed = false; |
| 1095 | |
| 1096 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1097 | FPPassManager *FP = getContainedManager(Index); |
| 1098 | Changed |= FP->doInitialization(M); |
| 1099 | } |
| 1100 | |
| 1101 | return Changed; |
| 1102 | } |
| 1103 | |
| 1104 | inline bool FunctionPassManagerImpl::doFinalization(Module &M) { |
| 1105 | bool Changed = false; |
| 1106 | |
| 1107 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1108 | FPPassManager *FP = getContainedManager(Index); |
| 1109 | Changed |= FP->doFinalization(M); |
| 1110 | } |
| 1111 | |
| 1112 | return Changed; |
| 1113 | } |
| 1114 | |
| 1115 | // Execute all the passes managed by this top level manager. |
| 1116 | // Return true if any function is modified by a pass. |
| 1117 | bool FunctionPassManagerImpl::run(Function &F) { |
| 1118 | |
| 1119 | bool Changed = false; |
| 1120 | |
| 1121 | TimingInfo::createTheTimeInfo(); |
| 1122 | |
| 1123 | dumpArguments(); |
| 1124 | dumpPasses(); |
| 1125 | |
Devang Patel | 1336a6b | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 1126 | initializeAllAnalysisInfo(); |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1127 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1128 | FPPassManager *FP = getContainedManager(Index); |
| 1129 | Changed |= FP->runOnFunction(F); |
| 1130 | } |
| 1131 | return Changed; |
| 1132 | } |
| 1133 | |
| 1134 | //===----------------------------------------------------------------------===// |
| 1135 | // FPPassManager implementation |
Devang Patel | 448d27c | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1136 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 1137 | char FPPassManager::ID = 0; |
Devang Patel | ab7752c | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1138 | /// Print passes managed by this manager |
| 1139 | void FPPassManager::dumpPassStructure(unsigned Offset) { |
| 1140 | llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n"; |
| 1141 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1142 | FunctionPass *FP = getContainedPass(Index); |
| 1143 | FP->dumpPassStructure(Offset + 1); |
| 1144 | dumpLastUses(FP, Offset+1); |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | |
Devang Patel | 448d27c | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1149 | /// Execute all of the passes scheduled for execution by invoking |
| 1150 | /// runOnFunction method. Keep track of whether any of the passes modifies |
| 1151 | /// the function, and if so, return true. |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1152 | bool FPPassManager::runOnFunction(Function &F) { |
Devang Patel | 214ca23 | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1153 | |
| 1154 | bool Changed = false; |
Devang Patel | 1fbe2c9 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1155 | |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1156 | if (F.isDeclaration()) |
Devang Patel | 1fbe2c9 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1157 | return false; |
| 1158 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1159 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1160 | FunctionPass *FP = getContainedPass(Index); |
| 1161 | |
Devang Patel | 017b5d9 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1162 | AnalysisUsage AnUsage; |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1163 | FP->getAnalysisUsage(AnUsage); |
Devang Patel | 693a74e | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1164 | |
Devang Patel | 6b4af74 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 1165 | dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart()); |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1166 | dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet()); |
Devang Patel | 693a74e | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1167 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1168 | initializeAnalysisImpl(FP); |
Devang Patel | 8e58a1b | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1169 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1170 | if (TheTimeInfo) TheTimeInfo->passStarted(FP); |
Devang Patel | 214ca23 | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1171 | Changed |= FP->runOnFunction(F); |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1172 | if (TheTimeInfo) TheTimeInfo->passEnded(FP); |
Devang Patel | 693a74e | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1173 | |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1174 | if (Changed) |
Devang Patel | 6b4af74 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 1175 | dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart()); |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1176 | dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet()); |
Devang Patel | 693a74e | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1177 | |
Devang Patel | 58e0ef1 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1178 | verifyPreservedAnalysis(FP); |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1179 | removeNotPreservedAnalysis(FP); |
| 1180 | recordAvailableAnalysis(FP); |
Devang Patel | 6b4af74 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 1181 | removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG); |
Devang Patel | 214ca23 | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1182 | } |
| 1183 | return Changed; |
| 1184 | } |
| 1185 | |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1186 | bool FPPassManager::runOnModule(Module &M) { |
Devang Patel | 214ca23 | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1187 | |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1188 | bool Changed = doInitialization(M); |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1189 | |
| 1190 | for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 1191 | this->runOnFunction(*I); |
| 1192 | |
| 1193 | return Changed |= doFinalization(M); |
| 1194 | } |
| 1195 | |
| 1196 | inline bool FPPassManager::doInitialization(Module &M) { |
Devang Patel | 3799f97 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1197 | bool Changed = false; |
| 1198 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1199 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1200 | FunctionPass *FP = getContainedPass(Index); |
Devang Patel | 3799f97 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1201 | Changed |= FP->doInitialization(M); |
| 1202 | } |
| 1203 | |
| 1204 | return Changed; |
| 1205 | } |
| 1206 | |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1207 | inline bool FPPassManager::doFinalization(Module &M) { |
Devang Patel | 3799f97 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1208 | bool Changed = false; |
| 1209 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1210 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1211 | FunctionPass *FP = getContainedPass(Index); |
Devang Patel | 3799f97 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1212 | Changed |= FP->doFinalization(M); |
| 1213 | } |
| 1214 | |
Devang Patel | 3799f97 | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1215 | return Changed; |
| 1216 | } |
| 1217 | |
Devang Patel | 06e8656 | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1218 | //===----------------------------------------------------------------------===// |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1219 | // MPPassManager implementation |
Devang Patel | 92c45ee | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1220 | |
Devang Patel | 92c45ee | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1221 | /// Execute all of the passes scheduled for execution by invoking |
| 1222 | /// runOnModule method. Keep track of whether any of the passes modifies |
| 1223 | /// the module, and if so, return true. |
| 1224 | bool |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1225 | MPPassManager::runOnModule(Module &M) { |
Devang Patel | 92c45ee | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1226 | bool Changed = false; |
Devang Patel | c1d6e1f | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 1227 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1228 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1229 | ModulePass *MP = getContainedPass(Index); |
| 1230 | |
Devang Patel | 017b5d9 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1231 | AnalysisUsage AnUsage; |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1232 | MP->getAnalysisUsage(AnUsage); |
Devang Patel | 693a74e | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1233 | |
Devang Patel | 6b4af74 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 1234 | dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier().c_str()); |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1235 | dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet()); |
Devang Patel | 693a74e | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1236 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1237 | initializeAnalysisImpl(MP); |
Devang Patel | 8e58a1b | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1238 | |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1239 | if (TheTimeInfo) TheTimeInfo->passStarted(MP); |
Devang Patel | 92c45ee | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1240 | Changed |= MP->runOnModule(M); |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1241 | if (TheTimeInfo) TheTimeInfo->passEnded(MP); |
Devang Patel | 693a74e | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1242 | |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1243 | if (Changed) |
Devang Patel | 6b4af74 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 1244 | dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG, M.getModuleIdentifier().c_str()); |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1245 | dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet()); |
Devang Patel | 017b5d9 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1246 | |
Devang Patel | 58e0ef1 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1247 | verifyPreservedAnalysis(MP); |
Devang Patel | 1554c85 | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1248 | removeNotPreservedAnalysis(MP); |
| 1249 | recordAvailableAnalysis(MP); |
Devang Patel | 6b4af74 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 1250 | removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG); |
Devang Patel | 92c45ee | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1251 | } |
| 1252 | return Changed; |
| 1253 | } |
| 1254 | |
Devang Patel | 569a6fd | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1255 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 1256 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 1257 | /// through getAnalysis interface. |
| 1258 | void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { |
| 1259 | |
| 1260 | assert (P->getPotentialPassManagerType() == PMT_ModulePassManager |
| 1261 | && "Unable to handle Pass that requires lower level Analysis pass"); |
| 1262 | assert ((P->getPotentialPassManagerType() < |
| 1263 | RequiredPass->getPotentialPassManagerType()) |
| 1264 | && "Unable to handle Pass that requires lower level Analysis pass"); |
| 1265 | |
Devang Patel | dfa1ec3 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1266 | FunctionPassManagerImpl *FPP = OnTheFlyManagers[P]; |
Devang Patel | 0ed8df3 | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1267 | if (!FPP) { |
Devang Patel | dfa1ec3 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1268 | FPP = new FunctionPassManagerImpl(0); |
| 1269 | // FPP is the top level manager. |
| 1270 | FPP->setTopLevelManager(FPP); |
| 1271 | |
Devang Patel | 0ed8df3 | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1272 | OnTheFlyManagers[P] = FPP; |
| 1273 | } |
Devang Patel | dfa1ec3 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1274 | FPP->add(RequiredPass); |
Devang Patel | 0ed8df3 | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1275 | |
Devang Patel | dfa1ec3 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1276 | // Register P as the last user of RequiredPass. |
Devang Patel | edbef38 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 1277 | SmallVector<Pass *, 12> LU; |
Devang Patel | dfa1ec3 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1278 | LU.push_back(RequiredPass); |
| 1279 | FPP->setLastUser(LU, P); |
Devang Patel | 569a6fd | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1280 | } |
Devang Patel | 0ed8df3 | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1281 | |
| 1282 | /// Return function pass corresponding to PassInfo PI, that is |
| 1283 | /// required by module pass MP. Instantiate analysis pass, by using |
| 1284 | /// its runOnFunction() for function F. |
| 1285 | Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, |
| 1286 | Function &F) { |
| 1287 | AnalysisID AID = PI; |
Devang Patel | dfa1ec3 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1288 | FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP]; |
Devang Patel | 0ed8df3 | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1289 | assert (FPP && "Unable to find on the fly pass"); |
| 1290 | |
Devang Patel | dfa1ec3 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1291 | FPP->run(F); |
| 1292 | return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID); |
Devang Patel | 0ed8df3 | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | |
Devang Patel | 06e8656 | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1296 | //===----------------------------------------------------------------------===// |
| 1297 | // PassManagerImpl implementation |
Devang Patel | 37a6f79 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1298 | // |
Devang Patel | b30803b | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1299 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1300 | /// whether any of the passes modifies the module, and if so, return true. |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1301 | bool PassManagerImpl::run(Module &M) { |
Devang Patel | b30803b | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1302 | |
Devang Patel | b30803b | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1303 | bool Changed = false; |
Devang Patel | 45dc02d | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 1304 | |
Devang Patel | 8e58a1b | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1305 | TimingInfo::createTheTimeInfo(); |
| 1306 | |
Devang Patel | c32cf54 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 1307 | dumpArguments(); |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1308 | dumpPasses(); |
Devang Patel | 45dc02d | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 1309 | |
Devang Patel | 1336a6b | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 1310 | initializeAllAnalysisInfo(); |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1311 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1312 | MPPassManager *MP = getContainedManager(Index); |
Devang Patel | a083e94 | 2006-12-08 22:47:25 +0000 | [diff] [blame] | 1313 | Changed |= MP->runOnModule(M); |
Devang Patel | b30803b | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1314 | } |
| 1315 | return Changed; |
| 1316 | } |
Devang Patel | 5a39b2e | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1317 | |
Devang Patel | 06e8656 | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1318 | //===----------------------------------------------------------------------===// |
| 1319 | // PassManager implementation |
| 1320 | |
Devang Patel | 5a39b2e | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1321 | /// Create new pass manager |
Devang Patel | 3162691 | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1322 | PassManager::PassManager() { |
Devang Patel | 5f4ddf5 | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1323 | PM = new PassManagerImpl(0); |
Devang Patel | dff33ef | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1324 | // PM is the top level manager |
| 1325 | PM->setTopLevelManager(PM); |
Devang Patel | 5a39b2e | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Devang Patel | 3162691 | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1328 | PassManager::~PassManager() { |
Devang Patel | 37a6f79 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1329 | delete PM; |
| 1330 | } |
| 1331 | |
Devang Patel | 5a39b2e | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1332 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 1333 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 1334 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 1335 | /// implies that all passes MUST be allocated with 'new'. |
| 1336 | void |
Devang Patel | 3162691 | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1337 | PassManager::add(Pass *P) { |
Devang Patel | 5a39b2e | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1338 | PM->add(P); |
| 1339 | } |
| 1340 | |
| 1341 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1342 | /// whether any of the passes modifies the module, and if so, return true. |
| 1343 | bool |
Devang Patel | 3162691 | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1344 | PassManager::run(Module &M) { |
Devang Patel | 5a39b2e | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1345 | return PM->run(M); |
| 1346 | } |
| 1347 | |
Devang Patel | 8e58a1b | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1348 | //===----------------------------------------------------------------------===// |
| 1349 | // TimingInfo Class - This class is used to calculate information about the |
| 1350 | // amount of time each pass takes to execute. This only happens with |
| 1351 | // -time-passes is enabled on the command line. |
| 1352 | // |
| 1353 | bool llvm::TimePassesIsEnabled = false; |
| 1354 | static cl::opt<bool,true> |
| 1355 | EnableTiming("time-passes", cl::location(TimePassesIsEnabled), |
| 1356 | cl::desc("Time each pass, printing elapsed time for each on exit")); |
| 1357 | |
| 1358 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to |
| 1359 | // a non null value (if the -time-passes option is enabled) or it leaves it |
| 1360 | // null. It may be called multiple times. |
| 1361 | void TimingInfo::createTheTimeInfo() { |
| 1362 | if (!TimePassesIsEnabled || TheTimeInfo) return; |
| 1363 | |
| 1364 | // Constructed the first time this is called, iff -time-passes is enabled. |
| 1365 | // This guarantees that the object will be constructed before static globals, |
| 1366 | // thus it will be destroyed before them. |
| 1367 | static ManagedStatic<TimingInfo> TTI; |
| 1368 | TheTimeInfo = &*TTI; |
| 1369 | } |
| 1370 | |
Devang Patel | c874eb5 | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 1371 | /// If TimingInfo is enabled then start pass timer. |
| 1372 | void StartPassTimer(Pass *P) { |
| 1373 | if (TheTimeInfo) |
| 1374 | TheTimeInfo->passStarted(P); |
| 1375 | } |
| 1376 | |
| 1377 | /// If TimingInfo is enabled then stop pass timer. |
| 1378 | void StopPassTimer(Pass *P) { |
| 1379 | if (TheTimeInfo) |
| 1380 | TheTimeInfo->passEnded(P); |
| 1381 | } |
| 1382 | |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1383 | //===----------------------------------------------------------------------===// |
| 1384 | // PMStack implementation |
| 1385 | // |
Devang Patel | 36bcb82 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 1386 | |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1387 | // Pop Pass Manager from the stack and clear its analysis info. |
| 1388 | void PMStack::pop() { |
| 1389 | |
| 1390 | PMDataManager *Top = this->top(); |
| 1391 | Top->initializeAnalysisInfo(); |
| 1392 | |
| 1393 | S.pop_back(); |
| 1394 | } |
| 1395 | |
| 1396 | // Push PM on the stack and set its top level manager. |
Devang Patel | 9714973 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1397 | void PMStack::push(Pass *P) { |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1398 | |
Devang Patel | 9714973 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1399 | PMDataManager *Top = NULL; |
| 1400 | PMDataManager *PM = dynamic_cast<PMDataManager *>(P); |
| 1401 | assert (PM && "Unable to push. Pass Manager expected"); |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1402 | |
Devang Patel | 9714973 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1403 | if (this->empty()) { |
| 1404 | Top = PM; |
| 1405 | } |
| 1406 | else { |
| 1407 | Top = this->top(); |
| 1408 | PMTopLevelManager *TPM = Top->getTopLevelManager(); |
| 1409 | |
| 1410 | assert (TPM && "Unable to find top level manager"); |
| 1411 | TPM->addIndirectPassManager(PM); |
| 1412 | PM->setTopLevelManager(TPM); |
| 1413 | } |
| 1414 | |
Devang Patel | 9714973 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1415 | S.push_back(PM); |
| 1416 | } |
| 1417 | |
| 1418 | // Dump content of the pass manager stack. |
| 1419 | void PMStack::dump() { |
| 1420 | for(std::deque<PMDataManager *>::iterator I = S.begin(), |
| 1421 | E = S.end(); I != E; ++I) { |
| 1422 | Pass *P = dynamic_cast<Pass *>(*I); |
Chris Lattner | fc23bc7 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 1423 | printf("%s ", P->getPassName()); |
Devang Patel | 9714973 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1424 | } |
| 1425 | if (!S.empty()) |
Chris Lattner | fc23bc7 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 1426 | printf("\n"); |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1429 | /// Find appropriate Module Pass Manager in the PM Stack and |
| 1430 | /// add self into that manager. |
Devang Patel | be1ffc6 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1431 | void ModulePass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1432 | PassManagerType PreferredType) { |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1433 | |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1434 | // Find Module Pass Manager |
| 1435 | while(!PMS.empty()) { |
Devang Patel | 44b0d29 | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1436 | PassManagerType TopPMType = PMS.top()->getPassManagerType(); |
| 1437 | if (TopPMType == PreferredType) |
| 1438 | break; // We found desired pass manager |
| 1439 | else if (TopPMType > PMT_ModulePassManager) |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1440 | PMS.pop(); // Pop children pass managers |
Devang Patel | 6b9420e | 2007-01-11 19:59:06 +0000 | [diff] [blame] | 1441 | else |
| 1442 | break; |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Devang Patel | 44b0d29 | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1445 | PMS.top()->add(this); |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Devang Patel | 9d133e1 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1448 | /// Find appropriate Function Pass Manager or Call Graph Pass Manager |
| 1449 | /// in the PM Stack and add self into that manager. |
Devang Patel | be1ffc6 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1450 | void FunctionPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1451 | PassManagerType PreferredType) { |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1452 | |
Devang Patel | 9714973 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1453 | // Find Module Pass Manager (TODO : Or Call Graph Pass Manager) |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1454 | while(!PMS.empty()) { |
Devang Patel | 6b9420e | 2007-01-11 19:59:06 +0000 | [diff] [blame] | 1455 | if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager) |
| 1456 | PMS.pop(); |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1457 | else |
Devang Patel | 9d133e1 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1458 | break; |
| 1459 | } |
| 1460 | FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top()); |
| 1461 | |
| 1462 | // Create new Function Pass Manager |
| 1463 | if (!FPP) { |
| 1464 | assert(!PMS.empty() && "Unable to create Function Pass Manager"); |
| 1465 | PMDataManager *PMD = PMS.top(); |
| 1466 | |
| 1467 | // [1] Create new Function Pass Manager |
| 1468 | FPP = new FPPassManager(PMD->getDepth() + 1); |
| 1469 | |
| 1470 | // [2] Set up new manager's top level manager |
| 1471 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 1472 | TPM->addIndirectPassManager(FPP); |
| 1473 | |
| 1474 | // [3] Assign manager to manage this new manager. This may create |
| 1475 | // and push new managers into PMS |
| 1476 | Pass *P = dynamic_cast<Pass *>(FPP); |
Devang Patel | be1ffc6 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1477 | |
| 1478 | // If Call Graph Pass Manager is active then use it to manage |
| 1479 | // this new Function Pass manager. |
| 1480 | if (PMD->getPassManagerType() == PMT_CallGraphPassManager) |
| 1481 | P->assignPassManager(PMS, PMT_CallGraphPassManager); |
| 1482 | else |
| 1483 | P->assignPassManager(PMS); |
Devang Patel | 9d133e1 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1484 | |
| 1485 | // [4] Push new manager into PMS |
| 1486 | PMS.push(FPP); |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Devang Patel | 9d133e1 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1489 | // Assign FPP as the manager of this pass. |
| 1490 | FPP->add(this); |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Devang Patel | 9d133e1 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1493 | /// Find appropriate Basic Pass Manager or Call Graph Pass Manager |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1494 | /// in the PM Stack and add self into that manager. |
Devang Patel | be1ffc6 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1495 | void BasicBlockPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1496 | PassManagerType PreferredType) { |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1497 | |
| 1498 | BBPassManager *BBP = NULL; |
| 1499 | |
Devang Patel | 9714973 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1500 | // Basic Pass Manager is a leaf pass manager. It does not handle |
| 1501 | // any other pass manager. |
Chris Lattner | fc23bc7 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 1502 | if (!PMS.empty()) |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1503 | BBP = dynamic_cast<BBPassManager *>(PMS.top()); |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1504 | |
Devang Patel | 9d133e1 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1505 | // If leaf manager is not Basic Block Pass manager then create new |
| 1506 | // basic Block Pass manager. |
Devang Patel | 9714973 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1507 | |
Devang Patel | 9d133e1 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1508 | if (!BBP) { |
| 1509 | assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager"); |
| 1510 | PMDataManager *PMD = PMS.top(); |
| 1511 | |
| 1512 | // [1] Create new Basic Block Manager |
| 1513 | BBP = new BBPassManager(PMD->getDepth() + 1); |
| 1514 | |
| 1515 | // [2] Set up new manager's top level manager |
| 1516 | // Basic Block Pass Manager does not live by itself |
| 1517 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 1518 | TPM->addIndirectPassManager(BBP); |
| 1519 | |
Devang Patel | 9714973 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1520 | // [3] Assign manager to manage this new manager. This may create |
| 1521 | // and push new managers into PMS |
Devang Patel | 9d133e1 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1522 | Pass *P = dynamic_cast<Pass *>(BBP); |
| 1523 | P->assignPassManager(PMS); |
Devang Patel | 9714973 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1524 | |
Devang Patel | 9d133e1 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1525 | // [4] Push new manager into PMS |
| 1526 | PMS.push(BBP); |
| 1527 | } |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1528 | |
Devang Patel | 9d133e1 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1529 | // Assign BBP as the manager of this pass. |
| 1530 | BBP->add(this); |
Devang Patel | 09e6e43 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Devang Patel | cccd80d | 2007-01-05 20:16:23 +0000 | [diff] [blame] | 1533 | |