1 //===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
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
26 // No need to include Pass.h, we are being included by it!
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.
37 // Sets of analyses required and preserved by a pass
38 std::vector<AnalysisID> Required, RequiredTransitive, Preserved;
41 AnalysisUsage() : PreservesAll(false) {}
43 // addRequired - Add the specified ID to the required set of the usage info
46 AnalysisUsage &addRequiredID(AnalysisID ID) {
47 Required.push_back(ID);
50 template<class PassClass>
51 AnalysisUsage &addRequired() {
52 assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
53 Required.push_back(Pass::getClassPassInfo<PassClass>());
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);
66 // addPreserved - Add the specified ID to the set of analyses preserved by
69 AnalysisUsage &addPreservedID(AnalysisID ID) {
70 Preserved.push_back(ID);
74 template<class PassClass>
75 AnalysisUsage &addPreserved() {
76 assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
77 Preserved.push_back(Pass::getClassPassInfo<PassClass>());
81 // setPreservesAll - Set by analyses that do not transform their input at all
82 void setPreservesAll() { PreservesAll = true; }
83 bool getPreservesAll() const { return PreservesAll; }
85 /// setPreservesCFG - This function should be called by the pass, iff they do
88 /// 1. Add or remove basic blocks from the function
89 /// 2. Modify terminator instructions in any way.
91 /// This function annotates the AnalysisUsage info object to say that analyses
92 /// that only depend on the CFG are preserved by this pass.
94 void setPreservesCFG();
96 const std::vector<AnalysisID> &getRequiredSet() const { return Required; }
97 const std::vector<AnalysisID> &getRequiredTransitiveSet() const {
98 return RequiredTransitive;
100 const std::vector<AnalysisID> &getPreservedSet() const { return Preserved; }
103 //===----------------------------------------------------------------------===//
104 // AnalysisResolver - Simple interface used by Pass objects to pull all
105 // analysis information out of pass manager that is responsible to manage
109 class AnalysisResolver_New {
111 AnalysisResolver_New(); // DO NOT IMPLEMENT
114 AnalysisResolver_New(PMDataManager &P) : PM(P) { }
116 inline PMDataManager &getPMDataManager() { return PM; }
118 // Find pass that is implementing PI.
119 Pass *findImplPass(const PassInfo *PI) {
120 Pass *ResultPass = 0;
121 for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) {
122 if (AnalysisImpls[i].first == PI) {
123 ResultPass = AnalysisImpls[i].second;
130 void addAnalysisImplsPair(const PassInfo *PI, Pass *P) {
131 std::pair<const PassInfo*, Pass*> pir = std::make_pair(PI,P);
132 AnalysisImpls.push_back(pir);
135 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
136 Pass *getAnalysisToUpdate(AnalysisID ID, bool Direction) const;
138 // AnalysisImpls - This keeps track of which passes implements the interfaces
139 // that are required by the current pass (to implement getAnalysis()).
140 // NOTE : Remove AnalysisImpls from class Pass, when AnalysisResolver_New
141 // replaces AnalysisResolver
142 std::vector<std::pair<const PassInfo*, Pass*> > AnalysisImpls;
145 // PassManager that is used to resolve analysis info
149 //===----------------------------------------------------------------------===//
150 // AnalysisResolver - Simple interface implemented by PassManager objects that
151 // is used to pull analysis information out of them.
153 struct AnalysisResolver {
154 virtual ~AnalysisResolver();
155 virtual Pass *getAnalysisOrNullUp(AnalysisID ID) const = 0;
156 virtual Pass *getAnalysisOrNullDown(AnalysisID ID) const = 0;
157 virtual void addPass(ImmutablePass *IP, AnalysisUsage &AU) = 0;
158 Pass *getAnalysis(AnalysisID ID) const {
159 Pass *Result = getAnalysisOrNullUp(ID);
160 assert(Result && "Pass has an incorrect analysis uses set!");
164 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
165 Pass *getAnalysisToUpdate(AnalysisID ID) const {
166 return getAnalysisOrNullUp(ID);
169 // Methods for introspecting into pass manager objects...
170 virtual unsigned getDepth() const = 0;
171 virtual unsigned getNumContainedPasses() const = 0;
172 virtual const Pass *getContainedPass(unsigned N) const = 0;
174 virtual void markPassUsed(AnalysisID P, Pass *User) = 0;
176 void startPass(Pass *P) {}
177 void endPass(Pass *P) {}
179 void setAnalysisResolver(Pass *P, AnalysisResolver *AR);
182 /// getAnalysisToUpdate<AnalysisType>() - This function is used by subclasses
183 /// to get to the analysis information that might be around that needs to be
184 /// updated. This is different than getAnalysis in that it can fail (ie the
185 /// analysis results haven't been computed), so should only be used if you
186 /// provide the capability to update an analysis that exists. This method is
187 /// often used by transformation APIs to update analysis results for a pass
188 /// automatically as the transform is performed.
190 template<typename AnalysisType>
191 AnalysisType *Pass::getAnalysisToUpdate() const {
192 assert(Resolver && "Pass not resident in a PassManager object!");
193 const PassInfo *PI = getClassPassInfo<AnalysisType>();
194 if (PI == 0) return 0;
195 return dynamic_cast<AnalysisType*>(Resolver->getAnalysisToUpdate(PI));
198 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
199 /// to the analysis information that they claim to use by overriding the
200 /// getAnalysisUsage function.
202 template<typename AnalysisType>
203 AnalysisType &Pass::getAnalysis() const {
204 assert(Resolver && "Pass has not been inserted into a PassManager object!");
205 const PassInfo *PI = getClassPassInfo<AnalysisType>();
206 return getAnalysisID<AnalysisType>(PI);
209 template<typename AnalysisType>
210 AnalysisType &Pass::getAnalysisID(const PassInfo *PI) const {
211 assert(Resolver && "Pass has not been inserted into a PassManager object!");
212 assert(PI && "getAnalysis for unregistered pass!");
214 // PI *must* appear in AnalysisImpls. Because the number of passes used
215 // should be a small number, we just do a linear search over a (dense)
217 Pass *ResultPass = 0;
218 for (unsigned i = 0; ; ++i) {
219 assert(i != AnalysisImpls.size() &&
220 "getAnalysis*() called on an analysis that was not "
221 "'required' by pass!");
222 if (AnalysisImpls[i].first == PI) {
223 ResultPass = AnalysisImpls[i].second;
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).
233 AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
234 assert(Result && "Pass does not implement interface required!");
238 } // End llvm namespace