Add a lock() function in PassRegistry to speed up multi-thread synchronization.
[oota-llvm.git] / lib / IR / PassRegistry.cpp
1 //===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the PassRegistry, with which passes are registered on
11 // initialization, and supports the PassManager in dependency resolution.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/PassRegistry.h"
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/PassSupport.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "llvm/Support/RWMutex.h"
22 #include <vector>
23
24 using namespace llvm;
25
26 // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
27 // Unfortunately, passes are registered with static ctors, and having
28 // llvm_shutdown clear this map prevents successful resurrection after
29 // llvm_shutdown is run.  Ideally we should find a solution so that we don't
30 // leak the map, AND can still resurrect after shutdown.
31 static ManagedStatic<PassRegistry> PassRegistryObj;
32 PassRegistry *PassRegistry::getPassRegistry() {
33   return &*PassRegistryObj;
34 }
35
36 //===----------------------------------------------------------------------===//
37 // Accessors
38 //
39
40 PassRegistry::~PassRegistry() {}
41
42 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
43   // We don't need thread synchronization after the PassRegistry is locked
44   // (that means: is read-only).
45   Optional<sys::SmartScopedReader<true>> Guard;
46   if (!locked)
47     Guard.emplace(Lock);
48
49   MapType::const_iterator I = PassInfoMap.find(TI);
50   return I != PassInfoMap.end() ? I->second : nullptr;
51 }
52
53 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
54   // We don't need thread synchronization after the PassRegistry is locked
55   // (that means: is read-only).
56   Optional<sys::SmartScopedReader<true>> Guard;
57   if (!locked)
58     Guard.emplace(Lock);
59
60   StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
61   return I != PassInfoStringMap.end() ? I->second : nullptr;
62 }
63
64 //===----------------------------------------------------------------------===//
65 // Pass Registration mechanism
66 //
67
68 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
69
70   assert(!locked && "Trying to register a pass in a locked PassRegistry");
71
72   sys::SmartScopedWriter<true> Guard(Lock);
73   bool Inserted =
74       PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
75   assert(Inserted && "Pass registered multiple times!");
76   (void)Inserted;
77   PassInfoStringMap[PI.getPassArgument()] = &PI;
78
79   // Notify any listeners.
80   for (auto *Listener : Listeners)
81     Listener->passRegistered(&PI);
82
83   if (ShouldFree)
84     ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
85
86   assert(!locked && "PassRegistry locked during registering a pass");
87 }
88
89 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
90   sys::SmartScopedReader<true> Guard(Lock);
91   for (auto PassInfoPair : PassInfoMap)
92     L->passEnumerate(PassInfoPair.second);
93 }
94
95 /// Analysis Group Mechanisms.
96 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
97                                          const void *PassID,
98                                          PassInfo &Registeree, bool isDefault,
99                                          bool ShouldFree) {
100   PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
101   if (!InterfaceInfo) {
102     // First reference to Interface, register it now.
103     registerPass(Registeree);
104     InterfaceInfo = &Registeree;
105   }
106   assert(Registeree.isAnalysisGroup() &&
107          "Trying to join an analysis group that is a normal pass!");
108
109   if (PassID) {
110     PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
111     assert(ImplementationInfo &&
112            "Must register pass before adding to AnalysisGroup!");
113
114     sys::SmartScopedWriter<true> Guard(Lock);
115
116     // Make sure we keep track of the fact that the implementation implements
117     // the interface.
118     ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
119
120     if (isDefault) {
121       assert(InterfaceInfo->getNormalCtor() == nullptr &&
122              "Default implementation for analysis group already specified!");
123       assert(
124           ImplementationInfo->getNormalCtor() &&
125           "Cannot specify pass as default if it does not have a default ctor");
126       InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
127       InterfaceInfo->setTargetMachineCtor(
128           ImplementationInfo->getTargetMachineCtor());
129     }
130   }
131
132   if (ShouldFree)
133     ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
134 }
135
136 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
137   sys::SmartScopedWriter<true> Guard(Lock);
138   Listeners.push_back(L);
139 }
140
141 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
142   sys::SmartScopedWriter<true> Guard(Lock);
143
144   auto I = std::find(Listeners.begin(), Listeners.end(), L);
145   Listeners.erase(I);
146 }