1 //===- llvm/PassManager.h - Pass Inftrastructre classes --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Devang Patel and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the LLVM Pass Manager infrastructure.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/PassManager.h"
17 class llvm::PMDataManager;
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 hierarcy uses multiple inheritance but pass managers do not derive
33 // 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 //===----------------------------------------------------------------------===//
89 /// FunctionPassManager and PassManager, two top level managers, serve
90 /// as the public interface of pass manager infrastructure.
91 enum TopLevelManagerType {
92 TLM_Function, // FunctionPassManager
93 TLM_Pass // PassManager
96 // enums for debugging strings
97 enum PassDebuggingString {
98 EXECUTION_MSG, // "Executing Pass '"
99 MODIFICATION_MSG, // "' Made Modification '"
100 FREEING_MSG, // " Freeing Pass '"
101 ON_BASICBLOCK_MSG, // "' on BasicBlock '" + PassName + "'...\n"
102 ON_FUNCTION_MSG, // "' on Function '" + FunctionName + "'...\n"
103 ON_MODULE_MSG, // "' on Module '" + ModuleName + "'...\n"
104 ON_LOOP_MSG, // " 'on Loop ...\n'"
105 ON_CG_MSG // "' on Call Graph ...\n'"
108 //===----------------------------------------------------------------------===//
111 /// PMTopLevelManager manages LastUser info and collects common APIs used by
112 /// top level pass managers.
113 class PMTopLevelManager {
116 virtual unsigned getNumContainedManagers() {
117 return PassManagers.size();
120 /// Schedule pass P for execution. Make sure that passes required by
121 /// P are run before P is run. Update analysis info maintained by
122 /// the manager. Remove dead passes. This is a recursive function.
123 void schedulePass(Pass *P);
125 /// This is implemented by top level pass manager and used by
126 /// schedulePass() to add analysis info passes that are not available.
127 virtual void addTopLevelPass(Pass *P) = 0;
129 /// Set pass P as the last user of the given analysis passes.
130 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
132 /// Collect passes whose last user is P
133 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
135 /// Find the pass that implements Analysis AID. Search immutable
136 /// passes and all pass managers. If desired pass is not found
137 /// then return NULL.
138 Pass *findAnalysisPass(AnalysisID AID);
140 PMTopLevelManager(enum TopLevelManagerType t);
141 virtual ~PMTopLevelManager();
143 /// Add immutable pass and initialize it.
144 inline void addImmutablePass(ImmutablePass *P) {
146 ImmutablePasses.push_back(P);
149 inline std::vector<ImmutablePass *>& getImmutablePasses() {
150 return ImmutablePasses;
153 void addPassManager(Pass *Manager) {
154 PassManagers.push_back(Manager);
157 // Add Manager into the list of managers that are not directly
158 // maintained by this top level pass manager
159 inline void addIndirectPassManager(PMDataManager *Manager) {
160 IndirectPassManagers.push_back(Manager);
163 // Print passes managed by this top level manager.
164 void dumpPasses() const;
165 void dumpArguments() const;
167 void initializeAllAnalysisInfo();
169 // Active Pass Managers
174 /// Collection of pass managers
175 std::vector<Pass *> PassManagers;
179 /// Collection of pass managers that are not directly maintained
180 /// by this pass manager
181 std::vector<PMDataManager *> IndirectPassManagers;
183 // Map to keep track of last user of the analysis pass.
184 // LastUser->second is the last user of Lastuser->first.
185 std::map<Pass *, Pass *> LastUser;
187 /// Immutable passes are managed by top level manager.
188 std::vector<ImmutablePass *> ImmutablePasses;
193 //===----------------------------------------------------------------------===//
196 /// PMDataManager provides the common place to manage the analysis data
197 /// used by pass managers.
198 class PMDataManager {
200 PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
201 initializeAnalysisInfo();
204 virtual ~PMDataManager();
206 /// Return true IFF pass P's required analysis set does not required new
208 bool manageablePass(Pass *P);
210 /// Augment AvailableAnalysis by adding analysis made available by pass P.
211 void recordAvailableAnalysis(Pass *P);
213 /// Remove Analysis that is not preserved by the pass
214 void removeNotPreservedAnalysis(Pass *P);
216 /// Remove dead passes
217 void removeDeadPasses(Pass *P, std::string Msg, enum PassDebuggingString);
219 /// Add pass P into the PassVector. Update
220 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
221 void add(Pass *P, bool ProcessAnalysis = true);
223 /// Initialize available analysis information.
224 void initializeAnalysisInfo() {
225 TransferLastUses.clear();
226 AvailableAnalysis.clear();
229 /// Populate RequiredPasses with the analysis pass that are required by
231 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
234 /// All Required analyses should be available to the pass as it runs! Here
235 /// we fill in the AnalysisImpls member of the pass so that it can
236 /// successfully use the getAnalysis() method to retrieve the
237 /// implementations it needs.
238 void initializeAnalysisImpl(Pass *P);
240 /// Find the pass that implements Analysis AID. If desired pass is not found
241 /// then return NULL.
242 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
244 // Access toplevel manager
245 PMTopLevelManager *getTopLevelManager() { return TPM; }
246 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
248 unsigned getDepth() const { return Depth; }
250 // Print routines used by debug-pass
251 void dumpLastUses(Pass *P, unsigned Offset) const;
252 void dumpPassArguments() const;
253 void dumpPassInfo(Pass *P, enum PassDebuggingString S1,
254 enum PassDebuggingString S2, std::string Msg);
255 void dumpAnalysisSetInfo(const char *Msg, Pass *P,
256 const std::vector<AnalysisID> &Set) const;
258 std::vector<Pass *>& getTransferredLastUses() {
259 return TransferLastUses;
262 virtual unsigned getNumContainedPasses() {
263 return PassVector.size();
266 virtual PassManagerType getPassManagerType() const {
267 assert ( 0 && "Invalid use of getPassManagerType");
272 // If a FunctionPass F is the last user of ModulePass info M
273 // then the F's manager, not F, records itself as a last user of M.
274 // Current pass manage is requesting parent manager to record parent
275 // manager as the last user of these TrransferLastUses passes.
276 std::vector<Pass *> TransferLastUses;
278 // Top level manager.
279 PMTopLevelManager *TPM;
281 // Collection of pass that are managed by this manager
282 std::vector<Pass *> PassVector;
285 // Set of available Analysis. This information is used while scheduling
286 // pass. If a pass requires an analysis which is not not available then
287 // equired analysis pass is scheduled to run before the pass itself is
289 std::map<AnalysisID, Pass*> AvailableAnalysis;
294 //===----------------------------------------------------------------------===//
297 /// FPPassManager manages BBPassManagers and FunctionPasses.
298 /// It batches all function passes and basic block pass managers together and
299 /// sequence them to process one function at a time before processing next
302 class FPPassManager : public ModulePass, public PMDataManager {
305 FPPassManager(int Depth) : PMDataManager(Depth) { }
307 /// run - Execute all of the passes scheduled for execution. Keep track of
308 /// whether any of the passes modifies the module, and if so, return true.
309 bool runOnFunction(Function &F);
310 bool runOnModule(Module &M);
312 /// doInitialization - Run all of the initializers for the function passes.
314 bool doInitialization(Module &M);
316 /// doFinalization - Run all of the initializers for the function passes.
318 bool doFinalization(Module &M);
320 /// Pass Manager itself does not invalidate any analysis info.
321 void getAnalysisUsage(AnalysisUsage &Info) const {
322 Info.setPreservesAll();
325 // Print passes managed by this manager
326 void dumpPassStructure(unsigned Offset);
328 virtual const char *getPassName() const {
329 return "Function Pass Manager";
332 FunctionPass *getContainedPass(unsigned N) {
333 assert ( N < PassVector.size() && "Pass number out of range!");
334 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
338 virtual PassManagerType getPassManagerType() const {
339 return PMT_FunctionPassManager;
345 extern void StartPassTimer(Pass *);
346 extern void StopPassTimer(Pass *);