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_PASSMANAGER_H
39 #define LLVM_IR_PASSMANAGER_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 friend void swap(PreservedAnalyses &LHS, PreservedAnalyses &RHS) {
76 swap(LHS.PreservedPassIDs, RHS.PreservedPassIDs);
78 PreservedAnalyses &operator=(PreservedAnalyses RHS) {
83 /// \brief Convenience factory function for the empty preserved set.
84 static PreservedAnalyses none() { return PreservedAnalyses(); }
86 /// \brief Construct a special preserved set that preserves all passes.
87 static PreservedAnalyses all() {
89 PA.PreservedPassIDs.insert((void *)AllPassesID);
93 /// \brief Mark a particular pass as preserved, adding it to the set.
94 template <typename PassT> void preserve() {
95 if (!areAllPreserved())
96 PreservedPassIDs.insert(PassT::ID());
99 /// \brief Intersect this set with another in place.
101 /// This is a mutating operation on this preserved set, removing all
102 /// preserved passes which are not also preserved in the argument.
103 void intersect(const PreservedAnalyses &Arg) {
104 if (Arg.areAllPreserved())
106 if (areAllPreserved()) {
107 PreservedPassIDs = Arg.PreservedPassIDs;
110 for (SmallPtrSet<void *, 2>::const_iterator I = PreservedPassIDs.begin(),
111 E = PreservedPassIDs.end();
113 if (!Arg.PreservedPassIDs.count(*I))
114 PreservedPassIDs.erase(*I);
117 /// \brief Intersect this set with a temporary other set in place.
119 /// This is a mutating operation on this preserved set, removing all
120 /// preserved passes which are not also preserved in the argument.
121 void intersect(PreservedAnalyses &&Arg) {
122 if (Arg.areAllPreserved())
124 if (areAllPreserved()) {
125 PreservedPassIDs = std::move(Arg.PreservedPassIDs);
128 for (SmallPtrSet<void *, 2>::const_iterator I = PreservedPassIDs.begin(),
129 E = PreservedPassIDs.end();
131 if (!Arg.PreservedPassIDs.count(*I))
132 PreservedPassIDs.erase(*I);
135 /// \brief Query whether a pass is marked as preserved by this set.
136 template <typename PassT> bool preserved() const {
137 return preserved(PassT::ID());
140 /// \brief Query whether an abstract pass ID is marked as preserved by this
142 bool preserved(void *PassID) const {
143 return PreservedPassIDs.count((void *)AllPassesID) ||
144 PreservedPassIDs.count(PassID);
148 // Note that this must not be -1 or -2 as those are already used by the
150 static const uintptr_t AllPassesID = (intptr_t)(-3);
152 bool areAllPreserved() const {
153 return PreservedPassIDs.count((void *)AllPassesID);
156 SmallPtrSet<void *, 2> PreservedPassIDs;
159 /// \brief Implementation details of the pass manager interfaces.
162 /// \brief Template for the abstract base class used to dispatch
163 /// polymorphically over pass objects.
164 template <typename IRUnitT, typename AnalysisManagerT> struct PassConcept {
165 // Boiler plate necessary for the container of derived classes.
166 virtual ~PassConcept() {}
168 /// \brief The polymorphic API which runs the pass over a given IR entity.
170 /// Note that actual pass object can omit the analysis manager argument if
171 /// desired. Also that the analysis manager may be null if there is no
172 /// analysis manager in the pass pipeline.
173 virtual PreservedAnalyses run(IRUnitT IR, AnalysisManagerT *AM) = 0;
175 /// \brief Polymorphic method to access the name of a pass.
176 virtual StringRef name() = 0;
179 /// \brief SFINAE metafunction for computing whether \c PassT has a run method
180 /// accepting an \c AnalysisManagerT.
181 template <typename IRUnitT, typename AnalysisManagerT, typename PassT,
183 class PassRunAcceptsAnalysisManager {
184 typedef char SmallType;
189 template <typename T, ResultT (T::*)(IRUnitT, AnalysisManagerT *)>
192 template <typename T> static SmallType f(Checker<T, &T::run> *);
193 template <typename T> static BigType f(...);
196 enum { Value = sizeof(f<PassT>(nullptr)) == sizeof(SmallType) };
199 /// \brief A template wrapper used to implement the polymorphic API.
201 /// Can be instantiated for any object which provides a \c run method accepting
202 /// an \c IRUnitT. It requires the pass to be a copyable object. When the
203 /// \c run method also accepts an \c AnalysisManagerT*, we pass it along.
204 template <typename IRUnitT, typename AnalysisManagerT, typename PassT,
205 bool AcceptsAnalysisManager = PassRunAcceptsAnalysisManager<
206 IRUnitT, AnalysisManagerT, PassT, PreservedAnalyses>::Value>
209 /// \brief Specialization of \c PassModel for passes that accept an analyis
211 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
212 struct PassModel<IRUnitT, AnalysisManagerT, PassT, true>
213 : PassConcept<IRUnitT, AnalysisManagerT> {
214 explicit PassModel(PassT Pass) : Pass(std::move(Pass)) {}
215 // We have to explicitly define all the special member functions because MSVC
216 // refuses to generate them.
217 PassModel(const PassModel &Arg) : Pass(Arg.Pass) {}
218 PassModel(PassModel &&Arg) : Pass(std::move(Arg.Pass)) {}
219 friend void swap(PassModel &LHS, PassModel &RHS) {
221 swap(LHS.Pass, RHS.Pass);
223 PassModel &operator=(PassModel RHS) {
228 PreservedAnalyses run(IRUnitT IR, AnalysisManagerT *AM) override {
229 return Pass.run(IR, AM);
231 StringRef name() override { return PassT::name(); }
235 /// \brief Specialization of \c PassModel for passes that accept an analyis
237 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
238 struct PassModel<IRUnitT, AnalysisManagerT, PassT, false>
239 : PassConcept<IRUnitT, AnalysisManagerT> {
240 explicit PassModel(PassT Pass) : Pass(std::move(Pass)) {}
241 // We have to explicitly define all the special member functions because MSVC
242 // refuses to generate them.
243 PassModel(const PassModel &Arg) : Pass(Arg.Pass) {}
244 PassModel(PassModel &&Arg) : Pass(std::move(Arg.Pass)) {}
245 friend void swap(PassModel &LHS, PassModel &RHS) {
247 swap(LHS.Pass, RHS.Pass);
249 PassModel &operator=(PassModel RHS) {
254 PreservedAnalyses run(IRUnitT IR, AnalysisManagerT *AM) override {
257 StringRef name() override { return PassT::name(); }
261 /// \brief Abstract concept of an analysis result.
263 /// This concept is parameterized over the IR unit that this result pertains
265 template <typename IRUnitT> struct AnalysisResultConcept {
266 virtual ~AnalysisResultConcept() {}
268 /// \brief Method to try and mark a result as invalid.
270 /// When the outer analysis manager detects a change in some underlying
271 /// unit of the IR, it will call this method on all of the results cached.
273 /// This method also receives a set of preserved analyses which can be used
274 /// to avoid invalidation because the pass which changed the underlying IR
275 /// took care to update or preserve the analysis result in some way.
277 /// \returns true if the result is indeed invalid (the default).
278 virtual bool invalidate(IRUnitT IR, const PreservedAnalyses &PA) = 0;
281 /// \brief SFINAE metafunction for computing whether \c ResultT provides an
282 /// \c invalidate member function.
283 template <typename IRUnitT, typename ResultT> class ResultHasInvalidateMethod {
284 typedef char SmallType;
289 template <typename T, bool (T::*)(IRUnitT, const PreservedAnalyses &)>
292 template <typename T> static SmallType f(Checker<T, &T::invalidate> *);
293 template <typename T> static BigType f(...);
296 enum { Value = sizeof(f<ResultT>(nullptr)) == sizeof(SmallType) };
299 /// \brief Wrapper to model the analysis result concept.
301 /// By default, this will implement the invalidate method with a trivial
302 /// implementation so that the actual analysis result doesn't need to provide
303 /// an invalidation handler. It is only selected when the invalidation handler
304 /// is not part of the ResultT's interface.
305 template <typename IRUnitT, typename PassT, typename ResultT,
306 bool HasInvalidateHandler =
307 ResultHasInvalidateMethod<IRUnitT, ResultT>::Value>
308 struct AnalysisResultModel;
310 /// \brief Specialization of \c AnalysisResultModel which provides the default
311 /// invalidate functionality.
312 template <typename IRUnitT, typename PassT, typename ResultT>
313 struct AnalysisResultModel<IRUnitT, PassT, ResultT, false>
314 : AnalysisResultConcept<IRUnitT> {
315 explicit AnalysisResultModel(ResultT Result) : Result(std::move(Result)) {}
316 // We have to explicitly define all the special member functions because MSVC
317 // refuses to generate them.
318 AnalysisResultModel(const AnalysisResultModel &Arg) : Result(Arg.Result) {}
319 AnalysisResultModel(AnalysisResultModel &&Arg)
320 : Result(std::move(Arg.Result)) {}
321 friend void swap(AnalysisResultModel &LHS, AnalysisResultModel &RHS) {
323 swap(LHS.Result, RHS.Result);
325 AnalysisResultModel &operator=(AnalysisResultModel RHS) {
330 /// \brief The model bases invalidation solely on being in the preserved set.
332 // FIXME: We should actually use two different concepts for analysis results
333 // rather than two different models, and avoid the indirect function call for
334 // ones that use the trivial behavior.
335 bool invalidate(IRUnitT, const PreservedAnalyses &PA) override {
336 return !PA.preserved(PassT::ID());
342 /// \brief Specialization of \c AnalysisResultModel which delegates invalidate
343 /// handling to \c ResultT.
344 template <typename IRUnitT, typename PassT, typename ResultT>
345 struct AnalysisResultModel<IRUnitT, PassT, ResultT, true>
346 : AnalysisResultConcept<IRUnitT> {
347 explicit AnalysisResultModel(ResultT Result) : Result(std::move(Result)) {}
348 // We have to explicitly define all the special member functions because MSVC
349 // refuses to generate them.
350 AnalysisResultModel(const AnalysisResultModel &Arg) : Result(Arg.Result) {}
351 AnalysisResultModel(AnalysisResultModel &&Arg)
352 : Result(std::move(Arg.Result)) {}
353 friend void swap(AnalysisResultModel &LHS, AnalysisResultModel &RHS) {
355 swap(LHS.Result, RHS.Result);
357 AnalysisResultModel &operator=(AnalysisResultModel RHS) {
362 /// \brief The model delegates to the \c ResultT method.
363 bool invalidate(IRUnitT IR, const PreservedAnalyses &PA) override {
364 return Result.invalidate(IR, PA);
370 /// \brief Abstract concept of an analysis pass.
372 /// This concept is parameterized over the IR unit that it can run over and
373 /// produce an analysis result.
374 template <typename IRUnitT, typename AnalysisManagerT>
375 struct AnalysisPassConcept {
376 virtual ~AnalysisPassConcept() {}
378 /// \brief Method to run this analysis over a unit of IR.
379 /// \returns A unique_ptr to the analysis result object to be queried by
381 virtual std::unique_ptr<AnalysisResultConcept<IRUnitT>>
382 run(IRUnitT IR, AnalysisManagerT *AM) = 0;
385 /// \brief Wrapper to model the analysis pass concept.
387 /// Can wrap any type which implements a suitable \c run method. The method
388 /// must accept the IRUnitT as an argument and produce an object which can be
389 /// wrapped in a \c AnalysisResultModel.
390 template <typename IRUnitT, typename AnalysisManagerT, typename PassT,
391 bool AcceptsAnalysisManager = PassRunAcceptsAnalysisManager<
392 IRUnitT, AnalysisManagerT, PassT, typename PassT::Result>::Value>
393 struct AnalysisPassModel;
395 /// \brief Specialization of \c AnalysisPassModel which passes an
396 /// \c AnalysisManager to PassT's run method.
397 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
398 struct AnalysisPassModel<IRUnitT, AnalysisManagerT, PassT, true>
399 : AnalysisPassConcept<IRUnitT, AnalysisManagerT> {
400 explicit AnalysisPassModel(PassT Pass) : Pass(std::move(Pass)) {}
401 // We have to explicitly define all the special member functions because MSVC
402 // refuses to generate them.
403 AnalysisPassModel(const AnalysisPassModel &Arg) : Pass(Arg.Pass) {}
404 AnalysisPassModel(AnalysisPassModel &&Arg) : Pass(std::move(Arg.Pass)) {}
405 friend void swap(AnalysisPassModel &LHS, AnalysisPassModel &RHS) {
407 swap(LHS.Pass, RHS.Pass);
409 AnalysisPassModel &operator=(AnalysisPassModel RHS) {
414 // FIXME: Replace PassT::Result with type traits when we use C++11.
415 typedef AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
418 /// \brief The model delegates to the \c PassT::run method.
420 /// The return is wrapped in an \c AnalysisResultModel.
421 std::unique_ptr<AnalysisResultConcept<IRUnitT>>
422 run(IRUnitT IR, AnalysisManagerT *AM) override {
423 return make_unique<ResultModelT>(Pass.run(IR, AM));
429 /// \brief Specialization of \c AnalysisPassModel which does not pass an
430 /// \c AnalysisManager to PassT's run method.
431 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
432 struct AnalysisPassModel<IRUnitT, AnalysisManagerT, PassT, false>
433 : AnalysisPassConcept<IRUnitT, AnalysisManagerT> {
434 explicit AnalysisPassModel(PassT Pass) : Pass(std::move(Pass)) {}
435 // We have to explicitly define all the special member functions because MSVC
436 // refuses to generate them.
437 AnalysisPassModel(const AnalysisPassModel &Arg) : Pass(Arg.Pass) {}
438 AnalysisPassModel(AnalysisPassModel &&Arg) : Pass(std::move(Arg.Pass)) {}
439 friend void swap(AnalysisPassModel &LHS, AnalysisPassModel &RHS) {
441 swap(LHS.Pass, RHS.Pass);
443 AnalysisPassModel &operator=(AnalysisPassModel RHS) {
448 // FIXME: Replace PassT::Result with type traits when we use C++11.
449 typedef AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
452 /// \brief The model delegates to the \c PassT::run method.
454 /// The return is wrapped in an \c AnalysisResultModel.
455 std::unique_ptr<AnalysisResultConcept<IRUnitT>>
456 run(IRUnitT IR, AnalysisManagerT *) override {
457 return make_unique<ResultModelT>(Pass.run(IR));
463 } // End namespace detail
465 class ModuleAnalysisManager;
467 class ModulePassManager {
469 // We have to explicitly define all the special member functions because MSVC
470 // refuses to generate them.
471 ModulePassManager() {}
472 ModulePassManager(ModulePassManager &&Arg) : Passes(std::move(Arg.Passes)) {}
473 ModulePassManager &operator=(ModulePassManager &&RHS) {
474 Passes = std::move(RHS.Passes);
478 /// \brief Run all of the module passes in this module pass manager over
481 /// This method should only be called for a single module as there is the
482 /// expectation that the lifetime of a pass is bounded to that of a module.
483 PreservedAnalyses run(Module *M, ModuleAnalysisManager *AM = nullptr);
485 template <typename ModulePassT> void addPass(ModulePassT Pass) {
486 Passes.emplace_back(new ModulePassModel<ModulePassT>(std::move(Pass)));
489 static StringRef name() { return "ModulePassManager"; }
492 // Pull in the concept type and model template specialized for modules.
493 typedef detail::PassConcept<Module *, ModuleAnalysisManager>
495 template <typename PassT>
496 struct ModulePassModel
497 : detail::PassModel<Module *, ModuleAnalysisManager, PassT> {
498 ModulePassModel(PassT Pass)
499 : detail::PassModel<Module *, ModuleAnalysisManager, PassT>(
503 ModulePassManager(const ModulePassManager &) LLVM_DELETED_FUNCTION;
504 ModulePassManager &operator=(const ModulePassManager &) LLVM_DELETED_FUNCTION;
506 std::vector<std::unique_ptr<ModulePassConcept>> Passes;
509 class FunctionAnalysisManager;
511 class FunctionPassManager {
513 // We have to explicitly define all the special member functions because MSVC
514 // refuses to generate them.
515 FunctionPassManager() {}
516 FunctionPassManager(FunctionPassManager &&Arg)
517 : Passes(std::move(Arg.Passes)) {}
518 FunctionPassManager &operator=(FunctionPassManager &&RHS) {
519 Passes = std::move(RHS.Passes);
523 template <typename FunctionPassT> void addPass(FunctionPassT Pass) {
524 Passes.emplace_back(new FunctionPassModel<FunctionPassT>(std::move(Pass)));
527 PreservedAnalyses run(Function *F, FunctionAnalysisManager *AM = nullptr);
529 static StringRef name() { return "FunctionPassManager"; }
532 // Pull in the concept type and model template specialized for functions.
533 typedef detail::PassConcept<Function *, FunctionAnalysisManager>
535 template <typename PassT>
536 struct FunctionPassModel
537 : detail::PassModel<Function *, FunctionAnalysisManager, PassT> {
538 FunctionPassModel(PassT Pass)
539 : detail::PassModel<Function *, FunctionAnalysisManager, PassT>(
543 FunctionPassManager(const FunctionPassManager &) LLVM_DELETED_FUNCTION;
544 FunctionPassManager &
545 operator=(const FunctionPassManager &) LLVM_DELETED_FUNCTION;
547 std::vector<std::unique_ptr<FunctionPassConcept>> Passes;
552 /// \brief A CRTP base used to implement analysis managers.
554 /// This class template serves as the boiler plate of an analysis manager. Any
555 /// analysis manager can be implemented on top of this base class. Any
556 /// implementation will be required to provide specific hooks:
559 /// - getCachedResultImpl
562 /// The details of the call pattern are within.
563 template <typename DerivedT, typename IRUnitT> class AnalysisManagerBase {
564 DerivedT *derived_this() { return static_cast<DerivedT *>(this); }
565 const DerivedT *derived_this() const {
566 return static_cast<const DerivedT *>(this);
569 AnalysisManagerBase(const AnalysisManagerBase &) LLVM_DELETED_FUNCTION;
570 AnalysisManagerBase &
571 operator=(const AnalysisManagerBase &) LLVM_DELETED_FUNCTION;
574 typedef detail::AnalysisResultConcept<IRUnitT> ResultConceptT;
575 typedef detail::AnalysisPassConcept<IRUnitT, DerivedT> PassConceptT;
577 // FIXME: Provide template aliases for the models when we're using C++11 in
578 // a mode supporting them.
580 // We have to explicitly define all the special member functions because MSVC
581 // refuses to generate them.
582 AnalysisManagerBase() {}
583 AnalysisManagerBase(AnalysisManagerBase &&Arg)
584 : AnalysisPasses(std::move(Arg.AnalysisPasses)) {}
585 AnalysisManagerBase &operator=(AnalysisManagerBase &&RHS) {
586 AnalysisPasses = std::move(RHS.AnalysisPasses);
591 /// \brief Get the result of an analysis pass for this module.
593 /// If there is not a valid cached result in the manager already, this will
594 /// re-run the analysis to produce a valid result.
595 template <typename PassT> typename PassT::Result &getResult(IRUnitT IR) {
596 assert(AnalysisPasses.count(PassT::ID()) &&
597 "This analysis pass was not registered prior to being queried");
599 ResultConceptT &ResultConcept =
600 derived_this()->getResultImpl(PassT::ID(), IR);
601 typedef detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
603 return static_cast<ResultModelT &>(ResultConcept).Result;
606 /// \brief Get the cached result of an analysis pass for this module.
608 /// This method never runs the analysis.
610 /// \returns null if there is no cached result.
611 template <typename PassT>
612 typename PassT::Result *getCachedResult(IRUnitT IR) const {
613 assert(AnalysisPasses.count(PassT::ID()) &&
614 "This analysis pass was not registered prior to being queried");
616 ResultConceptT *ResultConcept =
617 derived_this()->getCachedResultImpl(PassT::ID(), IR);
621 typedef detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
623 return &static_cast<ResultModelT *>(ResultConcept)->Result;
626 /// \brief Register an analysis pass with the manager.
628 /// This provides an initialized and set-up analysis pass to the analysis
629 /// manager. Whomever is setting up analysis passes must use this to populate
630 /// the manager with all of the analysis passes available.
631 template <typename PassT> void registerPass(PassT Pass) {
632 assert(!AnalysisPasses.count(PassT::ID()) &&
633 "Registered the same analysis pass twice!");
634 typedef detail::AnalysisPassModel<IRUnitT, DerivedT, PassT> PassModelT;
635 AnalysisPasses[PassT::ID()].reset(new PassModelT(std::move(Pass)));
638 /// \brief Invalidate a specific analysis pass for an IR module.
640 /// Note that the analysis result can disregard invalidation.
641 template <typename PassT> void invalidate(Module *M) {
642 assert(AnalysisPasses.count(PassT::ID()) &&
643 "This analysis pass was not registered prior to being invalidated");
644 derived_this()->invalidateImpl(PassT::ID(), M);
647 /// \brief Invalidate analyses cached for an IR unit.
649 /// Walk through all of the analyses pertaining to this unit of IR and
650 /// invalidate them unless they are preserved by the PreservedAnalyses set.
651 void invalidate(IRUnitT IR, const PreservedAnalyses &PA) {
652 derived_this()->invalidateImpl(IR, PA);
656 /// \brief Lookup a registered analysis pass.
657 PassConceptT &lookupPass(void *PassID) {
658 typename AnalysisPassMapT::iterator PI = AnalysisPasses.find(PassID);
659 assert(PI != AnalysisPasses.end() &&
660 "Analysis passes must be registered prior to being queried!");
664 /// \brief Lookup a registered analysis pass.
665 const PassConceptT &lookupPass(void *PassID) const {
666 typename AnalysisPassMapT::const_iterator PI = AnalysisPasses.find(PassID);
667 assert(PI != AnalysisPasses.end() &&
668 "Analysis passes must be registered prior to being queried!");
673 /// \brief Map type from module analysis pass ID to pass concept pointer.
674 typedef DenseMap<void *, std::unique_ptr<PassConceptT>> AnalysisPassMapT;
676 /// \brief Collection of module analysis passes, indexed by ID.
677 AnalysisPassMapT AnalysisPasses;
680 } // End namespace detail
682 /// \brief A module analysis pass manager with lazy running and caching of
684 class ModuleAnalysisManager
685 : public detail::AnalysisManagerBase<ModuleAnalysisManager, Module *> {
686 friend class detail::AnalysisManagerBase<ModuleAnalysisManager, Module *>;
687 typedef detail::AnalysisManagerBase<ModuleAnalysisManager, Module *> BaseT;
688 typedef BaseT::ResultConceptT ResultConceptT;
689 typedef BaseT::PassConceptT PassConceptT;
692 // We have to explicitly define all the special member functions because MSVC
693 // refuses to generate them.
694 ModuleAnalysisManager() {}
695 ModuleAnalysisManager(ModuleAnalysisManager &&Arg)
696 : BaseT(std::move(static_cast<BaseT &>(Arg))),
697 ModuleAnalysisResults(std::move(Arg.ModuleAnalysisResults)) {}
698 ModuleAnalysisManager &operator=(ModuleAnalysisManager &&RHS) {
699 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
700 ModuleAnalysisResults = std::move(RHS.ModuleAnalysisResults);
705 ModuleAnalysisManager(const ModuleAnalysisManager &) LLVM_DELETED_FUNCTION;
706 ModuleAnalysisManager &
707 operator=(const ModuleAnalysisManager &) LLVM_DELETED_FUNCTION;
709 /// \brief Get a module pass result, running the pass if necessary.
710 ResultConceptT &getResultImpl(void *PassID, Module *M);
712 /// \brief Get a cached module pass result or return null.
713 ResultConceptT *getCachedResultImpl(void *PassID, Module *M) const;
715 /// \brief Invalidate a module pass result.
716 void invalidateImpl(void *PassID, Module *M);
718 /// \brief Invalidate results across a module.
719 void invalidateImpl(Module *M, const PreservedAnalyses &PA);
721 /// \brief Map type from module analysis pass ID to pass result concept
723 typedef DenseMap<void *,
724 std::unique_ptr<detail::AnalysisResultConcept<Module *>>>
725 ModuleAnalysisResultMapT;
727 /// \brief Cache of computed module analysis results for this module.
728 ModuleAnalysisResultMapT ModuleAnalysisResults;
731 /// \brief A function analysis manager to coordinate and cache analyses run over
733 class FunctionAnalysisManager
734 : public detail::AnalysisManagerBase<FunctionAnalysisManager, Function *> {
735 friend class detail::AnalysisManagerBase<FunctionAnalysisManager, Function *>;
736 typedef detail::AnalysisManagerBase<FunctionAnalysisManager, Function *>
738 typedef BaseT::ResultConceptT ResultConceptT;
739 typedef BaseT::PassConceptT PassConceptT;
742 // Most public APIs are inherited from the CRTP base class.
744 // We have to explicitly define all the special member functions because MSVC
745 // refuses to generate them.
746 FunctionAnalysisManager() {}
747 FunctionAnalysisManager(FunctionAnalysisManager &&Arg)
748 : BaseT(std::move(static_cast<BaseT &>(Arg))),
749 FunctionAnalysisResults(std::move(Arg.FunctionAnalysisResults)) {}
750 FunctionAnalysisManager &operator=(FunctionAnalysisManager &&RHS) {
751 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
752 FunctionAnalysisResults = std::move(RHS.FunctionAnalysisResults);
756 /// \brief Returns true if the analysis manager has an empty results cache.
759 /// \brief Clear the function analysis result cache.
761 /// This routine allows cleaning up when the set of functions itself has
762 /// potentially changed, and thus we can't even look up a a result and
763 /// invalidate it directly. Notably, this does *not* call invalidate
764 /// functions as there is nothing to be done for them.
768 FunctionAnalysisManager(const FunctionAnalysisManager &)
769 LLVM_DELETED_FUNCTION;
770 FunctionAnalysisManager &
771 operator=(const FunctionAnalysisManager &) LLVM_DELETED_FUNCTION;
773 /// \brief Get a function pass result, running the pass if necessary.
774 ResultConceptT &getResultImpl(void *PassID, Function *F);
776 /// \brief Get a cached function pass result or return null.
777 ResultConceptT *getCachedResultImpl(void *PassID, Function *F) const;
779 /// \brief Invalidate a function pass result.
780 void invalidateImpl(void *PassID, Function *F);
782 /// \brief Invalidate the results for a function..
783 void invalidateImpl(Function *F, const PreservedAnalyses &PA);
785 /// \brief List of function analysis pass IDs and associated concept pointers.
787 /// Requires iterators to be valid across appending new entries and arbitrary
788 /// erases. Provides both the pass ID and concept pointer such that it is
789 /// half of a bijection and provides storage for the actual result concept.
790 typedef std::list<std::pair<
791 void *, std::unique_ptr<detail::AnalysisResultConcept<Function *>>>>
792 FunctionAnalysisResultListT;
794 /// \brief Map type from function pointer to our custom list type.
795 typedef DenseMap<Function *, FunctionAnalysisResultListT>
796 FunctionAnalysisResultListMapT;
798 /// \brief Map from function to a list of function analysis results.
800 /// Provides linear time removal of all analysis results for a function and
801 /// the ultimate storage for a particular cached analysis result.
802 FunctionAnalysisResultListMapT FunctionAnalysisResultLists;
804 /// \brief Map type from a pair of analysis ID and function pointer to an
805 /// iterator into a particular result list.
806 typedef DenseMap<std::pair<void *, Function *>,
807 FunctionAnalysisResultListT::iterator>
808 FunctionAnalysisResultMapT;
810 /// \brief Map from an analysis ID and function to a particular cached
812 FunctionAnalysisResultMapT FunctionAnalysisResults;
815 /// \brief A module analysis which acts as a proxy for a function analysis
818 /// This primarily proxies invalidation information from the module analysis
819 /// manager and module pass manager to a function analysis manager. You should
820 /// never use a function analysis manager from within (transitively) a module
821 /// pass manager unless your parent module pass has received a proxy result
823 class FunctionAnalysisManagerModuleProxy {
827 static void *ID() { return (void *)&PassID; }
829 explicit FunctionAnalysisManagerModuleProxy(FunctionAnalysisManager &FAM)
831 // We have to explicitly define all the special member functions because MSVC
832 // refuses to generate them.
833 FunctionAnalysisManagerModuleProxy(
834 const FunctionAnalysisManagerModuleProxy &Arg)
836 FunctionAnalysisManagerModuleProxy(FunctionAnalysisManagerModuleProxy &&Arg)
837 : FAM(std::move(Arg.FAM)) {}
838 FunctionAnalysisManagerModuleProxy &
839 operator=(FunctionAnalysisManagerModuleProxy RHS) {
840 std::swap(FAM, RHS.FAM);
844 /// \brief Run the analysis pass and create our proxy result object.
846 /// This doesn't do any interesting work, it is primarily used to insert our
847 /// proxy result object into the module analysis cache so that we can proxy
848 /// invalidation to the function analysis manager.
850 /// In debug builds, it will also assert that the analysis manager is empty
851 /// as no queries should arrive at the function analysis manager prior to
852 /// this analysis being requested.
853 Result run(Module *M);
858 FunctionAnalysisManager *FAM;
861 /// \brief The result proxy object for the
862 /// \c FunctionAnalysisManagerModuleProxy.
864 /// See its documentation for more information.
865 class FunctionAnalysisManagerModuleProxy::Result {
867 explicit Result(FunctionAnalysisManager &FAM) : FAM(&FAM) {}
868 // We have to explicitly define all the special member functions because MSVC
869 // refuses to generate them.
870 Result(const Result &Arg) : FAM(Arg.FAM) {}
871 Result(Result &&Arg) : FAM(std::move(Arg.FAM)) {}
872 Result &operator=(Result RHS) {
873 std::swap(FAM, RHS.FAM);
878 /// \brief Accessor for the \c FunctionAnalysisManager.
879 FunctionAnalysisManager &getManager() { return *FAM; }
881 /// \brief Handler for invalidation of the module.
883 /// If this analysis itself is preserved, then we assume that the set of \c
884 /// Function objects in the \c Module hasn't changed and thus we don't need
885 /// to invalidate *all* cached data associated with a \c Function* in the \c
886 /// FunctionAnalysisManager.
888 /// Regardless of whether this analysis is marked as preserved, all of the
889 /// analyses in the \c FunctionAnalysisManager are potentially invalidated
890 /// based on the set of preserved analyses.
891 bool invalidate(Module *M, const PreservedAnalyses &PA);
894 FunctionAnalysisManager *FAM;
897 /// \brief A function analysis which acts as a proxy for a module analysis
900 /// This primarily provides an accessor to a parent module analysis manager to
901 /// function passes. Only the const interface of the module analysis manager is
902 /// provided to indicate that once inside of a function analysis pass you
903 /// cannot request a module analysis to actually run. Instead, the user must
904 /// rely on the \c getCachedResult API.
906 /// This proxy *doesn't* manage the invalidation in any way. That is handled by
907 /// the recursive return path of each layer of the pass manager and the
908 /// returned PreservedAnalysis set.
909 class ModuleAnalysisManagerFunctionProxy {
911 /// \brief Result proxy object for \c ModuleAnalysisManagerFunctionProxy.
914 explicit Result(const ModuleAnalysisManager &MAM) : MAM(&MAM) {}
915 // We have to explicitly define all the special member functions because
916 // MSVC refuses to generate them.
917 Result(const Result &Arg) : MAM(Arg.MAM) {}
918 Result(Result &&Arg) : MAM(std::move(Arg.MAM)) {}
919 Result &operator=(Result RHS) {
920 std::swap(MAM, RHS.MAM);
924 const ModuleAnalysisManager &getManager() const { return *MAM; }
926 /// \brief Handle invalidation by ignoring it, this pass is immutable.
927 bool invalidate(Function *) { return false; }
930 const ModuleAnalysisManager *MAM;
933 static void *ID() { return (void *)&PassID; }
935 ModuleAnalysisManagerFunctionProxy(const ModuleAnalysisManager &MAM)
937 // We have to explicitly define all the special member functions because MSVC
938 // refuses to generate them.
939 ModuleAnalysisManagerFunctionProxy(
940 const ModuleAnalysisManagerFunctionProxy &Arg)
942 ModuleAnalysisManagerFunctionProxy(ModuleAnalysisManagerFunctionProxy &&Arg)
943 : MAM(std::move(Arg.MAM)) {}
944 ModuleAnalysisManagerFunctionProxy &
945 operator=(ModuleAnalysisManagerFunctionProxy RHS) {
946 std::swap(MAM, RHS.MAM);
950 /// \brief Run the analysis pass and create our proxy result object.
951 /// Nothing to see here, it just forwards the \c MAM reference into the
953 Result run(Function *) { return Result(*MAM); }
958 const ModuleAnalysisManager *MAM;
961 /// \brief Trivial adaptor that maps from a module to its functions.
963 /// Designed to allow composition of a FunctionPass(Manager) and
964 /// a ModulePassManager. Note that if this pass is constructed with a pointer
965 /// to a \c ModuleAnalysisManager it will run the
966 /// \c FunctionAnalysisManagerModuleProxy analysis prior to running the function
967 /// pass over the module to enable a \c FunctionAnalysisManager to be used
968 /// within this run safely.
969 template <typename FunctionPassT> class ModuleToFunctionPassAdaptor {
971 explicit ModuleToFunctionPassAdaptor(FunctionPassT Pass)
972 : Pass(std::move(Pass)) {}
973 // We have to explicitly define all the special member functions because MSVC
974 // refuses to generate them.
975 ModuleToFunctionPassAdaptor(const ModuleToFunctionPassAdaptor &Arg)
977 ModuleToFunctionPassAdaptor(ModuleToFunctionPassAdaptor &&Arg)
978 : Pass(std::move(Arg.Pass)) {}
979 friend void swap(ModuleToFunctionPassAdaptor &LHS, ModuleToFunctionPassAdaptor &RHS) {
981 swap(LHS.Pass, RHS.Pass);
983 ModuleToFunctionPassAdaptor &operator=(ModuleToFunctionPassAdaptor RHS) {
988 /// \brief Runs the function pass across every function in the module.
989 PreservedAnalyses run(Module *M, ModuleAnalysisManager *AM) {
990 FunctionAnalysisManager *FAM = nullptr;
992 // Setup the function analysis manager from its proxy.
993 FAM = &AM->getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
995 PreservedAnalyses PA = PreservedAnalyses::all();
996 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
997 PreservedAnalyses PassPA = Pass.run(I, FAM);
999 // We know that the function pass couldn't have invalidated any other
1000 // function's analyses (that's the contract of a function pass), so
1001 // directly handle the function analysis manager's invalidation here.
1003 FAM->invalidate(I, PassPA);
1005 // Then intersect the preserved set so that invalidation of module
1006 // analyses will eventually occur when the module pass completes.
1007 PA.intersect(std::move(PassPA));
1010 // By definition we preserve the proxy. This precludes *any* invalidation
1011 // of function analyses by the proxy, but that's OK because we've taken
1012 // care to invalidate analyses in the function analysis manager
1013 // incrementally above.
1014 PA.preserve<FunctionAnalysisManagerModuleProxy>();
1018 static StringRef name() { return "ModuleToFunctionPassAdaptor"; }
1024 /// \brief A function to deduce a function pass type and wrap it in the
1025 /// templated adaptor.
1026 template <typename FunctionPassT>
1027 ModuleToFunctionPassAdaptor<FunctionPassT>
1028 createModuleToFunctionPassAdaptor(FunctionPassT Pass) {
1029 return std::move(ModuleToFunctionPassAdaptor<FunctionPassT>(std::move(Pass)));