PassRegistry.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the PassRegistry, with which passes are registered on
  10. // initialization, and supports the PassManager in dependency resolution.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/PassRegistry.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/PassInfo.h"
  16. #include "llvm/PassSupport.h"
  17. #include "llvm/Support/ManagedStatic.h"
  18. #include <cassert>
  19. #include <memory>
  20. #include <utility>
  21. using namespace llvm;
  22. // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
  23. // Unfortunately, passes are registered with static ctors, and having
  24. // llvm_shutdown clear this map prevents successful resurrection after
  25. // llvm_shutdown is run. Ideally we should find a solution so that we don't
  26. // leak the map, AND can still resurrect after shutdown.
  27. static ManagedStatic<PassRegistry> PassRegistryObj;
  28. PassRegistry *PassRegistry::getPassRegistry() {
  29. return &*PassRegistryObj;
  30. }
  31. //===----------------------------------------------------------------------===//
  32. // Accessors
  33. //
  34. PassRegistry::~PassRegistry() = default;
  35. const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
  36. sys::SmartScopedReader<true> Guard(Lock);
  37. MapType::const_iterator I = PassInfoMap.find(TI);
  38. return I != PassInfoMap.end() ? I->second : nullptr;
  39. }
  40. const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
  41. sys::SmartScopedReader<true> Guard(Lock);
  42. StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
  43. return I != PassInfoStringMap.end() ? I->second : nullptr;
  44. }
  45. //===----------------------------------------------------------------------===//
  46. // Pass Registration mechanism
  47. //
  48. void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
  49. sys::SmartScopedWriter<true> Guard(Lock);
  50. bool Inserted =
  51. PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
  52. assert(Inserted && "Pass registered multiple times!");
  53. (void)Inserted;
  54. PassInfoStringMap[PI.getPassArgument()] = &PI;
  55. // Notify any listeners.
  56. for (auto *Listener : Listeners)
  57. Listener->passRegistered(&PI);
  58. if (ShouldFree)
  59. ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
  60. }
  61. void PassRegistry::enumerateWith(PassRegistrationListener *L) {
  62. sys::SmartScopedReader<true> Guard(Lock);
  63. for (auto PassInfoPair : PassInfoMap)
  64. L->passEnumerate(PassInfoPair.second);
  65. }
  66. /// Analysis Group Mechanisms.
  67. void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
  68. const void *PassID,
  69. PassInfo &Registeree, bool isDefault,
  70. bool ShouldFree) {
  71. PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
  72. if (!InterfaceInfo) {
  73. // First reference to Interface, register it now.
  74. registerPass(Registeree);
  75. InterfaceInfo = &Registeree;
  76. }
  77. assert(Registeree.isAnalysisGroup() &&
  78. "Trying to join an analysis group that is a normal pass!");
  79. if (PassID) {
  80. PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
  81. assert(ImplementationInfo &&
  82. "Must register pass before adding to AnalysisGroup!");
  83. sys::SmartScopedWriter<true> Guard(Lock);
  84. // Make sure we keep track of the fact that the implementation implements
  85. // the interface.
  86. ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
  87. if (isDefault) {
  88. assert(InterfaceInfo->getNormalCtor() == nullptr &&
  89. "Default implementation for analysis group already specified!");
  90. assert(
  91. ImplementationInfo->getNormalCtor() &&
  92. "Cannot specify pass as default if it does not have a default ctor");
  93. InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
  94. }
  95. }
  96. if (ShouldFree)
  97. ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
  98. }
  99. void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
  100. sys::SmartScopedWriter<true> Guard(Lock);
  101. Listeners.push_back(L);
  102. }
  103. void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
  104. sys::SmartScopedWriter<true> Guard(Lock);
  105. auto I = llvm::find(Listeners, L);
  106. Listeners.erase(I);
  107. }