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