blob: b8717ea42f7ca382f95bab8d3cb565852ce83dd1 [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 Patel9f3083e2006-11-15 19:39:54 +000020#include <iostream>
Devang Patel6e5a1132006-11-07 21:31:57 +000021
22using namespace llvm;
23
Devang Patelca58e352006-11-08 10:05:38 +000024namespace llvm {
25
Devang Patela9844592006-11-11 01:31:05 +000026/// CommonPassManagerImpl helps pass manager analysis required by
27/// the managed passes. It provides methods to add/remove analysis
28/// available and query if certain analysis is available or not.
Devang Patel42add712006-11-15 01:11:27 +000029class CommonPassManagerImpl {
Devang Patela9844592006-11-11 01:31:05 +000030
31public:
32
33 /// Return true IFF pass P's required analysis set does not required new
34 /// manager.
35 bool manageablePass(Pass *P);
36
Devang Patelf60b5d92006-11-14 01:59:59 +000037 Pass *getAnalysisPass(AnalysisID AID) const {
38
39 std::map<AnalysisID, Pass*>::const_iterator I =
40 AvailableAnalysis.find(AID);
41
42 if (I != AvailableAnalysis.end())
43 return NULL;
44 else
45 return I->second;
Devang Patelebba9702006-11-13 22:40:09 +000046 }
Devang Patela9844592006-11-11 01:31:05 +000047
48 /// Augment RequiredAnalysis by adding analysis required by pass P.
49 void noteDownRequiredAnalysis(Pass *P);
50
51 /// Augment AvailableAnalysis by adding analysis made available by pass P.
52 void noteDownAvailableAnalysis(Pass *P);
53
Devang Patela9844592006-11-11 01:31:05 +000054 /// Remove Analysis that is not preserved by the pass
55 void removeNotPreservedAnalysis(Pass *P);
56
57 /// Remove dead passes
Devang Patelca189262006-11-14 03:05:08 +000058 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +000059
Devang Patel8cad70d2006-11-11 01:51:02 +000060 /// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +000061 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
62 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +000063
Devang Patel050ec722006-11-14 01:23:29 +000064 /// Clear analysis vectors RequiredAnalysis and AvailableAnalysis.
65 /// This is used before running passes managed by the manager.
66 void clearAnalysis() {
67 RequiredAnalysis.clear();
68 AvailableAnalysis.clear();
Devang Patel3f0832a2006-11-14 02:54:23 +000069 LastUser.clear();
Devang Patel050ec722006-11-14 01:23:29 +000070 }
71
Devang Patelf60b5d92006-11-14 01:59:59 +000072 // All Required analyses should be available to the pass as it runs! Here
73 // we fill in the AnalysisImpls member of the pass so that it can
74 // successfully use the getAnalysis() method to retrieve the
75 // implementations it needs.
76 //
Devang Patel07f4f582006-11-14 21:49:36 +000077 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +000078
Devang Patel8cad70d2006-11-11 01:51:02 +000079 inline std::vector<Pass *>::iterator passVectorBegin() {
80 return PassVector.begin();
81 }
82
83 inline std::vector<Pass *>::iterator passVectorEnd() {
84 return PassVector.end();
85 }
86
Devang Patel4a3fa4f2006-11-15 01:48:14 +000087 inline void setLastUser(Pass *P, Pass *LU) {
Devang Patel07f4f582006-11-14 21:49:36 +000088 LastUser[P] = LU;
89 // TODO : Check if pass P is available.
Devang Patel07f4f582006-11-14 21:49:36 +000090 }
Devang Patel3f0832a2006-11-14 02:54:23 +000091
Devang Patela9844592006-11-11 01:31:05 +000092private:
Devang Pateldafa4dd2006-11-14 00:03:04 +000093 // Analysis required by the passes managed by this manager. This information
94 // used while selecting pass manager during addPass. If a pass does not
95 // preserve any analysis required by other passes managed by current
96 // pass manager then new pass manager is used.
Devang Patela9844592006-11-11 01:31:05 +000097 std::vector<AnalysisID> RequiredAnalysis;
98
Devang Pateldafa4dd2006-11-14 00:03:04 +000099 // Set of available Analysis. This information is used while scheduling
100 // pass. If a pass requires an analysis which is not not available then
101 // equired analysis pass is scheduled to run before the pass itself is
102 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000103 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000104
Devang Patel3f0832a2006-11-14 02:54:23 +0000105 // Map to keep track of last user of the analysis pass.
106 // LastUser->second is the last user of Lastuser->first.
107 std::map<Pass *, Pass *> LastUser;
108
Devang Patel8cad70d2006-11-11 01:51:02 +0000109 // Collection of pass that are managed by this manager
110 std::vector<Pass *> PassVector;
Devang Patela9844592006-11-11 01:31:05 +0000111};
112
Devang Patelca58e352006-11-08 10:05:38 +0000113/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
114/// pass together and sequence them to process one basic block before
115/// processing next basic block.
Devang Patel42add712006-11-15 01:11:27 +0000116class BasicBlockPassManager_New : public CommonPassManagerImpl,
117 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000118
119public:
120 BasicBlockPassManager_New() { }
121
122 /// Add a pass into a passmanager queue.
123 bool addPass(Pass *p);
124
125 /// Execute all of the passes scheduled for execution. Keep track of
126 /// whether any of the passes modifies the function, and if so, return true.
127 bool runOnFunction(Function &F);
128
Devang Patelebba9702006-11-13 22:40:09 +0000129 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000130 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000131
Devang Patelca58e352006-11-08 10:05:38 +0000132private:
Devang Patelca58e352006-11-08 10:05:38 +0000133};
134
Devang Patel4e12f862006-11-08 10:44:40 +0000135/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000136/// It batches all function passes and basic block pass managers together and
137/// sequence them to process one function at a time before processing next
138/// function.
Devang Patel42add712006-11-15 01:11:27 +0000139class FunctionPassManagerImpl_New : public CommonPassManagerImpl,
140 public ModulePass {
Devang Patelca58e352006-11-08 10:05:38 +0000141public:
Devang Patel4e12f862006-11-08 10:44:40 +0000142 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
143 FunctionPassManagerImpl_New() {
Devang Patelca58e352006-11-08 10:05:38 +0000144 activeBBPassManager = NULL;
145 }
Devang Patel4e12f862006-11-08 10:44:40 +0000146 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000147
148 /// add - Add a pass to the queue of passes to run. This passes
149 /// ownership of the Pass to the PassManager. When the
150 /// PassManager_X is destroyed, the pass will be destroyed as well, so
151 /// there is no need to delete the pass. (TODO delete passes.)
152 /// This implies that all passes MUST be allocated with 'new'.
153 void add(Pass *P) { /* TODO*/ }
154
155 /// Add pass into the pass manager queue.
156 bool addPass(Pass *P);
157
158 /// Execute all of the passes scheduled for execution. Keep
159 /// track of whether any of the passes modifies the function, and if
160 /// so, return true.
161 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000162 bool runOnFunction(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000163
Devang Patelebba9702006-11-13 22:40:09 +0000164 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000165 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000166
Devang Patelff631ae2006-11-15 01:27:05 +0000167 /// doInitialization - Run all of the initializers for the function passes.
168 ///
169 bool doInitialization(Module &M);
170
171 /// doFinalization - Run all of the initializers for the function passes.
172 ///
173 bool doFinalization(Module &M);
Devang Patelca58e352006-11-08 10:05:38 +0000174private:
Devang Patelca58e352006-11-08 10:05:38 +0000175 // Active Pass Managers
176 BasicBlockPassManager_New *activeBBPassManager;
177};
178
179/// ModulePassManager_New manages ModulePasses and function pass managers.
180/// It batches all Module passes passes and function pass managers together and
181/// sequence them to process one module.
Devang Patel0ed47792006-11-10 21:33:13 +0000182class ModulePassManager_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000183
184public:
185 ModulePassManager_New() { activeFunctionPassManager = NULL; }
186
187 /// Add a pass into a passmanager queue.
188 bool addPass(Pass *p);
189
190 /// run - Execute all of the passes scheduled for execution. Keep track of
191 /// whether any of the passes modifies the module, and if so, return true.
192 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000193
194 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000195 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelca58e352006-11-08 10:05:38 +0000196
197private:
Devang Patelca58e352006-11-08 10:05:38 +0000198 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000199 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000200};
201
Devang Patel376fefa2006-11-08 10:29:57 +0000202/// PassManager_New manages ModulePassManagers
Devang Patel0ed47792006-11-10 21:33:13 +0000203class PassManagerImpl_New : public CommonPassManagerImpl {
Devang Patel376fefa2006-11-08 10:29:57 +0000204
205public:
206
207 /// add - Add a pass to the queue of passes to run. This passes ownership of
208 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
209 /// will be destroyed as well, so there is no need to delete the pass. This
210 /// implies that all passes MUST be allocated with 'new'.
211 void add(Pass *P);
212
213 /// run - Execute all of the passes scheduled for execution. Keep track of
214 /// whether any of the passes modifies the module, and if so, return true.
215 bool run(Module &M);
216
Devang Patelebba9702006-11-13 22:40:09 +0000217 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000218 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000219
Devang Patel376fefa2006-11-08 10:29:57 +0000220private:
221
222 /// Add a pass into a passmanager queue. This is used by schedulePasses
223 bool addPass(Pass *p);
224
Devang Patel1a6eaa42006-11-11 02:22:31 +0000225 /// Schedule pass P for execution. Make sure that passes required by
226 /// P are run before P is run. Update analysis info maintained by
227 /// the manager. Remove dead passes. This is a recursive function.
228 void schedulePass(Pass *P);
229
Devang Patel376fefa2006-11-08 10:29:57 +0000230 /// Schedule all passes collected in pass queue using add(). Add all the
231 /// schedule passes into various manager's queue using addPass().
232 void schedulePasses();
233
234 // Collection of pass managers
235 std::vector<ModulePassManager_New *> PassManagers;
236
Devang Patel376fefa2006-11-08 10:29:57 +0000237 // Active Pass Manager
238 ModulePassManager_New *activeManager;
239};
240
Devang Patelca58e352006-11-08 10:05:38 +0000241} // End of llvm namespace
242
Devang Patel0ed47792006-11-10 21:33:13 +0000243// CommonPassManagerImpl implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000244
Devang Pateld65e9e92006-11-08 01:31:28 +0000245/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000246/// manager.
Devang Patel0ed47792006-11-10 21:33:13 +0000247bool CommonPassManagerImpl::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000248
249 AnalysisUsage AnUsage;
250 P->getAnalysisUsage(AnUsage);
251
Devang Patel6c9f5482006-11-11 00:42:16 +0000252 // If this pass is not preserving information that is required by the other
253 // passes managed by this manager then use new manager
254 if (!AnUsage.getPreservesAll()) {
255 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
256 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
257 E = RequiredAnalysis.end(); I != E; ++I) {
258 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
259 PreservedSet.end())
260 // This analysis is not preserved. Need new manager.
261 return false;
262 }
263 }
Devang Patelf68a3492006-11-07 22:35:17 +0000264 return true;
265}
266
Devang Patel643676c2006-11-11 01:10:19 +0000267/// Augment RequiredAnalysis by adding analysis required by pass P.
Devang Patel0ed47792006-11-10 21:33:13 +0000268void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
Devang Patel6c9f5482006-11-11 00:42:16 +0000269 AnalysisUsage AnUsage;
270 P->getAnalysisUsage(AnUsage);
271 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000272
Devang Patel6c9f5482006-11-11 00:42:16 +0000273 // FIXME: What about duplicates ?
Devang Patel349170f2006-11-11 01:24:55 +0000274 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
275 RequiredSet.end());
Devang Patel07f4f582006-11-14 21:49:36 +0000276
277 initializeAnalysisImpl(P);
Devang Patelf68a3492006-11-07 22:35:17 +0000278}
279
Devang Patel643676c2006-11-11 01:10:19 +0000280/// Augement AvailableAnalysis by adding analysis made available by pass P.
281void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000282
Devang Patel643676c2006-11-11 01:10:19 +0000283 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000284 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000285
286 //TODO This pass is the current implementation of all of the interfaces it
287 //TODO implements as well.
288 //TODO
289 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
290 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
291 //TODO CurrentAnalyses[II[i]] = P;
292 }
293}
294
Devang Patelf68a3492006-11-07 22:35:17 +0000295/// Remove Analyss not preserved by Pass P
Devang Patel0ed47792006-11-10 21:33:13 +0000296void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000297 AnalysisUsage AnUsage;
298 P->getAnalysisUsage(AnUsage);
299 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000300
Devang Patelf60b5d92006-11-14 01:59:59 +0000301 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000302 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000303 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000304 PreservedSet.end()) {
305 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000306 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000307 AvailableAnalysis.erase(J);
308 }
309 }
Devang Patelf68a3492006-11-07 22:35:17 +0000310}
311
Devang Patelca189262006-11-14 03:05:08 +0000312/// Remove analysis passes that are not used any longer
313void CommonPassManagerImpl::removeDeadPasses(Pass *P) {
314
315 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
316 E = LastUser.end(); I !=E; ++I) {
317 if (I->second == P) {
318 Pass *deadPass = I->first;
319 deadPass->releaseMemory();
320
321 std::map<AnalysisID, Pass*>::iterator Pos =
322 AvailableAnalysis.find(deadPass->getPassInfo());
323
324 assert (Pos != AvailableAnalysis.end() &&
325 "Pass is not available");
326 AvailableAnalysis.erase(Pos);
327 }
328 }
329}
330
Devang Patel8cad70d2006-11-11 01:51:02 +0000331/// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +0000332/// AvailableAnalysis appropriately if ProcessAnalysis is true.
333void CommonPassManagerImpl::addPassToManager (Pass *P,
334 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000335
Devang Patel90b05e02006-11-11 02:04:19 +0000336 if (ProcessAnalysis) {
337 // Take a note of analysis required and made available by this pass
338 noteDownRequiredAnalysis(P);
339 noteDownAvailableAnalysis(P);
340
341 // Remove the analysis not preserved by this pass
342 removeNotPreservedAnalysis(P);
343 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000344
345 // Add pass
346 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000347}
348
Devang Patel07f4f582006-11-14 21:49:36 +0000349// All Required analyses should be available to the pass as it runs! Here
350// we fill in the AnalysisImpls member of the pass so that it can
351// successfully use the getAnalysis() method to retrieve the
352// implementations it needs.
353//
354void CommonPassManagerImpl::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000355 AnalysisUsage AnUsage;
356 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000357
358 for (std::vector<const PassInfo *>::const_iterator
359 I = AnUsage.getRequiredSet().begin(),
360 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
361 Pass *Impl = getAnalysisPass(*I);
362 if (Impl == 0)
363 assert(0 && "Analysis used but not available!");
364 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
365 }
366}
367
Devang Patel6e5a1132006-11-07 21:31:57 +0000368/// BasicBlockPassManager implementation
369
Devang Pateld65e9e92006-11-08 01:31:28 +0000370/// Add pass P into PassVector and return true. If this pass is not
371/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000372bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000373BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000374
375 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
376 if (!BP)
377 return false;
378
Devang Patel3c8eb622006-11-07 22:56:50 +0000379 // If this pass does not preserve anlysis that is used by other passes
380 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000381 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000382 return false;
383
Devang Patel8cad70d2006-11-11 01:51:02 +0000384 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000385
Devang Patel6e5a1132006-11-07 21:31:57 +0000386 return true;
387}
388
389/// Execute all of the passes scheduled for execution by invoking
390/// runOnBasicBlock method. Keep track of whether any of the passes modifies
391/// the function, and if so, return true.
392bool
393BasicBlockPassManager_New::runOnFunction(Function &F) {
394
395 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000396 clearAnalysis();
397
Devang Patel6e5a1132006-11-07 21:31:57 +0000398 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000399 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
400 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000401 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000402
403 noteDownAvailableAnalysis(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000404 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
405 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000406 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000407 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000408 }
409 return Changed;
410}
411
Devang Patelebba9702006-11-13 22:40:09 +0000412/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000413Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
414 return getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000415}
416
Devang Patel0c2012f2006-11-07 21:49:50 +0000417// FunctionPassManager_New implementation
Devang Patel4e12f862006-11-08 10:44:40 +0000418/// Create new Function pass manager
419FunctionPassManager_New::FunctionPassManager_New() {
420 FPM = new FunctionPassManagerImpl_New();
421}
422
423/// add - Add a pass to the queue of passes to run. This passes
424/// ownership of the Pass to the PassManager. When the
425/// PassManager_X is destroyed, the pass will be destroyed as well, so
426/// there is no need to delete the pass. (TODO delete passes.)
427/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000428void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000429 FPM->add(P);
430}
431
432/// Execute all of the passes scheduled for execution. Keep
433/// track of whether any of the passes modifies the function, and if
434/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000435bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000436 return FPM->runOnModule(M);
437}
438
Devang Patel9f3083e2006-11-15 19:39:54 +0000439/// run - Execute all of the passes scheduled for execution. Keep
440/// track of whether any of the passes modifies the function, and if
441/// so, return true.
442///
443bool FunctionPassManager_New::run(Function &F) {
444 std::string errstr;
445 if (MP->materializeFunction(&F, &errstr)) {
446 std::cerr << "Error reading bytecode file: " << errstr << "\n";
447 abort();
448 }
449 return FPM->runOnFunction(F);
450}
451
452
Devang Patelff631ae2006-11-15 01:27:05 +0000453/// doInitialization - Run all of the initializers for the function passes.
454///
455bool FunctionPassManager_New::doInitialization() {
456 return FPM->doInitialization(*MP->getModule());
457}
458
459/// doFinalization - Run all of the initializers for the function passes.
460///
461bool FunctionPassManager_New::doFinalization() {
462 return FPM->doFinalization(*MP->getModule());
463}
464
Devang Patel4e12f862006-11-08 10:44:40 +0000465// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000466
Devang Patel0c2012f2006-11-07 21:49:50 +0000467// FunctionPassManager
468
469/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
470/// either use it into active basic block pass manager or create new basic
471/// block pass manager to handle pass P.
472bool
Devang Patel4e12f862006-11-08 10:44:40 +0000473FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000474
475 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
476 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
477
478 if (!activeBBPassManager
479 || !activeBBPassManager->addPass(BP)) {
480
481 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000482 addPassToManager(activeBBPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000483 if (!activeBBPassManager->addPass(BP))
484 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000485 }
486 return true;
487 }
488
489 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
490 if (!FP)
491 return false;
492
Devang Patel3c8eb622006-11-07 22:56:50 +0000493 // If this pass does not preserve anlysis that is used by other passes
494 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000495 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000496 return false;
497
Devang Patel8cad70d2006-11-11 01:51:02 +0000498 addPassToManager (FP);
Devang Patel0c2012f2006-11-07 21:49:50 +0000499 activeBBPassManager = NULL;
500 return true;
501}
502
503/// Execute all of the passes scheduled for execution by invoking
504/// runOnFunction method. Keep track of whether any of the passes modifies
505/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000506bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000507
508 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000509 clearAnalysis();
510
Devang Patel0c2012f2006-11-07 21:49:50 +0000511 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000512 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
513 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000514 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000515
516 noteDownAvailableAnalysis(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000517 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
518 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000519 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000520 removeDeadPasses(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000521 }
522 return Changed;
523}
524
Devang Patel9f3083e2006-11-15 19:39:54 +0000525/// Execute all of the passes scheduled for execution by invoking
526/// runOnFunction method. Keep track of whether any of the passes modifies
527/// the function, and if so, return true.
528bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
529
530 bool Changed = false;
531 clearAnalysis();
532
533 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
534 e = passVectorEnd(); itr != e; ++itr) {
535 Pass *P = *itr;
536
537 noteDownAvailableAnalysis(P);
538 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
539 Changed |= FP->runOnFunction(F);
540 removeNotPreservedAnalysis(P);
541 removeDeadPasses(P);
542 }
543 return Changed;
544}
545
546
Devang Patelebba9702006-11-13 22:40:09 +0000547/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000548Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000549
Devang Patel3f0832a2006-11-14 02:54:23 +0000550 Pass *P = getAnalysisPass(AID);
551 if (P)
552 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000553
554 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000555 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000556 return activeBBPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000557
558 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000559 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000560}
Devang Patel0c2012f2006-11-07 21:49:50 +0000561
Devang Patelff631ae2006-11-15 01:27:05 +0000562inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
563 bool Changed = false;
564
565 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
566 e = passVectorEnd(); itr != e; ++itr) {
567 Pass *P = *itr;
568
569 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
570 Changed |= FP->doInitialization(M);
571 }
572
573 return Changed;
574}
575
576inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
577 bool Changed = false;
578
579 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
580 e = passVectorEnd(); itr != e; ++itr) {
581 Pass *P = *itr;
582
583 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
584 Changed |= FP->doFinalization(M);
585 }
586
587
588 return Changed;
589}
590
591
Devang Patel05e1a972006-11-07 22:03:15 +0000592// ModulePassManager implementation
593
594/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000595/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000596/// is not manageable by this manager.
597bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000598ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000599
600 // If P is FunctionPass then use function pass maanager.
601 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
602
603 activeFunctionPassManager = NULL;
604
605 if (!activeFunctionPassManager
606 || !activeFunctionPassManager->addPass(P)) {
607
Devang Patel4e12f862006-11-08 10:44:40 +0000608 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000609 addPassToManager(activeFunctionPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000610 if (!activeFunctionPassManager->addPass(FP))
611 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000612 }
613 return true;
614 }
615
616 ModulePass *MP = dynamic_cast<ModulePass *>(P);
617 if (!MP)
618 return false;
619
Devang Patel3c8eb622006-11-07 22:56:50 +0000620 // If this pass does not preserve anlysis that is used by other passes
621 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000622 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000623 return false;
624
Devang Patel8cad70d2006-11-11 01:51:02 +0000625 addPassToManager(MP);
Devang Patel05e1a972006-11-07 22:03:15 +0000626 activeFunctionPassManager = NULL;
627 return true;
628}
629
630
631/// Execute all of the passes scheduled for execution by invoking
632/// runOnModule method. Keep track of whether any of the passes modifies
633/// the module, and if so, return true.
634bool
635ModulePassManager_New::runOnModule(Module &M) {
636 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000637 clearAnalysis();
638
Devang Patel8cad70d2006-11-11 01:51:02 +0000639 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
640 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000641 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000642
643 noteDownAvailableAnalysis(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000644 ModulePass *MP = dynamic_cast<ModulePass*>(P);
645 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000646 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000647 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000648 }
649 return Changed;
650}
651
Devang Patelebba9702006-11-13 22:40:09 +0000652/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000653Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000654
Devang Patel3f0832a2006-11-14 02:54:23 +0000655
656 Pass *P = getAnalysisPass(AID);
657 if (P)
658 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000659
660 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000661 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000662 return activeFunctionPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000663
664 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000665 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000666}
667
668/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000669Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000670
Devang Patel3f0832a2006-11-14 02:54:23 +0000671 Pass *P = NULL;
Devang Patel70868442006-11-13 22:53:19 +0000672 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
Devang Patel3f0832a2006-11-14 02:54:23 +0000673 e = PassManagers.end(); !P && itr != e; ++itr)
674 P = (*itr)->getAnalysisPassFromManager(AID);
675 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000676}
677
Devang Patel1a6eaa42006-11-11 02:22:31 +0000678/// Schedule pass P for execution. Make sure that passes required by
679/// P are run before P is run. Update analysis info maintained by
680/// the manager. Remove dead passes. This is a recursive function.
681void PassManagerImpl_New::schedulePass(Pass *P) {
682
683 AnalysisUsage AnUsage;
684 P->getAnalysisUsage(AnUsage);
685 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
686 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
687 E = RequiredSet.end(); I != E; ++I) {
688
Devang Patel3f0832a2006-11-14 02:54:23 +0000689 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
690 if (!AnalysisPass) {
Devang Patel1a6eaa42006-11-11 02:22:31 +0000691 // Schedule this analysis run first.
Devang Patel3f0832a2006-11-14 02:54:23 +0000692 AnalysisPass = (*I)->createPass();
693 schedulePass(AnalysisPass);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000694 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000695 setLastUser (AnalysisPass, P);
Devang Patel4a3fa4f2006-11-15 01:48:14 +0000696
697 // Prolong live range of analyses that are needed after an analysis pass
698 // is destroyed, for querying by subsequent passes
699 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
700 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
701 E = IDs.end(); I != E; ++I) {
702 Pass *AP = getAnalysisPassFromManager(*I);
703 assert (AP && "Analysis pass is not available");
704 setLastUser(AP, P);
705 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000706 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000707 addPass(P);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000708}
709
Devang Patelc290c8a2006-11-07 22:23:34 +0000710/// Schedule all passes from the queue by adding them in their
711/// respective manager's queue.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000712void PassManagerImpl_New::schedulePasses() {
713 for (std::vector<Pass *>::iterator I = passVectorBegin(),
714 E = passVectorEnd(); I != E; ++I)
715 schedulePass (*I);
Devang Patelc290c8a2006-11-07 22:23:34 +0000716}
717
718/// Add pass P to the queue of passes to run.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000719void PassManagerImpl_New::add(Pass *P) {
720 // Do not process Analysis now. Analysis is process while scheduling
721 // the pass vector.
Devang Pateldb789fb2006-11-11 02:06:21 +0000722 addPassToManager(P, false);
Devang Patelc290c8a2006-11-07 22:23:34 +0000723}
724
725// PassManager_New implementation
726/// Add P into active pass manager or use new module pass manager to
727/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000728bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000729
Devang Patel6c9f5482006-11-11 00:42:16 +0000730 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000731 activeManager = new ModulePassManager_New();
732 PassManagers.push_back(activeManager);
733 }
734
735 return activeManager->addPass(P);
736}
737
738/// run - Execute all of the passes scheduled for execution. Keep track of
739/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000740bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000741
742 schedulePasses();
743 bool Changed = false;
744 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
745 e = PassManagers.end(); itr != e; ++itr) {
746 ModulePassManager_New *pm = *itr;
747 Changed |= pm->runOnModule(M);
748 }
749 return Changed;
750}
Devang Patel376fefa2006-11-08 10:29:57 +0000751
752/// Create new pass manager
753PassManager_New::PassManager_New() {
754 PM = new PassManagerImpl_New();
755}
756
757/// add - Add a pass to the queue of passes to run. This passes ownership of
758/// the Pass to the PassManager. When the PassManager is destroyed, the pass
759/// will be destroyed as well, so there is no need to delete the pass. This
760/// implies that all passes MUST be allocated with 'new'.
761void
762PassManager_New::add(Pass *P) {
763 PM->add(P);
764}
765
766/// run - Execute all of the passes scheduled for execution. Keep track of
767/// whether any of the passes modifies the module, and if so, return true.
768bool
769PassManager_New::run(Module &M) {
770 return PM->run(M);
771}
772