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/STLExtras.h"
26 #include "llvm/ADT/SmallVector.h"
39 //===----------------------------------------------------------------------===//
40 /// Represent the analysis usage information of a pass. This tracks analyses
41 /// that the pass REQUIRES (must be available when the pass runs), REQUIRES
42 /// TRANSITIVE (must be available throughout the lifetime of the pass), and
43 /// analyses that the pass PRESERVES (the pass does not invalidate the results
44 /// of these analyses). This information is provided by a pass to the Pass
45 /// infrastructure through the getAnalysisUsage virtual function.
49 using VectorType = SmallVectorImpl<AnalysisID>;
52 /// Sets of analyses required and preserved by a pass
53 // TODO: It's not clear that SmallVector is an appropriate data structure for
54 // this usecase. The sizes were picked to minimize wasted space, but are
55 // otherwise fairly meaningless.
56 SmallVector<AnalysisID, 8> Required;
57 SmallVector<AnalysisID, 2> RequiredTransitive;
58 SmallVector<AnalysisID, 2> Preserved;
59 SmallVector<AnalysisID, 0> Used;
60 bool PreservesAll = false;
62 void pushUnique(VectorType &Set, AnalysisID ID) {
63 if (!llvm::is_contained(Set, ID))
68 AnalysisUsage() = default;
71 /// Add the specified ID to the required set of the usage info for a pass.
72 AnalysisUsage &addRequiredID(const void *ID);
73 AnalysisUsage &addRequiredID(char &ID);
74 template<class PassClass>
75 AnalysisUsage &addRequired() {
76 return addRequiredID(PassClass::ID);
79 AnalysisUsage &addRequiredTransitiveID(char &ID);
80 template<class PassClass>
81 AnalysisUsage &addRequiredTransitive() {
82 return addRequiredTransitiveID(PassClass::ID);
87 /// Add the specified ID to the set of analyses preserved by this pass.
88 AnalysisUsage &addPreservedID(const void *ID) {
89 pushUnique(Preserved, ID);
92 AnalysisUsage &addPreservedID(char &ID) {
93 pushUnique(Preserved, &ID);
96 /// Add the specified Pass class to the set of analyses preserved by this pass.
97 template<class PassClass>
98 AnalysisUsage &addPreserved() {
99 pushUnique(Preserved, &PassClass::ID);
105 /// Add the specified ID to the set of analyses used by this pass if they are
107 AnalysisUsage &addUsedIfAvailableID(const void *ID) {
108 pushUnique(Used, ID);
111 AnalysisUsage &addUsedIfAvailableID(char &ID) {
112 pushUnique(Used, &ID);
115 /// Add the specified Pass class to the set of analyses used by this pass.
116 template<class PassClass>
117 AnalysisUsage &addUsedIfAvailable() {
118 pushUnique(Used, &PassClass::ID);
123 /// Add the Pass with the specified argument string to the set of analyses
124 /// preserved by this pass. If no such Pass exists, do nothing. This can be
125 /// useful when a pass is trivially preserved, but may not be linked in. Be
126 /// careful about spelling!
127 AnalysisUsage &addPreserved(StringRef Arg);
129 /// Set by analyses that do not transform their input at all
130 void setPreservesAll() { PreservesAll = true; }
132 /// Determine whether a pass said it does not transform its input at all
133 bool getPreservesAll() const { return PreservesAll; }
135 /// This function should be called by the pass, iff they do not:
137 /// 1. Add or remove basic blocks from the function
138 /// 2. Modify terminator instructions in any way.
140 /// This function annotates the AnalysisUsage info object to say that analyses
141 /// that only depend on the CFG are preserved by this pass.
142 void setPreservesCFG();
144 const VectorType &getRequiredSet() const { return Required; }
145 const VectorType &getRequiredTransitiveSet() const {
146 return RequiredTransitive;
148 const VectorType &getPreservedSet() const { return Preserved; }
149 const VectorType &getUsedSet() const { return Used; }
152 //===----------------------------------------------------------------------===//
153 /// AnalysisResolver - Simple interface used by Pass objects to pull all
154 /// analysis information out of pass manager that is responsible to manage
157 class AnalysisResolver {
159 AnalysisResolver() = delete;
160 explicit AnalysisResolver(PMDataManager &P) : PM(P) {}
162 PMDataManager &getPMDataManager() { return PM; }
164 /// Find pass that is implementing PI.
165 Pass *findImplPass(AnalysisID PI) {
166 Pass *ResultPass = nullptr;
167 for (const auto &AnalysisImpl : AnalysisImpls) {
168 if (AnalysisImpl.first == PI) {
169 ResultPass = AnalysisImpl.second;
176 /// Find pass that is implementing PI. Initialize pass for Function F.
177 std::tuple<Pass *, bool> findImplPass(Pass *P, AnalysisID PI, Function &F);
179 void addAnalysisImplsPair(AnalysisID PI, Pass *P) {
180 if (findImplPass(PI) == P)
182 std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P);
183 AnalysisImpls.push_back(pir);
186 /// Clear cache that is used to connect a pass to the analysis (PassInfo).
187 void clearAnalysisImpls() {
188 AnalysisImpls.clear();
191 /// Return analysis result or null if it doesn't exist.
192 Pass *getAnalysisIfAvailable(AnalysisID ID) const;
195 /// This keeps track of which passes implements the interfaces that are
196 /// required by the current pass (to implement getAnalysis()).
197 std::vector<std::pair<AnalysisID, Pass *>> AnalysisImpls;
199 /// PassManager that is used to resolve analysis info
203 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
204 /// get analysis information that might be around, for example to update it.
205 /// This is different than getAnalysis in that it can fail (if the analysis
206 /// results haven't been computed), so should only be used if you can handle
207 /// the case when the analysis is not available. This method is often used by
208 /// transformation APIs to update analysis results for a pass automatically as
209 /// the transform is performed.
210 template<typename AnalysisType>
211 AnalysisType *Pass::getAnalysisIfAvailable() const {
212 assert(Resolver && "Pass not resident in a PassManager object!");
214 const void *PI = &AnalysisType::ID;
216 Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI);
217 if (!ResultPass) return nullptr;
219 // Because the AnalysisType may not be a subclass of pass (for
220 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
221 // adjust the return pointer (because the class may multiply inherit, once
222 // from pass, once from AnalysisType).
223 return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
226 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
227 /// to the analysis information that they claim to use by overriding the
228 /// getAnalysisUsage function.
229 template<typename AnalysisType>
230 AnalysisType &Pass::getAnalysis() const {
231 assert(Resolver && "Pass has not been inserted into a PassManager object!");
232 return getAnalysisID<AnalysisType>(&AnalysisType::ID);
235 template<typename AnalysisType>
236 AnalysisType &Pass::getAnalysisID(AnalysisID PI) const {
237 assert(PI && "getAnalysis for unregistered pass!");
238 assert(Resolver&&"Pass has not been inserted into a PassManager object!");
239 // PI *must* appear in AnalysisImpls. Because the number of passes used
240 // should be a small number, we just do a linear search over a (dense)
242 Pass *ResultPass = Resolver->findImplPass(PI);
244 "getAnalysis*() called on an analysis that was not "
245 "'required' by pass!");
247 // Because the AnalysisType may not be a subclass of pass (for
248 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
249 // adjust the return pointer (because the class may multiply inherit, once
250 // from pass, once from AnalysisType).
251 return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
254 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
255 /// to the analysis information that they claim to use by overriding the
256 /// getAnalysisUsage function. If as part of the dependencies, an IR
257 /// transformation is triggered (e.g. because the analysis requires
258 /// BreakCriticalEdges), and Changed is non null, *Changed is updated.
259 template <typename AnalysisType>
260 AnalysisType &Pass::getAnalysis(Function &F, bool *Changed) {
261 assert(Resolver &&"Pass has not been inserted into a PassManager object!");
263 return getAnalysisID<AnalysisType>(&AnalysisType::ID, F, Changed);
266 template <typename AnalysisType>
267 AnalysisType &Pass::getAnalysisID(AnalysisID PI, Function &F, bool *Changed) {
268 assert(PI && "getAnalysis for unregistered pass!");
269 assert(Resolver && "Pass has not been inserted into a PassManager object!");
270 // PI *must* appear in AnalysisImpls. Because the number of passes used
271 // should be a small number, we just do a linear search over a (dense)
275 std::tie(ResultPass, LocalChanged) = Resolver->findImplPass(this, PI, F);
277 assert(ResultPass && "Unable to find requested analysis info");
279 *Changed |= LocalChanged;
281 assert(!LocalChanged &&
282 "A pass trigged a code update but the update status is lost");
284 // Because the AnalysisType may not be a subclass of pass (for
285 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
286 // adjust the return pointer (because the class may multiply inherit, once
287 // from pass, once from AnalysisType).
288 return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
291 } // end namespace llvm
293 #endif // LLVM_PASSANALYSISSUPPORT_H