Introduce an AnalysisManager which is like a pass manager but with a lot
[oota-llvm.git] / include / llvm / IR / PassManager.h
1 //===- PassManager.h - LegacyContainer for Passes --------------*- 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 //===----------------------------------------------------------------------===//
29
30 #include "llvm/ADT/DenseMap.h"
31 #include "llvm/ADT/polymorphic_ptr.h"
32 #include "llvm/Support/type_traits.h"
33 #include "llvm/IR/Function.h"
34 #include "llvm/IR/Module.h"
35 #include <list>
36 #include <vector>
37
38 namespace llvm {
39
40 class Module;
41 class Function;
42
43 /// \brief Implementation details of the pass manager interfaces.
44 namespace detail {
45
46 /// \brief Template for the abstract base class used to dispatch
47 /// polymorphically over pass objects.
48 template <typename T> struct PassConcept {
49   // Boiler plate necessary for the container of derived classes.
50   virtual ~PassConcept() {}
51   virtual PassConcept *clone() = 0;
52
53   /// \brief The polymorphic API which runs the pass over a given IR entity.
54   virtual bool run(T Arg) = 0;
55 };
56
57 /// \brief A template wrapper used to implement the polymorphic API.
58 ///
59 /// Can be instantiated for any object which provides a \c run method
60 /// accepting a \c T. It requires the pass to be a copyable
61 /// object.
62 template <typename T, typename PassT> struct PassModel : PassConcept<T> {
63   PassModel(PassT Pass) : Pass(llvm_move(Pass)) {}
64   virtual PassModel *clone() { return new PassModel(Pass); }
65   virtual bool run(T Arg) { return Pass.run(Arg); }
66   PassT Pass;
67 };
68
69 }
70
71 class AnalysisManager;
72
73 class ModulePassManager {
74 public:
75   ModulePassManager(Module *M, AnalysisManager *AM = 0) : M(M), AM(AM) {}
76
77   template <typename ModulePassT> void addPass(ModulePassT Pass) {
78     Passes.push_back(new ModulePassModel<ModulePassT>(llvm_move(Pass)));
79   }
80
81   void run();
82
83 private:
84   // Pull in the concept type and model template specialized for modules.
85   typedef detail::PassConcept<Module *> ModulePassConcept;
86   template <typename PassT>
87   struct ModulePassModel : detail::PassModel<Module *, PassT> {
88     ModulePassModel(PassT Pass) : detail::PassModel<Module *, PassT>(Pass) {}
89   };
90
91   Module *M;
92   AnalysisManager *AM;
93   std::vector<polymorphic_ptr<ModulePassConcept> > Passes;
94 };
95
96 class FunctionPassManager {
97 public:
98   FunctionPassManager(AnalysisManager *AM = 0) : AM(AM) {}
99
100   template <typename FunctionPassT> void addPass(FunctionPassT Pass) {
101     Passes.push_back(new FunctionPassModel<FunctionPassT>(llvm_move(Pass)));
102   }
103
104   bool run(Module *M);
105
106 private:
107   // Pull in the concept type and model template specialized for functions.
108   typedef detail::PassConcept<Function *> FunctionPassConcept;
109   template <typename PassT>
110   struct FunctionPassModel : detail::PassModel<Function *, PassT> {
111     FunctionPassModel(PassT Pass)
112         : detail::PassModel<Function *, PassT>(Pass) {}
113   };
114
115   AnalysisManager *AM;
116   std::vector<polymorphic_ptr<FunctionPassConcept> > Passes;
117 };
118
119
120 /// \brief An analysis manager to coordinate and cache analyses run over
121 /// a module.
122 ///
123 /// The analysis manager is typically used by passes in a pass pipeline
124 /// (consisting potentially of several individual pass managers) over a module
125 /// of IR. It provides registration of available analyses, declaring
126 /// requirements on support for specific analyses, running of an specific
127 /// analysis over a specific unit of IR to compute an analysis result, and
128 /// caching of the analysis results to reuse them across multiple passes.
129 ///
130 /// It is the responsibility of callers to use the invalidation API to
131 /// invalidate analysis results when the IR they correspond to changes. The
132 /// \c ModulePassManager and \c FunctionPassManager do this automatically.
133 class AnalysisManager {
134 public:
135   AnalysisManager(Module *M) : M(M) {}
136
137   /// \brief Get the result of an analysis pass for this module.
138   ///
139   /// If there is not a valid cached result in the manager already, this will
140   /// re-run the analysis to produce a valid result.
141   ///
142   /// The module passed in must be the same module as the analysis manager was
143   /// constructed around.
144   template <typename PassT>
145   const typename PassT::Result &getResult(Module *M) {
146     const AnalysisResultConcept<Module> &ResultConcept =
147         getResultImpl(PassT::ID(), M);
148     typedef AnalysisResultModel<Module, typename PassT::Result> ResultModelT;
149     return static_cast<const ResultModelT &>(ResultConcept).Result;
150   }
151
152   /// \brief Get the result of an analysis pass for a function.
153   ///
154   /// If there is not a valid cached result in the manager already, this will
155   /// re-run the analysis to produce a valid result.
156   template <typename PassT>
157   const typename PassT::Result &getResult(Function *F) {
158     const AnalysisResultConcept<Function> &ResultConcept =
159         getResultImpl(PassT::ID(), F);
160     typedef AnalysisResultModel<Function, typename PassT::Result> ResultModelT;
161     return static_cast<const ResultModelT &>(ResultConcept).Result;
162   }
163
164   /// \brief Register an analysis pass with the manager.
165   ///
166   /// This provides an initialized and set-up analysis pass to the
167   /// analysis
168   /// manager. Whomever is setting up analysis passes must use this to
169   /// populate
170   /// the manager with all of the analysis passes available.
171   template <typename PassT> void registerAnalysisPass(PassT Pass) {
172     registerAnalysisPassImpl<PassT>(llvm_move(Pass));
173   }
174
175   /// \brief Require that a particular analysis pass is provided by the manager.
176   ///
177   /// This allows transform passes to assert ther requirements during
178   /// construction and fail fast if the analysis manager doesn't provide the
179   /// needed facilities.
180   ///
181   /// We force the analysis manager to have these passes explicitly registered
182   /// first to ensure that there is exactly one place in the code responsible
183   /// for adding an analysis pass to the manager as all transforms will share
184   /// a single pass within the manager and each may not be the canonical place
185   /// to initialize such a pass.
186   template <typename PassT> void requireAnalysisPass() {
187     requireAnalysisPassImpl<PassT>();
188   }
189
190   /// \brief Invalidate a specific analysis pass for an IR module.
191   ///
192   /// Note that the analysis result can disregard invalidation.
193   template <typename PassT> void invalidate(Module *M) {
194     invalidateImpl(PassT::ID(), M);
195   }
196
197   /// \brief Invalidate a specific analysis pass for an IR function.
198   ///
199   /// Note that the analysis result can disregard invalidation.
200   template <typename PassT> void invalidate(Function *F) {
201     invalidateImpl(PassT::ID(), F);
202   }
203
204   /// \brief Invalidate analyses cached for an IR Module.
205   ///
206   /// Note that specific analysis results can disregard invalidation by
207   /// overriding their invalidate method.
208   ///
209   /// The module must be the module this analysis manager was constructed
210   /// around.
211   void invalidateAll(Module *M);
212
213   /// \brief Invalidate analyses cached for an IR Function.
214   ///
215   /// Note that specific analysis results can disregard invalidation by
216   /// overriding the invalidate method.
217   void invalidateAll(Function *F);
218
219 private:
220   /// \brief Abstract concept of an analysis result.
221   ///
222   /// This concept is parameterized over the IR unit that this result pertains
223   /// to.
224   template <typename IRUnitT> struct AnalysisResultConcept {
225     virtual ~AnalysisResultConcept() {}
226     virtual AnalysisResultConcept *clone() = 0;
227
228     /// \brief Method to try and mark a result as invalid.
229     ///
230     /// When the outer \c AnalysisManager detects a change in some underlying
231     /// unit of the IR, it will call this method on all of the results cached.
232     ///
233     /// \returns true if the result should indeed be invalidated (the default).
234     virtual bool invalidate(IRUnitT *IR) = 0;
235   };
236
237   /// \brief Wrapper to model the analysis result concept.
238   ///
239   /// Can wrap any type which implements a suitable invalidate member and model
240   /// the AnalysisResultConcept for the AnalysisManager.
241   template <typename IRUnitT, typename ResultT>
242   struct AnalysisResultModel : AnalysisResultConcept<IRUnitT> {
243     AnalysisResultModel(ResultT Result) : Result(llvm_move(Result)) {}
244     virtual AnalysisResultModel *clone() {
245       return new AnalysisResultModel(Result);
246     }
247
248     /// \brief The model delegates to the \c ResultT method.
249     virtual bool invalidate(IRUnitT *IR) { return Result.invalidate(IR); }
250
251     ResultT Result;
252   };
253
254   /// \brief Abstract concept of an analysis pass.
255   ///
256   /// This concept is parameterized over the IR unit that it can run over and
257   /// produce an analysis result.
258   template <typename IRUnitT> struct AnalysisPassConcept {
259     virtual ~AnalysisPassConcept() {}
260     virtual AnalysisPassConcept *clone() = 0;
261
262     /// \brief Method to run this analysis over a unit of IR.
263     /// \returns The analysis result object to be queried by users, the caller
264     /// takes ownership.
265     virtual AnalysisResultConcept<IRUnitT> *run(IRUnitT *IR) = 0;
266   };
267
268   /// \brief Wrapper to model the analysis pass concept.
269   ///
270   /// Can wrap any type which implements a suitable \c run method. The method
271   /// must accept the IRUnitT as an argument and produce an object which can be
272   /// wrapped in a \c AnalysisResultModel.
273   template <typename PassT>
274   struct AnalysisPassModel : AnalysisPassConcept<typename PassT::IRUnitT> {
275     AnalysisPassModel(PassT Pass) : Pass(llvm_move(Pass)) {}
276     virtual AnalysisPassModel *clone() { return new AnalysisPassModel(Pass); }
277
278     // FIXME: Replace PassT::IRUnitT with type traits when we use C++11.
279     typedef typename PassT::IRUnitT IRUnitT;
280
281     // FIXME: Replace PassT::Result with type traits when we use C++11.
282     typedef AnalysisResultModel<IRUnitT, typename PassT::Result> ResultModelT;
283
284     /// \brief The model delegates to the \c PassT::run method.
285     ///
286     /// The return is wrapped in an \c AnalysisResultModel.
287     virtual ResultModelT *run(IRUnitT *IR) {
288       return new ResultModelT(Pass.run(IR));
289     }
290
291     PassT Pass;
292   };
293
294
295   /// \brief Get a module pass result, running the pass if necessary.
296   const AnalysisResultConcept<Module> &getResultImpl(void *PassID, Module *M);
297
298   /// \brief Get a function pass result, running the pass if necessary.
299   const AnalysisResultConcept<Function> &getResultImpl(void *PassID,
300                                                        Function *F);
301
302   /// \brief Invalidate a module pass result.
303   void invalidateImpl(void *PassID, Module *M);
304
305   /// \brief Invalidate a function pass result.
306   void invalidateImpl(void *PassID, Function *F);
307
308
309   /// \brief Module pass specific implementation of registration.
310   template <typename PassT>
311   typename enable_if<is_same<typename PassT::IRUnitT, Module> >::type
312   registerAnalysisPassImpl(PassT Pass) {
313     assert(!ModuleAnalysisPasses.count(PassT::ID()) &&
314            "Registered the same analysis pass twice!");
315     ModuleAnalysisPasses[PassT::ID()] =
316         new AnalysisPassModel<PassT>(llvm_move(Pass));
317   }
318
319   /// \brief Function pass specific implementation of registration.
320   template <typename PassT>
321   typename enable_if<is_same<typename PassT::IRUnitT, Function> >::type
322   registerAnalysisPassImpl(PassT Pass) {
323     assert(!FunctionAnalysisPasses.count(PassT::ID()) &&
324            "Registered the same analysis pass twice!");
325     FunctionAnalysisPasses[PassT::ID()] =
326         new AnalysisPassModel<PassT>(llvm_move(Pass));
327   }
328
329   /// \brief Module pass specific implementation of requirement declaration.
330   template <typename PassT>
331   typename enable_if<is_same<typename PassT::IRUnitT, Module> >::type
332   requireAnalysisPassImpl() {
333     assert(ModuleAnalysisPasses.count(PassT::ID()) &&
334            "This analysis pass was not registered prior to being required");
335   }
336
337   /// \brief Function pass specific implementation of requirement declaration.
338   template <typename PassT>
339   typename enable_if<is_same<typename PassT::IRUnitT, Function> >::type
340   requireAnalysisPassImpl() {
341     assert(FunctionAnalysisPasses.count(PassT::ID()) &&
342            "This analysis pass was not registered prior to being required");
343   }
344
345
346   /// \brief Map type from module analysis pass ID to pass concept pointer.
347   typedef DenseMap<void *, polymorphic_ptr<AnalysisPassConcept<Module> > >
348   ModuleAnalysisPassMapT;
349
350   /// \brief Collection of module analysis passes, indexed by ID.
351   ModuleAnalysisPassMapT ModuleAnalysisPasses;
352
353   /// \brief Map type from module analysis pass ID to pass result concept pointer.
354   typedef DenseMap<void *, polymorphic_ptr<AnalysisResultConcept<Module> > >
355   ModuleAnalysisResultMapT;
356
357   /// \brief Cache of computed module analysis results for this module.
358   ModuleAnalysisResultMapT ModuleAnalysisResults;
359
360
361   /// \brief Map type from function analysis pass ID to pass concept pointer.
362   typedef DenseMap<void *, polymorphic_ptr<AnalysisPassConcept<Function> > >
363   FunctionAnalysisPassMapT;
364
365   /// \brief Collection of function analysis passes, indexed by ID.
366   FunctionAnalysisPassMapT FunctionAnalysisPasses;
367
368   /// \brief List of function analysis pass IDs and associated concept pointers.
369   ///
370   /// Requires iterators to be valid across appending new entries and arbitrary
371   /// erases. Provides both the pass ID and concept pointer such that it is
372   /// half of a bijection and provides storage for the actual result concept.
373   typedef std::list<
374       std::pair<void *, polymorphic_ptr<AnalysisResultConcept<Function> > > >
375   FunctionAnalysisResultListT;
376
377   /// \brief Map type from function pointer to our custom list type.
378   typedef DenseMap<Function *, FunctionAnalysisResultListT> FunctionAnalysisResultListMapT;
379
380   /// \brief Map from function to a list of function analysis results.
381   ///
382   /// Provides linear time removal of all analysis results for a function and
383   /// the ultimate storage for a particular cached analysis result.
384   FunctionAnalysisResultListMapT FunctionAnalysisResultLists;
385
386   /// \brief Map type from a pair of analysis ID and function pointer to an
387   /// iterator into a particular result list.
388   typedef DenseMap<std::pair<void *, Function *>,
389                    FunctionAnalysisResultListT::iterator>
390   FunctionAnalysisResultMapT;
391
392   /// \brief Map from an analysis ID and function to a particular cached
393   /// analysis result.
394   FunctionAnalysisResultMapT FunctionAnalysisResults;
395
396   /// \brief Module handle for the \c AnalysisManager.
397   Module *M;
398 };
399
400 }