• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- PassManager.h - Pass management infrastructure -----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 ///
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.
20 ///
21 /// The core IR library provides managers for running passes over
22 /// modules and functions.
23 ///
24 /// * FunctionPassManager can run over a Module, runs each pass over
25 ///   a Function.
26 /// * ModulePassManager must be directly run, runs each pass over the Module.
27 ///
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
35 ///
36 //===----------------------------------------------------------------------===//
37 
38 #ifndef LLVM_IR_PASSMANAGER_H
39 #define LLVM_IR_PASSMANAGER_H
40 
41 #include "llvm/ADT/DenseMap.h"
42 #include "llvm/ADT/SmallPtrSet.h"
43 #include "llvm/ADT/StringRef.h"
44 #include "llvm/ADT/TinyPtrVector.h"
45 #include "llvm/IR/Function.h"
46 #include "llvm/IR/Module.h"
47 #include "llvm/IR/PassManagerInternal.h"
48 #include "llvm/Support/Debug.h"
49 #include "llvm/Support/TypeName.h"
50 #include "llvm/Support/raw_ostream.h"
51 #include <algorithm>
52 #include <cassert>
53 #include <cstring>
54 #include <iterator>
55 #include <list>
56 #include <memory>
57 #include <tuple>
58 #include <type_traits>
59 #include <utility>
60 #include <vector>
61 
62 namespace llvm {
63 
64 /// A special type used by analysis passes to provide an address that
65 /// identifies that particular analysis pass type.
66 ///
67 /// Analysis passes should have a static data member of this type and derive
68 /// from the \c AnalysisInfoMixin to get a static ID method used to identify
69 /// the analysis in the pass management infrastructure.
70 struct alignas(8) AnalysisKey {};
71 
72 /// A special type used to provide an address that identifies a set of related
73 /// analyses.  These sets are primarily used below to mark sets of analyses as
74 /// preserved.
75 ///
76 /// For example, a transformation can indicate that it preserves the CFG of a
77 /// function by preserving the appropriate AnalysisSetKey.  An analysis that
78 /// depends only on the CFG can then check if that AnalysisSetKey is preserved;
79 /// if it is, the analysis knows that it itself is preserved.
80 struct alignas(8) AnalysisSetKey {};
81 
82 /// This templated class represents "all analyses that operate over \<a
83 /// particular IR unit\>" (e.g. a Function or a Module) in instances of
84 /// PreservedAnalysis.
85 ///
86 /// This lets a transformation say e.g. "I preserved all function analyses".
87 ///
88 /// Note that you must provide an explicit instantiation declaration and
89 /// definition for this template in order to get the correct behavior on
90 /// Windows. Otherwise, the address of SetKey will not be stable.
91 template <typename IRUnitT> class AllAnalysesOn {
92 public:
ID()93   static AnalysisSetKey *ID() { return &SetKey; }
94 
95 private:
96   static AnalysisSetKey SetKey;
97 };
98 
99 template <typename IRUnitT> AnalysisSetKey AllAnalysesOn<IRUnitT>::SetKey;
100 
101 extern template class AllAnalysesOn<Module>;
102 extern template class AllAnalysesOn<Function>;
103 
104 /// Represents analyses that only rely on functions' control flow.
105 ///
106 /// This can be used with \c PreservedAnalyses to mark the CFG as preserved and
107 /// to query whether it has been preserved.
108 ///
109 /// The CFG of a function is defined as the set of basic blocks and the edges
110 /// between them. Changing the set of basic blocks in a function is enough to
111 /// mutate the CFG. Mutating the condition of a branch or argument of an
112 /// invoked function does not mutate the CFG, but changing the successor labels
113 /// of those instructions does.
114 class CFGAnalyses {
115 public:
ID()116   static AnalysisSetKey *ID() { return &SetKey; }
117 
118 private:
119   static AnalysisSetKey SetKey;
120 };
121 
122 /// A set of analyses that are preserved following a run of a transformation
123 /// pass.
124 ///
125 /// Transformation passes build and return these objects to communicate which
126 /// analyses are still valid after the transformation. For most passes this is
127 /// fairly simple: if they don't change anything all analyses are preserved,
128 /// otherwise only a short list of analyses that have been explicitly updated
129 /// are preserved.
130 ///
131 /// This class also lets transformation passes mark abstract *sets* of analyses
132 /// as preserved. A transformation that (say) does not alter the CFG can
133 /// indicate such by marking a particular AnalysisSetKey as preserved, and
134 /// then analyses can query whether that AnalysisSetKey is preserved.
135 ///
136 /// Finally, this class can represent an "abandoned" analysis, which is
137 /// not preserved even if it would be covered by some abstract set of analyses.
138 ///
139 /// Given a `PreservedAnalyses` object, an analysis will typically want to
140 /// figure out whether it is preserved. In the example below, MyAnalysisType is
141 /// preserved if it's not abandoned, and (a) it's explicitly marked as
142 /// preserved, (b), the set AllAnalysesOn<MyIRUnit> is preserved, or (c) both
143 /// AnalysisSetA and AnalysisSetB are preserved.
144 ///
145 /// ```
146 ///   auto PAC = PA.getChecker<MyAnalysisType>();
147 ///   if (PAC.preserved() || PAC.preservedSet<AllAnalysesOn<MyIRUnit>>() ||
148 ///       (PAC.preservedSet<AnalysisSetA>() &&
149 ///        PAC.preservedSet<AnalysisSetB>())) {
150 ///     // The analysis has been successfully preserved ...
151 ///   }
152 /// ```
153 class PreservedAnalyses {
154 public:
155   /// Convenience factory function for the empty preserved set.
none()156   static PreservedAnalyses none() { return PreservedAnalyses(); }
157 
158   /// Construct a special preserved set that preserves all passes.
all()159   static PreservedAnalyses all() {
160     PreservedAnalyses PA;
161     PA.PreservedIDs.insert(&AllAnalysesKey);
162     return PA;
163   }
164 
165   /// Construct a preserved analyses object with a single preserved set.
166   template <typename AnalysisSetT>
allInSet()167   static PreservedAnalyses allInSet() {
168     PreservedAnalyses PA;
169     PA.preserveSet<AnalysisSetT>();
170     return PA;
171   }
172 
173   /// Mark an analysis as preserved.
preserve()174   template <typename AnalysisT> void preserve() { preserve(AnalysisT::ID()); }
175 
176   /// Given an analysis's ID, mark the analysis as preserved, adding it
177   /// to the set.
preserve(AnalysisKey * ID)178   void preserve(AnalysisKey *ID) {
179     // Clear this ID from the explicit not-preserved set if present.
180     NotPreservedAnalysisIDs.erase(ID);
181 
182     // If we're not already preserving all analyses (other than those in
183     // NotPreservedAnalysisIDs).
184     if (!areAllPreserved())
185       PreservedIDs.insert(ID);
186   }
187 
188   /// Mark an analysis set as preserved.
preserveSet()189   template <typename AnalysisSetT> void preserveSet() {
190     preserveSet(AnalysisSetT::ID());
191   }
192 
193   /// Mark an analysis set as preserved using its ID.
preserveSet(AnalysisSetKey * ID)194   void preserveSet(AnalysisSetKey *ID) {
195     // If we're not already in the saturated 'all' state, add this set.
196     if (!areAllPreserved())
197       PreservedIDs.insert(ID);
198   }
199 
200   /// Mark an analysis as abandoned.
201   ///
202   /// An abandoned analysis is not preserved, even if it is nominally covered
203   /// by some other set or was previously explicitly marked as preserved.
204   ///
205   /// Note that you can only abandon a specific analysis, not a *set* of
206   /// analyses.
abandon()207   template <typename AnalysisT> void abandon() { abandon(AnalysisT::ID()); }
208 
209   /// Mark an analysis as abandoned using its ID.
210   ///
211   /// An abandoned analysis is not preserved, even if it is nominally covered
212   /// by some other set or was previously explicitly marked as preserved.
213   ///
214   /// Note that you can only abandon a specific analysis, not a *set* of
215   /// analyses.
abandon(AnalysisKey * ID)216   void abandon(AnalysisKey *ID) {
217     PreservedIDs.erase(ID);
218     NotPreservedAnalysisIDs.insert(ID);
219   }
220 
221   /// Intersect this set with another in place.
222   ///
223   /// This is a mutating operation on this preserved set, removing all
224   /// preserved passes which are not also preserved in the argument.
intersect(const PreservedAnalyses & Arg)225   void intersect(const PreservedAnalyses &Arg) {
226     if (Arg.areAllPreserved())
227       return;
228     if (areAllPreserved()) {
229       *this = Arg;
230       return;
231     }
232     // The intersection requires the *union* of the explicitly not-preserved
233     // IDs and the *intersection* of the preserved IDs.
234     for (auto ID : Arg.NotPreservedAnalysisIDs) {
235       PreservedIDs.erase(ID);
236       NotPreservedAnalysisIDs.insert(ID);
237     }
238     for (auto ID : PreservedIDs)
239       if (!Arg.PreservedIDs.count(ID))
240         PreservedIDs.erase(ID);
241   }
242 
243   /// Intersect this set with a temporary other set in place.
244   ///
245   /// This is a mutating operation on this preserved set, removing all
246   /// preserved passes which are not also preserved in the argument.
intersect(PreservedAnalyses && Arg)247   void intersect(PreservedAnalyses &&Arg) {
248     if (Arg.areAllPreserved())
249       return;
250     if (areAllPreserved()) {
251       *this = std::move(Arg);
252       return;
253     }
254     // The intersection requires the *union* of the explicitly not-preserved
255     // IDs and the *intersection* of the preserved IDs.
256     for (auto ID : Arg.NotPreservedAnalysisIDs) {
257       PreservedIDs.erase(ID);
258       NotPreservedAnalysisIDs.insert(ID);
259     }
260     for (auto ID : PreservedIDs)
261       if (!Arg.PreservedIDs.count(ID))
262         PreservedIDs.erase(ID);
263   }
264 
265   /// A checker object that makes it easy to query for whether an analysis or
266   /// some set covering it is preserved.
267   class PreservedAnalysisChecker {
268     friend class PreservedAnalyses;
269 
270     const PreservedAnalyses &PA;
271     AnalysisKey *const ID;
272     const bool IsAbandoned;
273 
274     /// A PreservedAnalysisChecker is tied to a particular Analysis because
275     /// `preserved()` and `preservedSet()` both return false if the Analysis
276     /// was abandoned.
PreservedAnalysisChecker(const PreservedAnalyses & PA,AnalysisKey * ID)277     PreservedAnalysisChecker(const PreservedAnalyses &PA, AnalysisKey *ID)
278         : PA(PA), ID(ID), IsAbandoned(PA.NotPreservedAnalysisIDs.count(ID)) {}
279 
280   public:
281     /// Returns true if the checker's analysis was not abandoned and either
282     ///  - the analysis is explicitly preserved or
283     ///  - all analyses are preserved.
preserved()284     bool preserved() {
285       return !IsAbandoned && (PA.PreservedIDs.count(&AllAnalysesKey) ||
286                               PA.PreservedIDs.count(ID));
287     }
288 
289     /// Returns true if the checker's analysis was not abandoned and either
290     ///  - \p AnalysisSetT is explicitly preserved or
291     ///  - all analyses are preserved.
preservedSet()292     template <typename AnalysisSetT> bool preservedSet() {
293       AnalysisSetKey *SetID = AnalysisSetT::ID();
294       return !IsAbandoned && (PA.PreservedIDs.count(&AllAnalysesKey) ||
295                               PA.PreservedIDs.count(SetID));
296     }
297   };
298 
299   /// Build a checker for this `PreservedAnalyses` and the specified analysis
300   /// type.
301   ///
302   /// You can use the returned object to query whether an analysis was
303   /// preserved. See the example in the comment on `PreservedAnalysis`.
getChecker()304   template <typename AnalysisT> PreservedAnalysisChecker getChecker() const {
305     return PreservedAnalysisChecker(*this, AnalysisT::ID());
306   }
307 
308   /// Build a checker for this `PreservedAnalyses` and the specified analysis
309   /// ID.
310   ///
311   /// You can use the returned object to query whether an analysis was
312   /// preserved. See the example in the comment on `PreservedAnalysis`.
getChecker(AnalysisKey * ID)313   PreservedAnalysisChecker getChecker(AnalysisKey *ID) const {
314     return PreservedAnalysisChecker(*this, ID);
315   }
316 
317   /// Test whether all analyses are preserved (and none are abandoned).
318   ///
319   /// This is used primarily to optimize for the common case of a transformation
320   /// which makes no changes to the IR.
areAllPreserved()321   bool areAllPreserved() const {
322     return NotPreservedAnalysisIDs.empty() &&
323            PreservedIDs.count(&AllAnalysesKey);
324   }
325 
326   /// Directly test whether a set of analyses is preserved.
327   ///
328   /// This is only true when no analyses have been explicitly abandoned.
allAnalysesInSetPreserved()329   template <typename AnalysisSetT> bool allAnalysesInSetPreserved() const {
330     return allAnalysesInSetPreserved(AnalysisSetT::ID());
331   }
332 
333   /// Directly test whether a set of analyses is preserved.
334   ///
335   /// This is only true when no analyses have been explicitly abandoned.
allAnalysesInSetPreserved(AnalysisSetKey * SetID)336   bool allAnalysesInSetPreserved(AnalysisSetKey *SetID) const {
337     return NotPreservedAnalysisIDs.empty() &&
338            (PreservedIDs.count(&AllAnalysesKey) || PreservedIDs.count(SetID));
339   }
340 
341 private:
342   /// A special key used to indicate all analyses.
343   static AnalysisSetKey AllAnalysesKey;
344 
345   /// The IDs of analyses and analysis sets that are preserved.
346   SmallPtrSet<void *, 2> PreservedIDs;
347 
348   /// The IDs of explicitly not-preserved analyses.
349   ///
350   /// If an analysis in this set is covered by a set in `PreservedIDs`, we
351   /// consider it not-preserved. That is, `NotPreservedAnalysisIDs` always
352   /// "wins" over analysis sets in `PreservedIDs`.
353   ///
354   /// Also, a given ID should never occur both here and in `PreservedIDs`.
355   SmallPtrSet<AnalysisKey *, 2> NotPreservedAnalysisIDs;
356 };
357 
358 // Forward declare the analysis manager template.
359 template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
360 
361 /// A CRTP mix-in to automatically provide informational APIs needed for
362 /// passes.
363 ///
364 /// This provides some boilerplate for types that are passes.
365 template <typename DerivedT> struct PassInfoMixin {
366   /// Gets the name of the pass we are mixed into.
namePassInfoMixin367   static StringRef name() {
368     static_assert(std::is_base_of<PassInfoMixin, DerivedT>::value,
369                   "Must pass the derived type as the template argument!");
370     StringRef Name = getTypeName<DerivedT>();
371     if (Name.startswith("llvm::"))
372       Name = Name.drop_front(strlen("llvm::"));
373     return Name;
374   }
375 };
376 
377 /// A CRTP mix-in that provides informational APIs needed for analysis passes.
378 ///
379 /// This provides some boilerplate for types that are analysis passes. It
380 /// automatically mixes in \c PassInfoMixin.
381 template <typename DerivedT>
382 struct AnalysisInfoMixin : PassInfoMixin<DerivedT> {
383   /// Returns an opaque, unique ID for this analysis type.
384   ///
385   /// This ID is a pointer type that is guaranteed to be 8-byte aligned and thus
386   /// suitable for use in sets, maps, and other data structures that use the low
387   /// bits of pointers.
388   ///
389   /// Note that this requires the derived type provide a static \c AnalysisKey
390   /// member called \c Key.
391   ///
392   /// FIXME: The only reason the mixin type itself can't declare the Key value
393   /// is that some compilers cannot correctly unique a templated static variable
394   /// so it has the same addresses in each instantiation. The only currently
395   /// known platform with this limitation is Windows DLL builds, specifically
396   /// building each part of LLVM as a DLL. If we ever remove that build
397   /// configuration, this mixin can provide the static key as well.
IDAnalysisInfoMixin398   static AnalysisKey *ID() {
399     static_assert(std::is_base_of<AnalysisInfoMixin, DerivedT>::value,
400                   "Must pass the derived type as the template argument!");
401     return &DerivedT::Key;
402   }
403 };
404 
405 /// Manages a sequence of passes over a particular unit of IR.
406 ///
407 /// A pass manager contains a sequence of passes to run over a particular unit
408 /// of IR (e.g. Functions, Modules). It is itself a valid pass over that unit of
409 /// IR, and when run over some given IR will run each of its contained passes in
410 /// sequence. Pass managers are the primary and most basic building block of a
411 /// pass pipeline.
412 ///
413 /// When you run a pass manager, you provide an \c AnalysisManager<IRUnitT>
414 /// argument. The pass manager will propagate that analysis manager to each
415 /// pass it runs, and will call the analysis manager's invalidation routine with
416 /// the PreservedAnalyses of each pass it runs.
417 template <typename IRUnitT,
418           typename AnalysisManagerT = AnalysisManager<IRUnitT>,
419           typename... ExtraArgTs>
420 class PassManager : public PassInfoMixin<
421                         PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...>> {
422 public:
423   /// Construct a pass manager.
424   ///
425   /// If \p DebugLogging is true, we'll log our progress to llvm::dbgs().
DebugLogging(DebugLogging)426   explicit PassManager(bool DebugLogging = false) : DebugLogging(DebugLogging) {}
427 
428   // FIXME: These are equivalent to the default move constructor/move
429   // assignment. However, using = default triggers linker errors due to the
430   // explicit instantiations below. Find away to use the default and remove the
431   // duplicated code here.
PassManager(PassManager && Arg)432   PassManager(PassManager &&Arg)
433       : Passes(std::move(Arg.Passes)),
434         DebugLogging(std::move(Arg.DebugLogging)) {}
435 
436   PassManager &operator=(PassManager &&RHS) {
437     Passes = std::move(RHS.Passes);
438     DebugLogging = std::move(RHS.DebugLogging);
439     return *this;
440   }
441 
442   /// Run all of the passes in this manager over the given unit of IR.
443   /// ExtraArgs are passed to each pass.
run(IRUnitT & IR,AnalysisManagerT & AM,ExtraArgTs...ExtraArgs)444   PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM,
445                         ExtraArgTs... ExtraArgs) {
446     PreservedAnalyses PA = PreservedAnalyses::all();
447 
448     if (DebugLogging)
449       dbgs() << "Starting " << getTypeName<IRUnitT>() << " pass manager run.\n";
450 
451     for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
452       if (DebugLogging)
453         dbgs() << "Running pass: " << Passes[Idx]->name() << " on "
454                << IR.getName() << "\n";
455 
456       PreservedAnalyses PassPA = Passes[Idx]->run(IR, AM, ExtraArgs...);
457 
458       // Update the analysis manager as each pass runs and potentially
459       // invalidates analyses.
460       AM.invalidate(IR, PassPA);
461 
462       // Finally, intersect the preserved analyses to compute the aggregate
463       // preserved set for this pass manager.
464       PA.intersect(std::move(PassPA));
465 
466       // FIXME: Historically, the pass managers all called the LLVM context's
467       // yield function here. We don't have a generic way to acquire the
468       // context and it isn't yet clear what the right pattern is for yielding
469       // in the new pass manager so it is currently omitted.
470       //IR.getContext().yield();
471     }
472 
473     // Invalidation was handled after each pass in the above loop for the
474     // current unit of IR. Therefore, the remaining analysis results in the
475     // AnalysisManager are preserved. We mark this with a set so that we don't
476     // need to inspect each one individually.
477     PA.preserveSet<AllAnalysesOn<IRUnitT>>();
478 
479     if (DebugLogging)
480       dbgs() << "Finished " << getTypeName<IRUnitT>() << " pass manager run.\n";
481 
482     return PA;
483   }
484 
addPass(PassT Pass)485   template <typename PassT> void addPass(PassT Pass) {
486     using PassModelT =
487         detail::PassModel<IRUnitT, PassT, PreservedAnalyses, AnalysisManagerT,
488                           ExtraArgTs...>;
489 
490     Passes.emplace_back(new PassModelT(std::move(Pass)));
491   }
492 
493 private:
494   using PassConceptT =
495       detail::PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...>;
496 
497   std::vector<std::unique_ptr<PassConceptT>> Passes;
498 
499   /// Flag indicating whether we should do debug logging.
500   bool DebugLogging;
501 };
502 
503 extern template class PassManager<Module>;
504 
505 /// Convenience typedef for a pass manager over modules.
506 using ModulePassManager = PassManager<Module>;
507 
508 extern template class PassManager<Function>;
509 
510 /// Convenience typedef for a pass manager over functions.
511 using FunctionPassManager = PassManager<Function>;
512 
513 /// A container for analyses that lazily runs them and caches their
514 /// results.
515 ///
516 /// This class can manage analyses for any IR unit where the address of the IR
517 /// unit sufficies as its identity.
518 template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager {
519 public:
520   class Invalidator;
521 
522 private:
523   // Now that we've defined our invalidator, we can define the concept types.
524   using ResultConceptT =
525       detail::AnalysisResultConcept<IRUnitT, PreservedAnalyses, Invalidator>;
526   using PassConceptT =
527       detail::AnalysisPassConcept<IRUnitT, PreservedAnalyses, Invalidator,
528                                   ExtraArgTs...>;
529 
530   /// List of analysis pass IDs and associated concept pointers.
531   ///
532   /// Requires iterators to be valid across appending new entries and arbitrary
533   /// erases. Provides the analysis ID to enable finding iterators to a given
534   /// entry in maps below, and provides the storage for the actual result
535   /// concept.
536   using AnalysisResultListT =
537       std::list<std::pair<AnalysisKey *, std::unique_ptr<ResultConceptT>>>;
538 
539   /// Map type from IRUnitT pointer to our custom list type.
540   using AnalysisResultListMapT = DenseMap<IRUnitT *, AnalysisResultListT>;
541 
542   /// Map type from a pair of analysis ID and IRUnitT pointer to an
543   /// iterator into a particular result list (which is where the actual analysis
544   /// result is stored).
545   using AnalysisResultMapT =
546       DenseMap<std::pair<AnalysisKey *, IRUnitT *>,
547                typename AnalysisResultListT::iterator>;
548 
549 public:
550   /// API to communicate dependencies between analyses during invalidation.
551   ///
552   /// When an analysis result embeds handles to other analysis results, it
553   /// needs to be invalidated both when its own information isn't preserved and
554   /// when any of its embedded analysis results end up invalidated. We pass an
555   /// \c Invalidator object as an argument to \c invalidate() in order to let
556   /// the analysis results themselves define the dependency graph on the fly.
557   /// This lets us avoid building building an explicit representation of the
558   /// dependencies between analysis results.
559   class Invalidator {
560   public:
561     /// Trigger the invalidation of some other analysis pass if not already
562     /// handled and return whether it was in fact invalidated.
563     ///
564     /// This is expected to be called from within a given analysis result's \c
565     /// invalidate method to trigger a depth-first walk of all inter-analysis
566     /// dependencies. The same \p IR unit and \p PA passed to that result's \c
567     /// invalidate method should in turn be provided to this routine.
568     ///
569     /// The first time this is called for a given analysis pass, it will call
570     /// the corresponding result's \c invalidate method.  Subsequent calls will
571     /// use a cache of the results of that initial call.  It is an error to form
572     /// cyclic dependencies between analysis results.
573     ///
574     /// This returns true if the given analysis's result is invalid. Any
575     /// dependecies on it will become invalid as a result.
576     template <typename PassT>
invalidate(IRUnitT & IR,const PreservedAnalyses & PA)577     bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA) {
578       using ResultModelT =
579           detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
580                                       PreservedAnalyses, Invalidator>;
581 
582       return invalidateImpl<ResultModelT>(PassT::ID(), IR, PA);
583     }
584 
585     /// A type-erased variant of the above invalidate method with the same core
586     /// API other than passing an analysis ID rather than an analysis type
587     /// parameter.
588     ///
589     /// This is sadly less efficient than the above routine, which leverages
590     /// the type parameter to avoid the type erasure overhead.
invalidate(AnalysisKey * ID,IRUnitT & IR,const PreservedAnalyses & PA)591     bool invalidate(AnalysisKey *ID, IRUnitT &IR, const PreservedAnalyses &PA) {
592       return invalidateImpl<>(ID, IR, PA);
593     }
594 
595   private:
596     friend class AnalysisManager;
597 
598     template <typename ResultT = ResultConceptT>
invalidateImpl(AnalysisKey * ID,IRUnitT & IR,const PreservedAnalyses & PA)599     bool invalidateImpl(AnalysisKey *ID, IRUnitT &IR,
600                         const PreservedAnalyses &PA) {
601       // If we've already visited this pass, return true if it was invalidated
602       // and false otherwise.
603       auto IMapI = IsResultInvalidated.find(ID);
604       if (IMapI != IsResultInvalidated.end())
605         return IMapI->second;
606 
607       // Otherwise look up the result object.
608       auto RI = Results.find({ID, &IR});
609       assert(RI != Results.end() &&
610              "Trying to invalidate a dependent result that isn't in the "
611              "manager's cache is always an error, likely due to a stale result "
612              "handle!");
613 
614       auto &Result = static_cast<ResultT &>(*RI->second->second);
615 
616       // Insert into the map whether the result should be invalidated and return
617       // that. Note that we cannot reuse IMapI and must do a fresh insert here,
618       // as calling invalidate could (recursively) insert things into the map,
619       // making any iterator or reference invalid.
620       bool Inserted;
621       std::tie(IMapI, Inserted) =
622           IsResultInvalidated.insert({ID, Result.invalidate(IR, PA, *this)});
623       (void)Inserted;
624       assert(Inserted && "Should not have already inserted this ID, likely "
625                          "indicates a dependency cycle!");
626       return IMapI->second;
627     }
628 
Invalidator(SmallDenseMap<AnalysisKey *,bool,8> & IsResultInvalidated,const AnalysisResultMapT & Results)629     Invalidator(SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated,
630                 const AnalysisResultMapT &Results)
631         : IsResultInvalidated(IsResultInvalidated), Results(Results) {}
632 
633     SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated;
634     const AnalysisResultMapT &Results;
635   };
636 
637   /// Construct an empty analysis manager.
638   ///
639   /// If \p DebugLogging is true, we'll log our progress to llvm::dbgs().
DebugLogging(DebugLogging)640   AnalysisManager(bool DebugLogging = false) : DebugLogging(DebugLogging) {}
641   AnalysisManager(AnalysisManager &&) = default;
642   AnalysisManager &operator=(AnalysisManager &&) = default;
643 
644   /// Returns true if the analysis manager has an empty results cache.
empty()645   bool empty() const {
646     assert(AnalysisResults.empty() == AnalysisResultLists.empty() &&
647            "The storage and index of analysis results disagree on how many "
648            "there are!");
649     return AnalysisResults.empty();
650   }
651 
652   /// Clear any cached analysis results for a single unit of IR.
653   ///
654   /// This doesn't invalidate, but instead simply deletes, the relevant results.
655   /// It is useful when the IR is being removed and we want to clear out all the
656   /// memory pinned for it.
clear(IRUnitT & IR,llvm::StringRef Name)657   void clear(IRUnitT &IR, llvm::StringRef Name) {
658     if (DebugLogging)
659       dbgs() << "Clearing all analysis results for: " << Name << "\n";
660 
661     auto ResultsListI = AnalysisResultLists.find(&IR);
662     if (ResultsListI == AnalysisResultLists.end())
663       return;
664     // Delete the map entries that point into the results list.
665     for (auto &IDAndResult : ResultsListI->second)
666       AnalysisResults.erase({IDAndResult.first, &IR});
667 
668     // And actually destroy and erase the results associated with this IR.
669     AnalysisResultLists.erase(ResultsListI);
670   }
671 
672   /// Clear all analysis results cached by this AnalysisManager.
673   ///
674   /// Like \c clear(IRUnitT&), this doesn't invalidate the results; it simply
675   /// deletes them.  This lets you clean up the AnalysisManager when the set of
676   /// IR units itself has potentially changed, and thus we can't even look up a
677   /// a result and invalidate/clear it directly.
clear()678   void clear() {
679     AnalysisResults.clear();
680     AnalysisResultLists.clear();
681   }
682 
683   /// Get the result of an analysis pass for a given IR unit.
684   ///
685   /// Runs the analysis if a cached result is not available.
686   template <typename PassT>
getResult(IRUnitT & IR,ExtraArgTs...ExtraArgs)687   typename PassT::Result &getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs) {
688     assert(AnalysisPasses.count(PassT::ID()) &&
689            "This analysis pass was not registered prior to being queried");
690     ResultConceptT &ResultConcept =
691         getResultImpl(PassT::ID(), IR, ExtraArgs...);
692 
693     using ResultModelT =
694         detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
695                                     PreservedAnalyses, Invalidator>;
696 
697     return static_cast<ResultModelT &>(ResultConcept).Result;
698   }
699 
700   /// Get the cached result of an analysis pass for a given IR unit.
701   ///
702   /// This method never runs the analysis.
703   ///
704   /// \returns null if there is no cached result.
705   template <typename PassT>
getCachedResult(IRUnitT & IR)706   typename PassT::Result *getCachedResult(IRUnitT &IR) const {
707     assert(AnalysisPasses.count(PassT::ID()) &&
708            "This analysis pass was not registered prior to being queried");
709 
710     ResultConceptT *ResultConcept = getCachedResultImpl(PassT::ID(), IR);
711     if (!ResultConcept)
712       return nullptr;
713 
714     using ResultModelT =
715         detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
716                                     PreservedAnalyses, Invalidator>;
717 
718     return &static_cast<ResultModelT *>(ResultConcept)->Result;
719   }
720 
721   /// Register an analysis pass with the manager.
722   ///
723   /// The parameter is a callable whose result is an analysis pass. This allows
724   /// passing in a lambda to construct the analysis.
725   ///
726   /// The analysis type to register is the type returned by calling the \c
727   /// PassBuilder argument. If that type has already been registered, then the
728   /// argument will not be called and this function will return false.
729   /// Otherwise, we register the analysis returned by calling \c PassBuilder(),
730   /// and this function returns true.
731   ///
732   /// (Note: Although the return value of this function indicates whether or not
733   /// an analysis was previously registered, there intentionally isn't a way to
734   /// query this directly.  Instead, you should just register all the analyses
735   /// you might want and let this class run them lazily.  This idiom lets us
736   /// minimize the number of times we have to look up analyses in our
737   /// hashtable.)
738   template <typename PassBuilderT>
registerPass(PassBuilderT && PassBuilder)739   bool registerPass(PassBuilderT &&PassBuilder) {
740     using PassT = decltype(PassBuilder());
741     using PassModelT =
742         detail::AnalysisPassModel<IRUnitT, PassT, PreservedAnalyses,
743                                   Invalidator, ExtraArgTs...>;
744 
745     auto &PassPtr = AnalysisPasses[PassT::ID()];
746     if (PassPtr)
747       // Already registered this pass type!
748       return false;
749 
750     // Construct a new model around the instance returned by the builder.
751     PassPtr.reset(new PassModelT(PassBuilder()));
752     return true;
753   }
754 
755   /// Invalidate a specific analysis pass for an IR module.
756   ///
757   /// Note that the analysis result can disregard invalidation, if it determines
758   /// it is in fact still valid.
invalidate(IRUnitT & IR)759   template <typename PassT> void invalidate(IRUnitT &IR) {
760     assert(AnalysisPasses.count(PassT::ID()) &&
761            "This analysis pass was not registered prior to being invalidated");
762     invalidateImpl(PassT::ID(), IR);
763   }
764 
765   /// Invalidate cached analyses for an IR unit.
766   ///
767   /// Walk through all of the analyses pertaining to this unit of IR and
768   /// invalidate them, unless they are preserved by the PreservedAnalyses set.
invalidate(IRUnitT & IR,const PreservedAnalyses & PA)769   void invalidate(IRUnitT &IR, const PreservedAnalyses &PA) {
770     // We're done if all analyses on this IR unit are preserved.
771     if (PA.allAnalysesInSetPreserved<AllAnalysesOn<IRUnitT>>())
772       return;
773 
774     if (DebugLogging)
775       dbgs() << "Invalidating all non-preserved analyses for: " << IR.getName()
776              << "\n";
777 
778     // Track whether each analysis's result is invalidated in
779     // IsResultInvalidated.
780     SmallDenseMap<AnalysisKey *, bool, 8> IsResultInvalidated;
781     Invalidator Inv(IsResultInvalidated, AnalysisResults);
782     AnalysisResultListT &ResultsList = AnalysisResultLists[&IR];
783     for (auto &AnalysisResultPair : ResultsList) {
784       // This is basically the same thing as Invalidator::invalidate, but we
785       // can't call it here because we're operating on the type-erased result.
786       // Moreover if we instead called invalidate() directly, it would do an
787       // unnecessary look up in ResultsList.
788       AnalysisKey *ID = AnalysisResultPair.first;
789       auto &Result = *AnalysisResultPair.second;
790 
791       auto IMapI = IsResultInvalidated.find(ID);
792       if (IMapI != IsResultInvalidated.end())
793         // This result was already handled via the Invalidator.
794         continue;
795 
796       // Try to invalidate the result, giving it the Invalidator so it can
797       // recursively query for any dependencies it has and record the result.
798       // Note that we cannot reuse 'IMapI' here or pre-insert the ID, as
799       // Result.invalidate may insert things into the map, invalidating our
800       // iterator.
801       bool Inserted =
802           IsResultInvalidated.insert({ID, Result.invalidate(IR, PA, Inv)})
803               .second;
804       (void)Inserted;
805       assert(Inserted && "Should never have already inserted this ID, likely "
806                          "indicates a cycle!");
807     }
808 
809     // Now erase the results that were marked above as invalidated.
810     if (!IsResultInvalidated.empty()) {
811       for (auto I = ResultsList.begin(), E = ResultsList.end(); I != E;) {
812         AnalysisKey *ID = I->first;
813         if (!IsResultInvalidated.lookup(ID)) {
814           ++I;
815           continue;
816         }
817 
818         if (DebugLogging)
819           dbgs() << "Invalidating analysis: " << this->lookUpPass(ID).name()
820                  << " on " << IR.getName() << "\n";
821 
822         I = ResultsList.erase(I);
823         AnalysisResults.erase({ID, &IR});
824       }
825     }
826 
827     if (ResultsList.empty())
828       AnalysisResultLists.erase(&IR);
829   }
830 
831 private:
832   /// Look up a registered analysis pass.
lookUpPass(AnalysisKey * ID)833   PassConceptT &lookUpPass(AnalysisKey *ID) {
834     typename AnalysisPassMapT::iterator PI = AnalysisPasses.find(ID);
835     assert(PI != AnalysisPasses.end() &&
836            "Analysis passes must be registered prior to being queried!");
837     return *PI->second;
838   }
839 
840   /// Look up a registered analysis pass.
lookUpPass(AnalysisKey * ID)841   const PassConceptT &lookUpPass(AnalysisKey *ID) const {
842     typename AnalysisPassMapT::const_iterator PI = AnalysisPasses.find(ID);
843     assert(PI != AnalysisPasses.end() &&
844            "Analysis passes must be registered prior to being queried!");
845     return *PI->second;
846   }
847 
848   /// Get an analysis result, running the pass if necessary.
getResultImpl(AnalysisKey * ID,IRUnitT & IR,ExtraArgTs...ExtraArgs)849   ResultConceptT &getResultImpl(AnalysisKey *ID, IRUnitT &IR,
850                                 ExtraArgTs... ExtraArgs) {
851     typename AnalysisResultMapT::iterator RI;
852     bool Inserted;
853     std::tie(RI, Inserted) = AnalysisResults.insert(std::make_pair(
854         std::make_pair(ID, &IR), typename AnalysisResultListT::iterator()));
855 
856     // If we don't have a cached result for this function, look up the pass and
857     // run it to produce a result, which we then add to the cache.
858     if (Inserted) {
859       auto &P = this->lookUpPass(ID);
860       if (DebugLogging)
861         dbgs() << "Running analysis: " << P.name() << " on " << IR.getName()
862                << "\n";
863       AnalysisResultListT &ResultList = AnalysisResultLists[&IR];
864       ResultList.emplace_back(ID, P.run(IR, *this, ExtraArgs...));
865 
866       // P.run may have inserted elements into AnalysisResults and invalidated
867       // RI.
868       RI = AnalysisResults.find({ID, &IR});
869       assert(RI != AnalysisResults.end() && "we just inserted it!");
870 
871       RI->second = std::prev(ResultList.end());
872     }
873 
874     return *RI->second->second;
875   }
876 
877   /// Get a cached analysis result or return null.
getCachedResultImpl(AnalysisKey * ID,IRUnitT & IR)878   ResultConceptT *getCachedResultImpl(AnalysisKey *ID, IRUnitT &IR) const {
879     typename AnalysisResultMapT::const_iterator RI =
880         AnalysisResults.find({ID, &IR});
881     return RI == AnalysisResults.end() ? nullptr : &*RI->second->second;
882   }
883 
884   /// Invalidate a function pass result.
invalidateImpl(AnalysisKey * ID,IRUnitT & IR)885   void invalidateImpl(AnalysisKey *ID, IRUnitT &IR) {
886     typename AnalysisResultMapT::iterator RI =
887         AnalysisResults.find({ID, &IR});
888     if (RI == AnalysisResults.end())
889       return;
890 
891     if (DebugLogging)
892       dbgs() << "Invalidating analysis: " << this->lookUpPass(ID).name()
893              << " on " << IR.getName() << "\n";
894     AnalysisResultLists[&IR].erase(RI->second);
895     AnalysisResults.erase(RI);
896   }
897 
898   /// Map type from module analysis pass ID to pass concept pointer.
899   using AnalysisPassMapT =
900       DenseMap<AnalysisKey *, std::unique_ptr<PassConceptT>>;
901 
902   /// Collection of module analysis passes, indexed by ID.
903   AnalysisPassMapT AnalysisPasses;
904 
905   /// Map from function to a list of function analysis results.
906   ///
907   /// Provides linear time removal of all analysis results for a function and
908   /// the ultimate storage for a particular cached analysis result.
909   AnalysisResultListMapT AnalysisResultLists;
910 
911   /// Map from an analysis ID and function to a particular cached
912   /// analysis result.
913   AnalysisResultMapT AnalysisResults;
914 
915   /// Indicates whether we log to \c llvm::dbgs().
916   bool DebugLogging;
917 };
918 
919 extern template class AnalysisManager<Module>;
920 
921 /// Convenience typedef for the Module analysis manager.
922 using ModuleAnalysisManager = AnalysisManager<Module>;
923 
924 extern template class AnalysisManager<Function>;
925 
926 /// Convenience typedef for the Function analysis manager.
927 using FunctionAnalysisManager = AnalysisManager<Function>;
928 
929 /// An analysis over an "outer" IR unit that provides access to an
930 /// analysis manager over an "inner" IR unit.  The inner unit must be contained
931 /// in the outer unit.
932 ///
933 /// Fore example, InnerAnalysisManagerProxy<FunctionAnalysisManager, Module> is
934 /// an analysis over Modules (the "outer" unit) that provides access to a
935 /// Function analysis manager.  The FunctionAnalysisManager is the "inner"
936 /// manager being proxied, and Functions are the "inner" unit.  The inner/outer
937 /// relationship is valid because each Function is contained in one Module.
938 ///
939 /// If you're (transitively) within a pass manager for an IR unit U that
940 /// contains IR unit V, you should never use an analysis manager over V, except
941 /// via one of these proxies.
942 ///
943 /// Note that the proxy's result is a move-only RAII object.  The validity of
944 /// the analyses in the inner analysis manager is tied to its lifetime.
945 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
946 class InnerAnalysisManagerProxy
947     : public AnalysisInfoMixin<
948           InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>> {
949 public:
950   class Result {
951   public:
Result(AnalysisManagerT & InnerAM)952     explicit Result(AnalysisManagerT &InnerAM) : InnerAM(&InnerAM) {}
953 
Result(Result && Arg)954     Result(Result &&Arg) : InnerAM(std::move(Arg.InnerAM)) {
955       // We have to null out the analysis manager in the moved-from state
956       // because we are taking ownership of the responsibilty to clear the
957       // analysis state.
958       Arg.InnerAM = nullptr;
959     }
960 
~Result()961     ~Result() {
962       // InnerAM is cleared in a moved from state where there is nothing to do.
963       if (!InnerAM)
964         return;
965 
966       // Clear out the analysis manager if we're being destroyed -- it means we
967       // didn't even see an invalidate call when we got invalidated.
968       InnerAM->clear();
969     }
970 
971     Result &operator=(Result &&RHS) {
972       InnerAM = RHS.InnerAM;
973       // We have to null out the analysis manager in the moved-from state
974       // because we are taking ownership of the responsibilty to clear the
975       // analysis state.
976       RHS.InnerAM = nullptr;
977       return *this;
978     }
979 
980     /// Accessor for the analysis manager.
getManager()981     AnalysisManagerT &getManager() { return *InnerAM; }
982 
983     /// Handler for invalidation of the outer IR unit, \c IRUnitT.
984     ///
985     /// If the proxy analysis itself is not preserved, we assume that the set of
986     /// inner IR objects contained in IRUnit may have changed.  In this case,
987     /// we have to call \c clear() on the inner analysis manager, as it may now
988     /// have stale pointers to its inner IR objects.
989     ///
990     /// Regardless of whether the proxy analysis is marked as preserved, all of
991     /// the analyses in the inner analysis manager are potentially invalidated
992     /// based on the set of preserved analyses.
993     bool invalidate(
994         IRUnitT &IR, const PreservedAnalyses &PA,
995         typename AnalysisManager<IRUnitT, ExtraArgTs...>::Invalidator &Inv);
996 
997   private:
998     AnalysisManagerT *InnerAM;
999   };
1000 
InnerAnalysisManagerProxy(AnalysisManagerT & InnerAM)1001   explicit InnerAnalysisManagerProxy(AnalysisManagerT &InnerAM)
1002       : InnerAM(&InnerAM) {}
1003 
1004   /// Run the analysis pass and create our proxy result object.
1005   ///
1006   /// This doesn't do any interesting work; it is primarily used to insert our
1007   /// proxy result object into the outer analysis cache so that we can proxy
1008   /// invalidation to the inner analysis manager.
run(IRUnitT & IR,AnalysisManager<IRUnitT,ExtraArgTs...> & AM,ExtraArgTs...)1009   Result run(IRUnitT &IR, AnalysisManager<IRUnitT, ExtraArgTs...> &AM,
1010              ExtraArgTs...) {
1011     return Result(*InnerAM);
1012   }
1013 
1014 private:
1015   friend AnalysisInfoMixin<
1016       InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>>;
1017 
1018   static AnalysisKey Key;
1019 
1020   AnalysisManagerT *InnerAM;
1021 };
1022 
1023 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1024 AnalysisKey
1025     InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
1026 
1027 /// Provide the \c FunctionAnalysisManager to \c Module proxy.
1028 using FunctionAnalysisManagerModuleProxy =
1029     InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>;
1030 
1031 /// Specialization of the invalidate method for the \c
1032 /// FunctionAnalysisManagerModuleProxy's result.
1033 template <>
1034 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
1035     Module &M, const PreservedAnalyses &PA,
1036     ModuleAnalysisManager::Invalidator &Inv);
1037 
1038 // Ensure the \c FunctionAnalysisManagerModuleProxy is provided as an extern
1039 // template.
1040 extern template class InnerAnalysisManagerProxy<FunctionAnalysisManager,
1041                                                 Module>;
1042 
1043 /// An analysis over an "inner" IR unit that provides access to an
1044 /// analysis manager over a "outer" IR unit.  The inner unit must be contained
1045 /// in the outer unit.
1046 ///
1047 /// For example OuterAnalysisManagerProxy<ModuleAnalysisManager, Function> is an
1048 /// analysis over Functions (the "inner" unit) which provides access to a Module
1049 /// analysis manager.  The ModuleAnalysisManager is the "outer" manager being
1050 /// proxied, and Modules are the "outer" IR unit.  The inner/outer relationship
1051 /// is valid because each Function is contained in one Module.
1052 ///
1053 /// This proxy only exposes the const interface of the outer analysis manager,
1054 /// to indicate that you cannot cause an outer analysis to run from within an
1055 /// inner pass.  Instead, you must rely on the \c getCachedResult API.
1056 ///
1057 /// This proxy doesn't manage invalidation in any way -- that is handled by the
1058 /// recursive return path of each layer of the pass manager.  A consequence of
1059 /// this is the outer analyses may be stale.  We invalidate the outer analyses
1060 /// only when we're done running passes over the inner IR units.
1061 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1062 class OuterAnalysisManagerProxy
1063     : public AnalysisInfoMixin<
1064           OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>> {
1065 public:
1066   /// Result proxy object for \c OuterAnalysisManagerProxy.
1067   class Result {
1068   public:
Result(const AnalysisManagerT & AM)1069     explicit Result(const AnalysisManagerT &AM) : AM(&AM) {}
1070 
getManager()1071     const AnalysisManagerT &getManager() const { return *AM; }
1072 
1073     /// When invalidation occurs, remove any registered invalidation events.
invalidate(IRUnitT & IRUnit,const PreservedAnalyses & PA,typename AnalysisManager<IRUnitT,ExtraArgTs...>::Invalidator & Inv)1074     bool invalidate(
1075         IRUnitT &IRUnit, const PreservedAnalyses &PA,
1076         typename AnalysisManager<IRUnitT, ExtraArgTs...>::Invalidator &Inv) {
1077       // Loop over the set of registered outer invalidation mappings and if any
1078       // of them map to an analysis that is now invalid, clear it out.
1079       SmallVector<AnalysisKey *, 4> DeadKeys;
1080       for (auto &KeyValuePair : OuterAnalysisInvalidationMap) {
1081         AnalysisKey *OuterID = KeyValuePair.first;
1082         auto &InnerIDs = KeyValuePair.second;
1083         InnerIDs.erase(llvm::remove_if(InnerIDs, [&](AnalysisKey *InnerID) {
1084           return Inv.invalidate(InnerID, IRUnit, PA); }),
1085                        InnerIDs.end());
1086         if (InnerIDs.empty())
1087           DeadKeys.push_back(OuterID);
1088       }
1089 
1090       for (auto OuterID : DeadKeys)
1091         OuterAnalysisInvalidationMap.erase(OuterID);
1092 
1093       // The proxy itself remains valid regardless of anything else.
1094       return false;
1095     }
1096 
1097     /// Register a deferred invalidation event for when the outer analysis
1098     /// manager processes its invalidations.
1099     template <typename OuterAnalysisT, typename InvalidatedAnalysisT>
registerOuterAnalysisInvalidation()1100     void registerOuterAnalysisInvalidation() {
1101       AnalysisKey *OuterID = OuterAnalysisT::ID();
1102       AnalysisKey *InvalidatedID = InvalidatedAnalysisT::ID();
1103 
1104       auto &InvalidatedIDList = OuterAnalysisInvalidationMap[OuterID];
1105       // Note, this is a linear scan. If we end up with large numbers of
1106       // analyses that all trigger invalidation on the same outer analysis,
1107       // this entire system should be changed to some other deterministic
1108       // data structure such as a `SetVector` of a pair of pointers.
1109       auto InvalidatedIt = std::find(InvalidatedIDList.begin(),
1110                                      InvalidatedIDList.end(), InvalidatedID);
1111       if (InvalidatedIt == InvalidatedIDList.end())
1112         InvalidatedIDList.push_back(InvalidatedID);
1113     }
1114 
1115     /// Access the map from outer analyses to deferred invalidation requiring
1116     /// analyses.
1117     const SmallDenseMap<AnalysisKey *, TinyPtrVector<AnalysisKey *>, 2> &
getOuterInvalidations()1118     getOuterInvalidations() const {
1119       return OuterAnalysisInvalidationMap;
1120     }
1121 
1122   private:
1123     const AnalysisManagerT *AM;
1124 
1125     /// A map from an outer analysis ID to the set of this IR-unit's analyses
1126     /// which need to be invalidated.
1127     SmallDenseMap<AnalysisKey *, TinyPtrVector<AnalysisKey *>, 2>
1128         OuterAnalysisInvalidationMap;
1129   };
1130 
OuterAnalysisManagerProxy(const AnalysisManagerT & AM)1131   OuterAnalysisManagerProxy(const AnalysisManagerT &AM) : AM(&AM) {}
1132 
1133   /// Run the analysis pass and create our proxy result object.
1134   /// Nothing to see here, it just forwards the \c AM reference into the
1135   /// result.
run(IRUnitT &,AnalysisManager<IRUnitT,ExtraArgTs...> &,ExtraArgTs...)1136   Result run(IRUnitT &, AnalysisManager<IRUnitT, ExtraArgTs...> &,
1137              ExtraArgTs...) {
1138     return Result(*AM);
1139   }
1140 
1141 private:
1142   friend AnalysisInfoMixin<
1143       OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>>;
1144 
1145   static AnalysisKey Key;
1146 
1147   const AnalysisManagerT *AM;
1148 };
1149 
1150 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1151 AnalysisKey
1152     OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
1153 
1154 extern template class OuterAnalysisManagerProxy<ModuleAnalysisManager,
1155                                                 Function>;
1156 /// Provide the \c ModuleAnalysisManager to \c Function proxy.
1157 using ModuleAnalysisManagerFunctionProxy =
1158     OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>;
1159 
1160 /// Trivial adaptor that maps from a module to its functions.
1161 ///
1162 /// Designed to allow composition of a FunctionPass(Manager) and
1163 /// a ModulePassManager, by running the FunctionPass(Manager) over every
1164 /// function in the module.
1165 ///
1166 /// Function passes run within this adaptor can rely on having exclusive access
1167 /// to the function they are run over. They should not read or modify any other
1168 /// functions! Other threads or systems may be manipulating other functions in
1169 /// the module, and so their state should never be relied on.
1170 /// FIXME: Make the above true for all of LLVM's actual passes, some still
1171 /// violate this principle.
1172 ///
1173 /// Function passes can also read the module containing the function, but they
1174 /// should not modify that module outside of the use lists of various globals.
1175 /// For example, a function pass is not permitted to add functions to the
1176 /// module.
1177 /// FIXME: Make the above true for all of LLVM's actual passes, some still
1178 /// violate this principle.
1179 ///
1180 /// Note that although function passes can access module analyses, module
1181 /// analyses are not invalidated while the function passes are running, so they
1182 /// may be stale.  Function analyses will not be stale.
1183 template <typename FunctionPassT>
1184 class ModuleToFunctionPassAdaptor
1185     : public PassInfoMixin<ModuleToFunctionPassAdaptor<FunctionPassT>> {
1186 public:
ModuleToFunctionPassAdaptor(FunctionPassT Pass)1187   explicit ModuleToFunctionPassAdaptor(FunctionPassT Pass)
1188       : Pass(std::move(Pass)) {}
1189 
1190   /// Runs the function pass across every function in the module.
run(Module & M,ModuleAnalysisManager & AM)1191   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM) {
1192     FunctionAnalysisManager &FAM =
1193         AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
1194 
1195     PreservedAnalyses PA = PreservedAnalyses::all();
1196     for (Function &F : M) {
1197       if (F.isDeclaration())
1198         continue;
1199 
1200       PreservedAnalyses PassPA = Pass.run(F, FAM);
1201 
1202       // We know that the function pass couldn't have invalidated any other
1203       // function's analyses (that's the contract of a function pass), so
1204       // directly handle the function analysis manager's invalidation here.
1205       FAM.invalidate(F, PassPA);
1206 
1207       // Then intersect the preserved set so that invalidation of module
1208       // analyses will eventually occur when the module pass completes.
1209       PA.intersect(std::move(PassPA));
1210     }
1211 
1212     // The FunctionAnalysisManagerModuleProxy is preserved because (we assume)
1213     // the function passes we ran didn't add or remove any functions.
1214     //
1215     // We also preserve all analyses on Functions, because we did all the
1216     // invalidation we needed to do above.
1217     PA.preserveSet<AllAnalysesOn<Function>>();
1218     PA.preserve<FunctionAnalysisManagerModuleProxy>();
1219     return PA;
1220   }
1221 
1222 private:
1223   FunctionPassT Pass;
1224 };
1225 
1226 /// A function to deduce a function pass type and wrap it in the
1227 /// templated adaptor.
1228 template <typename FunctionPassT>
1229 ModuleToFunctionPassAdaptor<FunctionPassT>
createModuleToFunctionPassAdaptor(FunctionPassT Pass)1230 createModuleToFunctionPassAdaptor(FunctionPassT Pass) {
1231   return ModuleToFunctionPassAdaptor<FunctionPassT>(std::move(Pass));
1232 }
1233 
1234 /// A utility pass template to force an analysis result to be available.
1235 ///
1236 /// If there are extra arguments at the pass's run level there may also be
1237 /// extra arguments to the analysis manager's \c getResult routine. We can't
1238 /// guess how to effectively map the arguments from one to the other, and so
1239 /// this specialization just ignores them.
1240 ///
1241 /// Specific patterns of run-method extra arguments and analysis manager extra
1242 /// arguments will have to be defined as appropriate specializations.
1243 template <typename AnalysisT, typename IRUnitT,
1244           typename AnalysisManagerT = AnalysisManager<IRUnitT>,
1245           typename... ExtraArgTs>
1246 struct RequireAnalysisPass
1247     : PassInfoMixin<RequireAnalysisPass<AnalysisT, IRUnitT, AnalysisManagerT,
1248                                         ExtraArgTs...>> {
1249   /// Run this pass over some unit of IR.
1250   ///
1251   /// This pass can be run over any unit of IR and use any analysis manager
1252   /// provided they satisfy the basic API requirements. When this pass is
1253   /// created, these methods can be instantiated to satisfy whatever the
1254   /// context requires.
runRequireAnalysisPass1255   PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM,
1256                         ExtraArgTs &&... Args) {
1257     (void)AM.template getResult<AnalysisT>(Arg,
1258                                            std::forward<ExtraArgTs>(Args)...);
1259 
1260     return PreservedAnalyses::all();
1261   }
1262 };
1263 
1264 /// A no-op pass template which simply forces a specific analysis result
1265 /// to be invalidated.
1266 template <typename AnalysisT>
1267 struct InvalidateAnalysisPass
1268     : PassInfoMixin<InvalidateAnalysisPass<AnalysisT>> {
1269   /// Run this pass over some unit of IR.
1270   ///
1271   /// This pass can be run over any unit of IR and use any analysis manager,
1272   /// provided they satisfy the basic API requirements. When this pass is
1273   /// created, these methods can be instantiated to satisfy whatever the
1274   /// context requires.
1275   template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
runInvalidateAnalysisPass1276   PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&...) {
1277     auto PA = PreservedAnalyses::all();
1278     PA.abandon<AnalysisT>();
1279     return PA;
1280   }
1281 };
1282 
1283 /// A utility pass that does nothing, but preserves no analyses.
1284 ///
1285 /// Because this preserves no analyses, any analysis passes queried after this
1286 /// pass runs will recompute fresh results.
1287 struct InvalidateAllAnalysesPass : PassInfoMixin<InvalidateAllAnalysesPass> {
1288   /// Run this pass over some unit of IR.
1289   template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
runInvalidateAllAnalysesPass1290   PreservedAnalyses run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) {
1291     return PreservedAnalyses::none();
1292   }
1293 };
1294 
1295 /// A utility pass template that simply runs another pass multiple times.
1296 ///
1297 /// This can be useful when debugging or testing passes. It also serves as an
1298 /// example of how to extend the pass manager in ways beyond composition.
1299 template <typename PassT>
1300 class RepeatedPass : public PassInfoMixin<RepeatedPass<PassT>> {
1301 public:
RepeatedPass(int Count,PassT P)1302   RepeatedPass(int Count, PassT P) : Count(Count), P(std::move(P)) {}
1303 
1304   template <typename IRUnitT, typename AnalysisManagerT, typename... Ts>
run(IRUnitT & Arg,AnalysisManagerT & AM,Ts &&...Args)1305   PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, Ts &&... Args) {
1306     auto PA = PreservedAnalyses::all();
1307     for (int i = 0; i < Count; ++i)
1308       PA.intersect(P.run(Arg, AM, std::forward<Ts>(Args)...));
1309     return PA;
1310   }
1311 
1312 private:
1313   int Count;
1314   PassT P;
1315 };
1316 
1317 template <typename PassT>
createRepeatedPass(int Count,PassT P)1318 RepeatedPass<PassT> createRepeatedPass(int Count, PassT P) {
1319   return RepeatedPass<PassT>(Count, std::move(P));
1320 }
1321 
1322 } // end namespace llvm
1323 
1324 #endif // LLVM_IR_PASSMANAGER_H
1325