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
64 namespace 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.
72 struct 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.
82 struct 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.
93 template <typename IRUnitT> class AllAnalysesOn {
94 public:
ID()95 static AnalysisSetKey *ID() { return &SetKey; }
96
97 private:
98 static AnalysisSetKey SetKey;
99 };
100
101 template <typename IRUnitT> AnalysisSetKey AllAnalysesOn<IRUnitT>::SetKey;
102
103 extern template class AllAnalysesOn<Module>;
104 extern 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.
116 class CFGAnalyses {
117 public:
ID()118 static AnalysisSetKey *ID() { return &SetKey; }
119
120 private:
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 /// ```
155 class PreservedAnalyses {
156 public:
157 /// Convenience factory function for the empty preserved set.
none()158 static PreservedAnalyses none() { return PreservedAnalyses(); }
159
160 /// Construct a special preserved set that preserves all passes.
all()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>
allInSet()169 static PreservedAnalyses allInSet() {
170 PreservedAnalyses PA;
171 PA.preserveSet<AnalysisSetT>();
172 return PA;
173 }
174
175 /// Mark an analysis as preserved.
preserve()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.
preserve(AnalysisKey * ID)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.
preserveSet()191 template <typename AnalysisSetT> void preserveSet() {
192 preserveSet(AnalysisSetT::ID());
193 }
194
195 /// Mark an analysis set as preserved using its ID.
preserveSet(AnalysisSetKey * 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.
abandon()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.
abandon(AnalysisKey * ID)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.
intersect(const PreservedAnalyses & Arg)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.
intersect(PreservedAnalyses && Arg)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.
PreservedAnalysisChecker(const PreservedAnalyses & PA,AnalysisKey * ID)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.
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.
preservedWhenStateless()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.
preservedSet()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`.
getChecker()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`.
getChecker(AnalysisKey * ID)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.
areAllPreserved()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.
allAnalysesInSetPreserved()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.
allAnalysesInSetPreserved(AnalysisSetKey * SetID)345 bool allAnalysesInSetPreserved(AnalysisSetKey *SetID) const {
346 return NotPreservedAnalysisIDs.empty() &&
347 (PreservedIDs.count(&AllAnalysesKey) || PreservedIDs.count(SetID));
348 }
349
350 private:
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.
368 template <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.
374 template <typename DerivedT> struct PassInfoMixin {
375 /// Gets the name of the pass we are mixed into.
namePassInfoMixin376 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.
390 template <typename DerivedT>
391 struct 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.
IDAnalysisInfoMixin407 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
414 namespace detail {
415
416 /// Actual unpacker of extra arguments in getAnalysisResult,
417 /// passes only those tuple arguments that are mentioned in index_sequence.
418 template <typename PassT, typename IRUnitT, typename AnalysisManagerT,
419 typename... ArgTs, size_t... Ns>
420 typename PassT::Result
getAnalysisResultUnpackTuple(AnalysisManagerT & AM,IRUnitT & IR,std::tuple<ArgTs...> Args,std::index_sequence<Ns...>)421 getAnalysisResultUnpackTuple(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.
433 template <typename PassT, typename IRUnitT, typename... AnalysisArgTs,
434 typename... MainArgTs>
435 typename PassT::Result
getAnalysisResult(AnalysisManager<IRUnitT,AnalysisArgTs...> & AM,IRUnitT & IR,std::tuple<MainArgTs...> Args)436 getAnalysisResult(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.
449 class 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.
463 template <typename IRUnitT,
464 typename AnalysisManagerT = AnalysisManager<IRUnitT>,
465 typename... ExtraArgTs>
466 class PassManager : public PassInfoMixin<
467 PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...>> {
468 public:
469 /// Construct a pass manager.
470 ///
471 /// If \p DebugLogging is true, we'll log our progress to llvm::dbgs().
DebugLogging(DebugLogging)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.
PassManager(PassManager && Arg)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.
run(IRUnitT & IR,AnalysisManagerT & AM,ExtraArgTs...ExtraArgs)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>
addPass(PassT Pass)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>
addPass(PassT && Pass)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.
isEmpty()574 bool isEmpty() const { return Passes.empty(); }
575
isRequired()576 static bool isRequired() { return true; }
577
578 protected:
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
588 extern template class PassManager<Module>;
589
590 /// Convenience typedef for a pass manager over modules.
591 using ModulePassManager = PassManager<Module>;
592
593 extern template class PassManager<Function>;
594
595 /// Convenience typedef for a pass manager over functions.
596 using 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.
603 class PassInstrumentationAnalysis
604 : public AnalysisInfoMixin<PassInstrumentationAnalysis> {
605 friend AnalysisInfoMixin<PassInstrumentationAnalysis>;
606 static AnalysisKey Key;
607
608 PassInstrumentationCallbacks *Callbacks;
609
610 public:
611 /// PassInstrumentationCallbacks object is shared, owned by something else,
612 /// not this analysis.
613 PassInstrumentationAnalysis(PassInstrumentationCallbacks *Callbacks = nullptr)
Callbacks(Callbacks)614 : Callbacks(Callbacks) {}
615
616 using Result = PassInstrumentation;
617
618 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
run(IRUnitT &,AnalysisManagerT &,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.
629 template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager {
630 public:
631 class Invalidator;
632
633 private:
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
660 public:
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>
invalidate(IRUnitT & IR,const PreservedAnalyses & PA)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.
invalidate(AnalysisKey * ID,IRUnitT & IR,const PreservedAnalyses & PA)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>
invalidateImpl(AnalysisKey * ID,IRUnitT & IR,const PreservedAnalyses & PA)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
Invalidator(SmallDenseMap<AnalysisKey *,bool,8> & IsResultInvalidated,const AnalysisResultMapT & Results)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.
empty()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.
clear()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>
getResult(IRUnitT & IR,ExtraArgTs...ExtraArgs)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>
getCachedResult(IRUnitT & IR)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>
verifyNotInvalidated(IRUnitT & IR,typename PassT::Result * Result)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>
registerPass(PassBuilderT && PassBuilder)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 a specific analysis pass for an IR unit.
864 ///
865 /// Note that the analysis result can disregard invalidation, if it determines
866 /// it is in fact still valid.
invalidate(IRUnitT & IR)867 template <typename PassT> void invalidate(IRUnitT &IR) {
868 assert(AnalysisPasses.count(PassT::ID()) &&
869 "This analysis pass was not registered prior to being invalidated");
870 invalidateImpl(PassT::ID(), IR);
871 }
872
873 /// Invalidate cached analyses for an IR unit.
874 ///
875 /// Walk through all of the analyses pertaining to this unit of IR and
876 /// invalidate them, unless they are preserved by the PreservedAnalyses set.
877 void invalidate(IRUnitT &IR, const PreservedAnalyses &PA);
878
879 private:
880 /// Look up a registered analysis pass.
lookUpPass(AnalysisKey * ID)881 PassConceptT &lookUpPass(AnalysisKey *ID) {
882 typename AnalysisPassMapT::iterator PI = AnalysisPasses.find(ID);
883 assert(PI != AnalysisPasses.end() &&
884 "Analysis passes must be registered prior to being queried!");
885 return *PI->second;
886 }
887
888 /// Look up a registered analysis pass.
lookUpPass(AnalysisKey * ID)889 const PassConceptT &lookUpPass(AnalysisKey *ID) const {
890 typename AnalysisPassMapT::const_iterator PI = AnalysisPasses.find(ID);
891 assert(PI != AnalysisPasses.end() &&
892 "Analysis passes must be registered prior to being queried!");
893 return *PI->second;
894 }
895
896 /// Get an analysis result, running the pass if necessary.
897 ResultConceptT &getResultImpl(AnalysisKey *ID, IRUnitT &IR,
898 ExtraArgTs... ExtraArgs);
899
900 /// Get a cached analysis result or return null.
getCachedResultImpl(AnalysisKey * ID,IRUnitT & IR)901 ResultConceptT *getCachedResultImpl(AnalysisKey *ID, IRUnitT &IR) const {
902 typename AnalysisResultMapT::const_iterator RI =
903 AnalysisResults.find({ID, &IR});
904 return RI == AnalysisResults.end() ? nullptr : &*RI->second->second;
905 }
906
907 /// Invalidate a pass result for a IR unit.
invalidateImpl(AnalysisKey * ID,IRUnitT & IR)908 void invalidateImpl(AnalysisKey *ID, IRUnitT &IR) {
909 typename AnalysisResultMapT::iterator RI =
910 AnalysisResults.find({ID, &IR});
911 if (RI == AnalysisResults.end())
912 return;
913
914 if (DebugLogging)
915 dbgs() << "Invalidating analysis: " << this->lookUpPass(ID).name()
916 << " on " << IR.getName() << "\n";
917 AnalysisResultLists[&IR].erase(RI->second);
918 AnalysisResults.erase(RI);
919 }
920
921 /// Map type from analysis pass ID to pass concept pointer.
922 using AnalysisPassMapT =
923 DenseMap<AnalysisKey *, std::unique_ptr<PassConceptT>>;
924
925 /// Collection of analysis passes, indexed by ID.
926 AnalysisPassMapT AnalysisPasses;
927
928 /// Map from IR unit to a list of analysis results.
929 ///
930 /// Provides linear time removal of all analysis results for a IR unit and
931 /// the ultimate storage for a particular cached analysis result.
932 AnalysisResultListMapT AnalysisResultLists;
933
934 /// Map from an analysis ID and IR unit to a particular cached
935 /// analysis result.
936 AnalysisResultMapT AnalysisResults;
937
938 /// Indicates whether we log to \c llvm::dbgs().
939 bool DebugLogging;
940 };
941
942 extern template class AnalysisManager<Module>;
943
944 /// Convenience typedef for the Module analysis manager.
945 using ModuleAnalysisManager = AnalysisManager<Module>;
946
947 extern template class AnalysisManager<Function>;
948
949 /// Convenience typedef for the Function analysis manager.
950 using FunctionAnalysisManager = AnalysisManager<Function>;
951
952 /// An analysis over an "outer" IR unit that provides access to an
953 /// analysis manager over an "inner" IR unit. The inner unit must be contained
954 /// in the outer unit.
955 ///
956 /// For example, InnerAnalysisManagerProxy<FunctionAnalysisManager, Module> is
957 /// an analysis over Modules (the "outer" unit) that provides access to a
958 /// Function analysis manager. The FunctionAnalysisManager is the "inner"
959 /// manager being proxied, and Functions are the "inner" unit. The inner/outer
960 /// relationship is valid because each Function is contained in one Module.
961 ///
962 /// If you're (transitively) within a pass manager for an IR unit U that
963 /// contains IR unit V, you should never use an analysis manager over V, except
964 /// via one of these proxies.
965 ///
966 /// Note that the proxy's result is a move-only RAII object. The validity of
967 /// the analyses in the inner analysis manager is tied to its lifetime.
968 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
969 class InnerAnalysisManagerProxy
970 : public AnalysisInfoMixin<
971 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>> {
972 public:
973 class Result {
974 public:
Result(AnalysisManagerT & InnerAM)975 explicit Result(AnalysisManagerT &InnerAM) : InnerAM(&InnerAM) {}
976
Result(Result && Arg)977 Result(Result &&Arg) : InnerAM(std::move(Arg.InnerAM)) {
978 // We have to null out the analysis manager in the moved-from state
979 // because we are taking ownership of the responsibilty to clear the
980 // analysis state.
981 Arg.InnerAM = nullptr;
982 }
983
~Result()984 ~Result() {
985 // InnerAM is cleared in a moved from state where there is nothing to do.
986 if (!InnerAM)
987 return;
988
989 // Clear out the analysis manager if we're being destroyed -- it means we
990 // didn't even see an invalidate call when we got invalidated.
991 InnerAM->clear();
992 }
993
994 Result &operator=(Result &&RHS) {
995 InnerAM = RHS.InnerAM;
996 // We have to null out the analysis manager in the moved-from state
997 // because we are taking ownership of the responsibilty to clear the
998 // analysis state.
999 RHS.InnerAM = nullptr;
1000 return *this;
1001 }
1002
1003 /// Accessor for the analysis manager.
getManager()1004 AnalysisManagerT &getManager() { return *InnerAM; }
1005
1006 /// Handler for invalidation of the outer IR unit, \c IRUnitT.
1007 ///
1008 /// If the proxy analysis itself is not preserved, we assume that the set of
1009 /// inner IR objects contained in IRUnit may have changed. In this case,
1010 /// we have to call \c clear() on the inner analysis manager, as it may now
1011 /// have stale pointers to its inner IR objects.
1012 ///
1013 /// Regardless of whether the proxy analysis is marked as preserved, all of
1014 /// the analyses in the inner analysis manager are potentially invalidated
1015 /// based on the set of preserved analyses.
1016 bool invalidate(
1017 IRUnitT &IR, const PreservedAnalyses &PA,
1018 typename AnalysisManager<IRUnitT, ExtraArgTs...>::Invalidator &Inv);
1019
1020 private:
1021 AnalysisManagerT *InnerAM;
1022 };
1023
InnerAnalysisManagerProxy(AnalysisManagerT & InnerAM)1024 explicit InnerAnalysisManagerProxy(AnalysisManagerT &InnerAM)
1025 : InnerAM(&InnerAM) {}
1026
1027 /// Run the analysis pass and create our proxy result object.
1028 ///
1029 /// This doesn't do any interesting work; it is primarily used to insert our
1030 /// proxy result object into the outer analysis cache so that we can proxy
1031 /// invalidation to the inner analysis manager.
run(IRUnitT & IR,AnalysisManager<IRUnitT,ExtraArgTs...> & AM,ExtraArgTs...)1032 Result run(IRUnitT &IR, AnalysisManager<IRUnitT, ExtraArgTs...> &AM,
1033 ExtraArgTs...) {
1034 return Result(*InnerAM);
1035 }
1036
1037 private:
1038 friend AnalysisInfoMixin<
1039 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>>;
1040
1041 static AnalysisKey Key;
1042
1043 AnalysisManagerT *InnerAM;
1044 };
1045
1046 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1047 AnalysisKey
1048 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
1049
1050 /// Provide the \c FunctionAnalysisManager to \c Module proxy.
1051 using FunctionAnalysisManagerModuleProxy =
1052 InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>;
1053
1054 /// Specialization of the invalidate method for the \c
1055 /// FunctionAnalysisManagerModuleProxy's result.
1056 template <>
1057 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
1058 Module &M, const PreservedAnalyses &PA,
1059 ModuleAnalysisManager::Invalidator &Inv);
1060
1061 // Ensure the \c FunctionAnalysisManagerModuleProxy is provided as an extern
1062 // template.
1063 extern template class InnerAnalysisManagerProxy<FunctionAnalysisManager,
1064 Module>;
1065
1066 /// An analysis over an "inner" IR unit that provides access to an
1067 /// analysis manager over a "outer" IR unit. The inner unit must be contained
1068 /// in the outer unit.
1069 ///
1070 /// For example OuterAnalysisManagerProxy<ModuleAnalysisManager, Function> is an
1071 /// analysis over Functions (the "inner" unit) which provides access to a Module
1072 /// analysis manager. The ModuleAnalysisManager is the "outer" manager being
1073 /// proxied, and Modules are the "outer" IR unit. The inner/outer relationship
1074 /// is valid because each Function is contained in one Module.
1075 ///
1076 /// This proxy only exposes the const interface of the outer analysis manager,
1077 /// to indicate that you cannot cause an outer analysis to run from within an
1078 /// inner pass. Instead, you must rely on the \c getCachedResult API. This is
1079 /// due to keeping potential future concurrency in mind. To give an example,
1080 /// running a module analysis before any function passes may give a different
1081 /// result than running it in a function pass. Both may be valid, but it would
1082 /// produce non-deterministic results. GlobalsAA is a good analysis example,
1083 /// because the cached information has the mod/ref info for all memory for each
1084 /// function at the time the analysis was computed. The information is still
1085 /// valid after a function transformation, but it may be *different* if
1086 /// recomputed after that transform. GlobalsAA is never invalidated.
1087
1088 ///
1089 /// This proxy doesn't manage invalidation in any way -- that is handled by the
1090 /// recursive return path of each layer of the pass manager. A consequence of
1091 /// this is the outer analyses may be stale. We invalidate the outer analyses
1092 /// only when we're done running passes over the inner IR units.
1093 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1094 class OuterAnalysisManagerProxy
1095 : public AnalysisInfoMixin<
1096 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>> {
1097 public:
1098 /// Result proxy object for \c OuterAnalysisManagerProxy.
1099 class Result {
1100 public:
Result(const AnalysisManagerT & OuterAM)1101 explicit Result(const AnalysisManagerT &OuterAM) : OuterAM(&OuterAM) {}
1102
1103 /// Get a cached analysis. If the analysis can be invalidated, this will
1104 /// assert.
1105 template <typename PassT, typename IRUnitTParam>
getCachedResult(IRUnitTParam & IR)1106 typename PassT::Result *getCachedResult(IRUnitTParam &IR) const {
1107 typename PassT::Result *Res =
1108 OuterAM->template getCachedResult<PassT>(IR);
1109 if (Res)
1110 OuterAM->template verifyNotInvalidated<PassT>(IR, Res);
1111 return Res;
1112 }
1113
1114 /// Method provided for unit testing, not intended for general use.
1115 template <typename PassT, typename IRUnitTParam>
cachedResultExists(IRUnitTParam & IR)1116 bool cachedResultExists(IRUnitTParam &IR) const {
1117 typename PassT::Result *Res =
1118 OuterAM->template getCachedResult<PassT>(IR);
1119 return Res != nullptr;
1120 }
1121
1122 /// When invalidation occurs, remove any registered invalidation events.
invalidate(IRUnitT & IRUnit,const PreservedAnalyses & PA,typename AnalysisManager<IRUnitT,ExtraArgTs...>::Invalidator & Inv)1123 bool invalidate(
1124 IRUnitT &IRUnit, const PreservedAnalyses &PA,
1125 typename AnalysisManager<IRUnitT, ExtraArgTs...>::Invalidator &Inv) {
1126 // Loop over the set of registered outer invalidation mappings and if any
1127 // of them map to an analysis that is now invalid, clear it out.
1128 SmallVector<AnalysisKey *, 4> DeadKeys;
1129 for (auto &KeyValuePair : OuterAnalysisInvalidationMap) {
1130 AnalysisKey *OuterID = KeyValuePair.first;
1131 auto &InnerIDs = KeyValuePair.second;
1132 InnerIDs.erase(llvm::remove_if(InnerIDs, [&](AnalysisKey *InnerID) {
1133 return Inv.invalidate(InnerID, IRUnit, PA); }),
1134 InnerIDs.end());
1135 if (InnerIDs.empty())
1136 DeadKeys.push_back(OuterID);
1137 }
1138
1139 for (auto OuterID : DeadKeys)
1140 OuterAnalysisInvalidationMap.erase(OuterID);
1141
1142 // The proxy itself remains valid regardless of anything else.
1143 return false;
1144 }
1145
1146 /// Register a deferred invalidation event for when the outer analysis
1147 /// manager processes its invalidations.
1148 template <typename OuterAnalysisT, typename InvalidatedAnalysisT>
registerOuterAnalysisInvalidation()1149 void registerOuterAnalysisInvalidation() {
1150 AnalysisKey *OuterID = OuterAnalysisT::ID();
1151 AnalysisKey *InvalidatedID = InvalidatedAnalysisT::ID();
1152
1153 auto &InvalidatedIDList = OuterAnalysisInvalidationMap[OuterID];
1154 // Note, this is a linear scan. If we end up with large numbers of
1155 // analyses that all trigger invalidation on the same outer analysis,
1156 // this entire system should be changed to some other deterministic
1157 // data structure such as a `SetVector` of a pair of pointers.
1158 if (!llvm::is_contained(InvalidatedIDList, InvalidatedID))
1159 InvalidatedIDList.push_back(InvalidatedID);
1160 }
1161
1162 /// Access the map from outer analyses to deferred invalidation requiring
1163 /// analyses.
1164 const SmallDenseMap<AnalysisKey *, TinyPtrVector<AnalysisKey *>, 2> &
getOuterInvalidations()1165 getOuterInvalidations() const {
1166 return OuterAnalysisInvalidationMap;
1167 }
1168
1169 private:
1170 const AnalysisManagerT *OuterAM;
1171
1172 /// A map from an outer analysis ID to the set of this IR-unit's analyses
1173 /// which need to be invalidated.
1174 SmallDenseMap<AnalysisKey *, TinyPtrVector<AnalysisKey *>, 2>
1175 OuterAnalysisInvalidationMap;
1176 };
1177
OuterAnalysisManagerProxy(const AnalysisManagerT & OuterAM)1178 OuterAnalysisManagerProxy(const AnalysisManagerT &OuterAM)
1179 : OuterAM(&OuterAM) {}
1180
1181 /// Run the analysis pass and create our proxy result object.
1182 /// Nothing to see here, it just forwards the \c OuterAM reference into the
1183 /// result.
run(IRUnitT &,AnalysisManager<IRUnitT,ExtraArgTs...> &,ExtraArgTs...)1184 Result run(IRUnitT &, AnalysisManager<IRUnitT, ExtraArgTs...> &,
1185 ExtraArgTs...) {
1186 return Result(*OuterAM);
1187 }
1188
1189 private:
1190 friend AnalysisInfoMixin<
1191 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>>;
1192
1193 static AnalysisKey Key;
1194
1195 const AnalysisManagerT *OuterAM;
1196 };
1197
1198 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1199 AnalysisKey
1200 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
1201
1202 extern template class OuterAnalysisManagerProxy<ModuleAnalysisManager,
1203 Function>;
1204 /// Provide the \c ModuleAnalysisManager to \c Function proxy.
1205 using ModuleAnalysisManagerFunctionProxy =
1206 OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>;
1207
1208 /// Trivial adaptor that maps from a module to its functions.
1209 ///
1210 /// Designed to allow composition of a FunctionPass(Manager) and
1211 /// a ModulePassManager, by running the FunctionPass(Manager) over every
1212 /// function in the module.
1213 ///
1214 /// Function passes run within this adaptor can rely on having exclusive access
1215 /// to the function they are run over. They should not read or modify any other
1216 /// functions! Other threads or systems may be manipulating other functions in
1217 /// the module, and so their state should never be relied on.
1218 /// FIXME: Make the above true for all of LLVM's actual passes, some still
1219 /// violate this principle.
1220 ///
1221 /// Function passes can also read the module containing the function, but they
1222 /// should not modify that module outside of the use lists of various globals.
1223 /// For example, a function pass is not permitted to add functions to the
1224 /// module.
1225 /// FIXME: Make the above true for all of LLVM's actual passes, some still
1226 /// violate this principle.
1227 ///
1228 /// Note that although function passes can access module analyses, module
1229 /// analyses are not invalidated while the function passes are running, so they
1230 /// may be stale. Function analyses will not be stale.
1231 class ModuleToFunctionPassAdaptor
1232 : public PassInfoMixin<ModuleToFunctionPassAdaptor> {
1233 public:
1234 using PassConceptT = detail::PassConcept<Function, FunctionAnalysisManager>;
1235
ModuleToFunctionPassAdaptor(std::unique_ptr<PassConceptT> Pass)1236 explicit ModuleToFunctionPassAdaptor(std::unique_ptr<PassConceptT> Pass)
1237 : Pass(std::move(Pass)) {}
1238
1239 /// Runs the function pass across every function in the module.
1240 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
1241
isRequired()1242 static bool isRequired() { return true; }
1243
1244 private:
1245 std::unique_ptr<PassConceptT> Pass;
1246 };
1247
1248 /// A function to deduce a function pass type and wrap it in the
1249 /// templated adaptor.
1250 template <typename FunctionPassT>
1251 ModuleToFunctionPassAdaptor
createModuleToFunctionPassAdaptor(FunctionPassT Pass)1252 createModuleToFunctionPassAdaptor(FunctionPassT Pass) {
1253 using PassModelT =
1254 detail::PassModel<Function, FunctionPassT, PreservedAnalyses,
1255 FunctionAnalysisManager>;
1256
1257 return ModuleToFunctionPassAdaptor(
1258 std::make_unique<PassModelT>(std::move(Pass)));
1259 }
1260
1261 /// A utility pass template to force an analysis result to be available.
1262 ///
1263 /// If there are extra arguments at the pass's run level there may also be
1264 /// extra arguments to the analysis manager's \c getResult routine. We can't
1265 /// guess how to effectively map the arguments from one to the other, and so
1266 /// this specialization just ignores them.
1267 ///
1268 /// Specific patterns of run-method extra arguments and analysis manager extra
1269 /// arguments will have to be defined as appropriate specializations.
1270 template <typename AnalysisT, typename IRUnitT,
1271 typename AnalysisManagerT = AnalysisManager<IRUnitT>,
1272 typename... ExtraArgTs>
1273 struct RequireAnalysisPass
1274 : PassInfoMixin<RequireAnalysisPass<AnalysisT, IRUnitT, AnalysisManagerT,
1275 ExtraArgTs...>> {
1276 /// Run this pass over some unit of IR.
1277 ///
1278 /// This pass can be run over any unit of IR and use any analysis manager
1279 /// provided they satisfy the basic API requirements. When this pass is
1280 /// created, these methods can be instantiated to satisfy whatever the
1281 /// context requires.
runRequireAnalysisPass1282 PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM,
1283 ExtraArgTs &&... Args) {
1284 (void)AM.template getResult<AnalysisT>(Arg,
1285 std::forward<ExtraArgTs>(Args)...);
1286
1287 return PreservedAnalyses::all();
1288 }
isRequiredRequireAnalysisPass1289 static bool isRequired() { return true; }
1290 };
1291
1292 /// A no-op pass template which simply forces a specific analysis result
1293 /// to be invalidated.
1294 template <typename AnalysisT>
1295 struct InvalidateAnalysisPass
1296 : PassInfoMixin<InvalidateAnalysisPass<AnalysisT>> {
1297 /// Run this pass over some unit of IR.
1298 ///
1299 /// This pass can be run over any unit of IR and use any analysis manager,
1300 /// provided they satisfy the basic API requirements. When this pass is
1301 /// created, these methods can be instantiated to satisfy whatever the
1302 /// context requires.
1303 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
runInvalidateAnalysisPass1304 PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&...) {
1305 auto PA = PreservedAnalyses::all();
1306 PA.abandon<AnalysisT>();
1307 return PA;
1308 }
1309 };
1310
1311 /// A utility pass that does nothing, but preserves no analyses.
1312 ///
1313 /// Because this preserves no analyses, any analysis passes queried after this
1314 /// pass runs will recompute fresh results.
1315 struct InvalidateAllAnalysesPass : PassInfoMixin<InvalidateAllAnalysesPass> {
1316 /// Run this pass over some unit of IR.
1317 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
runInvalidateAllAnalysesPass1318 PreservedAnalyses run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) {
1319 return PreservedAnalyses::none();
1320 }
1321 };
1322
1323 /// A utility pass template that simply runs another pass multiple times.
1324 ///
1325 /// This can be useful when debugging or testing passes. It also serves as an
1326 /// example of how to extend the pass manager in ways beyond composition.
1327 template <typename PassT>
1328 class RepeatedPass : public PassInfoMixin<RepeatedPass<PassT>> {
1329 public:
RepeatedPass(int Count,PassT P)1330 RepeatedPass(int Count, PassT P) : Count(Count), P(std::move(P)) {}
1331
1332 template <typename IRUnitT, typename AnalysisManagerT, typename... Ts>
run(IRUnitT & IR,AnalysisManagerT & AM,Ts &&...Args)1333 PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, Ts &&... Args) {
1334
1335 // Request PassInstrumentation from analysis manager, will use it to run
1336 // instrumenting callbacks for the passes later.
1337 // Here we use std::tuple wrapper over getResult which helps to extract
1338 // AnalysisManager's arguments out of the whole Args set.
1339 PassInstrumentation PI =
1340 detail::getAnalysisResult<PassInstrumentationAnalysis>(
1341 AM, IR, std::tuple<Ts...>(Args...));
1342
1343 auto PA = PreservedAnalyses::all();
1344 for (int i = 0; i < Count; ++i) {
1345 // Check the PassInstrumentation's BeforePass callbacks before running the
1346 // pass, skip its execution completely if asked to (callback returns
1347 // false).
1348 if (!PI.runBeforePass<IRUnitT>(P, IR))
1349 continue;
1350 PreservedAnalyses IterPA = P.run(IR, AM, std::forward<Ts>(Args)...);
1351 PA.intersect(IterPA);
1352 PI.runAfterPass(P, IR, IterPA);
1353 }
1354 return PA;
1355 }
1356
1357 private:
1358 int Count;
1359 PassT P;
1360 };
1361
1362 template <typename PassT>
createRepeatedPass(int Count,PassT P)1363 RepeatedPass<PassT> createRepeatedPass(int Count, PassT P) {
1364 return RepeatedPass<PassT>(Count, std::move(P));
1365 }
1366
1367 } // end namespace llvm
1368
1369 #endif // LLVM_IR_PASSMANAGER_H
1370