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" |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 14 | #include <typeinfo> |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame^] | 16 | #include <sys/resource.h> |
| 17 | #include <sys/unistd.h> |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 18 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | // AnalysisID Class Implementation |
| 21 | // |
| 22 | |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 23 | static std::vector<const PassInfo*> CFGOnlyAnalyses; |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 25 | void RegisterPassBase::setPreservesCFG() { |
| 26 | CFGOnlyAnalyses.push_back(PIObj); |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // AnalysisResolver Class Implementation |
| 31 | // |
| 32 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 33 | void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) { |
| 34 | assert(P->Resolver == 0 && "Pass already in a PassManager!"); |
| 35 | P->Resolver = AR; |
| 36 | } |
| 37 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
| 39 | // AnalysisUsage Class Implementation |
| 40 | // |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 41 | |
| 42 | // preservesCFG - This function should be called to by the pass, iff they do |
| 43 | // not: |
| 44 | // |
| 45 | // 1. Add or remove basic blocks from the function |
| 46 | // 2. Modify terminator instructions in any way. |
| 47 | // |
| 48 | // This function annotates the AnalysisUsage info object to say that analyses |
| 49 | // that only depend on the CFG are preserved by this pass. |
| 50 | // |
| 51 | void AnalysisUsage::preservesCFG() { |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 52 | // Since this transformation doesn't modify the CFG, it preserves all analyses |
| 53 | // that only depend on the CFG (like dominators, loop info, etc...) |
| 54 | // |
| 55 | Preserved.insert(Preserved.end(), |
| 56 | CFGOnlyAnalyses.begin(), CFGOnlyAnalyses.end()); |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 60 | //===----------------------------------------------------------------------===// |
| 61 | // PassManager implementation - The PassManager class is a simple Pimpl class |
| 62 | // that wraps the PassManagerT template. |
| 63 | // |
| 64 | PassManager::PassManager() : PM(new PassManagerT<Module>()) {} |
| 65 | PassManager::~PassManager() { delete PM; } |
| 66 | void PassManager::add(Pass *P) { PM->add(P); } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 67 | bool PassManager::run(Module &M) { return PM->run(M); } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 69 | |
| 70 | //===----------------------------------------------------------------------===// |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 71 | // TimingInfo Class - This class is used to calculate information about the |
| 72 | // amount of time each pass takes to execute. This only happens with |
| 73 | // -time-passes is enabled on the command line. |
| 74 | // |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 75 | static cl::opt<bool> |
| 76 | EnableTiming("time-passes", |
| 77 | cl::desc("Time each pass, printing elapsed time for each on exit")); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame^] | 79 | static TimeRecord getTimeRecord() { |
| 80 | static unsigned long PageSize = 0; |
| 81 | |
| 82 | if (PageSize == 0) { |
| 83 | #ifdef _SC_PAGE_SIZE |
| 84 | PageSize = sysconf(_SC_PAGE_SIZE); |
| 85 | #else |
| 86 | #ifdef _SC_PAGESIZE |
| 87 | PageSize = sysconf(_SC_PAGESIZE); |
| 88 | #else |
| 89 | PageSize = getpagesize(); |
| 90 | #endif |
| 91 | #endif |
| 92 | } |
| 93 | |
| 94 | struct rusage RU; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 95 | struct timeval T; |
| 96 | gettimeofday(&T, 0); |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame^] | 97 | if (getrusage(RUSAGE_SELF, &RU)) { |
| 98 | perror("getrusage call failed: -time-passes info incorrect!"); |
| 99 | } |
| 100 | |
| 101 | TimeRecord Result; |
| 102 | Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0; |
| 103 | Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0; |
| 104 | Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0; |
| 105 | Result.MaxRSS = RU.ru_maxrss*PageSize; |
| 106 | |
| 107 | return Result; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame^] | 110 | void TimeRecord::passStart(const TimeRecord &T) { |
| 111 | Elapsed -= T.Elapsed; |
| 112 | UserTime -= T.UserTime; |
| 113 | SystemTime -= T.SystemTime; |
| 114 | RSSTemp = T.MaxRSS; |
| 115 | } |
| 116 | |
| 117 | void TimeRecord::passEnd(const TimeRecord &T) { |
| 118 | Elapsed += T.Elapsed; |
| 119 | UserTime += T.UserTime; |
| 120 | SystemTime += T.SystemTime; |
| 121 | RSSTemp = T.MaxRSS - RSSTemp; |
| 122 | MaxRSS = std::max(MaxRSS, RSSTemp); |
| 123 | } |
| 124 | |
| 125 | void TimeRecord::print(const char *PassName, const TimeRecord &Total) const { |
| 126 | fprintf(stderr, |
| 127 | " %7.4f (%5.1f%%) %7.4f (%5.1f%%) %7.4f (%5.1f%%) %7.4f (%5.1f%%) ", |
| 128 | UserTime , UserTime *100/Total.UserTime, |
| 129 | SystemTime, SystemTime*100/Total.SystemTime, |
| 130 | UserTime+SystemTime, (UserTime+SystemTime)*100/(Total.UserTime+Total.SystemTime), |
| 131 | Elapsed , Elapsed *100/Total.Elapsed); |
| 132 | |
| 133 | if (Total.MaxRSS) |
| 134 | std::cerr << MaxRSS << "\t"; |
| 135 | std::cerr << PassName << "\n"; |
| 136 | } |
| 137 | |
| 138 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 139 | // Create method. If Timing is enabled, this creates and returns a new timing |
| 140 | // object, otherwise it returns null. |
| 141 | // |
| 142 | TimingInfo *TimingInfo::create() { |
| 143 | return EnableTiming ? new TimingInfo() : 0; |
| 144 | } |
| 145 | |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame^] | 146 | void TimingInfo::passStarted(Pass *P) { |
| 147 | TimingData[P].passStart(getTimeRecord()); |
| 148 | } |
| 149 | void TimingInfo::passEnded(Pass *P) { |
| 150 | TimingData[P].passEnd(getTimeRecord()); |
| 151 | } |
| 152 | void TimeRecord::sum(const TimeRecord &TR) { |
| 153 | Elapsed += TR.Elapsed; |
| 154 | UserTime += TR.UserTime; |
| 155 | SystemTime += TR.SystemTime; |
| 156 | MaxRSS += TR.MaxRSS; |
| 157 | } |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 158 | |
| 159 | // TimingDtor - Print out information about timing information |
| 160 | TimingInfo::~TimingInfo() { |
| 161 | // Iterate over all of the data, converting it into the dual of the data map, |
| 162 | // so that the data is sorted by amount of time taken, instead of pointer. |
| 163 | // |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame^] | 164 | std::vector<std::pair<TimeRecord, Pass*> > Data; |
| 165 | TimeRecord Total; |
| 166 | for (std::map<Pass*, TimeRecord>::iterator I = TimingData.begin(), |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 167 | E = TimingData.end(); I != E; ++I) |
| 168 | // Throw out results for "grouping" pass managers... |
| 169 | if (!dynamic_cast<AnalysisResolver*>(I->first)) { |
| 170 | Data.push_back(std::make_pair(I->second, I->first)); |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame^] | 171 | Total.sum(I->second); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Sort the data by time as the primary key, in reverse order... |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame^] | 175 | std::sort(Data.begin(), Data.end(), |
| 176 | std::greater<std::pair<TimeRecord, Pass*> >()); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 177 | |
| 178 | // Print out timing header... |
Anand Shukla | 8c37789 | 2002-06-25 22:07:38 +0000 | [diff] [blame] | 179 | std::cerr << std::string(79, '=') << "\n" |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame^] | 180 | << " ... Pass execution timing report ...\n" |
| 181 | << std::string(79, '=') << "\n Total Execution Time: " |
| 182 | << (Total.UserTime+Total.SystemTime) << " seconds (" |
| 183 | << Total.Elapsed << " wall clock)\n\n ---User Time--- " |
| 184 | << "--System Time-- --User+System-- ---Wall Time---"; |
| 185 | |
| 186 | if (Total.MaxRSS) |
| 187 | std::cerr << " ---Mem---"; |
| 188 | std::cerr << " --- Pass Name ---\n"; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 189 | |
| 190 | // Loop through all of the timing data, printing it out... |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame^] | 191 | for (unsigned i = 0, e = Data.size(); i != e; ++i) |
| 192 | Data[i].first.print(Data[i].second->getPassName(), Total); |
| 193 | |
| 194 | Total.print("TOTAL", Total); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 198 | void PMDebug::PrintArgumentInformation(const Pass *P) { |
| 199 | // Print out passes in pass manager... |
| 200 | if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) { |
| 201 | for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i) |
| 202 | PrintArgumentInformation(PM->getContainedPass(i)); |
| 203 | |
| 204 | } else { // Normal pass. Print argument information... |
| 205 | // Print out arguments for registered passes that are _optimizations_ |
| 206 | if (const PassInfo *PI = P->getPassInfo()) |
| 207 | if (PI->getPassType() & PassInfo::Optimization) |
| 208 | std::cerr << " -" << PI->getPassArgument(); |
| 209 | } |
| 210 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 211 | |
| 212 | void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 213 | Pass *P, Annotable *V) { |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 214 | if (PassDebugging >= Executions) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 215 | std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 216 | << P->getPassName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 217 | if (V) { |
| 218 | std::cerr << "' on "; |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 219 | |
| 220 | if (dynamic_cast<Module*>(V)) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 221 | std::cerr << "Module\n"; return; |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 222 | } else if (Function *F = dynamic_cast<Function*>(V)) |
| 223 | std::cerr << "Function '" << F->getName(); |
| 224 | else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V)) |
| 225 | std::cerr << "BasicBlock '" << BB->getName(); |
| 226 | else if (Value *Val = dynamic_cast<Value*>(V)) |
| 227 | std::cerr << typeid(*Val).name() << " '" << Val->getName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 228 | } |
| 229 | std::cerr << "'...\n"; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 234 | Pass *P, const std::vector<AnalysisID> &Set){ |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 235 | if (PassDebugging >= Details && !Set.empty()) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 236 | std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:"; |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 237 | for (unsigned i = 0; i != Set.size(); ++i) |
| 238 | std::cerr << " " << Set[i]->getPassName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 239 | std::cerr << "\n"; |
| 240 | } |
| 241 | } |
| 242 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 243 | //===----------------------------------------------------------------------===// |
| 244 | // Pass Implementation |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 245 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 246 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 247 | void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) { |
| 248 | PM->addPass(this, AU); |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 249 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 251 | // dumpPassStructure - Implement the -debug-passes=Structure option |
| 252 | void Pass::dumpPassStructure(unsigned Offset) { |
| 253 | std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n"; |
| 254 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 255 | |
| 256 | // getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass. |
| 257 | // |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 258 | const char *Pass::getPassName() const { |
| 259 | if (const PassInfo *PI = getPassInfo()) |
| 260 | return PI->getPassName(); |
| 261 | return typeid(*this).name(); |
| 262 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 264 | // print - Print out the internal state of the pass. This is called by Analyse |
| 265 | // to print out the contents of an analysis. Otherwise it is not neccesary to |
| 266 | // implement this method. |
| 267 | // |
| 268 | void Pass::print(std::ostream &O) const { |
| 269 | O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n"; |
| 270 | } |
| 271 | |
| 272 | // dump - call print(std::cerr); |
| 273 | void Pass::dump() const { |
| 274 | print(std::cerr, 0); |
| 275 | } |
| 276 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 277 | //===----------------------------------------------------------------------===// |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 278 | // FunctionPass Implementation |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 279 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 280 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 281 | // run - On a module, we run this pass by initializing, runOnFunction'ing once |
| 282 | // for every function in the module, then by finalizing. |
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 FunctionPass::run(Module &M) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 285 | bool Changed = doInitialization(M); |
| 286 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 287 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 288 | if (!I->isExternal()) // Passes are not run on external functions! |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 289 | Changed |= runOnFunction(*I); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 290 | |
| 291 | return Changed | doFinalization(M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 294 | // run - On a function, we simply initialize, run the function, then finalize. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 295 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 296 | bool FunctionPass::run(Function &F) { |
| 297 | if (F.isExternal()) return false;// Passes are not run on external functions! |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 299 | return doInitialization(*F.getParent()) | runOnFunction(F) |
| 300 | | doFinalization(*F.getParent()); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 301 | } |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 302 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 303 | void FunctionPass::addToPassManager(PassManagerT<Module> *PM, |
| 304 | AnalysisUsage &AU) { |
| 305 | PM->addPass(this, AU); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 306 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 307 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 308 | void FunctionPass::addToPassManager(PassManagerT<Function> *PM, |
| 309 | AnalysisUsage &AU) { |
| 310 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | //===----------------------------------------------------------------------===// |
| 314 | // BasicBlockPass Implementation |
| 315 | // |
| 316 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 317 | // To run this pass on a function, we simply call runOnBasicBlock once for each |
| 318 | // function. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 319 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 320 | bool BasicBlockPass::runOnFunction(Function &F) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 321 | bool Changed = false; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 322 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 323 | Changed |= runOnBasicBlock(*I); |
| 324 | return Changed; |
| 325 | } |
| 326 | |
| 327 | // To run directly on the basic block, we initialize, runOnBasicBlock, then |
| 328 | // finalize. |
| 329 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 330 | bool BasicBlockPass::run(BasicBlock &BB) { |
| 331 | Module &M = *BB.getParent()->getParent(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 332 | return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M); |
| 333 | } |
| 334 | |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 335 | void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 336 | AnalysisUsage &AU) { |
| 337 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 341 | AnalysisUsage &AU) { |
| 342 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 345 | |
| 346 | //===----------------------------------------------------------------------===// |
| 347 | // Pass Registration mechanism |
| 348 | // |
| 349 | static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0; |
| 350 | static std::vector<PassRegistrationListener*> *Listeners = 0; |
| 351 | |
| 352 | // getPassInfo - Return the PassInfo data structure that corresponds to this |
| 353 | // pass... |
| 354 | const PassInfo *Pass::getPassInfo() const { |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 355 | if (PassInfoCache) return PassInfoCache; |
| 356 | if (PassInfoMap == 0) return 0; |
| 357 | std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(typeid(*this)); |
| 358 | return (I != PassInfoMap->end()) ? I->second : 0; |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void RegisterPassBase::registerPass(PassInfo *PI) { |
| 362 | if (PassInfoMap == 0) |
| 363 | PassInfoMap = new std::map<TypeInfo, PassInfo*>(); |
| 364 | |
| 365 | assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() && |
| 366 | "Pass already registered!"); |
| 367 | PIObj = PI; |
| 368 | PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI)); |
| 369 | |
| 370 | // Notify any listeners... |
| 371 | if (Listeners) |
| 372 | for (std::vector<PassRegistrationListener*>::iterator |
| 373 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
| 374 | (*I)->passRegistered(PI); |
| 375 | } |
| 376 | |
| 377 | RegisterPassBase::~RegisterPassBase() { |
| 378 | assert(PassInfoMap && "Pass registered but not in map!"); |
| 379 | std::map<TypeInfo, PassInfo*>::iterator I = |
| 380 | PassInfoMap->find(PIObj->getTypeInfo()); |
| 381 | assert(I != PassInfoMap->end() && "Pass registered but not in map!"); |
| 382 | |
| 383 | // Remove pass from the map... |
| 384 | PassInfoMap->erase(I); |
| 385 | if (PassInfoMap->empty()) { |
| 386 | delete PassInfoMap; |
| 387 | PassInfoMap = 0; |
| 388 | } |
| 389 | |
| 390 | // Notify any listeners... |
| 391 | if (Listeners) |
| 392 | for (std::vector<PassRegistrationListener*>::iterator |
| 393 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
| 394 | (*I)->passUnregistered(PIObj); |
| 395 | |
| 396 | // Delete the PassInfo object itself... |
| 397 | delete PIObj; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | |
| 402 | //===----------------------------------------------------------------------===// |
| 403 | // PassRegistrationListener implementation |
| 404 | // |
| 405 | |
| 406 | // PassRegistrationListener ctor - Add the current object to the list of |
| 407 | // PassRegistrationListeners... |
| 408 | PassRegistrationListener::PassRegistrationListener() { |
| 409 | if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>(); |
| 410 | Listeners->push_back(this); |
| 411 | } |
| 412 | |
| 413 | // dtor - Remove object from list of listeners... |
| 414 | PassRegistrationListener::~PassRegistrationListener() { |
| 415 | std::vector<PassRegistrationListener*>::iterator I = |
| 416 | std::find(Listeners->begin(), Listeners->end(), this); |
| 417 | assert(Listeners && I != Listeners->end() && |
| 418 | "PassRegistrationListener not registered!"); |
| 419 | Listeners->erase(I); |
| 420 | |
| 421 | if (Listeners->empty()) { |
| 422 | delete Listeners; |
| 423 | Listeners = 0; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | // enumeratePasses - Iterate over the registered passes, calling the |
| 428 | // passEnumerate callback on each PassInfo object. |
| 429 | // |
| 430 | void PassRegistrationListener::enumeratePasses() { |
| 431 | if (PassInfoMap) |
| 432 | for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(), |
| 433 | E = PassInfoMap->end(); I != E; ++I) |
| 434 | passEnumerate(I->second); |
| 435 | } |