1 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Devang Patel and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVM Pass Manager infrastructure.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/PassManager.h"
16 #include "llvm/Module.h"
22 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
23 /// pass together and sequence them to process one basic block before
24 /// processing next basic block.
25 class BasicBlockPassManager_New : public CommonPassManagerImpl {
28 BasicBlockPassManager_New() { }
30 /// Add a pass into a passmanager queue.
31 bool addPass(Pass *p);
33 /// Execute all of the passes scheduled for execution. Keep track of
34 /// whether any of the passes modifies the function, and if so, return true.
35 bool runOnFunction(Function &F);
38 // Collection of pass that are managed by this manager
39 std::vector<Pass *> PassVector;
42 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
43 /// It batches all function passes and basic block pass managers together and
44 /// sequence them to process one function at a time before processing next
46 class FunctionPassManagerImpl_New : public CommonPassManagerImpl {
48 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
49 FunctionPassManagerImpl_New() {
50 activeBBPassManager = NULL;
52 ~FunctionPassManagerImpl_New() { /* TODO */ };
54 /// add - Add a pass to the queue of passes to run. This passes
55 /// ownership of the Pass to the PassManager. When the
56 /// PassManager_X is destroyed, the pass will be destroyed as well, so
57 /// there is no need to delete the pass. (TODO delete passes.)
58 /// This implies that all passes MUST be allocated with 'new'.
59 void add(Pass *P) { /* TODO*/ }
61 /// Add pass into the pass manager queue.
62 bool addPass(Pass *P);
64 /// Execute all of the passes scheduled for execution. Keep
65 /// track of whether any of the passes modifies the function, and if
67 bool runOnModule(Module &M);
70 // Collection of pass that are manged by this manager
71 std::vector<Pass *> PassVector;
73 // Active Pass Managers
74 BasicBlockPassManager_New *activeBBPassManager;
77 /// ModulePassManager_New manages ModulePasses and function pass managers.
78 /// It batches all Module passes passes and function pass managers together and
79 /// sequence them to process one module.
80 class ModulePassManager_New : public CommonPassManagerImpl {
83 ModulePassManager_New() { activeFunctionPassManager = NULL; }
85 /// Add a pass into a passmanager queue.
86 bool addPass(Pass *p);
88 /// run - Execute all of the passes scheduled for execution. Keep track of
89 /// whether any of the passes modifies the module, and if so, return true.
90 bool runOnModule(Module &M);
93 // Collection of pass that are managed by this manager
94 std::vector<Pass *> PassVector;
96 // Active Pass Manager
97 FunctionPassManagerImpl_New *activeFunctionPassManager;
100 /// PassManager_New manages ModulePassManagers
101 class PassManagerImpl_New : public CommonPassManagerImpl {
105 /// add - Add a pass to the queue of passes to run. This passes ownership of
106 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
107 /// will be destroyed as well, so there is no need to delete the pass. This
108 /// implies that all passes MUST be allocated with 'new'.
111 /// run - Execute all of the passes scheduled for execution. Keep track of
112 /// whether any of the passes modifies the module, and if so, return true.
117 /// Add a pass into a passmanager queue. This is used by schedulePasses
118 bool addPass(Pass *p);
120 /// Schedule all passes collected in pass queue using add(). Add all the
121 /// schedule passes into various manager's queue using addPass().
122 void schedulePasses();
124 // Collection of pass managers
125 std::vector<ModulePassManager_New *> PassManagers;
127 // Collection of pass that are not yet scheduled
128 std::vector<Pass *> PassVector;
130 // Active Pass Manager
131 ModulePassManager_New *activeManager;
134 } // End of llvm namespace
136 // CommonPassManagerImpl implementation
138 /// Return true IFF pass P's required analysis set does not required new
140 bool CommonPassManagerImpl::manageablePass(Pass *P) {
142 AnalysisUsage AnUsage;
143 P->getAnalysisUsage(AnUsage);
145 // If this pass is not preserving information that is required by the other passes
146 // managed by this manager then use new manager
151 /// Return true IFF AnalysisID AID is currently available.
152 bool CommonPassManagerImpl::analysisCurrentlyAvailable(AnalysisID AID) {
158 /// Augment RequiredSet by adding analysis required by pass P.
159 void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
164 /// Remove AnalysisID from the RequiredSet
165 void CommonPassManagerImpl::removeAnalysis(AnalysisID AID) {
170 /// Remove Analyss not preserved by Pass P
171 void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
176 /// BasicBlockPassManager implementation
178 /// Add pass P into PassVector and return true. If this pass is not
179 /// manageable by this manager then return false.
181 BasicBlockPassManager_New::addPass(Pass *P) {
183 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
187 // If this pass does not preserve anlysis that is used by other passes
188 // managed by this manager than it is not a suiable pass for this manager.
189 if (!manageablePass(P))
192 // Take a note of analysis required by this pass.
193 noteDownRequiredAnalysis(P);
196 PassVector.push_back(BP);
200 /// Execute all of the passes scheduled for execution by invoking
201 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
202 /// the function, and if so, return true.
204 BasicBlockPassManager_New::runOnFunction(Function &F) {
206 bool Changed = false;
207 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
208 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
209 e = PassVector.end(); itr != e; ++itr) {
211 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
212 Changed |= BP->runOnBasicBlock(*I);
217 // FunctionPassManager_New implementation
218 /// Create new Function pass manager
219 FunctionPassManager_New::FunctionPassManager_New() {
220 FPM = new FunctionPassManagerImpl_New();
223 /// add - Add a pass to the queue of passes to run. This passes
224 /// ownership of the Pass to the PassManager. When the
225 /// PassManager_X is destroyed, the pass will be destroyed as well, so
226 /// there is no need to delete the pass. (TODO delete passes.)
227 /// This implies that all passes MUST be allocated with 'new'.
229 FunctionPassManager_New::add(Pass *P) {
233 /// Execute all of the passes scheduled for execution. Keep
234 /// track of whether any of the passes modifies the function, and if
237 FunctionPassManager_New::runOnModule(Module &M) {
238 return FPM->runOnModule(M);
241 // FunctionPassManagerImpl_New implementation
243 // FunctionPassManager
245 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
246 /// either use it into active basic block pass manager or create new basic
247 /// block pass manager to handle pass P.
249 FunctionPassManagerImpl_New::addPass(Pass *P) {
251 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
252 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
254 if (!activeBBPassManager
255 || !activeBBPassManager->addPass(BP)) {
257 activeBBPassManager = new BasicBlockPassManager_New();
259 PassVector.push_back(activeBBPassManager);
260 if (!activeBBPassManager->addPass(BP))
261 assert(0 && "Unable to add Pass");
266 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
270 // If this pass does not preserve anlysis that is used by other passes
271 // managed by this manager than it is not a suiable pass for this manager.
272 if (!manageablePass(P))
275 // Take a note of analysis required by this pass.
276 noteDownRequiredAnalysis(P);
278 PassVector.push_back(FP);
279 activeBBPassManager = NULL;
283 /// Execute all of the passes scheduled for execution by invoking
284 /// runOnFunction method. Keep track of whether any of the passes modifies
285 /// the function, and if so, return true.
287 FunctionPassManagerImpl_New::runOnModule(Module &M) {
289 bool Changed = false;
290 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
291 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
292 e = PassVector.end(); itr != e; ++itr) {
294 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
295 Changed |= FP->runOnFunction(*I);
301 // ModulePassManager implementation
303 /// Add P into pass vector if it is manageble. If P is a FunctionPass
304 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
305 /// is not manageable by this manager.
307 ModulePassManager_New::addPass(Pass *P) {
309 // If P is FunctionPass then use function pass maanager.
310 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
312 activeFunctionPassManager = NULL;
314 if (!activeFunctionPassManager
315 || !activeFunctionPassManager->addPass(P)) {
317 activeFunctionPassManager = new FunctionPassManagerImpl_New();
319 PassVector.push_back(activeFunctionPassManager);
320 if (!activeFunctionPassManager->addPass(FP))
321 assert(0 && "Unable to add pass");
326 ModulePass *MP = dynamic_cast<ModulePass *>(P);
330 // If this pass does not preserve anlysis that is used by other passes
331 // managed by this manager than it is not a suiable pass for this manager.
332 if (!manageablePass(P))
335 // Take a note of analysis required by this pass.
336 noteDownRequiredAnalysis(P);
338 PassVector.push_back(MP);
339 activeFunctionPassManager = NULL;
344 /// Execute all of the passes scheduled for execution by invoking
345 /// runOnModule method. Keep track of whether any of the passes modifies
346 /// the module, and if so, return true.
348 ModulePassManager_New::runOnModule(Module &M) {
349 bool Changed = false;
350 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
351 e = PassVector.end(); itr != e; ++itr) {
353 ModulePass *MP = dynamic_cast<ModulePass*>(P);
354 Changed |= MP->runOnModule(M);
359 /// Schedule all passes from the queue by adding them in their
360 /// respective manager's queue.
362 PassManagerImpl_New::schedulePasses() {
366 /// Add pass P to the queue of passes to run.
368 PassManagerImpl_New::add(Pass *P) {
372 // PassManager_New implementation
373 /// Add P into active pass manager or use new module pass manager to
376 PassManagerImpl_New::addPass(Pass *P) {
378 if (!activeManager) {
379 activeManager = new ModulePassManager_New();
380 PassManagers.push_back(activeManager);
383 return activeManager->addPass(P);
386 /// run - Execute all of the passes scheduled for execution. Keep track of
387 /// whether any of the passes modifies the module, and if so, return true.
389 PassManagerImpl_New::run(Module &M) {
392 bool Changed = false;
393 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
394 e = PassManagers.end(); itr != e; ++itr) {
395 ModulePassManager_New *pm = *itr;
396 Changed |= pm->runOnModule(M);
401 /// Create new pass manager
402 PassManager_New::PassManager_New() {
403 PM = new PassManagerImpl_New();
406 /// add - Add a pass to the queue of passes to run. This passes ownership of
407 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
408 /// will be destroyed as well, so there is no need to delete the pass. This
409 /// implies that all passes MUST be allocated with 'new'.
411 PassManager_New::add(Pass *P) {
415 /// run - Execute all of the passes scheduled for execution. Keep track of
416 /// whether any of the passes modifies the module, and if so, return true.
418 PassManager_New::run(Module &M) {