LLVM 17.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
12#include <optional>
14using namespace llvm;
16namespace llvm {
17// Explicit template instantiations and specialization defininitions for core
18// template typedefs.
19template class AllAnalysesOn<Module>;
20template class AllAnalysesOn<Function>;
21template class PassManager<Module>;
22template class PassManager<Function>;
23template class AnalysisManager<Module>;
24template class AnalysisManager<Function>;
28template <>
29bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
30 Module &M, const PreservedAnalyses &PA,
32 // If literally everything is preserved, we're done.
33 if (PA.areAllPreserved())
34 return false; // This is still a valid proxy.
36 // If this proxy isn't marked as preserved, then even if the result remains
37 // valid, the key itself may no longer be valid, so we clear everything.
38 //
39 // Note that in order to preserve this proxy, a module pass must ensure that
40 // the FAM has been completely updated to handle the deletion of functions.
41 // Specifically, any FAM-cached results for those functions need to have been
42 // forcibly cleared. When preserved, this proxy will only invalidate results
43 // cached on functions *still in the module* at the end of the module pass.
45 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) {
46 InnerAM->clear();
47 return true;
48 }
50 // Directly check if the relevant set is preserved.
51 bool AreFunctionAnalysesPreserved =
54 // Now walk all the functions to see if any inner analysis invalidation is
55 // necessary.
56 for (Function &F : M) {
57 std::optional<PreservedAnalyses> FunctionPA;
59 // Check to see whether the preserved set needs to be pruned based on
60 // module-level analysis invalidation that triggers deferred invalidation
61 // registered with the outer analysis manager proxy for this function.
62 if (auto *OuterProxy =
63 InnerAM->getCachedResult<ModuleAnalysisManagerFunctionProxy>(F))
64 for (const auto &OuterInvalidationPair :
65 OuterProxy->getOuterInvalidations()) {
66 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
67 const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
68 if (Inv.invalidate(OuterAnalysisID, M, PA)) {
69 if (!FunctionPA)
70 FunctionPA = PA;
71 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
72 FunctionPA->abandon(InnerAnalysisID);
73 }
74 }
76 // Check if we needed a custom PA set, and if so we'll need to run the
77 // inner invalidation.
78 if (FunctionPA) {
79 InnerAM->invalidate(F, *FunctionPA);
80 continue;
81 }
83 // Otherwise we only need to do invalidation if the original PA set didn't
84 // preserve all function analyses.
85 if (!AreFunctionAnalysesPreserved)
86 InnerAM->invalidate(F, PA);
87 }
89 // Return false to indicate that this result is still a valid proxy.
90 return false;
92} // namespace llvm
95 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
96 OS << "function";
97 if (EagerlyInvalidate)
98 OS << "<eager-inv>";
99 OS << '(';
100 Pass->printPipeline(OS, MapClassName2PassName);
101 OS << ')';
109 // Request PassInstrumentation from analysis manager, will use it to run
110 // instrumenting callbacks for the passes later.
114 for (Function &F : M) {
115 if (F.isDeclaration())
116 continue;
118 // Check the PassInstrumentation's BeforePass callbacks before running the
119 // pass, skip its execution completely if asked to (callback returns
120 // false).
121 if (!PI.runBeforePass<Function>(*Pass, F))
122 continue;
124 PreservedAnalyses PassPA = Pass->run(F, FAM);
126 // We know that the function pass couldn't have invalidated any other
127 // function's analyses (that's the contract of a function pass), so
128 // directly handle the function analysis manager's invalidation here.
129 FAM.invalidate(F, EagerlyInvalidate ? PreservedAnalyses::none() : PassPA);
131 PI.runAfterPass(*Pass, F, PassPA);
133 // Then intersect the preserved set so that invalidation of module
134 // analyses will eventually occur when the module pass completes.
135 PA.intersect(std::move(PassPA));
136 }
138 // The FunctionAnalysisManagerModuleProxy is preserved because (we assume)
139 // the function passes we ran didn't add or remove any functions.
140 //
141 // We also preserve all analyses on Functions, because we did all the
142 // invalidation we needed to do above.
143 PA.preserveSet<AllAnalysesOn<Function>>();
145 return PA;
148AnalysisSetKey CFGAnalyses::SetKey;
150AnalysisSetKey PreservedAnalyses::AllAnalysesKey;
This file defines DenseMapInfo traits for DenseMap.
#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: PassManager.h:90
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:661
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:679
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
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:774
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:933
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:94
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:1058
Pseudo-analysis pass that exposes the PassInstrumentation to pass managers.
Definition: PassManager.h:595
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:470
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
bool areAllPreserved() const
Test whether all analyses are preserved (and none are abandoned).
Definition: PassManager.h:327
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
bool allAnalysesInSetPreserved() const
Directly test whether a set of analyses is preserved.
Definition: PassManager.h:335
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: PassManager.h:310
void abandon()
Mark an analysis as abandoned.
Definition: PassManager.h:206
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: PassManager.h:69
A special type used to provide an address that identifies a set of related analyses.
Definition: PassManager.h:79