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 #ifndef LLVM_IR_PASS_MANAGER_H
39 #define LLVM_IR_PASS_MANAGER_H
41 #include "llvm/ADT/DenseMap.h"
42 #include "llvm/ADT/STLExtras.h"
43 #include "llvm/ADT/SmallPtrSet.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/Module.h"
46 #include "llvm/Support/type_traits.h"
56 /// \brief An abstract set of preserved analyses following a transformation pass
59 /// When a transformation pass is run, it can return a set of analyses whose
60 /// results were preserved by that transformation. The default set is "none",
61 /// and preserving analyses must be done explicitly.
63 /// There is also an explicit all state which can be used (for example) when
64 /// the IR is not mutated at all.
65 class PreservedAnalyses {
67 // We have to explicitly define all the special member functions because MSVC
68 // refuses to generate them.
69 PreservedAnalyses() {}
70 PreservedAnalyses(const PreservedAnalyses &Arg)
71 : PreservedPassIDs(Arg.PreservedPassIDs) {}
72 PreservedAnalyses(PreservedAnalyses &&Arg)
73 : PreservedPassIDs(std::move(Arg.PreservedPassIDs)) {}
74 PreservedAnalyses &operator=(PreservedAnalyses RHS) {
75 std::swap(*this, RHS);
79 /// \brief Convenience factory function for the empty preserved set.
80 static PreservedAnalyses none() { return PreservedAnalyses(); }
82 /// \brief Construct a special preserved set that preserves all passes.
83 static PreservedAnalyses all() {
85 PA.PreservedPassIDs.insert((void *)AllPassesID);
89 /// \brief Mark a particular pass as preserved, adding it to the set.
90 template <typename PassT> void preserve() {
91 if (!areAllPreserved())
92 PreservedPassIDs.insert(PassT::ID());
95 /// \brief Intersect this set with another in place.
97 /// This is a mutating operation on this preserved set, removing all
98 /// preserved passes which are not also preserved in the argument.
99 void intersect(const PreservedAnalyses &Arg) {
100 if (Arg.areAllPreserved())
102 if (areAllPreserved()) {
103 PreservedPassIDs = Arg.PreservedPassIDs;
106 for (SmallPtrSet<void *, 2>::const_iterator I = PreservedPassIDs.begin(),
107 E = PreservedPassIDs.end();
109 if (!Arg.PreservedPassIDs.count(*I))
110 PreservedPassIDs.erase(*I);
113 /// \brief Intersect this set with a temporary other set in place.
115 /// This is a mutating operation on this preserved set, removing all
116 /// preserved passes which are not also preserved in the argument.
117 void intersect(PreservedAnalyses &&Arg) {
118 if (Arg.areAllPreserved())
120 if (areAllPreserved()) {
121 PreservedPassIDs = std::move(Arg.PreservedPassIDs);
124 for (SmallPtrSet<void *, 2>::const_iterator I = PreservedPassIDs.begin(),
125 E = PreservedPassIDs.end();
127 if (!Arg.PreservedPassIDs.count(*I))
128 PreservedPassIDs.erase(*I);
131 /// \brief Query whether a pass is marked as preserved by this set.
132 template <typename PassT> bool preserved() const {
133 return preserved(PassT::ID());
136 /// \brief Query whether an abstract pass ID is marked as preserved by this
138 bool preserved(void *PassID) const {
139 return PreservedPassIDs.count((void *)AllPassesID) ||
140 PreservedPassIDs.count(PassID);
144 // Note that this must not be -1 or -2 as those are already used by the
146 static const uintptr_t AllPassesID = (intptr_t)(-3);
148 bool areAllPreserved() const {
149 return PreservedPassIDs.count((void *)AllPassesID);
152 SmallPtrSet<void *, 2> PreservedPassIDs;
155 /// \brief Implementation details of the pass manager interfaces.
158 /// \brief Template for the abstract base class used to dispatch
159 /// polymorphically over pass objects.
160 template <typename IRUnitT, typename AnalysisManagerT> struct PassConcept {
161 // Boiler plate necessary for the container of derived classes.
162 virtual ~PassConcept() {}
164 /// \brief The polymorphic API which runs the pass over a given IR entity.
166 /// Note that actual pass object can omit the analysis manager argument if
167 /// desired. Also that the analysis manager may be null if there is no
168 /// analysis manager in the pass pipeline.
169 virtual PreservedAnalyses run(IRUnitT IR, AnalysisManagerT *AM) = 0;
171 /// \brief Polymorphic method to access the name of a pass.
172 virtual StringRef name() = 0;
175 /// \brief SFINAE metafunction for computing whether \c PassT has a run method
176 /// accepting an \c AnalysisManagerT.
177 template <typename IRUnitT, typename AnalysisManagerT, typename PassT,
179 class PassRunAcceptsAnalysisManager {
180 typedef char SmallType;
185 template <typename T, ResultT (T::*)(IRUnitT, AnalysisManagerT *)>
188 template <typename T> static SmallType f(Checker<T, &T::run> *);
189 template <typename T> static BigType f(...);
192 enum { Value = sizeof(f<PassT>(0)) == sizeof(SmallType) };
195 /// \brief A template wrapper used to implement the polymorphic API.
197 /// Can be instantiated for any object which provides a \c run method accepting
198 /// an \c IRUnitT. It requires the pass to be a copyable object. When the
199 /// \c run method also accepts an \c AnalysisManagerT*, we pass it along.
200 template <typename IRUnitT, typename AnalysisManagerT, typename PassT,
201 bool AcceptsAnalysisManager = PassRunAcceptsAnalysisManager<
202 IRUnitT, AnalysisManagerT, PassT, PreservedAnalyses>::Value>
205 /// \brief Specialization of \c PassModel for passes that accept an analyis
207 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
208 struct PassModel<IRUnitT, AnalysisManagerT, PassT, true>
209 : PassConcept<IRUnitT, AnalysisManagerT> {
210 explicit PassModel(PassT Pass) : Pass(std::move(Pass)) {}
211 // We have to explicitly define all the special member functions because MSVC
212 // refuses to generate them.
213 PassModel(const PassModel &Arg) : Pass(Arg.Pass) {}
214 PassModel(PassModel &&Arg) : Pass(std::move(Arg.Pass)) {}
215 PassModel &operator=(PassModel RHS) {
216 std::swap(*this, RHS);
220 PreservedAnalyses run(IRUnitT IR, AnalysisManagerT *AM) override {
221 return Pass.run(IR, AM);
223 StringRef name() override { return PassT::name(); }
227 /// \brief Specialization of \c PassModel for passes that accept an analyis
229 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
230 struct PassModel<IRUnitT, AnalysisManagerT, PassT, false>
231 : PassConcept<IRUnitT, AnalysisManagerT> {
232 explicit PassModel(PassT Pass) : Pass(std::move(Pass)) {}
233 // We have to explicitly define all the special member functions because MSVC
234 // refuses to generate them.
235 PassModel(const PassModel &Arg) : Pass(Arg.Pass) {}
236 PassModel(PassModel &&Arg) : Pass(std::move(Arg.Pass)) {}
237 PassModel &operator=(PassModel RHS) {
238 std::swap(*this, RHS);
242 PreservedAnalyses run(IRUnitT IR, AnalysisManagerT *AM) override {
245 StringRef name() override { return PassT::name(); }
249 /// \brief Abstract concept of an analysis result.
251 /// This concept is parameterized over the IR unit that this result pertains
253 template <typename IRUnitT> struct AnalysisResultConcept {
254 virtual ~AnalysisResultConcept() {}
256 /// \brief Method to try and mark a result as invalid.
258 /// When the outer analysis manager detects a change in some underlying
259 /// unit of the IR, it will call this method on all of the results cached.
261 /// This method also receives a set of preserved analyses which can be used
262 /// to avoid invalidation because the pass which changed the underlying IR
263 /// took care to update or preserve the analysis result in some way.
265 /// \returns true if the result is indeed invalid (the default).
266 virtual bool invalidate(IRUnitT IR, const PreservedAnalyses &PA) = 0;
269 /// \brief SFINAE metafunction for computing whether \c ResultT provides an
270 /// \c invalidate member function.
271 template <typename IRUnitT, typename ResultT> class ResultHasInvalidateMethod {
272 typedef char SmallType;
277 template <typename T, bool (T::*)(IRUnitT, const PreservedAnalyses &)>
280 template <typename T> static SmallType f(Checker<T, &T::invalidate> *);
281 template <typename T> static BigType f(...);
284 enum { Value = sizeof(f<ResultT>(0)) == sizeof(SmallType) };
287 /// \brief Wrapper to model the analysis result concept.
289 /// By default, this will implement the invalidate method with a trivial
290 /// implementation so that the actual analysis result doesn't need to provide
291 /// an invalidation handler. It is only selected when the invalidation handler
292 /// is not part of the ResultT's interface.
293 template <typename IRUnitT, typename PassT, typename ResultT,
294 bool HasInvalidateHandler =
295 ResultHasInvalidateMethod<IRUnitT, ResultT>::Value>
296 struct AnalysisResultModel;
298 /// \brief Specialization of \c AnalysisResultModel which provides the default
299 /// invalidate functionality.
300 template <typename IRUnitT, typename PassT, typename ResultT>
301 struct AnalysisResultModel<IRUnitT, PassT, ResultT, false>
302 : AnalysisResultConcept<IRUnitT> {
303 explicit AnalysisResultModel(ResultT Result) : Result(std::move(Result)) {}
304 // We have to explicitly define all the special member functions because MSVC
305 // refuses to generate them.
306 AnalysisResultModel(const AnalysisResultModel &Arg) : Result(Arg.Result) {}
307 AnalysisResultModel(AnalysisResultModel &&Arg)
308 : Result(std::move(Arg.Result)) {}
309 AnalysisResultModel &operator=(AnalysisResultModel RHS) {
310 std::swap(*this, RHS);
314 /// \brief The model bases invalidation solely on being in the preserved set.
316 // FIXME: We should actually use two different concepts for analysis results
317 // rather than two different models, and avoid the indirect function call for
318 // ones that use the trivial behavior.
319 bool invalidate(IRUnitT, const PreservedAnalyses &PA) override {
320 return !PA.preserved(PassT::ID());
326 /// \brief Specialization of \c AnalysisResultModel which delegates invalidate
327 /// handling to \c ResultT.
328 template <typename IRUnitT, typename PassT, typename ResultT>
329 struct AnalysisResultModel<IRUnitT, PassT, ResultT, true>
330 : AnalysisResultConcept<IRUnitT> {
331 explicit AnalysisResultModel(ResultT Result) : Result(std::move(Result)) {}
332 // We have to explicitly define all the special member functions because MSVC
333 // refuses to generate them.
334 AnalysisResultModel(const AnalysisResultModel &Arg) : Result(Arg.Result) {}
335 AnalysisResultModel(AnalysisResultModel &&Arg)
336 : Result(std::move(Arg.Result)) {}
337 AnalysisResultModel &operator=(AnalysisResultModel RHS) {
338 std::swap(*this, RHS);
342 /// \brief The model delegates to the \c ResultT method.
343 bool invalidate(IRUnitT IR, const PreservedAnalyses &PA) override {
344 return Result.invalidate(IR, PA);
350 /// \brief Abstract concept of an analysis pass.
352 /// This concept is parameterized over the IR unit that it can run over and
353 /// produce an analysis result.
354 template <typename IRUnitT, typename AnalysisManagerT>
355 struct AnalysisPassConcept {
356 virtual ~AnalysisPassConcept() {}
358 /// \brief Method to run this analysis over a unit of IR.
359 /// \returns A unique_ptr to the analysis result object to be queried by
361 virtual std::unique_ptr<AnalysisResultConcept<IRUnitT>>
362 run(IRUnitT IR, AnalysisManagerT *AM) = 0;
365 /// \brief Wrapper to model the analysis pass concept.
367 /// Can wrap any type which implements a suitable \c run method. The method
368 /// must accept the IRUnitT as an argument and produce an object which can be
369 /// wrapped in a \c AnalysisResultModel.
370 template <typename IRUnitT, typename AnalysisManagerT, typename PassT,
371 bool AcceptsAnalysisManager = PassRunAcceptsAnalysisManager<
372 IRUnitT, AnalysisManagerT, PassT, typename PassT::Result>::Value>
373 struct AnalysisPassModel;
375 /// \brief Specialization of \c AnalysisPassModel which passes an
376 /// \c AnalysisManager to PassT's run method.
377 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
378 struct AnalysisPassModel<IRUnitT, AnalysisManagerT, PassT, true>
379 : AnalysisPassConcept<IRUnitT, AnalysisManagerT> {
380 explicit AnalysisPassModel(PassT Pass) : Pass(std::move(Pass)) {}
381 // We have to explicitly define all the special member functions because MSVC
382 // refuses to generate them.
383 AnalysisPassModel(const AnalysisPassModel &Arg) : Pass(Arg.Pass) {}
384 AnalysisPassModel(AnalysisPassModel &&Arg) : Pass(std::move(Arg.Pass)) {}
385 AnalysisPassModel &operator=(AnalysisPassModel RHS) {
386 std::swap(*this, RHS);
390 // FIXME: Replace PassT::Result with type traits when we use C++11.
391 typedef AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
394 /// \brief The model delegates to the \c PassT::run method.
396 /// The return is wrapped in an \c AnalysisResultModel.
397 std::unique_ptr<AnalysisResultConcept<IRUnitT>>
398 run(IRUnitT IR, AnalysisManagerT *AM) override {
399 return make_unique<ResultModelT>(Pass.run(IR, AM));
405 /// \brief Specialization of \c AnalysisPassModel which does not pass an
406 /// \c AnalysisManager to PassT's run method.
407 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
408 struct AnalysisPassModel<IRUnitT, AnalysisManagerT, PassT, false>
409 : AnalysisPassConcept<IRUnitT, AnalysisManagerT> {
410 explicit AnalysisPassModel(PassT Pass) : Pass(std::move(Pass)) {}
411 // We have to explicitly define all the special member functions because MSVC
412 // refuses to generate them.
413 AnalysisPassModel(const AnalysisPassModel &Arg) : Pass(Arg.Pass) {}
414 AnalysisPassModel(AnalysisPassModel &&Arg) : Pass(std::move(Arg.Pass)) {}
415 AnalysisPassModel &operator=(AnalysisPassModel RHS) {
416 std::swap(*this, RHS);
420 // FIXME: Replace PassT::Result with type traits when we use C++11.
421 typedef AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
424 /// \brief The model delegates to the \c PassT::run method.
426 /// The return is wrapped in an \c AnalysisResultModel.
427 std::unique_ptr<AnalysisResultConcept<IRUnitT>>
428 run(IRUnitT IR, AnalysisManagerT *) override {
429 return make_unique<ResultModelT>(Pass.run(IR));
435 } // End namespace detail
437 class ModuleAnalysisManager;
439 class ModulePassManager {
441 // We have to explicitly define all the special member functions because MSVC
442 // refuses to generate them.
443 ModulePassManager() {}
444 ModulePassManager(ModulePassManager &&Arg) : Passes(std::move(Arg.Passes)) {}
445 ModulePassManager &operator=(ModulePassManager &&RHS) {
446 Passes = std::move(RHS.Passes);
450 /// \brief Run all of the module passes in this module pass manager over
453 /// This method should only be called for a single module as there is the
454 /// expectation that the lifetime of a pass is bounded to that of a module.
455 PreservedAnalyses run(Module *M, ModuleAnalysisManager *AM = 0);
457 template <typename ModulePassT> void addPass(ModulePassT Pass) {
458 Passes.emplace_back(new ModulePassModel<ModulePassT>(std::move(Pass)));
461 static StringRef name() { return "ModulePassManager"; }
464 // Pull in the concept type and model template specialized for modules.
465 typedef detail::PassConcept<Module *, ModuleAnalysisManager>
467 template <typename PassT>
468 struct ModulePassModel
469 : detail::PassModel<Module *, ModuleAnalysisManager, PassT> {
470 ModulePassModel(PassT Pass)
471 : detail::PassModel<Module *, ModuleAnalysisManager, PassT>(
475 ModulePassManager(const ModulePassManager &) LLVM_DELETED_FUNCTION;
476 ModulePassManager &operator=(const ModulePassManager &) LLVM_DELETED_FUNCTION;
478 std::vector<std::unique_ptr<ModulePassConcept>> Passes;
481 class FunctionAnalysisManager;
483 class FunctionPassManager {
485 // We have to explicitly define all the special member functions because MSVC
486 // refuses to generate them.
487 FunctionPassManager() {}
488 FunctionPassManager(FunctionPassManager &&Arg)
489 : Passes(std::move(Arg.Passes)) {}
490 FunctionPassManager &operator=(FunctionPassManager &&RHS) {
491 Passes = std::move(RHS.Passes);
495 template <typename FunctionPassT> void addPass(FunctionPassT Pass) {
496 Passes.emplace_back(new FunctionPassModel<FunctionPassT>(std::move(Pass)));
499 PreservedAnalyses run(Function *F, FunctionAnalysisManager *AM = 0);
501 static StringRef name() { return "FunctionPassManager"; }
504 // Pull in the concept type and model template specialized for functions.
505 typedef detail::PassConcept<Function *, FunctionAnalysisManager>
507 template <typename PassT>
508 struct FunctionPassModel
509 : detail::PassModel<Function *, FunctionAnalysisManager, PassT> {
510 FunctionPassModel(PassT Pass)
511 : detail::PassModel<Function *, FunctionAnalysisManager, PassT>(
515 FunctionPassManager(const FunctionPassManager &) LLVM_DELETED_FUNCTION;
516 FunctionPassManager &
517 operator=(const FunctionPassManager &) LLVM_DELETED_FUNCTION;
519 std::vector<std::unique_ptr<FunctionPassConcept>> Passes;
524 /// \brief A CRTP base used to implement analysis managers.
526 /// This class template serves as the boiler plate of an analysis manager. Any
527 /// analysis manager can be implemented on top of this base class. Any
528 /// implementation will be required to provide specific hooks:
531 /// - getCachedResultImpl
534 /// The details of the call pattern are within.
535 template <typename DerivedT, typename IRUnitT> class AnalysisManagerBase {
536 DerivedT *derived_this() { return static_cast<DerivedT *>(this); }
537 const DerivedT *derived_this() const {
538 return static_cast<const DerivedT *>(this);
541 AnalysisManagerBase(const AnalysisManagerBase &) LLVM_DELETED_FUNCTION;
542 AnalysisManagerBase &
543 operator=(const AnalysisManagerBase &) LLVM_DELETED_FUNCTION;
546 typedef detail::AnalysisResultConcept<IRUnitT> ResultConceptT;
547 typedef detail::AnalysisPassConcept<IRUnitT, DerivedT> PassConceptT;
549 // FIXME: Provide template aliases for the models when we're using C++11 in
550 // a mode supporting them.
552 // We have to explicitly define all the special member functions because MSVC
553 // refuses to generate them.
554 AnalysisManagerBase() {}
555 AnalysisManagerBase(AnalysisManagerBase &&Arg)
556 : AnalysisPasses(std::move(Arg.AnalysisPasses)) {}
557 AnalysisManagerBase &operator=(AnalysisManagerBase &&RHS) {
558 AnalysisPasses = std::move(RHS.AnalysisPasses);
563 /// \brief Get the result of an analysis pass for this module.
565 /// If there is not a valid cached result in the manager already, this will
566 /// re-run the analysis to produce a valid result.
567 template <typename PassT> typename PassT::Result &getResult(IRUnitT IR) {
568 assert(AnalysisPasses.count(PassT::ID()) &&
569 "This analysis pass was not registered prior to being queried");
571 ResultConceptT &ResultConcept =
572 derived_this()->getResultImpl(PassT::ID(), IR);
573 typedef detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
575 return static_cast<ResultModelT &>(ResultConcept).Result;
578 /// \brief Get the cached result of an analysis pass for this module.
580 /// This method never runs the analysis.
582 /// \returns null if there is no cached result.
583 template <typename PassT>
584 typename PassT::Result *getCachedResult(IRUnitT IR) const {
585 assert(AnalysisPasses.count(PassT::ID()) &&
586 "This analysis pass was not registered prior to being queried");
588 ResultConceptT *ResultConcept =
589 derived_this()->getCachedResultImpl(PassT::ID(), IR);
593 typedef detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
595 return &static_cast<ResultModelT *>(ResultConcept)->Result;
598 /// \brief Register an analysis pass with the manager.
600 /// This provides an initialized and set-up analysis pass to the analysis
601 /// manager. Whomever is setting up analysis passes must use this to populate
602 /// the manager with all of the analysis passes available.
603 template <typename PassT> void registerPass(PassT Pass) {
604 assert(!AnalysisPasses.count(PassT::ID()) &&
605 "Registered the same analysis pass twice!");
606 typedef detail::AnalysisPassModel<IRUnitT, DerivedT, PassT> PassModelT;
607 AnalysisPasses[PassT::ID()].reset(new PassModelT(std::move(Pass)));
610 /// \brief Invalidate a specific analysis pass for an IR module.
612 /// Note that the analysis result can disregard invalidation.
613 template <typename PassT> void invalidate(Module *M) {
614 assert(AnalysisPasses.count(PassT::ID()) &&
615 "This analysis pass was not registered prior to being invalidated");
616 derived_this()->invalidateImpl(PassT::ID(), M);
619 /// \brief Invalidate analyses cached for an IR unit.
621 /// Walk through all of the analyses pertaining to this unit of IR and
622 /// invalidate them unless they are preserved by the PreservedAnalyses set.
623 void invalidate(IRUnitT IR, const PreservedAnalyses &PA) {
624 derived_this()->invalidateImpl(IR, PA);
628 /// \brief Lookup a registered analysis pass.
629 PassConceptT &lookupPass(void *PassID) {
630 typename AnalysisPassMapT::iterator PI = AnalysisPasses.find(PassID);
631 assert(PI != AnalysisPasses.end() &&
632 "Analysis passes must be registered prior to being queried!");
636 /// \brief Lookup a registered analysis pass.
637 const PassConceptT &lookupPass(void *PassID) const {
638 typename AnalysisPassMapT::const_iterator PI = AnalysisPasses.find(PassID);
639 assert(PI != AnalysisPasses.end() &&
640 "Analysis passes must be registered prior to being queried!");
645 /// \brief Map type from module analysis pass ID to pass concept pointer.
646 typedef DenseMap<void *, std::unique_ptr<PassConceptT>> AnalysisPassMapT;
648 /// \brief Collection of module analysis passes, indexed by ID.
649 AnalysisPassMapT AnalysisPasses;
652 } // End namespace detail
654 /// \brief A module analysis pass manager with lazy running and caching of
656 class ModuleAnalysisManager
657 : public detail::AnalysisManagerBase<ModuleAnalysisManager, Module *> {
658 friend class detail::AnalysisManagerBase<ModuleAnalysisManager, Module *>;
659 typedef detail::AnalysisManagerBase<ModuleAnalysisManager, Module *> BaseT;
660 typedef BaseT::ResultConceptT ResultConceptT;
661 typedef BaseT::PassConceptT PassConceptT;
664 // We have to explicitly define all the special member functions because MSVC
665 // refuses to generate them.
666 ModuleAnalysisManager() {}
667 ModuleAnalysisManager(ModuleAnalysisManager &&Arg)
668 : BaseT(std::move(static_cast<BaseT &>(Arg))),
669 ModuleAnalysisResults(std::move(Arg.ModuleAnalysisResults)) {}
670 ModuleAnalysisManager &operator=(ModuleAnalysisManager &&RHS) {
671 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
672 ModuleAnalysisResults = std::move(RHS.ModuleAnalysisResults);
677 ModuleAnalysisManager(const ModuleAnalysisManager &) LLVM_DELETED_FUNCTION;
678 ModuleAnalysisManager &
679 operator=(const ModuleAnalysisManager &) LLVM_DELETED_FUNCTION;
681 /// \brief Get a module pass result, running the pass if necessary.
682 ResultConceptT &getResultImpl(void *PassID, Module *M);
684 /// \brief Get a cached module pass result or return null.
685 ResultConceptT *getCachedResultImpl(void *PassID, Module *M) const;
687 /// \brief Invalidate a module pass result.
688 void invalidateImpl(void *PassID, Module *M);
690 /// \brief Invalidate results across a module.
691 void invalidateImpl(Module *M, const PreservedAnalyses &PA);
693 /// \brief Map type from module analysis pass ID to pass result concept
695 typedef DenseMap<void *,
696 std::unique_ptr<detail::AnalysisResultConcept<Module *>>>
697 ModuleAnalysisResultMapT;
699 /// \brief Cache of computed module analysis results for this module.
700 ModuleAnalysisResultMapT ModuleAnalysisResults;
703 /// \brief A function analysis manager to coordinate and cache analyses run over
705 class FunctionAnalysisManager
706 : public detail::AnalysisManagerBase<FunctionAnalysisManager, Function *> {
707 friend class detail::AnalysisManagerBase<FunctionAnalysisManager, Function *>;
708 typedef detail::AnalysisManagerBase<FunctionAnalysisManager, Function *>
710 typedef BaseT::ResultConceptT ResultConceptT;
711 typedef BaseT::PassConceptT PassConceptT;
714 // Most public APIs are inherited from the CRTP base class.
716 // We have to explicitly define all the special member functions because MSVC
717 // refuses to generate them.
718 FunctionAnalysisManager() {}
719 FunctionAnalysisManager(FunctionAnalysisManager &&Arg)
720 : BaseT(std::move(static_cast<BaseT &>(Arg))),
721 FunctionAnalysisResults(std::move(Arg.FunctionAnalysisResults)) {}
722 FunctionAnalysisManager &operator=(FunctionAnalysisManager &&RHS) {
723 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
724 FunctionAnalysisResults = std::move(RHS.FunctionAnalysisResults);
728 /// \brief Returns true if the analysis manager has an empty results cache.
731 /// \brief Clear the function analysis result cache.
733 /// This routine allows cleaning up when the set of functions itself has
734 /// potentially changed, and thus we can't even look up a a result and
735 /// invalidate it directly. Notably, this does *not* call invalidate
736 /// functions as there is nothing to be done for them.
740 FunctionAnalysisManager(const FunctionAnalysisManager &)
741 LLVM_DELETED_FUNCTION;
742 FunctionAnalysisManager &
743 operator=(const FunctionAnalysisManager &) LLVM_DELETED_FUNCTION;
745 /// \brief Get a function pass result, running the pass if necessary.
746 ResultConceptT &getResultImpl(void *PassID, Function *F);
748 /// \brief Get a cached function pass result or return null.
749 ResultConceptT *getCachedResultImpl(void *PassID, Function *F) const;
751 /// \brief Invalidate a function pass result.
752 void invalidateImpl(void *PassID, Function *F);
754 /// \brief Invalidate the results for a function..
755 void invalidateImpl(Function *F, const PreservedAnalyses &PA);
757 /// \brief List of function analysis pass IDs and associated concept pointers.
759 /// Requires iterators to be valid across appending new entries and arbitrary
760 /// erases. Provides both the pass ID and concept pointer such that it is
761 /// half of a bijection and provides storage for the actual result concept.
762 typedef std::list<std::pair<
763 void *, std::unique_ptr<detail::AnalysisResultConcept<Function *>>>>
764 FunctionAnalysisResultListT;
766 /// \brief Map type from function pointer to our custom list type.
767 typedef DenseMap<Function *, FunctionAnalysisResultListT>
768 FunctionAnalysisResultListMapT;
770 /// \brief Map from function to a list of function analysis results.
772 /// Provides linear time removal of all analysis results for a function and
773 /// the ultimate storage for a particular cached analysis result.
774 FunctionAnalysisResultListMapT FunctionAnalysisResultLists;
776 /// \brief Map type from a pair of analysis ID and function pointer to an
777 /// iterator into a particular result list.
778 typedef DenseMap<std::pair<void *, Function *>,
779 FunctionAnalysisResultListT::iterator>
780 FunctionAnalysisResultMapT;
782 /// \brief Map from an analysis ID and function to a particular cached
784 FunctionAnalysisResultMapT FunctionAnalysisResults;
787 /// \brief A module analysis which acts as a proxy for a function analysis
790 /// This primarily proxies invalidation information from the module analysis
791 /// manager and module pass manager to a function analysis manager. You should
792 /// never use a function analysis manager from within (transitively) a module
793 /// pass manager unless your parent module pass has received a proxy result
795 class FunctionAnalysisManagerModuleProxy {
799 static void *ID() { return (void *)&PassID; }
801 explicit FunctionAnalysisManagerModuleProxy(FunctionAnalysisManager &FAM)
803 // We have to explicitly define all the special member functions because MSVC
804 // refuses to generate them.
805 FunctionAnalysisManagerModuleProxy(
806 const FunctionAnalysisManagerModuleProxy &Arg)
808 FunctionAnalysisManagerModuleProxy(FunctionAnalysisManagerModuleProxy &&Arg)
809 : FAM(std::move(Arg.FAM)) {}
810 FunctionAnalysisManagerModuleProxy &
811 operator=(FunctionAnalysisManagerModuleProxy RHS) {
812 std::swap(*this, RHS);
816 /// \brief Run the analysis pass and create our proxy result object.
818 /// This doesn't do any interesting work, it is primarily used to insert our
819 /// proxy result object into the module analysis cache so that we can proxy
820 /// invalidation to the function analysis manager.
822 /// In debug builds, it will also assert that the analysis manager is empty
823 /// as no queries should arrive at the function analysis manager prior to
824 /// this analysis being requested.
825 Result run(Module *M);
830 FunctionAnalysisManager *FAM;
833 /// \brief The result proxy object for the
834 /// \c FunctionAnalysisManagerModuleProxy.
836 /// See its documentation for more information.
837 class FunctionAnalysisManagerModuleProxy::Result {
839 explicit Result(FunctionAnalysisManager &FAM) : FAM(&FAM) {}
840 // We have to explicitly define all the special member functions because MSVC
841 // refuses to generate them.
842 Result(const Result &Arg) : FAM(Arg.FAM) {}
843 Result(Result &&Arg) : FAM(std::move(Arg.FAM)) {}
844 Result &operator=(Result RHS) {
845 std::swap(*this, RHS);
850 /// \brief Accessor for the \c FunctionAnalysisManager.
851 FunctionAnalysisManager &getManager() { return *FAM; }
853 /// \brief Handler for invalidation of the module.
855 /// If this analysis itself is preserved, then we assume that the set of \c
856 /// Function objects in the \c Module hasn't changed and thus we don't need
857 /// to invalidate *all* cached data associated with a \c Function* in the \c
858 /// FunctionAnalysisManager.
860 /// Regardless of whether this analysis is marked as preserved, all of the
861 /// analyses in the \c FunctionAnalysisManager are potentially invalidated
862 /// based on the set of preserved analyses.
863 bool invalidate(Module *M, const PreservedAnalyses &PA);
866 FunctionAnalysisManager *FAM;
869 /// \brief A function analysis which acts as a proxy for a module analysis
872 /// This primarily provides an accessor to a parent module analysis manager to
873 /// function passes. Only the const interface of the module analysis manager is
874 /// provided to indicate that once inside of a function analysis pass you
875 /// cannot request a module analysis to actually run. Instead, the user must
876 /// rely on the \c getCachedResult API.
878 /// This proxy *doesn't* manage the invalidation in any way. That is handled by
879 /// the recursive return path of each layer of the pass manager and the
880 /// returned PreservedAnalysis set.
881 class ModuleAnalysisManagerFunctionProxy {
883 /// \brief Result proxy object for \c ModuleAnalysisManagerFunctionProxy.
886 explicit Result(const ModuleAnalysisManager &MAM) : MAM(&MAM) {}
887 // We have to explicitly define all the special member functions because
888 // MSVC refuses to generate them.
889 Result(const Result &Arg) : MAM(Arg.MAM) {}
890 Result(Result &&Arg) : MAM(std::move(Arg.MAM)) {}
891 Result &operator=(Result RHS) {
892 std::swap(*this, RHS);
896 const ModuleAnalysisManager &getManager() const { return *MAM; }
898 /// \brief Handle invalidation by ignoring it, this pass is immutable.
899 bool invalidate(Function *) { return false; }
902 const ModuleAnalysisManager *MAM;
905 static void *ID() { return (void *)&PassID; }
907 ModuleAnalysisManagerFunctionProxy(const ModuleAnalysisManager &MAM)
909 // We have to explicitly define all the special member functions because MSVC
910 // refuses to generate them.
911 ModuleAnalysisManagerFunctionProxy(
912 const ModuleAnalysisManagerFunctionProxy &Arg)
914 ModuleAnalysisManagerFunctionProxy(ModuleAnalysisManagerFunctionProxy &&Arg)
915 : MAM(std::move(Arg.MAM)) {}
916 ModuleAnalysisManagerFunctionProxy &
917 operator=(ModuleAnalysisManagerFunctionProxy RHS) {
918 std::swap(*this, RHS);
922 /// \brief Run the analysis pass and create our proxy result object.
923 /// Nothing to see here, it just forwards the \c MAM reference into the
925 Result run(Function *) { return Result(*MAM); }
930 const ModuleAnalysisManager *MAM;
933 /// \brief Trivial adaptor that maps from a module to its functions.
935 /// Designed to allow composition of a FunctionPass(Manager) and
936 /// a ModulePassManager. Note that if this pass is constructed with a pointer
937 /// to a \c ModuleAnalysisManager it will run the
938 /// \c FunctionAnalysisManagerModuleProxy analysis prior to running the function
939 /// pass over the module to enable a \c FunctionAnalysisManager to be used
940 /// within this run safely.
941 template <typename FunctionPassT> class ModuleToFunctionPassAdaptor {
943 explicit ModuleToFunctionPassAdaptor(FunctionPassT Pass)
944 : Pass(std::move(Pass)) {}
945 // We have to explicitly define all the special member functions because MSVC
946 // refuses to generate them.
947 ModuleToFunctionPassAdaptor(const ModuleToFunctionPassAdaptor &Arg)
949 ModuleToFunctionPassAdaptor(ModuleToFunctionPassAdaptor &&Arg)
950 : Pass(std::move(Arg.Pass)) {}
951 ModuleToFunctionPassAdaptor &operator=(ModuleToFunctionPassAdaptor RHS) {
952 std::swap(*this, RHS);
956 /// \brief Runs the function pass across every function in the module.
957 PreservedAnalyses run(Module *M, ModuleAnalysisManager *AM) {
958 FunctionAnalysisManager *FAM = 0;
960 // Setup the function analysis manager from its proxy.
961 FAM = &AM->getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
963 PreservedAnalyses PA = PreservedAnalyses::all();
964 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
965 PreservedAnalyses PassPA = Pass.run(I, FAM);
967 // We know that the function pass couldn't have invalidated any other
968 // function's analyses (that's the contract of a function pass), so
969 // directly handle the function analysis manager's invalidation here.
971 FAM->invalidate(I, PassPA);
973 // Then intersect the preserved set so that invalidation of module
974 // analyses will eventually occur when the module pass completes.
975 PA.intersect(std::move(PassPA));
978 // By definition we preserve the proxy. This precludes *any* invalidation
979 // of function analyses by the proxy, but that's OK because we've taken
980 // care to invalidate analyses in the function analysis manager
981 // incrementally above.
982 PA.preserve<FunctionAnalysisManagerModuleProxy>();
986 static StringRef name() { return "ModuleToFunctionPassAdaptor"; }
992 /// \brief A function to deduce a function pass type and wrap it in the
993 /// templated adaptor.
994 template <typename FunctionPassT>
995 ModuleToFunctionPassAdaptor<FunctionPassT>
996 createModuleToFunctionPassAdaptor(FunctionPassT Pass) {
997 return std::move(ModuleToFunctionPassAdaptor<FunctionPassT>(std::move(Pass)));