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/DenseMap.h"
20 //===----------------------------------------------------------------------===//
22 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
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.
30 // Pass Manager Infrastructure uses multiple pass managers. They are
31 // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
32 // This class hierarchy uses multiple inheritance but pass managers do not
33 // derive from another pass manager.
35 // PassManager and FunctionPassManager are two top-level pass manager that
36 // represents the external interface of this entire pass manager infrastucture.
38 // Important classes :
40 // [o] class PMTopLevelManager;
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.
46 // [o] class PMDataManager;
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
53 // [o] class BBPassManager : public FunctionPass, public PMDataManager;
55 // BBPassManager manages BasicBlockPasses.
57 // [o] class FunctionPassManager;
59 // This is a external interface used by JIT to manage FunctionPasses. This
60 // interface relies on FunctionPassManagerImpl to do all the tasks.
62 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
63 // public PMTopLevelManager;
65 // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
67 // [o] class FPPassManager : public ModulePass, public PMDataManager;
69 // FPPassManager manages FunctionPasses and BBPassManagers
71 // [o] class MPPassManager : public Pass, public PMDataManager;
73 // MPPassManager manages ModulePasses and FPPassManagers
75 // [o] class PassManager;
77 // This is a external interface used by various tools to manages passes. It
78 // relies on PassManagerImpl to do all the tasks.
80 // [o] class PassManagerImpl : public Pass, public PMDataManager,
81 // public PMDTopLevelManager
83 // PassManagerImpl is a top level pass manager responsible for managing
85 //===----------------------------------------------------------------------===//
87 #ifndef PASSMANAGERS_H
88 #define PASSMANAGERS_H
90 #include "llvm/Pass.h"
95 /// FunctionPassManager and PassManager, two top level managers, serve
96 /// as the public interface of pass manager infrastructure.
97 enum TopLevelManagerType {
98 TLM_Function, // FunctionPassManager
99 TLM_Pass // PassManager
102 // enums for debugging strings
103 enum PassDebuggingString {
104 EXECUTION_MSG, // "Executing Pass '"
105 MODIFICATION_MSG, // "' Made Modification '"
106 FREEING_MSG, // " Freeing Pass '"
107 ON_BASICBLOCK_MSG, // "' on BasicBlock '" + PassName + "'...\n"
108 ON_FUNCTION_MSG, // "' on Function '" + FunctionName + "'...\n"
109 ON_MODULE_MSG, // "' on Module '" + ModuleName + "'...\n"
110 ON_LOOP_MSG, // " 'on Loop ...\n'"
111 ON_CG_MSG // "' on Call Graph ...\n'"
114 //===----------------------------------------------------------------------===//
118 /// Top level pass managers (see PassManager.cpp) maintain active Pass Managers
119 /// using PMStack. Each Pass implements assignPassManager() to connect itself
120 /// with appropriate manager. assignPassManager() walks PMStack to find
121 /// suitable manager.
123 /// PMStack is just a wrapper around standard deque that overrides pop() and
127 typedef std::deque<PMDataManager *>::reverse_iterator iterator;
128 iterator begin() { return S.rbegin(); }
129 iterator end() { return S.rend(); }
131 void handleLastUserOverflow();
134 inline PMDataManager *top() { return S.back(); }
135 void push(PMDataManager *PM);
136 inline bool empty() { return S.empty(); }
140 std::deque<PMDataManager *> S;
144 //===----------------------------------------------------------------------===//
147 /// PMTopLevelManager manages LastUser info and collects common APIs used by
148 /// top level pass managers.
149 class PMTopLevelManager {
152 virtual unsigned getNumContainedManagers() const {
153 return (unsigned)PassManagers.size();
156 /// Schedule pass P for execution. Make sure that passes required by
157 /// P are run before P is run. Update analysis info maintained by
158 /// the manager. Remove dead passes. This is a recursive function.
159 void schedulePass(Pass *P);
161 /// This is implemented by top level pass manager and used by
162 /// schedulePass() to add analysis info passes that are not available.
163 virtual void addTopLevelPass(Pass *P) = 0;
165 /// Set pass P as the last user of the given analysis passes.
166 void setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, Pass *P);
168 /// Collect passes whose last user is P
169 void collectLastUses(SmallVector<Pass *, 12> &LastUses, Pass *P);
171 /// Find the pass that implements Analysis AID. Search immutable
172 /// passes and all pass managers. If desired pass is not found
173 /// then return NULL.
174 Pass *findAnalysisPass(AnalysisID AID);
176 /// Find analysis usage information for the pass P.
177 AnalysisUsage *findAnalysisUsage(Pass *P);
179 explicit PMTopLevelManager(enum TopLevelManagerType t);
180 virtual ~PMTopLevelManager();
182 /// Add immutable pass and initialize it.
183 inline void addImmutablePass(ImmutablePass *P) {
185 ImmutablePasses.push_back(P);
188 inline std::vector<ImmutablePass *>& getImmutablePasses() {
189 return ImmutablePasses;
192 void addPassManager(PMDataManager *Manager) {
193 PassManagers.push_back(Manager);
196 // Add Manager into the list of managers that are not directly
197 // maintained by this top level pass manager
198 inline void addIndirectPassManager(PMDataManager *Manager) {
199 IndirectPassManagers.push_back(Manager);
202 // Print passes managed by this top level manager.
203 void dumpPasses() const;
204 void dumpArguments() const;
206 void initializeAllAnalysisInfo();
208 // Active Pass Managers
213 /// Collection of pass managers
214 std::vector<PMDataManager *> PassManagers;
218 /// Collection of pass managers that are not directly maintained
219 /// by this pass manager
220 std::vector<PMDataManager *> IndirectPassManagers;
222 // Map to keep track of last user of the analysis pass.
223 // LastUser->second is the last user of Lastuser->first.
224 std::map<Pass *, Pass *> LastUser;
226 /// Immutable passes are managed by top level manager.
227 std::vector<ImmutablePass *> ImmutablePasses;
229 DenseMap<Pass *, AnalysisUsage *> AnUsageMap;
234 //===----------------------------------------------------------------------===//
237 /// PMDataManager provides the common place to manage the analysis data
238 /// used by pass managers.
239 class PMDataManager {
242 explicit PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
243 initializeAnalysisInfo();
246 virtual ~PMDataManager();
248 /// Augment AvailableAnalysis by adding analysis made available by pass P.
249 void recordAvailableAnalysis(Pass *P);
251 /// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
252 void verifyPreservedAnalysis(Pass *P);
254 /// verifyDomInfo -- Verify dominator information if it is available.
255 void verifyDomInfo(Pass &P, Function &F);
257 /// Remove Analysis that is not preserved by the pass
258 void removeNotPreservedAnalysis(Pass *P);
260 /// Remove dead passes
261 void removeDeadPasses(Pass *P, const char *Msg, enum PassDebuggingString);
263 /// Add pass P into the PassVector. Update
264 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
265 void add(Pass *P, bool ProcessAnalysis = true);
267 /// Add RequiredPass into list of lower level passes required by pass P.
268 /// RequiredPass is run on the fly by Pass Manager when P requests it
269 /// through getAnalysis interface.
270 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
272 virtual Pass * getOnTheFlyPass(Pass *P, const PassInfo *PI, Function &F) {
273 assert (0 && "Unable to find on the fly pass");
277 /// Initialize available analysis information.
278 void initializeAnalysisInfo() {
279 AvailableAnalysis.clear();
280 for (unsigned i = 0; i < PMT_Last; ++i)
281 InheritedAnalysis[i] = NULL;
284 // Return true if P preserves high level analysis used by other
285 // passes that are managed by this manager.
286 bool preserveHigherLevelAnalysis(Pass *P);
289 /// Populate RequiredPasses with analysis pass that are required by
290 /// pass P and are available. Populate ReqPassNotAvailable with analysis
291 /// pass that are required by pass P but are not available.
292 void collectRequiredAnalysis(SmallVector<Pass *, 8> &RequiredPasses,
293 SmallVector<AnalysisID, 8> &ReqPassNotAvailable,
296 /// All Required analyses should be available to the pass as it runs! Here
297 /// we fill in the AnalysisImpls member of the pass so that it can
298 /// successfully use the getAnalysis() method to retrieve the
299 /// implementations it needs.
300 void initializeAnalysisImpl(Pass *P);
302 /// Find the pass that implements Analysis AID. If desired pass is not found
303 /// then return NULL.
304 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
306 // Access toplevel manager
307 PMTopLevelManager *getTopLevelManager() { return TPM; }
308 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
310 unsigned getDepth() const { return Depth; }
312 // Print routines used by debug-pass
313 void dumpLastUses(Pass *P, unsigned Offset) const;
314 void dumpPassArguments() const;
315 void dumpPassInfo(Pass *P, enum PassDebuggingString S1,
316 enum PassDebuggingString S2, const char *Msg);
317 void dumpRequiredSet(const Pass *P) const;
318 void dumpPreservedSet(const Pass *P) const;
320 virtual unsigned getNumContainedPasses() const {
321 return (unsigned)PassVector.size();
324 virtual PassManagerType getPassManagerType() const {
325 assert ( 0 && "Invalid use of getPassManagerType");
329 std::map<AnalysisID, Pass*> *getAvailableAnalysis() {
330 return &AvailableAnalysis;
333 // Collect AvailableAnalysis from all the active Pass Managers.
334 void populateInheritedAnalysis(PMStack &PMS) {
336 for (PMStack::iterator I = PMS.begin(), E = PMS.end();
338 InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis();
343 // Top level manager.
344 PMTopLevelManager *TPM;
346 // Collection of pass that are managed by this manager
347 std::vector<Pass *> PassVector;
349 // Collection of Analysis provided by Parent pass manager and
350 // used by current pass manager. At at time there can not be more
351 // then PMT_Last active pass mangers.
352 std::map<AnalysisID, Pass *> *InheritedAnalysis[PMT_Last];
355 void dumpAnalysisUsage(const char *Msg, const Pass *P,
356 const AnalysisUsage::VectorType &Set) const;
358 // Set of available Analysis. This information is used while scheduling
359 // pass. If a pass requires an analysis which is not not available then
360 // equired analysis pass is scheduled to run before the pass itself is
362 std::map<AnalysisID, Pass*> AvailableAnalysis;
364 // Collection of higher level analysis used by the pass managed by
366 std::vector<Pass *> HigherLevelAnalysis;
371 //===----------------------------------------------------------------------===//
374 /// FPPassManager manages BBPassManagers and FunctionPasses.
375 /// It batches all function passes and basic block pass managers together and
376 /// sequence them to process one function at a time before processing next
379 class FPPassManager : public ModulePass, public PMDataManager {
383 explicit FPPassManager(int Depth)
384 : ModulePass(intptr_t(&ID)), PMDataManager(Depth) { }
386 /// run - Execute all of the passes scheduled for execution. Keep track of
387 /// whether any of the passes modifies the module, and if so, return true.
388 bool runOnFunction(Function &F);
389 bool runOnModule(Module &M);
391 /// doInitialization - Run all of the initializers for the function passes.
393 bool doInitialization(Module &M);
395 /// doFinalization - Run all of the finalizers for the function passes.
397 bool doFinalization(Module &M);
399 /// Pass Manager itself does not invalidate any analysis info.
400 void getAnalysisUsage(AnalysisUsage &Info) const {
401 Info.setPreservesAll();
404 // Print passes managed by this manager
405 void dumpPassStructure(unsigned Offset);
407 virtual const char *getPassName() const {
408 return "Function Pass Manager";
411 FunctionPass *getContainedPass(unsigned N) {
412 assert ( N < PassVector.size() && "Pass number out of range!");
413 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
417 virtual PassManagerType getPassManagerType() const {
418 return PMT_FunctionPassManager;
424 extern void StartPassTimer(llvm::Pass *);
425 extern void StopPassTimer(llvm::Pass *);