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
27namespace llvm {
28
29//===----------------------------------------------------------------------===//
30// AnalysisUsage - Represent the analysis usage information of a pass.  This
31// tracks analyses that the pass REQUIRES (must be available when the pass
32// runs), REQUIRES TRANSITIVE (must be available throughout the lifetime of the
33// pass), and analyses that the pass PRESERVES (the pass does not invalidate the
34// results of these analyses).  This information is provided by a pass to the
35// Pass infrastructure through the getAnalysisUsage virtual function.
36//
37class AnalysisUsage {
38public:
39  typedef SmallVector<AnalysisID, 32> VectorType;
40
41private:
42  // Sets of analyses required and preserved by a pass
43  VectorType Required, RequiredTransitive, Preserved;
44  bool PreservesAll;
45
46public:
47  AnalysisUsage() : PreservesAll(false) {}
48
49  // addRequired - Add the specified ID to the required set of the usage info
50  // for a pass.
51  //
52  AnalysisUsage &addRequiredID(const void *ID);
53  AnalysisUsage &addRequiredID(char &ID);
54  template<class PassClass>
55  AnalysisUsage &addRequired() {
56    return addRequiredID(PassClass::ID);
57  }
58
59  AnalysisUsage &addRequiredTransitiveID(char &ID);
60  template<class PassClass>
61  AnalysisUsage &addRequiredTransitive() {
62    return addRequiredTransitiveID(PassClass::ID);
63  }
64
65  // addPreserved - Add the specified ID to the set of analyses preserved by
66  // this pass
67  //
68  AnalysisUsage &addPreservedID(const void *ID) {
69    Preserved.push_back(ID);
70    return *this;
71  }
72  AnalysisUsage &addPreservedID(char &ID) {
73    Preserved.push_back(&ID);
74    return *this;
75  }
76
77  // addPreserved - Add the specified Pass class to the set of analyses
78  // preserved by this pass.
79  //
80  template<class PassClass>
81  AnalysisUsage &addPreserved() {
82    Preserved.push_back(&PassClass::ID);
83    return *this;
84  }
85
86  // addPreserved - Add the Pass with the specified argument string to the set
87  // of analyses preserved by this pass. If no such Pass exists, do nothing.
88  // This can be useful when a pass is trivially preserved, but may not be
89  // linked in. Be careful about spelling!
90  //
91  AnalysisUsage &addPreserved(StringRef Arg);
92
93  // setPreservesAll - Set by analyses that do not transform their input at all
94  void setPreservesAll() { PreservesAll = true; }
95  bool getPreservesAll() const { return PreservesAll; }
96
97  /// setPreservesCFG - This function should be called by the pass, iff they do
98  /// not:
99  ///
100  ///  1. Add or remove basic blocks from the function
101  ///  2. Modify terminator instructions in any way.
102  ///
103  /// This function annotates the AnalysisUsage info object to say that analyses
104  /// that only depend on the CFG are preserved by this pass.
105  ///
106  void setPreservesCFG();
107
108  const VectorType &getRequiredSet() const { return Required; }
109  const VectorType &getRequiredTransitiveSet() const {
110    return RequiredTransitive;
111  }
112  const VectorType &getPreservedSet() const { return Preserved; }
113};
114
115//===----------------------------------------------------------------------===//
116// AnalysisResolver - Simple interface used by Pass objects to pull all
117// analysis information out of pass manager that is responsible to manage
118// the pass.
119//
120class PMDataManager;
121class AnalysisResolver {
122private:
123  AnalysisResolver() LLVM_DELETED_FUNCTION;
124
125public:
126  explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
127
128  inline PMDataManager &getPMDataManager() { return PM; }
129
130  // Find pass that is implementing PI.
131  Pass *findImplPass(AnalysisID PI) {
132    Pass *ResultPass = 0;
133    for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) {
134      if (AnalysisImpls[i].first == PI) {
135        ResultPass = AnalysisImpls[i].second;
136        break;
137      }
138    }
139    return ResultPass;
140  }
141
142  // Find pass that is implementing PI. Initialize pass for Function F.
143  Pass *findImplPass(Pass *P, AnalysisID PI, Function &F);
144
145  void addAnalysisImplsPair(AnalysisID PI, Pass *P) {
146    if (findImplPass(PI) == P)
147      return;
148    std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P);
149    AnalysisImpls.push_back(pir);
150  }
151
152  /// clearAnalysisImpls - Clear cache that is used to connect a pass to the
153  /// the analysis (PassInfo).
154  void clearAnalysisImpls() {
155    AnalysisImpls.clear();
156  }
157
158  // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist
159  Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
160
161private:
162  // AnalysisImpls - This keeps track of which passes implements the interfaces
163  // that are required by the current pass (to implement getAnalysis()).
164  std::vector<std::pair<AnalysisID, Pass*> > AnalysisImpls;
165
166  // PassManager that is used to resolve analysis info
167  PMDataManager &PM;
168};
169
170/// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
171/// get analysis information that might be around, for example to update it.
172/// This is different than getAnalysis in that it can fail (if the analysis
173/// results haven't been computed), so should only be used if you can handle
174/// the case when the analysis is not available.  This method is often used by
175/// transformation APIs to update analysis results for a pass automatically as
176/// the transform is performed.
177///
178template<typename AnalysisType>
179AnalysisType *Pass::getAnalysisIfAvailable() const {
180  assert(Resolver && "Pass not resident in a PassManager object!");
181
182  const void *PI = &AnalysisType::ID;
183
184  Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true);
185  if (ResultPass == 0) return 0;
186
187  // Because the AnalysisType may not be a subclass of pass (for
188  // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
189  // adjust the return pointer (because the class may multiply inherit, once
190  // from pass, once from AnalysisType).
191  return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
192}
193
194/// getAnalysis<AnalysisType>() - This function is used by subclasses to get
195/// to the analysis information that they claim to use by overriding the
196/// getAnalysisUsage function.
197///
198template<typename AnalysisType>
199AnalysisType &Pass::getAnalysis() const {
200  assert(Resolver && "Pass has not been inserted into a PassManager object!");
201  return getAnalysisID<AnalysisType>(&AnalysisType::ID);
202}
203
204template<typename AnalysisType>
205AnalysisType &Pass::getAnalysisID(AnalysisID PI) const {
206  assert(PI && "getAnalysis for unregistered pass!");
207  assert(Resolver&&"Pass has not been inserted into a PassManager object!");
208  // PI *must* appear in AnalysisImpls.  Because the number of passes used
209  // should be a small number, we just do a linear search over a (dense)
210  // vector.
211  Pass *ResultPass = Resolver->findImplPass(PI);
212  assert (ResultPass &&
213          "getAnalysis*() called on an analysis that was not "
214          "'required' by pass!");
215
216  // Because the AnalysisType may not be a subclass of pass (for
217  // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
218  // adjust the return pointer (because the class may multiply inherit, once
219  // from pass, once from AnalysisType).
220  return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
221}
222
223/// getAnalysis<AnalysisType>() - This function is used by subclasses to get
224/// to the analysis information that they claim to use by overriding the
225/// getAnalysisUsage function.
226///
227template<typename AnalysisType>
228AnalysisType &Pass::getAnalysis(Function &F) {
229  assert(Resolver &&"Pass has not been inserted into a PassManager object!");
230
231  return getAnalysisID<AnalysisType>(&AnalysisType::ID, F);
232}
233
234template<typename AnalysisType>
235AnalysisType &Pass::getAnalysisID(AnalysisID PI, Function &F) {
236  assert(PI && "getAnalysis for unregistered pass!");
237  assert(Resolver && "Pass has not been inserted into a PassManager object!");
238  // PI *must* appear in AnalysisImpls.  Because the number of passes used
239  // should be a small number, we just do a linear search over a (dense)
240  // vector.
241  Pass *ResultPass = Resolver->findImplPass(this, PI, F);
242  assert(ResultPass && "Unable to find requested analysis info");
243
244  // Because the AnalysisType may not be a subclass of pass (for
245  // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
246  // adjust the return pointer (because the class may multiply inherit, once
247  // from pass, once from AnalysisType).
248  return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
249}
250
251} // End llvm namespace
252
253#endif
254