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