LCOV - code coverage report
Current view: top level - include/llvm/IR - PassManager.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 1878 3312 56.7 %
Date: 2018-10-20 13:21:21 Functions: 447 1100 40.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13