Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1 | //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LLVM Pass Manager infrastructure. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 15 | #include "llvm/PassManagers.h" |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CommandLine.h" |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Timer.h" |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 19 | #include "llvm/ModuleProvider.h" |
Bill Wendling | dfc9189 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Streams.h" |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ManagedStatic.h" |
Jeff Cohen | b622c11 | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 23 | #include <vector> |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 24 | #include <map> |
Dan Gohman | 8c43e41 | 2007-10-03 19:04:09 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 26 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 27 | // See PassManagers.h for Pass Manager infrastructure overview. |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 28 | |
Devang Patel | f1567a5 | 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 | 03fb587 | 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 | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 43 | static cl::opt<enum PassDebugLevel> |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 44 | PassDebugging("debug-pass", cl::Hidden, |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 45 | cl::desc("Print PassManager debugging information"), |
| 46 | cl::values( |
Devang Patel | 03fb587 | 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 | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 52 | clEnumValEnd)); |
| 53 | } // End of llvm namespace |
| 54 | |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 55 | namespace { |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 56 | |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 57 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 58 | // BBPassManager |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 59 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 60 | /// BBPassManager manages BasicBlockPass. It batches all the |
Devang Patel | ca58e35 | 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 | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 63 | class VISIBILITY_HIDDEN BBPassManager : public PMDataManager, |
| 64 | public FunctionPass { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 65 | |
| 66 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 67 | static char ID; |
Dan Gohman | 13ab93e | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 68 | explicit BBPassManager(int Depth) |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 69 | : PMDataManager(Depth), FunctionPass((intptr_t)&ID) {} |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 70 | |
Devang Patel | ca58e35 | 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 | f9d96b9 | 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 | 475c453 | 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 | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 85 | virtual const char *getPassName() const { |
| 86 | return "BasicBlock Pass Manager"; |
| 87 | } |
| 88 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 89 | // Print passes managed by this manager |
| 90 | void dumpPassStructure(unsigned Offset) { |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 91 | llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n"; |
Devang Patel | abfbe3b | 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 | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
Devang Patel | abfbe3b | 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 | 3b3f899 | 2007-01-11 01:10:25 +0000 | [diff] [blame] | 104 | |
Devang Patel | 28349ab | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 105 | virtual PassManagerType getPassManagerType() const { |
Devang Patel | 3b3f899 | 2007-01-11 01:10:25 +0000 | [diff] [blame] | 106 | return PMT_BasicBlockPassManager; |
| 107 | } |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 110 | char BBPassManager::ID = 0; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 111 | } |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 112 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 113 | namespace llvm { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 114 | |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 115 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 116 | // FunctionPassManagerImpl |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 117 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 118 | /// FunctionPassManagerImpl manages FPPassManagers |
| 119 | class FunctionPassManagerImpl : public Pass, |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 120 | public PMDataManager, |
| 121 | public PMTopLevelManager { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 122 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 123 | static char ID; |
Dan Gohman | 13ab93e | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 124 | explicit FunctionPassManagerImpl(int Depth) : |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 125 | Pass((intptr_t)&ID), PMDataManager(Depth), |
| 126 | PMTopLevelManager(TLM_Function) { } |
Devang Patel | 67d6a5e | 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 | e6656eb | 2007-07-30 14:51:13 +0000 | [diff] [blame] | 144 | /// doFinalization - Run all of the finalizers for the function passes. |
Devang Patel | 67d6a5e | 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 | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 159 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 160 | P->setResolver(AR); |
| 161 | initializeAnalysisImpl(P); |
| 162 | addImmutablePass(IP); |
| 163 | recordAvailableAnalysis(IP); |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 164 | } else { |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 165 | P->assignPassManager(activeStack); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 166 | } |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 167 | |
Devang Patel | 67d6a5e | 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 | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 177 | char FunctionPassManagerImpl::ID = 0; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 178 | //===----------------------------------------------------------------------===// |
| 179 | // MPPassManager |
| 180 | // |
| 181 | /// MPPassManager manages ModulePasses and function pass managers. |
Dan Gohman | dfdf2c0 | 2008-03-11 16:18:48 +0000 | [diff] [blame^] | 182 | /// It batches all Module passes and function pass managers together and |
| 183 | /// sequences them to process one module. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 184 | class MPPassManager : public Pass, public PMDataManager { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 185 | |
| 186 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 187 | static char ID; |
Dan Gohman | 13ab93e | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 188 | explicit MPPassManager(int Depth) : |
| 189 | Pass((intptr_t)&ID), PMDataManager(Depth) { } |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 190 | |
| 191 | // Delete on the fly managers. |
| 192 | virtual ~MPPassManager() { |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 193 | for (std::map<Pass *, FunctionPassManagerImpl *>::iterator |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 194 | I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); |
| 195 | I != E; ++I) { |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 196 | FunctionPassManagerImpl *FPP = I->second; |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 197 | delete FPP; |
| 198 | } |
| 199 | } |
| 200 | |
Devang Patel | ca58e35 | 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 | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 204 | |
Devang Patel | f9d96b9 | 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 | e64d305 | 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 | 69e9f6d | 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 | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 220 | virtual const char *getPassName() const { |
| 221 | return "Module Pass Manager"; |
| 222 | } |
| 223 | |
Devang Patel | eda5617 | 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 | abfbe3b | 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 | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 230 | if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP]) |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 231 | FPP->dumpPassStructure(Offset + 2); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 232 | dumpLastUses(MP, Offset+1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Devang Patel | abfbe3b | 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 | 28349ab | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 242 | virtual PassManagerType getPassManagerType() const { |
| 243 | return PMT_ModulePassManager; |
| 244 | } |
Devang Patel | 69e9f6d | 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 | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 249 | std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 250 | }; |
| 251 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 252 | char MPPassManager::ID = 0; |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 253 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 254 | // PassManagerImpl |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 255 | // |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 256 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 257 | /// PassManagerImpl manages MPPassManagers |
| 258 | class PassManagerImpl : public Pass, |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 259 | public PMDataManager, |
| 260 | public PMTopLevelManager { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 261 | |
| 262 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 263 | static char ID; |
Dan Gohman | 13ab93e | 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 | 4c36e6b | 2006-12-07 23:24:58 +0000 | [diff] [blame] | 267 | |
Devang Patel | 376fefa | 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 | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 272 | void add(Pass *P) { |
Devang Patel | df6c9ae | 2006-12-08 22:34:02 +0000 | [diff] [blame] | 273 | schedulePass(P); |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 274 | } |
Devang Patel | 376fefa | 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 | f9d96b9 | 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 | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 285 | inline void addTopLevelPass(Pass *P) { |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 286 | |
Devang Patel | fa971cd | 2006-12-08 23:57:43 +0000 | [diff] [blame] | 287 | if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) { |
Devang Patel | d440cd9 | 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 | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 291 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 292 | P->setResolver(AR); |
Devang Patel | 9525754 | 2006-12-12 22:21:37 +0000 | [diff] [blame] | 293 | initializeAnalysisImpl(P); |
Devang Patel | fa971cd | 2006-12-08 23:57:43 +0000 | [diff] [blame] | 294 | addImmutablePass(IP); |
Devang Patel | 9525754 | 2006-12-12 22:21:37 +0000 | [diff] [blame] | 295 | recordAvailableAnalysis(IP); |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 296 | } else { |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 297 | P->assignPassManager(activeStack); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 298 | } |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 299 | |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Devang Patel | 67d6a5e | 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 | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 310 | char PassManagerImpl::ID = 0; |
Devang Patel | 1c3633e | 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 | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 362 | static TimingInfo *TheTimeInfo; |
| 363 | |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 364 | } // End of anon namespace |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 365 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 366 | //===----------------------------------------------------------------------===// |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 367 | // PMTopLevelManager implementation |
| 368 | |
Devang Patel | 4268fc0 | 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 | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 386 | /// Set pass P as the last user of the given analysis passes. |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 387 | void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 388 | Pass *P) { |
| 389 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 390 | for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(), |
Devang Patel | afb1f362 | 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 | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 394 | |
| 395 | if (P == AP) |
| 396 | continue; |
| 397 | |
Devang Patel | afb1f362 | 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 | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /// Collect passes whose last user is P |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 409 | void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses, |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 410 | Pass *P) { |
Chris Lattner | 9df8be4 | 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 | afb1f362 | 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 | 3312f75 | 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 | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 424 | |
Devang Patel | d74ede7 | 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 | afb1f362 | 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 | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 436 | AnalysisPass = (*I)->createPass(); |
Devang Patel | e64d305 | 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 | afb1f362 | 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 | cd6ba15 | 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 | afb1f362 | 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) { |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 478 | const std::vector<const PassInfo*> &ImmPI = |
| 479 | PI->getInterfacesImplemented(); |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 480 | if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end()) |
| 481 | P = *I; |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 485 | return P; |
| 486 | } |
| 487 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 488 | // Print passes managed by this top level manager. |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 489 | void PMTopLevelManager::dumpPasses() const { |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 490 | |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 491 | if (PassDebugging < Structure) |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 492 | return; |
| 493 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 494 | // Print out the immutable passes |
| 495 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { |
| 496 | ImmutablePasses[i]->dumpPassStructure(0); |
| 497 | } |
| 498 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 499 | for (std::vector<Pass *>::const_iterator I = PassManagers.begin(), |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 500 | E = PassManagers.end(); I != E; ++I) |
| 501 | (*I)->dumpPassStructure(1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 504 | void PMTopLevelManager::dumpArguments() const { |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 505 | |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 506 | if (PassDebugging < Arguments) |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 507 | return; |
| 508 | |
| 509 | cerr << "Pass Arguments: "; |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 510 | for (std::vector<Pass *>::const_iterator I = PassManagers.begin(), |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 511 | E = PassManagers.end(); I != E; ++I) { |
| 512 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I); |
| 513 | assert(PMD && "This is not a PassManager"); |
| 514 | PMD->dumpPassArguments(); |
| 515 | } |
| 516 | cerr << "\n"; |
| 517 | } |
| 518 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 519 | void PMTopLevelManager::initializeAllAnalysisInfo() { |
| 520 | |
| 521 | for (std::vector<Pass *>::iterator I = PassManagers.begin(), |
| 522 | E = PassManagers.end(); I != E; ++I) { |
| 523 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I); |
| 524 | assert(PMD && "This is not a PassManager"); |
| 525 | PMD->initializeAnalysisInfo(); |
| 526 | } |
| 527 | |
| 528 | // Initailize other pass managers |
| 529 | for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(), |
| 530 | E = IndirectPassManagers.end(); I != E; ++I) |
| 531 | (*I)->initializeAnalysisInfo(); |
| 532 | } |
| 533 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 534 | /// Destructor |
| 535 | PMTopLevelManager::~PMTopLevelManager() { |
| 536 | for (std::vector<Pass *>::iterator I = PassManagers.begin(), |
| 537 | E = PassManagers.end(); I != E; ++I) |
| 538 | delete *I; |
| 539 | |
| 540 | for (std::vector<ImmutablePass *>::iterator |
| 541 | I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) |
| 542 | delete *I; |
| 543 | |
| 544 | PassManagers.clear(); |
| 545 | } |
| 546 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 547 | //===----------------------------------------------------------------------===// |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 548 | // PMDataManager implementation |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 549 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 550 | /// Augement AvailableAnalysis by adding analysis made available by pass P. |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 551 | void PMDataManager::recordAvailableAnalysis(Pass *P) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 552 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 553 | if (const PassInfo *PI = P->getPassInfo()) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 554 | AvailableAnalysis[PI] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 555 | |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 556 | //This pass is the current implementation of all of the interfaces it |
| 557 | //implements as well. |
| 558 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 559 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 560 | AvailableAnalysis[II[i]] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 564 | // Return true if P preserves high level analysis used by other |
| 565 | // passes managed by this manager |
| 566 | bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) { |
| 567 | |
| 568 | AnalysisUsage AnUsage; |
| 569 | P->getAnalysisUsage(AnUsage); |
| 570 | |
| 571 | if (AnUsage.getPreservesAll()) |
| 572 | return true; |
| 573 | |
| 574 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
| 575 | for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(), |
| 576 | E = HigherLevelAnalysis.end(); I != E; ++I) { |
| 577 | Pass *P1 = *I; |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 578 | if (!dynamic_cast<ImmutablePass*>(P1) && |
| 579 | std::find(PreservedSet.begin(), PreservedSet.end(), |
| 580 | P1->getPassInfo()) == |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 581 | PreservedSet.end()) |
| 582 | return false; |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | return true; |
| 586 | } |
| 587 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 588 | /// verifyPreservedAnalysis -- Verify analysis presreved by pass P. |
| 589 | void PMDataManager::verifyPreservedAnalysis(Pass *P) { |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 590 | AnalysisUsage AnUsage; |
| 591 | P->getAnalysisUsage(AnUsage); |
Devang Patel | ef43253 | 2007-07-19 05:36:09 +0000 | [diff] [blame] | 592 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 593 | |
Devang Patel | ef43253 | 2007-07-19 05:36:09 +0000 | [diff] [blame] | 594 | // Verify preserved analysis |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 595 | for (std::vector<AnalysisID>::const_iterator I = PreservedSet.begin(), |
| 596 | E = PreservedSet.end(); I != E; ++I) { |
| 597 | AnalysisID AID = *I; |
| 598 | Pass *AP = findAnalysisPass(AID, true); |
| 599 | if (AP) |
| 600 | AP->verifyAnalysis(); |
Devang Patel | ef43253 | 2007-07-19 05:36:09 +0000 | [diff] [blame] | 601 | } |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /// Remove Analyss not preserved by Pass P |
| 605 | void PMDataManager::removeNotPreservedAnalysis(Pass *P) { |
| 606 | AnalysisUsage AnUsage; |
| 607 | P->getAnalysisUsage(AnUsage); |
Devang Patel | 2e169c3 | 2006-12-07 20:03:49 +0000 | [diff] [blame] | 608 | if (AnUsage.getPreservesAll()) |
| 609 | return; |
| 610 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 611 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 612 | for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), |
Devang Patel | be6bd55e | 2006-12-12 23:07:44 +0000 | [diff] [blame] | 613 | E = AvailableAnalysis.end(); I != E; ) { |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 614 | std::map<AnalysisID, Pass*>::iterator Info = I++; |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 615 | if (!dynamic_cast<ImmutablePass*>(Info->second) |
| 616 | && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == |
| 617 | PreservedSet.end()) |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 618 | // Remove this analysis |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 619 | AvailableAnalysis.erase(Info); |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 620 | } |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 621 | |
| 622 | // Check inherited analysis also. If P is not preserving analysis |
| 623 | // provided by parent manager then remove it here. |
| 624 | for (unsigned Index = 0; Index < PMT_Last; ++Index) { |
| 625 | |
| 626 | if (!InheritedAnalysis[Index]) |
| 627 | continue; |
| 628 | |
| 629 | for (std::map<AnalysisID, Pass*>::iterator |
| 630 | I = InheritedAnalysis[Index]->begin(), |
| 631 | E = InheritedAnalysis[Index]->end(); I != E; ) { |
| 632 | std::map<AnalysisID, Pass *>::iterator Info = I++; |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 633 | if (!dynamic_cast<ImmutablePass*>(Info->second) && |
| 634 | std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 635 | PreservedSet.end()) |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 636 | // Remove this analysis |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 637 | InheritedAnalysis[Index]->erase(Info); |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 643 | /// Remove analysis passes that are not used any longer |
Devang Patel | d305c40 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 644 | void PMDataManager::removeDeadPasses(Pass *P, const char *Msg, |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 645 | enum PassDebuggingString DBG_STR) { |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 646 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 647 | SmallVector<Pass *, 12> DeadPasses; |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 648 | |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 649 | // If this is a on the fly manager then it does not have TPM. |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 650 | if (!TPM) |
| 651 | return; |
| 652 | |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 653 | TPM->collectLastUses(DeadPasses, P); |
| 654 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 655 | for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(), |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 656 | E = DeadPasses.end(); I != E; ++I) { |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 657 | |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 658 | dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg); |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 659 | |
Devang Patel | 7ebf09d | 2007-03-05 18:20:51 +0000 | [diff] [blame] | 660 | if (TheTimeInfo) TheTimeInfo->passStarted(*I); |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 661 | (*I)->releaseMemory(); |
Devang Patel | 7ebf09d | 2007-03-05 18:20:51 +0000 | [diff] [blame] | 662 | if (TheTimeInfo) TheTimeInfo->passEnded(*I); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 663 | |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 664 | std::map<AnalysisID, Pass*>::iterator Pos = |
| 665 | AvailableAnalysis.find((*I)->getPassInfo()); |
| 666 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 667 | // It is possible that pass is already removed from the AvailableAnalysis |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 668 | if (Pos != AvailableAnalysis.end()) |
| 669 | AvailableAnalysis.erase(Pos); |
| 670 | } |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Devang Patel | 8f677ce | 2006-12-07 18:47:25 +0000 | [diff] [blame] | 673 | /// Add pass P into the PassVector. Update |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 674 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
Devang Patel | f85793d | 2007-01-12 20:07:16 +0000 | [diff] [blame] | 675 | void PMDataManager::add(Pass *P, |
Devang Patel | 6975b6e | 2007-01-15 23:06:56 +0000 | [diff] [blame] | 676 | bool ProcessAnalysis) { |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 677 | |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 678 | // This manager is going to manage pass P. Set up analysis resolver |
| 679 | // to connect them. |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 680 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 681 | P->setResolver(AR); |
| 682 | |
Devang Patel | ec2b9a7 | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 683 | // If a FunctionPass F is the last user of ModulePass info M |
| 684 | // then the F's manager, not F, records itself as a last user of M. |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 685 | SmallVector<Pass *, 12> TransferLastUses; |
Devang Patel | ec2b9a7 | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 686 | |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 687 | if (ProcessAnalysis) { |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 688 | |
| 689 | // At the moment, this pass is the last user of all required passes. |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 690 | SmallVector<Pass *, 12> LastUses; |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 691 | SmallVector<Pass *, 8> RequiredPasses; |
| 692 | SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable; |
| 693 | |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 694 | unsigned PDepth = this->getDepth(); |
| 695 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 696 | collectRequiredAnalysis(RequiredPasses, |
| 697 | ReqAnalysisNotAvailable, P); |
| 698 | for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(), |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 699 | E = RequiredPasses.end(); I != E; ++I) { |
| 700 | Pass *PRequired = *I; |
| 701 | unsigned RDepth = 0; |
Devang Patel | 64619be | 2006-12-09 00:07:38 +0000 | [diff] [blame] | 702 | |
| 703 | PMDataManager &DM = PRequired->getResolver()->getPMDataManager(); |
| 704 | RDepth = DM.getDepth(); |
| 705 | |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 706 | if (PDepth == RDepth) |
| 707 | LastUses.push_back(PRequired); |
| 708 | else if (PDepth > RDepth) { |
| 709 | // Let the parent claim responsibility of last use |
Devang Patel | 832bc07 | 2006-12-15 00:08:26 +0000 | [diff] [blame] | 710 | TransferLastUses.push_back(PRequired); |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 711 | // Keep track of higher level analysis used by this manager. |
| 712 | HigherLevelAnalysis.push_back(PRequired); |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 713 | } else |
| 714 | assert (0 && "Unable to accomodate Required Pass"); |
| 715 | } |
| 716 | |
Devang Patel | 39786a9 | 2007-01-15 20:31:54 +0000 | [diff] [blame] | 717 | // Set P as P's last user until someone starts using P. |
| 718 | // However, if P is a Pass Manager then it does not need |
| 719 | // to record its last user. |
Devang Patel | d85662f | 2007-01-16 22:38:10 +0000 | [diff] [blame] | 720 | if (!dynamic_cast<PMDataManager *>(P)) |
Devang Patel | 39786a9 | 2007-01-15 20:31:54 +0000 | [diff] [blame] | 721 | LastUses.push_back(P); |
Devang Patel | d85662f | 2007-01-16 22:38:10 +0000 | [diff] [blame] | 722 | TPM->setLastUser(LastUses, P); |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 723 | |
Devang Patel | ec2b9a7 | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 724 | if (!TransferLastUses.empty()) { |
| 725 | Pass *My_PM = dynamic_cast<Pass *>(this); |
| 726 | TPM->setLastUser(TransferLastUses, My_PM); |
| 727 | TransferLastUses.clear(); |
| 728 | } |
| 729 | |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 730 | // Now, take care of required analysises that are not available. |
| 731 | for (SmallVector<AnalysisID, 8>::iterator |
| 732 | I = ReqAnalysisNotAvailable.begin(), |
| 733 | E = ReqAnalysisNotAvailable.end() ;I != E; ++I) { |
| 734 | Pass *AnalysisPass = (*I)->createPass(); |
| 735 | this->addLowerLevelRequiredPass(P, AnalysisPass); |
| 736 | } |
| 737 | |
Devang Patel | 17bff0d | 2006-12-07 22:09:36 +0000 | [diff] [blame] | 738 | // Take a note of analysis required and made available by this pass. |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 739 | // Remove the analysis not preserved by this pass |
| 740 | removeNotPreservedAnalysis(P); |
Devang Patel | 17bff0d | 2006-12-07 22:09:36 +0000 | [diff] [blame] | 741 | recordAvailableAnalysis(P); |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 742 | } |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 743 | |
| 744 | // Add pass |
| 745 | PassVector.push_back(P); |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 748 | |
| 749 | /// Populate RP with analysis pass that are required by |
| 750 | /// pass P and are available. Populate RP_NotAvail with analysis |
| 751 | /// pass that are required by pass P but are not available. |
| 752 | void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP, |
| 753 | SmallVector<AnalysisID, 8> &RP_NotAvail, |
| 754 | Pass *P) { |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 755 | AnalysisUsage AnUsage; |
| 756 | P->getAnalysisUsage(AnUsage); |
| 757 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
| 758 | for (std::vector<AnalysisID>::const_iterator |
| 759 | I = RequiredSet.begin(), E = RequiredSet.end(); |
| 760 | I != E; ++I) { |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 761 | AnalysisID AID = *I; |
| 762 | if (Pass *AnalysisPass = findAnalysisPass(*I, true)) |
| 763 | RP.push_back(AnalysisPass); |
| 764 | else |
| 765 | RP_NotAvail.push_back(AID); |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 766 | } |
Devang Patel | f58183d | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 767 | |
| 768 | const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet(); |
| 769 | for (std::vector<AnalysisID>::const_iterator I = IDs.begin(), |
| 770 | E = IDs.end(); I != E; ++I) { |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 771 | AnalysisID AID = *I; |
| 772 | if (Pass *AnalysisPass = findAnalysisPass(*I, true)) |
| 773 | RP.push_back(AnalysisPass); |
| 774 | else |
| 775 | RP_NotAvail.push_back(AID); |
Devang Patel | f58183d | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 776 | } |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 779 | // All Required analyses should be available to the pass as it runs! Here |
| 780 | // we fill in the AnalysisImpls member of the pass so that it can |
| 781 | // successfully use the getAnalysis() method to retrieve the |
| 782 | // implementations it needs. |
| 783 | // |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 784 | void PMDataManager::initializeAnalysisImpl(Pass *P) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 785 | AnalysisUsage AnUsage; |
| 786 | P->getAnalysisUsage(AnUsage); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 787 | |
| 788 | for (std::vector<const PassInfo *>::const_iterator |
| 789 | I = AnUsage.getRequiredSet().begin(), |
| 790 | E = AnUsage.getRequiredSet().end(); I != E; ++I) { |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 791 | Pass *Impl = findAnalysisPass(*I, true); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 792 | if (Impl == 0) |
Devang Patel | 56a5c62 | 2007-04-16 20:44:16 +0000 | [diff] [blame] | 793 | // This may be analysis pass that is initialized on the fly. |
| 794 | // If that is not the case then it will raise an assert when it is used. |
| 795 | continue; |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 796 | AnalysisResolver *AR = P->getResolver(); |
Devang Patel | 984698a | 2006-12-09 01:11:34 +0000 | [diff] [blame] | 797 | AR->addAnalysisImplsPair(*I, Impl); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 801 | /// Find the pass that implements Analysis AID. If desired pass is not found |
| 802 | /// then return NULL. |
| 803 | Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) { |
| 804 | |
| 805 | // Check if AvailableAnalysis map has one entry. |
| 806 | std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID); |
| 807 | |
| 808 | if (I != AvailableAnalysis.end()) |
| 809 | return I->second; |
| 810 | |
| 811 | // Search Parents through TopLevelManager |
| 812 | if (SearchParent) |
| 813 | return TPM->findAnalysisPass(AID); |
| 814 | |
Devang Patel | 9d759b8 | 2006-12-09 00:09:12 +0000 | [diff] [blame] | 815 | return NULL; |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 818 | // Print list of passes that are last used by P. |
| 819 | void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{ |
| 820 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 821 | SmallVector<Pass *, 12> LUses; |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 822 | |
| 823 | // If this is a on the fly manager then it does not have TPM. |
| 824 | if (!TPM) |
| 825 | return; |
| 826 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 827 | TPM->collectLastUses(LUses, P); |
| 828 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 829 | for (SmallVector<Pass *, 12>::iterator I = LUses.begin(), |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 830 | E = LUses.end(); I != E; ++I) { |
| 831 | llvm::cerr << "--" << std::string(Offset*2, ' '); |
| 832 | (*I)->dumpPassStructure(0); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | void PMDataManager::dumpPassArguments() const { |
| 837 | for(std::vector<Pass *>::const_iterator I = PassVector.begin(), |
| 838 | E = PassVector.end(); I != E; ++I) { |
| 839 | if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I)) |
| 840 | PMD->dumpPassArguments(); |
| 841 | else |
| 842 | if (const PassInfo *PI = (*I)->getPassInfo()) |
| 843 | if (!PI->isAnalysisGroup()) |
| 844 | cerr << " -" << PI->getPassArgument(); |
| 845 | } |
| 846 | } |
| 847 | |
Chris Lattner | dd6304f | 2007-08-10 06:17:04 +0000 | [diff] [blame] | 848 | void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1, |
| 849 | enum PassDebuggingString S2, |
Devang Patel | d305c40 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 850 | const char *Msg) { |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 851 | if (PassDebugging < Executions) |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 852 | return; |
| 853 | cerr << (void*)this << std::string(getDepth()*2+1, ' '); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 854 | switch (S1) { |
| 855 | case EXECUTION_MSG: |
| 856 | cerr << "Executing Pass '" << P->getPassName(); |
| 857 | break; |
| 858 | case MODIFICATION_MSG: |
Devang Patel | d56e491 | 2007-06-18 21:32:29 +0000 | [diff] [blame] | 859 | cerr << "Made Modification '" << P->getPassName(); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 860 | break; |
| 861 | case FREEING_MSG: |
| 862 | cerr << " Freeing Pass '" << P->getPassName(); |
| 863 | break; |
| 864 | default: |
| 865 | break; |
| 866 | } |
| 867 | switch (S2) { |
| 868 | case ON_BASICBLOCK_MSG: |
Devang Patel | d56e491 | 2007-06-18 21:32:29 +0000 | [diff] [blame] | 869 | cerr << "' on BasicBlock '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 870 | break; |
| 871 | case ON_FUNCTION_MSG: |
Devang Patel | d56e491 | 2007-06-18 21:32:29 +0000 | [diff] [blame] | 872 | cerr << "' on Function '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 873 | break; |
| 874 | case ON_MODULE_MSG: |
Devang Patel | d56e491 | 2007-06-18 21:32:29 +0000 | [diff] [blame] | 875 | cerr << "' on Module '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 876 | break; |
| 877 | case ON_LOOP_MSG: |
Devang Patel | d56e491 | 2007-06-18 21:32:29 +0000 | [diff] [blame] | 878 | cerr << "' on Loop " << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 879 | break; |
| 880 | case ON_CG_MSG: |
Devang Patel | d56e491 | 2007-06-18 21:32:29 +0000 | [diff] [blame] | 881 | cerr << "' on Call Graph " << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 882 | break; |
| 883 | default: |
| 884 | break; |
| 885 | } |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P, |
| 889 | const std::vector<AnalysisID> &Set) |
| 890 | const { |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 891 | if (PassDebugging >= Details && !Set.empty()) { |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 892 | cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:"; |
| 893 | for (unsigned i = 0; i != Set.size(); ++i) { |
| 894 | if (i) cerr << ","; |
| 895 | cerr << " " << Set[i]->getPassName(); |
| 896 | } |
| 897 | cerr << "\n"; |
| 898 | } |
| 899 | } |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 900 | |
Devang Patel | 004937b | 2007-07-27 20:06:09 +0000 | [diff] [blame] | 901 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 902 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 903 | /// through getAnalysis interface. |
| 904 | /// This should be handled by specific pass manager. |
| 905 | void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { |
| 906 | if (TPM) { |
| 907 | TPM->dumpArguments(); |
| 908 | TPM->dumpPasses(); |
| 909 | } |
Devang Patel | 8df7cc1 | 2008-02-02 01:43:30 +0000 | [diff] [blame] | 910 | |
| 911 | // Module Level pass may required Function Level analysis info |
| 912 | // (e.g. dominator info). Pass manager uses on the fly function pass manager |
| 913 | // to provide this on demand. In that case, in Pass manager terminology, |
| 914 | // module level pass is requiring lower level analysis info managed by |
| 915 | // lower level pass manager. |
| 916 | |
| 917 | // When Pass manager is not able to order required analysis info, Pass manager |
| 918 | // checks whether any lower level manager will be able to provide this |
| 919 | // analysis info on demand or not. |
Devang Patel | 004937b | 2007-07-27 20:06:09 +0000 | [diff] [blame] | 920 | assert (0 && "Unable to handle Pass that requires lower level Analysis pass"); |
| 921 | } |
| 922 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 923 | // Destructor |
| 924 | PMDataManager::~PMDataManager() { |
| 925 | |
| 926 | for (std::vector<Pass *>::iterator I = PassVector.begin(), |
| 927 | E = PassVector.end(); I != E; ++I) |
| 928 | delete *I; |
| 929 | |
| 930 | PassVector.clear(); |
| 931 | } |
| 932 | |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 933 | //===----------------------------------------------------------------------===// |
| 934 | // NOTE: Is this the right place to define this method ? |
| 935 | // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 936 | Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const { |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 937 | return PM.findAnalysisPass(ID, dir); |
| 938 | } |
| 939 | |
Devang Patel | 9294281 | 2007-04-16 20:56:24 +0000 | [diff] [blame] | 940 | Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI, |
| 941 | Function &F) { |
| 942 | return PM.getOnTheFlyPass(P, AnalysisPI, F); |
| 943 | } |
| 944 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 945 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 946 | // BBPassManager implementation |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 947 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 948 | /// Execute all of the passes scheduled for execution by invoking |
| 949 | /// runOnBasicBlock method. Keep track of whether any of the passes modifies |
| 950 | /// the function, and if so, return true. |
| 951 | bool |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 952 | BBPassManager::runOnFunction(Function &F) { |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 953 | |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 954 | if (F.isDeclaration()) |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 955 | return false; |
| 956 | |
Devang Patel | e958559 | 2006-12-08 01:38:28 +0000 | [diff] [blame] | 957 | bool Changed = doInitialization(F); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 958 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 959 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 960 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 961 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 962 | AnalysisUsage AnUsage; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 963 | BP->getAnalysisUsage(AnUsage); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 964 | |
Devang Patel | d305c40 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 965 | dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart()); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 966 | dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet()); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 967 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 968 | initializeAnalysisImpl(BP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 969 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 970 | if (TheTimeInfo) TheTimeInfo->passStarted(BP); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 971 | Changed |= BP->runOnBasicBlock(*I); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 972 | if (TheTimeInfo) TheTimeInfo->passEnded(BP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 973 | |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 974 | if (Changed) |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 975 | dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, |
| 976 | I->getNameStart()); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 977 | dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet()); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 978 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 979 | verifyPreservedAnalysis(BP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 980 | removeNotPreservedAnalysis(BP); |
| 981 | recordAvailableAnalysis(BP); |
Devang Patel | d305c40 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 982 | removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 983 | } |
Chris Lattner | de2aa65 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 984 | |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 985 | return Changed |= doFinalization(F); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 988 | // Implement doInitialization and doFinalization |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 989 | inline bool BBPassManager::doInitialization(Module &M) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 990 | bool Changed = false; |
| 991 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 992 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 993 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 994 | Changed |= BP->doInitialization(M); |
| 995 | } |
| 996 | |
| 997 | return Changed; |
| 998 | } |
| 999 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1000 | inline bool BBPassManager::doFinalization(Module &M) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1001 | bool Changed = false; |
| 1002 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1003 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1004 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1005 | Changed |= BP->doFinalization(M); |
| 1006 | } |
| 1007 | |
| 1008 | return Changed; |
| 1009 | } |
| 1010 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1011 | inline bool BBPassManager::doInitialization(Function &F) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1012 | bool Changed = false; |
| 1013 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1014 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1015 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1016 | Changed |= BP->doInitialization(F); |
| 1017 | } |
| 1018 | |
| 1019 | return Changed; |
| 1020 | } |
| 1021 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1022 | inline bool BBPassManager::doFinalization(Function &F) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1023 | bool Changed = false; |
| 1024 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1025 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1026 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1027 | Changed |= BP->doFinalization(F); |
| 1028 | } |
| 1029 | |
| 1030 | return Changed; |
| 1031 | } |
| 1032 | |
| 1033 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1034 | //===----------------------------------------------------------------------===// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1035 | // FunctionPassManager implementation |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1036 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1037 | /// Create new Function pass manager |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1038 | FunctionPassManager::FunctionPassManager(ModuleProvider *P) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1039 | FPM = new FunctionPassManagerImpl(0); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1040 | // FPM is the top level manager. |
| 1041 | FPM->setTopLevelManager(FPM); |
Devang Patel | 1036b65 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1042 | |
| 1043 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM); |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 1044 | AnalysisResolver *AR = new AnalysisResolver(*PMD); |
Devang Patel | 1036b65 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1045 | FPM->setResolver(AR); |
| 1046 | |
Devang Patel | 1f65368 | 2006-12-08 18:57:16 +0000 | [diff] [blame] | 1047 | MP = P; |
| 1048 | } |
| 1049 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1050 | FunctionPassManager::~FunctionPassManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1051 | delete FPM; |
| 1052 | } |
| 1053 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1054 | /// add - Add a pass to the queue of passes to run. This passes |
| 1055 | /// ownership of the Pass to the PassManager. When the |
| 1056 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 1057 | /// there is no need to delete the pass. (TODO delete passes.) |
| 1058 | /// This implies that all passes MUST be allocated with 'new'. |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1059 | void FunctionPassManager::add(Pass *P) { |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1060 | FPM->add(P); |
| 1061 | } |
| 1062 | |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1063 | /// run - Execute all of the passes scheduled for execution. Keep |
| 1064 | /// track of whether any of the passes modifies the function, and if |
| 1065 | /// so, return true. |
| 1066 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1067 | bool FunctionPassManager::run(Function &F) { |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1068 | std::string errstr; |
| 1069 | if (MP->materializeFunction(&F, &errstr)) { |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 1070 | cerr << "Error reading bitcode file: " << errstr << "\n"; |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1071 | abort(); |
| 1072 | } |
Devang Patel | 272908d | 2006-12-08 22:57:48 +0000 | [diff] [blame] | 1073 | return FPM->run(F); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1077 | /// doInitialization - Run all of the initializers for the function passes. |
| 1078 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1079 | bool FunctionPassManager::doInitialization() { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1080 | return FPM->doInitialization(*MP->getModule()); |
| 1081 | } |
| 1082 | |
Dan Gohman | e6656eb | 2007-07-30 14:51:13 +0000 | [diff] [blame] | 1083 | /// doFinalization - Run all of the finalizers for the function passes. |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1084 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1085 | bool FunctionPassManager::doFinalization() { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1086 | return FPM->doFinalization(*MP->getModule()); |
| 1087 | } |
| 1088 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1089 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1090 | // FunctionPassManagerImpl implementation |
| 1091 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1092 | inline bool FunctionPassManagerImpl::doInitialization(Module &M) { |
| 1093 | bool Changed = false; |
| 1094 | |
| 1095 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1096 | FPPassManager *FP = getContainedManager(Index); |
| 1097 | Changed |= FP->doInitialization(M); |
| 1098 | } |
| 1099 | |
| 1100 | return Changed; |
| 1101 | } |
| 1102 | |
| 1103 | inline bool FunctionPassManagerImpl::doFinalization(Module &M) { |
| 1104 | bool Changed = false; |
| 1105 | |
| 1106 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1107 | FPPassManager *FP = getContainedManager(Index); |
| 1108 | Changed |= FP->doFinalization(M); |
| 1109 | } |
| 1110 | |
| 1111 | return Changed; |
| 1112 | } |
| 1113 | |
| 1114 | // Execute all the passes managed by this top level manager. |
| 1115 | // Return true if any function is modified by a pass. |
| 1116 | bool FunctionPassManagerImpl::run(Function &F) { |
| 1117 | |
| 1118 | bool Changed = false; |
| 1119 | |
| 1120 | TimingInfo::createTheTimeInfo(); |
| 1121 | |
| 1122 | dumpArguments(); |
| 1123 | dumpPasses(); |
| 1124 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 1125 | initializeAllAnalysisInfo(); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1126 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1127 | FPPassManager *FP = getContainedManager(Index); |
| 1128 | Changed |= FP->runOnFunction(F); |
| 1129 | } |
| 1130 | return Changed; |
| 1131 | } |
| 1132 | |
| 1133 | //===----------------------------------------------------------------------===// |
| 1134 | // FPPassManager implementation |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1135 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 1136 | char FPPassManager::ID = 0; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1137 | /// Print passes managed by this manager |
| 1138 | void FPPassManager::dumpPassStructure(unsigned Offset) { |
| 1139 | llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n"; |
| 1140 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1141 | FunctionPass *FP = getContainedPass(Index); |
| 1142 | FP->dumpPassStructure(Offset + 1); |
| 1143 | dumpLastUses(FP, Offset+1); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1148 | /// Execute all of the passes scheduled for execution by invoking |
| 1149 | /// runOnFunction method. Keep track of whether any of the passes modifies |
| 1150 | /// the function, and if so, return true. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1151 | bool FPPassManager::runOnFunction(Function &F) { |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1152 | |
| 1153 | bool Changed = false; |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1154 | |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1155 | if (F.isDeclaration()) |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1156 | return false; |
| 1157 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1158 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1159 | FunctionPass *FP = getContainedPass(Index); |
| 1160 | |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1161 | AnalysisUsage AnUsage; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1162 | FP->getAnalysisUsage(AnUsage); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1163 | |
Devang Patel | d305c40 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 1164 | dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart()); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1165 | dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet()); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1166 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1167 | initializeAnalysisImpl(FP); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1168 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1169 | if (TheTimeInfo) TheTimeInfo->passStarted(FP); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1170 | Changed |= FP->runOnFunction(F); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1171 | if (TheTimeInfo) TheTimeInfo->passEnded(FP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1172 | |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1173 | if (Changed) |
Devang Patel | d305c40 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 1174 | dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart()); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1175 | dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet()); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1176 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1177 | verifyPreservedAnalysis(FP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1178 | removeNotPreservedAnalysis(FP); |
| 1179 | recordAvailableAnalysis(FP); |
Devang Patel | d305c40 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 1180 | removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1181 | } |
| 1182 | return Changed; |
| 1183 | } |
| 1184 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1185 | bool FPPassManager::runOnModule(Module &M) { |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1186 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1187 | bool Changed = doInitialization(M); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1188 | |
| 1189 | for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 1190 | this->runOnFunction(*I); |
| 1191 | |
| 1192 | return Changed |= doFinalization(M); |
| 1193 | } |
| 1194 | |
| 1195 | inline bool FPPassManager::doInitialization(Module &M) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1196 | bool Changed = false; |
| 1197 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1198 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1199 | FunctionPass *FP = getContainedPass(Index); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1200 | Changed |= FP->doInitialization(M); |
| 1201 | } |
| 1202 | |
| 1203 | return Changed; |
| 1204 | } |
| 1205 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1206 | inline bool FPPassManager::doFinalization(Module &M) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1207 | bool Changed = false; |
| 1208 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1209 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1210 | FunctionPass *FP = getContainedPass(Index); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1211 | Changed |= FP->doFinalization(M); |
| 1212 | } |
| 1213 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1214 | return Changed; |
| 1215 | } |
| 1216 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1217 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1218 | // MPPassManager implementation |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1219 | |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1220 | /// Execute all of the passes scheduled for execution by invoking |
| 1221 | /// runOnModule method. Keep track of whether any of the passes modifies |
| 1222 | /// the module, and if so, return true. |
| 1223 | bool |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1224 | MPPassManager::runOnModule(Module &M) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1225 | bool Changed = false; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 1226 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1227 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1228 | ModulePass *MP = getContainedPass(Index); |
| 1229 | |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1230 | AnalysisUsage AnUsage; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1231 | MP->getAnalysisUsage(AnUsage); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1232 | |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 1233 | dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, |
| 1234 | M.getModuleIdentifier().c_str()); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1235 | dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet()); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1236 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1237 | initializeAnalysisImpl(MP); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1238 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1239 | if (TheTimeInfo) TheTimeInfo->passStarted(MP); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1240 | Changed |= MP->runOnModule(M); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1241 | if (TheTimeInfo) TheTimeInfo->passEnded(MP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1242 | |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1243 | if (Changed) |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 1244 | dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG, |
| 1245 | M.getModuleIdentifier().c_str()); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1246 | dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet()); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1247 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1248 | verifyPreservedAnalysis(MP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1249 | removeNotPreservedAnalysis(MP); |
| 1250 | recordAvailableAnalysis(MP); |
Devang Patel | d305c40 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 1251 | removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1252 | } |
| 1253 | return Changed; |
| 1254 | } |
| 1255 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1256 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 1257 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 1258 | /// through getAnalysis interface. |
| 1259 | void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { |
| 1260 | |
| 1261 | assert (P->getPotentialPassManagerType() == PMT_ModulePassManager |
| 1262 | && "Unable to handle Pass that requires lower level Analysis pass"); |
| 1263 | assert ((P->getPotentialPassManagerType() < |
| 1264 | RequiredPass->getPotentialPassManagerType()) |
| 1265 | && "Unable to handle Pass that requires lower level Analysis pass"); |
| 1266 | |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1267 | FunctionPassManagerImpl *FPP = OnTheFlyManagers[P]; |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1268 | if (!FPP) { |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1269 | FPP = new FunctionPassManagerImpl(0); |
| 1270 | // FPP is the top level manager. |
| 1271 | FPP->setTopLevelManager(FPP); |
| 1272 | |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1273 | OnTheFlyManagers[P] = FPP; |
| 1274 | } |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1275 | FPP->add(RequiredPass); |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1276 | |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1277 | // Register P as the last user of RequiredPass. |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 1278 | SmallVector<Pass *, 12> LU; |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1279 | LU.push_back(RequiredPass); |
| 1280 | FPP->setLastUser(LU, P); |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1281 | } |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1282 | |
| 1283 | /// Return function pass corresponding to PassInfo PI, that is |
| 1284 | /// required by module pass MP. Instantiate analysis pass, by using |
| 1285 | /// its runOnFunction() for function F. |
| 1286 | Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, |
| 1287 | Function &F) { |
| 1288 | AnalysisID AID = PI; |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1289 | FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP]; |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1290 | assert (FPP && "Unable to find on the fly pass"); |
| 1291 | |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1292 | FPP->run(F); |
| 1293 | return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID); |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1297 | //===----------------------------------------------------------------------===// |
| 1298 | // PassManagerImpl implementation |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1299 | // |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1300 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1301 | /// whether any of the passes modifies the module, and if so, return true. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1302 | bool PassManagerImpl::run(Module &M) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1303 | |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1304 | bool Changed = false; |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 1305 | |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1306 | TimingInfo::createTheTimeInfo(); |
| 1307 | |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 1308 | dumpArguments(); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1309 | dumpPasses(); |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 1310 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 1311 | initializeAllAnalysisInfo(); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1312 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1313 | MPPassManager *MP = getContainedManager(Index); |
Devang Patel | 5bbeb49 | 2006-12-08 22:47:25 +0000 | [diff] [blame] | 1314 | Changed |= MP->runOnModule(M); |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1315 | } |
| 1316 | return Changed; |
| 1317 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1318 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1319 | //===----------------------------------------------------------------------===// |
| 1320 | // PassManager implementation |
| 1321 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1322 | /// Create new pass manager |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1323 | PassManager::PassManager() { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1324 | PM = new PassManagerImpl(0); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1325 | // PM is the top level manager |
| 1326 | PM->setTopLevelManager(PM); |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1329 | PassManager::~PassManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1330 | delete PM; |
| 1331 | } |
| 1332 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1333 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 1334 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 1335 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 1336 | /// implies that all passes MUST be allocated with 'new'. |
| 1337 | void |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1338 | PassManager::add(Pass *P) { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1339 | PM->add(P); |
| 1340 | } |
| 1341 | |
| 1342 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1343 | /// whether any of the passes modifies the module, and if so, return true. |
| 1344 | bool |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1345 | PassManager::run(Module &M) { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1346 | return PM->run(M); |
| 1347 | } |
| 1348 | |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1349 | //===----------------------------------------------------------------------===// |
| 1350 | // TimingInfo Class - This class is used to calculate information about the |
| 1351 | // amount of time each pass takes to execute. This only happens with |
| 1352 | // -time-passes is enabled on the command line. |
| 1353 | // |
| 1354 | bool llvm::TimePassesIsEnabled = false; |
| 1355 | static cl::opt<bool,true> |
| 1356 | EnableTiming("time-passes", cl::location(TimePassesIsEnabled), |
| 1357 | cl::desc("Time each pass, printing elapsed time for each on exit")); |
| 1358 | |
| 1359 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to |
| 1360 | // a non null value (if the -time-passes option is enabled) or it leaves it |
| 1361 | // null. It may be called multiple times. |
| 1362 | void TimingInfo::createTheTimeInfo() { |
| 1363 | if (!TimePassesIsEnabled || TheTimeInfo) return; |
| 1364 | |
| 1365 | // Constructed the first time this is called, iff -time-passes is enabled. |
| 1366 | // This guarantees that the object will be constructed before static globals, |
| 1367 | // thus it will be destroyed before them. |
| 1368 | static ManagedStatic<TimingInfo> TTI; |
| 1369 | TheTimeInfo = &*TTI; |
| 1370 | } |
| 1371 | |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 1372 | /// If TimingInfo is enabled then start pass timer. |
| 1373 | void StartPassTimer(Pass *P) { |
| 1374 | if (TheTimeInfo) |
| 1375 | TheTimeInfo->passStarted(P); |
| 1376 | } |
| 1377 | |
| 1378 | /// If TimingInfo is enabled then stop pass timer. |
| 1379 | void StopPassTimer(Pass *P) { |
| 1380 | if (TheTimeInfo) |
| 1381 | TheTimeInfo->passEnded(P); |
| 1382 | } |
| 1383 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1384 | //===----------------------------------------------------------------------===// |
| 1385 | // PMStack implementation |
| 1386 | // |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 1387 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1388 | // Pop Pass Manager from the stack and clear its analysis info. |
| 1389 | void PMStack::pop() { |
| 1390 | |
| 1391 | PMDataManager *Top = this->top(); |
| 1392 | Top->initializeAnalysisInfo(); |
| 1393 | |
| 1394 | S.pop_back(); |
| 1395 | } |
| 1396 | |
| 1397 | // Push PM on the stack and set its top level manager. |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1398 | void PMStack::push(Pass *P) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1399 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1400 | PMDataManager *Top = NULL; |
| 1401 | PMDataManager *PM = dynamic_cast<PMDataManager *>(P); |
| 1402 | assert (PM && "Unable to push. Pass Manager expected"); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1403 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1404 | if (this->empty()) { |
| 1405 | Top = PM; |
| 1406 | } |
| 1407 | else { |
| 1408 | Top = this->top(); |
| 1409 | PMTopLevelManager *TPM = Top->getTopLevelManager(); |
| 1410 | |
| 1411 | assert (TPM && "Unable to find top level manager"); |
| 1412 | TPM->addIndirectPassManager(PM); |
| 1413 | PM->setTopLevelManager(TPM); |
| 1414 | } |
| 1415 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1416 | S.push_back(PM); |
| 1417 | } |
| 1418 | |
| 1419 | // Dump content of the pass manager stack. |
| 1420 | void PMStack::dump() { |
| 1421 | for(std::deque<PMDataManager *>::iterator I = S.begin(), |
| 1422 | E = S.end(); I != E; ++I) { |
| 1423 | Pass *P = dynamic_cast<Pass *>(*I); |
Chris Lattner | de2aa65 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 1424 | printf("%s ", P->getPassName()); |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1425 | } |
| 1426 | if (!S.empty()) |
Chris Lattner | de2aa65 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 1427 | printf("\n"); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1430 | /// Find appropriate Module Pass Manager in the PM Stack and |
| 1431 | /// add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1432 | void ModulePass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1433 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1434 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1435 | // Find Module Pass Manager |
| 1436 | while(!PMS.empty()) { |
Devang Patel | 23f8aa9 | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1437 | PassManagerType TopPMType = PMS.top()->getPassManagerType(); |
| 1438 | if (TopPMType == PreferredType) |
| 1439 | break; // We found desired pass manager |
| 1440 | else if (TopPMType > PMT_ModulePassManager) |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1441 | PMS.pop(); // Pop children pass managers |
Devang Patel | ac99eca | 2007-01-11 19:59:06 +0000 | [diff] [blame] | 1442 | else |
| 1443 | break; |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Devang Patel | 23f8aa9 | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1446 | PMS.top()->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1449 | /// Find appropriate Function Pass Manager or Call Graph Pass Manager |
| 1450 | /// in the PM Stack and add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1451 | void FunctionPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1452 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1453 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1454 | // Find Module Pass Manager (TODO : Or Call Graph Pass Manager) |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1455 | while(!PMS.empty()) { |
Devang Patel | ac99eca | 2007-01-11 19:59:06 +0000 | [diff] [blame] | 1456 | if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager) |
| 1457 | PMS.pop(); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1458 | else |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1459 | break; |
| 1460 | } |
| 1461 | FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top()); |
| 1462 | |
| 1463 | // Create new Function Pass Manager |
| 1464 | if (!FPP) { |
| 1465 | assert(!PMS.empty() && "Unable to create Function Pass Manager"); |
| 1466 | PMDataManager *PMD = PMS.top(); |
| 1467 | |
| 1468 | // [1] Create new Function Pass Manager |
| 1469 | FPP = new FPPassManager(PMD->getDepth() + 1); |
| 1470 | |
| 1471 | // [2] Set up new manager's top level manager |
| 1472 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 1473 | TPM->addIndirectPassManager(FPP); |
| 1474 | |
| 1475 | // [3] Assign manager to manage this new manager. This may create |
| 1476 | // and push new managers into PMS |
| 1477 | Pass *P = dynamic_cast<Pass *>(FPP); |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1478 | |
| 1479 | // If Call Graph Pass Manager is active then use it to manage |
| 1480 | // this new Function Pass manager. |
| 1481 | if (PMD->getPassManagerType() == PMT_CallGraphPassManager) |
| 1482 | P->assignPassManager(PMS, PMT_CallGraphPassManager); |
| 1483 | else |
| 1484 | P->assignPassManager(PMS); |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1485 | |
| 1486 | // [4] Push new manager into PMS |
| 1487 | PMS.push(FPP); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1490 | // Assign FPP as the manager of this pass. |
| 1491 | FPP->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1494 | /// Find appropriate Basic Pass Manager or Call Graph Pass Manager |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1495 | /// in the PM Stack and add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1496 | void BasicBlockPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1497 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1498 | |
| 1499 | BBPassManager *BBP = NULL; |
| 1500 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1501 | // Basic Pass Manager is a leaf pass manager. It does not handle |
| 1502 | // any other pass manager. |
Chris Lattner | de2aa65 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 1503 | if (!PMS.empty()) |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1504 | BBP = dynamic_cast<BBPassManager *>(PMS.top()); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1505 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1506 | // If leaf manager is not Basic Block Pass manager then create new |
| 1507 | // basic Block Pass manager. |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1508 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1509 | if (!BBP) { |
| 1510 | assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager"); |
| 1511 | PMDataManager *PMD = PMS.top(); |
| 1512 | |
| 1513 | // [1] Create new Basic Block Manager |
| 1514 | BBP = new BBPassManager(PMD->getDepth() + 1); |
| 1515 | |
| 1516 | // [2] Set up new manager's top level manager |
| 1517 | // Basic Block Pass Manager does not live by itself |
| 1518 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 1519 | TPM->addIndirectPassManager(BBP); |
| 1520 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1521 | // [3] Assign manager to manage this new manager. This may create |
| 1522 | // and push new managers into PMS |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1523 | Pass *P = dynamic_cast<Pass *>(BBP); |
| 1524 | P->assignPassManager(PMS); |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1525 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1526 | // [4] Push new manager into PMS |
| 1527 | PMS.push(BBP); |
| 1528 | } |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1529 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1530 | // Assign BBP as the manager of this pass. |
| 1531 | BBP->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Devang Patel | c6b5a55 | 2007-01-05 20:16:23 +0000 | [diff] [blame] | 1534 | |