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"
15 #include "llvm/Support/Timer.h"
18 class llvm::PMDataManager;
21 //===----------------------------------------------------------------------===//
23 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
25 // o Manage optimization pass execution order
26 // o Make required Analysis information available before pass P is run
27 // o Release memory occupied by dead passes
28 // o If Analysis information is dirtied by a pass then regenerate Analysis
29 // information before it is consumed by another pass.
31 // Pass Manager Infrastructure uses multiple pass managers. They are
32 // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
33 // This class hierarcy uses multiple inheritance but pass managers do not derive
34 // from another pass manager.
36 // PassManager and FunctionPassManager are two top-level pass manager that
37 // represents the external interface of this entire pass manager infrastucture.
39 // Important classes :
41 // [o] class PMTopLevelManager;
43 // Two top level managers, PassManager and FunctionPassManager, derive from
44 // PMTopLevelManager. PMTopLevelManager manages information used by top level
45 // managers such as last user info.
47 // [o] class PMDataManager;
49 // PMDataManager manages information, e.g. list of available analysis info,
50 // used by a pass manager to manage execution order of passes. It also provides
51 // a place to implement common pass manager APIs. All pass managers derive from
54 // [o] class BBPassManager : public FunctionPass, public PMDataManager;
56 // BBPassManager manages BasicBlockPasses.
58 // [o] class FunctionPassManager;
60 // This is a external interface used by JIT to manage FunctionPasses. This
61 // interface relies on FunctionPassManagerImpl to do all the tasks.
63 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
64 // public PMTopLevelManager;
66 // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
68 // [o] class FPPassManager : public ModulePass, public PMDataManager;
70 // FPPassManager manages FunctionPasses and BBPassManagers
72 // [o] class MPPassManager : public Pass, public PMDataManager;
74 // MPPassManager manages ModulePasses and FPPassManagers
76 // [o] class PassManager;
78 // This is a external interface used by various tools to manages passes. It
79 // relies on PassManagerImpl to do all the tasks.
81 // [o] class PassManagerImpl : public Pass, public PMDataManager,
82 // public PMDTopLevelManager
84 // PassManagerImpl is a top level pass manager responsible for managing
86 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
100 /// PMTopLevelManager manages LastUser info and collects common APIs used by
101 /// top level pass managers.
102 class PMTopLevelManager {
105 virtual unsigned getNumContainedManagers() {
106 return PassManagers.size();
109 /// Schedule pass P for execution. Make sure that passes required by
110 /// P are run before P is run. Update analysis info maintained by
111 /// the manager. Remove dead passes. This is a recursive function.
112 void schedulePass(Pass *P);
114 /// This is implemented by top level pass manager and used by
115 /// schedulePass() to add analysis info passes that are not available.
116 virtual void addTopLevelPass(Pass *P) = 0;
118 /// Set pass P as the last user of the given analysis passes.
119 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
121 /// Collect passes whose last user is P
122 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
124 /// Find the pass that implements Analysis AID. Search immutable
125 /// passes and all pass managers. If desired pass is not found
126 /// then return NULL.
127 Pass *findAnalysisPass(AnalysisID AID);
129 PMTopLevelManager(enum TopLevelManagerType t);
130 virtual ~PMTopLevelManager();
132 /// Add immutable pass and initialize it.
133 inline void addImmutablePass(ImmutablePass *P) {
135 ImmutablePasses.push_back(P);
138 inline std::vector<ImmutablePass *>& getImmutablePasses() {
139 return ImmutablePasses;
142 void addPassManager(Pass *Manager) {
143 PassManagers.push_back(Manager);
146 // Add Manager into the list of managers that are not directly
147 // maintained by this top level pass manager
148 inline void addIndirectPassManager(PMDataManager *Manager) {
149 IndirectPassManagers.push_back(Manager);
152 // Print passes managed by this top level manager.
153 void dumpPasses() const;
154 void dumpArguments() const;
156 void initializeAllAnalysisInfo();
158 // Active Pass Managers
163 /// Collection of pass managers
164 std::vector<Pass *> PassManagers;
168 /// Collection of pass managers that are not directly maintained
169 /// by this pass manager
170 std::vector<PMDataManager *> IndirectPassManagers;
172 // Map to keep track of last user of the analysis pass.
173 // LastUser->second is the last user of Lastuser->first.
174 std::map<Pass *, Pass *> LastUser;
176 /// Immutable passes are managed by top level manager.
177 std::vector<ImmutablePass *> ImmutablePasses;
182 //===----------------------------------------------------------------------===//
185 /// PMDataManager provides the common place to manage the analysis data
186 /// used by pass managers.
187 class PMDataManager {
189 PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
190 initializeAnalysisInfo();
193 virtual ~PMDataManager();
195 /// Return true IFF pass P's required analysis set does not required new
197 bool manageablePass(Pass *P);
199 /// Augment AvailableAnalysis by adding analysis made available by pass P.
200 void recordAvailableAnalysis(Pass *P);
202 /// Remove Analysis that is not preserved by the pass
203 void removeNotPreservedAnalysis(Pass *P);
205 /// Remove dead passes
206 void removeDeadPasses(Pass *P, std::string &Msg);
208 /// Add pass P into the PassVector. Update
209 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
210 void add(Pass *P, bool ProcessAnalysis = true);
212 /// Initialize available analysis information.
213 void initializeAnalysisInfo() {
214 TransferLastUses.clear();
215 AvailableAnalysis.clear();
218 /// Populate RequiredPasses with the analysis pass that are required by
220 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
223 /// All Required analyses should be available to the pass as it runs! Here
224 /// we fill in the AnalysisImpls member of the pass so that it can
225 /// successfully use the getAnalysis() method to retrieve the
226 /// implementations it needs.
227 void initializeAnalysisImpl(Pass *P);
229 /// Find the pass that implements Analysis AID. If desired pass is not found
230 /// then return NULL.
231 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
233 // Access toplevel manager
234 PMTopLevelManager *getTopLevelManager() { return TPM; }
235 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
237 unsigned getDepth() const { return Depth; }
239 // Print routines used by debug-pass
240 void dumpLastUses(Pass *P, unsigned Offset) const;
241 void dumpPassArguments() const;
242 void dumpPassInfo(Pass *P, std::string &Msg1, std::string &Msg2) const;
243 void dumpAnalysisSetInfo(const char *Msg, Pass *P,
244 const std::vector<AnalysisID> &Set) const;
246 std::vector<Pass *>& getTransferredLastUses() {
247 return TransferLastUses;
250 virtual unsigned getNumContainedPasses() {
251 return PassVector.size();
254 virtual PassManagerType getPassManagerType() {
255 assert ( 0 && "Invalid use of getPassManagerType");
260 // If a FunctionPass F is the last user of ModulePass info M
261 // then the F's manager, not F, records itself as a last user of M.
262 // Current pass manage is requesting parent manager to record parent
263 // manager as the last user of these TrransferLastUses passes.
264 std::vector<Pass *> TransferLastUses;
266 // Top level manager.
267 PMTopLevelManager *TPM;
269 // Collection of pass that are managed by this manager
270 std::vector<Pass *> PassVector;
273 // Set of available Analysis. This information is used while scheduling
274 // pass. If a pass requires an analysis which is not not available then
275 // equired analysis pass is scheduled to run before the pass itself is
277 std::map<AnalysisID, Pass*> AvailableAnalysis;
282 //===----------------------------------------------------------------------===//
285 /// FPPassManager manages BBPassManagers and FunctionPasses.
286 /// It batches all function passes and basic block pass managers together and
287 /// sequence them to process one function at a time before processing next
290 class FPPassManager : public ModulePass, public PMDataManager {
293 FPPassManager(int Depth) : PMDataManager(Depth) { }
295 /// run - Execute all of the passes scheduled for execution. Keep track of
296 /// whether any of the passes modifies the module, and if so, return true.
297 bool runOnFunction(Function &F);
298 bool runOnModule(Module &M);
300 /// doInitialization - Run all of the initializers for the function passes.
302 bool doInitialization(Module &M);
304 /// doFinalization - Run all of the initializers for the function passes.
306 bool doFinalization(Module &M);
308 /// Pass Manager itself does not invalidate any analysis info.
309 void getAnalysisUsage(AnalysisUsage &Info) const {
310 Info.setPreservesAll();
313 // Print passes managed by this manager
314 void dumpPassStructure(unsigned Offset);
316 FunctionPass *getContainedPass(unsigned N) {
317 assert ( N < PassVector.size() && "Pass number out of range!");
318 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
322 virtual PassManagerType getPassManagerType() {
323 return PMT_FunctionPassManager;
327 //===----------------------------------------------------------------------===//
328 // TimingInfo Class - This class is used to calculate information about the
329 // amount of time each pass takes to execute. This only happens when
330 // -time-passes is enabled on the command line.
334 std::map<Pass*, Timer> TimingData;
338 // Use 'create' member to get this.
339 TimingInfo() : TG("... Pass execution timing report ...") {}
341 // TimingDtor - Print out information about timing information
343 // Delete all of the timers...
345 // TimerGroup is deleted next, printing the report.
348 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
349 // to a non null value (if the -time-passes option is enabled) or it leaves it
350 // null. It may be called multiple times.
351 static void createTheTimeInfo();
353 void passStarted(Pass *P) {
355 if (dynamic_cast<PMDataManager *>(P))
358 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
359 if (I == TimingData.end())
360 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
361 I->second.startTimer();
363 void passEnded(Pass *P) {
365 if (dynamic_cast<PMDataManager *>(P))
368 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
369 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
370 I->second.stopTimer();
374 extern TimingInfo *getTheTimeInfo();