blob: a6c3ef9a7d096e02f75e8746b572627d67c04565 [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 Patel6e5a1132006-11-07 21:31:57 +000024using namespace llvm;
25
Devang Patel6fea2852006-12-07 18:23:30 +000026//===----------------------------------------------------------------------===//
27// Overview:
28// The Pass Manager Infrastructure manages passes. It's responsibilities are:
29//
30// o Manage optimization pass execution order
31// o Make required Analysis information available before pass P is run
32// o Release memory occupied by dead passes
33// o If Analysis information is dirtied by a pass then regenerate Analysis
34// information before it is consumed by another pass.
35//
Chris Lattnerce22ca32006-12-14 18:22:14 +000036// Pass Manager Infrastructure uses multiple pass managers. They are
37// PassManager, FunctionPassManager, ModulePassManager, BasicBlockPassManager.
38// This class hierarcy uses multiple inheritance but pass managers do not derive
39// from another pass manager.
Devang Patel6fea2852006-12-07 18:23:30 +000040//
Chris Lattnerce22ca32006-12-14 18:22:14 +000041// PassManager and FunctionPassManager are two top-level pass manager that
Devang Patel6fea2852006-12-07 18:23:30 +000042// represents the external interface of this entire pass manager infrastucture.
43//
44// Important classes :
45//
46// [o] class PMTopLevelManager;
47//
48// Two top level managers, PassManager and FunctionPassManager, derive from
49// PMTopLevelManager. PMTopLevelManager manages information used by top level
50// managers such as last user info.
51//
52// [o] class PMDataManager;
53//
54// PMDataManager manages information, e.g. list of available analysis info,
55// used by a pass manager to manage execution order of passes. It also provides
56// a place to implement common pass manager APIs. All pass managers derive from
57// PMDataManager.
58//
59// [o] class BasicBlockPassManager : public FunctionPass, public PMDataManager;
60//
61// BasicBlockPassManager manages BasicBlockPasses.
62//
63// [o] class FunctionPassManager;
64//
65// This is a external interface used by JIT to manage FunctionPasses. This
66// interface relies on FunctionPassManagerImpl to do all the tasks.
67//
68// [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
69// public PMTopLevelManager;
70//
71// FunctionPassManagerImpl is a top level manager. It manages FunctionPasses
72// and BasicBlockPassManagers.
73//
74// [o] class ModulePassManager : public Pass, public PMDataManager;
75//
76// ModulePassManager manages ModulePasses and FunctionPassManagerImpls.
77//
78// [o] class PassManager;
79//
80// This is a external interface used by various tools to manages passes. It
81// relies on PassManagerImpl to do all the tasks.
82//
83// [o] class PassManagerImpl : public Pass, public PMDataManager,
84// public PMDTopLevelManager
85//
86// PassManagerImpl is a top level pass manager responsible for managing
87// ModulePassManagers.
88//===----------------------------------------------------------------------===//
89
Devang Patelf1567a52006-12-13 20:03:48 +000090namespace llvm {
91
92//===----------------------------------------------------------------------===//
93// Pass debugging information. Often it is useful to find out what pass is
94// running when a crash occurs in a utility. When this library is compiled with
95// debugging on, a command line option (--debug-pass) is enabled that causes the
96// pass name to be printed before it executes.
97//
98
Devang Patel03fb5872006-12-13 21:13:31 +000099// Different debug levels that can be enabled...
100enum PassDebugLevel {
101 None, Arguments, Structure, Executions, Details
102};
103
Devang Patelf1567a52006-12-13 20:03:48 +0000104static cl::opt<enum PassDebugLevel>
105PassDebugging_New("debug-pass", cl::Hidden,
106 cl::desc("Print PassManager debugging information"),
107 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +0000108 clEnumVal(None , "disable debug output"),
109 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
110 clEnumVal(Structure , "print pass structure before run()"),
111 clEnumVal(Executions, "print pass name before it is executed"),
112 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +0000113 clEnumValEnd));
114} // End of llvm namespace
115
Devang Patelb67904d2006-12-13 02:36:01 +0000116#ifndef USE_OLD_PASSMANAGER
Devang Patelca58e352006-11-08 10:05:38 +0000117namespace llvm {
118
Devang Patelafb1f3622006-12-12 22:35:25 +0000119class PMDataManager;
120
Devang Patelf33f3eb2006-12-07 19:21:29 +0000121//===----------------------------------------------------------------------===//
122// PMTopLevelManager
123//
124/// PMTopLevelManager manages LastUser info and collects common APIs used by
125/// top level pass managers.
126class PMTopLevelManager {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000127public:
128
129 inline std::vector<Pass *>::iterator passManagersBegin() {
130 return PassManagers.begin();
131 }
132
133 inline std::vector<Pass *>::iterator passManagersEnd() {
134 return PassManagers.end();
135 }
136
137 /// Schedule pass P for execution. Make sure that passes required by
138 /// P are run before P is run. Update analysis info maintained by
139 /// the manager. Remove dead passes. This is a recursive function.
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000140 void schedulePass(Pass *P);
Devang Patelf33f3eb2006-12-07 19:21:29 +0000141
142 /// This is implemented by top level pass manager and used by
143 /// schedulePass() to add analysis info passes that are not available.
144 virtual void addTopLevelPass(Pass *P) = 0;
145
146 /// Set pass P as the last user of the given analysis passes.
147 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
148
149 /// Collect passes whose last user is P
150 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
151
Devang Patel640c5bb2006-12-08 22:30:11 +0000152 /// Find the pass that implements Analysis AID. Search immutable
153 /// passes and all pass managers. If desired pass is not found
154 /// then return NULL.
155 Pass *findAnalysisPass(AnalysisID AID);
156
Devang Patelb67904d2006-12-13 02:36:01 +0000157 inline void clearManagers() {
158 PassManagers.clear();
159 }
160
Devang Patelf33f3eb2006-12-07 19:21:29 +0000161 virtual ~PMTopLevelManager() {
Devang Patelab97cf42006-12-13 00:09:23 +0000162 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
163 E = PassManagers.end(); I != E; ++I)
164 delete *I;
165
166 for (std::vector<ImmutablePass *>::iterator
167 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
168 delete *I;
169
Devang Patelf33f3eb2006-12-07 19:21:29 +0000170 PassManagers.clear();
171 }
172
Devang Patele0eb9d82006-12-07 20:51:18 +0000173 /// Add immutable pass and initialize it.
174 inline void addImmutablePass(ImmutablePass *P) {
175 P->initializePass();
176 ImmutablePasses.push_back(P);
177 }
178
179 inline std::vector<ImmutablePass *>& getImmutablePasses() {
180 return ImmutablePasses;
181 }
182
Devang Patel5bbeb492006-12-08 22:47:25 +0000183 void addPassManager(Pass *Manager) {
184 PassManagers.push_back(Manager);
185 }
186
Devang Patelaf1fca52006-12-08 23:11:43 +0000187 // Add Manager into the list of managers that are not directly
188 // maintained by this top level pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000189 inline void addIndirectPassManager(PMDataManager *Manager) {
190 IndirectPassManagers.push_back(Manager);
Devang Patelaf1fca52006-12-08 23:11:43 +0000191 }
192
Devang Pateleda56172006-12-12 23:34:33 +0000193 // Print passes managed by this top level manager.
194 void dumpPasses();
Devang Patelcfd70c42006-12-13 22:10:00 +0000195 void dumpArguments();
Devang Pateleda56172006-12-12 23:34:33 +0000196
Devang Patelf33f3eb2006-12-07 19:21:29 +0000197private:
198
199 /// Collection of pass managers
200 std::vector<Pass *> PassManagers;
201
Devang Patelaf1fca52006-12-08 23:11:43 +0000202 /// Collection of pass managers that are not directly maintained
203 /// by this pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000204 std::vector<PMDataManager *> IndirectPassManagers;
Devang Patelaf1fca52006-12-08 23:11:43 +0000205
Devang Patelf33f3eb2006-12-07 19:21:29 +0000206 // Map to keep track of last user of the analysis pass.
207 // LastUser->second is the last user of Lastuser->first.
208 std::map<Pass *, Pass *> LastUser;
Devang Patele0eb9d82006-12-07 20:51:18 +0000209
210 /// Immutable passes are managed by top level manager.
211 std::vector<ImmutablePass *> ImmutablePasses;
Devang Patelf33f3eb2006-12-07 19:21:29 +0000212};
213
Devang Patelf3827bc2006-12-07 19:54:15 +0000214//===----------------------------------------------------------------------===//
215// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000216
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000217/// PMDataManager provides the common place to manage the analysis data
218/// used by pass managers.
219class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000220public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000221 PMDataManager(int D) : TPM(NULL), Depth(D) {
Devang Patelf3827bc2006-12-07 19:54:15 +0000222 initializeAnalysisInfo();
223 }
224
Devang Patelab97cf42006-12-13 00:09:23 +0000225 virtual ~PMDataManager() {
226
227 for (std::vector<Pass *>::iterator I = PassVector.begin(),
228 E = PassVector.end(); I != E; ++I)
229 delete *I;
230
231 PassVector.clear();
232 }
233
Devang Patela9844592006-11-11 01:31:05 +0000234 /// Return true IFF pass P's required analysis set does not required new
235 /// manager.
236 bool manageablePass(Pass *P);
237
Devang Patela9844592006-11-11 01:31:05 +0000238 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000239 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000240
Devang Patela9844592006-11-11 01:31:05 +0000241 /// Remove Analysis that is not preserved by the pass
242 void removeNotPreservedAnalysis(Pass *P);
243
244 /// Remove dead passes
Devang Patel200d3052006-12-13 23:50:44 +0000245 void removeDeadPasses(Pass *P, std::string &Msg);
Devang Patela9844592006-11-11 01:31:05 +0000246
Devang Patel8f677ce2006-12-07 18:47:25 +0000247 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000248 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattnerce22ca32006-12-14 18:22:14 +0000249 void addPassToManager(Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000250
Devang Patel1d6267c2006-12-07 23:05:44 +0000251 /// Initialize available analysis information.
Devang Patela6b6dcb2006-12-07 18:41:09 +0000252 void initializeAnalysisInfo() {
Devang Patelbc03f132006-12-07 23:55:10 +0000253 ForcedLastUses.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000254 AvailableAnalysis.clear();
255 }
256
Devang Patel1d6267c2006-12-07 23:05:44 +0000257 /// Populate RequiredPasses with the analysis pass that are required by
258 /// pass P.
259 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
260 Pass *P);
261
262 /// All Required analyses should be available to the pass as it runs! Here
263 /// we fill in the AnalysisImpls member of the pass so that it can
264 /// successfully use the getAnalysis() method to retrieve the
265 /// implementations it needs.
266 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000267
Devang Patel640c5bb2006-12-08 22:30:11 +0000268 /// Find the pass that implements Analysis AID. If desired pass is not found
269 /// then return NULL.
270 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
271
Devang Patel8cad70d2006-11-11 01:51:02 +0000272 inline std::vector<Pass *>::iterator passVectorBegin() {
273 return PassVector.begin();
274 }
275
276 inline std::vector<Pass *>::iterator passVectorEnd() {
277 return PassVector.end();
278 }
279
Devang Patelf3827bc2006-12-07 19:54:15 +0000280 // Access toplevel manager
281 PMTopLevelManager *getTopLevelManager() { return TPM; }
282 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
283
Devang Patel4c36e6b2006-12-07 23:24:58 +0000284 unsigned getDepth() { return Depth; }
285
Devang Pateleda56172006-12-12 23:34:33 +0000286 // Print list of passes that are last used by P.
287 void dumpLastUses(Pass *P, unsigned Offset) {
288
289 std::vector<Pass *> LUses;
290
291 assert (TPM && "Top Level Manager is missing");
292 TPM->collectLastUses(LUses, P);
293
294 for (std::vector<Pass *>::iterator I = LUses.begin(),
295 E = LUses.end(); I != E; ++I) {
296 llvm::cerr << "--" << std::string(Offset*2, ' ');
297 (*I)->dumpPassStructure(0);
298 }
299 }
300
Devang Patelcfd70c42006-12-13 22:10:00 +0000301 void dumpPassArguments() {
302 for(std::vector<Pass *>::iterator I = PassVector.begin(),
303 E = PassVector.end(); I != E; ++I) {
304 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
305 PMD->dumpPassArguments();
306 else
307 if (const PassInfo *PI = (*I)->getPassInfo())
308 if (!PI->isAnalysisGroup())
309 cerr << " -" << PI->getPassArgument();
310 }
311 }
312
Devang Patel200d3052006-12-13 23:50:44 +0000313 void dumpPassInfo(Pass *P, std::string &Msg1, std::string &Msg2) {
314 if (PassDebugging_New < Executions)
315 return;
316 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
317 cerr << Msg1;
318 cerr << P->getPassName();
319 cerr << Msg2;
320 }
321
Devang Patelf6d1d212006-12-14 00:25:06 +0000322 void dumpAnalysisSetInfo(const char *Msg, Pass *P,
323 const std::vector<AnalysisID> &Set) {
324 if (PassDebugging_New >= Details && !Set.empty()) {
325 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
326 for (unsigned i = 0; i != Set.size(); ++i) {
327 if (i) cerr << ",";
328 cerr << " " << Set[i]->getPassName();
329 }
330 cerr << "\n";
331 }
332 }
Devang Patelbc03f132006-12-07 23:55:10 +0000333protected:
334
335 // Collection of pass whose last user asked this manager to claim
336 // last use. If a FunctionPass F is the last user of ModulePass info M
337 // then the F's manager, not F, records itself as a last user of M.
338 std::vector<Pass *> ForcedLastUses;
339
340 // Top level manager.
Devang Patelbc03f132006-12-07 23:55:10 +0000341 PMTopLevelManager *TPM;
342
Devang Patela9844592006-11-11 01:31:05 +0000343private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000344 // Set of available Analysis. This information is used while scheduling
345 // pass. If a pass requires an analysis which is not not available then
346 // equired analysis pass is scheduled to run before the pass itself is
347 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000348 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000349
350 // Collection of pass that are managed by this manager
351 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000352
Devang Patel4c36e6b2006-12-07 23:24:58 +0000353 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000354};
355
Devang Patel10c2ca62006-12-12 22:47:13 +0000356//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +0000357// BasicBlockPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000358//
Devang Patelb67904d2006-12-13 02:36:01 +0000359/// BasicBlockPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000360/// pass together and sequence them to process one basic block before
361/// processing next basic block.
Devang Patelb67904d2006-12-13 02:36:01 +0000362class BasicBlockPassManager : public PMDataManager,
Chris Lattnerce22ca32006-12-14 18:22:14 +0000363 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000364
365public:
Devang Patelb67904d2006-12-13 02:36:01 +0000366 BasicBlockPassManager(int D) : PMDataManager(D) { }
Devang Patelca58e352006-11-08 10:05:38 +0000367
368 /// Add a pass into a passmanager queue.
369 bool addPass(Pass *p);
370
371 /// Execute all of the passes scheduled for execution. Keep track of
372 /// whether any of the passes modifies the function, and if so, return true.
373 bool runOnFunction(Function &F);
374
Devang Patelf9d96b92006-12-07 19:57:52 +0000375 /// Pass Manager itself does not invalidate any analysis info.
376 void getAnalysisUsage(AnalysisUsage &Info) const {
377 Info.setPreservesAll();
378 }
379
Devang Patel475c4532006-12-08 00:59:05 +0000380 bool doInitialization(Module &M);
381 bool doInitialization(Function &F);
382 bool doFinalization(Module &M);
383 bool doFinalization(Function &F);
384
Devang Pateleda56172006-12-12 23:34:33 +0000385 // Print passes managed by this manager
386 void dumpPassStructure(unsigned Offset) {
387 llvm::cerr << std::string(Offset*2, ' ') << "BasicBLockPass Manager\n";
388 for (std::vector<Pass *>::iterator I = passVectorBegin(),
389 E = passVectorEnd(); I != E; ++I) {
390 (*I)->dumpPassStructure(Offset + 1);
391 dumpLastUses(*I, Offset+1);
392 }
393 }
Devang Patelca58e352006-11-08 10:05:38 +0000394};
395
Devang Patel10c2ca62006-12-12 22:47:13 +0000396//===----------------------------------------------------------------------===//
397// FunctionPassManagerImpl_New
398//
Chris Lattnerf0f611a2006-12-13 21:56:10 +0000399/// FunctionPassManagerImpl_New manages FunctionPasses and
400/// BasicBlockPassManagers. It batches all function passes and basic block pass
401/// managers together and sequence them to process one function at a time before
402/// processing next function.
Devang Patelabcd1d32006-12-07 21:27:23 +0000403class FunctionPassManagerImpl_New : public ModulePass,
404 public PMDataManager,
405 public PMTopLevelManager {
Devang Patelca58e352006-11-08 10:05:38 +0000406public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000407 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
Devang Patelca58e352006-11-08 10:05:38 +0000408 activeBBPassManager = NULL;
409 }
Devang Patel4e12f862006-11-08 10:44:40 +0000410 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000411
Devang Patelabcd1d32006-12-07 21:27:23 +0000412 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000413
Devang Patelfa971cd2006-12-08 23:57:43 +0000414 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000415
416 // P is a immutable pass then it will be managed by this
417 // top level manager. Set up analysis resolver to connect them.
418 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
419 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000420 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000421 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000422 recordAvailableAnalysis(IP);
Devang Patelfa971cd2006-12-08 23:57:43 +0000423 }
424 else
425 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000426 }
427
Devang Patelca58e352006-11-08 10:05:38 +0000428 /// add - Add a pass to the queue of passes to run. This passes
429 /// ownership of the Pass to the PassManager. When the
430 /// PassManager_X is destroyed, the pass will be destroyed as well, so
431 /// there is no need to delete the pass. (TODO delete passes.)
432 /// This implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000433 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000434 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000435 }
Devang Patelca58e352006-11-08 10:05:38 +0000436
437 /// Add pass into the pass manager queue.
438 bool addPass(Pass *P);
439
440 /// Execute all of the passes scheduled for execution. Keep
441 /// track of whether any of the passes modifies the function, and if
442 /// so, return true.
443 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000444 bool runOnFunction(Function &F);
Devang Patel272908d2006-12-08 22:57:48 +0000445 bool run(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000446
Devang Patelff631ae2006-11-15 01:27:05 +0000447 /// doInitialization - Run all of the initializers for the function passes.
448 ///
449 bool doInitialization(Module &M);
450
451 /// doFinalization - Run all of the initializers for the function passes.
452 ///
453 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000454
455 /// Pass Manager itself does not invalidate any analysis info.
456 void getAnalysisUsage(AnalysisUsage &Info) const {
457 Info.setPreservesAll();
458 }
459
Devang Pateleda56172006-12-12 23:34:33 +0000460 // Print passes managed by this manager
461 void dumpPassStructure(unsigned Offset) {
462 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
463 for (std::vector<Pass *>::iterator I = passVectorBegin(),
464 E = passVectorEnd(); I != E; ++I) {
465 (*I)->dumpPassStructure(Offset + 1);
466 dumpLastUses(*I, Offset+1);
467 }
468 }
469
Devang Patelca58e352006-11-08 10:05:38 +0000470private:
Devang Patelca58e352006-11-08 10:05:38 +0000471 // Active Pass Managers
Devang Patelb67904d2006-12-13 02:36:01 +0000472 BasicBlockPassManager *activeBBPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000473};
474
Devang Patel10c2ca62006-12-12 22:47:13 +0000475//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +0000476// ModulePassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000477//
Devang Patelb67904d2006-12-13 02:36:01 +0000478/// ModulePassManager manages ModulePasses and function pass managers.
Devang Patelca58e352006-11-08 10:05:38 +0000479/// It batches all Module passes passes and function pass managers together and
480/// sequence them to process one module.
Chris Lattnerce22ca32006-12-14 18:22:14 +0000481class ModulePassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000482
483public:
Devang Patelb67904d2006-12-13 02:36:01 +0000484 ModulePassManager(int D) : PMDataManager(D) {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000485 activeFunctionPassManager = NULL;
486 }
Devang Patelca58e352006-11-08 10:05:38 +0000487
488 /// Add a pass into a passmanager queue.
489 bool addPass(Pass *p);
490
491 /// run - Execute all of the passes scheduled for execution. Keep track of
492 /// whether any of the passes modifies the module, and if so, return true.
493 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000494
Devang Patelf9d96b92006-12-07 19:57:52 +0000495 /// Pass Manager itself does not invalidate any analysis info.
496 void getAnalysisUsage(AnalysisUsage &Info) const {
497 Info.setPreservesAll();
498 }
499
Devang Pateleda56172006-12-12 23:34:33 +0000500 // Print passes managed by this manager
501 void dumpPassStructure(unsigned Offset) {
502 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
503 for (std::vector<Pass *>::iterator I = passVectorBegin(),
504 E = passVectorEnd(); I != E; ++I) {
505 (*I)->dumpPassStructure(Offset + 1);
506 dumpLastUses(*I, Offset+1);
507 }
508 }
509
Devang Patelca58e352006-11-08 10:05:38 +0000510private:
Devang Patelca58e352006-11-08 10:05:38 +0000511 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000512 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000513};
514
Devang Patel10c2ca62006-12-12 22:47:13 +0000515//===----------------------------------------------------------------------===//
516// PassManagerImpl_New
517//
518/// PassManagerImpl_New manages ModulePassManagers
Devang Patel31217af2006-12-07 21:32:57 +0000519class PassManagerImpl_New : public Pass,
520 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000521 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000522
523public:
524
Devang Patelad6b7fe2006-12-12 22:57:43 +0000525 PassManagerImpl_New(int D) : PMDataManager(D) {
526 activeManager = NULL;
527 }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000528
Devang Patel376fefa2006-11-08 10:29:57 +0000529 /// add - Add a pass to the queue of passes to run. This passes ownership of
530 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
531 /// will be destroyed as well, so there is no need to delete the pass. This
532 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000533 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000534 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000535 }
Devang Patel376fefa2006-11-08 10:29:57 +0000536
537 /// run - Execute all of the passes scheduled for execution. Keep track of
538 /// whether any of the passes modifies the module, and if so, return true.
539 bool run(Module &M);
540
Devang Patelf9d96b92006-12-07 19:57:52 +0000541 /// Pass Manager itself does not invalidate any analysis info.
542 void getAnalysisUsage(AnalysisUsage &Info) const {
543 Info.setPreservesAll();
544 }
545
Devang Patelabcd1d32006-12-07 21:27:23 +0000546 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000547
Devang Patelfa971cd2006-12-08 23:57:43 +0000548 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000549
550 // P is a immutable pass and it will be managed by this
551 // top level manager. Set up analysis resolver to connect them.
552 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
553 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000554 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000555 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000556 recordAvailableAnalysis(IP);
Devang Pateld440cd92006-12-08 23:53:00 +0000557 }
Devang Patelfa971cd2006-12-08 23:57:43 +0000558 else
559 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000560 }
561
Devang Patel376fefa2006-11-08 10:29:57 +0000562private:
563
Devang Patelde124182006-12-07 21:10:57 +0000564 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000565 bool addPass(Pass *p);
566
Devang Patel376fefa2006-11-08 10:29:57 +0000567 // Active Pass Manager
Devang Patelb67904d2006-12-13 02:36:01 +0000568 ModulePassManager *activeManager;
Devang Patel376fefa2006-11-08 10:29:57 +0000569};
570
Devang Patelb8817b92006-12-14 00:59:42 +0000571//===----------------------------------------------------------------------===//
572// TimingInfo Class - This class is used to calculate information about the
573// amount of time each pass takes to execute. This only happens when
574// -time-passes is enabled on the command line.
575//
576
577class TimingInfo {
578 std::map<Pass*, Timer> TimingData;
579 TimerGroup TG;
580
581public:
582 // Use 'create' member to get this.
583 TimingInfo() : TG("... Pass execution timing report ...") {}
584
585 // TimingDtor - Print out information about timing information
586 ~TimingInfo() {
587 // Delete all of the timers...
588 TimingData.clear();
589 // TimerGroup is deleted next, printing the report.
590 }
591
592 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
593 // to a non null value (if the -time-passes option is enabled) or it leaves it
594 // null. It may be called multiple times.
595 static void createTheTimeInfo();
596
597 void passStarted(Pass *P) {
598
599 if (dynamic_cast<PMDataManager *>(P))
600 return;
601
602 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
603 if (I == TimingData.end())
604 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
605 I->second.startTimer();
606 }
607 void passEnded(Pass *P) {
608
609 if (dynamic_cast<PMDataManager *>(P))
610 return;
611
612 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
613 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
614 I->second.stopTimer();
615 }
616};
617
618static TimingInfo *TheTimeInfo;
619
Devang Patelca58e352006-11-08 10:05:38 +0000620} // End of llvm namespace
621
Devang Patela1514cb2006-12-07 19:39:39 +0000622//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000623// PMTopLevelManager implementation
624
625/// Set pass P as the last user of the given analysis passes.
626void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
627 Pass *P) {
628
629 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
630 E = AnalysisPasses.end(); I != E; ++I) {
631 Pass *AP = *I;
632 LastUser[AP] = P;
633 // If AP is the last user of other passes then make P last user of
634 // such passes.
635 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
636 LUE = LastUser.end(); LUI != LUE; ++LUI) {
637 if (LUI->second == AP)
638 LastUser[LUI->first] = P;
639 }
640 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000641}
642
643/// Collect passes whose last user is P
644void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
645 Pass *P) {
646 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
647 LUE = LastUser.end(); LUI != LUE; ++LUI)
648 if (LUI->second == P)
649 LastUses.push_back(LUI->first);
650}
651
652/// Schedule pass P for execution. Make sure that passes required by
653/// P are run before P is run. Update analysis info maintained by
654/// the manager. Remove dead passes. This is a recursive function.
655void PMTopLevelManager::schedulePass(Pass *P) {
656
657 // TODO : Allocate function manager for this pass, other wise required set
658 // may be inserted into previous function manager
659
660 AnalysisUsage AnUsage;
661 P->getAnalysisUsage(AnUsage);
662 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
663 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
664 E = RequiredSet.end(); I != E; ++I) {
665
666 Pass *AnalysisPass = findAnalysisPass(*I);
667 if (!AnalysisPass) {
668 // Schedule this analysis run first.
669 AnalysisPass = (*I)->createPass();
670 schedulePass(AnalysisPass);
671 }
672 }
673
674 // Now all required passes are available.
675 addTopLevelPass(P);
676}
677
678/// Find the pass that implements Analysis AID. Search immutable
679/// passes and all pass managers. If desired pass is not found
680/// then return NULL.
681Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
682
683 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000684 // Check pass managers
685 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
686 E = PassManagers.end(); P == NULL && I != E; ++I) {
687 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
688 assert(PMD && "This is not a PassManager");
689 P = PMD->findAnalysisPass(AID, false);
690 }
691
692 // Check other pass managers
693 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
694 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
695 P = (*I)->findAnalysisPass(AID, false);
696
Devang Patelafb1f3622006-12-12 22:35:25 +0000697 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
698 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
699 const PassInfo *PI = (*I)->getPassInfo();
700 if (PI == AID)
701 P = *I;
702
703 // If Pass not found then check the interfaces implemented by Immutable Pass
704 if (!P) {
705 const std::vector<const PassInfo*> &ImmPI =
706 PI->getInterfacesImplemented();
707 for (unsigned Index = 0, End = ImmPI.size();
708 P == NULL && Index != End; ++Index)
709 if (ImmPI[Index] == AID)
710 P = *I;
711 }
712 }
713
Devang Patelafb1f3622006-12-12 22:35:25 +0000714 return P;
715}
716
Devang Pateleda56172006-12-12 23:34:33 +0000717// Print passes managed by this top level manager.
718void PMTopLevelManager::dumpPasses() {
719
720 // Print out the immutable passes
721 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
722 ImmutablePasses[i]->dumpPassStructure(0);
723 }
724
725 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
726 E = PassManagers.end(); I != E; ++I)
727 (*I)->dumpPassStructure(1);
728
729}
730
Devang Patelcfd70c42006-12-13 22:10:00 +0000731void PMTopLevelManager::dumpArguments() {
732
733 if (PassDebugging_New < Arguments)
734 return;
735
736 cerr << "Pass Arguments: ";
737 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
738 E = PassManagers.end(); I != E; ++I) {
739 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
740 assert(PMD && "This is not a PassManager");
741 PMD->dumpPassArguments();
742 }
743 cerr << "\n";
744}
745
Devang Patelafb1f3622006-12-12 22:35:25 +0000746//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000747// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000748
Devang Pateld65e9e92006-11-08 01:31:28 +0000749/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000750/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000751bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000752
Devang Patel8f677ce2006-12-07 18:47:25 +0000753 // TODO
754 // If this pass is not preserving information that is required by a
755 // pass maintained by higher level pass manager then do not insert
756 // this pass into current manager. Use new manager. For example,
757 // For example, If FunctionPass F is not preserving ModulePass Info M1
758 // that is used by another ModulePass M2 then do not insert F in
759 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000760 return true;
761}
762
Devang Patel643676c2006-11-11 01:10:19 +0000763/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000764void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000765
Devang Patel643676c2006-11-11 01:10:19 +0000766 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000767 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000768
Devang Patele9976aa2006-12-07 19:33:53 +0000769 //This pass is the current implementation of all of the interfaces it
770 //implements as well.
771 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
772 for (unsigned i = 0, e = II.size(); i != e; ++i)
773 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000774 }
775}
776
Devang Patelf68a3492006-11-07 22:35:17 +0000777/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000778void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000779 AnalysisUsage AnUsage;
780 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000781
Devang Patel2e169c32006-12-07 20:03:49 +0000782 if (AnUsage.getPreservesAll())
783 return;
784
785 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000786 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000787 E = AvailableAnalysis.end(); I != E; ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000788 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000789 PreservedSet.end()) {
790 // Remove this analysis
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000791 if (!dynamic_cast<ImmutablePass*>(I->second)) {
792 std::map<AnalysisID, Pass*>::iterator J = I++;
793 AvailableAnalysis.erase(J);
794 } else
795 ++I;
796 } else
797 ++I;
Devang Patel349170f2006-11-11 01:24:55 +0000798 }
Devang Patelf68a3492006-11-07 22:35:17 +0000799}
800
Devang Patelca189262006-11-14 03:05:08 +0000801/// Remove analysis passes that are not used any longer
Devang Patel200d3052006-12-13 23:50:44 +0000802void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
Devang Patel17ad0962006-12-08 00:37:52 +0000803
804 std::vector<Pass *> DeadPasses;
805 TPM->collectLastUses(DeadPasses, P);
806
807 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
808 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000809
810 std::string Msg1 = " Freeing Pass '";
811 dumpPassInfo(*I, Msg1, Msg);
812
Devang Patelb8817b92006-12-14 00:59:42 +0000813 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Devang Patel17ad0962006-12-08 00:37:52 +0000814 (*I)->releaseMemory();
Devang Patelb8817b92006-12-14 00:59:42 +0000815 if (TheTimeInfo) TheTimeInfo->passEnded(P);
816
Devang Patel17ad0962006-12-08 00:37:52 +0000817 std::map<AnalysisID, Pass*>::iterator Pos =
818 AvailableAnalysis.find((*I)->getPassInfo());
819
Devang Patel475c4532006-12-08 00:59:05 +0000820 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000821 if (Pos != AvailableAnalysis.end())
822 AvailableAnalysis.erase(Pos);
823 }
Devang Patelca189262006-11-14 03:05:08 +0000824}
825
Devang Patel8f677ce2006-12-07 18:47:25 +0000826/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000827/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000828void PMDataManager::addPassToManager(Pass *P,
829 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000830
Devang Pateld440cd92006-12-08 23:53:00 +0000831 // This manager is going to manage pass P. Set up analysis resolver
832 // to connect them.
833 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
834 P->setResolver(AR);
835
Devang Patel90b05e02006-11-11 02:04:19 +0000836 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000837
838 // At the moment, this pass is the last user of all required passes.
839 std::vector<Pass *> LastUses;
840 std::vector<Pass *> RequiredPasses;
841 unsigned PDepth = this->getDepth();
842
843 collectRequiredAnalysisPasses(RequiredPasses, P);
844 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
845 E = RequiredPasses.end(); I != E; ++I) {
846 Pass *PRequired = *I;
847 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000848
849 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
850 RDepth = DM.getDepth();
851
Devang Patelbc03f132006-12-07 23:55:10 +0000852 if (PDepth == RDepth)
853 LastUses.push_back(PRequired);
854 else if (PDepth > RDepth) {
855 // Let the parent claim responsibility of last use
856 ForcedLastUses.push_back(PRequired);
857 } else {
858 // Note : This feature is not yet implemented
859 assert (0 &&
860 "Unable to handle Pass that requires lower level Analysis pass");
861 }
862 }
863
864 if (!LastUses.empty())
865 TPM->setLastUser(LastUses, P);
866
Devang Patel17bff0d2006-12-07 22:09:36 +0000867 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000868 // Remove the analysis not preserved by this pass
869 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000870 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000871 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000872
873 // Add pass
874 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000875}
876
Devang Patel1d6267c2006-12-07 23:05:44 +0000877/// Populate RequiredPasses with the analysis pass that are required by
878/// pass P.
879void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
880 Pass *P) {
881 AnalysisUsage AnUsage;
882 P->getAnalysisUsage(AnUsage);
883 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
884 for (std::vector<AnalysisID>::const_iterator
885 I = RequiredSet.begin(), E = RequiredSet.end();
886 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000887 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000888 assert (AnalysisPass && "Analysis pass is not available");
889 RP.push_back(AnalysisPass);
890 }
Devang Patelf58183d2006-12-12 23:09:32 +0000891
892 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
893 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
894 E = IDs.end(); I != E; ++I) {
895 Pass *AnalysisPass = findAnalysisPass(*I, true);
896 assert (AnalysisPass && "Analysis pass is not available");
897 RP.push_back(AnalysisPass);
898 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000899}
900
Devang Patel07f4f582006-11-14 21:49:36 +0000901// All Required analyses should be available to the pass as it runs! Here
902// we fill in the AnalysisImpls member of the pass so that it can
903// successfully use the getAnalysis() method to retrieve the
904// implementations it needs.
905//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000906void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000907 AnalysisUsage AnUsage;
908 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000909
910 for (std::vector<const PassInfo *>::const_iterator
911 I = AnUsage.getRequiredSet().begin(),
912 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000913 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000914 if (Impl == 0)
915 assert(0 && "Analysis used but not available!");
Devang Patel984698a2006-12-09 01:11:34 +0000916 AnalysisResolver_New *AR = P->getResolver();
917 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000918 }
919}
920
Devang Patel640c5bb2006-12-08 22:30:11 +0000921/// Find the pass that implements Analysis AID. If desired pass is not found
922/// then return NULL.
923Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
924
925 // Check if AvailableAnalysis map has one entry.
926 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
927
928 if (I != AvailableAnalysis.end())
929 return I->second;
930
931 // Search Parents through TopLevelManager
932 if (SearchParent)
933 return TPM->findAnalysisPass(AID);
934
Devang Patel9d759b82006-12-09 00:09:12 +0000935 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000936}
937
Devang Patel9bdf7d42006-12-08 23:28:54 +0000938
939//===----------------------------------------------------------------------===//
940// NOTE: Is this the right place to define this method ?
941// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
942Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
943 return PM.findAnalysisPass(ID, dir);
944}
945
Devang Patela1514cb2006-12-07 19:39:39 +0000946//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +0000947// BasicBlockPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000948
Devang Pateld65e9e92006-11-08 01:31:28 +0000949/// Add pass P into PassVector and return true. If this pass is not
950/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000951bool
Devang Patelb67904d2006-12-13 02:36:01 +0000952BasicBlockPassManager::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000953
954 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
955 if (!BP)
956 return false;
957
Devang Patel3c8eb622006-11-07 22:56:50 +0000958 // If this pass does not preserve anlysis that is used by other passes
959 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000960 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000961 return false;
962
Chris Lattnerce22ca32006-12-14 18:22:14 +0000963 addPassToManager(BP);
Devang Patel349170f2006-11-11 01:24:55 +0000964
Devang Patel6e5a1132006-11-07 21:31:57 +0000965 return true;
966}
967
968/// Execute all of the passes scheduled for execution by invoking
969/// runOnBasicBlock method. Keep track of whether any of the passes modifies
970/// the function, and if so, return true.
971bool
Devang Patelb67904d2006-12-13 02:36:01 +0000972BasicBlockPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000973
Devang Patel745a6962006-12-12 23:15:28 +0000974 if (F.isExternal())
975 return false;
976
Devang Patele9585592006-12-08 01:38:28 +0000977 bool Changed = doInitialization(F);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000978 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000979
Devang Patel93a197c2006-12-14 00:08:04 +0000980 std::string Msg1 = "Executing Pass '";
981 std::string Msg3 = "' Made Modification '";
982
Devang Patel6e5a1132006-11-07 21:31:57 +0000983 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000984 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
985 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000986 Pass *P = *itr;
Devang Patelf6d1d212006-12-14 00:25:06 +0000987 AnalysisUsage AnUsage;
988 P->getAnalysisUsage(AnUsage);
989
Devang Patel200d3052006-12-13 23:50:44 +0000990 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
991 dumpPassInfo(P, Msg1, Msg2);
Devang Patelf6d1d212006-12-14 00:25:06 +0000992 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
993
Devang Patel47d7df72006-12-12 23:13:09 +0000994 initializeAnalysisImpl(P);
Devang Patel93a197c2006-12-14 00:08:04 +0000995
Devang Patel6e5a1132006-11-07 21:31:57 +0000996 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
Devang Patelb8817b92006-12-14 00:59:42 +0000997 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000998 Changed |= BP->runOnBasicBlock(*I);
Devang Patelb8817b92006-12-14 00:59:42 +0000999 if (TheTimeInfo) TheTimeInfo->passEnded(P);
Devang Patel93a197c2006-12-14 00:08:04 +00001000
1001 if (Changed)
1002 dumpPassInfo(P, Msg3, Msg2);
Devang Patelf6d1d212006-12-14 00:25:06 +00001003 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001004
Devang Patel050ec722006-11-14 01:23:29 +00001005 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001006 recordAvailableAnalysis(P);
Devang Patel200d3052006-12-13 23:50:44 +00001007 removeDeadPasses(P, Msg2);
Devang Patel6e5a1132006-11-07 21:31:57 +00001008 }
Devang Patele9585592006-12-08 01:38:28 +00001009 return Changed | doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001010}
1011
Devang Patel475c4532006-12-08 00:59:05 +00001012// Implement doInitialization and doFinalization
Devang Patelb67904d2006-12-13 02:36:01 +00001013inline bool BasicBlockPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001014 bool Changed = false;
1015
1016 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1017 e = passVectorEnd(); itr != e; ++itr) {
1018 Pass *P = *itr;
1019 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
1020 Changed |= BP->doInitialization(M);
1021 }
1022
1023 return Changed;
1024}
1025
Devang Patelb67904d2006-12-13 02:36:01 +00001026inline bool BasicBlockPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001027 bool Changed = false;
1028
1029 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1030 e = passVectorEnd(); itr != e; ++itr) {
1031 Pass *P = *itr;
1032 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
1033 Changed |= BP->doFinalization(M);
1034 }
1035
1036 return Changed;
1037}
1038
Devang Patelb67904d2006-12-13 02:36:01 +00001039inline bool BasicBlockPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001040 bool Changed = false;
1041
1042 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1043 e = passVectorEnd(); itr != e; ++itr) {
1044 Pass *P = *itr;
1045 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
1046 Changed |= BP->doInitialization(F);
1047 }
1048
1049 return Changed;
1050}
1051
Devang Patelb67904d2006-12-13 02:36:01 +00001052inline bool BasicBlockPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001053 bool Changed = false;
1054
1055 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1056 e = passVectorEnd(); itr != e; ++itr) {
1057 Pass *P = *itr;
1058 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
1059 Changed |= BP->doFinalization(F);
1060 }
1061
1062 return Changed;
1063}
1064
1065
Devang Patela1514cb2006-12-07 19:39:39 +00001066//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001067// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001068
Devang Patel4e12f862006-11-08 10:44:40 +00001069/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001070FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel1f653682006-12-08 18:57:16 +00001071 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001072 // FPM is the top level manager.
1073 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001074
1075 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
1076 AnalysisResolver_New *AR = new AnalysisResolver_New(*PMD);
1077 FPM->setResolver(AR);
1078
1079 FPM->addPassManager(FPM);
Devang Patel1f653682006-12-08 18:57:16 +00001080 MP = P;
1081}
1082
Devang Patelb67904d2006-12-13 02:36:01 +00001083FunctionPassManager::~FunctionPassManager() {
1084 // Note : FPM maintains one entry in PassManagers vector.
1085 // This one entry is FPM itself. This is not ideal. One
1086 // alternative is have one additional layer between
1087 // FunctionPassManager and FunctionPassManagerImpl.
1088 // Meanwhile, to avoid going into infinte loop, first
1089 // remove FPM from its PassMangers vector.
1090 FPM->clearManagers();
Devang Patelab97cf42006-12-13 00:09:23 +00001091 delete FPM;
1092}
1093
Devang Patel4e12f862006-11-08 10:44:40 +00001094/// add - Add a pass to the queue of passes to run. This passes
1095/// ownership of the Pass to the PassManager. When the
1096/// PassManager_X is destroyed, the pass will be destroyed as well, so
1097/// there is no need to delete the pass. (TODO delete passes.)
1098/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001099void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001100 FPM->add(P);
1101}
1102
Devang Patel9f3083e2006-11-15 19:39:54 +00001103/// run - Execute all of the passes scheduled for execution. Keep
1104/// track of whether any of the passes modifies the function, and if
1105/// so, return true.
1106///
Devang Patelb67904d2006-12-13 02:36:01 +00001107bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001108 std::string errstr;
1109 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001110 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001111 abort();
1112 }
Devang Patel272908d2006-12-08 22:57:48 +00001113 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001114}
1115
1116
Devang Patelff631ae2006-11-15 01:27:05 +00001117/// doInitialization - Run all of the initializers for the function passes.
1118///
Devang Patelb67904d2006-12-13 02:36:01 +00001119bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001120 return FPM->doInitialization(*MP->getModule());
1121}
1122
1123/// doFinalization - Run all of the initializers for the function passes.
1124///
Devang Patelb67904d2006-12-13 02:36:01 +00001125bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001126 return FPM->doFinalization(*MP->getModule());
1127}
1128
Devang Patela1514cb2006-12-07 19:39:39 +00001129//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +00001130// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001131
Devang Patel0c2012f2006-11-07 21:49:50 +00001132/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
1133/// either use it into active basic block pass manager or create new basic
1134/// block pass manager to handle pass P.
1135bool
Devang Patel4e12f862006-11-08 10:44:40 +00001136FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +00001137
Devang Patelb67904d2006-12-13 02:36:01 +00001138 // If P is a BasicBlockPass then use BasicBlockPassManager.
Devang Patel0c2012f2006-11-07 21:49:50 +00001139 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
1140
Devang Patel4949fe02006-12-07 22:34:21 +00001141 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +00001142
Devang Patel4949fe02006-12-07 22:34:21 +00001143 // If active manager exists then clear its analysis info.
1144 if (activeBBPassManager)
1145 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001146
Devang Patel4949fe02006-12-07 22:34:21 +00001147 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001148 activeBBPassManager =
Devang Patelb67904d2006-12-13 02:36:01 +00001149 new BasicBlockPassManager(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001150 // Inherit top level manager
1151 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001152
1153 // Add new manager into current manager's list.
Devang Patel90b05e02006-11-11 02:04:19 +00001154 addPassToManager(activeBBPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001155
1156 // Add new manager into top level manager's indirect passes list
1157 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
1158 assert (PMD && "Manager is not Pass Manager");
1159 TPM->addIndirectPassManager(PMD);
Devang Patel4949fe02006-12-07 22:34:21 +00001160
1161 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001162 if (!activeBBPassManager->addPass(BP))
1163 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +00001164 }
Devang Patelbc03f132006-12-07 23:55:10 +00001165
1166 if (!ForcedLastUses.empty())
1167 TPM->setLastUser(ForcedLastUses, this);
1168
Devang Patel0c2012f2006-11-07 21:49:50 +00001169 return true;
1170 }
1171
1172 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
1173 if (!FP)
1174 return false;
1175
Devang Patel3c8eb622006-11-07 22:56:50 +00001176 // If this pass does not preserve anlysis that is used by other passes
1177 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001178 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001179 return false;
1180
Devang Patel8cad70d2006-11-11 01:51:02 +00001181 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +00001182
1183 // If active manager exists then clear its analysis info.
1184 if (activeBBPassManager) {
1185 activeBBPassManager->initializeAnalysisInfo();
1186 activeBBPassManager = NULL;
1187 }
1188
Devang Patel0c2012f2006-11-07 21:49:50 +00001189 return true;
1190}
1191
1192/// Execute all of the passes scheduled for execution by invoking
1193/// runOnFunction method. Keep track of whether any of the passes modifies
1194/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +00001195bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +00001196
Devang Patel0e29e292006-12-08 19:04:09 +00001197 bool Changed = doInitialization(M);
Devang Patela6b6dcb2006-12-07 18:41:09 +00001198 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001199
Devang Patel0c2012f2006-11-07 21:49:50 +00001200 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel19290892006-12-08 19:03:05 +00001201 this->runOnFunction(*I);
1202
Devang Patel0e29e292006-12-08 19:04:09 +00001203 return Changed | doFinalization(M);
Devang Patel0c2012f2006-11-07 21:49:50 +00001204}
1205
Devang Patel9f3083e2006-11-15 19:39:54 +00001206/// Execute all of the passes scheduled for execution by invoking
1207/// runOnFunction method. Keep track of whether any of the passes modifies
1208/// the function, and if so, return true.
1209bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
1210
1211 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001212
1213 if (F.isExternal())
1214 return false;
1215
Devang Patela6b6dcb2006-12-07 18:41:09 +00001216 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +00001217
Devang Patel93a197c2006-12-14 00:08:04 +00001218 std::string Msg1 = "Executing Pass '";
1219 std::string Msg3 = "' Made Modification '";
1220
Devang Patel9f3083e2006-11-15 19:39:54 +00001221 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1222 e = passVectorEnd(); itr != e; ++itr) {
1223 Pass *P = *itr;
Devang Patelf6d1d212006-12-14 00:25:06 +00001224 AnalysisUsage AnUsage;
1225 P->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001226
Devang Patel200d3052006-12-13 23:50:44 +00001227 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
1228 dumpPassInfo(P, Msg1, Msg2);
Devang Patelf6d1d212006-12-14 00:25:06 +00001229 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001230
Devang Patel47d7df72006-12-12 23:13:09 +00001231 initializeAnalysisImpl(P);
Devang Patel9f3083e2006-11-15 19:39:54 +00001232 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
Devang Patelb8817b92006-12-14 00:59:42 +00001233
1234 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Devang Patel9f3083e2006-11-15 19:39:54 +00001235 Changed |= FP->runOnFunction(F);
Devang Patelb8817b92006-12-14 00:59:42 +00001236 if (TheTimeInfo) TheTimeInfo->passEnded(P);
Devang Patel93a197c2006-12-14 00:08:04 +00001237
1238 if (Changed)
1239 dumpPassInfo(P, Msg3, Msg2);
Devang Patelf6d1d212006-12-14 00:25:06 +00001240 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001241
Devang Patel9f3083e2006-11-15 19:39:54 +00001242 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001243 recordAvailableAnalysis(P);
Devang Patel200d3052006-12-13 23:50:44 +00001244 removeDeadPasses(P, Msg2);
Devang Patel9f3083e2006-11-15 19:39:54 +00001245 }
1246 return Changed;
1247}
1248
1249
Devang Patelff631ae2006-11-15 01:27:05 +00001250inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
1251 bool Changed = false;
1252
1253 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1254 e = passVectorEnd(); itr != e; ++itr) {
1255 Pass *P = *itr;
1256
1257 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1258 Changed |= FP->doInitialization(M);
1259 }
1260
1261 return Changed;
1262}
1263
1264inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
1265 bool Changed = false;
1266
1267 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1268 e = passVectorEnd(); itr != e; ++itr) {
1269 Pass *P = *itr;
1270
1271 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1272 Changed |= FP->doFinalization(M);
1273 }
1274
Devang Patelff631ae2006-11-15 01:27:05 +00001275 return Changed;
1276}
1277
Devang Patel272908d2006-12-08 22:57:48 +00001278// Execute all the passes managed by this top level manager.
1279// Return true if any function is modified by a pass.
1280bool FunctionPassManagerImpl_New::run(Function &F) {
1281
1282 bool Changed = false;
1283 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1284 E = passManagersEnd(); I != E; ++I) {
Devang Patelb67904d2006-12-13 02:36:01 +00001285 FunctionPassManagerImpl_New *FP =
1286 dynamic_cast<FunctionPassManagerImpl_New *>(*I);
Devang Patel272908d2006-12-08 22:57:48 +00001287 Changed |= FP->runOnFunction(F);
1288 }
1289 return Changed;
1290}
1291
Devang Patela1514cb2006-12-07 19:39:39 +00001292//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +00001293// ModulePassManager implementation
1294
1295/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +00001296/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +00001297/// is not manageable by this manager.
1298bool
Devang Patelb67904d2006-12-13 02:36:01 +00001299ModulePassManager::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +00001300
1301 // If P is FunctionPass then use function pass maanager.
1302 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1303
Devang Patel640c5bb2006-12-08 22:30:11 +00001304 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +00001305
Devang Patel4949fe02006-12-07 22:34:21 +00001306 // If active manager exists then clear its analysis info.
1307 if (activeFunctionPassManager)
1308 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001309
Devang Patel4949fe02006-12-07 22:34:21 +00001310 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001311 activeFunctionPassManager =
1312 new FunctionPassManagerImpl_New(getDepth() + 1);
Devang Patelafb1f3622006-12-12 22:35:25 +00001313
1314 // Add new manager into current manager's list
Devang Patel90b05e02006-11-11 02:04:19 +00001315 addPassToManager(activeFunctionPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001316
Devang Patel9c6290c2006-12-12 22:02:16 +00001317 // Inherit top level manager
1318 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001319
1320 // Add new manager into top level manager's indirect passes list
Chris Lattnerf0f611a2006-12-13 21:56:10 +00001321 PMDataManager *PMD =
1322 dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1323 assert(PMD && "Manager is not Pass Manager");
Devang Patelafb1f3622006-12-12 22:35:25 +00001324 TPM->addIndirectPassManager(PMD);
Devang Patelaf1fca52006-12-08 23:11:43 +00001325
Devang Patel4949fe02006-12-07 22:34:21 +00001326 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001327 if (!activeFunctionPassManager->addPass(FP))
1328 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +00001329 }
Devang Patelbc03f132006-12-07 23:55:10 +00001330
1331 if (!ForcedLastUses.empty())
1332 TPM->setLastUser(ForcedLastUses, this);
1333
Devang Patel05e1a972006-11-07 22:03:15 +00001334 return true;
1335 }
1336
1337 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1338 if (!MP)
1339 return false;
1340
Devang Patel3c8eb622006-11-07 22:56:50 +00001341 // If this pass does not preserve anlysis that is used by other passes
1342 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001343 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001344 return false;
1345
Devang Patel8cad70d2006-11-11 01:51:02 +00001346 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +00001347 // If active manager exists then clear its analysis info.
1348 if (activeFunctionPassManager) {
1349 activeFunctionPassManager->initializeAnalysisInfo();
1350 activeFunctionPassManager = NULL;
1351 }
1352
Devang Patel05e1a972006-11-07 22:03:15 +00001353 return true;
1354}
1355
1356
1357/// Execute all of the passes scheduled for execution by invoking
1358/// runOnModule method. Keep track of whether any of the passes modifies
1359/// the module, and if so, return true.
1360bool
Devang Patelb67904d2006-12-13 02:36:01 +00001361ModulePassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001362 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +00001363 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001364
Devang Patel93a197c2006-12-14 00:08:04 +00001365 std::string Msg1 = "Executing Pass '";
1366 std::string Msg3 = "' Made Modification '";
1367
Devang Patel8cad70d2006-11-11 01:51:02 +00001368 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1369 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +00001370 Pass *P = *itr;
Devang Patelf6d1d212006-12-14 00:25:06 +00001371 AnalysisUsage AnUsage;
1372 P->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001373
Devang Patel200d3052006-12-13 23:50:44 +00001374 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
1375 dumpPassInfo(P, Msg1, Msg2);
Devang Patelf6d1d212006-12-14 00:25:06 +00001376 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001377
Devang Patel47d7df72006-12-12 23:13:09 +00001378 initializeAnalysisImpl(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001379 ModulePass *MP = dynamic_cast<ModulePass*>(P);
Devang Patelb8817b92006-12-14 00:59:42 +00001380
1381 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001382 Changed |= MP->runOnModule(M);
Devang Patelb8817b92006-12-14 00:59:42 +00001383 if (TheTimeInfo) TheTimeInfo->passEnded(P);
Devang Patel93a197c2006-12-14 00:08:04 +00001384
1385 if (Changed)
1386 dumpPassInfo(P, Msg3, Msg2);
Devang Patelf6d1d212006-12-14 00:25:06 +00001387 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
1388
Devang Patel050ec722006-11-14 01:23:29 +00001389 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001390 recordAvailableAnalysis(P);
Devang Patel200d3052006-12-13 23:50:44 +00001391 removeDeadPasses(P, Msg2);
Devang Patel05e1a972006-11-07 22:03:15 +00001392 }
1393 return Changed;
1394}
1395
Devang Patela1514cb2006-12-07 19:39:39 +00001396//===----------------------------------------------------------------------===//
1397// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001398//
Devang Patelc290c8a2006-11-07 22:23:34 +00001399/// Add P into active pass manager or use new module pass manager to
1400/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001401bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001402
Devang Patel6c9f5482006-11-11 00:42:16 +00001403 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelb67904d2006-12-13 02:36:01 +00001404 activeManager = new ModulePassManager(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001405 // Inherit top level manager
1406 activeManager->setTopLevelManager(this->getTopLevelManager());
Devang Pateld440cd92006-12-08 23:53:00 +00001407
1408 // This top level manager is going to manage activeManager.
1409 // Set up analysis resolver to connect them.
1410 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1411 activeManager->setResolver(AR);
1412
Devang Patel5bbeb492006-12-08 22:47:25 +00001413 addPassManager(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001414 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001415 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001416 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001417}
1418
1419/// run - Execute all of the passes scheduled for execution. Keep track of
1420/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001421bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001422
Devang Patelc290c8a2006-11-07 22:23:34 +00001423 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001424
Devang Patelb8817b92006-12-14 00:59:42 +00001425 TimingInfo::createTheTimeInfo();
1426
Devang Patelcfd70c42006-12-13 22:10:00 +00001427 dumpArguments();
Devang Patel03fb5872006-12-13 21:13:31 +00001428 if (PassDebugging_New >= Structure)
Devang Patelf1567a52006-12-13 20:03:48 +00001429 dumpPasses();
1430
Devang Patel5bbeb492006-12-08 22:47:25 +00001431 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1432 E = passManagersEnd(); I != E; ++I) {
Devang Patelb67904d2006-12-13 02:36:01 +00001433 ModulePassManager *MP = dynamic_cast<ModulePassManager *>(*I);
Devang Patel5bbeb492006-12-08 22:47:25 +00001434 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001435 }
1436 return Changed;
1437}
Devang Patel376fefa2006-11-08 10:29:57 +00001438
Devang Patela1514cb2006-12-07 19:39:39 +00001439//===----------------------------------------------------------------------===//
1440// PassManager implementation
1441
Devang Patel376fefa2006-11-08 10:29:57 +00001442/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001443PassManager::PassManager() {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001444 PM = new PassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001445 // PM is the top level manager
1446 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001447}
1448
Devang Patelb67904d2006-12-13 02:36:01 +00001449PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001450 delete PM;
1451}
1452
Devang Patel376fefa2006-11-08 10:29:57 +00001453/// add - Add a pass to the queue of passes to run. This passes ownership of
1454/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1455/// will be destroyed as well, so there is no need to delete the pass. This
1456/// implies that all passes MUST be allocated with 'new'.
1457void
Devang Patelb67904d2006-12-13 02:36:01 +00001458PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001459 PM->add(P);
1460}
1461
1462/// run - Execute all of the passes scheduled for execution. Keep track of
1463/// whether any of the passes modifies the module, and if so, return true.
1464bool
Devang Patelb67904d2006-12-13 02:36:01 +00001465PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001466 return PM->run(M);
1467}
1468
Devang Patelb8817b92006-12-14 00:59:42 +00001469//===----------------------------------------------------------------------===//
1470// TimingInfo Class - This class is used to calculate information about the
1471// amount of time each pass takes to execute. This only happens with
1472// -time-passes is enabled on the command line.
1473//
1474bool llvm::TimePassesIsEnabled = false;
1475static cl::opt<bool,true>
1476EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1477 cl::desc("Time each pass, printing elapsed time for each on exit"));
1478
1479// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1480// a non null value (if the -time-passes option is enabled) or it leaves it
1481// null. It may be called multiple times.
1482void TimingInfo::createTheTimeInfo() {
1483 if (!TimePassesIsEnabled || TheTimeInfo) return;
1484
1485 // Constructed the first time this is called, iff -time-passes is enabled.
1486 // This guarantees that the object will be constructed before static globals,
1487 // thus it will be destroyed before them.
1488 static ManagedStatic<TimingInfo> TTI;
1489 TheTimeInfo = &*TTI;
1490}
1491
Devang Patelb67904d2006-12-13 02:36:01 +00001492#endif