//
// The LLVM Compiler Infrastructure
//
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//
//===----------------------------------------------------------------------===//
+#include "llvm/Pass.h"
#include "llvm/PassManager.h"
#include "llvm/Module.h"
#include "llvm/ModuleProvider.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/ManagedStatic.h"
-#include "llvm/Support/TypeInfo.h"
+#include "llvm/System/Atomic.h"
+#include "llvm/System/Mutex.h"
+#include "llvm/System/Threading.h"
+#include <algorithm>
+#include <map>
#include <set>
using namespace llvm;
//===----------------------------------------------------------------------===//
-// AnalysisResolver Class Implementation
+// Pass Implementation
//
-AnalysisResolver::~AnalysisResolver() {
+// Force out-of-line virtual method.
+Pass::~Pass() {
+ delete Resolver;
}
-//===----------------------------------------------------------------------===//
-// Pass Implementation
-//
-
// Force out-of-line virtual method.
ModulePass::~ModulePass() { }
bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
- return Resolver_New->getAnalysisToUpdate(AnalysisID, true) != 0;
+ return Resolver->getAnalysisIfAvailable(AnalysisID, true) != 0;
}
// dumpPassStructure - Implement the -debug-passes=Structure option
cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
}
-// getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
-//
+/// getPassName - Return a nice clean name for a pass. This usually
+/// implemented in terms of the name that is registered by one of the
+/// Registration templates, but can be overloaded directly.
+///
const char *Pass::getPassName() const {
if (const PassInfo *PI = getPassInfo())
return PI->getPassName();
- return typeid(*this).name();
+ return "Unnamed pass: implement Pass::getPassName()";
}
// print - Print out the internal state of the pass. This is called by Analyze
bool Changed = doInitialization(M);
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
- if (!I->isExternal()) // Passes are not run on external functions!
+ if (!I->isDeclaration()) // Passes are not run on external functions!
Changed |= runOnFunction(*I);
return Changed | doFinalization(M);
// run - On a function, we simply initialize, run the function, then finalize.
//
bool FunctionPass::run(Function &F) {
- if (F.isExternal()) return false;// Passes are not run on external functions!
+ // Passes are not run on external functions!
+ if (F.isDeclaration()) return false;
bool Changed = doInitialization(*F.getParent());
Changed |= runOnFunction(F);
return Changed | doFinalization(F);
}
-// To run directly on the basic block, we initialize, runOnBasicBlock, then
-// finalize.
-//
-bool BasicBlockPass::runPass(BasicBlock &BB) {
- Function &F = *BB.getParent();
- Module &M = *F.getParent();
- bool Changed = doInitialization(M);
- Changed |= doInitialization(F);
- Changed |= runOnBasicBlock(BB);
- Changed |= doFinalization(F);
- Changed |= doFinalization(M);
- return Changed;
-}
-
//===----------------------------------------------------------------------===//
// Pass Registration mechanism
//
class PassRegistrar {
/// PassInfoMap - Keep track of the passinfo object for each registered llvm
/// pass.
- std::map<TypeInfo, PassInfo*> PassInfoMap;
+ typedef std::map<intptr_t, const PassInfo*> MapType;
+ MapType PassInfoMap;
/// AnalysisGroupInfo - Keep track of information for each analysis group.
struct AnalysisGroupInfo {
public:
- const PassInfo *GetPassInfo(const std::type_info &TI) const {
- std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.find(TI);
+ const PassInfo *GetPassInfo(intptr_t TI) const {
+ MapType::const_iterator I = PassInfoMap.find(TI);
return I != PassInfoMap.end() ? I->second : 0;
}
- void RegisterPass(PassInfo &PI) {
+ void RegisterPass(const PassInfo &PI) {
bool Inserted =
- PassInfoMap.insert(std::make_pair(TypeInfo(PI.getTypeInfo()),&PI)).second;
- assert(Inserted && "Pass registered multiple times!");
+ PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
+ assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
}
- void UnregisterPass(PassInfo &PI) {
- std::map<TypeInfo, PassInfo*>::iterator I =
- PassInfoMap.find(PI.getTypeInfo());
+ void UnregisterPass(const PassInfo &PI) {
+ MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
assert(I != PassInfoMap.end() && "Pass registered but not in map!");
// Remove pass from the map.
}
void EnumerateWith(PassRegistrationListener *L) {
- for (std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.begin(),
+ for (MapType::const_iterator I = PassInfoMap.begin(),
E = PassInfoMap.end(); I != E; ++I)
L->passEnumerate(I->second);
}
};
}
-static ManagedStatic<PassRegistrar> PassRegistrarObj;
static std::vector<PassRegistrationListener*> *Listeners = 0;
+static sys::SmartMutex<true> ListenersLock;
+
+// FIXME: This should use ManagedStatic to manage the pass registrar.
+// Unfortunately, we can't do this, because passes are registered with static
+// ctors, and having llvm_shutdown clear this map prevents successful
+// ressurection after llvm_shutdown is run.
+static PassRegistrar *getPassRegistrar() {
+ static PassRegistrar *PassRegistrarObj = 0;
+
+ // Use double-checked locking to safely initialize the registrar when
+ // we're running in multithreaded mode.
+ PassRegistrar* tmp = PassRegistrarObj;
+ if (llvm_is_multithreaded()) {
+ sys::MemoryFence();
+ if (!tmp) {
+ llvm_acquire_global_lock();
+ tmp = PassRegistrarObj;
+ if (!tmp) {
+ tmp = new PassRegistrar();
+ sys::MemoryFence();
+ PassRegistrarObj = tmp;
+ }
+ llvm_release_global_lock();
+ }
+ } else if (!tmp) {
+ PassRegistrarObj = new PassRegistrar();
+ }
+
+ return PassRegistrarObj;
+}
// getPassInfo - Return the PassInfo data structure that corresponds to this
// pass...
const PassInfo *Pass::getPassInfo() const {
- if (PassInfoCache) return PassInfoCache;
- return lookupPassInfo(typeid(*this));
+ return lookupPassInfo(PassID);
}
-const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
- return PassRegistrarObj->GetPassInfo(TI);
+const PassInfo *Pass::lookupPassInfo(intptr_t TI) {
+ return getPassRegistrar()->GetPassInfo(TI);
}
-void RegisterPassBase::registerPass() {
- PassRegistrarObj->RegisterPass(PIObj);
+void PassInfo::registerPass() {
+ getPassRegistrar()->RegisterPass(*this);
// Notify any listeners.
+ sys::SmartScopedLock<true> Lock(ListenersLock);
if (Listeners)
for (std::vector<PassRegistrationListener*>::iterator
I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
- (*I)->passRegistered(&PIObj);
+ (*I)->passRegistered(this);
}
-void RegisterPassBase::unregisterPass() {
- PassRegistrarObj->UnregisterPass(PIObj);
+void PassInfo::unregisterPass() {
+ getPassRegistrar()->UnregisterPass(*this);
}
//===----------------------------------------------------------------------===//
// RegisterAGBase implementation
//
-RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
- const std::type_info *Pass, bool isDefault)
- : RegisterPassBase(Interface),
+RegisterAGBase::RegisterAGBase(const char *Name, intptr_t InterfaceID,
+ intptr_t PassID, bool isDefault)
+ : PassInfo(Name, InterfaceID),
ImplementationInfo(0), isDefaultImplementation(isDefault) {
- InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
+ InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(InterfaceID));
if (InterfaceInfo == 0) {
// First reference to Interface, register it now.
registerPass();
- InterfaceInfo = &PIObj;
+ InterfaceInfo = this;
}
- assert(PIObj.isAnalysisGroup() &&
+ assert(isAnalysisGroup() &&
"Trying to join an analysis group that is a normal pass!");
- if (Pass) {
- ImplementationInfo = Pass::lookupPassInfo(*Pass);
+ if (PassID) {
+ ImplementationInfo = Pass::lookupPassInfo(PassID);
assert(ImplementationInfo &&
"Must register pass before adding to AnalysisGroup!");
PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
IIPI->addInterfaceImplemented(InterfaceInfo);
- PassRegistrarObj->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
+ getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
}
}
-void RegisterAGBase::setGroupName(const char *Name) {
- assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
- InterfaceInfo->setPassName(Name);
-}
-
//===----------------------------------------------------------------------===//
// PassRegistrationListener implementation
// PassRegistrationListener ctor - Add the current object to the list of
// PassRegistrationListeners...
PassRegistrationListener::PassRegistrationListener() {
+ sys::SmartScopedLock<true> Lock(ListenersLock);
if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
Listeners->push_back(this);
}
// dtor - Remove object from list of listeners...
PassRegistrationListener::~PassRegistrationListener() {
+ sys::SmartScopedLock<true> Lock(ListenersLock);
std::vector<PassRegistrationListener*>::iterator I =
std::find(Listeners->begin(), Listeners->end(), this);
assert(Listeners && I != Listeners->end() &&
// passEnumerate callback on each PassInfo object.
//
void PassRegistrationListener::enumeratePasses() {
- PassRegistrarObj->EnumerateWith(this);
+ getPassRegistrar()->EnumerateWith(this);
}
//===----------------------------------------------------------------------===//
namespace {
struct GetCFGOnlyPasses : public PassRegistrationListener {
- std::vector<AnalysisID> &CFGOnlyList;
- GetCFGOnlyPasses(std::vector<AnalysisID> &L) : CFGOnlyList(L) {}
+ typedef AnalysisUsage::VectorType VectorType;
+ VectorType &CFGOnlyList;
+ GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
void passEnumerate(const PassInfo *P) {
if (P->isCFGOnlyPass())