PassAnalysisSupport.h revision cb2610ea037a17115ef3a01a6bdaab4e3cfdca27
1//===- llvm/PassAnalysisSupport.h - Analysis Pass Support code ---*- C++ -*-==// 2// 3// This file defines stuff that is used to define and "use" Analysis Passes. 4// This file is automatically #included by Pass.h, so: 5// 6// NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY 7// 8// Instead, #include Pass.h 9// 10//===----------------------------------------------------------------------===// 11 12#ifndef LLVM_PASS_ANALYSIS_SUPPORT_H 13#define LLVM_PASS_ANALYSIS_SUPPORT_H 14 15// No need to include Pass.h, we are being included by it! 16 17 18 19//===----------------------------------------------------------------------===// 20// AnalysisUsage - Represent the analysis usage information of a pass. This 21// tracks analyses that the pass REQUIRES (must available when the pass runs), 22// and analyses that the pass PRESERVES (the pass does not invalidate the 23// results of these analyses). This information is provided by a pass to the 24// Pass infrastructure through the getAnalysisUsage virtual function. 25// 26class AnalysisUsage { 27 // Sets of analyses required and preserved by a pass 28 std::vector<AnalysisID> Required, Preserved; 29 bool PreservesAll; 30public: 31 AnalysisUsage() : PreservesAll(false) {} 32 33 // addRequired - Add the specified ID to the required set of the usage info 34 // for a pass. 35 // 36 AnalysisUsage &addRequiredID(AnalysisID ID) { 37 Required.push_back(ID); 38 return *this; 39 } 40 template<class PassClass> 41 AnalysisUsage &addRequired() { 42 assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!"); 43 Required.push_back(Pass::getClassPassInfo<PassClass>()); 44 return *this; 45 } 46 47 // addPreserved - Add the specified ID to the set of analyses preserved by 48 // this pass 49 // 50 AnalysisUsage &addPreservedID(AnalysisID ID) { 51 Preserved.push_back(ID); 52 return *this; 53 } 54 55 template<class PassClass> 56 AnalysisUsage &addPreserved() { 57 assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!"); 58 Preserved.push_back(Pass::getClassPassInfo<PassClass>()); 59 return *this; 60 } 61 62 // setPreservesAll - Set by analyses that do not transform their input at all 63 void setPreservesAll() { PreservesAll = true; } 64 bool getPreservesAll() const { return PreservesAll; } 65 66 /// setPreservesCFG - This function should be called by the pass, iff they do 67 /// not: 68 /// 69 /// 1. Add or remove basic blocks from the function 70 /// 2. Modify terminator instructions in any way. 71 /// 72 /// This function annotates the AnalysisUsage info object to say that analyses 73 /// that only depend on the CFG are preserved by this pass. 74 /// 75 void setPreservesCFG(); 76 77 const std::vector<AnalysisID> &getRequiredSet() const { return Required; } 78 const std::vector<AnalysisID> &getPreservedSet() const { return Preserved; } 79}; 80 81 82 83//===----------------------------------------------------------------------===// 84// AnalysisResolver - Simple interface implemented by PassManager objects that 85// is used to pull analysis information out of them. 86// 87struct AnalysisResolver { 88 virtual Pass *getAnalysisOrNullUp(AnalysisID ID) const = 0; 89 virtual Pass *getAnalysisOrNullDown(AnalysisID ID) const = 0; 90 virtual void addPass(ImmutablePass *IP, AnalysisUsage &AU) = 0; 91 Pass *getAnalysis(AnalysisID ID) const { 92 Pass *Result = getAnalysisOrNullUp(ID); 93 assert(Result && "Pass has an incorrect analysis uses set!"); 94 return Result; 95 } 96 97 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist 98 Pass *getAnalysisToUpdate(AnalysisID ID) const { 99 return getAnalysisOrNullUp(ID); 100 } 101 102 // Methods for introspecting into pass manager objects... 103 virtual unsigned getDepth() const = 0; 104 virtual unsigned getNumContainedPasses() const = 0; 105 virtual const Pass *getContainedPass(unsigned N) const = 0; 106 107 virtual void markPassUsed(AnalysisID P, Pass *User) = 0; 108 109 void startPass(Pass *P) {} 110 void endPass(Pass *P) {} 111protected: 112 void setAnalysisResolver(Pass *P, AnalysisResolver *AR); 113}; 114 115#endif 116