1 //===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines stuff that is used to define and "use" Analysis Passes.
10 // This file is automatically #included by Pass.h, so:
12 // NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
14 // Instead, #include Pass.h
16 //===----------------------------------------------------------------------===//
18 #if !defined(LLVM_PASS_H) || defined(LLVM_PASSANALYSISSUPPORT_H)
19 #error "Do not include <PassAnalysisSupport.h>; include <Pass.h> instead"
22 #ifndef LLVM_PASSANALYSISSUPPORT_H
23 #define LLVM_PASSANALYSISSUPPORT_H
25 #include "llvm/ADT/SmallVector.h"
38 //===----------------------------------------------------------------------===//
39 /// Represent the analysis usage information of a pass. This tracks analyses
40 /// that the pass REQUIRES (must be available when the pass runs), REQUIRES
41 /// TRANSITIVE (must be available throughout the lifetime of the pass), and
42 /// analyses that the pass PRESERVES (the pass does not invalidate the results
43 /// of these analyses). This information is provided by a pass to the Pass
44 /// infrastructure through the getAnalysisUsage virtual function.
48 using VectorType = SmallVectorImpl<AnalysisID>;
51 /// Sets of analyses required and preserved by a pass
52 // TODO: It's not clear that SmallVector is an appropriate data structure for
53 // this usecase. The sizes were picked to minimize wasted space, but are
54 // otherwise fairly meaningless.
55 SmallVector<AnalysisID, 8> Required;
56 SmallVector<AnalysisID, 2> RequiredTransitive;
57 SmallVector<AnalysisID, 2> Preserved;
58 SmallVector<AnalysisID, 0> Used;
59 bool PreservesAll = false;
62 AnalysisUsage() = default;
65 /// Add the specified ID to the required set of the usage info for a pass.
66 AnalysisUsage &addRequiredID(const void *ID);
67 AnalysisUsage &addRequiredID(char &ID);
68 template<class PassClass>
69 AnalysisUsage &addRequired() {
70 return addRequiredID(PassClass::ID);
73 AnalysisUsage &addRequiredTransitiveID(char &ID);
74 template<class PassClass>
75 AnalysisUsage &addRequiredTransitive() {
76 return addRequiredTransitiveID(PassClass::ID);
81 /// Add the specified ID to the set of analyses preserved by this pass.
82 AnalysisUsage &addPreservedID(const void *ID) {
83 Preserved.push_back(ID);
86 AnalysisUsage &addPreservedID(char &ID) {
87 Preserved.push_back(&ID);
90 /// Add the specified Pass class to the set of analyses preserved by this pass.
91 template<class PassClass>
92 AnalysisUsage &addPreserved() {
93 Preserved.push_back(&PassClass::ID);
99 /// Add the specified ID to the set of analyses used by this pass if they are
101 AnalysisUsage &addUsedIfAvailableID(const void *ID) {
105 AnalysisUsage &addUsedIfAvailableID(char &ID) {
109 /// Add the specified Pass class to the set of analyses used by this pass.
110 template<class PassClass>
111 AnalysisUsage &addUsedIfAvailable() {
112 Used.push_back(&PassClass::ID);
117 /// Add the Pass with the specified argument string to the set of analyses
118 /// preserved by this pass. If no such Pass exists, do nothing. This can be
119 /// useful when a pass is trivially preserved, but may not be linked in. Be
120 /// careful about spelling!
121 AnalysisUsage &addPreserved(StringRef Arg);
123 /// Set by analyses that do not transform their input at all
124 void setPreservesAll() { PreservesAll = true; }
126 /// Determine whether a pass said it does not transform its input at all
127 bool getPreservesAll() const { return PreservesAll; }
129 /// This function should be called by the pass, iff they do not:
131 /// 1. Add or remove basic blocks from the function
132 /// 2. Modify terminator instructions in any way.
134 /// This function annotates the AnalysisUsage info object to say that analyses
135 /// that only depend on the CFG are preserved by this pass.
136 void setPreservesCFG();
138 const VectorType &getRequiredSet() const { return Required; }
139 const VectorType &getRequiredTransitiveSet() const {
140 return RequiredTransitive;
142 const VectorType &getPreservedSet() const { return Preserved; }
143 const VectorType &getUsedSet() const { return Used; }
146 //===----------------------------------------------------------------------===//
147 /// AnalysisResolver - Simple interface used by Pass objects to pull all
148 /// analysis information out of pass manager that is responsible to manage
151 class AnalysisResolver {
153 AnalysisResolver() = delete;
154 explicit AnalysisResolver(PMDataManager &P) : PM(P) {}
156 PMDataManager &getPMDataManager() { return PM; }
158 /// Find pass that is implementing PI.
159 Pass *findImplPass(AnalysisID PI) {
160 Pass *ResultPass = nullptr;
161 for (const auto &AnalysisImpl : AnalysisImpls) {
162 if (AnalysisImpl.first == PI) {
163 ResultPass = AnalysisImpl.second;
170 /// Find pass that is implementing PI. Initialize pass for Function F.
171 std::tuple<Pass *, bool> findImplPass(Pass *P, AnalysisID PI, Function &F);
173 void addAnalysisImplsPair(AnalysisID PI, Pass *P) {
174 if (findImplPass(PI) == P)
176 std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P);
177 AnalysisImpls.push_back(pir);
180 /// Clear cache that is used to connect a pass to the analysis (PassInfo).
181 void clearAnalysisImpls() {
182 AnalysisImpls.clear();
185 /// Return analysis result or null if it doesn't exist.
186 Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
189 /// This keeps track of which passes implements the interfaces that are
190 /// required by the current pass (to implement getAnalysis()).
191 std::vector<std::pair<AnalysisID, Pass *>> AnalysisImpls;
193 /// PassManager that is used to resolve analysis info
197 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
198 /// get analysis information that might be around, for example to update it.
199 /// This is different than getAnalysis in that it can fail (if the analysis
200 /// results haven't been computed), so should only be used if you can handle
201 /// the case when the analysis is not available. This method is often used by
202 /// transformation APIs to update analysis results for a pass automatically as
203 /// the transform is performed.
204 template<typename AnalysisType>
205 AnalysisType *Pass::getAnalysisIfAvailable() const {
206 assert(Resolver && "Pass not resident in a PassManager object!");
208 const void *PI = &AnalysisType::ID;
210 Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true);
211 if (!ResultPass) return nullptr;
213 // Because the AnalysisType may not be a subclass of pass (for
214 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
215 // adjust the return pointer (because the class may multiply inherit, once
216 // from pass, once from AnalysisType).
217 return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
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.
223 template<typename AnalysisType>
224 AnalysisType &Pass::getAnalysis() const {
225 assert(Resolver && "Pass has not been inserted into a PassManager object!");
226 return getAnalysisID<AnalysisType>(&AnalysisType::ID);
229 template<typename AnalysisType>
230 AnalysisType &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)
236 Pass *ResultPass = Resolver->findImplPass(PI);
238 "getAnalysis*() called on an analysis that was not "
239 "'required' by pass!");
241 // Because the AnalysisType may not be a subclass of pass (for
242 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
243 // adjust the return pointer (because the class may multiply inherit, once
244 // from pass, once from AnalysisType).
245 return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
248 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
249 /// to the analysis information that they claim to use by overriding the
250 /// getAnalysisUsage function. If as part of the dependencies, an IR
251 /// transformation is triggered (e.g. because the analysis requires
252 /// BreakCriticalEdges), and Changed is non null, *Changed is updated.
253 template <typename AnalysisType>
254 AnalysisType &Pass::getAnalysis(Function &F, bool *Changed) {
255 assert(Resolver &&"Pass has not been inserted into a PassManager object!");
257 return getAnalysisID<AnalysisType>(&AnalysisType::ID, F, Changed);
260 template <typename AnalysisType>
261 AnalysisType &Pass::getAnalysisID(AnalysisID PI, Function &F, bool *Changed) {
262 assert(PI && "getAnalysis for unregistered pass!");
263 assert(Resolver && "Pass has not been inserted into a PassManager object!");
264 // PI *must* appear in AnalysisImpls. Because the number of passes used
265 // should be a small number, we just do a linear search over a (dense)
269 std::tie(ResultPass, LocalChanged) = Resolver->findImplPass(this, PI, F);
271 assert(ResultPass && "Unable to find requested analysis info");
273 *Changed |= LocalChanged;
275 assert(!LocalChanged &&
276 "A pass trigged a code update but the update status is lost");
278 // Because the AnalysisType may not be a subclass of pass (for
279 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
280 // adjust the return pointer (because the class may multiply inherit, once
281 // from pass, once from AnalysisType).
282 return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
285 } // end namespace llvm
287 #endif // LLVM_PASSANALYSISSUPPORT_H