PassRegistry.cpp revision ee9886ed3dfac7b4459c9cb5bf8845b27e241792
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/System/Mutex.h"
17
18const PassInfo *PassRegistry::getPassInfo(intptr_t TI) const {
19  sys::SmartScopedLock<true> Guard(Lock);
20  MapType::const_iterator I = PassInfoMap.find(TI);
21  return I != PassInfoMap.end() ? I->second : 0;
22}
23
24const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
25  sys::SmartScopedLock<true> Guard(Lock);
26  StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
27  return I != PassInfoStringMap.end() ? I->second : 0;
28}
29
30void PassRegistry::registerPass(const PassInfo &PI) {
31  sys::SmartScopedLock<true> Guard(Lock);
32  bool Inserted =
33    PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
34  assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
35  PassInfoStringMap[PI.getPassArgument()] = &PI;
36}
37
38void PassRegistry::unregisterPass(const PassInfo &PI) {
39  sys::SmartScopedLock<true> Guard(Lock);
40  MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
41  assert(I != PassInfoMap.end() && "Pass registered but not in map!");
42
43  // Remove pass from the map.
44  PassInfoMap.erase(I);
45  PassInfoStringMap.erase(PI.getPassArgument());
46}
47
48void PassRegistry::enumerateWith(PassRegistrationListener *L) {
49  sys::SmartScopedLock<true> Guard(Lock);
50  for (MapType::const_iterator I = PassInfoMap.begin(),
51       E = PassInfoMap.end(); I != E; ++I)
52    L->passEnumerate(I->second);
53}
54
55
56/// Analysis Group Mechanisms.
57void PassRegistry::registerAnalysisGroup(PassInfo *InterfaceInfo,
58                                         const PassInfo *ImplementationInfo,
59                                         bool isDefault) {
60  sys::SmartScopedLock<true> Guard(Lock);
61  AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
62  assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
63         "Cannot add a pass to the same analysis group more than once!");
64  AGI.Implementations.insert(ImplementationInfo);
65  if (isDefault) {
66    assert(InterfaceInfo->getNormalCtor() == 0 &&
67           "Default implementation for analysis group already specified!");
68    assert(ImplementationInfo->getNormalCtor() &&
69         "Cannot specify pass as default if it does not have a default ctor");
70    InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
71  }
72}
73