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