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/Pass.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"
101 // enums for debugging strings
102 enum PassDebuggingString {
103 EXECUTION_MSG, // "Executing Pass '"
104 MODIFICATION_MSG, // "' Made Modification '"
105 FREEING_MSG, // " Freeing Pass '"
106 ON_BASICBLOCK_MSG, // "' on BasicBlock '" + PassName + "'...\n"
107 ON_FUNCTION_MSG, // "' on Function '" + FunctionName + "'...\n"
108 ON_MODULE_MSG, // "' on Module '" + ModuleName + "'...\n"
109 ON_LOOP_MSG, // " 'on Loop ...\n'"
110 ON_CG_MSG // "' on Call Graph ...\n'"
113 /// PassManagerPrettyStackEntry - This is used to print informative information
114 /// about what pass is running when/if a stack trace is generated.
115 class PassManagerPrettyStackEntry : public PrettyStackTraceEntry {
120 explicit PassManagerPrettyStackEntry(Pass *p)
121 : P(p), V(0), M(0) {} // When P is releaseMemory'd.
122 PassManagerPrettyStackEntry(Pass *p, Value &v)
123 : P(p), V(&v), M(0) {} // When P is run on V
124 PassManagerPrettyStackEntry(Pass *p, Module &m)
125 : P(p), V(0), M(&m) {} // When P is run on M
127 /// print - Emit information about this stack frame to OS.
128 virtual void print(raw_ostream &OS) const;
132 //===----------------------------------------------------------------------===//
135 /// PMStack - This class implements a stack data structure of PMDataManager
138 /// Top level pass managers (see PassManager.cpp) maintain active Pass Managers
139 /// using PMStack. Each Pass implements assignPassManager() to connect itself
140 /// with appropriate manager. assignPassManager() walks PMStack to find
141 /// suitable manager.
144 typedef std::vector<PMDataManager *>::const_reverse_iterator iterator;
145 iterator begin() const { return S.rbegin(); }
146 iterator end() const { return S.rend(); }
149 PMDataManager *top() const { return S.back(); }
150 void push(PMDataManager *PM);
151 bool empty() const { return S.empty(); }
156 std::vector<PMDataManager *> S;
160 //===----------------------------------------------------------------------===//
163 /// PMTopLevelManager manages LastUser info and collects common APIs used by
164 /// top level pass managers.
165 class PMTopLevelManager {
168 virtual unsigned getNumContainedManagers() const {
169 return (unsigned)PassManagers.size();
172 /// Schedule pass P for execution. Make sure that passes required by
173 /// P are run before P is run. Update analysis info maintained by
174 /// the manager. Remove dead passes. This is a recursive function.
175 void schedulePass(Pass *P);
177 /// This is implemented by top level pass manager and used by
178 /// schedulePass() to add analysis info passes that are not available.
179 virtual void addTopLevelPass(Pass *P) = 0;
181 /// Set pass P as the last user of the given analysis passes.
182 void setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, Pass *P);
184 /// Collect passes whose last user is P
185 void collectLastUses(SmallVector<Pass *, 12> &LastUses, Pass *P);
187 /// Find the pass that implements Analysis AID. Search immutable
188 /// passes and all pass managers. If desired pass is not found
189 /// then return NULL.
190 Pass *findAnalysisPass(AnalysisID AID);
192 /// Find analysis usage information for the pass P.
193 AnalysisUsage *findAnalysisUsage(Pass *P);
195 explicit PMTopLevelManager(PMDataManager *PMDM);
196 virtual ~PMTopLevelManager();
198 /// Add immutable pass and initialize it.
199 inline void addImmutablePass(ImmutablePass *P) {
201 ImmutablePasses.push_back(P);
204 inline SmallVector<ImmutablePass *, 8>& getImmutablePasses() {
205 return ImmutablePasses;
208 void addPassManager(PMDataManager *Manager) {
209 PassManagers.push_back(Manager);
212 // Add Manager into the list of managers that are not directly
213 // maintained by this top level pass manager
214 inline void addIndirectPassManager(PMDataManager *Manager) {
215 IndirectPassManagers.push_back(Manager);
218 // Print passes managed by this top level manager.
219 void dumpPasses() const;
220 void dumpArguments() const;
222 void initializeAllAnalysisInfo();
224 // Active Pass Managers
229 /// Collection of pass managers
230 SmallVector<PMDataManager *, 8> PassManagers;
234 /// Collection of pass managers that are not directly maintained
235 /// by this pass manager
236 SmallVector<PMDataManager *, 8> IndirectPassManagers;
238 // Map to keep track of last user of the analysis pass.
239 // LastUser->second is the last user of Lastuser->first.
240 DenseMap<Pass *, Pass *> LastUser;
242 // Map to keep track of passes that are last used by a pass.
243 // This inverse map is initialized at PM->run() based on
245 DenseMap<Pass *, SmallPtrSet<Pass *, 8> > InversedLastUser;
247 /// Immutable passes are managed by top level manager.
248 SmallVector<ImmutablePass *, 8> ImmutablePasses;
250 DenseMap<Pass *, AnalysisUsage *> AnUsageMap;
255 //===----------------------------------------------------------------------===//
258 /// PMDataManager provides the common place to manage the analysis data
259 /// used by pass managers.
260 class PMDataManager {
263 explicit PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
264 initializeAnalysisInfo();
267 virtual ~PMDataManager();
269 virtual Pass *getAsPass() = 0;
271 /// Augment AvailableAnalysis by adding analysis made available by pass P.
272 void recordAvailableAnalysis(Pass *P);
274 /// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
275 void verifyPreservedAnalysis(Pass *P);
277 /// Remove Analysis that is not preserved by the pass
278 void removeNotPreservedAnalysis(Pass *P);
280 /// Remove dead passes used by P.
281 void removeDeadPasses(Pass *P, StringRef Msg,
282 enum PassDebuggingString);
285 void freePass(Pass *P, StringRef Msg,
286 enum PassDebuggingString);
288 /// Add pass P into the PassVector. Update
289 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
290 void add(Pass *P, bool ProcessAnalysis = true);
292 /// Add RequiredPass into list of lower level passes required by pass P.
293 /// RequiredPass is run on the fly by Pass Manager when P requests it
294 /// through getAnalysis interface.
295 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
297 virtual Pass *getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F);
299 /// Initialize available analysis information.
300 void initializeAnalysisInfo() {
301 AvailableAnalysis.clear();
302 for (unsigned i = 0; i < PMT_Last; ++i)
303 InheritedAnalysis[i] = NULL;
306 // Return true if P preserves high level analysis used by other
307 // passes that are managed by this manager.
308 bool preserveHigherLevelAnalysis(Pass *P);
311 /// Populate RequiredPasses with analysis pass that are required by
312 /// pass P and are available. Populate ReqPassNotAvailable with analysis
313 /// pass that are required by pass P but are not available.
314 void collectRequiredAnalysis(SmallVector<Pass *, 8> &RequiredPasses,
315 SmallVector<AnalysisID, 8> &ReqPassNotAvailable,
318 /// All Required analyses should be available to the pass as it runs! Here
319 /// we fill in the AnalysisImpls member of the pass so that it can
320 /// successfully use the getAnalysis() method to retrieve the
321 /// implementations it needs.
322 void initializeAnalysisImpl(Pass *P);
324 /// Find the pass that implements Analysis AID. If desired pass is not found
325 /// then return NULL.
326 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
328 // Access toplevel manager
329 PMTopLevelManager *getTopLevelManager() { return TPM; }
330 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
332 unsigned getDepth() const { return Depth; }
334 // Print routines used by debug-pass
335 void dumpLastUses(Pass *P, unsigned Offset) const;
336 void dumpPassArguments() const;
337 void dumpPassInfo(Pass *P, enum PassDebuggingString S1,
338 enum PassDebuggingString S2, StringRef Msg);
339 void dumpRequiredSet(const Pass *P) const;
340 void dumpPreservedSet(const Pass *P) const;
342 virtual unsigned getNumContainedPasses() const {
343 return (unsigned)PassVector.size();
346 virtual PassManagerType getPassManagerType() const {
347 assert ( 0 && "Invalid use of getPassManagerType");
351 std::map<AnalysisID, Pass*> *getAvailableAnalysis() {
352 return &AvailableAnalysis;
355 // Collect AvailableAnalysis from all the active Pass Managers.
356 void populateInheritedAnalysis(PMStack &PMS) {
358 for (PMStack::iterator I = PMS.begin(), E = PMS.end();
360 InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis();
365 // Top level manager.
366 PMTopLevelManager *TPM;
368 // Collection of pass that are managed by this manager
369 SmallVector<Pass *, 16> PassVector;
371 // Collection of Analysis provided by Parent pass manager and
372 // used by current pass manager. At at time there can not be more
373 // then PMT_Last active pass mangers.
374 std::map<AnalysisID, Pass *> *InheritedAnalysis[PMT_Last];
377 /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
378 /// or higher is specified.
379 bool isPassDebuggingExecutionsOrMore() const;
382 void dumpAnalysisUsage(StringRef Msg, const Pass *P,
383 const AnalysisUsage::VectorType &Set) const;
385 // Set of available Analysis. This information is used while scheduling
386 // pass. If a pass requires an analysis which is not available then
387 // the required analysis pass is scheduled to run before the pass itself is
389 std::map<AnalysisID, Pass*> AvailableAnalysis;
391 // Collection of higher level analysis used by the pass managed by
393 SmallVector<Pass *, 8> HigherLevelAnalysis;
398 //===----------------------------------------------------------------------===//
401 /// FPPassManager manages BBPassManagers and FunctionPasses.
402 /// It batches all function passes and basic block pass managers together and
403 /// sequence them to process one function at a time before processing next
405 class FPPassManager : public ModulePass, public PMDataManager {
408 explicit FPPassManager(int Depth)
409 : ModulePass(ID), PMDataManager(Depth) { }
411 /// run - Execute all of the passes scheduled for execution. Keep track of
412 /// whether any of the passes modifies the module, and if so, return true.
413 bool runOnFunction(Function &F);
414 bool runOnModule(Module &M);
416 /// cleanup - After running all passes, clean up pass manager cache.
419 /// doInitialization - Run all of the initializers for the function passes.
421 bool doInitialization(Module &M);
423 /// doFinalization - Run all of the finalizers for the function passes.
425 bool doFinalization(Module &M);
427 virtual PMDataManager *getAsPMDataManager() { return this; }
428 virtual Pass *getAsPass() { return this; }
430 /// Pass Manager itself does not invalidate any analysis info.
431 void getAnalysisUsage(AnalysisUsage &Info) const {
432 Info.setPreservesAll();
435 // Print passes managed by this manager
436 void dumpPassStructure(unsigned Offset);
438 virtual const char *getPassName() const {
439 return "Function Pass Manager";
442 FunctionPass *getContainedPass(unsigned N) {
443 assert ( N < PassVector.size() && "Pass number out of range!");
444 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
448 virtual PassManagerType getPassManagerType() const {
449 return PMT_FunctionPassManager;
453 Timer *getPassTimer(Pass *);