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"
20 // PassManagerAnalysisHelper implementation
22 /// Return true IFF pass P's required analysis set does not required new
24 bool PassManagerAnalysisHelper::manageablePass(Pass *P) {
26 AnalysisUsage AnUsage;
27 P->getAnalysisUsage(AnUsage);
29 // If this pass is not preserving information that is required by the other passes
30 // managed by this manager then use new manager
35 /// Return true IFF AnalysisID AID is currently available.
36 bool PassManagerAnalysisHelper::analysisCurrentlyAvailable(AnalysisID AID) {
42 /// Augment RequiredSet by adding analysis required by pass P.
43 void PassManagerAnalysisHelper::noteDownRequiredAnalysis(Pass *P) {
48 /// Remove AnalysisID from the RequiredSet
49 void PassManagerAnalysisHelper::removeAnalysis(AnalysisID AID) {
54 /// Remove Analyss not preserved by Pass P
55 void PassManagerAnalysisHelper::removeNotPreservedAnalysis(Pass *P) {
60 /// BasicBlockPassManager implementation
62 /// Add pass P into PassVector and return true. If this pass is not
63 /// manageable by this manager then return false.
65 BasicBlockPassManager_New::addPass(Pass *P) {
67 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
71 // If this pass does not preserve anlysis that is used by other passes
72 // managed by this manager than it is not a suiable pass for this manager.
73 if (!manageablePass(P))
76 // Take a note of analysis required by this pass.
77 noteDownRequiredAnalysis(P);
80 PassVector.push_back(BP);
84 /// Execute all of the passes scheduled for execution by invoking
85 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
86 /// the function, and if so, return true.
88 BasicBlockPassManager_New::runOnFunction(Function &F) {
91 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
92 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
93 e = PassVector.end(); itr != e; ++itr) {
95 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
96 Changed |= BP->runOnBasicBlock(*I);
101 // FunctionPassManager_New implementation
103 // FunctionPassManager
105 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
106 /// either use it into active basic block pass manager or create new basic
107 /// block pass manager to handle pass P.
109 FunctionPassManager_New::addPass(Pass *P) {
111 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
112 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
114 if (!activeBBPassManager
115 || !activeBBPassManager->addPass(BP)) {
117 activeBBPassManager = new BasicBlockPassManager_New();
119 PassVector.push_back(activeBBPassManager);
120 if (!activeBBPassManager->addPass(BP))
121 assert(0 && "Unable to add Pass");
126 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
130 // If this pass does not preserve anlysis that is used by other passes
131 // managed by this manager than it is not a suiable pass for this manager.
132 if (!manageablePass(P))
135 // Take a note of analysis required by this pass.
136 noteDownRequiredAnalysis(P);
138 PassVector.push_back(FP);
139 activeBBPassManager = NULL;
143 /// Execute all of the passes scheduled for execution by invoking
144 /// runOnFunction method. Keep track of whether any of the passes modifies
145 /// the function, and if so, return true.
147 FunctionPassManager_New::runOnModule(Module &M) {
149 bool Changed = false;
150 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
151 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
152 e = PassVector.end(); itr != e; ++itr) {
154 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
155 Changed |= FP->runOnFunction(*I);
161 // ModulePassManager implementation
163 /// Add P into pass vector if it is manageble. If P is a FunctionPass
164 /// then use FunctionPassManager_New to manage it. Return false if P
165 /// is not manageable by this manager.
167 ModulePassManager_New::addPass(Pass *P) {
169 // If P is FunctionPass then use function pass maanager.
170 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
172 activeFunctionPassManager = NULL;
174 if (!activeFunctionPassManager
175 || !activeFunctionPassManager->addPass(P)) {
177 activeFunctionPassManager = new FunctionPassManager_New();
179 PassVector.push_back(activeFunctionPassManager);
180 if (!activeFunctionPassManager->addPass(FP))
181 assert(0 && "Unable to add pass");
186 ModulePass *MP = dynamic_cast<ModulePass *>(P);
190 // If this pass does not preserve anlysis that is used by other passes
191 // managed by this manager than it is not a suiable pass for this manager.
192 if (!manageablePass(P))
195 // Take a note of analysis required by this pass.
196 noteDownRequiredAnalysis(P);
198 PassVector.push_back(MP);
199 activeFunctionPassManager = NULL;
204 /// Execute all of the passes scheduled for execution by invoking
205 /// runOnModule method. Keep track of whether any of the passes modifies
206 /// the module, and if so, return true.
208 ModulePassManager_New::runOnModule(Module &M) {
209 bool Changed = false;
210 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
211 e = PassVector.end(); itr != e; ++itr) {
213 ModulePass *MP = dynamic_cast<ModulePass*>(P);
214 Changed |= MP->runOnModule(M);
219 /// Schedule all passes from the queue by adding them in their
220 /// respective manager's queue.
222 PassManager_New::schedulePasses() {
226 /// Add pass P to the queue of passes to run.
228 PassManager_New::add(Pass *P) {
232 // PassManager_New implementation
233 /// Add P into active pass manager or use new module pass manager to
236 PassManager_New::addPass(Pass *P) {
238 if (!activeManager) {
239 activeManager = new ModulePassManager_New();
240 PassManagers.push_back(activeManager);
243 return activeManager->addPass(P);
246 /// run - Execute all of the passes scheduled for execution. Keep track of
247 /// whether any of the passes modifies the module, and if so, return true.
249 PassManager_New::run(Module &M) {
252 bool Changed = false;
253 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
254 e = PassManagers.end(); itr != e; ++itr) {
255 ModulePassManager_New *pm = *itr;
256 Changed |= pm->runOnModule(M);