LLVM API Documentation

PassAnalysisSupport.h

Go to the documentation of this file.
00001 //===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===//
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 defines stuff that is used to define and "use" Analysis Passes.
00011 // This file is automatically #included by Pass.h, so:
00012 //
00013 //           NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
00014 //
00015 // Instead, #include Pass.h
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_PASS_ANALYSIS_SUPPORT_H
00020 #define LLVM_PASS_ANALYSIS_SUPPORT_H
00021 
00022 #include <vector>
00023 #include "llvm/ADT/SmallVector.h"
00024 
00025 namespace llvm {
00026 
00027 // No need to include Pass.h, we are being included by it!
00028 
00029 //===----------------------------------------------------------------------===//
00030 // AnalysisUsage - Represent the analysis usage information of a pass.  This
00031 // tracks analyses that the pass REQUIRES (must be available when the pass
00032 // runs), REQUIRES TRANSITIVE (must be available throughout the lifetime of the
00033 // pass), and analyses that the pass PRESERVES (the pass does not invalidate the
00034 // results of these analyses).  This information is provided by a pass to the
00035 // Pass infrastructure through the getAnalysisUsage virtual function.
00036 //
00037 class AnalysisUsage {
00038 public:
00039   typedef SmallVector<AnalysisID, 32> VectorType;
00040 
00041 private:
00042   // Sets of analyses required and preserved by a pass
00043   VectorType Required, RequiredTransitive, Preserved;
00044   bool PreservesAll;
00045 
00046 public:
00047   AnalysisUsage() : PreservesAll(false) {}
00048 
00049   // addRequired - Add the specified ID to the required set of the usage info
00050   // for a pass.
00051   //
00052   AnalysisUsage &addRequiredID(AnalysisID ID) {
00053     assert(ID && "Pass class not registered!");
00054     Required.push_back(ID);
00055     return *this;
00056   }
00057   template<class PassClass>
00058   AnalysisUsage &addRequired() {
00059     return addRequiredID(Pass::getClassPassInfo<PassClass>());
00060   }
00061 
00062   AnalysisUsage &addRequiredTransitiveID(AnalysisID ID) {
00063     assert(ID && "Pass class not registered!");
00064     Required.push_back(ID);
00065     RequiredTransitive.push_back(ID);
00066     return *this;
00067   }
00068   template<class PassClass>
00069   AnalysisUsage &addRequiredTransitive() {
00070     AnalysisID ID = Pass::getClassPassInfo<PassClass>();
00071     return addRequiredTransitiveID(ID);
00072   }
00073 
00074   // addPreserved - Add the specified ID to the set of analyses preserved by
00075   // this pass
00076   //
00077   AnalysisUsage &addPreservedID(AnalysisID ID) {
00078     Preserved.push_back(ID);
00079     return *this;
00080   }
00081 
00082   template<class PassClass>
00083   AnalysisUsage &addPreserved() {
00084     assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
00085     Preserved.push_back(Pass::getClassPassInfo<PassClass>());
00086     return *this;
00087   }
00088 
00089   // setPreservesAll - Set by analyses that do not transform their input at all
00090   void setPreservesAll() { PreservesAll = true; }
00091   bool getPreservesAll() const { return PreservesAll; }
00092 
00093   /// setPreservesCFG - This function should be called by the pass, iff they do
00094   /// not:
00095   ///
00096   ///  1. Add or remove basic blocks from the function
00097   ///  2. Modify terminator instructions in any way.
00098   ///
00099   /// This function annotates the AnalysisUsage info object to say that analyses
00100   /// that only depend on the CFG are preserved by this pass.
00101   ///
00102   void setPreservesCFG();
00103 
00104   const VectorType &getRequiredSet() const { return Required; }
00105   const VectorType &getRequiredTransitiveSet() const {
00106     return RequiredTransitive;
00107   }
00108   const VectorType &getPreservedSet() const { return Preserved; }
00109 };
00110 
00111 //===----------------------------------------------------------------------===//
00112 // AnalysisResolver - Simple interface used by Pass objects to pull all
00113 // analysis information out of pass manager that is responsible to manage
00114 // the pass.
00115 //
00116 class PMDataManager;
00117 class AnalysisResolver {
00118 private:
00119   AnalysisResolver();  // DO NOT IMPLEMENT
00120 
00121 public:
00122   explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
00123   
00124   inline PMDataManager &getPMDataManager() { return PM; }
00125 
00126   // Find pass that is implementing PI.
00127   Pass *findImplPass(const PassInfo *PI) {
00128     Pass *ResultPass = 0;
00129     for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) {
00130       if (AnalysisImpls[i].first == PI) {
00131         ResultPass = AnalysisImpls[i].second;
00132         break;
00133       }
00134     }
00135     return ResultPass;
00136   }
00137 
00138   // Find pass that is implementing PI. Initialize pass for Function F.
00139   Pass *findImplPass(Pass *P, const PassInfo *PI, Function &F);
00140 
00141   void addAnalysisImplsPair(const PassInfo *PI, Pass *P) {
00142     std::pair<const PassInfo*, Pass*> pir = std::make_pair(PI,P);
00143     AnalysisImpls.push_back(pir);
00144   }
00145 
00146   /// clearAnalysisImpls - Clear cache that is used to connect a pass to the
00147   /// the analysis (PassInfo).
00148   void clearAnalysisImpls() {
00149     AnalysisImpls.clear();
00150   }
00151 
00152   // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist
00153   Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
00154 
00155   // AnalysisImpls - This keeps track of which passes implements the interfaces
00156   // that are required by the current pass (to implement getAnalysis()).
00157   std::vector<std::pair<const PassInfo*, Pass*> > AnalysisImpls;
00158 
00159 private:
00160   // PassManager that is used to resolve analysis info
00161   PMDataManager &PM;
00162 };
00163 
00164 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
00165 /// get analysis information that might be around, for example to update it.
00166 /// This is different than getAnalysis in that it can fail (if the analysis
00167 /// results haven't been computed), so should only be used if you can handle
00168 /// the case when the analysis is not available.  This method is often used by
00169 /// transformation APIs to update analysis results for a pass automatically as
00170 /// the transform is performed.
00171 ///
00172 template<typename AnalysisType>
00173 AnalysisType *Pass::getAnalysisIfAvailable() const {
00174   assert(Resolver && "Pass not resident in a PassManager object!");
00175 
00176   const PassInfo *PI = getClassPassInfo<AnalysisType>();
00177   if (PI == 0) return 0;
00178   return dynamic_cast<AnalysisType*>
00179     (Resolver->getAnalysisIfAvailable(PI, true));
00180 }
00181 
00182 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
00183 /// to the analysis information that they claim to use by overriding the
00184 /// getAnalysisUsage function.
00185 ///
00186 template<typename AnalysisType>
00187 AnalysisType &Pass::getAnalysis() const {
00188   assert(Resolver &&"Pass has not been inserted into a PassManager object!");
00189 
00190   return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>());
00191 }
00192 
00193 template<typename AnalysisType>
00194 AnalysisType &Pass::getAnalysisID(const PassInfo *PI) const {
00195   assert(PI && "getAnalysis for unregistered pass!");
00196   assert(Resolver&&"Pass has not been inserted into a PassManager object!");
00197   // PI *must* appear in AnalysisImpls.  Because the number of passes used
00198   // should be a small number, we just do a linear search over a (dense)
00199   // vector.
00200   Pass *ResultPass = Resolver->findImplPass(PI);
00201   assert (ResultPass && 
00202           "getAnalysis*() called on an analysis that was not "
00203           "'required' by pass!");
00204 
00205   // Because the AnalysisType may not be a subclass of pass (for
00206   // AnalysisGroups), we must use dynamic_cast here to potentially adjust the
00207   // return pointer (because the class may multiply inherit, once from pass,
00208   // once from AnalysisType).
00209   //
00210   AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
00211   assert(Result && "Pass does not implement interface required!");
00212   return *Result;
00213 }
00214 
00215 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
00216 /// to the analysis information that they claim to use by overriding the
00217 /// getAnalysisUsage function.
00218 ///
00219 template<typename AnalysisType>
00220 AnalysisType &Pass::getAnalysis(Function &F) {
00221   assert(Resolver &&"Pass has not been inserted into a PassManager object!");
00222 
00223   return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>(), F);
00224 }
00225 
00226 template<typename AnalysisType>
00227 AnalysisType &Pass::getAnalysisID(const PassInfo *PI, Function &F) {
00228   assert(PI && "getAnalysis for unregistered pass!");
00229   assert(Resolver && "Pass has not been inserted into a PassManager object!");
00230   // PI *must* appear in AnalysisImpls.  Because the number of passes used
00231   // should be a small number, we just do a linear search over a (dense)
00232   // vector.
00233   Pass *ResultPass = Resolver->findImplPass(this, PI, F);
00234   assert (ResultPass &&  "Unable to find requested analysis info");
00235   
00236   // Because the AnalysisType may not be a subclass of pass (for
00237   // AnalysisGroups), we must use dynamic_cast here to potentially adjust the
00238   // return pointer (because the class may multiply inherit, once from pass,
00239   // once from AnalysisType).
00240   //
00241   AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
00242   assert(Result && "Pass does not implement interface required!");
00243   return *Result;
00244 }
00245 
00246 } // End llvm namespace
00247 
00248 #endif