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 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;
160 /// \brief Get the result of an analysis pass for a function.
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;
172 /// \brief Register an analysis pass with the manager.
174 /// This provides an initialized and set-up analysis pass to the
176 /// manager. Whomever is setting up analysis passes must use this to
178 /// the manager with all of the analysis passes available.
179 template <typename PassT> void registerAnalysisPass(PassT Pass) {
180 registerAnalysisPassImpl<PassT>(llvm_move(Pass));
183 /// \brief Require that a particular analysis pass is provided by the manager.
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.
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>();
198 /// \brief Invalidate a specific analysis pass for an IR module.
200 /// Note that the analysis result can disregard invalidation.
201 template <typename PassT> void invalidate(Module *M) {
202 invalidateImpl(PassT::ID(), M);
205 /// \brief Invalidate a specific analysis pass for an IR function.
207 /// Note that the analysis result can disregard invalidation.
208 template <typename PassT> void invalidate(Function *F) {
209 invalidateImpl(PassT::ID(), F);
212 /// \brief Invalidate analyses cached for an IR Module.
214 /// Note that specific analysis results can disregard invalidation by
215 /// overriding their invalidate method.
217 /// The module must be the module this analysis manager was constructed
219 void invalidateAll(Module *M);
221 /// \brief Invalidate analyses cached for an IR Function.
223 /// Note that specific analysis results can disregard invalidation by
224 /// overriding the invalidate method.
225 void invalidateAll(Function *F);
228 /// \brief Abstract concept of an analysis result.
230 /// This concept is parameterized over the IR unit that this result pertains
232 template <typename IRUnitT> struct AnalysisResultConcept {
233 virtual ~AnalysisResultConcept() {}
234 virtual AnalysisResultConcept *clone() = 0;
236 /// \brief Method to try and mark a result as invalid.
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.
241 /// \returns true if the result should indeed be invalidated (the default).
242 virtual bool invalidate(IRUnitT *IR) = 0;
245 /// \brief Wrapper to model the analysis result concept.
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);
256 /// \brief The model delegates to the \c ResultT method.
257 virtual bool invalidate(IRUnitT *IR) { return Result.invalidate(IR); }
262 /// \brief Abstract concept of an analysis pass.
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;
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
273 virtual AnalysisResultConcept<IRUnitT> *run(IRUnitT *IR) = 0;
276 /// \brief Wrapper to model the analysis pass concept.
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); }
286 // FIXME: Replace PassT::IRUnitT with type traits when we use C++11.
287 typedef typename PassT::IRUnitT IRUnitT;
289 // FIXME: Replace PassT::Result with type traits when we use C++11.
290 typedef AnalysisResultModel<IRUnitT, typename PassT::Result> ResultModelT;
292 /// \brief The model delegates to the \c PassT::run method.
294 /// The return is wrapped in an \c AnalysisResultModel.
295 virtual ResultModelT *run(IRUnitT *IR) {
296 return new ResultModelT(Pass.run(IR));
303 /// \brief Get a module pass result, running the pass if necessary.
304 const AnalysisResultConcept<Module> &getResultImpl(void *PassID, Module *M);
306 /// \brief Get a function pass result, running the pass if necessary.
307 const AnalysisResultConcept<Function> &getResultImpl(void *PassID,
310 /// \brief Invalidate a module pass result.
311 void invalidateImpl(void *PassID, Module *M);
313 /// \brief Invalidate a function pass result.
314 void invalidateImpl(void *PassID, Function *F);
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));
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));
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");
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");
354 /// \brief Map type from module analysis pass ID to pass concept pointer.
355 typedef DenseMap<void *, polymorphic_ptr<AnalysisPassConcept<Module> > >
356 ModuleAnalysisPassMapT;
358 /// \brief Collection of module analysis passes, indexed by ID.
359 ModuleAnalysisPassMapT ModuleAnalysisPasses;
361 /// \brief Map type from module analysis pass ID to pass result concept pointer.
362 typedef DenseMap<void *, polymorphic_ptr<AnalysisResultConcept<Module> > >
363 ModuleAnalysisResultMapT;
365 /// \brief Cache of computed module analysis results for this module.
366 ModuleAnalysisResultMapT ModuleAnalysisResults;
369 /// \brief Map type from function analysis pass ID to pass concept pointer.
370 typedef DenseMap<void *, polymorphic_ptr<AnalysisPassConcept<Function> > >
371 FunctionAnalysisPassMapT;
373 /// \brief Collection of function analysis passes, indexed by ID.
374 FunctionAnalysisPassMapT FunctionAnalysisPasses;
376 /// \brief List of function analysis pass IDs and associated concept pointers.
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.
382 std::pair<void *, polymorphic_ptr<AnalysisResultConcept<Function> > > >
383 FunctionAnalysisResultListT;
385 /// \brief Map type from function pointer to our custom list type.
386 typedef DenseMap<Function *, FunctionAnalysisResultListT> FunctionAnalysisResultListMapT;
388 /// \brief Map from function to a list of function analysis results.
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;
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;
400 /// \brief Map from an analysis ID and function to a particular cached
402 FunctionAnalysisResultMapT FunctionAnalysisResults;
404 /// \brief Module handle for the \c AnalysisManager.