PassRegistry.cpp 3.9 KB

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