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 | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 13 | #include "Support/CommandLine.h" |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame^] | 14 | #include "Support/TypeInfo.h" |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 15 | #include <typeinfo> |
| 16 | #include <iostream> |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 17 | #include <sys/time.h> |
| 18 | #include <stdio.h> |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
| 21 | // AnalysisID Class Implementation |
| 22 | // |
| 23 | |
| 24 | static std::vector<AnalysisID> CFGOnlyAnalyses; |
| 25 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 26 | // Source of unique analysis ID #'s. |
| 27 | unsigned AnalysisID::NextID = 0; |
| 28 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 29 | AnalysisID::AnalysisID(const AnalysisID &AID, bool DependsOnlyOnCFG) { |
| 30 | ID = AID.ID; // Implement the copy ctor part... |
| 31 | Constructor = AID.Constructor; |
| 32 | |
| 33 | // If this analysis only depends on the CFG of the function, add it to the CFG |
| 34 | // only list... |
| 35 | if (DependsOnlyOnCFG) |
| 36 | CFGOnlyAnalyses.push_back(AID); |
| 37 | } |
| 38 | |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | // AnalysisResolver Class Implementation |
| 41 | // |
| 42 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 43 | void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) { |
| 44 | assert(P->Resolver == 0 && "Pass already in a PassManager!"); |
| 45 | P->Resolver = AR; |
| 46 | } |
| 47 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 48 | //===----------------------------------------------------------------------===// |
| 49 | // AnalysisUsage Class Implementation |
| 50 | // |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 51 | |
| 52 | // preservesCFG - This function should be called to by the pass, iff they do |
| 53 | // not: |
| 54 | // |
| 55 | // 1. Add or remove basic blocks from the function |
| 56 | // 2. Modify terminator instructions in any way. |
| 57 | // |
| 58 | // This function annotates the AnalysisUsage info object to say that analyses |
| 59 | // that only depend on the CFG are preserved by this pass. |
| 60 | // |
| 61 | void AnalysisUsage::preservesCFG() { |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 62 | // Since this transformation doesn't modify the CFG, it preserves all analyses |
| 63 | // that only depend on the CFG (like dominators, loop info, etc...) |
| 64 | // |
| 65 | Preserved.insert(Preserved.end(), |
| 66 | CFGOnlyAnalyses.begin(), CFGOnlyAnalyses.end()); |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 70 | //===----------------------------------------------------------------------===// |
| 71 | // PassManager implementation - The PassManager class is a simple Pimpl class |
| 72 | // that wraps the PassManagerT template. |
| 73 | // |
| 74 | PassManager::PassManager() : PM(new PassManagerT<Module>()) {} |
| 75 | PassManager::~PassManager() { delete PM; } |
| 76 | void PassManager::add(Pass *P) { PM->add(P); } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 77 | bool PassManager::run(Module &M) { return PM->run(M); } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 79 | |
| 80 | //===----------------------------------------------------------------------===// |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 81 | // TimingInfo Class - This class is used to calculate information about the |
| 82 | // amount of time each pass takes to execute. This only happens with |
| 83 | // -time-passes is enabled on the command line. |
| 84 | // |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 85 | static cl::opt<bool> |
| 86 | EnableTiming("time-passes", |
| 87 | cl::desc("Time each pass, printing elapsed time for each on exit")); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 88 | |
| 89 | static double getTime() { |
| 90 | struct timeval T; |
| 91 | gettimeofday(&T, 0); |
| 92 | return T.tv_sec + T.tv_usec/1000000.0; |
| 93 | } |
| 94 | |
| 95 | // Create method. If Timing is enabled, this creates and returns a new timing |
| 96 | // object, otherwise it returns null. |
| 97 | // |
| 98 | TimingInfo *TimingInfo::create() { |
| 99 | return EnableTiming ? new TimingInfo() : 0; |
| 100 | } |
| 101 | |
| 102 | void TimingInfo::passStarted(Pass *P) { TimingData[P] -= getTime(); } |
| 103 | void TimingInfo::passEnded(Pass *P) { TimingData[P] += getTime(); } |
| 104 | |
| 105 | // TimingDtor - Print out information about timing information |
| 106 | TimingInfo::~TimingInfo() { |
| 107 | // Iterate over all of the data, converting it into the dual of the data map, |
| 108 | // so that the data is sorted by amount of time taken, instead of pointer. |
| 109 | // |
Anand Shukla | 8c37789 | 2002-06-25 22:07:38 +0000 | [diff] [blame] | 110 | std::vector<std::pair<double, Pass*> > Data; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 111 | double TotalTime = 0; |
| 112 | for (std::map<Pass*, double>::iterator I = TimingData.begin(), |
| 113 | E = TimingData.end(); I != E; ++I) |
| 114 | // Throw out results for "grouping" pass managers... |
| 115 | if (!dynamic_cast<AnalysisResolver*>(I->first)) { |
| 116 | Data.push_back(std::make_pair(I->second, I->first)); |
| 117 | TotalTime += I->second; |
| 118 | } |
| 119 | |
| 120 | // Sort the data by time as the primary key, in reverse order... |
Anand Shukla | 8c37789 | 2002-06-25 22:07:38 +0000 | [diff] [blame] | 121 | std::sort(Data.begin(), Data.end(), std::greater<std::pair<double, Pass*> >()); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 122 | |
| 123 | // Print out timing header... |
Anand Shukla | 8c37789 | 2002-06-25 22:07:38 +0000 | [diff] [blame] | 124 | std::cerr << std::string(79, '=') << "\n" |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 125 | << " ... Pass execution timing report ...\n" |
| 126 | << std::string(79, '=') << "\n Total Execution Time: " << TotalTime |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 127 | << " seconds\n\n % Time: Seconds:\tPass Name:\n"; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 128 | |
| 129 | // Loop through all of the timing data, printing it out... |
| 130 | for (unsigned i = 0, e = Data.size(); i != e; ++i) { |
| 131 | fprintf(stderr, " %6.2f%% %fs\t%s\n", Data[i].first*100 / TotalTime, |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 132 | Data[i].first, Data[i].second->getPassName()); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 133 | } |
Anand Shukla | 8c37789 | 2002-06-25 22:07:38 +0000 | [diff] [blame] | 134 | std::cerr << " 100.00% " << TotalTime << "s\tTOTAL\n" |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 135 | << std::string(79, '=') << "\n"; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | //===----------------------------------------------------------------------===// |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 140 | // Pass debugging information. Often it is useful to find out what pass is |
| 141 | // running when a crash occurs in a utility. When this library is compiled with |
| 142 | // debugging on, a command line option (--debug-pass) is enabled that causes the |
| 143 | // pass name to be printed before it executes. |
| 144 | // |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 145 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 146 | // Different debug levels that can be enabled... |
| 147 | enum PassDebugLevel { |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 148 | None, Structure, Executions, Details |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 149 | }; |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 150 | |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 151 | static cl::opt<enum PassDebugLevel> |
| 152 | PassDebugging("debug-pass", cl::Hidden, |
| 153 | cl::desc("Print PassManager debugging information"), |
| 154 | cl::values( |
| 155 | clEnumVal(None , "disable debug output"), |
| 156 | // TODO: add option to print out pass names "PassOptions" |
| 157 | clEnumVal(Structure , "print pass structure before run()"), |
| 158 | clEnumVal(Executions, "print pass name before it is executed"), |
| 159 | clEnumVal(Details , "print pass details when it is executed"), |
| 160 | 0)); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 161 | |
| 162 | void PMDebug::PrintPassStructure(Pass *P) { |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 163 | if (PassDebugging >= Structure) |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 164 | P->dumpPassStructure(); |
| 165 | } |
| 166 | |
| 167 | void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 168 | Pass *P, Annotable *V) { |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 169 | if (PassDebugging >= Executions) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 170 | std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 171 | << P->getPassName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 172 | if (V) { |
| 173 | std::cerr << "' on "; |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 174 | |
| 175 | if (dynamic_cast<Module*>(V)) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 176 | std::cerr << "Module\n"; return; |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 177 | } else if (Function *F = dynamic_cast<Function*>(V)) |
| 178 | std::cerr << "Function '" << F->getName(); |
| 179 | else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V)) |
| 180 | std::cerr << "BasicBlock '" << BB->getName(); |
| 181 | else if (Value *Val = dynamic_cast<Value*>(V)) |
| 182 | std::cerr << typeid(*Val).name() << " '" << Val->getName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 183 | } |
| 184 | std::cerr << "'...\n"; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 189 | Pass *P, const std::vector<AnalysisID> &Set){ |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 190 | if (PassDebugging >= Details && !Set.empty()) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 191 | std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:"; |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 192 | for (unsigned i = 0; i != Set.size(); ++i) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 193 | Pass *P = Set[i].createPass(); // Good thing this is just debug code... |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 194 | std::cerr << " " << P->getPassName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 195 | delete P; |
| 196 | } |
| 197 | std::cerr << "\n"; |
| 198 | } |
| 199 | } |
| 200 | |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 201 | // dumpPassStructure - Implement the -debug-passes=Structure option |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 202 | void Pass::dumpPassStructure(unsigned Offset = 0) { |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 203 | std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n"; |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 204 | } |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 205 | |
| 206 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 207 | //===----------------------------------------------------------------------===// |
| 208 | // Pass Implementation |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 209 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 210 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 211 | void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) { |
| 212 | PM->addPass(this, AU); |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 213 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 215 | |
| 216 | // getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass. |
| 217 | // |
| 218 | const char *Pass::getPassName() const { return typeid(*this).name(); } |
| 219 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 220 | //===----------------------------------------------------------------------===// |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 221 | // FunctionPass Implementation |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 222 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 223 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 224 | // run - On a module, we run this pass by initializing, runOnFunction'ing once |
| 225 | // for every function in the module, then by finalizing. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 226 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 227 | bool FunctionPass::run(Module &M) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 228 | bool Changed = doInitialization(M); |
| 229 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 230 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 231 | if (!I->isExternal()) // Passes are not run on external functions! |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 232 | Changed |= runOnFunction(*I); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 233 | |
| 234 | return Changed | doFinalization(M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 237 | // run - On a function, we simply initialize, run the function, then finalize. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 238 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 239 | bool FunctionPass::run(Function &F) { |
| 240 | if (F.isExternal()) return false;// Passes are not run on external functions! |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 242 | return doInitialization(*F.getParent()) | runOnFunction(F) |
| 243 | | doFinalization(*F.getParent()); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 244 | } |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 245 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 246 | void FunctionPass::addToPassManager(PassManagerT<Module> *PM, |
| 247 | AnalysisUsage &AU) { |
| 248 | PM->addPass(this, AU); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 249 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 250 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 251 | void FunctionPass::addToPassManager(PassManagerT<Function> *PM, |
| 252 | AnalysisUsage &AU) { |
| 253 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | //===----------------------------------------------------------------------===// |
| 257 | // BasicBlockPass Implementation |
| 258 | // |
| 259 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 260 | // To run this pass on a function, we simply call runOnBasicBlock once for each |
| 261 | // function. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 262 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 263 | bool BasicBlockPass::runOnFunction(Function &F) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 264 | bool Changed = false; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 265 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 266 | Changed |= runOnBasicBlock(*I); |
| 267 | return Changed; |
| 268 | } |
| 269 | |
| 270 | // To run directly on the basic block, we initialize, runOnBasicBlock, then |
| 271 | // finalize. |
| 272 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 273 | bool BasicBlockPass::run(BasicBlock &BB) { |
| 274 | Module &M = *BB.getParent()->getParent(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 275 | return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M); |
| 276 | } |
| 277 | |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 278 | void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 279 | AnalysisUsage &AU) { |
| 280 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 284 | AnalysisUsage &AU) { |
| 285 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame^] | 288 | |
| 289 | //===----------------------------------------------------------------------===// |
| 290 | // Pass Registration mechanism |
| 291 | // |
| 292 | static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0; |
| 293 | static std::vector<PassRegistrationListener*> *Listeners = 0; |
| 294 | |
| 295 | // getPassInfo - Return the PassInfo data structure that corresponds to this |
| 296 | // pass... |
| 297 | const PassInfo *Pass::getPassInfo() const { |
| 298 | assert(PassInfoMap && "PassInfoMap not constructed yet??"); |
| 299 | std::map<TypeInfo, PassInfo*>::iterator I = |
| 300 | PassInfoMap->find(typeid(*this)); |
| 301 | assert(I != PassInfoMap->end() && "Pass has not been registered!"); |
| 302 | return I->second; |
| 303 | } |
| 304 | |
| 305 | void RegisterPassBase::registerPass(PassInfo *PI) { |
| 306 | if (PassInfoMap == 0) |
| 307 | PassInfoMap = new std::map<TypeInfo, PassInfo*>(); |
| 308 | |
| 309 | assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() && |
| 310 | "Pass already registered!"); |
| 311 | PIObj = PI; |
| 312 | PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI)); |
| 313 | |
| 314 | // Notify any listeners... |
| 315 | if (Listeners) |
| 316 | for (std::vector<PassRegistrationListener*>::iterator |
| 317 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
| 318 | (*I)->passRegistered(PI); |
| 319 | } |
| 320 | |
| 321 | RegisterPassBase::~RegisterPassBase() { |
| 322 | assert(PassInfoMap && "Pass registered but not in map!"); |
| 323 | std::map<TypeInfo, PassInfo*>::iterator I = |
| 324 | PassInfoMap->find(PIObj->getTypeInfo()); |
| 325 | assert(I != PassInfoMap->end() && "Pass registered but not in map!"); |
| 326 | |
| 327 | // Remove pass from the map... |
| 328 | PassInfoMap->erase(I); |
| 329 | if (PassInfoMap->empty()) { |
| 330 | delete PassInfoMap; |
| 331 | PassInfoMap = 0; |
| 332 | } |
| 333 | |
| 334 | // Notify any listeners... |
| 335 | if (Listeners) |
| 336 | for (std::vector<PassRegistrationListener*>::iterator |
| 337 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
| 338 | (*I)->passUnregistered(PIObj); |
| 339 | |
| 340 | // Delete the PassInfo object itself... |
| 341 | delete PIObj; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | |
| 346 | //===----------------------------------------------------------------------===// |
| 347 | // PassRegistrationListener implementation |
| 348 | // |
| 349 | |
| 350 | // PassRegistrationListener ctor - Add the current object to the list of |
| 351 | // PassRegistrationListeners... |
| 352 | PassRegistrationListener::PassRegistrationListener() { |
| 353 | if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>(); |
| 354 | Listeners->push_back(this); |
| 355 | } |
| 356 | |
| 357 | // dtor - Remove object from list of listeners... |
| 358 | PassRegistrationListener::~PassRegistrationListener() { |
| 359 | std::vector<PassRegistrationListener*>::iterator I = |
| 360 | std::find(Listeners->begin(), Listeners->end(), this); |
| 361 | assert(Listeners && I != Listeners->end() && |
| 362 | "PassRegistrationListener not registered!"); |
| 363 | Listeners->erase(I); |
| 364 | |
| 365 | if (Listeners->empty()) { |
| 366 | delete Listeners; |
| 367 | Listeners = 0; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | // enumeratePasses - Iterate over the registered passes, calling the |
| 372 | // passEnumerate callback on each PassInfo object. |
| 373 | // |
| 374 | void PassRegistrationListener::enumeratePasses() { |
| 375 | if (PassInfoMap) |
| 376 | for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(), |
| 377 | E = PassInfoMap->end(); I != E; ++I) |
| 378 | passEnumerate(I->second); |
| 379 | } |