1 //===- llvm/PassManagers.h - Pass Infrastructure 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 #ifndef LLVM_PASSMANAGERS_H
15 #define LLVM_PASSMANAGERS_H
17 #include "llvm/PassManager.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/DenseMap.h"
24 //===----------------------------------------------------------------------===//
26 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
28 // o Manage optimization pass execution order
29 // o Make required Analysis information available before pass P is run
30 // o Release memory occupied by dead passes
31 // o If Analysis information is dirtied by a pass then regenerate Analysis
32 // information before it is consumed by another pass.
34 // Pass Manager Infrastructure uses multiple pass managers. They are
35 // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
36 // This class hierarchy uses multiple inheritance but pass managers do not
37 // derive from another pass manager.
39 // PassManager and FunctionPassManager are two top-level pass manager that
40 // represents the external interface of this entire pass manager infrastucture.
42 // Important classes :
44 // [o] class PMTopLevelManager;
46 // Two top level managers, PassManager and FunctionPassManager, derive from
47 // PMTopLevelManager. PMTopLevelManager manages information used by top level
48 // managers such as last user info.
50 // [o] class PMDataManager;
52 // PMDataManager manages information, e.g. list of available analysis info,
53 // used by a pass manager to manage execution order of passes. It also provides
54 // a place to implement common pass manager APIs. All pass managers derive from
57 // [o] class BBPassManager : public FunctionPass, public PMDataManager;
59 // BBPassManager manages BasicBlockPasses.
61 // [o] class FunctionPassManager;
63 // This is a external interface used by JIT to manage FunctionPasses. This
64 // interface relies on FunctionPassManagerImpl to do all the tasks.
66 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
67 // public PMTopLevelManager;
69 // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
71 // [o] class FPPassManager : public ModulePass, public PMDataManager;
73 // FPPassManager manages FunctionPasses and BBPassManagers
75 // [o] class MPPassManager : public Pass, public PMDataManager;
77 // MPPassManager manages ModulePasses and FPPassManagers
79 // [o] class PassManager;
81 // This is a external interface used by various tools to manages passes. It
82 // relies on PassManagerImpl to do all the tasks.
84 // [o] class PassManagerImpl : public Pass, public PMDataManager,
85 // public PMDTopLevelManager
87 // PassManagerImpl is a top level pass manager responsible for managing
89 //===----------------------------------------------------------------------===//
91 #include "llvm/Support/PrettyStackTrace.h"
99 /// FunctionPassManager and PassManager, two top level managers, serve
100 /// as the public interface of pass manager infrastructure.
101 enum TopLevelManagerType {
102 TLM_Function, // FunctionPassManager
103 TLM_Pass // PassManager
106 // enums for debugging strings
107 enum PassDebuggingString {
108 EXECUTION_MSG, // "Executing Pass '"
109 MODIFICATION_MSG, // "' Made Modification '"
110 FREEING_MSG, // " Freeing Pass '"
111 ON_BASICBLOCK_MSG, // "' on BasicBlock '" + PassName + "'...\n"
112 ON_FUNCTION_MSG, // "' on Function '" + FunctionName + "'...\n"
113 ON_MODULE_MSG, // "' on Module '" + ModuleName + "'...\n"
114 ON_LOOP_MSG, // " 'on Loop ...\n'"
115 ON_CG_MSG // "' on Call Graph ...\n'"
118 /// PassManagerPrettyStackEntry - This is used to print informative information
119 /// about what pass is running when/if a stack trace is generated.
120 class PassManagerPrettyStackEntry : public PrettyStackTraceEntry {
125 explicit PassManagerPrettyStackEntry(Pass *p)
126 : P(p), V(0), M(0) {} // When P is releaseMemory'd.
127 PassManagerPrettyStackEntry(Pass *p, Value &v)
128 : P(p), V(&v), M(0) {} // When P is run on V
129 PassManagerPrettyStackEntry(Pass *p, Module &m)
130 : P(p), V(0), M(&m) {} // When P is run on M
132 /// print - Emit information about this stack frame to OS.
133 virtual void print(raw_ostream &OS) const;
137 //===----------------------------------------------------------------------===//
141 /// Top level pass managers (see PassManager.cpp) maintain active Pass Managers
142 /// using PMStack. Each Pass implements assignPassManager() to connect itself
143 /// with appropriate manager. assignPassManager() walks PMStack to find
144 /// suitable manager.
146 /// PMStack is just a wrapper around standard deque that overrides pop() and
150 typedef std::deque<PMDataManager *>::reverse_iterator iterator;
151 iterator begin() { return S.rbegin(); }
152 iterator end() { return S.rend(); }
154 void handleLastUserOverflow();
157 inline PMDataManager *top() { return S.back(); }
158 void push(PMDataManager *PM);
159 inline bool empty() { return S.empty(); }
163 std::deque<PMDataManager *> S;
167 //===----------------------------------------------------------------------===//
170 /// PMTopLevelManager manages LastUser info and collects common APIs used by
171 /// top level pass managers.
172 class PMTopLevelManager {
175 virtual unsigned getNumContainedManagers() const {
176 return (unsigned)PassManagers.size();
179 /// Schedule pass P for execution. Make sure that passes required by
180 /// P are run before P is run. Update analysis info maintained by
181 /// the manager. Remove dead passes. This is a recursive function.
182 void schedulePass(Pass *P);
184 /// This is implemented by top level pass manager and used by
185 /// schedulePass() to add analysis info passes that are not available.
186 virtual void addTopLevelPass(Pass *P) = 0;
188 /// Set pass P as the last user of the given analysis passes.
189 void setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, Pass *P);
191 /// Collect passes whose last user is P
192 void collectLastUses(SmallVector<Pass *, 12> &LastUses, Pass *P);
194 /// Find the pass that implements Analysis AID. Search immutable
195 /// passes and all pass managers. If desired pass is not found
196 /// then return NULL.
197 Pass *findAnalysisPass(AnalysisID AID);
199 /// Find analysis usage information for the pass P.
200 AnalysisUsage *findAnalysisUsage(Pass *P);
202 explicit PMTopLevelManager(enum TopLevelManagerType t);
203 virtual ~PMTopLevelManager();
205 /// Add immutable pass and initialize it.
206 inline void addImmutablePass(ImmutablePass *P) {
208 ImmutablePasses.push_back(P);
211 inline SmallVector<ImmutablePass *, 8>& getImmutablePasses() {
212 return ImmutablePasses;
215 void addPassManager(PMDataManager *Manager) {
216 PassManagers.push_back(Manager);
219 // Add Manager into the list of managers that are not directly
220 // maintained by this top level pass manager
221 inline void addIndirectPassManager(PMDataManager *Manager) {
222 IndirectPassManagers.push_back(Manager);
225 // Print passes managed by this top level manager.
226 void dumpPasses() const;
227 void dumpArguments() const;
229 void initializeAllAnalysisInfo();
231 // Active Pass Managers
236 /// Collection of pass managers
237 SmallVector<PMDataManager *, 8> PassManagers;
241 /// Collection of pass managers that are not directly maintained
242 /// by this pass manager
243 SmallVector<PMDataManager *, 8> IndirectPassManagers;
245 // Map to keep track of last user of the analysis pass.
246 // LastUser->second is the last user of Lastuser->first.
247 DenseMap<Pass *, Pass *> LastUser;
249 // Map to keep track of passes that are last used by a pass.
250 // This inverse map is initialized at PM->run() based on
252 DenseMap<Pass *, SmallPtrSet<Pass *, 8> > InversedLastUser;
254 /// Immutable passes are managed by top level manager.
255 SmallVector<ImmutablePass *, 8> ImmutablePasses;
257 DenseMap<Pass *, AnalysisUsage *> AnUsageMap;
262 //===----------------------------------------------------------------------===//
265 /// PMDataManager provides the common place to manage the analysis data
266 /// used by pass managers.
267 class PMDataManager {
270 explicit PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
271 initializeAnalysisInfo();
274 virtual ~PMDataManager();
276 /// Augment AvailableAnalysis by adding analysis made available by pass P.
277 void recordAvailableAnalysis(Pass *P);
279 /// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
280 void verifyPreservedAnalysis(Pass *P);
282 /// verifyDomInfo -- Verify dominator information if it is available.
283 void verifyDomInfo(Pass &P, Function &F);
285 /// Remove Analysis that is not preserved by the pass
286 void removeNotPreservedAnalysis(Pass *P);
288 /// Remove dead passes used by P.
289 void removeDeadPasses(Pass *P, const StringRef &Msg,
290 enum PassDebuggingString);
293 void freePass(Pass *P, const StringRef &Msg,
294 enum PassDebuggingString);
296 /// Add pass P into the PassVector. Update
297 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
298 void add(Pass *P, bool ProcessAnalysis = true);
300 /// Add RequiredPass into list of lower level passes required by pass P.
301 /// RequiredPass is run on the fly by Pass Manager when P requests it
302 /// through getAnalysis interface.
303 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
305 virtual Pass * getOnTheFlyPass(Pass *P, const PassInfo *PI, Function &F) {
306 assert (0 && "Unable to find on the fly pass");
310 /// Initialize available analysis information.
311 void initializeAnalysisInfo() {
312 AvailableAnalysis.clear();
313 for (unsigned i = 0; i < PMT_Last; ++i)
314 InheritedAnalysis[i] = NULL;
317 // Return true if P preserves high level analysis used by other
318 // passes that are managed by this manager.
319 bool preserveHigherLevelAnalysis(Pass *P);
322 /// Populate RequiredPasses with analysis pass that are required by
323 /// pass P and are available. Populate ReqPassNotAvailable with analysis
324 /// pass that are required by pass P but are not available.
325 void collectRequiredAnalysis(SmallVector<Pass *, 8> &RequiredPasses,
326 SmallVector<AnalysisID, 8> &ReqPassNotAvailable,
329 /// All Required analyses should be available to the pass as it runs! Here
330 /// we fill in the AnalysisImpls member of the pass so that it can
331 /// successfully use the getAnalysis() method to retrieve the
332 /// implementations it needs.
333 void initializeAnalysisImpl(Pass *P);
335 /// Find the pass that implements Analysis AID. If desired pass is not found
336 /// then return NULL.
337 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
339 // Access toplevel manager
340 PMTopLevelManager *getTopLevelManager() { return TPM; }
341 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
343 unsigned getDepth() const { return Depth; }
345 // Print routines used by debug-pass
346 void dumpLastUses(Pass *P, unsigned Offset) const;
347 void dumpPassArguments() const;
348 void dumpPassInfo(Pass *P, enum PassDebuggingString S1,
349 enum PassDebuggingString S2, const StringRef &Msg);
350 void dumpRequiredSet(const Pass *P) const;
351 void dumpPreservedSet(const Pass *P) const;
353 virtual unsigned getNumContainedPasses() const {
354 return (unsigned)PassVector.size();
357 virtual PassManagerType getPassManagerType() const {
358 assert ( 0 && "Invalid use of getPassManagerType");
362 std::map<AnalysisID, Pass*> *getAvailableAnalysis() {
363 return &AvailableAnalysis;
366 // Collect AvailableAnalysis from all the active Pass Managers.
367 void populateInheritedAnalysis(PMStack &PMS) {
369 for (PMStack::iterator I = PMS.begin(), E = PMS.end();
371 InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis();
376 // Top level manager.
377 PMTopLevelManager *TPM;
379 // Collection of pass that are managed by this manager
380 SmallVector<Pass *, 16> PassVector;
382 // Collection of Analysis provided by Parent pass manager and
383 // used by current pass manager. At at time there can not be more
384 // then PMT_Last active pass mangers.
385 std::map<AnalysisID, Pass *> *InheritedAnalysis[PMT_Last];
388 /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
389 /// or higher is specified.
390 bool isPassDebuggingExecutionsOrMore() const;
393 void dumpAnalysisUsage(const StringRef &Msg, const Pass *P,
394 const AnalysisUsage::VectorType &Set) const;
396 // Set of available Analysis. This information is used while scheduling
397 // pass. If a pass requires an analysis which is not not available then
398 // equired analysis pass is scheduled to run before the pass itself is
400 std::map<AnalysisID, Pass*> AvailableAnalysis;
402 // Collection of higher level analysis used by the pass managed by
404 SmallVector<Pass *, 8> HigherLevelAnalysis;
409 //===----------------------------------------------------------------------===//
412 /// FPPassManager manages BBPassManagers and FunctionPasses.
413 /// It batches all function passes and basic block pass managers together and
414 /// sequence them to process one function at a time before processing next
417 class FPPassManager : public ModulePass, public PMDataManager {
421 explicit FPPassManager(int Depth)
422 : ModulePass(&ID), PMDataManager(Depth) { }
424 /// run - Execute all of the passes scheduled for execution. Keep track of
425 /// whether any of the passes modifies the module, and if so, return true.
426 bool runOnFunction(Function &F);
427 bool runOnModule(Module &M);
429 /// cleanup - After running all passes, clean up pass manager cache.
432 /// doInitialization - Run all of the initializers for the function passes.
434 bool doInitialization(Module &M);
436 /// doFinalization - Run all of the finalizers for the function passes.
438 bool doFinalization(Module &M);
440 /// Pass Manager itself does not invalidate any analysis info.
441 void getAnalysisUsage(AnalysisUsage &Info) const {
442 Info.setPreservesAll();
445 // Print passes managed by this manager
446 void dumpPassStructure(unsigned Offset);
448 virtual const char *getPassName() const {
449 return "Function Pass Manager";
452 FunctionPass *getContainedPass(unsigned N) {
453 assert ( N < PassVector.size() && "Pass number out of range!");
454 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
458 virtual PassManagerType getPassManagerType() const {
459 return PMT_FunctionPassManager;
463 extern void StartPassTimer(llvm::Pass *);
464 extern void StopPassTimer(llvm::Pass *);