blob: d01ef3e4a7baefbadeef970a4f7def1c2daaa776 [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 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);
Devang Patel9f3083e2006-11-15 19:39:54 +0000161 bool runOnFunction(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000162
Devang Patelebba9702006-11-13 22:40:09 +0000163 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000164 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000165
Devang Patelff631ae2006-11-15 01:27:05 +0000166 /// doInitialization - Run all of the initializers for the function passes.
167 ///
168 bool doInitialization(Module &M);
169
170 /// doFinalization - Run all of the initializers for the function passes.
171 ///
172 bool doFinalization(Module &M);
Devang Patelca58e352006-11-08 10:05:38 +0000173private:
Devang Patelca58e352006-11-08 10:05:38 +0000174 // Active Pass Managers
175 BasicBlockPassManager_New *activeBBPassManager;
176};
177
178/// ModulePassManager_New manages ModulePasses and function pass managers.
179/// It batches all Module passes passes and function pass managers together and
180/// sequence them to process one module.
Devang Patel0ed47792006-11-10 21:33:13 +0000181class ModulePassManager_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000182
183public:
184 ModulePassManager_New() { activeFunctionPassManager = NULL; }
185
186 /// Add a pass into a passmanager queue.
187 bool addPass(Pass *p);
188
189 /// run - Execute all of the passes scheduled for execution. Keep track of
190 /// whether any of the passes modifies the module, and if so, return true.
191 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000192
193 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000194 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelca58e352006-11-08 10:05:38 +0000195
196private:
Devang Patelca58e352006-11-08 10:05:38 +0000197 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000198 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000199};
200
Devang Patel376fefa2006-11-08 10:29:57 +0000201/// PassManager_New manages ModulePassManagers
Devang Patel0ed47792006-11-10 21:33:13 +0000202class PassManagerImpl_New : public CommonPassManagerImpl {
Devang Patel376fefa2006-11-08 10:29:57 +0000203
204public:
205
206 /// add - Add a pass to the queue of passes to run. This passes ownership of
207 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
208 /// will be destroyed as well, so there is no need to delete the pass. This
209 /// implies that all passes MUST be allocated with 'new'.
210 void add(Pass *P);
211
212 /// run - Execute all of the passes scheduled for execution. Keep track of
213 /// whether any of the passes modifies the module, and if so, return true.
214 bool run(Module &M);
215
Devang Patelebba9702006-11-13 22:40:09 +0000216 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000217 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000218
Devang Patel376fefa2006-11-08 10:29:57 +0000219private:
220
221 /// Add a pass into a passmanager queue. This is used by schedulePasses
222 bool addPass(Pass *p);
223
Devang Patel1a6eaa42006-11-11 02:22:31 +0000224 /// Schedule pass P for execution. Make sure that passes required by
225 /// P are run before P is run. Update analysis info maintained by
226 /// the manager. Remove dead passes. This is a recursive function.
227 void schedulePass(Pass *P);
228
Devang Patel376fefa2006-11-08 10:29:57 +0000229 /// Schedule all passes collected in pass queue using add(). Add all the
230 /// schedule passes into various manager's queue using addPass().
231 void schedulePasses();
232
233 // Collection of pass managers
234 std::vector<ModulePassManager_New *> PassManagers;
235
Devang Patel376fefa2006-11-08 10:29:57 +0000236 // Active Pass Manager
237 ModulePassManager_New *activeManager;
238};
239
Devang Patelca58e352006-11-08 10:05:38 +0000240} // End of llvm namespace
241
Devang Patel0ed47792006-11-10 21:33:13 +0000242// CommonPassManagerImpl implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000243
Devang Pateld65e9e92006-11-08 01:31:28 +0000244/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000245/// manager.
Devang Patel0ed47792006-11-10 21:33:13 +0000246bool CommonPassManagerImpl::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000247
248 AnalysisUsage AnUsage;
249 P->getAnalysisUsage(AnUsage);
250
Devang Patel6c9f5482006-11-11 00:42:16 +0000251 // If this pass is not preserving information that is required by the other
252 // passes managed by this manager then use new manager
253 if (!AnUsage.getPreservesAll()) {
254 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
255 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
256 E = RequiredAnalysis.end(); I != E; ++I) {
257 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
258 PreservedSet.end())
259 // This analysis is not preserved. Need new manager.
260 return false;
261 }
262 }
Devang Patelf68a3492006-11-07 22:35:17 +0000263 return true;
264}
265
Devang Patel643676c2006-11-11 01:10:19 +0000266/// Augment RequiredAnalysis by adding analysis required by pass P.
Devang Patel0ed47792006-11-10 21:33:13 +0000267void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
Devang Patel6c9f5482006-11-11 00:42:16 +0000268 AnalysisUsage AnUsage;
269 P->getAnalysisUsage(AnUsage);
270 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000271
Devang Patel6c9f5482006-11-11 00:42:16 +0000272 // FIXME: What about duplicates ?
Devang Patel349170f2006-11-11 01:24:55 +0000273 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
274 RequiredSet.end());
Devang Patel07f4f582006-11-14 21:49:36 +0000275
276 initializeAnalysisImpl(P);
Devang Patelf68a3492006-11-07 22:35:17 +0000277}
278
Devang Patel643676c2006-11-11 01:10:19 +0000279/// Augement AvailableAnalysis by adding analysis made available by pass P.
280void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000281
Devang Patel643676c2006-11-11 01:10:19 +0000282 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000283 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000284
285 //TODO This pass is the current implementation of all of the interfaces it
286 //TODO implements as well.
287 //TODO
288 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
289 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
290 //TODO CurrentAnalyses[II[i]] = P;
291 }
292}
293
Devang Patelf68a3492006-11-07 22:35:17 +0000294/// Remove Analyss not preserved by Pass P
Devang Patel0ed47792006-11-10 21:33:13 +0000295void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000296 AnalysisUsage AnUsage;
297 P->getAnalysisUsage(AnUsage);
298 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000299
Devang Patelf60b5d92006-11-14 01:59:59 +0000300 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000301 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000302 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000303 PreservedSet.end()) {
304 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000305 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000306 AvailableAnalysis.erase(J);
307 }
308 }
Devang Patelf68a3492006-11-07 22:35:17 +0000309}
310
Devang Patelca189262006-11-14 03:05:08 +0000311/// Remove analysis passes that are not used any longer
312void CommonPassManagerImpl::removeDeadPasses(Pass *P) {
313
314 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
315 E = LastUser.end(); I !=E; ++I) {
316 if (I->second == P) {
317 Pass *deadPass = I->first;
318 deadPass->releaseMemory();
319
320 std::map<AnalysisID, Pass*>::iterator Pos =
321 AvailableAnalysis.find(deadPass->getPassInfo());
322
323 assert (Pos != AvailableAnalysis.end() &&
324 "Pass is not available");
325 AvailableAnalysis.erase(Pos);
326 }
327 }
328}
329
Devang Patel8cad70d2006-11-11 01:51:02 +0000330/// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +0000331/// AvailableAnalysis appropriately if ProcessAnalysis is true.
332void CommonPassManagerImpl::addPassToManager (Pass *P,
333 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000334
Devang Patel90b05e02006-11-11 02:04:19 +0000335 if (ProcessAnalysis) {
336 // Take a note of analysis required and made available by this pass
337 noteDownRequiredAnalysis(P);
338 noteDownAvailableAnalysis(P);
339
340 // Remove the analysis not preserved by this pass
341 removeNotPreservedAnalysis(P);
342 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000343
344 // Add pass
345 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000346}
347
Devang Patel07f4f582006-11-14 21:49:36 +0000348// All Required analyses should be available to the pass as it runs! Here
349// we fill in the AnalysisImpls member of the pass so that it can
350// successfully use the getAnalysis() method to retrieve the
351// implementations it needs.
352//
353void CommonPassManagerImpl::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000354 AnalysisUsage AnUsage;
355 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000356
357 for (std::vector<const PassInfo *>::const_iterator
358 I = AnUsage.getRequiredSet().begin(),
359 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
360 Pass *Impl = getAnalysisPass(*I);
361 if (Impl == 0)
362 assert(0 && "Analysis used but not available!");
363 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
364 }
365}
366
Devang Patel6e5a1132006-11-07 21:31:57 +0000367/// BasicBlockPassManager implementation
368
Devang Pateld65e9e92006-11-08 01:31:28 +0000369/// Add pass P into PassVector and return true. If this pass is not
370/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000371bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000372BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000373
374 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
375 if (!BP)
376 return false;
377
Devang Patel3c8eb622006-11-07 22:56:50 +0000378 // If this pass does not preserve anlysis that is used by other passes
379 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000380 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000381 return false;
382
Devang Patel8cad70d2006-11-11 01:51:02 +0000383 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000384
Devang Patel6e5a1132006-11-07 21:31:57 +0000385 return true;
386}
387
388/// Execute all of the passes scheduled for execution by invoking
389/// runOnBasicBlock method. Keep track of whether any of the passes modifies
390/// the function, and if so, return true.
391bool
392BasicBlockPassManager_New::runOnFunction(Function &F) {
393
394 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000395 clearAnalysis();
396
Devang Patel6e5a1132006-11-07 21:31:57 +0000397 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000398 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
399 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000400 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000401
402 noteDownAvailableAnalysis(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000403 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
404 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000405 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000406 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000407 }
408 return Changed;
409}
410
Devang Patelebba9702006-11-13 22:40:09 +0000411/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000412Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
413 return getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000414}
415
Devang Patel0c2012f2006-11-07 21:49:50 +0000416// FunctionPassManager_New implementation
Devang Patel4e12f862006-11-08 10:44:40 +0000417/// Create new Function pass manager
418FunctionPassManager_New::FunctionPassManager_New() {
419 FPM = new FunctionPassManagerImpl_New();
420}
421
422/// add - Add a pass to the queue of passes to run. This passes
423/// ownership of the Pass to the PassManager. When the
424/// PassManager_X is destroyed, the pass will be destroyed as well, so
425/// there is no need to delete the pass. (TODO delete passes.)
426/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000427void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000428 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.
Devang Patel9f3083e2006-11-15 19:39:54 +0000434bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000435 return FPM->runOnModule(M);
436}
437
Devang Patel9f3083e2006-11-15 19:39:54 +0000438/// run - Execute all of the passes scheduled for execution. Keep
439/// track of whether any of the passes modifies the function, and if
440/// so, return true.
441///
442bool FunctionPassManager_New::run(Function &F) {
443 std::string errstr;
444 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingdfc91892006-11-28 02:09:03 +0000445 llvm_cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000446 abort();
447 }
448 return FPM->runOnFunction(F);
449}
450
451
Devang Patelff631ae2006-11-15 01:27:05 +0000452/// doInitialization - Run all of the initializers for the function passes.
453///
454bool FunctionPassManager_New::doInitialization() {
455 return FPM->doInitialization(*MP->getModule());
456}
457
458/// doFinalization - Run all of the initializers for the function passes.
459///
460bool FunctionPassManager_New::doFinalization() {
461 return FPM->doFinalization(*MP->getModule());
462}
463
Devang Patel4e12f862006-11-08 10:44:40 +0000464// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000465
Devang Patel0c2012f2006-11-07 21:49:50 +0000466// FunctionPassManager
467
468/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
469/// either use it into active basic block pass manager or create new basic
470/// block pass manager to handle pass P.
471bool
Devang Patel4e12f862006-11-08 10:44:40 +0000472FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000473
474 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
475 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
476
477 if (!activeBBPassManager
478 || !activeBBPassManager->addPass(BP)) {
479
480 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000481 addPassToManager(activeBBPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000482 if (!activeBBPassManager->addPass(BP))
483 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000484 }
485 return true;
486 }
487
488 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
489 if (!FP)
490 return false;
491
Devang Patel3c8eb622006-11-07 22:56:50 +0000492 // If this pass does not preserve anlysis that is used by other passes
493 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000494 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000495 return false;
496
Devang Patel8cad70d2006-11-11 01:51:02 +0000497 addPassToManager (FP);
Devang Patel0c2012f2006-11-07 21:49:50 +0000498 activeBBPassManager = NULL;
499 return true;
500}
501
502/// Execute all of the passes scheduled for execution by invoking
503/// runOnFunction method. Keep track of whether any of the passes modifies
504/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000505bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000506
507 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000508 clearAnalysis();
509
Devang Patel0c2012f2006-11-07 21:49:50 +0000510 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000511 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
512 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000513 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000514
515 noteDownAvailableAnalysis(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000516 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
517 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000518 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000519 removeDeadPasses(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000520 }
521 return Changed;
522}
523
Devang Patel9f3083e2006-11-15 19:39:54 +0000524/// Execute all of the passes scheduled for execution by invoking
525/// runOnFunction method. Keep track of whether any of the passes modifies
526/// the function, and if so, return true.
527bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
528
529 bool Changed = false;
530 clearAnalysis();
531
532 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
533 e = passVectorEnd(); itr != e; ++itr) {
534 Pass *P = *itr;
535
536 noteDownAvailableAnalysis(P);
537 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
538 Changed |= FP->runOnFunction(F);
539 removeNotPreservedAnalysis(P);
540 removeDeadPasses(P);
541 }
542 return Changed;
543}
544
545
Devang Patelebba9702006-11-13 22:40:09 +0000546/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000547Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000548
Devang Patel3f0832a2006-11-14 02:54:23 +0000549 Pass *P = getAnalysisPass(AID);
550 if (P)
551 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000552
553 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000554 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000555 return activeBBPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000556
557 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000558 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000559}
Devang Patel0c2012f2006-11-07 21:49:50 +0000560
Devang Patelff631ae2006-11-15 01:27:05 +0000561inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
562 bool Changed = false;
563
564 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
565 e = passVectorEnd(); itr != e; ++itr) {
566 Pass *P = *itr;
567
568 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
569 Changed |= FP->doInitialization(M);
570 }
571
572 return Changed;
573}
574
575inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
576 bool Changed = false;
577
578 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
579 e = passVectorEnd(); itr != e; ++itr) {
580 Pass *P = *itr;
581
582 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
583 Changed |= FP->doFinalization(M);
584 }
585
586
587 return Changed;
588}
589
590
Devang Patel05e1a972006-11-07 22:03:15 +0000591// ModulePassManager implementation
592
593/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000594/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000595/// is not manageable by this manager.
596bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000597ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000598
599 // If P is FunctionPass then use function pass maanager.
600 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
601
602 activeFunctionPassManager = NULL;
603
604 if (!activeFunctionPassManager
605 || !activeFunctionPassManager->addPass(P)) {
606
Devang Patel4e12f862006-11-08 10:44:40 +0000607 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000608 addPassToManager(activeFunctionPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000609 if (!activeFunctionPassManager->addPass(FP))
610 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000611 }
612 return true;
613 }
614
615 ModulePass *MP = dynamic_cast<ModulePass *>(P);
616 if (!MP)
617 return false;
618
Devang Patel3c8eb622006-11-07 22:56:50 +0000619 // If this pass does not preserve anlysis that is used by other passes
620 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000621 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000622 return false;
623
Devang Patel8cad70d2006-11-11 01:51:02 +0000624 addPassToManager(MP);
Devang Patel05e1a972006-11-07 22:03:15 +0000625 activeFunctionPassManager = NULL;
626 return true;
627}
628
629
630/// Execute all of the passes scheduled for execution by invoking
631/// runOnModule method. Keep track of whether any of the passes modifies
632/// the module, and if so, return true.
633bool
634ModulePassManager_New::runOnModule(Module &M) {
635 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000636 clearAnalysis();
637
Devang Patel8cad70d2006-11-11 01:51:02 +0000638 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
639 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000640 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000641
642 noteDownAvailableAnalysis(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000643 ModulePass *MP = dynamic_cast<ModulePass*>(P);
644 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000645 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000646 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000647 }
648 return Changed;
649}
650
Devang Patelebba9702006-11-13 22:40:09 +0000651/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000652Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000653
Devang Patel3f0832a2006-11-14 02:54:23 +0000654
655 Pass *P = getAnalysisPass(AID);
656 if (P)
657 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000658
659 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000660 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000661 return activeFunctionPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000662
663 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000664 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000665}
666
667/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000668Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000669
Devang Patel3f0832a2006-11-14 02:54:23 +0000670 Pass *P = NULL;
Devang Patel70868442006-11-13 22:53:19 +0000671 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
Devang Patel3f0832a2006-11-14 02:54:23 +0000672 e = PassManagers.end(); !P && itr != e; ++itr)
673 P = (*itr)->getAnalysisPassFromManager(AID);
674 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000675}
676
Devang Patel1a6eaa42006-11-11 02:22:31 +0000677/// Schedule pass P for execution. Make sure that passes required by
678/// P are run before P is run. Update analysis info maintained by
679/// the manager. Remove dead passes. This is a recursive function.
680void PassManagerImpl_New::schedulePass(Pass *P) {
681
682 AnalysisUsage AnUsage;
683 P->getAnalysisUsage(AnUsage);
684 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
685 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
686 E = RequiredSet.end(); I != E; ++I) {
687
Devang Patel3f0832a2006-11-14 02:54:23 +0000688 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
689 if (!AnalysisPass) {
Devang Patel1a6eaa42006-11-11 02:22:31 +0000690 // Schedule this analysis run first.
Devang Patel3f0832a2006-11-14 02:54:23 +0000691 AnalysisPass = (*I)->createPass();
692 schedulePass(AnalysisPass);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000693 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000694 setLastUser (AnalysisPass, P);
Devang Patel4a3fa4f2006-11-15 01:48:14 +0000695
696 // Prolong live range of analyses that are needed after an analysis pass
697 // is destroyed, for querying by subsequent passes
698 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
699 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
700 E = IDs.end(); I != E; ++I) {
701 Pass *AP = getAnalysisPassFromManager(*I);
702 assert (AP && "Analysis pass is not available");
703 setLastUser(AP, P);
704 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000705 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000706 addPass(P);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000707}
708
Devang Patelc290c8a2006-11-07 22:23:34 +0000709/// Schedule all passes from the queue by adding them in their
710/// respective manager's queue.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000711void PassManagerImpl_New::schedulePasses() {
712 for (std::vector<Pass *>::iterator I = passVectorBegin(),
713 E = passVectorEnd(); I != E; ++I)
714 schedulePass (*I);
Devang Patelc290c8a2006-11-07 22:23:34 +0000715}
716
717/// Add pass P to the queue of passes to run.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000718void PassManagerImpl_New::add(Pass *P) {
719 // Do not process Analysis now. Analysis is process while scheduling
720 // the pass vector.
Devang Pateldb789fb2006-11-11 02:06:21 +0000721 addPassToManager(P, false);
Devang Patelc290c8a2006-11-07 22:23:34 +0000722}
723
724// PassManager_New implementation
725/// Add P into active pass manager or use new module pass manager to
726/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000727bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000728
Devang Patel6c9f5482006-11-11 00:42:16 +0000729 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000730 activeManager = new ModulePassManager_New();
731 PassManagers.push_back(activeManager);
732 }
733
734 return activeManager->addPass(P);
735}
736
737/// run - Execute all of the passes scheduled for execution. Keep track of
738/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000739bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000740
741 schedulePasses();
742 bool Changed = false;
743 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
744 e = PassManagers.end(); itr != e; ++itr) {
745 ModulePassManager_New *pm = *itr;
746 Changed |= pm->runOnModule(M);
747 }
748 return Changed;
749}
Devang Patel376fefa2006-11-08 10:29:57 +0000750
751/// Create new pass manager
752PassManager_New::PassManager_New() {
753 PM = new PassManagerImpl_New();
754}
755
756/// add - Add a pass to the queue of passes to run. This passes ownership of
757/// the Pass to the PassManager. When the PassManager is destroyed, the pass
758/// will be destroyed as well, so there is no need to delete the pass. This
759/// implies that all passes MUST be allocated with 'new'.
760void
761PassManager_New::add(Pass *P) {
762 PM->add(P);
763}
764
765/// run - Execute all of the passes scheduled for execution. Keep track of
766/// whether any of the passes modifies the module, and if so, return true.
767bool
768PassManager_New::run(Module &M) {
769 return PM->run(M);
770}
771