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