1 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVM Pass Manager infrastructure.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/PassManager.h"
16 #include "llvm/Function.h"
17 #include "llvm/Module.h"
21 // PassManagerAnalysisHelper implementation
23 /// Return TRUE IFF pass P's required analysis set does not required new
25 bool PassManagerAnalysisHelper::manageablePass(Pass *P) {
27 AnalysisUsage AnUsage;
28 P->getAnalysisUsage(AnUsage);
30 // If this pass is not preserving information that is required by the other passes
31 // managed by this manager then use new manager
36 /// Return TRUE iff AnalysisID AID is currently available.
37 bool PassManagerAnalysisHelper::analysisCurrentlyAvailable(AnalysisID AID) {
43 /// Augment RequiredSet by adding analysis required by pass P.
44 void PassManagerAnalysisHelper::noteDownRequiredAnalysis(Pass *P) {
49 /// Remove AnalysisID from the RequiredSet
50 void PassManagerAnalysisHelper::removeAnalysis(AnalysisID AID) {
55 /// Remove Analyss not preserved by Pass P
56 void PassManagerAnalysisHelper::removeNotPreservedAnalysis(Pass *P) {
61 /// BasicBlockPassManager implementation
63 /// Add pass P into PassVector and return TRUE. If this pass is not
64 /// manageable by this manager then return FALSE.
66 BasicBlockPassManager_New::addPass (Pass *P) {
68 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
72 // TODO: Check if it suitable to manage P using this BasicBlockPassManager
73 // or we need another instance of BasicBlockPassManager
76 PassVector.push_back(BP);
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.
84 BasicBlockPassManager_New::runOnFunction(Function &F) {
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) {
91 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
92 Changed |= BP->runOnBasicBlock(*I);
97 // FunctionPassManager_New implementation
99 ///////////////////////////////////////////////////////////////////////////////
100 // FunctionPassManager
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.
106 FunctionPassManager_New::addPass (Pass *P) {
108 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
109 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
111 if (!activeBBPassManager
112 || !activeBBPassManager->addPass(BP)) {
114 activeBBPassManager = new BasicBlockPassManager_New();
116 PassVector.push_back(activeBBPassManager);
117 assert (!activeBBPassManager->addPass(BP) &&
118 "Unable to add Pass");
123 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
127 // TODO: Check if it suitable to manage P using this FunctionPassManager
128 // or we need another instance of FunctionPassManager
130 PassVector.push_back(FP);
131 activeBBPassManager = NULL;
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.
139 FunctionPassManager_New::runOnModule(Module &M) {
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) {
146 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
147 Changed |= FP->runOnFunction(*I);
153 // ModulePassManager implementation
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.
159 ModulePassManager_New::addPass (Pass *P) {
161 // If P is FunctionPass then use function pass maanager.
162 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
164 activeFunctionPassManager = NULL;
166 if (!activeFunctionPassManager
167 || !activeFunctionPassManager->addPass(P)) {
169 activeFunctionPassManager = new FunctionPassManager_New();
171 PassVector.push_back(activeFunctionPassManager);
172 assert (!activeFunctionPassManager->addPass(FP) &&
173 "Unable to add Pass");
178 ModulePass *MP = dynamic_cast<ModulePass *>(P);
182 // TODO: Check if it suitable to manage P using this ModulePassManager
183 // or we need another instance of ModulePassManager
185 PassVector.push_back(MP);
186 activeFunctionPassManager = NULL;
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.
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) {
200 ModulePass *MP = dynamic_cast<ModulePass*>(P);
201 Changed |= MP->runOnModule(M);
206 /// Schedule all passes from the queue by adding them in their
207 /// respective manager's queue.
209 PassManager_New::schedulePasses() {
213 /// Add pass P to the queue of passes to run.
215 PassManager_New::add(Pass *P) {
219 // PassManager_New implementation
220 /// Add P into active pass manager or use new module pass manager to
223 PassManager_New::addPass (Pass *P) {
225 if (!activeManager) {
226 activeManager = new ModulePassManager_New();
227 PassManagers.push_back(activeManager);
230 return activeManager->addPass(P);
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.
236 PassManager_New::run(Module &M) {
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);