blob: d71192689af5eb817a75c66a09142c38564c173f [file] [log] [blame]
Devang Patel6e5a1132006-11-07 21:31:57 +00001//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Devang Patelca58e352006-11-08 10:05:38 +00005// This file was developed by Devang Patel and is distributed under
Devang Patel6e5a1132006-11-07 21:31:57 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVM Pass Manager infrastructure.
11//
12//===----------------------------------------------------------------------===//
13
14
15#include "llvm/PassManager.h"
Devang Patelf1567a52006-12-13 20:03:48 +000016#include "llvm/Support/CommandLine.h"
Devang Patelb8817b92006-12-14 00:59:42 +000017#include "llvm/Support/Timer.h"
Devang Patel6e5a1132006-11-07 21:31:57 +000018#include "llvm/Module.h"
Devang Patelff631ae2006-11-15 01:27:05 +000019#include "llvm/ModuleProvider.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000020#include "llvm/Support/Streams.h"
Devang Patelb8817b92006-12-14 00:59:42 +000021#include "llvm/Support/ManagedStatic.h"
Devang Patela9844592006-11-11 01:31:05 +000022#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000023#include <map>
Devang Patelffca9102006-12-15 19:39:30 +000024
Devang Patel6e5a1132006-11-07 21:31:57 +000025using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000026class llvm::PMDataManager;
Devang Patel6e5a1132006-11-07 21:31:57 +000027
Devang Patel6fea2852006-12-07 18:23:30 +000028//===----------------------------------------------------------------------===//
29// Overview:
30// The Pass Manager Infrastructure manages passes. It's responsibilities are:
31//
32// o Manage optimization pass execution order
33// o Make required Analysis information available before pass P is run
34// o Release memory occupied by dead passes
35// o If Analysis information is dirtied by a pass then regenerate Analysis
36// information before it is consumed by another pass.
37//
Chris Lattnerce22ca32006-12-14 18:22:14 +000038// Pass Manager Infrastructure uses multiple pass managers. They are
Devang Patel67d6a5e2006-12-19 19:46:59 +000039// PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
Chris Lattnerce22ca32006-12-14 18:22:14 +000040// This class hierarcy uses multiple inheritance but pass managers do not derive
41// from another pass manager.
Devang Patel6fea2852006-12-07 18:23:30 +000042//
Chris Lattnerce22ca32006-12-14 18:22:14 +000043// PassManager and FunctionPassManager are two top-level pass manager that
Devang Patel6fea2852006-12-07 18:23:30 +000044// represents the external interface of this entire pass manager infrastucture.
45//
46// Important classes :
47//
48// [o] class PMTopLevelManager;
49//
50// Two top level managers, PassManager and FunctionPassManager, derive from
51// PMTopLevelManager. PMTopLevelManager manages information used by top level
52// managers such as last user info.
53//
54// [o] class PMDataManager;
55//
56// PMDataManager manages information, e.g. list of available analysis info,
57// used by a pass manager to manage execution order of passes. It also provides
58// a place to implement common pass manager APIs. All pass managers derive from
59// PMDataManager.
60//
Devang Patel67d6a5e2006-12-19 19:46:59 +000061// [o] class BBPassManager : public FunctionPass, public PMDataManager;
Devang Patel6fea2852006-12-07 18:23:30 +000062//
Devang Patel67d6a5e2006-12-19 19:46:59 +000063// BBPassManager manages BasicBlockPasses.
Devang Patel6fea2852006-12-07 18:23:30 +000064//
65// [o] class FunctionPassManager;
66//
67// This is a external interface used by JIT to manage FunctionPasses. This
68// interface relies on FunctionPassManagerImpl to do all the tasks.
69//
70// [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
71// public PMTopLevelManager;
72//
Devang Patel67d6a5e2006-12-19 19:46:59 +000073// FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
Devang Patel6fea2852006-12-07 18:23:30 +000074//
Devang Patel67d6a5e2006-12-19 19:46:59 +000075// [o] class FPPassManager : public ModulePass, public PMDataManager;
Devang Patel6fea2852006-12-07 18:23:30 +000076//
Devang Patel67d6a5e2006-12-19 19:46:59 +000077// FPPassManager manages FunctionPasses and BBPassManagers
78//
79// [o] class MPPassManager : public Pass, public PMDataManager;
80//
81// MPPassManager manages ModulePasses and FPPassManagers
Devang Patel6fea2852006-12-07 18:23:30 +000082//
83// [o] class PassManager;
84//
85// This is a external interface used by various tools to manages passes. It
86// relies on PassManagerImpl to do all the tasks.
87//
88// [o] class PassManagerImpl : public Pass, public PMDataManager,
89// public PMDTopLevelManager
90//
91// PassManagerImpl is a top level pass manager responsible for managing
Devang Patel67d6a5e2006-12-19 19:46:59 +000092// MPPassManagers.
Devang Patel6fea2852006-12-07 18:23:30 +000093//===----------------------------------------------------------------------===//
94
Devang Patelf1567a52006-12-13 20:03:48 +000095namespace llvm {
96
97//===----------------------------------------------------------------------===//
98// Pass debugging information. Often it is useful to find out what pass is
99// running when a crash occurs in a utility. When this library is compiled with
100// debugging on, a command line option (--debug-pass) is enabled that causes the
101// pass name to be printed before it executes.
102//
103
Devang Patel03fb5872006-12-13 21:13:31 +0000104// Different debug levels that can be enabled...
105enum PassDebugLevel {
106 None, Arguments, Structure, Executions, Details
107};
108
Devang Patelf1567a52006-12-13 20:03:48 +0000109static cl::opt<enum PassDebugLevel>
110PassDebugging_New("debug-pass", cl::Hidden,
111 cl::desc("Print PassManager debugging information"),
112 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +0000113 clEnumVal(None , "disable debug output"),
114 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
115 clEnumVal(Structure , "print pass structure before run()"),
116 clEnumVal(Executions, "print pass name before it is executed"),
117 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +0000118 clEnumValEnd));
119} // End of llvm namespace
120
Devang Patelffca9102006-12-15 19:39:30 +0000121namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000122
Devang Patelf33f3eb2006-12-07 19:21:29 +0000123//===----------------------------------------------------------------------===//
124// PMTopLevelManager
125//
126/// PMTopLevelManager manages LastUser info and collects common APIs used by
127/// top level pass managers.
Devang Patelffca9102006-12-15 19:39:30 +0000128class VISIBILITY_HIDDEN PMTopLevelManager {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000129public:
130
Devang Patel67d6a5e2006-12-19 19:46:59 +0000131 virtual unsigned getNumContainedManagers() {
132 return PassManagers.size();
Devang Patelf33f3eb2006-12-07 19:21:29 +0000133 }
134
135 /// Schedule pass P for execution. Make sure that passes required by
136 /// P are run before P is run. Update analysis info maintained by
137 /// the manager. Remove dead passes. This is a recursive function.
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000138 void schedulePass(Pass *P);
Devang Patelf33f3eb2006-12-07 19:21:29 +0000139
140 /// This is implemented by top level pass manager and used by
141 /// schedulePass() to add analysis info passes that are not available.
142 virtual void addTopLevelPass(Pass *P) = 0;
143
144 /// Set pass P as the last user of the given analysis passes.
145 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
146
147 /// Collect passes whose last user is P
148 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
149
Devang Patel640c5bb2006-12-08 22:30:11 +0000150 /// Find the pass that implements Analysis AID. Search immutable
151 /// passes and all pass managers. If desired pass is not found
152 /// then return NULL.
153 Pass *findAnalysisPass(AnalysisID AID);
154
Devang Patelf33f3eb2006-12-07 19:21:29 +0000155 virtual ~PMTopLevelManager() {
Devang Patelab97cf42006-12-13 00:09:23 +0000156 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
157 E = PassManagers.end(); I != E; ++I)
158 delete *I;
159
160 for (std::vector<ImmutablePass *>::iterator
161 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
162 delete *I;
163
Devang Patelf33f3eb2006-12-07 19:21:29 +0000164 PassManagers.clear();
165 }
166
Devang Patele0eb9d82006-12-07 20:51:18 +0000167 /// Add immutable pass and initialize it.
168 inline void addImmutablePass(ImmutablePass *P) {
169 P->initializePass();
170 ImmutablePasses.push_back(P);
171 }
172
173 inline std::vector<ImmutablePass *>& getImmutablePasses() {
174 return ImmutablePasses;
175 }
176
Devang Patel5bbeb492006-12-08 22:47:25 +0000177 void addPassManager(Pass *Manager) {
178 PassManagers.push_back(Manager);
179 }
180
Devang Patelaf1fca52006-12-08 23:11:43 +0000181 // Add Manager into the list of managers that are not directly
182 // maintained by this top level pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000183 inline void addIndirectPassManager(PMDataManager *Manager) {
184 IndirectPassManagers.push_back(Manager);
Devang Patelaf1fca52006-12-08 23:11:43 +0000185 }
186
Devang Pateleda56172006-12-12 23:34:33 +0000187 // Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000188 void dumpPasses() const;
189 void dumpArguments() const;
Devang Pateleda56172006-12-12 23:34:33 +0000190
Devang Patele3068402006-12-21 00:16:50 +0000191 void initializeAllAnalysisInfo();
192
Devang Patel67d6a5e2006-12-19 19:46:59 +0000193protected:
Devang Patelf33f3eb2006-12-07 19:21:29 +0000194
195 /// Collection of pass managers
196 std::vector<Pass *> PassManagers;
197
Devang Patel67d6a5e2006-12-19 19:46:59 +0000198private:
199
Devang Patelaf1fca52006-12-08 23:11:43 +0000200 /// Collection of pass managers that are not directly maintained
201 /// by this pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000202 std::vector<PMDataManager *> IndirectPassManagers;
Devang Patelaf1fca52006-12-08 23:11:43 +0000203
Devang Patelf33f3eb2006-12-07 19:21:29 +0000204 // Map to keep track of last user of the analysis pass.
205 // LastUser->second is the last user of Lastuser->first.
206 std::map<Pass *, Pass *> LastUser;
Devang Patele0eb9d82006-12-07 20:51:18 +0000207
208 /// Immutable passes are managed by top level manager.
209 std::vector<ImmutablePass *> ImmutablePasses;
Devang Patelf33f3eb2006-12-07 19:21:29 +0000210};
Devang Patelffca9102006-12-15 19:39:30 +0000211
212} // End of anon namespace
Devang Patelf33f3eb2006-12-07 19:21:29 +0000213
Devang Patelf3827bc2006-12-07 19:54:15 +0000214//===----------------------------------------------------------------------===//
215// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000216
Devang Patelffca9102006-12-15 19:39:30 +0000217namespace llvm {
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000218/// PMDataManager provides the common place to manage the analysis data
219/// used by pass managers.
220class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000221public:
Devang Patel56d48ec2006-12-15 22:57:49 +0000222 PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
Devang Patelf3827bc2006-12-07 19:54:15 +0000223 initializeAnalysisInfo();
224 }
225
Devang Patelab97cf42006-12-13 00:09:23 +0000226 virtual ~PMDataManager() {
227
228 for (std::vector<Pass *>::iterator I = PassVector.begin(),
229 E = PassVector.end(); I != E; ++I)
230 delete *I;
231
232 PassVector.clear();
233 }
234
Devang Patela9844592006-11-11 01:31:05 +0000235 /// Return true IFF pass P's required analysis set does not required new
236 /// manager.
237 bool manageablePass(Pass *P);
238
Devang Patela9844592006-11-11 01:31:05 +0000239 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000240 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000241
Devang Patela9844592006-11-11 01:31:05 +0000242 /// Remove Analysis that is not preserved by the pass
243 void removeNotPreservedAnalysis(Pass *P);
244
245 /// Remove dead passes
Devang Patel200d3052006-12-13 23:50:44 +0000246 void removeDeadPasses(Pass *P, std::string &Msg);
Devang Patela9844592006-11-11 01:31:05 +0000247
Devang Patel8f677ce2006-12-07 18:47:25 +0000248 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000249 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattnerce22ca32006-12-14 18:22:14 +0000250 void addPassToManager(Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000251
Devang Patel1d6267c2006-12-07 23:05:44 +0000252 /// Initialize available analysis information.
Devang Patela6b6dcb2006-12-07 18:41:09 +0000253 void initializeAnalysisInfo() {
Devang Patel832bc072006-12-15 00:08:26 +0000254 TransferLastUses.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000255 AvailableAnalysis.clear();
256 }
257
Devang Patel1d6267c2006-12-07 23:05:44 +0000258 /// Populate RequiredPasses with the analysis pass that are required by
259 /// pass P.
260 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
261 Pass *P);
262
263 /// All Required analyses should be available to the pass as it runs! Here
264 /// we fill in the AnalysisImpls member of the pass so that it can
265 /// successfully use the getAnalysis() method to retrieve the
266 /// implementations it needs.
267 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000268
Devang Patel640c5bb2006-12-08 22:30:11 +0000269 /// Find the pass that implements Analysis AID. If desired pass is not found
270 /// then return NULL.
271 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
272
Devang Patelf3827bc2006-12-07 19:54:15 +0000273 // Access toplevel manager
274 PMTopLevelManager *getTopLevelManager() { return TPM; }
275 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
276
Devang Patel991aeba2006-12-15 20:13:01 +0000277 unsigned getDepth() const { return Depth; }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000278
Devang Patel991aeba2006-12-15 20:13:01 +0000279 // Print routines used by debug-pass
280 void dumpLastUses(Pass *P, unsigned Offset) const;
281 void dumpPassArguments() const;
282 void dumpPassInfo(Pass *P, std::string &Msg1, std::string &Msg2) const;
Devang Patelf6d1d212006-12-14 00:25:06 +0000283 void dumpAnalysisSetInfo(const char *Msg, Pass *P,
Devang Patel991aeba2006-12-15 20:13:01 +0000284 const std::vector<AnalysisID> &Set) const;
Devang Patel832bc072006-12-15 00:08:26 +0000285
286 std::vector<Pass *>& getTransferredLastUses() {
287 return TransferLastUses;
288 }
289
Devang Patelabfbe3b2006-12-16 00:56:26 +0000290 virtual unsigned getNumContainedPasses() {
291 return PassVector.size();
292 }
293
Devang Patel3b3f8992007-01-11 01:10:25 +0000294 virtual PassManagerType getPassManagerType() {
295 assert ( 0 && "Invalid use of getPassManagerType");
296 return PMT_Unknown;
297 }
Devang Patelbc03f132006-12-07 23:55:10 +0000298protected:
299
Devang Patel832bc072006-12-15 00:08:26 +0000300 // If a FunctionPass F is the last user of ModulePass info M
Devang Patelbc03f132006-12-07 23:55:10 +0000301 // then the F's manager, not F, records itself as a last user of M.
Devang Patel832bc072006-12-15 00:08:26 +0000302 // Current pass manage is requesting parent manager to record parent
303 // manager as the last user of these TrransferLastUses passes.
304 std::vector<Pass *> TransferLastUses;
Devang Patelbc03f132006-12-07 23:55:10 +0000305
306 // Top level manager.
Devang Patelbc03f132006-12-07 23:55:10 +0000307 PMTopLevelManager *TPM;
308
Devang Patelabfbe3b2006-12-16 00:56:26 +0000309 // Collection of pass that are managed by this manager
310 std::vector<Pass *> PassVector;
311
Devang Patela9844592006-11-11 01:31:05 +0000312private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000313 // Set of available Analysis. This information is used while scheduling
314 // pass. If a pass requires an analysis which is not not available then
315 // equired analysis pass is scheduled to run before the pass itself is
316 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000317 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000318
Devang Patel4c36e6b2006-12-07 23:24:58 +0000319 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000320};
321
Devang Patel10c2ca62006-12-12 22:47:13 +0000322//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000323// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000324//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000325/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000326/// pass together and sequence them to process one basic block before
327/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000328class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
329 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000330
331public:
Devang Patel67d6a5e2006-12-19 19:46:59 +0000332 BBPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +0000333
334 /// Add a pass into a passmanager queue.
335 bool addPass(Pass *p);
336
337 /// Execute all of the passes scheduled for execution. Keep track of
338 /// whether any of the passes modifies the function, and if so, return true.
339 bool runOnFunction(Function &F);
340
Devang Patelf9d96b92006-12-07 19:57:52 +0000341 /// Pass Manager itself does not invalidate any analysis info.
342 void getAnalysisUsage(AnalysisUsage &Info) const {
343 Info.setPreservesAll();
344 }
345
Devang Patel475c4532006-12-08 00:59:05 +0000346 bool doInitialization(Module &M);
347 bool doInitialization(Function &F);
348 bool doFinalization(Module &M);
349 bool doFinalization(Function &F);
350
Devang Pateleda56172006-12-12 23:34:33 +0000351 // Print passes managed by this manager
352 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +0000353 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000354 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
355 BasicBlockPass *BP = getContainedPass(Index);
356 BP->dumpPassStructure(Offset + 1);
357 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000358 }
359 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000360
361 BasicBlockPass *getContainedPass(unsigned N) {
362 assert ( N < PassVector.size() && "Pass number out of range!");
363 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
364 return BP;
365 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000366
367 virtual PassManagerType getPassManagerType() {
368 return PMT_BasicBlockPassManager;
369 }
Devang Patelca58e352006-11-08 10:05:38 +0000370};
371
Devang Patel10c2ca62006-12-12 22:47:13 +0000372//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000373// FPPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000374//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000375/// FPPassManager manages BBPassManagers and FunctionPasses.
376/// It batches all function passes and basic block pass managers together and
377/// sequence them to process one function at a time before processing next
378/// function.
379
380class FPPassManager : public ModulePass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000381
Devang Patel67d6a5e2006-12-19 19:46:59 +0000382public:
383 FPPassManager(int Depth) : PMDataManager(Depth) {
384 activeBBPassManager = NULL;
Devang Patelabcd1d32006-12-07 21:27:23 +0000385 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000386
387 /// Add a pass into a passmanager queue.
388 bool addPass(Pass *p);
389
390 /// run - Execute all of the passes scheduled for execution. Keep track of
391 /// whether any of the passes modifies the module, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000392 bool runOnFunction(Function &F);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000393 bool runOnModule(Module &M);
Devang Patelca58e352006-11-08 10:05:38 +0000394
Devang Patelff631ae2006-11-15 01:27:05 +0000395 /// doInitialization - Run all of the initializers for the function passes.
396 ///
397 bool doInitialization(Module &M);
398
399 /// doFinalization - Run all of the initializers for the function passes.
400 ///
401 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000402
403 /// Pass Manager itself does not invalidate any analysis info.
404 void getAnalysisUsage(AnalysisUsage &Info) const {
405 Info.setPreservesAll();
406 }
407
Devang Pateleda56172006-12-12 23:34:33 +0000408 // Print passes managed by this manager
409 void dumpPassStructure(unsigned Offset) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000410 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000411 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
412 FunctionPass *FP = getContainedPass(Index);
413 FP->dumpPassStructure(Offset + 1);
414 dumpLastUses(FP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000415 }
416 }
417
Devang Patelabfbe3b2006-12-16 00:56:26 +0000418 FunctionPass *getContainedPass(unsigned N) {
419 assert ( N < PassVector.size() && "Pass number out of range!");
420 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
421 return FP;
422 }
423
Devang Patel3b3f8992007-01-11 01:10:25 +0000424 virtual PassManagerType getPassManagerType() {
425 return PMT_FunctionPassManager;
426 }
Devang Patelca58e352006-11-08 10:05:38 +0000427private:
Devang Patel67d6a5e2006-12-19 19:46:59 +0000428 // Active Pass Manager
429 BBPassManager *activeBBPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000430};
431
Devang Patel10c2ca62006-12-12 22:47:13 +0000432//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000433// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000434//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000435/// FunctionPassManagerImpl manages FPPassManagers
436class FunctionPassManagerImpl : public Pass,
437 public PMDataManager,
438 public PMTopLevelManager {
439
440public:
441
442 FunctionPassManagerImpl(int Depth) : PMDataManager(Depth) {
443 activeManager = NULL;
444 }
445
446 /// add - Add a pass to the queue of passes to run. This passes ownership of
447 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
448 /// will be destroyed as well, so there is no need to delete the pass. This
449 /// implies that all passes MUST be allocated with 'new'.
450 void add(Pass *P) {
451 schedulePass(P);
452 }
453
454 /// run - Execute all of the passes scheduled for execution. Keep track of
455 /// whether any of the passes modifies the module, and if so, return true.
456 bool run(Function &F);
457
458 /// doInitialization - Run all of the initializers for the function passes.
459 ///
460 bool doInitialization(Module &M);
461
462 /// doFinalization - Run all of the initializers for the function passes.
463 ///
464 bool doFinalization(Module &M);
465
466 /// Pass Manager itself does not invalidate any analysis info.
467 void getAnalysisUsage(AnalysisUsage &Info) const {
468 Info.setPreservesAll();
469 }
470
471 inline void addTopLevelPass(Pass *P) {
472
473 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
474
475 // P is a immutable pass and it will be managed by this
476 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000477 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000478 P->setResolver(AR);
479 initializeAnalysisImpl(P);
480 addImmutablePass(IP);
481 recordAvailableAnalysis(IP);
482 }
483 else
484 addPass(P);
485 }
486
487 FPPassManager *getContainedManager(unsigned N) {
488 assert ( N < PassManagers.size() && "Pass number out of range!");
489 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
490 return FP;
491 }
492
493 /// Add a pass into a passmanager queue.
494 bool addPass(Pass *p);
495
496private:
497
498 // Active Pass Manager
499 FPPassManager *activeManager;
500};
501
502//===----------------------------------------------------------------------===//
503// MPPassManager
504//
505/// MPPassManager manages ModulePasses and function pass managers.
Devang Patelca58e352006-11-08 10:05:38 +0000506/// It batches all Module passes passes and function pass managers together and
507/// sequence them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000508class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000509
510public:
Devang Patel67d6a5e2006-12-19 19:46:59 +0000511 MPPassManager(int Depth) : PMDataManager(Depth) {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000512 activeFunctionPassManager = NULL;
513 }
Devang Patelca58e352006-11-08 10:05:38 +0000514
515 /// Add a pass into a passmanager queue.
516 bool addPass(Pass *p);
517
518 /// run - Execute all of the passes scheduled for execution. Keep track of
519 /// whether any of the passes modifies the module, and if so, return true.
520 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000521
Devang Patelf9d96b92006-12-07 19:57:52 +0000522 /// Pass Manager itself does not invalidate any analysis info.
523 void getAnalysisUsage(AnalysisUsage &Info) const {
524 Info.setPreservesAll();
525 }
526
Devang Pateleda56172006-12-12 23:34:33 +0000527 // Print passes managed by this manager
528 void dumpPassStructure(unsigned Offset) {
529 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000530 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
531 ModulePass *MP = getContainedPass(Index);
532 MP->dumpPassStructure(Offset + 1);
533 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000534 }
535 }
536
Devang Patelabfbe3b2006-12-16 00:56:26 +0000537 ModulePass *getContainedPass(unsigned N) {
538 assert ( N < PassVector.size() && "Pass number out of range!");
539 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
540 return MP;
541 }
542
Devang Patel3b3f8992007-01-11 01:10:25 +0000543 virtual PassManagerType getPassManagerType() { return PMT_ModulePassManager; }
Devang Patelca58e352006-11-08 10:05:38 +0000544private:
Devang Patelca58e352006-11-08 10:05:38 +0000545 // Active Pass Manager
Devang Patel67d6a5e2006-12-19 19:46:59 +0000546 FPPassManager *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000547};
548
Devang Patel10c2ca62006-12-12 22:47:13 +0000549//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000550// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000551//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000552/// PassManagerImpl manages MPPassManagers
553class PassManagerImpl : public Pass,
Devang Patel31217af2006-12-07 21:32:57 +0000554 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000555 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000556
557public:
558
Devang Patel67d6a5e2006-12-19 19:46:59 +0000559 PassManagerImpl(int Depth) : PMDataManager(Depth) {
Devang Patelad6b7fe2006-12-12 22:57:43 +0000560 activeManager = NULL;
561 }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000562
Devang Patel376fefa2006-11-08 10:29:57 +0000563 /// add - Add a pass to the queue of passes to run. This passes ownership of
564 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
565 /// will be destroyed as well, so there is no need to delete the pass. This
566 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000567 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000568 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000569 }
Devang Patel376fefa2006-11-08 10:29:57 +0000570
571 /// run - Execute all of the passes scheduled for execution. Keep track of
572 /// whether any of the passes modifies the module, and if so, return true.
573 bool run(Module &M);
574
Devang Patelf9d96b92006-12-07 19:57:52 +0000575 /// Pass Manager itself does not invalidate any analysis info.
576 void getAnalysisUsage(AnalysisUsage &Info) const {
577 Info.setPreservesAll();
578 }
579
Devang Patelabcd1d32006-12-07 21:27:23 +0000580 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000581
Devang Patelfa971cd2006-12-08 23:57:43 +0000582 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000583
584 // P is a immutable pass and it will be managed by this
585 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000586 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000587 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000588 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000589 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000590 recordAvailableAnalysis(IP);
Devang Pateld440cd92006-12-08 23:53:00 +0000591 }
Devang Patelfa971cd2006-12-08 23:57:43 +0000592 else
593 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000594 }
595
Devang Patel67d6a5e2006-12-19 19:46:59 +0000596 MPPassManager *getContainedManager(unsigned N) {
597 assert ( N < PassManagers.size() && "Pass number out of range!");
598 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
599 return MP;
600 }
601
Devang Patel376fefa2006-11-08 10:29:57 +0000602private:
603
Devang Patelde124182006-12-07 21:10:57 +0000604 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000605 bool addPass(Pass *p);
606
Devang Patel376fefa2006-11-08 10:29:57 +0000607 // Active Pass Manager
Devang Patel67d6a5e2006-12-19 19:46:59 +0000608 MPPassManager *activeManager;
Devang Patel376fefa2006-11-08 10:29:57 +0000609};
610
Devang Patelffca9102006-12-15 19:39:30 +0000611} // End of llvm namespace
612
613namespace {
614
Devang Patelb8817b92006-12-14 00:59:42 +0000615//===----------------------------------------------------------------------===//
616// TimingInfo Class - This class is used to calculate information about the
617// amount of time each pass takes to execute. This only happens when
618// -time-passes is enabled on the command line.
619//
620
Devang Patelffca9102006-12-15 19:39:30 +0000621class VISIBILITY_HIDDEN TimingInfo {
Devang Patelb8817b92006-12-14 00:59:42 +0000622 std::map<Pass*, Timer> TimingData;
623 TimerGroup TG;
624
625public:
626 // Use 'create' member to get this.
627 TimingInfo() : TG("... Pass execution timing report ...") {}
628
629 // TimingDtor - Print out information about timing information
630 ~TimingInfo() {
631 // Delete all of the timers...
632 TimingData.clear();
633 // TimerGroup is deleted next, printing the report.
634 }
635
636 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
637 // to a non null value (if the -time-passes option is enabled) or it leaves it
638 // null. It may be called multiple times.
639 static void createTheTimeInfo();
640
641 void passStarted(Pass *P) {
642
643 if (dynamic_cast<PMDataManager *>(P))
644 return;
645
646 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
647 if (I == TimingData.end())
648 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
649 I->second.startTimer();
650 }
651 void passEnded(Pass *P) {
652
653 if (dynamic_cast<PMDataManager *>(P))
654 return;
655
656 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
657 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
658 I->second.stopTimer();
659 }
660};
661
662static TimingInfo *TheTimeInfo;
663
Devang Patelffca9102006-12-15 19:39:30 +0000664} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000665
Devang Patela1514cb2006-12-07 19:39:39 +0000666//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000667// PMTopLevelManager implementation
668
669/// Set pass P as the last user of the given analysis passes.
670void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
671 Pass *P) {
672
673 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
674 E = AnalysisPasses.end(); I != E; ++I) {
675 Pass *AP = *I;
676 LastUser[AP] = P;
677 // If AP is the last user of other passes then make P last user of
678 // such passes.
679 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
680 LUE = LastUser.end(); LUI != LUE; ++LUI) {
681 if (LUI->second == AP)
682 LastUser[LUI->first] = P;
683 }
684 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000685}
686
687/// Collect passes whose last user is P
688void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
689 Pass *P) {
690 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
691 LUE = LastUser.end(); LUI != LUE; ++LUI)
692 if (LUI->second == P)
693 LastUses.push_back(LUI->first);
694}
695
696/// Schedule pass P for execution. Make sure that passes required by
697/// P are run before P is run. Update analysis info maintained by
698/// the manager. Remove dead passes. This is a recursive function.
699void PMTopLevelManager::schedulePass(Pass *P) {
700
701 // TODO : Allocate function manager for this pass, other wise required set
702 // may be inserted into previous function manager
703
704 AnalysisUsage AnUsage;
705 P->getAnalysisUsage(AnUsage);
706 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
707 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
708 E = RequiredSet.end(); I != E; ++I) {
709
710 Pass *AnalysisPass = findAnalysisPass(*I);
711 if (!AnalysisPass) {
712 // Schedule this analysis run first.
713 AnalysisPass = (*I)->createPass();
714 schedulePass(AnalysisPass);
715 }
716 }
717
718 // Now all required passes are available.
719 addTopLevelPass(P);
720}
721
722/// Find the pass that implements Analysis AID. Search immutable
723/// passes and all pass managers. If desired pass is not found
724/// then return NULL.
725Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
726
727 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000728 // Check pass managers
729 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
730 E = PassManagers.end(); P == NULL && I != E; ++I) {
731 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
732 assert(PMD && "This is not a PassManager");
733 P = PMD->findAnalysisPass(AID, false);
734 }
735
736 // Check other pass managers
737 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
738 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
739 P = (*I)->findAnalysisPass(AID, false);
740
Devang Patelafb1f3622006-12-12 22:35:25 +0000741 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
742 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
743 const PassInfo *PI = (*I)->getPassInfo();
744 if (PI == AID)
745 P = *I;
746
747 // If Pass not found then check the interfaces implemented by Immutable Pass
748 if (!P) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000749 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
750 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
751 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000752 }
753 }
754
Devang Patelafb1f3622006-12-12 22:35:25 +0000755 return P;
756}
757
Devang Pateleda56172006-12-12 23:34:33 +0000758// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000759void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000760
Devang Patel67d6a5e2006-12-19 19:46:59 +0000761 if (PassDebugging_New < Structure)
762 return;
763
Devang Pateleda56172006-12-12 23:34:33 +0000764 // Print out the immutable passes
765 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
766 ImmutablePasses[i]->dumpPassStructure(0);
767 }
768
Devang Patel991aeba2006-12-15 20:13:01 +0000769 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000770 E = PassManagers.end(); I != E; ++I)
771 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000772}
773
Devang Patel991aeba2006-12-15 20:13:01 +0000774void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000775
776 if (PassDebugging_New < Arguments)
777 return;
778
779 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000780 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000781 E = PassManagers.end(); I != E; ++I) {
782 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
783 assert(PMD && "This is not a PassManager");
784 PMD->dumpPassArguments();
785 }
786 cerr << "\n";
787}
788
Devang Patele3068402006-12-21 00:16:50 +0000789void PMTopLevelManager::initializeAllAnalysisInfo() {
790
791 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
792 E = PassManagers.end(); I != E; ++I) {
793 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
794 assert(PMD && "This is not a PassManager");
795 PMD->initializeAnalysisInfo();
796 }
797
798 // Initailize other pass managers
799 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
800 E = IndirectPassManagers.end(); I != E; ++I)
801 (*I)->initializeAnalysisInfo();
802}
803
Devang Patelafb1f3622006-12-12 22:35:25 +0000804//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000805// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000806
Devang Pateld65e9e92006-11-08 01:31:28 +0000807/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000808/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000809bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000810
Devang Patel8f677ce2006-12-07 18:47:25 +0000811 // TODO
812 // If this pass is not preserving information that is required by a
813 // pass maintained by higher level pass manager then do not insert
814 // this pass into current manager. Use new manager. For example,
815 // For example, If FunctionPass F is not preserving ModulePass Info M1
816 // that is used by another ModulePass M2 then do not insert F in
817 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000818 return true;
819}
820
Devang Patel643676c2006-11-11 01:10:19 +0000821/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000822void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000823
Devang Patel643676c2006-11-11 01:10:19 +0000824 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000825 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000826
Devang Patele9976aa2006-12-07 19:33:53 +0000827 //This pass is the current implementation of all of the interfaces it
828 //implements as well.
829 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
830 for (unsigned i = 0, e = II.size(); i != e; ++i)
831 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000832 }
833}
834
Devang Patelf68a3492006-11-07 22:35:17 +0000835/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000836void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000837 AnalysisUsage AnUsage;
838 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000839
Devang Patel2e169c32006-12-07 20:03:49 +0000840 if (AnUsage.getPreservesAll())
841 return;
842
843 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000844 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000845 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000846 std::map<AnalysisID, Pass*>::iterator Info = I++;
847 if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000848 PreservedSet.end()) {
849 // Remove this analysis
Devang Patel56d48ec2006-12-15 22:57:49 +0000850 if (!dynamic_cast<ImmutablePass*>(Info->second))
851 AvailableAnalysis.erase(Info);
852 }
Devang Patel349170f2006-11-11 01:24:55 +0000853 }
Devang Patelf68a3492006-11-07 22:35:17 +0000854}
855
Devang Patelca189262006-11-14 03:05:08 +0000856/// Remove analysis passes that are not used any longer
Devang Patel200d3052006-12-13 23:50:44 +0000857void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
Devang Patel17ad0962006-12-08 00:37:52 +0000858
859 std::vector<Pass *> DeadPasses;
860 TPM->collectLastUses(DeadPasses, P);
861
862 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
863 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000864
865 std::string Msg1 = " Freeing Pass '";
866 dumpPassInfo(*I, Msg1, Msg);
867
Devang Patelb8817b92006-12-14 00:59:42 +0000868 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Devang Patel17ad0962006-12-08 00:37:52 +0000869 (*I)->releaseMemory();
Devang Patelb8817b92006-12-14 00:59:42 +0000870 if (TheTimeInfo) TheTimeInfo->passEnded(P);
871
Devang Patel17ad0962006-12-08 00:37:52 +0000872 std::map<AnalysisID, Pass*>::iterator Pos =
873 AvailableAnalysis.find((*I)->getPassInfo());
874
Devang Patel475c4532006-12-08 00:59:05 +0000875 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000876 if (Pos != AvailableAnalysis.end())
877 AvailableAnalysis.erase(Pos);
878 }
Devang Patelca189262006-11-14 03:05:08 +0000879}
880
Devang Patel8f677ce2006-12-07 18:47:25 +0000881/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000882/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000883void PMDataManager::addPassToManager(Pass *P,
884 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000885
Devang Pateld440cd92006-12-08 23:53:00 +0000886 // This manager is going to manage pass P. Set up analysis resolver
887 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000888 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000889 P->setResolver(AR);
890
Devang Patel90b05e02006-11-11 02:04:19 +0000891 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000892
893 // At the moment, this pass is the last user of all required passes.
894 std::vector<Pass *> LastUses;
895 std::vector<Pass *> RequiredPasses;
896 unsigned PDepth = this->getDepth();
897
898 collectRequiredAnalysisPasses(RequiredPasses, P);
899 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
900 E = RequiredPasses.end(); I != E; ++I) {
901 Pass *PRequired = *I;
902 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000903
904 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
905 RDepth = DM.getDepth();
906
Devang Patelbc03f132006-12-07 23:55:10 +0000907 if (PDepth == RDepth)
908 LastUses.push_back(PRequired);
909 else if (PDepth > RDepth) {
910 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000911 TransferLastUses.push_back(PRequired);
Devang Patelbc03f132006-12-07 23:55:10 +0000912 } else {
913 // Note : This feature is not yet implemented
914 assert (0 &&
915 "Unable to handle Pass that requires lower level Analysis pass");
916 }
917 }
918
Devang Patel832bc072006-12-15 00:08:26 +0000919 LastUses.push_back(P);
920 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000921
Devang Patel17bff0d2006-12-07 22:09:36 +0000922 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000923 // Remove the analysis not preserved by this pass
924 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000925 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000926 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000927
928 // Add pass
929 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000930}
931
Devang Patel1d6267c2006-12-07 23:05:44 +0000932/// Populate RequiredPasses with the analysis pass that are required by
933/// pass P.
934void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
935 Pass *P) {
936 AnalysisUsage AnUsage;
937 P->getAnalysisUsage(AnUsage);
938 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
939 for (std::vector<AnalysisID>::const_iterator
940 I = RequiredSet.begin(), E = RequiredSet.end();
941 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000942 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000943 assert (AnalysisPass && "Analysis pass is not available");
944 RP.push_back(AnalysisPass);
945 }
Devang Patelf58183d2006-12-12 23:09:32 +0000946
947 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
948 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
949 E = IDs.end(); I != E; ++I) {
950 Pass *AnalysisPass = findAnalysisPass(*I, true);
951 assert (AnalysisPass && "Analysis pass is not available");
952 RP.push_back(AnalysisPass);
953 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000954}
955
Devang Patel07f4f582006-11-14 21:49:36 +0000956// All Required analyses should be available to the pass as it runs! Here
957// we fill in the AnalysisImpls member of the pass so that it can
958// successfully use the getAnalysis() method to retrieve the
959// implementations it needs.
960//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000961void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000962 AnalysisUsage AnUsage;
963 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000964
965 for (std::vector<const PassInfo *>::const_iterator
966 I = AnUsage.getRequiredSet().begin(),
967 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000968 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000969 if (Impl == 0)
970 assert(0 && "Analysis used but not available!");
Devang Patelb66334b2007-01-05 22:47:07 +0000971 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000972 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000973 }
974}
975
Devang Patel640c5bb2006-12-08 22:30:11 +0000976/// Find the pass that implements Analysis AID. If desired pass is not found
977/// then return NULL.
978Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
979
980 // Check if AvailableAnalysis map has one entry.
981 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
982
983 if (I != AvailableAnalysis.end())
984 return I->second;
985
986 // Search Parents through TopLevelManager
987 if (SearchParent)
988 return TPM->findAnalysisPass(AID);
989
Devang Patel9d759b82006-12-09 00:09:12 +0000990 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000991}
992
Devang Patel991aeba2006-12-15 20:13:01 +0000993// Print list of passes that are last used by P.
994void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
995
996 std::vector<Pass *> LUses;
997
998 assert (TPM && "Top Level Manager is missing");
999 TPM->collectLastUses(LUses, P);
1000
1001 for (std::vector<Pass *>::iterator I = LUses.begin(),
1002 E = LUses.end(); I != E; ++I) {
1003 llvm::cerr << "--" << std::string(Offset*2, ' ');
1004 (*I)->dumpPassStructure(0);
1005 }
1006}
1007
1008void PMDataManager::dumpPassArguments() const {
1009 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
1010 E = PassVector.end(); I != E; ++I) {
1011 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
1012 PMD->dumpPassArguments();
1013 else
1014 if (const PassInfo *PI = (*I)->getPassInfo())
1015 if (!PI->isAnalysisGroup())
1016 cerr << " -" << PI->getPassArgument();
1017 }
1018}
1019
1020void PMDataManager:: dumpPassInfo(Pass *P, std::string &Msg1,
1021 std::string &Msg2) const {
1022 if (PassDebugging_New < Executions)
1023 return;
1024 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
1025 cerr << Msg1;
1026 cerr << P->getPassName();
1027 cerr << Msg2;
1028}
1029
1030void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
1031 const std::vector<AnalysisID> &Set)
1032 const {
1033 if (PassDebugging_New >= Details && !Set.empty()) {
1034 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
1035 for (unsigned i = 0; i != Set.size(); ++i) {
1036 if (i) cerr << ",";
1037 cerr << " " << Set[i]->getPassName();
1038 }
1039 cerr << "\n";
1040 }
1041}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001042
1043//===----------------------------------------------------------------------===//
1044// NOTE: Is this the right place to define this method ?
1045// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +00001046Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001047 return PM.findAnalysisPass(ID, dir);
1048}
1049
Devang Patela1514cb2006-12-07 19:39:39 +00001050//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001051// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001052
Devang Pateld65e9e92006-11-08 01:31:28 +00001053/// Add pass P into PassVector and return true. If this pass is not
1054/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +00001055bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001056BBPassManager::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +00001057
1058 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
1059 if (!BP)
1060 return false;
1061
Devang Patel56d48ec2006-12-15 22:57:49 +00001062 // If this pass does not preserve analysis that is used by other passes
1063 // managed by this manager than it is not a suitable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001064 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001065 return false;
1066
Chris Lattnerce22ca32006-12-14 18:22:14 +00001067 addPassToManager(BP);
Devang Patel349170f2006-11-11 01:24:55 +00001068
Devang Patel6e5a1132006-11-07 21:31:57 +00001069 return true;
1070}
1071
1072/// Execute all of the passes scheduled for execution by invoking
1073/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1074/// the function, and if so, return true.
1075bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001076BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +00001077
Devang Patel745a6962006-12-12 23:15:28 +00001078 if (F.isExternal())
1079 return false;
1080
Devang Patele9585592006-12-08 01:38:28 +00001081 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001082
Devang Patel93a197c2006-12-14 00:08:04 +00001083 std::string Msg1 = "Executing Pass '";
1084 std::string Msg3 = "' Made Modification '";
1085
Devang Patel6e5a1132006-11-07 21:31:57 +00001086 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001087 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1088 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001089 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001090 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +00001091
Devang Patel200d3052006-12-13 23:50:44 +00001092 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001093 dumpPassInfo(BP, Msg1, Msg2);
1094 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001095
Devang Patelabfbe3b2006-12-16 00:56:26 +00001096 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001097
Devang Patelabfbe3b2006-12-16 00:56:26 +00001098 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +00001099 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001100 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001101
1102 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001103 dumpPassInfo(BP, Msg3, Msg2);
1104 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001105
Devang Patelabfbe3b2006-12-16 00:56:26 +00001106 removeNotPreservedAnalysis(BP);
1107 recordAvailableAnalysis(BP);
1108 removeDeadPasses(BP, Msg2);
Devang Patel6e5a1132006-11-07 21:31:57 +00001109 }
Devang Patel56d48ec2006-12-15 22:57:49 +00001110 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001111}
1112
Devang Patel475c4532006-12-08 00:59:05 +00001113// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +00001114inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001115 bool Changed = false;
1116
Devang Patelabfbe3b2006-12-16 00:56:26 +00001117 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1118 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001119 Changed |= BP->doInitialization(M);
1120 }
1121
1122 return Changed;
1123}
1124
Devang Patel67d6a5e2006-12-19 19:46:59 +00001125inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001126 bool Changed = false;
1127
Devang Patelabfbe3b2006-12-16 00:56:26 +00001128 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1129 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001130 Changed |= BP->doFinalization(M);
1131 }
1132
1133 return Changed;
1134}
1135
Devang Patel67d6a5e2006-12-19 19:46:59 +00001136inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001137 bool Changed = false;
1138
Devang Patelabfbe3b2006-12-16 00:56:26 +00001139 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1140 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001141 Changed |= BP->doInitialization(F);
1142 }
1143
1144 return Changed;
1145}
1146
Devang Patel67d6a5e2006-12-19 19:46:59 +00001147inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001148 bool Changed = false;
1149
Devang Patelabfbe3b2006-12-16 00:56:26 +00001150 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1151 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001152 Changed |= BP->doFinalization(F);
1153 }
1154
1155 return Changed;
1156}
1157
1158
Devang Patela1514cb2006-12-07 19:39:39 +00001159//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001160// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001161
Devang Patel4e12f862006-11-08 10:44:40 +00001162/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001163FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001164 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001165 // FPM is the top level manager.
1166 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001167
1168 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelb66334b2007-01-05 22:47:07 +00001169 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patel1036b652006-12-12 23:27:37 +00001170 FPM->setResolver(AR);
1171
Devang Patel1f653682006-12-08 18:57:16 +00001172 MP = P;
1173}
1174
Devang Patelb67904d2006-12-13 02:36:01 +00001175FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001176 delete FPM;
1177}
1178
Devang Patel4e12f862006-11-08 10:44:40 +00001179/// add - Add a pass to the queue of passes to run. This passes
1180/// ownership of the Pass to the PassManager. When the
1181/// PassManager_X is destroyed, the pass will be destroyed as well, so
1182/// there is no need to delete the pass. (TODO delete passes.)
1183/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001184void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001185 FPM->add(P);
1186}
1187
Devang Patel9f3083e2006-11-15 19:39:54 +00001188/// run - Execute all of the passes scheduled for execution. Keep
1189/// track of whether any of the passes modifies the function, and if
1190/// so, return true.
1191///
Devang Patelb67904d2006-12-13 02:36:01 +00001192bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001193 std::string errstr;
1194 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001195 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001196 abort();
1197 }
Devang Patel272908d2006-12-08 22:57:48 +00001198 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001199}
1200
1201
Devang Patelff631ae2006-11-15 01:27:05 +00001202/// doInitialization - Run all of the initializers for the function passes.
1203///
Devang Patelb67904d2006-12-13 02:36:01 +00001204bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001205 return FPM->doInitialization(*MP->getModule());
1206}
1207
1208/// doFinalization - Run all of the initializers for the function passes.
1209///
Devang Patelb67904d2006-12-13 02:36:01 +00001210bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001211 return FPM->doFinalization(*MP->getModule());
1212}
1213
Devang Patela1514cb2006-12-07 19:39:39 +00001214//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001215// FunctionPassManagerImpl implementation
1216//
1217/// Add P into active pass manager or use new module pass manager to
1218/// manage it.
1219bool FunctionPassManagerImpl::addPass(Pass *P) {
1220
1221 if (!activeManager || !activeManager->addPass(P)) {
1222 activeManager = new FPPassManager(getDepth() + 1);
1223 // Inherit top level manager
1224 activeManager->setTopLevelManager(this->getTopLevelManager());
1225
1226 // This top level manager is going to manage activeManager.
1227 // Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +00001228 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001229 activeManager->setResolver(AR);
1230
1231 addPassManager(activeManager);
1232 return activeManager->addPass(P);
1233 }
1234 return true;
1235}
1236
1237inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1238 bool Changed = false;
1239
1240 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1241 FPPassManager *FP = getContainedManager(Index);
1242 Changed |= FP->doInitialization(M);
1243 }
1244
1245 return Changed;
1246}
1247
1248inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1249 bool Changed = false;
1250
1251 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1252 FPPassManager *FP = getContainedManager(Index);
1253 Changed |= FP->doFinalization(M);
1254 }
1255
1256 return Changed;
1257}
1258
1259// Execute all the passes managed by this top level manager.
1260// Return true if any function is modified by a pass.
1261bool FunctionPassManagerImpl::run(Function &F) {
1262
1263 bool Changed = false;
1264
1265 TimingInfo::createTheTimeInfo();
1266
1267 dumpArguments();
1268 dumpPasses();
1269
Devang Patele3068402006-12-21 00:16:50 +00001270 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001271 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1272 FPPassManager *FP = getContainedManager(Index);
1273 Changed |= FP->runOnFunction(F);
1274 }
1275 return Changed;
1276}
1277
1278//===----------------------------------------------------------------------===//
1279// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001280
Devang Patel0c2012f2006-11-07 21:49:50 +00001281/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
1282/// either use it into active basic block pass manager or create new basic
1283/// block pass manager to handle pass P.
1284bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001285FPPassManager::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +00001286
Devang Patel67d6a5e2006-12-19 19:46:59 +00001287 // If P is a BasicBlockPass then use BBPassManager.
Devang Patel0c2012f2006-11-07 21:49:50 +00001288 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
1289
Devang Patel4949fe02006-12-07 22:34:21 +00001290 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +00001291
Devang Patel4949fe02006-12-07 22:34:21 +00001292 // If active manager exists then clear its analysis info.
1293 if (activeBBPassManager)
1294 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001295
Devang Patel4949fe02006-12-07 22:34:21 +00001296 // Create and add new manager
Devang Patel67d6a5e2006-12-19 19:46:59 +00001297 activeBBPassManager = new BBPassManager(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001298 // Inherit top level manager
1299 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001300
1301 // Add new manager into current manager's list.
Devang Patel90b05e02006-11-11 02:04:19 +00001302 addPassToManager(activeBBPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001303
1304 // Add new manager into top level manager's indirect passes list
1305 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
1306 assert (PMD && "Manager is not Pass Manager");
1307 TPM->addIndirectPassManager(PMD);
Devang Patel4949fe02006-12-07 22:34:21 +00001308
1309 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001310 if (!activeBBPassManager->addPass(BP))
1311 assert(0 && "Unable to add Pass");
Devang Patel832bc072006-12-15 00:08:26 +00001312
1313 // If activeBBPassManager transfered any Last Uses then handle them here.
1314 std::vector<Pass *> &TLU = activeBBPassManager->getTransferredLastUses();
1315 if (!TLU.empty())
1316 TPM->setLastUser(TLU, this);
1317
Devang Patel0c2012f2006-11-07 21:49:50 +00001318 }
Devang Patelbc03f132006-12-07 23:55:10 +00001319
Devang Patel0c2012f2006-11-07 21:49:50 +00001320 return true;
1321 }
1322
1323 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
1324 if (!FP)
1325 return false;
1326
Devang Patel56d48ec2006-12-15 22:57:49 +00001327 // If this pass does not preserve analysis that is used by other passes
1328 // managed by this manager than it is not a suitable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001329 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001330 return false;
1331
Devang Patel8cad70d2006-11-11 01:51:02 +00001332 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +00001333
1334 // If active manager exists then clear its analysis info.
1335 if (activeBBPassManager) {
1336 activeBBPassManager->initializeAnalysisInfo();
1337 activeBBPassManager = NULL;
1338 }
1339
Devang Patel0c2012f2006-11-07 21:49:50 +00001340 return true;
1341}
1342
1343/// Execute all of the passes scheduled for execution by invoking
1344/// runOnFunction method. Keep track of whether any of the passes modifies
1345/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001346bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001347
1348 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001349
1350 if (F.isExternal())
1351 return false;
1352
Devang Patel93a197c2006-12-14 00:08:04 +00001353 std::string Msg1 = "Executing Pass '";
1354 std::string Msg3 = "' Made Modification '";
1355
Devang Patelabfbe3b2006-12-16 00:56:26 +00001356 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1357 FunctionPass *FP = getContainedPass(Index);
1358
Devang Patelf6d1d212006-12-14 00:25:06 +00001359 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001360 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001361
Devang Patel200d3052006-12-13 23:50:44 +00001362 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001363 dumpPassInfo(FP, Msg1, Msg2);
1364 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001365
Devang Patelabfbe3b2006-12-16 00:56:26 +00001366 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001367
Devang Patelabfbe3b2006-12-16 00:56:26 +00001368 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001369 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001370 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001371
1372 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001373 dumpPassInfo(FP, Msg3, Msg2);
1374 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001375
Devang Patelabfbe3b2006-12-16 00:56:26 +00001376 removeNotPreservedAnalysis(FP);
1377 recordAvailableAnalysis(FP);
1378 removeDeadPasses(FP, Msg2);
Devang Patel9f3083e2006-11-15 19:39:54 +00001379 }
1380 return Changed;
1381}
1382
Devang Patel67d6a5e2006-12-19 19:46:59 +00001383bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001384
Devang Patel67d6a5e2006-12-19 19:46:59 +00001385 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001386
1387 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1388 this->runOnFunction(*I);
1389
1390 return Changed |= doFinalization(M);
1391}
1392
1393inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001394 bool Changed = false;
1395
Devang Patelabfbe3b2006-12-16 00:56:26 +00001396 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1397 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001398 Changed |= FP->doInitialization(M);
1399 }
1400
1401 return Changed;
1402}
1403
Devang Patel67d6a5e2006-12-19 19:46:59 +00001404inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001405 bool Changed = false;
1406
Devang Patelabfbe3b2006-12-16 00:56:26 +00001407 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1408 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001409 Changed |= FP->doFinalization(M);
1410 }
1411
Devang Patelff631ae2006-11-15 01:27:05 +00001412 return Changed;
1413}
1414
Devang Patela1514cb2006-12-07 19:39:39 +00001415//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001416// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001417
1418/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel67d6a5e2006-12-19 19:46:59 +00001419/// then use FPPassManager to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +00001420/// is not manageable by this manager.
1421bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001422MPPassManager::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +00001423
1424 // If P is FunctionPass then use function pass maanager.
1425 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1426
Devang Patel640c5bb2006-12-08 22:30:11 +00001427 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +00001428
Devang Patel4949fe02006-12-07 22:34:21 +00001429 // If active manager exists then clear its analysis info.
1430 if (activeFunctionPassManager)
1431 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001432
Devang Patel4949fe02006-12-07 22:34:21 +00001433 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001434 activeFunctionPassManager =
Devang Patel67d6a5e2006-12-19 19:46:59 +00001435 new FPPassManager(getDepth() + 1);
Devang Patelafb1f3622006-12-12 22:35:25 +00001436
1437 // Add new manager into current manager's list
Devang Patel90b05e02006-11-11 02:04:19 +00001438 addPassToManager(activeFunctionPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001439
Devang Patel9c6290c2006-12-12 22:02:16 +00001440 // Inherit top level manager
1441 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001442
1443 // Add new manager into top level manager's indirect passes list
Chris Lattnerf0f611a2006-12-13 21:56:10 +00001444 PMDataManager *PMD =
1445 dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1446 assert(PMD && "Manager is not Pass Manager");
Devang Patelafb1f3622006-12-12 22:35:25 +00001447 TPM->addIndirectPassManager(PMD);
Devang Patelaf1fca52006-12-08 23:11:43 +00001448
Devang Patel4949fe02006-12-07 22:34:21 +00001449 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001450 if (!activeFunctionPassManager->addPass(FP))
1451 assert(0 && "Unable to add pass");
Devang Patelbc03f132006-12-07 23:55:10 +00001452
Devang Patel832bc072006-12-15 00:08:26 +00001453 // If activeFunctionPassManager transfered any Last Uses then
1454 // handle them here.
1455 std::vector<Pass *> &TLU =
1456 activeFunctionPassManager->getTransferredLastUses();
1457 if (!TLU.empty())
1458 TPM->setLastUser(TLU, this);
1459 }
Devang Patelbc03f132006-12-07 23:55:10 +00001460
Devang Patel05e1a972006-11-07 22:03:15 +00001461 return true;
1462 }
1463
1464 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1465 if (!MP)
1466 return false;
1467
Devang Patel56d48ec2006-12-15 22:57:49 +00001468 // If this pass does not preserve analysis that is used by other passes
1469 // managed by this manager than it is not a suitable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001470 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001471 return false;
1472
Devang Patel8cad70d2006-11-11 01:51:02 +00001473 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +00001474 // If active manager exists then clear its analysis info.
1475 if (activeFunctionPassManager) {
1476 activeFunctionPassManager->initializeAnalysisInfo();
1477 activeFunctionPassManager = NULL;
1478 }
1479
Devang Patel05e1a972006-11-07 22:03:15 +00001480 return true;
1481}
1482
1483
1484/// Execute all of the passes scheduled for execution by invoking
1485/// runOnModule method. Keep track of whether any of the passes modifies
1486/// the module, and if so, return true.
1487bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001488MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001489 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001490
Devang Patel93a197c2006-12-14 00:08:04 +00001491 std::string Msg1 = "Executing Pass '";
1492 std::string Msg3 = "' Made Modification '";
1493
Devang Patelabfbe3b2006-12-16 00:56:26 +00001494 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1495 ModulePass *MP = getContainedPass(Index);
1496
Devang Patelf6d1d212006-12-14 00:25:06 +00001497 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001498 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001499
Devang Patel200d3052006-12-13 23:50:44 +00001500 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001501 dumpPassInfo(MP, Msg1, Msg2);
1502 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001503
Devang Patelabfbe3b2006-12-16 00:56:26 +00001504 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001505
Devang Patelabfbe3b2006-12-16 00:56:26 +00001506 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001507 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001508 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001509
1510 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001511 dumpPassInfo(MP, Msg3, Msg2);
1512 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001513
Devang Patelabfbe3b2006-12-16 00:56:26 +00001514 removeNotPreservedAnalysis(MP);
1515 recordAvailableAnalysis(MP);
1516 removeDeadPasses(MP, Msg2);
Devang Patel05e1a972006-11-07 22:03:15 +00001517 }
1518 return Changed;
1519}
1520
Devang Patela1514cb2006-12-07 19:39:39 +00001521//===----------------------------------------------------------------------===//
1522// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001523//
Devang Patelc290c8a2006-11-07 22:23:34 +00001524/// Add P into active pass manager or use new module pass manager to
1525/// manage it.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001526bool PassManagerImpl::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001527
Devang Patel6c9f5482006-11-11 00:42:16 +00001528 if (!activeManager || !activeManager->addPass(P)) {
Devang Patel1c56a632007-01-08 19:29:38 +00001529
Devang Patel67d6a5e2006-12-19 19:46:59 +00001530 activeManager = new MPPassManager(getDepth() + 1);
Devang Patel1c56a632007-01-08 19:29:38 +00001531
Devang Patel9c6290c2006-12-12 22:02:16 +00001532 // Inherit top level manager
1533 activeManager->setTopLevelManager(this->getTopLevelManager());
Devang Pateld440cd92006-12-08 23:53:00 +00001534
1535 // This top level manager is going to manage activeManager.
1536 // Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +00001537 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +00001538 activeManager->setResolver(AR);
1539
Devang Patel5bbeb492006-12-08 22:47:25 +00001540 addPassManager(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001541 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001542 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001543 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001544}
1545
1546/// run - Execute all of the passes scheduled for execution. Keep track of
1547/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001548bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001549
Devang Patelc290c8a2006-11-07 22:23:34 +00001550 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001551
Devang Patelb8817b92006-12-14 00:59:42 +00001552 TimingInfo::createTheTimeInfo();
1553
Devang Patelcfd70c42006-12-13 22:10:00 +00001554 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001555 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001556
Devang Patele3068402006-12-21 00:16:50 +00001557 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001558 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1559 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001560 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001561 }
1562 return Changed;
1563}
Devang Patel376fefa2006-11-08 10:29:57 +00001564
Devang Patela1514cb2006-12-07 19:39:39 +00001565//===----------------------------------------------------------------------===//
1566// PassManager implementation
1567
Devang Patel376fefa2006-11-08 10:29:57 +00001568/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001569PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001570 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001571 // PM is the top level manager
1572 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001573}
1574
Devang Patelb67904d2006-12-13 02:36:01 +00001575PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001576 delete PM;
1577}
1578
Devang Patel376fefa2006-11-08 10:29:57 +00001579/// add - Add a pass to the queue of passes to run. This passes ownership of
1580/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1581/// will be destroyed as well, so there is no need to delete the pass. This
1582/// implies that all passes MUST be allocated with 'new'.
1583void
Devang Patelb67904d2006-12-13 02:36:01 +00001584PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001585 PM->add(P);
1586}
1587
1588/// run - Execute all of the passes scheduled for execution. Keep track of
1589/// whether any of the passes modifies the module, and if so, return true.
1590bool
Devang Patelb67904d2006-12-13 02:36:01 +00001591PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001592 return PM->run(M);
1593}
1594
Devang Patelb8817b92006-12-14 00:59:42 +00001595//===----------------------------------------------------------------------===//
1596// TimingInfo Class - This class is used to calculate information about the
1597// amount of time each pass takes to execute. This only happens with
1598// -time-passes is enabled on the command line.
1599//
1600bool llvm::TimePassesIsEnabled = false;
1601static cl::opt<bool,true>
1602EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1603 cl::desc("Time each pass, printing elapsed time for each on exit"));
1604
1605// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1606// a non null value (if the -time-passes option is enabled) or it leaves it
1607// null. It may be called multiple times.
1608void TimingInfo::createTheTimeInfo() {
1609 if (!TimePassesIsEnabled || TheTimeInfo) return;
1610
1611 // Constructed the first time this is called, iff -time-passes is enabled.
1612 // This guarantees that the object will be constructed before static globals,
1613 // thus it will be destroyed before them.
1614 static ManagedStatic<TimingInfo> TTI;
1615 TheTimeInfo = &*TTI;
1616}
1617
Devang Patel1c56a632007-01-08 19:29:38 +00001618//===----------------------------------------------------------------------===//
1619// PMStack implementation
1620//
1621// Pop Pass Manager from the stack and clear its analysis info.
1622void PMStack::pop() {
1623
1624 PMDataManager *Top = this->top();
1625 Top->initializeAnalysisInfo();
1626
1627 S.pop_back();
1628}
1629
1630// Push PM on the stack and set its top level manager.
Devang Patel15701b52007-01-11 00:19:00 +00001631void PMStack::push(Pass *P) {
Devang Patel1c56a632007-01-08 19:29:38 +00001632
Devang Patel15701b52007-01-11 00:19:00 +00001633 PMDataManager *Top = NULL;
1634 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1635 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001636
Devang Patel15701b52007-01-11 00:19:00 +00001637 if (this->empty()) {
1638 Top = PM;
1639 }
1640 else {
1641 Top = this->top();
1642 PMTopLevelManager *TPM = Top->getTopLevelManager();
1643
1644 assert (TPM && "Unable to find top level manager");
1645 TPM->addIndirectPassManager(PM);
1646 PM->setTopLevelManager(TPM);
1647 }
1648
1649 AnalysisResolver *AR = new AnalysisResolver(*Top);
1650 P->setResolver(AR);
1651
1652 S.push_back(PM);
1653}
1654
1655// Dump content of the pass manager stack.
1656void PMStack::dump() {
1657 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1658 E = S.end(); I != E; ++I) {
1659 Pass *P = dynamic_cast<Pass *>(*I);
1660 printf ("%s ", P->getPassName());
1661 }
1662 if (!S.empty())
1663 printf ("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001664}
1665
1666// Walk Pass Manager stack and set LastUse markers if any
1667// manager is transfering this priviledge to its parent manager
1668void PMStack::handleLastUserOverflow() {
1669
1670 for(PMStack::iterator I = this->begin(), E = this->end(); I != E;) {
1671
1672 PMDataManager *Child = *I++;
1673 if (I != E) {
1674 PMDataManager *Parent = *I++;
1675 PMTopLevelManager *TPM = Parent->getTopLevelManager();
1676 std::vector<Pass *> &TLU = Child->getTransferredLastUses();
1677 if (!TLU.empty()) {
1678 Pass *P = dynamic_cast<Pass *>(Parent);
1679 TPM->setLastUser(TLU, P);
1680 }
1681 }
1682 }
1683}
1684
1685/// Find appropriate Module Pass Manager in the PM Stack and
1686/// add self into that manager.
1687void ModulePass::assignPassManager(PMStack &PMS) {
1688
Devang Patel1c56a632007-01-08 19:29:38 +00001689 // Find Module Pass Manager
1690 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001691 if (PMS.top()->getPassManagerType() > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001692 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001693 else
1694 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001695 }
Devang Patelac99eca2007-01-11 19:59:06 +00001696 MPPassManager *MPP = dynamic_cast<MPPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001697
1698 assert(MPP && "Unable to find Module Pass Manager");
Devang Patel1c56a632007-01-08 19:29:38 +00001699 MPP->addPassToManager(this);
1700}
1701
1702/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1703/// in the PM Stack and add self into that manager.
1704void FunctionPass::assignPassManager(PMStack &PMS) {
1705
Devang Patel15701b52007-01-11 00:19:00 +00001706 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001707 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001708 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1709 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001710 else
Devang Patelac99eca2007-01-11 19:59:06 +00001711 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001712 }
Devang Patelac99eca2007-01-11 19:59:06 +00001713 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001714
Devang Patel15701b52007-01-11 00:19:00 +00001715 // Create new Function Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001716 if (!FPP) {
Devang Patel1c56a632007-01-08 19:29:38 +00001717 assert(!PMS.empty() && "Unable to create Function Pass Manager");
Devang Patel1c56a632007-01-08 19:29:38 +00001718 PMDataManager *PMD = PMS.top();
Devang Patel1c56a632007-01-08 19:29:38 +00001719
Devang Patel15701b52007-01-11 00:19:00 +00001720 // [1] Create new Function Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001721 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patel15701b52007-01-11 00:19:00 +00001722
1723 // [2] Set up new manager's top level manager
1724 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1725 TPM->addIndirectPassManager(FPP);
1726
1727 // [3] Assign manager to manage this new manager. This may create
1728 // and push new managers into PMS
1729 Pass *P = dynamic_cast<Pass *>(FPP);
1730 P->assignPassManager(PMS);
1731
1732 // [4] Push new manager into PMS
Devang Patel1c56a632007-01-08 19:29:38 +00001733 PMS.push(FPP);
1734 }
1735
Devang Patel15701b52007-01-11 00:19:00 +00001736 // Assign FPP as the manager of this pass.
Devang Patel1c56a632007-01-08 19:29:38 +00001737 FPP->addPassToManager(this);
1738}
1739
1740/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
1741/// in the PM Stack and add self into that manager.
1742void BasicBlockPass::assignPassManager(PMStack &PMS) {
1743
1744 BBPassManager *BBP = NULL;
1745
Devang Patel15701b52007-01-11 00:19:00 +00001746 // Basic Pass Manager is a leaf pass manager. It does not handle
1747 // any other pass manager.
1748 if (!PMS.empty()) {
Devang Patel1c56a632007-01-08 19:29:38 +00001749 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001750 }
1751
Devang Patel15701b52007-01-11 00:19:00 +00001752 // If leaf manager is not Basic Block Pass manager then create new
1753 // basic Block Pass manager.
1754
Devang Patel1c56a632007-01-08 19:29:38 +00001755 if (!BBP) {
Devang Patel1c56a632007-01-08 19:29:38 +00001756 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
Devang Patel1c56a632007-01-08 19:29:38 +00001757 PMDataManager *PMD = PMS.top();
Devang Patel1c56a632007-01-08 19:29:38 +00001758
Devang Patel15701b52007-01-11 00:19:00 +00001759 // [1] Create new Basic Block Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001760 BBP = new BBPassManager(PMD->getDepth() + 1);
Devang Patel15701b52007-01-11 00:19:00 +00001761
1762 // [2] Set up new manager's top level manager
1763 // Basic Block Pass Manager does not live by itself
1764 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1765 TPM->addIndirectPassManager(BBP);
1766
1767 // [3] Assign manager to manage this new manager. This may create
1768 // and push new managers into PMS
1769 Pass *P = dynamic_cast<Pass *>(BBP);
1770 P->assignPassManager(PMS);
1771
1772 // [4] Push new manager into PMS
Devang Patel1c56a632007-01-08 19:29:38 +00001773 PMS.push(BBP);
1774 }
1775
Devang Patel15701b52007-01-11 00:19:00 +00001776 // Assign BBP as the manager of this pass.
Devang Patel1c56a632007-01-08 19:29:38 +00001777 BBP->addPassToManager(this);
1778}
1779
Devang Patelc6b5a552007-01-05 20:16:23 +00001780