PassAnalysisSupport.h revision 6fbcc26f1460eaee4e0eb8b426fc1ff0c7af11be
1//===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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_PASS_ANALYSIS_SUPPORT_H
20#define LLVM_PASS_ANALYSIS_SUPPORT_H
21
22// No need to include Pass.h, we are being included by it!
23
24//===----------------------------------------------------------------------===//
25// AnalysisUsage - Represent the analysis usage information of a pass.  This
26// tracks analyses that the pass REQUIRES (must available when the pass runs),
27// and analyses that the pass PRESERVES (the pass does not invalidate the
28// results of these analyses).  This information is provided by a pass to the
29// Pass infrastructure through the getAnalysisUsage virtual function.
30//
31class AnalysisUsage {
32  // Sets of analyses required and preserved by a pass
33  std::vector<AnalysisID> Required, Preserved;
34  bool PreservesAll;
35public:
36  AnalysisUsage() : PreservesAll(false) {}
37
38  // addRequired - Add the specified ID to the required set of the usage info
39  // for a pass.
40  //
41  AnalysisUsage &addRequiredID(AnalysisID ID) {
42    Required.push_back(ID);
43    return *this;
44  }
45  template<class PassClass>
46  AnalysisUsage &addRequired() {
47    assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
48    Required.push_back(Pass::getClassPassInfo<PassClass>());
49    return *this;
50  }
51
52  // addPreserved - Add the specified ID to the set of analyses preserved by
53  // this pass
54  //
55  AnalysisUsage &addPreservedID(AnalysisID ID) {
56    Preserved.push_back(ID);
57    return *this;
58  }
59
60  template<class PassClass>
61  AnalysisUsage &addPreserved() {
62    assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
63    Preserved.push_back(Pass::getClassPassInfo<PassClass>());
64    return *this;
65  }
66
67  // setPreservesAll - Set by analyses that do not transform their input at all
68  void setPreservesAll() { PreservesAll = true; }
69  bool getPreservesAll() const { return PreservesAll; }
70
71  /// setPreservesCFG - This function should be called by the pass, iff they do
72  /// not:
73  ///
74  ///  1. Add or remove basic blocks from the function
75  ///  2. Modify terminator instructions in any way.
76  ///
77  /// This function annotates the AnalysisUsage info object to say that analyses
78  /// that only depend on the CFG are preserved by this pass.
79  ///
80  void setPreservesCFG();
81
82  const std::vector<AnalysisID> &getRequiredSet() const { return Required; }
83  const std::vector<AnalysisID> &getPreservedSet() const { return Preserved; }
84};
85
86
87
88//===----------------------------------------------------------------------===//
89// AnalysisResolver - Simple interface implemented by PassManager objects that
90// is used to pull analysis information out of them.
91//
92struct AnalysisResolver {
93  virtual Pass *getAnalysisOrNullUp(AnalysisID ID) const = 0;
94  virtual Pass *getAnalysisOrNullDown(AnalysisID ID) const = 0;
95  virtual void addPass(ImmutablePass *IP, AnalysisUsage &AU) = 0;
96  Pass *getAnalysis(AnalysisID ID) const {
97    Pass *Result = getAnalysisOrNullUp(ID);
98    assert(Result && "Pass has an incorrect analysis uses set!");
99    return Result;
100  }
101
102  // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
103  Pass *getAnalysisToUpdate(AnalysisID ID) const {
104    return getAnalysisOrNullUp(ID);
105  }
106
107  // Methods for introspecting into pass manager objects...
108  virtual unsigned getDepth() const = 0;
109  virtual unsigned getNumContainedPasses() const = 0;
110  virtual const Pass *getContainedPass(unsigned N) const = 0;
111
112  virtual void markPassUsed(AnalysisID P, Pass *User) = 0;
113
114  void startPass(Pass *P) {}
115  void endPass(Pass *P) {}
116protected:
117  void setAnalysisResolver(Pass *P, AnalysisResolver *AR);
118};
119
120/// getAnalysisToUpdate<AnalysisType>() - This function is used by subclasses
121/// to get to the analysis information that might be around that needs to be
122/// updated.  This is different than getAnalysis in that it can fail (ie the
123/// analysis results haven't been computed), so should only be used if you
124/// provide the capability to update an analysis that exists.  This method is
125/// often used by transformation APIs to update analysis results for a pass
126/// automatically as the transform is performed.
127///
128template<typename AnalysisType>
129AnalysisType *Pass::getAnalysisToUpdate() const {
130  assert(Resolver && "Pass not resident in a PassManager object!");
131  const PassInfo *PI = getClassPassInfo<AnalysisType>();
132  if (PI == 0) return 0;
133  return dynamic_cast<AnalysisType*>(Resolver->getAnalysisToUpdate(PI));
134}
135
136#endif
137