1 //===- PassManager.h - Pass management infrastructure -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This header defines various interfaces for pass management in LLVM. There
12 /// is no "pass" interface in LLVM per se. Instead, an instance of any class
13 /// which supports a method to 'run' it over a unit of IR can be used as
14 /// a pass. A pass manager is generally a tool to collect a sequence of passes
15 /// which run over a particular IR construct, and run each of them in sequence
16 /// over each such construct in the containing IR construct. As there is no
17 /// containing IR construct for a Module, a manager for passes over modules
18 /// forms the base case which runs its managed passes in sequence over the
19 /// single module provided.
21 /// The core IR library provides managers for running passes over
22 /// modules and functions.
24 /// * FunctionPassManager can run over a Module, runs each pass over
26 /// * ModulePassManager must be directly run, runs each pass over the Module.
28 /// Note that the implementations of the pass managers use concept-based
29 /// polymorphism as outlined in the "Value Semantics and Concept-based
30 /// Polymorphism" talk (or its abbreviated sibling "Inheritance Is The Base
31 /// Class of Evil") by Sean Parent:
32 /// * http://github.com/sean-parent/sean-parent.github.com/wiki/Papers-and-Presentations
33 /// * http://www.youtube.com/watch?v=_BpMYeUFXv8
34 /// * http://channel9.msdn.com/Events/GoingNative/2013/Inheritance-Is-The-Base-Class-of-Evil
36 //===----------------------------------------------------------------------===//
38 #include "llvm/ADT/DenseMap.h"
39 #include "llvm/ADT/SmallPtrSet.h"
40 #include "llvm/ADT/polymorphic_ptr.h"
41 #include "llvm/Support/type_traits.h"
42 #include "llvm/IR/Function.h"
43 #include "llvm/IR/Module.h"
52 /// \brief An abstract set of preserved analyses following a transformation pass
55 /// When a transformation pass is run, it can return a set of analyses whose
56 /// results were preserved by that transformation. The default set is "none",
57 /// and preserving analyses must be done explicitly.
59 /// There is also an explicit all state which can be used (for example) when
60 /// the IR is not mutated at all.
61 class PreservedAnalyses {
63 /// \brief Convenience factory function for the empty preserved set.
64 static PreservedAnalyses none() { return PreservedAnalyses(); }
66 /// \brief Construct a special preserved set that preserves all passes.
67 static PreservedAnalyses all() {
69 PA.PreservedPassIDs.insert((void *)AllPassesID);
73 PreservedAnalyses &operator=(PreservedAnalyses Arg) {
78 void swap(PreservedAnalyses &Arg) {
79 PreservedPassIDs.swap(Arg.PreservedPassIDs);
82 /// \brief Mark a particular pass as preserved, adding it to the set.
83 template <typename PassT> void preserve() {
84 if (!areAllPreserved())
85 PreservedPassIDs.insert(PassT::ID());
88 /// \brief Intersect this set with another in place.
90 /// This is a mutating operation on this preserved set, removing all
91 /// preserved passes which are not also preserved in the argument.
92 void intersect(const PreservedAnalyses &Arg) {
93 if (Arg.areAllPreserved())
95 if (areAllPreserved()) {
96 PreservedPassIDs = Arg.PreservedPassIDs;
99 for (SmallPtrSet<void *, 2>::const_iterator I = PreservedPassIDs.begin(),
100 E = PreservedPassIDs.end();
102 if (!Arg.PreservedPassIDs.count(*I))
103 PreservedPassIDs.erase(*I);
106 #if LLVM_HAS_RVALUE_REFERENCES
107 /// \brief Intersect this set with a temporary other set in place.
109 /// This is a mutating operation on this preserved set, removing all
110 /// preserved passes which are not also preserved in the argument.
111 void intersect(PreservedAnalyses &&Arg) {
112 if (Arg.areAllPreserved())
114 if (areAllPreserved()) {
115 PreservedPassIDs = std::move(Arg.PreservedPassIDs);
118 for (SmallPtrSet<void *, 2>::const_iterator I = PreservedPassIDs.begin(),
119 E = PreservedPassIDs.end();
121 if (!Arg.PreservedPassIDs.count(*I))
122 PreservedPassIDs.erase(*I);
126 /// \brief Query whether a pass is marked as preserved by this set.
127 template <typename PassT> bool preserved() const {
128 return preserved(PassT::ID());
131 /// \brief Query whether an abstract pass ID is marked as preserved by this
133 bool preserved(void *PassID) const {
134 return PreservedPassIDs.count((void *)AllPassesID) ||
135 PreservedPassIDs.count(PassID);
139 // Note that this must not be -1 or -2 as those are already used by the
141 static const uintptr_t AllPassesID = (intptr_t)-3;
143 bool areAllPreserved() const { return PreservedPassIDs.count((void *)AllPassesID); }
145 SmallPtrSet<void *, 2> PreservedPassIDs;
148 inline void swap(PreservedAnalyses &LHS, PreservedAnalyses &RHS) {
152 /// \brief Implementation details of the pass manager interfaces.
155 /// \brief Template for the abstract base class used to dispatch
156 /// polymorphically over pass objects.
157 template <typename IRUnitT, typename AnalysisManagerT> struct PassConcept {
158 // Boiler plate necessary for the container of derived classes.
159 virtual ~PassConcept() {}
160 virtual PassConcept *clone() = 0;
162 /// \brief The polymorphic API which runs the pass over a given IR entity.
164 /// Note that actual pass object can omit the analysis manager argument if
165 /// desired. Also that the analysis manager may be null if there is no
166 /// analysis manager in the pass pipeline.
167 virtual PreservedAnalyses run(IRUnitT IR, AnalysisManagerT *AM) = 0;
170 /// \brief SFINAE metafunction for computing whether \c PassT has a run method
171 /// accepting an \c AnalysisManagerT.
172 template <typename IRUnitT, typename AnalysisManagerT, typename PassT,
174 class PassRunAcceptsAnalysisManager {
175 typedef char SmallType;
176 struct BigType { char a, b; };
178 template <typename T, ResultT (T::*)(IRUnitT, AnalysisManagerT *)>
181 template <typename T> static SmallType f(Checker<T, &T::run> *);
182 template <typename T> static BigType f(...);
185 enum { Value = sizeof(f<PassT>(0)) == sizeof(SmallType) };
188 /// \brief A template wrapper used to implement the polymorphic API.
190 /// Can be instantiated for any object which provides a \c run method accepting
191 /// an \c IRUnitT. It requires the pass to be a copyable object. When the
192 /// \c run method also accepts an \c AnalysisManagerT*, we pass it along.
193 template <typename IRUnitT, typename AnalysisManagerT, typename PassT,
194 bool AcceptsAnalysisManager = PassRunAcceptsAnalysisManager<
195 IRUnitT, AnalysisManagerT, PassT, PreservedAnalyses>::Value>
198 /// \brief Specialization of \c PassModel for passes that accept an analyis
200 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
201 struct PassModel<IRUnitT, AnalysisManagerT, PassT,
202 true> : PassConcept<IRUnitT, AnalysisManagerT> {
203 PassModel(PassT Pass) : Pass(llvm_move(Pass)) {}
204 virtual PassModel *clone() { return new PassModel(Pass); }
205 virtual PreservedAnalyses run(IRUnitT IR, AnalysisManagerT *AM) {
206 return Pass.run(IR, AM);
211 /// \brief Specialization of \c PassModel for passes that accept an analyis
213 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
214 struct PassModel<IRUnitT, AnalysisManagerT, PassT,
215 false> : PassConcept<IRUnitT, AnalysisManagerT> {
216 PassModel(PassT Pass) : Pass(llvm_move(Pass)) {}
217 virtual PassModel *clone() { return new PassModel(Pass); }
218 virtual PreservedAnalyses run(IRUnitT IR, AnalysisManagerT *AM) {
224 /// \brief Abstract concept of an analysis result.
226 /// This concept is parameterized over the IR unit that this result pertains
228 template <typename IRUnitT> struct AnalysisResultConcept {
229 virtual ~AnalysisResultConcept() {}
230 virtual AnalysisResultConcept *clone() = 0;
232 /// \brief Method to try and mark a result as invalid.
234 /// When the outer analysis manager detects a change in some underlying
235 /// unit of the IR, it will call this method on all of the results cached.
237 /// This method also receives a set of preserved analyses which can be used
238 /// to avoid invalidation because the pass which changed the underlying IR
239 /// took care to update or preserve the analysis result in some way.
241 /// \returns true if the result is indeed invalid (the default).
242 virtual bool invalidate(IRUnitT IR, const PreservedAnalyses &PA) = 0;
245 /// \brief SFINAE metafunction for computing whether \c ResultT provides an
246 /// \c invalidate member function.
247 template <typename IRUnitT, typename ResultT> class ResultHasInvalidateMethod {
248 typedef char SmallType;
249 struct BigType { char a, b; };
251 template <typename T, bool (T::*)(IRUnitT, const PreservedAnalyses &)>
254 template <typename T> static SmallType f(Checker<T, &T::invalidate> *);
255 template <typename T> static BigType f(...);
258 enum { Value = sizeof(f<ResultT>(0)) == sizeof(SmallType) };
261 /// \brief Wrapper to model the analysis result concept.
263 /// By default, this will implement the invalidate method with a trivial
264 /// implementation so that the actual analysis result doesn't need to provide
265 /// an invalidation handler. It is only selected when the invalidation handler
266 /// is not part of the ResultT's interface.
267 template <typename IRUnitT, typename PassT, typename ResultT,
268 bool HasInvalidateHandler =
269 ResultHasInvalidateMethod<IRUnitT, ResultT>::Value>
270 struct AnalysisResultModel;
272 /// \brief Specialization of \c AnalysisResultModel which provides the default
273 /// invalidate functionality.
274 template <typename IRUnitT, typename PassT, typename ResultT>
275 struct AnalysisResultModel<IRUnitT, PassT, ResultT,
276 false> : AnalysisResultConcept<IRUnitT> {
277 AnalysisResultModel(ResultT Result) : Result(llvm_move(Result)) {}
278 virtual AnalysisResultModel *clone() {
279 return new AnalysisResultModel(Result);
282 /// \brief The model bases invalidation solely on being in the preserved set.
284 // FIXME: We should actually use two different concepts for analysis results
285 // rather than two different models, and avoid the indirect function call for
286 // ones that use the trivial behavior.
287 virtual bool invalidate(IRUnitT, const PreservedAnalyses &PA) {
288 return !PA.preserved(PassT::ID());
294 /// \brief Specialization of \c AnalysisResultModel which delegates invalidate
295 /// handling to \c ResultT.
296 template <typename IRUnitT, typename PassT, typename ResultT>
297 struct AnalysisResultModel<IRUnitT, PassT, ResultT,
298 true> : AnalysisResultConcept<IRUnitT> {
299 AnalysisResultModel(ResultT Result) : Result(llvm_move(Result)) {}
300 virtual AnalysisResultModel *clone() {
301 return new AnalysisResultModel(Result);
304 /// \brief The model delegates to the \c ResultT method.
305 virtual bool invalidate(IRUnitT IR, const PreservedAnalyses &PA) {
306 return Result.invalidate(IR, PA);
312 /// \brief Abstract concept of an analysis pass.
314 /// This concept is parameterized over the IR unit that it can run over and
315 /// produce an analysis result.
316 template <typename IRUnitT, typename AnalysisManagerT>
317 struct AnalysisPassConcept {
318 virtual ~AnalysisPassConcept() {}
319 virtual AnalysisPassConcept *clone() = 0;
321 /// \brief Method to run this analysis over a unit of IR.
322 /// \returns The analysis result object to be queried by users, the caller
324 virtual AnalysisResultConcept<IRUnitT> *run(IRUnitT IR,
325 AnalysisManagerT *AM) = 0;
328 /// \brief Wrapper to model the analysis pass concept.
330 /// Can wrap any type which implements a suitable \c run method. The method
331 /// must accept the IRUnitT as an argument and produce an object which can be
332 /// wrapped in a \c AnalysisResultModel.
333 template <typename IRUnitT, typename AnalysisManagerT, typename PassT,
334 bool AcceptsAnalysisManager = PassRunAcceptsAnalysisManager<
335 IRUnitT, AnalysisManagerT, PassT,
336 typename PassT::Result>::Value > struct AnalysisPassModel;
338 /// \brief Specialization of \c AnalysisPassModel which passes an
339 /// \c AnalysisManager to PassT's run method.
340 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
341 struct AnalysisPassModel<IRUnitT, AnalysisManagerT, PassT,
342 true> : AnalysisPassConcept<IRUnitT,
344 AnalysisPassModel(PassT Pass) : Pass(llvm_move(Pass)) {}
345 virtual AnalysisPassModel *clone() { return new AnalysisPassModel(Pass); }
347 // FIXME: Replace PassT::Result with type traits when we use C++11.
348 typedef AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
351 /// \brief The model delegates to the \c PassT::run method.
353 /// The return is wrapped in an \c AnalysisResultModel.
354 virtual ResultModelT *run(IRUnitT IR, AnalysisManagerT *AM) {
355 return new ResultModelT(Pass.run(IR, AM));
361 /// \brief Specialization of \c AnalysisPassModel which does not pass an
362 /// \c AnalysisManager to PassT's run method.
363 template <typename IRUnitT, typename AnalysisManagerT, typename PassT>
364 struct AnalysisPassModel<IRUnitT, AnalysisManagerT, PassT,
365 false> : AnalysisPassConcept<IRUnitT,
367 AnalysisPassModel(PassT Pass) : Pass(llvm_move(Pass)) {}
368 virtual AnalysisPassModel *clone() { return new AnalysisPassModel(Pass); }
370 // FIXME: Replace PassT::Result with type traits when we use C++11.
371 typedef AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
374 /// \brief The model delegates to the \c PassT::run method.
376 /// The return is wrapped in an \c AnalysisResultModel.
377 virtual ResultModelT *run(IRUnitT IR, AnalysisManagerT *) {
378 return new ResultModelT(Pass.run(IR));
386 class ModuleAnalysisManager;
388 class ModulePassManager {
390 explicit ModulePassManager() {}
392 /// \brief Run all of the module passes in this module pass manager over
395 /// This method should only be called for a single module as there is the
396 /// expectation that the lifetime of a pass is bounded to that of a module.
397 PreservedAnalyses run(Module *M, ModuleAnalysisManager *AM = 0);
399 template <typename ModulePassT> void addPass(ModulePassT Pass) {
400 Passes.push_back(new ModulePassModel<ModulePassT>(llvm_move(Pass)));
404 // Pull in the concept type and model template specialized for modules.
405 typedef detail::PassConcept<Module *, ModuleAnalysisManager> ModulePassConcept;
406 template <typename PassT>
407 struct ModulePassModel
408 : detail::PassModel<Module *, ModuleAnalysisManager, PassT> {
409 ModulePassModel(PassT Pass)
410 : detail::PassModel<Module *, ModuleAnalysisManager, PassT>(Pass) {}
413 std::vector<polymorphic_ptr<ModulePassConcept> > Passes;
416 class FunctionAnalysisManager;
418 class FunctionPassManager {
420 explicit FunctionPassManager() {}
422 template <typename FunctionPassT> void addPass(FunctionPassT Pass) {
423 Passes.push_back(new FunctionPassModel<FunctionPassT>(llvm_move(Pass)));
426 PreservedAnalyses run(Function *F, FunctionAnalysisManager *AM = 0);
429 // Pull in the concept type and model template specialized for functions.
430 typedef detail::PassConcept<Function *, FunctionAnalysisManager>
432 template <typename PassT>
433 struct FunctionPassModel
434 : detail::PassModel<Function *, FunctionAnalysisManager, PassT> {
435 FunctionPassModel(PassT Pass)
436 : detail::PassModel<Function *, FunctionAnalysisManager, PassT>(Pass) {}
439 std::vector<polymorphic_ptr<FunctionPassConcept> > Passes;
442 /// \brief A module analysis pass manager with lazy running and caching of
444 class ModuleAnalysisManager {
446 ModuleAnalysisManager() {}
448 /// \brief Get the result of an analysis pass for this module.
450 /// If there is not a valid cached result in the manager already, this will
451 /// re-run the analysis to produce a valid result.
452 template <typename PassT> const typename PassT::Result &getResult(Module *M) {
453 assert(ModuleAnalysisPasses.count(PassT::ID()) &&
454 "This analysis pass was not registered prior to being queried");
456 const detail::AnalysisResultConcept<Module *> &ResultConcept =
457 getResultImpl(PassT::ID(), M);
458 typedef detail::AnalysisResultModel<Module *, PassT, typename PassT::Result>
460 return static_cast<const ResultModelT &>(ResultConcept).Result;
463 /// \brief Register an analysis pass with the manager.
465 /// This provides an initialized and set-up analysis pass to the
467 /// manager. Whomever is setting up analysis passes must use this to
469 /// the manager with all of the analysis passes available.
470 template <typename PassT> void registerPass(PassT Pass) {
471 assert(!ModuleAnalysisPasses.count(PassT::ID()) &&
472 "Registered the same analysis pass twice!");
473 ModuleAnalysisPasses[PassT::ID()] =
474 new detail::AnalysisPassModel<Module *, ModuleAnalysisManager, PassT>(
478 /// \brief Invalidate a specific analysis pass for an IR module.
480 /// Note that the analysis result can disregard invalidation.
481 template <typename PassT> void invalidate(Module *M) {
482 assert(ModuleAnalysisPasses.count(PassT::ID()) &&
483 "This analysis pass was not registered prior to being invalidated");
484 invalidateImpl(PassT::ID(), M);
487 /// \brief Invalidate analyses cached for an IR Module.
489 /// Walk through all of the analyses pertaining to this module and invalidate
490 /// them unless they are preserved by the PreservedAnalyses set.
491 void invalidate(Module *M, const PreservedAnalyses &PA);
494 /// \brief Get a module pass result, running the pass if necessary.
495 const detail::AnalysisResultConcept<Module *> &getResultImpl(void *PassID,
498 /// \brief Invalidate a module pass result.
499 void invalidateImpl(void *PassID, Module *M);
501 /// \brief Map type from module analysis pass ID to pass concept pointer.
502 typedef DenseMap<void *, polymorphic_ptr<detail::AnalysisPassConcept<
503 Module *, ModuleAnalysisManager> > >
504 ModuleAnalysisPassMapT;
506 /// \brief Collection of module analysis passes, indexed by ID.
507 ModuleAnalysisPassMapT ModuleAnalysisPasses;
509 /// \brief Map type from module analysis pass ID to pass result concept pointer.
510 typedef DenseMap<void *,
511 polymorphic_ptr<detail::AnalysisResultConcept<Module *> > >
512 ModuleAnalysisResultMapT;
514 /// \brief Cache of computed module analysis results for this module.
515 ModuleAnalysisResultMapT ModuleAnalysisResults;
518 /// \brief A function analysis manager to coordinate and cache analyses run over
520 class FunctionAnalysisManager {
522 FunctionAnalysisManager() {}
524 /// \brief Get the result of an analysis pass for a function.
526 /// If there is not a valid cached result in the manager already, this will
527 /// re-run the analysis to produce a valid result.
528 template <typename PassT>
529 const typename PassT::Result &getResult(Function *F) {
530 assert(FunctionAnalysisPasses.count(PassT::ID()) &&
531 "This analysis pass was not registered prior to being queried");
533 const detail::AnalysisResultConcept<Function *> &ResultConcept =
534 getResultImpl(PassT::ID(), F);
535 typedef detail::AnalysisResultModel<Function *, PassT,
536 typename PassT::Result> ResultModelT;
537 return static_cast<const ResultModelT &>(ResultConcept).Result;
540 /// \brief Register an analysis pass with the manager.
542 /// This provides an initialized and set-up analysis pass to the
544 /// manager. Whomever is setting up analysis passes must use this to
546 /// the manager with all of the analysis passes available.
547 template <typename PassT> void registerPass(PassT Pass) {
548 assert(!FunctionAnalysisPasses.count(PassT::ID()) &&
549 "Registered the same analysis pass twice!");
550 FunctionAnalysisPasses[PassT::ID()] = new detail::AnalysisPassModel<
551 Function *, FunctionAnalysisManager, PassT>(llvm_move(Pass));
554 /// \brief Invalidate a specific analysis pass for an IR module.
556 /// Note that the analysis result can disregard invalidation.
557 template <typename PassT> void invalidate(Function *F) {
558 assert(FunctionAnalysisPasses.count(PassT::ID()) &&
559 "This analysis pass was not registered prior to being invalidated");
560 invalidateImpl(PassT::ID(), F);
563 /// \brief Invalidate analyses cached for an IR Function.
565 /// Walk through all of the analyses cache for this IR function and
566 /// invalidate them unless they are preserved by the provided
567 /// PreservedAnalyses set.
568 void invalidate(Function *F, const PreservedAnalyses &PA);
570 /// \brief Returns true if the analysis manager has an empty results cache.
573 /// \brief Clear the function analysis result cache.
575 /// This routine allows cleaning up when the set of functions itself has
576 /// potentially changed, and thus we can't even look up a a result and
577 /// invalidate it directly. Notably, this does *not* call invalidate
578 /// functions as there is nothing to be done for them.
582 /// \brief Get a function pass result, running the pass if necessary.
583 const detail::AnalysisResultConcept<Function *> &getResultImpl(void *PassID,
586 /// \brief Invalidate a function pass result.
587 void invalidateImpl(void *PassID, Function *F);
589 /// \brief Map type from function analysis pass ID to pass concept pointer.
590 typedef DenseMap<void *, polymorphic_ptr<detail::AnalysisPassConcept<
591 Function *, FunctionAnalysisManager> > >
592 FunctionAnalysisPassMapT;
594 /// \brief Collection of function analysis passes, indexed by ID.
595 FunctionAnalysisPassMapT FunctionAnalysisPasses;
597 /// \brief List of function analysis pass IDs and associated concept pointers.
599 /// Requires iterators to be valid across appending new entries and arbitrary
600 /// erases. Provides both the pass ID and concept pointer such that it is
601 /// half of a bijection and provides storage for the actual result concept.
602 typedef std::list<std::pair<
603 void *, polymorphic_ptr<detail::AnalysisResultConcept<Function *> > > >
604 FunctionAnalysisResultListT;
606 /// \brief Map type from function pointer to our custom list type.
607 typedef DenseMap<Function *, FunctionAnalysisResultListT>
608 FunctionAnalysisResultListMapT;
610 /// \brief Map from function to a list of function analysis results.
612 /// Provides linear time removal of all analysis results for a function and
613 /// the ultimate storage for a particular cached analysis result.
614 FunctionAnalysisResultListMapT FunctionAnalysisResultLists;
616 /// \brief Map type from a pair of analysis ID and function pointer to an
617 /// iterator into a particular result list.
618 typedef DenseMap<std::pair<void *, Function *>,
619 FunctionAnalysisResultListT::iterator>
620 FunctionAnalysisResultMapT;
622 /// \brief Map from an analysis ID and function to a particular cached
624 FunctionAnalysisResultMapT FunctionAnalysisResults;
627 /// \brief A module analysis which acts as a proxy for a function analysis
630 /// This primarily proxies invalidation information from the module analysis
631 /// manager and module pass manager to a function analysis manager. You should
632 /// never use a function analysis manager from within (transitively) a module
633 /// pass manager unless your parent module pass has received a proxy result
636 /// FIXME: It might be really nice to "enforce" this (softly) by making this
637 /// proxy the API path to access a function analysis manager within a module
639 class FunctionAnalysisManagerModuleProxy {
643 static void *ID() { return (void *)&PassID; }
645 FunctionAnalysisManagerModuleProxy(FunctionAnalysisManager &FAM) : FAM(FAM) {}
647 /// \brief Run the analysis pass and create our proxy result object.
649 /// This doesn't do any interesting work, it is primarily used to insert our
650 /// proxy result object into the module analysis cache so that we can proxy
651 /// invalidation to the function analysis manager.
653 /// In debug builds, it will also assert that the analysis manager is empty
654 /// as no queries should arrive at the function analysis manager prior to
655 /// this analysis being requested.
656 Result run(Module *M);
661 FunctionAnalysisManager &FAM;
664 /// \brief The result proxy object for the
665 /// \c FunctionAnalysisManagerModuleProxy.
667 /// See its documentation for more information.
668 class FunctionAnalysisManagerModuleProxy::Result {
670 Result(FunctionAnalysisManager &FAM) : FAM(FAM) {}
673 /// \brief Accessor for the \c FunctionAnalysisManager.
674 FunctionAnalysisManager &getManager() const { return FAM; }
676 /// \brief Handler for invalidation of the module.
678 /// If this analysis itself is preserved, then we assume that the set of \c
679 /// Function objects in the \c Module hasn't changed and thus we don't need
680 /// to invalidate *all* cached data associated with a \c Function* in the \c
681 /// FunctionAnalysisManager.
683 /// Regardless of whether this analysis is marked as preserved, all of the
684 /// analyses in the \c FunctionAnalysisManager are potentially invalidated
685 /// based on the set of preserved analyses.
686 bool invalidate(Module *M, const PreservedAnalyses &PA);
689 FunctionAnalysisManager &FAM;
692 /// \brief Trivial adaptor that maps from a module to its functions.
694 /// Designed to allow composition of a FunctionPass(Manager) and
695 /// a ModulePassManager. Note that if this pass is constructed with a pointer
696 /// to a \c ModuleAnalysisManager it will run the
697 /// \c FunctionAnalysisManagerModuleProxy analysis prior to running the function
698 /// pass over the module to enable a \c FunctionAnalysisManager to be used
699 /// within this run safely.
700 template <typename FunctionPassT>
701 class ModuleToFunctionPassAdaptor {
703 explicit ModuleToFunctionPassAdaptor(FunctionPassT Pass)
704 : Pass(llvm_move(Pass)) {}
706 /// \brief Runs the function pass across every function in the module.
707 PreservedAnalyses run(Module *M, ModuleAnalysisManager *AM) {
708 FunctionAnalysisManager *FAM = 0;
710 // Setup the function analysis manager from its proxy.
711 FAM = &AM->getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
713 PreservedAnalyses PA = PreservedAnalyses::all();
714 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
715 PreservedAnalyses PassPA = Pass.run(I, FAM);
716 PA.intersect(llvm_move(PassPA));
719 // By definition we preserve the proxy.
720 PA.preserve<FunctionAnalysisManagerModuleProxy>();
728 /// \brief A function to deduce a function pass type and wrap it in the
729 /// templated adaptor.
730 template <typename FunctionPassT>
731 ModuleToFunctionPassAdaptor<FunctionPassT>
732 createModuleToFunctionPassAdaptor(FunctionPassT Pass) {
733 return ModuleToFunctionPassAdaptor<FunctionPassT>(llvm_move(Pass));