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