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