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