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