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 to the results in the 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] =
135 AnalysisResults.try_emplace(std::make_pair(ID, &IR), nullptr);
136
137 // If we don't have a cached result for this function, look up the pass and
138 // run it to produce a result, which we then add to the cache.
139 if (Inserted) {
140 auto &P = this->lookUpPass(ID);
141
144 PI = getResult<PassInstrumentationAnalysis>(IR, ExtraArgs...);
146 }
147
148 // Run the analysis first: running it can recursively cache another
149 // result and rehash the map.
150 auto Result = P.run(IR, *this, std::forward<ExtraArgTs>(ExtraArgs)...);
151 ResultConceptT *ResultPtr = Result.get();
152 AnalysisResultListT &ResultList = AnalysisResultLists[&IR];
153 ResultList.emplace_back(ID, std::move(Result));
154
155 PI.runAfterAnalysis(P, IR);
156
157 // P.run may have inserted elements into AnalysisResults and invalidated
158 // RI.
159 RI = AnalysisResults.find({ID, &IR});
160 assert(RI != AnalysisResults.end() && "we just inserted it!");
161
162 RI->second = ResultPtr;
163 }
164
165 return *RI->second;
166}
167
168template <typename IRUnitT, typename... ExtraArgTs>
170 IRUnitT &IR, const PreservedAnalyses &PA) {
171 // We're done if all analyses on this IR unit are preserved.
173 return;
174
175 auto ResultsListI = AnalysisResultLists.find(&IR);
176 if (ResultsListI == AnalysisResultLists.end())
177 return;
178 AnalysisResultListT &ResultsList = ResultsListI->second;
179
180 // Track whether each result is invalidated, by its index in ResultsList. The
181 // Invalidator lets results recursively query their dependencies.
182 using ResultState = typename Invalidator::ResultState;
183 SmallVector<ResultState, 32> States(ResultsList.size(), ResultState::Unknown);
184 Invalidator Inv(IR, ResultsList, States);
185 bool AnyInvalidated = false;
186 for (size_t Idx = 0, E = ResultsList.size(); Idx != E; ++Idx)
187 AnyInvalidated |= Inv.invalidateResult(Idx, PA);
188
189 // Now erase the results that were marked above as invalidated.
190 if (AnyInvalidated) {
192 size_t WriteIdx = 0;
193 for (size_t ReadIdx = 0, E = ResultsList.size(); ReadIdx < E; ++ReadIdx) {
194 if (States[ReadIdx] != ResultState::Invalid) {
195 if (WriteIdx != ReadIdx)
196 ResultsList[WriteIdx] = std::move(ResultsList[ReadIdx]);
197 ++WriteIdx;
198 continue;
199 }
200
201 AnalysisKey *ID = ResultsList[ReadIdx].first;
202 if (PI)
203 PI->runAnalysisInvalidated(this->lookUpPass(ID), IR);
204
205 AnalysisResults.erase({ID, &IR});
206 }
207 ResultsList.resize(WriteIdx);
208 }
209
210 if (ResultsList.empty())
211 AnalysisResultLists.erase(&IR);
212}
213} // end namespace llvm
214
215#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:85
#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.
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:97
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:152
void intersect(const PreservedAnalyses &Arg)
Intersect this set with another in place.
Definition Analysis.h:194
PrettyStackTraceEntry - This class is used to represent a frame of the "pretty" stack trace that is d...
void resize(size_type N)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
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
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