LLVM  3.7.0
PassAnalysisSupport.h
Go to the documentation of this file.
1 //===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines stuff that is used to define and "use" Analysis Passes.
11 // This file is automatically #included by Pass.h, so:
12 //
13 // NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
14 //
15 // Instead, #include Pass.h
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_PASSANALYSISSUPPORT_H
20 #define LLVM_PASSANALYSISSUPPORT_H
21 
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Pass.h"
25 #include <vector>
26 
27 namespace llvm {
28 
29 //===----------------------------------------------------------------------===//
30 /// Represent the analysis usage information of a pass. This tracks analyses
31 /// that the pass REQUIRES (must be available when the pass runs), REQUIRES
32 /// TRANSITIVE (must be available throughout the lifetime of the pass), and
33 /// analyses that the pass PRESERVES (the pass does not invalidate the results
34 /// of these analyses). This information is provided by a pass to the Pass
35 /// infrastructure through the getAnalysisUsage virtual function.
36 ///
38 public:
40 
41 private:
42  /// Sets of analyses required and preserved by a pass
43  VectorType Required, RequiredTransitive, Preserved;
44  bool PreservesAll;
45 
46 public:
47  AnalysisUsage() : PreservesAll(false) {}
48 
49  ///@{
50  /// Add the specified ID to the required set of the usage info for a pass.
51  AnalysisUsage &addRequiredID(const void *ID);
53  template<class PassClass>
56  }
57 
59  template<class PassClass>
62  }
63  ///@}
64 
65  ///@{
66  /// Add the specified ID to the set of analyses preserved by this pass.
68  Preserved.push_back(ID);
69  return *this;
70  }
72  Preserved.push_back(&ID);
73  return *this;
74  }
75  ///@}
76 
77  /// Add the specified Pass class to the set of analyses preserved by this pass.
78  template<class PassClass>
80  Preserved.push_back(&PassClass::ID);
81  return *this;
82  }
83 
84  /// Add the Pass with the specified argument string to the set of analyses
85  /// preserved by this pass. If no such Pass exists, do nothing. This can be
86  /// useful when a pass is trivially preserved, but may not be linked in. Be
87  /// careful about spelling!
89 
90  /// Set by analyses that do not transform their input at all
91  void setPreservesAll() { PreservesAll = true; }
92 
93  /// Determine whether a pass said it does not transform its input at all
94  bool getPreservesAll() const { return PreservesAll; }
95 
96  /// This function should be called by the pass, iff they do not:
97  ///
98  /// 1. Add or remove basic blocks from the function
99  /// 2. Modify terminator instructions in any way.
100  ///
101  /// This function annotates the AnalysisUsage info object to say that analyses
102  /// that only depend on the CFG are preserved by this pass.
103  ///
104  void setPreservesCFG();
105 
106  const VectorType &getRequiredSet() const { return Required; }
108  return RequiredTransitive;
109  }
110  const VectorType &getPreservedSet() const { return Preserved; }
111 };
112 
113 //===----------------------------------------------------------------------===//
114 /// AnalysisResolver - Simple interface used by Pass objects to pull all
115 /// analysis information out of pass manager that is responsible to manage
116 /// the pass.
117 ///
118 class PMDataManager;
120 private:
121  AnalysisResolver() = delete;
122 
123 public:
124  explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
125 
126  inline PMDataManager &getPMDataManager() { return PM; }
127 
128  /// Find pass that is implementing PI.
130  Pass *ResultPass = nullptr;
131  for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) {
132  if (AnalysisImpls[i].first == PI) {
133  ResultPass = AnalysisImpls[i].second;
134  break;
135  }
136  }
137  return ResultPass;
138  }
139 
140  /// Find pass that is implementing PI. Initialize pass for Function F.
142 
144  if (findImplPass(PI) == P)
145  return;
146  std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P);
147  AnalysisImpls.push_back(pir);
148  }
149 
150  /// Clear cache that is used to connect a pass to the the analysis (PassInfo).
152  AnalysisImpls.clear();
153  }
154 
155  /// Return analysis result or null if it doesn't exist.
156  Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
157 
158 private:
159  /// This keeps track of which passes implements the interfaces that are
160  /// required by the current pass (to implement getAnalysis()).
161  std::vector<std::pair<AnalysisID, Pass*> > AnalysisImpls;
162 
163  /// PassManager that is used to resolve analysis info
164  PMDataManager &PM;
165 };
166 
167 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
168 /// get analysis information that might be around, for example to update it.
169 /// This is different than getAnalysis in that it can fail (if the analysis
170 /// results haven't been computed), so should only be used if you can handle
171 /// the case when the analysis is not available. This method is often used by
172 /// transformation APIs to update analysis results for a pass automatically as
173 /// the transform is performed.
174 ///
175 template<typename AnalysisType>
176 AnalysisType *Pass::getAnalysisIfAvailable() const {
177  assert(Resolver && "Pass not resident in a PassManager object!");
178 
179  const void *PI = &AnalysisType::ID;
180 
181  Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true);
182  if (!ResultPass) return nullptr;
183 
184  // Because the AnalysisType may not be a subclass of pass (for
185  // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
186  // adjust the return pointer (because the class may multiply inherit, once
187  // from pass, once from AnalysisType).
188  return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
189 }
190 
191 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
192 /// to the analysis information that they claim to use by overriding the
193 /// getAnalysisUsage function.
194 ///
195 template<typename AnalysisType>
196 AnalysisType &Pass::getAnalysis() const {
197  assert(Resolver && "Pass has not been inserted into a PassManager object!");
198  return getAnalysisID<AnalysisType>(&AnalysisType::ID);
199 }
200 
201 template<typename AnalysisType>
202 AnalysisType &Pass::getAnalysisID(AnalysisID PI) const {
203  assert(PI && "getAnalysis for unregistered pass!");
204  assert(Resolver&&"Pass has not been inserted into a PassManager object!");
205  // PI *must* appear in AnalysisImpls. Because the number of passes used
206  // should be a small number, we just do a linear search over a (dense)
207  // vector.
208  Pass *ResultPass = Resolver->findImplPass(PI);
209  assert (ResultPass &&
210  "getAnalysis*() called on an analysis that was not "
211  "'required' by pass!");
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 ///
224 template<typename AnalysisType>
225 AnalysisType &Pass::getAnalysis(Function &F) {
226  assert(Resolver &&"Pass has not been inserted into a PassManager object!");
227 
228  return getAnalysisID<AnalysisType>(&AnalysisType::ID, F);
229 }
230 
231 template<typename AnalysisType>
233  assert(PI && "getAnalysis for unregistered pass!");
234  assert(Resolver && "Pass has not been inserted into a PassManager object!");
235  // PI *must* appear in AnalysisImpls. Because the number of passes used
236  // should be a small number, we just do a linear search over a (dense)
237  // vector.
238  Pass *ResultPass = Resolver->findImplPass(this, PI, F);
239  assert(ResultPass && "Unable to find requested analysis info");
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 } // End llvm namespace
249 
250 #endif
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:82
void push_back(const T &Elt)
Definition: SmallVector.h:222
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
AnalysisType & getAnalysisID(AnalysisID PI) const
const VectorType & getPreservedSet() const
F(f)
AnalysisUsage & addRequired()
AnalysisType * getAnalysisIfAvailable() const
getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to get analysis information tha...
AnalysisUsage & addPreservedID(char &ID)
#define false
Definition: ConvertUTF.c:65
Pass * getAnalysisIfAvailable(AnalysisID ID, bool Direction) const
Return analysis result or null if it doesn't exist.
AnalysisUsage & addPreservedID(const void *ID)
AnalysisResolver(PMDataManager &P)
#define P(N)
const VectorType & getRequiredTransitiveSet() const
Represent the analysis usage information of a pass.
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:276
bool getPreservesAll() const
Determine whether a pass said it does not transform its input at all.
SmallVector< AnalysisID, 32 > VectorType
void addAnalysisImplsPair(AnalysisID PI, Pass *P)
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:263
void clearAnalysisImpls()
Clear cache that is used to connect a pass to the the analysis (PassInfo).
Pass * findImplPass(AnalysisID PI)
Find pass that is implementing PI.
void setPreservesAll()
Set by analyses that do not transform their input at all.
const VectorType & getRequiredSet() const
PMDataManager & getPMDataManager()
AnalysisUsage & addRequiredTransitiveID(char &ID)
Definition: Pass.cpp:286
AnalysisUsage & addRequiredTransitive()
const void * AnalysisID
Definition: Pass.h:47
PMDataManager provides the common place to manage the analysis data used by pass managers.
virtual void * getAdjustedAnalysisPointer(AnalysisID ID)
getAdjustedAnalysisPointer - This method is used when a pass implements an analysis interface through...
Definition: Pass.cpp:90
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40