LLVM API Documentation

PassRegistry.cpp
Go to the documentation of this file.
00001 //===- PassRegistry.cpp - Pass Registration 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 PassRegistry, with which passes are registered on
00011 // initialization, and supports the PassManager in dependency resolution.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/PassRegistry.h"
00016 #include "llvm/ADT/DenseMap.h"
00017 #include "llvm/ADT/SmallPtrSet.h"
00018 #include "llvm/ADT/StringMap.h"
00019 #include "llvm/IR/Function.h"
00020 #include "llvm/PassSupport.h"
00021 #include "llvm/Support/Compiler.h"
00022 #include "llvm/Support/ManagedStatic.h"
00023 #include "llvm/Support/Mutex.h"
00024 #include <vector>
00025 
00026 using namespace llvm;
00027 
00028 // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
00029 // Unfortunately, passes are registered with static ctors, and having
00030 // llvm_shutdown clear this map prevents successful resurrection after
00031 // llvm_shutdown is run.  Ideally we should find a solution so that we don't
00032 // leak the map, AND can still resurrect after shutdown.
00033 static ManagedStatic<PassRegistry> PassRegistryObj;
00034 PassRegistry *PassRegistry::getPassRegistry() {
00035   return &*PassRegistryObj;
00036 }
00037 
00038 static ManagedStatic<sys::SmartMutex<true> > Lock;
00039 
00040 //===----------------------------------------------------------------------===//
00041 // PassRegistryImpl
00042 //
00043 
00044 namespace {
00045 struct PassRegistryImpl {
00046   /// PassInfoMap - Keep track of the PassInfo object for each registered pass.
00047   typedef DenseMap<const void*, const PassInfo*> MapType;
00048   MapType PassInfoMap;
00049   
00050   typedef StringMap<const PassInfo*> StringMapType;
00051   StringMapType PassInfoStringMap;
00052   
00053   /// AnalysisGroupInfo - Keep track of information for each analysis group.
00054   struct AnalysisGroupInfo {
00055     SmallPtrSet<const PassInfo *, 8> Implementations;
00056   };
00057   DenseMap<const PassInfo*, AnalysisGroupInfo> AnalysisGroupInfoMap;
00058   
00059   std::vector<const PassInfo*> ToFree;
00060   std::vector<PassRegistrationListener*> Listeners;
00061 };
00062 } // end anonymous namespace
00063 
00064 void *PassRegistry::getImpl() const {
00065   if (!pImpl)
00066     pImpl = new PassRegistryImpl();
00067   return pImpl;
00068 }
00069 
00070 //===----------------------------------------------------------------------===//
00071 // Accessors
00072 //
00073 
00074 PassRegistry::~PassRegistry() {
00075   sys::SmartScopedLock<true> Guard(*Lock);
00076   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(pImpl);
00077   
00078   for (std::vector<const PassInfo*>::iterator I = Impl->ToFree.begin(),
00079        E = Impl->ToFree.end(); I != E; ++I)
00080     delete *I;
00081   
00082   delete Impl;
00083   pImpl = 0;
00084 }
00085 
00086 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
00087   sys::SmartScopedLock<true> Guard(*Lock);
00088   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
00089   PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.find(TI);
00090   return I != Impl->PassInfoMap.end() ? I->second : 0;
00091 }
00092 
00093 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
00094   sys::SmartScopedLock<true> Guard(*Lock);
00095   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
00096   PassRegistryImpl::StringMapType::const_iterator
00097     I = Impl->PassInfoStringMap.find(Arg);
00098   return I != Impl->PassInfoStringMap.end() ? I->second : 0;
00099 }
00100 
00101 //===----------------------------------------------------------------------===//
00102 // Pass Registration mechanism
00103 //
00104 
00105 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
00106   sys::SmartScopedLock<true> Guard(*Lock);
00107   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
00108   bool Inserted =
00109     Impl->PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
00110   assert(Inserted && "Pass registered multiple times!");
00111   (void)Inserted;
00112   Impl->PassInfoStringMap[PI.getPassArgument()] = &PI;
00113   
00114   // Notify any listeners.
00115   for (std::vector<PassRegistrationListener*>::iterator
00116        I = Impl->Listeners.begin(), E = Impl->Listeners.end(); I != E; ++I)
00117     (*I)->passRegistered(&PI);
00118   
00119   if (ShouldFree) Impl->ToFree.push_back(&PI);
00120 }
00121 
00122 void PassRegistry::unregisterPass(const PassInfo &PI) {
00123   sys::SmartScopedLock<true> Guard(*Lock);
00124   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
00125   PassRegistryImpl::MapType::iterator I = 
00126     Impl->PassInfoMap.find(PI.getTypeInfo());
00127   assert(I != Impl->PassInfoMap.end() && "Pass registered but not in map!");
00128   
00129   // Remove pass from the map.
00130   Impl->PassInfoMap.erase(I);
00131   Impl->PassInfoStringMap.erase(PI.getPassArgument());
00132 }
00133 
00134 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
00135   sys::SmartScopedLock<true> Guard(*Lock);
00136   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
00137   for (PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.begin(),
00138        E = Impl->PassInfoMap.end(); I != E; ++I)
00139     L->passEnumerate(I->second);
00140 }
00141 
00142 
00143 /// Analysis Group Mechanisms.
00144 void PassRegistry::registerAnalysisGroup(const void *InterfaceID, 
00145                                          const void *PassID,
00146                                          PassInfo& Registeree,
00147                                          bool isDefault,
00148                                          bool ShouldFree) {
00149   PassInfo *InterfaceInfo =  const_cast<PassInfo*>(getPassInfo(InterfaceID));
00150   if (InterfaceInfo == 0) {
00151     // First reference to Interface, register it now.
00152     registerPass(Registeree);
00153     InterfaceInfo = &Registeree;
00154   }
00155   assert(Registeree.isAnalysisGroup() && 
00156          "Trying to join an analysis group that is a normal pass!");
00157 
00158   if (PassID) {
00159     PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID));
00160     assert(ImplementationInfo &&
00161            "Must register pass before adding to AnalysisGroup!");
00162 
00163     sys::SmartScopedLock<true> Guard(*Lock);
00164     
00165     // Make sure we keep track of the fact that the implementation implements
00166     // the interface.
00167     ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
00168 
00169     PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
00170     PassRegistryImpl::AnalysisGroupInfo &AGI =
00171       Impl->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(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       InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
00181     }
00182   }
00183   
00184   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
00185   if (ShouldFree) Impl->ToFree.push_back(&Registeree);
00186 }
00187 
00188 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
00189   sys::SmartScopedLock<true> Guard(*Lock);
00190   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
00191   Impl->Listeners.push_back(L);
00192 }
00193 
00194 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
00195   sys::SmartScopedLock<true> Guard(*Lock);
00196   
00197   // NOTE: This is necessary, because removeRegistrationListener() can be called
00198   // as part of the llvm_shutdown sequence.  Since we have no control over the
00199   // order of that sequence, we need to gracefully handle the case where the
00200   // PassRegistry is destructed before the object that triggers this call.
00201   if (!pImpl) return;
00202   
00203   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
00204   std::vector<PassRegistrationListener*>::iterator I =
00205     std::find(Impl->Listeners.begin(), Impl->Listeners.end(), L);
00206   assert(I != Impl->Listeners.end() &&
00207          "PassRegistrationListener not registered!");
00208   Impl->Listeners.erase(I);
00209 }