1 //===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the PassRegistry, with which passes are registered on
11 // initialization, and supports the PassManager in dependency resolution.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/PassRegistry.h"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/ManagedStatic.h"
19 static PassRegistry *PassRegistryObj = 0;
20 PassRegistry *PassRegistry::getPassRegistry() {
21 // Use double-checked locking to safely initialize the registrar when
22 // we're running in multithreaded mode.
23 PassRegistry* tmp = PassRegistryObj;
24 if (llvm_is_multithreaded()) {
27 llvm_acquire_global_lock();
28 tmp = PassRegistryObj;
30 tmp = new PassRegistry();
32 PassRegistryObj = tmp;
34 llvm_release_global_lock();
37 PassRegistryObj = new PassRegistry();
40 return PassRegistryObj;
45 // FIXME: We use ManagedCleanup to erase the pass registrar on shutdown.
46 // Unfortunately, passes are registered with static ctors, and having
47 // llvm_shutdown clear this map prevents successful ressurection after
48 // llvm_shutdown is run. Ideally we should find a solution so that we don't
49 // leak the map, AND can still resurrect after shutdown.
50 void cleanupPassRegistry(void*) {
51 if (PassRegistryObj) {
52 delete PassRegistryObj;
56 ManagedCleanup<&cleanupPassRegistry> registryCleanup ATTRIBUTE_USED;
60 const PassInfo *PassRegistry::getPassInfo(intptr_t TI) const {
61 sys::SmartScopedLock<true> Guard(Lock);
62 MapType::const_iterator I = PassInfoMap.find(TI);
63 return I != PassInfoMap.end() ? I->second : 0;
66 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
67 sys::SmartScopedLock<true> Guard(Lock);
68 StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
69 return I != PassInfoStringMap.end() ? I->second : 0;
72 void PassRegistry::registerPass(const PassInfo &PI) {
73 sys::SmartScopedLock<true> Guard(Lock);
75 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
76 assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
77 PassInfoStringMap[PI.getPassArgument()] = &PI;
80 void PassRegistry::unregisterPass(const PassInfo &PI) {
81 sys::SmartScopedLock<true> Guard(Lock);
82 MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
83 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
85 // Remove pass from the map.
87 PassInfoStringMap.erase(PI.getPassArgument());
90 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
91 sys::SmartScopedLock<true> Guard(Lock);
92 for (MapType::const_iterator I = PassInfoMap.begin(),
93 E = PassInfoMap.end(); I != E; ++I)
94 L->passEnumerate(I->second);
98 /// Analysis Group Mechanisms.
99 void PassRegistry::registerAnalysisGroup(PassInfo *InterfaceInfo,
100 const PassInfo *ImplementationInfo,
102 sys::SmartScopedLock<true> Guard(Lock);
103 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
104 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
105 "Cannot add a pass to the same analysis group more than once!");
106 AGI.Implementations.insert(ImplementationInfo);
108 assert(InterfaceInfo->getNormalCtor() == 0 &&
109 "Default implementation for analysis group already specified!");
110 assert(ImplementationInfo->getNormalCtor() &&
111 "Cannot specify pass as default if it does not have a default ctor");
112 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());