1 //===- llvm/PassManager.h - Pass Inftrastructre classes --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the LLVM Pass Manager infrastructure.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/PassManager.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/DenseMap.h"
21 //===----------------------------------------------------------------------===//
23 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
25 // o Manage optimization pass execution order
26 // o Make required Analysis information available before pass P is run
27 // o Release memory occupied by dead passes
28 // o If Analysis information is dirtied by a pass then regenerate Analysis
29 // information before it is consumed by another pass.
31 // Pass Manager Infrastructure uses multiple pass managers. They are
32 // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
33 // This class hierarchy uses multiple inheritance but pass managers do not
34 // derive from another pass manager.
36 // PassManager and FunctionPassManager are two top-level pass manager that
37 // represents the external interface of this entire pass manager infrastucture.
39 // Important classes :
41 // [o] class PMTopLevelManager;
43 // Two top level managers, PassManager and FunctionPassManager, derive from
44 // PMTopLevelManager. PMTopLevelManager manages information used by top level
45 // managers such as last user info.
47 // [o] class PMDataManager;
49 // PMDataManager manages information, e.g. list of available analysis info,
50 // used by a pass manager to manage execution order of passes. It also provides
51 // a place to implement common pass manager APIs. All pass managers derive from
54 // [o] class BBPassManager : public FunctionPass, public PMDataManager;
56 // BBPassManager manages BasicBlockPasses.
58 // [o] class FunctionPassManager;
60 // This is a external interface used by JIT to manage FunctionPasses. This
61 // interface relies on FunctionPassManagerImpl to do all the tasks.
63 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
64 // public PMTopLevelManager;
66 // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
68 // [o] class FPPassManager : public ModulePass, public PMDataManager;
70 // FPPassManager manages FunctionPasses and BBPassManagers
72 // [o] class MPPassManager : public Pass, public PMDataManager;
74 // MPPassManager manages ModulePasses and FPPassManagers
76 // [o] class PassManager;
78 // This is a external interface used by various tools to manages passes. It
79 // relies on PassManagerImpl to do all the tasks.
81 // [o] class PassManagerImpl : public Pass, public PMDataManager,
82 // public PMDTopLevelManager
84 // PassManagerImpl is a top level pass manager responsible for managing
86 //===----------------------------------------------------------------------===//
88 #ifndef PASSMANAGERS_H
89 #define PASSMANAGERS_H
91 #include "llvm/Pass.h"
96 /// FunctionPassManager and PassManager, two top level managers, serve
97 /// as the public interface of pass manager infrastructure.
98 enum TopLevelManagerType {
99 TLM_Function, // FunctionPassManager
100 TLM_Pass // PassManager
103 // enums for debugging strings
104 enum PassDebuggingString {
105 EXECUTION_MSG, // "Executing Pass '"
106 MODIFICATION_MSG, // "' Made Modification '"
107 FREEING_MSG, // " Freeing Pass '"
108 ON_BASICBLOCK_MSG, // "' on BasicBlock '" + PassName + "'...\n"
109 ON_FUNCTION_MSG, // "' on Function '" + FunctionName + "'...\n"
110 ON_MODULE_MSG, // "' on Module '" + ModuleName + "'...\n"
111 ON_LOOP_MSG, // " 'on Loop ...\n'"
112 ON_CG_MSG // "' on Call Graph ...\n'"
115 //===----------------------------------------------------------------------===//
119 /// Top level pass managers (see PassManager.cpp) maintain active Pass Managers
120 /// using PMStack. Each Pass implements assignPassManager() to connect itself
121 /// with appropriate manager. assignPassManager() walks PMStack to find
122 /// suitable manager.
124 /// PMStack is just a wrapper around standard deque that overrides pop() and
128 typedef std::deque<PMDataManager *>::reverse_iterator iterator;
129 iterator begin() { return S.rbegin(); }
130 iterator end() { return S.rend(); }
132 void handleLastUserOverflow();
135 inline PMDataManager *top() { return S.back(); }
136 void push(PMDataManager *PM);
137 inline bool empty() { return S.empty(); }
141 std::deque<PMDataManager *> S;
145 //===----------------------------------------------------------------------===//
148 /// PMTopLevelManager manages LastUser info and collects common APIs used by
149 /// top level pass managers.
150 class PMTopLevelManager {
153 virtual unsigned getNumContainedManagers() const {
154 return (unsigned)PassManagers.size();
157 /// Schedule pass P for execution. Make sure that passes required by
158 /// P are run before P is run. Update analysis info maintained by
159 /// the manager. Remove dead passes. This is a recursive function.
160 void schedulePass(Pass *P);
162 /// This is implemented by top level pass manager and used by
163 /// schedulePass() to add analysis info passes that are not available.
164 virtual void addTopLevelPass(Pass *P) = 0;
166 /// Set pass P as the last user of the given analysis passes.
167 void setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, Pass *P);
169 /// Collect passes whose last user is P
170 void collectLastUses(SmallVector<Pass *, 12> &LastUses, Pass *P);
172 /// Find the pass that implements Analysis AID. Search immutable
173 /// passes and all pass managers. If desired pass is not found
174 /// then return NULL.
175 Pass *findAnalysisPass(AnalysisID AID);
177 /// Find analysis usage information for the pass P.
178 AnalysisUsage *findAnalysisUsage(Pass *P);
180 explicit PMTopLevelManager(enum TopLevelManagerType t);
181 virtual ~PMTopLevelManager();
183 /// Add immutable pass and initialize it.
184 inline void addImmutablePass(ImmutablePass *P) {
186 ImmutablePasses.push_back(P);
189 inline SmallVector<ImmutablePass *, 8>& getImmutablePasses() {
190 return ImmutablePasses;
193 void addPassManager(PMDataManager *Manager) {
194 PassManagers.push_back(Manager);
197 // Add Manager into the list of managers that are not directly
198 // maintained by this top level pass manager
199 inline void addIndirectPassManager(PMDataManager *Manager) {
200 IndirectPassManagers.push_back(Manager);
203 // Print passes managed by this top level manager.
204 void dumpPasses() const;
205 void dumpArguments() const;
207 void initializeAllAnalysisInfo();
209 // Active Pass Managers
214 /// Collection of pass managers
215 SmallVector<PMDataManager *, 8> PassManagers;
219 /// Collection of pass managers that are not directly maintained
220 /// by this pass manager
221 SmallVector<PMDataManager *, 8> IndirectPassManagers;
223 // Map to keep track of last user of the analysis pass.
224 // LastUser->second is the last user of Lastuser->first.
225 DenseMap<Pass *, Pass *> LastUser;
227 // Map to keep track of passes that are last used by a pass.
228 // This inverse map is initialized at PM->run() based on
230 DenseMap<Pass *, SmallPtrSet<Pass *, 8> > InversedLastUser;
232 /// Immutable passes are managed by top level manager.
233 SmallVector<ImmutablePass *, 8> ImmutablePasses;
235 DenseMap<Pass *, AnalysisUsage *> AnUsageMap;
240 //===----------------------------------------------------------------------===//
243 /// PMDataManager provides the common place to manage the analysis data
244 /// used by pass managers.
245 class PMDataManager {
248 explicit PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
249 initializeAnalysisInfo();
252 virtual ~PMDataManager();
254 /// Augment AvailableAnalysis by adding analysis made available by pass P.
255 void recordAvailableAnalysis(Pass *P);
257 /// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
258 void verifyPreservedAnalysis(Pass *P);
260 /// verifyDomInfo -- Verify dominator information if it is available.
261 void verifyDomInfo(Pass &P, Function &F);
263 /// Remove Analysis that is not preserved by the pass
264 void removeNotPreservedAnalysis(Pass *P);
266 /// Remove dead passes
267 void removeDeadPasses(Pass *P, const char *Msg, enum PassDebuggingString);
269 /// Add pass P into the PassVector. Update
270 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
271 void add(Pass *P, bool ProcessAnalysis = true);
273 /// Add RequiredPass into list of lower level passes required by pass P.
274 /// RequiredPass is run on the fly by Pass Manager when P requests it
275 /// through getAnalysis interface.
276 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
278 virtual Pass * getOnTheFlyPass(Pass *P, const PassInfo *PI, Function &F) {
279 assert (0 && "Unable to find on the fly pass");
283 /// Initialize available analysis information.
284 void initializeAnalysisInfo() {
285 AvailableAnalysis.clear();
286 for (unsigned i = 0; i < PMT_Last; ++i)
287 InheritedAnalysis[i] = NULL;
290 // Return true if P preserves high level analysis used by other
291 // passes that are managed by this manager.
292 bool preserveHigherLevelAnalysis(Pass *P);
295 /// Populate RequiredPasses with analysis pass that are required by
296 /// pass P and are available. Populate ReqPassNotAvailable with analysis
297 /// pass that are required by pass P but are not available.
298 void collectRequiredAnalysis(SmallVector<Pass *, 8> &RequiredPasses,
299 SmallVector<AnalysisID, 8> &ReqPassNotAvailable,
302 /// All Required analyses should be available to the pass as it runs! Here
303 /// we fill in the AnalysisImpls member of the pass so that it can
304 /// successfully use the getAnalysis() method to retrieve the
305 /// implementations it needs.
306 void initializeAnalysisImpl(Pass *P);
308 /// Find the pass that implements Analysis AID. If desired pass is not found
309 /// then return NULL.
310 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
312 // Access toplevel manager
313 PMTopLevelManager *getTopLevelManager() { return TPM; }
314 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
316 unsigned getDepth() const { return Depth; }
318 // Print routines used by debug-pass
319 void dumpLastUses(Pass *P, unsigned Offset) const;
320 void dumpPassArguments() const;
321 void dumpPassInfo(Pass *P, enum PassDebuggingString S1,
322 enum PassDebuggingString S2, const char *Msg);
323 void dumpRequiredSet(const Pass *P) const;
324 void dumpPreservedSet(const Pass *P) const;
326 virtual unsigned getNumContainedPasses() const {
327 return (unsigned)PassVector.size();
330 virtual PassManagerType getPassManagerType() const {
331 assert ( 0 && "Invalid use of getPassManagerType");
335 std::map<AnalysisID, Pass*> *getAvailableAnalysis() {
336 return &AvailableAnalysis;
339 // Collect AvailableAnalysis from all the active Pass Managers.
340 void populateInheritedAnalysis(PMStack &PMS) {
342 for (PMStack::iterator I = PMS.begin(), E = PMS.end();
344 InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis();
349 // Top level manager.
350 PMTopLevelManager *TPM;
352 // Collection of pass that are managed by this manager
353 SmallVector<Pass *, 16> PassVector;
355 // Collection of Analysis provided by Parent pass manager and
356 // used by current pass manager. At at time there can not be more
357 // then PMT_Last active pass mangers.
358 std::map<AnalysisID, Pass *> *InheritedAnalysis[PMT_Last];
361 void dumpAnalysisUsage(const char *Msg, const Pass *P,
362 const AnalysisUsage::VectorType &Set) const;
364 // Set of available Analysis. This information is used while scheduling
365 // pass. If a pass requires an analysis which is not not available then
366 // equired analysis pass is scheduled to run before the pass itself is
368 std::map<AnalysisID, Pass*> AvailableAnalysis;
370 // Collection of higher level analysis used by the pass managed by
372 SmallVector<Pass *, 8> HigherLevelAnalysis;
377 //===----------------------------------------------------------------------===//
380 /// FPPassManager manages BBPassManagers and FunctionPasses.
381 /// It batches all function passes and basic block pass managers together and
382 /// sequence them to process one function at a time before processing next
385 class FPPassManager : public ModulePass, public PMDataManager {
389 explicit FPPassManager(int Depth)
390 : ModulePass(intptr_t(&ID)), PMDataManager(Depth) { }
392 /// run - Execute all of the passes scheduled for execution. Keep track of
393 /// whether any of the passes modifies the module, and if so, return true.
394 bool runOnFunction(Function &F);
395 bool runOnModule(Module &M);
397 /// doInitialization - Run all of the initializers for the function passes.
399 bool doInitialization(Module &M);
401 /// doFinalization - Run all of the finalizers for the function passes.
403 bool doFinalization(Module &M);
405 /// Pass Manager itself does not invalidate any analysis info.
406 void getAnalysisUsage(AnalysisUsage &Info) const {
407 Info.setPreservesAll();
410 // Print passes managed by this manager
411 void dumpPassStructure(unsigned Offset);
413 virtual const char *getPassName() const {
414 return "Function Pass Manager";
417 FunctionPass *getContainedPass(unsigned N) {
418 assert ( N < PassVector.size() && "Pass number out of range!");
419 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
423 virtual PassManagerType getPassManagerType() const {
424 return PMT_FunctionPassManager;
430 extern void StartPassTimer(llvm::Pass *);
431 extern void StopPassTimer(llvm::Pass *);