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