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