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   // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
00147   Pass *getAnalysisToUpdate(AnalysisID ID, bool Direction) const;
00148 
00149   // AnalysisImpls - This keeps track of which passes implements the interfaces
00150   // that are required by the current pass (to implement getAnalysis()).
00151   // NOTE : Remove AnalysisImpls from class Pass, when AnalysisResolver
00152   // replaces AnalysisResolver
00153   std::vector<std::pair<const PassInfo*, Pass*> > AnalysisImpls;
00154 
00155 private:
00156   // PassManager that is used to resolve analysis info
00157   PMDataManager &PM;
00158 };
00159 
00160 /// getAnalysisToUpdate<AnalysisType>() - This function is used by subclasses
00161 /// to get to the analysis information that might be around that needs to be
00162 /// updated.  This is different than getAnalysis in that it can fail (ie the
00163 /// analysis results haven't been computed), so should only be used if you
00164 /// provide the capability to update an analysis that exists.  This method is
00165 /// often used by transformation APIs to update analysis results for a pass
00166 /// automatically as the transform is performed.
00167 ///
00168 template<typename AnalysisType>
00169 AnalysisType *Pass::getAnalysisToUpdate() const {
00170   assert(Resolver && "Pass not resident in a PassManager object!");
00171 
00172   const PassInfo *PI = getClassPassInfo<AnalysisType>();
00173   if (PI == 0) return 0;
00174   return dynamic_cast<AnalysisType*>
00175     (Resolver->getAnalysisToUpdate(PI, true));
00176 }
00177 
00178 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
00179 /// to the analysis information that they claim to use by overriding the
00180 /// getAnalysisUsage function.
00181 ///
00182 template<typename AnalysisType>
00183 AnalysisType &Pass::getAnalysis() const {
00184   assert(Resolver &&"Pass has not been inserted into a PassManager object!");
00185 
00186   return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>());
00187 }
00188 
00189 template<typename AnalysisType>
00190 AnalysisType &Pass::getAnalysisID(const PassInfo *PI) const {
00191   assert(PI && "getAnalysis for unregistered pass!");
00192   assert(Resolver&&"Pass has not been inserted into a PassManager object!");
00193   // PI *must* appear in AnalysisImpls.  Because the number of passes used
00194   // should be a small number, we just do a linear search over a (dense)
00195   // vector.
00196   Pass *ResultPass = Resolver->findImplPass(PI);
00197   assert (ResultPass && 
00198           "getAnalysis*() called on an analysis that was not "
00199           "'required' by pass!");
00200 
00201   // Because the AnalysisType may not be a subclass of pass (for
00202   // AnalysisGroups), we must use dynamic_cast here to potentially adjust the
00203   // return pointer (because the class may multiply inherit, once from pass,
00204   // once from AnalysisType).
00205   //
00206   AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
00207   assert(Result && "Pass does not implement interface required!");
00208   return *Result;
00209 }
00210 
00211 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
00212 /// to the analysis information that they claim to use by overriding the
00213 /// getAnalysisUsage function.
00214 ///
00215 template<typename AnalysisType>
00216 AnalysisType &Pass::getAnalysis(Function &F) {
00217   assert(Resolver &&"Pass has not been inserted into a PassManager object!");
00218 
00219   return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>(), F);
00220 }
00221 
00222 template<typename AnalysisType>
00223 AnalysisType &Pass::getAnalysisID(const PassInfo *PI, Function &F) {
00224   assert(PI && "getAnalysis for unregistered pass!");
00225    assert(Resolver&&"Pass has not been inserted into a PassManager object!");
00226    // PI *must* appear in AnalysisImpls.  Because the number of passes used
00227    // should be a small number, we just do a linear search over a (dense)
00228    // vector.
00229    Pass *ResultPass = Resolver->findImplPass(this, PI, F);
00230    assert (ResultPass && 
00231            "getAnalysis*() called on an analysis that was not "
00232            "'required' by pass!");
00233  
00234    // Because the AnalysisType may not be a subclass of pass (for
00235    // AnalysisGroups), we must use dynamic_cast here to potentially adjust the
00236    // return pointer (because the class may multiply inherit, once from pass,
00237    // once from AnalysisType).
00238    //
00239    AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
00240    assert(Result && "Pass does not implement interface required!");
00241    return *Result;
00242 }
00243 
00244 } // End llvm namespace
00245 
00246 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.