blob: 3d3858e4a4d2acd91835a0890ffca2c83b0180bf [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 Patelb67904d2006-12-13 02:36:01 +0000121#ifndef USE_OLD_PASSMANAGER
Devang Patelffca9102006-12-15 19:39:30 +0000122namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000123
Devang Patelf33f3eb2006-12-07 19:21:29 +0000124//===----------------------------------------------------------------------===//
125// PMTopLevelManager
126//
127/// PMTopLevelManager manages LastUser info and collects common APIs used by
128/// top level pass managers.
Devang Patelffca9102006-12-15 19:39:30 +0000129class VISIBILITY_HIDDEN PMTopLevelManager {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000130public:
131
Devang Patel67d6a5e2006-12-19 19:46:59 +0000132 virtual unsigned getNumContainedManagers() {
133 return PassManagers.size();
Devang Patelf33f3eb2006-12-07 19:21:29 +0000134 }
135
136 /// Schedule pass P for execution. Make sure that passes required by
137 /// P are run before P is run. Update analysis info maintained by
138 /// the manager. Remove dead passes. This is a recursive function.
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000139 void schedulePass(Pass *P);
Devang Patelf33f3eb2006-12-07 19:21:29 +0000140
141 /// This is implemented by top level pass manager and used by
142 /// schedulePass() to add analysis info passes that are not available.
143 virtual void addTopLevelPass(Pass *P) = 0;
144
145 /// Set pass P as the last user of the given analysis passes.
146 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
147
148 /// Collect passes whose last user is P
149 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
150
Devang Patel640c5bb2006-12-08 22:30:11 +0000151 /// Find the pass that implements Analysis AID. Search immutable
152 /// passes and all pass managers. If desired pass is not found
153 /// then return NULL.
154 Pass *findAnalysisPass(AnalysisID AID);
155
Devang Patelf33f3eb2006-12-07 19:21:29 +0000156 virtual ~PMTopLevelManager() {
Devang Patelab97cf42006-12-13 00:09:23 +0000157 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
158 E = PassManagers.end(); I != E; ++I)
159 delete *I;
160
161 for (std::vector<ImmutablePass *>::iterator
162 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
163 delete *I;
164
Devang Patelf33f3eb2006-12-07 19:21:29 +0000165 PassManagers.clear();
166 }
167
Devang Patele0eb9d82006-12-07 20:51:18 +0000168 /// Add immutable pass and initialize it.
169 inline void addImmutablePass(ImmutablePass *P) {
170 P->initializePass();
171 ImmutablePasses.push_back(P);
172 }
173
174 inline std::vector<ImmutablePass *>& getImmutablePasses() {
175 return ImmutablePasses;
176 }
177
Devang Patel5bbeb492006-12-08 22:47:25 +0000178 void addPassManager(Pass *Manager) {
179 PassManagers.push_back(Manager);
180 }
181
Devang Patelaf1fca52006-12-08 23:11:43 +0000182 // Add Manager into the list of managers that are not directly
183 // maintained by this top level pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000184 inline void addIndirectPassManager(PMDataManager *Manager) {
185 IndirectPassManagers.push_back(Manager);
Devang Patelaf1fca52006-12-08 23:11:43 +0000186 }
187
Devang Pateleda56172006-12-12 23:34:33 +0000188 // Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000189 void dumpPasses() const;
190 void dumpArguments() const;
Devang Pateleda56172006-12-12 23:34:33 +0000191
Devang Patele3068402006-12-21 00:16:50 +0000192 void initializeAllAnalysisInfo();
193
Devang Patel67d6a5e2006-12-19 19:46:59 +0000194protected:
Devang Patelf33f3eb2006-12-07 19:21:29 +0000195
196 /// Collection of pass managers
197 std::vector<Pass *> PassManagers;
198
Devang Patel67d6a5e2006-12-19 19:46:59 +0000199private:
200
Devang Patelaf1fca52006-12-08 23:11:43 +0000201 /// Collection of pass managers that are not directly maintained
202 /// by this pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000203 std::vector<PMDataManager *> IndirectPassManagers;
Devang Patelaf1fca52006-12-08 23:11:43 +0000204
Devang Patelf33f3eb2006-12-07 19:21:29 +0000205 // Map to keep track of last user of the analysis pass.
206 // LastUser->second is the last user of Lastuser->first.
207 std::map<Pass *, Pass *> LastUser;
Devang Patele0eb9d82006-12-07 20:51:18 +0000208
209 /// Immutable passes are managed by top level manager.
210 std::vector<ImmutablePass *> ImmutablePasses;
Devang Patelf33f3eb2006-12-07 19:21:29 +0000211};
Devang Patelffca9102006-12-15 19:39:30 +0000212
213} // End of anon namespace
Devang Patelf33f3eb2006-12-07 19:21:29 +0000214
Devang Patelf3827bc2006-12-07 19:54:15 +0000215//===----------------------------------------------------------------------===//
216// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000217
Devang Patelffca9102006-12-15 19:39:30 +0000218namespace llvm {
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000219/// PMDataManager provides the common place to manage the analysis data
220/// used by pass managers.
221class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000222public:
Devang Patel56d48ec2006-12-15 22:57:49 +0000223 PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
Devang Patelf3827bc2006-12-07 19:54:15 +0000224 initializeAnalysisInfo();
225 }
226
Devang Patelab97cf42006-12-13 00:09:23 +0000227 virtual ~PMDataManager() {
228
229 for (std::vector<Pass *>::iterator I = PassVector.begin(),
230 E = PassVector.end(); I != E; ++I)
231 delete *I;
232
233 PassVector.clear();
234 }
235
Devang Patela9844592006-11-11 01:31:05 +0000236 /// Return true IFF pass P's required analysis set does not required new
237 /// manager.
238 bool manageablePass(Pass *P);
239
Devang Patela9844592006-11-11 01:31:05 +0000240 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000241 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000242
Devang Patela9844592006-11-11 01:31:05 +0000243 /// Remove Analysis that is not preserved by the pass
244 void removeNotPreservedAnalysis(Pass *P);
245
246 /// Remove dead passes
Devang Patel200d3052006-12-13 23:50:44 +0000247 void removeDeadPasses(Pass *P, std::string &Msg);
Devang Patela9844592006-11-11 01:31:05 +0000248
Devang Patel8f677ce2006-12-07 18:47:25 +0000249 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000250 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattnerce22ca32006-12-14 18:22:14 +0000251 void addPassToManager(Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000252
Devang Patel1d6267c2006-12-07 23:05:44 +0000253 /// Initialize available analysis information.
Devang Patela6b6dcb2006-12-07 18:41:09 +0000254 void initializeAnalysisInfo() {
Devang Patel832bc072006-12-15 00:08:26 +0000255 TransferLastUses.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000256 AvailableAnalysis.clear();
257 }
258
Devang Patel1d6267c2006-12-07 23:05:44 +0000259 /// Populate RequiredPasses with the analysis pass that are required by
260 /// pass P.
261 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
262 Pass *P);
263
264 /// All Required analyses should be available to the pass as it runs! Here
265 /// we fill in the AnalysisImpls member of the pass so that it can
266 /// successfully use the getAnalysis() method to retrieve the
267 /// implementations it needs.
268 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000269
Devang Patel640c5bb2006-12-08 22:30:11 +0000270 /// Find the pass that implements Analysis AID. If desired pass is not found
271 /// then return NULL.
272 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
273
Devang Patelf3827bc2006-12-07 19:54:15 +0000274 // Access toplevel manager
275 PMTopLevelManager *getTopLevelManager() { return TPM; }
276 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
277
Devang Patel991aeba2006-12-15 20:13:01 +0000278 unsigned getDepth() const { return Depth; }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000279
Devang Patel991aeba2006-12-15 20:13:01 +0000280 // Print routines used by debug-pass
281 void dumpLastUses(Pass *P, unsigned Offset) const;
282 void dumpPassArguments() const;
283 void dumpPassInfo(Pass *P, std::string &Msg1, std::string &Msg2) const;
Devang Patelf6d1d212006-12-14 00:25:06 +0000284 void dumpAnalysisSetInfo(const char *Msg, Pass *P,
Devang Patel991aeba2006-12-15 20:13:01 +0000285 const std::vector<AnalysisID> &Set) const;
Devang Patel832bc072006-12-15 00:08:26 +0000286
287 std::vector<Pass *>& getTransferredLastUses() {
288 return TransferLastUses;
289 }
290
Devang Patelabfbe3b2006-12-16 00:56:26 +0000291 virtual unsigned getNumContainedPasses() {
292 return PassVector.size();
293 }
294
Devang Patelbc03f132006-12-07 23:55:10 +0000295protected:
296
Devang Patel832bc072006-12-15 00:08:26 +0000297 // If a FunctionPass F is the last user of ModulePass info M
Devang Patelbc03f132006-12-07 23:55:10 +0000298 // then the F's manager, not F, records itself as a last user of M.
Devang Patel832bc072006-12-15 00:08:26 +0000299 // Current pass manage is requesting parent manager to record parent
300 // manager as the last user of these TrransferLastUses passes.
301 std::vector<Pass *> TransferLastUses;
Devang Patelbc03f132006-12-07 23:55:10 +0000302
303 // Top level manager.
Devang Patelbc03f132006-12-07 23:55:10 +0000304 PMTopLevelManager *TPM;
305
Devang Patelabfbe3b2006-12-16 00:56:26 +0000306 // Collection of pass that are managed by this manager
307 std::vector<Pass *> PassVector;
308
Devang Patela9844592006-11-11 01:31:05 +0000309private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000310 // Set of available Analysis. This information is used while scheduling
311 // pass. If a pass requires an analysis which is not not available then
312 // equired analysis pass is scheduled to run before the pass itself is
313 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000314 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000315
Devang Patel4c36e6b2006-12-07 23:24:58 +0000316 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000317};
318
Devang Patel10c2ca62006-12-12 22:47:13 +0000319//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000320// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000321//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000322/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000323/// pass together and sequence them to process one basic block before
324/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000325class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
326 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000327
328public:
Devang Patel67d6a5e2006-12-19 19:46:59 +0000329 BBPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +0000330
331 /// Add a pass into a passmanager queue.
332 bool addPass(Pass *p);
333
334 /// Execute all of the passes scheduled for execution. Keep track of
335 /// whether any of the passes modifies the function, and if so, return true.
336 bool runOnFunction(Function &F);
337
Devang Patelf9d96b92006-12-07 19:57:52 +0000338 /// Pass Manager itself does not invalidate any analysis info.
339 void getAnalysisUsage(AnalysisUsage &Info) const {
340 Info.setPreservesAll();
341 }
342
Devang Patel475c4532006-12-08 00:59:05 +0000343 bool doInitialization(Module &M);
344 bool doInitialization(Function &F);
345 bool doFinalization(Module &M);
346 bool doFinalization(Function &F);
347
Devang Pateleda56172006-12-12 23:34:33 +0000348 // Print passes managed by this manager
349 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +0000350 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000351 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
352 BasicBlockPass *BP = getContainedPass(Index);
353 BP->dumpPassStructure(Offset + 1);
354 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000355 }
356 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000357
358 BasicBlockPass *getContainedPass(unsigned N) {
359 assert ( N < PassVector.size() && "Pass number out of range!");
360 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
361 return BP;
362 }
Devang Patelca58e352006-11-08 10:05:38 +0000363};
364
Devang Patel10c2ca62006-12-12 22:47:13 +0000365//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000366// FPPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000367//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000368/// FPPassManager manages BBPassManagers and FunctionPasses.
369/// It batches all function passes and basic block pass managers together and
370/// sequence them to process one function at a time before processing next
371/// function.
372
373class FPPassManager : public ModulePass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000374
Devang Patel67d6a5e2006-12-19 19:46:59 +0000375public:
376 FPPassManager(int Depth) : PMDataManager(Depth) {
377 activeBBPassManager = NULL;
Devang Patelabcd1d32006-12-07 21:27:23 +0000378 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000379
380 /// Add a pass into a passmanager queue.
381 bool addPass(Pass *p);
382
383 /// run - Execute all of the passes scheduled for execution. Keep track of
384 /// whether any of the passes modifies the module, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000385 bool runOnFunction(Function &F);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000386 bool runOnModule(Module &M);
Devang Patelca58e352006-11-08 10:05:38 +0000387
Devang Patelff631ae2006-11-15 01:27:05 +0000388 /// doInitialization - Run all of the initializers for the function passes.
389 ///
390 bool doInitialization(Module &M);
391
392 /// doFinalization - Run all of the initializers for the function passes.
393 ///
394 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000395
396 /// Pass Manager itself does not invalidate any analysis info.
397 void getAnalysisUsage(AnalysisUsage &Info) const {
398 Info.setPreservesAll();
399 }
400
Devang Pateleda56172006-12-12 23:34:33 +0000401 // Print passes managed by this manager
402 void dumpPassStructure(unsigned Offset) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000403 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000404 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
405 FunctionPass *FP = getContainedPass(Index);
406 FP->dumpPassStructure(Offset + 1);
407 dumpLastUses(FP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000408 }
409 }
410
Devang Patelabfbe3b2006-12-16 00:56:26 +0000411 FunctionPass *getContainedPass(unsigned N) {
412 assert ( N < PassVector.size() && "Pass number out of range!");
413 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
414 return FP;
415 }
416
Devang Patelca58e352006-11-08 10:05:38 +0000417private:
Devang Patel67d6a5e2006-12-19 19:46:59 +0000418 // Active Pass Manager
419 BBPassManager *activeBBPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000420};
421
Devang Patel10c2ca62006-12-12 22:47:13 +0000422//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000423// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000424//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000425/// FunctionPassManagerImpl manages FPPassManagers
426class FunctionPassManagerImpl : public Pass,
427 public PMDataManager,
428 public PMTopLevelManager {
429
430public:
431
432 FunctionPassManagerImpl(int Depth) : PMDataManager(Depth) {
433 activeManager = NULL;
434 }
435
436 /// add - Add a pass to the queue of passes to run. This passes ownership of
437 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
438 /// will be destroyed as well, so there is no need to delete the pass. This
439 /// implies that all passes MUST be allocated with 'new'.
440 void add(Pass *P) {
441 schedulePass(P);
442 }
443
444 /// run - Execute all of the passes scheduled for execution. Keep track of
445 /// whether any of the passes modifies the module, and if so, return true.
446 bool run(Function &F);
447
448 /// doInitialization - Run all of the initializers for the function passes.
449 ///
450 bool doInitialization(Module &M);
451
452 /// doFinalization - Run all of the initializers for the function passes.
453 ///
454 bool doFinalization(Module &M);
455
456 /// Pass Manager itself does not invalidate any analysis info.
457 void getAnalysisUsage(AnalysisUsage &Info) const {
458 Info.setPreservesAll();
459 }
460
461 inline void addTopLevelPass(Pass *P) {
462
463 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
464
465 // P is a immutable pass and it will be managed by this
466 // top level manager. Set up analysis resolver to connect them.
467 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
468 P->setResolver(AR);
469 initializeAnalysisImpl(P);
470 addImmutablePass(IP);
471 recordAvailableAnalysis(IP);
472 }
473 else
474 addPass(P);
475 }
476
477 FPPassManager *getContainedManager(unsigned N) {
478 assert ( N < PassManagers.size() && "Pass number out of range!");
479 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
480 return FP;
481 }
482
483 /// Add a pass into a passmanager queue.
484 bool addPass(Pass *p);
485
486private:
487
488 // Active Pass Manager
489 FPPassManager *activeManager;
490};
491
492//===----------------------------------------------------------------------===//
493// MPPassManager
494//
495/// MPPassManager manages ModulePasses and function pass managers.
Devang Patelca58e352006-11-08 10:05:38 +0000496/// It batches all Module passes passes and function pass managers together and
497/// sequence them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000498class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000499
500public:
Devang Patel67d6a5e2006-12-19 19:46:59 +0000501 MPPassManager(int Depth) : PMDataManager(Depth) {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000502 activeFunctionPassManager = NULL;
503 }
Devang Patelca58e352006-11-08 10:05:38 +0000504
505 /// Add a pass into a passmanager queue.
506 bool addPass(Pass *p);
507
508 /// run - Execute all of the passes scheduled for execution. Keep track of
509 /// whether any of the passes modifies the module, and if so, return true.
510 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000511
Devang Patelf9d96b92006-12-07 19:57:52 +0000512 /// Pass Manager itself does not invalidate any analysis info.
513 void getAnalysisUsage(AnalysisUsage &Info) const {
514 Info.setPreservesAll();
515 }
516
Devang Pateleda56172006-12-12 23:34:33 +0000517 // Print passes managed by this manager
518 void dumpPassStructure(unsigned Offset) {
519 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000520 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
521 ModulePass *MP = getContainedPass(Index);
522 MP->dumpPassStructure(Offset + 1);
523 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000524 }
525 }
526
Devang Patelabfbe3b2006-12-16 00:56:26 +0000527 ModulePass *getContainedPass(unsigned N) {
528 assert ( N < PassVector.size() && "Pass number out of range!");
529 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
530 return MP;
531 }
532
Devang Patelca58e352006-11-08 10:05:38 +0000533private:
Devang Patelca58e352006-11-08 10:05:38 +0000534 // Active Pass Manager
Devang Patel67d6a5e2006-12-19 19:46:59 +0000535 FPPassManager *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000536};
537
Devang Patel10c2ca62006-12-12 22:47:13 +0000538//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000539// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000540//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000541/// PassManagerImpl manages MPPassManagers
542class PassManagerImpl : public Pass,
Devang Patel31217af2006-12-07 21:32:57 +0000543 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000544 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000545
546public:
547
Devang Patel67d6a5e2006-12-19 19:46:59 +0000548 PassManagerImpl(int Depth) : PMDataManager(Depth) {
Devang Patelad6b7fe2006-12-12 22:57:43 +0000549 activeManager = NULL;
550 }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000551
Devang Patel376fefa2006-11-08 10:29:57 +0000552 /// add - Add a pass to the queue of passes to run. This passes ownership of
553 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
554 /// will be destroyed as well, so there is no need to delete the pass. This
555 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000556 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000557 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000558 }
Devang Patel376fefa2006-11-08 10:29:57 +0000559
560 /// run - Execute all of the passes scheduled for execution. Keep track of
561 /// whether any of the passes modifies the module, and if so, return true.
562 bool run(Module &M);
563
Devang Patelf9d96b92006-12-07 19:57:52 +0000564 /// Pass Manager itself does not invalidate any analysis info.
565 void getAnalysisUsage(AnalysisUsage &Info) const {
566 Info.setPreservesAll();
567 }
568
Devang Patelabcd1d32006-12-07 21:27:23 +0000569 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000570
Devang Patelfa971cd2006-12-08 23:57:43 +0000571 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000572
573 // P is a immutable pass and it will be managed by this
574 // top level manager. Set up analysis resolver to connect them.
575 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
576 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000577 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000578 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000579 recordAvailableAnalysis(IP);
Devang Pateld440cd92006-12-08 23:53:00 +0000580 }
Devang Patelfa971cd2006-12-08 23:57:43 +0000581 else
582 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000583 }
584
Devang Patel67d6a5e2006-12-19 19:46:59 +0000585 MPPassManager *getContainedManager(unsigned N) {
586 assert ( N < PassManagers.size() && "Pass number out of range!");
587 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
588 return MP;
589 }
590
Devang Patel376fefa2006-11-08 10:29:57 +0000591private:
592
Devang Patelde124182006-12-07 21:10:57 +0000593 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000594 bool addPass(Pass *p);
595
Devang Patel376fefa2006-11-08 10:29:57 +0000596 // Active Pass Manager
Devang Patel67d6a5e2006-12-19 19:46:59 +0000597 MPPassManager *activeManager;
Devang Patel376fefa2006-11-08 10:29:57 +0000598};
599
Devang Patelffca9102006-12-15 19:39:30 +0000600} // End of llvm namespace
601
602namespace {
603
Devang Patelb8817b92006-12-14 00:59:42 +0000604//===----------------------------------------------------------------------===//
605// TimingInfo Class - This class is used to calculate information about the
606// amount of time each pass takes to execute. This only happens when
607// -time-passes is enabled on the command line.
608//
609
Devang Patelffca9102006-12-15 19:39:30 +0000610class VISIBILITY_HIDDEN TimingInfo {
Devang Patelb8817b92006-12-14 00:59:42 +0000611 std::map<Pass*, Timer> TimingData;
612 TimerGroup TG;
613
614public:
615 // Use 'create' member to get this.
616 TimingInfo() : TG("... Pass execution timing report ...") {}
617
618 // TimingDtor - Print out information about timing information
619 ~TimingInfo() {
620 // Delete all of the timers...
621 TimingData.clear();
622 // TimerGroup is deleted next, printing the report.
623 }
624
625 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
626 // to a non null value (if the -time-passes option is enabled) or it leaves it
627 // null. It may be called multiple times.
628 static void createTheTimeInfo();
629
630 void passStarted(Pass *P) {
631
632 if (dynamic_cast<PMDataManager *>(P))
633 return;
634
635 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
636 if (I == TimingData.end())
637 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
638 I->second.startTimer();
639 }
640 void passEnded(Pass *P) {
641
642 if (dynamic_cast<PMDataManager *>(P))
643 return;
644
645 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
646 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
647 I->second.stopTimer();
648 }
649};
650
651static TimingInfo *TheTimeInfo;
652
Devang Patelffca9102006-12-15 19:39:30 +0000653} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000654
Devang Patela1514cb2006-12-07 19:39:39 +0000655//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000656// PMTopLevelManager implementation
657
658/// Set pass P as the last user of the given analysis passes.
659void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
660 Pass *P) {
661
662 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
663 E = AnalysisPasses.end(); I != E; ++I) {
664 Pass *AP = *I;
665 LastUser[AP] = P;
666 // If AP is the last user of other passes then make P last user of
667 // such passes.
668 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
669 LUE = LastUser.end(); LUI != LUE; ++LUI) {
670 if (LUI->second == AP)
671 LastUser[LUI->first] = P;
672 }
673 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000674}
675
676/// Collect passes whose last user is P
677void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
678 Pass *P) {
679 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
680 LUE = LastUser.end(); LUI != LUE; ++LUI)
681 if (LUI->second == P)
682 LastUses.push_back(LUI->first);
683}
684
685/// Schedule pass P for execution. Make sure that passes required by
686/// P are run before P is run. Update analysis info maintained by
687/// the manager. Remove dead passes. This is a recursive function.
688void PMTopLevelManager::schedulePass(Pass *P) {
689
690 // TODO : Allocate function manager for this pass, other wise required set
691 // may be inserted into previous function manager
692
693 AnalysisUsage AnUsage;
694 P->getAnalysisUsage(AnUsage);
695 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
696 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
697 E = RequiredSet.end(); I != E; ++I) {
698
699 Pass *AnalysisPass = findAnalysisPass(*I);
700 if (!AnalysisPass) {
701 // Schedule this analysis run first.
702 AnalysisPass = (*I)->createPass();
703 schedulePass(AnalysisPass);
704 }
705 }
706
707 // Now all required passes are available.
708 addTopLevelPass(P);
709}
710
711/// Find the pass that implements Analysis AID. Search immutable
712/// passes and all pass managers. If desired pass is not found
713/// then return NULL.
714Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
715
716 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000717 // Check pass managers
718 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
719 E = PassManagers.end(); P == NULL && I != E; ++I) {
720 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
721 assert(PMD && "This is not a PassManager");
722 P = PMD->findAnalysisPass(AID, false);
723 }
724
725 // Check other pass managers
726 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
727 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
728 P = (*I)->findAnalysisPass(AID, false);
729
Devang Patelafb1f3622006-12-12 22:35:25 +0000730 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
731 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
732 const PassInfo *PI = (*I)->getPassInfo();
733 if (PI == AID)
734 P = *I;
735
736 // If Pass not found then check the interfaces implemented by Immutable Pass
737 if (!P) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000738 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
739 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
740 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000741 }
742 }
743
Devang Patelafb1f3622006-12-12 22:35:25 +0000744 return P;
745}
746
Devang Pateleda56172006-12-12 23:34:33 +0000747// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000748void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000749
Devang Patel67d6a5e2006-12-19 19:46:59 +0000750 if (PassDebugging_New < Structure)
751 return;
752
Devang Pateleda56172006-12-12 23:34:33 +0000753 // Print out the immutable passes
754 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
755 ImmutablePasses[i]->dumpPassStructure(0);
756 }
757
Devang Patel991aeba2006-12-15 20:13:01 +0000758 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000759 E = PassManagers.end(); I != E; ++I)
760 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000761}
762
Devang Patel991aeba2006-12-15 20:13:01 +0000763void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000764
765 if (PassDebugging_New < Arguments)
766 return;
767
768 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000769 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000770 E = PassManagers.end(); I != E; ++I) {
771 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
772 assert(PMD && "This is not a PassManager");
773 PMD->dumpPassArguments();
774 }
775 cerr << "\n";
776}
777
Devang Patele3068402006-12-21 00:16:50 +0000778void PMTopLevelManager::initializeAllAnalysisInfo() {
779
780 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
781 E = PassManagers.end(); I != E; ++I) {
782 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
783 assert(PMD && "This is not a PassManager");
784 PMD->initializeAnalysisInfo();
785 }
786
787 // Initailize other pass managers
788 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
789 E = IndirectPassManagers.end(); I != E; ++I)
790 (*I)->initializeAnalysisInfo();
791}
792
Devang Patelafb1f3622006-12-12 22:35:25 +0000793//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000794// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000795
Devang Pateld65e9e92006-11-08 01:31:28 +0000796/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000797/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000798bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000799
Devang Patel8f677ce2006-12-07 18:47:25 +0000800 // TODO
801 // If this pass is not preserving information that is required by a
802 // pass maintained by higher level pass manager then do not insert
803 // this pass into current manager. Use new manager. For example,
804 // For example, If FunctionPass F is not preserving ModulePass Info M1
805 // that is used by another ModulePass M2 then do not insert F in
806 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000807 return true;
808}
809
Devang Patel643676c2006-11-11 01:10:19 +0000810/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000811void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000812
Devang Patel643676c2006-11-11 01:10:19 +0000813 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000814 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000815
Devang Patele9976aa2006-12-07 19:33:53 +0000816 //This pass is the current implementation of all of the interfaces it
817 //implements as well.
818 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
819 for (unsigned i = 0, e = II.size(); i != e; ++i)
820 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000821 }
822}
823
Devang Patelf68a3492006-11-07 22:35:17 +0000824/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000825void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000826 AnalysisUsage AnUsage;
827 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000828
Devang Patel2e169c32006-12-07 20:03:49 +0000829 if (AnUsage.getPreservesAll())
830 return;
831
832 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000833 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000834 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000835 std::map<AnalysisID, Pass*>::iterator Info = I++;
836 if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000837 PreservedSet.end()) {
838 // Remove this analysis
Devang Patel56d48ec2006-12-15 22:57:49 +0000839 if (!dynamic_cast<ImmutablePass*>(Info->second))
840 AvailableAnalysis.erase(Info);
841 }
Devang Patel349170f2006-11-11 01:24:55 +0000842 }
Devang Patelf68a3492006-11-07 22:35:17 +0000843}
844
Devang Patelca189262006-11-14 03:05:08 +0000845/// Remove analysis passes that are not used any longer
Devang Patel200d3052006-12-13 23:50:44 +0000846void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
Devang Patel17ad0962006-12-08 00:37:52 +0000847
848 std::vector<Pass *> DeadPasses;
849 TPM->collectLastUses(DeadPasses, P);
850
851 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
852 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000853
854 std::string Msg1 = " Freeing Pass '";
855 dumpPassInfo(*I, Msg1, Msg);
856
Devang Patelb8817b92006-12-14 00:59:42 +0000857 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Devang Patel17ad0962006-12-08 00:37:52 +0000858 (*I)->releaseMemory();
Devang Patelb8817b92006-12-14 00:59:42 +0000859 if (TheTimeInfo) TheTimeInfo->passEnded(P);
860
Devang Patel17ad0962006-12-08 00:37:52 +0000861 std::map<AnalysisID, Pass*>::iterator Pos =
862 AvailableAnalysis.find((*I)->getPassInfo());
863
Devang Patel475c4532006-12-08 00:59:05 +0000864 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000865 if (Pos != AvailableAnalysis.end())
866 AvailableAnalysis.erase(Pos);
867 }
Devang Patelca189262006-11-14 03:05:08 +0000868}
869
Devang Patel8f677ce2006-12-07 18:47:25 +0000870/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000871/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000872void PMDataManager::addPassToManager(Pass *P,
873 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000874
Devang Pateld440cd92006-12-08 23:53:00 +0000875 // This manager is going to manage pass P. Set up analysis resolver
876 // to connect them.
877 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
878 P->setResolver(AR);
879
Devang Patel90b05e02006-11-11 02:04:19 +0000880 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000881
882 // At the moment, this pass is the last user of all required passes.
883 std::vector<Pass *> LastUses;
884 std::vector<Pass *> RequiredPasses;
885 unsigned PDepth = this->getDepth();
886
887 collectRequiredAnalysisPasses(RequiredPasses, P);
888 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
889 E = RequiredPasses.end(); I != E; ++I) {
890 Pass *PRequired = *I;
891 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000892
893 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
894 RDepth = DM.getDepth();
895
Devang Patelbc03f132006-12-07 23:55:10 +0000896 if (PDepth == RDepth)
897 LastUses.push_back(PRequired);
898 else if (PDepth > RDepth) {
899 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000900 TransferLastUses.push_back(PRequired);
Devang Patelbc03f132006-12-07 23:55:10 +0000901 } else {
902 // Note : This feature is not yet implemented
903 assert (0 &&
904 "Unable to handle Pass that requires lower level Analysis pass");
905 }
906 }
907
Devang Patel832bc072006-12-15 00:08:26 +0000908 LastUses.push_back(P);
909 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000910
Devang Patel17bff0d2006-12-07 22:09:36 +0000911 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000912 // Remove the analysis not preserved by this pass
913 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000914 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000915 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000916
917 // Add pass
918 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000919}
920
Devang Patel1d6267c2006-12-07 23:05:44 +0000921/// Populate RequiredPasses with the analysis pass that are required by
922/// pass P.
923void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
924 Pass *P) {
925 AnalysisUsage AnUsage;
926 P->getAnalysisUsage(AnUsage);
927 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
928 for (std::vector<AnalysisID>::const_iterator
929 I = RequiredSet.begin(), E = RequiredSet.end();
930 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000931 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000932 assert (AnalysisPass && "Analysis pass is not available");
933 RP.push_back(AnalysisPass);
934 }
Devang Patelf58183d2006-12-12 23:09:32 +0000935
936 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
937 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
938 E = IDs.end(); I != E; ++I) {
939 Pass *AnalysisPass = findAnalysisPass(*I, true);
940 assert (AnalysisPass && "Analysis pass is not available");
941 RP.push_back(AnalysisPass);
942 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000943}
944
Devang Patel07f4f582006-11-14 21:49:36 +0000945// All Required analyses should be available to the pass as it runs! Here
946// we fill in the AnalysisImpls member of the pass so that it can
947// successfully use the getAnalysis() method to retrieve the
948// implementations it needs.
949//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000950void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000951 AnalysisUsage AnUsage;
952 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000953
954 for (std::vector<const PassInfo *>::const_iterator
955 I = AnUsage.getRequiredSet().begin(),
956 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000957 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000958 if (Impl == 0)
959 assert(0 && "Analysis used but not available!");
Devang Patel984698a2006-12-09 01:11:34 +0000960 AnalysisResolver_New *AR = P->getResolver();
961 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000962 }
963}
964
Devang Patel640c5bb2006-12-08 22:30:11 +0000965/// Find the pass that implements Analysis AID. If desired pass is not found
966/// then return NULL.
967Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
968
969 // Check if AvailableAnalysis map has one entry.
970 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
971
972 if (I != AvailableAnalysis.end())
973 return I->second;
974
975 // Search Parents through TopLevelManager
976 if (SearchParent)
977 return TPM->findAnalysisPass(AID);
978
Devang Patel9d759b82006-12-09 00:09:12 +0000979 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000980}
981
Devang Patel991aeba2006-12-15 20:13:01 +0000982// Print list of passes that are last used by P.
983void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
984
985 std::vector<Pass *> LUses;
986
987 assert (TPM && "Top Level Manager is missing");
988 TPM->collectLastUses(LUses, P);
989
990 for (std::vector<Pass *>::iterator I = LUses.begin(),
991 E = LUses.end(); I != E; ++I) {
992 llvm::cerr << "--" << std::string(Offset*2, ' ');
993 (*I)->dumpPassStructure(0);
994 }
995}
996
997void PMDataManager::dumpPassArguments() const {
998 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
999 E = PassVector.end(); I != E; ++I) {
1000 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
1001 PMD->dumpPassArguments();
1002 else
1003 if (const PassInfo *PI = (*I)->getPassInfo())
1004 if (!PI->isAnalysisGroup())
1005 cerr << " -" << PI->getPassArgument();
1006 }
1007}
1008
1009void PMDataManager:: dumpPassInfo(Pass *P, std::string &Msg1,
1010 std::string &Msg2) const {
1011 if (PassDebugging_New < Executions)
1012 return;
1013 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
1014 cerr << Msg1;
1015 cerr << P->getPassName();
1016 cerr << Msg2;
1017}
1018
1019void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
1020 const std::vector<AnalysisID> &Set)
1021 const {
1022 if (PassDebugging_New >= Details && !Set.empty()) {
1023 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
1024 for (unsigned i = 0; i != Set.size(); ++i) {
1025 if (i) cerr << ",";
1026 cerr << " " << Set[i]->getPassName();
1027 }
1028 cerr << "\n";
1029 }
1030}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001031
1032//===----------------------------------------------------------------------===//
1033// NOTE: Is this the right place to define this method ?
1034// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
1035Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
1036 return PM.findAnalysisPass(ID, dir);
1037}
1038
Devang Patela1514cb2006-12-07 19:39:39 +00001039//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001040// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001041
Devang Pateld65e9e92006-11-08 01:31:28 +00001042/// Add pass P into PassVector and return true. If this pass is not
1043/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +00001044bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001045BBPassManager::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +00001046
1047 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
1048 if (!BP)
1049 return false;
1050
Devang Patel56d48ec2006-12-15 22:57:49 +00001051 // If this pass does not preserve analysis that is used by other passes
1052 // managed by this manager than it is not a suitable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001053 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001054 return false;
1055
Chris Lattnerce22ca32006-12-14 18:22:14 +00001056 addPassToManager(BP);
Devang Patel349170f2006-11-11 01:24:55 +00001057
Devang Patel6e5a1132006-11-07 21:31:57 +00001058 return true;
1059}
1060
1061/// Execute all of the passes scheduled for execution by invoking
1062/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1063/// the function, and if so, return true.
1064bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001065BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +00001066
Devang Patel745a6962006-12-12 23:15:28 +00001067 if (F.isExternal())
1068 return false;
1069
Devang Patele9585592006-12-08 01:38:28 +00001070 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001071
Devang Patel93a197c2006-12-14 00:08:04 +00001072 std::string Msg1 = "Executing Pass '";
1073 std::string Msg3 = "' Made Modification '";
1074
Devang Patel6e5a1132006-11-07 21:31:57 +00001075 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001076 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1077 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001078 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001079 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +00001080
Devang Patel200d3052006-12-13 23:50:44 +00001081 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001082 dumpPassInfo(BP, Msg1, Msg2);
1083 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001084
Devang Patelabfbe3b2006-12-16 00:56:26 +00001085 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001086
Devang Patelabfbe3b2006-12-16 00:56:26 +00001087 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +00001088 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001089 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001090
1091 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001092 dumpPassInfo(BP, Msg3, Msg2);
1093 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001094
Devang Patelabfbe3b2006-12-16 00:56:26 +00001095 removeNotPreservedAnalysis(BP);
1096 recordAvailableAnalysis(BP);
1097 removeDeadPasses(BP, Msg2);
Devang Patel6e5a1132006-11-07 21:31:57 +00001098 }
Devang Patel56d48ec2006-12-15 22:57:49 +00001099 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001100}
1101
Devang Patel475c4532006-12-08 00:59:05 +00001102// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +00001103inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001104 bool Changed = false;
1105
Devang Patelabfbe3b2006-12-16 00:56:26 +00001106 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1107 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001108 Changed |= BP->doInitialization(M);
1109 }
1110
1111 return Changed;
1112}
1113
Devang Patel67d6a5e2006-12-19 19:46:59 +00001114inline bool BBPassManager::doFinalization(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->doFinalization(M);
1120 }
1121
1122 return Changed;
1123}
1124
Devang Patel67d6a5e2006-12-19 19:46:59 +00001125inline bool BBPassManager::doInitialization(Function &F) {
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->doInitialization(F);
1131 }
1132
1133 return Changed;
1134}
1135
Devang Patel67d6a5e2006-12-19 19:46:59 +00001136inline bool BBPassManager::doFinalization(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->doFinalization(F);
1142 }
1143
1144 return Changed;
1145}
1146
1147
Devang Patela1514cb2006-12-07 19:39:39 +00001148//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001149// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001150
Devang Patel4e12f862006-11-08 10:44:40 +00001151/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001152FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001153 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001154 // FPM is the top level manager.
1155 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001156
1157 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
1158 AnalysisResolver_New *AR = new AnalysisResolver_New(*PMD);
1159 FPM->setResolver(AR);
1160
Devang Patel1f653682006-12-08 18:57:16 +00001161 MP = P;
1162}
1163
Devang Patelb67904d2006-12-13 02:36:01 +00001164FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001165 delete FPM;
1166}
1167
Devang Patel4e12f862006-11-08 10:44:40 +00001168/// add - Add a pass to the queue of passes to run. This passes
1169/// ownership of the Pass to the PassManager. When the
1170/// PassManager_X is destroyed, the pass will be destroyed as well, so
1171/// there is no need to delete the pass. (TODO delete passes.)
1172/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001173void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001174 FPM->add(P);
1175}
1176
Devang Patel9f3083e2006-11-15 19:39:54 +00001177/// run - Execute all of the passes scheduled for execution. Keep
1178/// track of whether any of the passes modifies the function, and if
1179/// so, return true.
1180///
Devang Patelb67904d2006-12-13 02:36:01 +00001181bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001182 std::string errstr;
1183 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001184 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001185 abort();
1186 }
Devang Patel272908d2006-12-08 22:57:48 +00001187 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001188}
1189
1190
Devang Patelff631ae2006-11-15 01:27:05 +00001191/// doInitialization - Run all of the initializers for the function passes.
1192///
Devang Patelb67904d2006-12-13 02:36:01 +00001193bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001194 return FPM->doInitialization(*MP->getModule());
1195}
1196
1197/// doFinalization - Run all of the initializers for the function passes.
1198///
Devang Patelb67904d2006-12-13 02:36:01 +00001199bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001200 return FPM->doFinalization(*MP->getModule());
1201}
1202
Devang Patela1514cb2006-12-07 19:39:39 +00001203//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001204// FunctionPassManagerImpl implementation
1205//
1206/// Add P into active pass manager or use new module pass manager to
1207/// manage it.
1208bool FunctionPassManagerImpl::addPass(Pass *P) {
1209
1210 if (!activeManager || !activeManager->addPass(P)) {
1211 activeManager = new FPPassManager(getDepth() + 1);
1212 // Inherit top level manager
1213 activeManager->setTopLevelManager(this->getTopLevelManager());
1214
1215 // This top level manager is going to manage activeManager.
1216 // Set up analysis resolver to connect them.
1217 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1218 activeManager->setResolver(AR);
1219
1220 addPassManager(activeManager);
1221 return activeManager->addPass(P);
1222 }
1223 return true;
1224}
1225
1226inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1227 bool Changed = false;
1228
1229 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1230 FPPassManager *FP = getContainedManager(Index);
1231 Changed |= FP->doInitialization(M);
1232 }
1233
1234 return Changed;
1235}
1236
1237inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1238 bool Changed = false;
1239
1240 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1241 FPPassManager *FP = getContainedManager(Index);
1242 Changed |= FP->doFinalization(M);
1243 }
1244
1245 return Changed;
1246}
1247
1248// Execute all the passes managed by this top level manager.
1249// Return true if any function is modified by a pass.
1250bool FunctionPassManagerImpl::run(Function &F) {
1251
1252 bool Changed = false;
1253
1254 TimingInfo::createTheTimeInfo();
1255
1256 dumpArguments();
1257 dumpPasses();
1258
Devang Patele3068402006-12-21 00:16:50 +00001259 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001260 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1261 FPPassManager *FP = getContainedManager(Index);
1262 Changed |= FP->runOnFunction(F);
1263 }
1264 return Changed;
1265}
1266
1267//===----------------------------------------------------------------------===//
1268// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001269
Devang Patel0c2012f2006-11-07 21:49:50 +00001270/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
1271/// either use it into active basic block pass manager or create new basic
1272/// block pass manager to handle pass P.
1273bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001274FPPassManager::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +00001275
Devang Patel67d6a5e2006-12-19 19:46:59 +00001276 // If P is a BasicBlockPass then use BBPassManager.
Devang Patel0c2012f2006-11-07 21:49:50 +00001277 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
1278
Devang Patel4949fe02006-12-07 22:34:21 +00001279 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +00001280
Devang Patel4949fe02006-12-07 22:34:21 +00001281 // If active manager exists then clear its analysis info.
1282 if (activeBBPassManager)
1283 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001284
Devang Patel4949fe02006-12-07 22:34:21 +00001285 // Create and add new manager
Devang Patel67d6a5e2006-12-19 19:46:59 +00001286 activeBBPassManager = new BBPassManager(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001287 // Inherit top level manager
1288 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001289
1290 // Add new manager into current manager's list.
Devang Patel90b05e02006-11-11 02:04:19 +00001291 addPassToManager(activeBBPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001292
1293 // Add new manager into top level manager's indirect passes list
1294 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
1295 assert (PMD && "Manager is not Pass Manager");
1296 TPM->addIndirectPassManager(PMD);
Devang Patel4949fe02006-12-07 22:34:21 +00001297
1298 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001299 if (!activeBBPassManager->addPass(BP))
1300 assert(0 && "Unable to add Pass");
Devang Patel832bc072006-12-15 00:08:26 +00001301
1302 // If activeBBPassManager transfered any Last Uses then handle them here.
1303 std::vector<Pass *> &TLU = activeBBPassManager->getTransferredLastUses();
1304 if (!TLU.empty())
1305 TPM->setLastUser(TLU, this);
1306
Devang Patel0c2012f2006-11-07 21:49:50 +00001307 }
Devang Patelbc03f132006-12-07 23:55:10 +00001308
Devang Patel0c2012f2006-11-07 21:49:50 +00001309 return true;
1310 }
1311
1312 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
1313 if (!FP)
1314 return false;
1315
Devang Patel56d48ec2006-12-15 22:57:49 +00001316 // If this pass does not preserve analysis that is used by other passes
1317 // managed by this manager than it is not a suitable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001318 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001319 return false;
1320
Devang Patel8cad70d2006-11-11 01:51:02 +00001321 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +00001322
1323 // If active manager exists then clear its analysis info.
1324 if (activeBBPassManager) {
1325 activeBBPassManager->initializeAnalysisInfo();
1326 activeBBPassManager = NULL;
1327 }
1328
Devang Patel0c2012f2006-11-07 21:49:50 +00001329 return true;
1330}
1331
1332/// Execute all of the passes scheduled for execution by invoking
1333/// runOnFunction method. Keep track of whether any of the passes modifies
1334/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001335bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001336
1337 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001338
1339 if (F.isExternal())
1340 return false;
1341
Devang Patel93a197c2006-12-14 00:08:04 +00001342 std::string Msg1 = "Executing Pass '";
1343 std::string Msg3 = "' Made Modification '";
1344
Devang Patelabfbe3b2006-12-16 00:56:26 +00001345 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1346 FunctionPass *FP = getContainedPass(Index);
1347
Devang Patelf6d1d212006-12-14 00:25:06 +00001348 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001349 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001350
Devang Patel200d3052006-12-13 23:50:44 +00001351 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001352 dumpPassInfo(FP, Msg1, Msg2);
1353 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001354
Devang Patelabfbe3b2006-12-16 00:56:26 +00001355 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001356
Devang Patelabfbe3b2006-12-16 00:56:26 +00001357 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001358 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001359 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001360
1361 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001362 dumpPassInfo(FP, Msg3, Msg2);
1363 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001364
Devang Patelabfbe3b2006-12-16 00:56:26 +00001365 removeNotPreservedAnalysis(FP);
1366 recordAvailableAnalysis(FP);
1367 removeDeadPasses(FP, Msg2);
Devang Patel9f3083e2006-11-15 19:39:54 +00001368 }
1369 return Changed;
1370}
1371
Devang Patel67d6a5e2006-12-19 19:46:59 +00001372bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001373
Devang Patel67d6a5e2006-12-19 19:46:59 +00001374 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001375
1376 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1377 this->runOnFunction(*I);
1378
1379 return Changed |= doFinalization(M);
1380}
1381
1382inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001383 bool Changed = false;
1384
Devang Patelabfbe3b2006-12-16 00:56:26 +00001385 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1386 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001387 Changed |= FP->doInitialization(M);
1388 }
1389
1390 return Changed;
1391}
1392
Devang Patel67d6a5e2006-12-19 19:46:59 +00001393inline bool FPPassManager::doFinalization(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->doFinalization(M);
1399 }
1400
Devang Patelff631ae2006-11-15 01:27:05 +00001401 return Changed;
1402}
1403
Devang Patela1514cb2006-12-07 19:39:39 +00001404//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001405// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001406
1407/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel67d6a5e2006-12-19 19:46:59 +00001408/// then use FPPassManager to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +00001409/// is not manageable by this manager.
1410bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001411MPPassManager::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +00001412
1413 // If P is FunctionPass then use function pass maanager.
1414 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1415
Devang Patel640c5bb2006-12-08 22:30:11 +00001416 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +00001417
Devang Patel4949fe02006-12-07 22:34:21 +00001418 // If active manager exists then clear its analysis info.
1419 if (activeFunctionPassManager)
1420 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001421
Devang Patel4949fe02006-12-07 22:34:21 +00001422 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001423 activeFunctionPassManager =
Devang Patel67d6a5e2006-12-19 19:46:59 +00001424 new FPPassManager(getDepth() + 1);
Devang Patelafb1f3622006-12-12 22:35:25 +00001425
1426 // Add new manager into current manager's list
Devang Patel90b05e02006-11-11 02:04:19 +00001427 addPassToManager(activeFunctionPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001428
Devang Patel9c6290c2006-12-12 22:02:16 +00001429 // Inherit top level manager
1430 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001431
1432 // Add new manager into top level manager's indirect passes list
Chris Lattnerf0f611a2006-12-13 21:56:10 +00001433 PMDataManager *PMD =
1434 dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1435 assert(PMD && "Manager is not Pass Manager");
Devang Patelafb1f3622006-12-12 22:35:25 +00001436 TPM->addIndirectPassManager(PMD);
Devang Patelaf1fca52006-12-08 23:11:43 +00001437
Devang Patel4949fe02006-12-07 22:34:21 +00001438 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001439 if (!activeFunctionPassManager->addPass(FP))
1440 assert(0 && "Unable to add pass");
Devang Patelbc03f132006-12-07 23:55:10 +00001441
Devang Patel832bc072006-12-15 00:08:26 +00001442 // If activeFunctionPassManager transfered any Last Uses then
1443 // handle them here.
1444 std::vector<Pass *> &TLU =
1445 activeFunctionPassManager->getTransferredLastUses();
1446 if (!TLU.empty())
1447 TPM->setLastUser(TLU, this);
1448 }
Devang Patelbc03f132006-12-07 23:55:10 +00001449
Devang Patel05e1a972006-11-07 22:03:15 +00001450 return true;
1451 }
1452
1453 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1454 if (!MP)
1455 return false;
1456
Devang Patel56d48ec2006-12-15 22:57:49 +00001457 // If this pass does not preserve analysis that is used by other passes
1458 // managed by this manager than it is not a suitable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001459 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001460 return false;
1461
Devang Patel8cad70d2006-11-11 01:51:02 +00001462 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +00001463 // If active manager exists then clear its analysis info.
1464 if (activeFunctionPassManager) {
1465 activeFunctionPassManager->initializeAnalysisInfo();
1466 activeFunctionPassManager = NULL;
1467 }
1468
Devang Patel05e1a972006-11-07 22:03:15 +00001469 return true;
1470}
1471
1472
1473/// Execute all of the passes scheduled for execution by invoking
1474/// runOnModule method. Keep track of whether any of the passes modifies
1475/// the module, and if so, return true.
1476bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001477MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001478 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001479
Devang Patel93a197c2006-12-14 00:08:04 +00001480 std::string Msg1 = "Executing Pass '";
1481 std::string Msg3 = "' Made Modification '";
1482
Devang Patelabfbe3b2006-12-16 00:56:26 +00001483 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1484 ModulePass *MP = getContainedPass(Index);
1485
Devang Patelf6d1d212006-12-14 00:25:06 +00001486 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001487 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001488
Devang Patel200d3052006-12-13 23:50:44 +00001489 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001490 dumpPassInfo(MP, Msg1, Msg2);
1491 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001492
Devang Patelabfbe3b2006-12-16 00:56:26 +00001493 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001494
Devang Patelabfbe3b2006-12-16 00:56:26 +00001495 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001496 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001497 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001498
1499 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001500 dumpPassInfo(MP, Msg3, Msg2);
1501 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001502
Devang Patelabfbe3b2006-12-16 00:56:26 +00001503 removeNotPreservedAnalysis(MP);
1504 recordAvailableAnalysis(MP);
1505 removeDeadPasses(MP, Msg2);
Devang Patel05e1a972006-11-07 22:03:15 +00001506 }
1507 return Changed;
1508}
1509
Devang Patela1514cb2006-12-07 19:39:39 +00001510//===----------------------------------------------------------------------===//
1511// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001512//
Devang Patelc290c8a2006-11-07 22:23:34 +00001513/// Add P into active pass manager or use new module pass manager to
1514/// manage it.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001515bool PassManagerImpl::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001516
Devang Patel6c9f5482006-11-11 00:42:16 +00001517 if (!activeManager || !activeManager->addPass(P)) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001518 activeManager = new MPPassManager(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001519 // Inherit top level manager
1520 activeManager->setTopLevelManager(this->getTopLevelManager());
Devang Pateld440cd92006-12-08 23:53:00 +00001521
1522 // This top level manager is going to manage activeManager.
1523 // Set up analysis resolver to connect them.
1524 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1525 activeManager->setResolver(AR);
1526
Devang Patel5bbeb492006-12-08 22:47:25 +00001527 addPassManager(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001528 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001529 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001530 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001531}
1532
1533/// run - Execute all of the passes scheduled for execution. Keep track of
1534/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001535bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001536
Devang Patelc290c8a2006-11-07 22:23:34 +00001537 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001538
Devang Patelb8817b92006-12-14 00:59:42 +00001539 TimingInfo::createTheTimeInfo();
1540
Devang Patelcfd70c42006-12-13 22:10:00 +00001541 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001542 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001543
Devang Patele3068402006-12-21 00:16:50 +00001544 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001545 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1546 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001547 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001548 }
1549 return Changed;
1550}
Devang Patel376fefa2006-11-08 10:29:57 +00001551
Devang Patela1514cb2006-12-07 19:39:39 +00001552//===----------------------------------------------------------------------===//
1553// PassManager implementation
1554
Devang Patel376fefa2006-11-08 10:29:57 +00001555/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001556PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001557 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001558 // PM is the top level manager
1559 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001560}
1561
Devang Patelb67904d2006-12-13 02:36:01 +00001562PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001563 delete PM;
1564}
1565
Devang Patel376fefa2006-11-08 10:29:57 +00001566/// add - Add a pass to the queue of passes to run. This passes ownership of
1567/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1568/// will be destroyed as well, so there is no need to delete the pass. This
1569/// implies that all passes MUST be allocated with 'new'.
1570void
Devang Patelb67904d2006-12-13 02:36:01 +00001571PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001572 PM->add(P);
1573}
1574
1575/// run - Execute all of the passes scheduled for execution. Keep track of
1576/// whether any of the passes modifies the module, and if so, return true.
1577bool
Devang Patelb67904d2006-12-13 02:36:01 +00001578PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001579 return PM->run(M);
1580}
1581
Devang Patelb8817b92006-12-14 00:59:42 +00001582//===----------------------------------------------------------------------===//
1583// TimingInfo Class - This class is used to calculate information about the
1584// amount of time each pass takes to execute. This only happens with
1585// -time-passes is enabled on the command line.
1586//
1587bool llvm::TimePassesIsEnabled = false;
1588static cl::opt<bool,true>
1589EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1590 cl::desc("Time each pass, printing elapsed time for each on exit"));
1591
1592// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1593// a non null value (if the -time-passes option is enabled) or it leaves it
1594// null. It may be called multiple times.
1595void TimingInfo::createTheTimeInfo() {
1596 if (!TimePassesIsEnabled || TheTimeInfo) return;
1597
1598 // Constructed the first time this is called, iff -time-passes is enabled.
1599 // This guarantees that the object will be constructed before static globals,
1600 // thus it will be destroyed before them.
1601 static ManagedStatic<TimingInfo> TTI;
1602 TheTimeInfo = &*TTI;
1603}
1604
Devang Patelb67904d2006-12-13 02:36:01 +00001605#endif