PassRegistry.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //===- llvm/PassRegistry.h - Pass Information Registry ----------*- C++ -*-===//
  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 defines PassRegistry, a class that is used in the initialization
  10. // and registration of passes. At application startup, passes are registered
  11. // with the PassRegistry, which is later provided to the PassManager for
  12. // dependency resolution and similar tasks.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_PASSREGISTRY_H
  16. #define LLVM_PASSREGISTRY_H
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/ADT/StringMap.h"
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/Support/CBindingWrapping.h"
  21. #include "llvm/Support/RWMutex.h"
  22. #include <memory>
  23. #include <vector>
  24. namespace llvm {
  25. class PassInfo;
  26. struct PassRegistrationListener;
  27. /// PassRegistry - This class manages the registration and intitialization of
  28. /// the pass subsystem as application startup, and assists the PassManager
  29. /// in resolving pass dependencies.
  30. /// NOTE: PassRegistry is NOT thread-safe. If you want to use LLVM on multiple
  31. /// threads simultaneously, you will need to use a separate PassRegistry on
  32. /// each thread.
  33. class PassRegistry {
  34. mutable sys::SmartRWMutex<true> Lock;
  35. /// PassInfoMap - Keep track of the PassInfo object for each registered pass.
  36. using MapType = DenseMap<const void *, const PassInfo *>;
  37. MapType PassInfoMap;
  38. using StringMapType = StringMap<const PassInfo *>;
  39. StringMapType PassInfoStringMap;
  40. std::vector<std::unique_ptr<const PassInfo>> ToFree;
  41. std::vector<PassRegistrationListener *> Listeners;
  42. public:
  43. PassRegistry() = default;
  44. ~PassRegistry();
  45. /// getPassRegistry - Access the global registry object, which is
  46. /// automatically initialized at application launch and destroyed by
  47. /// llvm_shutdown.
  48. static PassRegistry *getPassRegistry();
  49. /// getPassInfo - Look up a pass' corresponding PassInfo, indexed by the pass'
  50. /// type identifier (&MyPass::ID).
  51. const PassInfo *getPassInfo(const void *TI) const;
  52. /// getPassInfo - Look up a pass' corresponding PassInfo, indexed by the pass'
  53. /// argument string.
  54. const PassInfo *getPassInfo(StringRef Arg) const;
  55. /// registerPass - Register a pass (by means of its PassInfo) with the
  56. /// registry. Required in order to use the pass with a PassManager.
  57. void registerPass(const PassInfo &PI, bool ShouldFree = false);
  58. /// registerAnalysisGroup - Register an analysis group (or a pass implementing
  59. // an analysis group) with the registry. Like registerPass, this is required
  60. // in order for a PassManager to be able to use this group/pass.
  61. void registerAnalysisGroup(const void *InterfaceID, const void *PassID,
  62. PassInfo &Registeree, bool isDefault,
  63. bool ShouldFree = false);
  64. /// enumerateWith - Enumerate the registered passes, calling the provided
  65. /// PassRegistrationListener's passEnumerate() callback on each of them.
  66. void enumerateWith(PassRegistrationListener *L);
  67. /// addRegistrationListener - Register the given PassRegistrationListener
  68. /// to receive passRegistered() callbacks whenever a new pass is registered.
  69. void addRegistrationListener(PassRegistrationListener *L);
  70. /// removeRegistrationListener - Unregister a PassRegistrationListener so that
  71. /// it no longer receives passRegistered() callbacks.
  72. void removeRegistrationListener(PassRegistrationListener *L);
  73. };
  74. // Create wrappers for C Binding types (see CBindingWrapping.h).
  75. DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassRegistry, LLVMPassRegistryRef)
  76. } // end namespace llvm
  77. #endif // LLVM_PASSREGISTRY_H