LLVM 24.0.0git
PassManager.h
Go to the documentation of this file.
1//===- PassManager.h - Pass management infrastructure -----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9///
10/// This header defines various interfaces for pass management in LLVM. There
11/// is no "pass" interface in LLVM per se. Instead, an instance of any class
12/// which supports a method to 'run' it over a unit of IR can be used as
13/// a pass. A pass manager is generally a tool to collect a sequence of passes
14/// which run over a particular IR construct, and run each of them in sequence
15/// over each such construct in the containing IR construct. As there is no
16/// containing IR construct for a Module, a manager for passes over modules
17/// forms the base case which runs its managed passes in sequence over the
18/// single module provided.
19///
20/// The core IR library provides managers for running passes over
21/// modules and functions.
22///
23/// * FunctionPassManager can run over a Module, runs each pass over
24/// a Function.
25/// * ModulePassManager must be directly run, runs each pass over the Module.
26///
27/// Note that the implementations of the pass managers use concept-based
28/// polymorphism as outlined in the "Value Semantics and Concept-based
29/// Polymorphism" talk (or its abbreviated sibling "Inheritance Is The Base
30/// Class of Evil") by Sean Parent:
31/// * https://sean-parent.stlab.cc/papers-and-presentations
32/// * http://www.youtube.com/watch?v=_BpMYeUFXv8
33/// * https://learn.microsoft.com/en-us/shows/goingnative-2013/inheritance-base-class-of-evil
34///
35//===----------------------------------------------------------------------===//
36
37#ifndef LLVM_IR_PASSMANAGER_H
38#define LLVM_IR_PASSMANAGER_H
39
40#include "llvm/ADT/DenseMap.h"
41#include "llvm/ADT/STLExtras.h"
43#include "llvm/ADT/StringRef.h"
45#include "llvm/IR/Analysis.h"
49#include <cassert>
50#include <cstring>
51#include <list>
52#include <memory>
53#include <tuple>
54#include <type_traits>
55#include <utility>
56#include <vector>
57
58namespace llvm {
59
60namespace detail {
61template <typename DerivedT> struct InfoMixin {
62 /// Gets the name of the pass we are mixed into.
63 static StringRef name() {
64 static_assert(std::is_base_of<InfoMixin, DerivedT>::value,
65 "Must pass the derived type as the template argument!");
67 Name.consume_front("llvm::");
68 return Name;
69 }
70};
71
72/// A CRTP mix-in to automatically provide informational APIs needed for
73/// passes.
74///
75/// This provides some boilerplate for types that are passes.
76///
77/// Actual passes should inherit from RequiredPassInfoMixin or
78/// OptionalPassInfoMixin.
79///
80template <typename DerivedT>
81struct PassInfoMixin : detail::InfoMixin<DerivedT> {
83 function_ref<StringRef(StringRef)> MapClassName2PassName) {
84 StringRef ClassName = DerivedT::name();
85 auto PassName = MapClassName2PassName(ClassName);
86 OS << PassName;
87 }
88
89 // TODO: remove once out of tree users are updated.
90 static bool isRequired() { return false; }
91};
92} // namespace detail
93
94class Function;
95class Module;
96
97// Forward declare the analysis manager template.
98template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
99
100/// A CRTP mix-in for passes that should not be skipped.
101template <typename DerivedT>
103 static bool isRequired() { return true; }
104};
105
106/// A CRTP mix-in for passes that can be skipped.
107template <typename DerivedT>
109 static bool isRequired() { return false; }
110};
111
112/// A CRTP mix-in that provides informational APIs needed for analysis passes.
113///
114/// This provides some boilerplate for types that are analysis passes. It
115/// automatically mixes in \c PassInfoMixin.
116template <typename DerivedT>
118 /// Returns an opaque, unique ID for this analysis type.
119 ///
120 /// This ID is a pointer type that is guaranteed to be 8-byte aligned and thus
121 /// suitable for use in sets, maps, and other data structures that use the low
122 /// bits of pointers.
123 ///
124 /// Note that this requires the derived type provide a static \c AnalysisKey
125 /// member called \c Key.
126 ///
127 /// FIXME: The only reason the mixin type itself can't declare the Key value
128 /// is that some compilers cannot correctly unique a templated static variable
129 /// so it has the same addresses in each instantiation. The only currently
130 /// known platform with this limitation is Windows DLL builds, specifically
131 /// building each part of LLVM as a DLL. If we ever remove that build
132 /// configuration, this mixin can provide the static key as well.
133 static AnalysisKey *ID() {
134 static_assert(std::is_base_of<AnalysisInfoMixin, DerivedT>::value,
135 "Must pass the derived type as the template argument!");
136 return &DerivedT::Key;
137 }
138};
139
140namespace detail {
141
142/// Actual unpacker of extra arguments in getAnalysisResult,
143/// passes only those tuple arguments that are mentioned in index_sequence.
144template <typename PassT, typename IRUnitT, typename AnalysisManagerT,
145 typename... ArgTs, size_t... Ns>
146typename PassT::Result
147getAnalysisResultUnpackTuple(AnalysisManagerT &AM, IRUnitT &IR,
148 std::tuple<ArgTs...> Args,
149 std::index_sequence<Ns...>) {
150 (void)Args;
151 return AM.template getResult<PassT>(IR, std::get<Ns>(Args)...);
152}
153
154/// Helper for *partial* unpacking of extra arguments in getAnalysisResult.
155///
156/// Arguments passed in tuple come from PassManager, so they might have extra
157/// arguments after those AnalysisManager's ExtraArgTs ones that we need to
158/// pass to getResult.
159template <typename PassT, typename IRUnitT, typename... AnalysisArgTs,
160 typename... MainArgTs>
161typename PassT::Result
163 std::tuple<MainArgTs...> Args) {
165 PassT, IRUnitT>)(AM, IR, Args,
166 std::index_sequence_for<AnalysisArgTs...>{});
167}
168
169} // namespace detail
170
171/// Manages a sequence of passes over a particular unit of IR.
172///
173/// A pass manager contains a sequence of passes to run over a particular unit
174/// of IR (e.g. Functions, Modules). It is itself a valid pass over that unit of
175/// IR, and when run over some given IR will run each of its contained passes in
176/// sequence. Pass managers are the primary and most basic building block of a
177/// pass pipeline.
178///
179/// When you run a pass manager, you provide an \c AnalysisManager<IRUnitT>
180/// argument. The pass manager will propagate that analysis manager to each
181/// pass it runs, and will call the analysis manager's invalidation routine with
182/// the PreservedAnalyses of each pass it runs.
183template <typename IRUnitT,
184 typename AnalysisManagerT = AnalysisManager<IRUnitT>,
185 typename... ExtraArgTs>
187 PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...>> {
188public:
189 /// Construct a pass manager.
190 explicit PassManager() = default;
191
192 // FIXME: These are equivalent to the default move constructor/move
193 // assignment. However, using = default triggers linker errors due to the
194 // explicit instantiations below. Find away to use the default and remove the
195 // duplicated code here.
197
199 Passes = std::move(RHS.Passes);
200 return *this;
201 }
202
204 function_ref<StringRef(StringRef)> MapClassName2PassName) {
205 ListSeparator LS(",");
206 for (auto &P : Passes) {
207 OS << LS;
208 P->printPipeline(OS, MapClassName2PassName);
209 }
210 }
211
212 /// Run all of the passes in this manager over the given unit of IR.
213 /// ExtraArgs are passed to each pass.
214 PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM,
215 ExtraArgTs... ExtraArgs);
216
217 template <typename PassT>
218 LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same_v<PassT, PassManager>>
219 addPass(PassT &&Pass) {
220 using PassModelT =
221 detail::PassModel<IRUnitT, PassT, AnalysisManagerT, ExtraArgTs...>;
222 Passes.push_back(PassModelT::create(std::move(Pass)));
223 }
224
225 /// When adding a pass manager pass that has the same type as this pass
226 /// manager, simply move the passes over. This is because we don't have
227 /// use cases rely on executing nested pass managers. Doing this could
228 /// reduce implementation complexity and avoid potential invalidation
229 /// issues that may happen with nested pass managers of the same type.
230 template <typename PassT>
231 LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<std::is_same_v<PassT, PassManager>>
232 addPass(PassT &&Pass) {
233 for (auto &P : Pass.Passes)
234 Passes.push_back(std::move(P));
235 }
236
237 /// Returns if the pass manager contains any passes.
238 bool isEmpty() const { return Passes.empty(); }
239
240protected:
242 detail::PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...>;
243
244 std::vector<typename PassConceptT::unique_ptr> Passes;
245};
246
247template <typename IRUnitT>
249
250template <>
252 const Module &IR);
253
254extern template class LLVM_TEMPLATE_ABI PassManager<Module>;
255
256/// Convenience typedef for a pass manager over modules.
258
259template <>
261 const Function &IR);
262
263extern template class LLVM_TEMPLATE_ABI PassManager<Function>;
264
265/// Convenience typedef for a pass manager over functions.
267
268/// A container for analyses that lazily runs them and caches their
269/// results.
270///
271/// This class can manage analyses for any IR unit where the address of the IR
272/// unit sufficies as its identity.
273template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager {
274public:
275 class Invalidator;
276
277private:
278 // Now that we've defined our invalidator, we can define the concept types.
280 using PassConceptT =
281 detail::AnalysisPassConcept<IRUnitT, Invalidator, ExtraArgTs...>;
282
283 /// List of analysis pass IDs and associated concept pointers.
284 ///
285 /// Requires iterators to be valid across appending new entries and arbitrary
286 /// erases. Provides the analysis ID to enable finding iterators to a given
287 /// entry in maps below, and provides the storage for the actual result
288 /// concept.
289 using AnalysisResultListT =
290 std::list<std::pair<AnalysisKey *, typename ResultConceptT::unique_ptr>>;
291
292 /// Map type from IRUnitT pointer to our custom list type.
293 using AnalysisResultListMapT = DenseMap<IRUnitT *, AnalysisResultListT>;
294
295 /// Map type from a pair of analysis ID and IRUnitT pointer to an
296 /// iterator into a particular result list (which is where the actual analysis
297 /// result is stored).
298 using AnalysisResultMapT =
300 typename AnalysisResultListT::iterator>;
301
302public:
303 /// API to communicate dependencies between analyses during invalidation.
304 ///
305 /// When an analysis result embeds handles to other analysis results, it
306 /// needs to be invalidated both when its own information isn't preserved and
307 /// when any of its embedded analysis results end up invalidated. We pass an
308 /// \c Invalidator object as an argument to \c invalidate() in order to let
309 /// the analysis results themselves define the dependency graph on the fly.
310 /// This lets us avoid building an explicit representation of the
311 /// dependencies between analysis results.
312 class Invalidator {
313 public:
314 /// Trigger the invalidation of some other analysis pass if not already
315 /// handled and return whether it was in fact invalidated.
316 ///
317 /// This is expected to be called from within a given analysis result's \c
318 /// invalidate method to trigger a depth-first walk of all inter-analysis
319 /// dependencies. The same \p IR unit and \p PA passed to that result's \c
320 /// invalidate method should in turn be provided to this routine.
321 ///
322 /// The first time this is called for a given analysis pass, it will call
323 /// the corresponding result's \c invalidate method. Subsequent calls will
324 /// use a cache of the results of that initial call. It is an error to form
325 /// cyclic dependencies between analysis results.
326 ///
327 /// This returns true if the given analysis's result is invalid. Any
328 /// dependecies on it will become invalid as a result.
329 template <typename PassT>
330 bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA) {
331 using ResultModelT =
332 detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
333 Invalidator>;
334
335 return invalidateImpl<ResultModelT>(PassT::ID(), IR, PA);
336 }
337
338 /// A type-erased variant of the above invalidate method with the same core
339 /// API other than passing an analysis ID rather than an analysis type
340 /// parameter.
341 ///
342 /// This is sadly less efficient than the above routine, which leverages
343 /// the type parameter to avoid the type erasure overhead.
344 bool invalidate(AnalysisKey *ID, IRUnitT &IR, const PreservedAnalyses &PA) {
345 return invalidateImpl<>(ID, IR, PA);
346 }
347
348 private:
349 friend class AnalysisManager;
350
351 template <typename ResultT = ResultConceptT>
352 bool invalidateImpl(AnalysisKey *ID, IRUnitT &IR,
353 const PreservedAnalyses &PA) {
354 // If we've already visited this pass, return true if it was invalidated
355 // and false otherwise.
356 auto IMapI = IsResultInvalidated.find(ID);
357 if (IMapI != IsResultInvalidated.end())
358 return IMapI->second;
359
360 // Otherwise look up the result object.
361 auto RI = Results.find({ID, &IR});
362 assert(RI != Results.end() &&
363 "Trying to invalidate a dependent result that isn't in the "
364 "manager's cache is always an error, likely due to a stale result "
365 "handle!");
366
367 auto &Result = static_cast<ResultT &>(*RI->second->second);
368
369 // Insert into the map whether the result should be invalidated and return
370 // that. Note that we cannot reuse IMapI and must do a fresh insert here,
371 // as calling invalidate could (recursively) insert things into the map,
372 // making any iterator or reference invalid.
373 bool Inserted;
374 std::tie(IMapI, Inserted) =
375 IsResultInvalidated.insert({ID, Result.invalidate(IR, PA, *this)});
376 (void)Inserted;
377 assert(Inserted && "Should not have already inserted this ID, likely "
378 "indicates a dependency cycle!");
379 return IMapI->second;
380 }
381
382 Invalidator(SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated,
383 const AnalysisResultMapT &Results)
384 : IsResultInvalidated(IsResultInvalidated), Results(Results) {}
385
386 SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated;
387 const AnalysisResultMapT &Results;
388 };
389
390 /// Construct an empty analysis manager.
394
395 /// Returns true if the analysis manager has an empty results cache.
396 bool empty() const {
397 assert(AnalysisResults.empty() == AnalysisResultLists.empty() &&
398 "The storage and index of analysis results disagree on how many "
399 "there are!");
400 return AnalysisResults.empty();
401 }
402
403 /// Clear any cached analysis results for a single unit of IR.
404 ///
405 /// This doesn't invalidate, but instead simply deletes, the relevant results.
406 /// It is useful when the IR is being removed and we want to clear out all the
407 /// memory pinned for it.
408 void clear(IRUnitT &IR, llvm::StringRef Name);
409
410 /// Clear all analysis results cached by this AnalysisManager.
411 ///
412 /// Like \c clear(IRUnitT&), this doesn't invalidate the results; it simply
413 /// deletes them. This lets you clean up the AnalysisManager when the set of
414 /// IR units itself has potentially changed, and thus we can't even look up a
415 /// a result and invalidate/clear it directly.
416 void clear() {
417 AnalysisResults.clear();
418 AnalysisResultLists.clear();
419 }
420
421 /// Returns true if the specified analysis pass is registered.
422 template <typename PassT> bool isPassRegistered() const {
423 return AnalysisPasses.count(PassT::ID());
424 }
425
426 /// Get the result of an analysis pass for a given IR unit.
427 ///
428 /// Runs the analysis if a cached result is not available.
429 template <typename PassT>
430 typename PassT::Result &getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs) {
431 assert(AnalysisPasses.count(PassT::ID()) &&
432 "This analysis pass was not registered prior to being queried");
433 ResultConceptT &ResultConcept =
434 getResultImpl(PassT::ID(), IR, ExtraArgs...);
435
436 using ResultModelT =
437 detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
438 Invalidator>;
439
440 return static_cast<ResultModelT &>(ResultConcept).Result;
441 }
442
443 /// Get the cached result of an analysis pass for a given IR unit.
444 ///
445 /// This method never runs the analysis.
446 ///
447 /// \returns null if there is no cached result.
448 template <typename PassT>
449 typename PassT::Result *getCachedResult(IRUnitT &IR) const {
450 assert(AnalysisPasses.count(PassT::ID()) &&
451 "This analysis pass was not registered prior to being queried");
452
453 ResultConceptT *ResultConcept = getCachedResultImpl(PassT::ID(), IR);
454 if (!ResultConcept)
455 return nullptr;
456
457 using ResultModelT =
458 detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
459 Invalidator>;
460
461 return &static_cast<ResultModelT *>(ResultConcept)->Result;
462 }
463
464 /// Verify that the given Result cannot be invalidated, assert otherwise.
465 template <typename PassT>
466 void verifyNotInvalidated(IRUnitT &IR, typename PassT::Result *Result) const {
468 SmallDenseMap<AnalysisKey *, bool, 8> IsResultInvalidated;
469 Invalidator Inv(IsResultInvalidated, AnalysisResults);
470 assert(!Result->invalidate(IR, PA, Inv) &&
471 "Cached result cannot be invalidated");
472 }
473
474 /// Register an analysis pass with the manager.
475 ///
476 /// The parameter is a callable whose result is an analysis pass. This allows
477 /// passing in a lambda to construct the analysis.
478 ///
479 /// The analysis type to register is the type returned by calling the \c
480 /// PassBuilder argument. If that type has already been registered, then the
481 /// argument will not be called and this function will return false.
482 /// Otherwise, we register the analysis returned by calling \c PassBuilder(),
483 /// and this function returns true.
484 ///
485 /// (Note: Although the return value of this function indicates whether or not
486 /// an analysis was previously registered, you should just register all the
487 /// analyses you might want and let this class run them lazily. This idiom
488 /// lets us minimize the number of times we have to look up analyses in our
489 /// hashtable.)
490 template <typename PassBuilderT>
491 bool registerPass(PassBuilderT &&PassBuilder) {
492 using PassT = decltype(PassBuilder());
493 using PassModelT =
494 detail::AnalysisPassModel<IRUnitT, PassT, Invalidator, ExtraArgTs...>;
495
496 auto &PassPtr = AnalysisPasses[PassT::ID()];
497 if (PassPtr)
498 // Already registered this pass type!
499 return false;
500
501 // Construct a new model around the instance returned by the builder.
502 PassPtr = PassModelT::create(PassBuilder());
503 return true;
504 }
505
506 /// Invalidate cached analyses for an IR unit.
507 ///
508 /// Walk through all of the analyses pertaining to this unit of IR and
509 /// invalidate them, unless they are preserved by the PreservedAnalyses set.
510 void invalidate(IRUnitT &IR, const PreservedAnalyses &PA);
511
512 /// Directly clear a cached analysis for an IR unit.
513 ///
514 /// Using invalidate() over this is preferred unless you are really
515 /// sure you want to *only* clear this analysis without asking if it is
516 /// invalid.
517 template <typename AnalysisT> void clearAnalysis(IRUnitT &IR) {
518 AnalysisResultListT &ResultsList = AnalysisResultLists[&IR];
519 AnalysisKey *ID = AnalysisT::ID();
520
521 auto I =
522 llvm::find_if(ResultsList, [&ID](auto &E) { return E.first == ID; });
523 assert(I != ResultsList.end() && "Analysis must be available");
524 ResultsList.erase(I);
525 AnalysisResults.erase({ID, &IR});
526 }
527
528private:
529 /// Look up a registered analysis pass.
530 PassConceptT &lookUpPass(AnalysisKey *ID) {
531 typename AnalysisPassMapT::iterator PI = AnalysisPasses.find(ID);
532 assert(PI != AnalysisPasses.end() &&
533 "Analysis passes must be registered prior to being queried!");
534 return *PI->second;
535 }
536
537 /// Look up a registered analysis pass.
538 const PassConceptT &lookUpPass(AnalysisKey *ID) const {
539 typename AnalysisPassMapT::const_iterator PI = AnalysisPasses.find(ID);
540 assert(PI != AnalysisPasses.end() &&
541 "Analysis passes must be registered prior to being queried!");
542 return *PI->second;
543 }
544
545 /// Get an analysis result, running the pass if necessary.
546 ResultConceptT &getResultImpl(AnalysisKey *ID, IRUnitT &IR,
547 ExtraArgTs... ExtraArgs);
548
549 /// Get a cached analysis result or return null.
550 ResultConceptT *getCachedResultImpl(AnalysisKey *ID, IRUnitT &IR) const {
551 typename AnalysisResultMapT::const_iterator RI =
552 AnalysisResults.find({ID, &IR});
553 return RI == AnalysisResults.end() ? nullptr : &*RI->second->second;
554 }
555
556 /// Map type from analysis pass ID to pass concept pointer.
557 using AnalysisPassMapT =
558 DenseMap<AnalysisKey *, typename PassConceptT::unique_ptr>;
559
560 /// Collection of analysis passes, indexed by ID.
561 AnalysisPassMapT AnalysisPasses;
562
563 /// Map from IR unit to a list of analysis results.
564 ///
565 /// Provides linear time removal of all analysis results for a IR unit and
566 /// the ultimate storage for a particular cached analysis result.
567 AnalysisResultListMapT AnalysisResultLists;
568
569 /// Map from an analysis ID and IR unit to a particular cached
570 /// analysis result.
571 AnalysisResultMapT AnalysisResults;
572};
573
574extern template class LLVM_TEMPLATE_ABI AnalysisManager<Module>;
575
576/// Convenience typedef for the Module analysis manager.
578
579extern template class LLVM_TEMPLATE_ABI AnalysisManager<Function>;
580
581/// Convenience typedef for the Function analysis manager.
583
584/// An analysis over an "outer" IR unit that provides access to an
585/// analysis manager over an "inner" IR unit. The inner unit must be contained
586/// in the outer unit.
587///
588/// For example, InnerAnalysisManagerProxy<FunctionAnalysisManager, Module> is
589/// an analysis over Modules (the "outer" unit) that provides access to a
590/// Function analysis manager. The FunctionAnalysisManager is the "inner"
591/// manager being proxied, and Functions are the "inner" unit. The inner/outer
592/// relationship is valid because each Function is contained in one Module.
593///
594/// If you're (transitively) within a pass manager for an IR unit U that
595/// contains IR unit V, you should never use an analysis manager over V, except
596/// via one of these proxies.
597///
598/// Note that the proxy's result is a move-only RAII object. The validity of
599/// the analyses in the inner analysis manager is tied to its lifetime.
600template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
602 : public AnalysisInfoMixin<
603 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>> {
604public:
605 class Result {
606 public:
607 explicit Result(AnalysisManagerT &InnerAM) : InnerAM(&InnerAM) {}
608
609 Result(Result &&Arg) : InnerAM(std::move(Arg.InnerAM)) {
610 // We have to null out the analysis manager in the moved-from state
611 // because we are taking ownership of the responsibility to clear the
612 // analysis state.
613 Arg.InnerAM = nullptr;
614 }
615
617 // InnerAM is cleared in a moved from state where there is nothing to do.
618 if (!InnerAM)
619 return;
620
621 // Clear out the analysis manager if we're being destroyed -- it means we
622 // didn't even see an invalidate call when we got invalidated.
623 InnerAM->clear();
624 }
625
627 InnerAM = RHS.InnerAM;
628 // We have to null out the analysis manager in the moved-from state
629 // because we are taking ownership of the responsibility to clear the
630 // analysis state.
631 RHS.InnerAM = nullptr;
632 return *this;
633 }
634
635 /// Accessor for the analysis manager.
636 AnalysisManagerT &getManager() { return *InnerAM; }
637
638 /// Handler for invalidation of the outer IR unit, \c IRUnitT.
639 ///
640 /// If the proxy analysis itself is not preserved, we assume that the set of
641 /// inner IR objects contained in IRUnit may have changed. In this case,
642 /// we have to call \c clear() on the inner analysis manager, as it may now
643 /// have stale pointers to its inner IR objects.
644 ///
645 /// Regardless of whether the proxy analysis is marked as preserved, all of
646 /// the analyses in the inner analysis manager are potentially invalidated
647 /// based on the set of preserved analyses.
649 IRUnitT &IR, const PreservedAnalyses &PA,
651
652 private:
653 AnalysisManagerT *InnerAM;
654 };
655
656 explicit InnerAnalysisManagerProxy(AnalysisManagerT &InnerAM)
657 : InnerAM(&InnerAM) {}
658
659 /// Run the analysis pass and create our proxy result object.
660 ///
661 /// This doesn't do any interesting work; it is primarily used to insert our
662 /// proxy result object into the outer analysis cache so that we can proxy
663 /// invalidation to the inner analysis manager.
665 ExtraArgTs...) {
666 return Result(*InnerAM);
667 }
668
669private:
670 friend AnalysisInfoMixin<
672
673 static AnalysisKey Key;
674
675 AnalysisManagerT *InnerAM;
676};
677
678// NOTE: The LLVM_ABI annotation cannot be used here because MSVC disallows
679// storage-class specifiers on class members outside of the class declaration
680// (C2720). LLVM_ATTRIBUTE_VISIBILITY_DEFAULT only applies to non-Windows
681// targets so it is used instead. Without this annotation, compiling LLVM as a
682// shared library with -fvisibility=hidden using GCC fails to export the symbol
683// even though InnerAnalysisManagerProxy is already annotated with LLVM_ABI.
684template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
686 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
687
688/// Provide the \c FunctionAnalysisManager to \c Module proxy.
691
692/// Specialization of the invalidate method for the \c
693/// FunctionAnalysisManagerModuleProxy's result.
694template <>
695LLVM_ABI bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
696 Module &M, const PreservedAnalyses &PA,
697 ModuleAnalysisManager::Invalidator &Inv);
698
699// Ensure the \c FunctionAnalysisManagerModuleProxy is provided as an extern
700// template.
702 Module>;
703
704/// An analysis over an "inner" IR unit that provides access to an
705/// analysis manager over a "outer" IR unit. The inner unit must be contained
706/// in the outer unit.
707///
708/// For example OuterAnalysisManagerProxy<ModuleAnalysisManager, Function> is an
709/// analysis over Functions (the "inner" unit) which provides access to a Module
710/// analysis manager. The ModuleAnalysisManager is the "outer" manager being
711/// proxied, and Modules are the "outer" IR unit. The inner/outer relationship
712/// is valid because each Function is contained in one Module.
713///
714/// This proxy only exposes the const interface of the outer analysis manager,
715/// to indicate that you cannot cause an outer analysis to run from within an
716/// inner pass. Instead, you must rely on the \c getCachedResult API. This is
717/// due to keeping potential future concurrency in mind. To give an example,
718/// running a module analysis before any function passes may give a different
719/// result than running it in a function pass. Both may be valid, but it would
720/// produce non-deterministic results. GlobalsAA is a good analysis example,
721/// because the cached information has the mod/ref info for all memory for each
722/// function at the time the analysis was computed. The information is still
723/// valid after a function transformation, but it may be *different* if
724/// recomputed after that transform. GlobalsAA is never invalidated.
725
726///
727/// This proxy doesn't manage invalidation in any way -- that is handled by the
728/// recursive return path of each layer of the pass manager. A consequence of
729/// this is the outer analyses may be stale. We invalidate the outer analyses
730/// only when we're done running passes over the inner IR units.
731template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
733 : public AnalysisInfoMixin<
734 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>> {
735public:
736 /// Result proxy object for \c OuterAnalysisManagerProxy.
737 class Result {
738 public:
739 explicit Result(const AnalysisManagerT &OuterAM) : OuterAM(&OuterAM) {}
740
741 /// Get a cached analysis. If the analysis can be invalidated, this will
742 /// assert.
743 template <typename PassT, typename IRUnitTParam>
744 typename PassT::Result *getCachedResult(IRUnitTParam &IR) const {
745 typename PassT::Result *Res =
746 OuterAM->template getCachedResult<PassT>(IR);
747 if (Res)
748 OuterAM->template verifyNotInvalidated<PassT>(IR, Res);
749 return Res;
750 }
751
752 /// Method provided for unit testing, not intended for general use.
753 template <typename PassT, typename IRUnitTParam>
754 bool cachedResultExists(IRUnitTParam &IR) const {
755 typename PassT::Result *Res =
756 OuterAM->template getCachedResult<PassT>(IR);
757 return Res != nullptr;
758 }
759
760 /// When invalidation occurs, remove any registered invalidation events.
762 IRUnitT &IRUnit, const PreservedAnalyses &PA,
764 // Loop over the set of registered outer invalidation mappings and if any
765 // of them map to an analysis that is now invalid, clear it out.
767 for (auto &KeyValuePair : OuterAnalysisInvalidationMap) {
768 AnalysisKey *OuterID = KeyValuePair.first;
769 auto &InnerIDs = KeyValuePair.second;
770 llvm::erase_if(InnerIDs, [&](AnalysisKey *InnerID) {
771 return Inv.invalidate(InnerID, IRUnit, PA);
772 });
773 if (InnerIDs.empty())
774 DeadKeys.push_back(OuterID);
775 }
776
777 for (auto *OuterID : DeadKeys)
778 OuterAnalysisInvalidationMap.erase(OuterID);
779
780 // The proxy itself remains valid regardless of anything else.
781 return false;
782 }
783
784 /// Register a deferred invalidation event for when the outer analysis
785 /// manager processes its invalidations.
786 template <typename OuterAnalysisT, typename InvalidatedAnalysisT>
788 AnalysisKey *OuterID = OuterAnalysisT::ID();
789 AnalysisKey *InvalidatedID = InvalidatedAnalysisT::ID();
790
791 auto &InvalidatedIDList = OuterAnalysisInvalidationMap[OuterID];
792 // Note, this is a linear scan. If we end up with large numbers of
793 // analyses that all trigger invalidation on the same outer analysis,
794 // this entire system should be changed to some other deterministic
795 // data structure such as a `SetVector` of a pair of pointers.
796 if (!llvm::is_contained(InvalidatedIDList, InvalidatedID))
797 InvalidatedIDList.push_back(InvalidatedID);
798 }
799
800 /// Access the map from outer analyses to deferred invalidation requiring
801 /// analyses.
804 return OuterAnalysisInvalidationMap;
805 }
806
807 private:
808 const AnalysisManagerT *OuterAM;
809
810 /// A map from an outer analysis ID to the set of this IR-unit's analyses
811 /// which need to be invalidated.
813 OuterAnalysisInvalidationMap;
814 };
815
816 OuterAnalysisManagerProxy(const AnalysisManagerT &OuterAM)
817 : OuterAM(&OuterAM) {}
818
819 /// Run the analysis pass and create our proxy result object.
820 /// Nothing to see here, it just forwards the \c OuterAM reference into the
821 /// result.
823 ExtraArgTs...) {
824 return Result(*OuterAM);
825 }
826
827private:
828 friend AnalysisInfoMixin<
829 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>>;
830
831 static AnalysisKey Key;
832
833 const AnalysisManagerT *OuterAM;
834};
835
836template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
837AnalysisKey
838 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
839
840extern template class LLVM_TEMPLATE_ABI
842/// Provide the \c ModuleAnalysisManager to \c Function proxy.
845
846/// Trivial adaptor that maps from a module to its functions.
847///
848/// Designed to allow composition of a FunctionPass(Manager) and
849/// a ModulePassManager, by running the FunctionPass(Manager) over every
850/// function in the module.
851///
852/// Function passes run within this adaptor can rely on having exclusive access
853/// to the function they are run over. They should not read or modify any other
854/// functions! Other threads or systems may be manipulating other functions in
855/// the module, and so their state should never be relied on.
856/// FIXME: Make the above true for all of LLVM's actual passes, some still
857/// violate this principle.
858///
859/// Function passes can also read the module containing the function, but they
860/// should not modify that module outside of the use lists of various globals.
861/// For example, a function pass is not permitted to add functions to the
862/// module.
863/// FIXME: Make the above true for all of LLVM's actual passes, some still
864/// violate this principle.
865///
866/// Note that although function passes can access module analyses, module
867/// analyses are not invalidated while the function passes are running, so they
868/// may be stale. Function analyses will not be stale.
871public:
873
875 bool EagerlyInvalidate)
876 : Pass(std::move(Pass)), EagerlyInvalidate(EagerlyInvalidate) {}
877
878 /// Runs the function pass across every function in the module.
880 LLVM_ABI void
881 printPipeline(raw_ostream &OS,
882 function_ref<StringRef(StringRef)> MapClassName2PassName);
883
884private:
885 PassConceptT::unique_ptr Pass;
886 bool EagerlyInvalidate;
887};
888
889/// A function to deduce a function pass type and wrap it in the
890/// templated adaptor.
891template <typename FunctionPassT>
892ModuleToFunctionPassAdaptor
894 bool EagerlyInvalidate = false) {
895 using PassModelT =
897 return ModuleToFunctionPassAdaptor(PassModelT::create(std::move(Pass)),
898 EagerlyInvalidate);
899}
900
901/// A utility pass template to force an analysis result to be available.
902///
903/// If there are extra arguments at the pass's run level there may also be
904/// extra arguments to the analysis manager's \c getResult routine. We can't
905/// guess how to effectively map the arguments from one to the other, and so
906/// this specialization just ignores them.
907///
908/// Specific patterns of run-method extra arguments and analysis manager extra
909/// arguments will have to be defined as appropriate specializations.
910template <typename AnalysisT, typename IRUnitT,
911 typename AnalysisManagerT = AnalysisManager<IRUnitT>,
912 typename... ExtraArgTs>
914 : RequiredPassInfoMixin<RequireAnalysisPass<
915 AnalysisT, IRUnitT, AnalysisManagerT, ExtraArgTs...>> {
916 /// Run this pass over some unit of IR.
917 ///
918 /// This pass can be run over any unit of IR and use any analysis manager
919 /// provided they satisfy the basic API requirements. When this pass is
920 /// created, these methods can be instantiated to satisfy whatever the
921 /// context requires.
922 PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM,
923 ExtraArgTs &&... Args) {
924 (void)AM.template getResult<AnalysisT>(Arg,
925 std::forward<ExtraArgTs>(Args)...);
926
927 return PreservedAnalyses::all();
928 }
930 function_ref<StringRef(StringRef)> MapClassName2PassName) {
931 auto ClassName = AnalysisT::name();
932 auto PassName = MapClassName2PassName(ClassName);
933 OS << "require<" << PassName << '>';
934 }
935};
936
937/// A no-op pass template which simply forces a specific analysis result
938/// to be invalidated.
939template <typename AnalysisT>
941 : RequiredPassInfoMixin<InvalidateAnalysisPass<AnalysisT>> {
942 /// Run this pass over some unit of IR.
943 ///
944 /// This pass can be run over any unit of IR and use any analysis manager,
945 /// provided they satisfy the basic API requirements. When this pass is
946 /// created, these methods can be instantiated to satisfy whatever the
947 /// context requires.
948 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
949 PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&...) {
950 auto PA = PreservedAnalyses::all();
951 PA.abandon<AnalysisT>();
952 return PA;
953 }
955 function_ref<StringRef(StringRef)> MapClassName2PassName) {
956 auto ClassName = AnalysisT::name();
957 auto PassName = MapClassName2PassName(ClassName);
958 OS << "invalidate<" << PassName << '>';
959 }
960};
961
962/// A utility pass that does nothing, but preserves no analyses.
963///
964/// Because this preserves no analyses, any analysis passes queried after this
965/// pass runs will recompute fresh results.
967 : OptionalPassInfoMixin<InvalidateAllAnalysesPass> {
968 /// Run this pass over some unit of IR.
969 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
970 PreservedAnalyses run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) {
972 }
973};
974
975} // end namespace llvm
976
977#endif // LLVM_IR_PASSMANAGER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Function Alias Analysis Results
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
#define LLVM_TEMPLATE_ABI
Definition Compiler.h:216
#define LLVM_ATTRIBUTE_MINSIZE
Definition Compiler.h:336
#define LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
Definition Compiler.h:126
This file defines the DenseMap class.
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:85
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
#define P(N)
This header provides internal APIs and implementation details used by the pass management interfaces ...
This file contains some templates that are useful if you are working with the STL at all.
This file contains some functions that are useful when dealing with strings.
static const char PassName[]
Value * RHS
API to communicate dependencies between analyses during invalidation.
bool invalidate(AnalysisKey *ID, IRUnitT &IR, const PreservedAnalyses &PA)
A type-erased variant of the above invalidate method with the same core API other than passing an ana...
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
A container for analyses that lazily runs them and caches their results.
bool isPassRegistered() const
Returns true if the specified analysis pass is registered.
AnalysisManager()
Construct an empty analysis manager.
void clear()
Clear all analysis results cached by this AnalysisManager.
AnalysisManager(AnalysisManager &&)
void verifyNotInvalidated(IRUnitT &IR, typename PassT::Result *Result) const
Verify that the given Result cannot be invalidated, assert otherwise.
AnalysisManager & operator=(AnalysisManager &&)
void clear(IRUnitT &IR, llvm::StringRef Name)
Clear any cached analysis results for a single unit of IR.
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
void clearAnalysis(IRUnitT &IR)
Directly clear a cached analysis for an IR unit.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
bool registerPass(PassBuilderT &&PassBuilder)
Register an analysis pass with the manager.
bool empty() const
Returns true if the analysis manager has an empty results cache.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:223
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:133
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:134
iterator end()
Definition DenseMap.h:141
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA, typename AnalysisManager< IRUnitT, ExtraArgTs... >::Invalidator &Inv)
Handler for invalidation of the outer IR unit, IRUnitT.
Result(AnalysisManagerT &InnerAM)
AnalysisManagerT & getManager()
Accessor for the analysis manager.
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Result run(IRUnitT &IR, AnalysisManager< IRUnitT, ExtraArgTs... > &AM, ExtraArgTs...)
Run the analysis pass and create our proxy result object.
InnerAnalysisManagerProxy(AnalysisManagerT &InnerAM)
A helper class to return the specified delimiter string after the first invocation of operator String...
Trivial adaptor that maps from a module to its functions.
detail::PassConcept< Function, FunctionAnalysisManager > PassConceptT
ModuleToFunctionPassAdaptor(PassConceptT::unique_ptr Pass, bool EagerlyInvalidate)
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Result(const AnalysisManagerT &OuterAM)
PassT::Result * getCachedResult(IRUnitTParam &IR) const
Get a cached analysis.
bool invalidate(IRUnitT &IRUnit, const PreservedAnalyses &PA, typename AnalysisManager< IRUnitT, ExtraArgTs... >::Invalidator &Inv)
When invalidation occurs, remove any registered invalidation events.
bool cachedResultExists(IRUnitTParam &IR) const
Method provided for unit testing, not intended for general use.
const SmallDenseMap< AnalysisKey *, TinyPtrVector< AnalysisKey * >, 2 > & getOuterInvalidations() const
Access the map from outer analyses to deferred invalidation requiring analyses.
void registerOuterAnalysisInvalidation()
Register a deferred invalidation event for when the outer analysis manager processes its invalidation...
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Result run(IRUnitT &, AnalysisManager< IRUnitT, ExtraArgTs... > &, ExtraArgTs...)
Run the analysis pass and create our proxy result object.
OuterAnalysisManagerProxy(const AnalysisManagerT &OuterAM)
This class provides access to building LLVM's passes.
Manages a sequence of passes over a particular unit of IR.
PassManager(PassManager &&Arg)
PassManager & operator=(PassManager &&RHS)
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same_v< PassT, PassManager > > addPass(PassT &&Pass)
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
PassManager()=default
Construct a pass manager.
detail::PassConcept< LazyCallGraph::SCC, CGSCCAnalysisManager, ExtraArgTs... > PassConceptT
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t< std::is_same_v< PassT, PassManager > > addPass(PassT &&Pass)
When adding a pass manager pass that has the same type as this pass manager, simply move the passes o...
PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs)
Run all of the passes in this manager over the given unit of IR.
bool isEmpty() const
Returns if the pass manager contains any passes.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & abandon()
Mark an analysis as abandoned.
Definition Analysis.h:171
iterator erase(const_iterator CI)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Abstract concept of an analysis pass.
Wrapper to model the analysis pass concept.
Template for the abstract base class used to dispatch over pass objects.
A template wrapper used to implement PassConcept.
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Pass manager infrastructure for declaring and invalidating analyses.
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
Definition ADL.h:123
PassT::Result getAnalysisResultUnpackTuple(AnalysisManagerT &AM, IRUnitT &IR, std::tuple< ArgTs... > Args, std::index_sequence< Ns... >)
Actual unpacker of extra arguments in getAnalysisResult, passes only those tuple arguments that are m...
PassT::Result getAnalysisResult(AnalysisManager< IRUnitT, AnalysisArgTs... > &AM, IRUnitT &IR, std::tuple< MainArgTs... > Args)
Helper for partial unpacking of extra arguments in getAnalysisResult.
This is an optimization pass for GlobalISel generic memory operations.
ModuleToFunctionPassAdaptor createModuleToFunctionPassAdaptor(FunctionPassT &&Pass, bool EagerlyInvalidate=false)
A function to deduce a function pass type and wrap it in the templated adaptor.
OuterAnalysisManagerProxy< ModuleAnalysisManager, Function > ModuleAnalysisManagerFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
LLVM_GET_TYPE_NAME_CONSTEXPR StringRef getTypeName()
We provide a function which tries to compute the (demangled) name of a type statically.
Definition TypeName.h:42
LLVM_ABI void printIRUnitNameForStackTrace< Function >(raw_ostream &OS, const Function &IR)
void printIRUnitNameForStackTrace(raw_ostream &OS, const IRUnitT &IR)
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
LLVM_ABI void printIRUnitNameForStackTrace< Module >(raw_ostream &OS, const Module &IR)
PassManager< Module > ModulePassManager
Convenience typedef for a pass manager over modules.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
PassManager< Function > FunctionPassManager
Convenience typedef for a pass manager over functions.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1772
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2192
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
A CRTP mix-in that provides informational APIs needed for analysis passes.
static AnalysisKey * ID()
Returns an opaque, unique ID for this analysis type.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
A utility pass that does nothing, but preserves no analyses.
PreservedAnalyses run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...)
Run this pass over some unit of IR.
A no-op pass template which simply forces a specific analysis result to be invalidated.
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&...)
Run this pass over some unit of IR.
A CRTP mix-in for passes that can be skipped.
A utility pass template to force an analysis result to be available.
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&... Args)
Run this pass over some unit of IR.
A CRTP mix-in for passes that should not be skipped.
Abstract concept of an analysis result.
Wrapper to model the analysis result concept.
static StringRef name()
Gets the name of the pass we are mixed into.
Definition PassManager.h:63
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:81
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition PassManager.h:82