LLVM 22.0.0git
PassManagerImpl.h
Go to the documentation of this file.
1//===- PassManagerImpl.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/// Provides implementations for PassManager and AnalysisManager template
10/// methods. These classes should be explicitly instantiated for any IR unit,
11/// and files doing the explicit instantiation should include this header.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_IR_PASSMANAGERIMPL_H
16#define LLVM_IR_PASSMANAGERIMPL_H
17
18#include "llvm/IR/Function.h"
20#include "llvm/IR/PassManager.h"
24
25namespace llvm {
26
27template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
29 IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs) {
30 class StackTraceEntry : public PrettyStackTraceEntry {
31 const PassInstrumentation &PI;
32 IRUnitT &IR;
33 PassConceptT *Pass = nullptr;
34
35 public:
36 explicit StackTraceEntry(const PassInstrumentation &PI, IRUnitT &IR)
37 : PI(PI), IR(IR) {}
38
39 void setPass(PassConceptT *P) { Pass = P; }
40
41 void print(raw_ostream &OS) const override {
42 OS << "Running pass \"";
43 if (Pass)
44 Pass->printPipeline(OS, [this](StringRef ClassName) {
45 auto PassName = PI.getPassNameForClassName(ClassName);
46 return PassName.empty() ? ClassName : PassName;
47 });
48 else
49 OS << "unknown";
50 OS << "\" on ";
52 OS << "\n";
53 }
54 };
55
57
58 // Request PassInstrumentation from analysis manager, will use it to run
59 // instrumenting callbacks for the passes later.
60 // Here we use std::tuple wrapper over getResult which helps to extract
61 // AnalysisManager's arguments out of the whole ExtraArgs set.
63 detail::getAnalysisResult<PassInstrumentationAnalysis>(
64 AM, IR, std::tuple<ExtraArgTs...>(ExtraArgs...));
65
66 StackTraceEntry Entry(PI, IR);
67 for (auto &Pass : Passes) {
68 Entry.setPass(&*Pass);
69
70 // Check the PassInstrumentation's BeforePass callbacks before running the
71 // pass, skip its execution completely if asked to (callback returns
72 // false).
73 if (!PI.runBeforePass<IRUnitT>(*Pass, IR))
74 continue;
75
76 PreservedAnalyses PassPA = Pass->run(IR, AM, ExtraArgs...);
77
78 // Update the analysis manager as each pass runs and potentially
79 // invalidates analyses.
80 AM.invalidate(IR, PassPA);
81
82 // Call onto PassInstrumentation's AfterPass callbacks immediately after
83 // running the pass.
84 PI.runAfterPass<IRUnitT>(*Pass, IR, PassPA);
85
86 // Finally, intersect the preserved analyses to compute the aggregate
87 // preserved set for this pass manager.
88 PA.intersect(std::move(PassPA));
89 }
90
91 // Invalidation was handled after each pass in the above loop for the
92 // current unit of IR. Therefore, the remaining analysis results in the
93 // AnalysisManager are preserved. We mark this with a set so that we don't
94 // need to inspect each one individually.
96
97 return PA;
98}
99
100template <typename IRUnitT, typename... ExtraArgTs>
102
103template <typename IRUnitT, typename... ExtraArgTs>
105 AnalysisManager &&) = default;
106
107template <typename IRUnitT, typename... ExtraArgTs>
108inline AnalysisManager<IRUnitT, ExtraArgTs...> &
110 default;
111
112template <typename IRUnitT, typename... ExtraArgTs>
113inline void
116 if (auto *PI = getCachedResult<PassInstrumentationAnalysis>(IR))
117 PI->runAnalysesCleared(Name);
118
119 auto ResultsListI = AnalysisResultLists.find(&IR);
120 if (ResultsListI == AnalysisResultLists.end())
121 return;
122 // Delete the map entries that point into the results list.
123 for (auto &IDAndResult : ResultsListI->second)
124 AnalysisResults.erase({IDAndResult.first, &IR});
125
126 // And actually destroy and erase the results associated with this IR.
127 AnalysisResultLists.erase(ResultsListI);
128}
129
130template <typename IRUnitT, typename... ExtraArgTs>
131inline typename AnalysisManager<IRUnitT, ExtraArgTs...>::ResultConceptT &
133 AnalysisKey *ID, IRUnitT &IR, ExtraArgTs... ExtraArgs) {
134 auto [RI, Inserted] = AnalysisResults.try_emplace(std::make_pair(ID, &IR));
135
136 // If we don't have a cached result for this function, look up the pass and
137 // run it to produce a result, which we then add to the cache.
138 if (Inserted) {
139 auto &P = this->lookUpPass(ID);
140
143 PI = getResult<PassInstrumentationAnalysis>(IR, ExtraArgs...);
145 }
146
147 AnalysisResultListT &ResultList = AnalysisResultLists[&IR];
148 ResultList.emplace_back(ID, P.run(IR, *this, ExtraArgs...));
149
150 PI.runAfterAnalysis(P, IR);
151
152 // P.run may have inserted elements into AnalysisResults and invalidated
153 // RI.
154 RI = AnalysisResults.find({ID, &IR});
155 assert(RI != AnalysisResults.end() && "we just inserted it!");
156
157 RI->second = std::prev(ResultList.end());
158 }
159
160 return *RI->second->second;
161}
162
163template <typename IRUnitT, typename... ExtraArgTs>
165 IRUnitT &IR, const PreservedAnalyses &PA) {
166 // We're done if all analyses on this IR unit are preserved.
168 return;
169
170 // Track whether each analysis's result is invalidated in
171 // IsResultInvalidated.
172 SmallDenseMap<AnalysisKey *, bool, 8> IsResultInvalidated;
173 Invalidator Inv(IsResultInvalidated, AnalysisResults);
174 AnalysisResultListT &ResultsList = AnalysisResultLists[&IR];
175 for (auto &AnalysisResultPair : ResultsList) {
176 // This is basically the same thing as Invalidator::invalidate, but we
177 // can't call it here because we're operating on the type-erased result.
178 // Moreover if we instead called invalidate() directly, it would do an
179 // unnecessary look up in ResultsList.
180 AnalysisKey *ID = AnalysisResultPair.first;
181 auto &Result = *AnalysisResultPair.second;
182
183 auto IMapI = IsResultInvalidated.find(ID);
184 if (IMapI != IsResultInvalidated.end())
185 // This result was already handled via the Invalidator.
186 continue;
187
188 // Try to invalidate the result, giving it the Invalidator so it can
189 // recursively query for any dependencies it has and record the result.
190 // Note that we cannot reuse 'IMapI' here or pre-insert the ID, as
191 // Result.invalidate may insert things into the map, invalidating our
192 // iterator.
193 bool Inserted =
194 IsResultInvalidated.insert({ID, Result.invalidate(IR, PA, Inv)}).second;
195 (void)Inserted;
196 assert(Inserted && "Should never have already inserted this ID, likely "
197 "indicates a cycle!");
198 }
199
200 // Now erase the results that were marked above as invalidated.
201 if (!IsResultInvalidated.empty()) {
202 for (auto I = ResultsList.begin(), E = ResultsList.end(); I != E;) {
203 AnalysisKey *ID = I->first;
204 if (!IsResultInvalidated.lookup(ID)) {
205 ++I;
206 continue;
207 }
208
209 if (auto *PI = getCachedResult<PassInstrumentationAnalysis>(IR))
210 PI->runAnalysisInvalidated(this->lookUpPass(ID), IR);
211
212 I = ResultsList.erase(I);
213 AnalysisResults.erase({ID, &IR});
214 }
215 }
216
217 if (ResultsList.empty())
218 AnalysisResultLists.erase(&IR);
219}
220} // end namespace llvm
221
222#endif // LLVM_IR_PASSMANAGERIMPL_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::string Name
This header defines various interfaces for pass management in LLVM.
Legalize the Machine IR a function s Machine IR
Definition: Legalizer.cpp:80
#define I(x, y, z)
Definition: MD5.cpp:58
#define P(N)
Function const char * Passes
This file defines the Pass Instrumentation classes that provide instrumentation points into the pass ...
raw_pwrite_stream & OS
static const char PassName[]
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
AnalysisManager()
Construct an empty analysis manager.
void clear()
Clear all analysis results cached by this AnalysisManager.
Definition: PassManager.h:398
AnalysisManager & operator=(AnalysisManager &&)
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:187
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:165
bool empty() const
Definition: DenseMap.h:107
iterator end()
Definition: DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:214
This class provides instrumentation entry points for the Pass Manager, doing calls to callbacks regis...
void runBeforeAnalysis(const PassT &Analysis, const IRUnitT &IR) const
BeforeAnalysis instrumentation point - takes Analysis instance to be executed and constant reference ...
void runAnalysisInvalidated(const PassT &Analysis, const IRUnitT &IR) const
AnalysisInvalidated instrumentation point - takes Analysis instance that has just been invalidated an...
StringRef getPassNameForClassName(StringRef ClassName) const
Get the pass name for a given pass class name.
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...
void runAfterAnalysis(const PassT &Analysis, const IRUnitT &IR) const
AfterAnalysis instrumentation point - takes Analysis instance that has just been executed and constan...
bool runBeforePass(const PassT &Pass, const IRUnitT &IR) const
BeforePass instrumentation point - takes Pass instance to be executed and constant reference to IR it...
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
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
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:151
void intersect(const PreservedAnalyses &Arg)
Intersect this set with another in place.
Definition: Analysis.h:193
PrettyStackTraceEntry - This class is used to represent a frame of the "pretty" stack trace that is d...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void printIRUnitNameForStackTrace(raw_ostream &OS, const IRUnitT &IR)
static AnalysisKey * ID()
Returns an opaque, unique ID for this analysis type.
Definition: PassManager.h:109
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29