blob: d0d54d4c4f736eede2e7e2c77e023305b73ca98b [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"
Devang Patela9844592006-11-11 01:31:05 +000018#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000019#include <map>
Devang Patel6e5a1132006-11-07 21:31:57 +000020
21using namespace llvm;
22
Devang Patelca58e352006-11-08 10:05:38 +000023namespace llvm {
24
Devang Patela9844592006-11-11 01:31:05 +000025/// CommonPassManagerImpl helps pass manager analysis required by
26/// the managed passes. It provides methods to add/remove analysis
27/// available and query if certain analysis is available or not.
Devang Patel42add712006-11-15 01:11:27 +000028class CommonPassManagerImpl {
Devang Patela9844592006-11-11 01:31:05 +000029
30public:
31
32 /// Return true IFF pass P's required analysis set does not required new
33 /// manager.
34 bool manageablePass(Pass *P);
35
Devang Patelf60b5d92006-11-14 01:59:59 +000036 Pass *getAnalysisPass(AnalysisID AID) const {
37
38 std::map<AnalysisID, Pass*>::const_iterator I =
39 AvailableAnalysis.find(AID);
40
41 if (I != AvailableAnalysis.end())
42 return NULL;
43 else
44 return I->second;
Devang Patelebba9702006-11-13 22:40:09 +000045 }
Devang Patela9844592006-11-11 01:31:05 +000046
47 /// Augment RequiredAnalysis by adding analysis required by pass P.
48 void noteDownRequiredAnalysis(Pass *P);
49
50 /// Augment AvailableAnalysis by adding analysis made available by pass P.
51 void noteDownAvailableAnalysis(Pass *P);
52
Devang Patela9844592006-11-11 01:31:05 +000053 /// Remove Analysis that is not preserved by the pass
54 void removeNotPreservedAnalysis(Pass *P);
55
56 /// Remove dead passes
Devang Patelca189262006-11-14 03:05:08 +000057 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +000058
Devang Patel8cad70d2006-11-11 01:51:02 +000059 /// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +000060 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
61 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +000062
Devang Patel050ec722006-11-14 01:23:29 +000063 /// Clear analysis vectors RequiredAnalysis and AvailableAnalysis.
64 /// This is used before running passes managed by the manager.
65 void clearAnalysis() {
66 RequiredAnalysis.clear();
67 AvailableAnalysis.clear();
Devang Patel3f0832a2006-11-14 02:54:23 +000068 LastUser.clear();
Devang Patel050ec722006-11-14 01:23:29 +000069 }
70
Devang Patelf60b5d92006-11-14 01:59:59 +000071 // All Required analyses should be available to the pass as it runs! Here
72 // we fill in the AnalysisImpls member of the pass so that it can
73 // successfully use the getAnalysis() method to retrieve the
74 // implementations it needs.
75 //
Devang Patel07f4f582006-11-14 21:49:36 +000076 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +000077
Devang Patel8cad70d2006-11-11 01:51:02 +000078 inline std::vector<Pass *>::iterator passVectorBegin() {
79 return PassVector.begin();
80 }
81
82 inline std::vector<Pass *>::iterator passVectorEnd() {
83 return PassVector.end();
84 }
85
Devang Patel4a3fa4f2006-11-15 01:48:14 +000086 inline void setLastUser(Pass *P, Pass *LU) {
Devang Patel07f4f582006-11-14 21:49:36 +000087 LastUser[P] = LU;
88 // TODO : Check if pass P is available.
Devang Patel07f4f582006-11-14 21:49:36 +000089 }
Devang Patel3f0832a2006-11-14 02:54:23 +000090
Devang Patela9844592006-11-11 01:31:05 +000091private:
Devang Pateldafa4dd2006-11-14 00:03:04 +000092 // Analysis required by the passes managed by this manager. This information
93 // used while selecting pass manager during addPass. If a pass does not
94 // preserve any analysis required by other passes managed by current
95 // pass manager then new pass manager is used.
Devang Patela9844592006-11-11 01:31:05 +000096 std::vector<AnalysisID> RequiredAnalysis;
97
Devang Pateldafa4dd2006-11-14 00:03:04 +000098 // Set of available Analysis. This information is used while scheduling
99 // pass. If a pass requires an analysis which is not not available then
100 // equired analysis pass is scheduled to run before the pass itself is
101 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000102 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000103
Devang Patel3f0832a2006-11-14 02:54:23 +0000104 // Map to keep track of last user of the analysis pass.
105 // LastUser->second is the last user of Lastuser->first.
106 std::map<Pass *, Pass *> LastUser;
107
Devang Patel8cad70d2006-11-11 01:51:02 +0000108 // Collection of pass that are managed by this manager
109 std::vector<Pass *> PassVector;
Devang Patela9844592006-11-11 01:31:05 +0000110};
111
Devang Patelca58e352006-11-08 10:05:38 +0000112/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
113/// pass together and sequence them to process one basic block before
114/// processing next basic block.
Devang Patel42add712006-11-15 01:11:27 +0000115class BasicBlockPassManager_New : public CommonPassManagerImpl,
116 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000117
118public:
119 BasicBlockPassManager_New() { }
120
121 /// Add a pass into a passmanager queue.
122 bool addPass(Pass *p);
123
124 /// Execute all of the passes scheduled for execution. Keep track of
125 /// whether any of the passes modifies the function, and if so, return true.
126 bool runOnFunction(Function &F);
127
Devang Patelebba9702006-11-13 22:40:09 +0000128 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000129 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000130
Devang Patelca58e352006-11-08 10:05:38 +0000131private:
Devang Patelca58e352006-11-08 10:05:38 +0000132};
133
Devang Patel4e12f862006-11-08 10:44:40 +0000134/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000135/// It batches all function passes and basic block pass managers together and
136/// sequence them to process one function at a time before processing next
137/// function.
Devang Patel42add712006-11-15 01:11:27 +0000138class FunctionPassManagerImpl_New : public CommonPassManagerImpl,
139 public ModulePass {
Devang Patelca58e352006-11-08 10:05:38 +0000140public:
Devang Patel4e12f862006-11-08 10:44:40 +0000141 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
142 FunctionPassManagerImpl_New() {
Devang Patelca58e352006-11-08 10:05:38 +0000143 activeBBPassManager = NULL;
144 }
Devang Patel4e12f862006-11-08 10:44:40 +0000145 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000146
147 /// add - Add a pass to the queue of passes to run. This passes
148 /// ownership of the Pass to the PassManager. When the
149 /// PassManager_X is destroyed, the pass will be destroyed as well, so
150 /// there is no need to delete the pass. (TODO delete passes.)
151 /// This implies that all passes MUST be allocated with 'new'.
152 void add(Pass *P) { /* TODO*/ }
153
154 /// Add pass into the pass manager queue.
155 bool addPass(Pass *P);
156
157 /// Execute all of the passes scheduled for execution. Keep
158 /// track of whether any of the passes modifies the function, and if
159 /// so, return true.
160 bool runOnModule(Module &M);
161
Devang Patelebba9702006-11-13 22:40:09 +0000162 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000163 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000164
Devang Patelff631ae2006-11-15 01:27:05 +0000165 /// doInitialization - Run all of the initializers for the function passes.
166 ///
167 bool doInitialization(Module &M);
168
169 /// doFinalization - Run all of the initializers for the function passes.
170 ///
171 bool doFinalization(Module &M);
Devang Patelca58e352006-11-08 10:05:38 +0000172private:
Devang Patelca58e352006-11-08 10:05:38 +0000173 // Active Pass Managers
174 BasicBlockPassManager_New *activeBBPassManager;
175};
176
177/// ModulePassManager_New manages ModulePasses and function pass managers.
178/// It batches all Module passes passes and function pass managers together and
179/// sequence them to process one module.
Devang Patel0ed47792006-11-10 21:33:13 +0000180class ModulePassManager_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000181
182public:
183 ModulePassManager_New() { activeFunctionPassManager = NULL; }
184
185 /// Add a pass into a passmanager queue.
186 bool addPass(Pass *p);
187
188 /// run - Execute all of the passes scheduled for execution. Keep track of
189 /// whether any of the passes modifies the module, and if so, return true.
190 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000191
192 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000193 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelca58e352006-11-08 10:05:38 +0000194
195private:
Devang Patelca58e352006-11-08 10:05:38 +0000196 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000197 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000198};
199
Devang Patel376fefa2006-11-08 10:29:57 +0000200/// PassManager_New manages ModulePassManagers
Devang Patel0ed47792006-11-10 21:33:13 +0000201class PassManagerImpl_New : public CommonPassManagerImpl {
Devang Patel376fefa2006-11-08 10:29:57 +0000202
203public:
204
205 /// add - Add a pass to the queue of passes to run. This passes ownership of
206 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
207 /// will be destroyed as well, so there is no need to delete the pass. This
208 /// implies that all passes MUST be allocated with 'new'.
209 void add(Pass *P);
210
211 /// run - Execute all of the passes scheduled for execution. Keep track of
212 /// whether any of the passes modifies the module, and if so, return true.
213 bool run(Module &M);
214
Devang Patelebba9702006-11-13 22:40:09 +0000215 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000216 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000217
Devang Patel376fefa2006-11-08 10:29:57 +0000218private:
219
220 /// Add a pass into a passmanager queue. This is used by schedulePasses
221 bool addPass(Pass *p);
222
Devang Patel1a6eaa42006-11-11 02:22:31 +0000223 /// Schedule pass P for execution. Make sure that passes required by
224 /// P are run before P is run. Update analysis info maintained by
225 /// the manager. Remove dead passes. This is a recursive function.
226 void schedulePass(Pass *P);
227
Devang Patel376fefa2006-11-08 10:29:57 +0000228 /// Schedule all passes collected in pass queue using add(). Add all the
229 /// schedule passes into various manager's queue using addPass().
230 void schedulePasses();
231
232 // Collection of pass managers
233 std::vector<ModulePassManager_New *> PassManagers;
234
Devang Patel376fefa2006-11-08 10:29:57 +0000235 // Active Pass Manager
236 ModulePassManager_New *activeManager;
237};
238
Devang Patelca58e352006-11-08 10:05:38 +0000239} // End of llvm namespace
240
Devang Patel0ed47792006-11-10 21:33:13 +0000241// CommonPassManagerImpl implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000242
Devang Pateld65e9e92006-11-08 01:31:28 +0000243/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000244/// manager.
Devang Patel0ed47792006-11-10 21:33:13 +0000245bool CommonPassManagerImpl::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000246
247 AnalysisUsage AnUsage;
248 P->getAnalysisUsage(AnUsage);
249
Devang Patel6c9f5482006-11-11 00:42:16 +0000250 // If this pass is not preserving information that is required by the other
251 // passes managed by this manager then use new manager
252 if (!AnUsage.getPreservesAll()) {
253 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
254 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
255 E = RequiredAnalysis.end(); I != E; ++I) {
256 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
257 PreservedSet.end())
258 // This analysis is not preserved. Need new manager.
259 return false;
260 }
261 }
Devang Patelf68a3492006-11-07 22:35:17 +0000262 return true;
263}
264
Devang Patel643676c2006-11-11 01:10:19 +0000265/// Augment RequiredAnalysis by adding analysis required by pass P.
Devang Patel0ed47792006-11-10 21:33:13 +0000266void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
Devang Patel6c9f5482006-11-11 00:42:16 +0000267 AnalysisUsage AnUsage;
268 P->getAnalysisUsage(AnUsage);
269 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000270
Devang Patel6c9f5482006-11-11 00:42:16 +0000271 // FIXME: What about duplicates ?
Devang Patel349170f2006-11-11 01:24:55 +0000272 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
273 RequiredSet.end());
Devang Patel07f4f582006-11-14 21:49:36 +0000274
275 initializeAnalysisImpl(P);
Devang Patelf68a3492006-11-07 22:35:17 +0000276}
277
Devang Patel643676c2006-11-11 01:10:19 +0000278/// Augement AvailableAnalysis by adding analysis made available by pass P.
279void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000280
Devang Patel643676c2006-11-11 01:10:19 +0000281 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000282 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000283
284 //TODO This pass is the current implementation of all of the interfaces it
285 //TODO implements as well.
286 //TODO
287 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
288 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
289 //TODO CurrentAnalyses[II[i]] = P;
290 }
291}
292
Devang Patelf68a3492006-11-07 22:35:17 +0000293/// Remove Analyss not preserved by Pass P
Devang Patel0ed47792006-11-10 21:33:13 +0000294void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000295 AnalysisUsage AnUsage;
296 P->getAnalysisUsage(AnUsage);
297 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000298
Devang Patelf60b5d92006-11-14 01:59:59 +0000299 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000300 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000301 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000302 PreservedSet.end()) {
303 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000304 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000305 AvailableAnalysis.erase(J);
306 }
307 }
Devang Patelf68a3492006-11-07 22:35:17 +0000308}
309
Devang Patelca189262006-11-14 03:05:08 +0000310/// Remove analysis passes that are not used any longer
311void CommonPassManagerImpl::removeDeadPasses(Pass *P) {
312
313 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
314 E = LastUser.end(); I !=E; ++I) {
315 if (I->second == P) {
316 Pass *deadPass = I->first;
317 deadPass->releaseMemory();
318
319 std::map<AnalysisID, Pass*>::iterator Pos =
320 AvailableAnalysis.find(deadPass->getPassInfo());
321
322 assert (Pos != AvailableAnalysis.end() &&
323 "Pass is not available");
324 AvailableAnalysis.erase(Pos);
325 }
326 }
327}
328
Devang Patel8cad70d2006-11-11 01:51:02 +0000329/// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +0000330/// AvailableAnalysis appropriately if ProcessAnalysis is true.
331void CommonPassManagerImpl::addPassToManager (Pass *P,
332 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000333
Devang Patel90b05e02006-11-11 02:04:19 +0000334 if (ProcessAnalysis) {
335 // Take a note of analysis required and made available by this pass
336 noteDownRequiredAnalysis(P);
337 noteDownAvailableAnalysis(P);
338
339 // Remove the analysis not preserved by this pass
340 removeNotPreservedAnalysis(P);
341 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000342
343 // Add pass
344 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000345}
346
Devang Patel07f4f582006-11-14 21:49:36 +0000347// All Required analyses should be available to the pass as it runs! Here
348// we fill in the AnalysisImpls member of the pass so that it can
349// successfully use the getAnalysis() method to retrieve the
350// implementations it needs.
351//
352void CommonPassManagerImpl::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000353 AnalysisUsage AnUsage;
354 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000355
356 for (std::vector<const PassInfo *>::const_iterator
357 I = AnUsage.getRequiredSet().begin(),
358 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
359 Pass *Impl = getAnalysisPass(*I);
360 if (Impl == 0)
361 assert(0 && "Analysis used but not available!");
362 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
363 }
364}
365
Devang Patel6e5a1132006-11-07 21:31:57 +0000366/// BasicBlockPassManager implementation
367
Devang Pateld65e9e92006-11-08 01:31:28 +0000368/// Add pass P into PassVector and return true. If this pass is not
369/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000370bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000371BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000372
373 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
374 if (!BP)
375 return false;
376
Devang Patel3c8eb622006-11-07 22:56:50 +0000377 // If this pass does not preserve anlysis that is used by other passes
378 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000379 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000380 return false;
381
Devang Patel8cad70d2006-11-11 01:51:02 +0000382 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000383
Devang Patel6e5a1132006-11-07 21:31:57 +0000384 return true;
385}
386
387/// Execute all of the passes scheduled for execution by invoking
388/// runOnBasicBlock method. Keep track of whether any of the passes modifies
389/// the function, and if so, return true.
390bool
391BasicBlockPassManager_New::runOnFunction(Function &F) {
392
393 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000394 clearAnalysis();
395
Devang Patel6e5a1132006-11-07 21:31:57 +0000396 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000397 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
398 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000399 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000400
401 noteDownAvailableAnalysis(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000402 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
403 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000404 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000405 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000406 }
407 return Changed;
408}
409
Devang Patelebba9702006-11-13 22:40:09 +0000410/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000411Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
412 return getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000413}
414
Devang Patel0c2012f2006-11-07 21:49:50 +0000415// FunctionPassManager_New implementation
Devang Patel4e12f862006-11-08 10:44:40 +0000416/// Create new Function pass manager
417FunctionPassManager_New::FunctionPassManager_New() {
418 FPM = new FunctionPassManagerImpl_New();
419}
420
421/// add - Add a pass to the queue of passes to run. This passes
422/// ownership of the Pass to the PassManager. When the
423/// PassManager_X is destroyed, the pass will be destroyed as well, so
424/// there is no need to delete the pass. (TODO delete passes.)
425/// This implies that all passes MUST be allocated with 'new'.
426void
427FunctionPassManager_New::add(Pass *P) {
428 FPM->add(P);
429}
430
431/// Execute all of the passes scheduled for execution. Keep
432/// track of whether any of the passes modifies the function, and if
433/// so, return true.
434bool
435FunctionPassManager_New::runOnModule(Module &M) {
436 return FPM->runOnModule(M);
437}
438
Devang Patelff631ae2006-11-15 01:27:05 +0000439/// doInitialization - Run all of the initializers for the function passes.
440///
441bool FunctionPassManager_New::doInitialization() {
442 return FPM->doInitialization(*MP->getModule());
443}
444
445/// doFinalization - Run all of the initializers for the function passes.
446///
447bool FunctionPassManager_New::doFinalization() {
448 return FPM->doFinalization(*MP->getModule());
449}
450
Devang Patel4e12f862006-11-08 10:44:40 +0000451// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000452
Devang Patel0c2012f2006-11-07 21:49:50 +0000453// FunctionPassManager
454
455/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
456/// either use it into active basic block pass manager or create new basic
457/// block pass manager to handle pass P.
458bool
Devang Patel4e12f862006-11-08 10:44:40 +0000459FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000460
461 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
462 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
463
464 if (!activeBBPassManager
465 || !activeBBPassManager->addPass(BP)) {
466
467 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000468 addPassToManager(activeBBPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000469 if (!activeBBPassManager->addPass(BP))
470 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000471 }
472 return true;
473 }
474
475 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
476 if (!FP)
477 return false;
478
Devang Patel3c8eb622006-11-07 22:56:50 +0000479 // If this pass does not preserve anlysis that is used by other passes
480 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000481 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000482 return false;
483
Devang Patel8cad70d2006-11-11 01:51:02 +0000484 addPassToManager (FP);
Devang Patel0c2012f2006-11-07 21:49:50 +0000485 activeBBPassManager = NULL;
486 return true;
487}
488
489/// Execute all of the passes scheduled for execution by invoking
490/// runOnFunction method. Keep track of whether any of the passes modifies
491/// the function, and if so, return true.
492bool
Devang Patel4e12f862006-11-08 10:44:40 +0000493FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000494
495 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000496 clearAnalysis();
497
Devang Patel0c2012f2006-11-07 21:49:50 +0000498 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000499 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
500 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000501 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000502
503 noteDownAvailableAnalysis(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000504 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
505 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000506 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000507 removeDeadPasses(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000508 }
509 return Changed;
510}
511
Devang Patelebba9702006-11-13 22:40:09 +0000512/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000513Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000514
Devang Patel3f0832a2006-11-14 02:54:23 +0000515 Pass *P = getAnalysisPass(AID);
516 if (P)
517 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000518
519 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000520 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000521 return activeBBPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000522
523 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000524 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000525}
Devang Patel0c2012f2006-11-07 21:49:50 +0000526
Devang Patelff631ae2006-11-15 01:27:05 +0000527inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
528 bool Changed = false;
529
530 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
531 e = passVectorEnd(); itr != e; ++itr) {
532 Pass *P = *itr;
533
534 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
535 Changed |= FP->doInitialization(M);
536 }
537
538 return Changed;
539}
540
541inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
542 bool Changed = false;
543
544 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
545 e = passVectorEnd(); itr != e; ++itr) {
546 Pass *P = *itr;
547
548 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
549 Changed |= FP->doFinalization(M);
550 }
551
552
553 return Changed;
554}
555
556
Devang Patel05e1a972006-11-07 22:03:15 +0000557// ModulePassManager implementation
558
559/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000560/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000561/// is not manageable by this manager.
562bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000563ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000564
565 // If P is FunctionPass then use function pass maanager.
566 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
567
568 activeFunctionPassManager = NULL;
569
570 if (!activeFunctionPassManager
571 || !activeFunctionPassManager->addPass(P)) {
572
Devang Patel4e12f862006-11-08 10:44:40 +0000573 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000574 addPassToManager(activeFunctionPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000575 if (!activeFunctionPassManager->addPass(FP))
576 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000577 }
578 return true;
579 }
580
581 ModulePass *MP = dynamic_cast<ModulePass *>(P);
582 if (!MP)
583 return false;
584
Devang Patel3c8eb622006-11-07 22:56:50 +0000585 // If this pass does not preserve anlysis that is used by other passes
586 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000587 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000588 return false;
589
Devang Patel8cad70d2006-11-11 01:51:02 +0000590 addPassToManager(MP);
Devang Patel05e1a972006-11-07 22:03:15 +0000591 activeFunctionPassManager = NULL;
592 return true;
593}
594
595
596/// Execute all of the passes scheduled for execution by invoking
597/// runOnModule method. Keep track of whether any of the passes modifies
598/// the module, and if so, return true.
599bool
600ModulePassManager_New::runOnModule(Module &M) {
601 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000602 clearAnalysis();
603
Devang Patel8cad70d2006-11-11 01:51:02 +0000604 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
605 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000606 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000607
608 noteDownAvailableAnalysis(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000609 ModulePass *MP = dynamic_cast<ModulePass*>(P);
610 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000611 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000612 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000613 }
614 return Changed;
615}
616
Devang Patelebba9702006-11-13 22:40:09 +0000617/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000618Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000619
Devang Patel3f0832a2006-11-14 02:54:23 +0000620
621 Pass *P = getAnalysisPass(AID);
622 if (P)
623 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000624
625 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000626 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000627 return activeFunctionPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000628
629 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000630 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000631}
632
633/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000634Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000635
Devang Patel3f0832a2006-11-14 02:54:23 +0000636 Pass *P = NULL;
Devang Patel70868442006-11-13 22:53:19 +0000637 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
Devang Patel3f0832a2006-11-14 02:54:23 +0000638 e = PassManagers.end(); !P && itr != e; ++itr)
639 P = (*itr)->getAnalysisPassFromManager(AID);
640 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000641}
642
Devang Patel1a6eaa42006-11-11 02:22:31 +0000643/// Schedule pass P for execution. Make sure that passes required by
644/// P are run before P is run. Update analysis info maintained by
645/// the manager. Remove dead passes. This is a recursive function.
646void PassManagerImpl_New::schedulePass(Pass *P) {
647
648 AnalysisUsage AnUsage;
649 P->getAnalysisUsage(AnUsage);
650 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
651 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
652 E = RequiredSet.end(); I != E; ++I) {
653
Devang Patel3f0832a2006-11-14 02:54:23 +0000654 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
655 if (!AnalysisPass) {
Devang Patel1a6eaa42006-11-11 02:22:31 +0000656 // Schedule this analysis run first.
Devang Patel3f0832a2006-11-14 02:54:23 +0000657 AnalysisPass = (*I)->createPass();
658 schedulePass(AnalysisPass);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000659 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000660 setLastUser (AnalysisPass, P);
Devang Patel4a3fa4f2006-11-15 01:48:14 +0000661
662 // Prolong live range of analyses that are needed after an analysis pass
663 // is destroyed, for querying by subsequent passes
664 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
665 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
666 E = IDs.end(); I != E; ++I) {
667 Pass *AP = getAnalysisPassFromManager(*I);
668 assert (AP && "Analysis pass is not available");
669 setLastUser(AP, P);
670 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000671 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000672 addPass(P);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000673}
674
Devang Patelc290c8a2006-11-07 22:23:34 +0000675/// Schedule all passes from the queue by adding them in their
676/// respective manager's queue.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000677void PassManagerImpl_New::schedulePasses() {
678 for (std::vector<Pass *>::iterator I = passVectorBegin(),
679 E = passVectorEnd(); I != E; ++I)
680 schedulePass (*I);
Devang Patelc290c8a2006-11-07 22:23:34 +0000681}
682
683/// Add pass P to the queue of passes to run.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000684void PassManagerImpl_New::add(Pass *P) {
685 // Do not process Analysis now. Analysis is process while scheduling
686 // the pass vector.
Devang Pateldb789fb2006-11-11 02:06:21 +0000687 addPassToManager(P, false);
Devang Patelc290c8a2006-11-07 22:23:34 +0000688}
689
690// PassManager_New implementation
691/// Add P into active pass manager or use new module pass manager to
692/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000693bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000694
Devang Patel6c9f5482006-11-11 00:42:16 +0000695 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000696 activeManager = new ModulePassManager_New();
697 PassManagers.push_back(activeManager);
698 }
699
700 return activeManager->addPass(P);
701}
702
703/// run - Execute all of the passes scheduled for execution. Keep track of
704/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000705bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000706
707 schedulePasses();
708 bool Changed = false;
709 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
710 e = PassManagers.end(); itr != e; ++itr) {
711 ModulePassManager_New *pm = *itr;
712 Changed |= pm->runOnModule(M);
713 }
714 return Changed;
715}
Devang Patel376fefa2006-11-08 10:29:57 +0000716
717/// Create new pass manager
718PassManager_New::PassManager_New() {
719 PM = new PassManagerImpl_New();
720}
721
722/// add - Add a pass to the queue of passes to run. This passes ownership of
723/// the Pass to the PassManager. When the PassManager is destroyed, the pass
724/// will be destroyed as well, so there is no need to delete the pass. This
725/// implies that all passes MUST be allocated with 'new'.
726void
727PassManager_New::add(Pass *P) {
728 PM->add(P);
729}
730
731/// run - Execute all of the passes scheduled for execution. Keep track of
732/// whether any of the passes modifies the module, and if so, return true.
733bool
734PassManager_New::run(Module &M) {
735 return PM->run(M);
736}
737