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 class llvm::PMDataManager;
19 //===----------------------------------------------------------------------===//
21 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
23 // o Manage optimization pass execution order
24 // o Make required Analysis information available before pass P is run
25 // o Release memory occupied by dead passes
26 // o If Analysis information is dirtied by a pass then regenerate Analysis
27 // information before it is consumed by another pass.
29 // Pass Manager Infrastructure uses multiple pass managers. They are
30 // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
31 // This class hierarcy uses multiple inheritance but pass managers do not derive
32 // from another pass manager.
34 // PassManager and FunctionPassManager are two top-level pass manager that
35 // represents the external interface of this entire pass manager infrastucture.
37 // Important classes :
39 // [o] class PMTopLevelManager;
41 // Two top level managers, PassManager and FunctionPassManager, derive from
42 // PMTopLevelManager. PMTopLevelManager manages information used by top level
43 // managers such as last user info.
45 // [o] class PMDataManager;
47 // PMDataManager manages information, e.g. list of available analysis info,
48 // used by a pass manager to manage execution order of passes. It also provides
49 // a place to implement common pass manager APIs. All pass managers derive from
52 // [o] class BBPassManager : public FunctionPass, public PMDataManager;
54 // BBPassManager manages BasicBlockPasses.
56 // [o] class FunctionPassManager;
58 // This is a external interface used by JIT to manage FunctionPasses. This
59 // interface relies on FunctionPassManagerImpl to do all the tasks.
61 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
62 // public PMTopLevelManager;
64 // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
66 // [o] class FPPassManager : public ModulePass, public PMDataManager;
68 // FPPassManager manages FunctionPasses and BBPassManagers
70 // [o] class MPPassManager : public Pass, public PMDataManager;
72 // MPPassManager manages ModulePasses and FPPassManagers
74 // [o] class PassManager;
76 // This is a external interface used by various tools to manages passes. It
77 // relies on PassManagerImpl to do all the tasks.
79 // [o] class PassManagerImpl : public Pass, public PMDataManager,
80 // public PMDTopLevelManager
82 // PassManagerImpl is a top level pass manager responsible for managing
84 //===----------------------------------------------------------------------===//
86 #ifndef PASSMANAGERS_H
87 #define PASSMANAGERS_H
91 /// FunctionPassManager and PassManager, two top level managers, serve
92 /// as the public interface of pass manager infrastructure.
93 enum TopLevelManagerType {
94 TLM_Function, // FunctionPassManager
95 TLM_Pass // PassManager
98 // enums for debugging strings
99 enum PassDebuggingString {
100 EXECUTION_MSG, // "Executing Pass '"
101 MODIFICATION_MSG, // "' Made Modification '"
102 FREEING_MSG, // " Freeing Pass '"
103 ON_BASICBLOCK_MSG, // "' on BasicBlock '" + PassName + "'...\n"
104 ON_FUNCTION_MSG, // "' on Function '" + FunctionName + "'...\n"
105 ON_MODULE_MSG, // "' on Module '" + ModuleName + "'...\n"
106 ON_LOOP_MSG, // " 'on Loop ...\n'"
107 ON_CG_MSG // "' on Call Graph ...\n'"
110 //===----------------------------------------------------------------------===//
113 /// PMTopLevelManager manages LastUser info and collects common APIs used by
114 /// top level pass managers.
115 class PMTopLevelManager {
118 virtual unsigned getNumContainedManagers() {
119 return PassManagers.size();
122 /// Schedule pass P for execution. Make sure that passes required by
123 /// P are run before P is run. Update analysis info maintained by
124 /// the manager. Remove dead passes. This is a recursive function.
125 void schedulePass(Pass *P);
127 /// This is implemented by top level pass manager and used by
128 /// schedulePass() to add analysis info passes that are not available.
129 virtual void addTopLevelPass(Pass *P) = 0;
131 /// Set pass P as the last user of the given analysis passes.
132 void setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, Pass *P);
134 /// Collect passes whose last user is P
135 void collectLastUses(SmallVector<Pass *, 12> &LastUses, Pass *P);
137 /// Find the pass that implements Analysis AID. Search immutable
138 /// passes and all pass managers. If desired pass is not found
139 /// then return NULL.
140 Pass *findAnalysisPass(AnalysisID AID);
142 explicit PMTopLevelManager(enum TopLevelManagerType t);
143 virtual ~PMTopLevelManager();
145 /// Add immutable pass and initialize it.
146 inline void addImmutablePass(ImmutablePass *P) {
148 ImmutablePasses.push_back(P);
151 inline std::vector<ImmutablePass *>& getImmutablePasses() {
152 return ImmutablePasses;
155 void addPassManager(Pass *Manager) {
156 PassManagers.push_back(Manager);
159 // Add Manager into the list of managers that are not directly
160 // maintained by this top level pass manager
161 inline void addIndirectPassManager(PMDataManager *Manager) {
162 IndirectPassManagers.push_back(Manager);
165 // Print passes managed by this top level manager.
166 void dumpPasses() const;
167 void dumpArguments() const;
169 void initializeAllAnalysisInfo();
171 // Active Pass Managers
176 /// Collection of pass managers
177 std::vector<Pass *> PassManagers;
181 /// Collection of pass managers that are not directly maintained
182 /// by this pass manager
183 std::vector<PMDataManager *> IndirectPassManagers;
185 // Map to keep track of last user of the analysis pass.
186 // LastUser->second is the last user of Lastuser->first.
187 std::map<Pass *, Pass *> LastUser;
189 /// Immutable passes are managed by top level manager.
190 std::vector<ImmutablePass *> ImmutablePasses;
195 //===----------------------------------------------------------------------===//
198 /// PMDataManager provides the common place to manage the analysis data
199 /// used by pass managers.
200 class PMDataManager {
203 explicit PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
204 initializeAnalysisInfo();
207 virtual ~PMDataManager();
209 /// Return true IFF pass P's required analysis set does not required new
211 bool manageablePass(Pass *P);
213 /// Augment AvailableAnalysis by adding analysis made available by pass P.
214 void recordAvailableAnalysis(Pass *P);
216 /// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
217 void verifyPreservedAnalysis(Pass *P);
219 /// Remove Analysis that is not preserved by the pass
220 void removeNotPreservedAnalysis(Pass *P);
222 /// Remove dead passes
223 void removeDeadPasses(Pass *P, const char *Msg, enum PassDebuggingString);
225 /// Add pass P into the PassVector. Update
226 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
227 void add(Pass *P, bool ProcessAnalysis = true);
229 /// Add RequiredPass into list of lower level passes required by pass P.
230 /// RequiredPass is run on the fly by Pass Manager when P requests it
231 /// through getAnalysis interface.
232 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
234 virtual Pass * getOnTheFlyPass(Pass *P, const PassInfo *PI, Function &F) {
235 assert (0 && "Unable to find on the fly pass");
239 /// Initialize available analysis information.
240 void initializeAnalysisInfo() {
241 AvailableAnalysis.clear();
242 for (unsigned i = 0; i < PMT_Last; ++i)
243 InheritedAnalysis[i] = NULL;
246 // Return true if P preserves high level analysis used by other
247 // passes that are managed by this manager.
248 bool preserveHigherLevelAnalysis(Pass *P);
251 /// Populate RequiredPasses with analysis pass that are required by
252 /// pass P and are available. Populate ReqPassNotAvailable with analysis
253 /// pass that are required by pass P but are not available.
254 void collectRequiredAnalysis(SmallVector<Pass *, 8> &RequiredPasses,
255 SmallVector<AnalysisID, 8> &ReqPassNotAvailable,
258 /// All Required analyses should be available to the pass as it runs! Here
259 /// we fill in the AnalysisImpls member of the pass so that it can
260 /// successfully use the getAnalysis() method to retrieve the
261 /// implementations it needs.
262 void initializeAnalysisImpl(Pass *P);
264 /// Find the pass that implements Analysis AID. If desired pass is not found
265 /// then return NULL.
266 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
268 // Access toplevel manager
269 PMTopLevelManager *getTopLevelManager() { return TPM; }
270 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
272 unsigned getDepth() const { return Depth; }
274 // Print routines used by debug-pass
275 void dumpLastUses(Pass *P, unsigned Offset) const;
276 void dumpPassArguments() const;
277 void dumpPassInfo(Pass *P, enum PassDebuggingString S1,
278 enum PassDebuggingString S2, const char *Msg);
279 void dumpAnalysisSetInfo(const char *Msg, Pass *P,
280 const std::vector<AnalysisID> &Set) const;
282 virtual unsigned getNumContainedPasses() {
283 return PassVector.size();
286 virtual PassManagerType getPassManagerType() const {
287 assert ( 0 && "Invalid use of getPassManagerType");
291 std::map<AnalysisID, Pass*> *getAvailableAnalysis() {
292 return &AvailableAnalysis;
295 // Collect AvailableAnalysis from all the active Pass Managers.
296 void populateInheritedAnalysis(PMStack &PMS) {
298 for (PMStack::iterator I = PMS.begin(), E = PMS.end();
300 InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis();
305 // Top level manager.
306 PMTopLevelManager *TPM;
308 // Collection of pass that are managed by this manager
309 std::vector<Pass *> PassVector;
311 // Collection of Analysis provided by Parent pass manager and
312 // used by current pass manager. At at time there can not be more
313 // then PMT_Last active pass mangers.
314 std::map<AnalysisID, Pass *> *InheritedAnalysis[PMT_Last];
317 // Set of available Analysis. This information is used while scheduling
318 // pass. If a pass requires an analysis which is not not available then
319 // equired analysis pass is scheduled to run before the pass itself is
321 std::map<AnalysisID, Pass*> AvailableAnalysis;
323 // Collection of higher level analysis used by the pass managed by
325 std::vector<Pass *> HigherLevelAnalysis;
330 //===----------------------------------------------------------------------===//
333 /// FPPassManager manages BBPassManagers and FunctionPasses.
334 /// It batches all function passes and basic block pass managers together and
335 /// sequence them to process one function at a time before processing next
338 class FPPassManager : public ModulePass, public PMDataManager {
342 explicit FPPassManager(int Depth)
343 : ModulePass(intptr_t(&ID)), PMDataManager(Depth) { }
345 /// run - Execute all of the passes scheduled for execution. Keep track of
346 /// whether any of the passes modifies the module, and if so, return true.
347 bool runOnFunction(Function &F);
348 bool runOnModule(Module &M);
350 /// doInitialization - Run all of the initializers for the function passes.
352 bool doInitialization(Module &M);
354 /// doFinalization - Run all of the finalizers for the function passes.
356 bool doFinalization(Module &M);
358 /// Pass Manager itself does not invalidate any analysis info.
359 void getAnalysisUsage(AnalysisUsage &Info) const {
360 Info.setPreservesAll();
363 // Print passes managed by this manager
364 void dumpPassStructure(unsigned Offset);
366 virtual const char *getPassName() const {
367 return "Function Pass Manager";
370 FunctionPass *getContainedPass(unsigned N) {
371 assert ( N < PassVector.size() && "Pass number out of range!");
372 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
376 virtual PassManagerType getPassManagerType() const {
377 return PMT_FunctionPassManager;
383 extern void StartPassTimer(llvm::Pass *);
384 extern void StopPassTimer(llvm::Pass *);