Add PassManager_New.
[oota-llvm.git] / lib / VMCore / PassManager.cpp
1 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // 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"
16 #include "llvm/Function.h"
17 #include "llvm/Module.h"
18
19 using namespace llvm;
20
21 /// BasicBlockPassManager implementation
22
23 /// Add pass P into PassVector and return TRUE. If this pass is not
24 /// manageable by this manager then return FALSE.
25 bool
26 BasicBlockPassManager_New::addPass (Pass *P) {
27
28   BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
29   if (!BP)
30     return false;
31
32   // TODO: Check if it suitable to manage P using this BasicBlockPassManager
33   // or we need another instance of BasicBlockPassManager
34
35   // Add pass
36   PassVector.push_back(BP);
37   return true;
38 }
39
40 /// Execute all of the passes scheduled for execution by invoking 
41 /// runOnBasicBlock method.  Keep track of whether any of the passes modifies 
42 /// the function, and if so, return true.
43 bool
44 BasicBlockPassManager_New::runOnFunction(Function &F) {
45
46   bool Changed = false;
47   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
48     for (std::vector<Pass *>::iterator itr = PassVector.begin(),
49            e = PassVector.end(); itr != e; ++itr) {
50       Pass *P = *itr;
51       BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
52       Changed |= BP->runOnBasicBlock(*I);
53     }
54   return Changed;
55 }
56
57 // FunctionPassManager_New implementation
58
59 ///////////////////////////////////////////////////////////////////////////////
60 // FunctionPassManager
61
62 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
63 /// either use it into active basic block pass manager or create new basic
64 /// block pass manager to handle pass P.
65 bool
66 FunctionPassManager_New::addPass (Pass *P) {
67
68   // If P is a BasicBlockPass then use BasicBlockPassManager_New.
69   if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
70
71     if (!activeBBPassManager
72         || !activeBBPassManager->addPass(BP)) {
73
74       activeBBPassManager = new BasicBlockPassManager_New();
75
76       PassVector.push_back(activeBBPassManager);
77       assert (!activeBBPassManager->addPass(BP) &&
78               "Unable to add Pass");
79     }
80     return true;
81   }
82
83   FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
84   if (!FP)
85     return false;
86
87   // TODO: Check if it suitable to manage P using this FunctionPassManager
88   // or we need another instance of FunctionPassManager
89
90   PassVector.push_back(FP);
91   activeBBPassManager = NULL;
92   return true;
93 }
94
95 /// Execute all of the passes scheduled for execution by invoking 
96 /// runOnFunction method.  Keep track of whether any of the passes modifies 
97 /// the function, and if so, return true.
98 bool
99 FunctionPassManager_New::runOnModule(Module &M) {
100
101   bool Changed = false;
102   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
103     for (std::vector<Pass *>::iterator itr = PassVector.begin(),
104            e = PassVector.end(); itr != e; ++itr) {
105       Pass *P = *itr;
106       FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
107       Changed |= FP->runOnFunction(*I);
108     }
109   return Changed;
110 }
111
112
113 // ModulePassManager implementation
114
115 /// Add P into pass vector if it is manageble. If P is a FunctionPass
116 /// then use FunctionPassManager_New to manage it. Return FALSE if P
117 /// is not manageable by this manager.
118 bool
119 ModulePassManager_New::addPass (Pass *P) {
120
121   // If P is FunctionPass then use function pass maanager.
122   if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
123
124     activeFunctionPassManager = NULL;
125
126     if (!activeFunctionPassManager
127         || !activeFunctionPassManager->addPass(P)) {
128
129       activeFunctionPassManager = new FunctionPassManager_New();
130
131       PassVector.push_back(activeFunctionPassManager);
132       assert (!activeFunctionPassManager->addPass(FP) &&
133               "Unable to add Pass");
134     }
135     return true;
136   }
137
138   ModulePass *MP = dynamic_cast<ModulePass *>(P);
139   if (!MP)
140     return false;
141
142   // TODO: Check if it suitable to manage P using this ModulePassManager
143   // or we need another instance of ModulePassManager
144
145   PassVector.push_back(MP);
146   activeFunctionPassManager = NULL;
147   return true;
148 }
149
150
151 /// Execute all of the passes scheduled for execution by invoking 
152 /// runOnModule method.  Keep track of whether any of the passes modifies 
153 /// the module, and if so, return true.
154 bool
155 ModulePassManager_New::runOnModule(Module &M) {
156   bool Changed = false;
157   for (std::vector<Pass *>::iterator itr = PassVector.begin(),
158          e = PassVector.end(); itr != e; ++itr) {
159     Pass *P = *itr;
160     ModulePass *MP = dynamic_cast<ModulePass*>(P);
161     Changed |= MP->runOnModule(M);
162   }
163   return Changed;
164 }
165
166 /// Schedule all passes from the queue by adding them in their
167 /// respective manager's queue. 
168 void
169 PassManager_New::schedulePasses() {
170   /* TODO */
171 }
172
173 /// Add pass P to the queue of passes to run.
174 void
175 PassManager_New::add(Pass *P) {
176   /* TODO */
177 }
178
179 // PassManager_New implementation
180 /// Add P into active pass manager or use new module pass manager to
181 /// manage it.
182 bool
183 PassManager_New::addPass (Pass *P) {
184
185   if (!activeManager) {
186     activeManager = new ModulePassManager_New();
187     PassManagers.push_back(activeManager);
188   }
189
190   return activeManager->addPass(P);
191 }
192
193 /// run - Execute all of the passes scheduled for execution.  Keep track of
194 /// whether any of the passes modifies the module, and if so, return true.
195 bool
196 PassManager_New::run(Module &M) {
197
198   schedulePasses();
199   bool Changed = false;
200   for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
201          e = PassManagers.end(); itr != e; ++itr) {
202     ModulePassManager_New *pm = *itr;
203     Changed |= pm->runOnModule(M);
204   }
205   return Changed;
206 }