1 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
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 implements 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 //===----------------------------------------------------------------------===//
92 /// PMTopLevelManager manages LastUser info and collects common APIs used by
93 /// top level pass managers.
94 class PMTopLevelManager {
97 virtual unsigned getNumContainedManagers() {
98 return PassManagers.size();
101 /// Schedule pass P for execution. Make sure that passes required by
102 /// P are run before P is run. Update analysis info maintained by
103 /// the manager. Remove dead passes. This is a recursive function.
104 void schedulePass(Pass *P);
106 /// This is implemented by top level pass manager and used by
107 /// schedulePass() to add analysis info passes that are not available.
108 virtual void addTopLevelPass(Pass *P) = 0;
110 /// Set pass P as the last user of the given analysis passes.
111 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
113 /// Collect passes whose last user is P
114 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
116 /// Find the pass that implements Analysis AID. Search immutable
117 /// passes and all pass managers. If desired pass is not found
118 /// then return NULL.
119 Pass *findAnalysisPass(AnalysisID AID);
121 virtual ~PMTopLevelManager();
123 /// Add immutable pass and initialize it.
124 inline void addImmutablePass(ImmutablePass *P) {
126 ImmutablePasses.push_back(P);
129 inline std::vector<ImmutablePass *>& getImmutablePasses() {
130 return ImmutablePasses;
133 void addPassManager(Pass *Manager) {
134 PassManagers.push_back(Manager);
137 // Add Manager into the list of managers that are not directly
138 // maintained by this top level pass manager
139 inline void addIndirectPassManager(PMDataManager *Manager) {
140 IndirectPassManagers.push_back(Manager);
143 // Print passes managed by this top level manager.
144 void dumpPasses() const;
145 void dumpArguments() const;
147 void initializeAllAnalysisInfo();
149 // Active Pass Managers
154 /// Collection of pass managers
155 std::vector<Pass *> PassManagers;
159 /// Collection of pass managers that are not directly maintained
160 /// by this pass manager
161 std::vector<PMDataManager *> IndirectPassManagers;
163 // Map to keep track of last user of the analysis pass.
164 // LastUser->second is the last user of Lastuser->first.
165 std::map<Pass *, Pass *> LastUser;
167 /// Immutable passes are managed by top level manager.
168 std::vector<ImmutablePass *> ImmutablePasses;
173 //===----------------------------------------------------------------------===//
176 /// PMDataManager provides the common place to manage the analysis data
177 /// used by pass managers.
178 class PMDataManager {
180 PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
181 initializeAnalysisInfo();
184 virtual ~PMDataManager();
186 /// Return true IFF pass P's required analysis set does not required new
188 bool manageablePass(Pass *P);
190 /// Augment AvailableAnalysis by adding analysis made available by pass P.
191 void recordAvailableAnalysis(Pass *P);
193 /// Remove Analysis that is not preserved by the pass
194 void removeNotPreservedAnalysis(Pass *P);
196 /// Remove dead passes
197 void removeDeadPasses(Pass *P, std::string &Msg);
199 /// Add pass P into the PassVector. Update
200 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
201 void addPassToManager(Pass *P, bool ProcessAnalysis = true);
203 /// Initialize available analysis information.
204 void initializeAnalysisInfo() {
205 TransferLastUses.clear();
206 AvailableAnalysis.clear();
209 /// Populate RequiredPasses with the analysis pass that are required by
211 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
214 /// All Required analyses should be available to the pass as it runs! Here
215 /// we fill in the AnalysisImpls member of the pass so that it can
216 /// successfully use the getAnalysis() method to retrieve the
217 /// implementations it needs.
218 void initializeAnalysisImpl(Pass *P);
220 /// Find the pass that implements Analysis AID. If desired pass is not found
221 /// then return NULL.
222 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
224 // Access toplevel manager
225 PMTopLevelManager *getTopLevelManager() { return TPM; }
226 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
228 unsigned getDepth() const { return Depth; }
230 // Print routines used by debug-pass
231 void dumpLastUses(Pass *P, unsigned Offset) const;
232 void dumpPassArguments() const;
233 void dumpPassInfo(Pass *P, std::string &Msg1, std::string &Msg2) const;
234 void dumpAnalysisSetInfo(const char *Msg, Pass *P,
235 const std::vector<AnalysisID> &Set) const;
237 std::vector<Pass *>& getTransferredLastUses() {
238 return TransferLastUses;
241 virtual unsigned getNumContainedPasses() {
242 return PassVector.size();
245 virtual PassManagerType getPassManagerType() {
246 assert ( 0 && "Invalid use of getPassManagerType");
251 // If a FunctionPass F is the last user of ModulePass info M
252 // then the F's manager, not F, records itself as a last user of M.
253 // Current pass manage is requesting parent manager to record parent
254 // manager as the last user of these TrransferLastUses passes.
255 std::vector<Pass *> TransferLastUses;
257 // Top level manager.
258 PMTopLevelManager *TPM;
260 // Collection of pass that are managed by this manager
261 std::vector<Pass *> PassVector;
264 // Set of available Analysis. This information is used while scheduling
265 // pass. If a pass requires an analysis which is not not available then
266 // equired analysis pass is scheduled to run before the pass itself is
268 std::map<AnalysisID, Pass*> AvailableAnalysis;
273 //===----------------------------------------------------------------------===//
276 /// FPPassManager manages BBPassManagers and FunctionPasses.
277 /// It batches all function passes and basic block pass managers together and
278 /// sequence them to process one function at a time before processing next
281 class FPPassManager : public ModulePass, public PMDataManager {
284 FPPassManager(int Depth) : PMDataManager(Depth) { }
286 /// run - Execute all of the passes scheduled for execution. Keep track of
287 /// whether any of the passes modifies the module, and if so, return true.
288 bool runOnFunction(Function &F);
289 bool runOnModule(Module &M);
291 /// doInitialization - Run all of the initializers for the function passes.
293 bool doInitialization(Module &M);
295 /// doFinalization - Run all of the initializers for the function passes.
297 bool doFinalization(Module &M);
299 /// Pass Manager itself does not invalidate any analysis info.
300 void getAnalysisUsage(AnalysisUsage &Info) const {
301 Info.setPreservesAll();
304 // Print passes managed by this manager
305 void dumpPassStructure(unsigned Offset);
307 FunctionPass *getContainedPass(unsigned N) {
308 assert ( N < PassVector.size() && "Pass number out of range!");
309 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
313 virtual PassManagerType getPassManagerType() {
314 return PMT_FunctionPassManager;