1 //===- PassManager.h - LegacyContainer for Passes --------------*- 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 //===----------------------------------------------------------------------===//
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"
43 /// \brief Implementation details of the pass manager interfaces.
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;
53 /// \brief The polymorphic API which runs the pass over a given IR entity.
54 virtual bool run(T Arg) = 0;
57 /// \brief A template wrapper used to implement the polymorphic API.
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
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); }
71 class AnalysisManager;
73 class ModulePassManager {
75 ModulePassManager(Module *M, AnalysisManager *AM = 0) : M(M), AM(AM) {}
77 template <typename ModulePassT> void addPass(ModulePassT Pass) {
78 Passes.push_back(new ModulePassModel<ModulePassT>(llvm_move(Pass)));
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) {}
93 std::vector<polymorphic_ptr<ModulePassConcept> > Passes;
96 class FunctionPassManager {
98 FunctionPassManager(AnalysisManager *AM = 0) : AM(AM) {}
100 template <typename FunctionPassT> void addPass(FunctionPassT Pass) {
101 Passes.push_back(new FunctionPassModel<FunctionPassT>(llvm_move(Pass)));
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) {}
116 std::vector<polymorphic_ptr<FunctionPassConcept> > Passes;
120 /// \brief An analysis manager to coordinate and cache analyses run over
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.
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 {
135 AnalysisManager(Module *M) : M(M) {}
137 /// \brief Get the result of an analysis pass for this module.
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.
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;
152 /// \brief Get the result of an analysis pass for a function.
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;
164 /// \brief Register an analysis pass with the manager.
166 /// This provides an initialized and set-up analysis pass to the
168 /// manager. Whomever is setting up analysis passes must use this to
170 /// the manager with all of the analysis passes available.
171 template <typename PassT> void registerAnalysisPass(PassT Pass) {
172 registerAnalysisPassImpl<PassT>(llvm_move(Pass));
175 /// \brief Require that a particular analysis pass is provided by the manager.
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.
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>();
190 /// \brief Invalidate a specific analysis pass for an IR module.
192 /// Note that the analysis result can disregard invalidation.
193 template <typename PassT> void invalidate(Module *M) {
194 invalidateImpl(PassT::ID(), M);
197 /// \brief Invalidate a specific analysis pass for an IR function.
199 /// Note that the analysis result can disregard invalidation.
200 template <typename PassT> void invalidate(Function *F) {
201 invalidateImpl(PassT::ID(), F);
204 /// \brief Invalidate analyses cached for an IR Module.
206 /// Note that specific analysis results can disregard invalidation by
207 /// overriding their invalidate method.
209 /// The module must be the module this analysis manager was constructed
211 void invalidateAll(Module *M);
213 /// \brief Invalidate analyses cached for an IR Function.
215 /// Note that specific analysis results can disregard invalidation by
216 /// overriding the invalidate method.
217 void invalidateAll(Function *F);
220 /// \brief Abstract concept of an analysis result.
222 /// This concept is parameterized over the IR unit that this result pertains
224 template <typename IRUnitT> struct AnalysisResultConcept {
225 virtual ~AnalysisResultConcept() {}
226 virtual AnalysisResultConcept *clone() = 0;
228 /// \brief Method to try and mark a result as invalid.
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.
233 /// \returns true if the result should indeed be invalidated (the default).
234 virtual bool invalidate(IRUnitT *IR) = 0;
237 /// \brief Wrapper to model the analysis result concept.
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);
248 /// \brief The model delegates to the \c ResultT method.
249 virtual bool invalidate(IRUnitT *IR) { return Result.invalidate(IR); }
254 /// \brief Abstract concept of an analysis pass.
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;
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
265 virtual AnalysisResultConcept<IRUnitT> *run(IRUnitT *IR) = 0;
268 /// \brief Wrapper to model the analysis pass concept.
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); }
278 // FIXME: Replace PassT::IRUnitT with type traits when we use C++11.
279 typedef typename PassT::IRUnitT IRUnitT;
281 // FIXME: Replace PassT::Result with type traits when we use C++11.
282 typedef AnalysisResultModel<IRUnitT, typename PassT::Result> ResultModelT;
284 /// \brief The model delegates to the \c PassT::run method.
286 /// The return is wrapped in an \c AnalysisResultModel.
287 virtual ResultModelT *run(IRUnitT *IR) {
288 return new ResultModelT(Pass.run(IR));
295 /// \brief Get a module pass result, running the pass if necessary.
296 const AnalysisResultConcept<Module> &getResultImpl(void *PassID, Module *M);
298 /// \brief Get a function pass result, running the pass if necessary.
299 const AnalysisResultConcept<Function> &getResultImpl(void *PassID,
302 /// \brief Invalidate a module pass result.
303 void invalidateImpl(void *PassID, Module *M);
305 /// \brief Invalidate a function pass result.
306 void invalidateImpl(void *PassID, Function *F);
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));
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));
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");
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");
346 /// \brief Map type from module analysis pass ID to pass concept pointer.
347 typedef DenseMap<void *, polymorphic_ptr<AnalysisPassConcept<Module> > >
348 ModuleAnalysisPassMapT;
350 /// \brief Collection of module analysis passes, indexed by ID.
351 ModuleAnalysisPassMapT ModuleAnalysisPasses;
353 /// \brief Map type from module analysis pass ID to pass result concept pointer.
354 typedef DenseMap<void *, polymorphic_ptr<AnalysisResultConcept<Module> > >
355 ModuleAnalysisResultMapT;
357 /// \brief Cache of computed module analysis results for this module.
358 ModuleAnalysisResultMapT ModuleAnalysisResults;
361 /// \brief Map type from function analysis pass ID to pass concept pointer.
362 typedef DenseMap<void *, polymorphic_ptr<AnalysisPassConcept<Function> > >
363 FunctionAnalysisPassMapT;
365 /// \brief Collection of function analysis passes, indexed by ID.
366 FunctionAnalysisPassMapT FunctionAnalysisPasses;
368 /// \brief List of function analysis pass IDs and associated concept pointers.
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.
374 std::pair<void *, polymorphic_ptr<AnalysisResultConcept<Function> > > >
375 FunctionAnalysisResultListT;
377 /// \brief Map type from function pointer to our custom list type.
378 typedef DenseMap<Function *, FunctionAnalysisResultListT> FunctionAnalysisResultListMapT;
380 /// \brief Map from function to a list of function analysis results.
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;
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;
392 /// \brief Map from an analysis ID and function to a particular cached
394 FunctionAnalysisResultMapT FunctionAnalysisResults;
396 /// \brief Module handle for the \c AnalysisManager.