Get the order of the hext digits right!
[oota-llvm.git] / lib / VMCore / PassManager.cpp
index 02a27c252732a7de8355e1227600fc49e2cd4dda..e590a6cc2a8eb8018267e84e189020bc58a1e7d2 100644 (file)
@@ -63,7 +63,9 @@ class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
                                         public FunctionPass {
 
 public:
-  BBPassManager(int Depth) : PMDataManager(Depth) { }
+  static char ID;
+  BBPassManager(int Depth) 
+    : PMDataManager(Depth), FunctionPass((intptr_t)&ID) {}
 
   /// Execute all of the passes scheduled for execution.  Keep track of
   /// whether any of the passes modifies the function, and if so, return true.
@@ -104,6 +106,7 @@ public:
   }
 };
 
+char BBPassManager::ID = 0;
 }
 
 namespace llvm {
@@ -116,9 +119,10 @@ class FunctionPassManagerImpl : public Pass,
                                 public PMDataManager,
                                 public PMTopLevelManager {
 public:
-
-  FunctionPassManagerImpl(int Depth) : PMDataManager(Depth),
-                                       PMTopLevelManager(TLM_Function) { }
+  static char ID;
+  FunctionPassManagerImpl(int Depth) : 
+    Pass((intptr_t)&ID), PMDataManager(Depth), 
+    PMTopLevelManager(TLM_Function) { }
 
   /// 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
@@ -167,9 +171,9 @@ public:
     FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
     return FP;
   }
-
 };
 
+char FunctionPassManagerImpl::ID = 0;
 //===----------------------------------------------------------------------===//
 // MPPassManager
 //
@@ -179,8 +183,19 @@ public:
 class MPPassManager : public Pass, public PMDataManager {
  
 public:
-  MPPassManager(int Depth) : PMDataManager(Depth) { }
-  
+  static char ID;
+  MPPassManager(int Depth) : Pass((intptr_t)&ID), PMDataManager(Depth) { }
+
+  // Delete on the fly managers.
+  virtual ~MPPassManager() {
+    for (std::map<Pass *, FunctionPassManagerImpl *>::iterator 
+           I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
+         I != E; ++I) {
+      FunctionPassManagerImpl *FPP = I->second;
+      delete FPP;
+    }
+  }
+
   /// 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 runOnModule(Module &M);
@@ -195,6 +210,11 @@ public:
   /// through getAnalysis interface.
   virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
 
+  /// Return function pass corresponding to PassInfo PI, that is 
+  /// required by module pass MP. Instantiate analysis pass, by using
+  /// its runOnFunction() for function F.
+  virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
+
   virtual const char *getPassName() const {
     return "Module Pass Manager";
   }
@@ -205,6 +225,8 @@ public:
     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
       ModulePass *MP = getContainedPass(Index);
       MP->dumpPassStructure(Offset + 1);
+      if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
+        FPP->dumpPassStructure(Offset + 2);
       dumpLastUses(MP, Offset+1);
     }
   }
@@ -218,19 +240,26 @@ public:
   virtual PassManagerType getPassManagerType() const { 
     return PMT_ModulePassManager; 
   }
+
+ private:
+  /// Collection of on the fly FPPassManagers. These managers manage
+  /// function passes that are required by module passes.
+  std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
 };
 
+char MPPassManager::ID = 0;
 //===----------------------------------------------------------------------===//
 // PassManagerImpl
 //
+
 /// PassManagerImpl manages MPPassManagers
 class PassManagerImpl : public Pass,
                         public PMDataManager,
                         public PMTopLevelManager {
 
 public:
-
-  PassManagerImpl(int Depth) : PMDataManager(Depth),
+  static char ID;
+  PassManagerImpl(int Depth) : Pass((intptr_t)&ID), PMDataManager(Depth),
                                PMTopLevelManager(TLM_Pass) { }
 
   /// add - Add a pass to the queue of passes to run.  This passes ownership of
@@ -275,6 +304,7 @@ public:
 
 };
 
+char PassManagerImpl::ID = 0;
 } // End of llvm namespace
 
 namespace {
@@ -614,6 +644,11 @@ void PMDataManager::removeDeadPasses(Pass *P, std::string Msg,
                                      enum PassDebuggingString DBG_STR) {
 
   std::vector<Pass *> DeadPasses;
+
+  // If this is a on the fly manager then it does not have TPM.
+  if (!TPM)
+    return;
+
   TPM->collectLastUses(DeadPasses, P);
 
   for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
@@ -678,14 +713,6 @@ void PMDataManager::add(Pass *P,
         assert (0 && "Unable to accomodate Required Pass");
     }
 
-    // Now, take care of required analysises that are not available.
-    for (SmallVector<AnalysisID, 8>::iterator 
-           I = ReqAnalysisNotAvailable.begin(), 
-           E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
-      Pass *AnalysisPass = (*I)->createPass();
-      this->addLowerLevelRequiredPass(P, AnalysisPass);
-    }
-
     // Set P as P's last user until someone starts using P.
     // However, if P is a Pass Manager then it does not need
     // to record its last user.
@@ -699,6 +726,14 @@ void PMDataManager::add(Pass *P,
       TransferLastUses.clear();
     }
 
+    // Now, take care of required analysises that are not available.
+    for (SmallVector<AnalysisID, 8>::iterator 
+           I = ReqAnalysisNotAvailable.begin(), 
+           E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
+      Pass *AnalysisPass = (*I)->createPass();
+      this->addLowerLevelRequiredPass(P, AnalysisPass);
+    }
+
     // Take a note of analysis required and made available by this pass.
     // Remove the analysis not preserved by this pass
     removeNotPreservedAnalysis(P);
@@ -754,7 +789,9 @@ void PMDataManager::initializeAnalysisImpl(Pass *P) {
          E = AnUsage.getRequiredSet().end(); I != E; ++I) {
     Pass *Impl = findAnalysisPass(*I, true);
     if (Impl == 0)
-      assert(0 && "Analysis used but not available!");
+      // This may be analysis pass that is initialized on the fly.
+      // If that is not the case then it will raise an assert when it is used.
+      continue;
     AnalysisResolver *AR = P->getResolver();
     AR->addAnalysisImplsPair(*I, Impl);
   }
@@ -781,8 +818,11 @@ Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
 void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
 
   std::vector<Pass *> LUses;
-  
-  assert (TPM && "Top Level Manager is missing");
+
+  // If this is a on the fly manager then it does not have TPM.
+  if (!TPM)
+    return;
+
   TPM->collectLastUses(LUses, P);
   
   for (std::vector<Pass *>::iterator I = LUses.begin(),
@@ -874,6 +914,11 @@ Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
   return PM.findAnalysisPass(ID, dir);
 }
 
+Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI, 
+                                     Function &F) {
+  return PM.getOnTheFlyPass(P, AnalysisPI, F);
+}
+
 //===----------------------------------------------------------------------===//
 // BBPassManager implementation
 
@@ -1063,6 +1108,7 @@ bool FunctionPassManagerImpl::run(Function &F) {
 //===----------------------------------------------------------------------===//
 // FPPassManager implementation
 
+char FPPassManager::ID = 0;
 /// Print passes managed by this manager
 void FPPassManager::dumpPassStructure(unsigned Offset) {
   llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
@@ -1190,10 +1236,36 @@ void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
            RequiredPass->getPotentialPassManagerType())
           && "Unable to handle Pass that requires lower level Analysis pass");
 
-  assert (0 && 
-          "Unable to handle Pass that requires lower level Analysis pass");
+  FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
+  if (!FPP) {
+    FPP = new FunctionPassManagerImpl(0);
+    // FPP is the top level manager.
+    FPP->setTopLevelManager(FPP);
+
+    OnTheFlyManagers[P] = FPP;
+  }
+  FPP->add(RequiredPass);
+
+  // Register P as the last user of RequiredPass.
+  std::vector<Pass *> LU; 
+  LU.push_back(RequiredPass);
+  FPP->setLastUser(LU,  P);
 }
+
+/// Return function pass corresponding to PassInfo PI, that is 
+/// required by module pass MP. Instantiate analysis pass, by using
+/// its runOnFunction() for function F.
+Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, 
+                                     Function &F) {
+   AnalysisID AID = PI;
+  FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
+  assert (FPP && "Unable to find on the fly pass");
+  
+  FPP->run(F);
+  return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
+}
+
+
 //===----------------------------------------------------------------------===//
 // PassManagerImpl implementation
 //