[Alignment][NFC] Use Align with TargetLowering::setMinFunctionAlignment
[llvm-core.git] / include / llvm / IR / PassManager.h
blob965c01b167ccbd570c9bd953f39703780a23d695
1 //===- PassManager.h - Pass management infrastructure -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This header defines various interfaces for pass management in LLVM. There
11 /// is no "pass" interface in LLVM per se. Instead, an instance of any class
12 /// which supports a method to 'run' it over a unit of IR can be used as
13 /// a pass. A pass manager is generally a tool to collect a sequence of passes
14 /// which run over a particular IR construct, and run each of them in sequence
15 /// over each such construct in the containing IR construct. As there is no
16 /// containing IR construct for a Module, a manager for passes over modules
17 /// forms the base case which runs its managed passes in sequence over the
18 /// single module provided.
19 ///
20 /// The core IR library provides managers for running passes over
21 /// modules and functions.
22 ///
23 /// * FunctionPassManager can run over a Module, runs each pass over
24 /// a Function.
25 /// * ModulePassManager must be directly run, runs each pass over the Module.
26 ///
27 /// Note that the implementations of the pass managers use concept-based
28 /// polymorphism as outlined in the "Value Semantics and Concept-based
29 /// Polymorphism" talk (or its abbreviated sibling "Inheritance Is The Base
30 /// Class of Evil") by Sean Parent:
31 /// * http://github.com/sean-parent/sean-parent.github.com/wiki/Papers-and-Presentations
32 /// * http://www.youtube.com/watch?v=_BpMYeUFXv8
33 /// * http://channel9.msdn.com/Events/GoingNative/2013/Inheritance-Is-The-Base-Class-of-Evil
34 ///
35 //===----------------------------------------------------------------------===//
37 #ifndef LLVM_IR_PASSMANAGER_H
38 #define LLVM_IR_PASSMANAGER_H
40 #include "llvm/ADT/DenseMap.h"
41 #include "llvm/ADT/SmallPtrSet.h"
42 #include "llvm/ADT/StringRef.h"
43 #include "llvm/ADT/TinyPtrVector.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/Module.h"
46 #include "llvm/IR/PassInstrumentation.h"
47 #include "llvm/IR/PassManagerInternal.h"
48 #include "llvm/Support/Debug.h"
49 #include "llvm/Support/TypeName.h"
50 #include "llvm/Support/raw_ostream.h"
51 #include <algorithm>
52 #include <cassert>
53 #include <cstring>
54 #include <iterator>
55 #include <list>
56 #include <memory>
57 #include <tuple>
58 #include <type_traits>
59 #include <utility>
60 #include <vector>
62 namespace llvm {
64 /// A special type used by analysis passes to provide an address that
65 /// identifies that particular analysis pass type.
66 ///
67 /// Analysis passes should have a static data member of this type and derive
68 /// from the \c AnalysisInfoMixin to get a static ID method used to identify
69 /// the analysis in the pass management infrastructure.
70 struct alignas(8) AnalysisKey {};
72 /// A special type used to provide an address that identifies a set of related
73 /// analyses. These sets are primarily used below to mark sets of analyses as
74 /// preserved.
75 ///
76 /// For example, a transformation can indicate that it preserves the CFG of a
77 /// function by preserving the appropriate AnalysisSetKey. An analysis that
78 /// depends only on the CFG can then check if that AnalysisSetKey is preserved;
79 /// if it is, the analysis knows that it itself is preserved.
80 struct alignas(8) AnalysisSetKey {};
82 /// This templated class represents "all analyses that operate over \<a
83 /// particular IR unit\>" (e.g. a Function or a Module) in instances of
84 /// PreservedAnalysis.
85 ///
86 /// This lets a transformation say e.g. "I preserved all function analyses".
87 ///
88 /// Note that you must provide an explicit instantiation declaration and
89 /// definition for this template in order to get the correct behavior on
90 /// Windows. Otherwise, the address of SetKey will not be stable.
91 template <typename IRUnitT> class AllAnalysesOn {
92 public:
93 static AnalysisSetKey *ID() { return &SetKey; }
95 private:
96 static AnalysisSetKey SetKey;
99 template <typename IRUnitT> AnalysisSetKey AllAnalysesOn<IRUnitT>::SetKey;
101 extern template class AllAnalysesOn<Module>;
102 extern template class AllAnalysesOn<Function>;
104 /// Represents analyses that only rely on functions' control flow.
106 /// This can be used with \c PreservedAnalyses to mark the CFG as preserved and
107 /// to query whether it has been preserved.
109 /// The CFG of a function is defined as the set of basic blocks and the edges
110 /// between them. Changing the set of basic blocks in a function is enough to
111 /// mutate the CFG. Mutating the condition of a branch or argument of an
112 /// invoked function does not mutate the CFG, but changing the successor labels
113 /// of those instructions does.
114 class CFGAnalyses {
115 public:
116 static AnalysisSetKey *ID() { return &SetKey; }
118 private:
119 static AnalysisSetKey SetKey;
122 /// A set of analyses that are preserved following a run of a transformation
123 /// pass.
125 /// Transformation passes build and return these objects to communicate which
126 /// analyses are still valid after the transformation. For most passes this is
127 /// fairly simple: if they don't change anything all analyses are preserved,
128 /// otherwise only a short list of analyses that have been explicitly updated
129 /// are preserved.
131 /// This class also lets transformation passes mark abstract *sets* of analyses
132 /// as preserved. A transformation that (say) does not alter the CFG can
133 /// indicate such by marking a particular AnalysisSetKey as preserved, and
134 /// then analyses can query whether that AnalysisSetKey is preserved.
136 /// Finally, this class can represent an "abandoned" analysis, which is
137 /// not preserved even if it would be covered by some abstract set of analyses.
139 /// Given a `PreservedAnalyses` object, an analysis will typically want to
140 /// figure out whether it is preserved. In the example below, MyAnalysisType is
141 /// preserved if it's not abandoned, and (a) it's explicitly marked as
142 /// preserved, (b), the set AllAnalysesOn<MyIRUnit> is preserved, or (c) both
143 /// AnalysisSetA and AnalysisSetB are preserved.
145 /// ```
146 /// auto PAC = PA.getChecker<MyAnalysisType>();
147 /// if (PAC.preserved() || PAC.preservedSet<AllAnalysesOn<MyIRUnit>>() ||
148 /// (PAC.preservedSet<AnalysisSetA>() &&
149 /// PAC.preservedSet<AnalysisSetB>())) {
150 /// // The analysis has been successfully preserved ...
151 /// }
152 /// ```
153 class PreservedAnalyses {
154 public:
155 /// Convenience factory function for the empty preserved set.
156 static PreservedAnalyses none() { return PreservedAnalyses(); }
158 /// Construct a special preserved set that preserves all passes.
159 static PreservedAnalyses all() {
160 PreservedAnalyses PA;
161 PA.PreservedIDs.insert(&AllAnalysesKey);
162 return PA;
165 /// Construct a preserved analyses object with a single preserved set.
166 template <typename AnalysisSetT>
167 static PreservedAnalyses allInSet() {
168 PreservedAnalyses PA;
169 PA.preserveSet<AnalysisSetT>();
170 return PA;
173 /// Mark an analysis as preserved.
174 template <typename AnalysisT> void preserve() { preserve(AnalysisT::ID()); }
176 /// Given an analysis's ID, mark the analysis as preserved, adding it
177 /// to the set.
178 void preserve(AnalysisKey *ID) {
179 // Clear this ID from the explicit not-preserved set if present.
180 NotPreservedAnalysisIDs.erase(ID);
182 // If we're not already preserving all analyses (other than those in
183 // NotPreservedAnalysisIDs).
184 if (!areAllPreserved())
185 PreservedIDs.insert(ID);
188 /// Mark an analysis set as preserved.
189 template <typename AnalysisSetT> void preserveSet() {
190 preserveSet(AnalysisSetT::ID());
193 /// Mark an analysis set as preserved using its ID.
194 void preserveSet(AnalysisSetKey *ID) {
195 // If we're not already in the saturated 'all' state, add this set.
196 if (!areAllPreserved())
197 PreservedIDs.insert(ID);
200 /// Mark an analysis as abandoned.
202 /// An abandoned analysis is not preserved, even if it is nominally covered
203 /// by some other set or was previously explicitly marked as preserved.
205 /// Note that you can only abandon a specific analysis, not a *set* of
206 /// analyses.
207 template <typename AnalysisT> void abandon() { abandon(AnalysisT::ID()); }
209 /// Mark an analysis as abandoned using its ID.
211 /// An abandoned analysis is not preserved, even if it is nominally covered
212 /// by some other set or was previously explicitly marked as preserved.
214 /// Note that you can only abandon a specific analysis, not a *set* of
215 /// analyses.
216 void abandon(AnalysisKey *ID) {
217 PreservedIDs.erase(ID);
218 NotPreservedAnalysisIDs.insert(ID);
221 /// Intersect this set with another in place.
223 /// This is a mutating operation on this preserved set, removing all
224 /// preserved passes which are not also preserved in the argument.
225 void intersect(const PreservedAnalyses &Arg) {
226 if (Arg.areAllPreserved())
227 return;
228 if (areAllPreserved()) {
229 *this = Arg;
230 return;
232 // The intersection requires the *union* of the explicitly not-preserved
233 // IDs and the *intersection* of the preserved IDs.
234 for (auto ID : Arg.NotPreservedAnalysisIDs) {
235 PreservedIDs.erase(ID);
236 NotPreservedAnalysisIDs.insert(ID);
238 for (auto ID : PreservedIDs)
239 if (!Arg.PreservedIDs.count(ID))
240 PreservedIDs.erase(ID);
243 /// Intersect this set with a temporary other set in place.
245 /// This is a mutating operation on this preserved set, removing all
246 /// preserved passes which are not also preserved in the argument.
247 void intersect(PreservedAnalyses &&Arg) {
248 if (Arg.areAllPreserved())
249 return;
250 if (areAllPreserved()) {
251 *this = std::move(Arg);
252 return;
254 // The intersection requires the *union* of the explicitly not-preserved
255 // IDs and the *intersection* of the preserved IDs.
256 for (auto ID : Arg.NotPreservedAnalysisIDs) {
257 PreservedIDs.erase(ID);
258 NotPreservedAnalysisIDs.insert(ID);
260 for (auto ID : PreservedIDs)
261 if (!Arg.PreservedIDs.count(ID))
262 PreservedIDs.erase(ID);
265 /// A checker object that makes it easy to query for whether an analysis or
266 /// some set covering it is preserved.
267 class PreservedAnalysisChecker {
268 friend class PreservedAnalyses;
270 const PreservedAnalyses &PA;
271 AnalysisKey *const ID;
272 const bool IsAbandoned;
274 /// A PreservedAnalysisChecker is tied to a particular Analysis because
275 /// `preserved()` and `preservedSet()` both return false if the Analysis
276 /// was abandoned.
277 PreservedAnalysisChecker(const PreservedAnalyses &PA, AnalysisKey *ID)
278 : PA(PA), ID(ID), IsAbandoned(PA.NotPreservedAnalysisIDs.count(ID)) {}
280 public:
281 /// Returns true if the checker's analysis was not abandoned and either
282 /// - the analysis is explicitly preserved or
283 /// - all analyses are preserved.
284 bool preserved() {
285 return !IsAbandoned && (PA.PreservedIDs.count(&AllAnalysesKey) ||
286 PA.PreservedIDs.count(ID));
289 /// Return true if the checker's analysis was not abandoned, i.e. it was not
290 /// explicitly invalidated. Even if the analysis is not explicitly
291 /// preserved, if the analysis is known stateless, then it is preserved.
292 bool preservedWhenStateless() {
293 return !IsAbandoned;
296 /// Returns true if the checker's analysis was not abandoned and either
297 /// - \p AnalysisSetT is explicitly preserved or
298 /// - all analyses are preserved.
299 template <typename AnalysisSetT> bool preservedSet() {
300 AnalysisSetKey *SetID = AnalysisSetT::ID();
301 return !IsAbandoned && (PA.PreservedIDs.count(&AllAnalysesKey) ||
302 PA.PreservedIDs.count(SetID));
306 /// Build a checker for this `PreservedAnalyses` and the specified analysis
307 /// type.
309 /// You can use the returned object to query whether an analysis was
310 /// preserved. See the example in the comment on `PreservedAnalysis`.
311 template <typename AnalysisT> PreservedAnalysisChecker getChecker() const {
312 return PreservedAnalysisChecker(*this, AnalysisT::ID());
315 /// Build a checker for this `PreservedAnalyses` and the specified analysis
316 /// ID.
318 /// You can use the returned object to query whether an analysis was
319 /// preserved. See the example in the comment on `PreservedAnalysis`.
320 PreservedAnalysisChecker getChecker(AnalysisKey *ID) const {
321 return PreservedAnalysisChecker(*this, ID);
324 /// Test whether all analyses are preserved (and none are abandoned).
326 /// This is used primarily to optimize for the common case of a transformation
327 /// which makes no changes to the IR.
328 bool areAllPreserved() const {
329 return NotPreservedAnalysisIDs.empty() &&
330 PreservedIDs.count(&AllAnalysesKey);
333 /// Directly test whether a set of analyses is preserved.
335 /// This is only true when no analyses have been explicitly abandoned.
336 template <typename AnalysisSetT> bool allAnalysesInSetPreserved() const {
337 return allAnalysesInSetPreserved(AnalysisSetT::ID());
340 /// Directly test whether a set of analyses is preserved.
342 /// This is only true when no analyses have been explicitly abandoned.
343 bool allAnalysesInSetPreserved(AnalysisSetKey *SetID) const {
344 return NotPreservedAnalysisIDs.empty() &&
345 (PreservedIDs.count(&AllAnalysesKey) || PreservedIDs.count(SetID));
348 private:
349 /// A special key used to indicate all analyses.
350 static AnalysisSetKey AllAnalysesKey;
352 /// The IDs of analyses and analysis sets that are preserved.
353 SmallPtrSet<void *, 2> PreservedIDs;
355 /// The IDs of explicitly not-preserved analyses.
357 /// If an analysis in this set is covered by a set in `PreservedIDs`, we
358 /// consider it not-preserved. That is, `NotPreservedAnalysisIDs` always
359 /// "wins" over analysis sets in `PreservedIDs`.
361 /// Also, a given ID should never occur both here and in `PreservedIDs`.
362 SmallPtrSet<AnalysisKey *, 2> NotPreservedAnalysisIDs;
365 // Forward declare the analysis manager template.
366 template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
368 /// A CRTP mix-in to automatically provide informational APIs needed for
369 /// passes.
371 /// This provides some boilerplate for types that are passes.
372 template <typename DerivedT> struct PassInfoMixin {
373 /// Gets the name of the pass we are mixed into.
374 static StringRef name() {
375 static_assert(std::is_base_of<PassInfoMixin, DerivedT>::value,
376 "Must pass the derived type as the template argument!");
377 StringRef Name = getTypeName<DerivedT>();
378 if (Name.startswith("llvm::"))
379 Name = Name.drop_front(strlen("llvm::"));
380 return Name;
384 /// A CRTP mix-in that provides informational APIs needed for analysis passes.
386 /// This provides some boilerplate for types that are analysis passes. It
387 /// automatically mixes in \c PassInfoMixin.
388 template <typename DerivedT>
389 struct AnalysisInfoMixin : PassInfoMixin<DerivedT> {
390 /// Returns an opaque, unique ID for this analysis type.
392 /// This ID is a pointer type that is guaranteed to be 8-byte aligned and thus
393 /// suitable for use in sets, maps, and other data structures that use the low
394 /// bits of pointers.
396 /// Note that this requires the derived type provide a static \c AnalysisKey
397 /// member called \c Key.
399 /// FIXME: The only reason the mixin type itself can't declare the Key value
400 /// is that some compilers cannot correctly unique a templated static variable
401 /// so it has the same addresses in each instantiation. The only currently
402 /// known platform with this limitation is Windows DLL builds, specifically
403 /// building each part of LLVM as a DLL. If we ever remove that build
404 /// configuration, this mixin can provide the static key as well.
405 static AnalysisKey *ID() {
406 static_assert(std::is_base_of<AnalysisInfoMixin, DerivedT>::value,
407 "Must pass the derived type as the template argument!");
408 return &DerivedT::Key;
412 namespace detail {
414 /// Actual unpacker of extra arguments in getAnalysisResult,
415 /// passes only those tuple arguments that are mentioned in index_sequence.
416 template <typename PassT, typename IRUnitT, typename AnalysisManagerT,
417 typename... ArgTs, size_t... Ns>
418 typename PassT::Result
419 getAnalysisResultUnpackTuple(AnalysisManagerT &AM, IRUnitT &IR,
420 std::tuple<ArgTs...> Args,
421 std::index_sequence<Ns...>) {
422 (void)Args;
423 return AM.template getResult<PassT>(IR, std::get<Ns>(Args)...);
426 /// Helper for *partial* unpacking of extra arguments in getAnalysisResult.
428 /// Arguments passed in tuple come from PassManager, so they might have extra
429 /// arguments after those AnalysisManager's ExtraArgTs ones that we need to
430 /// pass to getResult.
431 template <typename PassT, typename IRUnitT, typename... AnalysisArgTs,
432 typename... MainArgTs>
433 typename PassT::Result
434 getAnalysisResult(AnalysisManager<IRUnitT, AnalysisArgTs...> &AM, IRUnitT &IR,
435 std::tuple<MainArgTs...> Args) {
436 return (getAnalysisResultUnpackTuple<
437 PassT, IRUnitT>)(AM, IR, Args,
438 std::index_sequence_for<AnalysisArgTs...>{});
441 } // namespace detail
443 // Forward declare the pass instrumentation analysis explicitly queried in
444 // generic PassManager code.
445 // FIXME: figure out a way to move PassInstrumentationAnalysis into its own
446 // header.
447 class PassInstrumentationAnalysis;
449 /// Manages a sequence of passes over a particular unit of IR.
451 /// A pass manager contains a sequence of passes to run over a particular unit
452 /// of IR (e.g. Functions, Modules). It is itself a valid pass over that unit of
453 /// IR, and when run over some given IR will run each of its contained passes in
454 /// sequence. Pass managers are the primary and most basic building block of a
455 /// pass pipeline.
457 /// When you run a pass manager, you provide an \c AnalysisManager<IRUnitT>
458 /// argument. The pass manager will propagate that analysis manager to each
459 /// pass it runs, and will call the analysis manager's invalidation routine with
460 /// the PreservedAnalyses of each pass it runs.
461 template <typename IRUnitT,
462 typename AnalysisManagerT = AnalysisManager<IRUnitT>,
463 typename... ExtraArgTs>
464 class PassManager : public PassInfoMixin<
465 PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...>> {
466 public:
467 /// Construct a pass manager.
469 /// If \p DebugLogging is true, we'll log our progress to llvm::dbgs().
470 explicit PassManager(bool DebugLogging = false) : DebugLogging(DebugLogging) {}
472 // FIXME: These are equivalent to the default move constructor/move
473 // assignment. However, using = default triggers linker errors due to the
474 // explicit instantiations below. Find away to use the default and remove the
475 // duplicated code here.
476 PassManager(PassManager &&Arg)
477 : Passes(std::move(Arg.Passes)),
478 DebugLogging(std::move(Arg.DebugLogging)) {}
480 PassManager &operator=(PassManager &&RHS) {
481 Passes = std::move(RHS.Passes);
482 DebugLogging = std::move(RHS.DebugLogging);
483 return *this;
486 /// Run all of the passes in this manager over the given unit of IR.
487 /// ExtraArgs are passed to each pass.
488 PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM,
489 ExtraArgTs... ExtraArgs) {
490 PreservedAnalyses PA = PreservedAnalyses::all();
492 // Request PassInstrumentation from analysis manager, will use it to run
493 // instrumenting callbacks for the passes later.
494 // Here we use std::tuple wrapper over getResult which helps to extract
495 // AnalysisManager's arguments out of the whole ExtraArgs set.
496 PassInstrumentation PI =
497 detail::getAnalysisResult<PassInstrumentationAnalysis>(
498 AM, IR, std::tuple<ExtraArgTs...>(ExtraArgs...));
500 if (DebugLogging)
501 dbgs() << "Starting " << getTypeName<IRUnitT>() << " pass manager run.\n";
503 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
504 auto *P = Passes[Idx].get();
505 if (DebugLogging)
506 dbgs() << "Running pass: " << P->name() << " on " << IR.getName()
507 << "\n";
509 // Check the PassInstrumentation's BeforePass callbacks before running the
510 // pass, skip its execution completely if asked to (callback returns
511 // false).
512 if (!PI.runBeforePass<IRUnitT>(*P, IR))
513 continue;
515 PreservedAnalyses PassPA = P->run(IR, AM, ExtraArgs...);
517 // Call onto PassInstrumentation's AfterPass callbacks immediately after
518 // running the pass.
519 PI.runAfterPass<IRUnitT>(*P, IR);
521 // Update the analysis manager as each pass runs and potentially
522 // invalidates analyses.
523 AM.invalidate(IR, PassPA);
525 // Finally, intersect the preserved analyses to compute the aggregate
526 // preserved set for this pass manager.
527 PA.intersect(std::move(PassPA));
529 // FIXME: Historically, the pass managers all called the LLVM context's
530 // yield function here. We don't have a generic way to acquire the
531 // context and it isn't yet clear what the right pattern is for yielding
532 // in the new pass manager so it is currently omitted.
533 //IR.getContext().yield();
536 // Invalidation was handled after each pass in the above loop for the
537 // current unit of IR. Therefore, the remaining analysis results in the
538 // AnalysisManager are preserved. We mark this with a set so that we don't
539 // need to inspect each one individually.
540 PA.preserveSet<AllAnalysesOn<IRUnitT>>();
542 if (DebugLogging)
543 dbgs() << "Finished " << getTypeName<IRUnitT>() << " pass manager run.\n";
545 return PA;
548 template <typename PassT> void addPass(PassT Pass) {
549 using PassModelT =
550 detail::PassModel<IRUnitT, PassT, PreservedAnalyses, AnalysisManagerT,
551 ExtraArgTs...>;
553 Passes.emplace_back(new PassModelT(std::move(Pass)));
556 private:
557 using PassConceptT =
558 detail::PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...>;
560 std::vector<std::unique_ptr<PassConceptT>> Passes;
562 /// Flag indicating whether we should do debug logging.
563 bool DebugLogging;
566 extern template class PassManager<Module>;
568 /// Convenience typedef for a pass manager over modules.
569 using ModulePassManager = PassManager<Module>;
571 extern template class PassManager<Function>;
573 /// Convenience typedef for a pass manager over functions.
574 using FunctionPassManager = PassManager<Function>;
576 /// Pseudo-analysis pass that exposes the \c PassInstrumentation to pass
577 /// managers. Goes before AnalysisManager definition to provide its
578 /// internals (e.g PassInstrumentationAnalysis::ID) for use there if needed.
579 /// FIXME: figure out a way to move PassInstrumentationAnalysis into its own
580 /// header.
581 class PassInstrumentationAnalysis
582 : public AnalysisInfoMixin<PassInstrumentationAnalysis> {
583 friend AnalysisInfoMixin<PassInstrumentationAnalysis>;
584 static AnalysisKey Key;
586 PassInstrumentationCallbacks *Callbacks;
588 public:
589 /// PassInstrumentationCallbacks object is shared, owned by something else,
590 /// not this analysis.
591 PassInstrumentationAnalysis(PassInstrumentationCallbacks *Callbacks = nullptr)
592 : Callbacks(Callbacks) {}
594 using Result = PassInstrumentation;
596 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
597 Result run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) {
598 return PassInstrumentation(Callbacks);
602 /// A container for analyses that lazily runs them and caches their
603 /// results.
605 /// This class can manage analyses for any IR unit where the address of the IR
606 /// unit sufficies as its identity.
607 template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager {
608 public:
609 class Invalidator;
611 private:
612 // Now that we've defined our invalidator, we can define the concept types.
613 using ResultConceptT =
614 detail::AnalysisResultConcept<IRUnitT, PreservedAnalyses, Invalidator>;
615 using PassConceptT =
616 detail::AnalysisPassConcept<IRUnitT, PreservedAnalyses, Invalidator,
617 ExtraArgTs...>;
619 /// List of analysis pass IDs and associated concept pointers.
621 /// Requires iterators to be valid across appending new entries and arbitrary
622 /// erases. Provides the analysis ID to enable finding iterators to a given
623 /// entry in maps below, and provides the storage for the actual result
624 /// concept.
625 using AnalysisResultListT =
626 std::list<std::pair<AnalysisKey *, std::unique_ptr<ResultConceptT>>>;
628 /// Map type from IRUnitT pointer to our custom list type.
629 using AnalysisResultListMapT = DenseMap<IRUnitT *, AnalysisResultListT>;
631 /// Map type from a pair of analysis ID and IRUnitT pointer to an
632 /// iterator into a particular result list (which is where the actual analysis
633 /// result is stored).
634 using AnalysisResultMapT =
635 DenseMap<std::pair<AnalysisKey *, IRUnitT *>,
636 typename AnalysisResultListT::iterator>;
638 public:
639 /// API to communicate dependencies between analyses during invalidation.
641 /// When an analysis result embeds handles to other analysis results, it
642 /// needs to be invalidated both when its own information isn't preserved and
643 /// when any of its embedded analysis results end up invalidated. We pass an
644 /// \c Invalidator object as an argument to \c invalidate() in order to let
645 /// the analysis results themselves define the dependency graph on the fly.
646 /// This lets us avoid building building an explicit representation of the
647 /// dependencies between analysis results.
648 class Invalidator {
649 public:
650 /// Trigger the invalidation of some other analysis pass if not already
651 /// handled and return whether it was in fact invalidated.
653 /// This is expected to be called from within a given analysis result's \c
654 /// invalidate method to trigger a depth-first walk of all inter-analysis
655 /// dependencies. The same \p IR unit and \p PA passed to that result's \c
656 /// invalidate method should in turn be provided to this routine.
658 /// The first time this is called for a given analysis pass, it will call
659 /// the corresponding result's \c invalidate method. Subsequent calls will
660 /// use a cache of the results of that initial call. It is an error to form
661 /// cyclic dependencies between analysis results.
663 /// This returns true if the given analysis's result is invalid. Any
664 /// dependecies on it will become invalid as a result.
665 template <typename PassT>
666 bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA) {
667 using ResultModelT =
668 detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
669 PreservedAnalyses, Invalidator>;
671 return invalidateImpl<ResultModelT>(PassT::ID(), IR, PA);
674 /// A type-erased variant of the above invalidate method with the same core
675 /// API other than passing an analysis ID rather than an analysis type
676 /// parameter.
678 /// This is sadly less efficient than the above routine, which leverages
679 /// the type parameter to avoid the type erasure overhead.
680 bool invalidate(AnalysisKey *ID, IRUnitT &IR, const PreservedAnalyses &PA) {
681 return invalidateImpl<>(ID, IR, PA);
684 private:
685 friend class AnalysisManager;
687 template <typename ResultT = ResultConceptT>
688 bool invalidateImpl(AnalysisKey *ID, IRUnitT &IR,
689 const PreservedAnalyses &PA) {
690 // If we've already visited this pass, return true if it was invalidated
691 // and false otherwise.
692 auto IMapI = IsResultInvalidated.find(ID);
693 if (IMapI != IsResultInvalidated.end())
694 return IMapI->second;
696 // Otherwise look up the result object.
697 auto RI = Results.find({ID, &IR});
698 assert(RI != Results.end() &&
699 "Trying to invalidate a dependent result that isn't in the "
700 "manager's cache is always an error, likely due to a stale result "
701 "handle!");
703 auto &Result = static_cast<ResultT &>(*RI->second->second);
705 // Insert into the map whether the result should be invalidated and return
706 // that. Note that we cannot reuse IMapI and must do a fresh insert here,
707 // as calling invalidate could (recursively) insert things into the map,
708 // making any iterator or reference invalid.
709 bool Inserted;
710 std::tie(IMapI, Inserted) =
711 IsResultInvalidated.insert({ID, Result.invalidate(IR, PA, *this)});
712 (void)Inserted;
713 assert(Inserted && "Should not have already inserted this ID, likely "
714 "indicates a dependency cycle!");
715 return IMapI->second;
718 Invalidator(SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated,
719 const AnalysisResultMapT &Results)
720 : IsResultInvalidated(IsResultInvalidated), Results(Results) {}
722 SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated;
723 const AnalysisResultMapT &Results;
726 /// Construct an empty analysis manager.
728 /// If \p DebugLogging is true, we'll log our progress to llvm::dbgs().
729 AnalysisManager(bool DebugLogging = false) : DebugLogging(DebugLogging) {}
730 AnalysisManager(AnalysisManager &&) = default;
731 AnalysisManager &operator=(AnalysisManager &&) = default;
733 /// Returns true if the analysis manager has an empty results cache.
734 bool empty() const {
735 assert(AnalysisResults.empty() == AnalysisResultLists.empty() &&
736 "The storage and index of analysis results disagree on how many "
737 "there are!");
738 return AnalysisResults.empty();
741 /// Clear any cached analysis results for a single unit of IR.
743 /// This doesn't invalidate, but instead simply deletes, the relevant results.
744 /// It is useful when the IR is being removed and we want to clear out all the
745 /// memory pinned for it.
746 void clear(IRUnitT &IR, llvm::StringRef Name) {
747 if (DebugLogging)
748 dbgs() << "Clearing all analysis results for: " << Name << "\n";
750 auto ResultsListI = AnalysisResultLists.find(&IR);
751 if (ResultsListI == AnalysisResultLists.end())
752 return;
753 // Delete the map entries that point into the results list.
754 for (auto &IDAndResult : ResultsListI->second)
755 AnalysisResults.erase({IDAndResult.first, &IR});
757 // And actually destroy and erase the results associated with this IR.
758 AnalysisResultLists.erase(ResultsListI);
761 /// Clear all analysis results cached by this AnalysisManager.
763 /// Like \c clear(IRUnitT&), this doesn't invalidate the results; it simply
764 /// deletes them. This lets you clean up the AnalysisManager when the set of
765 /// IR units itself has potentially changed, and thus we can't even look up a
766 /// a result and invalidate/clear it directly.
767 void clear() {
768 AnalysisResults.clear();
769 AnalysisResultLists.clear();
772 /// Get the result of an analysis pass for a given IR unit.
774 /// Runs the analysis if a cached result is not available.
775 template <typename PassT>
776 typename PassT::Result &getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs) {
777 assert(AnalysisPasses.count(PassT::ID()) &&
778 "This analysis pass was not registered prior to being queried");
779 ResultConceptT &ResultConcept =
780 getResultImpl(PassT::ID(), IR, ExtraArgs...);
782 using ResultModelT =
783 detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
784 PreservedAnalyses, Invalidator>;
786 return static_cast<ResultModelT &>(ResultConcept).Result;
789 /// Get the cached result of an analysis pass for a given IR unit.
791 /// This method never runs the analysis.
793 /// \returns null if there is no cached result.
794 template <typename PassT>
795 typename PassT::Result *getCachedResult(IRUnitT &IR) const {
796 assert(AnalysisPasses.count(PassT::ID()) &&
797 "This analysis pass was not registered prior to being queried");
799 ResultConceptT *ResultConcept = getCachedResultImpl(PassT::ID(), IR);
800 if (!ResultConcept)
801 return nullptr;
803 using ResultModelT =
804 detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
805 PreservedAnalyses, Invalidator>;
807 return &static_cast<ResultModelT *>(ResultConcept)->Result;
810 /// Register an analysis pass with the manager.
812 /// The parameter is a callable whose result is an analysis pass. This allows
813 /// passing in a lambda to construct the analysis.
815 /// The analysis type to register is the type returned by calling the \c
816 /// PassBuilder argument. If that type has already been registered, then the
817 /// argument will not be called and this function will return false.
818 /// Otherwise, we register the analysis returned by calling \c PassBuilder(),
819 /// and this function returns true.
821 /// (Note: Although the return value of this function indicates whether or not
822 /// an analysis was previously registered, there intentionally isn't a way to
823 /// query this directly. Instead, you should just register all the analyses
824 /// you might want and let this class run them lazily. This idiom lets us
825 /// minimize the number of times we have to look up analyses in our
826 /// hashtable.)
827 template <typename PassBuilderT>
828 bool registerPass(PassBuilderT &&PassBuilder) {
829 using PassT = decltype(PassBuilder());
830 using PassModelT =
831 detail::AnalysisPassModel<IRUnitT, PassT, PreservedAnalyses,
832 Invalidator, ExtraArgTs...>;
834 auto &PassPtr = AnalysisPasses[PassT::ID()];
835 if (PassPtr)
836 // Already registered this pass type!
837 return false;
839 // Construct a new model around the instance returned by the builder.
840 PassPtr.reset(new PassModelT(PassBuilder()));
841 return true;
844 /// Invalidate a specific analysis pass for an IR module.
846 /// Note that the analysis result can disregard invalidation, if it determines
847 /// it is in fact still valid.
848 template <typename PassT> void invalidate(IRUnitT &IR) {
849 assert(AnalysisPasses.count(PassT::ID()) &&
850 "This analysis pass was not registered prior to being invalidated");
851 invalidateImpl(PassT::ID(), IR);
854 /// Invalidate cached analyses for an IR unit.
856 /// Walk through all of the analyses pertaining to this unit of IR and
857 /// invalidate them, unless they are preserved by the PreservedAnalyses set.
858 void invalidate(IRUnitT &IR, const PreservedAnalyses &PA) {
859 // We're done if all analyses on this IR unit are preserved.
860 if (PA.allAnalysesInSetPreserved<AllAnalysesOn<IRUnitT>>())
861 return;
863 if (DebugLogging)
864 dbgs() << "Invalidating all non-preserved analyses for: " << IR.getName()
865 << "\n";
867 // Track whether each analysis's result is invalidated in
868 // IsResultInvalidated.
869 SmallDenseMap<AnalysisKey *, bool, 8> IsResultInvalidated;
870 Invalidator Inv(IsResultInvalidated, AnalysisResults);
871 AnalysisResultListT &ResultsList = AnalysisResultLists[&IR];
872 for (auto &AnalysisResultPair : ResultsList) {
873 // This is basically the same thing as Invalidator::invalidate, but we
874 // can't call it here because we're operating on the type-erased result.
875 // Moreover if we instead called invalidate() directly, it would do an
876 // unnecessary look up in ResultsList.
877 AnalysisKey *ID = AnalysisResultPair.first;
878 auto &Result = *AnalysisResultPair.second;
880 auto IMapI = IsResultInvalidated.find(ID);
881 if (IMapI != IsResultInvalidated.end())
882 // This result was already handled via the Invalidator.
883 continue;
885 // Try to invalidate the result, giving it the Invalidator so it can
886 // recursively query for any dependencies it has and record the result.
887 // Note that we cannot reuse 'IMapI' here or pre-insert the ID, as
888 // Result.invalidate may insert things into the map, invalidating our
889 // iterator.
890 bool Inserted =
891 IsResultInvalidated.insert({ID, Result.invalidate(IR, PA, Inv)})
892 .second;
893 (void)Inserted;
894 assert(Inserted && "Should never have already inserted this ID, likely "
895 "indicates a cycle!");
898 // Now erase the results that were marked above as invalidated.
899 if (!IsResultInvalidated.empty()) {
900 for (auto I = ResultsList.begin(), E = ResultsList.end(); I != E;) {
901 AnalysisKey *ID = I->first;
902 if (!IsResultInvalidated.lookup(ID)) {
903 ++I;
904 continue;
907 if (DebugLogging)
908 dbgs() << "Invalidating analysis: " << this->lookUpPass(ID).name()
909 << " on " << IR.getName() << "\n";
911 I = ResultsList.erase(I);
912 AnalysisResults.erase({ID, &IR});
916 if (ResultsList.empty())
917 AnalysisResultLists.erase(&IR);
920 private:
921 /// Look up a registered analysis pass.
922 PassConceptT &lookUpPass(AnalysisKey *ID) {
923 typename AnalysisPassMapT::iterator PI = AnalysisPasses.find(ID);
924 assert(PI != AnalysisPasses.end() &&
925 "Analysis passes must be registered prior to being queried!");
926 return *PI->second;
929 /// Look up a registered analysis pass.
930 const PassConceptT &lookUpPass(AnalysisKey *ID) const {
931 typename AnalysisPassMapT::const_iterator PI = AnalysisPasses.find(ID);
932 assert(PI != AnalysisPasses.end() &&
933 "Analysis passes must be registered prior to being queried!");
934 return *PI->second;
937 /// Get an analysis result, running the pass if necessary.
938 ResultConceptT &getResultImpl(AnalysisKey *ID, IRUnitT &IR,
939 ExtraArgTs... ExtraArgs) {
940 typename AnalysisResultMapT::iterator RI;
941 bool Inserted;
942 std::tie(RI, Inserted) = AnalysisResults.insert(std::make_pair(
943 std::make_pair(ID, &IR), typename AnalysisResultListT::iterator()));
945 // If we don't have a cached result for this function, look up the pass and
946 // run it to produce a result, which we then add to the cache.
947 if (Inserted) {
948 auto &P = this->lookUpPass(ID);
949 if (DebugLogging)
950 dbgs() << "Running analysis: " << P.name() << " on " << IR.getName()
951 << "\n";
953 PassInstrumentation PI;
954 if (ID != PassInstrumentationAnalysis::ID()) {
955 PI = getResult<PassInstrumentationAnalysis>(IR, ExtraArgs...);
956 PI.runBeforeAnalysis(P, IR);
959 AnalysisResultListT &ResultList = AnalysisResultLists[&IR];
960 ResultList.emplace_back(ID, P.run(IR, *this, ExtraArgs...));
962 PI.runAfterAnalysis(P, IR);
964 // P.run may have inserted elements into AnalysisResults and invalidated
965 // RI.
966 RI = AnalysisResults.find({ID, &IR});
967 assert(RI != AnalysisResults.end() && "we just inserted it!");
969 RI->second = std::prev(ResultList.end());
972 return *RI->second->second;
975 /// Get a cached analysis result or return null.
976 ResultConceptT *getCachedResultImpl(AnalysisKey *ID, IRUnitT &IR) const {
977 typename AnalysisResultMapT::const_iterator RI =
978 AnalysisResults.find({ID, &IR});
979 return RI == AnalysisResults.end() ? nullptr : &*RI->second->second;
982 /// Invalidate a function pass result.
983 void invalidateImpl(AnalysisKey *ID, IRUnitT &IR) {
984 typename AnalysisResultMapT::iterator RI =
985 AnalysisResults.find({ID, &IR});
986 if (RI == AnalysisResults.end())
987 return;
989 if (DebugLogging)
990 dbgs() << "Invalidating analysis: " << this->lookUpPass(ID).name()
991 << " on " << IR.getName() << "\n";
992 AnalysisResultLists[&IR].erase(RI->second);
993 AnalysisResults.erase(RI);
996 /// Map type from module analysis pass ID to pass concept pointer.
997 using AnalysisPassMapT =
998 DenseMap<AnalysisKey *, std::unique_ptr<PassConceptT>>;
1000 /// Collection of module analysis passes, indexed by ID.
1001 AnalysisPassMapT AnalysisPasses;
1003 /// Map from function to a list of function analysis results.
1005 /// Provides linear time removal of all analysis results for a function and
1006 /// the ultimate storage for a particular cached analysis result.
1007 AnalysisResultListMapT AnalysisResultLists;
1009 /// Map from an analysis ID and function to a particular cached
1010 /// analysis result.
1011 AnalysisResultMapT AnalysisResults;
1013 /// Indicates whether we log to \c llvm::dbgs().
1014 bool DebugLogging;
1017 extern template class AnalysisManager<Module>;
1019 /// Convenience typedef for the Module analysis manager.
1020 using ModuleAnalysisManager = AnalysisManager<Module>;
1022 extern template class AnalysisManager<Function>;
1024 /// Convenience typedef for the Function analysis manager.
1025 using FunctionAnalysisManager = AnalysisManager<Function>;
1027 /// An analysis over an "outer" IR unit that provides access to an
1028 /// analysis manager over an "inner" IR unit. The inner unit must be contained
1029 /// in the outer unit.
1031 /// For example, InnerAnalysisManagerProxy<FunctionAnalysisManager, Module> is
1032 /// an analysis over Modules (the "outer" unit) that provides access to a
1033 /// Function analysis manager. The FunctionAnalysisManager is the "inner"
1034 /// manager being proxied, and Functions are the "inner" unit. The inner/outer
1035 /// relationship is valid because each Function is contained in one Module.
1037 /// If you're (transitively) within a pass manager for an IR unit U that
1038 /// contains IR unit V, you should never use an analysis manager over V, except
1039 /// via one of these proxies.
1041 /// Note that the proxy's result is a move-only RAII object. The validity of
1042 /// the analyses in the inner analysis manager is tied to its lifetime.
1043 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1044 class InnerAnalysisManagerProxy
1045 : public AnalysisInfoMixin<
1046 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>> {
1047 public:
1048 class Result {
1049 public:
1050 explicit Result(AnalysisManagerT &InnerAM) : InnerAM(&InnerAM) {}
1052 Result(Result &&Arg) : InnerAM(std::move(Arg.InnerAM)) {
1053 // We have to null out the analysis manager in the moved-from state
1054 // because we are taking ownership of the responsibilty to clear the
1055 // analysis state.
1056 Arg.InnerAM = nullptr;
1059 ~Result() {
1060 // InnerAM is cleared in a moved from state where there is nothing to do.
1061 if (!InnerAM)
1062 return;
1064 // Clear out the analysis manager if we're being destroyed -- it means we
1065 // didn't even see an invalidate call when we got invalidated.
1066 InnerAM->clear();
1069 Result &operator=(Result &&RHS) {
1070 InnerAM = RHS.InnerAM;
1071 // We have to null out the analysis manager in the moved-from state
1072 // because we are taking ownership of the responsibilty to clear the
1073 // analysis state.
1074 RHS.InnerAM = nullptr;
1075 return *this;
1078 /// Accessor for the analysis manager.
1079 AnalysisManagerT &getManager() { return *InnerAM; }
1081 /// Handler for invalidation of the outer IR unit, \c IRUnitT.
1083 /// If the proxy analysis itself is not preserved, we assume that the set of
1084 /// inner IR objects contained in IRUnit may have changed. In this case,
1085 /// we have to call \c clear() on the inner analysis manager, as it may now
1086 /// have stale pointers to its inner IR objects.
1088 /// Regardless of whether the proxy analysis is marked as preserved, all of
1089 /// the analyses in the inner analysis manager are potentially invalidated
1090 /// based on the set of preserved analyses.
1091 bool invalidate(
1092 IRUnitT &IR, const PreservedAnalyses &PA,
1093 typename AnalysisManager<IRUnitT, ExtraArgTs...>::Invalidator &Inv);
1095 private:
1096 AnalysisManagerT *InnerAM;
1099 explicit InnerAnalysisManagerProxy(AnalysisManagerT &InnerAM)
1100 : InnerAM(&InnerAM) {}
1102 /// Run the analysis pass and create our proxy result object.
1104 /// This doesn't do any interesting work; it is primarily used to insert our
1105 /// proxy result object into the outer analysis cache so that we can proxy
1106 /// invalidation to the inner analysis manager.
1107 Result run(IRUnitT &IR, AnalysisManager<IRUnitT, ExtraArgTs...> &AM,
1108 ExtraArgTs...) {
1109 return Result(*InnerAM);
1112 private:
1113 friend AnalysisInfoMixin<
1114 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>>;
1116 static AnalysisKey Key;
1118 AnalysisManagerT *InnerAM;
1121 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1122 AnalysisKey
1123 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
1125 /// Provide the \c FunctionAnalysisManager to \c Module proxy.
1126 using FunctionAnalysisManagerModuleProxy =
1127 InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>;
1129 /// Specialization of the invalidate method for the \c
1130 /// FunctionAnalysisManagerModuleProxy's result.
1131 template <>
1132 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
1133 Module &M, const PreservedAnalyses &PA,
1134 ModuleAnalysisManager::Invalidator &Inv);
1136 // Ensure the \c FunctionAnalysisManagerModuleProxy is provided as an extern
1137 // template.
1138 extern template class InnerAnalysisManagerProxy<FunctionAnalysisManager,
1139 Module>;
1141 /// An analysis over an "inner" IR unit that provides access to an
1142 /// analysis manager over a "outer" IR unit. The inner unit must be contained
1143 /// in the outer unit.
1145 /// For example OuterAnalysisManagerProxy<ModuleAnalysisManager, Function> is an
1146 /// analysis over Functions (the "inner" unit) which provides access to a Module
1147 /// analysis manager. The ModuleAnalysisManager is the "outer" manager being
1148 /// proxied, and Modules are the "outer" IR unit. The inner/outer relationship
1149 /// is valid because each Function is contained in one Module.
1151 /// This proxy only exposes the const interface of the outer analysis manager,
1152 /// to indicate that you cannot cause an outer analysis to run from within an
1153 /// inner pass. Instead, you must rely on the \c getCachedResult API.
1155 /// This proxy doesn't manage invalidation in any way -- that is handled by the
1156 /// recursive return path of each layer of the pass manager. A consequence of
1157 /// this is the outer analyses may be stale. We invalidate the outer analyses
1158 /// only when we're done running passes over the inner IR units.
1159 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1160 class OuterAnalysisManagerProxy
1161 : public AnalysisInfoMixin<
1162 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>> {
1163 public:
1164 /// Result proxy object for \c OuterAnalysisManagerProxy.
1165 class Result {
1166 public:
1167 explicit Result(const AnalysisManagerT &AM) : AM(&AM) {}
1169 const AnalysisManagerT &getManager() const { return *AM; }
1171 /// When invalidation occurs, remove any registered invalidation events.
1172 bool invalidate(
1173 IRUnitT &IRUnit, const PreservedAnalyses &PA,
1174 typename AnalysisManager<IRUnitT, ExtraArgTs...>::Invalidator &Inv) {
1175 // Loop over the set of registered outer invalidation mappings and if any
1176 // of them map to an analysis that is now invalid, clear it out.
1177 SmallVector<AnalysisKey *, 4> DeadKeys;
1178 for (auto &KeyValuePair : OuterAnalysisInvalidationMap) {
1179 AnalysisKey *OuterID = KeyValuePair.first;
1180 auto &InnerIDs = KeyValuePair.second;
1181 InnerIDs.erase(llvm::remove_if(InnerIDs, [&](AnalysisKey *InnerID) {
1182 return Inv.invalidate(InnerID, IRUnit, PA); }),
1183 InnerIDs.end());
1184 if (InnerIDs.empty())
1185 DeadKeys.push_back(OuterID);
1188 for (auto OuterID : DeadKeys)
1189 OuterAnalysisInvalidationMap.erase(OuterID);
1191 // The proxy itself remains valid regardless of anything else.
1192 return false;
1195 /// Register a deferred invalidation event for when the outer analysis
1196 /// manager processes its invalidations.
1197 template <typename OuterAnalysisT, typename InvalidatedAnalysisT>
1198 void registerOuterAnalysisInvalidation() {
1199 AnalysisKey *OuterID = OuterAnalysisT::ID();
1200 AnalysisKey *InvalidatedID = InvalidatedAnalysisT::ID();
1202 auto &InvalidatedIDList = OuterAnalysisInvalidationMap[OuterID];
1203 // Note, this is a linear scan. If we end up with large numbers of
1204 // analyses that all trigger invalidation on the same outer analysis,
1205 // this entire system should be changed to some other deterministic
1206 // data structure such as a `SetVector` of a pair of pointers.
1207 auto InvalidatedIt = std::find(InvalidatedIDList.begin(),
1208 InvalidatedIDList.end(), InvalidatedID);
1209 if (InvalidatedIt == InvalidatedIDList.end())
1210 InvalidatedIDList.push_back(InvalidatedID);
1213 /// Access the map from outer analyses to deferred invalidation requiring
1214 /// analyses.
1215 const SmallDenseMap<AnalysisKey *, TinyPtrVector<AnalysisKey *>, 2> &
1216 getOuterInvalidations() const {
1217 return OuterAnalysisInvalidationMap;
1220 private:
1221 const AnalysisManagerT *AM;
1223 /// A map from an outer analysis ID to the set of this IR-unit's analyses
1224 /// which need to be invalidated.
1225 SmallDenseMap<AnalysisKey *, TinyPtrVector<AnalysisKey *>, 2>
1226 OuterAnalysisInvalidationMap;
1229 OuterAnalysisManagerProxy(const AnalysisManagerT &AM) : AM(&AM) {}
1231 /// Run the analysis pass and create our proxy result object.
1232 /// Nothing to see here, it just forwards the \c AM reference into the
1233 /// result.
1234 Result run(IRUnitT &, AnalysisManager<IRUnitT, ExtraArgTs...> &,
1235 ExtraArgTs...) {
1236 return Result(*AM);
1239 private:
1240 friend AnalysisInfoMixin<
1241 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>>;
1243 static AnalysisKey Key;
1245 const AnalysisManagerT *AM;
1248 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1249 AnalysisKey
1250 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
1252 extern template class OuterAnalysisManagerProxy<ModuleAnalysisManager,
1253 Function>;
1254 /// Provide the \c ModuleAnalysisManager to \c Function proxy.
1255 using ModuleAnalysisManagerFunctionProxy =
1256 OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>;
1258 /// Trivial adaptor that maps from a module to its functions.
1260 /// Designed to allow composition of a FunctionPass(Manager) and
1261 /// a ModulePassManager, by running the FunctionPass(Manager) over every
1262 /// function in the module.
1264 /// Function passes run within this adaptor can rely on having exclusive access
1265 /// to the function they are run over. They should not read or modify any other
1266 /// functions! Other threads or systems may be manipulating other functions in
1267 /// the module, and so their state should never be relied on.
1268 /// FIXME: Make the above true for all of LLVM's actual passes, some still
1269 /// violate this principle.
1271 /// Function passes can also read the module containing the function, but they
1272 /// should not modify that module outside of the use lists of various globals.
1273 /// For example, a function pass is not permitted to add functions to the
1274 /// module.
1275 /// FIXME: Make the above true for all of LLVM's actual passes, some still
1276 /// violate this principle.
1278 /// Note that although function passes can access module analyses, module
1279 /// analyses are not invalidated while the function passes are running, so they
1280 /// may be stale. Function analyses will not be stale.
1281 template <typename FunctionPassT>
1282 class ModuleToFunctionPassAdaptor
1283 : public PassInfoMixin<ModuleToFunctionPassAdaptor<FunctionPassT>> {
1284 public:
1285 explicit ModuleToFunctionPassAdaptor(FunctionPassT Pass)
1286 : Pass(std::move(Pass)) {}
1288 /// Runs the function pass across every function in the module.
1289 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM) {
1290 FunctionAnalysisManager &FAM =
1291 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
1293 // Request PassInstrumentation from analysis manager, will use it to run
1294 // instrumenting callbacks for the passes later.
1295 PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(M);
1297 PreservedAnalyses PA = PreservedAnalyses::all();
1298 for (Function &F : M) {
1299 if (F.isDeclaration())
1300 continue;
1302 // Check the PassInstrumentation's BeforePass callbacks before running the
1303 // pass, skip its execution completely if asked to (callback returns
1304 // false).
1305 if (!PI.runBeforePass<Function>(Pass, F))
1306 continue;
1307 PreservedAnalyses PassPA = Pass.run(F, FAM);
1309 PI.runAfterPass(Pass, F);
1311 // We know that the function pass couldn't have invalidated any other
1312 // function's analyses (that's the contract of a function pass), so
1313 // directly handle the function analysis manager's invalidation here.
1314 FAM.invalidate(F, PassPA);
1316 // Then intersect the preserved set so that invalidation of module
1317 // analyses will eventually occur when the module pass completes.
1318 PA.intersect(std::move(PassPA));
1321 // The FunctionAnalysisManagerModuleProxy is preserved because (we assume)
1322 // the function passes we ran didn't add or remove any functions.
1324 // We also preserve all analyses on Functions, because we did all the
1325 // invalidation we needed to do above.
1326 PA.preserveSet<AllAnalysesOn<Function>>();
1327 PA.preserve<FunctionAnalysisManagerModuleProxy>();
1328 return PA;
1331 private:
1332 FunctionPassT Pass;
1335 /// A function to deduce a function pass type and wrap it in the
1336 /// templated adaptor.
1337 template <typename FunctionPassT>
1338 ModuleToFunctionPassAdaptor<FunctionPassT>
1339 createModuleToFunctionPassAdaptor(FunctionPassT Pass) {
1340 return ModuleToFunctionPassAdaptor<FunctionPassT>(std::move(Pass));
1343 /// A utility pass template to force an analysis result to be available.
1345 /// If there are extra arguments at the pass's run level there may also be
1346 /// extra arguments to the analysis manager's \c getResult routine. We can't
1347 /// guess how to effectively map the arguments from one to the other, and so
1348 /// this specialization just ignores them.
1350 /// Specific patterns of run-method extra arguments and analysis manager extra
1351 /// arguments will have to be defined as appropriate specializations.
1352 template <typename AnalysisT, typename IRUnitT,
1353 typename AnalysisManagerT = AnalysisManager<IRUnitT>,
1354 typename... ExtraArgTs>
1355 struct RequireAnalysisPass
1356 : PassInfoMixin<RequireAnalysisPass<AnalysisT, IRUnitT, AnalysisManagerT,
1357 ExtraArgTs...>> {
1358 /// Run this pass over some unit of IR.
1360 /// This pass can be run over any unit of IR and use any analysis manager
1361 /// provided they satisfy the basic API requirements. When this pass is
1362 /// created, these methods can be instantiated to satisfy whatever the
1363 /// context requires.
1364 PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM,
1365 ExtraArgTs &&... Args) {
1366 (void)AM.template getResult<AnalysisT>(Arg,
1367 std::forward<ExtraArgTs>(Args)...);
1369 return PreservedAnalyses::all();
1373 /// A no-op pass template which simply forces a specific analysis result
1374 /// to be invalidated.
1375 template <typename AnalysisT>
1376 struct InvalidateAnalysisPass
1377 : PassInfoMixin<InvalidateAnalysisPass<AnalysisT>> {
1378 /// Run this pass over some unit of IR.
1380 /// This pass can be run over any unit of IR and use any analysis manager,
1381 /// provided they satisfy the basic API requirements. When this pass is
1382 /// created, these methods can be instantiated to satisfy whatever the
1383 /// context requires.
1384 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
1385 PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&...) {
1386 auto PA = PreservedAnalyses::all();
1387 PA.abandon<AnalysisT>();
1388 return PA;
1392 /// A utility pass that does nothing, but preserves no analyses.
1394 /// Because this preserves no analyses, any analysis passes queried after this
1395 /// pass runs will recompute fresh results.
1396 struct InvalidateAllAnalysesPass : PassInfoMixin<InvalidateAllAnalysesPass> {
1397 /// Run this pass over some unit of IR.
1398 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
1399 PreservedAnalyses run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) {
1400 return PreservedAnalyses::none();
1404 /// A utility pass template that simply runs another pass multiple times.
1406 /// This can be useful when debugging or testing passes. It also serves as an
1407 /// example of how to extend the pass manager in ways beyond composition.
1408 template <typename PassT>
1409 class RepeatedPass : public PassInfoMixin<RepeatedPass<PassT>> {
1410 public:
1411 RepeatedPass(int Count, PassT P) : Count(Count), P(std::move(P)) {}
1413 template <typename IRUnitT, typename AnalysisManagerT, typename... Ts>
1414 PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, Ts &&... Args) {
1416 // Request PassInstrumentation from analysis manager, will use it to run
1417 // instrumenting callbacks for the passes later.
1418 // Here we use std::tuple wrapper over getResult which helps to extract
1419 // AnalysisManager's arguments out of the whole Args set.
1420 PassInstrumentation PI =
1421 detail::getAnalysisResult<PassInstrumentationAnalysis>(
1422 AM, IR, std::tuple<Ts...>(Args...));
1424 auto PA = PreservedAnalyses::all();
1425 for (int i = 0; i < Count; ++i) {
1426 // Check the PassInstrumentation's BeforePass callbacks before running the
1427 // pass, skip its execution completely if asked to (callback returns
1428 // false).
1429 if (!PI.runBeforePass<IRUnitT>(P, IR))
1430 continue;
1431 PA.intersect(P.run(IR, AM, std::forward<Ts>(Args)...));
1432 PI.runAfterPass(P, IR);
1434 return PA;
1437 private:
1438 int Count;
1439 PassT P;
1442 template <typename PassT>
1443 RepeatedPass<PassT> createRepeatedPass(int Count, PassT P) {
1444 return RepeatedPass<PassT>(Count, std::move(P));
1447 } // end namespace llvm
1449 #endif // LLVM_IR_PASSMANAGER_H