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_PASSANALYSISSUPPORT_H
00020 #define LLVM_PASSANALYSISSUPPORT_H
00021 
00022 #include "llvm/ADT/SmallVector.h"
00023 #include "llvm/ADT/StringRef.h"
00024 #include "llvm/Pass.h"
00025 #include <vector>
00026 
00027 namespace llvm {
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(const void *ID);
00053   AnalysisUsage &addRequiredID(char &ID);
00054   template<class PassClass>
00055   AnalysisUsage &addRequired() {
00056     return addRequiredID(PassClass::ID);
00057   }
00058 
00059   AnalysisUsage &addRequiredTransitiveID(char &ID);
00060   template<class PassClass>
00061   AnalysisUsage &addRequiredTransitive() {
00062     return addRequiredTransitiveID(PassClass::ID);
00063   }
00064 
00065   // addPreserved - Add the specified ID to the set of analyses preserved by
00066   // this pass
00067   //
00068   AnalysisUsage &addPreservedID(const void *ID) {
00069     Preserved.push_back(ID);
00070     return *this;
00071   }
00072   AnalysisUsage &addPreservedID(char &ID) {
00073     Preserved.push_back(&ID);
00074     return *this;
00075   }
00076 
00077   // addPreserved - Add the specified Pass class to the set of analyses
00078   // preserved by this pass.
00079   //
00080   template<class PassClass>
00081   AnalysisUsage &addPreserved() {
00082     Preserved.push_back(&PassClass::ID);
00083     return *this;
00084   }
00085 
00086   // addPreserved - Add the Pass with the specified argument string to the set
00087   // of analyses preserved by this pass. If no such Pass exists, do nothing.
00088   // This can be useful when a pass is trivially preserved, but may not be
00089   // linked in. Be careful about spelling!
00090   //
00091   AnalysisUsage &addPreserved(StringRef Arg);
00092 
00093   // setPreservesAll - Set by analyses that do not transform their input at all
00094   void setPreservesAll() { PreservesAll = true; }
00095   bool getPreservesAll() const { return PreservesAll; }
00096 
00097   /// setPreservesCFG - This function should be called by the pass, iff they do
00098   /// not:
00099   ///
00100   ///  1. Add or remove basic blocks from the function
00101   ///  2. Modify terminator instructions in any way.
00102   ///
00103   /// This function annotates the AnalysisUsage info object to say that analyses
00104   /// that only depend on the CFG are preserved by this pass.
00105   ///
00106   void setPreservesCFG();
00107 
00108   const VectorType &getRequiredSet() const { return Required; }
00109   const VectorType &getRequiredTransitiveSet() const {
00110     return RequiredTransitive;
00111   }
00112   const VectorType &getPreservedSet() const { return Preserved; }
00113 };
00114 
00115 //===----------------------------------------------------------------------===//
00116 // AnalysisResolver - Simple interface used by Pass objects to pull all
00117 // analysis information out of pass manager that is responsible to manage
00118 // the pass.
00119 //
00120 class PMDataManager;
00121 class AnalysisResolver {
00122 private:
00123   AnalysisResolver() LLVM_DELETED_FUNCTION;
00124 
00125 public:
00126   explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
00127   
00128   inline PMDataManager &getPMDataManager() { return PM; }
00129 
00130   // Find pass that is implementing PI.
00131   Pass *findImplPass(AnalysisID PI) {
00132     Pass *ResultPass = nullptr;
00133     for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) {
00134       if (AnalysisImpls[i].first == PI) {
00135         ResultPass = AnalysisImpls[i].second;
00136         break;
00137       }
00138     }
00139     return ResultPass;
00140   }
00141 
00142   // Find pass that is implementing PI. Initialize pass for Function F.
00143   Pass *findImplPass(Pass *P, AnalysisID PI, Function &F);
00144 
00145   void addAnalysisImplsPair(AnalysisID PI, Pass *P) {
00146     if (findImplPass(PI) == P)
00147       return;
00148     std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P);
00149     AnalysisImpls.push_back(pir);
00150   }
00151 
00152   /// clearAnalysisImpls - Clear cache that is used to connect a pass to the
00153   /// the analysis (PassInfo).
00154   void clearAnalysisImpls() {
00155     AnalysisImpls.clear();
00156   }
00157 
00158   // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist
00159   Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
00160 
00161 private:
00162   // AnalysisImpls - This keeps track of which passes implements the interfaces
00163   // that are required by the current pass (to implement getAnalysis()).
00164   std::vector<std::pair<AnalysisID, Pass*> > AnalysisImpls;
00165 
00166   // PassManager that is used to resolve analysis info
00167   PMDataManager &PM;
00168 };
00169 
00170 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
00171 /// get analysis information that might be around, for example to update it.
00172 /// This is different than getAnalysis in that it can fail (if the analysis
00173 /// results haven't been computed), so should only be used if you can handle
00174 /// the case when the analysis is not available.  This method is often used by
00175 /// transformation APIs to update analysis results for a pass automatically as
00176 /// the transform is performed.
00177 ///
00178 template<typename AnalysisType>
00179 AnalysisType *Pass::getAnalysisIfAvailable() const {
00180   assert(Resolver && "Pass not resident in a PassManager object!");
00181 
00182   const void *PI = &AnalysisType::ID;
00183 
00184   Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true);
00185   if (!ResultPass) return nullptr;
00186 
00187   // Because the AnalysisType may not be a subclass of pass (for
00188   // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
00189   // adjust the return pointer (because the class may multiply inherit, once
00190   // from pass, once from AnalysisType).
00191   return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
00192 }
00193 
00194 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
00195 /// to the analysis information that they claim to use by overriding the
00196 /// getAnalysisUsage function.
00197 ///
00198 template<typename AnalysisType>
00199 AnalysisType &Pass::getAnalysis() const {
00200   assert(Resolver && "Pass has not been inserted into a PassManager object!");
00201   return getAnalysisID<AnalysisType>(&AnalysisType::ID);
00202 }
00203 
00204 template<typename AnalysisType>
00205 AnalysisType &Pass::getAnalysisID(AnalysisID PI) const {
00206   assert(PI && "getAnalysis for unregistered pass!");
00207   assert(Resolver&&"Pass has not been inserted into a PassManager object!");
00208   // PI *must* appear in AnalysisImpls.  Because the number of passes used
00209   // should be a small number, we just do a linear search over a (dense)
00210   // vector.
00211   Pass *ResultPass = Resolver->findImplPass(PI);
00212   assert (ResultPass && 
00213           "getAnalysis*() called on an analysis that was not "
00214           "'required' by pass!");
00215 
00216   // Because the AnalysisType may not be a subclass of pass (for
00217   // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
00218   // adjust the return pointer (because the class may multiply inherit, once
00219   // from pass, once from AnalysisType).
00220   return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
00221 }
00222 
00223 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
00224 /// to the analysis information that they claim to use by overriding the
00225 /// getAnalysisUsage function.
00226 ///
00227 template<typename AnalysisType>
00228 AnalysisType &Pass::getAnalysis(Function &F) {
00229   assert(Resolver &&"Pass has not been inserted into a PassManager object!");
00230 
00231   return getAnalysisID<AnalysisType>(&AnalysisType::ID, F);
00232 }
00233 
00234 template<typename AnalysisType>
00235 AnalysisType &Pass::getAnalysisID(AnalysisID PI, Function &F) {
00236   assert(PI && "getAnalysis for unregistered pass!");
00237   assert(Resolver && "Pass has not been inserted into a PassManager object!");
00238   // PI *must* appear in AnalysisImpls.  Because the number of passes used
00239   // should be a small number, we just do a linear search over a (dense)
00240   // vector.
00241   Pass *ResultPass = Resolver->findImplPass(this, PI, F);
00242   assert(ResultPass && "Unable to find requested analysis info");
00243   
00244   // Because the AnalysisType may not be a subclass of pass (for
00245   // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
00246   // adjust the return pointer (because the class may multiply inherit, once
00247   // from pass, once from AnalysisType).
00248   return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
00249 }
00250 
00251 } // End llvm namespace
00252 
00253 #endif