blob: 46ad33bf96489695eaff7cfdcf6efbfa5c53740e [file] [log] [blame]
Devang Patel6e5a1132006-11-07 21:31:57 +00001//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Devang Patelca58e352006-11-08 10:05:38 +00005// This file was developed by Devang Patel and is distributed under
Devang Patel6e5a1132006-11-07 21:31:57 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVM Pass Manager infrastructure.
11//
12//===----------------------------------------------------------------------===//
13
14
15#include "llvm/PassManager.h"
Devang Patelf1567a52006-12-13 20:03:48 +000016#include "llvm/Support/CommandLine.h"
Devang Patelb8817b92006-12-14 00:59:42 +000017#include "llvm/Support/Timer.h"
Devang Patel6e5a1132006-11-07 21:31:57 +000018#include "llvm/Module.h"
Devang Patelff631ae2006-11-15 01:27:05 +000019#include "llvm/ModuleProvider.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000020#include "llvm/Support/Streams.h"
Devang Patelb8817b92006-12-14 00:59:42 +000021#include "llvm/Support/ManagedStatic.h"
Devang Patela9844592006-11-11 01:31:05 +000022#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000023#include <map>
Devang Patelffca9102006-12-15 19:39:30 +000024
Devang Patel6e5a1132006-11-07 21:31:57 +000025using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000026class llvm::PMDataManager;
Devang Patel6e5a1132006-11-07 21:31:57 +000027
Devang Patel6fea2852006-12-07 18:23:30 +000028//===----------------------------------------------------------------------===//
29// Overview:
30// The Pass Manager Infrastructure manages passes. It's responsibilities are:
31//
32// o Manage optimization pass execution order
33// o Make required Analysis information available before pass P is run
34// o Release memory occupied by dead passes
35// o If Analysis information is dirtied by a pass then regenerate Analysis
36// information before it is consumed by another pass.
37//
Chris Lattnerce22ca32006-12-14 18:22:14 +000038// Pass Manager Infrastructure uses multiple pass managers. They are
Devang Patel67d6a5e2006-12-19 19:46:59 +000039// PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
Chris Lattnerce22ca32006-12-14 18:22:14 +000040// This class hierarcy uses multiple inheritance but pass managers do not derive
41// from another pass manager.
Devang Patel6fea2852006-12-07 18:23:30 +000042//
Chris Lattnerce22ca32006-12-14 18:22:14 +000043// PassManager and FunctionPassManager are two top-level pass manager that
Devang Patel6fea2852006-12-07 18:23:30 +000044// represents the external interface of this entire pass manager infrastucture.
45//
46// Important classes :
47//
48// [o] class PMTopLevelManager;
49//
50// Two top level managers, PassManager and FunctionPassManager, derive from
51// PMTopLevelManager. PMTopLevelManager manages information used by top level
52// managers such as last user info.
53//
54// [o] class PMDataManager;
55//
56// PMDataManager manages information, e.g. list of available analysis info,
57// used by a pass manager to manage execution order of passes. It also provides
58// a place to implement common pass manager APIs. All pass managers derive from
59// PMDataManager.
60//
Devang Patel67d6a5e2006-12-19 19:46:59 +000061// [o] class BBPassManager : public FunctionPass, public PMDataManager;
Devang Patel6fea2852006-12-07 18:23:30 +000062//
Devang Patel67d6a5e2006-12-19 19:46:59 +000063// BBPassManager manages BasicBlockPasses.
Devang Patel6fea2852006-12-07 18:23:30 +000064//
65// [o] class FunctionPassManager;
66//
67// This is a external interface used by JIT to manage FunctionPasses. This
68// interface relies on FunctionPassManagerImpl to do all the tasks.
69//
70// [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
71// public PMTopLevelManager;
72//
Devang Patel67d6a5e2006-12-19 19:46:59 +000073// FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
Devang Patel6fea2852006-12-07 18:23:30 +000074//
Devang Patel67d6a5e2006-12-19 19:46:59 +000075// [o] class FPPassManager : public ModulePass, public PMDataManager;
Devang Patel6fea2852006-12-07 18:23:30 +000076//
Devang Patel67d6a5e2006-12-19 19:46:59 +000077// FPPassManager manages FunctionPasses and BBPassManagers
78//
79// [o] class MPPassManager : public Pass, public PMDataManager;
80//
81// MPPassManager manages ModulePasses and FPPassManagers
Devang Patel6fea2852006-12-07 18:23:30 +000082//
83// [o] class PassManager;
84//
85// This is a external interface used by various tools to manages passes. It
86// relies on PassManagerImpl to do all the tasks.
87//
88// [o] class PassManagerImpl : public Pass, public PMDataManager,
89// public PMDTopLevelManager
90//
91// PassManagerImpl is a top level pass manager responsible for managing
Devang Patel67d6a5e2006-12-19 19:46:59 +000092// MPPassManagers.
Devang Patel6fea2852006-12-07 18:23:30 +000093//===----------------------------------------------------------------------===//
94
Devang Patelf1567a52006-12-13 20:03:48 +000095namespace llvm {
96
97//===----------------------------------------------------------------------===//
98// Pass debugging information. Often it is useful to find out what pass is
99// running when a crash occurs in a utility. When this library is compiled with
100// debugging on, a command line option (--debug-pass) is enabled that causes the
101// pass name to be printed before it executes.
102//
103
Devang Patel03fb5872006-12-13 21:13:31 +0000104// Different debug levels that can be enabled...
105enum PassDebugLevel {
106 None, Arguments, Structure, Executions, Details
107};
108
Devang Patelf1567a52006-12-13 20:03:48 +0000109static cl::opt<enum PassDebugLevel>
110PassDebugging_New("debug-pass", cl::Hidden,
111 cl::desc("Print PassManager debugging information"),
112 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +0000113 clEnumVal(None , "disable debug output"),
114 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
115 clEnumVal(Structure , "print pass structure before run()"),
116 clEnumVal(Executions, "print pass name before it is executed"),
117 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +0000118 clEnumValEnd));
119} // End of llvm namespace
120
Devang Patelffca9102006-12-15 19:39:30 +0000121namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000122
Devang Patelf33f3eb2006-12-07 19:21:29 +0000123//===----------------------------------------------------------------------===//
124// PMTopLevelManager
125//
126/// PMTopLevelManager manages LastUser info and collects common APIs used by
127/// top level pass managers.
Devang Patelffca9102006-12-15 19:39:30 +0000128class VISIBILITY_HIDDEN PMTopLevelManager {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000129public:
130
Devang Patel67d6a5e2006-12-19 19:46:59 +0000131 virtual unsigned getNumContainedManagers() {
132 return PassManagers.size();
Devang Patelf33f3eb2006-12-07 19:21:29 +0000133 }
134
135 /// Schedule pass P for execution. Make sure that passes required by
136 /// P are run before P is run. Update analysis info maintained by
137 /// the manager. Remove dead passes. This is a recursive function.
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000138 void schedulePass(Pass *P);
Devang Patelf33f3eb2006-12-07 19:21:29 +0000139
140 /// This is implemented by top level pass manager and used by
141 /// schedulePass() to add analysis info passes that are not available.
142 virtual void addTopLevelPass(Pass *P) = 0;
143
144 /// Set pass P as the last user of the given analysis passes.
145 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
146
147 /// Collect passes whose last user is P
148 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
149
Devang Patel640c5bb2006-12-08 22:30:11 +0000150 /// Find the pass that implements Analysis AID. Search immutable
151 /// passes and all pass managers. If desired pass is not found
152 /// then return NULL.
153 Pass *findAnalysisPass(AnalysisID AID);
154
Devang Patelf33f3eb2006-12-07 19:21:29 +0000155 virtual ~PMTopLevelManager() {
Devang Patelab97cf42006-12-13 00:09:23 +0000156 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
157 E = PassManagers.end(); I != E; ++I)
158 delete *I;
159
160 for (std::vector<ImmutablePass *>::iterator
161 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
162 delete *I;
163
Devang Patelf33f3eb2006-12-07 19:21:29 +0000164 PassManagers.clear();
165 }
166
Devang Patele0eb9d82006-12-07 20:51:18 +0000167 /// Add immutable pass and initialize it.
168 inline void addImmutablePass(ImmutablePass *P) {
169 P->initializePass();
170 ImmutablePasses.push_back(P);
171 }
172
173 inline std::vector<ImmutablePass *>& getImmutablePasses() {
174 return ImmutablePasses;
175 }
176
Devang Patel5bbeb492006-12-08 22:47:25 +0000177 void addPassManager(Pass *Manager) {
178 PassManagers.push_back(Manager);
179 }
180
Devang Patelaf1fca52006-12-08 23:11:43 +0000181 // Add Manager into the list of managers that are not directly
182 // maintained by this top level pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000183 inline void addIndirectPassManager(PMDataManager *Manager) {
184 IndirectPassManagers.push_back(Manager);
Devang Patelaf1fca52006-12-08 23:11:43 +0000185 }
186
Devang Pateleda56172006-12-12 23:34:33 +0000187 // Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000188 void dumpPasses() const;
189 void dumpArguments() const;
Devang Pateleda56172006-12-12 23:34:33 +0000190
Devang Patele3068402006-12-21 00:16:50 +0000191 void initializeAllAnalysisInfo();
192
Devang Patel67d6a5e2006-12-19 19:46:59 +0000193protected:
Devang Patelf33f3eb2006-12-07 19:21:29 +0000194
195 /// Collection of pass managers
196 std::vector<Pass *> PassManagers;
197
Devang Patel67d6a5e2006-12-19 19:46:59 +0000198private:
199
Devang Patelaf1fca52006-12-08 23:11:43 +0000200 /// Collection of pass managers that are not directly maintained
201 /// by this pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000202 std::vector<PMDataManager *> IndirectPassManagers;
Devang Patelaf1fca52006-12-08 23:11:43 +0000203
Devang Patelf33f3eb2006-12-07 19:21:29 +0000204 // Map to keep track of last user of the analysis pass.
205 // LastUser->second is the last user of Lastuser->first.
206 std::map<Pass *, Pass *> LastUser;
Devang Patele0eb9d82006-12-07 20:51:18 +0000207
208 /// Immutable passes are managed by top level manager.
209 std::vector<ImmutablePass *> ImmutablePasses;
Devang Patelf33f3eb2006-12-07 19:21:29 +0000210};
Devang Patelffca9102006-12-15 19:39:30 +0000211
212} // End of anon namespace
Devang Patelf33f3eb2006-12-07 19:21:29 +0000213
Devang Patelf3827bc2006-12-07 19:54:15 +0000214//===----------------------------------------------------------------------===//
215// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000216
Devang Patelffca9102006-12-15 19:39:30 +0000217namespace llvm {
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000218/// PMDataManager provides the common place to manage the analysis data
219/// used by pass managers.
220class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000221public:
Devang Patel56d48ec2006-12-15 22:57:49 +0000222 PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
Devang Patelf3827bc2006-12-07 19:54:15 +0000223 initializeAnalysisInfo();
224 }
225
Devang Patelab97cf42006-12-13 00:09:23 +0000226 virtual ~PMDataManager() {
227
228 for (std::vector<Pass *>::iterator I = PassVector.begin(),
229 E = PassVector.end(); I != E; ++I)
230 delete *I;
231
232 PassVector.clear();
233 }
234
Devang Patela9844592006-11-11 01:31:05 +0000235 /// Return true IFF pass P's required analysis set does not required new
236 /// manager.
237 bool manageablePass(Pass *P);
238
Devang Patela9844592006-11-11 01:31:05 +0000239 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000240 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000241
Devang Patela9844592006-11-11 01:31:05 +0000242 /// Remove Analysis that is not preserved by the pass
243 void removeNotPreservedAnalysis(Pass *P);
244
245 /// Remove dead passes
Devang Patel200d3052006-12-13 23:50:44 +0000246 void removeDeadPasses(Pass *P, std::string &Msg);
Devang Patela9844592006-11-11 01:31:05 +0000247
Devang Patel8f677ce2006-12-07 18:47:25 +0000248 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000249 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattnerce22ca32006-12-14 18:22:14 +0000250 void addPassToManager(Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000251
Devang Patel1d6267c2006-12-07 23:05:44 +0000252 /// Initialize available analysis information.
Devang Patela6b6dcb2006-12-07 18:41:09 +0000253 void initializeAnalysisInfo() {
Devang Patel832bc072006-12-15 00:08:26 +0000254 TransferLastUses.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000255 AvailableAnalysis.clear();
256 }
257
Devang Patel1d6267c2006-12-07 23:05:44 +0000258 /// Populate RequiredPasses with the analysis pass that are required by
259 /// pass P.
260 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
261 Pass *P);
262
263 /// All Required analyses should be available to the pass as it runs! Here
264 /// we fill in the AnalysisImpls member of the pass so that it can
265 /// successfully use the getAnalysis() method to retrieve the
266 /// implementations it needs.
267 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000268
Devang Patel640c5bb2006-12-08 22:30:11 +0000269 /// Find the pass that implements Analysis AID. If desired pass is not found
270 /// then return NULL.
271 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
272
Devang Patelf3827bc2006-12-07 19:54:15 +0000273 // Access toplevel manager
274 PMTopLevelManager *getTopLevelManager() { return TPM; }
275 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
276
Devang Patel991aeba2006-12-15 20:13:01 +0000277 unsigned getDepth() const { return Depth; }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000278
Devang Patel991aeba2006-12-15 20:13:01 +0000279 // Print routines used by debug-pass
280 void dumpLastUses(Pass *P, unsigned Offset) const;
281 void dumpPassArguments() const;
282 void dumpPassInfo(Pass *P, std::string &Msg1, std::string &Msg2) const;
Devang Patelf6d1d212006-12-14 00:25:06 +0000283 void dumpAnalysisSetInfo(const char *Msg, Pass *P,
Devang Patel991aeba2006-12-15 20:13:01 +0000284 const std::vector<AnalysisID> &Set) const;
Devang Patel832bc072006-12-15 00:08:26 +0000285
286 std::vector<Pass *>& getTransferredLastUses() {
287 return TransferLastUses;
288 }
289
Devang Patelabfbe3b2006-12-16 00:56:26 +0000290 virtual unsigned getNumContainedPasses() {
291 return PassVector.size();
292 }
293
Devang Patelbc03f132006-12-07 23:55:10 +0000294protected:
295
Devang Patel832bc072006-12-15 00:08:26 +0000296 // If a FunctionPass F is the last user of ModulePass info M
Devang Patelbc03f132006-12-07 23:55:10 +0000297 // then the F's manager, not F, records itself as a last user of M.
Devang Patel832bc072006-12-15 00:08:26 +0000298 // Current pass manage is requesting parent manager to record parent
299 // manager as the last user of these TrransferLastUses passes.
300 std::vector<Pass *> TransferLastUses;
Devang Patelbc03f132006-12-07 23:55:10 +0000301
302 // Top level manager.
Devang Patelbc03f132006-12-07 23:55:10 +0000303 PMTopLevelManager *TPM;
304
Devang Patelabfbe3b2006-12-16 00:56:26 +0000305 // Collection of pass that are managed by this manager
306 std::vector<Pass *> PassVector;
307
Devang Patela9844592006-11-11 01:31:05 +0000308private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000309 // Set of available Analysis. This information is used while scheduling
310 // pass. If a pass requires an analysis which is not not available then
311 // equired analysis pass is scheduled to run before the pass itself is
312 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000313 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000314
Devang Patel4c36e6b2006-12-07 23:24:58 +0000315 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000316};
317
Devang Patel10c2ca62006-12-12 22:47:13 +0000318//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000319// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000320//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000321/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000322/// pass together and sequence them to process one basic block before
323/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000324class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
325 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000326
327public:
Devang Patel67d6a5e2006-12-19 19:46:59 +0000328 BBPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +0000329
330 /// Add a pass into a passmanager queue.
331 bool addPass(Pass *p);
332
333 /// Execute all of the passes scheduled for execution. Keep track of
334 /// whether any of the passes modifies the function, and if so, return true.
335 bool runOnFunction(Function &F);
336
Devang Patelf9d96b92006-12-07 19:57:52 +0000337 /// Pass Manager itself does not invalidate any analysis info.
338 void getAnalysisUsage(AnalysisUsage &Info) const {
339 Info.setPreservesAll();
340 }
341
Devang Patel475c4532006-12-08 00:59:05 +0000342 bool doInitialization(Module &M);
343 bool doInitialization(Function &F);
344 bool doFinalization(Module &M);
345 bool doFinalization(Function &F);
346
Devang Pateleda56172006-12-12 23:34:33 +0000347 // Print passes managed by this manager
348 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +0000349 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000350 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
351 BasicBlockPass *BP = getContainedPass(Index);
352 BP->dumpPassStructure(Offset + 1);
353 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000354 }
355 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000356
357 BasicBlockPass *getContainedPass(unsigned N) {
358 assert ( N < PassVector.size() && "Pass number out of range!");
359 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
360 return BP;
361 }
Devang Patelca58e352006-11-08 10:05:38 +0000362};
363
Devang Patel10c2ca62006-12-12 22:47:13 +0000364//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000365// FPPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000366//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000367/// FPPassManager manages BBPassManagers and FunctionPasses.
368/// It batches all function passes and basic block pass managers together and
369/// sequence them to process one function at a time before processing next
370/// function.
371
372class FPPassManager : public ModulePass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000373
Devang Patel67d6a5e2006-12-19 19:46:59 +0000374public:
375 FPPassManager(int Depth) : PMDataManager(Depth) {
376 activeBBPassManager = NULL;
Devang Patelabcd1d32006-12-07 21:27:23 +0000377 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000378
379 /// Add a pass into a passmanager queue.
380 bool addPass(Pass *p);
381
382 /// run - Execute all of the passes scheduled for execution. Keep track of
383 /// whether any of the passes modifies the module, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000384 bool runOnFunction(Function &F);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000385 bool runOnModule(Module &M);
Devang Patelca58e352006-11-08 10:05:38 +0000386
Devang Patelff631ae2006-11-15 01:27:05 +0000387 /// doInitialization - Run all of the initializers for the function passes.
388 ///
389 bool doInitialization(Module &M);
390
391 /// doFinalization - Run all of the initializers for the function passes.
392 ///
393 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000394
395 /// Pass Manager itself does not invalidate any analysis info.
396 void getAnalysisUsage(AnalysisUsage &Info) const {
397 Info.setPreservesAll();
398 }
399
Devang Pateleda56172006-12-12 23:34:33 +0000400 // Print passes managed by this manager
401 void dumpPassStructure(unsigned Offset) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000402 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000403 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
404 FunctionPass *FP = getContainedPass(Index);
405 FP->dumpPassStructure(Offset + 1);
406 dumpLastUses(FP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000407 }
408 }
409
Devang Patelabfbe3b2006-12-16 00:56:26 +0000410 FunctionPass *getContainedPass(unsigned N) {
411 assert ( N < PassVector.size() && "Pass number out of range!");
412 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
413 return FP;
414 }
415
Devang Patelca58e352006-11-08 10:05:38 +0000416private:
Devang Patel67d6a5e2006-12-19 19:46:59 +0000417 // Active Pass Manager
418 BBPassManager *activeBBPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000419};
420
Devang Patel10c2ca62006-12-12 22:47:13 +0000421//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000422// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000423//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000424/// FunctionPassManagerImpl manages FPPassManagers
425class FunctionPassManagerImpl : public Pass,
426 public PMDataManager,
427 public PMTopLevelManager {
428
429public:
430
431 FunctionPassManagerImpl(int Depth) : PMDataManager(Depth) {
432 activeManager = NULL;
433 }
434
435 /// add - Add a pass to the queue of passes to run. This passes ownership of
436 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
437 /// will be destroyed as well, so there is no need to delete the pass. This
438 /// implies that all passes MUST be allocated with 'new'.
439 void add(Pass *P) {
440 schedulePass(P);
441 }
442
443 /// run - Execute all of the passes scheduled for execution. Keep track of
444 /// whether any of the passes modifies the module, and if so, return true.
445 bool run(Function &F);
446
447 /// doInitialization - Run all of the initializers for the function passes.
448 ///
449 bool doInitialization(Module &M);
450
451 /// doFinalization - Run all of the initializers for the function passes.
452 ///
453 bool doFinalization(Module &M);
454
455 /// Pass Manager itself does not invalidate any analysis info.
456 void getAnalysisUsage(AnalysisUsage &Info) const {
457 Info.setPreservesAll();
458 }
459
460 inline void addTopLevelPass(Pass *P) {
461
462 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
463
464 // P is a immutable pass and it will be managed by this
465 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000466 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000467 P->setResolver(AR);
468 initializeAnalysisImpl(P);
469 addImmutablePass(IP);
470 recordAvailableAnalysis(IP);
471 }
472 else
473 addPass(P);
474 }
475
476 FPPassManager *getContainedManager(unsigned N) {
477 assert ( N < PassManagers.size() && "Pass number out of range!");
478 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
479 return FP;
480 }
481
482 /// Add a pass into a passmanager queue.
483 bool addPass(Pass *p);
484
485private:
486
487 // Active Pass Manager
488 FPPassManager *activeManager;
489};
490
491//===----------------------------------------------------------------------===//
492// MPPassManager
493//
494/// MPPassManager manages ModulePasses and function pass managers.
Devang Patelca58e352006-11-08 10:05:38 +0000495/// It batches all Module passes passes and function pass managers together and
496/// sequence them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000497class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000498
499public:
Devang Patel67d6a5e2006-12-19 19:46:59 +0000500 MPPassManager(int Depth) : PMDataManager(Depth) {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000501 activeFunctionPassManager = NULL;
502 }
Devang Patelca58e352006-11-08 10:05:38 +0000503
504 /// Add a pass into a passmanager queue.
505 bool addPass(Pass *p);
506
507 /// run - Execute all of the passes scheduled for execution. Keep track of
508 /// whether any of the passes modifies the module, and if so, return true.
509 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000510
Devang Patelf9d96b92006-12-07 19:57:52 +0000511 /// Pass Manager itself does not invalidate any analysis info.
512 void getAnalysisUsage(AnalysisUsage &Info) const {
513 Info.setPreservesAll();
514 }
515
Devang Pateleda56172006-12-12 23:34:33 +0000516 // Print passes managed by this manager
517 void dumpPassStructure(unsigned Offset) {
518 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000519 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
520 ModulePass *MP = getContainedPass(Index);
521 MP->dumpPassStructure(Offset + 1);
522 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000523 }
524 }
525
Devang Patelabfbe3b2006-12-16 00:56:26 +0000526 ModulePass *getContainedPass(unsigned N) {
527 assert ( N < PassVector.size() && "Pass number out of range!");
528 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
529 return MP;
530 }
531
Devang Patelca58e352006-11-08 10:05:38 +0000532private:
Devang Patelca58e352006-11-08 10:05:38 +0000533 // Active Pass Manager
Devang Patel67d6a5e2006-12-19 19:46:59 +0000534 FPPassManager *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000535};
536
Devang Patel10c2ca62006-12-12 22:47:13 +0000537//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000538// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000539//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000540/// PassManagerImpl manages MPPassManagers
541class PassManagerImpl : public Pass,
Devang Patel31217af2006-12-07 21:32:57 +0000542 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000543 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000544
545public:
546
Devang Patel67d6a5e2006-12-19 19:46:59 +0000547 PassManagerImpl(int Depth) : PMDataManager(Depth) {
Devang Patelad6b7fe2006-12-12 22:57:43 +0000548 activeManager = NULL;
549 }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000550
Devang Patel376fefa2006-11-08 10:29:57 +0000551 /// add - Add a pass to the queue of passes to run. This passes ownership of
552 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
553 /// will be destroyed as well, so there is no need to delete the pass. This
554 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000555 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000556 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000557 }
Devang Patel376fefa2006-11-08 10:29:57 +0000558
559 /// run - Execute all of the passes scheduled for execution. Keep track of
560 /// whether any of the passes modifies the module, and if so, return true.
561 bool run(Module &M);
562
Devang Patelf9d96b92006-12-07 19:57:52 +0000563 /// Pass Manager itself does not invalidate any analysis info.
564 void getAnalysisUsage(AnalysisUsage &Info) const {
565 Info.setPreservesAll();
566 }
567
Devang Patelabcd1d32006-12-07 21:27:23 +0000568 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000569
Devang Patelfa971cd2006-12-08 23:57:43 +0000570 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000571
572 // P is a immutable pass and it will be managed by this
573 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000574 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000575 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000576 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000577 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000578 recordAvailableAnalysis(IP);
Devang Pateld440cd92006-12-08 23:53:00 +0000579 }
Devang Patelfa971cd2006-12-08 23:57:43 +0000580 else
581 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000582 }
583
Devang Patel67d6a5e2006-12-19 19:46:59 +0000584 MPPassManager *getContainedManager(unsigned N) {
585 assert ( N < PassManagers.size() && "Pass number out of range!");
586 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
587 return MP;
588 }
589
Devang Patel376fefa2006-11-08 10:29:57 +0000590private:
591
Devang Patelde124182006-12-07 21:10:57 +0000592 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000593 bool addPass(Pass *p);
594
Devang Patel376fefa2006-11-08 10:29:57 +0000595 // Active Pass Manager
Devang Patel67d6a5e2006-12-19 19:46:59 +0000596 MPPassManager *activeManager;
Devang Patel376fefa2006-11-08 10:29:57 +0000597};
598
Devang Patelffca9102006-12-15 19:39:30 +0000599} // End of llvm namespace
600
601namespace {
602
Devang Patelb8817b92006-12-14 00:59:42 +0000603//===----------------------------------------------------------------------===//
604// TimingInfo Class - This class is used to calculate information about the
605// amount of time each pass takes to execute. This only happens when
606// -time-passes is enabled on the command line.
607//
608
Devang Patelffca9102006-12-15 19:39:30 +0000609class VISIBILITY_HIDDEN TimingInfo {
Devang Patelb8817b92006-12-14 00:59:42 +0000610 std::map<Pass*, Timer> TimingData;
611 TimerGroup TG;
612
613public:
614 // Use 'create' member to get this.
615 TimingInfo() : TG("... Pass execution timing report ...") {}
616
617 // TimingDtor - Print out information about timing information
618 ~TimingInfo() {
619 // Delete all of the timers...
620 TimingData.clear();
621 // TimerGroup is deleted next, printing the report.
622 }
623
624 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
625 // to a non null value (if the -time-passes option is enabled) or it leaves it
626 // null. It may be called multiple times.
627 static void createTheTimeInfo();
628
629 void passStarted(Pass *P) {
630
631 if (dynamic_cast<PMDataManager *>(P))
632 return;
633
634 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
635 if (I == TimingData.end())
636 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
637 I->second.startTimer();
638 }
639 void passEnded(Pass *P) {
640
641 if (dynamic_cast<PMDataManager *>(P))
642 return;
643
644 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
645 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
646 I->second.stopTimer();
647 }
648};
649
650static TimingInfo *TheTimeInfo;
651
Devang Patelffca9102006-12-15 19:39:30 +0000652} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000653
Devang Patela1514cb2006-12-07 19:39:39 +0000654//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000655// PMTopLevelManager implementation
656
657/// Set pass P as the last user of the given analysis passes.
658void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
659 Pass *P) {
660
661 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
662 E = AnalysisPasses.end(); I != E; ++I) {
663 Pass *AP = *I;
664 LastUser[AP] = P;
665 // If AP is the last user of other passes then make P last user of
666 // such passes.
667 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
668 LUE = LastUser.end(); LUI != LUE; ++LUI) {
669 if (LUI->second == AP)
670 LastUser[LUI->first] = P;
671 }
672 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000673}
674
675/// Collect passes whose last user is P
676void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
677 Pass *P) {
678 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
679 LUE = LastUser.end(); LUI != LUE; ++LUI)
680 if (LUI->second == P)
681 LastUses.push_back(LUI->first);
682}
683
684/// Schedule pass P for execution. Make sure that passes required by
685/// P are run before P is run. Update analysis info maintained by
686/// the manager. Remove dead passes. This is a recursive function.
687void PMTopLevelManager::schedulePass(Pass *P) {
688
689 // TODO : Allocate function manager for this pass, other wise required set
690 // may be inserted into previous function manager
691
692 AnalysisUsage AnUsage;
693 P->getAnalysisUsage(AnUsage);
694 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
695 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
696 E = RequiredSet.end(); I != E; ++I) {
697
698 Pass *AnalysisPass = findAnalysisPass(*I);
699 if (!AnalysisPass) {
700 // Schedule this analysis run first.
701 AnalysisPass = (*I)->createPass();
702 schedulePass(AnalysisPass);
703 }
704 }
705
706 // Now all required passes are available.
707 addTopLevelPass(P);
708}
709
710/// Find the pass that implements Analysis AID. Search immutable
711/// passes and all pass managers. If desired pass is not found
712/// then return NULL.
713Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
714
715 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000716 // Check pass managers
717 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
718 E = PassManagers.end(); P == NULL && I != E; ++I) {
719 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
720 assert(PMD && "This is not a PassManager");
721 P = PMD->findAnalysisPass(AID, false);
722 }
723
724 // Check other pass managers
725 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
726 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
727 P = (*I)->findAnalysisPass(AID, false);
728
Devang Patelafb1f3622006-12-12 22:35:25 +0000729 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
730 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
731 const PassInfo *PI = (*I)->getPassInfo();
732 if (PI == AID)
733 P = *I;
734
735 // If Pass not found then check the interfaces implemented by Immutable Pass
736 if (!P) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000737 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
738 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
739 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000740 }
741 }
742
Devang Patelafb1f3622006-12-12 22:35:25 +0000743 return P;
744}
745
Devang Pateleda56172006-12-12 23:34:33 +0000746// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000747void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000748
Devang Patel67d6a5e2006-12-19 19:46:59 +0000749 if (PassDebugging_New < Structure)
750 return;
751
Devang Pateleda56172006-12-12 23:34:33 +0000752 // Print out the immutable passes
753 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
754 ImmutablePasses[i]->dumpPassStructure(0);
755 }
756
Devang Patel991aeba2006-12-15 20:13:01 +0000757 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000758 E = PassManagers.end(); I != E; ++I)
759 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000760}
761
Devang Patel991aeba2006-12-15 20:13:01 +0000762void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000763
764 if (PassDebugging_New < Arguments)
765 return;
766
767 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000768 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000769 E = PassManagers.end(); I != E; ++I) {
770 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
771 assert(PMD && "This is not a PassManager");
772 PMD->dumpPassArguments();
773 }
774 cerr << "\n";
775}
776
Devang Patele3068402006-12-21 00:16:50 +0000777void PMTopLevelManager::initializeAllAnalysisInfo() {
778
779 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
780 E = PassManagers.end(); I != E; ++I) {
781 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
782 assert(PMD && "This is not a PassManager");
783 PMD->initializeAnalysisInfo();
784 }
785
786 // Initailize other pass managers
787 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
788 E = IndirectPassManagers.end(); I != E; ++I)
789 (*I)->initializeAnalysisInfo();
790}
791
Devang Patelafb1f3622006-12-12 22:35:25 +0000792//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000793// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000794
Devang Pateld65e9e92006-11-08 01:31:28 +0000795/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000796/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000797bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000798
Devang Patel8f677ce2006-12-07 18:47:25 +0000799 // TODO
800 // If this pass is not preserving information that is required by a
801 // pass maintained by higher level pass manager then do not insert
802 // this pass into current manager. Use new manager. For example,
803 // For example, If FunctionPass F is not preserving ModulePass Info M1
804 // that is used by another ModulePass M2 then do not insert F in
805 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000806 return true;
807}
808
Devang Patel643676c2006-11-11 01:10:19 +0000809/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000810void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000811
Devang Patel643676c2006-11-11 01:10:19 +0000812 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000813 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000814
Devang Patele9976aa2006-12-07 19:33:53 +0000815 //This pass is the current implementation of all of the interfaces it
816 //implements as well.
817 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
818 for (unsigned i = 0, e = II.size(); i != e; ++i)
819 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000820 }
821}
822
Devang Patelf68a3492006-11-07 22:35:17 +0000823/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000824void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000825 AnalysisUsage AnUsage;
826 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000827
Devang Patel2e169c32006-12-07 20:03:49 +0000828 if (AnUsage.getPreservesAll())
829 return;
830
831 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000832 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000833 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000834 std::map<AnalysisID, Pass*>::iterator Info = I++;
835 if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000836 PreservedSet.end()) {
837 // Remove this analysis
Devang Patel56d48ec2006-12-15 22:57:49 +0000838 if (!dynamic_cast<ImmutablePass*>(Info->second))
839 AvailableAnalysis.erase(Info);
840 }
Devang Patel349170f2006-11-11 01:24:55 +0000841 }
Devang Patelf68a3492006-11-07 22:35:17 +0000842}
843
Devang Patelca189262006-11-14 03:05:08 +0000844/// Remove analysis passes that are not used any longer
Devang Patel200d3052006-12-13 23:50:44 +0000845void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
Devang Patel17ad0962006-12-08 00:37:52 +0000846
847 std::vector<Pass *> DeadPasses;
848 TPM->collectLastUses(DeadPasses, P);
849
850 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
851 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000852
853 std::string Msg1 = " Freeing Pass '";
854 dumpPassInfo(*I, Msg1, Msg);
855
Devang Patelb8817b92006-12-14 00:59:42 +0000856 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Devang Patel17ad0962006-12-08 00:37:52 +0000857 (*I)->releaseMemory();
Devang Patelb8817b92006-12-14 00:59:42 +0000858 if (TheTimeInfo) TheTimeInfo->passEnded(P);
859
Devang Patel17ad0962006-12-08 00:37:52 +0000860 std::map<AnalysisID, Pass*>::iterator Pos =
861 AvailableAnalysis.find((*I)->getPassInfo());
862
Devang Patel475c4532006-12-08 00:59:05 +0000863 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000864 if (Pos != AvailableAnalysis.end())
865 AvailableAnalysis.erase(Pos);
866 }
Devang Patelca189262006-11-14 03:05:08 +0000867}
868
Devang Patel8f677ce2006-12-07 18:47:25 +0000869/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000870/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000871void PMDataManager::addPassToManager(Pass *P,
872 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000873
Devang Pateld440cd92006-12-08 23:53:00 +0000874 // This manager is going to manage pass P. Set up analysis resolver
875 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000876 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000877 P->setResolver(AR);
878
Devang Patel90b05e02006-11-11 02:04:19 +0000879 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000880
881 // At the moment, this pass is the last user of all required passes.
882 std::vector<Pass *> LastUses;
883 std::vector<Pass *> RequiredPasses;
884 unsigned PDepth = this->getDepth();
885
886 collectRequiredAnalysisPasses(RequiredPasses, P);
887 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
888 E = RequiredPasses.end(); I != E; ++I) {
889 Pass *PRequired = *I;
890 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000891
892 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
893 RDepth = DM.getDepth();
894
Devang Patelbc03f132006-12-07 23:55:10 +0000895 if (PDepth == RDepth)
896 LastUses.push_back(PRequired);
897 else if (PDepth > RDepth) {
898 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000899 TransferLastUses.push_back(PRequired);
Devang Patelbc03f132006-12-07 23:55:10 +0000900 } else {
901 // Note : This feature is not yet implemented
902 assert (0 &&
903 "Unable to handle Pass that requires lower level Analysis pass");
904 }
905 }
906
Devang Patel832bc072006-12-15 00:08:26 +0000907 LastUses.push_back(P);
908 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000909
Devang Patel17bff0d2006-12-07 22:09:36 +0000910 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000911 // Remove the analysis not preserved by this pass
912 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000913 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000914 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000915
916 // Add pass
917 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000918}
919
Devang Patel1d6267c2006-12-07 23:05:44 +0000920/// Populate RequiredPasses with the analysis pass that are required by
921/// pass P.
922void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
923 Pass *P) {
924 AnalysisUsage AnUsage;
925 P->getAnalysisUsage(AnUsage);
926 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
927 for (std::vector<AnalysisID>::const_iterator
928 I = RequiredSet.begin(), E = RequiredSet.end();
929 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000930 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000931 assert (AnalysisPass && "Analysis pass is not available");
932 RP.push_back(AnalysisPass);
933 }
Devang Patelf58183d2006-12-12 23:09:32 +0000934
935 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
936 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
937 E = IDs.end(); I != E; ++I) {
938 Pass *AnalysisPass = findAnalysisPass(*I, true);
939 assert (AnalysisPass && "Analysis pass is not available");
940 RP.push_back(AnalysisPass);
941 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000942}
943
Devang Patel07f4f582006-11-14 21:49:36 +0000944// All Required analyses should be available to the pass as it runs! Here
945// we fill in the AnalysisImpls member of the pass so that it can
946// successfully use the getAnalysis() method to retrieve the
947// implementations it needs.
948//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000949void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000950 AnalysisUsage AnUsage;
951 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000952
953 for (std::vector<const PassInfo *>::const_iterator
954 I = AnUsage.getRequiredSet().begin(),
955 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000956 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000957 if (Impl == 0)
958 assert(0 && "Analysis used but not available!");
Devang Patelb66334b2007-01-05 22:47:07 +0000959 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000960 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000961 }
962}
963
Devang Patel640c5bb2006-12-08 22:30:11 +0000964/// Find the pass that implements Analysis AID. If desired pass is not found
965/// then return NULL.
966Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
967
968 // Check if AvailableAnalysis map has one entry.
969 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
970
971 if (I != AvailableAnalysis.end())
972 return I->second;
973
974 // Search Parents through TopLevelManager
975 if (SearchParent)
976 return TPM->findAnalysisPass(AID);
977
Devang Patel9d759b82006-12-09 00:09:12 +0000978 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000979}
980
Devang Patel991aeba2006-12-15 20:13:01 +0000981// Print list of passes that are last used by P.
982void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
983
984 std::vector<Pass *> LUses;
985
986 assert (TPM && "Top Level Manager is missing");
987 TPM->collectLastUses(LUses, P);
988
989 for (std::vector<Pass *>::iterator I = LUses.begin(),
990 E = LUses.end(); I != E; ++I) {
991 llvm::cerr << "--" << std::string(Offset*2, ' ');
992 (*I)->dumpPassStructure(0);
993 }
994}
995
996void PMDataManager::dumpPassArguments() const {
997 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
998 E = PassVector.end(); I != E; ++I) {
999 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
1000 PMD->dumpPassArguments();
1001 else
1002 if (const PassInfo *PI = (*I)->getPassInfo())
1003 if (!PI->isAnalysisGroup())
1004 cerr << " -" << PI->getPassArgument();
1005 }
1006}
1007
1008void PMDataManager:: dumpPassInfo(Pass *P, std::string &Msg1,
1009 std::string &Msg2) const {
1010 if (PassDebugging_New < Executions)
1011 return;
1012 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
1013 cerr << Msg1;
1014 cerr << P->getPassName();
1015 cerr << Msg2;
1016}
1017
1018void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
1019 const std::vector<AnalysisID> &Set)
1020 const {
1021 if (PassDebugging_New >= Details && !Set.empty()) {
1022 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
1023 for (unsigned i = 0; i != Set.size(); ++i) {
1024 if (i) cerr << ",";
1025 cerr << " " << Set[i]->getPassName();
1026 }
1027 cerr << "\n";
1028 }
1029}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001030
1031//===----------------------------------------------------------------------===//
1032// NOTE: Is this the right place to define this method ?
1033// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +00001034Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001035 return PM.findAnalysisPass(ID, dir);
1036}
1037
Devang Patela1514cb2006-12-07 19:39:39 +00001038//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001039// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001040
Devang Pateld65e9e92006-11-08 01:31:28 +00001041/// Add pass P into PassVector and return true. If this pass is not
1042/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +00001043bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001044BBPassManager::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +00001045
1046 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
1047 if (!BP)
1048 return false;
1049
Devang Patel56d48ec2006-12-15 22:57:49 +00001050 // If this pass does not preserve analysis that is used by other passes
1051 // managed by this manager than it is not a suitable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001052 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001053 return false;
1054
Chris Lattnerce22ca32006-12-14 18:22:14 +00001055 addPassToManager(BP);
Devang Patel349170f2006-11-11 01:24:55 +00001056
Devang Patel6e5a1132006-11-07 21:31:57 +00001057 return true;
1058}
1059
1060/// Execute all of the passes scheduled for execution by invoking
1061/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1062/// the function, and if so, return true.
1063bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001064BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +00001065
Devang Patel745a6962006-12-12 23:15:28 +00001066 if (F.isExternal())
1067 return false;
1068
Devang Patele9585592006-12-08 01:38:28 +00001069 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001070
Devang Patel93a197c2006-12-14 00:08:04 +00001071 std::string Msg1 = "Executing Pass '";
1072 std::string Msg3 = "' Made Modification '";
1073
Devang Patel6e5a1132006-11-07 21:31:57 +00001074 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001075 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1076 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001077 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001078 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +00001079
Devang Patel200d3052006-12-13 23:50:44 +00001080 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001081 dumpPassInfo(BP, Msg1, Msg2);
1082 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001083
Devang Patelabfbe3b2006-12-16 00:56:26 +00001084 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001085
Devang Patelabfbe3b2006-12-16 00:56:26 +00001086 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +00001087 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001088 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001089
1090 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001091 dumpPassInfo(BP, Msg3, Msg2);
1092 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001093
Devang Patelabfbe3b2006-12-16 00:56:26 +00001094 removeNotPreservedAnalysis(BP);
1095 recordAvailableAnalysis(BP);
1096 removeDeadPasses(BP, Msg2);
Devang Patel6e5a1132006-11-07 21:31:57 +00001097 }
Devang Patel56d48ec2006-12-15 22:57:49 +00001098 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001099}
1100
Devang Patel475c4532006-12-08 00:59:05 +00001101// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +00001102inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001103 bool Changed = false;
1104
Devang Patelabfbe3b2006-12-16 00:56:26 +00001105 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1106 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001107 Changed |= BP->doInitialization(M);
1108 }
1109
1110 return Changed;
1111}
1112
Devang Patel67d6a5e2006-12-19 19:46:59 +00001113inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001114 bool Changed = false;
1115
Devang Patelabfbe3b2006-12-16 00:56:26 +00001116 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1117 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001118 Changed |= BP->doFinalization(M);
1119 }
1120
1121 return Changed;
1122}
1123
Devang Patel67d6a5e2006-12-19 19:46:59 +00001124inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001125 bool Changed = false;
1126
Devang Patelabfbe3b2006-12-16 00:56:26 +00001127 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1128 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001129 Changed |= BP->doInitialization(F);
1130 }
1131
1132 return Changed;
1133}
1134
Devang Patel67d6a5e2006-12-19 19:46:59 +00001135inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001136 bool Changed = false;
1137
Devang Patelabfbe3b2006-12-16 00:56:26 +00001138 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1139 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001140 Changed |= BP->doFinalization(F);
1141 }
1142
1143 return Changed;
1144}
1145
1146
Devang Patela1514cb2006-12-07 19:39:39 +00001147//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001148// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001149
Devang Patel4e12f862006-11-08 10:44:40 +00001150/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001151FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001152 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001153 // FPM is the top level manager.
1154 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001155
1156 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelb66334b2007-01-05 22:47:07 +00001157 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patel1036b652006-12-12 23:27:37 +00001158 FPM->setResolver(AR);
1159
Devang Patel1f653682006-12-08 18:57:16 +00001160 MP = P;
1161}
1162
Devang Patelb67904d2006-12-13 02:36:01 +00001163FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001164 delete FPM;
1165}
1166
Devang Patel4e12f862006-11-08 10:44:40 +00001167/// add - Add a pass to the queue of passes to run. This passes
1168/// ownership of the Pass to the PassManager. When the
1169/// PassManager_X is destroyed, the pass will be destroyed as well, so
1170/// there is no need to delete the pass. (TODO delete passes.)
1171/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001172void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001173 FPM->add(P);
1174}
1175
Devang Patel9f3083e2006-11-15 19:39:54 +00001176/// run - Execute all of the passes scheduled for execution. Keep
1177/// track of whether any of the passes modifies the function, and if
1178/// so, return true.
1179///
Devang Patelb67904d2006-12-13 02:36:01 +00001180bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001181 std::string errstr;
1182 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001183 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001184 abort();
1185 }
Devang Patel272908d2006-12-08 22:57:48 +00001186 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001187}
1188
1189
Devang Patelff631ae2006-11-15 01:27:05 +00001190/// doInitialization - Run all of the initializers for the function passes.
1191///
Devang Patelb67904d2006-12-13 02:36:01 +00001192bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001193 return FPM->doInitialization(*MP->getModule());
1194}
1195
1196/// doFinalization - Run all of the initializers for the function passes.
1197///
Devang Patelb67904d2006-12-13 02:36:01 +00001198bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001199 return FPM->doFinalization(*MP->getModule());
1200}
1201
Devang Patela1514cb2006-12-07 19:39:39 +00001202//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001203// FunctionPassManagerImpl implementation
1204//
1205/// Add P into active pass manager or use new module pass manager to
1206/// manage it.
1207bool FunctionPassManagerImpl::addPass(Pass *P) {
1208
1209 if (!activeManager || !activeManager->addPass(P)) {
1210 activeManager = new FPPassManager(getDepth() + 1);
1211 // Inherit top level manager
1212 activeManager->setTopLevelManager(this->getTopLevelManager());
1213
1214 // This top level manager is going to manage activeManager.
1215 // Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +00001216 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001217 activeManager->setResolver(AR);
1218
1219 addPassManager(activeManager);
1220 return activeManager->addPass(P);
1221 }
1222 return true;
1223}
1224
1225inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1226 bool Changed = false;
1227
1228 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1229 FPPassManager *FP = getContainedManager(Index);
1230 Changed |= FP->doInitialization(M);
1231 }
1232
1233 return Changed;
1234}
1235
1236inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1237 bool Changed = false;
1238
1239 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1240 FPPassManager *FP = getContainedManager(Index);
1241 Changed |= FP->doFinalization(M);
1242 }
1243
1244 return Changed;
1245}
1246
1247// Execute all the passes managed by this top level manager.
1248// Return true if any function is modified by a pass.
1249bool FunctionPassManagerImpl::run(Function &F) {
1250
1251 bool Changed = false;
1252
1253 TimingInfo::createTheTimeInfo();
1254
1255 dumpArguments();
1256 dumpPasses();
1257
Devang Patele3068402006-12-21 00:16:50 +00001258 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001259 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1260 FPPassManager *FP = getContainedManager(Index);
1261 Changed |= FP->runOnFunction(F);
1262 }
1263 return Changed;
1264}
1265
1266//===----------------------------------------------------------------------===//
1267// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001268
Devang Patel0c2012f2006-11-07 21:49:50 +00001269/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
1270/// either use it into active basic block pass manager or create new basic
1271/// block pass manager to handle pass P.
1272bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001273FPPassManager::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +00001274
Devang Patel67d6a5e2006-12-19 19:46:59 +00001275 // If P is a BasicBlockPass then use BBPassManager.
Devang Patel0c2012f2006-11-07 21:49:50 +00001276 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
1277
Devang Patel4949fe02006-12-07 22:34:21 +00001278 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +00001279
Devang Patel4949fe02006-12-07 22:34:21 +00001280 // If active manager exists then clear its analysis info.
1281 if (activeBBPassManager)
1282 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001283
Devang Patel4949fe02006-12-07 22:34:21 +00001284 // Create and add new manager
Devang Patel67d6a5e2006-12-19 19:46:59 +00001285 activeBBPassManager = new BBPassManager(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001286 // Inherit top level manager
1287 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001288
1289 // Add new manager into current manager's list.
Devang Patel90b05e02006-11-11 02:04:19 +00001290 addPassToManager(activeBBPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001291
1292 // Add new manager into top level manager's indirect passes list
1293 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
1294 assert (PMD && "Manager is not Pass Manager");
1295 TPM->addIndirectPassManager(PMD);
Devang Patel4949fe02006-12-07 22:34:21 +00001296
1297 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001298 if (!activeBBPassManager->addPass(BP))
1299 assert(0 && "Unable to add Pass");
Devang Patel832bc072006-12-15 00:08:26 +00001300
1301 // If activeBBPassManager transfered any Last Uses then handle them here.
1302 std::vector<Pass *> &TLU = activeBBPassManager->getTransferredLastUses();
1303 if (!TLU.empty())
1304 TPM->setLastUser(TLU, this);
1305
Devang Patel0c2012f2006-11-07 21:49:50 +00001306 }
Devang Patelbc03f132006-12-07 23:55:10 +00001307
Devang Patel0c2012f2006-11-07 21:49:50 +00001308 return true;
1309 }
1310
1311 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
1312 if (!FP)
1313 return false;
1314
Devang Patel56d48ec2006-12-15 22:57:49 +00001315 // If this pass does not preserve analysis that is used by other passes
1316 // managed by this manager than it is not a suitable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001317 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001318 return false;
1319
Devang Patel8cad70d2006-11-11 01:51:02 +00001320 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +00001321
1322 // If active manager exists then clear its analysis info.
1323 if (activeBBPassManager) {
1324 activeBBPassManager->initializeAnalysisInfo();
1325 activeBBPassManager = NULL;
1326 }
1327
Devang Patel0c2012f2006-11-07 21:49:50 +00001328 return true;
1329}
1330
1331/// Execute all of the passes scheduled for execution by invoking
1332/// runOnFunction method. Keep track of whether any of the passes modifies
1333/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001334bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001335
1336 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001337
1338 if (F.isExternal())
1339 return false;
1340
Devang Patel93a197c2006-12-14 00:08:04 +00001341 std::string Msg1 = "Executing Pass '";
1342 std::string Msg3 = "' Made Modification '";
1343
Devang Patelabfbe3b2006-12-16 00:56:26 +00001344 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1345 FunctionPass *FP = getContainedPass(Index);
1346
Devang Patelf6d1d212006-12-14 00:25:06 +00001347 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001348 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001349
Devang Patel200d3052006-12-13 23:50:44 +00001350 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001351 dumpPassInfo(FP, Msg1, Msg2);
1352 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001353
Devang Patelabfbe3b2006-12-16 00:56:26 +00001354 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001355
Devang Patelabfbe3b2006-12-16 00:56:26 +00001356 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001357 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001358 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001359
1360 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001361 dumpPassInfo(FP, Msg3, Msg2);
1362 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001363
Devang Patelabfbe3b2006-12-16 00:56:26 +00001364 removeNotPreservedAnalysis(FP);
1365 recordAvailableAnalysis(FP);
1366 removeDeadPasses(FP, Msg2);
Devang Patel9f3083e2006-11-15 19:39:54 +00001367 }
1368 return Changed;
1369}
1370
Devang Patel67d6a5e2006-12-19 19:46:59 +00001371bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001372
Devang Patel67d6a5e2006-12-19 19:46:59 +00001373 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001374
1375 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1376 this->runOnFunction(*I);
1377
1378 return Changed |= doFinalization(M);
1379}
1380
1381inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001382 bool Changed = false;
1383
Devang Patelabfbe3b2006-12-16 00:56:26 +00001384 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1385 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001386 Changed |= FP->doInitialization(M);
1387 }
1388
1389 return Changed;
1390}
1391
Devang Patel67d6a5e2006-12-19 19:46:59 +00001392inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001393 bool Changed = false;
1394
Devang Patelabfbe3b2006-12-16 00:56:26 +00001395 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1396 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001397 Changed |= FP->doFinalization(M);
1398 }
1399
Devang Patelff631ae2006-11-15 01:27:05 +00001400 return Changed;
1401}
1402
Devang Patela1514cb2006-12-07 19:39:39 +00001403//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001404// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001405
1406/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel67d6a5e2006-12-19 19:46:59 +00001407/// then use FPPassManager to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +00001408/// is not manageable by this manager.
1409bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001410MPPassManager::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +00001411
1412 // If P is FunctionPass then use function pass maanager.
1413 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1414
Devang Patel640c5bb2006-12-08 22:30:11 +00001415 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +00001416
Devang Patel4949fe02006-12-07 22:34:21 +00001417 // If active manager exists then clear its analysis info.
1418 if (activeFunctionPassManager)
1419 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001420
Devang Patel4949fe02006-12-07 22:34:21 +00001421 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001422 activeFunctionPassManager =
Devang Patel67d6a5e2006-12-19 19:46:59 +00001423 new FPPassManager(getDepth() + 1);
Devang Patelafb1f3622006-12-12 22:35:25 +00001424
1425 // Add new manager into current manager's list
Devang Patel90b05e02006-11-11 02:04:19 +00001426 addPassToManager(activeFunctionPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001427
Devang Patel9c6290c2006-12-12 22:02:16 +00001428 // Inherit top level manager
1429 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001430
1431 // Add new manager into top level manager's indirect passes list
Chris Lattnerf0f611a2006-12-13 21:56:10 +00001432 PMDataManager *PMD =
1433 dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1434 assert(PMD && "Manager is not Pass Manager");
Devang Patelafb1f3622006-12-12 22:35:25 +00001435 TPM->addIndirectPassManager(PMD);
Devang Patelaf1fca52006-12-08 23:11:43 +00001436
Devang Patel4949fe02006-12-07 22:34:21 +00001437 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001438 if (!activeFunctionPassManager->addPass(FP))
1439 assert(0 && "Unable to add pass");
Devang Patelbc03f132006-12-07 23:55:10 +00001440
Devang Patel832bc072006-12-15 00:08:26 +00001441 // If activeFunctionPassManager transfered any Last Uses then
1442 // handle them here.
1443 std::vector<Pass *> &TLU =
1444 activeFunctionPassManager->getTransferredLastUses();
1445 if (!TLU.empty())
1446 TPM->setLastUser(TLU, this);
1447 }
Devang Patelbc03f132006-12-07 23:55:10 +00001448
Devang Patel05e1a972006-11-07 22:03:15 +00001449 return true;
1450 }
1451
1452 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1453 if (!MP)
1454 return false;
1455
Devang Patel56d48ec2006-12-15 22:57:49 +00001456 // If this pass does not preserve analysis that is used by other passes
1457 // managed by this manager than it is not a suitable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001458 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001459 return false;
1460
Devang Patel8cad70d2006-11-11 01:51:02 +00001461 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +00001462 // If active manager exists then clear its analysis info.
1463 if (activeFunctionPassManager) {
1464 activeFunctionPassManager->initializeAnalysisInfo();
1465 activeFunctionPassManager = NULL;
1466 }
1467
Devang Patel05e1a972006-11-07 22:03:15 +00001468 return true;
1469}
1470
1471
1472/// Execute all of the passes scheduled for execution by invoking
1473/// runOnModule method. Keep track of whether any of the passes modifies
1474/// the module, and if so, return true.
1475bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001476MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001477 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001478
Devang Patel93a197c2006-12-14 00:08:04 +00001479 std::string Msg1 = "Executing Pass '";
1480 std::string Msg3 = "' Made Modification '";
1481
Devang Patelabfbe3b2006-12-16 00:56:26 +00001482 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1483 ModulePass *MP = getContainedPass(Index);
1484
Devang Patelf6d1d212006-12-14 00:25:06 +00001485 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001486 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001487
Devang Patel200d3052006-12-13 23:50:44 +00001488 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001489 dumpPassInfo(MP, Msg1, Msg2);
1490 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001491
Devang Patelabfbe3b2006-12-16 00:56:26 +00001492 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001493
Devang Patelabfbe3b2006-12-16 00:56:26 +00001494 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001495 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001496 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001497
1498 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001499 dumpPassInfo(MP, Msg3, Msg2);
1500 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001501
Devang Patelabfbe3b2006-12-16 00:56:26 +00001502 removeNotPreservedAnalysis(MP);
1503 recordAvailableAnalysis(MP);
1504 removeDeadPasses(MP, Msg2);
Devang Patel05e1a972006-11-07 22:03:15 +00001505 }
1506 return Changed;
1507}
1508
Devang Patela1514cb2006-12-07 19:39:39 +00001509//===----------------------------------------------------------------------===//
1510// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001511//
Devang Patelc290c8a2006-11-07 22:23:34 +00001512/// Add P into active pass manager or use new module pass manager to
1513/// manage it.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001514bool PassManagerImpl::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001515
Devang Patel6c9f5482006-11-11 00:42:16 +00001516 if (!activeManager || !activeManager->addPass(P)) {
Devang Patel1c56a632007-01-08 19:29:38 +00001517
Devang Patel67d6a5e2006-12-19 19:46:59 +00001518 activeManager = new MPPassManager(getDepth() + 1);
Devang Patel1c56a632007-01-08 19:29:38 +00001519
Devang Patel9c6290c2006-12-12 22:02:16 +00001520 // Inherit top level manager
1521 activeManager->setTopLevelManager(this->getTopLevelManager());
Devang Pateld440cd92006-12-08 23:53:00 +00001522
1523 // This top level manager is going to manage activeManager.
1524 // Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +00001525 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +00001526 activeManager->setResolver(AR);
1527
Devang Patel5bbeb492006-12-08 22:47:25 +00001528 addPassManager(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001529 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001530 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001531 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001532}
1533
1534/// run - Execute all of the passes scheduled for execution. Keep track of
1535/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001536bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001537
Devang Patelc290c8a2006-11-07 22:23:34 +00001538 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001539
Devang Patelb8817b92006-12-14 00:59:42 +00001540 TimingInfo::createTheTimeInfo();
1541
Devang Patelcfd70c42006-12-13 22:10:00 +00001542 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001543 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001544
Devang Patele3068402006-12-21 00:16:50 +00001545 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001546 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1547 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001548 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001549 }
1550 return Changed;
1551}
Devang Patel376fefa2006-11-08 10:29:57 +00001552
Devang Patela1514cb2006-12-07 19:39:39 +00001553//===----------------------------------------------------------------------===//
1554// PassManager implementation
1555
Devang Patel376fefa2006-11-08 10:29:57 +00001556/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001557PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001558 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001559 // PM is the top level manager
1560 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001561}
1562
Devang Patelb67904d2006-12-13 02:36:01 +00001563PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001564 delete PM;
1565}
1566
Devang Patel376fefa2006-11-08 10:29:57 +00001567/// add - Add a pass to the queue of passes to run. This passes ownership of
1568/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1569/// will be destroyed as well, so there is no need to delete the pass. This
1570/// implies that all passes MUST be allocated with 'new'.
1571void
Devang Patelb67904d2006-12-13 02:36:01 +00001572PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001573 PM->add(P);
1574}
1575
1576/// run - Execute all of the passes scheduled for execution. Keep track of
1577/// whether any of the passes modifies the module, and if so, return true.
1578bool
Devang Patelb67904d2006-12-13 02:36:01 +00001579PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001580 return PM->run(M);
1581}
1582
Devang Patelb8817b92006-12-14 00:59:42 +00001583//===----------------------------------------------------------------------===//
1584// TimingInfo Class - This class is used to calculate information about the
1585// amount of time each pass takes to execute. This only happens with
1586// -time-passes is enabled on the command line.
1587//
1588bool llvm::TimePassesIsEnabled = false;
1589static cl::opt<bool,true>
1590EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1591 cl::desc("Time each pass, printing elapsed time for each on exit"));
1592
1593// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1594// a non null value (if the -time-passes option is enabled) or it leaves it
1595// null. It may be called multiple times.
1596void TimingInfo::createTheTimeInfo() {
1597 if (!TimePassesIsEnabled || TheTimeInfo) return;
1598
1599 // Constructed the first time this is called, iff -time-passes is enabled.
1600 // This guarantees that the object will be constructed before static globals,
1601 // thus it will be destroyed before them.
1602 static ManagedStatic<TimingInfo> TTI;
1603 TheTimeInfo = &*TTI;
1604}
1605
Devang Patel1c56a632007-01-08 19:29:38 +00001606//===----------------------------------------------------------------------===//
1607// PMStack implementation
1608//
1609// Pop Pass Manager from the stack and clear its analysis info.
1610void PMStack::pop() {
1611
1612 PMDataManager *Top = this->top();
1613 Top->initializeAnalysisInfo();
1614
1615 S.pop_back();
1616}
1617
1618// Push PM on the stack and set its top level manager.
1619void PMStack::push(PMDataManager *PM) {
1620
1621 PMDataManager *Top = this->top();
1622
1623 // Inherit top level manager
1624 PMTopLevelManager *TPM = Top->getTopLevelManager();
1625 PM->setTopLevelManager(TPM);
1626 TPM->addIndirectPassManager(PM);
1627}
1628
1629// Walk Pass Manager stack and set LastUse markers if any
1630// manager is transfering this priviledge to its parent manager
1631void PMStack::handleLastUserOverflow() {
1632
1633 for(PMStack::iterator I = this->begin(), E = this->end(); I != E;) {
1634
1635 PMDataManager *Child = *I++;
1636 if (I != E) {
1637 PMDataManager *Parent = *I++;
1638 PMTopLevelManager *TPM = Parent->getTopLevelManager();
1639 std::vector<Pass *> &TLU = Child->getTransferredLastUses();
1640 if (!TLU.empty()) {
1641 Pass *P = dynamic_cast<Pass *>(Parent);
1642 TPM->setLastUser(TLU, P);
1643 }
1644 }
1645 }
1646}
1647
1648/// Find appropriate Module Pass Manager in the PM Stack and
1649/// add self into that manager.
1650void ModulePass::assignPassManager(PMStack &PMS) {
1651
1652 MPPassManager *MPP = NULL;
1653
1654 // Find Module Pass Manager
1655 while(!PMS.empty()) {
1656
1657 MPP = dynamic_cast<MPPassManager *>(PMS.top());
1658 if (MPP)
1659 break; // Found it
1660 else
1661 PMS.pop(); // Pop children pass managers
1662 }
1663
1664 assert(MPP && "Unable to find Module Pass Manager");
1665
1666 MPP->addPassToManager(this);
1667}
1668
1669/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1670/// in the PM Stack and add self into that manager.
1671void FunctionPass::assignPassManager(PMStack &PMS) {
1672
1673 FPPassManager *FPP = NULL;
1674
1675 // Find Module Pass Manager
1676 while(!PMS.empty()) {
1677
1678 FPP = dynamic_cast<FPPassManager *>(PMS.top());
1679 if (FPP || dynamic_cast<MPPassManager *>(PMS.top()))
1680 break; // Found it or it is not here
1681 else
1682 PMS.pop(); // Pop children pass managers
1683 }
1684
1685 if (!FPP) {
1686 /// Create new Function Pass Manager
1687
1688 /// Function Pass Manager does not live by itself
1689 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1690
1691 PMDataManager *PMD = PMS.top();
1692
1693 /// PMD should be either Module Pass Manager or Call Graph Pass Manager
1694 assert(dynamic_cast<MPPassManager *>(PMD) &&
1695 "Unable to create Function Pass Manager");
1696
1697 FPP = new FPPassManager(PMD->getDepth() + 1);
1698 PMD->addPassToManager(FPP, false);
1699 PMS.push(FPP);
1700 }
1701
1702
1703 FPP->addPassToManager(this);
1704}
1705
1706/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
1707/// in the PM Stack and add self into that manager.
1708void BasicBlockPass::assignPassManager(PMStack &PMS) {
1709
1710 BBPassManager *BBP = NULL;
1711
1712 // Find Module Pass Manager
1713 while(!PMS.empty()) {
1714
1715 BBP = dynamic_cast<BBPassManager *>(PMS.top());
1716 if (BBP || dynamic_cast<FPPassManager *>(PMS.top()))
1717 break; // Found it or it is not here
1718 else
1719 PMS.pop(); // Pop children pass managers
1720 }
1721
1722 if (!BBP) {
1723 /// Create new BasicBlock Pass Manager
1724
1725 /// BasicBlock Pass Manager does not live by itself
1726 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1727
1728 PMDataManager *PMD = PMS.top();
1729
1730 /// PMD should be Function Pass Manager
1731 assert(dynamic_cast<FPPassManager *>(PMD) &&
1732 "Unable to create BasicBlock Pass Manager");
1733
1734 BBP = new BBPassManager(PMD->getDepth() + 1);
1735 PMD->addPassToManager(BBP, false);
1736 PMS.push(BBP);
1737 }
1738
1739 BBP->addPassToManager(this);
1740}
1741
Devang Patelc6b5a552007-01-05 20:16:23 +00001742