LLVM 19.0.0git
Go to the documentation of this file.
1//===- PassManager.cpp - Infrastructure for managing & running IR passes --===//
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
11#include <optional>
13using namespace llvm;
15namespace llvm {
16// Explicit template instantiations and specialization defininitions for core
17// template typedefs.
18template class AllAnalysesOn<Module>;
19template class AllAnalysesOn<Function>;
20template class PassManager<Module>;
21template class PassManager<Function>;
22template class AnalysisManager<Module>;
23template class AnalysisManager<Function>;
27template <>
28bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
29 Module &M, const PreservedAnalyses &PA,
31 // If literally everything is preserved, we're done.
32 if (PA.areAllPreserved())
33 return false; // This is still a valid proxy.
35 // If this proxy isn't marked as preserved, then even if the result remains
36 // valid, the key itself may no longer be valid, so we clear everything.
37 //
38 // Note that in order to preserve this proxy, a module pass must ensure that
39 // the FAM has been completely updated to handle the deletion of functions.
40 // Specifically, any FAM-cached results for those functions need to have been
41 // forcibly cleared. When preserved, this proxy will only invalidate results
42 // cached on functions *still in the module* at the end of the module pass.
44 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) {
45 InnerAM->clear();
46 return true;
47 }
49 // Directly check if the relevant set is preserved.
50 bool AreFunctionAnalysesPreserved =
53 // Now walk all the functions to see if any inner analysis invalidation is
54 // necessary.
55 for (Function &F : M) {
56 std::optional<PreservedAnalyses> FunctionPA;
58 // Check to see whether the preserved set needs to be pruned based on
59 // module-level analysis invalidation that triggers deferred invalidation
60 // registered with the outer analysis manager proxy for this function.
61 if (auto *OuterProxy =
62 InnerAM->getCachedResult<ModuleAnalysisManagerFunctionProxy>(F))
63 for (const auto &OuterInvalidationPair :
64 OuterProxy->getOuterInvalidations()) {
65 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
66 const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
67 if (Inv.invalidate(OuterAnalysisID, M, PA)) {
68 if (!FunctionPA)
69 FunctionPA = PA;
70 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
71 FunctionPA->abandon(InnerAnalysisID);
72 }
73 }
75 // Check if we needed a custom PA set, and if so we'll need to run the
76 // inner invalidation.
77 if (FunctionPA) {
78 InnerAM->invalidate(F, *FunctionPA);
79 continue;
80 }
82 // Otherwise we only need to do invalidation if the original PA set didn't
83 // preserve all function analyses.
84 if (!AreFunctionAnalysesPreserved)
85 InnerAM->invalidate(F, PA);
86 }
88 // Return false to indicate that this result is still a valid proxy.
89 return false;
91} // namespace llvm
94 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
95 OS << "function";
96 if (EagerlyInvalidate)
97 OS << "<eager-inv>";
98 OS << '(';
99 Pass->printPipeline(OS, MapClassName2PassName);
100 OS << ')';
108 // Request PassInstrumentation from analysis manager, will use it to run
109 // instrumenting callbacks for the passes later.
113 for (Function &F : M) {
114 if (F.isDeclaration())
115 continue;
117 // Check the PassInstrumentation's BeforePass callbacks before running the
118 // pass, skip its execution completely if asked to (callback returns
119 // false).
120 if (!PI.runBeforePass<Function>(*Pass, F))
121 continue;
123 PreservedAnalyses PassPA = Pass->run(F, FAM);
125 // We know that the function pass couldn't have invalidated any other
126 // function's analyses (that's the contract of a function pass), so
127 // directly handle the function analysis manager's invalidation here.
128 FAM.invalidate(F, EagerlyInvalidate ? PreservedAnalyses::none() : PassPA);
130 PI.runAfterPass(*Pass, F, PassPA);
132 // Then intersect the preserved set so that invalidation of module
133 // analyses will eventually occur when the module pass completes.
134 PA.intersect(std::move(PassPA));
135 }
137 // The FunctionAnalysisManagerModuleProxy is preserved because (we assume)
138 // the function passes we ran didn't add or remove any functions.
139 //
140 // We also preserve all analyses on Functions, because we did all the
141 // invalidation we needed to do above.
142 PA.preserveSet<AllAnalysesOn<Function>>();
144 return PA;
147AnalysisSetKey CFGAnalyses::SetKey;
149AnalysisSetKey PreservedAnalyses::AllAnalysesKey;
#define F(x, y, z)
Definition: MD5.cpp:55
FunctionAnalysisManager FAM
Provides implementations for PassManager and AnalysisManager template methods.
This header defines various interfaces for pass management in LLVM.
raw_pwrite_stream & OS
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
Definition: Analysis.h:47
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:360
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
Definition: PassManager.h:378
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:321
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:473
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:631
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Runs the function pass across every function in the module.
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition: PassManager.cpp:93
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:756
Pseudo-analysis pass that exposes the PassInstrumentation to pass managers.
Definition: PassManager.h:296
This class provides instrumentation entry points for the Pass Manager, doing calls to callbacks regis...
void runAfterPass(const PassT &Pass, const IRUnitT &IR, const PreservedAnalyses &PA) const
AfterPass instrumentation point - takes Pass instance that has just been executed and constant refere...
bool runBeforePass(const PassT &Pass, const IRUnitT &IR) const
BeforePass instrumentation point - takes Pass instance to be executed and constant reference to IR it...
Manages a sequence of passes over a particular unit of IR.
Definition: PassManager.h:173
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:112
bool areAllPreserved() const
Test whether all analyses are preserved (and none are abandoned).
Definition: Analysis.h:281
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
bool allAnalysesInSetPreserved() const
Directly test whether a set of analyses is preserved.
Definition: Analysis.h:289
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: Analysis.h:264
void abandon()
Mark an analysis as abandoned.
Definition: Analysis.h:162
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:26
A special type used to provide an address that identifies a set of related analyses.
Definition: Analysis.h:36