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 Pass,
26 public PassManagerAnalysisHelper {
29 BasicBlockPassManager_New() { }
31 /// Add a pass into a passmanager queue.
32 bool addPass(Pass *p);
34 /// Execute all of the passes scheduled for execution. Keep track of
35 /// whether any of the passes modifies the function, and if so, return true.
36 bool runOnFunction(Function &F);
39 // Collection of pass that are managed by this manager
40 std::vector<Pass *> PassVector;
43 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
44 /// It batches all function passes and basic block pass managers together and
45 /// sequence them to process one function at a time before processing next
47 class FunctionPassManagerImpl_New : public Pass,
48 public PassManagerAnalysisHelper {
50 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
51 FunctionPassManagerImpl_New() {
52 activeBBPassManager = NULL;
54 ~FunctionPassManagerImpl_New() { /* TODO */ };
56 /// add - Add a pass to the queue of passes to run. This passes
57 /// ownership of the Pass to the PassManager. When the
58 /// PassManager_X is destroyed, the pass will be destroyed as well, so
59 /// there is no need to delete the pass. (TODO delete passes.)
60 /// This implies that all passes MUST be allocated with 'new'.
61 void add(Pass *P) { /* TODO*/ }
63 /// Add pass into the pass manager queue.
64 bool addPass(Pass *P);
66 /// Execute all of the passes scheduled for execution. Keep
67 /// track of whether any of the passes modifies the function, and if
69 bool runOnModule(Module &M);
72 // Collection of pass that are manged by this manager
73 std::vector<Pass *> PassVector;
75 // Active Pass Managers
76 BasicBlockPassManager_New *activeBBPassManager;
79 /// ModulePassManager_New manages ModulePasses and function pass managers.
80 /// It batches all Module passes passes and function pass managers together and
81 /// sequence them to process one module.
82 class ModulePassManager_New : public Pass,
83 public PassManagerAnalysisHelper {
86 ModulePassManager_New() { activeFunctionPassManager = NULL; }
88 /// Add a pass into a passmanager queue.
89 bool addPass(Pass *p);
91 /// run - Execute all of the passes scheduled for execution. Keep track of
92 /// whether any of the passes modifies the module, and if so, return true.
93 bool runOnModule(Module &M);
96 // Collection of pass that are managed by this manager
97 std::vector<Pass *> PassVector;
99 // Active Pass Manager
100 FunctionPassManagerImpl_New *activeFunctionPassManager;
103 /// PassManager_New manages ModulePassManagers
104 class PassManagerImpl_New : public Pass,
105 public PassManagerAnalysisHelper {
109 /// add - Add a pass to the queue of passes to run. This passes ownership of
110 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
111 /// will be destroyed as well, so there is no need to delete the pass. This
112 /// implies that all passes MUST be allocated with 'new'.
115 /// run - Execute all of the passes scheduled for execution. Keep track of
116 /// whether any of the passes modifies the module, and if so, return true.
121 /// Add a pass into a passmanager queue. This is used by schedulePasses
122 bool addPass(Pass *p);
124 /// Schedule all passes collected in pass queue using add(). Add all the
125 /// schedule passes into various manager's queue using addPass().
126 void schedulePasses();
128 // Collection of pass managers
129 std::vector<ModulePassManager_New *> PassManagers;
131 // Collection of pass that are not yet scheduled
132 std::vector<Pass *> PassVector;
134 // Active Pass Manager
135 ModulePassManager_New *activeManager;
138 } // End of llvm namespace
140 // PassManagerAnalysisHelper implementation
142 /// Return true IFF pass P's required analysis set does not required new
144 bool PassManagerAnalysisHelper::manageablePass(Pass *P) {
146 AnalysisUsage AnUsage;
147 P->getAnalysisUsage(AnUsage);
149 // If this pass is not preserving information that is required by the other passes
150 // managed by this manager then use new manager
155 /// Return true IFF AnalysisID AID is currently available.
156 bool PassManagerAnalysisHelper::analysisCurrentlyAvailable(AnalysisID AID) {
162 /// Augment RequiredSet by adding analysis required by pass P.
163 void PassManagerAnalysisHelper::noteDownRequiredAnalysis(Pass *P) {
168 /// Remove AnalysisID from the RequiredSet
169 void PassManagerAnalysisHelper::removeAnalysis(AnalysisID AID) {
174 /// Remove Analyss not preserved by Pass P
175 void PassManagerAnalysisHelper::removeNotPreservedAnalysis(Pass *P) {
180 /// BasicBlockPassManager implementation
182 /// Add pass P into PassVector and return true. If this pass is not
183 /// manageable by this manager then return false.
185 BasicBlockPassManager_New::addPass(Pass *P) {
187 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
191 // If this pass does not preserve anlysis that is used by other passes
192 // managed by this manager than it is not a suiable pass for this manager.
193 if (!manageablePass(P))
196 // Take a note of analysis required by this pass.
197 noteDownRequiredAnalysis(P);
200 PassVector.push_back(BP);
204 /// Execute all of the passes scheduled for execution by invoking
205 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
206 /// the function, and if so, return true.
208 BasicBlockPassManager_New::runOnFunction(Function &F) {
210 bool Changed = false;
211 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
212 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
213 e = PassVector.end(); itr != e; ++itr) {
215 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
216 Changed |= BP->runOnBasicBlock(*I);
221 // FunctionPassManager_New implementation
222 /// Create new Function pass manager
223 FunctionPassManager_New::FunctionPassManager_New() {
224 FPM = new FunctionPassManagerImpl_New();
227 /// add - Add a pass to the queue of passes to run. This passes
228 /// ownership of the Pass to the PassManager. When the
229 /// PassManager_X is destroyed, the pass will be destroyed as well, so
230 /// there is no need to delete the pass. (TODO delete passes.)
231 /// This implies that all passes MUST be allocated with 'new'.
233 FunctionPassManager_New::add(Pass *P) {
237 /// Execute all of the passes scheduled for execution. Keep
238 /// track of whether any of the passes modifies the function, and if
241 FunctionPassManager_New::runOnModule(Module &M) {
242 return FPM->runOnModule(M);
245 // FunctionPassManagerImpl_New implementation
247 // FunctionPassManager
249 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
250 /// either use it into active basic block pass manager or create new basic
251 /// block pass manager to handle pass P.
253 FunctionPassManagerImpl_New::addPass(Pass *P) {
255 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
256 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
258 if (!activeBBPassManager
259 || !activeBBPassManager->addPass(BP)) {
261 activeBBPassManager = new BasicBlockPassManager_New();
263 PassVector.push_back(activeBBPassManager);
264 if (!activeBBPassManager->addPass(BP))
265 assert(0 && "Unable to add Pass");
270 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
274 // If this pass does not preserve anlysis that is used by other passes
275 // managed by this manager than it is not a suiable pass for this manager.
276 if (!manageablePass(P))
279 // Take a note of analysis required by this pass.
280 noteDownRequiredAnalysis(P);
282 PassVector.push_back(FP);
283 activeBBPassManager = NULL;
287 /// Execute all of the passes scheduled for execution by invoking
288 /// runOnFunction method. Keep track of whether any of the passes modifies
289 /// the function, and if so, return true.
291 FunctionPassManagerImpl_New::runOnModule(Module &M) {
293 bool Changed = false;
294 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
295 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
296 e = PassVector.end(); itr != e; ++itr) {
298 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
299 Changed |= FP->runOnFunction(*I);
305 // ModulePassManager implementation
307 /// Add P into pass vector if it is manageble. If P is a FunctionPass
308 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
309 /// is not manageable by this manager.
311 ModulePassManager_New::addPass(Pass *P) {
313 // If P is FunctionPass then use function pass maanager.
314 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
316 activeFunctionPassManager = NULL;
318 if (!activeFunctionPassManager
319 || !activeFunctionPassManager->addPass(P)) {
321 activeFunctionPassManager = new FunctionPassManagerImpl_New();
323 PassVector.push_back(activeFunctionPassManager);
324 if (!activeFunctionPassManager->addPass(FP))
325 assert(0 && "Unable to add pass");
330 ModulePass *MP = dynamic_cast<ModulePass *>(P);
334 // If this pass does not preserve anlysis that is used by other passes
335 // managed by this manager than it is not a suiable pass for this manager.
336 if (!manageablePass(P))
339 // Take a note of analysis required by this pass.
340 noteDownRequiredAnalysis(P);
342 PassVector.push_back(MP);
343 activeFunctionPassManager = NULL;
348 /// Execute all of the passes scheduled for execution by invoking
349 /// runOnModule method. Keep track of whether any of the passes modifies
350 /// the module, and if so, return true.
352 ModulePassManager_New::runOnModule(Module &M) {
353 bool Changed = false;
354 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
355 e = PassVector.end(); itr != e; ++itr) {
357 ModulePass *MP = dynamic_cast<ModulePass*>(P);
358 Changed |= MP->runOnModule(M);
363 /// Schedule all passes from the queue by adding them in their
364 /// respective manager's queue.
366 PassManagerImpl_New::schedulePasses() {
370 /// Add pass P to the queue of passes to run.
372 PassManagerImpl_New::add(Pass *P) {
376 // PassManager_New implementation
377 /// Add P into active pass manager or use new module pass manager to
380 PassManagerImpl_New::addPass(Pass *P) {
382 if (!activeManager) {
383 activeManager = new ModulePassManager_New();
384 PassManagers.push_back(activeManager);
387 return activeManager->addPass(P);
390 /// run - Execute all of the passes scheduled for execution. Keep track of
391 /// whether any of the passes modifies the module, and if so, return true.
393 PassManagerImpl_New::run(Module &M) {
396 bool Changed = false;
397 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
398 e = PassManagers.end(); itr != e; ++itr) {
399 ModulePassManager_New *pm = *itr;
400 Changed |= pm->runOnModule(M);
405 /// Create new pass manager
406 PassManager_New::PassManager_New() {
407 PM = new PassManagerImpl_New();
410 /// add - Add a pass to the queue of passes to run. This passes ownership of
411 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
412 /// will be destroyed as well, so there is no need to delete the pass. This
413 /// implies that all passes MUST be allocated with 'new'.
415 PassManager_New::add(Pass *P) {
419 /// run - Execute all of the passes scheduled for execution. Keep track of
420 /// whether any of the passes modifies the module, and if so, return true.
422 PassManager_New::run(Module &M) {