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