1 //===- Pass.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 infrastructure. It is primarily
11 // responsible with ensuring that passes are executed and batched together
14 //===----------------------------------------------------------------------===//
16 #include "llvm/PassManager.h"
17 #include "llvm/Module.h"
18 #include "llvm/ModuleProvider.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "llvm/Support/TypeInfo.h"
25 //===----------------------------------------------------------------------===//
26 // Pass Implementation
29 // Force out-of-line virtual method.
30 ModulePass::~ModulePass() { }
32 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
33 return Resolver->getAnalysisToUpdate(AnalysisID, true) != 0;
36 // dumpPassStructure - Implement the -debug-passes=Structure option
37 void Pass::dumpPassStructure(unsigned Offset) {
38 cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
41 // getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
43 const char *Pass::getPassName() const {
44 if (const PassInfo *PI = getPassInfo())
45 return PI->getPassName();
46 return typeid(*this).name();
49 // print - Print out the internal state of the pass. This is called by Analyze
50 // to print out the contents of an analysis. Otherwise it is not necessary to
51 // implement this method.
53 void Pass::print(std::ostream &O,const Module*) const {
54 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
57 // dump - call print(cerr);
58 void Pass::dump() const {
59 print(*cerr.stream(), 0);
62 //===----------------------------------------------------------------------===//
63 // ImmutablePass Implementation
65 // Force out-of-line virtual method.
66 ImmutablePass::~ImmutablePass() { }
68 //===----------------------------------------------------------------------===//
69 // FunctionPass Implementation
72 // run - On a module, we run this pass by initializing, runOnFunction'ing once
73 // for every function in the module, then by finalizing.
75 bool FunctionPass::runOnModule(Module &M) {
76 bool Changed = doInitialization(M);
78 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
79 if (!I->isDeclaration()) // Passes are not run on external functions!
80 Changed |= runOnFunction(*I);
82 return Changed | doFinalization(M);
85 // run - On a function, we simply initialize, run the function, then finalize.
87 bool FunctionPass::run(Function &F) {
88 if (F.isDeclaration()) return false;// Passes are not run on external functions!
90 bool Changed = doInitialization(*F.getParent());
91 Changed |= runOnFunction(F);
92 return Changed | doFinalization(*F.getParent());
95 //===----------------------------------------------------------------------===//
96 // BasicBlockPass Implementation
99 // To run this pass on a function, we simply call runOnBasicBlock once for each
102 bool BasicBlockPass::runOnFunction(Function &F) {
103 bool Changed = doInitialization(F);
104 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
105 Changed |= runOnBasicBlock(*I);
106 return Changed | doFinalization(F);
109 // To run directly on the basic block, we initialize, runOnBasicBlock, then
112 bool BasicBlockPass::runPass(BasicBlock &BB) {
113 Function &F = *BB.getParent();
114 Module &M = *F.getParent();
115 bool Changed = doInitialization(M);
116 Changed |= doInitialization(F);
117 Changed |= runOnBasicBlock(BB);
118 Changed |= doFinalization(F);
119 Changed |= doFinalization(M);
123 //===----------------------------------------------------------------------===//
124 // Pass Registration mechanism
127 class PassRegistrar {
128 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
130 std::map<TypeInfo, PassInfo*> PassInfoMap;
132 /// AnalysisGroupInfo - Keep track of information for each analysis group.
133 struct AnalysisGroupInfo {
134 const PassInfo *DefaultImpl;
135 std::set<const PassInfo *> Implementations;
136 AnalysisGroupInfo() : DefaultImpl(0) {}
139 /// AnalysisGroupInfoMap - Information for each analysis group.
140 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
144 const PassInfo *GetPassInfo(const std::type_info &TI) const {
145 std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.find(TI);
146 return I != PassInfoMap.end() ? I->second : 0;
149 void RegisterPass(PassInfo &PI) {
151 PassInfoMap.insert(std::make_pair(TypeInfo(PI.getTypeInfo()),&PI)).second;
152 assert(Inserted && "Pass registered multiple times!");
155 void UnregisterPass(PassInfo &PI) {
156 std::map<TypeInfo, PassInfo*>::iterator I =
157 PassInfoMap.find(PI.getTypeInfo());
158 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
160 // Remove pass from the map.
161 PassInfoMap.erase(I);
164 void EnumerateWith(PassRegistrationListener *L) {
165 for (std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.begin(),
166 E = PassInfoMap.end(); I != E; ++I)
167 L->passEnumerate(I->second);
171 /// Analysis Group Mechanisms.
172 void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
173 const PassInfo *ImplementationInfo,
175 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
176 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
177 "Cannot add a pass to the same analysis group more than once!");
178 AGI.Implementations.insert(ImplementationInfo);
180 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
181 "Default implementation for analysis group already specified!");
182 assert(ImplementationInfo->getNormalCtor() &&
183 "Cannot specify pass as default if it does not have a default ctor");
184 AGI.DefaultImpl = ImplementationInfo;
185 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
191 static ManagedStatic<PassRegistrar> PassRegistrarObj;
192 static std::vector<PassRegistrationListener*> *Listeners = 0;
194 // getPassInfo - Return the PassInfo data structure that corresponds to this
196 const PassInfo *Pass::getPassInfo() const {
197 if (PassInfoCache) return PassInfoCache;
198 return lookupPassInfo(typeid(*this));
201 const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
202 return PassRegistrarObj->GetPassInfo(TI);
205 void RegisterPassBase::registerPass() {
206 PassRegistrarObj->RegisterPass(PIObj);
208 // Notify any listeners.
210 for (std::vector<PassRegistrationListener*>::iterator
211 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
212 (*I)->passRegistered(&PIObj);
215 void RegisterPassBase::unregisterPass() {
216 PassRegistrarObj->UnregisterPass(PIObj);
219 //===----------------------------------------------------------------------===//
220 // Analysis Group Implementation Code
221 //===----------------------------------------------------------------------===//
223 // RegisterAGBase implementation
225 RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
226 const std::type_info *Pass, bool isDefault)
227 : RegisterPassBase(Interface),
228 ImplementationInfo(0), isDefaultImplementation(isDefault) {
230 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
231 if (InterfaceInfo == 0) {
232 // First reference to Interface, register it now.
234 InterfaceInfo = &PIObj;
236 assert(PIObj.isAnalysisGroup() &&
237 "Trying to join an analysis group that is a normal pass!");
240 ImplementationInfo = Pass::lookupPassInfo(*Pass);
241 assert(ImplementationInfo &&
242 "Must register pass before adding to AnalysisGroup!");
244 // Make sure we keep track of the fact that the implementation implements
246 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
247 IIPI->addInterfaceImplemented(InterfaceInfo);
249 PassRegistrarObj->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
253 void RegisterAGBase::setGroupName(const char *Name) {
254 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
255 InterfaceInfo->setPassName(Name);
259 //===----------------------------------------------------------------------===//
260 // PassRegistrationListener implementation
263 // PassRegistrationListener ctor - Add the current object to the list of
264 // PassRegistrationListeners...
265 PassRegistrationListener::PassRegistrationListener() {
266 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
267 Listeners->push_back(this);
270 // dtor - Remove object from list of listeners...
271 PassRegistrationListener::~PassRegistrationListener() {
272 std::vector<PassRegistrationListener*>::iterator I =
273 std::find(Listeners->begin(), Listeners->end(), this);
274 assert(Listeners && I != Listeners->end() &&
275 "PassRegistrationListener not registered!");
278 if (Listeners->empty()) {
284 // enumeratePasses - Iterate over the registered passes, calling the
285 // passEnumerate callback on each PassInfo object.
287 void PassRegistrationListener::enumeratePasses() {
288 PassRegistrarObj->EnumerateWith(this);
291 //===----------------------------------------------------------------------===//
292 // AnalysisUsage Class Implementation
296 struct GetCFGOnlyPasses : public PassRegistrationListener {
297 std::vector<AnalysisID> &CFGOnlyList;
298 GetCFGOnlyPasses(std::vector<AnalysisID> &L) : CFGOnlyList(L) {}
300 void passEnumerate(const PassInfo *P) {
301 if (P->isCFGOnlyPass())
302 CFGOnlyList.push_back(P);
307 // setPreservesCFG - This function should be called to by the pass, iff they do
310 // 1. Add or remove basic blocks from the function
311 // 2. Modify terminator instructions in any way.
313 // This function annotates the AnalysisUsage info object to say that analyses
314 // that only depend on the CFG are preserved by this pass.
316 void AnalysisUsage::setPreservesCFG() {
317 // Since this transformation doesn't modify the CFG, it preserves all analyses
318 // that only depend on the CFG (like dominators, loop info, etc...)
319 GetCFGOnlyPasses(Preserved).enumeratePasses();