blob: 5b0e0a48c9a15bdc7127c08c91d4619837a8ca79 [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 Patel6e5a1132006-11-07 21:31:57 +000016#include "llvm/Module.h"
Devang Patelff631ae2006-11-15 01:27:05 +000017#include "llvm/ModuleProvider.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000018#include "llvm/Support/Streams.h"
Devang Patela9844592006-11-11 01:31:05 +000019#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000020#include <map>
Devang Patel6e5a1132006-11-07 21:31:57 +000021using namespace llvm;
22
Devang Patel6fea2852006-12-07 18:23:30 +000023//===----------------------------------------------------------------------===//
24// Overview:
25// The Pass Manager Infrastructure manages passes. It's responsibilities are:
26//
27// o Manage optimization pass execution order
28// o Make required Analysis information available before pass P is run
29// o Release memory occupied by dead passes
30// o If Analysis information is dirtied by a pass then regenerate Analysis
31// information before it is consumed by another pass.
32//
33// Pass Manager Infrastructure uses multipe pass managers. They are PassManager,
34// FunctionPassManager, ModulePassManager, BasicBlockPassManager. This class
35// hierarcy uses multiple inheritance but pass managers do not derive from
36// another pass manager.
37//
38// PassManager and FunctionPassManager are two top level pass manager that
39// represents the external interface of this entire pass manager infrastucture.
40//
41// Important classes :
42//
43// [o] class PMTopLevelManager;
44//
45// Two top level managers, PassManager and FunctionPassManager, derive from
46// PMTopLevelManager. PMTopLevelManager manages information used by top level
47// managers such as last user info.
48//
49// [o] class PMDataManager;
50//
51// PMDataManager manages information, e.g. list of available analysis info,
52// used by a pass manager to manage execution order of passes. It also provides
53// a place to implement common pass manager APIs. All pass managers derive from
54// PMDataManager.
55//
56// [o] class BasicBlockPassManager : public FunctionPass, public PMDataManager;
57//
58// BasicBlockPassManager manages BasicBlockPasses.
59//
60// [o] class FunctionPassManager;
61//
62// This is a external interface used by JIT to manage FunctionPasses. This
63// interface relies on FunctionPassManagerImpl to do all the tasks.
64//
65// [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
66// public PMTopLevelManager;
67//
68// FunctionPassManagerImpl is a top level manager. It manages FunctionPasses
69// and BasicBlockPassManagers.
70//
71// [o] class ModulePassManager : public Pass, public PMDataManager;
72//
73// ModulePassManager manages ModulePasses and FunctionPassManagerImpls.
74//
75// [o] class PassManager;
76//
77// This is a external interface used by various tools to manages passes. It
78// relies on PassManagerImpl to do all the tasks.
79//
80// [o] class PassManagerImpl : public Pass, public PMDataManager,
81// public PMDTopLevelManager
82//
83// PassManagerImpl is a top level pass manager responsible for managing
84// ModulePassManagers.
85//===----------------------------------------------------------------------===//
86
Devang Patelca58e352006-11-08 10:05:38 +000087namespace llvm {
88
Devang Pateldbe4a1e2006-12-07 18:36:24 +000089/// PMDataManager provides the common place to manage the analysis data
90/// used by pass managers.
91class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +000092
93public:
94
95 /// Return true IFF pass P's required analysis set does not required new
96 /// manager.
97 bool manageablePass(Pass *P);
98
Devang Patelf60b5d92006-11-14 01:59:59 +000099 Pass *getAnalysisPass(AnalysisID AID) const {
100
101 std::map<AnalysisID, Pass*>::const_iterator I =
102 AvailableAnalysis.find(AID);
103
104 if (I != AvailableAnalysis.end())
105 return NULL;
106 else
107 return I->second;
Devang Patelebba9702006-11-13 22:40:09 +0000108 }
Devang Patela9844592006-11-11 01:31:05 +0000109
110 /// Augment RequiredAnalysis by adding analysis required by pass P.
111 void noteDownRequiredAnalysis(Pass *P);
112
113 /// Augment AvailableAnalysis by adding analysis made available by pass P.
114 void noteDownAvailableAnalysis(Pass *P);
115
Devang Patela9844592006-11-11 01:31:05 +0000116 /// Remove Analysis that is not preserved by the pass
117 void removeNotPreservedAnalysis(Pass *P);
118
119 /// Remove dead passes
Devang Patelca189262006-11-14 03:05:08 +0000120 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000121
Devang Patel8cad70d2006-11-11 01:51:02 +0000122 /// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +0000123 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
124 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000125
Devang Patel050ec722006-11-14 01:23:29 +0000126 /// Clear analysis vectors RequiredAnalysis and AvailableAnalysis.
127 /// This is used before running passes managed by the manager.
128 void clearAnalysis() {
129 RequiredAnalysis.clear();
130 AvailableAnalysis.clear();
Devang Patel3f0832a2006-11-14 02:54:23 +0000131 LastUser.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000132 }
133
Devang Patelf60b5d92006-11-14 01:59:59 +0000134 // All Required analyses should be available to the pass as it runs! Here
135 // we fill in the AnalysisImpls member of the pass so that it can
136 // successfully use the getAnalysis() method to retrieve the
137 // implementations it needs.
138 //
Devang Patel07f4f582006-11-14 21:49:36 +0000139 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000140
Devang Patel8cad70d2006-11-11 01:51:02 +0000141 inline std::vector<Pass *>::iterator passVectorBegin() {
142 return PassVector.begin();
143 }
144
145 inline std::vector<Pass *>::iterator passVectorEnd() {
146 return PassVector.end();
147 }
148
Devang Patel4a3fa4f2006-11-15 01:48:14 +0000149 inline void setLastUser(Pass *P, Pass *LU) {
Devang Patel07f4f582006-11-14 21:49:36 +0000150 LastUser[P] = LU;
151 // TODO : Check if pass P is available.
Devang Patel07f4f582006-11-14 21:49:36 +0000152 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000153
Devang Patela9844592006-11-11 01:31:05 +0000154private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000155 // Analysis required by the passes managed by this manager. This information
156 // used while selecting pass manager during addPass. If a pass does not
157 // preserve any analysis required by other passes managed by current
158 // pass manager then new pass manager is used.
Devang Patela9844592006-11-11 01:31:05 +0000159 std::vector<AnalysisID> RequiredAnalysis;
160
Devang Pateldafa4dd2006-11-14 00:03:04 +0000161 // Set of available Analysis. This information is used while scheduling
162 // pass. If a pass requires an analysis which is not not available then
163 // equired analysis pass is scheduled to run before the pass itself is
164 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000165 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000166
Devang Patel3f0832a2006-11-14 02:54:23 +0000167 // Map to keep track of last user of the analysis pass.
168 // LastUser->second is the last user of Lastuser->first.
169 std::map<Pass *, Pass *> LastUser;
170
Devang Patel8cad70d2006-11-11 01:51:02 +0000171 // Collection of pass that are managed by this manager
172 std::vector<Pass *> PassVector;
Devang Patela9844592006-11-11 01:31:05 +0000173};
174
Devang Patelca58e352006-11-08 10:05:38 +0000175/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
176/// pass together and sequence them to process one basic block before
177/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000178class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000179 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000180
181public:
182 BasicBlockPassManager_New() { }
183
184 /// Add a pass into a passmanager queue.
185 bool addPass(Pass *p);
186
187 /// Execute all of the passes scheduled for execution. Keep track of
188 /// whether any of the passes modifies the function, and if so, return true.
189 bool runOnFunction(Function &F);
190
Devang Patelebba9702006-11-13 22:40:09 +0000191 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000192 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000193
Devang Patelca58e352006-11-08 10:05:38 +0000194private:
Devang Patelca58e352006-11-08 10:05:38 +0000195};
196
Devang Patel4e12f862006-11-08 10:44:40 +0000197/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000198/// It batches all function passes and basic block pass managers together and
199/// sequence them to process one function at a time before processing next
200/// function.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000201class FunctionPassManagerImpl_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000202 public ModulePass {
Devang Patelca58e352006-11-08 10:05:38 +0000203public:
Devang Patel4e12f862006-11-08 10:44:40 +0000204 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
205 FunctionPassManagerImpl_New() {
Devang Patelca58e352006-11-08 10:05:38 +0000206 activeBBPassManager = NULL;
207 }
Devang Patel4e12f862006-11-08 10:44:40 +0000208 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000209
210 /// add - Add a pass to the queue of passes to run. This passes
211 /// ownership of the Pass to the PassManager. When the
212 /// PassManager_X is destroyed, the pass will be destroyed as well, so
213 /// there is no need to delete the pass. (TODO delete passes.)
214 /// This implies that all passes MUST be allocated with 'new'.
215 void add(Pass *P) { /* TODO*/ }
216
217 /// Add pass into the pass manager queue.
218 bool addPass(Pass *P);
219
220 /// Execute all of the passes scheduled for execution. Keep
221 /// track of whether any of the passes modifies the function, and if
222 /// so, return true.
223 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000224 bool runOnFunction(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000225
Devang Patelebba9702006-11-13 22:40:09 +0000226 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000227 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000228
Devang Patelff631ae2006-11-15 01:27:05 +0000229 /// doInitialization - Run all of the initializers for the function passes.
230 ///
231 bool doInitialization(Module &M);
232
233 /// doFinalization - Run all of the initializers for the function passes.
234 ///
235 bool doFinalization(Module &M);
Devang Patelca58e352006-11-08 10:05:38 +0000236private:
Devang Patelca58e352006-11-08 10:05:38 +0000237 // Active Pass Managers
238 BasicBlockPassManager_New *activeBBPassManager;
239};
240
241/// ModulePassManager_New manages ModulePasses and function pass managers.
242/// It batches all Module passes passes and function pass managers together and
243/// sequence them to process one module.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000244class ModulePassManager_New : public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000245
246public:
247 ModulePassManager_New() { activeFunctionPassManager = NULL; }
248
249 /// Add a pass into a passmanager queue.
250 bool addPass(Pass *p);
251
252 /// run - Execute all of the passes scheduled for execution. Keep track of
253 /// whether any of the passes modifies the module, and if so, return true.
254 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000255
256 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000257 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelca58e352006-11-08 10:05:38 +0000258
259private:
Devang Patelca58e352006-11-08 10:05:38 +0000260 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000261 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000262};
263
Devang Patel376fefa2006-11-08 10:29:57 +0000264/// PassManager_New manages ModulePassManagers
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000265class PassManagerImpl_New : public PMDataManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000266
267public:
268
269 /// add - Add a pass to the queue of passes to run. This passes ownership of
270 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
271 /// will be destroyed as well, so there is no need to delete the pass. This
272 /// implies that all passes MUST be allocated with 'new'.
273 void add(Pass *P);
274
275 /// run - Execute all of the passes scheduled for execution. Keep track of
276 /// whether any of the passes modifies the module, and if so, return true.
277 bool run(Module &M);
278
Devang Patelebba9702006-11-13 22:40:09 +0000279 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000280 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000281
Devang Patel376fefa2006-11-08 10:29:57 +0000282private:
283
284 /// Add a pass into a passmanager queue. This is used by schedulePasses
285 bool addPass(Pass *p);
286
Devang Patel1a6eaa42006-11-11 02:22:31 +0000287 /// Schedule pass P for execution. Make sure that passes required by
288 /// P are run before P is run. Update analysis info maintained by
289 /// the manager. Remove dead passes. This is a recursive function.
290 void schedulePass(Pass *P);
291
Devang Patel376fefa2006-11-08 10:29:57 +0000292 /// Schedule all passes collected in pass queue using add(). Add all the
293 /// schedule passes into various manager's queue using addPass().
294 void schedulePasses();
295
296 // Collection of pass managers
297 std::vector<ModulePassManager_New *> PassManagers;
298
Devang Patel376fefa2006-11-08 10:29:57 +0000299 // Active Pass Manager
300 ModulePassManager_New *activeManager;
301};
302
Devang Patelca58e352006-11-08 10:05:38 +0000303} // End of llvm namespace
304
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000305// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000306
Devang Pateld65e9e92006-11-08 01:31:28 +0000307/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000308/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000309bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000310
311 AnalysisUsage AnUsage;
312 P->getAnalysisUsage(AnUsage);
313
Devang Patel6c9f5482006-11-11 00:42:16 +0000314 // If this pass is not preserving information that is required by the other
315 // passes managed by this manager then use new manager
316 if (!AnUsage.getPreservesAll()) {
317 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
318 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
319 E = RequiredAnalysis.end(); I != E; ++I) {
320 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
321 PreservedSet.end())
322 // This analysis is not preserved. Need new manager.
323 return false;
324 }
325 }
Devang Patelf68a3492006-11-07 22:35:17 +0000326 return true;
327}
328
Devang Patel643676c2006-11-11 01:10:19 +0000329/// Augment RequiredAnalysis by adding analysis required by pass P.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000330void PMDataManager::noteDownRequiredAnalysis(Pass *P) {
Devang Patel6c9f5482006-11-11 00:42:16 +0000331 AnalysisUsage AnUsage;
332 P->getAnalysisUsage(AnUsage);
333 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000334
Devang Patel6c9f5482006-11-11 00:42:16 +0000335 // FIXME: What about duplicates ?
Devang Patel349170f2006-11-11 01:24:55 +0000336 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
337 RequiredSet.end());
Devang Patel07f4f582006-11-14 21:49:36 +0000338
339 initializeAnalysisImpl(P);
Devang Patelf68a3492006-11-07 22:35:17 +0000340}
341
Devang Patel643676c2006-11-11 01:10:19 +0000342/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000343void PMDataManager::noteDownAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000344
Devang Patel643676c2006-11-11 01:10:19 +0000345 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000346 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000347
348 //TODO This pass is the current implementation of all of the interfaces it
349 //TODO implements as well.
350 //TODO
351 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
352 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
353 //TODO CurrentAnalyses[II[i]] = P;
354 }
355}
356
Devang Patelf68a3492006-11-07 22:35:17 +0000357/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000358void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000359 AnalysisUsage AnUsage;
360 P->getAnalysisUsage(AnUsage);
361 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000362
Devang Patelf60b5d92006-11-14 01:59:59 +0000363 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000364 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000365 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000366 PreservedSet.end()) {
367 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000368 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000369 AvailableAnalysis.erase(J);
370 }
371 }
Devang Patelf68a3492006-11-07 22:35:17 +0000372}
373
Devang Patelca189262006-11-14 03:05:08 +0000374/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000375void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patelca189262006-11-14 03:05:08 +0000376
377 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
378 E = LastUser.end(); I !=E; ++I) {
379 if (I->second == P) {
380 Pass *deadPass = I->first;
381 deadPass->releaseMemory();
382
383 std::map<AnalysisID, Pass*>::iterator Pos =
384 AvailableAnalysis.find(deadPass->getPassInfo());
385
386 assert (Pos != AvailableAnalysis.end() &&
387 "Pass is not available");
388 AvailableAnalysis.erase(Pos);
389 }
390 }
391}
392
Devang Patel8cad70d2006-11-11 01:51:02 +0000393/// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +0000394/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000395void PMDataManager::addPassToManager (Pass *P,
Devang Patel90b05e02006-11-11 02:04:19 +0000396 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000397
Devang Patel90b05e02006-11-11 02:04:19 +0000398 if (ProcessAnalysis) {
399 // Take a note of analysis required and made available by this pass
400 noteDownRequiredAnalysis(P);
401 noteDownAvailableAnalysis(P);
402
403 // Remove the analysis not preserved by this pass
404 removeNotPreservedAnalysis(P);
405 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000406
407 // Add pass
408 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000409}
410
Devang Patel07f4f582006-11-14 21:49:36 +0000411// All Required analyses should be available to the pass as it runs! Here
412// we fill in the AnalysisImpls member of the pass so that it can
413// successfully use the getAnalysis() method to retrieve the
414// implementations it needs.
415//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000416void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000417 AnalysisUsage AnUsage;
418 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000419
420 for (std::vector<const PassInfo *>::const_iterator
421 I = AnUsage.getRequiredSet().begin(),
422 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
423 Pass *Impl = getAnalysisPass(*I);
424 if (Impl == 0)
425 assert(0 && "Analysis used but not available!");
426 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
427 }
428}
429
Devang Patel6e5a1132006-11-07 21:31:57 +0000430/// BasicBlockPassManager implementation
431
Devang Pateld65e9e92006-11-08 01:31:28 +0000432/// Add pass P into PassVector and return true. If this pass is not
433/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000434bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000435BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000436
437 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
438 if (!BP)
439 return false;
440
Devang Patel3c8eb622006-11-07 22:56:50 +0000441 // If this pass does not preserve anlysis that is used by other passes
442 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000443 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000444 return false;
445
Devang Patel8cad70d2006-11-11 01:51:02 +0000446 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000447
Devang Patel6e5a1132006-11-07 21:31:57 +0000448 return true;
449}
450
451/// Execute all of the passes scheduled for execution by invoking
452/// runOnBasicBlock method. Keep track of whether any of the passes modifies
453/// the function, and if so, return true.
454bool
455BasicBlockPassManager_New::runOnFunction(Function &F) {
456
457 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000458 clearAnalysis();
459
Devang Patel6e5a1132006-11-07 21:31:57 +0000460 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000461 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
462 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000463 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000464
465 noteDownAvailableAnalysis(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000466 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
467 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000468 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000469 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000470 }
471 return Changed;
472}
473
Devang Patelebba9702006-11-13 22:40:09 +0000474/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000475Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
476 return getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000477}
478
Devang Patel0c2012f2006-11-07 21:49:50 +0000479// FunctionPassManager_New implementation
Devang Patel4e12f862006-11-08 10:44:40 +0000480/// Create new Function pass manager
481FunctionPassManager_New::FunctionPassManager_New() {
482 FPM = new FunctionPassManagerImpl_New();
483}
484
485/// add - Add a pass to the queue of passes to run. This passes
486/// ownership of the Pass to the PassManager. When the
487/// PassManager_X is destroyed, the pass will be destroyed as well, so
488/// there is no need to delete the pass. (TODO delete passes.)
489/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000490void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000491 FPM->add(P);
492}
493
494/// Execute all of the passes scheduled for execution. Keep
495/// track of whether any of the passes modifies the function, and if
496/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000497bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000498 return FPM->runOnModule(M);
499}
500
Devang Patel9f3083e2006-11-15 19:39:54 +0000501/// run - Execute all of the passes scheduled for execution. Keep
502/// track of whether any of the passes modifies the function, and if
503/// so, return true.
504///
505bool FunctionPassManager_New::run(Function &F) {
506 std::string errstr;
507 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000508 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000509 abort();
510 }
511 return FPM->runOnFunction(F);
512}
513
514
Devang Patelff631ae2006-11-15 01:27:05 +0000515/// doInitialization - Run all of the initializers for the function passes.
516///
517bool FunctionPassManager_New::doInitialization() {
518 return FPM->doInitialization(*MP->getModule());
519}
520
521/// doFinalization - Run all of the initializers for the function passes.
522///
523bool FunctionPassManager_New::doFinalization() {
524 return FPM->doFinalization(*MP->getModule());
525}
526
Devang Patel4e12f862006-11-08 10:44:40 +0000527// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000528
Devang Patel0c2012f2006-11-07 21:49:50 +0000529// FunctionPassManager
530
531/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
532/// either use it into active basic block pass manager or create new basic
533/// block pass manager to handle pass P.
534bool
Devang Patel4e12f862006-11-08 10:44:40 +0000535FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000536
537 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
538 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
539
540 if (!activeBBPassManager
541 || !activeBBPassManager->addPass(BP)) {
542
543 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000544 addPassToManager(activeBBPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000545 if (!activeBBPassManager->addPass(BP))
546 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000547 }
548 return true;
549 }
550
551 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
552 if (!FP)
553 return false;
554
Devang Patel3c8eb622006-11-07 22:56:50 +0000555 // If this pass does not preserve anlysis that is used by other passes
556 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000557 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000558 return false;
559
Devang Patel8cad70d2006-11-11 01:51:02 +0000560 addPassToManager (FP);
Devang Patel0c2012f2006-11-07 21:49:50 +0000561 activeBBPassManager = NULL;
562 return true;
563}
564
565/// Execute all of the passes scheduled for execution by invoking
566/// runOnFunction method. Keep track of whether any of the passes modifies
567/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000568bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000569
570 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000571 clearAnalysis();
572
Devang Patel0c2012f2006-11-07 21:49:50 +0000573 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000574 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
575 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000576 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000577
578 noteDownAvailableAnalysis(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000579 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
580 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000581 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000582 removeDeadPasses(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000583 }
584 return Changed;
585}
586
Devang Patel9f3083e2006-11-15 19:39:54 +0000587/// Execute all of the passes scheduled for execution by invoking
588/// runOnFunction method. Keep track of whether any of the passes modifies
589/// the function, and if so, return true.
590bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
591
592 bool Changed = false;
593 clearAnalysis();
594
595 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
596 e = passVectorEnd(); itr != e; ++itr) {
597 Pass *P = *itr;
598
599 noteDownAvailableAnalysis(P);
600 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
601 Changed |= FP->runOnFunction(F);
602 removeNotPreservedAnalysis(P);
603 removeDeadPasses(P);
604 }
605 return Changed;
606}
607
608
Devang Patelebba9702006-11-13 22:40:09 +0000609/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000610Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000611
Devang Patel3f0832a2006-11-14 02:54:23 +0000612 Pass *P = getAnalysisPass(AID);
613 if (P)
614 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000615
616 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000617 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000618 return activeBBPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000619
620 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000621 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000622}
Devang Patel0c2012f2006-11-07 21:49:50 +0000623
Devang Patelff631ae2006-11-15 01:27:05 +0000624inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
625 bool Changed = false;
626
627 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
628 e = passVectorEnd(); itr != e; ++itr) {
629 Pass *P = *itr;
630
631 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
632 Changed |= FP->doInitialization(M);
633 }
634
635 return Changed;
636}
637
638inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
639 bool Changed = false;
640
641 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
642 e = passVectorEnd(); itr != e; ++itr) {
643 Pass *P = *itr;
644
645 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
646 Changed |= FP->doFinalization(M);
647 }
648
649
650 return Changed;
651}
652
653
Devang Patel05e1a972006-11-07 22:03:15 +0000654// ModulePassManager implementation
655
656/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000657/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000658/// is not manageable by this manager.
659bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000660ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000661
662 // If P is FunctionPass then use function pass maanager.
663 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
664
665 activeFunctionPassManager = NULL;
666
667 if (!activeFunctionPassManager
668 || !activeFunctionPassManager->addPass(P)) {
669
Devang Patel4e12f862006-11-08 10:44:40 +0000670 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000671 addPassToManager(activeFunctionPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000672 if (!activeFunctionPassManager->addPass(FP))
673 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000674 }
675 return true;
676 }
677
678 ModulePass *MP = dynamic_cast<ModulePass *>(P);
679 if (!MP)
680 return false;
681
Devang Patel3c8eb622006-11-07 22:56:50 +0000682 // If this pass does not preserve anlysis that is used by other passes
683 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000684 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000685 return false;
686
Devang Patel8cad70d2006-11-11 01:51:02 +0000687 addPassToManager(MP);
Devang Patel05e1a972006-11-07 22:03:15 +0000688 activeFunctionPassManager = NULL;
689 return true;
690}
691
692
693/// Execute all of the passes scheduled for execution by invoking
694/// runOnModule method. Keep track of whether any of the passes modifies
695/// the module, and if so, return true.
696bool
697ModulePassManager_New::runOnModule(Module &M) {
698 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000699 clearAnalysis();
700
Devang Patel8cad70d2006-11-11 01:51:02 +0000701 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
702 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000703 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000704
705 noteDownAvailableAnalysis(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000706 ModulePass *MP = dynamic_cast<ModulePass*>(P);
707 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000708 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000709 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000710 }
711 return Changed;
712}
713
Devang Patelebba9702006-11-13 22:40:09 +0000714/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000715Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000716
Devang Patel3f0832a2006-11-14 02:54:23 +0000717
718 Pass *P = getAnalysisPass(AID);
719 if (P)
720 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000721
722 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000723 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000724 return activeFunctionPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000725
726 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000727 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000728}
729
730/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000731Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000732
Devang Patel3f0832a2006-11-14 02:54:23 +0000733 Pass *P = NULL;
Devang Patel70868442006-11-13 22:53:19 +0000734 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
Devang Patel3f0832a2006-11-14 02:54:23 +0000735 e = PassManagers.end(); !P && itr != e; ++itr)
736 P = (*itr)->getAnalysisPassFromManager(AID);
737 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000738}
739
Devang Patel1a6eaa42006-11-11 02:22:31 +0000740/// Schedule pass P for execution. Make sure that passes required by
741/// P are run before P is run. Update analysis info maintained by
742/// the manager. Remove dead passes. This is a recursive function.
743void PassManagerImpl_New::schedulePass(Pass *P) {
744
745 AnalysisUsage AnUsage;
746 P->getAnalysisUsage(AnUsage);
747 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
748 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
749 E = RequiredSet.end(); I != E; ++I) {
750
Devang Patel3f0832a2006-11-14 02:54:23 +0000751 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
752 if (!AnalysisPass) {
Devang Patel1a6eaa42006-11-11 02:22:31 +0000753 // Schedule this analysis run first.
Devang Patel3f0832a2006-11-14 02:54:23 +0000754 AnalysisPass = (*I)->createPass();
755 schedulePass(AnalysisPass);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000756 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000757 setLastUser (AnalysisPass, P);
Devang Patel4a3fa4f2006-11-15 01:48:14 +0000758
759 // Prolong live range of analyses that are needed after an analysis pass
760 // is destroyed, for querying by subsequent passes
761 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
762 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
763 E = IDs.end(); I != E; ++I) {
764 Pass *AP = getAnalysisPassFromManager(*I);
765 assert (AP && "Analysis pass is not available");
766 setLastUser(AP, P);
767 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000768 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000769 addPass(P);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000770}
771
Devang Patelc290c8a2006-11-07 22:23:34 +0000772/// Schedule all passes from the queue by adding them in their
773/// respective manager's queue.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000774void PassManagerImpl_New::schedulePasses() {
775 for (std::vector<Pass *>::iterator I = passVectorBegin(),
776 E = passVectorEnd(); I != E; ++I)
777 schedulePass (*I);
Devang Patelc290c8a2006-11-07 22:23:34 +0000778}
779
780/// Add pass P to the queue of passes to run.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000781void PassManagerImpl_New::add(Pass *P) {
782 // Do not process Analysis now. Analysis is process while scheduling
783 // the pass vector.
Devang Pateldb789fb2006-11-11 02:06:21 +0000784 addPassToManager(P, false);
Devang Patelc290c8a2006-11-07 22:23:34 +0000785}
786
787// PassManager_New implementation
788/// Add P into active pass manager or use new module pass manager to
789/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000790bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000791
Devang Patel6c9f5482006-11-11 00:42:16 +0000792 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000793 activeManager = new ModulePassManager_New();
794 PassManagers.push_back(activeManager);
795 }
796
797 return activeManager->addPass(P);
798}
799
800/// run - Execute all of the passes scheduled for execution. Keep track of
801/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000802bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000803
804 schedulePasses();
805 bool Changed = false;
806 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
807 e = PassManagers.end(); itr != e; ++itr) {
808 ModulePassManager_New *pm = *itr;
809 Changed |= pm->runOnModule(M);
810 }
811 return Changed;
812}
Devang Patel376fefa2006-11-08 10:29:57 +0000813
814/// Create new pass manager
815PassManager_New::PassManager_New() {
816 PM = new PassManagerImpl_New();
817}
818
819/// add - Add a pass to the queue of passes to run. This passes ownership of
820/// the Pass to the PassManager. When the PassManager is destroyed, the pass
821/// will be destroyed as well, so there is no need to delete the pass. This
822/// implies that all passes MUST be allocated with 'new'.
823void
824PassManager_New::add(Pass *P) {
825 PM->add(P);
826}
827
828/// run - Execute all of the passes scheduled for execution. Keep track of
829/// whether any of the passes modifies the module, and if so, return true.
830bool
831PassManager_New::run(Module &M) {
832 return PM->run(M);
833}
834