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/SmallPtrSet.h"
43 #include "llvm/ADT/polymorphic_ptr.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/Module.h"
46 #include "llvm/Support/type_traits.h"
55 /// \brief An abstract set of preserved analyses following a transformation pass
58 /// When a transformation pass is run, it can return a set of analyses whose
59 /// results were preserved by that transformation. The default set is "none",
60 /// and preserving analyses must be done explicitly.
62 /// There is also an explicit all state which can be used (for example) when
63 /// the IR is not mutated at all.
64 class PreservedAnalyses {
66 /// \brief Convenience factory function for the empty preserved set.
67 static PreservedAnalyses none() { return PreservedAnalyses(); }
69 /// \brief Construct a special preserved set that preserves all passes.
70 static PreservedAnalyses all() {
72 PA.PreservedPassIDs.insert((void *)AllPassesID);
76 PreservedAnalyses &operator=(PreservedAnalyses Arg) {
81 void swap(PreservedAnalyses &Arg) {
82 PreservedPassIDs.swap(Arg.PreservedPassIDs);
85 /// \brief Mark a particular pass as preserved, adding it to the set.
86 template <typename PassT> void preserve() {
87 if (!areAllPreserved())
88 PreservedPassIDs.insert(PassT::ID());
91 /// \brief Intersect this set with another in place.
93 /// This is a mutating operation on this preserved set, removing all
94 /// preserved passes which are not also preserved in the argument.
95 void intersect(const PreservedAnalyses &Arg) {
96 if (Arg.areAllPreserved())
98 if (areAllPreserved()) {
99 PreservedPassIDs = Arg.PreservedPassIDs;
102 for (SmallPtrSet<void *, 2>::const_iterator I = PreservedPassIDs.begin(),
103 E = PreservedPassIDs.end();
105 if (!Arg.PreservedPassIDs.count(*I))
106 PreservedPassIDs.erase(*I);
109 #if LLVM_HAS_RVALUE_REFERENCES
110 /// \brief Intersect this set with a temporary other set in place.
112 /// This is a mutating operation on this preserved set, removing all
113 /// preserved passes which are not also preserved in the argument.
114 void intersect(PreservedAnalyses &&Arg) {
115 if (Arg.areAllPreserved())
117 if (areAllPreserved()) {
118 PreservedPassIDs = std::move(Arg.PreservedPassIDs);
121 for (SmallPtrSet<void *, 2>::const_iterator I = PreservedPassIDs.begin(),
122 E = PreservedPassIDs.end();
124 if (!Arg.PreservedPassIDs.count(*I))
125 PreservedPassIDs.erase(*I);
129 /// \brief Query whether a pass is marked as preserved by this set.
130 template <typename PassT> bool preserved() const {
131 return preserved(PassT::ID());
134 /// \brief Query whether an abstract pass ID is marked as preserved by this
136 bool preserved(void *PassID) const {
137 return PreservedPassIDs.count((void *)AllPassesID) ||
138 PreservedPassIDs.count(PassID);
142 // Note that this must not be -1 or -2 as those are already used by the
144 static const uintptr_t AllPassesID = (intptr_t)-3;
146 bool areAllPreserved() const { return PreservedPassIDs.count((void *)AllPassesID); }
148 SmallPtrSet<void *, 2> PreservedPassIDs;
151 inline void swap(PreservedAnalyses &LHS, PreservedAnalyses &RHS) {
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() {}
163 virtual PassConcept *clone() = 0;
165 /// \brief The polymorphic API which runs the pass over a given IR entity.
167 /// Note that actual pass object can omit the analysis manager argument if
168 /// desired. Also that the analysis manager may be null if there is no
169 /// analysis manager in the pass pipeline.
170 virtual PreservedAnalyses run(IRUnitT IR, AnalysisManagerT *AM) = 0;
172 /// \brief Polymorphic method to access the name of a pass.
173 virtual StringRef name() = 0;
176 /// \brief SFINAE metafunction for computing whether \c PassT has a run method
177 /// accepting an \c AnalysisManagerT.
178 template <typename IRUnitT, typename AnalysisManagerT, typename PassT,
180 class PassRunAcceptsAnalysisManager {
181 typedef char SmallType;
182 struct BigType { char a, b; };
184 template <typename T, ResultT (T::*)(IRUnitT, AnalysisManagerT *)>
187 template <typename T> static SmallType f(Checker<T, &T::run> *);
188 template <typename T> static BigType f(...);
191 enum { Value = sizeof(f<PassT>(0)) == sizeof(SmallType) };
194 /// \brief A template wrapper used to implement the polymorphic API.
196 /// Can be instantiated for any object which provides a \c run method accepting
197 /// an \c IRUnitT. It requires the pass to be a copyable object. When the
198 /// \c run method also accepts an \c AnalysisManagerT*, we pass it along.
199 template <typename IRUnitT, typename AnalysisManagerT, typename PassT,
200 bool AcceptsAnalysisManager = PassRunAcceptsAnalysisManager<
201 IRUnitT, AnalysisManagerT, PassT, PreservedAnalyses>::Value>
204 /// \brief Specialization of \c PassModel for passes that accept an analyis
206 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
207 struct PassModel<IRUnitT, AnalysisManagerT, PassT,
208 true> : PassConcept<IRUnitT, AnalysisManagerT> {
209 PassModel(PassT Pass) : Pass(llvm_move(Pass)) {}
210 virtual PassModel *clone() { return new PassModel(Pass); }
211 virtual PreservedAnalyses run(IRUnitT IR, AnalysisManagerT *AM) {
212 return Pass.run(IR, AM);
214 virtual StringRef name() { return PassT::name(); }
218 /// \brief Specialization of \c PassModel for passes that accept an analyis
220 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
221 struct PassModel<IRUnitT, AnalysisManagerT, PassT,
222 false> : PassConcept<IRUnitT, AnalysisManagerT> {
223 PassModel(PassT Pass) : Pass(llvm_move(Pass)) {}
224 virtual PassModel *clone() { return new PassModel(Pass); }
225 virtual PreservedAnalyses run(IRUnitT IR, AnalysisManagerT *AM) {
228 virtual StringRef name() { return PassT::name(); }
232 /// \brief Abstract concept of an analysis result.
234 /// This concept is parameterized over the IR unit that this result pertains
236 template <typename IRUnitT> struct AnalysisResultConcept {
237 virtual ~AnalysisResultConcept() {}
238 virtual AnalysisResultConcept *clone() = 0;
240 /// \brief Method to try and mark a result as invalid.
242 /// When the outer analysis manager detects a change in some underlying
243 /// unit of the IR, it will call this method on all of the results cached.
245 /// This method also receives a set of preserved analyses which can be used
246 /// to avoid invalidation because the pass which changed the underlying IR
247 /// took care to update or preserve the analysis result in some way.
249 /// \returns true if the result is indeed invalid (the default).
250 virtual bool invalidate(IRUnitT IR, const PreservedAnalyses &PA) = 0;
253 /// \brief SFINAE metafunction for computing whether \c ResultT provides an
254 /// \c invalidate member function.
255 template <typename IRUnitT, typename ResultT> class ResultHasInvalidateMethod {
256 typedef char SmallType;
257 struct BigType { char a, b; };
259 template <typename T, bool (T::*)(IRUnitT, const PreservedAnalyses &)>
262 template <typename T> static SmallType f(Checker<T, &T::invalidate> *);
263 template <typename T> static BigType f(...);
266 enum { Value = sizeof(f<ResultT>(0)) == sizeof(SmallType) };
269 /// \brief Wrapper to model the analysis result concept.
271 /// By default, this will implement the invalidate method with a trivial
272 /// implementation so that the actual analysis result doesn't need to provide
273 /// an invalidation handler. It is only selected when the invalidation handler
274 /// is not part of the ResultT's interface.
275 template <typename IRUnitT, typename PassT, typename ResultT,
276 bool HasInvalidateHandler =
277 ResultHasInvalidateMethod<IRUnitT, ResultT>::Value>
278 struct AnalysisResultModel;
280 /// \brief Specialization of \c AnalysisResultModel which provides the default
281 /// invalidate functionality.
282 template <typename IRUnitT, typename PassT, typename ResultT>
283 struct AnalysisResultModel<IRUnitT, PassT, ResultT,
284 false> : AnalysisResultConcept<IRUnitT> {
285 AnalysisResultModel(ResultT Result) : Result(llvm_move(Result)) {}
286 virtual AnalysisResultModel *clone() {
287 return new AnalysisResultModel(Result);
290 /// \brief The model bases invalidation solely on being in the preserved set.
292 // FIXME: We should actually use two different concepts for analysis results
293 // rather than two different models, and avoid the indirect function call for
294 // ones that use the trivial behavior.
295 virtual bool invalidate(IRUnitT, const PreservedAnalyses &PA) {
296 return !PA.preserved(PassT::ID());
302 /// \brief Specialization of \c AnalysisResultModel which delegates invalidate
303 /// handling to \c ResultT.
304 template <typename IRUnitT, typename PassT, typename ResultT>
305 struct AnalysisResultModel<IRUnitT, PassT, ResultT,
306 true> : AnalysisResultConcept<IRUnitT> {
307 AnalysisResultModel(ResultT Result) : Result(llvm_move(Result)) {}
308 virtual AnalysisResultModel *clone() {
309 return new AnalysisResultModel(Result);
312 /// \brief The model delegates to the \c ResultT method.
313 virtual bool invalidate(IRUnitT IR, const PreservedAnalyses &PA) {
314 return Result.invalidate(IR, PA);
320 /// \brief Abstract concept of an analysis pass.
322 /// This concept is parameterized over the IR unit that it can run over and
323 /// produce an analysis result.
324 template <typename IRUnitT, typename AnalysisManagerT>
325 struct AnalysisPassConcept {
326 virtual ~AnalysisPassConcept() {}
327 virtual AnalysisPassConcept *clone() = 0;
329 /// \brief Method to run this analysis over a unit of IR.
330 /// \returns The analysis result object to be queried by users, the caller
332 virtual AnalysisResultConcept<IRUnitT> *run(IRUnitT IR,
333 AnalysisManagerT *AM) = 0;
336 /// \brief Wrapper to model the analysis pass concept.
338 /// Can wrap any type which implements a suitable \c run method. The method
339 /// must accept the IRUnitT as an argument and produce an object which can be
340 /// wrapped in a \c AnalysisResultModel.
341 template <typename IRUnitT, typename AnalysisManagerT, typename PassT,
342 bool AcceptsAnalysisManager = PassRunAcceptsAnalysisManager<
343 IRUnitT, AnalysisManagerT, PassT,
344 typename PassT::Result>::Value> struct AnalysisPassModel;
346 /// \brief Specialization of \c AnalysisPassModel which passes an
347 /// \c AnalysisManager to PassT's run method.
348 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
349 struct AnalysisPassModel<IRUnitT, AnalysisManagerT, PassT,
350 true> : AnalysisPassConcept<IRUnitT,
352 AnalysisPassModel(PassT Pass) : Pass(llvm_move(Pass)) {}
353 virtual AnalysisPassModel *clone() { return new AnalysisPassModel(Pass); }
355 // FIXME: Replace PassT::Result with type traits when we use C++11.
356 typedef AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
359 /// \brief The model delegates to the \c PassT::run method.
361 /// The return is wrapped in an \c AnalysisResultModel.
362 virtual ResultModelT *run(IRUnitT IR, AnalysisManagerT *AM) {
363 return new ResultModelT(Pass.run(IR, AM));
369 /// \brief Specialization of \c AnalysisPassModel which does not pass an
370 /// \c AnalysisManager to PassT's run method.
371 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
372 struct AnalysisPassModel<IRUnitT, AnalysisManagerT, PassT,
373 false> : AnalysisPassConcept<IRUnitT,
375 AnalysisPassModel(PassT Pass) : Pass(llvm_move(Pass)) {}
376 virtual AnalysisPassModel *clone() { return new AnalysisPassModel(Pass); }
378 // FIXME: Replace PassT::Result with type traits when we use C++11.
379 typedef AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
382 /// \brief The model delegates to the \c PassT::run method.
384 /// The return is wrapped in an \c AnalysisResultModel.
385 virtual ResultModelT *run(IRUnitT IR, AnalysisManagerT *) {
386 return new ResultModelT(Pass.run(IR));
394 class ModuleAnalysisManager;
396 class ModulePassManager {
398 explicit ModulePassManager() {}
400 /// \brief Run all of the module passes in this module pass manager over
403 /// This method should only be called for a single module as there is the
404 /// expectation that the lifetime of a pass is bounded to that of a module.
405 PreservedAnalyses run(Module *M, ModuleAnalysisManager *AM = 0);
407 template <typename ModulePassT> void addPass(ModulePassT Pass) {
408 Passes.push_back(new ModulePassModel<ModulePassT>(llvm_move(Pass)));
411 static StringRef name() { return "ModulePassManager"; }
414 // Pull in the concept type and model template specialized for modules.
415 typedef detail::PassConcept<Module *, ModuleAnalysisManager> ModulePassConcept;
416 template <typename PassT>
417 struct ModulePassModel
418 : detail::PassModel<Module *, ModuleAnalysisManager, PassT> {
419 ModulePassModel(PassT Pass)
420 : detail::PassModel<Module *, ModuleAnalysisManager, PassT>(Pass) {}
423 std::vector<polymorphic_ptr<ModulePassConcept> > Passes;
426 class FunctionAnalysisManager;
428 class FunctionPassManager {
430 explicit FunctionPassManager() {}
432 template <typename FunctionPassT> void addPass(FunctionPassT Pass) {
433 Passes.push_back(new FunctionPassModel<FunctionPassT>(llvm_move(Pass)));
436 PreservedAnalyses run(Function *F, FunctionAnalysisManager *AM = 0);
438 static StringRef name() { return "FunctionPassManager"; }
441 // Pull in the concept type and model template specialized for functions.
442 typedef detail::PassConcept<Function *, FunctionAnalysisManager>
444 template <typename PassT>
445 struct FunctionPassModel
446 : detail::PassModel<Function *, FunctionAnalysisManager, PassT> {
447 FunctionPassModel(PassT Pass)
448 : detail::PassModel<Function *, FunctionAnalysisManager, PassT>(Pass) {}
451 std::vector<polymorphic_ptr<FunctionPassConcept> > Passes;
456 /// \brief A CRTP base used to implement analysis managers.
458 /// This class template serves as the boiler plate of an analysis manager. Any
459 /// analysis manager can be implemented on top of this base class. Any
460 /// implementation will be required to provide specific hooks:
463 /// - getCachedResultImpl
466 /// The details of the call pattern are within.
467 template <typename DerivedT, typename IRUnitT>
468 class AnalysisManagerBase {
469 DerivedT *derived_this() { return static_cast<DerivedT *>(this); }
470 const DerivedT *derived_this() const { return static_cast<const DerivedT *>(this); }
473 typedef detail::AnalysisResultConcept<IRUnitT> ResultConceptT;
474 typedef detail::AnalysisPassConcept<IRUnitT, DerivedT> PassConceptT;
476 // FIXME: Provide template aliases for the models when we're using C++11 in
477 // a mode supporting them.
480 /// \brief Get the result of an analysis pass for this module.
482 /// If there is not a valid cached result in the manager already, this will
483 /// re-run the analysis to produce a valid result.
484 template <typename PassT> const typename PassT::Result &getResult(IRUnitT IR) {
485 assert(AnalysisPasses.count(PassT::ID()) &&
486 "This analysis pass was not registered prior to being queried");
488 const ResultConceptT &ResultConcept =
489 derived_this()->getResultImpl(PassT::ID(), IR);
490 typedef detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
492 return static_cast<const ResultModelT &>(ResultConcept).Result;
495 /// \brief Get the cached result of an analysis pass for this module.
497 /// This method never runs the analysis.
499 /// \returns null if there is no cached result.
500 template <typename PassT>
501 const typename PassT::Result *getCachedResult(IRUnitT IR) const {
502 assert(AnalysisPasses.count(PassT::ID()) &&
503 "This analysis pass was not registered prior to being queried");
505 const ResultConceptT *ResultConcept =
506 derived_this()->getCachedResultImpl(PassT::ID(), IR);
510 typedef detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
512 return &static_cast<const ResultModelT *>(ResultConcept)->Result;
515 /// \brief Register an analysis pass with the manager.
517 /// This provides an initialized and set-up analysis pass to the analysis
518 /// manager. Whomever is setting up analysis passes must use this to populate
519 /// the manager with all of the analysis passes available.
520 template <typename PassT> void registerPass(PassT Pass) {
521 assert(!AnalysisPasses.count(PassT::ID()) &&
522 "Registered the same analysis pass twice!");
523 typedef detail::AnalysisPassModel<IRUnitT, DerivedT, PassT> PassModelT;
524 AnalysisPasses[PassT::ID()] = new PassModelT(llvm_move(Pass));
527 /// \brief Invalidate a specific analysis pass for an IR module.
529 /// Note that the analysis result can disregard invalidation.
530 template <typename PassT> void invalidate(Module *M) {
531 assert(AnalysisPasses.count(PassT::ID()) &&
532 "This analysis pass was not registered prior to being invalidated");
533 derived_this()->invalidateImpl(PassT::ID(), M);
536 /// \brief Invalidate analyses cached for an IR unit.
538 /// Walk through all of the analyses pertaining to this unit of IR and
539 /// invalidate them unless they are preserved by the PreservedAnalyses set.
540 void invalidate(IRUnitT IR, const PreservedAnalyses &PA) {
541 derived_this()->invalidateImpl(IR, PA);
545 /// \brief Lookup a registered analysis pass.
546 PassConceptT &lookupPass(void *PassID) {
547 typename AnalysisPassMapT::iterator PI = AnalysisPasses.find(PassID);
548 assert(PI != AnalysisPasses.end() &&
549 "Analysis passes must be registered prior to being queried!");
553 /// \brief Lookup a registered analysis pass.
554 const PassConceptT &lookupPass(void *PassID) const {
555 typename AnalysisPassMapT::const_iterator PI = AnalysisPasses.find(PassID);
556 assert(PI != AnalysisPasses.end() &&
557 "Analysis passes must be registered prior to being queried!");
562 /// \brief Map type from module analysis pass ID to pass concept pointer.
563 typedef DenseMap<void *, polymorphic_ptr<PassConceptT> > AnalysisPassMapT;
565 /// \brief Collection of module analysis passes, indexed by ID.
566 AnalysisPassMapT AnalysisPasses;
571 /// \brief A module analysis pass manager with lazy running and caching of
573 class ModuleAnalysisManager
574 : public detail::AnalysisManagerBase<ModuleAnalysisManager, Module *> {
575 friend class detail::AnalysisManagerBase<ModuleAnalysisManager, Module *>;
576 typedef detail::AnalysisManagerBase<ModuleAnalysisManager, Module *> BaseT;
577 typedef BaseT::ResultConceptT ResultConceptT;
578 typedef BaseT::PassConceptT PassConceptT;
581 // Public methods provided by the base class.
584 /// \brief Get a module pass result, running the pass if necessary.
585 const ResultConceptT &getResultImpl(void *PassID, Module *M);
587 /// \brief Get a cached module pass result or return null.
588 const ResultConceptT *getCachedResultImpl(void *PassID, Module *M) const;
590 /// \brief Invalidate a module pass result.
591 void invalidateImpl(void *PassID, Module *M);
593 /// \brief Invalidate results across a module.
594 void invalidateImpl(Module *M, const PreservedAnalyses &PA);
596 /// \brief Map type from module analysis pass ID to pass result concept pointer.
597 typedef DenseMap<void *,
598 polymorphic_ptr<detail::AnalysisResultConcept<Module *> > >
599 ModuleAnalysisResultMapT;
601 /// \brief Cache of computed module analysis results for this module.
602 ModuleAnalysisResultMapT ModuleAnalysisResults;
605 /// \brief A function analysis manager to coordinate and cache analyses run over
607 class FunctionAnalysisManager
608 : public detail::AnalysisManagerBase<FunctionAnalysisManager, Function *> {
609 friend class detail::AnalysisManagerBase<FunctionAnalysisManager, Function *>;
610 typedef detail::AnalysisManagerBase<FunctionAnalysisManager, Function *> BaseT;
611 typedef BaseT::ResultConceptT ResultConceptT;
612 typedef BaseT::PassConceptT PassConceptT;
615 // Most public APIs are inherited from the CRTP base class.
617 /// \brief Returns true if the analysis manager has an empty results cache.
620 /// \brief Clear the function analysis result cache.
622 /// This routine allows cleaning up when the set of functions itself has
623 /// potentially changed, and thus we can't even look up a a result and
624 /// invalidate it directly. Notably, this does *not* call invalidate
625 /// functions as there is nothing to be done for them.
629 /// \brief Get a function pass result, running the pass if necessary.
630 const ResultConceptT &getResultImpl(void *PassID, Function *F);
632 /// \brief Get a cached function pass result or return null.
633 const ResultConceptT *getCachedResultImpl(void *PassID, Function *F) const;
635 /// \brief Invalidate a function pass result.
636 void invalidateImpl(void *PassID, Function *F);
638 /// \brief Invalidate the results for a function..
639 void invalidateImpl(Function *F, const PreservedAnalyses &PA);
641 /// \brief List of function analysis pass IDs and associated concept pointers.
643 /// Requires iterators to be valid across appending new entries and arbitrary
644 /// erases. Provides both the pass ID and concept pointer such that it is
645 /// half of a bijection and provides storage for the actual result concept.
646 typedef std::list<std::pair<
647 void *, polymorphic_ptr<detail::AnalysisResultConcept<Function *> > > >
648 FunctionAnalysisResultListT;
650 /// \brief Map type from function pointer to our custom list type.
651 typedef DenseMap<Function *, FunctionAnalysisResultListT>
652 FunctionAnalysisResultListMapT;
654 /// \brief Map from function to a list of function analysis results.
656 /// Provides linear time removal of all analysis results for a function and
657 /// the ultimate storage for a particular cached analysis result.
658 FunctionAnalysisResultListMapT FunctionAnalysisResultLists;
660 /// \brief Map type from a pair of analysis ID and function pointer to an
661 /// iterator into a particular result list.
662 typedef DenseMap<std::pair<void *, Function *>,
663 FunctionAnalysisResultListT::iterator>
664 FunctionAnalysisResultMapT;
666 /// \brief Map from an analysis ID and function to a particular cached
668 FunctionAnalysisResultMapT FunctionAnalysisResults;
671 /// \brief A module analysis which acts as a proxy for a function analysis
674 /// This primarily proxies invalidation information from the module analysis
675 /// manager and module pass manager to a function analysis manager. You should
676 /// never use a function analysis manager from within (transitively) a module
677 /// pass manager unless your parent module pass has received a proxy result
679 class FunctionAnalysisManagerModuleProxy {
683 static void *ID() { return (void *)&PassID; }
685 FunctionAnalysisManagerModuleProxy(FunctionAnalysisManager &FAM) : FAM(FAM) {}
687 /// \brief Run the analysis pass and create our proxy result object.
689 /// This doesn't do any interesting work, it is primarily used to insert our
690 /// proxy result object into the module analysis cache so that we can proxy
691 /// invalidation to the function analysis manager.
693 /// In debug builds, it will also assert that the analysis manager is empty
694 /// as no queries should arrive at the function analysis manager prior to
695 /// this analysis being requested.
696 Result run(Module *M);
701 FunctionAnalysisManager &FAM;
704 /// \brief The result proxy object for the
705 /// \c FunctionAnalysisManagerModuleProxy.
707 /// See its documentation for more information.
708 class FunctionAnalysisManagerModuleProxy::Result {
710 Result(FunctionAnalysisManager &FAM) : FAM(FAM) {}
713 /// \brief Accessor for the \c FunctionAnalysisManager.
714 FunctionAnalysisManager &getManager() const { return FAM; }
716 /// \brief Handler for invalidation of the module.
718 /// If this analysis itself is preserved, then we assume that the set of \c
719 /// Function objects in the \c Module hasn't changed and thus we don't need
720 /// to invalidate *all* cached data associated with a \c Function* in the \c
721 /// FunctionAnalysisManager.
723 /// Regardless of whether this analysis is marked as preserved, all of the
724 /// analyses in the \c FunctionAnalysisManager are potentially invalidated
725 /// based on the set of preserved analyses.
726 bool invalidate(Module *M, const PreservedAnalyses &PA);
729 FunctionAnalysisManager &FAM;
732 /// \brief A function analysis which acts as a proxy for a module analysis
735 /// This primarily provides an accessor to a parent module analysis manager to
736 /// function passes. Only the const interface of the module analysis manager is
737 /// provided to indicate that once inside of a function analysis pass you
738 /// cannot request a module analysis to actually run. Instead, the user must
739 /// rely on the \c getCachedResult API.
741 /// This proxy *doesn't* manage the invalidation in any way. That is handled by
742 /// the recursive return path of each layer of the pass manager and the
743 /// returned PreservedAnalysis set.
744 class ModuleAnalysisManagerFunctionProxy {
746 /// \brief Result proxy object for \c ModuleAnalysisManagerFunctionProxy.
749 Result(const ModuleAnalysisManager &MAM) : MAM(MAM) {}
751 const ModuleAnalysisManager &getManager() const { return MAM; }
753 /// \brief Handle invalidation by ignoring it, this pass is immutable.
754 bool invalidate(Function *) { return false; }
757 const ModuleAnalysisManager &MAM;
760 static void *ID() { return (void *)&PassID; }
762 ModuleAnalysisManagerFunctionProxy(const ModuleAnalysisManager &MAM)
765 /// \brief Run the analysis pass and create our proxy result object.
766 /// Nothing to see here, it just forwards the \c MAM reference into the
768 Result run(Function *) { return Result(MAM); }
773 const ModuleAnalysisManager &MAM;
776 /// \brief Trivial adaptor that maps from a module to its functions.
778 /// Designed to allow composition of a FunctionPass(Manager) and
779 /// a ModulePassManager. Note that if this pass is constructed with a pointer
780 /// to a \c ModuleAnalysisManager it will run the
781 /// \c FunctionAnalysisManagerModuleProxy analysis prior to running the function
782 /// pass over the module to enable a \c FunctionAnalysisManager to be used
783 /// within this run safely.
784 template <typename FunctionPassT>
785 class ModuleToFunctionPassAdaptor {
787 explicit ModuleToFunctionPassAdaptor(FunctionPassT Pass)
788 : Pass(llvm_move(Pass)) {}
790 /// \brief Runs the function pass across every function in the module.
791 PreservedAnalyses run(Module *M, ModuleAnalysisManager *AM) {
792 FunctionAnalysisManager *FAM = 0;
794 // Setup the function analysis manager from its proxy.
795 FAM = &AM->getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
797 PreservedAnalyses PA = PreservedAnalyses::all();
798 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
799 PreservedAnalyses PassPA = Pass.run(I, FAM);
801 // We know that the function pass couldn't have invalidated any other
802 // function's analyses (that's the contract of a function pass), so
803 // directly handle the function analysis manager's invalidation here.
805 FAM->invalidate(I, PassPA);
807 // Then intersect the preserved set so that invalidation of module
808 // analyses will eventually occur when the module pass completes.
809 PA.intersect(llvm_move(PassPA));
812 // By definition we preserve the proxy. This precludes *any* invalidation
813 // of function analyses by the proxy, but that's OK because we've taken
814 // care to invalidate analyses in the function analysis manager
815 // incrementally above.
816 PA.preserve<FunctionAnalysisManagerModuleProxy>();
820 static StringRef name() { return "ModuleToFunctionPassAdaptor"; }
826 /// \brief A function to deduce a function pass type and wrap it in the
827 /// templated adaptor.
828 template <typename FunctionPassT>
829 ModuleToFunctionPassAdaptor<FunctionPassT>
830 createModuleToFunctionPassAdaptor(FunctionPassT Pass) {
831 return ModuleToFunctionPassAdaptor<FunctionPassT>(llvm_move(Pass));