PassAnalysisSupport.h revision 410354fe0c052141dadeca939395743f8dd58e38
1//===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source 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 Required.push_back(ID); 48 return *this; 49 } 50 template<class PassClass> 51 AnalysisUsage &addRequired() { 52 assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!"); 53 Required.push_back(Pass::getClassPassInfo<PassClass>()); 54 return *this; 55 } 56 57 template<class PassClass> 58 AnalysisUsage &addRequiredTransitive() { 59 AnalysisID ID = Pass::getClassPassInfo<PassClass>(); 60 assert(ID && "Pass class not registered!"); 61 Required.push_back(ID); 62 RequiredTransitive.push_back(ID); 63 return *this; 64 } 65 66 // addPreserved - Add the specified ID to the set of analyses preserved by 67 // this pass 68 // 69 AnalysisUsage &addPreservedID(AnalysisID ID) { 70 Preserved.push_back(ID); 71 return *this; 72 } 73 74 template<class PassClass> 75 AnalysisUsage &addPreserved() { 76 assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!"); 77 Preserved.push_back(Pass::getClassPassInfo<PassClass>()); 78 return *this; 79 } 80 81 // setPreservesAll - Set by analyses that do not transform their input at all 82 void setPreservesAll() { PreservesAll = true; } 83 bool getPreservesAll() const { return PreservesAll; } 84 85 /// setPreservesCFG - This function should be called by the pass, iff they do 86 /// not: 87 /// 88 /// 1. Add or remove basic blocks from the function 89 /// 2. Modify terminator instructions in any way. 90 /// 91 /// This function annotates the AnalysisUsage info object to say that analyses 92 /// that only depend on the CFG are preserved by this pass. 93 /// 94 void setPreservesCFG(); 95 96 const std::vector<AnalysisID> &getRequiredSet() const { return Required; } 97 const std::vector<AnalysisID> &getRequiredTransitiveSet() const { 98 return RequiredTransitive; 99 } 100 const std::vector<AnalysisID> &getPreservedSet() const { return Preserved; } 101}; 102 103 104 105//===----------------------------------------------------------------------===// 106// AnalysisResolver - Simple interface implemented by PassManager objects that 107// is used to pull analysis information out of them. 108// 109struct AnalysisResolver { 110 virtual ~AnalysisResolver(); 111 virtual Pass *getAnalysisOrNullUp(AnalysisID ID) const = 0; 112 virtual Pass *getAnalysisOrNullDown(AnalysisID ID) const = 0; 113 virtual void addPass(ImmutablePass *IP, AnalysisUsage &AU) = 0; 114 Pass *getAnalysis(AnalysisID ID) const { 115 Pass *Result = getAnalysisOrNullUp(ID); 116 assert(Result && "Pass has an incorrect analysis uses set!"); 117 return Result; 118 } 119 120 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist 121 Pass *getAnalysisToUpdate(AnalysisID ID) const { 122 return getAnalysisOrNullUp(ID); 123 } 124 125 // Methods for introspecting into pass manager objects... 126 virtual unsigned getDepth() const = 0; 127 virtual unsigned getNumContainedPasses() const = 0; 128 virtual const Pass *getContainedPass(unsigned N) const = 0; 129 130 virtual void markPassUsed(AnalysisID P, Pass *User) = 0; 131 132 void startPass(Pass *P) {} 133 void endPass(Pass *P) {} 134protected: 135 void setAnalysisResolver(Pass *P, AnalysisResolver *AR); 136}; 137 138/// getAnalysisToUpdate<AnalysisType>() - This function is used by subclasses 139/// to get to the analysis information that might be around that needs to be 140/// updated. This is different than getAnalysis in that it can fail (ie the 141/// analysis results haven't been computed), so should only be used if you 142/// provide the capability to update an analysis that exists. This method is 143/// often used by transformation APIs to update analysis results for a pass 144/// automatically as the transform is performed. 145/// 146template<typename AnalysisType> 147AnalysisType *Pass::getAnalysisToUpdate() const { 148 assert(Resolver && "Pass not resident in a PassManager object!"); 149 const PassInfo *PI = getClassPassInfo<AnalysisType>(); 150 if (PI == 0) return 0; 151 return dynamic_cast<AnalysisType*>(Resolver->getAnalysisToUpdate(PI)); 152} 153 154} // End llvm namespace 155 156#endif 157