PassRegistry.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/Pass.h"
  16. #include "llvm/PassInfo.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. return PassInfoMap.lookup(TI);
  38. }
  39. const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
  40. sys::SmartScopedReader<true> Guard(Lock);
  41. return PassInfoStringMap.lookup(Arg);
  42. }
  43. //===----------------------------------------------------------------------===//
  44. // Pass Registration mechanism
  45. //
  46. void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
  47. sys::SmartScopedWriter<true> Guard(Lock);
  48. bool Inserted =
  49. PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
  50. assert(Inserted && "Pass registered multiple times!");
  51. (void)Inserted;
  52. PassInfoStringMap[PI.getPassArgument()] = &PI;
  53. // Notify any listeners.
  54. for (auto *Listener : Listeners)
  55. Listener->passRegistered(&PI);
  56. if (ShouldFree)
  57. ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
  58. }
  59. void PassRegistry::enumerateWith(PassRegistrationListener *L) {
  60. sys::SmartScopedReader<true> Guard(Lock);
  61. for (auto PassInfoPair : PassInfoMap)
  62. L->passEnumerate(PassInfoPair.second);
  63. }
  64. /// Analysis Group Mechanisms.
  65. void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
  66. const void *PassID,
  67. PassInfo &Registeree, bool isDefault,
  68. bool ShouldFree) {
  69. PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
  70. if (!InterfaceInfo) {
  71. // First reference to Interface, register it now.
  72. registerPass(Registeree);
  73. InterfaceInfo = &Registeree;
  74. }
  75. assert(Registeree.isAnalysisGroup() &&
  76. "Trying to join an analysis group that is a normal pass!");
  77. if (PassID) {
  78. PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
  79. assert(ImplementationInfo &&
  80. "Must register pass before adding to AnalysisGroup!");
  81. sys::SmartScopedWriter<true> Guard(Lock);
  82. // Make sure we keep track of the fact that the implementation implements
  83. // the interface.
  84. ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
  85. if (isDefault) {
  86. assert(InterfaceInfo->getNormalCtor() == nullptr &&
  87. "Default implementation for analysis group already specified!");
  88. assert(
  89. ImplementationInfo->getNormalCtor() &&
  90. "Cannot specify pass as default if it does not have a default ctor");
  91. InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
  92. }
  93. }
  94. if (ShouldFree)
  95. ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
  96. }
  97. void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
  98. sys::SmartScopedWriter<true> Guard(Lock);
  99. Listeners.push_back(L);
  100. }
  101. void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
  102. sys::SmartScopedWriter<true> Guard(Lock);
  103. auto I = llvm::find(Listeners, L);
  104. Listeners.erase(I);
  105. }