LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source 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 namespace llvm {
00023 
00024 // No need to include Pass.h, we are being included by it!
00025 
00026 //===----------------------------------------------------------------------===//
00027 // AnalysisUsage - Represent the analysis usage information of a pass.  This
00028 // tracks analyses that the pass REQUIRES (must be available when the pass 
00029 // runs), REQUIRES TRANSITIVE (must be available throughout the lifetime of the
00030 // pass), and analyses that the pass PRESERVES (the pass does not invalidate the
00031 // results of these analyses).  This information is provided by a pass to the 
00032 // Pass infrastructure through the getAnalysisUsage virtual function.
00033 //
00034 class AnalysisUsage {
00035   // Sets of analyses required and preserved by a pass
00036   std::vector<AnalysisID> Required, RequiredTransitive, Preserved;
00037   bool PreservesAll;
00038 public:
00039   AnalysisUsage() : PreservesAll(false) {}
00040   
00041   // addRequired - Add the specified ID to the required set of the usage info
00042   // for a pass.
00043   //
00044   AnalysisUsage &addRequiredID(AnalysisID ID) {
00045     Required.push_back(ID);
00046     return *this;
00047   }
00048   template<class PassClass>
00049   AnalysisUsage &addRequired() {
00050     assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
00051     Required.push_back(Pass::getClassPassInfo<PassClass>());
00052     return *this;
00053   }
00054 
00055   template<class PassClass>
00056   AnalysisUsage &addRequiredTransitive() {
00057     AnalysisID ID = Pass::getClassPassInfo<PassClass>();
00058     assert(ID && "Pass class not registered!");
00059     Required.push_back(ID);
00060     RequiredTransitive.push_back(ID);
00061     return *this;
00062   }
00063 
00064   // addPreserved - Add the specified ID to the set of analyses preserved by
00065   // this pass
00066   //
00067   AnalysisUsage &addPreservedID(AnalysisID ID) {
00068     Preserved.push_back(ID);
00069     return *this;
00070   }
00071 
00072   template<class PassClass>
00073   AnalysisUsage &addPreserved() {
00074     assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
00075     Preserved.push_back(Pass::getClassPassInfo<PassClass>());
00076     return *this;
00077   }
00078 
00079   // setPreservesAll - Set by analyses that do not transform their input at all
00080   void setPreservesAll() { PreservesAll = true; }
00081   bool getPreservesAll() const { return PreservesAll; }
00082 
00083   /// setPreservesCFG - This function should be called by the pass, iff they do
00084   /// not:
00085   ///
00086   ///  1. Add or remove basic blocks from the function
00087   ///  2. Modify terminator instructions in any way.
00088   ///
00089   /// This function annotates the AnalysisUsage info object to say that analyses
00090   /// that only depend on the CFG are preserved by this pass.
00091   ///
00092   void setPreservesCFG();
00093 
00094   const std::vector<AnalysisID> &getRequiredSet() const { return Required; }
00095   const std::vector<AnalysisID> &getRequiredTransitiveSet() const {
00096     return RequiredTransitive;
00097   }
00098   const std::vector<AnalysisID> &getPreservedSet() const { return Preserved; }
00099 };
00100 
00101 
00102 
00103 //===----------------------------------------------------------------------===//
00104 // AnalysisResolver - Simple interface implemented by PassManager objects that
00105 // is used to pull analysis information out of them.
00106 //
00107 struct AnalysisResolver {
00108   virtual Pass *getAnalysisOrNullUp(AnalysisID ID) const = 0;
00109   virtual Pass *getAnalysisOrNullDown(AnalysisID ID) const = 0;
00110   virtual void addPass(ImmutablePass *IP, AnalysisUsage &AU) = 0;
00111   Pass *getAnalysis(AnalysisID ID) const {
00112     Pass *Result = getAnalysisOrNullUp(ID);
00113     assert(Result && "Pass has an incorrect analysis uses set!");
00114     return Result;
00115   }
00116 
00117   // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
00118   Pass *getAnalysisToUpdate(AnalysisID ID) const {
00119     return getAnalysisOrNullUp(ID);
00120   }
00121 
00122   // Methods for introspecting into pass manager objects...
00123   virtual unsigned getDepth() const = 0;
00124   virtual unsigned getNumContainedPasses() const = 0;
00125   virtual const Pass *getContainedPass(unsigned N) const = 0;
00126 
00127   virtual void markPassUsed(AnalysisID P, Pass *User) = 0;
00128 
00129   void startPass(Pass *P) {}
00130   void endPass(Pass *P) {}
00131 protected:
00132   void setAnalysisResolver(Pass *P, AnalysisResolver *AR);
00133 };
00134 
00135 /// getAnalysisToUpdate<AnalysisType>() - This function is used by subclasses
00136 /// to get to the analysis information that might be around that needs to be
00137 /// updated.  This is different than getAnalysis in that it can fail (ie the
00138 /// analysis results haven't been computed), so should only be used if you
00139 /// provide the capability to update an analysis that exists.  This method is
00140 /// often used by transformation APIs to update analysis results for a pass
00141 /// automatically as the transform is performed.
00142 ///
00143 template<typename AnalysisType>
00144 AnalysisType *Pass::getAnalysisToUpdate() const {
00145   assert(Resolver && "Pass not resident in a PassManager object!");
00146   const PassInfo *PI = getClassPassInfo<AnalysisType>();
00147   if (PI == 0) return 0;
00148   return dynamic_cast<AnalysisType*>(Resolver->getAnalysisToUpdate(PI));
00149 }
00150 
00151 } // End llvm namespace
00152 
00153 #endif