LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IR/PassManager.h
Go to the documentation of this file.
1 //===- PassManager.h - Pass management infrastructure -----------*- 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 /// \file
10 ///
11 /// This header defines various interfaces for pass management in LLVM. There
12 /// is no "pass" interface in LLVM per se. Instead, an instance of any class
13 /// which supports a method to 'run' it over a unit of IR can be used as
14 /// a pass. A pass manager is generally a tool to collect a sequence of passes
15 /// which run over a particular IR construct, and run each of them in sequence
16 /// over each such construct in the containing IR construct. As there is no
17 /// containing IR construct for a Module, a manager for passes over modules
18 /// forms the base case which runs its managed passes in sequence over the
19 /// single module provided.
20 ///
21 /// The core IR library provides managers for running passes over
22 /// modules and functions.
23 ///
24 /// * FunctionPassManager can run over a Module, runs each pass over
25 /// a Function.
26 /// * ModulePassManager must be directly run, runs each pass over the Module.
27 ///
28 /// Note that the implementations of the pass managers use concept-based
29 /// polymorphism as outlined in the "Value Semantics and Concept-based
30 /// Polymorphism" talk (or its abbreviated sibling "Inheritance Is The Base
31 /// Class of Evil") by Sean Parent:
32 /// * http://github.com/sean-parent/sean-parent.github.com/wiki/Papers-and-Presentations
33 /// * http://www.youtube.com/watch?v=_BpMYeUFXv8
34 /// * http://channel9.msdn.com/Events/GoingNative/2013/Inheritance-Is-The-Base-Class-of-Evil
35 ///
36 //===----------------------------------------------------------------------===//
37 
38 #include "llvm/ADT/DenseMap.h"
41 #include "llvm/IR/Function.h"
42 #include "llvm/IR/Module.h"
43 #include <list>
44 #include <vector>
45 
46 namespace llvm {
47 
48 class Module;
49 class Function;
50 
51 /// \brief Implementation details of the pass manager interfaces.
52 namespace detail {
53 
54 /// \brief Template for the abstract base class used to dispatch
55 /// polymorphically over pass objects.
56 template <typename T> struct PassConcept {
57  // Boiler plate necessary for the container of derived classes.
58  virtual ~PassConcept() {}
59  virtual PassConcept *clone() = 0;
60 
61  /// \brief The polymorphic API which runs the pass over a given IR entity.
62  virtual bool run(T Arg) = 0;
63 };
64 
65 /// \brief A template wrapper used to implement the polymorphic API.
66 ///
67 /// Can be instantiated for any object which provides a \c run method
68 /// accepting a \c T. It requires the pass to be a copyable
69 /// object.
70 template <typename T, typename PassT> struct PassModel : PassConcept<T> {
71  PassModel(PassT Pass) : Pass(llvm_move(Pass)) {}
72  virtual PassModel *clone() { return new PassModel(Pass); }
73  virtual bool run(T Arg) { return Pass.run(Arg); }
74  PassT Pass;
75 };
76 
77 }
78 
79 class AnalysisManager;
80 
82 public:
83  ModulePassManager(Module *M, AnalysisManager *AM = 0) : M(M), AM(AM) {}
84 
85  template <typename ModulePassT> void addPass(ModulePassT Pass) {
86  Passes.push_back(new ModulePassModel<ModulePassT>(llvm_move(Pass)));
87  }
88 
89  void run();
90 
91 private:
92  // Pull in the concept type and model template specialized for modules.
93  typedef detail::PassConcept<Module *> ModulePassConcept;
94  template <typename PassT>
95  struct ModulePassModel : detail::PassModel<Module *, PassT> {
96  ModulePassModel(PassT Pass) : detail::PassModel<Module *, PassT>(Pass) {}
97  };
98 
99  Module *M;
100  AnalysisManager *AM;
101  std::vector<polymorphic_ptr<ModulePassConcept> > Passes;
102 };
103 
105 public:
107 
108  template <typename FunctionPassT> void addPass(FunctionPassT Pass) {
109  Passes.push_back(new FunctionPassModel<FunctionPassT>(llvm_move(Pass)));
110  }
111 
112  bool run(Module *M);
113 
114 private:
115  // Pull in the concept type and model template specialized for functions.
116  typedef detail::PassConcept<Function *> FunctionPassConcept;
117  template <typename PassT>
118  struct FunctionPassModel : detail::PassModel<Function *, PassT> {
119  FunctionPassModel(PassT Pass)
120  : detail::PassModel<Function *, PassT>(Pass) {}
121  };
122 
123  AnalysisManager *AM;
124  std::vector<polymorphic_ptr<FunctionPassConcept> > Passes;
125 };
126 
127 
128 /// \brief An analysis manager to coordinate and cache analyses run over
129 /// a module.
130 ///
131 /// The analysis manager is typically used by passes in a pass pipeline
132 /// (consisting potentially of several individual pass managers) over a module
133 /// of IR. It provides registration of available analyses, declaring
134 /// requirements on support for specific analyses, running of an specific
135 /// analysis over a specific unit of IR to compute an analysis result, and
136 /// caching of the analysis results to reuse them across multiple passes.
137 ///
138 /// It is the responsibility of callers to use the invalidation API to
139 /// invalidate analysis results when the IR they correspond to changes. The
140 /// \c ModulePassManager and \c FunctionPassManager do this automatically.
142 public:
143  AnalysisManager(Module *M) : M(M) {}
144 
145  /// \brief Get the result of an analysis pass for this module.
146  ///
147  /// If there is not a valid cached result in the manager already, this will
148  /// re-run the analysis to produce a valid result.
149  ///
150  /// The module passed in must be the same module as the analysis manager was
151  /// constructed around.
152  template <typename PassT>
153  const typename PassT::Result &getResult(Module *M) {
154  assert(ModuleAnalysisPasses.count(PassT::ID()) &&
155  "This analysis pass was not registered prior to being queried");
156 
157  const AnalysisResultConcept<Module> &ResultConcept =
158  getResultImpl(PassT::ID(), M);
159  typedef AnalysisResultModel<Module, typename PassT::Result> ResultModelT;
160  return static_cast<const ResultModelT &>(ResultConcept).Result;
161  }
162 
163  /// \brief Get the result of an analysis pass for a function.
164  ///
165  /// If there is not a valid cached result in the manager already, this will
166  /// re-run the analysis to produce a valid result.
167  template <typename PassT>
168  const typename PassT::Result &getResult(Function *F) {
169  assert(FunctionAnalysisPasses.count(PassT::ID()) &&
170  "This analysis pass was not registered prior to being queried");
171 
172  const AnalysisResultConcept<Function> &ResultConcept =
173  getResultImpl(PassT::ID(), F);
174  typedef AnalysisResultModel<Function, typename PassT::Result> ResultModelT;
175  return static_cast<const ResultModelT &>(ResultConcept).Result;
176  }
177 
178  /// \brief Register an analysis pass with the manager.
179  ///
180  /// This provides an initialized and set-up analysis pass to the
181  /// analysis
182  /// manager. Whomever is setting up analysis passes must use this to
183  /// populate
184  /// the manager with all of the analysis passes available.
185  template <typename PassT> void registerAnalysisPass(PassT Pass) {
186  registerAnalysisPassImpl<PassT>(llvm_move(Pass));
187  }
188 
189  /// \brief Invalidate a specific analysis pass for an IR module.
190  ///
191  /// Note that the analysis result can disregard invalidation.
192  template <typename PassT> void invalidate(Module *M) {
193  invalidateImpl(PassT::ID(), M);
194  }
195 
196  /// \brief Invalidate a specific analysis pass for an IR function.
197  ///
198  /// Note that the analysis result can disregard invalidation.
199  template <typename PassT> void invalidate(Function *F) {
200  invalidateImpl(PassT::ID(), F);
201  }
202 
203  /// \brief Invalidate analyses cached for an IR Module.
204  ///
205  /// Note that specific analysis results can disregard invalidation by
206  /// overriding their invalidate method.
207  ///
208  /// The module must be the module this analysis manager was constructed
209  /// around.
210  void invalidateAll(Module *M);
211 
212  /// \brief Invalidate analyses cached for an IR Function.
213  ///
214  /// Note that specific analysis results can disregard invalidation by
215  /// overriding the invalidate method.
216  void invalidateAll(Function *F);
217 
218 private:
219  /// \brief Abstract concept of an analysis result.
220  ///
221  /// This concept is parameterized over the IR unit that this result pertains
222  /// to.
223  template <typename IRUnitT> struct AnalysisResultConcept {
224  virtual ~AnalysisResultConcept() {}
225  virtual AnalysisResultConcept *clone() = 0;
226 
227  /// \brief Method to try and mark a result as invalid.
228  ///
229  /// When the outer \c AnalysisManager detects a change in some underlying
230  /// unit of the IR, it will call this method on all of the results cached.
231  ///
232  /// \returns true if the result should indeed be invalidated (the default).
233  virtual bool invalidate(IRUnitT *IR) = 0;
234  };
235 
236  /// \brief Wrapper to model the analysis result concept.
237  ///
238  /// Can wrap any type which implements a suitable invalidate member and model
239  /// the AnalysisResultConcept for the AnalysisManager.
240  template <typename IRUnitT, typename ResultT>
241  struct AnalysisResultModel : AnalysisResultConcept<IRUnitT> {
242  AnalysisResultModel(ResultT Result) : Result(llvm_move(Result)) {}
243  virtual AnalysisResultModel *clone() {
244  return new AnalysisResultModel(Result);
245  }
246 
247  /// \brief The model delegates to the \c ResultT method.
248  virtual bool invalidate(IRUnitT *IR) { return Result.invalidate(IR); }
249 
250  ResultT Result;
251  };
252 
253  /// \brief Abstract concept of an analysis pass.
254  ///
255  /// This concept is parameterized over the IR unit that it can run over and
256  /// produce an analysis result.
257  template <typename IRUnitT> struct AnalysisPassConcept {
258  virtual ~AnalysisPassConcept() {}
259  virtual AnalysisPassConcept *clone() = 0;
260 
261  /// \brief Method to run this analysis over a unit of IR.
262  /// \returns The analysis result object to be queried by users, the caller
263  /// takes ownership.
264  virtual AnalysisResultConcept<IRUnitT> *run(IRUnitT *IR) = 0;
265  };
266 
267  /// \brief Wrapper to model the analysis pass concept.
268  ///
269  /// Can wrap any type which implements a suitable \c run method. The method
270  /// must accept the IRUnitT as an argument and produce an object which can be
271  /// wrapped in a \c AnalysisResultModel.
272  template <typename PassT>
273  struct AnalysisPassModel : AnalysisPassConcept<typename PassT::IRUnitT> {
274  AnalysisPassModel(PassT Pass) : Pass(llvm_move(Pass)) {}
275  virtual AnalysisPassModel *clone() { return new AnalysisPassModel(Pass); }
276 
277  // FIXME: Replace PassT::IRUnitT with type traits when we use C++11.
278  typedef typename PassT::IRUnitT IRUnitT;
279 
280  // FIXME: Replace PassT::Result with type traits when we use C++11.
281  typedef AnalysisResultModel<IRUnitT, typename PassT::Result> ResultModelT;
282 
283  /// \brief The model delegates to the \c PassT::run method.
284  ///
285  /// The return is wrapped in an \c AnalysisResultModel.
286  virtual ResultModelT *run(IRUnitT *IR) {
287  return new ResultModelT(Pass.run(IR));
288  }
289 
290  PassT Pass;
291  };
292 
293 
294  /// \brief Get a module pass result, running the pass if necessary.
295  const AnalysisResultConcept<Module> &getResultImpl(void *PassID, Module *M);
296 
297  /// \brief Get a function pass result, running the pass if necessary.
298  const AnalysisResultConcept<Function> &getResultImpl(void *PassID,
299  Function *F);
300 
301  /// \brief Invalidate a module pass result.
302  void invalidateImpl(void *PassID, Module *M);
303 
304  /// \brief Invalidate a function pass result.
305  void invalidateImpl(void *PassID, Function *F);
306 
307 
308  /// \brief Module pass specific implementation of registration.
309  template <typename PassT>
310  typename enable_if<is_same<typename PassT::IRUnitT, Module> >::type
311  registerAnalysisPassImpl(PassT Pass) {
312  assert(!ModuleAnalysisPasses.count(PassT::ID()) &&
313  "Registered the same analysis pass twice!");
314  ModuleAnalysisPasses[PassT::ID()] =
315  new AnalysisPassModel<PassT>(llvm_move(Pass));
316  }
317 
318  /// \brief Function pass specific implementation of registration.
319  template <typename PassT>
320  typename enable_if<is_same<typename PassT::IRUnitT, Function> >::type
321  registerAnalysisPassImpl(PassT Pass) {
322  assert(!FunctionAnalysisPasses.count(PassT::ID()) &&
323  "Registered the same analysis pass twice!");
324  FunctionAnalysisPasses[PassT::ID()] =
325  new AnalysisPassModel<PassT>(llvm_move(Pass));
326  }
327 
328 
329  /// \brief Map type from module analysis pass ID to pass concept pointer.
330  typedef DenseMap<void *, polymorphic_ptr<AnalysisPassConcept<Module> > >
331  ModuleAnalysisPassMapT;
332 
333  /// \brief Collection of module analysis passes, indexed by ID.
334  ModuleAnalysisPassMapT ModuleAnalysisPasses;
335 
336  /// \brief Map type from module analysis pass ID to pass result concept pointer.
337  typedef DenseMap<void *, polymorphic_ptr<AnalysisResultConcept<Module> > >
338  ModuleAnalysisResultMapT;
339 
340  /// \brief Cache of computed module analysis results for this module.
341  ModuleAnalysisResultMapT ModuleAnalysisResults;
342 
343 
344  /// \brief Map type from function analysis pass ID to pass concept pointer.
345  typedef DenseMap<void *, polymorphic_ptr<AnalysisPassConcept<Function> > >
346  FunctionAnalysisPassMapT;
347 
348  /// \brief Collection of function analysis passes, indexed by ID.
349  FunctionAnalysisPassMapT FunctionAnalysisPasses;
350 
351  /// \brief List of function analysis pass IDs and associated concept pointers.
352  ///
353  /// Requires iterators to be valid across appending new entries and arbitrary
354  /// erases. Provides both the pass ID and concept pointer such that it is
355  /// half of a bijection and provides storage for the actual result concept.
356  typedef std::list<
357  std::pair<void *, polymorphic_ptr<AnalysisResultConcept<Function> > > >
358  FunctionAnalysisResultListT;
359 
360  /// \brief Map type from function pointer to our custom list type.
361  typedef DenseMap<Function *, FunctionAnalysisResultListT> FunctionAnalysisResultListMapT;
362 
363  /// \brief Map from function to a list of function analysis results.
364  ///
365  /// Provides linear time removal of all analysis results for a function and
366  /// the ultimate storage for a particular cached analysis result.
367  FunctionAnalysisResultListMapT FunctionAnalysisResultLists;
368 
369  /// \brief Map type from a pair of analysis ID and function pointer to an
370  /// iterator into a particular result list.
371  typedef DenseMap<std::pair<void *, Function *>,
372  FunctionAnalysisResultListT::iterator>
373  FunctionAnalysisResultMapT;
374 
375  /// \brief Map from an analysis ID and function to a particular cached
376  /// analysis result.
377  FunctionAnalysisResultMapT FunctionAnalysisResults;
378 
379  /// \brief Module handle for the \c AnalysisManager.
380  Module *M;
381 };
382 
383 }
void addPass(ModulePassT Pass)
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
const PassT::Result & getResult(Module *M)
Get the result of an analysis pass for this module.
Template for the abstract base class used to dispatch polymorphically over pass objects.
F(f)
virtual bool run(T Arg)
The polymorphic API which runs the pass over a given IR entity.
#define llvm_move(value)
Definition: Compiler.h:108
void registerAnalysisPass(PassT Pass)
Register an analysis pass with the manager.
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
virtual PassModel * clone()
bool count(const KeyT &Val) const
count - Return true if the specified key is in the map.
Definition: DenseMap.h:103
ModulePassManager(Module *M, AnalysisManager *AM=0)
void invalidateAll(Module *M)
Invalidate analyses cached for an IR Module.
Definition: PassManager.cpp:62
void invalidate(Module *M)
Invalidate a specific analysis pass for an IR module.
A template wrapper used to implement the polymorphic API.
FunctionPassManager(AnalysisManager *AM=0)
virtual PassConcept * clone()=0
void addPass(FunctionPassT Pass)
const PassT::Result & getResult(Function *F)
Get the result of an analysis pass for a function.
void invalidate(Function *F)
Invalidate a specific analysis pass for an IR function.
An analysis manager to coordinate and cache analyses run over a module.
Statically lint checks LLVM IR
Definition: Lint.cpp:158
virtual bool run(T Arg)=0
The polymorphic API which runs the pass over a given IR entity.