blob: c669565aa33b061851ec1528d8dbb4c729ed1ed5 [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"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020041#include "llvm/ADT/STLExtras.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042#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"
Andrew Scull0372a572018-11-16 15:47:06 +000047#include "llvm/IR/PassInstrumentation.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048#include "llvm/IR/PassManagerInternal.h"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020049#include "llvm/Pass.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050#include "llvm/Support/Debug.h"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020051#include "llvm/Support/TimeProfiler.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010052#include "llvm/Support/TypeName.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010053#include <algorithm>
54#include <cassert>
55#include <cstring>
56#include <iterator>
57#include <list>
58#include <memory>
59#include <tuple>
60#include <type_traits>
61#include <utility>
62#include <vector>
63
64namespace llvm {
65
66/// A special type used by analysis passes to provide an address that
67/// identifies that particular analysis pass type.
68///
69/// Analysis passes should have a static data member of this type and derive
70/// from the \c AnalysisInfoMixin to get a static ID method used to identify
71/// the analysis in the pass management infrastructure.
72struct alignas(8) AnalysisKey {};
73
74/// A special type used to provide an address that identifies a set of related
75/// analyses. These sets are primarily used below to mark sets of analyses as
76/// preserved.
77///
78/// For example, a transformation can indicate that it preserves the CFG of a
79/// function by preserving the appropriate AnalysisSetKey. An analysis that
80/// depends only on the CFG can then check if that AnalysisSetKey is preserved;
81/// if it is, the analysis knows that it itself is preserved.
82struct alignas(8) AnalysisSetKey {};
83
84/// This templated class represents "all analyses that operate over \<a
85/// particular IR unit\>" (e.g. a Function or a Module) in instances of
86/// PreservedAnalysis.
87///
88/// This lets a transformation say e.g. "I preserved all function analyses".
89///
90/// Note that you must provide an explicit instantiation declaration and
91/// definition for this template in order to get the correct behavior on
92/// Windows. Otherwise, the address of SetKey will not be stable.
93template <typename IRUnitT> class AllAnalysesOn {
94public:
95 static AnalysisSetKey *ID() { return &SetKey; }
96
97private:
98 static AnalysisSetKey SetKey;
99};
100
101template <typename IRUnitT> AnalysisSetKey AllAnalysesOn<IRUnitT>::SetKey;
102
103extern template class AllAnalysesOn<Module>;
104extern template class AllAnalysesOn<Function>;
105
106/// Represents analyses that only rely on functions' control flow.
107///
108/// This can be used with \c PreservedAnalyses to mark the CFG as preserved and
109/// to query whether it has been preserved.
110///
111/// The CFG of a function is defined as the set of basic blocks and the edges
112/// between them. Changing the set of basic blocks in a function is enough to
113/// mutate the CFG. Mutating the condition of a branch or argument of an
114/// invoked function does not mutate the CFG, but changing the successor labels
115/// of those instructions does.
116class CFGAnalyses {
117public:
118 static AnalysisSetKey *ID() { return &SetKey; }
119
120private:
121 static AnalysisSetKey SetKey;
122};
123
124/// A set of analyses that are preserved following a run of a transformation
125/// pass.
126///
127/// Transformation passes build and return these objects to communicate which
128/// analyses are still valid after the transformation. For most passes this is
129/// fairly simple: if they don't change anything all analyses are preserved,
130/// otherwise only a short list of analyses that have been explicitly updated
131/// are preserved.
132///
133/// This class also lets transformation passes mark abstract *sets* of analyses
134/// as preserved. A transformation that (say) does not alter the CFG can
135/// indicate such by marking a particular AnalysisSetKey as preserved, and
136/// then analyses can query whether that AnalysisSetKey is preserved.
137///
138/// Finally, this class can represent an "abandoned" analysis, which is
139/// not preserved even if it would be covered by some abstract set of analyses.
140///
141/// Given a `PreservedAnalyses` object, an analysis will typically want to
142/// figure out whether it is preserved. In the example below, MyAnalysisType is
143/// preserved if it's not abandoned, and (a) it's explicitly marked as
144/// preserved, (b), the set AllAnalysesOn<MyIRUnit> is preserved, or (c) both
145/// AnalysisSetA and AnalysisSetB are preserved.
146///
147/// ```
148/// auto PAC = PA.getChecker<MyAnalysisType>();
149/// if (PAC.preserved() || PAC.preservedSet<AllAnalysesOn<MyIRUnit>>() ||
150/// (PAC.preservedSet<AnalysisSetA>() &&
151/// PAC.preservedSet<AnalysisSetB>())) {
152/// // The analysis has been successfully preserved ...
153/// }
154/// ```
155class PreservedAnalyses {
156public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100157 /// Convenience factory function for the empty preserved set.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100158 static PreservedAnalyses none() { return PreservedAnalyses(); }
159
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100160 /// Construct a special preserved set that preserves all passes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100161 static PreservedAnalyses all() {
162 PreservedAnalyses PA;
163 PA.PreservedIDs.insert(&AllAnalysesKey);
164 return PA;
165 }
166
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100167 /// Construct a preserved analyses object with a single preserved set.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100168 template <typename AnalysisSetT>
169 static PreservedAnalyses allInSet() {
170 PreservedAnalyses PA;
171 PA.preserveSet<AnalysisSetT>();
172 return PA;
173 }
174
175 /// Mark an analysis as preserved.
176 template <typename AnalysisT> void preserve() { preserve(AnalysisT::ID()); }
177
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100178 /// Given an analysis's ID, mark the analysis as preserved, adding it
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100179 /// to the set.
180 void preserve(AnalysisKey *ID) {
181 // Clear this ID from the explicit not-preserved set if present.
182 NotPreservedAnalysisIDs.erase(ID);
183
184 // If we're not already preserving all analyses (other than those in
185 // NotPreservedAnalysisIDs).
186 if (!areAllPreserved())
187 PreservedIDs.insert(ID);
188 }
189
190 /// Mark an analysis set as preserved.
191 template <typename AnalysisSetT> void preserveSet() {
192 preserveSet(AnalysisSetT::ID());
193 }
194
195 /// Mark an analysis set as preserved using its ID.
196 void preserveSet(AnalysisSetKey *ID) {
197 // If we're not already in the saturated 'all' state, add this set.
198 if (!areAllPreserved())
199 PreservedIDs.insert(ID);
200 }
201
202 /// Mark an analysis as abandoned.
203 ///
204 /// An abandoned analysis is not preserved, even if it is nominally covered
205 /// by some other set or was previously explicitly marked as preserved.
206 ///
207 /// Note that you can only abandon a specific analysis, not a *set* of
208 /// analyses.
209 template <typename AnalysisT> void abandon() { abandon(AnalysisT::ID()); }
210
211 /// Mark an analysis as abandoned using its ID.
212 ///
213 /// An abandoned analysis is not preserved, even if it is nominally covered
214 /// by some other set or was previously explicitly marked as preserved.
215 ///
216 /// Note that you can only abandon a specific analysis, not a *set* of
217 /// analyses.
218 void abandon(AnalysisKey *ID) {
219 PreservedIDs.erase(ID);
220 NotPreservedAnalysisIDs.insert(ID);
221 }
222
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100223 /// Intersect this set with another in place.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100224 ///
225 /// This is a mutating operation on this preserved set, removing all
226 /// preserved passes which are not also preserved in the argument.
227 void intersect(const PreservedAnalyses &Arg) {
228 if (Arg.areAllPreserved())
229 return;
230 if (areAllPreserved()) {
231 *this = Arg;
232 return;
233 }
234 // The intersection requires the *union* of the explicitly not-preserved
235 // IDs and the *intersection* of the preserved IDs.
236 for (auto ID : Arg.NotPreservedAnalysisIDs) {
237 PreservedIDs.erase(ID);
238 NotPreservedAnalysisIDs.insert(ID);
239 }
240 for (auto ID : PreservedIDs)
241 if (!Arg.PreservedIDs.count(ID))
242 PreservedIDs.erase(ID);
243 }
244
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100245 /// Intersect this set with a temporary other set in place.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100246 ///
247 /// This is a mutating operation on this preserved set, removing all
248 /// preserved passes which are not also preserved in the argument.
249 void intersect(PreservedAnalyses &&Arg) {
250 if (Arg.areAllPreserved())
251 return;
252 if (areAllPreserved()) {
253 *this = std::move(Arg);
254 return;
255 }
256 // The intersection requires the *union* of the explicitly not-preserved
257 // IDs and the *intersection* of the preserved IDs.
258 for (auto ID : Arg.NotPreservedAnalysisIDs) {
259 PreservedIDs.erase(ID);
260 NotPreservedAnalysisIDs.insert(ID);
261 }
262 for (auto ID : PreservedIDs)
263 if (!Arg.PreservedIDs.count(ID))
264 PreservedIDs.erase(ID);
265 }
266
267 /// A checker object that makes it easy to query for whether an analysis or
268 /// some set covering it is preserved.
269 class PreservedAnalysisChecker {
270 friend class PreservedAnalyses;
271
272 const PreservedAnalyses &PA;
273 AnalysisKey *const ID;
274 const bool IsAbandoned;
275
276 /// A PreservedAnalysisChecker is tied to a particular Analysis because
277 /// `preserved()` and `preservedSet()` both return false if the Analysis
278 /// was abandoned.
279 PreservedAnalysisChecker(const PreservedAnalyses &PA, AnalysisKey *ID)
280 : PA(PA), ID(ID), IsAbandoned(PA.NotPreservedAnalysisIDs.count(ID)) {}
281
282 public:
283 /// Returns true if the checker's analysis was not abandoned and either
284 /// - the analysis is explicitly preserved or
285 /// - all analyses are preserved.
286 bool preserved() {
287 return !IsAbandoned && (PA.PreservedIDs.count(&AllAnalysesKey) ||
288 PA.PreservedIDs.count(ID));
289 }
290
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100291 /// Return true if the checker's analysis was not abandoned, i.e. it was not
292 /// explicitly invalidated. Even if the analysis is not explicitly
293 /// preserved, if the analysis is known stateless, then it is preserved.
294 bool preservedWhenStateless() {
295 return !IsAbandoned;
296 }
297
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100298 /// Returns true if the checker's analysis was not abandoned and either
299 /// - \p AnalysisSetT is explicitly preserved or
300 /// - all analyses are preserved.
301 template <typename AnalysisSetT> bool preservedSet() {
302 AnalysisSetKey *SetID = AnalysisSetT::ID();
303 return !IsAbandoned && (PA.PreservedIDs.count(&AllAnalysesKey) ||
304 PA.PreservedIDs.count(SetID));
305 }
306 };
307
308 /// Build a checker for this `PreservedAnalyses` and the specified analysis
309 /// type.
310 ///
311 /// You can use the returned object to query whether an analysis was
312 /// preserved. See the example in the comment on `PreservedAnalysis`.
313 template <typename AnalysisT> PreservedAnalysisChecker getChecker() const {
314 return PreservedAnalysisChecker(*this, AnalysisT::ID());
315 }
316
317 /// Build a checker for this `PreservedAnalyses` and the specified analysis
318 /// ID.
319 ///
320 /// You can use the returned object to query whether an analysis was
321 /// preserved. See the example in the comment on `PreservedAnalysis`.
322 PreservedAnalysisChecker getChecker(AnalysisKey *ID) const {
323 return PreservedAnalysisChecker(*this, ID);
324 }
325
326 /// Test whether all analyses are preserved (and none are abandoned).
327 ///
328 /// This is used primarily to optimize for the common case of a transformation
329 /// which makes no changes to the IR.
330 bool areAllPreserved() const {
331 return NotPreservedAnalysisIDs.empty() &&
332 PreservedIDs.count(&AllAnalysesKey);
333 }
334
335 /// Directly test whether a set of analyses is preserved.
336 ///
337 /// This is only true when no analyses have been explicitly abandoned.
338 template <typename AnalysisSetT> bool allAnalysesInSetPreserved() const {
339 return allAnalysesInSetPreserved(AnalysisSetT::ID());
340 }
341
342 /// Directly test whether a set of analyses is preserved.
343 ///
344 /// This is only true when no analyses have been explicitly abandoned.
345 bool allAnalysesInSetPreserved(AnalysisSetKey *SetID) const {
346 return NotPreservedAnalysisIDs.empty() &&
347 (PreservedIDs.count(&AllAnalysesKey) || PreservedIDs.count(SetID));
348 }
349
350private:
351 /// A special key used to indicate all analyses.
352 static AnalysisSetKey AllAnalysesKey;
353
354 /// The IDs of analyses and analysis sets that are preserved.
355 SmallPtrSet<void *, 2> PreservedIDs;
356
357 /// The IDs of explicitly not-preserved analyses.
358 ///
359 /// If an analysis in this set is covered by a set in `PreservedIDs`, we
360 /// consider it not-preserved. That is, `NotPreservedAnalysisIDs` always
361 /// "wins" over analysis sets in `PreservedIDs`.
362 ///
363 /// Also, a given ID should never occur both here and in `PreservedIDs`.
364 SmallPtrSet<AnalysisKey *, 2> NotPreservedAnalysisIDs;
365};
366
367// Forward declare the analysis manager template.
368template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
369
370/// A CRTP mix-in to automatically provide informational APIs needed for
371/// passes.
372///
373/// This provides some boilerplate for types that are passes.
374template <typename DerivedT> struct PassInfoMixin {
375 /// Gets the name of the pass we are mixed into.
376 static StringRef name() {
377 static_assert(std::is_base_of<PassInfoMixin, DerivedT>::value,
378 "Must pass the derived type as the template argument!");
379 StringRef Name = getTypeName<DerivedT>();
380 if (Name.startswith("llvm::"))
381 Name = Name.drop_front(strlen("llvm::"));
382 return Name;
383 }
384};
385
386/// A CRTP mix-in that provides informational APIs needed for analysis passes.
387///
388/// This provides some boilerplate for types that are analysis passes. It
389/// automatically mixes in \c PassInfoMixin.
390template <typename DerivedT>
391struct AnalysisInfoMixin : PassInfoMixin<DerivedT> {
392 /// Returns an opaque, unique ID for this analysis type.
393 ///
394 /// This ID is a pointer type that is guaranteed to be 8-byte aligned and thus
395 /// suitable for use in sets, maps, and other data structures that use the low
396 /// bits of pointers.
397 ///
398 /// Note that this requires the derived type provide a static \c AnalysisKey
399 /// member called \c Key.
400 ///
401 /// FIXME: The only reason the mixin type itself can't declare the Key value
402 /// is that some compilers cannot correctly unique a templated static variable
403 /// so it has the same addresses in each instantiation. The only currently
404 /// known platform with this limitation is Windows DLL builds, specifically
405 /// building each part of LLVM as a DLL. If we ever remove that build
406 /// configuration, this mixin can provide the static key as well.
407 static AnalysisKey *ID() {
408 static_assert(std::is_base_of<AnalysisInfoMixin, DerivedT>::value,
409 "Must pass the derived type as the template argument!");
410 return &DerivedT::Key;
411 }
412};
413
Andrew Scull0372a572018-11-16 15:47:06 +0000414namespace detail {
415
416/// Actual unpacker of extra arguments in getAnalysisResult,
417/// passes only those tuple arguments that are mentioned in index_sequence.
418template <typename PassT, typename IRUnitT, typename AnalysisManagerT,
419 typename... ArgTs, size_t... Ns>
420typename PassT::Result
421getAnalysisResultUnpackTuple(AnalysisManagerT &AM, IRUnitT &IR,
422 std::tuple<ArgTs...> Args,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200423 std::index_sequence<Ns...>) {
Andrew Scull0372a572018-11-16 15:47:06 +0000424 (void)Args;
425 return AM.template getResult<PassT>(IR, std::get<Ns>(Args)...);
426}
427
428/// Helper for *partial* unpacking of extra arguments in getAnalysisResult.
429///
430/// Arguments passed in tuple come from PassManager, so they might have extra
431/// arguments after those AnalysisManager's ExtraArgTs ones that we need to
432/// pass to getResult.
433template <typename PassT, typename IRUnitT, typename... AnalysisArgTs,
434 typename... MainArgTs>
435typename PassT::Result
436getAnalysisResult(AnalysisManager<IRUnitT, AnalysisArgTs...> &AM, IRUnitT &IR,
437 std::tuple<MainArgTs...> Args) {
438 return (getAnalysisResultUnpackTuple<
439 PassT, IRUnitT>)(AM, IR, Args,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200440 std::index_sequence_for<AnalysisArgTs...>{});
Andrew Scull0372a572018-11-16 15:47:06 +0000441}
442
443} // namespace detail
444
445// Forward declare the pass instrumentation analysis explicitly queried in
446// generic PassManager code.
447// FIXME: figure out a way to move PassInstrumentationAnalysis into its own
448// header.
449class PassInstrumentationAnalysis;
450
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100451/// Manages a sequence of passes over a particular unit of IR.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100452///
453/// A pass manager contains a sequence of passes to run over a particular unit
454/// of IR (e.g. Functions, Modules). It is itself a valid pass over that unit of
455/// IR, and when run over some given IR will run each of its contained passes in
456/// sequence. Pass managers are the primary and most basic building block of a
457/// pass pipeline.
458///
459/// When you run a pass manager, you provide an \c AnalysisManager<IRUnitT>
460/// argument. The pass manager will propagate that analysis manager to each
461/// pass it runs, and will call the analysis manager's invalidation routine with
462/// the PreservedAnalyses of each pass it runs.
463template <typename IRUnitT,
464 typename AnalysisManagerT = AnalysisManager<IRUnitT>,
465 typename... ExtraArgTs>
466class PassManager : public PassInfoMixin<
467 PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...>> {
468public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100469 /// Construct a pass manager.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100470 ///
471 /// If \p DebugLogging is true, we'll log our progress to llvm::dbgs().
472 explicit PassManager(bool DebugLogging = false) : DebugLogging(DebugLogging) {}
473
474 // FIXME: These are equivalent to the default move constructor/move
475 // assignment. However, using = default triggers linker errors due to the
476 // explicit instantiations below. Find away to use the default and remove the
477 // duplicated code here.
478 PassManager(PassManager &&Arg)
479 : Passes(std::move(Arg.Passes)),
480 DebugLogging(std::move(Arg.DebugLogging)) {}
481
482 PassManager &operator=(PassManager &&RHS) {
483 Passes = std::move(RHS.Passes);
484 DebugLogging = std::move(RHS.DebugLogging);
485 return *this;
486 }
487
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100488 /// Run all of the passes in this manager over the given unit of IR.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100489 /// ExtraArgs are passed to each pass.
490 PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM,
491 ExtraArgTs... ExtraArgs) {
492 PreservedAnalyses PA = PreservedAnalyses::all();
493
Andrew Scull0372a572018-11-16 15:47:06 +0000494 // 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100502 if (DebugLogging)
503 dbgs() << "Starting " << getTypeName<IRUnitT>() << " pass manager run.\n";
504
505 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
Andrew Scull0372a572018-11-16 15:47:06 +0000506 auto *P = Passes[Idx].get();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100507
Andrew Scull0372a572018-11-16 15:47:06 +0000508 // 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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200514 PreservedAnalyses PassPA;
515 {
516 TimeTraceScope TimeScope(P->name(), IR.getName());
517 PassPA = P->run(IR, AM, ExtraArgs...);
518 }
Andrew Scull0372a572018-11-16 15:47:06 +0000519
520 // Call onto PassInstrumentation's AfterPass callbacks immediately after
521 // running the pass.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200522 PI.runAfterPass<IRUnitT>(*P, IR, PassPA);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100523
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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200551 template <typename PassT>
552 std::enable_if_t<!std::is_same<PassT, PassManager>::value>
553 addPass(PassT Pass) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100554 using PassModelT =
555 detail::PassModel<IRUnitT, PassT, PreservedAnalyses, AnalysisManagerT,
556 ExtraArgTs...>;
557
558 Passes.emplace_back(new PassModelT(std::move(Pass)));
559 }
560
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200561 /// When adding a pass manager pass that has the same type as this pass
562 /// manager, simply move the passes over. This is because we don't have use
563 /// cases rely on executing nested pass managers. Doing this could reduce
564 /// implementation complexity and avoid potential invalidation issues that may
565 /// happen with nested pass managers of the same type.
566 template <typename PassT>
567 std::enable_if_t<std::is_same<PassT, PassManager>::value>
568 addPass(PassT &&Pass) {
569 for (auto &P : Pass.Passes)
570 Passes.emplace_back(std::move(P));
571 }
572
573 /// Returns if the pass manager contains any passes.
574 bool isEmpty() const { return Passes.empty(); }
575
576 static bool isRequired() { return true; }
577
578protected:
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100579 using PassConceptT =
580 detail::PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...>;
581
582 std::vector<std::unique_ptr<PassConceptT>> Passes;
583
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100584 /// Flag indicating whether we should do debug logging.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100585 bool DebugLogging;
586};
587
588extern template class PassManager<Module>;
589
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100590/// Convenience typedef for a pass manager over modules.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100591using ModulePassManager = PassManager<Module>;
592
593extern template class PassManager<Function>;
594
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100595/// Convenience typedef for a pass manager over functions.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100596using FunctionPassManager = PassManager<Function>;
597
Andrew Scull0372a572018-11-16 15:47:06 +0000598/// Pseudo-analysis pass that exposes the \c PassInstrumentation to pass
599/// managers. Goes before AnalysisManager definition to provide its
600/// internals (e.g PassInstrumentationAnalysis::ID) for use there if needed.
601/// FIXME: figure out a way to move PassInstrumentationAnalysis into its own
602/// header.
603class PassInstrumentationAnalysis
604 : public AnalysisInfoMixin<PassInstrumentationAnalysis> {
605 friend AnalysisInfoMixin<PassInstrumentationAnalysis>;
606 static AnalysisKey Key;
607
608 PassInstrumentationCallbacks *Callbacks;
609
610public:
611 /// PassInstrumentationCallbacks object is shared, owned by something else,
612 /// not this analysis.
613 PassInstrumentationAnalysis(PassInstrumentationCallbacks *Callbacks = nullptr)
614 : Callbacks(Callbacks) {}
615
616 using Result = PassInstrumentation;
617
618 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
619 Result run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) {
620 return PassInstrumentation(Callbacks);
621 }
622};
623
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100624/// A container for analyses that lazily runs them and caches their
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100625/// results.
626///
627/// This class can manage analyses for any IR unit where the address of the IR
628/// unit sufficies as its identity.
629template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager {
630public:
631 class Invalidator;
632
633private:
634 // Now that we've defined our invalidator, we can define the concept types.
635 using ResultConceptT =
636 detail::AnalysisResultConcept<IRUnitT, PreservedAnalyses, Invalidator>;
637 using PassConceptT =
638 detail::AnalysisPassConcept<IRUnitT, PreservedAnalyses, Invalidator,
639 ExtraArgTs...>;
640
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100641 /// List of analysis pass IDs and associated concept pointers.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100642 ///
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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100650 /// Map type from IRUnitT pointer to our custom list type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100651 using AnalysisResultListMapT = DenseMap<IRUnitT *, AnalysisResultListT>;
652
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100653 /// Map type from a pair of analysis ID and IRUnitT pointer to an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100654 /// iterator into a particular result list (which is where the actual analysis
655 /// result is stored).
656 using AnalysisResultMapT =
657 DenseMap<std::pair<AnalysisKey *, IRUnitT *>,
658 typename AnalysisResultListT::iterator>;
659
660public:
661 /// API to communicate dependencies between analyses during invalidation.
662 ///
663 /// When an analysis result embeds handles to other analysis results, it
664 /// needs to be invalidated both when its own information isn't preserved and
665 /// when any of its embedded analysis results end up invalidated. We pass an
666 /// \c Invalidator object as an argument to \c invalidate() in order to let
667 /// the analysis results themselves define the dependency graph on the fly.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200668 /// This lets us avoid building an explicit representation of the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100669 /// dependencies between analysis results.
670 class Invalidator {
671 public:
672 /// Trigger the invalidation of some other analysis pass if not already
673 /// handled and return whether it was in fact invalidated.
674 ///
675 /// This is expected to be called from within a given analysis result's \c
676 /// invalidate method to trigger a depth-first walk of all inter-analysis
677 /// dependencies. The same \p IR unit and \p PA passed to that result's \c
678 /// invalidate method should in turn be provided to this routine.
679 ///
680 /// The first time this is called for a given analysis pass, it will call
681 /// the corresponding result's \c invalidate method. Subsequent calls will
682 /// use a cache of the results of that initial call. It is an error to form
683 /// cyclic dependencies between analysis results.
684 ///
685 /// This returns true if the given analysis's result is invalid. Any
686 /// dependecies on it will become invalid as a result.
687 template <typename PassT>
688 bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA) {
689 using ResultModelT =
690 detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
691 PreservedAnalyses, Invalidator>;
692
693 return invalidateImpl<ResultModelT>(PassT::ID(), IR, PA);
694 }
695
696 /// A type-erased variant of the above invalidate method with the same core
697 /// API other than passing an analysis ID rather than an analysis type
698 /// parameter.
699 ///
700 /// This is sadly less efficient than the above routine, which leverages
701 /// the type parameter to avoid the type erasure overhead.
702 bool invalidate(AnalysisKey *ID, IRUnitT &IR, const PreservedAnalyses &PA) {
703 return invalidateImpl<>(ID, IR, PA);
704 }
705
706 private:
707 friend class AnalysisManager;
708
709 template <typename ResultT = ResultConceptT>
710 bool invalidateImpl(AnalysisKey *ID, IRUnitT &IR,
711 const PreservedAnalyses &PA) {
712 // If we've already visited this pass, return true if it was invalidated
713 // and false otherwise.
714 auto IMapI = IsResultInvalidated.find(ID);
715 if (IMapI != IsResultInvalidated.end())
716 return IMapI->second;
717
718 // Otherwise look up the result object.
719 auto RI = Results.find({ID, &IR});
720 assert(RI != Results.end() &&
721 "Trying to invalidate a dependent result that isn't in the "
722 "manager's cache is always an error, likely due to a stale result "
723 "handle!");
724
725 auto &Result = static_cast<ResultT &>(*RI->second->second);
726
727 // Insert into the map whether the result should be invalidated and return
728 // that. Note that we cannot reuse IMapI and must do a fresh insert here,
729 // as calling invalidate could (recursively) insert things into the map,
730 // making any iterator or reference invalid.
731 bool Inserted;
732 std::tie(IMapI, Inserted) =
733 IsResultInvalidated.insert({ID, Result.invalidate(IR, PA, *this)});
734 (void)Inserted;
735 assert(Inserted && "Should not have already inserted this ID, likely "
736 "indicates a dependency cycle!");
737 return IMapI->second;
738 }
739
740 Invalidator(SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated,
741 const AnalysisResultMapT &Results)
742 : IsResultInvalidated(IsResultInvalidated), Results(Results) {}
743
744 SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated;
745 const AnalysisResultMapT &Results;
746 };
747
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100748 /// Construct an empty analysis manager.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100749 ///
750 /// If \p DebugLogging is true, we'll log our progress to llvm::dbgs().
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200751 AnalysisManager(bool DebugLogging = false);
752 AnalysisManager(AnalysisManager &&);
753 AnalysisManager &operator=(AnalysisManager &&);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100754
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100755 /// Returns true if the analysis manager has an empty results cache.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100756 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100763 /// Clear any cached analysis results for a single unit of IR.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100764 ///
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.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200768 void clear(IRUnitT &IR, llvm::StringRef Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100769
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100770 /// Clear all analysis results cached by this AnalysisManager.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100771 ///
772 /// Like \c clear(IRUnitT&), this doesn't invalidate the results; it simply
773 /// deletes them. This lets you clean up the AnalysisManager when the set of
774 /// IR units itself has potentially changed, and thus we can't even look up a
775 /// a result and invalidate/clear it directly.
776 void clear() {
777 AnalysisResults.clear();
778 AnalysisResultLists.clear();
779 }
780
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100781 /// Get the result of an analysis pass for a given IR unit.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100782 ///
783 /// Runs the analysis if a cached result is not available.
784 template <typename PassT>
785 typename PassT::Result &getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs) {
786 assert(AnalysisPasses.count(PassT::ID()) &&
787 "This analysis pass was not registered prior to being queried");
788 ResultConceptT &ResultConcept =
789 getResultImpl(PassT::ID(), IR, ExtraArgs...);
790
791 using ResultModelT =
792 detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
793 PreservedAnalyses, Invalidator>;
794
795 return static_cast<ResultModelT &>(ResultConcept).Result;
796 }
797
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100798 /// Get the cached result of an analysis pass for a given IR unit.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100799 ///
800 /// This method never runs the analysis.
801 ///
802 /// \returns null if there is no cached result.
803 template <typename PassT>
804 typename PassT::Result *getCachedResult(IRUnitT &IR) const {
805 assert(AnalysisPasses.count(PassT::ID()) &&
806 "This analysis pass was not registered prior to being queried");
807
808 ResultConceptT *ResultConcept = getCachedResultImpl(PassT::ID(), IR);
809 if (!ResultConcept)
810 return nullptr;
811
812 using ResultModelT =
813 detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
814 PreservedAnalyses, Invalidator>;
815
816 return &static_cast<ResultModelT *>(ResultConcept)->Result;
817 }
818
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200819 /// Verify that the given Result cannot be invalidated, assert otherwise.
820 template <typename PassT>
821 void verifyNotInvalidated(IRUnitT &IR, typename PassT::Result *Result) const {
822 PreservedAnalyses PA = PreservedAnalyses::none();
823 SmallDenseMap<AnalysisKey *, bool, 8> IsResultInvalidated;
824 Invalidator Inv(IsResultInvalidated, AnalysisResults);
825 assert(!Result->invalidate(IR, PA, Inv) &&
826 "Cached result cannot be invalidated");
827 }
828
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100829 /// Register an analysis pass with the manager.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100830 ///
831 /// The parameter is a callable whose result is an analysis pass. This allows
832 /// passing in a lambda to construct the analysis.
833 ///
834 /// The analysis type to register is the type returned by calling the \c
835 /// PassBuilder argument. If that type has already been registered, then the
836 /// argument will not be called and this function will return false.
837 /// Otherwise, we register the analysis returned by calling \c PassBuilder(),
838 /// and this function returns true.
839 ///
840 /// (Note: Although the return value of this function indicates whether or not
841 /// an analysis was previously registered, there intentionally isn't a way to
842 /// query this directly. Instead, you should just register all the analyses
843 /// you might want and let this class run them lazily. This idiom lets us
844 /// minimize the number of times we have to look up analyses in our
845 /// hashtable.)
846 template <typename PassBuilderT>
847 bool registerPass(PassBuilderT &&PassBuilder) {
848 using PassT = decltype(PassBuilder());
849 using PassModelT =
850 detail::AnalysisPassModel<IRUnitT, PassT, PreservedAnalyses,
851 Invalidator, ExtraArgTs...>;
852
853 auto &PassPtr = AnalysisPasses[PassT::ID()];
854 if (PassPtr)
855 // Already registered this pass type!
856 return false;
857
858 // Construct a new model around the instance returned by the builder.
859 PassPtr.reset(new PassModelT(PassBuilder()));
860 return true;
861 }
862
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200863 /// Invalidate a specific analysis pass for an IR unit.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100864 ///
865 /// Note that the analysis result can disregard invalidation, if it determines
866 /// it is in fact still valid.
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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100873 /// Invalidate cached analyses for an IR unit.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100874 ///
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.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200877 void invalidate(IRUnitT &IR, const PreservedAnalyses &PA);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100878
879private:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100880 /// Look up a registered analysis pass.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100881 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100888 /// Look up a registered analysis pass.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100889 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100896 /// Get an analysis result, running the pass if necessary.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100897 ResultConceptT &getResultImpl(AnalysisKey *ID, IRUnitT &IR,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200898 ExtraArgTs... ExtraArgs);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100899
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100900 /// Get a cached analysis result or return null.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100901 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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200907 /// Invalidate a pass result for a IR unit.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100908 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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200921 /// Map type from analysis pass ID to pass concept pointer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100922 using AnalysisPassMapT =
923 DenseMap<AnalysisKey *, std::unique_ptr<PassConceptT>>;
924
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200925 /// Collection of analysis passes, indexed by ID.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100926 AnalysisPassMapT AnalysisPasses;
927
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200928 /// Map from IR unit to a list of analysis results.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100929 ///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200930 /// Provides linear time removal of all analysis results for a IR unit and
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100931 /// the ultimate storage for a particular cached analysis result.
932 AnalysisResultListMapT AnalysisResultLists;
933
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200934 /// Map from an analysis ID and IR unit to a particular cached
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100935 /// analysis result.
936 AnalysisResultMapT AnalysisResults;
937
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100938 /// Indicates whether we log to \c llvm::dbgs().
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100939 bool DebugLogging;
940};
941
942extern template class AnalysisManager<Module>;
943
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100944/// Convenience typedef for the Module analysis manager.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100945using ModuleAnalysisManager = AnalysisManager<Module>;
946
947extern template class AnalysisManager<Function>;
948
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100949/// Convenience typedef for the Function analysis manager.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100950using FunctionAnalysisManager = AnalysisManager<Function>;
951
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100952/// An analysis over an "outer" IR unit that provides access to an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100953/// analysis manager over an "inner" IR unit. The inner unit must be contained
954/// in the outer unit.
955///
Andrew Scull0372a572018-11-16 15:47:06 +0000956/// For example, InnerAnalysisManagerProxy<FunctionAnalysisManager, Module> is
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100957/// 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.
968template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
969class InnerAnalysisManagerProxy
970 : public AnalysisInfoMixin<
971 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>> {
972public:
973 class Result {
974 public:
975 explicit Result(AnalysisManagerT &InnerAM) : InnerAM(&InnerAM) {}
976
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
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
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001003 /// Accessor for the analysis manager.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001004 AnalysisManagerT &getManager() { return *InnerAM; }
1005
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001006 /// Handler for invalidation of the outer IR unit, \c IRUnitT.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001007 ///
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
1024 explicit InnerAnalysisManagerProxy(AnalysisManagerT &InnerAM)
1025 : InnerAM(&InnerAM) {}
1026
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001027 /// Run the analysis pass and create our proxy result object.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001028 ///
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.
1032 Result run(IRUnitT &IR, AnalysisManager<IRUnitT, ExtraArgTs...> &AM,
1033 ExtraArgTs...) {
1034 return Result(*InnerAM);
1035 }
1036
1037private:
1038 friend AnalysisInfoMixin<
1039 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>>;
1040
1041 static AnalysisKey Key;
1042
1043 AnalysisManagerT *InnerAM;
1044};
1045
1046template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1047AnalysisKey
1048 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
1049
1050/// Provide the \c FunctionAnalysisManager to \c Module proxy.
1051using FunctionAnalysisManagerModuleProxy =
1052 InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>;
1053
1054/// Specialization of the invalidate method for the \c
1055/// FunctionAnalysisManagerModuleProxy's result.
1056template <>
1057bool 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.
1063extern template class InnerAnalysisManagerProxy<FunctionAnalysisManager,
1064 Module>;
1065
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001066/// An analysis over an "inner" IR unit that provides access to an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001067/// 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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001078/// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001088///
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.
1093template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1094class OuterAnalysisManagerProxy
1095 : public AnalysisInfoMixin<
1096 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>> {
1097public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001098 /// Result proxy object for \c OuterAnalysisManagerProxy.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001099 class Result {
1100 public:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001101 explicit Result(const AnalysisManagerT &OuterAM) : OuterAM(&OuterAM) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001102
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001103 /// Get a cached analysis. If the analysis can be invalidated, this will
1104 /// assert.
1105 template <typename PassT, typename IRUnitTParam>
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>
1116 bool cachedResultExists(IRUnitTParam &IR) const {
1117 typename PassT::Result *Res =
1118 OuterAM->template getCachedResult<PassT>(IR);
1119 return Res != nullptr;
1120 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001121
1122 /// When invalidation occurs, remove any registered invalidation events.
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;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001132 llvm::erase_if(InnerIDs, [&](AnalysisKey *InnerID) {
1133 return Inv.invalidate(InnerID, IRUnit, PA);
1134 });
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001135 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>
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.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001158 if (!llvm::is_contained(InvalidatedIDList, InvalidatedID))
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001159 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> &
1165 getOuterInvalidations() const {
1166 return OuterAnalysisInvalidationMap;
1167 }
1168
1169 private:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001170 const AnalysisManagerT *OuterAM;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001171
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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001178 OuterAnalysisManagerProxy(const AnalysisManagerT &OuterAM)
1179 : OuterAM(&OuterAM) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001180
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001181 /// Run the analysis pass and create our proxy result object.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001182 /// Nothing to see here, it just forwards the \c OuterAM reference into the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001183 /// result.
1184 Result run(IRUnitT &, AnalysisManager<IRUnitT, ExtraArgTs...> &,
1185 ExtraArgTs...) {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001186 return Result(*OuterAM);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001187 }
1188
1189private:
1190 friend AnalysisInfoMixin<
1191 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>>;
1192
1193 static AnalysisKey Key;
1194
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001195 const AnalysisManagerT *OuterAM;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001196};
1197
1198template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
1199AnalysisKey
1200 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
1201
1202extern template class OuterAnalysisManagerProxy<ModuleAnalysisManager,
1203 Function>;
1204/// Provide the \c ModuleAnalysisManager to \c Function proxy.
1205using ModuleAnalysisManagerFunctionProxy =
1206 OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>;
1207
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001208/// Trivial adaptor that maps from a module to its functions.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001209///
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.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001231class ModuleToFunctionPassAdaptor
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001232 : public PassInfoMixin<ModuleToFunctionPassAdaptor> {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001233public:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001234 using PassConceptT = detail::PassConcept<Function, FunctionAnalysisManager>;
1235
1236 explicit ModuleToFunctionPassAdaptor(std::unique_ptr<PassConceptT> Pass)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001237 : Pass(std::move(Pass)) {}
1238
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001239 /// Runs the function pass across every function in the module.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001240 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001241
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001242 static bool isRequired() { return true; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001243
1244private:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001245 std::unique_ptr<PassConceptT> Pass;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001246};
1247
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001248/// A function to deduce a function pass type and wrap it in the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001249/// templated adaptor.
1250template <typename FunctionPassT>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001251ModuleToFunctionPassAdaptor
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001252createModuleToFunctionPassAdaptor(FunctionPassT Pass) {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001253 using PassModelT =
1254 detail::PassModel<Function, FunctionPassT, PreservedAnalyses,
1255 FunctionAnalysisManager>;
1256
1257 return ModuleToFunctionPassAdaptor(
1258 std::make_unique<PassModelT>(std::move(Pass)));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001259}
1260
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001261/// A utility pass template to force an analysis result to be available.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001262///
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.
1270template <typename AnalysisT, typename IRUnitT,
1271 typename AnalysisManagerT = AnalysisManager<IRUnitT>,
1272 typename... ExtraArgTs>
1273struct RequireAnalysisPass
1274 : PassInfoMixin<RequireAnalysisPass<AnalysisT, IRUnitT, AnalysisManagerT,
1275 ExtraArgTs...>> {
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001276 /// Run this pass over some unit of IR.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001277 ///
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.
1282 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 }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001289 static bool isRequired() { return true; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001290};
1291
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001292/// A no-op pass template which simply forces a specific analysis result
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001293/// to be invalidated.
1294template <typename AnalysisT>
1295struct InvalidateAnalysisPass
1296 : PassInfoMixin<InvalidateAnalysisPass<AnalysisT>> {
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001297 /// Run this pass over some unit of IR.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001298 ///
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>
1304 PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&...) {
1305 auto PA = PreservedAnalyses::all();
1306 PA.abandon<AnalysisT>();
1307 return PA;
1308 }
1309};
1310
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001311/// A utility pass that does nothing, but preserves no analyses.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001312///
1313/// Because this preserves no analyses, any analysis passes queried after this
1314/// pass runs will recompute fresh results.
1315struct InvalidateAllAnalysesPass : PassInfoMixin<InvalidateAllAnalysesPass> {
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001316 /// Run this pass over some unit of IR.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001317 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
1318 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.
1327template <typename PassT>
1328class RepeatedPass : public PassInfoMixin<RepeatedPass<PassT>> {
1329public:
1330 RepeatedPass(int Count, PassT P) : Count(Count), P(std::move(P)) {}
1331
1332 template <typename IRUnitT, typename AnalysisManagerT, typename... Ts>
Andrew Scull0372a572018-11-16 15:47:06 +00001333 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001343 auto PA = PreservedAnalyses::all();
Andrew Scull0372a572018-11-16 15:47:06 +00001344 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;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001350 PreservedAnalyses IterPA = P.run(IR, AM, std::forward<Ts>(Args)...);
1351 PA.intersect(IterPA);
1352 PI.runAfterPass(P, IR, IterPA);
Andrew Scull0372a572018-11-16 15:47:06 +00001353 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001354 return PA;
1355 }
1356
1357private:
1358 int Count;
1359 PassT P;
1360};
1361
1362template <typename PassT>
1363RepeatedPass<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