xref: /freebsd/contrib/llvm-project/llvm/include/llvm/PassAnalysisSupport.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/Support/Compiler.h"
28 #include <cassert>
29 #include <tuple>
30 #include <utility>
31 #include <vector>
32 
33 namespace llvm {
34 
35 class Function;
36 class Pass;
37 class PMDataManager;
38 class 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 ///
48 class AnalysisUsage {
49 public:
50   using VectorType = SmallVectorImpl<AnalysisID>;
51 
52 private:
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.
57   SmallVector<AnalysisID, 8> Required;
58   SmallVector<AnalysisID, 2> RequiredTransitive;
59   SmallVector<AnalysisID, 2> Preserved;
60   SmallVector<AnalysisID, 0> Used;
61   bool PreservesAll = false;
62 
pushUnique(VectorType & Set,AnalysisID ID)63   void pushUnique(VectorType &Set, AnalysisID ID) {
64     if (!llvm::is_contained(Set, ID))
65       Set.push_back(ID);
66   }
67 
68 public:
69   AnalysisUsage() = default;
70 
71   ///@{
72   /// Add the specified ID to the required set of the usage info for a pass.
73   LLVM_ABI AnalysisUsage &addRequiredID(const void *ID);
74   LLVM_ABI AnalysisUsage &addRequiredID(char &ID);
75   template<class PassClass>
addRequired()76   AnalysisUsage &addRequired() {
77     return addRequiredID(PassClass::ID);
78   }
79 
80   LLVM_ABI AnalysisUsage &addRequiredTransitiveID(char &ID);
81   template<class PassClass>
addRequiredTransitive()82   AnalysisUsage &addRequiredTransitive() {
83     return addRequiredTransitiveID(PassClass::ID);
84   }
85   ///@}
86 
87   ///@{
88   /// Add the specified ID to the set of analyses preserved by this pass.
addPreservedID(const void * ID)89   AnalysisUsage &addPreservedID(const void *ID) {
90     pushUnique(Preserved, ID);
91     return *this;
92   }
addPreservedID(char & ID)93   AnalysisUsage &addPreservedID(char &ID) {
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>
addPreserved()99   AnalysisUsage &addPreserved() {
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..
addUsedIfAvailableID(const void * ID)108   AnalysisUsage &addUsedIfAvailableID(const void *ID) {
109     pushUnique(Used, ID);
110     return *this;
111   }
addUsedIfAvailableID(char & ID)112   AnalysisUsage &addUsedIfAvailableID(char &ID) {
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>
addUsedIfAvailable()118   AnalysisUsage &addUsedIfAvailable() {
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!
128   LLVM_ABI AnalysisUsage &addPreserved(StringRef Arg);
129 
130   /// Set by analyses that do not transform their input at all
setPreservesAll()131   void setPreservesAll() { PreservesAll = true; }
132 
133   /// Determine whether a pass said it does not transform its input at all
getPreservesAll()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.
143   LLVM_ABI void setPreservesCFG();
144 
getRequiredSet()145   const VectorType &getRequiredSet() const { return Required; }
getRequiredTransitiveSet()146   const VectorType &getRequiredTransitiveSet() const {
147     return RequiredTransitive;
148   }
getPreservedSet()149   const VectorType &getPreservedSet() const { return Preserved; }
getUsedSet()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 ///
158 class AnalysisResolver {
159 public:
160   AnalysisResolver() = delete;
AnalysisResolver(PMDataManager & P)161   explicit AnalysisResolver(PMDataManager &P) : PM(P) {}
162 
getPMDataManager()163   PMDataManager &getPMDataManager() { return PM; }
164 
165   /// Find pass that is implementing PI.
findImplPass(AnalysisID PI)166   Pass *findImplPass(AnalysisID 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 
addAnalysisImplsPair(AnalysisID PI,Pass * P)181   void addAnalysisImplsPair(AnalysisID PI, Pass *P) {
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).
clearAnalysisImpls()189   void clearAnalysisImpls() {
190     AnalysisImpls.clear();
191   }
192 
193   /// Return analysis result or null if it doesn't exist.
194   LLVM_ABI Pass *getAnalysisIfAvailable(AnalysisID ID) const;
195 
196 private:
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.
212 template<typename AnalysisType>
getAnalysisIfAvailable()213 AnalysisType *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.
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   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.
248 template <typename AnalysisType>
getAnalysis(Function & F,bool * Changed)249 AnalysisType &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 
255 template <typename AnalysisType>
getAnalysisID(AnalysisID PI,Function & F,bool * Changed)256 AnalysisType &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
278