+ PassManagerImpl(int Depth) : PMDataManager(Depth) {
+ activeManager = NULL;
+ }
+
+ /// add - Add a pass to the queue of passes to run. This passes ownership of
+ /// the Pass to the PassManager. When the PassManager is destroyed, the pass
+ /// will be destroyed as well, so there is no need to delete the pass. This
+ /// implies that all passes MUST be allocated with 'new'.
+ void add(Pass *P) {
+ schedulePass(P);
+ }
+
+ /// run - Execute all of the passes scheduled for execution. Keep track of
+ /// whether any of the passes modifies the module, and if so, return true.
+ bool run(Module &M);
+
+ /// Pass Manager itself does not invalidate any analysis info.
+ void getAnalysisUsage(AnalysisUsage &Info) const {
+ Info.setPreservesAll();
+ }
+
+ inline void addTopLevelPass(Pass *P) {
+
+ if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
+
+ // P is a immutable pass and it will be managed by this
+ // top level manager. Set up analysis resolver to connect them.
+ AnalysisResolver *AR = new AnalysisResolver(*this);
+ P->setResolver(AR);
+ initializeAnalysisImpl(P);
+ addImmutablePass(IP);
+ recordAvailableAnalysis(IP);
+ }
+ else
+ addPass(P);
+ }
+
+ MPPassManager *getContainedManager(unsigned N) {
+ assert ( N < PassManagers.size() && "Pass number out of range!");
+ MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
+ return MP;
+ }
+
+private:
+
+ /// Add a pass into a passmanager queue.
+ bool addPass(Pass *p);
+
+ // Active Pass Manager
+ MPPassManager *activeManager;
+};
+
+} // End of llvm namespace
+
+namespace {
+
+//===----------------------------------------------------------------------===//
+// TimingInfo Class - This class is used to calculate information about the
+// amount of time each pass takes to execute. This only happens when
+// -time-passes is enabled on the command line.
+//
+
+class VISIBILITY_HIDDEN TimingInfo {
+ std::map<Pass*, Timer> TimingData;
+ TimerGroup TG;
+
+public:
+ // Use 'create' member to get this.
+ TimingInfo() : TG("... Pass execution timing report ...") {}
+
+ // TimingDtor - Print out information about timing information
+ ~TimingInfo() {
+ // Delete all of the timers...
+ TimingData.clear();
+ // TimerGroup is deleted next, printing the report.
+ }
+
+ // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
+ // to a non null value (if the -time-passes option is enabled) or it leaves it
+ // null. It may be called multiple times.
+ static void createTheTimeInfo();
+
+ void passStarted(Pass *P) {
+
+ if (dynamic_cast<PMDataManager *>(P))
+ return;
+
+ std::map<Pass*, Timer>::iterator I = TimingData.find(P);
+ if (I == TimingData.end())
+ I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
+ I->second.startTimer();
+ }
+ void passEnded(Pass *P) {
+
+ if (dynamic_cast<PMDataManager *>(P))
+ return;
+
+ std::map<Pass*, Timer>::iterator I = TimingData.find(P);
+ assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
+ I->second.stopTimer();
+ }
+};
+
+static TimingInfo *TheTimeInfo;
+
+} // End of anon namespace
+
+//===----------------------------------------------------------------------===//
+// PMTopLevelManager implementation
+
+/// Set pass P as the last user of the given analysis passes.
+void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
+ Pass *P) {
+
+ for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
+ E = AnalysisPasses.end(); I != E; ++I) {
+ Pass *AP = *I;
+ LastUser[AP] = P;
+ // If AP is the last user of other passes then make P last user of
+ // such passes.
+ for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
+ LUE = LastUser.end(); LUI != LUE; ++LUI) {
+ if (LUI->second == AP)
+ LastUser[LUI->first] = P;
+ }
+ }
+}
+
+/// Collect passes whose last user is P
+void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
+ Pass *P) {
+ for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
+ LUE = LastUser.end(); LUI != LUE; ++LUI)
+ if (LUI->second == P)
+ LastUses.push_back(LUI->first);
+}
+
+/// Schedule pass P for execution. Make sure that passes required by
+/// P are run before P is run. Update analysis info maintained by
+/// the manager. Remove dead passes. This is a recursive function.
+void PMTopLevelManager::schedulePass(Pass *P) {
+
+ // TODO : Allocate function manager for this pass, other wise required set
+ // may be inserted into previous function manager
+
+ AnalysisUsage AnUsage;
+ P->getAnalysisUsage(AnUsage);
+ const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
+ for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
+ E = RequiredSet.end(); I != E; ++I) {
+
+ Pass *AnalysisPass = findAnalysisPass(*I);
+ if (!AnalysisPass) {
+ // Schedule this analysis run first.
+ AnalysisPass = (*I)->createPass();
+ schedulePass(AnalysisPass);
+ }
+ }
+
+ // Now all required passes are available.
+ addTopLevelPass(P);
+}
+
+/// Find the pass that implements Analysis AID. Search immutable
+/// passes and all pass managers. If desired pass is not found
+/// then return NULL.
+Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
+
+ Pass *P = NULL;
+ // Check pass managers
+ for (std::vector<Pass *>::iterator I = PassManagers.begin(),
+ E = PassManagers.end(); P == NULL && I != E; ++I) {
+ PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
+ assert(PMD && "This is not a PassManager");
+ P = PMD->findAnalysisPass(AID, false);
+ }
+
+ // Check other pass managers
+ for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
+ E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
+ P = (*I)->findAnalysisPass(AID, false);
+
+ for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
+ E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
+ const PassInfo *PI = (*I)->getPassInfo();
+ if (PI == AID)
+ P = *I;