Chris Lattner | d0e9bd1 | 2002-04-28 20:42:50 +0000 | [diff] [blame] | 1 | //===- PassManagerT.h - Container for Passes ---------------------*- C++ -*--=// |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | d0e9bd1 | 2002-04-28 20:42:50 +0000 | [diff] [blame] | 3 | // This file defines the PassManagerT class. This class is used to hold, |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 4 | // maintain, and optimize execution of Pass's. The PassManager class ensures |
| 5 | // that analysis results are available before a pass runs, and that Pass's are |
| 6 | // destroyed when the PassManager is destroyed. |
| 7 | // |
Chris Lattner | d0e9bd1 | 2002-04-28 20:42:50 +0000 | [diff] [blame] | 8 | // The PassManagerT template is instantiated three times to do its job. The |
| 9 | // public PassManager class is a Pimpl around the PassManagerT<Module> interface |
| 10 | // to avoid having all of the PassManager clients being exposed to the |
| 11 | // implementation details herein. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | d0e9bd1 | 2002-04-28 20:42:50 +0000 | [diff] [blame] | 15 | #ifndef LLVM_PASSMANAGER_T_H |
| 16 | #define LLVM_PASSMANAGER_T_H |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 17 | |
| 18 | #include "llvm/Pass.h" |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 19 | #include "Support/CommandLine.h" |
Chris Lattner | ed6504b | 2002-09-08 19:00:07 +0000 | [diff] [blame] | 20 | #include "Support/LeakDetector.h" |
Chris Lattner | af751b8 | 2002-10-01 19:54:07 +0000 | [diff] [blame] | 21 | #include "Support/Timer.h" |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Chris Lattner | 395c27a | 2002-07-31 18:04:17 +0000 | [diff] [blame] | 23 | #include <iostream> |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 24 | class Annotable; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 27 | // Pass debugging information. Often it is useful to find out what pass is |
| 28 | // running when a crash occurs in a utility. When this library is compiled with |
| 29 | // debugging on, a command line option (--debug-pass) is enabled that causes the |
| 30 | // pass name to be printed before it executes. |
| 31 | // |
| 32 | |
| 33 | // Different debug levels that can be enabled... |
| 34 | enum PassDebugLevel { |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 35 | None, Arguments, Structure, Executions, Details |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static cl::opt<enum PassDebugLevel> |
| 39 | PassDebugging("debug-pass", cl::Hidden, |
| 40 | cl::desc("Print PassManager debugging information"), |
| 41 | cl::values( |
| 42 | clEnumVal(None , "disable debug output"), |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 43 | clEnumVal(Arguments , "print pass arguments to pass to 'opt'"), |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 44 | clEnumVal(Structure , "print pass structure before run()"), |
| 45 | clEnumVal(Executions, "print pass name before it is executed"), |
| 46 | clEnumVal(Details , "print pass details when it is executed"), |
| 47 | 0)); |
| 48 | |
| 49 | //===----------------------------------------------------------------------===// |
Chris Lattner | 57d2ba3 | 2002-04-04 19:35:24 +0000 | [diff] [blame] | 50 | // PMDebug class - a set of debugging functions, that are not to be |
| 51 | // instantiated by the template. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 52 | // |
| 53 | struct PMDebug { |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 54 | static void PerformPassStartupStuff(Pass *P) { |
| 55 | // If debugging is enabled, print out argument information... |
| 56 | if (PassDebugging >= Arguments) { |
| 57 | std::cerr << "Pass Arguments: "; |
| 58 | PrintArgumentInformation(P); |
| 59 | std::cerr << "\n"; |
| 60 | |
| 61 | // Print the pass execution structure |
| 62 | if (PassDebugging >= Structure) |
| 63 | P->dumpPassStructure(); |
| 64 | } |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 65 | } |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 66 | |
| 67 | static void PrintArgumentInformation(const Pass *P); |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 68 | static void PrintPassInformation(unsigned,const char*,Pass *, Annotable *); |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 69 | static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 70 | const std::vector<AnalysisID> &); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 74 | //===----------------------------------------------------------------------===// |
| 75 | // TimingInfo Class - This class is used to calculate information about the |
| 76 | // amount of time each pass takes to execute. This only happens when |
| 77 | // -time-passes is enabled on the command line. |
| 78 | // |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 79 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 80 | class TimingInfo { |
Chris Lattner | af751b8 | 2002-10-01 19:54:07 +0000 | [diff] [blame] | 81 | std::map<Pass*, Timer> TimingData; |
| 82 | TimerGroup TG; |
| 83 | |
| 84 | // Private ctor, must use 'create' member |
| 85 | TimingInfo() : TG("... Pass execution timing report ...") {} |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 86 | public: |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 87 | // TimingDtor - Print out information about timing information |
Chris Lattner | af751b8 | 2002-10-01 19:54:07 +0000 | [diff] [blame] | 88 | ~TimingInfo() { |
| 89 | // Delete all of the timers... |
| 90 | TimingData.clear(); |
| 91 | // TimerGroup is deleted next, printing the report. |
| 92 | } |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 93 | |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame^] | 94 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer |
| 95 | // to a non null value (if the -time-passes option is enabled) or it leaves it |
| 96 | // null. It may be called multiple times. |
| 97 | static void createTheTimeInfo(); |
| 98 | |
Chris Lattner | af751b8 | 2002-10-01 19:54:07 +0000 | [diff] [blame] | 99 | void passStarted(Pass *P) { |
Chris Lattner | d5fc902 | 2002-10-01 20:08:11 +0000 | [diff] [blame] | 100 | if (dynamic_cast<AnalysisResolver*>(P)) return; |
Chris Lattner | af751b8 | 2002-10-01 19:54:07 +0000 | [diff] [blame] | 101 | std::map<Pass*, Timer>::iterator I = TimingData.find(P); |
| 102 | if (I == TimingData.end()) |
Chris Lattner | 52db271 | 2002-10-01 20:12:06 +0000 | [diff] [blame] | 103 | I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first; |
Chris Lattner | af751b8 | 2002-10-01 19:54:07 +0000 | [diff] [blame] | 104 | I->second.startTimer(); |
| 105 | } |
| 106 | void passEnded(Pass *P) { |
Chris Lattner | d5fc902 | 2002-10-01 20:08:11 +0000 | [diff] [blame] | 107 | if (dynamic_cast<AnalysisResolver*>(P)) return; |
Chris Lattner | af751b8 | 2002-10-01 19:54:07 +0000 | [diff] [blame] | 108 | std::map<Pass*, Timer>::iterator I = TimingData.find(P); |
| 109 | assert (I != TimingData.end() && "passStarted/passEnded not nested right!"); |
| 110 | I->second.stopTimer(); |
| 111 | } |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame^] | 114 | static TimingInfo *TheTimeInfo; |
| 115 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 116 | //===----------------------------------------------------------------------===// |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 117 | // Declare the PassManagerTraits which will be specialized... |
| 118 | // |
| 119 | template<class UnitType> class PassManagerTraits; // Do not define. |
| 120 | |
| 121 | |
| 122 | //===----------------------------------------------------------------------===// |
| 123 | // PassManagerT - Container object for passes. The PassManagerT destructor |
| 124 | // deletes all passes contained inside of the PassManagerT, so you shouldn't |
| 125 | // delete passes manually, and all passes should be dynamically allocated. |
| 126 | // |
| 127 | template<typename UnitType> |
| 128 | class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{ |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 129 | typedef PassManagerTraits<UnitType> Traits; |
| 130 | typedef typename Traits::PassClass PassClass; |
| 131 | typedef typename Traits::SubPassClass SubPassClass; |
| 132 | typedef typename Traits::BatcherClass BatcherClass; |
| 133 | typedef typename Traits::ParentClass ParentClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 4c9cd82 | 2003-06-23 19:16:20 +0000 | [diff] [blame] | 135 | friend class PassManagerTraits<UnitType>::PassClass; |
| 136 | friend class PassManagerTraits<UnitType>::SubPassClass; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 137 | friend class Traits; |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 138 | friend class ImmutablePass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 140 | std::vector<PassClass*> Passes; // List of passes to run |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 141 | std::vector<ImmutablePass*> ImmutablePasses; // List of immutable passes |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 142 | |
| 143 | // The parent of this pass manager... |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 144 | ParentClass * const Parent; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 145 | |
| 146 | // The current batcher if one is in use, or null |
| 147 | BatcherClass *Batcher; |
| 148 | |
| 149 | // CurrentAnalyses - As the passes are being run, this map contains the |
| 150 | // analyses that are available to the current pass for use. This is accessed |
| 151 | // through the getAnalysis() function in this class and in Pass. |
| 152 | // |
| 153 | std::map<AnalysisID, Pass*> CurrentAnalyses; |
| 154 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 155 | // LastUseOf - This map keeps track of the last usage in our pipeline of a |
| 156 | // particular pass. When executing passes, the memory for .first is free'd |
| 157 | // after .second is run. |
| 158 | // |
| 159 | std::map<Pass*, Pass*> LastUseOf; |
| 160 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 161 | public: |
| 162 | PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {} |
| 163 | ~PassManagerT() { |
| 164 | // Delete all of the contained passes... |
Chris Lattner | a82ee2d | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 165 | for (typename std::vector<PassClass*>::iterator |
| 166 | I = Passes.begin(), E = Passes.end(); I != E; ++I) |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 167 | delete *I; |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 168 | |
| 169 | for (std::vector<ImmutablePass*>::iterator |
| 170 | I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) |
| 171 | delete *I; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // run - Run all of the queued passes on the specified module in an optimal |
| 175 | // way. |
| 176 | virtual bool runOnUnit(UnitType *M) { |
| 177 | bool MadeChanges = false; |
| 178 | closeBatcher(); |
| 179 | CurrentAnalyses.clear(); |
| 180 | |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame^] | 181 | TimingInfo::createTheTimeInfo(); |
| 182 | |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 183 | // Add any immutable passes to the CurrentAnalyses set... |
Chris Lattner | 480b37d | 2002-09-25 22:26:52 +0000 | [diff] [blame] | 184 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { |
| 185 | ImmutablePass *IPass = ImmutablePasses[i]; |
| 186 | if (const PassInfo *PI = IPass->getPassInfo()) { |
| 187 | CurrentAnalyses[PI] = IPass; |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 188 | |
| 189 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 190 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
Chris Lattner | 480b37d | 2002-09-25 22:26:52 +0000 | [diff] [blame] | 191 | CurrentAnalyses[II[i]] = IPass; |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 192 | } |
Chris Lattner | 480b37d | 2002-09-25 22:26:52 +0000 | [diff] [blame] | 193 | } |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 194 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 195 | // LastUserOf - This contains the inverted LastUseOfMap... |
| 196 | std::map<Pass *, std::vector<Pass*> > LastUserOf; |
| 197 | for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(), |
| 198 | E = LastUseOf.end(); I != E; ++I) |
| 199 | LastUserOf[I->second].push_back(I->first); |
| 200 | |
| 201 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 202 | // Output debug information... |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 203 | if (Parent == 0) PMDebug::PerformPassStartupStuff(this); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 204 | |
| 205 | // Run all of the passes |
| 206 | for (unsigned i = 0, e = Passes.size(); i < e; ++i) { |
| 207 | PassClass *P = Passes[i]; |
| 208 | |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 209 | PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P, |
| 210 | (Annotable*)M); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 211 | |
| 212 | // Get information about what analyses the pass uses... |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 213 | AnalysisUsage AnUsage; |
| 214 | P->getAnalysisUsage(AnUsage); |
| 215 | PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P, |
| 216 | AnUsage.getRequiredSet()); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 217 | |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 218 | // All Required analyses should be available to the pass as it runs! Here |
| 219 | // we fill in the AnalysisImpls member of the pass so that it can |
| 220 | // successfully use the getAnalysis() method to retrieve the |
| 221 | // implementations it needs. |
| 222 | // |
| 223 | P->AnalysisImpls.clear(); |
| 224 | P->AnalysisImpls.reserve(AnUsage.getRequiredSet().size()); |
| 225 | for (std::vector<const PassInfo *>::const_iterator |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 226 | I = AnUsage.getRequiredSet().begin(), |
| 227 | E = AnUsage.getRequiredSet().end(); I != E; ++I) { |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 228 | Pass *Impl = getAnalysisOrNullUp(*I); |
| 229 | if (Impl == 0) { |
| 230 | std::cerr << "Analysis '" << (*I)->getPassName() |
| 231 | << "' used but not available!"; |
| 232 | assert(0 && "Analysis used but not available!"); |
| 233 | } else if (PassDebugging == Details) { |
| 234 | if ((*I)->getPassName() != std::string(Impl->getPassName())) |
| 235 | std::cerr << " Interface '" << (*I)->getPassName() |
| 236 | << "' implemented by '" << Impl->getPassName() << "'\n"; |
| 237 | } |
| 238 | P->AnalysisImpls.push_back(std::make_pair(*I, Impl)); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 239 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 240 | |
| 241 | // Run the sub pass! |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame^] | 242 | if (TheTimeInfo) TheTimeInfo->passStarted(P); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 243 | bool Changed = runPass(P, M); |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame^] | 244 | if (TheTimeInfo) TheTimeInfo->passEnded(P); |
Chris Lattner | f2f31bd | 2002-02-01 04:53:36 +0000 | [diff] [blame] | 245 | MadeChanges |= Changed; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 246 | |
Chris Lattner | ed6504b | 2002-09-08 19:00:07 +0000 | [diff] [blame] | 247 | // Check for memory leaks by the pass... |
| 248 | LeakDetector::checkForGarbage(std::string("after running pass '") + |
| 249 | P->getPassName() + "'"); |
| 250 | |
Chris Lattner | f2f31bd | 2002-02-01 04:53:36 +0000 | [diff] [blame] | 251 | if (Changed) |
| 252 | PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P, |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 253 | (Annotable*)M); |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 254 | PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P, |
| 255 | AnUsage.getPreservedSet()); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 256 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 257 | |
| 258 | // Erase all analyses not in the preserved set... |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 259 | if (!AnUsage.getPreservesAll()) { |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 260 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
| 261 | for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(), |
| 262 | E = CurrentAnalyses.end(); I != E; ) |
| 263 | if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) != |
| 264 | PreservedSet.end()) |
| 265 | ++I; // This analysis is preserved, leave it in the available set... |
| 266 | else { |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 267 | if (!dynamic_cast<ImmutablePass*>(I->second)) { |
Chris Lattner | ade85ec | 2003-02-14 05:34:36 +0000 | [diff] [blame] | 268 | std::map<AnalysisID, Pass*>::iterator J = I++; |
| 269 | CurrentAnalyses.erase(J); // Analysis not preserved! |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 270 | } else { |
| 271 | ++I; |
| 272 | } |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 276 | // Add the current pass to the set of passes that have been run, and are |
| 277 | // thus available to users. |
| 278 | // |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 279 | if (const PassInfo *PI = P->getPassInfo()) { |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 280 | CurrentAnalyses[PI] = P; |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 281 | |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 282 | // This pass is the current implementation of all of the interfaces it |
| 283 | // implements as well. |
| 284 | // |
| 285 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 286 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 287 | CurrentAnalyses[II[i]] = P; |
| 288 | } |
| 289 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 290 | // Free memory for any passes that we are the last use of... |
| 291 | std::vector<Pass*> &DeadPass = LastUserOf[P]; |
| 292 | for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end(); |
| 293 | I != E; ++I) { |
| 294 | PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I, |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 295 | (Annotable*)M); |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 296 | (*I)->releaseMemory(); |
| 297 | } |
Chris Lattner | 6b6f540 | 2002-09-29 22:50:22 +0000 | [diff] [blame] | 298 | |
| 299 | // Make sure to remove dead passes from the CurrentAnalyses list... |
| 300 | for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(); |
| 301 | I != CurrentAnalyses.end(); ) { |
| 302 | std::vector<Pass*>::iterator DPI = std::find(DeadPass.begin(), |
| 303 | DeadPass.end(), I->second); |
| 304 | if (DPI != DeadPass.end()) { // This pass is dead now... remove it |
| 305 | std::map<AnalysisID, Pass*>::iterator IDead = I++; |
| 306 | CurrentAnalyses.erase(IDead); |
| 307 | } else { |
| 308 | ++I; // Move on to the next element... |
| 309 | } |
| 310 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 311 | } |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame^] | 312 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 313 | return MadeChanges; |
| 314 | } |
| 315 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 316 | // dumpPassStructure - Implement the -debug-passes=PassStructure option |
| 317 | virtual void dumpPassStructure(unsigned Offset = 0) { |
Chris Lattner | 480b37d | 2002-09-25 22:26:52 +0000 | [diff] [blame] | 318 | // Print out the immutable passes... |
| 319 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) |
| 320 | ImmutablePasses[i]->dumpPassStructure(0); |
| 321 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 322 | std::cerr << std::string(Offset*2, ' ') << Traits::getPMName() |
| 323 | << " Pass Manager\n"; |
Chris Lattner | a82ee2d | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 324 | for (typename std::vector<PassClass*>::iterator |
| 325 | I = Passes.begin(), E = Passes.end(); I != E; ++I) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 326 | PassClass *P = *I; |
| 327 | P->dumpPassStructure(Offset+1); |
| 328 | |
| 329 | // Loop through and see which classes are destroyed after this one... |
| 330 | for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(), |
| 331 | E = LastUseOf.end(); I != E; ++I) { |
| 332 | if (P == I->second) { |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 333 | std::cerr << "--" << std::string(Offset*2, ' '); |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 334 | I->first->dumpPassStructure(0); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | } |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 6043042 | 2003-04-24 20:07:38 +0000 | [diff] [blame] | 340 | Pass *getImmutablePassOrNull(const PassInfo *ID) const { |
| 341 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { |
| 342 | const PassInfo *IPID = ImmutablePasses[i]->getPassInfo(); |
| 343 | if (IPID == ID) |
| 344 | return ImmutablePasses[i]; |
| 345 | |
| 346 | // This pass is the current implementation of all of the interfaces it |
| 347 | // implements as well. |
| 348 | // |
| 349 | const std::vector<const PassInfo*> &II = |
| 350 | IPID->getInterfacesImplemented(); |
| 351 | for (unsigned j = 0, e = II.size(); j != e; ++j) |
| 352 | if (II[j] == ID) return ImmutablePasses[i]; |
| 353 | } |
| 354 | return 0; |
| 355 | } |
| 356 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 357 | Pass *getAnalysisOrNullDown(const PassInfo *ID) const { |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 358 | std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID); |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 359 | |
| 360 | if (I != CurrentAnalyses.end()) |
| 361 | return I->second; // Found it. |
| 362 | |
Chris Lattner | 6043042 | 2003-04-24 20:07:38 +0000 | [diff] [blame] | 363 | if (Pass *P = getImmutablePassOrNull(ID)) |
| 364 | return P; |
| 365 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 366 | if (Batcher) |
| 367 | return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID); |
| 368 | return 0; |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 371 | Pass *getAnalysisOrNullUp(const PassInfo *ID) const { |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 372 | std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID); |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 373 | if (I != CurrentAnalyses.end()) |
| 374 | return I->second; // Found it. |
| 375 | |
| 376 | if (Parent) // Try scanning... |
| 377 | return Parent->getAnalysisOrNullUp(ID); |
Chris Lattner | 6043042 | 2003-04-24 20:07:38 +0000 | [diff] [blame] | 378 | else if (!ImmutablePasses.empty()) |
| 379 | return getImmutablePassOrNull(ID); |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 380 | return 0; |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | // markPassUsed - Inform higher level pass managers (and ourselves) |
| 384 | // that these analyses are being used by this pass. This is used to |
| 385 | // make sure that analyses are not free'd before we have to use |
| 386 | // them... |
| 387 | // |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 388 | void markPassUsed(const PassInfo *P, Pass *User) { |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 389 | std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(P); |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 390 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 391 | if (I != CurrentAnalyses.end()) { |
| 392 | LastUseOf[I->second] = User; // Local pass, extend the lifetime |
| 393 | } else { |
| 394 | // Pass not in current available set, must be a higher level pass |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 395 | // available to us, propagate to parent pass manager... We tell the |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 396 | // parent that we (the passmanager) are using the analysis so that it |
| 397 | // frees the analysis AFTER this pass manager runs. |
| 398 | // |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 399 | if (Parent) { |
| 400 | Parent->markPassUsed(P, this); |
| 401 | } else { |
Chris Lattner | 6043042 | 2003-04-24 20:07:38 +0000 | [diff] [blame] | 402 | assert(getAnalysisOrNullUp(P) && |
| 403 | dynamic_cast<ImmutablePass*>(getAnalysisOrNullUp(P)) && |
| 404 | "Pass available but not found! " |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 405 | "Perhaps this is a module pass requiring a function pass?"); |
| 406 | } |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 409 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 410 | // Return the number of parent PassManagers that exist |
| 411 | virtual unsigned getDepth() const { |
| 412 | if (Parent == 0) return 0; |
| 413 | return 1 + Parent->getDepth(); |
| 414 | } |
| 415 | |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 416 | virtual unsigned getNumContainedPasses() const { return Passes.size(); } |
| 417 | virtual const Pass *getContainedPass(unsigned N) const { |
| 418 | assert(N < Passes.size() && "Pass number out of range!"); |
| 419 | return Passes[N]; |
| 420 | } |
| 421 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 422 | // add - Add a pass to the queue of passes to run. This gives ownership of |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 423 | // the Pass to the PassManager. When the PassManager is destroyed, the pass |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 424 | // will be destroyed as well, so there is no need to delete the pass. This |
| 425 | // implies that all passes MUST be new'd. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 426 | // |
| 427 | void add(PassClass *P) { |
| 428 | // Get information about what analyses the pass uses... |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 429 | AnalysisUsage AnUsage; |
| 430 | P->getAnalysisUsage(AnUsage); |
| 431 | const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet(); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 432 | |
| 433 | // Loop over all of the analyses used by this pass, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 434 | for (std::vector<AnalysisID>::const_iterator I = Required.begin(), |
| 435 | E = Required.end(); I != E; ++I) { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 436 | if (getAnalysisOrNullDown(*I) == 0) |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 437 | add((PassClass*)(*I)->createPass()); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | // Tell the pass to add itself to this PassManager... the way it does so |
| 441 | // depends on the class of the pass, and is critical to laying out passes in |
| 442 | // an optimal order.. |
| 443 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 444 | P->addToPassManager(this, AnUsage); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | private: |
| 448 | |
| 449 | // addPass - These functions are used to implement the subclass specific |
| 450 | // behaviors present in PassManager. Basically the add(Pass*) method ends up |
| 451 | // reflecting its behavior into a Pass::addToPassManager call. Subclasses of |
| 452 | // Pass override it specifically so that they can reflect the type |
| 453 | // information inherent in "this" back to the PassManager. |
| 454 | // |
| 455 | // For generic Pass subclasses (which are interprocedural passes), we simply |
| 456 | // add the pass to the end of the pass list and terminate any accumulation of |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 457 | // FunctionPass's that are present. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 458 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 459 | void addPass(PassClass *P, AnalysisUsage &AnUsage) { |
| 460 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 461 | |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 462 | // FIXME: If this pass being added isn't killed by any of the passes in the |
| 463 | // batcher class then we can reorder to pass to execute before the batcher |
| 464 | // does, which will potentially allow us to batch more passes! |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 465 | // |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 466 | //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet(); |
| 467 | if (Batcher /*&& ProvidedSet.empty()*/) |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 468 | closeBatcher(); // This pass cannot be batched! |
| 469 | |
| 470 | // Set the Resolver instance variable in the Pass so that it knows where to |
| 471 | // find this object... |
| 472 | // |
| 473 | setAnalysisResolver(P, this); |
| 474 | Passes.push_back(P); |
| 475 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 476 | // Inform higher level pass managers (and ourselves) that these analyses are |
| 477 | // being used by this pass. This is used to make sure that analyses are not |
| 478 | // free'd before we have to use them... |
| 479 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 480 | for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(), |
| 481 | E = RequiredSet.end(); I != E; ++I) |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 482 | markPassUsed(*I, P); // Mark *I as used by P |
| 483 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 484 | // Erase all analyses not in the preserved set... |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 485 | if (!AnUsage.getPreservesAll()) { |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 486 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
| 487 | for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(), |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 488 | E = CurrentAnalyses.end(); I != E; ) { |
| 489 | if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) == |
| 490 | PreservedSet.end()) { // Analysis not preserved! |
| 491 | CurrentAnalyses.erase(I); // Remove from available analyses |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 492 | I = CurrentAnalyses.begin(); |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 493 | } else { |
| 494 | ++I; |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 495 | } |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 496 | } |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 497 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 498 | |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 499 | // Add this pass to the currently available set... |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 500 | if (const PassInfo *PI = P->getPassInfo()) { |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 501 | CurrentAnalyses[PI] = P; |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 502 | |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 503 | // This pass is the current implementation of all of the interfaces it |
| 504 | // implements as well. |
| 505 | // |
| 506 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 507 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 508 | CurrentAnalyses[II[i]] = P; |
| 509 | } |
| 510 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 511 | // For now assume that our results are never used... |
| 512 | LastUseOf[P] = P; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 515 | // For FunctionPass subclasses, we must be sure to batch the FunctionPass's |
| 516 | // together in a BatcherClass object so that all of the analyses are run |
| 517 | // together a function at a time. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 518 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 519 | void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 520 | if (Batcher == 0) // If we don't have a batcher yet, make one now. |
| 521 | Batcher = new BatcherClass(this); |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 522 | // The Batcher will queue the passes up |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 523 | MP->addToPassManager(Batcher, AnUsage); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | // closeBatcher - Terminate the batcher that is being worked on. |
| 527 | void closeBatcher() { |
| 528 | if (Batcher) { |
| 529 | Passes.push_back(Batcher); |
| 530 | Batcher = 0; |
| 531 | } |
| 532 | } |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 533 | |
| 534 | public: |
| 535 | // When an ImmutablePass is added, it gets added to the top level pass |
| 536 | // manager. |
| 537 | void addPass(ImmutablePass *IP, AnalysisUsage &AU) { |
| 538 | if (Parent) { // Make sure this request goes to the top level passmanager... |
| 539 | Parent->addPass(IP, AU); |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | // Set the Resolver instance variable in the Pass so that it knows where to |
| 544 | // find this object... |
| 545 | // |
| 546 | setAnalysisResolver(IP, this); |
| 547 | ImmutablePasses.push_back(IP); |
Chris Lattner | d4ed5c6 | 2003-02-26 19:10:57 +0000 | [diff] [blame] | 548 | |
| 549 | // All Required analyses should be available to the pass as it initializes! |
| 550 | // Here we fill in the AnalysisImpls member of the pass so that it can |
| 551 | // successfully use the getAnalysis() method to retrieve the implementations |
| 552 | // it needs. |
| 553 | // |
| 554 | IP->AnalysisImpls.clear(); |
| 555 | IP->AnalysisImpls.reserve(AU.getRequiredSet().size()); |
| 556 | for (std::vector<const PassInfo *>::const_iterator |
| 557 | I = AU.getRequiredSet().begin(), |
| 558 | E = AU.getRequiredSet().end(); I != E; ++I) { |
| 559 | Pass *Impl = getAnalysisOrNullUp(*I); |
| 560 | if (Impl == 0) { |
| 561 | std::cerr << "Analysis '" << (*I)->getPassName() |
| 562 | << "' used but not available!"; |
| 563 | assert(0 && "Analysis used but not available!"); |
| 564 | } else if (PassDebugging == Details) { |
| 565 | if ((*I)->getPassName() != std::string(Impl->getPassName())) |
| 566 | std::cerr << " Interface '" << (*I)->getPassName() |
| 567 | << "' implemented by '" << Impl->getPassName() << "'\n"; |
| 568 | } |
| 569 | IP->AnalysisImpls.push_back(std::make_pair(*I, Impl)); |
| 570 | } |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 571 | |
Chris Lattner | d4ed5c6 | 2003-02-26 19:10:57 +0000 | [diff] [blame] | 572 | // Initialize the immutable pass... |
| 573 | IP->initializePass(); |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 574 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 575 | }; |
| 576 | |
| 577 | |
| 578 | |
| 579 | //===----------------------------------------------------------------------===// |
| 580 | // PassManagerTraits<BasicBlock> Specialization |
| 581 | // |
| 582 | // This pass manager is used to group together all of the BasicBlockPass's |
| 583 | // into a single unit. |
| 584 | // |
| 585 | template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass { |
| 586 | // PassClass - The type of passes tracked by this PassManager |
| 587 | typedef BasicBlockPass PassClass; |
| 588 | |
| 589 | // SubPassClass - The types of classes that should be collated together |
| 590 | // This is impossible to match, so BasicBlock instantiations of PassManagerT |
| 591 | // do not collate. |
| 592 | // |
| 593 | typedef PassManagerT<Module> SubPassClass; |
| 594 | |
| 595 | // BatcherClass - The type to use for collation of subtypes... This class is |
| 596 | // never instantiated for the PassManager<BasicBlock>, but it must be an |
| 597 | // instance of PassClass to typecheck. |
| 598 | // |
| 599 | typedef PassClass BatcherClass; |
| 600 | |
| 601 | // ParentClass - The type of the parent PassManager... |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 602 | typedef PassManagerT<Function> ParentClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 603 | |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 604 | // PMType - The type of the passmanager that subclasses this class |
| 605 | typedef PassManagerT<BasicBlock> PMType; |
| 606 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 607 | // runPass - Specify how the pass should be run on the UnitType |
| 608 | static bool runPass(PassClass *P, BasicBlock *M) { |
| 609 | // todo, init and finalize |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 610 | return P->runOnBasicBlock(*M); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 613 | // getPMName() - Return the name of the unit the PassManager operates on for |
| 614 | // debugging. |
| 615 | const char *getPMName() const { return "BasicBlock"; } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 616 | virtual const char *getPassName() const { return "BasicBlock Pass Manager"; } |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 617 | |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 618 | // Implement the BasicBlockPass interface... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 619 | virtual bool doInitialization(Module &M); |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 620 | virtual bool doInitialization(Function &F); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 621 | virtual bool runOnBasicBlock(BasicBlock &BB); |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 622 | virtual bool doFinalization(Function &F); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 623 | virtual bool doFinalization(Module &M); |
Chris Lattner | cb42906 | 2002-04-30 18:50:17 +0000 | [diff] [blame] | 624 | |
| 625 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 626 | AU.setPreservesAll(); |
| 627 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 628 | }; |
| 629 | |
| 630 | |
| 631 | |
| 632 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 633 | // PassManagerTraits<Function> Specialization |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 634 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 635 | // This pass manager is used to group together all of the FunctionPass's |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 636 | // into a single unit. |
| 637 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 638 | template<> struct PassManagerTraits<Function> : public FunctionPass { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 639 | // PassClass - The type of passes tracked by this PassManager |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 640 | typedef FunctionPass PassClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 641 | |
| 642 | // SubPassClass - The types of classes that should be collated together |
| 643 | typedef BasicBlockPass SubPassClass; |
| 644 | |
| 645 | // BatcherClass - The type to use for collation of subtypes... |
| 646 | typedef PassManagerT<BasicBlock> BatcherClass; |
| 647 | |
| 648 | // ParentClass - The type of the parent PassManager... |
| 649 | typedef PassManagerT<Module> ParentClass; |
| 650 | |
| 651 | // PMType - The type of the passmanager that subclasses this class |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 652 | typedef PassManagerT<Function> PMType; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 653 | |
| 654 | // runPass - Specify how the pass should be run on the UnitType |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 655 | static bool runPass(PassClass *P, Function *F) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 656 | return P->runOnFunction(*F); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 659 | // getPMName() - Return the name of the unit the PassManager operates on for |
| 660 | // debugging. |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 661 | const char *getPMName() const { return "Function"; } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 662 | virtual const char *getPassName() const { return "Function Pass Manager"; } |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 663 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 664 | // Implement the FunctionPass interface... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 665 | virtual bool doInitialization(Module &M); |
| 666 | virtual bool runOnFunction(Function &F); |
| 667 | virtual bool doFinalization(Module &M); |
Chris Lattner | cb42906 | 2002-04-30 18:50:17 +0000 | [diff] [blame] | 668 | |
| 669 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 670 | AU.setPreservesAll(); |
| 671 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 672 | }; |
| 673 | |
| 674 | |
| 675 | |
| 676 | //===----------------------------------------------------------------------===// |
| 677 | // PassManagerTraits<Module> Specialization |
| 678 | // |
| 679 | // This is the top level PassManager implementation that holds generic passes. |
| 680 | // |
| 681 | template<> struct PassManagerTraits<Module> : public Pass { |
| 682 | // PassClass - The type of passes tracked by this PassManager |
| 683 | typedef Pass PassClass; |
| 684 | |
| 685 | // SubPassClass - The types of classes that should be collated together |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 686 | typedef FunctionPass SubPassClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 687 | |
| 688 | // BatcherClass - The type to use for collation of subtypes... |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 689 | typedef PassManagerT<Function> BatcherClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 690 | |
| 691 | // ParentClass - The type of the parent PassManager... |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 692 | typedef AnalysisResolver ParentClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 693 | |
| 694 | // runPass - Specify how the pass should be run on the UnitType |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 695 | static bool runPass(PassClass *P, Module *M) { return P->run(*M); } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 696 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 697 | // getPMName() - Return the name of the unit the PassManager operates on for |
| 698 | // debugging. |
| 699 | const char *getPMName() const { return "Module"; } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 700 | virtual const char *getPassName() const { return "Module Pass Manager"; } |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 701 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 702 | // run - Implement the PassManager interface... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 703 | bool run(Module &M) { |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame^] | 704 | return ((PassManagerT<Module>*)this)->runOnUnit(&M); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 705 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 706 | }; |
| 707 | |
| 708 | |
| 709 | |
| 710 | //===----------------------------------------------------------------------===// |
| 711 | // PassManagerTraits Method Implementations |
| 712 | // |
| 713 | |
| 714 | // PassManagerTraits<BasicBlock> Implementations |
| 715 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 716 | inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) { |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 717 | bool Changed = false; |
| 718 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 719 | ((PMType*)this)->Passes[i]->doInitialization(M); |
| 720 | return Changed; |
| 721 | } |
| 722 | |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 723 | inline bool PassManagerTraits<BasicBlock>::doInitialization(Function &F) { |
| 724 | bool Changed = false; |
| 725 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 726 | ((PMType*)this)->Passes[i]->doInitialization(F); |
| 727 | return Changed; |
| 728 | } |
| 729 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 730 | inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) { |
| 731 | return ((PMType*)this)->runOnUnit(&BB); |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 734 | inline bool PassManagerTraits<BasicBlock>::doFinalization(Function &F) { |
| 735 | bool Changed = false; |
| 736 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 737 | ((PMType*)this)->Passes[i]->doFinalization(F); |
| 738 | return Changed; |
| 739 | } |
| 740 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 741 | inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) { |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 742 | bool Changed = false; |
| 743 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 744 | ((PMType*)this)->Passes[i]->doFinalization(M); |
| 745 | return Changed; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 749 | // PassManagerTraits<Function> Implementations |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 750 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 751 | inline bool PassManagerTraits<Function>::doInitialization(Module &M) { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 752 | bool Changed = false; |
| 753 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 754 | ((PMType*)this)->Passes[i]->doInitialization(M); |
| 755 | return Changed; |
| 756 | } |
| 757 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 758 | inline bool PassManagerTraits<Function>::runOnFunction(Function &F) { |
| 759 | return ((PMType*)this)->runOnUnit(&F); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 762 | inline bool PassManagerTraits<Function>::doFinalization(Module &M) { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 763 | bool Changed = false; |
| 764 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 765 | ((PMType*)this)->Passes[i]->doFinalization(M); |
| 766 | return Changed; |
| 767 | } |
| 768 | |
| 769 | #endif |