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"
18 //===----------------------------------------------------------------------===//
20 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
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.
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.
33 // PassManager and FunctionPassManager are two top-level pass manager that
34 // represents the external interface of this entire pass manager infrastucture.
36 // Important classes :
38 // [o] class PMTopLevelManager;
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.
44 // [o] class PMDataManager;
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
51 // [o] class BBPassManager : public FunctionPass, public PMDataManager;
53 // BBPassManager manages BasicBlockPasses.
55 // [o] class FunctionPassManager;
57 // This is a external interface used by JIT to manage FunctionPasses. This
58 // interface relies on FunctionPassManagerImpl to do all the tasks.
60 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
61 // public PMTopLevelManager;
63 // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
65 // [o] class FPPassManager : public ModulePass, public PMDataManager;
67 // FPPassManager manages FunctionPasses and BBPassManagers
69 // [o] class MPPassManager : public Pass, public PMDataManager;
71 // MPPassManager manages ModulePasses and FPPassManagers
73 // [o] class PassManager;
75 // This is a external interface used by various tools to manages passes. It
76 // relies on PassManagerImpl to do all the tasks.
78 // [o] class PassManagerImpl : public Pass, public PMDataManager,
79 // public PMDTopLevelManager
81 // PassManagerImpl is a top level pass manager responsible for managing
83 //===----------------------------------------------------------------------===//
85 #ifndef PASSMANAGERS_H
86 #define PASSMANAGERS_H
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
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'"
109 //===----------------------------------------------------------------------===//
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.
118 /// PMStack is just a wrapper around standard deque that overrides pop() and
122 typedef std::deque<PMDataManager *>::reverse_iterator iterator;
123 iterator begin() { return S.rbegin(); }
124 iterator end() { return S.rend(); }
126 void handleLastUserOverflow();
129 inline PMDataManager *top() { return S.back(); }
130 void push(PMDataManager *PM);
131 inline bool empty() { return S.empty(); }
135 std::deque<PMDataManager *> S;
139 //===----------------------------------------------------------------------===//
142 /// PMTopLevelManager manages LastUser info and collects common APIs used by
143 /// top level pass managers.
144 class PMTopLevelManager {
147 virtual unsigned getNumContainedManagers() {
148 return PassManagers.size();
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);
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;
160 /// Set pass P as the last user of the given analysis passes.
161 void setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, Pass *P);
163 /// Collect passes whose last user is P
164 void collectLastUses(SmallVector<Pass *, 12> &LastUses, Pass *P);
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);
171 explicit PMTopLevelManager(enum TopLevelManagerType t);
172 virtual ~PMTopLevelManager();
174 /// Add immutable pass and initialize it.
175 inline void addImmutablePass(ImmutablePass *P) {
177 ImmutablePasses.push_back(P);
180 inline std::vector<ImmutablePass *>& getImmutablePasses() {
181 return ImmutablePasses;
184 void addPassManager(PMDataManager *Manager) {
185 PassManagers.push_back(Manager);
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);
194 // Print passes managed by this top level manager.
195 void dumpPasses() const;
196 void dumpArguments() const;
198 void initializeAllAnalysisInfo();
200 // Active Pass Managers
205 /// Collection of pass managers
206 std::vector<PMDataManager *> PassManagers;
210 /// Collection of pass managers that are not directly maintained
211 /// by this pass manager
212 std::vector<PMDataManager *> IndirectPassManagers;
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;
218 /// Immutable passes are managed by top level manager.
219 std::vector<ImmutablePass *> ImmutablePasses;
224 //===----------------------------------------------------------------------===//
227 /// PMDataManager provides the common place to manage the analysis data
228 /// used by pass managers.
229 class PMDataManager {
232 explicit PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
233 initializeAnalysisInfo();
236 virtual ~PMDataManager();
238 /// Augment AvailableAnalysis by adding analysis made available by pass P.
239 void recordAvailableAnalysis(Pass *P);
241 /// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
242 void verifyPreservedAnalysis(Pass *P);
244 /// Remove Analysis that is not preserved by the pass
245 void removeNotPreservedAnalysis(Pass *P);
247 /// Remove dead passes
248 void removeDeadPasses(Pass *P, const char *Msg, enum PassDebuggingString);
250 /// Add pass P into the PassVector. Update
251 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
252 void add(Pass *P, bool ProcessAnalysis = true);
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);
259 virtual Pass * getOnTheFlyPass(Pass *P, const PassInfo *PI, Function &F) {
260 assert (0 && "Unable to find on the fly pass");
264 /// Initialize available analysis information.
265 void initializeAnalysisInfo() {
266 AvailableAnalysis.clear();
267 for (unsigned i = 0; i < PMT_Last; ++i)
268 InheritedAnalysis[i] = NULL;
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);
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,
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);
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);
293 // Access toplevel manager
294 PMTopLevelManager *getTopLevelManager() { return TPM; }
295 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
297 unsigned getDepth() const { return Depth; }
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;
307 virtual unsigned getNumContainedPasses() {
308 return PassVector.size();
311 virtual PassManagerType getPassManagerType() const {
312 assert ( 0 && "Invalid use of getPassManagerType");
316 std::map<AnalysisID, Pass*> *getAvailableAnalysis() {
317 return &AvailableAnalysis;
320 // Collect AvailableAnalysis from all the active Pass Managers.
321 void populateInheritedAnalysis(PMStack &PMS) {
323 for (PMStack::iterator I = PMS.begin(), E = PMS.end();
325 InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis();
330 // Top level manager.
331 PMTopLevelManager *TPM;
333 // Collection of pass that are managed by this manager
334 std::vector<Pass *> PassVector;
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];
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
346 std::map<AnalysisID, Pass*> AvailableAnalysis;
348 // Collection of higher level analysis used by the pass managed by
350 std::vector<Pass *> HigherLevelAnalysis;
355 //===----------------------------------------------------------------------===//
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
363 class FPPassManager : public ModulePass, public PMDataManager {
367 explicit FPPassManager(int Depth)
368 : ModulePass(intptr_t(&ID)), PMDataManager(Depth) { }
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);
375 /// doInitialization - Run all of the initializers for the function passes.
377 bool doInitialization(Module &M);
379 /// doFinalization - Run all of the finalizers for the function passes.
381 bool doFinalization(Module &M);
383 /// Pass Manager itself does not invalidate any analysis info.
384 void getAnalysisUsage(AnalysisUsage &Info) const {
385 Info.setPreservesAll();
388 // Print passes managed by this manager
389 void dumpPassStructure(unsigned Offset);
391 virtual const char *getPassName() const {
392 return "Function Pass Manager";
395 FunctionPass *getContainedPass(unsigned N) {
396 assert ( N < PassVector.size() && "Pass number out of range!");
397 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
401 virtual PassManagerType getPassManagerType() const {
402 return PMT_FunctionPassManager;
408 extern void StartPassTimer(llvm::Pass *);
409 extern void StopPassTimer(llvm::Pass *);