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