PassAnalysisSupport.h revision 7ed47a13356daed2a34cd2209a31f92552e3bdd8
1//===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines stuff that is used to define and "use" Analysis Passes. 11// This file is automatically #included by Pass.h, so: 12// 13// NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY 14// 15// Instead, #include Pass.h 16// 17//===----------------------------------------------------------------------===// 18 19#ifndef LLVM_PASS_ANALYSIS_SUPPORT_H 20#define LLVM_PASS_ANALYSIS_SUPPORT_H 21 22#include <vector> 23 24namespace llvm { 25 26// No need to include Pass.h, we are being included by it! 27 28//===----------------------------------------------------------------------===// 29// AnalysisUsage - Represent the analysis usage information of a pass. This 30// tracks analyses that the pass REQUIRES (must be available when the pass 31// runs), REQUIRES TRANSITIVE (must be available throughout the lifetime of the 32// pass), and analyses that the pass PRESERVES (the pass does not invalidate the 33// results of these analyses). This information is provided by a pass to the 34// Pass infrastructure through the getAnalysisUsage virtual function. 35// 36class AnalysisUsage { 37 // Sets of analyses required and preserved by a pass 38 std::vector<AnalysisID> Required, RequiredTransitive, Preserved; 39 bool PreservesAll; 40public: 41 AnalysisUsage() : PreservesAll(false) {} 42 43 // addRequired - Add the specified ID to the required set of the usage info 44 // for a pass. 45 // 46 AnalysisUsage &addRequiredID(AnalysisID ID) { 47 assert(ID && "Pass class not registered!"); 48 Required.push_back(ID); 49 return *this; 50 } 51 template<class PassClass> 52 AnalysisUsage &addRequired() { 53 return addRequiredID(Pass::getClassPassInfo<PassClass>()); 54 } 55 56 AnalysisUsage &addRequiredTransitiveID(AnalysisID ID) { 57 assert(ID && "Pass class not registered!"); 58 Required.push_back(ID); 59 RequiredTransitive.push_back(ID); 60 return *this; 61 } 62 template<class PassClass> 63 AnalysisUsage &addRequiredTransitive() { 64 AnalysisID ID = Pass::getClassPassInfo<PassClass>(); 65 return addRequiredTransitiveID(ID); 66 } 67 68 // addPreserved - Add the specified ID to the set of analyses preserved by 69 // this pass 70 // 71 AnalysisUsage &addPreservedID(AnalysisID ID) { 72 Preserved.push_back(ID); 73 return *this; 74 } 75 76 template<class PassClass> 77 AnalysisUsage &addPreserved() { 78 assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!"); 79 Preserved.push_back(Pass::getClassPassInfo<PassClass>()); 80 return *this; 81 } 82 83 // setPreservesAll - Set by analyses that do not transform their input at all 84 void setPreservesAll() { PreservesAll = true; } 85 bool getPreservesAll() const { return PreservesAll; } 86 87 /// setPreservesCFG - This function should be called by the pass, iff they do 88 /// not: 89 /// 90 /// 1. Add or remove basic blocks from the function 91 /// 2. Modify terminator instructions in any way. 92 /// 93 /// This function annotates the AnalysisUsage info object to say that analyses 94 /// that only depend on the CFG are preserved by this pass. 95 /// 96 void setPreservesCFG(); 97 98 const std::vector<AnalysisID> &getRequiredSet() const { return Required; } 99 const std::vector<AnalysisID> &getRequiredTransitiveSet() const { 100 return RequiredTransitive; 101 } 102 const std::vector<AnalysisID> &getPreservedSet() const { return Preserved; } 103}; 104 105//===----------------------------------------------------------------------===// 106// AnalysisResolver - Simple interface used by Pass objects to pull all 107// analysis information out of pass manager that is responsible to manage 108// the pass. 109// 110class PMDataManager; 111class AnalysisResolver { 112private: 113 AnalysisResolver(); // DO NOT IMPLEMENT 114 115public: 116 explicit AnalysisResolver(PMDataManager &P) : PM(P) { } 117 118 inline PMDataManager &getPMDataManager() { return PM; } 119 120 // Find pass that is implementing PI. 121 Pass *findImplPass(const PassInfo *PI) { 122 Pass *ResultPass = 0; 123 for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) { 124 if (AnalysisImpls[i].first == PI) { 125 ResultPass = AnalysisImpls[i].second; 126 break; 127 } 128 } 129 return ResultPass; 130 } 131 132 // Find pass that is implementing PI. Initialize pass for Function F. 133 Pass *findImplPass(Pass *P, const PassInfo *PI, Function &F); 134 135 void addAnalysisImplsPair(const PassInfo *PI, Pass *P) { 136 std::pair<const PassInfo*, Pass*> pir = std::make_pair(PI,P); 137 AnalysisImpls.push_back(pir); 138 } 139 140 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist 141 Pass *getAnalysisToUpdate(AnalysisID ID, bool Direction) const; 142 143 // AnalysisImpls - This keeps track of which passes implements the interfaces 144 // that are required by the current pass (to implement getAnalysis()). 145 // NOTE : Remove AnalysisImpls from class Pass, when AnalysisResolver 146 // replaces AnalysisResolver 147 std::vector<std::pair<const PassInfo*, Pass*> > AnalysisImpls; 148 149private: 150 // PassManager that is used to resolve analysis info 151 PMDataManager &PM; 152}; 153 154/// getAnalysisToUpdate<AnalysisType>() - This function is used by subclasses 155/// to get to the analysis information that might be around that needs to be 156/// updated. This is different than getAnalysis in that it can fail (ie the 157/// analysis results haven't been computed), so should only be used if you 158/// provide the capability to update an analysis that exists. This method is 159/// often used by transformation APIs to update analysis results for a pass 160/// automatically as the transform is performed. 161/// 162template<typename AnalysisType> 163AnalysisType *Pass::getAnalysisToUpdate() const { 164 assert(Resolver && "Pass not resident in a PassManager object!"); 165 166 const PassInfo *PI = getClassPassInfo<AnalysisType>(); 167 if (PI == 0) return 0; 168 return dynamic_cast<AnalysisType*> 169 (Resolver->getAnalysisToUpdate(PI, true)); 170} 171 172/// getAnalysis<AnalysisType>() - This function is used by subclasses to get 173/// to the analysis information that they claim to use by overriding the 174/// getAnalysisUsage function. 175/// 176template<typename AnalysisType> 177AnalysisType &Pass::getAnalysis() const { 178 assert(Resolver &&"Pass has not been inserted into a PassManager object!"); 179 180 return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>()); 181} 182 183template<typename AnalysisType> 184AnalysisType &Pass::getAnalysisID(const PassInfo *PI) const { 185 assert(PI && "getAnalysis for unregistered pass!"); 186 assert(Resolver&&"Pass has not been inserted into a PassManager object!"); 187 // PI *must* appear in AnalysisImpls. Because the number of passes used 188 // should be a small number, we just do a linear search over a (dense) 189 // vector. 190 Pass *ResultPass = Resolver->findImplPass(PI); 191 assert (ResultPass && 192 "getAnalysis*() called on an analysis that was not " 193 "'required' by pass!"); 194 195 // Because the AnalysisType may not be a subclass of pass (for 196 // AnalysisGroups), we must use dynamic_cast here to potentially adjust the 197 // return pointer (because the class may multiply inherit, once from pass, 198 // once from AnalysisType). 199 // 200 AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass); 201 assert(Result && "Pass does not implement interface required!"); 202 return *Result; 203} 204 205/// getAnalysis<AnalysisType>() - This function is used by subclasses to get 206/// to the analysis information that they claim to use by overriding the 207/// getAnalysisUsage function. 208/// 209template<typename AnalysisType> 210AnalysisType &Pass::getAnalysis(Function &F) { 211 assert(Resolver &&"Pass has not been inserted into a PassManager object!"); 212 213 return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>(), F); 214} 215 216template<typename AnalysisType> 217AnalysisType &Pass::getAnalysisID(const PassInfo *PI, Function &F) { 218 assert(PI && "getAnalysis for unregistered pass!"); 219 assert(Resolver&&"Pass has not been inserted into a PassManager object!"); 220 // PI *must* appear in AnalysisImpls. Because the number of passes used 221 // should be a small number, we just do a linear search over a (dense) 222 // vector. 223 Pass *ResultPass = Resolver->findImplPass(this, PI, F); 224 assert (ResultPass && 225 "getAnalysis*() called on an analysis that was not " 226 "'required' by pass!"); 227 228 // Because the AnalysisType may not be a subclass of pass (for 229 // AnalysisGroups), we must use dynamic_cast here to potentially adjust the 230 // return pointer (because the class may multiply inherit, once from pass, 231 // once from AnalysisType). 232 // 233 AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass); 234 assert(Result && "Pass does not implement interface required!"); 235 return *Result; 236} 237 238} // End llvm namespace 239 240#endif 241