__builtin_ia32_movntdqa reads memory
[oota-llvm.git] / include / llvm / PassManagers.h
1 //===- llvm/PassManager.h - Pass Inftrastructre classes  --------*- 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 declares the LLVM Pass Manager infrastructure. 
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/PassManager.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include <deque>
17
18 //===----------------------------------------------------------------------===//
19 // Overview:
20 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
21 // 
22 //   o Manage optimization pass execution order
23 //   o Make required Analysis information available before pass P is run
24 //   o Release memory occupied by dead passes
25 //   o If Analysis information is dirtied by a pass then regenerate Analysis 
26 //     information before it is consumed by another pass.
27 //
28 // Pass Manager Infrastructure uses multiple pass managers.  They are
29 // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
30 // This class hierarcy uses multiple inheritance but pass managers do not derive
31 // from another pass manager.
32 //
33 // PassManager and FunctionPassManager are two top-level pass manager that
34 // represents the external interface of this entire pass manager infrastucture.
35 //
36 // Important classes :
37 //
38 // [o] class PMTopLevelManager;
39 //
40 // Two top level managers, PassManager and FunctionPassManager, derive from 
41 // PMTopLevelManager. PMTopLevelManager manages information used by top level 
42 // managers such as last user info.
43 //
44 // [o] class PMDataManager;
45 //
46 // PMDataManager manages information, e.g. list of available analysis info, 
47 // used by a pass manager to manage execution order of passes. It also provides
48 // a place to implement common pass manager APIs. All pass managers derive from
49 // PMDataManager.
50 //
51 // [o] class BBPassManager : public FunctionPass, public PMDataManager;
52 //
53 // BBPassManager manages BasicBlockPasses.
54 //
55 // [o] class FunctionPassManager;
56 //
57 // This is a external interface used by JIT to manage FunctionPasses. This
58 // interface relies on FunctionPassManagerImpl to do all the tasks.
59 //
60 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
61 //                                     public PMTopLevelManager;
62 //
63 // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
64 //
65 // [o] class FPPassManager : public ModulePass, public PMDataManager;
66 //
67 // FPPassManager manages FunctionPasses and BBPassManagers
68 //
69 // [o] class MPPassManager : public Pass, public PMDataManager;
70 //
71 // MPPassManager manages ModulePasses and FPPassManagers
72 //
73 // [o] class PassManager;
74 //
75 // This is a external interface used by various tools to manages passes. It
76 // relies on PassManagerImpl to do all the tasks.
77 //
78 // [o] class PassManagerImpl : public Pass, public PMDataManager,
79 //                             public PMDTopLevelManager
80 //
81 // PassManagerImpl is a top level pass manager responsible for managing
82 // MPPassManagers.
83 //===----------------------------------------------------------------------===//
84
85 #ifndef PASSMANAGERS_H
86 #define PASSMANAGERS_H
87
88 namespace llvm {
89
90 /// FunctionPassManager and PassManager, two top level managers, serve 
91 /// as the public interface of pass manager infrastructure.
92 enum TopLevelManagerType {
93   TLM_Function,  // FunctionPassManager
94   TLM_Pass       // PassManager
95 };
96     
97 // enums for debugging strings
98 enum PassDebuggingString {
99   EXECUTION_MSG, // "Executing Pass '"
100   MODIFICATION_MSG, // "' Made Modification '"
101   FREEING_MSG, // " Freeing Pass '"
102   ON_BASICBLOCK_MSG, // "'  on BasicBlock '" + PassName + "'...\n"
103   ON_FUNCTION_MSG, // "' on Function '" + FunctionName + "'...\n"
104   ON_MODULE_MSG, // "' on Module '" + ModuleName + "'...\n"
105   ON_LOOP_MSG, // " 'on Loop ...\n'"
106   ON_CG_MSG // "' on Call Graph ...\n'"
107 };  
108
109 //===----------------------------------------------------------------------===//
110 // PMStack
111 //
112 /// PMStack
113 /// Top level pass managers (see PassManager.cpp) maintain active Pass Managers 
114 /// using PMStack. Each Pass implements assignPassManager() to connect itself
115 /// with appropriate manager. assignPassManager() walks PMStack to find
116 /// suitable manager.
117 ///
118 /// PMStack is just a wrapper around standard deque that overrides pop() and
119 /// push() methods.
120 class PMStack {
121 public:
122   typedef std::deque<PMDataManager *>::reverse_iterator iterator;
123   iterator begin() { return S.rbegin(); }
124   iterator end() { return S.rend(); }
125
126   void handleLastUserOverflow();
127
128   void pop();
129   inline PMDataManager *top() { return S.back(); }
130   void push(PMDataManager *PM);
131   inline bool empty() { return S.empty(); }
132
133   void dump();
134 private:
135   std::deque<PMDataManager *> S;
136 };
137
138
139 //===----------------------------------------------------------------------===//
140 // PMTopLevelManager
141 //
142 /// PMTopLevelManager manages LastUser info and collects common APIs used by
143 /// top level pass managers.
144 class PMTopLevelManager {
145 public:
146
147   virtual unsigned getNumContainedManagers() {
148     return PassManagers.size();
149   }
150
151   /// Schedule pass P for execution. Make sure that passes required by
152   /// P are run before P is run. Update analysis info maintained by
153   /// the manager. Remove dead passes. This is a recursive function.
154   void schedulePass(Pass *P);
155
156   /// This is implemented by top level pass manager and used by 
157   /// schedulePass() to add analysis info passes that are not available.
158   virtual void addTopLevelPass(Pass  *P) = 0;
159
160   /// Set pass P as the last user of the given analysis passes.
161   void setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, Pass *P);
162
163   /// Collect passes whose last user is P
164   void collectLastUses(SmallVector<Pass *, 12> &LastUses, Pass *P);
165
166   /// Find the pass that implements Analysis AID. Search immutable
167   /// passes and all pass managers. If desired pass is not found
168   /// then return NULL.
169   Pass *findAnalysisPass(AnalysisID AID);
170
171   explicit PMTopLevelManager(enum TopLevelManagerType t);
172   virtual ~PMTopLevelManager(); 
173
174   /// Add immutable pass and initialize it.
175   inline void addImmutablePass(ImmutablePass *P) {
176     P->initializePass();
177     ImmutablePasses.push_back(P);
178   }
179
180   inline std::vector<ImmutablePass *>& getImmutablePasses() {
181     return ImmutablePasses;
182   }
183
184   void addPassManager(PMDataManager *Manager) {
185     PassManagers.push_back(Manager);
186   }
187
188   // Add Manager into the list of managers that are not directly
189   // maintained by this top level pass manager
190   inline void addIndirectPassManager(PMDataManager *Manager) {
191     IndirectPassManagers.push_back(Manager);
192   }
193
194   // Print passes managed by this top level manager.
195   void dumpPasses() const;
196   void dumpArguments() const;
197
198   void initializeAllAnalysisInfo();
199
200   // Active Pass Managers
201   PMStack activeStack;
202
203 protected:
204   
205   /// Collection of pass managers
206   std::vector<PMDataManager *> PassManagers;
207
208 private:
209
210   /// Collection of pass managers that are not directly maintained
211   /// by this pass manager
212   std::vector<PMDataManager *> IndirectPassManagers;
213
214   // Map to keep track of last user of the analysis pass.
215   // LastUser->second is the last user of Lastuser->first.
216   std::map<Pass *, Pass *> LastUser;
217
218   /// Immutable passes are managed by top level manager.
219   std::vector<ImmutablePass *> ImmutablePasses;
220 };
221
222
223   
224 //===----------------------------------------------------------------------===//
225 // PMDataManager
226
227 /// PMDataManager provides the common place to manage the analysis data
228 /// used by pass managers.
229 class PMDataManager {
230 public:
231
232   explicit PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
233     initializeAnalysisInfo();
234   }
235
236   virtual ~PMDataManager();
237
238   /// Augment AvailableAnalysis by adding analysis made available by pass P.
239   void recordAvailableAnalysis(Pass *P);
240
241   /// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
242   void verifyPreservedAnalysis(Pass *P);
243
244   /// Remove Analysis that is not preserved by the pass
245   void removeNotPreservedAnalysis(Pass *P);
246   
247   /// Remove dead passes
248   void removeDeadPasses(Pass *P, const char *Msg, enum PassDebuggingString);
249
250   /// Add pass P into the PassVector. Update 
251   /// AvailableAnalysis appropriately if ProcessAnalysis is true.
252   void add(Pass *P, bool ProcessAnalysis = true);
253
254   /// Add RequiredPass into list of lower level passes required by pass P.
255   /// RequiredPass is run on the fly by Pass Manager when P requests it
256   /// through getAnalysis interface.
257   virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
258
259   virtual Pass * getOnTheFlyPass(Pass *P, const PassInfo *PI, Function &F) {
260     assert (0 && "Unable to find on the fly pass");
261     return NULL;
262   }
263
264   /// Initialize available analysis information.
265   void initializeAnalysisInfo() { 
266     AvailableAnalysis.clear();
267     for (unsigned i = 0; i < PMT_Last; ++i)
268       InheritedAnalysis[i] = NULL;
269   }
270
271   // Return true if P preserves high level analysis used by other
272   // passes that are managed by this manager.
273   bool preserveHigherLevelAnalysis(Pass *P);
274
275
276   /// Populate RequiredPasses with analysis pass that are required by
277   /// pass P and are available. Populate ReqPassNotAvailable with analysis
278   /// pass that are required by pass P but are not available.
279   void collectRequiredAnalysis(SmallVector<Pass *, 8> &RequiredPasses,
280                                SmallVector<AnalysisID, 8> &ReqPassNotAvailable,
281                                Pass *P);
282
283   /// All Required analyses should be available to the pass as it runs!  Here
284   /// we fill in the AnalysisImpls member of the pass so that it can
285   /// successfully use the getAnalysis() method to retrieve the
286   /// implementations it needs.
287   void initializeAnalysisImpl(Pass *P);
288
289   /// Find the pass that implements Analysis AID. If desired pass is not found
290   /// then return NULL.
291   Pass *findAnalysisPass(AnalysisID AID, bool Direction);
292
293   // Access toplevel manager
294   PMTopLevelManager *getTopLevelManager() { return TPM; }
295   void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
296
297   unsigned getDepth() const { return Depth; }
298
299   // Print routines used by debug-pass
300   void dumpLastUses(Pass *P, unsigned Offset) const;
301   void dumpPassArguments() const;
302   void dumpPassInfo(Pass *P, enum PassDebuggingString S1,
303                     enum PassDebuggingString S2, const char *Msg);
304   void dumpAnalysisSetInfo(const char *Msg, Pass *P,
305                            const std::vector<AnalysisID> &Set) const;
306
307   virtual unsigned getNumContainedPasses() { 
308     return PassVector.size();
309   }
310
311   virtual PassManagerType getPassManagerType() const { 
312     assert ( 0 && "Invalid use of getPassManagerType");
313     return PMT_Unknown; 
314   }
315
316   std::map<AnalysisID, Pass*> *getAvailableAnalysis() {
317     return &AvailableAnalysis;
318   }
319
320   // Collect AvailableAnalysis from all the active Pass Managers.
321   void populateInheritedAnalysis(PMStack &PMS) {
322     unsigned Index = 0;
323     for (PMStack::iterator I = PMS.begin(), E = PMS.end();
324          I != E; ++I)
325       InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis();
326   }
327
328 protected:
329
330   // Top level manager.
331   PMTopLevelManager *TPM;
332
333   // Collection of pass that are managed by this manager
334   std::vector<Pass *> PassVector;
335
336   // Collection of Analysis provided by Parent pass manager and
337   // used by current pass manager. At at time there can not be more
338   // then PMT_Last active pass mangers.
339   std::map<AnalysisID, Pass *> *InheritedAnalysis[PMT_Last];
340
341 private:
342   // Set of available Analysis. This information is used while scheduling 
343   // pass. If a pass requires an analysis which is not not available then 
344   // equired analysis pass is scheduled to run before the pass itself is 
345   // scheduled to run.
346   std::map<AnalysisID, Pass*> AvailableAnalysis;
347
348   // Collection of higher level analysis used by the pass managed by
349   // this manager.
350   std::vector<Pass *> HigherLevelAnalysis;
351
352   unsigned Depth;
353 };
354
355 //===----------------------------------------------------------------------===//
356 // FPPassManager
357 //
358 /// FPPassManager manages BBPassManagers and FunctionPasses.
359 /// It batches all function passes and basic block pass managers together and 
360 /// sequence them to process one function at a time before processing next 
361 /// function.
362
363 class FPPassManager : public ModulePass, public PMDataManager {
364  
365 public:
366   static char ID;
367   explicit FPPassManager(int Depth) 
368   : ModulePass(intptr_t(&ID)), PMDataManager(Depth) { }
369   
370   /// run - Execute all of the passes scheduled for execution.  Keep track of
371   /// whether any of the passes modifies the module, and if so, return true.
372   bool runOnFunction(Function &F);
373   bool runOnModule(Module &M);
374
375   /// doInitialization - Run all of the initializers for the function passes.
376   ///
377   bool doInitialization(Module &M);
378   
379   /// doFinalization - Run all of the finalizers for the function passes.
380   ///
381   bool doFinalization(Module &M);
382
383   /// Pass Manager itself does not invalidate any analysis info.
384   void getAnalysisUsage(AnalysisUsage &Info) const {
385     Info.setPreservesAll();
386   }
387
388   // Print passes managed by this manager
389   void dumpPassStructure(unsigned Offset);
390
391   virtual const char *getPassName() const {
392     return "Function Pass Manager";
393   }
394
395   FunctionPass *getContainedPass(unsigned N) {
396     assert ( N < PassVector.size() && "Pass number out of range!");
397     FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
398     return FP;
399   }
400
401   virtual PassManagerType getPassManagerType() const { 
402     return PMT_FunctionPassManager; 
403   }
404 };
405
406 }
407
408 extern void StartPassTimer(llvm::Pass *);
409 extern void StopPassTimer(llvm::Pass *);
410
411 #endif
412