Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 1 | //===- Pass.cpp - LLVM Pass Infrastructure Impementation ------------------===// |
| 2 | // |
| 3 | // This file implements the LLVM Pass infrastructure. It is primarily |
| 4 | // responsible with ensuring that passes are executed and batched together |
| 5 | // optimally. |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 9 | #include "llvm/PassManager.h" |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 10 | #include "PassManagerT.h" // PassManagerT implementation |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 11 | #include "llvm/Module.h" |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 12 | #include "Support/STLExtras.h" |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 13 | #include "Support/TypeInfo.h" |
John Criswell | 3ef61af | 2003-06-30 21:59:07 +0000 | [diff] [blame] | 14 | #include "Config/sys/resource.h" |
| 15 | #include "Config/sys/time.h" |
| 16 | #include "Config/unistd.h" |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 17 | #include <set> |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 18 | |
Chris Lattner | 675e7a9 | 2002-08-21 23:51:51 +0000 | [diff] [blame] | 19 | // IncludeFile - Stub function used to help linking out. |
| 20 | IncludeFile::IncludeFile(void*) {} |
| 21 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
| 23 | // AnalysisID Class Implementation |
| 24 | // |
| 25 | |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 26 | static std::vector<const PassInfo*> CFGOnlyAnalyses; |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 27 | |
Chris Lattner | dd99f5b | 2003-10-12 18:52:12 +0000 | [diff] [blame^] | 28 | void RegisterPassBase::setOnlyUsesCFG() { |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 29 | CFGOnlyAnalyses.push_back(PIObj); |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | // AnalysisResolver Class Implementation |
| 34 | // |
| 35 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 36 | void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) { |
| 37 | assert(P->Resolver == 0 && "Pass already in a PassManager!"); |
| 38 | P->Resolver = AR; |
| 39 | } |
| 40 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | // AnalysisUsage Class Implementation |
| 43 | // |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 45 | // setPreservesCFG - This function should be called to by the pass, iff they do |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 46 | // not: |
| 47 | // |
| 48 | // 1. Add or remove basic blocks from the function |
| 49 | // 2. Modify terminator instructions in any way. |
| 50 | // |
| 51 | // This function annotates the AnalysisUsage info object to say that analyses |
| 52 | // that only depend on the CFG are preserved by this pass. |
| 53 | // |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 54 | void AnalysisUsage::setPreservesCFG() { |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 55 | // Since this transformation doesn't modify the CFG, it preserves all analyses |
| 56 | // that only depend on the CFG (like dominators, loop info, etc...) |
| 57 | // |
| 58 | Preserved.insert(Preserved.end(), |
| 59 | CFGOnlyAnalyses.begin(), CFGOnlyAnalyses.end()); |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 63 | //===----------------------------------------------------------------------===// |
| 64 | // PassManager implementation - The PassManager class is a simple Pimpl class |
| 65 | // that wraps the PassManagerT template. |
| 66 | // |
| 67 | PassManager::PassManager() : PM(new PassManagerT<Module>()) {} |
| 68 | PassManager::~PassManager() { delete PM; } |
| 69 | void PassManager::add(Pass *P) { PM->add(P); } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 70 | bool PassManager::run(Module &M) { return PM->run(M); } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 71 | |
Brian Gaeke | b7a8388 | 2003-08-12 17:22:39 +0000 | [diff] [blame] | 72 | //===----------------------------------------------------------------------===// |
| 73 | // FunctionPassManager implementation - The FunctionPassManager class |
| 74 | // is a simple Pimpl class that wraps the PassManagerT template. It |
| 75 | // is like PassManager, but only deals in FunctionPasses. |
| 76 | // |
| 77 | FunctionPassManager::FunctionPassManager() : PM(new PassManagerT<Function>()) {} |
| 78 | FunctionPassManager::~FunctionPassManager() { delete PM; } |
| 79 | void FunctionPassManager::add(FunctionPass *P) { PM->add(P); } |
Brian Gaeke | 2cc4b9d | 2003-08-14 06:07:57 +0000 | [diff] [blame] | 80 | void FunctionPassManager::add(ImmutablePass *IP) { PM->add(IP); } |
Brian Gaeke | b7a8388 | 2003-08-12 17:22:39 +0000 | [diff] [blame] | 81 | bool FunctionPassManager::run(Function &F) { return PM->run(F); } |
| 82 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 83 | |
| 84 | //===----------------------------------------------------------------------===// |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 85 | // TimingInfo Class - This class is used to calculate information about the |
| 86 | // amount of time each pass takes to execute. This only happens with |
| 87 | // -time-passes is enabled on the command line. |
| 88 | // |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 89 | static cl::opt<bool> |
| 90 | EnableTiming("time-passes", |
| 91 | cl::desc("Time each pass, printing elapsed time for each on exit")); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 92 | |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame] | 93 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to |
| 94 | // a non null value (if the -time-passes option is enabled) or it leaves it |
| 95 | // null. It may be called multiple times. |
| 96 | void TimingInfo::createTheTimeInfo() { |
| 97 | if (!EnableTiming || TheTimeInfo) return; |
| 98 | |
| 99 | // Constructed the first time this is called, iff -time-passes is enabled. |
| 100 | // This guarantees that the object will be constructed before static globals, |
| 101 | // thus it will be destroyed before them. |
| 102 | static TimingInfo TTI; |
| 103 | TheTimeInfo = &TTI; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 106 | void PMDebug::PrintArgumentInformation(const Pass *P) { |
| 107 | // Print out passes in pass manager... |
| 108 | if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) { |
| 109 | for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i) |
| 110 | PrintArgumentInformation(PM->getContainedPass(i)); |
| 111 | |
| 112 | } else { // Normal pass. Print argument information... |
| 113 | // Print out arguments for registered passes that are _optimizations_ |
| 114 | if (const PassInfo *PI = P->getPassInfo()) |
| 115 | if (PI->getPassType() & PassInfo::Optimization) |
| 116 | std::cerr << " -" << PI->getPassArgument(); |
| 117 | } |
| 118 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 119 | |
| 120 | void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 121 | Pass *P, Annotable *V) { |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 122 | if (PassDebugging >= Executions) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 123 | std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 124 | << P->getPassName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 125 | if (V) { |
| 126 | std::cerr << "' on "; |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 127 | |
| 128 | if (dynamic_cast<Module*>(V)) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 129 | std::cerr << "Module\n"; return; |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 130 | } else if (Function *F = dynamic_cast<Function*>(V)) |
| 131 | std::cerr << "Function '" << F->getName(); |
| 132 | else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V)) |
| 133 | std::cerr << "BasicBlock '" << BB->getName(); |
| 134 | else if (Value *Val = dynamic_cast<Value*>(V)) |
| 135 | std::cerr << typeid(*Val).name() << " '" << Val->getName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 136 | } |
| 137 | std::cerr << "'...\n"; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 142 | Pass *P, const std::vector<AnalysisID> &Set){ |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 143 | if (PassDebugging >= Details && !Set.empty()) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 144 | std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:"; |
Chris Lattner | b3708e2 | 2002-08-30 20:23:45 +0000 | [diff] [blame] | 145 | for (unsigned i = 0; i != Set.size(); ++i) { |
| 146 | if (i) std::cerr << ","; |
| 147 | std::cerr << " " << Set[i]->getPassName(); |
| 148 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 149 | std::cerr << "\n"; |
| 150 | } |
| 151 | } |
| 152 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 153 | //===----------------------------------------------------------------------===// |
| 154 | // Pass Implementation |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 155 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 156 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 157 | void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) { |
| 158 | PM->addPass(this, AU); |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 159 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 9ad7757 | 2003-03-21 21:41:02 +0000 | [diff] [blame] | 161 | bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const { |
| 162 | return Resolver->getAnalysisToUpdate(AnalysisID) != 0; |
| 163 | } |
| 164 | |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 165 | // dumpPassStructure - Implement the -debug-passes=Structure option |
| 166 | void Pass::dumpPassStructure(unsigned Offset) { |
| 167 | std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n"; |
| 168 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 169 | |
| 170 | // getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass. |
| 171 | // |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 172 | const char *Pass::getPassName() const { |
| 173 | if (const PassInfo *PI = getPassInfo()) |
| 174 | return PI->getPassName(); |
| 175 | return typeid(*this).name(); |
| 176 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 178 | // print - Print out the internal state of the pass. This is called by Analyse |
Misha Brukman | 7eb05a1 | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 179 | // to print out the contents of an analysis. Otherwise it is not necessary to |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 180 | // implement this method. |
| 181 | // |
| 182 | void Pass::print(std::ostream &O) const { |
| 183 | O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n"; |
| 184 | } |
| 185 | |
| 186 | // dump - call print(std::cerr); |
| 187 | void Pass::dump() const { |
| 188 | print(std::cerr, 0); |
| 189 | } |
| 190 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 191 | //===----------------------------------------------------------------------===// |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 192 | // ImmutablePass Implementation |
| 193 | // |
| 194 | void ImmutablePass::addToPassManager(PassManagerT<Module> *PM, |
| 195 | AnalysisUsage &AU) { |
| 196 | PM->addPass(this, AU); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | //===----------------------------------------------------------------------===// |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 201 | // FunctionPass Implementation |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 202 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 203 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 204 | // run - On a module, we run this pass by initializing, runOnFunction'ing once |
| 205 | // for every function in the module, then by finalizing. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 206 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 207 | bool FunctionPass::run(Module &M) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 208 | bool Changed = doInitialization(M); |
| 209 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 210 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 211 | if (!I->isExternal()) // Passes are not run on external functions! |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 212 | Changed |= runOnFunction(*I); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 213 | |
| 214 | return Changed | doFinalization(M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 217 | // run - On a function, we simply initialize, run the function, then finalize. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 218 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 219 | bool FunctionPass::run(Function &F) { |
| 220 | if (F.isExternal()) return false;// Passes are not run on external functions! |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 222 | return doInitialization(*F.getParent()) | runOnFunction(F) |
| 223 | | doFinalization(*F.getParent()); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 224 | } |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 225 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 226 | void FunctionPass::addToPassManager(PassManagerT<Module> *PM, |
| 227 | AnalysisUsage &AU) { |
| 228 | PM->addPass(this, AU); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 229 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 230 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 231 | void FunctionPass::addToPassManager(PassManagerT<Function> *PM, |
| 232 | AnalysisUsage &AU) { |
| 233 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | //===----------------------------------------------------------------------===// |
| 237 | // BasicBlockPass Implementation |
| 238 | // |
| 239 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 240 | // To run this pass on a function, we simply call runOnBasicBlock once for each |
| 241 | // function. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 242 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 243 | bool BasicBlockPass::runOnFunction(Function &F) { |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 244 | bool Changed = doInitialization(F); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 245 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 246 | Changed |= runOnBasicBlock(*I); |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 247 | return Changed | doFinalization(F); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // To run directly on the basic block, we initialize, runOnBasicBlock, then |
| 251 | // finalize. |
| 252 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 253 | bool BasicBlockPass::run(BasicBlock &BB) { |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 254 | Function &F = *BB.getParent(); |
| 255 | Module &M = *F.getParent(); |
| 256 | return doInitialization(M) | doInitialization(F) | runOnBasicBlock(BB) | |
| 257 | doFinalization(F) | doFinalization(M); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 260 | void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 261 | AnalysisUsage &AU) { |
| 262 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 266 | AnalysisUsage &AU) { |
| 267 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 270 | |
| 271 | //===----------------------------------------------------------------------===// |
| 272 | // Pass Registration mechanism |
| 273 | // |
| 274 | static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0; |
| 275 | static std::vector<PassRegistrationListener*> *Listeners = 0; |
| 276 | |
| 277 | // getPassInfo - Return the PassInfo data structure that corresponds to this |
| 278 | // pass... |
| 279 | const PassInfo *Pass::getPassInfo() const { |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 280 | if (PassInfoCache) return PassInfoCache; |
Chris Lattner | 4b16963 | 2002-08-21 17:08:37 +0000 | [diff] [blame] | 281 | return lookupPassInfo(typeid(*this)); |
| 282 | } |
| 283 | |
| 284 | const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) { |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 285 | if (PassInfoMap == 0) return 0; |
Chris Lattner | 4b16963 | 2002-08-21 17:08:37 +0000 | [diff] [blame] | 286 | std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI); |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 287 | return (I != PassInfoMap->end()) ? I->second : 0; |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void RegisterPassBase::registerPass(PassInfo *PI) { |
| 291 | if (PassInfoMap == 0) |
| 292 | PassInfoMap = new std::map<TypeInfo, PassInfo*>(); |
| 293 | |
| 294 | assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() && |
| 295 | "Pass already registered!"); |
| 296 | PIObj = PI; |
| 297 | PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI)); |
| 298 | |
| 299 | // Notify any listeners... |
| 300 | if (Listeners) |
| 301 | for (std::vector<PassRegistrationListener*>::iterator |
| 302 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
| 303 | (*I)->passRegistered(PI); |
| 304 | } |
| 305 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 306 | void RegisterPassBase::unregisterPass(PassInfo *PI) { |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 307 | assert(PassInfoMap && "Pass registered but not in map!"); |
| 308 | std::map<TypeInfo, PassInfo*>::iterator I = |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 309 | PassInfoMap->find(PI->getTypeInfo()); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 310 | assert(I != PassInfoMap->end() && "Pass registered but not in map!"); |
| 311 | |
| 312 | // Remove pass from the map... |
| 313 | PassInfoMap->erase(I); |
| 314 | if (PassInfoMap->empty()) { |
| 315 | delete PassInfoMap; |
| 316 | PassInfoMap = 0; |
| 317 | } |
| 318 | |
| 319 | // Notify any listeners... |
| 320 | if (Listeners) |
| 321 | for (std::vector<PassRegistrationListener*>::iterator |
| 322 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 323 | (*I)->passUnregistered(PI); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 324 | |
| 325 | // Delete the PassInfo object itself... |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 326 | delete PI; |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 329 | //===----------------------------------------------------------------------===// |
| 330 | // Analysis Group Implementation Code |
| 331 | //===----------------------------------------------------------------------===// |
| 332 | |
| 333 | struct AnalysisGroupInfo { |
| 334 | const PassInfo *DefaultImpl; |
| 335 | std::set<const PassInfo *> Implementations; |
| 336 | AnalysisGroupInfo() : DefaultImpl(0) {} |
| 337 | }; |
| 338 | |
| 339 | static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0; |
| 340 | |
| 341 | // RegisterAGBase implementation |
| 342 | // |
| 343 | RegisterAGBase::RegisterAGBase(const std::type_info &Interface, |
| 344 | const std::type_info *Pass, bool isDefault) |
| 345 | : ImplementationInfo(0), isDefaultImplementation(isDefault) { |
| 346 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 347 | InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface)); |
| 348 | if (InterfaceInfo == 0) { // First reference to Interface, add it now. |
| 349 | InterfaceInfo = // Create the new PassInfo for the interface... |
| 350 | new PassInfo("", "", Interface, PassInfo::AnalysisGroup, 0, 0); |
| 351 | registerPass(InterfaceInfo); |
| 352 | PIObj = 0; |
| 353 | } |
| 354 | assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup && |
| 355 | "Trying to join an analysis group that is a normal pass!"); |
| 356 | |
| 357 | if (Pass) { |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 358 | ImplementationInfo = Pass::lookupPassInfo(*Pass); |
| 359 | assert(ImplementationInfo && |
| 360 | "Must register pass before adding to AnalysisGroup!"); |
| 361 | |
Chris Lattner | b3708e2 | 2002-08-30 20:23:45 +0000 | [diff] [blame] | 362 | // Make sure we keep track of the fact that the implementation implements |
| 363 | // the interface. |
| 364 | PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo); |
| 365 | IIPI->addInterfaceImplemented(InterfaceInfo); |
| 366 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 367 | // Lazily allocate to avoid nasty initialization order dependencies |
| 368 | if (AnalysisGroupInfoMap == 0) |
| 369 | AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>(); |
| 370 | |
| 371 | AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo]; |
| 372 | assert(AGI.Implementations.count(ImplementationInfo) == 0 && |
| 373 | "Cannot add a pass to the same analysis group more than once!"); |
| 374 | AGI.Implementations.insert(ImplementationInfo); |
| 375 | if (isDefault) { |
| 376 | assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 && |
| 377 | "Default implementation for analysis group already specified!"); |
| 378 | assert(ImplementationInfo->getNormalCtor() && |
| 379 | "Cannot specify pass as default if it does not have a default ctor"); |
| 380 | AGI.DefaultImpl = ImplementationInfo; |
| 381 | InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor()); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | void RegisterAGBase::setGroupName(const char *Name) { |
| 387 | assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!"); |
| 388 | InterfaceInfo->setPassName(Name); |
| 389 | } |
| 390 | |
| 391 | RegisterAGBase::~RegisterAGBase() { |
| 392 | if (ImplementationInfo) { |
| 393 | assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?"); |
| 394 | AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo]; |
| 395 | |
| 396 | assert(AGI.Implementations.count(ImplementationInfo) && |
| 397 | "Pass not a member of analysis group?"); |
| 398 | |
| 399 | if (AGI.DefaultImpl == ImplementationInfo) |
| 400 | AGI.DefaultImpl = 0; |
| 401 | |
| 402 | AGI.Implementations.erase(ImplementationInfo); |
| 403 | |
| 404 | // Last member of this analysis group? Unregister PassInfo, delete map entry |
| 405 | if (AGI.Implementations.empty()) { |
| 406 | assert(AGI.DefaultImpl == 0 && |
| 407 | "Default implementation didn't unregister?"); |
| 408 | AnalysisGroupInfoMap->erase(InterfaceInfo); |
| 409 | if (AnalysisGroupInfoMap->empty()) { // Delete map if empty |
| 410 | delete AnalysisGroupInfoMap; |
| 411 | AnalysisGroupInfoMap = 0; |
| 412 | } |
| 413 | |
| 414 | unregisterPass(InterfaceInfo); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 420 | //===----------------------------------------------------------------------===// |
| 421 | // PassRegistrationListener implementation |
| 422 | // |
| 423 | |
| 424 | // PassRegistrationListener ctor - Add the current object to the list of |
| 425 | // PassRegistrationListeners... |
| 426 | PassRegistrationListener::PassRegistrationListener() { |
| 427 | if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>(); |
| 428 | Listeners->push_back(this); |
| 429 | } |
| 430 | |
| 431 | // dtor - Remove object from list of listeners... |
| 432 | PassRegistrationListener::~PassRegistrationListener() { |
| 433 | std::vector<PassRegistrationListener*>::iterator I = |
| 434 | std::find(Listeners->begin(), Listeners->end(), this); |
| 435 | assert(Listeners && I != Listeners->end() && |
| 436 | "PassRegistrationListener not registered!"); |
| 437 | Listeners->erase(I); |
| 438 | |
| 439 | if (Listeners->empty()) { |
| 440 | delete Listeners; |
| 441 | Listeners = 0; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // enumeratePasses - Iterate over the registered passes, calling the |
| 446 | // passEnumerate callback on each PassInfo object. |
| 447 | // |
| 448 | void PassRegistrationListener::enumeratePasses() { |
| 449 | if (PassInfoMap) |
| 450 | for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(), |
| 451 | E = PassInfoMap->end(); I != E; ++I) |
| 452 | passEnumerate(I->second); |
| 453 | } |