blob: a45b8f8e4cdb8b4ed23325ff30be17ed20a811f9 [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 Patelad98d232007-01-11 22:15:30 +000027class llvm::PMStack;
Devang Patel6e5a1132006-11-07 21:31:57 +000028
Devang Patel6fea2852006-12-07 18:23:30 +000029//===----------------------------------------------------------------------===//
30// Overview:
31// The Pass Manager Infrastructure manages passes. It's responsibilities are:
32//
33// o Manage optimization pass execution order
34// o Make required Analysis information available before pass P is run
35// o Release memory occupied by dead passes
36// o If Analysis information is dirtied by a pass then regenerate Analysis
37// information before it is consumed by another pass.
38//
Chris Lattnerce22ca32006-12-14 18:22:14 +000039// Pass Manager Infrastructure uses multiple pass managers. They are
Devang Patel67d6a5e2006-12-19 19:46:59 +000040// PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
Chris Lattnerce22ca32006-12-14 18:22:14 +000041// This class hierarcy uses multiple inheritance but pass managers do not derive
42// from another pass manager.
Devang Patel6fea2852006-12-07 18:23:30 +000043//
Chris Lattnerce22ca32006-12-14 18:22:14 +000044// PassManager and FunctionPassManager are two top-level pass manager that
Devang Patel6fea2852006-12-07 18:23:30 +000045// represents the external interface of this entire pass manager infrastucture.
46//
47// Important classes :
48//
49// [o] class PMTopLevelManager;
50//
51// Two top level managers, PassManager and FunctionPassManager, derive from
52// PMTopLevelManager. PMTopLevelManager manages information used by top level
53// managers such as last user info.
54//
55// [o] class PMDataManager;
56//
57// PMDataManager manages information, e.g. list of available analysis info,
58// used by a pass manager to manage execution order of passes. It also provides
59// a place to implement common pass manager APIs. All pass managers derive from
60// PMDataManager.
61//
Devang Patel67d6a5e2006-12-19 19:46:59 +000062// [o] class BBPassManager : public FunctionPass, public PMDataManager;
Devang Patel6fea2852006-12-07 18:23:30 +000063//
Devang Patel67d6a5e2006-12-19 19:46:59 +000064// BBPassManager manages BasicBlockPasses.
Devang Patel6fea2852006-12-07 18:23:30 +000065//
66// [o] class FunctionPassManager;
67//
68// This is a external interface used by JIT to manage FunctionPasses. This
69// interface relies on FunctionPassManagerImpl to do all the tasks.
70//
71// [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
72// public PMTopLevelManager;
73//
Devang Patel67d6a5e2006-12-19 19:46:59 +000074// FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
Devang Patel6fea2852006-12-07 18:23:30 +000075//
Devang Patel67d6a5e2006-12-19 19:46:59 +000076// [o] class FPPassManager : public ModulePass, public PMDataManager;
Devang Patel6fea2852006-12-07 18:23:30 +000077//
Devang Patel67d6a5e2006-12-19 19:46:59 +000078// FPPassManager manages FunctionPasses and BBPassManagers
79//
80// [o] class MPPassManager : public Pass, public PMDataManager;
81//
82// MPPassManager manages ModulePasses and FPPassManagers
Devang Patel6fea2852006-12-07 18:23:30 +000083//
84// [o] class PassManager;
85//
86// This is a external interface used by various tools to manages passes. It
87// relies on PassManagerImpl to do all the tasks.
88//
89// [o] class PassManagerImpl : public Pass, public PMDataManager,
90// public PMDTopLevelManager
91//
92// PassManagerImpl is a top level pass manager responsible for managing
Devang Patel67d6a5e2006-12-19 19:46:59 +000093// MPPassManagers.
Devang Patel6fea2852006-12-07 18:23:30 +000094//===----------------------------------------------------------------------===//
95
Devang Patelf1567a52006-12-13 20:03:48 +000096namespace llvm {
97
98//===----------------------------------------------------------------------===//
99// Pass debugging information. Often it is useful to find out what pass is
100// running when a crash occurs in a utility. When this library is compiled with
101// debugging on, a command line option (--debug-pass) is enabled that causes the
102// pass name to be printed before it executes.
103//
104
Devang Patel03fb5872006-12-13 21:13:31 +0000105// Different debug levels that can be enabled...
106enum PassDebugLevel {
107 None, Arguments, Structure, Executions, Details
108};
109
Devang Patelf1567a52006-12-13 20:03:48 +0000110static cl::opt<enum PassDebugLevel>
111PassDebugging_New("debug-pass", cl::Hidden,
112 cl::desc("Print PassManager debugging information"),
113 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +0000114 clEnumVal(None , "disable debug output"),
115 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
116 clEnumVal(Structure , "print pass structure before run()"),
117 clEnumVal(Executions, "print pass name before it is executed"),
118 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +0000119 clEnumValEnd));
120} // End of llvm namespace
121
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 Patelad98d232007-01-11 22:15:30 +0000194 // Active Pass Managers
195 PMStack activeStack;
196
Devang Patel67d6a5e2006-12-19 19:46:59 +0000197protected:
Devang Patelf33f3eb2006-12-07 19:21:29 +0000198
199 /// Collection of pass managers
200 std::vector<Pass *> PassManagers;
201
Devang Patel67d6a5e2006-12-19 19:46:59 +0000202private:
203
Devang Patelaf1fca52006-12-08 23:11:43 +0000204 /// Collection of pass managers that are not directly maintained
205 /// by this pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000206 std::vector<PMDataManager *> IndirectPassManagers;
Devang Patelaf1fca52006-12-08 23:11:43 +0000207
Devang Patelf33f3eb2006-12-07 19:21:29 +0000208 // Map to keep track of last user of the analysis pass.
209 // LastUser->second is the last user of Lastuser->first.
210 std::map<Pass *, Pass *> LastUser;
Devang Patele0eb9d82006-12-07 20:51:18 +0000211
212 /// Immutable passes are managed by top level manager.
213 std::vector<ImmutablePass *> ImmutablePasses;
Devang Patelf33f3eb2006-12-07 19:21:29 +0000214};
Devang Patelffca9102006-12-15 19:39:30 +0000215
216} // End of anon namespace
Devang Patelf33f3eb2006-12-07 19:21:29 +0000217
Devang Patelf3827bc2006-12-07 19:54:15 +0000218//===----------------------------------------------------------------------===//
219// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000220
Devang Patelffca9102006-12-15 19:39:30 +0000221namespace llvm {
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000222/// PMDataManager provides the common place to manage the analysis data
223/// used by pass managers.
224class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000225public:
Devang Patel56d48ec2006-12-15 22:57:49 +0000226 PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
Devang Patelf3827bc2006-12-07 19:54:15 +0000227 initializeAnalysisInfo();
228 }
229
Devang Patelab97cf42006-12-13 00:09:23 +0000230 virtual ~PMDataManager() {
231
232 for (std::vector<Pass *>::iterator I = PassVector.begin(),
233 E = PassVector.end(); I != E; ++I)
234 delete *I;
235
236 PassVector.clear();
237 }
238
Devang Patela9844592006-11-11 01:31:05 +0000239 /// Return true IFF pass P's required analysis set does not required new
240 /// manager.
241 bool manageablePass(Pass *P);
242
Devang Patela9844592006-11-11 01:31:05 +0000243 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000244 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000245
Devang Patela9844592006-11-11 01:31:05 +0000246 /// Remove Analysis that is not preserved by the pass
247 void removeNotPreservedAnalysis(Pass *P);
248
249 /// Remove dead passes
Devang Patel200d3052006-12-13 23:50:44 +0000250 void removeDeadPasses(Pass *P, std::string &Msg);
Devang Patela9844592006-11-11 01:31:05 +0000251
Devang Patel8f677ce2006-12-07 18:47:25 +0000252 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000253 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattnerce22ca32006-12-14 18:22:14 +0000254 void addPassToManager(Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000255
Devang Patel1d6267c2006-12-07 23:05:44 +0000256 /// Initialize available analysis information.
Devang Patela6b6dcb2006-12-07 18:41:09 +0000257 void initializeAnalysisInfo() {
Devang Patel832bc072006-12-15 00:08:26 +0000258 TransferLastUses.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000259 AvailableAnalysis.clear();
260 }
261
Devang Patel1d6267c2006-12-07 23:05:44 +0000262 /// Populate RequiredPasses with the analysis pass that are required by
263 /// pass P.
264 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
265 Pass *P);
266
267 /// All Required analyses should be available to the pass as it runs! Here
268 /// we fill in the AnalysisImpls member of the pass so that it can
269 /// successfully use the getAnalysis() method to retrieve the
270 /// implementations it needs.
271 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000272
Devang Patel640c5bb2006-12-08 22:30:11 +0000273 /// Find the pass that implements Analysis AID. If desired pass is not found
274 /// then return NULL.
275 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
276
Devang Patelf3827bc2006-12-07 19:54:15 +0000277 // Access toplevel manager
278 PMTopLevelManager *getTopLevelManager() { return TPM; }
279 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
280
Devang Patel991aeba2006-12-15 20:13:01 +0000281 unsigned getDepth() const { return Depth; }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000282
Devang Patel991aeba2006-12-15 20:13:01 +0000283 // Print routines used by debug-pass
284 void dumpLastUses(Pass *P, unsigned Offset) const;
285 void dumpPassArguments() const;
286 void dumpPassInfo(Pass *P, std::string &Msg1, std::string &Msg2) const;
Devang Patelf6d1d212006-12-14 00:25:06 +0000287 void dumpAnalysisSetInfo(const char *Msg, Pass *P,
Devang Patel991aeba2006-12-15 20:13:01 +0000288 const std::vector<AnalysisID> &Set) const;
Devang Patel832bc072006-12-15 00:08:26 +0000289
290 std::vector<Pass *>& getTransferredLastUses() {
291 return TransferLastUses;
292 }
293
Devang Patelabfbe3b2006-12-16 00:56:26 +0000294 virtual unsigned getNumContainedPasses() {
295 return PassVector.size();
296 }
297
Devang Patel3b3f8992007-01-11 01:10:25 +0000298 virtual PassManagerType getPassManagerType() {
299 assert ( 0 && "Invalid use of getPassManagerType");
300 return PMT_Unknown;
301 }
Devang Patelbc03f132006-12-07 23:55:10 +0000302protected:
303
Devang Patel832bc072006-12-15 00:08:26 +0000304 // If a FunctionPass F is the last user of ModulePass info M
Devang Patelbc03f132006-12-07 23:55:10 +0000305 // then the F's manager, not F, records itself as a last user of M.
Devang Patel832bc072006-12-15 00:08:26 +0000306 // Current pass manage is requesting parent manager to record parent
307 // manager as the last user of these TrransferLastUses passes.
308 std::vector<Pass *> TransferLastUses;
Devang Patelbc03f132006-12-07 23:55:10 +0000309
310 // Top level manager.
Devang Patelbc03f132006-12-07 23:55:10 +0000311 PMTopLevelManager *TPM;
312
Devang Patelabfbe3b2006-12-16 00:56:26 +0000313 // Collection of pass that are managed by this manager
314 std::vector<Pass *> PassVector;
315
Devang Patela9844592006-11-11 01:31:05 +0000316private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000317 // Set of available Analysis. This information is used while scheduling
318 // pass. If a pass requires an analysis which is not not available then
319 // equired analysis pass is scheduled to run before the pass itself is
320 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000321 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000322
Devang Patel4c36e6b2006-12-07 23:24:58 +0000323 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000324};
325
Devang Patel10c2ca62006-12-12 22:47:13 +0000326//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000327// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000328//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000329/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000330/// pass together and sequence them to process one basic block before
331/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000332class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
333 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000334
335public:
Devang Patel67d6a5e2006-12-19 19:46:59 +0000336 BBPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +0000337
Devang Patelca58e352006-11-08 10:05:38 +0000338 /// Execute all of the passes scheduled for execution. Keep track of
339 /// whether any of the passes modifies the function, and if so, return true.
340 bool runOnFunction(Function &F);
341
Devang Patelf9d96b92006-12-07 19:57:52 +0000342 /// Pass Manager itself does not invalidate any analysis info.
343 void getAnalysisUsage(AnalysisUsage &Info) const {
344 Info.setPreservesAll();
345 }
346
Devang Patel475c4532006-12-08 00:59:05 +0000347 bool doInitialization(Module &M);
348 bool doInitialization(Function &F);
349 bool doFinalization(Module &M);
350 bool doFinalization(Function &F);
351
Devang Pateleda56172006-12-12 23:34:33 +0000352 // Print passes managed by this manager
353 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +0000354 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000355 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
356 BasicBlockPass *BP = getContainedPass(Index);
357 BP->dumpPassStructure(Offset + 1);
358 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000359 }
360 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000361
362 BasicBlockPass *getContainedPass(unsigned N) {
363 assert ( N < PassVector.size() && "Pass number out of range!");
364 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
365 return BP;
366 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000367
368 virtual PassManagerType getPassManagerType() {
369 return PMT_BasicBlockPassManager;
370 }
Devang Patelca58e352006-11-08 10:05:38 +0000371};
372
Devang Patel10c2ca62006-12-12 22:47:13 +0000373//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000374// FPPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000375//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000376/// FPPassManager manages BBPassManagers and FunctionPasses.
377/// It batches all function passes and basic block pass managers together and
378/// sequence them to process one function at a time before processing next
379/// function.
380
381class FPPassManager : public ModulePass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000382
Devang Patel67d6a5e2006-12-19 19:46:59 +0000383public:
Devang Patel0f080042007-01-12 17:23:48 +0000384 FPPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000385
386 /// run - Execute all of the passes scheduled for execution. Keep track of
387 /// whether any of the passes modifies the module, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000388 bool runOnFunction(Function &F);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000389 bool runOnModule(Module &M);
Devang Patelca58e352006-11-08 10:05:38 +0000390
Devang Patelff631ae2006-11-15 01:27:05 +0000391 /// doInitialization - Run all of the initializers for the function passes.
392 ///
393 bool doInitialization(Module &M);
394
395 /// doFinalization - Run all of the initializers for the function passes.
396 ///
397 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000398
399 /// Pass Manager itself does not invalidate any analysis info.
400 void getAnalysisUsage(AnalysisUsage &Info) const {
401 Info.setPreservesAll();
402 }
403
Devang Pateleda56172006-12-12 23:34:33 +0000404 // Print passes managed by this manager
405 void dumpPassStructure(unsigned Offset) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000406 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000407 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
408 FunctionPass *FP = getContainedPass(Index);
409 FP->dumpPassStructure(Offset + 1);
410 dumpLastUses(FP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000411 }
412 }
413
Devang Patelabfbe3b2006-12-16 00:56:26 +0000414 FunctionPass *getContainedPass(unsigned N) {
415 assert ( N < PassVector.size() && "Pass number out of range!");
416 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
417 return FP;
418 }
419
Devang Patel3b3f8992007-01-11 01:10:25 +0000420 virtual PassManagerType getPassManagerType() {
421 return PMT_FunctionPassManager;
422 }
Devang Patelca58e352006-11-08 10:05:38 +0000423};
424
Devang Patel10c2ca62006-12-12 22:47:13 +0000425//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000426// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000427//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000428/// FunctionPassManagerImpl manages FPPassManagers
429class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000430 public PMDataManager,
431 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000432public:
433
Devang Patel0f080042007-01-12 17:23:48 +0000434 FunctionPassManagerImpl(int Depth) : PMDataManager(Depth) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000435
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.
Devang Patelb66334b2007-01-05 22:47:07 +0000467 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000468 P->setResolver(AR);
469 initializeAnalysisImpl(P);
470 addImmutablePass(IP);
471 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000472 } else {
473 // Assign manager
474 if (activeStack.empty()) {
475 FPPassManager *FPP = new FPPassManager(getDepth() + 1);
476 FPP->setTopLevelManager(this->getTopLevelManager());
477 addPassManager(FPP);
478 activeStack.push(FPP);
479 }
480 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000481 }
Devang Patel0f080042007-01-12 17:23:48 +0000482
Devang Patel67d6a5e2006-12-19 19:46:59 +0000483 }
484
485 FPPassManager *getContainedManager(unsigned N) {
486 assert ( N < PassManagers.size() && "Pass number out of range!");
487 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
488 return FP;
489 }
490
Devang Patel67d6a5e2006-12-19 19:46:59 +0000491};
492
493//===----------------------------------------------------------------------===//
494// MPPassManager
495//
496/// MPPassManager manages ModulePasses and function pass managers.
Devang Patelca58e352006-11-08 10:05:38 +0000497/// It batches all Module passes passes and function pass managers together and
498/// sequence them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000499class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000500
501public:
Devang Patel0f080042007-01-12 17:23:48 +0000502 MPPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +0000503
504 /// run - Execute all of the passes scheduled for execution. Keep track of
505 /// whether any of the passes modifies the module, and if so, return true.
506 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000507
Devang Patelf9d96b92006-12-07 19:57:52 +0000508 /// Pass Manager itself does not invalidate any analysis info.
509 void getAnalysisUsage(AnalysisUsage &Info) const {
510 Info.setPreservesAll();
511 }
512
Devang Pateleda56172006-12-12 23:34:33 +0000513 // Print passes managed by this manager
514 void dumpPassStructure(unsigned Offset) {
515 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000516 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
517 ModulePass *MP = getContainedPass(Index);
518 MP->dumpPassStructure(Offset + 1);
519 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000520 }
521 }
522
Devang Patelabfbe3b2006-12-16 00:56:26 +0000523 ModulePass *getContainedPass(unsigned N) {
524 assert ( N < PassVector.size() && "Pass number out of range!");
525 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
526 return MP;
527 }
528
Devang Patel3b3f8992007-01-11 01:10:25 +0000529 virtual PassManagerType getPassManagerType() { return PMT_ModulePassManager; }
Devang Patelca58e352006-11-08 10:05:38 +0000530};
531
Devang Patel10c2ca62006-12-12 22:47:13 +0000532//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000533// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000534//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000535/// PassManagerImpl manages MPPassManagers
536class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000537 public PMDataManager,
538 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000539
540public:
541
Devang Patel0f080042007-01-12 17:23:48 +0000542 PassManagerImpl(int Depth) : PMDataManager(Depth) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000543
Devang Patel376fefa2006-11-08 10:29:57 +0000544 /// add - Add a pass to the queue of passes to run. This passes ownership of
545 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
546 /// will be destroyed as well, so there is no need to delete the pass. This
547 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000548 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000549 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000550 }
Devang Patel376fefa2006-11-08 10:29:57 +0000551
552 /// run - Execute all of the passes scheduled for execution. Keep track of
553 /// whether any of the passes modifies the module, and if so, return true.
554 bool run(Module &M);
555
Devang Patelf9d96b92006-12-07 19:57:52 +0000556 /// Pass Manager itself does not invalidate any analysis info.
557 void getAnalysisUsage(AnalysisUsage &Info) const {
558 Info.setPreservesAll();
559 }
560
Devang Patelabcd1d32006-12-07 21:27:23 +0000561 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000562
Devang Patelfa971cd2006-12-08 23:57:43 +0000563 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000564
565 // P is a immutable pass and it will be managed by this
566 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000567 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000568 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000569 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000570 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000571 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000572 } else {
573
574 // Assign manager
575 if (activeStack.empty()) {
576 MPPassManager *MPP = new MPPassManager(getDepth() + 1);
577 MPP->setTopLevelManager(this->getTopLevelManager());
578 addPassManager(MPP);
579 activeStack.push(MPP);
580 }
581
582 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000583 }
Devang Patel0f080042007-01-12 17:23:48 +0000584
Devang Patelabcd1d32006-12-07 21:27:23 +0000585 }
586
Devang Patel67d6a5e2006-12-19 19:46:59 +0000587 MPPassManager *getContainedManager(unsigned N) {
588 assert ( N < PassManagers.size() && "Pass number out of range!");
589 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
590 return MP;
591 }
592
Devang Patel376fefa2006-11-08 10:29:57 +0000593};
594
Devang Patelffca9102006-12-15 19:39:30 +0000595} // End of llvm namespace
596
597namespace {
598
Devang Patelb8817b92006-12-14 00:59:42 +0000599//===----------------------------------------------------------------------===//
600// TimingInfo Class - This class is used to calculate information about the
601// amount of time each pass takes to execute. This only happens when
602// -time-passes is enabled on the command line.
603//
604
Devang Patelffca9102006-12-15 19:39:30 +0000605class VISIBILITY_HIDDEN TimingInfo {
Devang Patelb8817b92006-12-14 00:59:42 +0000606 std::map<Pass*, Timer> TimingData;
607 TimerGroup TG;
608
609public:
610 // Use 'create' member to get this.
611 TimingInfo() : TG("... Pass execution timing report ...") {}
612
613 // TimingDtor - Print out information about timing information
614 ~TimingInfo() {
615 // Delete all of the timers...
616 TimingData.clear();
617 // TimerGroup is deleted next, printing the report.
618 }
619
620 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
621 // to a non null value (if the -time-passes option is enabled) or it leaves it
622 // null. It may be called multiple times.
623 static void createTheTimeInfo();
624
625 void passStarted(Pass *P) {
626
627 if (dynamic_cast<PMDataManager *>(P))
628 return;
629
630 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
631 if (I == TimingData.end())
632 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
633 I->second.startTimer();
634 }
635 void passEnded(Pass *P) {
636
637 if (dynamic_cast<PMDataManager *>(P))
638 return;
639
640 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
641 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
642 I->second.stopTimer();
643 }
644};
645
646static TimingInfo *TheTimeInfo;
647
Devang Patelffca9102006-12-15 19:39:30 +0000648} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000649
Devang Patela1514cb2006-12-07 19:39:39 +0000650//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000651// PMTopLevelManager implementation
652
653/// Set pass P as the last user of the given analysis passes.
654void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
655 Pass *P) {
656
657 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
658 E = AnalysisPasses.end(); I != E; ++I) {
659 Pass *AP = *I;
660 LastUser[AP] = P;
661 // If AP is the last user of other passes then make P last user of
662 // such passes.
663 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
664 LUE = LastUser.end(); LUI != LUE; ++LUI) {
665 if (LUI->second == AP)
666 LastUser[LUI->first] = P;
667 }
668 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000669}
670
671/// Collect passes whose last user is P
672void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
673 Pass *P) {
674 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
675 LUE = LastUser.end(); LUI != LUE; ++LUI)
676 if (LUI->second == P)
677 LastUses.push_back(LUI->first);
678}
679
680/// Schedule pass P for execution. Make sure that passes required by
681/// P are run before P is run. Update analysis info maintained by
682/// the manager. Remove dead passes. This is a recursive function.
683void PMTopLevelManager::schedulePass(Pass *P) {
684
685 // TODO : Allocate function manager for this pass, other wise required set
686 // may be inserted into previous function manager
687
688 AnalysisUsage AnUsage;
689 P->getAnalysisUsage(AnUsage);
690 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
691 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
692 E = RequiredSet.end(); I != E; ++I) {
693
694 Pass *AnalysisPass = findAnalysisPass(*I);
695 if (!AnalysisPass) {
696 // Schedule this analysis run first.
697 AnalysisPass = (*I)->createPass();
698 schedulePass(AnalysisPass);
699 }
700 }
701
702 // Now all required passes are available.
703 addTopLevelPass(P);
704}
705
706/// Find the pass that implements Analysis AID. Search immutable
707/// passes and all pass managers. If desired pass is not found
708/// then return NULL.
709Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
710
711 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000712 // Check pass managers
713 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
714 E = PassManagers.end(); P == NULL && I != E; ++I) {
715 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
716 assert(PMD && "This is not a PassManager");
717 P = PMD->findAnalysisPass(AID, false);
718 }
719
720 // Check other pass managers
721 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
722 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
723 P = (*I)->findAnalysisPass(AID, false);
724
Devang Patelafb1f3622006-12-12 22:35:25 +0000725 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
726 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
727 const PassInfo *PI = (*I)->getPassInfo();
728 if (PI == AID)
729 P = *I;
730
731 // If Pass not found then check the interfaces implemented by Immutable Pass
732 if (!P) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000733 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
734 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
735 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000736 }
737 }
738
Devang Patelafb1f3622006-12-12 22:35:25 +0000739 return P;
740}
741
Devang Pateleda56172006-12-12 23:34:33 +0000742// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000743void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000744
Devang Patel67d6a5e2006-12-19 19:46:59 +0000745 if (PassDebugging_New < Structure)
746 return;
747
Devang Pateleda56172006-12-12 23:34:33 +0000748 // Print out the immutable passes
749 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
750 ImmutablePasses[i]->dumpPassStructure(0);
751 }
752
Devang Patel991aeba2006-12-15 20:13:01 +0000753 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000754 E = PassManagers.end(); I != E; ++I)
755 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000756}
757
Devang Patel991aeba2006-12-15 20:13:01 +0000758void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000759
760 if (PassDebugging_New < Arguments)
761 return;
762
763 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000764 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000765 E = PassManagers.end(); I != E; ++I) {
766 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
767 assert(PMD && "This is not a PassManager");
768 PMD->dumpPassArguments();
769 }
770 cerr << "\n";
771}
772
Devang Patele3068402006-12-21 00:16:50 +0000773void PMTopLevelManager::initializeAllAnalysisInfo() {
774
775 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
776 E = PassManagers.end(); I != E; ++I) {
777 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
778 assert(PMD && "This is not a PassManager");
779 PMD->initializeAnalysisInfo();
780 }
781
782 // Initailize other pass managers
783 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
784 E = IndirectPassManagers.end(); I != E; ++I)
785 (*I)->initializeAnalysisInfo();
786}
787
Devang Patelafb1f3622006-12-12 22:35:25 +0000788//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000789// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000790
Devang Pateld65e9e92006-11-08 01:31:28 +0000791/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000792/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000793bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000794
Devang Patel8f677ce2006-12-07 18:47:25 +0000795 // TODO
796 // If this pass is not preserving information that is required by a
797 // pass maintained by higher level pass manager then do not insert
798 // this pass into current manager. Use new manager. For example,
799 // For example, If FunctionPass F is not preserving ModulePass Info M1
800 // that is used by another ModulePass M2 then do not insert F in
801 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000802 return true;
803}
804
Devang Patel643676c2006-11-11 01:10:19 +0000805/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000806void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000807
Devang Patel643676c2006-11-11 01:10:19 +0000808 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000809 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000810
Devang Patele9976aa2006-12-07 19:33:53 +0000811 //This pass is the current implementation of all of the interfaces it
812 //implements as well.
813 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
814 for (unsigned i = 0, e = II.size(); i != e; ++i)
815 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000816 }
817}
818
Devang Patelf68a3492006-11-07 22:35:17 +0000819/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000820void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000821 AnalysisUsage AnUsage;
822 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000823
Devang Patel2e169c32006-12-07 20:03:49 +0000824 if (AnUsage.getPreservesAll())
825 return;
826
827 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000828 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000829 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000830 std::map<AnalysisID, Pass*>::iterator Info = I++;
831 if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000832 PreservedSet.end()) {
833 // Remove this analysis
Devang Patel56d48ec2006-12-15 22:57:49 +0000834 if (!dynamic_cast<ImmutablePass*>(Info->second))
835 AvailableAnalysis.erase(Info);
836 }
Devang Patel349170f2006-11-11 01:24:55 +0000837 }
Devang Patelf68a3492006-11-07 22:35:17 +0000838}
839
Devang Patelca189262006-11-14 03:05:08 +0000840/// Remove analysis passes that are not used any longer
Devang Patel200d3052006-12-13 23:50:44 +0000841void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
Devang Patel17ad0962006-12-08 00:37:52 +0000842
843 std::vector<Pass *> DeadPasses;
844 TPM->collectLastUses(DeadPasses, P);
845
846 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
847 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000848
849 std::string Msg1 = " Freeing Pass '";
850 dumpPassInfo(*I, Msg1, Msg);
851
Devang Patelb8817b92006-12-14 00:59:42 +0000852 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Devang Patel17ad0962006-12-08 00:37:52 +0000853 (*I)->releaseMemory();
Devang Patelb8817b92006-12-14 00:59:42 +0000854 if (TheTimeInfo) TheTimeInfo->passEnded(P);
855
Devang Patel17ad0962006-12-08 00:37:52 +0000856 std::map<AnalysisID, Pass*>::iterator Pos =
857 AvailableAnalysis.find((*I)->getPassInfo());
858
Devang Patel475c4532006-12-08 00:59:05 +0000859 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000860 if (Pos != AvailableAnalysis.end())
861 AvailableAnalysis.erase(Pos);
862 }
Devang Patelca189262006-11-14 03:05:08 +0000863}
864
Devang Patel8f677ce2006-12-07 18:47:25 +0000865/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000866/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000867void PMDataManager::addPassToManager(Pass *P,
868 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000869
Devang Pateld440cd92006-12-08 23:53:00 +0000870 // This manager is going to manage pass P. Set up analysis resolver
871 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000872 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000873 P->setResolver(AR);
874
Devang Patel90b05e02006-11-11 02:04:19 +0000875 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000876
877 // At the moment, this pass is the last user of all required passes.
878 std::vector<Pass *> LastUses;
879 std::vector<Pass *> RequiredPasses;
880 unsigned PDepth = this->getDepth();
881
882 collectRequiredAnalysisPasses(RequiredPasses, P);
883 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
884 E = RequiredPasses.end(); I != E; ++I) {
885 Pass *PRequired = *I;
886 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000887
888 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
889 RDepth = DM.getDepth();
890
Devang Patelbc03f132006-12-07 23:55:10 +0000891 if (PDepth == RDepth)
892 LastUses.push_back(PRequired);
893 else if (PDepth > RDepth) {
894 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000895 TransferLastUses.push_back(PRequired);
Devang Patelbc03f132006-12-07 23:55:10 +0000896 } else {
897 // Note : This feature is not yet implemented
898 assert (0 &&
899 "Unable to handle Pass that requires lower level Analysis pass");
900 }
901 }
902
Devang Patel832bc072006-12-15 00:08:26 +0000903 LastUses.push_back(P);
904 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000905
Devang Patel17bff0d2006-12-07 22:09:36 +0000906 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000907 // Remove the analysis not preserved by this pass
908 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000909 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000910 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000911
912 // Add pass
913 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000914}
915
Devang Patel1d6267c2006-12-07 23:05:44 +0000916/// Populate RequiredPasses with the analysis pass that are required by
917/// pass P.
918void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
919 Pass *P) {
920 AnalysisUsage AnUsage;
921 P->getAnalysisUsage(AnUsage);
922 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
923 for (std::vector<AnalysisID>::const_iterator
924 I = RequiredSet.begin(), E = RequiredSet.end();
925 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000926 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000927 assert (AnalysisPass && "Analysis pass is not available");
928 RP.push_back(AnalysisPass);
929 }
Devang Patelf58183d2006-12-12 23:09:32 +0000930
931 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
932 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
933 E = IDs.end(); I != E; ++I) {
934 Pass *AnalysisPass = findAnalysisPass(*I, true);
935 assert (AnalysisPass && "Analysis pass is not available");
936 RP.push_back(AnalysisPass);
937 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000938}
939
Devang Patel07f4f582006-11-14 21:49:36 +0000940// All Required analyses should be available to the pass as it runs! Here
941// we fill in the AnalysisImpls member of the pass so that it can
942// successfully use the getAnalysis() method to retrieve the
943// implementations it needs.
944//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000945void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000946 AnalysisUsage AnUsage;
947 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000948
949 for (std::vector<const PassInfo *>::const_iterator
950 I = AnUsage.getRequiredSet().begin(),
951 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000952 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000953 if (Impl == 0)
954 assert(0 && "Analysis used but not available!");
Devang Patelb66334b2007-01-05 22:47:07 +0000955 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000956 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000957 }
958}
959
Devang Patel640c5bb2006-12-08 22:30:11 +0000960/// Find the pass that implements Analysis AID. If desired pass is not found
961/// then return NULL.
962Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
963
964 // Check if AvailableAnalysis map has one entry.
965 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
966
967 if (I != AvailableAnalysis.end())
968 return I->second;
969
970 // Search Parents through TopLevelManager
971 if (SearchParent)
972 return TPM->findAnalysisPass(AID);
973
Devang Patel9d759b82006-12-09 00:09:12 +0000974 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000975}
976
Devang Patel991aeba2006-12-15 20:13:01 +0000977// Print list of passes that are last used by P.
978void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
979
980 std::vector<Pass *> LUses;
981
982 assert (TPM && "Top Level Manager is missing");
983 TPM->collectLastUses(LUses, P);
984
985 for (std::vector<Pass *>::iterator I = LUses.begin(),
986 E = LUses.end(); I != E; ++I) {
987 llvm::cerr << "--" << std::string(Offset*2, ' ');
988 (*I)->dumpPassStructure(0);
989 }
990}
991
992void PMDataManager::dumpPassArguments() const {
993 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
994 E = PassVector.end(); I != E; ++I) {
995 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
996 PMD->dumpPassArguments();
997 else
998 if (const PassInfo *PI = (*I)->getPassInfo())
999 if (!PI->isAnalysisGroup())
1000 cerr << " -" << PI->getPassArgument();
1001 }
1002}
1003
1004void PMDataManager:: dumpPassInfo(Pass *P, std::string &Msg1,
1005 std::string &Msg2) const {
1006 if (PassDebugging_New < Executions)
1007 return;
1008 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
1009 cerr << Msg1;
1010 cerr << P->getPassName();
1011 cerr << Msg2;
1012}
1013
1014void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
1015 const std::vector<AnalysisID> &Set)
1016 const {
1017 if (PassDebugging_New >= Details && !Set.empty()) {
1018 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
1019 for (unsigned i = 0; i != Set.size(); ++i) {
1020 if (i) cerr << ",";
1021 cerr << " " << Set[i]->getPassName();
1022 }
1023 cerr << "\n";
1024 }
1025}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001026
1027//===----------------------------------------------------------------------===//
1028// NOTE: Is this the right place to define this method ?
1029// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +00001030Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001031 return PM.findAnalysisPass(ID, dir);
1032}
1033
Devang Patela1514cb2006-12-07 19:39:39 +00001034//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001035// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001036
Devang Patel6e5a1132006-11-07 21:31:57 +00001037/// Execute all of the passes scheduled for execution by invoking
1038/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1039/// the function, and if so, return true.
1040bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001041BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +00001042
Devang Patel745a6962006-12-12 23:15:28 +00001043 if (F.isExternal())
1044 return false;
1045
Devang Patele9585592006-12-08 01:38:28 +00001046 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001047
Devang Patel93a197c2006-12-14 00:08:04 +00001048 std::string Msg1 = "Executing Pass '";
1049 std::string Msg3 = "' Made Modification '";
1050
Devang Patel6e5a1132006-11-07 21:31:57 +00001051 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001052 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1053 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001054 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001055 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +00001056
Devang Patel200d3052006-12-13 23:50:44 +00001057 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001058 dumpPassInfo(BP, Msg1, Msg2);
1059 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001060
Devang Patelabfbe3b2006-12-16 00:56:26 +00001061 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001062
Devang Patelabfbe3b2006-12-16 00:56:26 +00001063 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +00001064 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001065 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001066
1067 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001068 dumpPassInfo(BP, Msg3, Msg2);
1069 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001070
Devang Patelabfbe3b2006-12-16 00:56:26 +00001071 removeNotPreservedAnalysis(BP);
1072 recordAvailableAnalysis(BP);
1073 removeDeadPasses(BP, Msg2);
Devang Patel6e5a1132006-11-07 21:31:57 +00001074 }
Devang Patel56d48ec2006-12-15 22:57:49 +00001075 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001076}
1077
Devang Patel475c4532006-12-08 00:59:05 +00001078// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +00001079inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001080 bool Changed = false;
1081
Devang Patelabfbe3b2006-12-16 00:56:26 +00001082 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1083 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001084 Changed |= BP->doInitialization(M);
1085 }
1086
1087 return Changed;
1088}
1089
Devang Patel67d6a5e2006-12-19 19:46:59 +00001090inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001091 bool Changed = false;
1092
Devang Patelabfbe3b2006-12-16 00:56:26 +00001093 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1094 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001095 Changed |= BP->doFinalization(M);
1096 }
1097
1098 return Changed;
1099}
1100
Devang Patel67d6a5e2006-12-19 19:46:59 +00001101inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001102 bool Changed = false;
1103
Devang Patelabfbe3b2006-12-16 00:56:26 +00001104 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1105 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001106 Changed |= BP->doInitialization(F);
1107 }
1108
1109 return Changed;
1110}
1111
Devang Patel67d6a5e2006-12-19 19:46:59 +00001112inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001113 bool Changed = false;
1114
Devang Patelabfbe3b2006-12-16 00:56:26 +00001115 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1116 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001117 Changed |= BP->doFinalization(F);
1118 }
1119
1120 return Changed;
1121}
1122
1123
Devang Patela1514cb2006-12-07 19:39:39 +00001124//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001125// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001126
Devang Patel4e12f862006-11-08 10:44:40 +00001127/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001128FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001129 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001130 // FPM is the top level manager.
1131 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001132
1133 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelb66334b2007-01-05 22:47:07 +00001134 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patel1036b652006-12-12 23:27:37 +00001135 FPM->setResolver(AR);
1136
Devang Patel1f653682006-12-08 18:57:16 +00001137 MP = P;
1138}
1139
Devang Patelb67904d2006-12-13 02:36:01 +00001140FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001141 delete FPM;
1142}
1143
Devang Patel4e12f862006-11-08 10:44:40 +00001144/// add - Add a pass to the queue of passes to run. This passes
1145/// ownership of the Pass to the PassManager. When the
1146/// PassManager_X is destroyed, the pass will be destroyed as well, so
1147/// there is no need to delete the pass. (TODO delete passes.)
1148/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001149void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001150 FPM->add(P);
1151}
1152
Devang Patel9f3083e2006-11-15 19:39:54 +00001153/// run - Execute all of the passes scheduled for execution. Keep
1154/// track of whether any of the passes modifies the function, and if
1155/// so, return true.
1156///
Devang Patelb67904d2006-12-13 02:36:01 +00001157bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001158 std::string errstr;
1159 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001160 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001161 abort();
1162 }
Devang Patel272908d2006-12-08 22:57:48 +00001163 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001164}
1165
1166
Devang Patelff631ae2006-11-15 01:27:05 +00001167/// doInitialization - Run all of the initializers for the function passes.
1168///
Devang Patelb67904d2006-12-13 02:36:01 +00001169bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001170 return FPM->doInitialization(*MP->getModule());
1171}
1172
1173/// doFinalization - Run all of the initializers for the function passes.
1174///
Devang Patelb67904d2006-12-13 02:36:01 +00001175bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001176 return FPM->doFinalization(*MP->getModule());
1177}
1178
Devang Patela1514cb2006-12-07 19:39:39 +00001179//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001180// FunctionPassManagerImpl implementation
1181//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001182inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1183 bool Changed = false;
1184
1185 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1186 FPPassManager *FP = getContainedManager(Index);
1187 Changed |= FP->doInitialization(M);
1188 }
1189
1190 return Changed;
1191}
1192
1193inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1194 bool Changed = false;
1195
1196 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1197 FPPassManager *FP = getContainedManager(Index);
1198 Changed |= FP->doFinalization(M);
1199 }
1200
1201 return Changed;
1202}
1203
1204// Execute all the passes managed by this top level manager.
1205// Return true if any function is modified by a pass.
1206bool FunctionPassManagerImpl::run(Function &F) {
1207
1208 bool Changed = false;
1209
1210 TimingInfo::createTheTimeInfo();
1211
1212 dumpArguments();
1213 dumpPasses();
1214
Devang Patele3068402006-12-21 00:16:50 +00001215 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001216 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1217 FPPassManager *FP = getContainedManager(Index);
1218 Changed |= FP->runOnFunction(F);
1219 }
1220 return Changed;
1221}
1222
1223//===----------------------------------------------------------------------===//
1224// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001225
Devang Patel0c2012f2006-11-07 21:49:50 +00001226/// Execute all of the passes scheduled for execution by invoking
1227/// runOnFunction method. Keep track of whether any of the passes modifies
1228/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001229bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001230
1231 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001232
1233 if (F.isExternal())
1234 return false;
1235
Devang Patel93a197c2006-12-14 00:08:04 +00001236 std::string Msg1 = "Executing Pass '";
1237 std::string Msg3 = "' Made Modification '";
1238
Devang Patelabfbe3b2006-12-16 00:56:26 +00001239 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1240 FunctionPass *FP = getContainedPass(Index);
1241
Devang Patelf6d1d212006-12-14 00:25:06 +00001242 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001243 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001244
Devang Patel200d3052006-12-13 23:50:44 +00001245 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001246 dumpPassInfo(FP, Msg1, Msg2);
1247 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001248
Devang Patelabfbe3b2006-12-16 00:56:26 +00001249 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001250
Devang Patelabfbe3b2006-12-16 00:56:26 +00001251 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001252 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001253 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001254
1255 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001256 dumpPassInfo(FP, Msg3, Msg2);
1257 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001258
Devang Patelabfbe3b2006-12-16 00:56:26 +00001259 removeNotPreservedAnalysis(FP);
1260 recordAvailableAnalysis(FP);
1261 removeDeadPasses(FP, Msg2);
Devang Patel9f3083e2006-11-15 19:39:54 +00001262 }
1263 return Changed;
1264}
1265
Devang Patel67d6a5e2006-12-19 19:46:59 +00001266bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001267
Devang Patel67d6a5e2006-12-19 19:46:59 +00001268 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001269
1270 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1271 this->runOnFunction(*I);
1272
1273 return Changed |= doFinalization(M);
1274}
1275
1276inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001277 bool Changed = false;
1278
Devang Patelabfbe3b2006-12-16 00:56:26 +00001279 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1280 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001281 Changed |= FP->doInitialization(M);
1282 }
1283
1284 return Changed;
1285}
1286
Devang Patel67d6a5e2006-12-19 19:46:59 +00001287inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001288 bool Changed = false;
1289
Devang Patelabfbe3b2006-12-16 00:56:26 +00001290 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1291 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001292 Changed |= FP->doFinalization(M);
1293 }
1294
Devang Patelff631ae2006-11-15 01:27:05 +00001295 return Changed;
1296}
1297
Devang Patela1514cb2006-12-07 19:39:39 +00001298//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001299// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001300
Devang Patel05e1a972006-11-07 22:03:15 +00001301/// Execute all of the passes scheduled for execution by invoking
1302/// runOnModule method. Keep track of whether any of the passes modifies
1303/// the module, and if so, return true.
1304bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001305MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001306 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001307
Devang Patel93a197c2006-12-14 00:08:04 +00001308 std::string Msg1 = "Executing Pass '";
1309 std::string Msg3 = "' Made Modification '";
1310
Devang Patelabfbe3b2006-12-16 00:56:26 +00001311 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1312 ModulePass *MP = getContainedPass(Index);
1313
Devang Patelf6d1d212006-12-14 00:25:06 +00001314 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001315 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001316
Devang Patel200d3052006-12-13 23:50:44 +00001317 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001318 dumpPassInfo(MP, Msg1, Msg2);
1319 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001320
Devang Patelabfbe3b2006-12-16 00:56:26 +00001321 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001322
Devang Patelabfbe3b2006-12-16 00:56:26 +00001323 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001324 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001325 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001326
1327 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001328 dumpPassInfo(MP, Msg3, Msg2);
1329 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001330
Devang Patelabfbe3b2006-12-16 00:56:26 +00001331 removeNotPreservedAnalysis(MP);
1332 recordAvailableAnalysis(MP);
1333 removeDeadPasses(MP, Msg2);
Devang Patel05e1a972006-11-07 22:03:15 +00001334 }
1335 return Changed;
1336}
1337
Devang Patela1514cb2006-12-07 19:39:39 +00001338//===----------------------------------------------------------------------===//
1339// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001340//
Devang Patelc290c8a2006-11-07 22:23:34 +00001341/// run - Execute all of the passes scheduled for execution. Keep track of
1342/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001343bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001344
Devang Patelc290c8a2006-11-07 22:23:34 +00001345 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001346
Devang Patelb8817b92006-12-14 00:59:42 +00001347 TimingInfo::createTheTimeInfo();
1348
Devang Patelcfd70c42006-12-13 22:10:00 +00001349 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001350 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001351
Devang Patele3068402006-12-21 00:16:50 +00001352 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001353 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1354 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001355 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001356 }
1357 return Changed;
1358}
Devang Patel376fefa2006-11-08 10:29:57 +00001359
Devang Patela1514cb2006-12-07 19:39:39 +00001360//===----------------------------------------------------------------------===//
1361// PassManager implementation
1362
Devang Patel376fefa2006-11-08 10:29:57 +00001363/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001364PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001365 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001366 // PM is the top level manager
1367 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001368}
1369
Devang Patelb67904d2006-12-13 02:36:01 +00001370PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001371 delete PM;
1372}
1373
Devang Patel376fefa2006-11-08 10:29:57 +00001374/// add - Add a pass to the queue of passes to run. This passes ownership of
1375/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1376/// will be destroyed as well, so there is no need to delete the pass. This
1377/// implies that all passes MUST be allocated with 'new'.
1378void
Devang Patelb67904d2006-12-13 02:36:01 +00001379PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001380 PM->add(P);
1381}
1382
1383/// run - Execute all of the passes scheduled for execution. Keep track of
1384/// whether any of the passes modifies the module, and if so, return true.
1385bool
Devang Patelb67904d2006-12-13 02:36:01 +00001386PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001387 return PM->run(M);
1388}
1389
Devang Patelb8817b92006-12-14 00:59:42 +00001390//===----------------------------------------------------------------------===//
1391// TimingInfo Class - This class is used to calculate information about the
1392// amount of time each pass takes to execute. This only happens with
1393// -time-passes is enabled on the command line.
1394//
1395bool llvm::TimePassesIsEnabled = false;
1396static cl::opt<bool,true>
1397EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1398 cl::desc("Time each pass, printing elapsed time for each on exit"));
1399
1400// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1401// a non null value (if the -time-passes option is enabled) or it leaves it
1402// null. It may be called multiple times.
1403void TimingInfo::createTheTimeInfo() {
1404 if (!TimePassesIsEnabled || TheTimeInfo) return;
1405
1406 // Constructed the first time this is called, iff -time-passes is enabled.
1407 // This guarantees that the object will be constructed before static globals,
1408 // thus it will be destroyed before them.
1409 static ManagedStatic<TimingInfo> TTI;
1410 TheTimeInfo = &*TTI;
1411}
1412
Devang Patel1c56a632007-01-08 19:29:38 +00001413//===----------------------------------------------------------------------===//
1414// PMStack implementation
1415//
Devang Patelad98d232007-01-11 22:15:30 +00001416
Devang Patel1c56a632007-01-08 19:29:38 +00001417// Pop Pass Manager from the stack and clear its analysis info.
1418void PMStack::pop() {
1419
1420 PMDataManager *Top = this->top();
1421 Top->initializeAnalysisInfo();
1422
1423 S.pop_back();
1424}
1425
1426// Push PM on the stack and set its top level manager.
Devang Patel15701b52007-01-11 00:19:00 +00001427void PMStack::push(Pass *P) {
Devang Patel1c56a632007-01-08 19:29:38 +00001428
Devang Patel15701b52007-01-11 00:19:00 +00001429 PMDataManager *Top = NULL;
1430 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1431 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001432
Devang Patel15701b52007-01-11 00:19:00 +00001433 if (this->empty()) {
1434 Top = PM;
1435 }
1436 else {
1437 Top = this->top();
1438 PMTopLevelManager *TPM = Top->getTopLevelManager();
1439
1440 assert (TPM && "Unable to find top level manager");
1441 TPM->addIndirectPassManager(PM);
1442 PM->setTopLevelManager(TPM);
1443 }
1444
1445 AnalysisResolver *AR = new AnalysisResolver(*Top);
1446 P->setResolver(AR);
1447
1448 S.push_back(PM);
1449}
1450
1451// Dump content of the pass manager stack.
1452void PMStack::dump() {
1453 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1454 E = S.end(); I != E; ++I) {
1455 Pass *P = dynamic_cast<Pass *>(*I);
1456 printf ("%s ", P->getPassName());
1457 }
1458 if (!S.empty())
1459 printf ("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001460}
1461
1462// Walk Pass Manager stack and set LastUse markers if any
1463// manager is transfering this priviledge to its parent manager
1464void PMStack::handleLastUserOverflow() {
1465
1466 for(PMStack::iterator I = this->begin(), E = this->end(); I != E;) {
1467
1468 PMDataManager *Child = *I++;
1469 if (I != E) {
1470 PMDataManager *Parent = *I++;
1471 PMTopLevelManager *TPM = Parent->getTopLevelManager();
1472 std::vector<Pass *> &TLU = Child->getTransferredLastUses();
1473 if (!TLU.empty()) {
1474 Pass *P = dynamic_cast<Pass *>(Parent);
1475 TPM->setLastUser(TLU, P);
1476 }
1477 }
1478 }
1479}
1480
1481/// Find appropriate Module Pass Manager in the PM Stack and
1482/// add self into that manager.
1483void ModulePass::assignPassManager(PMStack &PMS) {
1484
Devang Patel1c56a632007-01-08 19:29:38 +00001485 // Find Module Pass Manager
1486 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001487 if (PMS.top()->getPassManagerType() > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001488 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001489 else
1490 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001491 }
Devang Patelac99eca2007-01-11 19:59:06 +00001492 MPPassManager *MPP = dynamic_cast<MPPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001493
1494 assert(MPP && "Unable to find Module Pass Manager");
Devang Patel1c56a632007-01-08 19:29:38 +00001495 MPP->addPassToManager(this);
1496}
1497
1498/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1499/// in the PM Stack and add self into that manager.
1500void FunctionPass::assignPassManager(PMStack &PMS) {
1501
Devang Patel15701b52007-01-11 00:19:00 +00001502 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001503 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001504 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1505 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001506 else
Devang Patelac99eca2007-01-11 19:59:06 +00001507 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001508 }
Devang Patelac99eca2007-01-11 19:59:06 +00001509 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001510
Devang Patel15701b52007-01-11 00:19:00 +00001511 // Create new Function Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001512 if (!FPP) {
Devang Patel1c56a632007-01-08 19:29:38 +00001513 assert(!PMS.empty() && "Unable to create Function Pass Manager");
Devang Patel1c56a632007-01-08 19:29:38 +00001514 PMDataManager *PMD = PMS.top();
Devang Patel1c56a632007-01-08 19:29:38 +00001515
Devang Patel15701b52007-01-11 00:19:00 +00001516 // [1] Create new Function Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001517 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patel15701b52007-01-11 00:19:00 +00001518
1519 // [2] Set up new manager's top level manager
1520 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1521 TPM->addIndirectPassManager(FPP);
1522
1523 // [3] Assign manager to manage this new manager. This may create
1524 // and push new managers into PMS
1525 Pass *P = dynamic_cast<Pass *>(FPP);
1526 P->assignPassManager(PMS);
1527
1528 // [4] Push new manager into PMS
Devang Patel1c56a632007-01-08 19:29:38 +00001529 PMS.push(FPP);
1530 }
1531
Devang Patel15701b52007-01-11 00:19:00 +00001532 // Assign FPP as the manager of this pass.
Devang Patel1c56a632007-01-08 19:29:38 +00001533 FPP->addPassToManager(this);
1534}
1535
1536/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
1537/// in the PM Stack and add self into that manager.
1538void BasicBlockPass::assignPassManager(PMStack &PMS) {
1539
1540 BBPassManager *BBP = NULL;
1541
Devang Patel15701b52007-01-11 00:19:00 +00001542 // Basic Pass Manager is a leaf pass manager. It does not handle
1543 // any other pass manager.
1544 if (!PMS.empty()) {
Devang Patel1c56a632007-01-08 19:29:38 +00001545 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001546 }
1547
Devang Patel15701b52007-01-11 00:19:00 +00001548 // If leaf manager is not Basic Block Pass manager then create new
1549 // basic Block Pass manager.
1550
Devang Patel1c56a632007-01-08 19:29:38 +00001551 if (!BBP) {
Devang Patel1c56a632007-01-08 19:29:38 +00001552 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
Devang Patel1c56a632007-01-08 19:29:38 +00001553 PMDataManager *PMD = PMS.top();
Devang Patel1c56a632007-01-08 19:29:38 +00001554
Devang Patel15701b52007-01-11 00:19:00 +00001555 // [1] Create new Basic Block Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001556 BBP = new BBPassManager(PMD->getDepth() + 1);
Devang Patel15701b52007-01-11 00:19:00 +00001557
1558 // [2] Set up new manager's top level manager
1559 // Basic Block Pass Manager does not live by itself
1560 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1561 TPM->addIndirectPassManager(BBP);
1562
1563 // [3] Assign manager to manage this new manager. This may create
1564 // and push new managers into PMS
1565 Pass *P = dynamic_cast<Pass *>(BBP);
1566 P->assignPassManager(PMS);
1567
1568 // [4] Push new manager into PMS
Devang Patel1c56a632007-01-08 19:29:38 +00001569 PMS.push(BBP);
1570 }
1571
Devang Patel15701b52007-01-11 00:19:00 +00001572 // Assign BBP as the manager of this pass.
Devang Patel1c56a632007-01-08 19:29:38 +00001573 BBP->addPassToManager(this);
1574}
1575
Devang Patelc6b5a552007-01-05 20:16:23 +00001576