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/SmallVector.h"
26 #include <cassert>
27 #include <tuple>
28 #include <utility>
29 #include <vector>
30
31 namespace llvm {
32
33 class Function;
34 class Pass;
35 class PMDataManager;
36 class StringRef;
37
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.
45 ///
46 class AnalysisUsage {
47 public:
48 using VectorType = SmallVectorImpl<AnalysisID>;
49
50 private:
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;
60
61 public:
62 AnalysisUsage() = default;
63
64 ///@{
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>
addRequired()69 AnalysisUsage &addRequired() {
70 return addRequiredID(PassClass::ID);
71 }
72
73 AnalysisUsage &addRequiredTransitiveID(char &ID);
74 template<class PassClass>
addRequiredTransitive()75 AnalysisUsage &addRequiredTransitive() {
76 return addRequiredTransitiveID(PassClass::ID);
77 }
78 ///@}
79
80 ///@{
81 /// Add the specified ID to the set of analyses preserved by this pass.
addPreservedID(const void * ID)82 AnalysisUsage &addPreservedID(const void *ID) {
83 Preserved.push_back(ID);
84 return *this;
85 }
addPreservedID(char & ID)86 AnalysisUsage &addPreservedID(char &ID) {
87 Preserved.push_back(&ID);
88 return *this;
89 }
90 /// Add the specified Pass class to the set of analyses preserved by this pass.
91 template<class PassClass>
addPreserved()92 AnalysisUsage &addPreserved() {
93 Preserved.push_back(&PassClass::ID);
94 return *this;
95 }
96 ///@}
97
98 ///@{
99 /// Add the specified ID to the set of analyses used by this pass if they are
100 /// available..
addUsedIfAvailableID(const void * ID)101 AnalysisUsage &addUsedIfAvailableID(const void *ID) {
102 Used.push_back(ID);
103 return *this;
104 }
addUsedIfAvailableID(char & ID)105 AnalysisUsage &addUsedIfAvailableID(char &ID) {
106 Used.push_back(&ID);
107 return *this;
108 }
109 /// Add the specified Pass class to the set of analyses used by this pass.
110 template<class PassClass>
addUsedIfAvailable()111 AnalysisUsage &addUsedIfAvailable() {
112 Used.push_back(&PassClass::ID);
113 return *this;
114 }
115 ///@}
116
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);
122
123 /// Set by analyses that do not transform their input at all
setPreservesAll()124 void setPreservesAll() { PreservesAll = true; }
125
126 /// Determine whether a pass said it does not transform its input at all
getPreservesAll()127 bool getPreservesAll() const { return PreservesAll; }
128
129 /// This function should be called by the pass, iff they do not:
130 ///
131 /// 1. Add or remove basic blocks from the function
132 /// 2. Modify terminator instructions in any way.
133 ///
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();
137
getRequiredSet()138 const VectorType &getRequiredSet() const { return Required; }
getRequiredTransitiveSet()139 const VectorType &getRequiredTransitiveSet() const {
140 return RequiredTransitive;
141 }
getPreservedSet()142 const VectorType &getPreservedSet() const { return Preserved; }
getUsedSet()143 const VectorType &getUsedSet() const { return Used; }
144 };
145
146 //===----------------------------------------------------------------------===//
147 /// AnalysisResolver - Simple interface used by Pass objects to pull all
148 /// analysis information out of pass manager that is responsible to manage
149 /// the pass.
150 ///
151 class AnalysisResolver {
152 public:
153 AnalysisResolver() = delete;
AnalysisResolver(PMDataManager & P)154 explicit AnalysisResolver(PMDataManager &P) : PM(P) {}
155
getPMDataManager()156 PMDataManager &getPMDataManager() { return PM; }
157
158 /// Find pass that is implementing PI.
findImplPass(AnalysisID 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;
164 break;
165 }
166 }
167 return ResultPass;
168 }
169
170 /// Find pass that is implementing PI. Initialize pass for Function F.
171 std::tuple<Pass *, bool> findImplPass(Pass *P, AnalysisID PI, Function &F);
172
addAnalysisImplsPair(AnalysisID PI,Pass * P)173 void addAnalysisImplsPair(AnalysisID PI, Pass *P) {
174 if (findImplPass(PI) == P)
175 return;
176 std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P);
177 AnalysisImpls.push_back(pir);
178 }
179
180 /// Clear cache that is used to connect a pass to the analysis (PassInfo).
clearAnalysisImpls()181 void clearAnalysisImpls() {
182 AnalysisImpls.clear();
183 }
184
185 /// Return analysis result or null if it doesn't exist.
186 Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
187
188 private:
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;
192
193 /// PassManager that is used to resolve analysis info
194 PMDataManager &PM;
195 };
196
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>
getAnalysisIfAvailable()205 AnalysisType *Pass::getAnalysisIfAvailable() const {
206 assert(Resolver && "Pass not resident in a PassManager object!");
207
208 const void *PI = &AnalysisType::ID;
209
210 Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true);
211 if (!ResultPass) return nullptr;
212
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);
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.
223 template<typename AnalysisType>
getAnalysis()224 AnalysisType &Pass::getAnalysis() const {
225 assert(Resolver && "Pass has not been inserted into a PassManager object!");
226 return getAnalysisID<AnalysisType>(&AnalysisType::ID);
227 }
228
229 template<typename AnalysisType>
getAnalysisID(AnalysisID PI)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)
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
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);
246 }
247
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>
getAnalysis(Function & F,bool * Changed)254 AnalysisType &Pass::getAnalysis(Function &F, bool *Changed) {
255 assert(Resolver &&"Pass has not been inserted into a PassManager object!");
256
257 return getAnalysisID<AnalysisType>(&AnalysisType::ID, F, Changed);
258 }
259
260 template <typename AnalysisType>
getAnalysisID(AnalysisID PI,Function & F,bool * Changed)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)
266 // vector.
267 Pass *ResultPass;
268 bool LocalChanged;
269 std::tie(ResultPass, LocalChanged) = Resolver->findImplPass(this, PI, F);
270
271 assert(ResultPass && "Unable to find requested analysis info");
272 if (Changed)
273 *Changed |= LocalChanged;
274 else
275 assert(!LocalChanged &&
276 "A pass trigged a code update but the update status is lost");
277
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);
283 }
284
285 } // end namespace llvm
286
287 #endif // LLVM_PASSANALYSISSUPPORT_H
288