LLVM 22.0.0git
PassAnalysisSupport.h
Go to the documentation of this file.
1//===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- 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//
9// This file defines stuff that is used to define and "use" Analysis Passes.
10// This file is automatically #included by Pass.h, so:
11//
12// NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
13//
14// Instead, #include Pass.h
15//
16//===----------------------------------------------------------------------===//
17
18#if !defined(LLVM_PASS_H) || defined(LLVM_PASSANALYSISSUPPORT_H)
19#error "Do not include <PassAnalysisSupport.h>; include <Pass.h> instead"
20#endif
21
22#ifndef LLVM_PASSANALYSISSUPPORT_H
23#define LLVM_PASSANALYSISSUPPORT_H
24
25#include "llvm/ADT/STLExtras.h"
28#include <cassert>
29#include <tuple>
30#include <utility>
31#include <vector>
32
33namespace llvm {
34
35class Function;
36class Pass;
37class PMDataManager;
38class StringRef;
39
40//===----------------------------------------------------------------------===//
41/// Represent the analysis usage information of a pass. This tracks analyses
42/// that the pass REQUIRES (must be available when the pass runs), REQUIRES
43/// TRANSITIVE (must be available throughout the lifetime of the pass), and
44/// analyses that the pass PRESERVES (the pass does not invalidate the results
45/// of these analyses). This information is provided by a pass to the Pass
46/// infrastructure through the getAnalysisUsage virtual function.
47///
49public:
51
52private:
53 /// Sets of analyses required and preserved by a pass
54 // TODO: It's not clear that SmallVector is an appropriate data structure for
55 // this usecase. The sizes were picked to minimize wasted space, but are
56 // otherwise fairly meaningless.
58 SmallVector<AnalysisID, 2> RequiredTransitive;
61 bool PreservesAll = false;
62
63 void pushUnique(VectorType &Set, AnalysisID ID) {
64 if (!llvm::is_contained(Set, ID))
65 Set.push_back(ID);
66 }
67
68public:
69 AnalysisUsage() = default;
70
71 ///@{
72 /// Add the specified ID to the required set of the usage info for a pass.
75 template<class PassClass>
77 return addRequiredID(PassClass::ID);
78 }
79
81 template<class PassClass>
83 return addRequiredTransitiveID(PassClass::ID);
84 }
85 ///@}
86
87 ///@{
88 /// Add the specified ID to the set of analyses preserved by this pass.
90 pushUnique(Preserved, ID);
91 return *this;
92 }
94 pushUnique(Preserved, &ID);
95 return *this;
96 }
97 /// Add the specified Pass class to the set of analyses preserved by this pass.
98 template<class PassClass>
100 pushUnique(Preserved, &PassClass::ID);
101 return *this;
102 }
103 ///@}
104
105 ///@{
106 /// Add the specified ID to the set of analyses used by this pass if they are
107 /// available..
109 pushUnique(Used, ID);
110 return *this;
111 }
113 pushUnique(Used, &ID);
114 return *this;
115 }
116 /// Add the specified Pass class to the set of analyses used by this pass.
117 template<class PassClass>
119 pushUnique(Used, &PassClass::ID);
120 return *this;
121 }
122 ///@}
123
124 /// Add the Pass with the specified argument string to the set of analyses
125 /// preserved by this pass. If no such Pass exists, do nothing. This can be
126 /// useful when a pass is trivially preserved, but may not be linked in. Be
127 /// careful about spelling!
129
130 /// Set by analyses that do not transform their input at all
131 void setPreservesAll() { PreservesAll = true; }
132
133 /// Determine whether a pass said it does not transform its input at all
134 bool getPreservesAll() const { return PreservesAll; }
135
136 /// This function should be called by the pass, iff they do not:
137 ///
138 /// 1. Add or remove basic blocks from the function
139 /// 2. Modify terminator instructions in any way.
140 ///
141 /// This function annotates the AnalysisUsage info object to say that analyses
142 /// that only depend on the CFG are preserved by this pass.
144
145 const VectorType &getRequiredSet() const { return Required; }
147 return RequiredTransitive;
148 }
149 const VectorType &getPreservedSet() const { return Preserved; }
150 const VectorType &getUsedSet() const { return Used; }
151};
152
153//===----------------------------------------------------------------------===//
154/// AnalysisResolver - Simple interface used by Pass objects to pull all
155/// analysis information out of pass manager that is responsible to manage
156/// the pass.
157///
159public:
161 explicit AnalysisResolver(PMDataManager &P) : PM(P) {}
162
164
165 /// Find pass that is implementing PI.
167 Pass *ResultPass = nullptr;
168 for (const auto &AnalysisImpl : AnalysisImpls) {
169 if (AnalysisImpl.first == PI) {
170 ResultPass = AnalysisImpl.second;
171 break;
172 }
173 }
174 return ResultPass;
175 }
176
177 /// Find pass that is implementing PI. Initialize pass for Function F.
178 LLVM_ABI std::tuple<Pass *, bool> findImplPass(Pass *P, AnalysisID PI,
179 Function &F);
180
182 if (findImplPass(PI) == P)
183 return;
184 std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P);
185 AnalysisImpls.push_back(pir);
186 }
187
188 /// Clear cache that is used to connect a pass to the analysis (PassInfo).
190 AnalysisImpls.clear();
191 }
192
193 /// Return analysis result or null if it doesn't exist.
195
196private:
197 /// This keeps track of which passes implements the interfaces that are
198 /// required by the current pass (to implement getAnalysis()).
199 std::vector<std::pair<AnalysisID, Pass *>> AnalysisImpls;
200
201 /// PassManager that is used to resolve analysis info
202 PMDataManager &PM;
203};
204
205/// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
206/// get analysis information that might be around, for example to update it.
207/// This is different than getAnalysis in that it can fail (if the analysis
208/// results haven't been computed), so should only be used if you can handle
209/// the case when the analysis is not available. This method is often used by
210/// transformation APIs to update analysis results for a pass automatically as
211/// the transform is performed.
212template<typename AnalysisType>
213AnalysisType *Pass::getAnalysisIfAvailable() const {
214 assert(Resolver && "Pass not resident in a PassManager object!");
215
216 const void *PI = &AnalysisType::ID;
217 return (AnalysisType *)Resolver->getAnalysisIfAvailable(PI);
218}
219
220/// getAnalysis<AnalysisType>() - This function is used by subclasses to get
221/// to the analysis information that they claim to use by overriding the
222/// getAnalysisUsage function.
223template<typename AnalysisType>
224AnalysisType &Pass::getAnalysis() const {
225 assert(Resolver && "Pass has not been inserted into a PassManager object!");
226 return getAnalysisID<AnalysisType>(&AnalysisType::ID);
227}
228
229template<typename AnalysisType>
230AnalysisType &Pass::getAnalysisID(AnalysisID PI) const {
231 assert(PI && "getAnalysis for unregistered pass!");
232 assert(Resolver&&"Pass has not been inserted into a PassManager object!");
233 // PI *must* appear in AnalysisImpls. Because the number of passes used
234 // should be a small number, we just do a linear search over a (dense)
235 // vector.
236 Pass *ResultPass = Resolver->findImplPass(PI);
237 assert(ResultPass &&
238 "getAnalysis*() called on an analysis that was not "
239 "'required' by pass!");
240 return *(AnalysisType *)ResultPass;
241}
242
243/// getAnalysis<AnalysisType>() - This function is used by subclasses to get
244/// to the analysis information that they claim to use by overriding the
245/// getAnalysisUsage function. If as part of the dependencies, an IR
246/// transformation is triggered (e.g. because the analysis requires
247/// BreakCriticalEdges), and Changed is non null, *Changed is updated.
248template <typename AnalysisType>
249AnalysisType &Pass::getAnalysis(Function &F, bool *Changed) {
250 assert(Resolver &&"Pass has not been inserted into a PassManager object!");
251
252 return getAnalysisID<AnalysisType>(&AnalysisType::ID, F, Changed);
253}
254
255template <typename AnalysisType>
256AnalysisType &Pass::getAnalysisID(AnalysisID PI, Function &F, bool *Changed) {
257 assert(PI && "getAnalysis for unregistered pass!");
258 assert(Resolver && "Pass has not been inserted into a PassManager object!");
259 // PI *must* appear in AnalysisImpls. Because the number of passes used
260 // should be a small number, we just do a linear search over a (dense)
261 // vector.
262 Pass *ResultPass;
263 bool LocalChanged;
264 std::tie(ResultPass, LocalChanged) = Resolver->findImplPass(this, PI, F);
265
266 assert(ResultPass && "Unable to find requested analysis info");
267 if (Changed)
268 *Changed |= LocalChanged;
269 else
270 assert(!LocalChanged &&
271 "A pass trigged a code update but the update status is lost");
272 return *(AnalysisType *)ResultPass;
273}
274
275} // end namespace llvm
276
277#endif // LLVM_PASSANALYSISSUPPORT_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
#define F(x, y, z)
Definition: MD5.cpp:55
#define P(N)
This file contains some templates that are useful if you are working with the STL at all.
Shrink Wrap Pass
Definition: ShrinkWrap.cpp:301
This file defines the SmallVector class.
AnalysisResolver - Simple interface used by Pass objects to pull all analysis information out of pass...
void addAnalysisImplsPair(AnalysisID PI, Pass *P)
Pass * findImplPass(AnalysisID PI)
Find pass that is implementing PI.
PMDataManager & getPMDataManager()
AnalysisResolver(PMDataManager &P)
void clearAnalysisImpls()
Clear cache that is used to connect a pass to the analysis (PassInfo).
LLVM_ABI Pass * getAnalysisIfAvailable(AnalysisID ID) const
Return analysis result or null if it doesn't exist.
Represent the analysis usage information of a pass.
LLVM_ABI AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:284
const VectorType & getRequiredSet() const
LLVM_ABI AnalysisUsage & addRequiredTransitiveID(char &ID)
Definition: Pass.cpp:294
AnalysisUsage & addPreservedID(char &ID)
AnalysisUsage & addPreservedID(const void *ID)
AnalysisUsage & addUsedIfAvailableID(const void *ID)
AnalysisUsage & addUsedIfAvailableID(char &ID)
AnalysisUsage()=default
const VectorType & getRequiredTransitiveSet() const
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
const VectorType & getUsedSet() const
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:270
void setPreservesAll()
Set by analyses that do not transform their input at all.
bool getPreservesAll() const
Determine whether a pass said it does not transform its input at all.
const VectorType & getPreservedSet() const
AnalysisUsage & addRequiredTransitive()
PMDataManager provides the common place to manage the analysis data used by pass managers.
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:99
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
AnalysisType & getAnalysisID(AnalysisID PI) const
AnalysisType * getAnalysisIfAvailable() const
getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to get analysis information tha...
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2196
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
const void * AnalysisID
Definition: Pass.h:51
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1916