Turn live variable analysis into a real MethodPass.
authorChris Lattner <sabre@nondot.org>
Mon, 4 Feb 2002 20:00:08 +0000 (20:00 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 4 Feb 2002 20:00:08 +0000 (20:00 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1699 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/LiveVar/FunctionLiveVarInfo.h
include/llvm/CodeGen/FunctionLiveVarInfo.h
lib/Analysis/LiveVar/FunctionLiveVarInfo.cpp
lib/Target/SparcV9/LiveVar/FunctionLiveVarInfo.cpp

index a1470fe485cf20bdc9f6bf96bbb1b10931817237..9e44be4642a80b39a78bfd772a77d94b07755e2e 100644 (file)
 
 static const int DEBUG_LV = 0;
 
-#include "BBLiveVar.h"
+#include "llvm/Analysis/LiveVar/BBLiveVar.h"
+#include "llvm/Pass.h"
 
-class MethodLiveVarInfo {
+class MethodLiveVarInfo : public MethodPass {
 
   // Live var anal is done on this method - set by constructor
-  const Method *const Meth;   
+  const Method *Meth;   
 
   // A map betwn the BasicBlock and BBLiveVar
   BBToBBLiveVarMapType BB2BBLVMap;  
@@ -83,10 +84,6 @@ class MethodLiveVarInfo {
   // Machine Instr to LiveVarSet Map for providing LVset AFTER each inst
   MInstToLiveVarSetMapType MInst2LVSetAI; 
 
-  // True if the analyze() method has been called. This is checked when
-  // getInSet/OutSet is called to prevent calling those methods before analyze
-  bool HasAnalyzed;
-
 
   // --------- private methods -----------------------------------------
 
@@ -100,22 +97,36 @@ class MethodLiveVarInfo {
   void calcLiveVarSetsForBB(const BasicBlock *BB);
   
 
- public:
-  MethodLiveVarInfo(const Method *Meth);
-  ~MethodLiveVarInfo();
+public:
+  static AnalysisID ID;    // We are an analysis, we must have an ID
+
+  MethodLiveVarInfo(AnalysisID id = ID) : Meth(0) { assert(id == ID); }
+  ~MethodLiveVarInfo() { releaseMemory(); }
+
+  // --------- Implement the MethodPass interface ----------------------
+
+  // runOnMethod - Perform analysis, update internal data structures.
+  virtual bool runOnMethod(Method *M);
 
-  // performs a liver var analysis of a single method
-  void analyze();            
+  // releaseMemory - After LiveVariable analysis has been used, forget!
+  virtual void releaseMemory();
+
+  // getAnalysisUsageInfo - Provide self!
+  virtual void getAnalysisUsageInfo(AnalysisSet &Required,
+                                    AnalysisSet &Destroyed,
+                                    AnalysisSet &Provided) {
+    Provided.push_back(ID);
+  }
+
+  // --------- Functions to access analysis results -------------------
 
   // gets OutSet of a BB
   inline const LiveVarSet *getOutSetOfBB( const BasicBlock *BB) const { 
-    assert( HasAnalyzed && "call analyze() before calling this" );
     return BB2BBLVMap.find(BB)->second->getOutSet();
   }
 
   // gets InSet of a BB
   inline const LiveVarSet *getInSetOfBB( const BasicBlock *BB)  const { 
-    assert(HasAnalyzed && "call analyze() before calling this" );
     return BB2BBLVMap.find(BB)->second->getInSet();
   }
 
@@ -126,7 +137,6 @@ class MethodLiveVarInfo {
   // gets the Live var set AFTER an instruction
   const LiveVarSet * getLiveVarSetAfterMInst(const MachineInstr *MInst,
                                             const BasicBlock *CurBB);
-
 };
 
 #endif
index a1470fe485cf20bdc9f6bf96bbb1b10931817237..9e44be4642a80b39a78bfd772a77d94b07755e2e 100644 (file)
 
 static const int DEBUG_LV = 0;
 
-#include "BBLiveVar.h"
+#include "llvm/Analysis/LiveVar/BBLiveVar.h"
+#include "llvm/Pass.h"
 
-class MethodLiveVarInfo {
+class MethodLiveVarInfo : public MethodPass {
 
   // Live var anal is done on this method - set by constructor
-  const Method *const Meth;   
+  const Method *Meth;   
 
   // A map betwn the BasicBlock and BBLiveVar
   BBToBBLiveVarMapType BB2BBLVMap;  
@@ -83,10 +84,6 @@ class MethodLiveVarInfo {
   // Machine Instr to LiveVarSet Map for providing LVset AFTER each inst
   MInstToLiveVarSetMapType MInst2LVSetAI; 
 
-  // True if the analyze() method has been called. This is checked when
-  // getInSet/OutSet is called to prevent calling those methods before analyze
-  bool HasAnalyzed;
-
 
   // --------- private methods -----------------------------------------
 
@@ -100,22 +97,36 @@ class MethodLiveVarInfo {
   void calcLiveVarSetsForBB(const BasicBlock *BB);
   
 
- public:
-  MethodLiveVarInfo(const Method *Meth);
-  ~MethodLiveVarInfo();
+public:
+  static AnalysisID ID;    // We are an analysis, we must have an ID
+
+  MethodLiveVarInfo(AnalysisID id = ID) : Meth(0) { assert(id == ID); }
+  ~MethodLiveVarInfo() { releaseMemory(); }
+
+  // --------- Implement the MethodPass interface ----------------------
+
+  // runOnMethod - Perform analysis, update internal data structures.
+  virtual bool runOnMethod(Method *M);
 
-  // performs a liver var analysis of a single method
-  void analyze();            
+  // releaseMemory - After LiveVariable analysis has been used, forget!
+  virtual void releaseMemory();
+
+  // getAnalysisUsageInfo - Provide self!
+  virtual void getAnalysisUsageInfo(AnalysisSet &Required,
+                                    AnalysisSet &Destroyed,
+                                    AnalysisSet &Provided) {
+    Provided.push_back(ID);
+  }
+
+  // --------- Functions to access analysis results -------------------
 
   // gets OutSet of a BB
   inline const LiveVarSet *getOutSetOfBB( const BasicBlock *BB) const { 
-    assert( HasAnalyzed && "call analyze() before calling this" );
     return BB2BBLVMap.find(BB)->second->getOutSet();
   }
 
   // gets InSet of a BB
   inline const LiveVarSet *getInSetOfBB( const BasicBlock *BB)  const { 
-    assert(HasAnalyzed && "call analyze() before calling this" );
     return BB2BBLVMap.find(BB)->second->getInSet();
   }
 
@@ -126,7 +137,6 @@ class MethodLiveVarInfo {
   // gets the Live var set AFTER an instruction
   const LiveVarSet * getLiveVarSetAfterMInst(const MachineInstr *MInst,
                                             const BasicBlock *CurBB);
-
 };
 
 #endif
index 77c8c54e89c5ae17490b692b995f76856854cd8f..54980d796030c17b237dfc7fdbbf82ab9586d93d 100644 (file)
 using std::cout;
 using std::endl;
 
-//************************** Constructor/Destructor ***************************
+AnalysisID MethodLiveVarInfo::ID(AnalysisID::create<MethodLiveVarInfo>());
 
-
-MethodLiveVarInfo::MethodLiveVarInfo(const Method *const M) : Meth(M) {
-  assert(!M->isExternal() && "Cannot be a prototype declaration");
-  HasAnalyzed = false;                  // still we haven't called analyze()
-}
-
-
-
-MethodLiveVarInfo:: ~MethodLiveVarInfo()
-{
+void MethodLiveVarInfo::releaseMemory() {
   // First delete all BBLiveVar objects created in constructBBs(). A new object
   // of type  BBLiveVa is created for every BasicBlock in the method
 
@@ -36,11 +27,10 @@ MethodLiveVarInfo:: ~MethodLiveVarInfo()
   //
   BBToBBLiveVarMapType::iterator HMI = BB2BBLVMap.begin(); 
 
-  for( ; HMI != BB2BBLVMap.end() ; HMI ++ ) {  
-    if( (*HMI).first )                // delete all BBLiveVar in BB2BBLVMap
-      delete (*HMI).second;
-   }
+  for( ; HMI != BB2BBLVMap.end(); ++HMI)
+    delete HMI->second;                // delete all BBLiveVar in BB2BBLVMap
 
+  BB2BBLVMap.clear();
 
   // Then delete all objects of type LiveVarSet created in calcLiveVarSetsForBB
   // and entered into  MInst2LVSetBI and  MInst2LVSetAI (these are caches
@@ -50,16 +40,14 @@ MethodLiveVarInfo:: ~MethodLiveVarInfo()
 
   // hash map iterator for MInst2LVSetBI
   //
-  MInstToLiveVarSetMapType::iterator MI =  MInst2LVSetBI.begin(); 
+  MInstToLiveVarSetMapType::iterator MI = MInst2LVSetBI.begin(); 
 
-  for( ; MI !=  MInst2LVSetBI.end() ; MI ++ ) {  
-    if( (*MI).first )              // delete all LiveVarSets in  MInst2LVSetBI
-      delete (*MI).second;
-   }
-}
+  for( ; MI != MInst2LVSetBI.end(); ++MI)
+    delete MI->second;           // delete all LiveVarSets in  MInst2LVSetBI
 
-
-// ************************* support functions ********************************
+  MInst2LVSetBI.clear();
+  MInst2LVSetAI.clear();
+}
 
 
 //-----------------------------------------------------------------------------
@@ -154,17 +142,11 @@ bool MethodLiveVarInfo::doSingleBackwardPass()
 //-----------------------------------------------------------------------------
 // performs live var anal for a method
 //-----------------------------------------------------------------------------
-void MethodLiveVarInfo::analyze()        
-{
-  // Don't analyze the same method twice!
-  // Later, we need to add change notification here.
 
-  
-  if (HasAnalyzed)
-    return;
-    
+bool MethodLiveVarInfo::runOnMethod(Method *M) {
+  Meth = M;
 
-  if( DEBUG_LV) cout << "Analysing live variables ..." << endl;
+  if( DEBUG_LV) cout << "Analysing live variables ...\n";
 
   // create and initialize all the BBLiveVars of the CFG
   constructBBs();        
@@ -175,9 +157,8 @@ void MethodLiveVarInfo::analyze()
   } while (NeedAnotherIteration );    // repeat until we need more iterations
 
   
-  HasAnalyzed  = true;                // finished analysing
-
-  if( DEBUG_LV) cout << "Live Variable Analysis complete!" << endl;
+  if( DEBUG_LV) cout << "Live Variable Analysis complete!\n";
+  return false;
 }
 
 
index 77c8c54e89c5ae17490b692b995f76856854cd8f..54980d796030c17b237dfc7fdbbf82ab9586d93d 100644 (file)
 using std::cout;
 using std::endl;
 
-//************************** Constructor/Destructor ***************************
+AnalysisID MethodLiveVarInfo::ID(AnalysisID::create<MethodLiveVarInfo>());
 
-
-MethodLiveVarInfo::MethodLiveVarInfo(const Method *const M) : Meth(M) {
-  assert(!M->isExternal() && "Cannot be a prototype declaration");
-  HasAnalyzed = false;                  // still we haven't called analyze()
-}
-
-
-
-MethodLiveVarInfo:: ~MethodLiveVarInfo()
-{
+void MethodLiveVarInfo::releaseMemory() {
   // First delete all BBLiveVar objects created in constructBBs(). A new object
   // of type  BBLiveVa is created for every BasicBlock in the method
 
@@ -36,11 +27,10 @@ MethodLiveVarInfo:: ~MethodLiveVarInfo()
   //
   BBToBBLiveVarMapType::iterator HMI = BB2BBLVMap.begin(); 
 
-  for( ; HMI != BB2BBLVMap.end() ; HMI ++ ) {  
-    if( (*HMI).first )                // delete all BBLiveVar in BB2BBLVMap
-      delete (*HMI).second;
-   }
+  for( ; HMI != BB2BBLVMap.end(); ++HMI)
+    delete HMI->second;                // delete all BBLiveVar in BB2BBLVMap
 
+  BB2BBLVMap.clear();
 
   // Then delete all objects of type LiveVarSet created in calcLiveVarSetsForBB
   // and entered into  MInst2LVSetBI and  MInst2LVSetAI (these are caches
@@ -50,16 +40,14 @@ MethodLiveVarInfo:: ~MethodLiveVarInfo()
 
   // hash map iterator for MInst2LVSetBI
   //
-  MInstToLiveVarSetMapType::iterator MI =  MInst2LVSetBI.begin(); 
+  MInstToLiveVarSetMapType::iterator MI = MInst2LVSetBI.begin(); 
 
-  for( ; MI !=  MInst2LVSetBI.end() ; MI ++ ) {  
-    if( (*MI).first )              // delete all LiveVarSets in  MInst2LVSetBI
-      delete (*MI).second;
-   }
-}
+  for( ; MI != MInst2LVSetBI.end(); ++MI)
+    delete MI->second;           // delete all LiveVarSets in  MInst2LVSetBI
 
-
-// ************************* support functions ********************************
+  MInst2LVSetBI.clear();
+  MInst2LVSetAI.clear();
+}
 
 
 //-----------------------------------------------------------------------------
@@ -154,17 +142,11 @@ bool MethodLiveVarInfo::doSingleBackwardPass()
 //-----------------------------------------------------------------------------
 // performs live var anal for a method
 //-----------------------------------------------------------------------------
-void MethodLiveVarInfo::analyze()        
-{
-  // Don't analyze the same method twice!
-  // Later, we need to add change notification here.
 
-  
-  if (HasAnalyzed)
-    return;
-    
+bool MethodLiveVarInfo::runOnMethod(Method *M) {
+  Meth = M;
 
-  if( DEBUG_LV) cout << "Analysing live variables ..." << endl;
+  if( DEBUG_LV) cout << "Analysing live variables ...\n";
 
   // create and initialize all the BBLiveVars of the CFG
   constructBBs();        
@@ -175,9 +157,8 @@ void MethodLiveVarInfo::analyze()
   } while (NeedAnotherIteration );    // repeat until we need more iterations
 
   
-  HasAnalyzed  = true;                // finished analysing
-
-  if( DEBUG_LV) cout << "Live Variable Analysis complete!" << endl;
+  if( DEBUG_LV) cout << "Live Variable Analysis complete!\n";
+  return false;
 }