blob: 47c21eb98835af6e0f96dbc889083c0d6831b96f [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 Patela9844592006-11-11 01:31:05 +000089/// CommonPassManagerImpl helps pass manager analysis required by
90/// the managed passes. It provides methods to add/remove analysis
91/// available and query if certain analysis is available or not.
Devang Patel42add712006-11-15 01:11:27 +000092class CommonPassManagerImpl {
Devang Patela9844592006-11-11 01:31:05 +000093
94public:
95
96 /// Return true IFF pass P's required analysis set does not required new
97 /// manager.
98 bool manageablePass(Pass *P);
99
Devang Patelf60b5d92006-11-14 01:59:59 +0000100 Pass *getAnalysisPass(AnalysisID AID) const {
101
102 std::map<AnalysisID, Pass*>::const_iterator I =
103 AvailableAnalysis.find(AID);
104
105 if (I != AvailableAnalysis.end())
106 return NULL;
107 else
108 return I->second;
Devang Patelebba9702006-11-13 22:40:09 +0000109 }
Devang Patela9844592006-11-11 01:31:05 +0000110
111 /// Augment RequiredAnalysis by adding analysis required by pass P.
112 void noteDownRequiredAnalysis(Pass *P);
113
114 /// Augment AvailableAnalysis by adding analysis made available by pass P.
115 void noteDownAvailableAnalysis(Pass *P);
116
Devang Patela9844592006-11-11 01:31:05 +0000117 /// Remove Analysis that is not preserved by the pass
118 void removeNotPreservedAnalysis(Pass *P);
119
120 /// Remove dead passes
Devang Patelca189262006-11-14 03:05:08 +0000121 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000122
Devang Patel8cad70d2006-11-11 01:51:02 +0000123 /// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +0000124 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
125 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000126
Devang Patel050ec722006-11-14 01:23:29 +0000127 /// Clear analysis vectors RequiredAnalysis and AvailableAnalysis.
128 /// This is used before running passes managed by the manager.
129 void clearAnalysis() {
130 RequiredAnalysis.clear();
131 AvailableAnalysis.clear();
Devang Patel3f0832a2006-11-14 02:54:23 +0000132 LastUser.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000133 }
134
Devang Patelf60b5d92006-11-14 01:59:59 +0000135 // All Required analyses should be available to the pass as it runs! Here
136 // we fill in the AnalysisImpls member of the pass so that it can
137 // successfully use the getAnalysis() method to retrieve the
138 // implementations it needs.
139 //
Devang Patel07f4f582006-11-14 21:49:36 +0000140 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000141
Devang Patel8cad70d2006-11-11 01:51:02 +0000142 inline std::vector<Pass *>::iterator passVectorBegin() {
143 return PassVector.begin();
144 }
145
146 inline std::vector<Pass *>::iterator passVectorEnd() {
147 return PassVector.end();
148 }
149
Devang Patel4a3fa4f2006-11-15 01:48:14 +0000150 inline void setLastUser(Pass *P, Pass *LU) {
Devang Patel07f4f582006-11-14 21:49:36 +0000151 LastUser[P] = LU;
152 // TODO : Check if pass P is available.
Devang Patel07f4f582006-11-14 21:49:36 +0000153 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000154
Devang Patela9844592006-11-11 01:31:05 +0000155private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000156 // Analysis required by the passes managed by this manager. This information
157 // used while selecting pass manager during addPass. If a pass does not
158 // preserve any analysis required by other passes managed by current
159 // pass manager then new pass manager is used.
Devang Patela9844592006-11-11 01:31:05 +0000160 std::vector<AnalysisID> RequiredAnalysis;
161
Devang Pateldafa4dd2006-11-14 00:03:04 +0000162 // Set of available Analysis. This information is used while scheduling
163 // pass. If a pass requires an analysis which is not not available then
164 // equired analysis pass is scheduled to run before the pass itself is
165 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000166 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000167
Devang Patel3f0832a2006-11-14 02:54:23 +0000168 // Map to keep track of last user of the analysis pass.
169 // LastUser->second is the last user of Lastuser->first.
170 std::map<Pass *, Pass *> LastUser;
171
Devang Patel8cad70d2006-11-11 01:51:02 +0000172 // Collection of pass that are managed by this manager
173 std::vector<Pass *> PassVector;
Devang Patela9844592006-11-11 01:31:05 +0000174};
175
Devang Patelca58e352006-11-08 10:05:38 +0000176/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
177/// pass together and sequence them to process one basic block before
178/// processing next basic block.
Devang Patel42add712006-11-15 01:11:27 +0000179class BasicBlockPassManager_New : public CommonPassManagerImpl,
180 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000181
182public:
183 BasicBlockPassManager_New() { }
184
185 /// Add a pass into a passmanager queue.
186 bool addPass(Pass *p);
187
188 /// Execute all of the passes scheduled for execution. Keep track of
189 /// whether any of the passes modifies the function, and if so, return true.
190 bool runOnFunction(Function &F);
191
Devang Patelebba9702006-11-13 22:40:09 +0000192 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000193 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000194
Devang Patelca58e352006-11-08 10:05:38 +0000195private:
Devang Patelca58e352006-11-08 10:05:38 +0000196};
197
Devang Patel4e12f862006-11-08 10:44:40 +0000198/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000199/// It batches all function passes and basic block pass managers together and
200/// sequence them to process one function at a time before processing next
201/// function.
Devang Patel42add712006-11-15 01:11:27 +0000202class FunctionPassManagerImpl_New : public CommonPassManagerImpl,
203 public ModulePass {
Devang Patelca58e352006-11-08 10:05:38 +0000204public:
Devang Patel4e12f862006-11-08 10:44:40 +0000205 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
206 FunctionPassManagerImpl_New() {
Devang Patelca58e352006-11-08 10:05:38 +0000207 activeBBPassManager = NULL;
208 }
Devang Patel4e12f862006-11-08 10:44:40 +0000209 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000210
211 /// add - Add a pass to the queue of passes to run. This passes
212 /// ownership of the Pass to the PassManager. When the
213 /// PassManager_X is destroyed, the pass will be destroyed as well, so
214 /// there is no need to delete the pass. (TODO delete passes.)
215 /// This implies that all passes MUST be allocated with 'new'.
216 void add(Pass *P) { /* TODO*/ }
217
218 /// Add pass into the pass manager queue.
219 bool addPass(Pass *P);
220
221 /// Execute all of the passes scheduled for execution. Keep
222 /// track of whether any of the passes modifies the function, and if
223 /// so, return true.
224 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000225 bool runOnFunction(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000226
Devang Patelebba9702006-11-13 22:40:09 +0000227 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000228 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000229
Devang Patelff631ae2006-11-15 01:27:05 +0000230 /// doInitialization - Run all of the initializers for the function passes.
231 ///
232 bool doInitialization(Module &M);
233
234 /// doFinalization - Run all of the initializers for the function passes.
235 ///
236 bool doFinalization(Module &M);
Devang Patelca58e352006-11-08 10:05:38 +0000237private:
Devang Patelca58e352006-11-08 10:05:38 +0000238 // Active Pass Managers
239 BasicBlockPassManager_New *activeBBPassManager;
240};
241
242/// ModulePassManager_New manages ModulePasses and function pass managers.
243/// It batches all Module passes passes and function pass managers together and
244/// sequence them to process one module.
Devang Patel0ed47792006-11-10 21:33:13 +0000245class ModulePassManager_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000246
247public:
248 ModulePassManager_New() { activeFunctionPassManager = NULL; }
249
250 /// Add a pass into a passmanager queue.
251 bool addPass(Pass *p);
252
253 /// run - Execute all of the passes scheduled for execution. Keep track of
254 /// whether any of the passes modifies the module, and if so, return true.
255 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000256
257 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000258 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelca58e352006-11-08 10:05:38 +0000259
260private:
Devang Patelca58e352006-11-08 10:05:38 +0000261 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000262 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000263};
264
Devang Patel376fefa2006-11-08 10:29:57 +0000265/// PassManager_New manages ModulePassManagers
Devang Patel0ed47792006-11-10 21:33:13 +0000266class PassManagerImpl_New : public CommonPassManagerImpl {
Devang Patel376fefa2006-11-08 10:29:57 +0000267
268public:
269
270 /// add - Add a pass to the queue of passes to run. This passes ownership of
271 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
272 /// will be destroyed as well, so there is no need to delete the pass. This
273 /// implies that all passes MUST be allocated with 'new'.
274 void add(Pass *P);
275
276 /// run - Execute all of the passes scheduled for execution. Keep track of
277 /// whether any of the passes modifies the module, and if so, return true.
278 bool run(Module &M);
279
Devang Patelebba9702006-11-13 22:40:09 +0000280 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000281 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000282
Devang Patel376fefa2006-11-08 10:29:57 +0000283private:
284
285 /// Add a pass into a passmanager queue. This is used by schedulePasses
286 bool addPass(Pass *p);
287
Devang Patel1a6eaa42006-11-11 02:22:31 +0000288 /// Schedule pass P for execution. Make sure that passes required by
289 /// P are run before P is run. Update analysis info maintained by
290 /// the manager. Remove dead passes. This is a recursive function.
291 void schedulePass(Pass *P);
292
Devang Patel376fefa2006-11-08 10:29:57 +0000293 /// Schedule all passes collected in pass queue using add(). Add all the
294 /// schedule passes into various manager's queue using addPass().
295 void schedulePasses();
296
297 // Collection of pass managers
298 std::vector<ModulePassManager_New *> PassManagers;
299
Devang Patel376fefa2006-11-08 10:29:57 +0000300 // Active Pass Manager
301 ModulePassManager_New *activeManager;
302};
303
Devang Patelca58e352006-11-08 10:05:38 +0000304} // End of llvm namespace
305
Devang Patel0ed47792006-11-10 21:33:13 +0000306// CommonPassManagerImpl implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000307
Devang Pateld65e9e92006-11-08 01:31:28 +0000308/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000309/// manager.
Devang Patel0ed47792006-11-10 21:33:13 +0000310bool CommonPassManagerImpl::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000311
312 AnalysisUsage AnUsage;
313 P->getAnalysisUsage(AnUsage);
314
Devang Patel6c9f5482006-11-11 00:42:16 +0000315 // If this pass is not preserving information that is required by the other
316 // passes managed by this manager then use new manager
317 if (!AnUsage.getPreservesAll()) {
318 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
319 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
320 E = RequiredAnalysis.end(); I != E; ++I) {
321 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
322 PreservedSet.end())
323 // This analysis is not preserved. Need new manager.
324 return false;
325 }
326 }
Devang Patelf68a3492006-11-07 22:35:17 +0000327 return true;
328}
329
Devang Patel643676c2006-11-11 01:10:19 +0000330/// Augment RequiredAnalysis by adding analysis required by pass P.
Devang Patel0ed47792006-11-10 21:33:13 +0000331void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
Devang Patel6c9f5482006-11-11 00:42:16 +0000332 AnalysisUsage AnUsage;
333 P->getAnalysisUsage(AnUsage);
334 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000335
Devang Patel6c9f5482006-11-11 00:42:16 +0000336 // FIXME: What about duplicates ?
Devang Patel349170f2006-11-11 01:24:55 +0000337 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
338 RequiredSet.end());
Devang Patel07f4f582006-11-14 21:49:36 +0000339
340 initializeAnalysisImpl(P);
Devang Patelf68a3492006-11-07 22:35:17 +0000341}
342
Devang Patel643676c2006-11-11 01:10:19 +0000343/// Augement AvailableAnalysis by adding analysis made available by pass P.
344void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000345
Devang Patel643676c2006-11-11 01:10:19 +0000346 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000347 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000348
349 //TODO This pass is the current implementation of all of the interfaces it
350 //TODO implements as well.
351 //TODO
352 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
353 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
354 //TODO CurrentAnalyses[II[i]] = P;
355 }
356}
357
Devang Patelf68a3492006-11-07 22:35:17 +0000358/// Remove Analyss not preserved by Pass P
Devang Patel0ed47792006-11-10 21:33:13 +0000359void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000360 AnalysisUsage AnUsage;
361 P->getAnalysisUsage(AnUsage);
362 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000363
Devang Patelf60b5d92006-11-14 01:59:59 +0000364 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000365 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000366 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000367 PreservedSet.end()) {
368 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000369 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000370 AvailableAnalysis.erase(J);
371 }
372 }
Devang Patelf68a3492006-11-07 22:35:17 +0000373}
374
Devang Patelca189262006-11-14 03:05:08 +0000375/// Remove analysis passes that are not used any longer
376void CommonPassManagerImpl::removeDeadPasses(Pass *P) {
377
378 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
379 E = LastUser.end(); I !=E; ++I) {
380 if (I->second == P) {
381 Pass *deadPass = I->first;
382 deadPass->releaseMemory();
383
384 std::map<AnalysisID, Pass*>::iterator Pos =
385 AvailableAnalysis.find(deadPass->getPassInfo());
386
387 assert (Pos != AvailableAnalysis.end() &&
388 "Pass is not available");
389 AvailableAnalysis.erase(Pos);
390 }
391 }
392}
393
Devang Patel8cad70d2006-11-11 01:51:02 +0000394/// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +0000395/// AvailableAnalysis appropriately if ProcessAnalysis is true.
396void CommonPassManagerImpl::addPassToManager (Pass *P,
397 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000398
Devang Patel90b05e02006-11-11 02:04:19 +0000399 if (ProcessAnalysis) {
400 // Take a note of analysis required and made available by this pass
401 noteDownRequiredAnalysis(P);
402 noteDownAvailableAnalysis(P);
403
404 // Remove the analysis not preserved by this pass
405 removeNotPreservedAnalysis(P);
406 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000407
408 // Add pass
409 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000410}
411
Devang Patel07f4f582006-11-14 21:49:36 +0000412// All Required analyses should be available to the pass as it runs! Here
413// we fill in the AnalysisImpls member of the pass so that it can
414// successfully use the getAnalysis() method to retrieve the
415// implementations it needs.
416//
417void CommonPassManagerImpl::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000418 AnalysisUsage AnUsage;
419 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000420
421 for (std::vector<const PassInfo *>::const_iterator
422 I = AnUsage.getRequiredSet().begin(),
423 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
424 Pass *Impl = getAnalysisPass(*I);
425 if (Impl == 0)
426 assert(0 && "Analysis used but not available!");
427 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
428 }
429}
430
Devang Patel6e5a1132006-11-07 21:31:57 +0000431/// BasicBlockPassManager implementation
432
Devang Pateld65e9e92006-11-08 01:31:28 +0000433/// Add pass P into PassVector and return true. If this pass is not
434/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000435bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000436BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000437
438 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
439 if (!BP)
440 return false;
441
Devang Patel3c8eb622006-11-07 22:56:50 +0000442 // If this pass does not preserve anlysis that is used by other passes
443 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000444 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000445 return false;
446
Devang Patel8cad70d2006-11-11 01:51:02 +0000447 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000448
Devang Patel6e5a1132006-11-07 21:31:57 +0000449 return true;
450}
451
452/// Execute all of the passes scheduled for execution by invoking
453/// runOnBasicBlock method. Keep track of whether any of the passes modifies
454/// the function, and if so, return true.
455bool
456BasicBlockPassManager_New::runOnFunction(Function &F) {
457
458 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000459 clearAnalysis();
460
Devang Patel6e5a1132006-11-07 21:31:57 +0000461 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000462 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
463 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000464 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000465
466 noteDownAvailableAnalysis(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000467 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
468 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000469 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000470 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000471 }
472 return Changed;
473}
474
Devang Patelebba9702006-11-13 22:40:09 +0000475/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000476Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
477 return getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000478}
479
Devang Patel0c2012f2006-11-07 21:49:50 +0000480// FunctionPassManager_New implementation
Devang Patel4e12f862006-11-08 10:44:40 +0000481/// Create new Function pass manager
482FunctionPassManager_New::FunctionPassManager_New() {
483 FPM = new FunctionPassManagerImpl_New();
484}
485
486/// add - Add a pass to the queue of passes to run. This passes
487/// ownership of the Pass to the PassManager. When the
488/// PassManager_X is destroyed, the pass will be destroyed as well, so
489/// there is no need to delete the pass. (TODO delete passes.)
490/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000491void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000492 FPM->add(P);
493}
494
495/// Execute all of the passes scheduled for execution. Keep
496/// track of whether any of the passes modifies the function, and if
497/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000498bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000499 return FPM->runOnModule(M);
500}
501
Devang Patel9f3083e2006-11-15 19:39:54 +0000502/// run - Execute all of the passes scheduled for execution. Keep
503/// track of whether any of the passes modifies the function, and if
504/// so, return true.
505///
506bool FunctionPassManager_New::run(Function &F) {
507 std::string errstr;
508 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000509 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000510 abort();
511 }
512 return FPM->runOnFunction(F);
513}
514
515
Devang Patelff631ae2006-11-15 01:27:05 +0000516/// doInitialization - Run all of the initializers for the function passes.
517///
518bool FunctionPassManager_New::doInitialization() {
519 return FPM->doInitialization(*MP->getModule());
520}
521
522/// doFinalization - Run all of the initializers for the function passes.
523///
524bool FunctionPassManager_New::doFinalization() {
525 return FPM->doFinalization(*MP->getModule());
526}
527
Devang Patel4e12f862006-11-08 10:44:40 +0000528// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000529
Devang Patel0c2012f2006-11-07 21:49:50 +0000530// FunctionPassManager
531
532/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
533/// either use it into active basic block pass manager or create new basic
534/// block pass manager to handle pass P.
535bool
Devang Patel4e12f862006-11-08 10:44:40 +0000536FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000537
538 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
539 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
540
541 if (!activeBBPassManager
542 || !activeBBPassManager->addPass(BP)) {
543
544 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000545 addPassToManager(activeBBPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000546 if (!activeBBPassManager->addPass(BP))
547 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000548 }
549 return true;
550 }
551
552 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
553 if (!FP)
554 return false;
555
Devang Patel3c8eb622006-11-07 22:56:50 +0000556 // If this pass does not preserve anlysis that is used by other passes
557 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000558 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000559 return false;
560
Devang Patel8cad70d2006-11-11 01:51:02 +0000561 addPassToManager (FP);
Devang Patel0c2012f2006-11-07 21:49:50 +0000562 activeBBPassManager = NULL;
563 return true;
564}
565
566/// Execute all of the passes scheduled for execution by invoking
567/// runOnFunction method. Keep track of whether any of the passes modifies
568/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000569bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000570
571 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000572 clearAnalysis();
573
Devang Patel0c2012f2006-11-07 21:49:50 +0000574 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000575 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
576 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000577 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000578
579 noteDownAvailableAnalysis(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000580 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
581 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000582 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000583 removeDeadPasses(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000584 }
585 return Changed;
586}
587
Devang Patel9f3083e2006-11-15 19:39:54 +0000588/// Execute all of the passes scheduled for execution by invoking
589/// runOnFunction method. Keep track of whether any of the passes modifies
590/// the function, and if so, return true.
591bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
592
593 bool Changed = false;
594 clearAnalysis();
595
596 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
597 e = passVectorEnd(); itr != e; ++itr) {
598 Pass *P = *itr;
599
600 noteDownAvailableAnalysis(P);
601 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
602 Changed |= FP->runOnFunction(F);
603 removeNotPreservedAnalysis(P);
604 removeDeadPasses(P);
605 }
606 return Changed;
607}
608
609
Devang Patelebba9702006-11-13 22:40:09 +0000610/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000611Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000612
Devang Patel3f0832a2006-11-14 02:54:23 +0000613 Pass *P = getAnalysisPass(AID);
614 if (P)
615 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000616
617 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000618 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000619 return activeBBPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000620
621 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000622 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000623}
Devang Patel0c2012f2006-11-07 21:49:50 +0000624
Devang Patelff631ae2006-11-15 01:27:05 +0000625inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
626 bool Changed = false;
627
628 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
629 e = passVectorEnd(); itr != e; ++itr) {
630 Pass *P = *itr;
631
632 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
633 Changed |= FP->doInitialization(M);
634 }
635
636 return Changed;
637}
638
639inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
640 bool Changed = false;
641
642 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
643 e = passVectorEnd(); itr != e; ++itr) {
644 Pass *P = *itr;
645
646 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
647 Changed |= FP->doFinalization(M);
648 }
649
650
651 return Changed;
652}
653
654
Devang Patel05e1a972006-11-07 22:03:15 +0000655// ModulePassManager implementation
656
657/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000658/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000659/// is not manageable by this manager.
660bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000661ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000662
663 // If P is FunctionPass then use function pass maanager.
664 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
665
666 activeFunctionPassManager = NULL;
667
668 if (!activeFunctionPassManager
669 || !activeFunctionPassManager->addPass(P)) {
670
Devang Patel4e12f862006-11-08 10:44:40 +0000671 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000672 addPassToManager(activeFunctionPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000673 if (!activeFunctionPassManager->addPass(FP))
674 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000675 }
676 return true;
677 }
678
679 ModulePass *MP = dynamic_cast<ModulePass *>(P);
680 if (!MP)
681 return false;
682
Devang Patel3c8eb622006-11-07 22:56:50 +0000683 // If this pass does not preserve anlysis that is used by other passes
684 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000685 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000686 return false;
687
Devang Patel8cad70d2006-11-11 01:51:02 +0000688 addPassToManager(MP);
Devang Patel05e1a972006-11-07 22:03:15 +0000689 activeFunctionPassManager = NULL;
690 return true;
691}
692
693
694/// Execute all of the passes scheduled for execution by invoking
695/// runOnModule method. Keep track of whether any of the passes modifies
696/// the module, and if so, return true.
697bool
698ModulePassManager_New::runOnModule(Module &M) {
699 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000700 clearAnalysis();
701
Devang Patel8cad70d2006-11-11 01:51:02 +0000702 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
703 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000704 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000705
706 noteDownAvailableAnalysis(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000707 ModulePass *MP = dynamic_cast<ModulePass*>(P);
708 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000709 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000710 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000711 }
712 return Changed;
713}
714
Devang Patelebba9702006-11-13 22:40:09 +0000715/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000716Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000717
Devang Patel3f0832a2006-11-14 02:54:23 +0000718
719 Pass *P = getAnalysisPass(AID);
720 if (P)
721 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000722
723 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000724 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000725 return activeFunctionPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000726
727 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000728 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000729}
730
731/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000732Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000733
Devang Patel3f0832a2006-11-14 02:54:23 +0000734 Pass *P = NULL;
Devang Patel70868442006-11-13 22:53:19 +0000735 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
Devang Patel3f0832a2006-11-14 02:54:23 +0000736 e = PassManagers.end(); !P && itr != e; ++itr)
737 P = (*itr)->getAnalysisPassFromManager(AID);
738 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000739}
740
Devang Patel1a6eaa42006-11-11 02:22:31 +0000741/// Schedule pass P for execution. Make sure that passes required by
742/// P are run before P is run. Update analysis info maintained by
743/// the manager. Remove dead passes. This is a recursive function.
744void PassManagerImpl_New::schedulePass(Pass *P) {
745
746 AnalysisUsage AnUsage;
747 P->getAnalysisUsage(AnUsage);
748 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
749 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
750 E = RequiredSet.end(); I != E; ++I) {
751
Devang Patel3f0832a2006-11-14 02:54:23 +0000752 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
753 if (!AnalysisPass) {
Devang Patel1a6eaa42006-11-11 02:22:31 +0000754 // Schedule this analysis run first.
Devang Patel3f0832a2006-11-14 02:54:23 +0000755 AnalysisPass = (*I)->createPass();
756 schedulePass(AnalysisPass);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000757 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000758 setLastUser (AnalysisPass, P);
Devang Patel4a3fa4f2006-11-15 01:48:14 +0000759
760 // Prolong live range of analyses that are needed after an analysis pass
761 // is destroyed, for querying by subsequent passes
762 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
763 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
764 E = IDs.end(); I != E; ++I) {
765 Pass *AP = getAnalysisPassFromManager(*I);
766 assert (AP && "Analysis pass is not available");
767 setLastUser(AP, P);
768 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000769 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000770 addPass(P);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000771}
772
Devang Patelc290c8a2006-11-07 22:23:34 +0000773/// Schedule all passes from the queue by adding them in their
774/// respective manager's queue.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000775void PassManagerImpl_New::schedulePasses() {
776 for (std::vector<Pass *>::iterator I = passVectorBegin(),
777 E = passVectorEnd(); I != E; ++I)
778 schedulePass (*I);
Devang Patelc290c8a2006-11-07 22:23:34 +0000779}
780
781/// Add pass P to the queue of passes to run.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000782void PassManagerImpl_New::add(Pass *P) {
783 // Do not process Analysis now. Analysis is process while scheduling
784 // the pass vector.
Devang Pateldb789fb2006-11-11 02:06:21 +0000785 addPassToManager(P, false);
Devang Patelc290c8a2006-11-07 22:23:34 +0000786}
787
788// PassManager_New implementation
789/// Add P into active pass manager or use new module pass manager to
790/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000791bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000792
Devang Patel6c9f5482006-11-11 00:42:16 +0000793 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000794 activeManager = new ModulePassManager_New();
795 PassManagers.push_back(activeManager);
796 }
797
798 return activeManager->addPass(P);
799}
800
801/// run - Execute all of the passes scheduled for execution. Keep track of
802/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000803bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000804
805 schedulePasses();
806 bool Changed = false;
807 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
808 e = PassManagers.end(); itr != e; ++itr) {
809 ModulePassManager_New *pm = *itr;
810 Changed |= pm->runOnModule(M);
811 }
812 return Changed;
813}
Devang Patel376fefa2006-11-08 10:29:57 +0000814
815/// Create new pass manager
816PassManager_New::PassManager_New() {
817 PM = new PassManagerImpl_New();
818}
819
820/// add - Add a pass to the queue of passes to run. This passes ownership of
821/// the Pass to the PassManager. When the PassManager is destroyed, the pass
822/// will be destroyed as well, so there is no need to delete the pass. This
823/// implies that all passes MUST be allocated with 'new'.
824void
825PassManager_New::add(Pass *P) {
826 PM->add(P);
827}
828
829/// run - Execute all of the passes scheduled for execution. Keep track of
830/// whether any of the passes modifies the module, and if so, return true.
831bool
832PassManager_New::run(Module &M) {
833 return PM->run(M);
834}
835