• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include <vector>
25 
26 namespace llvm {
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 //
36 class AnalysisUsage {
37 public:
38   typedef SmallVector<AnalysisID, 32> VectorType;
39 
40 private:
41   // Sets of analyses required and preserved by a pass
42   VectorType Required, RequiredTransitive, Preserved;
43   bool PreservesAll;
44 
45 public:
AnalysisUsage()46   AnalysisUsage() : PreservesAll(false) {}
47 
48   // addRequired - Add the specified ID to the required set of the usage info
49   // for a pass.
50   //
51   AnalysisUsage &addRequiredID(const void *ID);
52   AnalysisUsage &addRequiredID(char &ID);
53   template<class PassClass>
addRequired()54   AnalysisUsage &addRequired() {
55     return addRequiredID(PassClass::ID);
56   }
57 
58   AnalysisUsage &addRequiredTransitiveID(char &ID);
59   template<class PassClass>
addRequiredTransitive()60   AnalysisUsage &addRequiredTransitive() {
61     return addRequiredTransitiveID(PassClass::ID);
62   }
63 
64   // addPreserved - Add the specified ID to the set of analyses preserved by
65   // this pass
66   //
addPreservedID(const void * ID)67   AnalysisUsage &addPreservedID(const void *ID) {
68     Preserved.push_back(ID);
69     return *this;
70   }
addPreservedID(char & ID)71   AnalysisUsage &addPreservedID(char &ID) {
72     Preserved.push_back(&ID);
73     return *this;
74   }
75 
76   // addPreserved - Add the specified Pass class to the set of analyses
77   // preserved by this pass.
78   //
79   template<class PassClass>
addPreserved()80   AnalysisUsage &addPreserved() {
81     Preserved.push_back(&PassClass::ID);
82     return *this;
83   }
84 
85   // addPreserved - Add the Pass with the specified argument string to the set
86   // of analyses preserved by this pass. If no such Pass exists, do nothing.
87   // This can be useful when a pass is trivially preserved, but may not be
88   // linked in. Be careful about spelling!
89   //
90   AnalysisUsage &addPreserved(StringRef Arg);
91 
92   // setPreservesAll - Set by analyses that do not transform their input at all
setPreservesAll()93   void setPreservesAll() { PreservesAll = true; }
getPreservesAll()94   bool getPreservesAll() const { return PreservesAll; }
95 
96   /// setPreservesCFG - This function should be called by the pass, iff they do
97   /// not:
98   ///
99   ///  1. Add or remove basic blocks from the function
100   ///  2. Modify terminator instructions in any way.
101   ///
102   /// This function annotates the AnalysisUsage info object to say that analyses
103   /// that only depend on the CFG are preserved by this pass.
104   ///
105   void setPreservesCFG();
106 
getRequiredSet()107   const VectorType &getRequiredSet() const { return Required; }
getRequiredTransitiveSet()108   const VectorType &getRequiredTransitiveSet() const {
109     return RequiredTransitive;
110   }
getPreservedSet()111   const VectorType &getPreservedSet() const { return Preserved; }
112 };
113 
114 //===----------------------------------------------------------------------===//
115 // AnalysisResolver - Simple interface used by Pass objects to pull all
116 // analysis information out of pass manager that is responsible to manage
117 // the pass.
118 //
119 class PMDataManager;
120 class AnalysisResolver {
121 private:
122   AnalysisResolver();  // DO NOT IMPLEMENT
123 
124 public:
AnalysisResolver(PMDataManager & P)125   explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
126 
getPMDataManager()127   inline PMDataManager &getPMDataManager() { return PM; }
128 
129   // Find pass that is implementing PI.
findImplPass(AnalysisID PI)130   Pass *findImplPass(AnalysisID PI) {
131     Pass *ResultPass = 0;
132     for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) {
133       if (AnalysisImpls[i].first == PI) {
134         ResultPass = AnalysisImpls[i].second;
135         break;
136       }
137     }
138     return ResultPass;
139   }
140 
141   // Find pass that is implementing PI. Initialize pass for Function F.
142   Pass *findImplPass(Pass *P, AnalysisID PI, Function &F);
143 
addAnalysisImplsPair(AnalysisID PI,Pass * P)144   void addAnalysisImplsPair(AnalysisID PI, Pass *P) {
145     if (findImplPass(PI) == P)
146       return;
147     std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P);
148     AnalysisImpls.push_back(pir);
149   }
150 
151   /// clearAnalysisImpls - Clear cache that is used to connect a pass to the
152   /// the analysis (PassInfo).
clearAnalysisImpls()153   void clearAnalysisImpls() {
154     AnalysisImpls.clear();
155   }
156 
157   // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist
158   Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
159 
160 private:
161   // AnalysisImpls - This keeps track of which passes implements the interfaces
162   // that are required by the current pass (to implement getAnalysis()).
163   std::vector<std::pair<AnalysisID, Pass*> > AnalysisImpls;
164 
165   // PassManager that is used to resolve analysis info
166   PMDataManager &PM;
167 };
168 
169 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
170 /// get analysis information that might be around, for example to update it.
171 /// This is different than getAnalysis in that it can fail (if the analysis
172 /// results haven't been computed), so should only be used if you can handle
173 /// the case when the analysis is not available.  This method is often used by
174 /// transformation APIs to update analysis results for a pass automatically as
175 /// the transform is performed.
176 ///
177 template<typename AnalysisType>
getAnalysisIfAvailable()178 AnalysisType *Pass::getAnalysisIfAvailable() const {
179   assert(Resolver && "Pass not resident in a PassManager object!");
180 
181   const void *PI = &AnalysisType::ID;
182 
183   Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true);
184   if (ResultPass == 0) return 0;
185 
186   // Because the AnalysisType may not be a subclass of pass (for
187   // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
188   // adjust the return pointer (because the class may multiply inherit, once
189   // from pass, once from AnalysisType).
190   return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
191 }
192 
193 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
194 /// to the analysis information that they claim to use by overriding the
195 /// getAnalysisUsage function.
196 ///
197 template<typename AnalysisType>
getAnalysis()198 AnalysisType &Pass::getAnalysis() const {
199   assert(Resolver && "Pass has not been inserted into a PassManager object!");
200   return getAnalysisID<AnalysisType>(&AnalysisType::ID);
201 }
202 
203 template<typename AnalysisType>
getAnalysisID(AnalysisID PI)204 AnalysisType &Pass::getAnalysisID(AnalysisID PI) const {
205   assert(PI && "getAnalysis for unregistered pass!");
206   assert(Resolver&&"Pass has not been inserted into a PassManager object!");
207   // PI *must* appear in AnalysisImpls.  Because the number of passes used
208   // should be a small number, we just do a linear search over a (dense)
209   // vector.
210   Pass *ResultPass = Resolver->findImplPass(PI);
211   assert (ResultPass &&
212           "getAnalysis*() called on an analysis that was not "
213           "'required' by pass!");
214 
215   // Because the AnalysisType may not be a subclass of pass (for
216   // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
217   // adjust the return pointer (because the class may multiply inherit, once
218   // from pass, once from AnalysisType).
219   return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
220 }
221 
222 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
223 /// to the analysis information that they claim to use by overriding the
224 /// getAnalysisUsage function.
225 ///
226 template<typename AnalysisType>
getAnalysis(Function & F)227 AnalysisType &Pass::getAnalysis(Function &F) {
228   assert(Resolver &&"Pass has not been inserted into a PassManager object!");
229 
230   return getAnalysisID<AnalysisType>(&AnalysisType::ID, F);
231 }
232 
233 template<typename AnalysisType>
getAnalysisID(AnalysisID PI,Function & F)234 AnalysisType &Pass::getAnalysisID(AnalysisID PI, Function &F) {
235   assert(PI && "getAnalysis for unregistered pass!");
236   assert(Resolver && "Pass has not been inserted into a PassManager object!");
237   // PI *must* appear in AnalysisImpls.  Because the number of passes used
238   // should be a small number, we just do a linear search over a (dense)
239   // vector.
240   Pass *ResultPass = Resolver->findImplPass(this, PI, F);
241   assert(ResultPass && "Unable to find requested analysis info");
242 
243   // Because the AnalysisType may not be a subclass of pass (for
244   // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
245   // adjust the return pointer (because the class may multiply inherit, once
246   // from pass, once from AnalysisType).
247   return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
248 }
249 
250 } // End llvm namespace
251 
252 #endif
253