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