LLVM 24.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.
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
115 llvm::StringRef Name) {
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 &
132AnalysisManager<IRUnitT, ExtraArgTs...>::getResultImpl(
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(
149 ID, P.run(IR, *this, std::forward<ExtraArgTs>(ExtraArgs)...));
150
151 PI.runAfterAnalysis(P, IR);
152
153 // P.run may have inserted elements into AnalysisResults and invalidated
154 // RI.
155 RI = AnalysisResults.find({ID, &IR});
156 assert(RI != AnalysisResults.end() && "we just inserted it!");
157
158 RI->second = std::prev(ResultList.end());
159 }
160
161 return *RI->second->second;
162}
163
164template <typename IRUnitT, typename... ExtraArgTs>
166 IRUnitT &IR, const PreservedAnalyses &PA) {
167 // We're done if all analyses on this IR unit are preserved.
169 return;
170
171 // Track whether each analysis's result is invalidated in
172 // IsResultInvalidated.
173 SmallDenseMap<AnalysisKey *, bool, 8> IsResultInvalidated;
174 Invalidator Inv(IsResultInvalidated, AnalysisResults);
175 AnalysisResultListT &ResultsList = AnalysisResultLists[&IR];
176 for (auto &AnalysisResultPair : ResultsList) {
177 // This is basically the same thing as Invalidator::invalidate, but we
178 // can't call it here because we're operating on the type-erased result.
179 // Moreover if we instead called invalidate() directly, it would do an
180 // unnecessary look up in ResultsList.
181 AnalysisKey *ID = AnalysisResultPair.first;
182 auto &Result = *AnalysisResultPair.second;
183
184 auto IMapI = IsResultInvalidated.find(ID);
185 if (IMapI != IsResultInvalidated.end())
186 // This result was already handled via the Invalidator.
187 continue;
188
189 // Try to invalidate the result, giving it the Invalidator so it can
190 // recursively query for any dependencies it has and record the result.
191 // Note that we cannot reuse 'IMapI' here or pre-insert the ID, as
192 // Result.invalidate may insert things into the map, invalidating our
193 // iterator.
194 bool Inserted =
195 IsResultInvalidated.insert({ID, Result.invalidate(IR, PA, Inv)}).second;
196 (void)Inserted;
197 assert(Inserted && "Should never have already inserted this ID, likely "
198 "indicates a cycle!");
199 }
200
201 // Now erase the results that were marked above as invalidated.
202 if (!IsResultInvalidated.empty()) {
203 for (auto I = ResultsList.begin(), E = ResultsList.end(); I != E;) {
204 AnalysisKey *ID = I->first;
205 if (!IsResultInvalidated.lookup(ID)) {
206 ++I;
207 continue;
208 }
209
211 PI->runAnalysisInvalidated(this->lookUpPass(ID), IR);
212
213 I = ResultsList.erase(I);
214 AnalysisResults.erase({ID, &IR});
215 }
216 }
217
218 if (ResultsList.empty())
219 AnalysisResultLists.erase(&IR);
220}
221} // end namespace llvm
222
223#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")
This header defines various interfaces for pass management in LLVM.
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:81
#define I(x, y, z)
Definition MD5.cpp:57
#define P(N)
This file defines the Pass Instrumentation classes that provide instrumentation points into the pass ...
static const char PassName[]
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
API to communicate dependencies between analyses during invalidation.
A container for analyses that lazily runs them and caches their results.
AnalysisManager()
Construct an empty analysis manager.
void clear()
Clear all analysis results cached by this AnalysisManager.
AnalysisManager & operator=(AnalysisManager &&)
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:250
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:223
bool empty() const
Definition DenseMap.h:171
iterator end()
Definition DenseMap.h:141
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:284
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...
detail::PassConcept< IRUnitT, AnalysisManagerT, ExtraArgTs... > PassConceptT
std::vector< typename PassConceptT::unique_ptr > Passes
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...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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
PassT::Result getAnalysisResult(AnalysisManager< IRUnitT, AnalysisArgTs... > &AM, IRUnitT &IR, std::tuple< MainArgTs... > Args)
Helper for partial unpacking of extra arguments in getAnalysisResult.
This is an optimization pass for GlobalISel generic memory operations.
void printIRUnitNameForStackTrace(raw_ostream &OS, const IRUnitT &IR)
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29