LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

PassManager.cpp

Go to the documentation of this file.
00001 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the LLVM Pass Manager infrastructure. 
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 
00015 #include "llvm/PassManagers.h"
00016 #include "llvm/Support/CommandLine.h"
00017 #include "llvm/Support/Timer.h"
00018 #include "llvm/Module.h"
00019 #include "llvm/ModuleProvider.h"
00020 #include "llvm/Support/Streams.h"
00021 #include "llvm/Support/ManagedStatic.h"
00022 #include "llvm/Analysis/Dominators.h"
00023 #include "llvm-c/Core.h"
00024 #include <algorithm>
00025 #include <vector>
00026 #include <map>
00027 using namespace llvm;
00028 
00029 // See PassManagers.h for Pass Manager infrastructure overview.
00030 
00031 namespace llvm {
00032 
00033 //===----------------------------------------------------------------------===//
00034 // Pass debugging information.  Often it is useful to find out what pass is
00035 // running when a crash occurs in a utility.  When this library is compiled with
00036 // debugging on, a command line option (--debug-pass) is enabled that causes the
00037 // pass name to be printed before it executes.
00038 //
00039 
00040 // Different debug levels that can be enabled...
00041 enum PassDebugLevel {
00042   None, Arguments, Structure, Executions, Details
00043 };
00044 
00045 bool VerifyDomInfo = false;
00046 static cl::opt<bool,true>
00047 VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
00048                cl::desc("Verify dominator info (time consuming)"));
00049 
00050 static cl::opt<enum PassDebugLevel>
00051 PassDebugging("debug-pass", cl::Hidden,
00052                   cl::desc("Print PassManager debugging information"),
00053                   cl::values(
00054   clEnumVal(None      , "disable debug output"),
00055   clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
00056   clEnumVal(Structure , "print pass structure before run()"),
00057   clEnumVal(Executions, "print pass name before it is executed"),
00058   clEnumVal(Details   , "print pass details when it is executed"),
00059                              clEnumValEnd));
00060 } // End of llvm namespace
00061 
00062 namespace {
00063 
00064 //===----------------------------------------------------------------------===//
00065 // BBPassManager
00066 //
00067 /// BBPassManager manages BasicBlockPass. It batches all the
00068 /// pass together and sequence them to process one basic block before
00069 /// processing next basic block.
00070 class VISIBILITY_HIDDEN BBPassManager : public PMDataManager, 
00071                                         public FunctionPass {
00072 
00073 public:
00074   static char ID;
00075   explicit BBPassManager(int Depth) 
00076     : PMDataManager(Depth), FunctionPass((intptr_t)&ID) {}
00077 
00078   /// Execute all of the passes scheduled for execution.  Keep track of
00079   /// whether any of the passes modifies the function, and if so, return true.
00080   bool runOnFunction(Function &F);
00081 
00082   /// Pass Manager itself does not invalidate any analysis info.
00083   void getAnalysisUsage(AnalysisUsage &Info) const {
00084     Info.setPreservesAll();
00085   }
00086 
00087   bool doInitialization(Module &M);
00088   bool doInitialization(Function &F);
00089   bool doFinalization(Module &M);
00090   bool doFinalization(Function &F);
00091 
00092   virtual const char *getPassName() const {
00093     return "BasicBlock Pass Manager";
00094   }
00095 
00096   // Print passes managed by this manager
00097   void dumpPassStructure(unsigned Offset) {
00098     llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
00099     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
00100       BasicBlockPass *BP = getContainedPass(Index);
00101       BP->dumpPassStructure(Offset + 1);
00102       dumpLastUses(BP, Offset+1);
00103     }
00104   }
00105 
00106   BasicBlockPass *getContainedPass(unsigned N) {
00107     assert ( N < PassVector.size() && "Pass number out of range!");
00108     BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
00109     return BP;
00110   }
00111 
00112   virtual PassManagerType getPassManagerType() const { 
00113     return PMT_BasicBlockPassManager; 
00114   }
00115 };
00116 
00117 char BBPassManager::ID = 0;
00118 }
00119 
00120 namespace llvm {
00121 
00122 //===----------------------------------------------------------------------===//
00123 // FunctionPassManagerImpl
00124 //
00125 /// FunctionPassManagerImpl manages FPPassManagers
00126 class FunctionPassManagerImpl : public Pass,
00127                                 public PMDataManager,
00128                                 public PMTopLevelManager {
00129 public:
00130   static char ID;
00131   explicit FunctionPassManagerImpl(int Depth) : 
00132     Pass((intptr_t)&ID), PMDataManager(Depth), 
00133     PMTopLevelManager(TLM_Function) { }
00134 
00135   /// add - Add a pass to the queue of passes to run.  This passes ownership of
00136   /// the Pass to the PassManager.  When the PassManager is destroyed, the pass
00137   /// will be destroyed as well, so there is no need to delete the pass.  This
00138   /// implies that all passes MUST be allocated with 'new'.
00139   void add(Pass *P) {
00140     schedulePass(P);
00141   }
00142  
00143   /// run - Execute all of the passes scheduled for execution.  Keep track of
00144   /// whether any of the passes modifies the module, and if so, return true.
00145   bool run(Function &F);
00146 
00147   /// doInitialization - Run all of the initializers for the function passes.
00148   ///
00149   bool doInitialization(Module &M);
00150   
00151   /// doFinalization - Run all of the finalizers for the function passes.
00152   ///
00153   bool doFinalization(Module &M);
00154 
00155   /// Pass Manager itself does not invalidate any analysis info.
00156   void getAnalysisUsage(AnalysisUsage &Info) const {
00157     Info.setPreservesAll();
00158   }
00159 
00160   inline void addTopLevelPass(Pass *P) {
00161 
00162     if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
00163       
00164       // P is a immutable pass and it will be managed by this
00165       // top level manager. Set up analysis resolver to connect them.
00166       AnalysisResolver *AR = new AnalysisResolver(*this);
00167       P->setResolver(AR);
00168       initializeAnalysisImpl(P);
00169       addImmutablePass(IP);
00170       recordAvailableAnalysis(IP);
00171     } else {
00172       P->assignPassManager(activeStack);
00173     }
00174 
00175   }
00176 
00177   FPPassManager *getContainedManager(unsigned N) {
00178     assert ( N < PassManagers.size() && "Pass number out of range!");
00179     FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
00180     return FP;
00181   }
00182 };
00183 
00184 char FunctionPassManagerImpl::ID = 0;
00185 //===----------------------------------------------------------------------===//
00186 // MPPassManager
00187 //
00188 /// MPPassManager manages ModulePasses and function pass managers.
00189 /// It batches all Module passes and function pass managers together and
00190 /// sequences them to process one module.
00191 class MPPassManager : public Pass, public PMDataManager {
00192  
00193 public:
00194   static char ID;
00195   explicit MPPassManager(int Depth) :
00196     Pass((intptr_t)&ID), PMDataManager(Depth) { }
00197 
00198   // Delete on the fly managers.
00199   virtual ~MPPassManager() {
00200     for (std::map<Pass *, FunctionPassManagerImpl *>::iterator 
00201            I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
00202          I != E; ++I) {
00203       FunctionPassManagerImpl *FPP = I->second;
00204       delete FPP;
00205     }
00206   }
00207 
00208   /// run - Execute all of the passes scheduled for execution.  Keep track of
00209   /// whether any of the passes modifies the module, and if so, return true.
00210   bool runOnModule(Module &M);
00211 
00212   /// Pass Manager itself does not invalidate any analysis info.
00213   void getAnalysisUsage(AnalysisUsage &Info) const {
00214     Info.setPreservesAll();
00215   }
00216 
00217   /// Add RequiredPass into list of lower level passes required by pass P.
00218   /// RequiredPass is run on the fly by Pass Manager when P requests it
00219   /// through getAnalysis interface.
00220   virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
00221 
00222   /// Return function pass corresponding to PassInfo PI, that is 
00223   /// required by module pass MP. Instantiate analysis pass, by using
00224   /// its runOnFunction() for function F.
00225   virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
00226 
00227   virtual const char *getPassName() const {
00228     return "Module Pass Manager";
00229   }
00230 
00231   // Print passes managed by this manager
00232   void dumpPassStructure(unsigned Offset) {
00233     llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
00234     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
00235       ModulePass *MP = getContainedPass(Index);
00236       MP->dumpPassStructure(Offset + 1);
00237       if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
00238         FPP->dumpPassStructure(Offset + 2);
00239       dumpLastUses(MP, Offset+1);
00240     }
00241   }
00242 
00243   ModulePass *getContainedPass(unsigned N) {
00244     assert ( N < PassVector.size() && "Pass number out of range!");
00245     ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
00246     return MP;
00247   }
00248 
00249   virtual PassManagerType getPassManagerType() const { 
00250     return PMT_ModulePassManager; 
00251   }
00252 
00253  private:
00254   /// Collection of on the fly FPPassManagers. These managers manage
00255   /// function passes that are required by module passes.
00256   std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
00257 };
00258 
00259 char MPPassManager::ID = 0;
00260 //===----------------------------------------------------------------------===//
00261 // PassManagerImpl
00262 //
00263 
00264 /// PassManagerImpl manages MPPassManagers
00265 class PassManagerImpl : public Pass,
00266                         public PMDataManager,
00267                         public PMTopLevelManager {
00268 
00269 public:
00270   static char ID;
00271   explicit PassManagerImpl(int Depth) :
00272     Pass((intptr_t)&ID), PMDataManager(Depth),
00273     PMTopLevelManager(TLM_Pass) { }
00274 
00275   /// add - Add a pass to the queue of passes to run.  This passes ownership of
00276   /// the Pass to the PassManager.  When the PassManager is destroyed, the pass
00277   /// will be destroyed as well, so there is no need to delete the pass.  This
00278   /// implies that all passes MUST be allocated with 'new'.
00279   void add(Pass *P) {
00280     schedulePass(P);
00281   }
00282  
00283   /// run - Execute all of the passes scheduled for execution.  Keep track of
00284   /// whether any of the passes modifies the module, and if so, return true.
00285   bool run(Module &M);
00286 
00287   /// Pass Manager itself does not invalidate any analysis info.
00288   void getAnalysisUsage(AnalysisUsage &Info) const {
00289     Info.setPreservesAll();
00290   }
00291 
00292   inline void addTopLevelPass(Pass *P) {
00293 
00294     if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
00295       
00296       // P is a immutable pass and it will be managed by this
00297       // top level manager. Set up analysis resolver to connect them.
00298       AnalysisResolver *AR = new AnalysisResolver(*this);
00299       P->setResolver(AR);
00300       initializeAnalysisImpl(P);
00301       addImmutablePass(IP);
00302       recordAvailableAnalysis(IP);
00303     } else {
00304       P->assignPassManager(activeStack);
00305     }
00306 
00307   }
00308 
00309   MPPassManager *getContainedManager(unsigned N) {
00310     assert ( N < PassManagers.size() && "Pass number out of range!");
00311     MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
00312     return MP;
00313   }
00314 
00315 };
00316 
00317 char PassManagerImpl::ID = 0;
00318 } // End of llvm namespace
00319 
00320 namespace {
00321 
00322 //===----------------------------------------------------------------------===//
00323 // TimingInfo Class - This class is used to calculate information about the
00324 // amount of time each pass takes to execute.  This only happens when
00325 // -time-passes is enabled on the command line.
00326 //
00327 
00328 class VISIBILITY_HIDDEN TimingInfo {
00329   std::map<Pass*, Timer> TimingData;
00330   TimerGroup TG;
00331 
00332 public:
00333   // Use 'create' member to get this.
00334   TimingInfo() : TG("... Pass execution timing report ...") {}
00335   
00336   // TimingDtor - Print out information about timing information
00337   ~TimingInfo() {
00338     // Delete all of the timers...
00339     TimingData.clear();
00340     // TimerGroup is deleted next, printing the report.
00341   }
00342 
00343   // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
00344   // to a non null value (if the -time-passes option is enabled) or it leaves it
00345   // null.  It may be called multiple times.
00346   static void createTheTimeInfo();
00347 
00348   void passStarted(Pass *P) {
00349 
00350     if (dynamic_cast<PMDataManager *>(P)) 
00351       return;
00352 
00353     std::map<Pass*, Timer>::iterator I = TimingData.find(P);
00354     if (I == TimingData.end())
00355       I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
00356     I->second.startTimer();
00357   }
00358   void passEnded(Pass *P) {
00359 
00360     if (dynamic_cast<PMDataManager *>(P)) 
00361       return;
00362 
00363     std::map<Pass*, Timer>::iterator I = TimingData.find(P);
00364     assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
00365     I->second.stopTimer();
00366   }
00367 };
00368 
00369 } // End of anon namespace
00370 
00371 static TimingInfo *TheTimeInfo;
00372 
00373 //===----------------------------------------------------------------------===//
00374 // PMTopLevelManager implementation
00375 
00376 /// Initialize top level manager. Create first pass manager.
00377 PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
00378 
00379   if (t == TLM_Pass) {
00380     MPPassManager *MPP = new MPPassManager(1);
00381     MPP->setTopLevelManager(this);
00382     addPassManager(MPP);
00383     activeStack.push(MPP);
00384   } 
00385   else if (t == TLM_Function) {
00386     FPPassManager *FPP = new FPPassManager(1);
00387     FPP->setTopLevelManager(this);
00388     addPassManager(FPP);
00389     activeStack.push(FPP);
00390   } 
00391 }
00392 
00393 /// Set pass P as the last user of the given analysis passes.
00394 void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, 
00395                                     Pass *P) {
00396 
00397   for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
00398          E = AnalysisPasses.end(); I != E; ++I) {
00399     Pass *AP = *I;
00400     LastUser[AP] = P;
00401     
00402     if (P == AP)
00403       continue;
00404 
00405     // If AP is the last user of other passes then make P last user of
00406     // such passes.
00407     for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
00408            LUE = LastUser.end(); LUI != LUE; ++LUI) {
00409       if (LUI->second == AP)
00410         // DenseMap iterator is not invalidated here because
00411         // this is just updating exisitng entry.
00412         LastUser[LUI->first] = P;
00413     }
00414   }
00415 }
00416 
00417 /// Collect passes whose last user is P
00418 void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
00419                                         Pass *P) {
00420   DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI = 
00421     InversedLastUser.find(P);
00422   if (DMI == InversedLastUser.end())
00423     return;
00424 
00425   SmallPtrSet<Pass *, 8> &LU = DMI->second;
00426   for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
00427          E = LU.end(); I != E; ++I) {
00428     LastUses.push_back(*I);
00429   }
00430 
00431 }
00432 
00433 AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
00434   AnalysisUsage *AnUsage = NULL;
00435   DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
00436   if (DMI != AnUsageMap.end()) 
00437     AnUsage = DMI->second;
00438   else {
00439     AnUsage = new AnalysisUsage();
00440     P->getAnalysisUsage(*AnUsage);
00441     AnUsageMap[P] = AnUsage;
00442   }
00443   return AnUsage;
00444 }
00445 
00446 /// Schedule pass P for execution. Make sure that passes required by
00447 /// P are run before P is run. Update analysis info maintained by
00448 /// the manager. Remove dead passes. This is a recursive function.
00449 void PMTopLevelManager::schedulePass(Pass *P) {
00450 
00451   // TODO : Allocate function manager for this pass, other wise required set
00452   // may be inserted into previous function manager
00453 
00454   // Give pass a chance to prepare the stage.
00455   P->preparePassManager(activeStack);
00456 
00457   // If P is an analysis pass and it is available then do not
00458   // generate the analysis again. Stale analysis info should not be
00459   // available at this point.
00460   if (P->getPassInfo() &&
00461       P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo()))
00462     return;
00463 
00464   AnalysisUsage *AnUsage = findAnalysisUsage(P);
00465 
00466   bool checkAnalysis = true;
00467   while (checkAnalysis) {
00468     checkAnalysis = false;
00469   
00470     const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
00471     for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
00472            E = RequiredSet.end(); I != E; ++I) {
00473       
00474       Pass *AnalysisPass = findAnalysisPass(*I);
00475       if (!AnalysisPass) {
00476         AnalysisPass = (*I)->createPass();
00477         if (P->getPotentialPassManagerType () ==
00478             AnalysisPass->getPotentialPassManagerType())
00479           // Schedule analysis pass that is managed by the same pass manager.
00480           schedulePass(AnalysisPass);
00481         else if (P->getPotentialPassManagerType () >
00482                  AnalysisPass->getPotentialPassManagerType()) {
00483           // Schedule analysis pass that is managed by a new manager.
00484           schedulePass(AnalysisPass);
00485           // Recheck analysis passes to ensure that required analysises that
00486           // are already checked are still available.
00487           checkAnalysis = true;
00488         }
00489         else
00490           // Do not schedule this analysis. Lower level analsyis 
00491           // passes are run on the fly.
00492           delete AnalysisPass;
00493       }
00494     }
00495   }
00496 
00497   // Now all required passes are available.
00498   addTopLevelPass(P);
00499 }
00500 
00501 /// Find the pass that implements Analysis AID. Search immutable
00502 /// passes and all pass managers. If desired pass is not found
00503 /// then return NULL.
00504 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
00505 
00506   Pass *P = NULL;
00507   // Check pass managers
00508   for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
00509          E = PassManagers.end(); P == NULL && I != E; ++I) {
00510     PMDataManager *PMD = *I;
00511     P = PMD->findAnalysisPass(AID, false);
00512   }
00513 
00514   // Check other pass managers
00515   for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
00516          E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
00517     P = (*I)->findAnalysisPass(AID, false);
00518 
00519   for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
00520          E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
00521     const PassInfo *PI = (*I)->getPassInfo();
00522     if (PI == AID)
00523       P = *I;
00524 
00525     // If Pass not found then check the interfaces implemented by Immutable Pass
00526     if (!P) {
00527       const std::vector<const PassInfo*> &ImmPI =
00528         PI->getInterfacesImplemented();
00529       if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
00530         P = *I;
00531     }
00532   }
00533 
00534   return P;
00535 }
00536 
00537 // Print passes managed by this top level manager.
00538 void PMTopLevelManager::dumpPasses() const {
00539 
00540   if (PassDebugging < Structure)
00541     return;
00542 
00543   // Print out the immutable passes
00544   for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
00545     ImmutablePasses[i]->dumpPassStructure(0);
00546   }
00547   
00548   // Every class that derives from PMDataManager also derives from Pass
00549   // (sometimes indirectly), but there's no inheritance relationship
00550   // between PMDataManager and Pass, so we have to dynamic_cast to get
00551   // from a PMDataManager* to a Pass*.
00552   for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
00553          E = PassManagers.end(); I != E; ++I)
00554     dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
00555 }
00556 
00557 void PMTopLevelManager::dumpArguments() const {
00558 
00559   if (PassDebugging < Arguments)
00560     return;
00561 
00562   cerr << "Pass Arguments: ";
00563   for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
00564          E = PassManagers.end(); I != E; ++I) {
00565     PMDataManager *PMD = *I;
00566     PMD->dumpPassArguments();
00567   }
00568   cerr << "\n";
00569 }
00570 
00571 void PMTopLevelManager::initializeAllAnalysisInfo() {
00572   
00573   for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
00574          E = PassManagers.end(); I != E; ++I) {
00575     PMDataManager *PMD = *I;
00576     PMD->initializeAnalysisInfo();
00577   }
00578   
00579   // Initailize other pass managers
00580   for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
00581          E = IndirectPassManagers.end(); I != E; ++I)
00582     (*I)->initializeAnalysisInfo();
00583 
00584   for(DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
00585         DME = LastUser.end(); DMI != DME; ++DMI) {
00586     DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI = 
00587       InversedLastUser.find(DMI->second);
00588     if (InvDMI != InversedLastUser.end()) {
00589       SmallPtrSet<Pass *, 8> &L = InvDMI->second;
00590       L.insert(DMI->first);
00591     } else {
00592       SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
00593       InversedLastUser[DMI->second] = L;
00594     }
00595   }
00596 }
00597 
00598 /// Destructor
00599 PMTopLevelManager::~PMTopLevelManager() {
00600   for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
00601          E = PassManagers.end(); I != E; ++I)
00602     delete *I;
00603   
00604   for (SmallVector<ImmutablePass *, 8>::iterator
00605          I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
00606     delete *I;
00607 
00608   for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
00609          DME = AnUsageMap.end(); DMI != DME; ++DMI) {
00610     AnalysisUsage *AU = DMI->second;
00611     delete AU;
00612   }
00613     
00614 }
00615 
00616 //===----------------------------------------------------------------------===//
00617 // PMDataManager implementation
00618 
00619 /// Augement AvailableAnalysis by adding analysis made available by pass P.
00620 void PMDataManager::recordAvailableAnalysis(Pass *P) {
00621                                                 
00622   if (const PassInfo *PI = P->getPassInfo()) {
00623     AvailableAnalysis[PI] = P;
00624 
00625     //This pass is the current implementation of all of the interfaces it
00626     //implements as well.
00627     const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
00628     for (unsigned i = 0, e = II.size(); i != e; ++i)
00629       AvailableAnalysis[II[i]] = P;
00630   }
00631 }
00632 
00633 // Return true if P preserves high level analysis used by other
00634 // passes managed by this manager
00635 bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
00636 
00637   AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
00638   
00639   if (AnUsage->getPreservesAll())
00640     return true;
00641   
00642   const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
00643   for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
00644          E = HigherLevelAnalysis.end(); I  != E; ++I) {
00645     Pass *P1 = *I;
00646     if (!dynamic_cast<ImmutablePass*>(P1) &&
00647         std::find(PreservedSet.begin(), PreservedSet.end(),
00648                   P1->getPassInfo()) == 
00649            PreservedSet.end())
00650       return false;
00651   }
00652   
00653   return true;
00654 }
00655 
00656 /// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
00657 void PMDataManager::verifyPreservedAnalysis(Pass *P) {
00658   // Don't do this unless assertions are enabled.
00659 #ifdef NDEBUG
00660   return;
00661 #endif
00662   AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
00663   const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
00664 
00665   // Verify preserved analysis
00666   for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
00667          E = PreservedSet.end(); I != E; ++I) {
00668     AnalysisID AID = *I;
00669     if (Pass *AP = findAnalysisPass(AID, true))
00670       AP->verifyAnalysis();
00671   }
00672 }
00673 
00674 /// verifyDomInfo - Verify dominator information if it is available.
00675 void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
00676   
00677   if (!VerifyDomInfo || !P.getResolver())
00678     return;
00679 
00680   DominatorTree *DT = P.getAnalysisToUpdate<DominatorTree>();
00681   if (!DT)
00682     return;
00683 
00684   DominatorTree OtherDT;
00685   OtherDT.getBase().recalculate(F);
00686   if (DT->compare(OtherDT)) {
00687     cerr << "Dominator Information for " << F.getNameStart() << "\n";
00688     cerr << "Pass '" << P.getPassName() << "'\n";
00689     cerr << "----- Valid -----\n";
00690     OtherDT.dump();
00691     cerr << "----- Invalid -----\n";
00692     DT->dump();
00693     assert (0 && "Invalid dominator info");
00694   }
00695 
00696   DominanceFrontier *DF = P.getAnalysisToUpdate<DominanceFrontier>();
00697   if (!DF) 
00698     return;
00699 
00700   DominanceFrontier OtherDF;
00701   std::vector<BasicBlock*> DTRoots = DT->getRoots();
00702   OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
00703   if (DF->compare(OtherDF)) {
00704     cerr << "Dominator Information for " << F.getNameStart() << "\n";
00705     cerr << "Pass '" << P.getPassName() << "'\n";
00706     cerr << "----- Valid -----\n";
00707     OtherDF.dump();
00708     cerr << "----- Invalid -----\n";
00709     DF->dump();
00710     assert (0 && "Invalid dominator info");
00711   }
00712 }
00713 
00714 /// Remove Analysis not preserved by Pass P
00715 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
00716   AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
00717   if (AnUsage->getPreservesAll())
00718     return;
00719 
00720   const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
00721   for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
00722          E = AvailableAnalysis.end(); I != E; ) {
00723     std::map<AnalysisID, Pass*>::iterator Info = I++;
00724     if (!dynamic_cast<ImmutablePass*>(Info->second)
00725         && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == 
00726         PreservedSet.end()) {
00727       // Remove this analysis
00728       AvailableAnalysis.erase(Info);
00729       if (PassDebugging >= Details) {
00730         Pass *S = Info->second;
00731         cerr << " -- '" <<  P->getPassName() << "' is not preserving '";
00732         cerr << S->getPassName() << "'\n";
00733       }
00734     }
00735   }
00736 
00737   // Check inherited analysis also. If P is not preserving analysis
00738   // provided by parent manager then remove it here.
00739   for (unsigned Index = 0; Index < PMT_Last; ++Index) {
00740 
00741     if (!InheritedAnalysis[Index])
00742       continue;
00743 
00744     for (std::map<AnalysisID, Pass*>::iterator 
00745            I = InheritedAnalysis[Index]->begin(),
00746            E = InheritedAnalysis[Index]->end(); I != E; ) {
00747       std::map<AnalysisID, Pass *>::iterator Info = I++;
00748       if (!dynamic_cast<ImmutablePass*>(Info->second) &&
00749           std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == 
00750              PreservedSet.end())
00751         // Remove this analysis
00752         InheritedAnalysis[Index]->erase(Info);
00753     }
00754   }
00755 }
00756 
00757 /// Remove analysis passes that are not used any longer
00758 void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
00759                                      enum PassDebuggingString DBG_STR) {
00760 
00761   SmallVector<Pass *, 12> DeadPasses;
00762 
00763   // If this is a on the fly manager then it does not have TPM.
00764   if (!TPM)
00765     return;
00766 
00767   TPM->collectLastUses(DeadPasses, P);
00768 
00769   if (PassDebugging >= Details && !DeadPasses.empty()) {
00770     cerr << " -*- '" <<  P->getPassName();
00771     cerr << "' is the last user of following pass instances.";
00772     cerr << " Free these instances\n";
00773   }
00774 
00775   for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
00776          E = DeadPasses.end(); I != E; ++I) {
00777 
00778     dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
00779 
00780     if (TheTimeInfo) TheTimeInfo->passStarted(*I);
00781     (*I)->releaseMemory();
00782     if (TheTimeInfo) TheTimeInfo->passEnded(*I);
00783 
00784     std::map<AnalysisID, Pass*>::iterator Pos = 
00785       AvailableAnalysis.find((*I)->getPassInfo());
00786     
00787     // It is possible that pass is already removed from the AvailableAnalysis
00788     if (Pos != AvailableAnalysis.end())
00789       AvailableAnalysis.erase(Pos);
00790   }
00791 }
00792 
00793 /// Add pass P into the PassVector. Update 
00794 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
00795 void PMDataManager::add(Pass *P, 
00796                         bool ProcessAnalysis) {
00797 
00798   // This manager is going to manage pass P. Set up analysis resolver
00799   // to connect them.
00800   AnalysisResolver *AR = new AnalysisResolver(*this);
00801   P->setResolver(AR);
00802 
00803   // If a FunctionPass F is the last user of ModulePass info M
00804   // then the F's manager, not F, records itself as a last user of M.
00805   SmallVector<Pass *, 12> TransferLastUses;
00806 
00807   if (ProcessAnalysis) {
00808 
00809     // At the moment, this pass is the last user of all required passes.
00810     SmallVector<Pass *, 12> LastUses;
00811     SmallVector<Pass *, 8> RequiredPasses;
00812     SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
00813 
00814     unsigned PDepth = this->getDepth();
00815 
00816     collectRequiredAnalysis(RequiredPasses, 
00817                             ReqAnalysisNotAvailable, P);
00818     for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
00819            E = RequiredPasses.end(); I != E; ++I) {
00820       Pass *PRequired = *I;
00821       unsigned RDepth = 0;
00822 
00823       assert (PRequired->getResolver() && "Analysis Resolver is not set");
00824       PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
00825       RDepth = DM.getDepth();
00826 
00827       if (PDepth == RDepth)
00828         LastUses.push_back(PRequired);
00829       else if (PDepth >  RDepth) {
00830         // Let the parent claim responsibility of last use
00831         TransferLastUses.push_back(PRequired);
00832         // Keep track of higher level analysis used by this manager.
00833         HigherLevelAnalysis.push_back(PRequired);
00834       } else 
00835         assert (0 && "Unable to accomodate Required Pass");
00836     }
00837 
00838     // Set P as P's last user until someone starts using P.
00839     // However, if P is a Pass Manager then it does not need
00840     // to record its last user.
00841     if (!dynamic_cast<PMDataManager *>(P))
00842       LastUses.push_back(P);
00843     TPM->setLastUser(LastUses, P);
00844 
00845     if (!TransferLastUses.empty()) {
00846       Pass *My_PM = dynamic_cast<Pass *>(this);
00847       TPM->setLastUser(TransferLastUses, My_PM);
00848       TransferLastUses.clear();
00849     }
00850 
00851     // Now, take care of required analysises that are not available.
00852     for (SmallVector<AnalysisID, 8>::iterator 
00853            I = ReqAnalysisNotAvailable.begin(), 
00854            E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
00855       Pass *AnalysisPass = (*I)->createPass();
00856       this->addLowerLevelRequiredPass(P, AnalysisPass);
00857     }
00858 
00859     // Take a note of analysis required and made available by this pass.
00860     // Remove the analysis not preserved by this pass
00861     removeNotPreservedAnalysis(P);
00862     recordAvailableAnalysis(P);
00863   }
00864 
00865   // Add pass
00866   PassVector.push_back(P);
00867 }
00868 
00869 
00870 /// Populate RP with analysis pass that are required by
00871 /// pass P and are available. Populate RP_NotAvail with analysis
00872 /// pass that are required by pass P but are not available.
00873 void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
00874                                        SmallVector<AnalysisID, 8> &RP_NotAvail,
00875                                             Pass *P) {
00876   AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
00877   const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
00878   for (AnalysisUsage::VectorType::const_iterator 
00879          I = RequiredSet.begin(), E = RequiredSet.end();
00880        I != E; ++I) {
00881     AnalysisID AID = *I;
00882     if (Pass *AnalysisPass = findAnalysisPass(*I, true))
00883       RP.push_back(AnalysisPass);   
00884     else
00885       RP_NotAvail.push_back(AID);
00886   }
00887 
00888   const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
00889   for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
00890          E = IDs.end(); I != E; ++I) {
00891     AnalysisID AID = *I;
00892     if (Pass *AnalysisPass = findAnalysisPass(*I, true))
00893       RP.push_back(AnalysisPass);   
00894     else
00895       RP_NotAvail.push_back(AID);
00896   }
00897 }
00898 
00899 // All Required analyses should be available to the pass as it runs!  Here
00900 // we fill in the AnalysisImpls member of the pass so that it can
00901 // successfully use the getAnalysis() method to retrieve the
00902 // implementations it needs.
00903 //
00904 void PMDataManager::initializeAnalysisImpl(Pass *P) {
00905   AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
00906 
00907   for (AnalysisUsage::VectorType::const_iterator
00908          I = AnUsage->getRequiredSet().begin(),
00909          E = AnUsage->getRequiredSet().end(); I != E; ++I) {
00910     Pass *Impl = findAnalysisPass(*I, true);
00911     if (Impl == 0)
00912       // This may be analysis pass that is initialized on the fly.
00913       // If that is not the case then it will raise an assert when it is used.
00914       continue;
00915     AnalysisResolver *AR = P->getResolver();
00916     assert (AR && "Analysis Resolver is not set");
00917     AR->addAnalysisImplsPair(*I, Impl);
00918   }
00919 }
00920 
00921 /// Find the pass that implements Analysis AID. If desired pass is not found
00922 /// then return NULL.
00923 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
00924 
00925   // Check if AvailableAnalysis map has one entry.
00926   std::map<AnalysisID, Pass*>::const_iterator I =  AvailableAnalysis.find(AID);
00927 
00928   if (I != AvailableAnalysis.end())
00929     return I->second;
00930 
00931   // Search Parents through TopLevelManager
00932   if (SearchParent)
00933     return TPM->findAnalysisPass(AID);
00934   
00935   return NULL;
00936 }
00937 
00938 // Print list of passes that are last used by P.
00939 void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
00940 
00941   SmallVector<Pass *, 12> LUses;
00942 
00943   // If this is a on the fly manager then it does not have TPM.
00944   if (!TPM)
00945     return;
00946 
00947   TPM->collectLastUses(LUses, P);
00948   
00949   for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
00950          E = LUses.end(); I != E; ++I) {
00951     llvm::cerr << "--" << std::string(Offset*2, ' ');
00952     (*I)->dumpPassStructure(0);
00953   }
00954 }
00955 
00956 void PMDataManager::dumpPassArguments() const {
00957   for(SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
00958         E = PassVector.end(); I != E; ++I) {
00959     if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
00960       PMD->dumpPassArguments();
00961     else
00962       if (const PassInfo *PI = (*I)->getPassInfo())
00963         if (!PI->isAnalysisGroup())
00964           cerr << " -" << PI->getPassArgument();
00965   }
00966 }
00967 
00968 void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
00969                                  enum PassDebuggingString S2,
00970                                  const char *Msg) {
00971   if (PassDebugging < Executions)
00972     return;
00973   cerr << (void*)this << std::string(getDepth()*2+1, ' ');
00974   switch (S1) {
00975   case EXECUTION_MSG:
00976     cerr << "Executing Pass '" << P->getPassName();
00977     break;
00978   case MODIFICATION_MSG:
00979     cerr << "Made Modification '" << P->getPassName();
00980     break;
00981   case FREEING_MSG:
00982     cerr << " Freeing Pass '" << P->getPassName();
00983     break;
00984   default:
00985     break;
00986   }
00987   switch (S2) {
00988   case ON_BASICBLOCK_MSG:
00989     cerr << "' on BasicBlock '" << Msg << "'...\n";
00990     break;
00991   case ON_FUNCTION_MSG:
00992     cerr << "' on Function '" << Msg << "'...\n";
00993     break;
00994   case ON_MODULE_MSG:
00995     cerr << "' on Module '"  << Msg << "'...\n";
00996     break;
00997   case ON_LOOP_MSG:
00998     cerr << "' on Loop " << Msg << "'...\n";
00999     break;
01000   case ON_CG_MSG:
01001     cerr << "' on Call Graph " << Msg << "'...\n";
01002     break;
01003   default:
01004     break;
01005   }
01006 }
01007 
01008 void PMDataManager::dumpRequiredSet(const Pass *P)
01009   const {
01010   if (PassDebugging < Details)
01011     return;
01012     
01013   AnalysisUsage analysisUsage;
01014   P->getAnalysisUsage(analysisUsage);
01015   dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
01016 }
01017 
01018 void PMDataManager::dumpPreservedSet(const Pass *P)
01019   const {
01020   if (PassDebugging < Details)
01021     return;
01022     
01023   AnalysisUsage analysisUsage;
01024   P->getAnalysisUsage(analysisUsage);
01025   dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
01026 }
01027 
01028 void PMDataManager::dumpAnalysisUsage(const char *Msg, const Pass *P,
01029                                         const AnalysisUsage::VectorType &Set)
01030   const {
01031   assert(PassDebugging >= Details);
01032   if (Set.empty())
01033     return;
01034   cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
01035     for (unsigned i = 0; i != Set.size(); ++i) {
01036       if (i) cerr << ",";
01037       cerr << " " << Set[i]->getPassName();
01038     }
01039     cerr << "\n";
01040 }
01041 
01042 /// Add RequiredPass into list of lower level passes required by pass P.
01043 /// RequiredPass is run on the fly by Pass Manager when P requests it
01044 /// through getAnalysis interface.
01045 /// This should be handled by specific pass manager.
01046 void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
01047   if (TPM) {
01048     TPM->dumpArguments();
01049     TPM->dumpPasses();
01050   }
01051 
01052   // Module Level pass may required Function Level analysis info 
01053   // (e.g. dominator info). Pass manager uses on the fly function pass manager 
01054   // to provide this on demand. In that case, in Pass manager terminology, 
01055   // module level pass is requiring lower level analysis info managed by
01056   // lower level pass manager.
01057 
01058   // When Pass manager is not able to order required analysis info, Pass manager
01059   // checks whether any lower level manager will be able to provide this 
01060   // analysis info on demand or not.
01061 #ifndef NDEBUG
01062   cerr << "Unable to schedule '" << RequiredPass->getPassName();
01063   cerr << "' required by '" << P->getPassName() << "'\n";
01064 #endif
01065   assert (0 && "Unable to schedule pass");
01066 }
01067 
01068 // Destructor
01069 PMDataManager::~PMDataManager() {
01070   
01071   for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
01072          E = PassVector.end(); I != E; ++I)
01073     delete *I;
01074   
01075 }
01076 
01077 //===----------------------------------------------------------------------===//
01078 // NOTE: Is this the right place to define this method ?
01079 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
01080 Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
01081   return PM.findAnalysisPass(ID, dir);
01082 }
01083 
01084 Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI, 
01085                                      Function &F) {
01086   return PM.getOnTheFlyPass(P, AnalysisPI, F);
01087 }
01088 
01089 //===----------------------------------------------------------------------===//
01090 // BBPassManager implementation
01091 
01092 /// Execute all of the passes scheduled for execution by invoking 
01093 /// runOnBasicBlock method.  Keep track of whether any of the passes modifies 
01094 /// the function, and if so, return true.
01095 bool
01096 BBPassManager::runOnFunction(Function &F) {
01097 
01098   if (F.isDeclaration())
01099     return false;
01100 
01101   bool Changed = doInitialization(F);
01102 
01103   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
01104     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
01105       BasicBlockPass *BP = getContainedPass(Index);
01106 
01107       dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
01108       dumpRequiredSet(BP);
01109 
01110       initializeAnalysisImpl(BP);
01111 
01112       if (TheTimeInfo) TheTimeInfo->passStarted(BP);
01113       Changed |= BP->runOnBasicBlock(*I);
01114       if (TheTimeInfo) TheTimeInfo->passEnded(BP);
01115 
01116       if (Changed) 
01117         dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
01118                      I->getNameStart());
01119       dumpPreservedSet(BP);
01120 
01121       verifyPreservedAnalysis(BP);
01122       removeNotPreservedAnalysis(BP);
01123       recordAvailableAnalysis(BP);
01124       removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
01125     }
01126 
01127   return Changed |= doFinalization(F);
01128 }
01129 
01130 // Implement doInitialization and doFinalization
01131 inline bool BBPassManager::doInitialization(Module &M) {
01132   bool Changed = false;
01133 
01134   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
01135     BasicBlockPass *BP = getContainedPass(Index);
01136     Changed |= BP->doInitialization(M);
01137   }
01138 
01139   return Changed;
01140 }
01141 
01142 inline bool BBPassManager::doFinalization(Module &M) {
01143   bool Changed = false;
01144 
01145   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
01146     BasicBlockPass *BP = getContainedPass(Index);
01147     Changed |= BP->doFinalization(M);
01148   }
01149 
01150   return Changed;
01151 }
01152 
01153 inline bool BBPassManager::doInitialization(Function &F) {
01154   bool Changed = false;
01155 
01156   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
01157     BasicBlockPass *BP = getContainedPass(Index);
01158     Changed |= BP->doInitialization(F);
01159   }
01160 
01161   return Changed;
01162 }
01163 
01164 inline bool BBPassManager::doFinalization(Function &F) {
01165   bool Changed = false;
01166 
01167   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
01168     BasicBlockPass *BP = getContainedPass(Index);
01169     Changed |= BP->doFinalization(F);
01170   }
01171 
01172   return Changed;
01173 }
01174 
01175 
01176 //===----------------------------------------------------------------------===//
01177 // FunctionPassManager implementation
01178 
01179 /// Create new Function pass manager
01180 FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
01181   FPM = new FunctionPassManagerImpl(0);
01182   // FPM is the top level manager.
01183   FPM->setTopLevelManager(FPM);
01184 
01185   AnalysisResolver *AR = new AnalysisResolver(*FPM);
01186   FPM->setResolver(AR);
01187   
01188   MP = P;
01189 }
01190 
01191 FunctionPassManager::~FunctionPassManager() {
01192   delete FPM;
01193 }
01194 
01195 /// add - Add a pass to the queue of passes to run.  This passes
01196 /// ownership of the Pass to the PassManager.  When the
01197 /// PassManager_X is destroyed, the pass will be destroyed as well, so
01198 /// there is no need to delete the pass. (TODO delete passes.)
01199 /// This implies that all passes MUST be allocated with 'new'.
01200 void FunctionPassManager::add(Pass *P) { 
01201   FPM->add(P);
01202 }
01203 
01204 /// run - Execute all of the passes scheduled for execution.  Keep
01205 /// track of whether any of the passes modifies the function, and if
01206 /// so, return true.
01207 ///
01208 bool FunctionPassManager::run(Function &F) {
01209   std::string errstr;
01210   if (MP->materializeFunction(&F, &errstr)) {
01211     cerr << "Error reading bitcode file: " << errstr << "\n";
01212     abort();
01213   }
01214   return FPM->run(F);
01215 }
01216 
01217 
01218 /// doInitialization - Run all of the initializers for the function passes.
01219 ///
01220 bool FunctionPassManager::doInitialization() {
01221   return FPM->doInitialization(*MP->getModule());
01222 }
01223 
01224 /// doFinalization - Run all of the finalizers for the function passes.
01225 ///
01226 bool FunctionPassManager::doFinalization() {
01227   return FPM->doFinalization(*MP->getModule());
01228 }
01229 
01230 //===----------------------------------------------------------------------===//
01231 // FunctionPassManagerImpl implementation
01232 //
01233 inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
01234   bool Changed = false;
01235 
01236   for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {  
01237     FPPassManager *FP = getContainedManager(Index);
01238     Changed |= FP->doInitialization(M);
01239   }
01240 
01241   return Changed;
01242 }
01243 
01244 inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
01245   bool Changed = false;
01246 
01247   for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {  
01248     FPPassManager *FP = getContainedManager(Index);
01249     Changed |= FP->doFinalization(M);
01250   }
01251 
01252   return Changed;
01253 }
01254 
01255 // Execute all the passes managed by this top level manager.
01256 // Return true if any function is modified by a pass.
01257 bool FunctionPassManagerImpl::run(Function &F) {
01258 
01259   bool Changed = false;
01260 
01261   TimingInfo::createTheTimeInfo();
01262 
01263   dumpArguments();
01264   dumpPasses();
01265 
01266   initializeAllAnalysisInfo();
01267   for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {  
01268     FPPassManager *FP = getContainedManager(Index);
01269     Changed |= FP->runOnFunction(F);
01270   }
01271   return Changed;
01272 }
01273 
01274 //===----------------------------------------------------------------------===//
01275 // FPPassManager implementation
01276 
01277 char FPPassManager::ID = 0;
01278 /// Print passes managed by this manager
01279 void FPPassManager::dumpPassStructure(unsigned Offset) {
01280   llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
01281   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
01282     FunctionPass *FP = getContainedPass(Index);
01283     FP->dumpPassStructure(Offset + 1);
01284     dumpLastUses(FP, Offset+1);
01285   }
01286 }
01287 
01288 
01289 /// Execute all of the passes scheduled for execution by invoking 
01290 /// runOnFunction method.  Keep track of whether any of the passes modifies 
01291 /// the function, and if so, return true.
01292 bool FPPassManager::runOnFunction(Function &F) {
01293 
01294   bool Changed = false;
01295 
01296   if (F.isDeclaration())
01297     return false;
01298   
01299   // Collect inherited analysis from Module level pass manager.
01300   populateInheritedAnalysis(TPM->activeStack);
01301 
01302   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
01303     FunctionPass *FP = getContainedPass(Index);
01304 
01305     dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
01306     dumpRequiredSet(FP);
01307 
01308     initializeAnalysisImpl(FP);
01309 
01310     if (TheTimeInfo) TheTimeInfo->passStarted(FP);
01311     Changed |= FP->runOnFunction(F);
01312     if (TheTimeInfo) TheTimeInfo->passEnded(FP);
01313 
01314     if (Changed) 
01315       dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
01316     dumpPreservedSet(FP);
01317 
01318     verifyPreservedAnalysis(FP);
01319     removeNotPreservedAnalysis(FP);
01320     recordAvailableAnalysis(FP);
01321     removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
01322 
01323     // If dominator information is available then verify the info if requested.
01324     verifyDomInfo(*FP, F);
01325   }
01326   return Changed;
01327 }
01328 
01329 bool FPPassManager::runOnModule(Module &M) {
01330 
01331   bool Changed = doInitialization(M);
01332 
01333   for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
01334     this->runOnFunction(*I);
01335 
01336   return Changed |= doFinalization(M);
01337 }
01338 
01339 inline bool FPPassManager::doInitialization(Module &M) {
01340   bool Changed = false;
01341 
01342   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
01343     FunctionPass *FP = getContainedPass(Index);
01344     Changed |= FP->doInitialization(M);
01345   }
01346 
01347   return Changed;
01348 }
01349 
01350 inline bool FPPassManager::doFinalization(Module &M) {
01351   bool Changed = false;
01352 
01353   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
01354     FunctionPass *FP = getContainedPass(Index);
01355     Changed |= FP->doFinalization(M);
01356   }
01357 
01358   return Changed;
01359 }
01360 
01361 //===----------------------------------------------------------------------===//
01362 // MPPassManager implementation
01363 
01364 /// Execute all of the passes scheduled for execution by invoking 
01365 /// runOnModule method.  Keep track of whether any of the passes modifies 
01366 /// the module, and if so, return true.
01367 bool
01368 MPPassManager::runOnModule(Module &M) {
01369   bool Changed = false;
01370 
01371   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
01372     ModulePass *MP = getContainedPass(Index);
01373 
01374     dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
01375                  M.getModuleId