Move PMTopLevelManager, PMDataManager and FPPassManger classes into
[oota-llvm.git] / include / llvm / PassManagers.h
1 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Devang Patel and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LLVM Pass Manager infrastructure. 
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/PassManager.h"
15
16 using namespace llvm;
17 class llvm::PMDataManager;
18 class llvm::PMStack;
19
20 //===----------------------------------------------------------------------===//
21 // Overview:
22 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
23 // 
24 //   o Manage optimization pass execution order
25 //   o Make required Analysis information available before pass P is run
26 //   o Release memory occupied by dead passes
27 //   o If Analysis information is dirtied by a pass then regenerate Analysis 
28 //     information before it is consumed by another pass.
29 //
30 // Pass Manager Infrastructure uses multiple pass managers.  They are
31 // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
32 // This class hierarcy uses multiple inheritance but pass managers do not derive
33 // from another pass manager.
34 //
35 // PassManager and FunctionPassManager are two top-level pass manager that
36 // represents the external interface of this entire pass manager infrastucture.
37 //
38 // Important classes :
39 //
40 // [o] class PMTopLevelManager;
41 //
42 // Two top level managers, PassManager and FunctionPassManager, derive from 
43 // PMTopLevelManager. PMTopLevelManager manages information used by top level 
44 // managers such as last user info.
45 //
46 // [o] class PMDataManager;
47 //
48 // PMDataManager manages information, e.g. list of available analysis info, 
49 // used by a pass manager to manage execution order of passes. It also provides
50 // a place to implement common pass manager APIs. All pass managers derive from
51 // PMDataManager.
52 //
53 // [o] class BBPassManager : public FunctionPass, public PMDataManager;
54 //
55 // BBPassManager manages BasicBlockPasses.
56 //
57 // [o] class FunctionPassManager;
58 //
59 // This is a external interface used by JIT to manage FunctionPasses. This
60 // interface relies on FunctionPassManagerImpl to do all the tasks.
61 //
62 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
63 //                                     public PMTopLevelManager;
64 //
65 // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
66 //
67 // [o] class FPPassManager : public ModulePass, public PMDataManager;
68 //
69 // FPPassManager manages FunctionPasses and BBPassManagers
70 //
71 // [o] class MPPassManager : public Pass, public PMDataManager;
72 //
73 // MPPassManager manages ModulePasses and FPPassManagers
74 //
75 // [o] class PassManager;
76 //
77 // This is a external interface used by various tools to manages passes. It
78 // relies on PassManagerImpl to do all the tasks.
79 //
80 // [o] class PassManagerImpl : public Pass, public PMDataManager,
81 //                             public PMDTopLevelManager
82 //
83 // PassManagerImpl is a top level pass manager responsible for managing
84 // MPPassManagers.
85 //===----------------------------------------------------------------------===//
86
87 namespace llvm {
88
89 //===----------------------------------------------------------------------===//
90 // PMTopLevelManager
91 //
92 /// PMTopLevelManager manages LastUser info and collects common APIs used by
93 /// top level pass managers.
94 class PMTopLevelManager {
95 public:
96
97   virtual unsigned getNumContainedManagers() {
98     return PassManagers.size();
99   }
100
101   /// Schedule pass P for execution. Make sure that passes required by
102   /// P are run before P is run. Update analysis info maintained by
103   /// the manager. Remove dead passes. This is a recursive function.
104   void schedulePass(Pass *P);
105
106   /// This is implemented by top level pass manager and used by 
107   /// schedulePass() to add analysis info passes that are not available.
108   virtual void addTopLevelPass(Pass  *P) = 0;
109
110   /// Set pass P as the last user of the given analysis passes.
111   void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
112
113   /// Collect passes whose last user is P
114   void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
115
116   /// Find the pass that implements Analysis AID. Search immutable
117   /// passes and all pass managers. If desired pass is not found
118   /// then return NULL.
119   Pass *findAnalysisPass(AnalysisID AID);
120
121   virtual ~PMTopLevelManager(); 
122
123   /// Add immutable pass and initialize it.
124   inline void addImmutablePass(ImmutablePass *P) {
125     P->initializePass();
126     ImmutablePasses.push_back(P);
127   }
128
129   inline std::vector<ImmutablePass *>& getImmutablePasses() {
130     return ImmutablePasses;
131   }
132
133   void addPassManager(Pass *Manager) {
134     PassManagers.push_back(Manager);
135   }
136
137   // Add Manager into the list of managers that are not directly
138   // maintained by this top level pass manager
139   inline void addIndirectPassManager(PMDataManager *Manager) {
140     IndirectPassManagers.push_back(Manager);
141   }
142
143   // Print passes managed by this top level manager.
144   void dumpPasses() const;
145   void dumpArguments() const;
146
147   void initializeAllAnalysisInfo();
148
149   // Active Pass Managers
150   PMStack activeStack;
151
152 protected:
153   
154   /// Collection of pass managers
155   std::vector<Pass *> PassManagers;
156
157 private:
158
159   /// Collection of pass managers that are not directly maintained
160   /// by this pass manager
161   std::vector<PMDataManager *> IndirectPassManagers;
162
163   // Map to keep track of last user of the analysis pass.
164   // LastUser->second is the last user of Lastuser->first.
165   std::map<Pass *, Pass *> LastUser;
166
167   /// Immutable passes are managed by top level manager.
168   std::vector<ImmutablePass *> ImmutablePasses;
169 };
170
171
172   
173 //===----------------------------------------------------------------------===//
174 // PMDataManager
175
176 /// PMDataManager provides the common place to manage the analysis data
177 /// used by pass managers.
178 class PMDataManager {
179 public:
180   PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
181     initializeAnalysisInfo();
182   }
183
184   virtual ~PMDataManager();
185
186   /// Return true IFF pass P's required analysis set does not required new
187   /// manager.
188   bool manageablePass(Pass *P);
189
190   /// Augment AvailableAnalysis by adding analysis made available by pass P.
191   void recordAvailableAnalysis(Pass *P);
192
193   /// Remove Analysis that is not preserved by the pass
194   void removeNotPreservedAnalysis(Pass *P);
195   
196   /// Remove dead passes
197   void removeDeadPasses(Pass *P, std::string &Msg);
198
199   /// Add pass P into the PassVector. Update 
200   /// AvailableAnalysis appropriately if ProcessAnalysis is true.
201   void addPassToManager(Pass *P, bool ProcessAnalysis = true);
202
203   /// Initialize available analysis information.
204   void initializeAnalysisInfo() { 
205     TransferLastUses.clear();
206     AvailableAnalysis.clear();
207   }
208
209   /// Populate RequiredPasses with the analysis pass that are required by
210   /// pass P.
211   void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
212                                      Pass *P);
213
214   /// All Required analyses should be available to the pass as it runs!  Here
215   /// we fill in the AnalysisImpls member of the pass so that it can
216   /// successfully use the getAnalysis() method to retrieve the
217   /// implementations it needs.
218   void initializeAnalysisImpl(Pass *P);
219
220   /// Find the pass that implements Analysis AID. If desired pass is not found
221   /// then return NULL.
222   Pass *findAnalysisPass(AnalysisID AID, bool Direction);
223
224   // Access toplevel manager
225   PMTopLevelManager *getTopLevelManager() { return TPM; }
226   void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
227
228   unsigned getDepth() const { return Depth; }
229
230   // Print routines used by debug-pass
231   void dumpLastUses(Pass *P, unsigned Offset) const;
232   void dumpPassArguments() const;
233   void dumpPassInfo(Pass *P,  std::string &Msg1, std::string &Msg2) const;
234   void dumpAnalysisSetInfo(const char *Msg, Pass *P,
235                            const std::vector<AnalysisID> &Set) const;
236
237   std::vector<Pass *>& getTransferredLastUses() {
238     return TransferLastUses;
239   }
240
241   virtual unsigned getNumContainedPasses() { 
242     return PassVector.size();
243   }
244
245   virtual PassManagerType getPassManagerType() { 
246     assert ( 0 && "Invalid use of getPassManagerType");
247     return PMT_Unknown; 
248   }
249 protected:
250
251   // If a FunctionPass F is the last user of ModulePass info M
252   // then the F's manager, not F, records itself as a last user of M.
253   // Current pass manage is requesting parent manager to record parent
254   // manager as the last user of these TrransferLastUses passes.
255   std::vector<Pass *> TransferLastUses;
256
257   // Top level manager.
258   PMTopLevelManager *TPM;
259
260   // Collection of pass that are managed by this manager
261   std::vector<Pass *> PassVector;
262
263 private:
264   // Set of available Analysis. This information is used while scheduling 
265   // pass. If a pass requires an analysis which is not not available then 
266   // equired analysis pass is scheduled to run before the pass itself is 
267   // scheduled to run.
268   std::map<AnalysisID, Pass*> AvailableAnalysis;
269
270   unsigned Depth;
271 };
272
273 //===----------------------------------------------------------------------===//
274 // FPPassManager
275 //
276 /// FPPassManager manages BBPassManagers and FunctionPasses.
277 /// It batches all function passes and basic block pass managers together and 
278 /// sequence them to process one function at a time before processing next 
279 /// function.
280
281 class FPPassManager : public ModulePass, public PMDataManager {
282  
283 public:
284   FPPassManager(int Depth) : PMDataManager(Depth) { }
285   
286   /// run - Execute all of the passes scheduled for execution.  Keep track of
287   /// whether any of the passes modifies the module, and if so, return true.
288   bool runOnFunction(Function &F);
289   bool runOnModule(Module &M);
290
291   /// doInitialization - Run all of the initializers for the function passes.
292   ///
293   bool doInitialization(Module &M);
294   
295   /// doFinalization - Run all of the initializers for the function passes.
296   ///
297   bool doFinalization(Module &M);
298
299   /// Pass Manager itself does not invalidate any analysis info.
300   void getAnalysisUsage(AnalysisUsage &Info) const {
301     Info.setPreservesAll();
302   }
303
304   // Print passes managed by this manager
305   void dumpPassStructure(unsigned Offset);
306
307   FunctionPass *getContainedPass(unsigned N) {
308     assert ( N < PassVector.size() && "Pass number out of range!");
309     FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
310     return FP;
311   }
312
313   virtual PassManagerType getPassManagerType() { 
314     return PMT_FunctionPassManager; 
315   }
316 };
317
318 }
319