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