Fix the header comment of the new pass manager stuff to not claim to be
[oota-llvm.git] / include / llvm / IR / PassManager.h
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"
39 #include "llvm/ADT/polymorphic_ptr.h"
40 #include "llvm/Support/type_traits.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
81 class ModulePassManager {
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
104 class FunctionPassManager {
105 public:
106   FunctionPassManager(AnalysisManager *AM = 0) : AM(AM) {}
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.
141 class AnalysisManager {
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     const AnalysisResultConcept<Module> &ResultConcept =
155         getResultImpl(PassT::ID(), M);
156     typedef AnalysisResultModel<Module, typename PassT::Result> ResultModelT;
157     return static_cast<const ResultModelT &>(ResultConcept).Result;
158   }
159
160   /// \brief Get the result of an analysis pass for a function.
161   ///
162   /// If there is not a valid cached result in the manager already, this will
163   /// re-run the analysis to produce a valid result.
164   template <typename PassT>
165   const typename PassT::Result &getResult(Function *F) {
166     const AnalysisResultConcept<Function> &ResultConcept =
167         getResultImpl(PassT::ID(), F);
168     typedef AnalysisResultModel<Function, typename PassT::Result> ResultModelT;
169     return static_cast<const ResultModelT &>(ResultConcept).Result;
170   }
171
172   /// \brief Register an analysis pass with the manager.
173   ///
174   /// This provides an initialized and set-up analysis pass to the
175   /// analysis
176   /// manager. Whomever is setting up analysis passes must use this to
177   /// populate
178   /// the manager with all of the analysis passes available.
179   template <typename PassT> void registerAnalysisPass(PassT Pass) {
180     registerAnalysisPassImpl<PassT>(llvm_move(Pass));
181   }
182
183   /// \brief Require that a particular analysis pass is provided by the manager.
184   ///
185   /// This allows transform passes to assert ther requirements during
186   /// construction and fail fast if the analysis manager doesn't provide the
187   /// needed facilities.
188   ///
189   /// We force the analysis manager to have these passes explicitly registered
190   /// first to ensure that there is exactly one place in the code responsible
191   /// for adding an analysis pass to the manager as all transforms will share
192   /// a single pass within the manager and each may not be the canonical place
193   /// to initialize such a pass.
194   template <typename PassT> void requireAnalysisPass() {
195     requireAnalysisPassImpl<PassT>();
196   }
197
198   /// \brief Invalidate a specific analysis pass for an IR module.
199   ///
200   /// Note that the analysis result can disregard invalidation.
201   template <typename PassT> void invalidate(Module *M) {
202     invalidateImpl(PassT::ID(), M);
203   }
204
205   /// \brief Invalidate a specific analysis pass for an IR function.
206   ///
207   /// Note that the analysis result can disregard invalidation.
208   template <typename PassT> void invalidate(Function *F) {
209     invalidateImpl(PassT::ID(), F);
210   }
211
212   /// \brief Invalidate analyses cached for an IR Module.
213   ///
214   /// Note that specific analysis results can disregard invalidation by
215   /// overriding their invalidate method.
216   ///
217   /// The module must be the module this analysis manager was constructed
218   /// around.
219   void invalidateAll(Module *M);
220
221   /// \brief Invalidate analyses cached for an IR Function.
222   ///
223   /// Note that specific analysis results can disregard invalidation by
224   /// overriding the invalidate method.
225   void invalidateAll(Function *F);
226
227 private:
228   /// \brief Abstract concept of an analysis result.
229   ///
230   /// This concept is parameterized over the IR unit that this result pertains
231   /// to.
232   template <typename IRUnitT> struct AnalysisResultConcept {
233     virtual ~AnalysisResultConcept() {}
234     virtual AnalysisResultConcept *clone() = 0;
235
236     /// \brief Method to try and mark a result as invalid.
237     ///
238     /// When the outer \c AnalysisManager detects a change in some underlying
239     /// unit of the IR, it will call this method on all of the results cached.
240     ///
241     /// \returns true if the result should indeed be invalidated (the default).
242     virtual bool invalidate(IRUnitT *IR) = 0;
243   };
244
245   /// \brief Wrapper to model the analysis result concept.
246   ///
247   /// Can wrap any type which implements a suitable invalidate member and model
248   /// the AnalysisResultConcept for the AnalysisManager.
249   template <typename IRUnitT, typename ResultT>
250   struct AnalysisResultModel : AnalysisResultConcept<IRUnitT> {
251     AnalysisResultModel(ResultT Result) : Result(llvm_move(Result)) {}
252     virtual AnalysisResultModel *clone() {
253       return new AnalysisResultModel(Result);
254     }
255
256     /// \brief The model delegates to the \c ResultT method.
257     virtual bool invalidate(IRUnitT *IR) { return Result.invalidate(IR); }
258
259     ResultT Result;
260   };
261
262   /// \brief Abstract concept of an analysis pass.
263   ///
264   /// This concept is parameterized over the IR unit that it can run over and
265   /// produce an analysis result.
266   template <typename IRUnitT> struct AnalysisPassConcept {
267     virtual ~AnalysisPassConcept() {}
268     virtual AnalysisPassConcept *clone() = 0;
269
270     /// \brief Method to run this analysis over a unit of IR.
271     /// \returns The analysis result object to be queried by users, the caller
272     /// takes ownership.
273     virtual AnalysisResultConcept<IRUnitT> *run(IRUnitT *IR) = 0;
274   };
275
276   /// \brief Wrapper to model the analysis pass concept.
277   ///
278   /// Can wrap any type which implements a suitable \c run method. The method
279   /// must accept the IRUnitT as an argument and produce an object which can be
280   /// wrapped in a \c AnalysisResultModel.
281   template <typename PassT>
282   struct AnalysisPassModel : AnalysisPassConcept<typename PassT::IRUnitT> {
283     AnalysisPassModel(PassT Pass) : Pass(llvm_move(Pass)) {}
284     virtual AnalysisPassModel *clone() { return new AnalysisPassModel(Pass); }
285
286     // FIXME: Replace PassT::IRUnitT with type traits when we use C++11.
287     typedef typename PassT::IRUnitT IRUnitT;
288
289     // FIXME: Replace PassT::Result with type traits when we use C++11.
290     typedef AnalysisResultModel<IRUnitT, typename PassT::Result> ResultModelT;
291
292     /// \brief The model delegates to the \c PassT::run method.
293     ///
294     /// The return is wrapped in an \c AnalysisResultModel.
295     virtual ResultModelT *run(IRUnitT *IR) {
296       return new ResultModelT(Pass.run(IR));
297     }
298
299     PassT Pass;
300   };
301
302
303   /// \brief Get a module pass result, running the pass if necessary.
304   const AnalysisResultConcept<Module> &getResultImpl(void *PassID, Module *M);
305
306   /// \brief Get a function pass result, running the pass if necessary.
307   const AnalysisResultConcept<Function> &getResultImpl(void *PassID,
308                                                        Function *F);
309
310   /// \brief Invalidate a module pass result.
311   void invalidateImpl(void *PassID, Module *M);
312
313   /// \brief Invalidate a function pass result.
314   void invalidateImpl(void *PassID, Function *F);
315
316
317   /// \brief Module pass specific implementation of registration.
318   template <typename PassT>
319   typename enable_if<is_same<typename PassT::IRUnitT, Module> >::type
320   registerAnalysisPassImpl(PassT Pass) {
321     assert(!ModuleAnalysisPasses.count(PassT::ID()) &&
322            "Registered the same analysis pass twice!");
323     ModuleAnalysisPasses[PassT::ID()] =
324         new AnalysisPassModel<PassT>(llvm_move(Pass));
325   }
326
327   /// \brief Function pass specific implementation of registration.
328   template <typename PassT>
329   typename enable_if<is_same<typename PassT::IRUnitT, Function> >::type
330   registerAnalysisPassImpl(PassT Pass) {
331     assert(!FunctionAnalysisPasses.count(PassT::ID()) &&
332            "Registered the same analysis pass twice!");
333     FunctionAnalysisPasses[PassT::ID()] =
334         new AnalysisPassModel<PassT>(llvm_move(Pass));
335   }
336
337   /// \brief Module pass specific implementation of requirement declaration.
338   template <typename PassT>
339   typename enable_if<is_same<typename PassT::IRUnitT, Module> >::type
340   requireAnalysisPassImpl() {
341     assert(ModuleAnalysisPasses.count(PassT::ID()) &&
342            "This analysis pass was not registered prior to being required");
343   }
344
345   /// \brief Function pass specific implementation of requirement declaration.
346   template <typename PassT>
347   typename enable_if<is_same<typename PassT::IRUnitT, Function> >::type
348   requireAnalysisPassImpl() {
349     assert(FunctionAnalysisPasses.count(PassT::ID()) &&
350            "This analysis pass was not registered prior to being required");
351   }
352
353
354   /// \brief Map type from module analysis pass ID to pass concept pointer.
355   typedef DenseMap<void *, polymorphic_ptr<AnalysisPassConcept<Module> > >
356   ModuleAnalysisPassMapT;
357
358   /// \brief Collection of module analysis passes, indexed by ID.
359   ModuleAnalysisPassMapT ModuleAnalysisPasses;
360
361   /// \brief Map type from module analysis pass ID to pass result concept pointer.
362   typedef DenseMap<void *, polymorphic_ptr<AnalysisResultConcept<Module> > >
363   ModuleAnalysisResultMapT;
364
365   /// \brief Cache of computed module analysis results for this module.
366   ModuleAnalysisResultMapT ModuleAnalysisResults;
367
368
369   /// \brief Map type from function analysis pass ID to pass concept pointer.
370   typedef DenseMap<void *, polymorphic_ptr<AnalysisPassConcept<Function> > >
371   FunctionAnalysisPassMapT;
372
373   /// \brief Collection of function analysis passes, indexed by ID.
374   FunctionAnalysisPassMapT FunctionAnalysisPasses;
375
376   /// \brief List of function analysis pass IDs and associated concept pointers.
377   ///
378   /// Requires iterators to be valid across appending new entries and arbitrary
379   /// erases. Provides both the pass ID and concept pointer such that it is
380   /// half of a bijection and provides storage for the actual result concept.
381   typedef std::list<
382       std::pair<void *, polymorphic_ptr<AnalysisResultConcept<Function> > > >
383   FunctionAnalysisResultListT;
384
385   /// \brief Map type from function pointer to our custom list type.
386   typedef DenseMap<Function *, FunctionAnalysisResultListT> FunctionAnalysisResultListMapT;
387
388   /// \brief Map from function to a list of function analysis results.
389   ///
390   /// Provides linear time removal of all analysis results for a function and
391   /// the ultimate storage for a particular cached analysis result.
392   FunctionAnalysisResultListMapT FunctionAnalysisResultLists;
393
394   /// \brief Map type from a pair of analysis ID and function pointer to an
395   /// iterator into a particular result list.
396   typedef DenseMap<std::pair<void *, Function *>,
397                    FunctionAnalysisResultListT::iterator>
398   FunctionAnalysisResultMapT;
399
400   /// \brief Map from an analysis ID and function to a particular cached
401   /// analysis result.
402   FunctionAnalysisResultMapT FunctionAnalysisResults;
403
404   /// \brief Module handle for the \c AnalysisManager.
405   Module *M;
406 };
407
408 }