[cleanup] Switch to using range-based for loops in two very obvious
[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/IR/Function.h"
17 #include "llvm/PassSupport.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/RWMutex.h"
21 #include <vector>
22
23 using namespace llvm;
24
25 // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
26 // Unfortunately, passes are registered with static ctors, and having
27 // llvm_shutdown clear this map prevents successful resurrection after
28 // llvm_shutdown is run.  Ideally we should find a solution so that we don't
29 // leak the map, AND can still resurrect after shutdown.
30 static ManagedStatic<PassRegistry> PassRegistryObj;
31 PassRegistry *PassRegistry::getPassRegistry() {
32   return &*PassRegistryObj;
33 }
34
35 //===----------------------------------------------------------------------===//
36 // Accessors
37 //
38
39 PassRegistry::~PassRegistry() {}
40
41 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
42   sys::SmartScopedReader<true> Guard(Lock);
43   MapType::const_iterator I = PassInfoMap.find(TI);
44   return I != PassInfoMap.end() ? I->second : nullptr;
45 }
46
47 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
48   sys::SmartScopedReader<true> Guard(Lock);
49   StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
50   return I != PassInfoStringMap.end() ? I->second : nullptr;
51 }
52
53 //===----------------------------------------------------------------------===//
54 // Pass Registration mechanism
55 //
56
57 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
58   sys::SmartScopedWriter<true> Guard(Lock);
59   bool Inserted =
60       PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
61   assert(Inserted && "Pass registered multiple times!");
62   (void)Inserted;
63   PassInfoStringMap[PI.getPassArgument()] = &PI;
64
65   // Notify any listeners.
66   for (auto *Listener : Listeners)
67     Listener->passRegistered(&PI);
68
69   if (ShouldFree)
70     ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
71 }
72
73 void PassRegistry::unregisterPass(const PassInfo &PI) {
74   sys::SmartScopedWriter<true> Guard(Lock);
75   MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
76   assert(I != PassInfoMap.end() && "Pass registered but not in map!");
77
78   // Remove pass from the map.
79   PassInfoMap.erase(I);
80   PassInfoStringMap.erase(PI.getPassArgument());
81 }
82
83 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
84   sys::SmartScopedReader<true> Guard(Lock);
85   for (auto PassInfoPair : PassInfoMap)
86     L->passEnumerate(PassInfoPair.second);
87 }
88
89 /// Analysis Group Mechanisms.
90 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
91                                          const void *PassID,
92                                          PassInfo &Registeree, bool isDefault,
93                                          bool ShouldFree) {
94   PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
95   if (!InterfaceInfo) {
96     // First reference to Interface, register it now.
97     registerPass(Registeree);
98     InterfaceInfo = &Registeree;
99   }
100   assert(Registeree.isAnalysisGroup() &&
101          "Trying to join an analysis group that is a normal pass!");
102
103   if (PassID) {
104     PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
105     assert(ImplementationInfo &&
106            "Must register pass before adding to AnalysisGroup!");
107
108     sys::SmartScopedWriter<true> Guard(Lock);
109
110     // Make sure we keep track of the fact that the implementation implements
111     // the interface.
112     ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
113
114     AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
115     assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
116            "Cannot add a pass to the same analysis group more than once!");
117     AGI.Implementations.insert(ImplementationInfo);
118     if (isDefault) {
119       assert(InterfaceInfo->getNormalCtor() == nullptr &&
120              "Default implementation for analysis group already specified!");
121       assert(
122           ImplementationInfo->getNormalCtor() &&
123           "Cannot specify pass as default if it does not have a default ctor");
124       InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
125       InterfaceInfo->setTargetMachineCtor(
126           ImplementationInfo->getTargetMachineCtor());
127     }
128   }
129
130   if (ShouldFree)
131     ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
132 }
133
134 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
135   sys::SmartScopedWriter<true> Guard(Lock);
136   Listeners.push_back(L);
137 }
138
139 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
140   sys::SmartScopedWriter<true> Guard(Lock);
141
142   auto I = std::find(Listeners.begin(), Listeners.end(), L);
143   Listeners.erase(I);
144 }