blob: d7684f07e70ea244dd9b010a5249260c95482227 [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 Patelf33f3eb2006-12-07 19:21:29 +000089//===----------------------------------------------------------------------===//
90// PMTopLevelManager
91//
92/// PMTopLevelManager manages LastUser info and collects common APIs used by
93/// top level pass managers.
94class PMTopLevelManager {
95
96public:
97
98 inline std::vector<Pass *>::iterator passManagersBegin() {
99 return PassManagers.begin();
100 }
101
102 inline std::vector<Pass *>::iterator passManagersEnd() {
103 return PassManagers.end();
104 }
105
106 /// Schedule pass P for execution. Make sure that passes required by
107 /// P are run before P is run. Update analysis info maintained by
108 /// the manager. Remove dead passes. This is a recursive function.
109 void schedulePass(Pass *P, Pass *PM);
110
111 /// This is implemented by top level pass manager and used by
112 /// schedulePass() to add analysis info passes that are not available.
113 virtual void addTopLevelPass(Pass *P) = 0;
114
115 /// Set pass P as the last user of the given analysis passes.
116 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
117
118 /// Collect passes whose last user is P
119 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
120
Devang Patel640c5bb2006-12-08 22:30:11 +0000121 /// Find the pass that implements Analysis AID. Search immutable
122 /// passes and all pass managers. If desired pass is not found
123 /// then return NULL.
124 Pass *findAnalysisPass(AnalysisID AID);
125
Devang Patelf33f3eb2006-12-07 19:21:29 +0000126 virtual ~PMTopLevelManager() {
127 PassManagers.clear();
128 }
129
Devang Patele0eb9d82006-12-07 20:51:18 +0000130 /// Add immutable pass and initialize it.
131 inline void addImmutablePass(ImmutablePass *P) {
132 P->initializePass();
133 ImmutablePasses.push_back(P);
134 }
135
136 inline std::vector<ImmutablePass *>& getImmutablePasses() {
137 return ImmutablePasses;
138 }
139
Devang Patelf33f3eb2006-12-07 19:21:29 +0000140private:
141
142 /// Collection of pass managers
143 std::vector<Pass *> PassManagers;
144
145 // Map to keep track of last user of the analysis pass.
146 // LastUser->second is the last user of Lastuser->first.
147 std::map<Pass *, Pass *> LastUser;
Devang Patele0eb9d82006-12-07 20:51:18 +0000148
149 /// Immutable passes are managed by top level manager.
150 std::vector<ImmutablePass *> ImmutablePasses;
Devang Patelf33f3eb2006-12-07 19:21:29 +0000151};
152
153/// Set pass P as the last user of the given analysis passes.
154void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
155 Pass *P) {
156
157 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
158 E = AnalysisPasses.end(); I != E; ++I) {
159 Pass *AP = *I;
160 LastUser[AP] = P;
161 // If AP is the last user of other passes then make P last user of
162 // such passes.
163 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
164 LUE = LastUser.end(); LUI != LUE; ++LUI) {
165 if (LUI->second == AP)
166 LastUser[LUI->first] = P;
167 }
168 }
169
170}
171
172/// Collect passes whose last user is P
173void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
174 Pass *P) {
175 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
176 LUE = LastUser.end(); LUI != LUE; ++LUI)
177 if (LUI->second == P)
178 LastUses.push_back(LUI->first);
179}
180
Devang Patelde124182006-12-07 21:10:57 +0000181/// Schedule pass P for execution. Make sure that passes required by
182/// P are run before P is run. Update analysis info maintained by
183/// the manager. Remove dead passes. This is a recursive function.
184void PMTopLevelManager::schedulePass(Pass *P, Pass *PM) {
185
186 // TODO : Allocate function manager for this pass, other wise required set
187 // may be inserted into previous function manager
188
189 AnalysisUsage AnUsage;
190 P->getAnalysisUsage(AnUsage);
191 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
192 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
193 E = RequiredSet.end(); I != E; ++I) {
194
Devang Patel640c5bb2006-12-08 22:30:11 +0000195 Pass *AnalysisPass = findAnalysisPass(*I);
Devang Patelde124182006-12-07 21:10:57 +0000196 if (!AnalysisPass) {
197 // Schedule this analysis run first.
198 AnalysisPass = (*I)->createPass();
199 schedulePass(AnalysisPass, PM);
200 }
201 }
202
203 // Now all required passes are available.
204 addTopLevelPass(P);
205}
206
Devang Patel640c5bb2006-12-08 22:30:11 +0000207/// Find the pass that implements Analysis AID. Search immutable
208/// passes and all pass managers. If desired pass is not found
209/// then return NULL.
210Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
211
212 Pass *P = NULL;
213 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
214 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
215 const PassInfo *PI = (*I)->getPassInfo();
216 if (PI == AID)
217 P = *I;
218
219 // If Pass not found then check the interfaces implemented by Immutable Pass
220 if (!P) {
221 const std::vector<const PassInfo*> &ImmPI =
222 PI->getInterfacesImplemented();
223 for (unsigned Index = 0, End = ImmPI.size();
224 P == NULL && Index != End; ++Index)
225 if (ImmPI[Index] == AID)
226 P = *I;
227 }
228 }
229
230 if (P)
231 return P;
232
233 // Check pass managers;
234 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
235 E = PassManagers.end(); P == NULL && I != E; ++I)
236 P = NULL; // FIXME: (*I)->findAnalysisPass(AID, false /* Search downward */);
237
238 return P;
239}
240
Devang Patelf3827bc2006-12-07 19:54:15 +0000241//===----------------------------------------------------------------------===//
242// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000243
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000244/// PMDataManager provides the common place to manage the analysis data
245/// used by pass managers.
246class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000247
248public:
249
Devang Patel4c36e6b2006-12-07 23:24:58 +0000250 PMDataManager(int D) : TPM(NULL), Depth(D) {
Devang Patelf3827bc2006-12-07 19:54:15 +0000251 initializeAnalysisInfo();
252 }
253
Devang Patela9844592006-11-11 01:31:05 +0000254 /// Return true IFF pass P's required analysis set does not required new
255 /// manager.
256 bool manageablePass(Pass *P);
257
Devang Patelf60b5d92006-11-14 01:59:59 +0000258 Pass *getAnalysisPass(AnalysisID AID) const {
259
260 std::map<AnalysisID, Pass*>::const_iterator I =
261 AvailableAnalysis.find(AID);
262
263 if (I != AvailableAnalysis.end())
264 return NULL;
265 else
266 return I->second;
Devang Patelebba9702006-11-13 22:40:09 +0000267 }
Devang Patela9844592006-11-11 01:31:05 +0000268
Devang Patela9844592006-11-11 01:31:05 +0000269 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000270 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000271
Devang Patela9844592006-11-11 01:31:05 +0000272 /// Remove Analysis that is not preserved by the pass
273 void removeNotPreservedAnalysis(Pass *P);
274
275 /// Remove dead passes
Devang Patelca189262006-11-14 03:05:08 +0000276 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000277
Devang Patel8f677ce2006-12-07 18:47:25 +0000278 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000279 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
280 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000281
Devang Patel1d6267c2006-12-07 23:05:44 +0000282 /// Initialize available analysis information.
Devang Patela6b6dcb2006-12-07 18:41:09 +0000283 void initializeAnalysisInfo() {
Devang Patelbc03f132006-12-07 23:55:10 +0000284 ForcedLastUses.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000285 AvailableAnalysis.clear();
Devang Patelb3900322006-12-07 21:02:08 +0000286
287 // Include immutable passes into AvailableAnalysis vector.
288 std::vector<ImmutablePass *> &ImmutablePasses = TPM->getImmutablePasses();
289 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
290 E = ImmutablePasses.end(); I != E; ++I)
291 recordAvailableAnalysis(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000292 }
293
Devang Patel1d6267c2006-12-07 23:05:44 +0000294 /// Populate RequiredPasses with the analysis pass that are required by
295 /// pass P.
296 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
297 Pass *P);
298
299 /// All Required analyses should be available to the pass as it runs! Here
300 /// we fill in the AnalysisImpls member of the pass so that it can
301 /// successfully use the getAnalysis() method to retrieve the
302 /// implementations it needs.
303 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000304
Devang Patel640c5bb2006-12-08 22:30:11 +0000305 /// Find the pass that implements Analysis AID. If desired pass is not found
306 /// then return NULL.
307 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
308
Devang Patel8cad70d2006-11-11 01:51:02 +0000309 inline std::vector<Pass *>::iterator passVectorBegin() {
310 return PassVector.begin();
311 }
312
313 inline std::vector<Pass *>::iterator passVectorEnd() {
314 return PassVector.end();
315 }
316
Devang Patelf3827bc2006-12-07 19:54:15 +0000317 // Access toplevel manager
318 PMTopLevelManager *getTopLevelManager() { return TPM; }
319 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
320
Devang Patel4c36e6b2006-12-07 23:24:58 +0000321 unsigned getDepth() { return Depth; }
322
Devang Patelbc03f132006-12-07 23:55:10 +0000323protected:
324
325 // Collection of pass whose last user asked this manager to claim
326 // last use. If a FunctionPass F is the last user of ModulePass info M
327 // then the F's manager, not F, records itself as a last user of M.
328 std::vector<Pass *> ForcedLastUses;
329
330 // Top level manager.
331 // TODO : Make it a reference.
332 PMTopLevelManager *TPM;
333
Devang Patela9844592006-11-11 01:31:05 +0000334private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000335 // Set of available Analysis. This information is used while scheduling
336 // pass. If a pass requires an analysis which is not not available then
337 // equired analysis pass is scheduled to run before the pass itself is
338 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000339 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000340
341 // Collection of pass that are managed by this manager
342 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000343
Devang Patel4c36e6b2006-12-07 23:24:58 +0000344 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000345};
346
Devang Patelca58e352006-11-08 10:05:38 +0000347/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
348/// pass together and sequence them to process one basic block before
349/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000350class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000351 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000352
353public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000354 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
Devang Patelca58e352006-11-08 10:05:38 +0000355
356 /// Add a pass into a passmanager queue.
357 bool addPass(Pass *p);
358
359 /// Execute all of the passes scheduled for execution. Keep track of
360 /// whether any of the passes modifies the function, and if so, return true.
361 bool runOnFunction(Function &F);
362
Devang Patelebba9702006-11-13 22:40:09 +0000363 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000364 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000365 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000366
Devang Patelf9d96b92006-12-07 19:57:52 +0000367 /// Pass Manager itself does not invalidate any analysis info.
368 void getAnalysisUsage(AnalysisUsage &Info) const {
369 Info.setPreservesAll();
370 }
371
Devang Patel475c4532006-12-08 00:59:05 +0000372 bool doInitialization(Module &M);
373 bool doInitialization(Function &F);
374 bool doFinalization(Module &M);
375 bool doFinalization(Function &F);
376
Devang Patelca58e352006-11-08 10:05:38 +0000377};
378
Devang Patel4e12f862006-11-08 10:44:40 +0000379/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000380/// It batches all function passes and basic block pass managers together and
381/// sequence them to process one function at a time before processing next
382/// function.
Devang Patelabcd1d32006-12-07 21:27:23 +0000383class FunctionPassManagerImpl_New : public ModulePass,
384 public PMDataManager,
385 public PMTopLevelManager {
Devang Patelca58e352006-11-08 10:05:38 +0000386public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000387 FunctionPassManagerImpl_New(ModuleProvider *P, int D) :
388 PMDataManager(D) { /* TODO */ }
389 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
Devang Patelca58e352006-11-08 10:05:38 +0000390 activeBBPassManager = NULL;
391 }
Devang Patel4e12f862006-11-08 10:44:40 +0000392 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000393
Devang Patelabcd1d32006-12-07 21:27:23 +0000394 inline void addTopLevelPass(Pass *P) {
395 addPass(P);
396 }
397
Devang Patelca58e352006-11-08 10:05:38 +0000398 /// add - Add a pass to the queue of passes to run. This passes
399 /// ownership of the Pass to the PassManager. When the
400 /// PassManager_X is destroyed, the pass will be destroyed as well, so
401 /// there is no need to delete the pass. (TODO delete passes.)
402 /// This implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000403 void add(Pass *P) {
404 schedulePass(P, this);
405 }
Devang Patelca58e352006-11-08 10:05:38 +0000406
407 /// Add pass into the pass manager queue.
408 bool addPass(Pass *P);
409
410 /// Execute all of the passes scheduled for execution. Keep
411 /// track of whether any of the passes modifies the function, and if
412 /// so, return true.
413 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000414 bool runOnFunction(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000415
Devang Patelebba9702006-11-13 22:40:09 +0000416 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000417 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000418 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000419
Devang Patelff631ae2006-11-15 01:27:05 +0000420 /// doInitialization - Run all of the initializers for the function passes.
421 ///
422 bool doInitialization(Module &M);
423
424 /// doFinalization - Run all of the initializers for the function passes.
425 ///
426 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000427
428 /// Pass Manager itself does not invalidate any analysis info.
429 void getAnalysisUsage(AnalysisUsage &Info) const {
430 Info.setPreservesAll();
431 }
432
Devang Patelca58e352006-11-08 10:05:38 +0000433private:
Devang Patelca58e352006-11-08 10:05:38 +0000434 // Active Pass Managers
435 BasicBlockPassManager_New *activeBBPassManager;
436};
437
438/// ModulePassManager_New manages ModulePasses and function pass managers.
439/// It batches all Module passes passes and function pass managers together and
440/// sequence them to process one module.
Devang Patelbc03f132006-12-07 23:55:10 +0000441class ModulePassManager_New : public Pass,
442 public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000443
444public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000445 ModulePassManager_New(int D) : PMDataManager(D) {
446 activeFunctionPassManager = NULL;
447 }
Devang Patelca58e352006-11-08 10:05:38 +0000448
449 /// Add a pass into a passmanager queue.
450 bool addPass(Pass *p);
451
452 /// run - Execute all of the passes scheduled for execution. Keep track of
453 /// whether any of the passes modifies the module, and if so, return true.
454 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000455
456 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000457 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000458 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelf9d96b92006-12-07 19:57:52 +0000459
460 /// Pass Manager itself does not invalidate any analysis info.
461 void getAnalysisUsage(AnalysisUsage &Info) const {
462 Info.setPreservesAll();
463 }
464
Devang Patelca58e352006-11-08 10:05:38 +0000465private:
Devang Patelca58e352006-11-08 10:05:38 +0000466 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000467 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000468};
469
Devang Patel376fefa2006-11-08 10:29:57 +0000470/// PassManager_New manages ModulePassManagers
Devang Patel31217af2006-12-07 21:32:57 +0000471class PassManagerImpl_New : public Pass,
472 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000473 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000474
475public:
476
Devang Patel4c36e6b2006-12-07 23:24:58 +0000477 PassManagerImpl_New(int D) : PMDataManager(D) {}
478
Devang Patel376fefa2006-11-08 10:29:57 +0000479 /// add - Add a pass to the queue of passes to run. This passes ownership of
480 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
481 /// will be destroyed as well, so there is no need to delete the pass. This
482 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000483 void add(Pass *P) {
484 schedulePass(P, this);
485 }
Devang Patel376fefa2006-11-08 10:29:57 +0000486
487 /// run - Execute all of the passes scheduled for execution. Keep track of
488 /// whether any of the passes modifies the module, and if so, return true.
489 bool run(Module &M);
490
Devang Patelebba9702006-11-13 22:40:09 +0000491 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000492 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000493 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000494
Devang Patelf9d96b92006-12-07 19:57:52 +0000495 /// Pass Manager itself does not invalidate any analysis info.
496 void getAnalysisUsage(AnalysisUsage &Info) const {
497 Info.setPreservesAll();
498 }
499
Devang Patelabcd1d32006-12-07 21:27:23 +0000500 inline void addTopLevelPass(Pass *P) {
501 addPass(P);
502 }
503
Devang Patel376fefa2006-11-08 10:29:57 +0000504private:
505
Devang Patelde124182006-12-07 21:10:57 +0000506 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000507 bool addPass(Pass *p);
508
Devang Patel376fefa2006-11-08 10:29:57 +0000509 // Collection of pass managers
510 std::vector<ModulePassManager_New *> PassManagers;
511
Devang Patel376fefa2006-11-08 10:29:57 +0000512 // Active Pass Manager
513 ModulePassManager_New *activeManager;
514};
515
Devang Patelca58e352006-11-08 10:05:38 +0000516} // End of llvm namespace
517
Devang Patela1514cb2006-12-07 19:39:39 +0000518//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000519// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000520
Devang Pateld65e9e92006-11-08 01:31:28 +0000521/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000522/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000523bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000524
Devang Patel8f677ce2006-12-07 18:47:25 +0000525 // TODO
526 // If this pass is not preserving information that is required by a
527 // pass maintained by higher level pass manager then do not insert
528 // this pass into current manager. Use new manager. For example,
529 // For example, If FunctionPass F is not preserving ModulePass Info M1
530 // that is used by another ModulePass M2 then do not insert F in
531 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000532 return true;
533}
534
Devang Patel643676c2006-11-11 01:10:19 +0000535/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000536void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000537
Devang Patel643676c2006-11-11 01:10:19 +0000538 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000539 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000540
Devang Patele9976aa2006-12-07 19:33:53 +0000541 //This pass is the current implementation of all of the interfaces it
542 //implements as well.
543 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
544 for (unsigned i = 0, e = II.size(); i != e; ++i)
545 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000546 }
547}
548
Devang Patelf68a3492006-11-07 22:35:17 +0000549/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000550void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000551 AnalysisUsage AnUsage;
552 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000553
Devang Patel2e169c32006-12-07 20:03:49 +0000554 if (AnUsage.getPreservesAll())
555 return;
556
557 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000558 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000559 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000560 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000561 PreservedSet.end()) {
562 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000563 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000564 AvailableAnalysis.erase(J);
565 }
566 }
Devang Patelf68a3492006-11-07 22:35:17 +0000567}
568
Devang Patelca189262006-11-14 03:05:08 +0000569/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000570void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patel17ad0962006-12-08 00:37:52 +0000571
572 std::vector<Pass *> DeadPasses;
573 TPM->collectLastUses(DeadPasses, P);
574
575 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
576 E = DeadPasses.end(); I != E; ++I) {
577 (*I)->releaseMemory();
578
579 std::map<AnalysisID, Pass*>::iterator Pos =
580 AvailableAnalysis.find((*I)->getPassInfo());
581
Devang Patel475c4532006-12-08 00:59:05 +0000582 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000583 if (Pos != AvailableAnalysis.end())
584 AvailableAnalysis.erase(Pos);
585 }
Devang Patelca189262006-11-14 03:05:08 +0000586}
587
Devang Patel8f677ce2006-12-07 18:47:25 +0000588/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000589/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000590void PMDataManager::addPassToManager(Pass *P,
591 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000592
Devang Patel90b05e02006-11-11 02:04:19 +0000593 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000594
595 // At the moment, this pass is the last user of all required passes.
596 std::vector<Pass *> LastUses;
597 std::vector<Pass *> RequiredPasses;
598 unsigned PDepth = this->getDepth();
599
600 collectRequiredAnalysisPasses(RequiredPasses, P);
601 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
602 E = RequiredPasses.end(); I != E; ++I) {
603 Pass *PRequired = *I;
604 unsigned RDepth = 0;
605 //FIXME: RDepth = PRequired->getResolver()->getDepth();
606 if (PDepth == RDepth)
607 LastUses.push_back(PRequired);
608 else if (PDepth > RDepth) {
609 // Let the parent claim responsibility of last use
610 ForcedLastUses.push_back(PRequired);
611 } else {
612 // Note : This feature is not yet implemented
613 assert (0 &&
614 "Unable to handle Pass that requires lower level Analysis pass");
615 }
616 }
617
618 if (!LastUses.empty())
619 TPM->setLastUser(LastUses, P);
620
Devang Patel17bff0d2006-12-07 22:09:36 +0000621 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000622 // Remove the analysis not preserved by this pass
Devang Patel17bff0d2006-12-07 22:09:36 +0000623 initializeAnalysisImpl(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000624 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000625 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000626 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000627
628 // Add pass
629 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000630}
631
Devang Patel1d6267c2006-12-07 23:05:44 +0000632/// Populate RequiredPasses with the analysis pass that are required by
633/// pass P.
634void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
635 Pass *P) {
636 AnalysisUsage AnUsage;
637 P->getAnalysisUsage(AnUsage);
638 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
639 for (std::vector<AnalysisID>::const_iterator
640 I = RequiredSet.begin(), E = RequiredSet.end();
641 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000642 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000643 assert (AnalysisPass && "Analysis pass is not available");
644 RP.push_back(AnalysisPass);
645 }
646}
647
Devang Patel07f4f582006-11-14 21:49:36 +0000648// All Required analyses should be available to the pass as it runs! Here
649// we fill in the AnalysisImpls member of the pass so that it can
650// successfully use the getAnalysis() method to retrieve the
651// implementations it needs.
652//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000653void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000654 AnalysisUsage AnUsage;
655 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000656
657 for (std::vector<const PassInfo *>::const_iterator
658 I = AnUsage.getRequiredSet().begin(),
659 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000660 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000661 if (Impl == 0)
662 assert(0 && "Analysis used but not available!");
663 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
664 }
665}
666
Devang Patel640c5bb2006-12-08 22:30:11 +0000667/// Find the pass that implements Analysis AID. If desired pass is not found
668/// then return NULL.
669Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
670
671 // Check if AvailableAnalysis map has one entry.
672 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
673
674 if (I != AvailableAnalysis.end())
675 return I->second;
676
677 // Search Parents through TopLevelManager
678 if (SearchParent)
679 return TPM->findAnalysisPass(AID);
680
681 // FIXME : This is expensive and requires. Need to check only managers not all passes.
682 // One solution is to collect managers in advance at TPM level.
683 Pass *P = NULL;
684 for(std::vector<Pass *>::iterator I = passVectorBegin(),
685 E = passVectorEnd(); P == NULL && I!= E; ++I )
686 P = NULL; // FIXME : P = (*I)->getResolver()->getAnalysisToUpdate(AID, false /* Do not search parents again */);
687
688 return P;
689}
690
Devang Patela1514cb2006-12-07 19:39:39 +0000691//===----------------------------------------------------------------------===//
692// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000693
Devang Pateld65e9e92006-11-08 01:31:28 +0000694/// Add pass P into PassVector and return true. If this pass is not
695/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000696bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000697BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000698
699 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
700 if (!BP)
701 return false;
702
Devang Patel3c8eb622006-11-07 22:56:50 +0000703 // If this pass does not preserve anlysis that is used by other passes
704 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000705 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000706 return false;
707
Devang Patel8cad70d2006-11-11 01:51:02 +0000708 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000709
Devang Patel6e5a1132006-11-07 21:31:57 +0000710 return true;
711}
712
713/// Execute all of the passes scheduled for execution by invoking
714/// runOnBasicBlock method. Keep track of whether any of the passes modifies
715/// the function, and if so, return true.
716bool
717BasicBlockPassManager_New::runOnFunction(Function &F) {
718
Devang Patele9585592006-12-08 01:38:28 +0000719 bool Changed = doInitialization(F);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000720 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000721
Devang Patel6e5a1132006-11-07 21:31:57 +0000722 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000723 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
724 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000725 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000726
Devang Patel6e5a1132006-11-07 21:31:57 +0000727 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
728 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000729 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000730 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000731 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000732 }
Devang Patele9585592006-12-08 01:38:28 +0000733 return Changed | doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000734}
735
Devang Patelebba9702006-11-13 22:40:09 +0000736/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000737/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000738Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
739 return getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000740}
741
Devang Patel475c4532006-12-08 00:59:05 +0000742// Implement doInitialization and doFinalization
743inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
744 bool Changed = false;
745
746 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
747 e = passVectorEnd(); itr != e; ++itr) {
748 Pass *P = *itr;
749 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
750 Changed |= BP->doInitialization(M);
751 }
752
753 return Changed;
754}
755
756inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
757 bool Changed = false;
758
759 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
760 e = passVectorEnd(); itr != e; ++itr) {
761 Pass *P = *itr;
762 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
763 Changed |= BP->doFinalization(M);
764 }
765
766 return Changed;
767}
768
769inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
770 bool Changed = false;
771
772 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
773 e = passVectorEnd(); itr != e; ++itr) {
774 Pass *P = *itr;
775 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
776 Changed |= BP->doInitialization(F);
777 }
778
779 return Changed;
780}
781
782inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
783 bool Changed = false;
784
785 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
786 e = passVectorEnd(); itr != e; ++itr) {
787 Pass *P = *itr;
788 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
789 Changed |= BP->doFinalization(F);
790 }
791
792 return Changed;
793}
794
795
Devang Patela1514cb2006-12-07 19:39:39 +0000796//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000797// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000798
Devang Patel4e12f862006-11-08 10:44:40 +0000799/// Create new Function pass manager
800FunctionPassManager_New::FunctionPassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000801 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel4e12f862006-11-08 10:44:40 +0000802}
803
Devang Patel1f653682006-12-08 18:57:16 +0000804FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
805 FPM = new FunctionPassManagerImpl_New(0);
806 MP = P;
807}
808
Devang Patel4e12f862006-11-08 10:44:40 +0000809/// add - Add a pass to the queue of passes to run. This passes
810/// ownership of the Pass to the PassManager. When the
811/// PassManager_X is destroyed, the pass will be destroyed as well, so
812/// there is no need to delete the pass. (TODO delete passes.)
813/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000814void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000815 FPM->add(P);
816}
817
818/// Execute all of the passes scheduled for execution. Keep
819/// track of whether any of the passes modifies the function, and if
820/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000821bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000822 return FPM->runOnModule(M);
823}
824
Devang Patel9f3083e2006-11-15 19:39:54 +0000825/// run - Execute all of the passes scheduled for execution. Keep
826/// track of whether any of the passes modifies the function, and if
827/// so, return true.
828///
829bool FunctionPassManager_New::run(Function &F) {
830 std::string errstr;
831 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000832 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000833 abort();
834 }
835 return FPM->runOnFunction(F);
836}
837
838
Devang Patelff631ae2006-11-15 01:27:05 +0000839/// doInitialization - Run all of the initializers for the function passes.
840///
841bool FunctionPassManager_New::doInitialization() {
842 return FPM->doInitialization(*MP->getModule());
843}
844
845/// doFinalization - Run all of the initializers for the function passes.
846///
847bool FunctionPassManager_New::doFinalization() {
848 return FPM->doFinalization(*MP->getModule());
849}
850
Devang Patela1514cb2006-12-07 19:39:39 +0000851//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000852// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000853
Devang Patel0c2012f2006-11-07 21:49:50 +0000854/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
855/// either use it into active basic block pass manager or create new basic
856/// block pass manager to handle pass P.
857bool
Devang Patel4e12f862006-11-08 10:44:40 +0000858FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000859
860 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
861 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
862
Devang Patel4949fe02006-12-07 22:34:21 +0000863 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000864
Devang Patel4949fe02006-12-07 22:34:21 +0000865 // If active manager exists then clear its analysis info.
866 if (activeBBPassManager)
867 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000868
Devang Patel4949fe02006-12-07 22:34:21 +0000869 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +0000870 activeBBPassManager =
871 new BasicBlockPassManager_New(getDepth() + 1);
Devang Patel90b05e02006-11-11 02:04:19 +0000872 addPassToManager(activeBBPassManager, false);
Devang Patel4949fe02006-12-07 22:34:21 +0000873
874 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000875 if (!activeBBPassManager->addPass(BP))
876 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000877 }
Devang Patelbc03f132006-12-07 23:55:10 +0000878
879 if (!ForcedLastUses.empty())
880 TPM->setLastUser(ForcedLastUses, this);
881
Devang Patel0c2012f2006-11-07 21:49:50 +0000882 return true;
883 }
884
885 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
886 if (!FP)
887 return false;
888
Devang Patel3c8eb622006-11-07 22:56:50 +0000889 // If this pass does not preserve anlysis that is used by other passes
890 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000891 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000892 return false;
893
Devang Patel8cad70d2006-11-11 01:51:02 +0000894 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +0000895
896 // If active manager exists then clear its analysis info.
897 if (activeBBPassManager) {
898 activeBBPassManager->initializeAnalysisInfo();
899 activeBBPassManager = NULL;
900 }
901
Devang Patel0c2012f2006-11-07 21:49:50 +0000902 return true;
903}
904
905/// Execute all of the passes scheduled for execution by invoking
906/// runOnFunction method. Keep track of whether any of the passes modifies
907/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000908bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000909
Devang Patel0e29e292006-12-08 19:04:09 +0000910 bool Changed = doInitialization(M);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000911 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000912
Devang Patel0c2012f2006-11-07 21:49:50 +0000913 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel19290892006-12-08 19:03:05 +0000914 this->runOnFunction(*I);
915
Devang Patel0e29e292006-12-08 19:04:09 +0000916 return Changed | doFinalization(M);
Devang Patel0c2012f2006-11-07 21:49:50 +0000917}
918
Devang Patel9f3083e2006-11-15 19:39:54 +0000919/// Execute all of the passes scheduled for execution by invoking
920/// runOnFunction method. Keep track of whether any of the passes modifies
921/// the function, and if so, return true.
922bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
923
924 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000925 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +0000926
927 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
928 e = passVectorEnd(); itr != e; ++itr) {
929 Pass *P = *itr;
930
Devang Patel9f3083e2006-11-15 19:39:54 +0000931 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
932 Changed |= FP->runOnFunction(F);
933 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000934 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +0000935 removeDeadPasses(P);
936 }
937 return Changed;
938}
939
940
Devang Patelebba9702006-11-13 22:40:09 +0000941/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000942/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000943Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000944
Devang Patel3f0832a2006-11-14 02:54:23 +0000945 Pass *P = getAnalysisPass(AID);
946 if (P)
947 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000948
949 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000950 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000951 return activeBBPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000952
953 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000954 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000955}
Devang Patel0c2012f2006-11-07 21:49:50 +0000956
Devang Patelff631ae2006-11-15 01:27:05 +0000957inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
958 bool Changed = false;
959
960 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
961 e = passVectorEnd(); itr != e; ++itr) {
962 Pass *P = *itr;
963
964 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
965 Changed |= FP->doInitialization(M);
966 }
967
968 return Changed;
969}
970
971inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
972 bool Changed = false;
973
974 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
975 e = passVectorEnd(); itr != e; ++itr) {
976 Pass *P = *itr;
977
978 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
979 Changed |= FP->doFinalization(M);
980 }
981
Devang Patelff631ae2006-11-15 01:27:05 +0000982 return Changed;
983}
984
Devang Patela1514cb2006-12-07 19:39:39 +0000985//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +0000986// ModulePassManager implementation
987
988/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000989/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000990/// is not manageable by this manager.
991bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000992ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000993
994 // If P is FunctionPass then use function pass maanager.
995 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
996
Devang Patel640c5bb2006-12-08 22:30:11 +0000997 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +0000998
Devang Patel4949fe02006-12-07 22:34:21 +0000999 // If active manager exists then clear its analysis info.
1000 if (activeFunctionPassManager)
1001 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001002
Devang Patel4949fe02006-12-07 22:34:21 +00001003 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001004 activeFunctionPassManager =
1005 new FunctionPassManagerImpl_New(getDepth() + 1);
Devang Patel90b05e02006-11-11 02:04:19 +00001006 addPassToManager(activeFunctionPassManager, false);
Devang Patel4949fe02006-12-07 22:34:21 +00001007
1008 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001009 if (!activeFunctionPassManager->addPass(FP))
1010 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +00001011 }
Devang Patelbc03f132006-12-07 23:55:10 +00001012
1013 if (!ForcedLastUses.empty())
1014 TPM->setLastUser(ForcedLastUses, this);
1015
Devang Patel05e1a972006-11-07 22:03:15 +00001016 return true;
1017 }
1018
1019 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1020 if (!MP)
1021 return false;
1022
Devang Patel3c8eb622006-11-07 22:56:50 +00001023 // If this pass does not preserve anlysis that is used by other passes
1024 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001025 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001026 return false;
1027
Devang Patel8cad70d2006-11-11 01:51:02 +00001028 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +00001029 // If active manager exists then clear its analysis info.
1030 if (activeFunctionPassManager) {
1031 activeFunctionPassManager->initializeAnalysisInfo();
1032 activeFunctionPassManager = NULL;
1033 }
1034
Devang Patel05e1a972006-11-07 22:03:15 +00001035 return true;
1036}
1037
1038
1039/// Execute all of the passes scheduled for execution by invoking
1040/// runOnModule method. Keep track of whether any of the passes modifies
1041/// the module, and if so, return true.
1042bool
1043ModulePassManager_New::runOnModule(Module &M) {
1044 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +00001045 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001046
Devang Patel8cad70d2006-11-11 01:51:02 +00001047 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1048 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +00001049 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +00001050
Devang Patel05e1a972006-11-07 22:03:15 +00001051 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1052 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +00001053 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001054 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +00001055 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001056 }
1057 return Changed;
1058}
1059
Devang Patelebba9702006-11-13 22:40:09 +00001060/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +00001061/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +00001062Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +00001063
Devang Patel3f0832a2006-11-14 02:54:23 +00001064 Pass *P = getAnalysisPass(AID);
1065 if (P)
1066 return P;
Devang Patelebba9702006-11-13 22:40:09 +00001067
1068 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +00001069 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +00001070 return activeFunctionPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +00001071
1072 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +00001073 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +00001074}
1075
Devang Patela1514cb2006-12-07 19:39:39 +00001076//===----------------------------------------------------------------------===//
1077// PassManagerImpl implementation
1078
Devang Patelebba9702006-11-13 22:40:09 +00001079/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +00001080/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +00001081Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +00001082
Devang Patel3f0832a2006-11-14 02:54:23 +00001083 Pass *P = NULL;
Devang Patel70868442006-11-13 22:53:19 +00001084 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
Devang Patel3f0832a2006-11-14 02:54:23 +00001085 e = PassManagers.end(); !P && itr != e; ++itr)
1086 P = (*itr)->getAnalysisPassFromManager(AID);
1087 return P;
Devang Patelebba9702006-11-13 22:40:09 +00001088}
1089
Devang Patelc290c8a2006-11-07 22:23:34 +00001090// PassManager_New implementation
1091/// Add P into active pass manager or use new module pass manager to
1092/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001093bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001094
Devang Patel6c9f5482006-11-11 00:42:16 +00001095 if (!activeManager || !activeManager->addPass(P)) {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001096 activeManager = new ModulePassManager_New(getDepth() + 1);
Devang Patelc290c8a2006-11-07 22:23:34 +00001097 PassManagers.push_back(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001098 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001099 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001100 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001101}
1102
1103/// run - Execute all of the passes scheduled for execution. Keep track of
1104/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001105bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001106
Devang Patelc290c8a2006-11-07 22:23:34 +00001107 bool Changed = false;
1108 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
1109 e = PassManagers.end(); itr != e; ++itr) {
1110 ModulePassManager_New *pm = *itr;
1111 Changed |= pm->runOnModule(M);
1112 }
1113 return Changed;
1114}
Devang Patel376fefa2006-11-08 10:29:57 +00001115
Devang Patela1514cb2006-12-07 19:39:39 +00001116//===----------------------------------------------------------------------===//
1117// PassManager implementation
1118
Devang Patel376fefa2006-11-08 10:29:57 +00001119/// Create new pass manager
1120PassManager_New::PassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001121 PM = new PassManagerImpl_New(0);
Devang Patel376fefa2006-11-08 10:29:57 +00001122}
1123
1124/// add - Add a pass to the queue of passes to run. This passes ownership of
1125/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1126/// will be destroyed as well, so there is no need to delete the pass. This
1127/// implies that all passes MUST be allocated with 'new'.
1128void
1129PassManager_New::add(Pass *P) {
1130 PM->add(P);
1131}
1132
1133/// run - Execute all of the passes scheduled for execution. Keep track of
1134/// whether any of the passes modifies the module, and if so, return true.
1135bool
1136PassManager_New::run(Module &M) {
1137 return PM->run(M);
1138}
1139