1 //===- PassManager.h - Pass management infrastructure -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
21 /// The core IR library provides managers for running passes over
22 /// modules and functions.
24 /// * FunctionPassManager can run over a Module, runs each pass over
26 /// * ModulePassManager must be directly run, runs each pass over the Module.
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
36 //===----------------------------------------------------------------------===//
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"
51 /// \brief Implementation details of the pass manager interfaces.
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;
61 /// \brief The polymorphic API which runs the pass over a given IR entity.
62 virtual bool run(T Arg) = 0;
65 /// \brief A template wrapper used to implement the polymorphic API.
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
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); }
79 class AnalysisManager;
81 class ModulePassManager {
83 ModulePassManager(Module *M, AnalysisManager *AM = 0) : M(M), AM(AM) {}
85 template <typename ModulePassT> void addPass(ModulePassT Pass) {
86 Passes.push_back(new ModulePassModel<ModulePassT>(llvm_move(Pass)));
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) {}
101 std::vector<polymorphic_ptr<ModulePassConcept> > Passes;
104 class FunctionPassManager {
106 FunctionPassManager(AnalysisManager *AM = 0) : AM(AM) {}
108 template <typename FunctionPassT> void addPass(FunctionPassT Pass) {
109 Passes.push_back(new FunctionPassModel<FunctionPassT>(llvm_move(Pass)));
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) {}
124 std::vector<polymorphic_ptr<FunctionPassConcept> > Passes;
128 /// \brief An analysis manager to coordinate and cache analyses run over
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.
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 {
143 AnalysisManager(Module *M) : M(M) {}
145 /// \brief Get the result of an analysis pass for this module.
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.
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");
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;
163 /// \brief Get the result of an analysis pass for a function.
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");
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;
178 /// \brief Register an analysis pass with the manager.
180 /// This provides an initialized and set-up analysis pass to the
182 /// manager. Whomever is setting up analysis passes must use this to
184 /// the manager with all of the analysis passes available.
185 template <typename PassT> void registerAnalysisPass(PassT Pass) {
186 registerAnalysisPassImpl<PassT>(llvm_move(Pass));
189 /// \brief Invalidate a specific analysis pass for an IR module.
191 /// Note that the analysis result can disregard invalidation.
192 template <typename PassT> void invalidate(Module *M) {
193 invalidateImpl(PassT::ID(), M);
196 /// \brief Invalidate a specific analysis pass for an IR function.
198 /// Note that the analysis result can disregard invalidation.
199 template <typename PassT> void invalidate(Function *F) {
200 invalidateImpl(PassT::ID(), F);
203 /// \brief Invalidate analyses cached for an IR Module.
205 /// Note that specific analysis results can disregard invalidation by
206 /// overriding their invalidate method.
208 /// The module must be the module this analysis manager was constructed
210 void invalidateAll(Module *M);
212 /// \brief Invalidate analyses cached for an IR Function.
214 /// Note that specific analysis results can disregard invalidation by
215 /// overriding the invalidate method.
216 void invalidateAll(Function *F);
219 /// \brief Abstract concept of an analysis result.
221 /// This concept is parameterized over the IR unit that this result pertains
223 template <typename IRUnitT> struct AnalysisResultConcept {
224 virtual ~AnalysisResultConcept() {}
225 virtual AnalysisResultConcept *clone() = 0;
227 /// \brief Method to try and mark a result as invalid.
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.
232 /// \returns true if the result should indeed be invalidated (the default).
233 virtual bool invalidate(IRUnitT *IR) = 0;
236 /// \brief Wrapper to model the analysis result concept.
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);
247 /// \brief The model delegates to the \c ResultT method.
248 virtual bool invalidate(IRUnitT *IR) { return Result.invalidate(IR); }
253 /// \brief Abstract concept of an analysis pass.
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;
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
264 virtual AnalysisResultConcept<IRUnitT> *run(IRUnitT *IR) = 0;
267 /// \brief Wrapper to model the analysis pass concept.
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); }
277 // FIXME: Replace PassT::IRUnitT with type traits when we use C++11.
278 typedef typename PassT::IRUnitT IRUnitT;
280 // FIXME: Replace PassT::Result with type traits when we use C++11.
281 typedef AnalysisResultModel<IRUnitT, typename PassT::Result> ResultModelT;
283 /// \brief The model delegates to the \c PassT::run method.
285 /// The return is wrapped in an \c AnalysisResultModel.
286 virtual ResultModelT *run(IRUnitT *IR) {
287 return new ResultModelT(Pass.run(IR));
294 /// \brief Get a module pass result, running the pass if necessary.
295 const AnalysisResultConcept<Module> &getResultImpl(void *PassID, Module *M);
297 /// \brief Get a function pass result, running the pass if necessary.
298 const AnalysisResultConcept<Function> &getResultImpl(void *PassID,
301 /// \brief Invalidate a module pass result.
302 void invalidateImpl(void *PassID, Module *M);
304 /// \brief Invalidate a function pass result.
305 void invalidateImpl(void *PassID, Function *F);
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));
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));
329 /// \brief Map type from module analysis pass ID to pass concept pointer.
330 typedef DenseMap<void *, polymorphic_ptr<AnalysisPassConcept<Module> > >
331 ModuleAnalysisPassMapT;
333 /// \brief Collection of module analysis passes, indexed by ID.
334 ModuleAnalysisPassMapT ModuleAnalysisPasses;
336 /// \brief Map type from module analysis pass ID to pass result concept pointer.
337 typedef DenseMap<void *, polymorphic_ptr<AnalysisResultConcept<Module> > >
338 ModuleAnalysisResultMapT;
340 /// \brief Cache of computed module analysis results for this module.
341 ModuleAnalysisResultMapT ModuleAnalysisResults;
344 /// \brief Map type from function analysis pass ID to pass concept pointer.
345 typedef DenseMap<void *, polymorphic_ptr<AnalysisPassConcept<Function> > >
346 FunctionAnalysisPassMapT;
348 /// \brief Collection of function analysis passes, indexed by ID.
349 FunctionAnalysisPassMapT FunctionAnalysisPasses;
351 /// \brief List of function analysis pass IDs and associated concept pointers.
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.
357 std::pair<void *, polymorphic_ptr<AnalysisResultConcept<Function> > > >
358 FunctionAnalysisResultListT;
360 /// \brief Map type from function pointer to our custom list type.
361 typedef DenseMap<Function *, FunctionAnalysisResultListT> FunctionAnalysisResultListMapT;
363 /// \brief Map from function to a list of function analysis results.
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;
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;
375 /// \brief Map from an analysis ID and function to a particular cached
377 FunctionAnalysisResultMapT FunctionAnalysisResults;
379 /// \brief Module handle for the \c AnalysisManager.