PassRegistry.cpp revision dce4a407a24b04eebc6a376f8e62b41aaa7b071f
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/DenseMap.h"
17#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/StringMap.h"
19#include "llvm/IR/Function.h"
20#include "llvm/PassSupport.h"
21#include "llvm/Support/Compiler.h"
22#include "llvm/Support/ManagedStatic.h"
23#include "llvm/Support/Mutex.h"
24#include "llvm/Support/RWMutex.h"
25#include <vector>
26
27using namespace llvm;
28
29// FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
30// Unfortunately, passes are registered with static ctors, and having
31// llvm_shutdown clear this map prevents successful resurrection after
32// llvm_shutdown is run.  Ideally we should find a solution so that we don't
33// leak the map, AND can still resurrect after shutdown.
34static ManagedStatic<PassRegistry> PassRegistryObj;
35PassRegistry *PassRegistry::getPassRegistry() {
36  return &*PassRegistryObj;
37}
38
39static ManagedStatic<sys::SmartRWMutex<true> > Lock;
40
41//===----------------------------------------------------------------------===//
42// PassRegistryImpl
43//
44
45namespace {
46struct PassRegistryImpl {
47  /// PassInfoMap - Keep track of the PassInfo object for each registered pass.
48  typedef DenseMap<const void*, const PassInfo*> MapType;
49  MapType PassInfoMap;
50
51  typedef StringMap<const PassInfo*> StringMapType;
52  StringMapType PassInfoStringMap;
53
54  /// AnalysisGroupInfo - Keep track of information for each analysis group.
55  struct AnalysisGroupInfo {
56    SmallPtrSet<const PassInfo *, 8> Implementations;
57  };
58  DenseMap<const PassInfo*, AnalysisGroupInfo> AnalysisGroupInfoMap;
59
60  std::vector<std::unique_ptr<const PassInfo>> ToFree;
61  std::vector<PassRegistrationListener*> Listeners;
62};
63} // end anonymous namespace
64
65void *PassRegistry::getImpl() const {
66  if (!pImpl)
67    pImpl = new PassRegistryImpl();
68  return pImpl;
69}
70
71//===----------------------------------------------------------------------===//
72// Accessors
73//
74
75PassRegistry::~PassRegistry() {
76  sys::SmartScopedWriter<true> Guard(*Lock);
77  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(pImpl);
78  delete Impl;
79  pImpl = nullptr;
80}
81
82const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
83  sys::SmartScopedReader<true> Guard(*Lock);
84  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
85  PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.find(TI);
86  return I != Impl->PassInfoMap.end() ? I->second : nullptr;
87}
88
89const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
90  sys::SmartScopedReader<true> Guard(*Lock);
91  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
92  PassRegistryImpl::StringMapType::const_iterator
93    I = Impl->PassInfoStringMap.find(Arg);
94  return I != Impl->PassInfoStringMap.end() ? I->second : nullptr;
95}
96
97//===----------------------------------------------------------------------===//
98// Pass Registration mechanism
99//
100
101void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
102  sys::SmartScopedWriter<true> Guard(*Lock);
103  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
104  bool Inserted =
105    Impl->PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
106  assert(Inserted && "Pass registered multiple times!");
107  (void)Inserted;
108  Impl->PassInfoStringMap[PI.getPassArgument()] = &PI;
109
110  // Notify any listeners.
111  for (std::vector<PassRegistrationListener*>::iterator
112       I = Impl->Listeners.begin(), E = Impl->Listeners.end(); I != E; ++I)
113    (*I)->passRegistered(&PI);
114
115  if (ShouldFree) Impl->ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
116}
117
118void PassRegistry::unregisterPass(const PassInfo &PI) {
119  sys::SmartScopedWriter<true> Guard(*Lock);
120  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
121  PassRegistryImpl::MapType::iterator I =
122    Impl->PassInfoMap.find(PI.getTypeInfo());
123  assert(I != Impl->PassInfoMap.end() && "Pass registered but not in map!");
124
125  // Remove pass from the map.
126  Impl->PassInfoMap.erase(I);
127  Impl->PassInfoStringMap.erase(PI.getPassArgument());
128}
129
130void PassRegistry::enumerateWith(PassRegistrationListener *L) {
131  sys::SmartScopedReader<true> Guard(*Lock);
132  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
133  for (PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.begin(),
134       E = Impl->PassInfoMap.end(); I != E; ++I)
135    L->passEnumerate(I->second);
136}
137
138
139/// Analysis Group Mechanisms.
140void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
141                                         const void *PassID,
142                                         PassInfo& Registeree,
143                                         bool isDefault,
144                                         bool ShouldFree) {
145  PassInfo *InterfaceInfo =  const_cast<PassInfo*>(getPassInfo(InterfaceID));
146  if (!InterfaceInfo) {
147    // First reference to Interface, register it now.
148    registerPass(Registeree);
149    InterfaceInfo = &Registeree;
150  }
151  assert(Registeree.isAnalysisGroup() &&
152         "Trying to join an analysis group that is a normal pass!");
153
154  if (PassID) {
155    PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID));
156    assert(ImplementationInfo &&
157           "Must register pass before adding to AnalysisGroup!");
158
159    sys::SmartScopedWriter<true> Guard(*Lock);
160
161    // Make sure we keep track of the fact that the implementation implements
162    // the interface.
163    ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
164
165    PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
166    PassRegistryImpl::AnalysisGroupInfo &AGI =
167      Impl->AnalysisGroupInfoMap[InterfaceInfo];
168    assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
169           "Cannot add a pass to the same analysis group more than once!");
170    AGI.Implementations.insert(ImplementationInfo);
171    if (isDefault) {
172      assert(InterfaceInfo->getNormalCtor() == nullptr &&
173             "Default implementation for analysis group already specified!");
174      assert(ImplementationInfo->getNormalCtor() &&
175           "Cannot specify pass as default if it does not have a default ctor");
176      InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
177      InterfaceInfo->setTargetMachineCtor(
178          ImplementationInfo->getTargetMachineCtor());
179    }
180  }
181
182  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
183  if (ShouldFree)
184    Impl->ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
185}
186
187void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
188  sys::SmartScopedWriter<true> Guard(*Lock);
189  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
190  Impl->Listeners.push_back(L);
191}
192
193void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
194  sys::SmartScopedWriter<true> Guard(*Lock);
195
196  // NOTE: This is necessary, because removeRegistrationListener() can be called
197  // as part of the llvm_shutdown sequence.  Since we have no control over the
198  // order of that sequence, we need to gracefully handle the case where the
199  // PassRegistry is destructed before the object that triggers this call.
200  if (!pImpl) return;
201
202  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
203  std::vector<PassRegistrationListener*>::iterator I =
204    std::find(Impl->Listeners.begin(), Impl->Listeners.end(), L);
205  assert(I != Impl->Listeners.end() &&
206         "PassRegistrationListener not registered!");
207  Impl->Listeners.erase(I);
208}
209