Introduce PassManagerAnalysisHelper.
[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 // PassManagerAnalysisHelper implementation
22
23 /// Return TRUE IFF pass P's required analysis set does not required new
24 /// manager.
25 bool PassManagerAnalysisHelper::manageablePass(Pass *P) {
26
27   AnalysisUsage AnUsage;
28   P->getAnalysisUsage(AnUsage);
29
30   // If this pass is not preserving information that is required by the other passes
31   // managed by this manager then use new manager
32   // TODO
33   return true;
34 }
35
36 /// Return TRUE iff AnalysisID AID is currently available.
37 bool PassManagerAnalysisHelper::analysisCurrentlyAvailable(AnalysisID AID) {
38
39   // TODO
40   return false;
41 }
42
43 /// Augment RequiredSet by adding analysis required by pass P.
44 void PassManagerAnalysisHelper::noteDownRequiredAnalysis(Pass *P) {
45
46   // TODO
47 }
48
49 /// Remove AnalysisID from the RequiredSet
50 void PassManagerAnalysisHelper::removeAnalysis(AnalysisID AID) {
51
52   // TODO
53 }
54
55 /// Remove Analyss not preserved by Pass P
56 void PassManagerAnalysisHelper::removeNotPreservedAnalysis(Pass *P) {
57
58   // TODO
59 }
60
61 /// BasicBlockPassManager implementation
62
63 /// Add pass P into PassVector and return TRUE. If this pass is not
64 /// manageable by this manager then return FALSE.
65 bool
66 BasicBlockPassManager_New::addPass (Pass *P) {
67
68   BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
69   if (!BP)
70     return false;
71
72   // TODO: Check if it suitable to manage P using this BasicBlockPassManager
73   // or we need another instance of BasicBlockPassManager
74
75   // Add pass
76   PassVector.push_back(BP);
77   return true;
78 }
79
80 /// Execute all of the passes scheduled for execution by invoking 
81 /// runOnBasicBlock method.  Keep track of whether any of the passes modifies 
82 /// the function, and if so, return true.
83 bool
84 BasicBlockPassManager_New::runOnFunction(Function &F) {
85
86   bool Changed = false;
87   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
88     for (std::vector<Pass *>::iterator itr = PassVector.begin(),
89            e = PassVector.end(); itr != e; ++itr) {
90       Pass *P = *itr;
91       BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
92       Changed |= BP->runOnBasicBlock(*I);
93     }
94   return Changed;
95 }
96
97 // FunctionPassManager_New implementation
98
99 ///////////////////////////////////////////////////////////////////////////////
100 // FunctionPassManager
101
102 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
103 /// either use it into active basic block pass manager or create new basic
104 /// block pass manager to handle pass P.
105 bool
106 FunctionPassManager_New::addPass (Pass *P) {
107
108   // If P is a BasicBlockPass then use BasicBlockPassManager_New.
109   if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
110
111     if (!activeBBPassManager
112         || !activeBBPassManager->addPass(BP)) {
113
114       activeBBPassManager = new BasicBlockPassManager_New();
115
116       PassVector.push_back(activeBBPassManager);
117       assert (!activeBBPassManager->addPass(BP) &&
118               "Unable to add Pass");
119     }
120     return true;
121   }
122
123   FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
124   if (!FP)
125     return false;
126
127   // TODO: Check if it suitable to manage P using this FunctionPassManager
128   // or we need another instance of FunctionPassManager
129
130   PassVector.push_back(FP);
131   activeBBPassManager = NULL;
132   return true;
133 }
134
135 /// Execute all of the passes scheduled for execution by invoking 
136 /// runOnFunction method.  Keep track of whether any of the passes modifies 
137 /// the function, and if so, return true.
138 bool
139 FunctionPassManager_New::runOnModule(Module &M) {
140
141   bool Changed = false;
142   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
143     for (std::vector<Pass *>::iterator itr = PassVector.begin(),
144            e = PassVector.end(); itr != e; ++itr) {
145       Pass *P = *itr;
146       FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
147       Changed |= FP->runOnFunction(*I);
148     }
149   return Changed;
150 }
151
152
153 // ModulePassManager implementation
154
155 /// Add P into pass vector if it is manageble. If P is a FunctionPass
156 /// then use FunctionPassManager_New to manage it. Return FALSE if P
157 /// is not manageable by this manager.
158 bool
159 ModulePassManager_New::addPass (Pass *P) {
160
161   // If P is FunctionPass then use function pass maanager.
162   if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
163
164     activeFunctionPassManager = NULL;
165
166     if (!activeFunctionPassManager
167         || !activeFunctionPassManager->addPass(P)) {
168
169       activeFunctionPassManager = new FunctionPassManager_New();
170
171       PassVector.push_back(activeFunctionPassManager);
172       assert (!activeFunctionPassManager->addPass(FP) &&
173               "Unable to add Pass");
174     }
175     return true;
176   }
177
178   ModulePass *MP = dynamic_cast<ModulePass *>(P);
179   if (!MP)
180     return false;
181
182   // TODO: Check if it suitable to manage P using this ModulePassManager
183   // or we need another instance of ModulePassManager
184
185   PassVector.push_back(MP);
186   activeFunctionPassManager = NULL;
187   return true;
188 }
189
190
191 /// Execute all of the passes scheduled for execution by invoking 
192 /// runOnModule method.  Keep track of whether any of the passes modifies 
193 /// the module, and if so, return true.
194 bool
195 ModulePassManager_New::runOnModule(Module &M) {
196   bool Changed = false;
197   for (std::vector<Pass *>::iterator itr = PassVector.begin(),
198          e = PassVector.end(); itr != e; ++itr) {
199     Pass *P = *itr;
200     ModulePass *MP = dynamic_cast<ModulePass*>(P);
201     Changed |= MP->runOnModule(M);
202   }
203   return Changed;
204 }
205
206 /// Schedule all passes from the queue by adding them in their
207 /// respective manager's queue. 
208 void
209 PassManager_New::schedulePasses() {
210   /* TODO */
211 }
212
213 /// Add pass P to the queue of passes to run.
214 void
215 PassManager_New::add(Pass *P) {
216   /* TODO */
217 }
218
219 // PassManager_New implementation
220 /// Add P into active pass manager or use new module pass manager to
221 /// manage it.
222 bool
223 PassManager_New::addPass (Pass *P) {
224
225   if (!activeManager) {
226     activeManager = new ModulePassManager_New();
227     PassManagers.push_back(activeManager);
228   }
229
230   return activeManager->addPass(P);
231 }
232
233 /// run - Execute all of the passes scheduled for execution.  Keep track of
234 /// whether any of the passes modifies the module, and if so, return true.
235 bool
236 PassManager_New::run(Module &M) {
237
238   schedulePasses();
239   bool Changed = false;
240   for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
241          e = PassManagers.end(); itr != e; ++itr) {
242     ModulePassManager_New *pm = *itr;
243     Changed |= pm->runOnModule(M);
244   }
245   return Changed;
246 }