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