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/IR/PassManagerInternal.h"
47 #include "llvm/Support/type_traits.h"
57 /// \brief An abstract set of preserved analyses following a transformation pass
60 /// When a transformation pass is run, it can return a set of analyses whose
61 /// results were preserved by that transformation. The default set is "none",
62 /// and preserving analyses must be done explicitly.
64 /// There is also an explicit all state which can be used (for example) when
65 /// the IR is not mutated at all.
66 class PreservedAnalyses {
68 // We have to explicitly define all the special member functions because MSVC
69 // refuses to generate them.
70 PreservedAnalyses() {}
71 PreservedAnalyses(const PreservedAnalyses &Arg)
72 : PreservedPassIDs(Arg.PreservedPassIDs) {}
73 PreservedAnalyses(PreservedAnalyses &&Arg)
74 : PreservedPassIDs(std::move(Arg.PreservedPassIDs)) {}
75 friend void swap(PreservedAnalyses &LHS, PreservedAnalyses &RHS) {
77 swap(LHS.PreservedPassIDs, RHS.PreservedPassIDs);
79 PreservedAnalyses &operator=(PreservedAnalyses RHS) {
84 /// \brief Convenience factory function for the empty preserved set.
85 static PreservedAnalyses none() { return PreservedAnalyses(); }
87 /// \brief Construct a special preserved set that preserves all passes.
88 static PreservedAnalyses all() {
90 PA.PreservedPassIDs.insert((void *)AllPassesID);
94 /// \brief Mark a particular pass as preserved, adding it to the set.
95 template <typename PassT> void preserve() { preserve(PassT::ID()); }
97 /// \brief Mark an abstract PassID as preserved, adding it to the set.
98 void preserve(void *PassID) {
99 if (!areAllPreserved())
100 PreservedPassIDs.insert(PassID);
103 /// \brief Intersect this set with another in place.
105 /// This is a mutating operation on this preserved set, removing all
106 /// preserved passes which are not also preserved in the argument.
107 void intersect(const PreservedAnalyses &Arg) {
108 if (Arg.areAllPreserved())
110 if (areAllPreserved()) {
111 PreservedPassIDs = Arg.PreservedPassIDs;
114 for (void *P : PreservedPassIDs)
115 if (!Arg.PreservedPassIDs.count(P))
116 PreservedPassIDs.erase(P);
119 /// \brief Intersect this set with a temporary other set in place.
121 /// This is a mutating operation on this preserved set, removing all
122 /// preserved passes which are not also preserved in the argument.
123 void intersect(PreservedAnalyses &&Arg) {
124 if (Arg.areAllPreserved())
126 if (areAllPreserved()) {
127 PreservedPassIDs = std::move(Arg.PreservedPassIDs);
130 for (void *P : PreservedPassIDs)
131 if (!Arg.PreservedPassIDs.count(P))
132 PreservedPassIDs.erase(P);
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);
147 /// \brief Test whether all passes are preserved.
149 /// This is used primarily to optimize for the case of no changes which will
150 /// common in many scenarios.
151 bool areAllPreserved() const {
152 return PreservedPassIDs.count((void *)AllPassesID);
156 // Note that this must not be -1 or -2 as those are already used by the
158 static const uintptr_t AllPassesID = (intptr_t)(-3);
160 SmallPtrSet<void *, 2> PreservedPassIDs;
163 // We define the pass managers prior to the analysis managers that they use.
164 class ModuleAnalysisManager;
166 /// \brief Manages a sequence of passes over Modules of IR.
168 /// A module pass manager contains a sequence of module passes. It is also
169 /// itself a module pass. When it is run over a module of LLVM IR, it will
170 /// sequentially run each pass it contains over that module.
172 /// If it is run with a \c ModuleAnalysisManager argument, it will propagate
173 /// that analysis manager to each pass it runs, as well as calling the analysis
174 /// manager's invalidation routine with the PreservedAnalyses of each pass it
177 /// Module passes can rely on having exclusive access to the module they are
178 /// run over. No other threads will access that module, and they can mutate it
179 /// freely. However, they must not mutate other LLVM IR modules.
180 class ModulePassManager {
182 // We have to explicitly define all the special member functions because MSVC
183 // refuses to generate them.
184 ModulePassManager() {}
185 ModulePassManager(ModulePassManager &&Arg) : Passes(std::move(Arg.Passes)) {}
186 ModulePassManager &operator=(ModulePassManager &&RHS) {
187 Passes = std::move(RHS.Passes);
191 /// \brief Run all of the module passes in this module pass manager over
194 /// This method should only be called for a single module as there is the
195 /// expectation that the lifetime of a pass is bounded to that of a module.
196 PreservedAnalyses run(Module &M, ModuleAnalysisManager *AM = nullptr);
198 template <typename ModulePassT> void addPass(ModulePassT Pass) {
199 Passes.emplace_back(new ModulePassModel<ModulePassT>(std::move(Pass)));
202 static StringRef name() { return "ModulePassManager"; }
205 // Pull in the concept type and model template specialized for modules.
206 typedef detail::PassConcept<Module &, ModuleAnalysisManager>
208 template <typename PassT>
209 struct ModulePassModel
210 : detail::PassModel<Module &, ModuleAnalysisManager, PassT> {
211 ModulePassModel(PassT Pass)
212 : detail::PassModel<Module &, ModuleAnalysisManager, PassT>(
216 ModulePassManager(const ModulePassManager &) LLVM_DELETED_FUNCTION;
217 ModulePassManager &operator=(const ModulePassManager &) LLVM_DELETED_FUNCTION;
219 std::vector<std::unique_ptr<ModulePassConcept>> Passes;
222 // We define the pass managers prior to the analysis managers that they use.
223 class FunctionAnalysisManager;
225 /// \brief Manages a sequence of passes over a Function of IR.
227 /// A function pass manager contains a sequence of function passes. It is also
228 /// itself a function pass. When it is run over a function of LLVM IR, it will
229 /// sequentially run each pass it contains over that function.
231 /// If it is run with a \c FunctionAnalysisManager argument, it will propagate
232 /// that analysis manager to each pass it runs, as well as calling the analysis
233 /// manager's invalidation routine with the PreservedAnalyses of each pass it
236 /// Function passes can rely on having exclusive access to the function they
237 /// are run over. They should not read or modify any other functions! Other
238 /// threads or systems may be manipulating other functions in the module, and
239 /// so their state should never be relied on.
240 /// FIXME: Make the above true for all of LLVM's actual passes, some still
241 /// violate this principle.
243 /// Function passes can also read the module containing the function, but they
244 /// should not modify that module outside of the use lists of various globals.
245 /// For example, a function pass is not permitted to add functions to the
247 /// FIXME: Make the above true for all of LLVM's actual passes, some still
248 /// violate this principle.
249 class FunctionPassManager {
251 // We have to explicitly define all the special member functions because MSVC
252 // refuses to generate them.
253 FunctionPassManager() {}
254 FunctionPassManager(FunctionPassManager &&Arg)
255 : Passes(std::move(Arg.Passes)) {}
256 FunctionPassManager &operator=(FunctionPassManager &&RHS) {
257 Passes = std::move(RHS.Passes);
261 template <typename FunctionPassT> void addPass(FunctionPassT Pass) {
262 Passes.emplace_back(new FunctionPassModel<FunctionPassT>(std::move(Pass)));
265 PreservedAnalyses run(Function &F, FunctionAnalysisManager *AM = nullptr);
267 static StringRef name() { return "FunctionPassManager"; }
270 // Pull in the concept type and model template specialized for functions.
271 typedef detail::PassConcept<Function &, FunctionAnalysisManager>
273 template <typename PassT>
274 struct FunctionPassModel
275 : detail::PassModel<Function &, FunctionAnalysisManager, PassT> {
276 FunctionPassModel(PassT Pass)
277 : detail::PassModel<Function &, FunctionAnalysisManager, PassT>(
281 FunctionPassManager(const FunctionPassManager &) LLVM_DELETED_FUNCTION;
282 FunctionPassManager &
283 operator=(const FunctionPassManager &) LLVM_DELETED_FUNCTION;
285 std::vector<std::unique_ptr<FunctionPassConcept>> Passes;
290 /// \brief A CRTP base used to implement analysis managers.
292 /// This class template serves as the boiler plate of an analysis manager. Any
293 /// analysis manager can be implemented on top of this base class. Any
294 /// implementation will be required to provide specific hooks:
297 /// - getCachedResultImpl
300 /// The details of the call pattern are within.
301 template <typename DerivedT, typename IRUnitT> class AnalysisManagerBase {
302 DerivedT *derived_this() { return static_cast<DerivedT *>(this); }
303 const DerivedT *derived_this() const {
304 return static_cast<const DerivedT *>(this);
307 AnalysisManagerBase(const AnalysisManagerBase &) LLVM_DELETED_FUNCTION;
308 AnalysisManagerBase &
309 operator=(const AnalysisManagerBase &) LLVM_DELETED_FUNCTION;
312 typedef detail::AnalysisResultConcept<IRUnitT> ResultConceptT;
313 typedef detail::AnalysisPassConcept<IRUnitT, DerivedT> PassConceptT;
315 // FIXME: Provide template aliases for the models when we're using C++11 in
316 // a mode supporting them.
318 // We have to explicitly define all the special member functions because MSVC
319 // refuses to generate them.
320 AnalysisManagerBase() {}
321 AnalysisManagerBase(AnalysisManagerBase &&Arg)
322 : AnalysisPasses(std::move(Arg.AnalysisPasses)) {}
323 AnalysisManagerBase &operator=(AnalysisManagerBase &&RHS) {
324 AnalysisPasses = std::move(RHS.AnalysisPasses);
329 /// \brief Get the result of an analysis pass for this module.
331 /// If there is not a valid cached result in the manager already, this will
332 /// re-run the analysis to produce a valid result.
333 template <typename PassT> typename PassT::Result &getResult(IRUnitT IR) {
334 assert(AnalysisPasses.count(PassT::ID()) &&
335 "This analysis pass was not registered prior to being queried");
337 ResultConceptT &ResultConcept =
338 derived_this()->getResultImpl(PassT::ID(), IR);
339 typedef detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
341 return static_cast<ResultModelT &>(ResultConcept).Result;
344 /// \brief Get the cached result of an analysis pass for this module.
346 /// This method never runs the analysis.
348 /// \returns null if there is no cached result.
349 template <typename PassT>
350 typename PassT::Result *getCachedResult(IRUnitT IR) const {
351 assert(AnalysisPasses.count(PassT::ID()) &&
352 "This analysis pass was not registered prior to being queried");
354 ResultConceptT *ResultConcept =
355 derived_this()->getCachedResultImpl(PassT::ID(), IR);
359 typedef detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result>
361 return &static_cast<ResultModelT *>(ResultConcept)->Result;
364 /// \brief Register an analysis pass with the manager.
366 /// This provides an initialized and set-up analysis pass to the analysis
367 /// manager. Whomever is setting up analysis passes must use this to populate
368 /// the manager with all of the analysis passes available.
369 template <typename PassT> void registerPass(PassT Pass) {
370 assert(!AnalysisPasses.count(PassT::ID()) &&
371 "Registered the same analysis pass twice!");
372 typedef detail::AnalysisPassModel<IRUnitT, DerivedT, PassT> PassModelT;
373 AnalysisPasses[PassT::ID()].reset(new PassModelT(std::move(Pass)));
376 /// \brief Invalidate a specific analysis pass for an IR module.
378 /// Note that the analysis result can disregard invalidation.
379 template <typename PassT> void invalidate(IRUnitT IR) {
380 assert(AnalysisPasses.count(PassT::ID()) &&
381 "This analysis pass was not registered prior to being invalidated");
382 derived_this()->invalidateImpl(PassT::ID(), IR);
385 /// \brief Invalidate analyses cached for an IR unit.
387 /// Walk through all of the analyses pertaining to this unit of IR and
388 /// invalidate them unless they are preserved by the PreservedAnalyses set.
389 /// We accept the PreservedAnalyses set by value and update it with each
390 /// analyis pass which has been successfully invalidated and thus can be
391 /// preserved going forward. The updated set is returned.
392 PreservedAnalyses invalidate(IRUnitT IR, PreservedAnalyses PA) {
393 return derived_this()->invalidateImpl(IR, std::move(PA));
397 /// \brief Lookup a registered analysis pass.
398 PassConceptT &lookupPass(void *PassID) {
399 typename AnalysisPassMapT::iterator PI = AnalysisPasses.find(PassID);
400 assert(PI != AnalysisPasses.end() &&
401 "Analysis passes must be registered prior to being queried!");
405 /// \brief Lookup a registered analysis pass.
406 const PassConceptT &lookupPass(void *PassID) const {
407 typename AnalysisPassMapT::const_iterator PI = AnalysisPasses.find(PassID);
408 assert(PI != AnalysisPasses.end() &&
409 "Analysis passes must be registered prior to being queried!");
414 /// \brief Map type from module analysis pass ID to pass concept pointer.
415 typedef DenseMap<void *, std::unique_ptr<PassConceptT>> AnalysisPassMapT;
417 /// \brief Collection of module analysis passes, indexed by ID.
418 AnalysisPassMapT AnalysisPasses;
421 } // End namespace detail
423 /// \brief A module analysis pass manager with lazy running and caching of
425 class ModuleAnalysisManager
426 : public detail::AnalysisManagerBase<ModuleAnalysisManager, Module &> {
427 friend class detail::AnalysisManagerBase<ModuleAnalysisManager, Module &>;
428 typedef detail::AnalysisManagerBase<ModuleAnalysisManager, Module &> BaseT;
429 typedef BaseT::ResultConceptT ResultConceptT;
430 typedef BaseT::PassConceptT PassConceptT;
433 // We have to explicitly define all the special member functions because MSVC
434 // refuses to generate them.
435 ModuleAnalysisManager() {}
436 ModuleAnalysisManager(ModuleAnalysisManager &&Arg)
437 : BaseT(std::move(static_cast<BaseT &>(Arg))),
438 ModuleAnalysisResults(std::move(Arg.ModuleAnalysisResults)) {}
439 ModuleAnalysisManager &operator=(ModuleAnalysisManager &&RHS) {
440 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
441 ModuleAnalysisResults = std::move(RHS.ModuleAnalysisResults);
446 ModuleAnalysisManager(const ModuleAnalysisManager &) LLVM_DELETED_FUNCTION;
447 ModuleAnalysisManager &
448 operator=(const ModuleAnalysisManager &) LLVM_DELETED_FUNCTION;
450 /// \brief Get a module pass result, running the pass if necessary.
451 ResultConceptT &getResultImpl(void *PassID, Module &M);
453 /// \brief Get a cached module pass result or return null.
454 ResultConceptT *getCachedResultImpl(void *PassID, Module &M) const;
456 /// \brief Invalidate a module pass result.
457 void invalidateImpl(void *PassID, Module &M);
459 /// \brief Invalidate results across a module.
460 PreservedAnalyses invalidateImpl(Module &M, PreservedAnalyses PA);
462 /// \brief Map type from module analysis pass ID to pass result concept
464 typedef DenseMap<void *,
465 std::unique_ptr<detail::AnalysisResultConcept<Module &>>>
466 ModuleAnalysisResultMapT;
468 /// \brief Cache of computed module analysis results for this module.
469 ModuleAnalysisResultMapT ModuleAnalysisResults;
472 /// \brief A function analysis manager to coordinate and cache analyses run over
474 class FunctionAnalysisManager
475 : public detail::AnalysisManagerBase<FunctionAnalysisManager, Function &> {
476 friend class detail::AnalysisManagerBase<FunctionAnalysisManager, Function &>;
477 typedef detail::AnalysisManagerBase<FunctionAnalysisManager, Function &>
479 typedef BaseT::ResultConceptT ResultConceptT;
480 typedef BaseT::PassConceptT PassConceptT;
483 // Most public APIs are inherited from the CRTP base class.
485 // We have to explicitly define all the special member functions because MSVC
486 // refuses to generate them.
487 FunctionAnalysisManager() {}
488 FunctionAnalysisManager(FunctionAnalysisManager &&Arg)
489 : BaseT(std::move(static_cast<BaseT &>(Arg))),
490 FunctionAnalysisResults(std::move(Arg.FunctionAnalysisResults)) {}
491 FunctionAnalysisManager &operator=(FunctionAnalysisManager &&RHS) {
492 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
493 FunctionAnalysisResults = std::move(RHS.FunctionAnalysisResults);
497 /// \brief Returns true if the analysis manager has an empty results cache.
500 /// \brief Clear the function analysis result cache.
502 /// This routine allows cleaning up when the set of functions itself has
503 /// potentially changed, and thus we can't even look up a a result and
504 /// invalidate it directly. Notably, this does *not* call invalidate
505 /// functions as there is nothing to be done for them.
509 FunctionAnalysisManager(const FunctionAnalysisManager &)
510 LLVM_DELETED_FUNCTION;
511 FunctionAnalysisManager &
512 operator=(const FunctionAnalysisManager &) LLVM_DELETED_FUNCTION;
514 /// \brief Get a function pass result, running the pass if necessary.
515 ResultConceptT &getResultImpl(void *PassID, Function &F);
517 /// \brief Get a cached function pass result or return null.
518 ResultConceptT *getCachedResultImpl(void *PassID, Function &F) const;
520 /// \brief Invalidate a function pass result.
521 void invalidateImpl(void *PassID, Function &F);
523 /// \brief Invalidate the results for a function..
524 PreservedAnalyses invalidateImpl(Function &F, PreservedAnalyses PA);
526 /// \brief List of function analysis pass IDs and associated concept pointers.
528 /// Requires iterators to be valid across appending new entries and arbitrary
529 /// erases. Provides both the pass ID and concept pointer such that it is
530 /// half of a bijection and provides storage for the actual result concept.
531 typedef std::list<std::pair<
532 void *, std::unique_ptr<detail::AnalysisResultConcept<Function &>>>>
533 FunctionAnalysisResultListT;
535 /// \brief Map type from function pointer to our custom list type.
536 typedef DenseMap<Function *, FunctionAnalysisResultListT>
537 FunctionAnalysisResultListMapT;
539 /// \brief Map from function to a list of function analysis results.
541 /// Provides linear time removal of all analysis results for a function and
542 /// the ultimate storage for a particular cached analysis result.
543 FunctionAnalysisResultListMapT FunctionAnalysisResultLists;
545 /// \brief Map type from a pair of analysis ID and function pointer to an
546 /// iterator into a particular result list.
547 typedef DenseMap<std::pair<void *, Function *>,
548 FunctionAnalysisResultListT::iterator>
549 FunctionAnalysisResultMapT;
551 /// \brief Map from an analysis ID and function to a particular cached
553 FunctionAnalysisResultMapT FunctionAnalysisResults;
556 /// \brief A module analysis which acts as a proxy for a function analysis
559 /// This primarily proxies invalidation information from the module analysis
560 /// manager and module pass manager to a function analysis manager. You should
561 /// never use a function analysis manager from within (transitively) a module
562 /// pass manager unless your parent module pass has received a proxy result
564 class FunctionAnalysisManagerModuleProxy {
568 static void *ID() { return (void *)&PassID; }
570 static StringRef name() { return "FunctionAnalysisManagerModuleProxy"; }
572 explicit FunctionAnalysisManagerModuleProxy(FunctionAnalysisManager &FAM)
574 // We have to explicitly define all the special member functions because MSVC
575 // refuses to generate them.
576 FunctionAnalysisManagerModuleProxy(
577 const FunctionAnalysisManagerModuleProxy &Arg)
579 FunctionAnalysisManagerModuleProxy(FunctionAnalysisManagerModuleProxy &&Arg)
580 : FAM(std::move(Arg.FAM)) {}
581 FunctionAnalysisManagerModuleProxy &
582 operator=(FunctionAnalysisManagerModuleProxy RHS) {
583 std::swap(FAM, RHS.FAM);
587 /// \brief Run the analysis pass and create our proxy result object.
589 /// This doesn't do any interesting work, it is primarily used to insert our
590 /// proxy result object into the module analysis cache so that we can proxy
591 /// invalidation to the function analysis manager.
593 /// In debug builds, it will also assert that the analysis manager is empty
594 /// as no queries should arrive at the function analysis manager prior to
595 /// this analysis being requested.
596 Result run(Module &M);
601 FunctionAnalysisManager *FAM;
604 /// \brief The result proxy object for the
605 /// \c FunctionAnalysisManagerModuleProxy.
607 /// See its documentation for more information.
608 class FunctionAnalysisManagerModuleProxy::Result {
610 explicit Result(FunctionAnalysisManager &FAM) : FAM(&FAM) {}
611 // We have to explicitly define all the special member functions because MSVC
612 // refuses to generate them.
613 Result(const Result &Arg) : FAM(Arg.FAM) {}
614 Result(Result &&Arg) : FAM(std::move(Arg.FAM)) {}
615 Result &operator=(Result RHS) {
616 std::swap(FAM, RHS.FAM);
621 /// \brief Accessor for the \c FunctionAnalysisManager.
622 FunctionAnalysisManager &getManager() { return *FAM; }
624 /// \brief Handler for invalidation of the module.
626 /// If this analysis itself is preserved, then we assume that the set of \c
627 /// Function objects in the \c Module hasn't changed and thus we don't need
628 /// to invalidate *all* cached data associated with a \c Function* in the \c
629 /// FunctionAnalysisManager.
631 /// Regardless of whether this analysis is marked as preserved, all of the
632 /// analyses in the \c FunctionAnalysisManager are potentially invalidated
633 /// based on the set of preserved analyses.
634 bool invalidate(Module &M, const PreservedAnalyses &PA);
637 FunctionAnalysisManager *FAM;
640 /// \brief A function analysis which acts as a proxy for a module analysis
643 /// This primarily provides an accessor to a parent module analysis manager to
644 /// function passes. Only the const interface of the module analysis manager is
645 /// provided to indicate that once inside of a function analysis pass you
646 /// cannot request a module analysis to actually run. Instead, the user must
647 /// rely on the \c getCachedResult API.
649 /// This proxy *doesn't* manage the invalidation in any way. That is handled by
650 /// the recursive return path of each layer of the pass manager and the
651 /// returned PreservedAnalysis set.
652 class ModuleAnalysisManagerFunctionProxy {
654 /// \brief Result proxy object for \c ModuleAnalysisManagerFunctionProxy.
657 explicit Result(const ModuleAnalysisManager &MAM) : MAM(&MAM) {}
658 // We have to explicitly define all the special member functions because
659 // MSVC refuses to generate them.
660 Result(const Result &Arg) : MAM(Arg.MAM) {}
661 Result(Result &&Arg) : MAM(std::move(Arg.MAM)) {}
662 Result &operator=(Result RHS) {
663 std::swap(MAM, RHS.MAM);
667 const ModuleAnalysisManager &getManager() const { return *MAM; }
669 /// \brief Handle invalidation by ignoring it, this pass is immutable.
670 bool invalidate(Function &) { return false; }
673 const ModuleAnalysisManager *MAM;
676 static void *ID() { return (void *)&PassID; }
678 static StringRef name() { return "ModuleAnalysisManagerFunctionProxy"; }
680 ModuleAnalysisManagerFunctionProxy(const ModuleAnalysisManager &MAM)
682 // We have to explicitly define all the special member functions because MSVC
683 // refuses to generate them.
684 ModuleAnalysisManagerFunctionProxy(
685 const ModuleAnalysisManagerFunctionProxy &Arg)
687 ModuleAnalysisManagerFunctionProxy(ModuleAnalysisManagerFunctionProxy &&Arg)
688 : MAM(std::move(Arg.MAM)) {}
689 ModuleAnalysisManagerFunctionProxy &
690 operator=(ModuleAnalysisManagerFunctionProxy RHS) {
691 std::swap(MAM, RHS.MAM);
695 /// \brief Run the analysis pass and create our proxy result object.
696 /// Nothing to see here, it just forwards the \c MAM reference into the
698 Result run(Function &) { return Result(*MAM); }
703 const ModuleAnalysisManager *MAM;
706 /// \brief Trivial adaptor that maps from a module to its functions.
708 /// Designed to allow composition of a FunctionPass(Manager) and
709 /// a ModulePassManager. Note that if this pass is constructed with a pointer
710 /// to a \c ModuleAnalysisManager it will run the
711 /// \c FunctionAnalysisManagerModuleProxy analysis prior to running the function
712 /// pass over the module to enable a \c FunctionAnalysisManager to be used
713 /// within this run safely.
714 template <typename FunctionPassT> class ModuleToFunctionPassAdaptor {
716 explicit ModuleToFunctionPassAdaptor(FunctionPassT Pass)
717 : Pass(std::move(Pass)) {}
718 // We have to explicitly define all the special member functions because MSVC
719 // refuses to generate them.
720 ModuleToFunctionPassAdaptor(const ModuleToFunctionPassAdaptor &Arg)
722 ModuleToFunctionPassAdaptor(ModuleToFunctionPassAdaptor &&Arg)
723 : Pass(std::move(Arg.Pass)) {}
724 friend void swap(ModuleToFunctionPassAdaptor &LHS,
725 ModuleToFunctionPassAdaptor &RHS) {
727 swap(LHS.Pass, RHS.Pass);
729 ModuleToFunctionPassAdaptor &operator=(ModuleToFunctionPassAdaptor RHS) {
734 /// \brief Runs the function pass across every function in the module.
735 PreservedAnalyses run(Module &M, ModuleAnalysisManager *AM) {
736 FunctionAnalysisManager *FAM = nullptr;
738 // Setup the function analysis manager from its proxy.
739 FAM = &AM->getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
741 PreservedAnalyses PA = PreservedAnalyses::all();
742 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
743 PreservedAnalyses PassPA = Pass.run(*I, FAM);
745 // We know that the function pass couldn't have invalidated any other
746 // function's analyses (that's the contract of a function pass), so
747 // directly handle the function analysis manager's invalidation here and
748 // update our preserved set to reflect that these have already been
751 PassPA = FAM->invalidate(*I, std::move(PassPA));
753 // Then intersect the preserved set so that invalidation of module
754 // analyses will eventually occur when the module pass completes.
755 PA.intersect(std::move(PassPA));
758 // By definition we preserve the proxy. This precludes *any* invalidation
759 // of function analyses by the proxy, but that's OK because we've taken
760 // care to invalidate analyses in the function analysis manager
761 // incrementally above.
762 PA.preserve<FunctionAnalysisManagerModuleProxy>();
766 static StringRef name() { return "ModuleToFunctionPassAdaptor"; }
772 /// \brief A function to deduce a function pass type and wrap it in the
773 /// templated adaptor.
774 template <typename FunctionPassT>
775 ModuleToFunctionPassAdaptor<FunctionPassT>
776 createModuleToFunctionPassAdaptor(FunctionPassT Pass) {
777 return std::move(ModuleToFunctionPassAdaptor<FunctionPassT>(std::move(Pass)));
780 /// \brief A template utility pass to force an analysis result to be available.
782 /// This is a no-op pass which simply forces a specific analysis pass's result
783 /// to be available when it is run.
784 template <typename AnalysisT> struct NoopAnalysisRequirementPass {
785 /// \brief Run this pass over some unit of IR.
787 /// This pass can be run over any unit of IR and use any analysis manager
788 /// provided they satisfy the basic API requirements. When this pass is
789 /// created, these methods can be instantiated to satisfy whatever the
790 /// context requires.
791 template <typename T, typename AnalysisManagerT>
792 PreservedAnalyses run(T &&Arg, AnalysisManagerT *AM) {
794 (void)AM->template getResult<AnalysisT>(std::forward<T>(Arg));
796 return PreservedAnalyses::all();
799 static StringRef name() { return "No-op Analysis Requirement Pass"; }
802 /// \brief A template utility pass to force an analysis result to be
805 /// This is a no-op pass which simply forces a specific analysis result to be
806 /// invalidated when it is run.
807 template <typename AnalysisT> struct NoopAnalysisInvalidationPass {
808 /// \brief Run this pass over some unit of IR.
810 /// This pass can be run over any unit of IR and use any analysis manager
811 /// provided they satisfy the basic API requirements. When this pass is
812 /// created, these methods can be instantiated to satisfy whatever the
813 /// context requires.
814 template <typename T, typename AnalysisManagerT>
815 PreservedAnalyses run(T &&Arg, AnalysisManagerT *AM) {
817 // We have to directly invalidate the analysis result as we can't
818 // enumerate all other analyses and use the preserved set to control it.
819 (void)AM->template invalidate<AnalysisT>(std::forward<T>(Arg));
821 return PreservedAnalyses::all();
824 static StringRef name() { return "No-op Analysis Invalidation Pass"; }
827 /// \brief A utility pass that does nothing but preserves no analyses.
829 /// As a consequence fo not preserving any analyses, this pass will force all
830 /// analysis passes to be re-run to produce fresh results if any are needed.
831 struct InvalidateAllAnalysesPass {
832 /// \brief Run this pass over some unit of IR.
833 template <typename T>
834 PreservedAnalyses run(T &&Arg) {
835 return PreservedAnalyses::none();
838 static StringRef name() { return "InvalidateAllAnalysesPass"; }