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