LLVM 22.0.0git
MachinePassManager.cpp
Go to the documentation of this file.
1//===---------- MachinePassManager.cpp ------------------------------------===//
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//
9// This file contains the pass management machinery for machine functions.
10//
11//===----------------------------------------------------------------------===//
12
17#include "llvm/IR/Function.h"
18#include "llvm/IR/Module.h"
21
22using namespace llvm;
23
25
26namespace llvm {
28template class PassManager<MachineFunction>;
29template class LLVM_EXPORT_TEMPLATE
31template class LLVM_EXPORT_TEMPLATE
33template class LLVM_EXPORT_TEMPLATE
35} // namespace llvm
36
40 // MachineFunction passes should not invalidate Function analyses.
41 // TODO: verify that PA doesn't invalidate Function analyses.
42 return false;
43}
44
45template <>
46bool MachineFunctionAnalysisManagerModuleProxy::Result::invalidate(
47 Module &M, const PreservedAnalyses &PA,
49 // If literally everything is preserved, we're done.
50 if (PA.areAllPreserved())
51 return false; // This is still a valid proxy.
52
53 // If this proxy isn't marked as preserved, then even if the result remains
54 // valid, the key itself may no longer be valid, so we clear everything.
55 //
56 // Note that in order to preserve this proxy, a module pass must ensure that
57 // the MFAM has been completely updated to handle the deletion of functions.
58 // Specifically, any MFAM-cached results for those functions need to have been
59 // forcibly cleared. When preserved, this proxy will only invalidate results
60 // cached on functions *still in the module* at the end of the module pass.
62 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) {
63 InnerAM->clear();
64 return true;
65 }
66
67 // FIXME: be more precise, see
68 // FunctionAnalysisManagerModuleProxy::Result::invalidate.
70 InnerAM->clear();
71 return true;
72 }
73
74 // Return false to indicate that this result is still a valid proxy.
75 return false;
76}
77
78template <>
79bool MachineFunctionAnalysisManagerFunctionProxy::Result::invalidate(
80 Function &F, const PreservedAnalyses &PA,
82 // If literally everything is preserved, we're done.
83 if (PA.areAllPreserved())
84 return false; // This is still a valid proxy.
85
86 // If this proxy isn't marked as preserved, then even if the result remains
87 // valid, the key itself may no longer be valid, so we clear everything.
88 //
89 // Note that in order to preserve this proxy, a module pass must ensure that
90 // the MFAM has been completely updated to handle the deletion of functions.
91 // Specifically, any MFAM-cached results for those functions need to have been
92 // forcibly cleared. When preserved, this proxy will only invalidate results
93 // cached on functions *still in the module* at the end of the module pass.
95 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>()) {
96 InnerAM->clear();
97 return true;
98 }
99
100 // FIXME: be more precise, see
101 // FunctionAnalysisManagerModuleProxy::Result::invalidate.
103 InnerAM->clear();
104 return true;
105 }
106
107 // Return false to indicate that this result is still a valid proxy.
108 return false;
109}
110
116 .getManager();
119 // Do not codegen any 'available_externally' functions at all, they have
120 // definitions outside the translation unit.
121 if (F.isDeclaration() || F.hasAvailableExternallyLinkage())
122 return PreservedAnalyses::all();
123
125
126 if (!PI.runBeforePass<MachineFunction>(*Pass, MF))
127 return PreservedAnalyses::all();
128 PreservedAnalyses PassPA = Pass->run(MF, MFAM);
129 MFAM.invalidate(MF, PassPA);
130 PI.runAfterPass(*Pass, MF, PassPA);
131 PA.intersect(std::move(PassPA));
132
133 return PA;
134}
135
137 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
138 OS << "machine-function(";
139 Pass->printPipeline(OS, MapClassName2PassName);
140 OS << ')';
141}
142
143template <>
149 for (auto &Pass : Passes) {
150 if (!PI.runBeforePass<MachineFunction>(*Pass, MF))
151 continue;
152
153 PreservedAnalyses PassPA = Pass->run(MF, MFAM);
154 MFAM.invalidate(MF, PassPA);
155 PI.runAfterPass(*Pass, MF, PassPA);
156 PA.intersect(std::move(PassPA));
157 }
158 PA.preserveSet<AllAnalysesOn<MachineFunction>>();
159 return PA;
160}
161
164 // Machine function passes are not allowed to modify the LLVM
165 // representation, therefore we should preserve all IR analyses.
166 PA.template preserveSet<AllAnalysesOn<Module>>();
167 PA.template preserveSet<AllAnalysesOn<Function>>();
168 return PA;
169}
#define LLVM_EXPORT_TEMPLATE
Definition: Compiler.h:215
Module.h This file contains the declarations for the Module class.
Legalize the Machine IR a function s Machine IR
Definition: Legalizer.cpp:80
#define F(x, y, z)
Definition: MD5.cpp:55
Function const char * Passes
FunctionAnalysisManager FAM
Provides implementations for PassManager and AnalysisManager template methods.
raw_pwrite_stream & OS
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
Definition: Analysis.h:50
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:294
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
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:412
LLVM_ABI bool invalidate(MachineFunction &IR, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &Inv)
Handler for invalidation of the outer IR unit, IRUnitT.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Runs the function pass across every function in the function.
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:585
This analysis create MachineFunction for given Function.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:716
Pseudo-analysis pass that exposes the PassInstrumentation to pass managers.
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:163
PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs)
Run all of the passes in this manager over the given unit of IR.
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
bool areAllPreserved() const
Test whether all analyses are preserved (and none are abandoned).
Definition: Analysis.h:292
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:118
bool allAnalysesInSetPreserved() const
Directly test whether a set of analyses is preserved.
Definition: Analysis.h:300
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: Analysis.h:275
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
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
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29