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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/PassManager.h"
10 #include "llvm/IR/PassManagerImpl.h"
16 // Explicit template instantiations and specialization defininitions for core
18 template class AllAnalysesOn
<Module
>;
19 template class AllAnalysesOn
<Function
>;
20 template class PassManager
<Module
>;
21 template class PassManager
<Function
>;
22 template class AnalysisManager
<Module
>;
23 template class AnalysisManager
<Function
>;
24 template class InnerAnalysisManagerProxy
<FunctionAnalysisManager
, Module
>;
25 template class OuterAnalysisManagerProxy
<ModuleAnalysisManager
, Function
>;
28 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
29 Module
&M
, const PreservedAnalyses
&PA
,
30 ModuleAnalysisManager::Invalidator
&Inv
) {
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.
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.
43 auto PAC
= PA
.getChecker
<FunctionAnalysisManagerModuleProxy
>();
44 if (!PAC
.preserved() && !PAC
.preservedSet
<AllAnalysesOn
<Module
>>()) {
49 // Directly check if the relevant set is preserved.
50 bool AreFunctionAnalysesPreserved
=
51 PA
.allAnalysesInSetPreserved
<AllAnalysesOn
<Function
>>();
53 // Now walk all the functions to see if any inner analysis invalidation is
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
)) {
70 for (AnalysisKey
*InnerAnalysisID
: InnerAnalysisIDs
)
71 FunctionPA
->abandon(InnerAnalysisID
);
75 // Check if we needed a custom PA set, and if so we'll need to run the
76 // inner invalidation.
78 InnerAM
->invalidate(F
, *FunctionPA
);
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
);
88 // Return false to indicate that this result is still a valid proxy.
93 void ModuleToFunctionPassAdaptor::printPipeline(
94 raw_ostream
&OS
, function_ref
<StringRef(StringRef
)> MapClassName2PassName
) {
96 if (EagerlyInvalidate
)
99 Pass
->printPipeline(OS
, MapClassName2PassName
);
103 PreservedAnalyses
ModuleToFunctionPassAdaptor::run(Module
&M
,
104 ModuleAnalysisManager
&AM
) {
105 FunctionAnalysisManager
&FAM
=
106 AM
.getResult
<FunctionAnalysisManagerModuleProxy
>(M
).getManager();
108 // Request PassInstrumentation from analysis manager, will use it to run
109 // instrumenting callbacks for the passes later.
110 PassInstrumentation PI
= AM
.getResult
<PassInstrumentationAnalysis
>(M
);
112 PreservedAnalyses PA
= PreservedAnalyses::all();
113 for (Function
&F
: M
) {
114 if (F
.isDeclaration())
117 // Check the PassInstrumentation's BeforePass callbacks before running the
118 // pass, skip its execution completely if asked to (callback returns
120 if (!PI
.runBeforePass
<Function
>(*Pass
, F
))
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
));
137 // The FunctionAnalysisManagerModuleProxy is preserved because (we assume)
138 // the function passes we ran didn't add or remove any functions.
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
>>();
143 PA
.preserve
<FunctionAnalysisManagerModuleProxy
>();
147 AnalysisSetKey
CFGAnalyses::SetKey
;
149 AnalysisSetKey
PreservedAnalyses::AllAnalysesKey
;