MachinePassRegistry.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/CodeGen/MachinePassRegistry.h -----------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains the mechanics for machine function pass registries. A
  15. // function pass registry (MachinePassRegistry) is auto filled by the static
  16. // constructors of MachinePassRegistryNode. Further there is a command line
  17. // parser (RegisterPassParser) which listens to each registry for additions
  18. // and deletions, so that the appropriate command option is updated.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_CODEGEN_MACHINEPASSREGISTRY_H
  22. #define LLVM_CODEGEN_MACHINEPASSREGISTRY_H
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/CodeGen/Passes.h"
  25. #include "llvm/Support/CommandLine.h"
  26. namespace llvm {
  27. //===----------------------------------------------------------------------===//
  28. ///
  29. /// MachinePassRegistryListener - Listener to adds and removals of nodes in
  30. /// registration list.
  31. ///
  32. //===----------------------------------------------------------------------===//
  33. template <class PassCtorTy> class MachinePassRegistryListener {
  34. virtual void anchor() {}
  35. public:
  36. MachinePassRegistryListener() = default;
  37. virtual ~MachinePassRegistryListener() = default;
  38. virtual void NotifyAdd(StringRef N, PassCtorTy C, StringRef D) = 0;
  39. virtual void NotifyRemove(StringRef N) = 0;
  40. };
  41. //===----------------------------------------------------------------------===//
  42. ///
  43. /// MachinePassRegistryNode - Machine pass node stored in registration list.
  44. ///
  45. //===----------------------------------------------------------------------===//
  46. template <typename PassCtorTy> class MachinePassRegistryNode {
  47. private:
  48. MachinePassRegistryNode *Next = nullptr; // Next function pass in list.
  49. StringRef Name; // Name of function pass.
  50. StringRef Description; // Description string.
  51. PassCtorTy Ctor; // Pass creator.
  52. public:
  53. MachinePassRegistryNode(const char *N, const char *D, PassCtorTy C)
  54. : Name(N), Description(D), Ctor(C) {}
  55. // Accessors
  56. MachinePassRegistryNode *getNext() const { return Next; }
  57. MachinePassRegistryNode **getNextAddress() { return &Next; }
  58. StringRef getName() const { return Name; }
  59. StringRef getDescription() const { return Description; }
  60. PassCtorTy getCtor() const { return Ctor; }
  61. void setNext(MachinePassRegistryNode *N) { Next = N; }
  62. };
  63. //===----------------------------------------------------------------------===//
  64. ///
  65. /// MachinePassRegistry - Track the registration of machine passes.
  66. ///
  67. //===----------------------------------------------------------------------===//
  68. template <typename PassCtorTy> class MachinePassRegistry {
  69. private:
  70. MachinePassRegistryNode<PassCtorTy> *List; // List of registry nodes.
  71. PassCtorTy Default; // Default function pass creator.
  72. MachinePassRegistryListener<PassCtorTy>
  73. *Listener; // Listener for list adds are removes.
  74. public:
  75. // NO CONSTRUCTOR - we don't want static constructor ordering to mess
  76. // with the registry.
  77. // Accessors.
  78. //
  79. MachinePassRegistryNode<PassCtorTy> *getList() { return List; }
  80. PassCtorTy getDefault() { return Default; }
  81. void setDefault(PassCtorTy C) { Default = C; }
  82. /// setDefault - Set the default constructor by name.
  83. void setDefault(StringRef Name) {
  84. PassCtorTy Ctor = nullptr;
  85. for (MachinePassRegistryNode<PassCtorTy> *R = getList(); R;
  86. R = R->getNext()) {
  87. if (R->getName() == Name) {
  88. Ctor = R->getCtor();
  89. break;
  90. }
  91. }
  92. assert(Ctor && "Unregistered pass name");
  93. setDefault(Ctor);
  94. }
  95. void setListener(MachinePassRegistryListener<PassCtorTy> *L) { Listener = L; }
  96. /// Add - Adds a function pass to the registration list.
  97. ///
  98. void Add(MachinePassRegistryNode<PassCtorTy> *Node) {
  99. Node->setNext(List);
  100. List = Node;
  101. if (Listener)
  102. Listener->NotifyAdd(Node->getName(), Node->getCtor(),
  103. Node->getDescription());
  104. }
  105. /// Remove - Removes a function pass from the registration list.
  106. ///
  107. void Remove(MachinePassRegistryNode<PassCtorTy> *Node) {
  108. for (MachinePassRegistryNode<PassCtorTy> **I = &List; *I;
  109. I = (*I)->getNextAddress()) {
  110. if (*I == Node) {
  111. if (Listener)
  112. Listener->NotifyRemove(Node->getName());
  113. *I = (*I)->getNext();
  114. break;
  115. }
  116. }
  117. }
  118. };
  119. //===----------------------------------------------------------------------===//
  120. ///
  121. /// RegisterPassParser class - Handle the addition of new machine passes.
  122. ///
  123. //===----------------------------------------------------------------------===//
  124. template <class RegistryClass>
  125. class RegisterPassParser
  126. : public MachinePassRegistryListener<
  127. typename RegistryClass::FunctionPassCtor>,
  128. public cl::parser<typename RegistryClass::FunctionPassCtor> {
  129. public:
  130. RegisterPassParser(cl::Option &O)
  131. : cl::parser<typename RegistryClass::FunctionPassCtor>(O) {}
  132. ~RegisterPassParser() override { RegistryClass::setListener(nullptr); }
  133. void initialize() {
  134. cl::parser<typename RegistryClass::FunctionPassCtor>::initialize();
  135. // Add existing passes to option.
  136. for (RegistryClass *Node = RegistryClass::getList();
  137. Node; Node = Node->getNext()) {
  138. this->addLiteralOption(Node->getName(),
  139. (typename RegistryClass::FunctionPassCtor)Node->getCtor(),
  140. Node->getDescription());
  141. }
  142. // Make sure we listen for list changes.
  143. RegistryClass::setListener(this);
  144. }
  145. // Implement the MachinePassRegistryListener callbacks.
  146. void NotifyAdd(StringRef N, typename RegistryClass::FunctionPassCtor C,
  147. StringRef D) override {
  148. this->addLiteralOption(N, C, D);
  149. }
  150. void NotifyRemove(StringRef N) override {
  151. this->removeLiteralOption(N);
  152. }
  153. };
  154. } // end namespace llvm
  155. #endif // LLVM_CODEGEN_MACHINEPASSREGISTRY_H
  156. #ifdef __GNUC__
  157. #pragma GCC diagnostic pop
  158. #endif