LLVM API Documentation
00001 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the LLVM Pass infrastructure. It is primarily 00011 // responsible with ensuring that passes are executed and batched together 00012 // optimally. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #include "llvm/Pass.h" 00017 #include "llvm/PassManager.h" 00018 #include "llvm/Module.h" 00019 #include "llvm/ModuleProvider.h" 00020 #include "llvm/ADT/STLExtras.h" 00021 #include "llvm/Support/ManagedStatic.h" 00022 #include <algorithm> 00023 #include <map> 00024 #include <set> 00025 using namespace llvm; 00026 00027 //===----------------------------------------------------------------------===// 00028 // Pass Implementation 00029 // 00030 00031 // Force out-of-line virtual method. 00032 Pass::~Pass() { 00033 delete Resolver; 00034 } 00035 00036 // Force out-of-line virtual method. 00037 ModulePass::~ModulePass() { } 00038 00039 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const { 00040 return Resolver->getAnalysisToUpdate(AnalysisID, true) != 0; 00041 } 00042 00043 // dumpPassStructure - Implement the -debug-passes=Structure option 00044 void Pass::dumpPassStructure(unsigned Offset) { 00045 cerr << std::string(Offset*2, ' ') << getPassName() << "\n"; 00046 } 00047 00048 /// getPassName - Return a nice clean name for a pass. This usually 00049 /// implemented in terms of the name that is registered by one of the 00050 /// Registration templates, but can be overloaded directly. 00051 /// 00052 const char *Pass::getPassName() const { 00053 if (const PassInfo *PI = getPassInfo()) 00054 return PI->getPassName(); 00055 return "Unnamed pass: implement Pass::getPassName()"; 00056 } 00057 00058 // print - Print out the internal state of the pass. This is called by Analyze 00059 // to print out the contents of an analysis. Otherwise it is not necessary to 00060 // implement this method. 00061 // 00062 void Pass::print(std::ostream &O,const Module*) const { 00063 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n"; 00064 } 00065 00066 // dump - call print(cerr); 00067 void Pass::dump() const { 00068 print(*cerr.stream(), 0); 00069 } 00070 00071 //===----------------------------------------------------------------------===// 00072 // ImmutablePass Implementation 00073 // 00074 // Force out-of-line virtual method. 00075 ImmutablePass::~ImmutablePass() { } 00076 00077 //===----------------------------------------------------------------------===// 00078 // FunctionPass Implementation 00079 // 00080 00081 // run - On a module, we run this pass by initializing, runOnFunction'ing once 00082 // for every function in the module, then by finalizing. 00083 // 00084 bool FunctionPass::runOnModule(Module &M) { 00085 bool Changed = doInitialization(M); 00086 00087 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 00088 if (!I->isDeclaration()) // Passes are not run on external functions! 00089 Changed |= runOnFunction(*I); 00090 00091 return Changed | doFinalization(M); 00092 } 00093 00094 // run - On a function, we simply initialize, run the function, then finalize. 00095 // 00096 bool FunctionPass::run(Function &F) { 00097 // Passes are not run on external functions! 00098 if (F.isDeclaration()) return false; 00099 00100 bool Changed = doInitialization(*F.getParent()); 00101 Changed |= runOnFunction(F); 00102 return Changed | doFinalization(*F.getParent()); 00103 } 00104 00105 //===----------------------------------------------------------------------===// 00106 // BasicBlockPass Implementation 00107 // 00108 00109 // To run this pass on a function, we simply call runOnBasicBlock once for each 00110 // function. 00111 // 00112 bool BasicBlockPass::runOnFunction(Function &F) { 00113 bool Changed = doInitialization(F); 00114 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) 00115 Changed |= runOnBasicBlock(*I); 00116 return Changed | doFinalization(F); 00117 } 00118 00119 //===----------------------------------------------------------------------===// 00120 // Pass Registration mechanism 00121 // 00122 namespace { 00123 class PassRegistrar { 00124 /// PassInfoMap - Keep track of the passinfo object for each registered llvm 00125 /// pass. 00126 typedef std::map<intptr_t, const PassInfo*> MapType; 00127 MapType PassInfoMap; 00128 00129 /// AnalysisGroupInfo - Keep track of information for each analysis group. 00130 struct AnalysisGroupInfo { 00131 const PassInfo *DefaultImpl; 00132 std::set<const PassInfo *> Implementations; 00133 AnalysisGroupInfo() : DefaultImpl(0) {} 00134 }; 00135 00136 /// AnalysisGroupInfoMap - Information for each analysis group. 00137 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap; 00138 00139 public: 00140 00141 const PassInfo *GetPassInfo(intptr_t TI) const { 00142 MapType::const_iterator I = PassInfoMap.find(TI); 00143 return I != PassInfoMap.end() ? I->second : 0; 00144 } 00145 00146 void RegisterPass(const PassInfo &PI) { 00147 bool Inserted = 00148 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second; 00149 assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted; 00150 } 00151 00152 void UnregisterPass(const PassInfo &PI) { 00153 MapType::iterator I = PassInfoMap.find(PI.getTypeInfo()); 00154 assert(I != PassInfoMap.end() && "Pass registered but not in map!"); 00155 00156 // Remove pass from the map. 00157 PassInfoMap.erase(I); 00158 } 00159 00160 void EnumerateWith(PassRegistrationListener *L) { 00161 for (MapType::const_iterator I = PassInfoMap.begin(), 00162 E = PassInfoMap.end(); I != E; ++I) 00163 L->passEnumerate(I->second); 00164 } 00165 00166 00167 /// Analysis Group Mechanisms. 00168 void RegisterAnalysisGroup(PassInfo *InterfaceInfo, 00169 const PassInfo *ImplementationInfo, 00170 bool isDefault) { 00171 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo]; 00172 assert(AGI.Implementations.count(ImplementationInfo) == 0 && 00173 "Cannot add a pass to the same analysis group more than once!"); 00174 AGI.Implementations.insert(ImplementationInfo); 00175 if (isDefault) { 00176 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 && 00177 "Default implementation for analysis group already specified!"); 00178 assert(ImplementationInfo->getNormalCtor() && 00179 "Cannot specify pass as default if it does not have a default ctor"); 00180 AGI.DefaultImpl = ImplementationInfo; 00181 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor()); 00182 } 00183 } 00184 }; 00185 } 00186 00187 static std::vector<PassRegistrationListener*> *Listeners = 0; 00188 00189 // FIXME: This should use ManagedStatic to manage the pass registrar. 00190 // Unfortunately, we can't do this, because passes are registered with static 00191 // ctors, and having llvm_shutdown clear this map prevents successful 00192 // ressurection after llvm_shutdown is run. 00193 static PassRegistrar *getPassRegistrar() { 00194 static PassRegistrar *PassRegistrarObj = 0; 00195 if (!PassRegistrarObj) 00196 PassRegistrarObj = new PassRegistrar(); 00197 return PassRegistrarObj; 00198 } 00199 00200 // getPassInfo - Return the PassInfo data structure that corresponds to this 00201 // pass... 00202 const PassInfo *Pass::getPassInfo() const { 00203 return lookupPassInfo(PassID); 00204 } 00205 00206 const PassInfo *Pass::lookupPassInfo(intptr_t TI) { 00207 return getPassRegistrar()->GetPassInfo(TI); 00208 } 00209 00210 void PassInfo::registerPass() { 00211 getPassRegistrar()->RegisterPass(*this); 00212 00213 // Notify any listeners. 00214 if (Listeners) 00215 for (std::vector<PassRegistrationListener*>::iterator 00216 I = Listeners->begin(), E = Listeners->end(); I != E; ++I) 00217 (*I)->passRegistered(this); 00218 } 00219 00220 void PassInfo::unregisterPass() { 00221 getPassRegistrar()->UnregisterPass(*this); 00222 } 00223 00224 //===----------------------------------------------------------------------===// 00225 // Analysis Group Implementation Code 00226 //===----------------------------------------------------------------------===// 00227 00228 // RegisterAGBase implementation 00229 // 00230 RegisterAGBase::RegisterAGBase(const char *Name, intptr_t InterfaceID, 00231 intptr_t PassID, bool isDefault) 00232 : PassInfo(Name, InterfaceID), 00233 ImplementationInfo(0), isDefaultImplementation(isDefault) { 00234 00235 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(InterfaceID)); 00236 if (InterfaceInfo == 0) { 00237 // First reference to Interface, register it now. 00238 registerPass(); 00239 InterfaceInfo = this; 00240 } 00241 assert(isAnalysisGroup() && 00242 "Trying to join an analysis group that is a normal pass!"); 00243 00244 if (PassID) { 00245 ImplementationInfo = Pass::lookupPassInfo(PassID); 00246 assert(ImplementationInfo && 00247 "Must register pass before adding to AnalysisGroup!"); 00248 00249 // Make sure we keep track of the fact that the implementation implements 00250 // the interface. 00251 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo); 00252 IIPI->addInterfaceImplemented(InterfaceInfo); 00253 00254 getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault); 00255 } 00256 } 00257 00258 00259 //===----------------------------------------------------------------------===// 00260 // PassRegistrationListener implementation 00261 // 00262 00263 // PassRegistrationListener ctor - Add the current object to the list of 00264 // PassRegistrationListeners... 00265 PassRegistrationListener::PassRegistrationListener() { 00266 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>(); 00267 Listeners->push_back(this); 00268 } 00269 00270 // dtor - Remove object from list of listeners... 00271 PassRegistrationListener::~PassRegistrationListener() { 00272 std::vector<PassRegistrationListener*>::iterator I = 00273 std::find(Listeners->begin(), Listeners->end(), this); 00274 assert(Listeners && I != Listeners->end() && 00275 "PassRegistrationListener not registered!"); 00276 Listeners->erase(I); 00277 00278 if (Listeners->empty()) { 00279 delete Listeners; 00280 Listeners = 0; 00281 } 00282 } 00283 00284 // enumeratePasses - Iterate over the registered passes, calling the 00285 // passEnumerate callback on each PassInfo object. 00286 // 00287 void PassRegistrationListener::enumeratePasses() { 00288 getPassRegistrar()->EnumerateWith(this); 00289 } 00290 00291 //===----------------------------------------------------------------------===// 00292 // AnalysisUsage Class Implementation 00293 // 00294 00295 namespace { 00296 struct GetCFGOnlyPasses : public PassRegistrationListener { 00297 typedef AnalysisUsage::VectorType VectorType; 00298 VectorType &CFGOnlyList; 00299 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {} 00300 00301 void passEnumerate(const PassInfo *P) { 00302 if (P->isCFGOnlyPass()) 00303 CFGOnlyList.push_back(P); 00304 } 00305 }; 00306 } 00307 00308 // setPreservesCFG - This function should be called to by the pass, iff they do 00309 // not: 00310 // 00311 // 1. Add or remove basic blocks from the function 00312 // 2. Modify terminator instructions in any way. 00313 // 00314 // This function annotates the AnalysisUsage info object to say that analyses 00315 // that only depend on the CFG are preserved by this pass. 00316 // 00317 void AnalysisUsage::setPreservesCFG() { 00318 // Since this transformation doesn't modify the CFG, it preserves all analyses 00319 // that only depend on the CFG (like dominators, loop info, etc...) 00320 GetCFGOnlyPasses(Preserved).enumeratePasses(); 00321 } 00322 00323