LLVM 24.0.0git
Analysis.h
Go to the documentation of this file.
1//===- Analysis.h --------------------------------------------*- 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/// Pass manager infrastructure for declaring and invalidating analyses.
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_IR_ANALYSIS_H
13#define LLVM_IR_ANALYSIS_H
14
17
18namespace llvm {
19
20class Function;
21class Module;
22
23/// A special type used by analysis passes to provide an address that
24/// identifies that particular analysis pass type.
25///
26/// Analysis passes should have a static data member of this type and derive
27/// from the \c AnalysisInfoMixin to get a static ID method used to identify
28/// the analysis in the pass management infrastructure.
29struct alignas(8) AnalysisKey {};
30
31/// A special type used to provide an address that identifies a set of related
32/// analyses. These sets are primarily used below to mark sets of analyses as
33/// preserved.
34///
35/// For example, a transformation can indicate that it preserves the CFG of a
36/// function by preserving the appropriate AnalysisSetKey. An analysis that
37/// depends only on the CFG can then check if that AnalysisSetKey is preserved;
38/// if it is, the analysis knows that it itself is preserved.
39struct alignas(8) AnalysisSetKey {};
40
41/// This templated class represents "all analyses that operate over <a
42/// particular IR unit>" (e.g. a Function or a Module) in instances of
43/// PreservedAnalysis.
44///
45/// This lets a transformation say e.g. "I preserved all function analyses".
46///
47/// Note that you must provide an explicit instantiation declaration and
48/// definition for this template in order to get the correct behavior on
49/// Windows. Otherwise, the address of SetKey will not be stable.
50template <typename IRUnitT> class AllAnalysesOn {
51public:
52 static AnalysisSetKey *ID() { return &SetKey; }
53
54private:
56};
57
58template <typename IRUnitT> AnalysisSetKey AllAnalysesOn<IRUnitT>::SetKey;
59
60extern template class LLVM_TEMPLATE_ABI AllAnalysesOn<Module>;
61extern template class LLVM_TEMPLATE_ABI AllAnalysesOn<Function>;
62
63/// Represents analyses that only rely on functions' control flow.
64///
65/// This can be used with \c PreservedAnalyses to mark the CFG as preserved and
66/// to query whether it has been preserved.
67///
68/// The CFG of a function is defined as the set of basic blocks and the edges
69/// between them. Changing the set of basic blocks in a function is enough to
70/// mutate the CFG. Mutating the condition of a branch or argument of an
71/// invoked function does not mutate the CFG, but changing the successor labels
72/// of those instructions does.
74public:
75 static AnalysisSetKey *ID() { return &SetKey; }
76
77private:
79};
80
81/// A set of analyses that are preserved following a run of a transformation
82/// pass.
83///
84/// Transformation passes build and return these objects to communicate which
85/// analyses are still valid after the transformation. For most passes this is
86/// fairly simple: if they don't change anything all analyses are preserved,
87/// otherwise only a short list of analyses that have been explicitly updated
88/// are preserved.
89///
90/// This class also lets transformation passes mark abstract *sets* of analyses
91/// as preserved. A transformation that (say) does not alter the CFG can
92/// indicate such by marking a particular AnalysisSetKey as preserved, and
93/// then analyses can query whether that AnalysisSetKey is preserved.
94///
95/// Finally, this class can represent an "abandoned" analysis, which is
96/// not preserved even if it would be covered by some abstract set of analyses.
97///
98/// Given a `PreservedAnalyses` object, an analysis will typically want to
99/// figure out whether it is preserved. In the example below, MyAnalysisType is
100/// preserved if it's not abandoned, and (a) it's explicitly marked as
101/// preserved, (b), the set AllAnalysesOn<MyIRUnit> is preserved, or (c) both
102/// AnalysisSetA and AnalysisSetB are preserved.
103///
104/// ```
105/// auto PAC = PA.getChecker<MyAnalysisType>();
106/// if (PAC.preserved() || PAC.preservedSet<AllAnalysesOn<MyIRUnit>>() ||
107/// (PAC.preservedSet<AnalysisSetA>() &&
108/// PAC.preservedSet<AnalysisSetB>())) {
109/// // The analysis has been successfully preserved ...
110/// }
111/// ```
113public:
114 /// Convenience factory function for the empty preserved set.
116
117 /// Construct a special preserved set that preserves all passes.
120 PA.PreservedIDs.insert(&AllAnalysesKey);
121 PA.PreservesAll = true;
122 return PA;
123 }
124
125 /// Construct a preserved analyses object with a single preserved set.
126 template <typename AnalysisSetT> static PreservedAnalyses allInSet() {
128 PA.preserveSet<AnalysisSetT>();
129 return PA;
130 }
131
132 /// Mark an analysis as preserved.
133 template <typename AnalysisT> PreservedAnalyses &preserve() {
134 preserve(AnalysisT::ID());
135 return *this;
136 }
137
138 /// Given an analysis's ID, mark the analysis as preserved, adding it
139 /// to the set.
141 // Clear this ID from the explicit not-preserved set if present.
142 NotPreservedAnalysisIDs.erase(ID);
143
144 // If we're not already preserving all analyses (other than those in
145 // NotPreservedAnalysisIDs).
146 if (!areAllPreserved())
147 PreservedIDs.insert(ID);
148 return *this;
149 }
150
151 /// Mark an analysis set as preserved.
152 template <typename AnalysisSetT> PreservedAnalyses &preserveSet() {
153 preserveSet(AnalysisSetT::ID());
154 return *this;
155 }
156
157 /// Mark an analysis set as preserved using its ID.
159 // If we're not already in the saturated 'all' state, add this set.
160 if (!areAllPreserved())
161 PreservedIDs.insert(ID);
162 return *this;
163 }
164
165 /// Mark an analysis as abandoned.
166 ///
167 /// An abandoned analysis is not preserved, even if it is nominally covered
168 /// by some other set or was previously explicitly marked as preserved.
169 ///
170 /// Note that you can only abandon a specific analysis, not a *set* of
171 /// analyses.
172 template <typename AnalysisT> PreservedAnalyses &abandon() {
173 abandon(AnalysisT::ID());
174 return *this;
175 }
176
177 /// Mark an analysis as abandoned using its ID.
178 ///
179 /// An abandoned analysis is not preserved, even if it is nominally covered
180 /// by some other set or was previously explicitly marked as preserved.
181 ///
182 /// Note that you can only abandon a specific analysis, not a *set* of
183 /// analyses.
185 PreservedIDs.erase(ID);
186 NotPreservedAnalysisIDs.insert(ID);
187 return *this;
188 }
189
190 /// Intersect this set with another in place.
191 ///
192 /// This is a mutating operation on this preserved set, removing all
193 /// preserved passes which are not also preserved in the argument.
194 void intersect(const PreservedAnalyses &Arg) {
195 if (Arg.areAllPreserved())
196 return;
197 if (areAllPreserved()) {
198 *this = Arg;
199 return;
200 }
201 // The intersection requires the *union* of the explicitly not-preserved
202 // IDs and the *intersection* of the preserved IDs.
203 for (auto *ID : Arg.NotPreservedAnalysisIDs) {
204 PreservedIDs.erase(ID);
205 NotPreservedAnalysisIDs.insert(ID);
206 }
207 PreservedIDs.remove_if(
208 [&](void *ID) { return !Arg.PreservedIDs.contains(ID); });
209 PreservesAll &= Arg.PreservesAll;
210 }
211
212 /// Intersect this set with a temporary other set in place.
213 ///
214 /// This is a mutating operation on this preserved set, removing all
215 /// preserved passes which are not also preserved in the argument.
217 if (Arg.areAllPreserved())
218 return;
219 if (areAllPreserved()) {
220 *this = std::move(Arg);
221 return;
222 }
223 // The intersection requires the *union* of the explicitly not-preserved
224 // IDs and the *intersection* of the preserved IDs.
225 for (auto *ID : Arg.NotPreservedAnalysisIDs) {
226 PreservedIDs.erase(ID);
227 NotPreservedAnalysisIDs.insert(ID);
228 }
229 PreservedIDs.remove_if(
230 [&](void *ID) { return !Arg.PreservedIDs.contains(ID); });
231 PreservesAll &= Arg.PreservesAll;
232 }
233
234 /// A checker object that makes it easy to query for whether an analysis or
235 /// some set covering it is preserved.
236 class PreservedAnalysisChecker {
237 friend class PreservedAnalyses;
238
239 const PreservedAnalyses &PA;
240 AnalysisKey *const ID;
241 const bool IsAbandoned;
242
243 /// A PreservedAnalysisChecker is tied to a particular Analysis because
244 /// `preserved()` and `preservedSet()` both return false if the Analysis
245 /// was abandoned.
246 PreservedAnalysisChecker(const PreservedAnalyses &PA, AnalysisKey *ID)
247 : PA(PA), ID(ID), IsAbandoned(PA.NotPreservedAnalysisIDs.count(ID)) {}
248
249 public:
250 /// Returns true if the checker's analysis was not abandoned and either
251 /// - the analysis is explicitly preserved or
252 /// - all analyses are preserved.
253 bool preserved() {
254 return !IsAbandoned && (PA.PreservesAll || PA.PreservedIDs.count(ID));
255 }
256
257 /// Return true if the checker's analysis was not abandoned, i.e. it was not
258 /// explicitly invalidated. Even if the analysis is not explicitly
259 /// preserved, if the analysis is known stateless, then it is preserved.
260 bool preservedWhenStateless() { return !IsAbandoned; }
261
262 /// Returns true if the checker's analysis was not abandoned and either
263 /// - \p AnalysisSetT is explicitly preserved or
264 /// - all analyses are preserved.
265 template <typename AnalysisSetT> bool preservedSet() {
266 AnalysisSetKey *SetID = AnalysisSetT::ID();
267 return !IsAbandoned && (PA.PreservesAll || PA.PreservedIDs.count(SetID));
268 }
269 };
270
271 /// Build a checker for this `PreservedAnalyses` and the specified analysis
272 /// type.
273 ///
274 /// You can use the returned object to query whether an analysis was
275 /// preserved. See the example in the comment on `PreservedAnalysis`.
276 template <typename AnalysisT> PreservedAnalysisChecker getChecker() const {
277 return PreservedAnalysisChecker(*this, AnalysisT::ID());
278 }
279
280 /// Build a checker for this `PreservedAnalyses` and the specified analysis
281 /// ID.
282 ///
283 /// You can use the returned object to query whether an analysis was
284 /// preserved. See the example in the comment on `PreservedAnalysis`.
288
289 /// Test whether all analyses are preserved (and none are abandoned).
290 ///
291 /// This is used primarily to optimize for the common case of a transformation
292 /// which makes no changes to the IR.
293 bool areAllPreserved() const {
294 return NotPreservedAnalysisIDs.empty() && PreservesAll;
295 }
296
297 /// Directly test whether a set of analyses is preserved.
298 ///
299 /// This is only true when no analyses have been explicitly abandoned.
300 template <typename AnalysisSetT> bool allAnalysesInSetPreserved() const {
301 return allAnalysesInSetPreserved(AnalysisSetT::ID());
302 }
303
304 /// Directly test whether a set of analyses is preserved.
305 ///
306 /// This is only true when no analyses have been explicitly abandoned.
308 return NotPreservedAnalysisIDs.empty() &&
309 (PreservesAll || PreservedIDs.count(SetID));
310 }
311
312private:
313 /// A special key used to indicate all analyses.
314 LLVM_ABI static AnalysisSetKey AllAnalysesKey;
315
316 /// The IDs of analyses and analysis sets that are preserved.
317 SmallPtrSet<void *, 8> PreservedIDs;
318
319 /// The IDs of explicitly not-preserved analyses.
320 ///
321 /// If an analysis in this set is covered by a set in `PreservedIDs`, we
322 /// consider it not-preserved. That is, `NotPreservedAnalysisIDs` always
323 /// "wins" over analysis sets in `PreservedIDs`.
324 ///
325 /// Also, a given ID should never occur both here and in `PreservedIDs`.
326 SmallPtrSet<AnalysisKey *, 2> NotPreservedAnalysisIDs;
327
328 /// Whether PreservedIDs contains AllAnalysesKey.
329 bool PreservesAll = false;
330};
331} // namespace llvm
332
333#endif
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_TEMPLATE_ABI
Definition Compiler.h:212
This file defines the SmallPtrSet class.
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
static AnalysisSetKey * ID()
Definition Analysis.h:52
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
static AnalysisSetKey * ID()
Definition Analysis.h:75
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A checker object that makes it easy to query for whether an analysis or some set covering it is prese...
Definition Analysis.h:236
bool preserved()
Returns true if the checker's analysis was not abandoned and either.
Definition Analysis.h:253
bool preservedWhenStateless()
Return true if the checker's analysis was not abandoned, i.e.
Definition Analysis.h:260
bool preservedSet()
Returns true if the checker's analysis was not abandoned and either.
Definition Analysis.h:265
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
bool areAllPreserved() const
Test whether all analyses are preserved (and none are abandoned).
Definition Analysis.h:293
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserve(AnalysisKey *ID)
Given an analysis's ID, mark the analysis as preserved, adding it to the set.
Definition Analysis.h:140
PreservedAnalyses & abandon()
Mark an analysis as abandoned.
Definition Analysis.h:172
bool allAnalysesInSetPreserved() const
Directly test whether a set of analyses is preserved.
Definition Analysis.h:300
PreservedAnalysisChecker getChecker(AnalysisKey *ID) const
Build a checker for this PreservedAnalyses and the specified analysis ID.
Definition Analysis.h:285
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
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:133
PreservedAnalyses & abandon(AnalysisKey *ID)
Mark an analysis as abandoned using its ID.
Definition Analysis.h:184
void intersect(PreservedAnalyses &&Arg)
Intersect this set with a temporary other set in place.
Definition Analysis.h:216
bool allAnalysesInSetPreserved(AnalysisSetKey *SetID) const
Directly test whether a set of analyses is preserved.
Definition Analysis.h:307
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition Analysis.h:276
PreservedAnalyses & preserveSet(AnalysisSetKey *ID)
Mark an analysis set as preserved using its ID.
Definition Analysis.h:158
static PreservedAnalyses allInSet()
Construct a preserved analyses object with a single preserved set.
Definition Analysis.h:126
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This is an optimization pass for GlobalISel generic memory operations.
AnalysisSetKey AllAnalysesOn< IRUnitT >::SetKey
Definition Analysis.h:58
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition STLExtras.h:2028
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
A special type used to provide an address that identifies a set of related analyses.
Definition Analysis.h:39