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:
ID()94   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:
ID()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.
none()157   static PreservedAnalyses none() { return PreservedAnalyses(); }
158 
159   /// Construct a special preserved set that preserves all passes.
all()160   static PreservedAnalyses all() {
161     PreservedAnalyses PA;
162     PA.PreservedIDs.insert(&AllAnalysesKey);
163     return PA;
164   }
165 
166   /// Construct a preserved analyses object with a single preserved set.
167   template <typename AnalysisSetT>
allInSet()168   static PreservedAnalyses allInSet() {
169     PreservedAnalyses PA;
170     PA.preserveSet<AnalysisSetT>();
171     return PA;
172   }
173 
174   /// Mark an analysis as preserved.
preserve()175   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.
preserve(AnalysisKey * ID)179   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       PreservedIDs.insert(ID);
187   }
188 
189   /// Mark an analysis set as preserved.
preserveSet()190   template <typename AnalysisSetT> void preserveSet() {
191     preserveSet(AnalysisSetT::ID());
192   }
193 
194   /// Mark an analysis set as preserved using its ID.
preserveSet(AnalysisSetKey * ID)195   void preserveSet(AnalysisSetKey *ID) {
196     // If we're not already in the saturated 'all' state, add this set.
197     if (!areAllPreserved())
198       PreservedIDs.insert(ID);
199   }
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.
abandon()208   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.
abandon(AnalysisKey * ID)217   void abandon(AnalysisKey *ID) {
218     PreservedIDs.erase(ID);
219     NotPreservedAnalysisIDs.insert(ID);
220   }
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.
intersect(const PreservedAnalyses & Arg)226   void intersect(const PreservedAnalyses &Arg) {
227     if (Arg.areAllPreserved())
228       return;
229     if (areAllPreserved()) {
230       *this = Arg;
231       return;
232     }
233     // The intersection requires the *union* of the explicitly not-preserved
234     // IDs and the *intersection* of the preserved IDs.
235     for (auto ID : Arg.NotPreservedAnalysisIDs) {
236       PreservedIDs.erase(ID);
237       NotPreservedAnalysisIDs.insert(ID);
238     }
239     for (auto ID : PreservedIDs)
240       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.
intersect(PreservedAnalyses && Arg)248   void intersect(PreservedAnalyses &&Arg) {
249     if (Arg.areAllPreserved())
250       return;
251     if (areAllPreserved()) {
252       *this = std::move(Arg);
253       return;
254     }
255     // The intersection requires the *union* of the explicitly not-preserved
256     // IDs and the *intersection* of the preserved IDs.
257     for (auto ID : Arg.NotPreservedAnalysisIDs) {
258       PreservedIDs.erase(ID);
259       NotPreservedAnalysisIDs.insert(ID);
260     }
261     for (auto ID : PreservedIDs)
262       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.
PreservedAnalysisChecker(const PreservedAnalyses & PA,AnalysisKey * ID)278     PreservedAnalysisChecker(const PreservedAnalyses &PA, AnalysisKey *ID)
279         : 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.
preserved()285     bool preserved() {
286       return !IsAbandoned && (PA.PreservedIDs.count(&AllAnalysesKey) ||
287                               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.
preservedSet()293     template <typename AnalysisSetT> bool preservedSet() {
294       AnalysisSetKey *SetID = AnalysisSetT::ID();
295       return !IsAbandoned && (PA.PreservedIDs.count(&AllAnalysesKey) ||
296                               PA.PreservedIDs.count(SetID));
297     }
298   };
299 
300   /// Build a checker for this `PreservedAnalyses` and the specified analysis
301   /// type.
302   ///
303   /// You can use the returned object to query whether an analysis was
304   /// preserved. See the example in the comment on `PreservedAnalysis`.
getChecker()305   template <typename AnalysisT> PreservedAnalysisChecker getChecker() const {
306     return PreservedAnalysisChecker(*this, AnalysisT::ID());
307   }
308 
309   /// Build a checker for this `PreservedAnalyses` and the specified analysis
310   /// ID.
311   ///
312   /// You can use the returned object to query whether an analysis was
313   /// preserved. See the example in the comment on `PreservedAnalysis`.
getChecker(AnalysisKey * ID)314   PreservedAnalysisChecker getChecker(AnalysisKey *ID) const {
315     return PreservedAnalysisChecker(*this, ID);
316   }
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.
areAllPreserved()322   bool areAllPreserved() const {
323     return NotPreservedAnalysisIDs.empty() &&
324            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.
allAnalysesInSetPreserved()330   template <typename AnalysisSetT> bool allAnalysesInSetPreserved() const {
331     return allAnalysesInSetPreserved(AnalysisSetT::ID());
332   }
333 
334   /// Directly test whether a set of analyses is preserved.
335   ///
336   /// This is only true when no analyses have been explicitly abandoned.
allAnalysesInSetPreserved(AnalysisSetKey * SetID)337   bool allAnalysesInSetPreserved(AnalysisSetKey *SetID) const {
338     return NotPreservedAnalysisIDs.empty() &&
339            (PreservedIDs.count(&AllAnalysesKey) || PreservedIDs.count(SetID));
340   }
341 
342 private:
343   /// A special key used to indicate all analyses.
344   static AnalysisSetKey AllAnalysesKey;
345 
346   /// The IDs of analyses and analysis sets that are preserved.
347   SmallPtrSet<void *, 2> PreservedIDs;
348 
349   /// The IDs of explicitly not-preserved analyses.
350   ///
351   /// If an analysis in this set is covered by a set in `PreservedIDs`, we
352   /// consider it not-preserved. That is, `NotPreservedAnalysisIDs` always
353   /// "wins" over analysis sets in `PreservedIDs`.
354   ///
355   /// Also, a given ID should never occur both here and in `PreservedIDs`.
356   SmallPtrSet<AnalysisKey *, 2> NotPreservedAnalysisIDs;
357 };
358 
359 // 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.
namePassInfoMixin368   static StringRef name() {
369     static_assert(std::is_base_of<PassInfoMixin, DerivedT>::value,
370                   "Must pass the derived type as the template argument!");
371     StringRef Name = getTypeName<DerivedT>();
372     if (Name.startswith("llvm::"))
373       Name = Name.drop_front(strlen("llvm::"));
374     return Name;
375   }
376 };
377 
378 /// A CRTP mix-in that provides informational APIs needed for analysis passes.
379 ///
380 /// This provides some boilerplate for types that are analysis passes. It
381 /// automatically mixes in \c PassInfoMixin.
382 template <typename DerivedT>
383 struct AnalysisInfoMixin : PassInfoMixin<DerivedT> {
384   /// Returns an opaque, unique ID for this analysis type.
385   ///
386   /// This ID is a pointer type that is guaranteed to be 8-byte aligned and thus
387   /// suitable for use in sets, maps, and other data structures that use the low
388   /// bits of pointers.
389   ///
390   /// Note that this requires the derived type provide a static \c AnalysisKey
391   /// member called \c Key.
392   ///
393   /// FIXME: The only reason the mixin type itself can't declare the Key value
394   /// is that some compilers cannot correctly unique a templated static variable
395   /// so it has the same addresses in each instantiation. The only currently
396   /// known platform with this limitation is Windows DLL builds, specifically
397   /// building each part of LLVM as a DLL. If we ever remove that build
398   /// configuration, this mixin can provide the static key as well.
IDAnalysisInfoMixin399   static AnalysisKey *ID() {
400     static_assert(std::is_base_of<AnalysisInfoMixin, DerivedT>::value,
401                   "Must pass the derived type as the template argument!");
402     return &DerivedT::Key;
403   }
404 };
405 
406 namespace detail {
407 
408 /// Actual unpacker of extra arguments in getAnalysisResult,
409 /// passes only those tuple arguments that are mentioned in index_sequence.
410 template <typename PassT, typename IRUnitT, typename AnalysisManagerT,
411           typename... ArgTs, size_t... Ns>
412 typename PassT::Result
getAnalysisResultUnpackTuple(AnalysisManagerT & AM,IRUnitT & IR,std::tuple<ArgTs...> Args,llvm::index_sequence<Ns...>)413 getAnalysisResultUnpackTuple(AnalysisManagerT &AM, IRUnitT &IR,
414                              std::tuple<ArgTs...> Args,
415                              llvm::index_sequence<Ns...>) {
416   (void)Args;
417   return AM.template getResult<PassT>(IR, std::get<Ns>(Args)...);
418 }
419 
420 /// Helper for *partial* unpacking of extra arguments in getAnalysisResult.
421 ///
422 /// Arguments passed in tuple come from PassManager, so they might have extra
423 /// arguments after those AnalysisManager's ExtraArgTs ones that we need to
424 /// pass to getResult.
425 template <typename PassT, typename IRUnitT, typename... AnalysisArgTs,
426           typename... MainArgTs>
427 typename PassT::Result
getAnalysisResult(AnalysisManager<IRUnitT,AnalysisArgTs...> & AM,IRUnitT & IR,std::tuple<MainArgTs...> Args)428 getAnalysisResult(AnalysisManager<IRUnitT, AnalysisArgTs...> &AM, IRUnitT &IR,
429                   std::tuple<MainArgTs...> Args) {
430   return (getAnalysisResultUnpackTuple<
431           PassT, IRUnitT>)(AM, IR, Args,
432                            llvm::index_sequence_for<AnalysisArgTs...>{});
433 }
434 
435 } // namespace detail
436 
437 // Forward declare the pass instrumentation analysis explicitly queried in
438 // generic PassManager code.
439 // FIXME: figure out a way to move PassInstrumentationAnalysis into its own
440 // header.
441 class PassInstrumentationAnalysis;
442 
443 /// Manages a sequence of passes over a particular unit of IR.
444 ///
445 /// A pass manager contains a sequence of passes to run over a particular unit
446 /// of IR (e.g. Functions, Modules). It is itself a valid pass over that unit of
447 /// IR, and when run over some given IR will run each of its contained passes in
448 /// sequence. Pass managers are the primary and most basic building block of a
449 /// pass pipeline.
450 ///
451 /// When you run a pass manager, you provide an \c AnalysisManager<IRUnitT>
452 /// argument. The pass manager will propagate that analysis manager to each
453 /// pass it runs, and will call the analysis manager's invalidation routine with
454 /// the PreservedAnalyses of each pass it runs.
455 template <typename IRUnitT,
456           typename AnalysisManagerT = AnalysisManager<IRUnitT>,
457           typename... ExtraArgTs>
458 class PassManager : public PassInfoMixin<
459                         PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...>> {
460 public:
461   /// Construct a pass manager.
462   ///
463   /// If \p DebugLogging is true, we'll log our progress to llvm::dbgs().
DebugLogging(DebugLogging)464   explicit PassManager(bool DebugLogging = false) : DebugLogging(DebugLogging) {}
465 
466   // FIXME: These are equivalent to the default move constructor/move
467   // assignment. However, using = default triggers linker errors due to the
468   // explicit instantiations below. Find away to use the default and remove the
469   // duplicated code here.
PassManager(PassManager && Arg)470   PassManager(PassManager &&Arg)
471       : Passes(std::move(Arg.Passes)),
472         DebugLogging(std::move(Arg.DebugLogging)) {}
473 
474   PassManager &operator=(PassManager &&RHS) {
475     Passes = std::move(RHS.Passes);
476     DebugLogging = std::move(RHS.DebugLogging);
477     return *this;
478   }
479 
480   /// Run all of the passes in this manager over the given unit of IR.
481   /// ExtraArgs are passed to each pass.
run(IRUnitT & IR,AnalysisManagerT & AM,ExtraArgTs...ExtraArgs)482   PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM,
483                         ExtraArgTs... ExtraArgs) {
484     PreservedAnalyses PA = PreservedAnalyses::all();
485 
486     // Request PassInstrumentation from analysis manager, will use it to run
487     // instrumenting callbacks for the passes later.
488     // Here we use std::tuple wrapper over getResult which helps to extract
489     // AnalysisManager's arguments out of the whole ExtraArgs set.
490     PassInstrumentation PI =
491         detail::getAnalysisResult<PassInstrumentationAnalysis>(
492             AM, IR, std::tuple<ExtraArgTs...>(ExtraArgs...));
493 
494     if (DebugLogging)
495       dbgs() << "Starting " << getTypeName<IRUnitT>() << " pass manager run.\n";
496 
497     for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
498       auto *P = Passes[Idx].get();
499       if (DebugLogging)
500         dbgs() << "Running pass: " << P->name() << " on " << IR.getName()
501                << "\n";
502 
503       // Check the PassInstrumentation's BeforePass callbacks before running the
504       // pass, skip its execution completely if asked to (callback returns
505       // false).
506       if (!PI.runBeforePass<IRUnitT>(*P, IR))
507         continue;
508 
509       PreservedAnalyses PassPA = P->run(IR, AM, ExtraArgs...);
510 
511       // Call onto PassInstrumentation's AfterPass callbacks immediately after
512       // running the pass.
513       PI.runAfterPass<IRUnitT>(*P, IR);
514 
515       // Update the analysis manager as each pass runs and potentially
516       // invalidates analyses.
517       AM.invalidate(IR, PassPA);
518 
519       // Finally, intersect the preserved analyses to compute the aggregate
520       // preserved set for this pass manager.
521       PA.intersect(std::move(PassPA));
522 
523       // FIXME: Historically, the pass managers all called the LLVM context's
524       // yield function here. We don't have a generic way to acquire the
525       // context and it isn't yet clear what the right pattern is for yielding
526       // in the new pass manager so it is currently omitted.
527       //IR.getContext().yield();
528     }
529 
530     // Invalidation was handled after each pass in the above loop for the
531     // current unit of IR. Therefore, the remaining analysis results in the
532     // AnalysisManager are preserved. We mark this with a set so that we don't
533     // need to inspect each one individually.
534     PA.preserveSet<AllAnalysesOn<IRUnitT>>();
535 
536     if (DebugLogging)
537       dbgs() << "Finished " << getTypeName<IRUnitT>() << " pass manager run.\n";
538 
539     return PA;
540   }
541 
addPass(PassT Pass)542   template <typename PassT> void addPass(PassT Pass) {
543     using PassModelT =
544         detail::PassModel<IRUnitT, PassT, PreservedAnalyses, AnalysisManagerT,
545                           ExtraArgTs...>;
546 
547     Passes.emplace_back(new PassModelT(std::move(Pass)));
548   }
549 
550 private:
551   using PassConceptT =
552       detail::PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...>;
553 
554   std::vector<std::unique_ptr<PassConceptT>> Passes;
555 
556   /// Flag indicating whether we should do debug logging.
557   bool DebugLogging;
558 };
559 
560 extern template class PassManager<Module>;
561 
562 /// Convenience typedef for a pass manager over modules.
563 using ModulePassManager = PassManager<Module>;
564 
565 extern template class PassManager<Function>;
566 
567 /// Convenience typedef for a pass manager over functions.
568 using FunctionPassManager = PassManager<Function>;
569 
570 /// Pseudo-analysis pass that exposes the \c PassInstrumentation to pass
571 /// managers. Goes before AnalysisManager definition to provide its
572 /// internals (e.g PassInstrumentationAnalysis::ID) for use there if needed.
573 /// FIXME: figure out a way to move PassInstrumentationAnalysis into its own
574 /// header.
575 class PassInstrumentationAnalysis
576     : public AnalysisInfoMixin<PassInstrumentationAnalysis> {
577   friend AnalysisInfoMixin<PassInstrumentationAnalysis>;
578   static AnalysisKey Key;
579 
580   PassInstrumentationCallbacks *Callbacks;
581 
582 public:
583   /// PassInstrumentationCallbacks object is shared, owned by something else,
584   /// not this analysis.
585   PassInstrumentationAnalysis(PassInstrumentationCallbacks *Callbacks = nullptr)
Callbacks(Callbacks)586       : Callbacks(Callbacks) {}
587 
588   using Result = PassInstrumentation;
589 
590   template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
run(IRUnitT &,AnalysisManagerT &,ExtraArgTs &&...)591   Result run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) {
592     return PassInstrumentation(Callbacks);
593   }
594 };
595 
596 /// A container for analyses that lazily runs them and caches their
597 /// results.
598 ///
599 /// This class can manage analyses for any IR unit where the address of the IR
600 /// unit sufficies as its identity.
601 template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager {
602 public:
603   class Invalidator;
604 
605 private:
606   // Now that we've defined our invalidator, we can define the concept types.
607   using ResultConceptT =
608       detail::AnalysisResultConcept<IRUnitT, PreservedAnalyses, Invalidator>;
609   using PassConceptT =
610       detail::AnalysisPassConcept<IRUnitT, PreservedAnalyses, Invalidator,
611                                   ExtraArgTs...>;
612 
613   /// List of analysis pass IDs and associated concept pointers.
614   ///
615   /// Requires iterators to be valid across appending new entries and arbitrary
616   /// erases. Provides the analysis ID to enable finding iterators to a given
617   /// entry in maps below, and provides the storage for the actual result
618   /// concept.
619   using AnalysisResultListT =
620       std::list<std::pair<AnalysisKey *, std::unique_ptr<ResultConceptT>>>;
621 
622   /// Map type from IRUnitT pointer to our custom list type.
623   using AnalysisResultListMapT = DenseMap<IRUnitT *, AnalysisResultListT>;
624 
625   /// Map type from a pair of analysis ID and IRUnitT pointer to an
626   /// iterator into a particular result list (which is where the actual analysis
627   /// result is stored).
628   using AnalysisResultMapT =
629       DenseMap<std::pair<AnalysisKey *, IRUnitT *>,
630                typename AnalysisResultListT::iterator>;
631 
632 public:
633   /// API to communicate dependencies between analyses during invalidation.
634   ///
635   /// When an analysis result embeds handles to other analysis results, it
636   /// needs to be invalidated both when its own information isn't preserved and
637   /// when any of its embedded analysis results end up invalidated. We pass an
638   /// \c Invalidator object as an argument to \c invalidate() in order to let
639   /// the analysis results themselves define the dependency graph on the fly.
640   /// This lets us avoid building building an explicit representation of the
641   /// dependencies between analysis results.
642   class Invalidator {
643   public:
644     /// Trigger the invalidation of some other analysis pass if not already
645     /// handled and return whether it was in fact invalidated.
646     ///
647     /// This is expected to be called from within a given analysis result's \c
648     /// invalidate method to trigger a depth-first walk of all inter-analysis
649     /// dependencies. The same \p IR unit and \p PA passed to that result's \c
650     /// invalidate method should in turn be provided to this routine.
651     ///
652     /// The first time this is called for a given analysis pass, it will call
653     /// 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     /// cyclic dependencies between analysis results.
656     ///
657     /// This returns true if the given analysis's result is invalid. Any
658     /// dependecies on it will become invalid as a result.
659     template <typename PassT>
invalidate(IRUnitT & IR,const PreservedAnalyses & PA)660     bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA) {
661       using ResultModelT =
662           detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
663                                       PreservedAnalyses, Invalidator>;
664 
665       return invalidateImpl<ResultModelT>(PassT::ID(), IR, PA);
666     }
667 
668     /// 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     /// parameter.
671     ///
672     /// This is sadly less efficient than the above routine, which leverages
673     /// the type parameter to avoid the type erasure overhead.
invalidate(AnalysisKey * ID,IRUnitT & IR,const PreservedAnalyses & PA)674     bool invalidate(AnalysisKey *ID, IRUnitT &IR, const PreservedAnalyses &PA) {
675       return invalidateImpl<>(ID, IR, PA);
676     }
677 
678   private:
679     friend class AnalysisManager;
680 
681     template <typename ResultT = ResultConceptT>
invalidateImpl(AnalysisKey * ID,IRUnitT & IR,const PreservedAnalyses & PA)682     bool invalidateImpl(AnalysisKey *ID, IRUnitT &IR,
683                         const PreservedAnalyses &PA) {
684       // If we've already visited this pass, return true if it was invalidated
685       // and false otherwise.
686       auto IMapI = IsResultInvalidated.find(ID);
687       if (IMapI != IsResultInvalidated.end())
688         return IMapI->second;
689 
690       // Otherwise look up the result object.
691       auto RI = Results.find({ID, &IR});
692       assert(RI != Results.end() &&
693              "Trying to invalidate a dependent result that isn't in the "
694              "manager's cache is always an error, likely due to a stale result "
695              "handle!");
696 
697       auto &Result = static_cast<ResultT &>(*RI->second->second);
698 
699       // Insert into the map whether the result should be invalidated and return
700       // that. Note that we cannot reuse IMapI and must do a fresh insert here,
701       // as calling invalidate could (recursively) insert things into the map,
702       // making any iterator or reference invalid.
703       bool Inserted;
704       std::tie(IMapI, Inserted) =
705           IsResultInvalidated.insert({ID, Result.invalidate(IR, PA, *this)});
706       (void)Inserted;
707       assert(Inserted && "Should not have already inserted this ID, likely "
708                          "indicates a dependency cycle!");
709       return IMapI->second;
710     }
711 
Invalidator(SmallDenseMap<AnalysisKey *,bool,8> & IsResultInvalidated,const AnalysisResultMapT & Results)712     Invalidator(SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated,
713                 const AnalysisResultMapT &Results)
714         : IsResultInvalidated(IsResultInvalidated), Results(Results) {}
715 
716     SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated;
717     const AnalysisResultMapT &Results;
718   };
719 
720   /// Construct an empty analysis manager.
721   ///
722   /// If \p DebugLogging is true, we'll log our progress to llvm::dbgs().
DebugLogging(DebugLogging)723   AnalysisManager(bool DebugLogging = false) : DebugLogging(DebugLogging) {}
724   AnalysisManager(AnalysisManager &&) = default;
725   AnalysisManager &operator=(AnalysisManager &&) = default;
726 
727   /// Returns true if the analysis manager has an empty results cache.
empty()728   bool empty() const {
729     assert(AnalysisResults.empty() == AnalysisResultLists.empty() &&
730            "The storage and index of analysis results disagree on how many "
731            "there are!");
732     return AnalysisResults.empty();
733   }
734 
735   /// Clear any cached analysis results for a single unit of IR.
736   ///
737   /// This doesn't invalidate, but instead simply deletes, the relevant results.
738   /// It is useful when the IR is being removed and we want to clear out all the
739   /// memory pinned for it.
clear(IRUnitT & IR,llvm::StringRef Name)740   void clear(IRUnitT &IR, llvm::StringRef Name) {
741     if (DebugLogging)
742       dbgs() << "Clearing all analysis results for: " << Name << "\n";
743 
744     auto ResultsListI = AnalysisResultLists.find(&IR);
745     if (ResultsListI == AnalysisResultLists.end())
746       return;
747     // Delete the map entries that point into the results list.
748     for (auto &IDAndResult : ResultsListI->second)
749       AnalysisResults.erase({IDAndResult.first, &IR});
750 
751     // And actually destroy and erase the results associated with this IR.
752     AnalysisResultLists.erase(ResultsListI);
753   }
754 
755   /// Clear all analysis results cached by this AnalysisManager.
756   ///
757   /// Like \c clear(IRUnitT&), this doesn't invalidate the results; it simply
758   /// deletes them.  This lets you clean up the AnalysisManager when the set of
759   /// IR units itself has potentially changed, and thus we can't even look up a
760   /// a result and invalidate/clear it directly.
clear()761   void clear() {
762     AnalysisResults.clear();
763     AnalysisResultLists.clear();
764   }
765 
766   /// Get the result of an analysis pass for a given IR unit.
767   ///
768   /// Runs the analysis if a cached result is not available.
769   template <typename PassT>
getResult(IRUnitT & IR,ExtraArgTs...ExtraArgs)770   typename PassT::Result &getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs) {
771     assert(AnalysisPasses.count(PassT::ID()) &&
772            "This analysis pass was not registered prior to being queried");
773     ResultConceptT &ResultConcept =
774         getResultImpl(PassT::ID(), IR, ExtraArgs...);
775 
776     using ResultModelT =
777         detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
778                                     PreservedAnalyses, Invalidator>;
779 
780     return static_cast<ResultModelT &>(ResultConcept).Result;
781   }
782 
783   /// Get the cached result of an analysis pass for a given IR unit.
784   ///
785   /// This method never runs the analysis.
786   ///
787   /// \returns null if there is no cached result.
788   template <typename PassT>
getCachedResult(IRUnitT & IR)789   typename PassT::Result *getCachedResult(IRUnitT &IR) const {
790     assert(AnalysisPasses.count(PassT::ID()) &&
791            "This analysis pass was not registered prior to being queried");
792 
793     ResultConceptT *ResultConcept = getCachedResultImpl(PassT::ID(), IR);
794     if (!ResultConcept)
795       return nullptr;
796 
797     using ResultModelT =
798         detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
799                                     PreservedAnalyses, Invalidator>;
800 
801     return &static_cast<ResultModelT *>(ResultConcept)->Result;
802   }
803 
804   /// Register an analysis pass with the manager.
805   ///
806   /// The parameter is a callable whose result is an analysis pass. This allows
807   /// passing in a lambda to construct the analysis.
808   ///
809   /// The analysis type to register is the type returned by calling the \c
810   /// PassBuilder argument. If that type has already been registered, then the
811   /// argument will not be called and this function will return false.
812   /// Otherwise, we register the analysis returned by calling \c PassBuilder(),
813   /// and this function returns true.
814   ///
815   /// (Note: Although the return value of this function indicates whether or not
816   /// an analysis was previously registered, there intentionally isn't a way to
817   /// query this directly.  Instead, you should just register all the analyses
818   /// 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   /// hashtable.)
821   template <typename PassBuilderT>
registerPass(PassBuilderT && PassBuilder)822   bool registerPass(PassBuilderT &&PassBuilder) {
823     using PassT = decltype(PassBuilder());
824     using PassModelT =
825         detail::AnalysisPassModel<IRUnitT, PassT, PreservedAnalyses,
826                                   Invalidator, ExtraArgTs...>;
827 
828     auto &PassPtr = AnalysisPasses[PassT::ID()];
829     if (PassPtr)
830       // Already registered this pass type!
831       return false;
832 
833     // Construct a new model around the instance returned by the builder.
834     PassPtr.reset(new PassModelT(PassBuilder()));
835     return true;
836   }
837 
838   /// Invalidate a specific analysis pass for an IR module.
839   ///
840   /// Note that the analysis result can disregard invalidation, if it determines
841   /// it is in fact still valid.
invalidate(IRUnitT & IR)842   template <typename PassT> void invalidate(IRUnitT &IR) {
843     assert(AnalysisPasses.count(PassT::ID()) &&
844            "This analysis pass was not registered prior to being invalidated");
845     invalidateImpl(PassT::ID(), IR);
846   }
847 
848   /// Invalidate cached analyses for an IR unit.
849   ///
850   /// Walk through all of the analyses pertaining to this unit of IR and
851   /// invalidate them, unless they are preserved by the PreservedAnalyses set.
invalidate(IRUnitT & IR,const PreservedAnalyses & PA)852   void invalidate(IRUnitT &IR, const PreservedAnalyses &PA) {
853     // We're done if all analyses on this IR unit are preserved.
854     if (PA.allAnalysesInSetPreserved<AllAnalysesOn<IRUnitT>>())
855       return;
856 
857     if (DebugLogging)
858       dbgs() << "Invalidating all non-preserved analyses for: " << IR.getName()
859              << "\n";
860 
861     // Track whether each analysis's result is invalidated in
862     // IsResultInvalidated.
863     SmallDenseMap<AnalysisKey *, bool, 8> IsResultInvalidated;
864     Invalidator Inv(IsResultInvalidated, AnalysisResults);
865     AnalysisResultListT &ResultsList = AnalysisResultLists[&IR];
866     for (auto &AnalysisResultPair : ResultsList) {
867       // This is basically the same thing as Invalidator::invalidate, but we
868       // can't call it here because we're operating on the type-erased result.
869       // Moreover if we instead called invalidate() directly, it would do an
870       // unnecessary look up in ResultsList.
871       AnalysisKey *ID = AnalysisResultPair.first;
872       auto &Result = *AnalysisResultPair.second;
873 
874       auto IMapI = IsResultInvalidated.find(ID);
875       if (IMapI != IsResultInvalidated.end())
876         // This result was already handled via the Invalidator.
877         continue;
878 
879       // Try to invalidate the result, giving it the Invalidator so it can
880       // recursively query for any dependencies it has and record the result.
881       // Note that we cannot reuse 'IMapI' here or pre-insert the ID, as
882       // Result.invalidate may insert things into the map, invalidating our
883       // iterator.
884       bool Inserted =
885           IsResultInvalidated.insert({ID, Result.invalidate(IR, PA, Inv)})
886               .second;
887       (void)Inserted;
888       assert(Inserted && "Should never have already inserted this ID, likely "
889                          "indicates a cycle!");
890     }
891 
892     // Now erase the results that were marked above as invalidated.
893     if (!IsResultInvalidated.empty()) {
894       for (auto I = ResultsList.begin(), E = ResultsList.end(); I != E;) {
895         AnalysisKey *ID = I->first;
896         if (!IsResultInvalidated.lookup(ID)) {
897           ++I;
898           continue;
899         }
900 
901         if (DebugLogging)
902           dbgs() << "Invalidating analysis: " << this->lookUpPass(ID).name()
903                  << " on " << IR.getName() << "\n";
904 
905         I = ResultsList.erase(I);
906         AnalysisResults.erase({ID, &IR});
907       }
908     }
909 
910     if (ResultsList.empty())
911       AnalysisResultLists.erase(&IR);
912   }
913 
914 private:
915   /// Look up a registered analysis pass.
lookUpPass(AnalysisKey * ID)916   PassConceptT &lookUpPass(AnalysisKey *ID) {
917     typename AnalysisPassMapT::iterator PI = AnalysisPasses.find(ID);
918     assert(PI != AnalysisPasses.end() &&
919            "Analysis passes must be registered prior to being queried!");
920     return *PI->second;
921   }
922 
923   /// Look up a registered analysis pass.
lookUpPass(AnalysisKey * ID)924   const PassConceptT &lookUpPass(AnalysisKey *ID) const {
925     typename AnalysisPassMapT::const_iterator PI = AnalysisPasses.find(ID);
926     assert(PI != AnalysisPasses.end() &&
927            "Analysis passes must be registered prior to being queried!");
928     return *PI->second;
929   }
930 
931   /// Get an analysis result, running the pass if necessary.
getResultImpl(AnalysisKey * ID,IRUnitT & IR,ExtraArgTs...ExtraArgs)932   ResultConceptT &getResultImpl(AnalysisKey *ID, IRUnitT &IR,
933                                 ExtraArgTs... ExtraArgs) {
934     typename AnalysisResultMapT::iterator RI;
935     bool Inserted;
936     std::tie(RI, Inserted) = AnalysisResults.insert(std::make_pair(
937         std::make_pair(ID, &IR), typename AnalysisResultListT::iterator()));
938 
939     // If we don't have a cached result for this function, look up the pass and
940     // run it to produce a result, which we then add to the cache.
941     if (Inserted) {
942       auto &P = this->lookUpPass(ID);
943       if (DebugLogging)
944         dbgs() << "Running analysis: " << P.name() << " on " << IR.getName()
945                << "\n";
946 
947       PassInstrumentation PI;
948       if (ID != PassInstrumentationAnalysis::ID()) {
949         PI = getResult<PassInstrumentationAnalysis>(IR, ExtraArgs...);
950         PI.runBeforeAnalysis(P, IR);
951       }
952 
953       AnalysisResultListT &ResultList = AnalysisResultLists[&IR];
954       ResultList.emplace_back(ID, P.run(IR, *this, ExtraArgs...));
955 
956       PI.runAfterAnalysis(P, IR);
957 
958       // P.run may have inserted elements into AnalysisResults and invalidated
959       // RI.
960       RI = AnalysisResults.find({ID, &IR});
961       assert(RI != AnalysisResults.end() && "we just inserted it!");
962 
963       RI->second = std::prev(ResultList.end());
964     }
965 
966     return *RI->second->second;
967   }
968 
969   /// Get a cached analysis result or return null.
getCachedResultImpl(AnalysisKey * ID,IRUnitT & IR)970   ResultConceptT *getCachedResultImpl(AnalysisKey *ID, IRUnitT &IR) const {
971     typename AnalysisResultMapT::const_iterator RI =
972         AnalysisResults.find({ID, &IR});
973     return RI == AnalysisResults.end() ? nullptr : &*RI->second->second;
974   }
975 
976   /// Invalidate a function pass result.
invalidateImpl(AnalysisKey * ID,IRUnitT & IR)977   void invalidateImpl(AnalysisKey *ID, IRUnitT &IR) {
978     typename AnalysisResultMapT::iterator RI =
979         AnalysisResults.find({ID, &IR});
980     if (RI == AnalysisResults.end())
981       return;
982 
983     if (DebugLogging)
984       dbgs() << "Invalidating analysis: " << this->lookUpPass(ID).name()
985              << " on " << IR.getName() << "\n";
986     AnalysisResultLists[&IR].erase(RI->second);
987     AnalysisResults.erase(RI);
988   }
989 
990   /// Map type from module analysis pass ID to pass concept pointer.
991   using AnalysisPassMapT =
992       DenseMap<AnalysisKey *, std::unique_ptr<PassConceptT>>;
993 
994   /// Collection of module analysis passes, indexed by ID.
995   AnalysisPassMapT AnalysisPasses;
996 
997   /// Map from function to a list of function analysis results.
998   ///
999   /// Provides linear time removal of all analysis results for a function and
1000   /// the ultimate storage for a particular cached analysis result.
1001   AnalysisResultListMapT AnalysisResultLists;
1002 
1003   /// Map from an analysis ID and function to a particular cached
1004   /// analysis result.
1005   AnalysisResultMapT AnalysisResults;
1006 
1007   /// Indicates whether we log to \c llvm::dbgs().
1008   bool DebugLogging;
1009 };
1010 
1011 extern template class AnalysisManager<Module>;
1012 
1013 /// Convenience typedef for the Module analysis manager.
1014 using ModuleAnalysisManager = AnalysisManager<Module>;
1015 
1016 extern template class AnalysisManager<Function>;
1017 
1018 /// Convenience typedef for the Function analysis manager.
1019 using FunctionAnalysisManager = AnalysisManager<Function>;
1020 
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 /// in the outer unit.
1024 ///
1025 /// For example, InnerAnalysisManagerProxy<FunctionAnalysisManager, Module> is
1026 /// an analysis over Modules (the "outer" unit) that provides access to a
1027 /// Function analysis manager.  The FunctionAnalysisManager is the "inner"
1028 /// manager being proxied, and Functions are the "inner" unit.  The inner/outer
1029 /// relationship is valid because each Function is contained in one Module.
1030 ///
1031 /// If you're (transitively) within a pass manager for an IR unit U that
1032 /// contains IR unit V, you should never use an analysis manager over V, except
1033 /// via one of these proxies.
1034 ///
1035 /// Note that the proxy's result is a move-only RAII object.  The validity of
1036 /// the analyses in the inner analysis manager is tied to its lifetime.
1037 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1038 class InnerAnalysisManagerProxy
1039     : public AnalysisInfoMixin<
1040           InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>> {
1041 public:
1042   class Result {
1043   public:
Result(AnalysisManagerT & InnerAM)1044     explicit Result(AnalysisManagerT &InnerAM) : InnerAM(&InnerAM) {}
1045 
Result(Result && Arg)1046     Result(Result &&Arg) : InnerAM(std::move(Arg.InnerAM)) {
1047       // We have to null out the analysis manager in the moved-from state
1048       // because we are taking ownership of the responsibilty to clear the
1049       // analysis state.
1050       Arg.InnerAM = nullptr;
1051     }
1052 
~Result()1053     ~Result() {
1054       // InnerAM is cleared in a moved from state where there is nothing to do.
1055       if (!InnerAM)
1056         return;
1057 
1058       // 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       InnerAM->clear();
1061     }
1062 
1063     Result &operator=(Result &&RHS) {
1064       InnerAM = RHS.InnerAM;
1065       // We have to null out the analysis manager in the moved-from state
1066       // because we are taking ownership of the responsibilty to clear the
1067       // analysis state.
1068       RHS.InnerAM = nullptr;
1069       return *this;
1070     }
1071 
1072     /// Accessor for the analysis manager.
getManager()1073     AnalysisManagerT &getManager() { return *InnerAM; }
1074 
1075     /// Handler for invalidation of the outer IR unit, \c IRUnitT.
1076     ///
1077     /// 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     /// we have to call \c clear() on the inner analysis manager, as it may now
1080     /// have stale pointers to its inner IR objects.
1081     ///
1082     /// Regardless of whether the proxy analysis is marked as preserved, all of
1083     /// the analyses in the inner analysis manager are potentially invalidated
1084     /// based on the set of preserved analyses.
1085     bool invalidate(
1086         IRUnitT &IR, const PreservedAnalyses &PA,
1087         typename AnalysisManager<IRUnitT, ExtraArgTs...>::Invalidator &Inv);
1088 
1089   private:
1090     AnalysisManagerT *InnerAM;
1091   };
1092 
InnerAnalysisManagerProxy(AnalysisManagerT & InnerAM)1093   explicit InnerAnalysisManagerProxy(AnalysisManagerT &InnerAM)
1094       : InnerAM(&InnerAM) {}
1095 
1096   /// Run the analysis pass and create our proxy result object.
1097   ///
1098   /// This doesn't do any interesting work; it is primarily used to insert our
1099   /// proxy result object into the outer analysis cache so that we can proxy
1100   /// invalidation to the inner analysis manager.
run(IRUnitT & IR,AnalysisManager<IRUnitT,ExtraArgTs...> & AM,ExtraArgTs...)1101   Result run(IRUnitT &IR, AnalysisManager<IRUnitT, ExtraArgTs...> &AM,
1102              ExtraArgTs...) {
1103     return Result(*InnerAM);
1104   }
1105 
1106 private:
1107   friend AnalysisInfoMixin<
1108       InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>>;
1109 
1110   static AnalysisKey Key;
1111 
1112   AnalysisManagerT *InnerAM;
1113 };
1114 
1115 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1116 AnalysisKey
1117     InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
1118 
1119 /// Provide the \c FunctionAnalysisManager to \c Module proxy.
1120 using FunctionAnalysisManagerModuleProxy =
1121     InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>;
1122 
1123 /// Specialization of the invalidate method for the \c
1124 /// FunctionAnalysisManagerModuleProxy's result.
1125 template <>
1126 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
1127     Module &M, const PreservedAnalyses &PA,
1128     ModuleAnalysisManager::Invalidator &Inv);
1129 
1130 // Ensure the \c FunctionAnalysisManagerModuleProxy is provided as an extern
1131 // template.
1132 extern template class InnerAnalysisManagerProxy<FunctionAnalysisManager,
1133                                                 Module>;
1134 
1135 /// 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 /// in the outer unit.
1138 ///
1139 /// For example OuterAnalysisManagerProxy<ModuleAnalysisManager, Function> is an
1140 /// analysis over Functions (the "inner" unit) which provides access to a Module
1141 /// analysis manager.  The ModuleAnalysisManager is the "outer" manager being
1142 /// proxied, and Modules are the "outer" IR unit.  The inner/outer relationship
1143 /// is valid because each Function is contained in one Module.
1144 ///
1145 /// This proxy only exposes the const interface of the outer analysis manager,
1146 /// to indicate that you cannot cause an outer analysis to run from within an
1147 /// inner pass.  Instead, you must rely on the \c getCachedResult API.
1148 ///
1149 /// This proxy doesn't manage invalidation in any way -- that is handled by the
1150 /// recursive return path of each layer of the pass manager.  A consequence of
1151 /// this is the outer analyses may be stale.  We invalidate the outer analyses
1152 /// only when we're done running passes over the inner IR units.
1153 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1154 class OuterAnalysisManagerProxy
1155     : public AnalysisInfoMixin<
1156           OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>> {
1157 public:
1158   /// Result proxy object for \c OuterAnalysisManagerProxy.
1159   class Result {
1160   public:
Result(const AnalysisManagerT & AM)1161     explicit Result(const AnalysisManagerT &AM) : AM(&AM) {}
1162 
getManager()1163     const AnalysisManagerT &getManager() const { return *AM; }
1164 
1165     /// When invalidation occurs, remove any registered invalidation events.
invalidate(IRUnitT & IRUnit,const PreservedAnalyses & PA,typename AnalysisManager<IRUnitT,ExtraArgTs...>::Invalidator & Inv)1166     bool invalidate(
1167         IRUnitT &IRUnit, const PreservedAnalyses &PA,
1168         typename AnalysisManager<IRUnitT, ExtraArgTs...>::Invalidator &Inv) {
1169       // Loop over the set of registered outer invalidation mappings and if any
1170       // of them map to an analysis that is now invalid, clear it out.
1171       SmallVector<AnalysisKey *, 4> DeadKeys;
1172       for (auto &KeyValuePair : OuterAnalysisInvalidationMap) {
1173         AnalysisKey *OuterID = KeyValuePair.first;
1174         auto &InnerIDs = KeyValuePair.second;
1175         InnerIDs.erase(llvm::remove_if(InnerIDs, [&](AnalysisKey *InnerID) {
1176           return Inv.invalidate(InnerID, IRUnit, PA); }),
1177                        InnerIDs.end());
1178         if (InnerIDs.empty())
1179           DeadKeys.push_back(OuterID);
1180       }
1181 
1182       for (auto OuterID : DeadKeys)
1183         OuterAnalysisInvalidationMap.erase(OuterID);
1184 
1185       // The proxy itself remains valid regardless of anything else.
1186       return false;
1187     }
1188 
1189     /// Register a deferred invalidation event for when the outer analysis
1190     /// manager processes its invalidations.
1191     template <typename OuterAnalysisT, typename InvalidatedAnalysisT>
registerOuterAnalysisInvalidation()1192     void registerOuterAnalysisInvalidation() {
1193       AnalysisKey *OuterID = OuterAnalysisT::ID();
1194       AnalysisKey *InvalidatedID = InvalidatedAnalysisT::ID();
1195 
1196       auto &InvalidatedIDList = OuterAnalysisInvalidationMap[OuterID];
1197       // Note, this is a linear scan. If we end up with large numbers of
1198       // analyses that all trigger invalidation on the same outer analysis,
1199       // this entire system should be changed to some other deterministic
1200       // data structure such as a `SetVector` of a pair of pointers.
1201       auto InvalidatedIt = std::find(InvalidatedIDList.begin(),
1202                                      InvalidatedIDList.end(), InvalidatedID);
1203       if (InvalidatedIt == InvalidatedIDList.end())
1204         InvalidatedIDList.push_back(InvalidatedID);
1205     }
1206 
1207     /// Access the map from outer analyses to deferred invalidation requiring
1208     /// analyses.
1209     const SmallDenseMap<AnalysisKey *, TinyPtrVector<AnalysisKey *>, 2> &
getOuterInvalidations()1210     getOuterInvalidations() const {
1211       return OuterAnalysisInvalidationMap;
1212     }
1213 
1214   private:
1215     const AnalysisManagerT *AM;
1216 
1217     /// A map from an outer analysis ID to the set of this IR-unit's analyses
1218     /// which need to be invalidated.
1219     SmallDenseMap<AnalysisKey *, TinyPtrVector<AnalysisKey *>, 2>
1220         OuterAnalysisInvalidationMap;
1221   };
1222 
OuterAnalysisManagerProxy(const AnalysisManagerT & AM)1223   OuterAnalysisManagerProxy(const AnalysisManagerT &AM) : AM(&AM) {}
1224 
1225   /// Run the analysis pass and create our proxy result object.
1226   /// Nothing to see here, it just forwards the \c AM reference into the
1227   /// result.
run(IRUnitT &,AnalysisManager<IRUnitT,ExtraArgTs...> &,ExtraArgTs...)1228   Result run(IRUnitT &, AnalysisManager<IRUnitT, ExtraArgTs...> &,
1229              ExtraArgTs...) {
1230     return Result(*AM);
1231   }
1232 
1233 private:
1234   friend AnalysisInfoMixin<
1235       OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>>;
1236 
1237   static AnalysisKey Key;
1238 
1239   const AnalysisManagerT *AM;
1240 };
1241 
1242 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1243 AnalysisKey
1244     OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
1245 
1246 extern template class OuterAnalysisManagerProxy<ModuleAnalysisManager,
1247                                                 Function>;
1248 /// Provide the \c ModuleAnalysisManager to \c Function proxy.
1249 using ModuleAnalysisManagerFunctionProxy =
1250     OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>;
1251 
1252 /// Trivial adaptor that maps from a module to its functions.
1253 ///
1254 /// Designed to allow composition of a FunctionPass(Manager) and
1255 /// a ModulePassManager, by running the FunctionPass(Manager) over every
1256 /// function in the module.
1257 ///
1258 /// 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 /// functions! Other threads or systems may be manipulating other functions in
1261 /// the module, and so their state should never be relied on.
1262 /// FIXME: Make the above true for all of LLVM's actual passes, some still
1263 /// violate this principle.
1264 ///
1265 /// Function passes can also read the module containing the function, but they
1266 /// should not modify that module outside of the use lists of various globals.
1267 /// For example, a function pass is not permitted to add functions to the
1268 /// module.
1269 /// FIXME: Make the above true for all of LLVM's actual passes, some still
1270 /// violate this principle.
1271 ///
1272 /// Note that although function passes can access module analyses, module
1273 /// analyses are not invalidated while the function passes are running, so they
1274 /// may be stale.  Function analyses will not be stale.
1275 template <typename FunctionPassT>
1276 class ModuleToFunctionPassAdaptor
1277     : public PassInfoMixin<ModuleToFunctionPassAdaptor<FunctionPassT>> {
1278 public:
ModuleToFunctionPassAdaptor(FunctionPassT Pass)1279   explicit ModuleToFunctionPassAdaptor(FunctionPassT Pass)
1280       : Pass(std::move(Pass)) {}
1281 
1282   /// Runs the function pass across every function in the module.
run(Module & M,ModuleAnalysisManager & AM)1283   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM) {
1284     FunctionAnalysisManager &FAM =
1285         AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
1286 
1287     // Request PassInstrumentation from analysis manager, will use it to run
1288     // instrumenting callbacks for the passes later.
1289     PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(M);
1290 
1291     PreservedAnalyses PA = PreservedAnalyses::all();
1292     for (Function &F : M) {
1293       if (F.isDeclaration())
1294         continue;
1295 
1296       // Check the PassInstrumentation's BeforePass callbacks before running the
1297       // pass, skip its execution completely if asked to (callback returns
1298       // false).
1299       if (!PI.runBeforePass<Function>(Pass, F))
1300         continue;
1301       PreservedAnalyses PassPA = Pass.run(F, FAM);
1302 
1303       PI.runAfterPass(Pass, F);
1304 
1305       // We know that the function pass couldn't have invalidated any other
1306       // function's analyses (that's the contract of a function pass), so
1307       // directly handle the function analysis manager's invalidation here.
1308       FAM.invalidate(F, PassPA);
1309 
1310       // Then intersect the preserved set so that invalidation of module
1311       // analyses will eventually occur when the module pass completes.
1312       PA.intersect(std::move(PassPA));
1313     }
1314 
1315     // The FunctionAnalysisManagerModuleProxy is preserved because (we assume)
1316     // the function passes we ran didn't add or remove any functions.
1317     //
1318     // We also preserve all analyses on Functions, because we did all the
1319     // invalidation we needed to do above.
1320     PA.preserveSet<AllAnalysesOn<Function>>();
1321     PA.preserve<FunctionAnalysisManagerModuleProxy>();
1322     return PA;
1323   }
1324 
1325 private:
1326   FunctionPassT Pass;
1327 };
1328 
1329 /// A function to deduce a function pass type and wrap it in the
1330 /// templated adaptor.
1331 template <typename FunctionPassT>
1332 ModuleToFunctionPassAdaptor<FunctionPassT>
createModuleToFunctionPassAdaptor(FunctionPassT Pass)1333 createModuleToFunctionPassAdaptor(FunctionPassT Pass) {
1334   return ModuleToFunctionPassAdaptor<FunctionPassT>(std::move(Pass));
1335 }
1336 
1337 /// A utility pass template to force an analysis result to be available.
1338 ///
1339 /// If there are extra arguments at the pass's run level there may also be
1340 /// extra arguments to the analysis manager's \c getResult routine. We can't
1341 /// guess how to effectively map the arguments from one to the other, and so
1342 /// this specialization just ignores them.
1343 ///
1344 /// Specific patterns of run-method extra arguments and analysis manager extra
1345 /// arguments will have to be defined as appropriate specializations.
1346 template <typename AnalysisT, typename IRUnitT,
1347           typename AnalysisManagerT = AnalysisManager<IRUnitT>,
1348           typename... ExtraArgTs>
1349 struct RequireAnalysisPass
1350     : PassInfoMixin<RequireAnalysisPass<AnalysisT, IRUnitT, AnalysisManagerT,
1351                                         ExtraArgTs...>> {
1352   /// Run this pass over some unit of IR.
1353   ///
1354   /// This pass can be run over any unit of IR and use any analysis manager
1355   /// provided they satisfy the basic API requirements. When this pass is
1356   /// created, these methods can be instantiated to satisfy whatever the
1357   /// context requires.
runRequireAnalysisPass1358   PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM,
1359                         ExtraArgTs &&... Args) {
1360     (void)AM.template getResult<AnalysisT>(Arg,
1361                                            std::forward<ExtraArgTs>(Args)...);
1362 
1363     return PreservedAnalyses::all();
1364   }
1365 };
1366 
1367 /// A no-op pass template which simply forces a specific analysis result
1368 /// to be invalidated.
1369 template <typename AnalysisT>
1370 struct InvalidateAnalysisPass
1371     : PassInfoMixin<InvalidateAnalysisPass<AnalysisT>> {
1372   /// Run this pass over some unit of IR.
1373   ///
1374   /// This pass can be run over any unit of IR and use any analysis manager,
1375   /// provided they satisfy the basic API requirements. When this pass is
1376   /// created, these methods can be instantiated to satisfy whatever the
1377   /// context requires.
1378   template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
runInvalidateAnalysisPass1379   PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&...) {
1380     auto PA = PreservedAnalyses::all();
1381     PA.abandon<AnalysisT>();
1382     return PA;
1383   }
1384 };
1385 
1386 /// A utility pass that does nothing, but preserves no analyses.
1387 ///
1388 /// Because this preserves no analyses, any analysis passes queried after this
1389 /// pass runs will recompute fresh results.
1390 struct InvalidateAllAnalysesPass : PassInfoMixin<InvalidateAllAnalysesPass> {
1391   /// Run this pass over some unit of IR.
1392   template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
runInvalidateAllAnalysesPass1393   PreservedAnalyses run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) {
1394     return PreservedAnalyses::none();
1395   }
1396 };
1397 
1398 /// A utility pass template that simply runs another pass multiple times.
1399 ///
1400 /// This can be useful when debugging or testing passes. It also serves as an
1401 /// example of how to extend the pass manager in ways beyond composition.
1402 template <typename PassT>
1403 class RepeatedPass : public PassInfoMixin<RepeatedPass<PassT>> {
1404 public:
RepeatedPass(int Count,PassT P)1405   RepeatedPass(int Count, PassT P) : Count(Count), P(std::move(P)) {}
1406 
1407   template <typename IRUnitT, typename AnalysisManagerT, typename... Ts>
run(IRUnitT & IR,AnalysisManagerT & AM,Ts &&...Args)1408   PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, Ts &&... Args) {
1409 
1410     // Request PassInstrumentation from analysis manager, will use it to run
1411     // instrumenting callbacks for the passes later.
1412     // Here we use std::tuple wrapper over getResult which helps to extract
1413     // AnalysisManager's arguments out of the whole Args set.
1414     PassInstrumentation PI =
1415         detail::getAnalysisResult<PassInstrumentationAnalysis>(
1416             AM, IR, std::tuple<Ts...>(Args...));
1417 
1418     auto PA = PreservedAnalyses::all();
1419     for (int i = 0; i < Count; ++i) {
1420       // Check the PassInstrumentation's BeforePass callbacks before running the
1421       // pass, skip its execution completely if asked to (callback returns
1422       // false).
1423       if (!PI.runBeforePass<IRUnitT>(P, IR))
1424         continue;
1425       PA.intersect(P.run(IR, AM, std::forward<Ts>(Args)...));
1426       PI.runAfterPass(P, IR);
1427     }
1428     return PA;
1429   }
1430 
1431 private:
1432   int Count;
1433   PassT P;
1434 };
1435 
1436 template <typename PassT>
createRepeatedPass(int Count,PassT P)1437 RepeatedPass<PassT> createRepeatedPass(int Count, PassT P) {
1438   return RepeatedPass<PassT>(Count, std::move(P));
1439 }
1440 
1441 } // end namespace llvm
1442 
1443 #endif // LLVM_IR_PASSMANAGER_H
1444