LLVM  10.0.0svn
MachinePassRegistry.h
Go to the documentation of this file.
1 //===- llvm/CodeGen/MachinePassRegistry.h -----------------------*- 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 contains the mechanics for machine function pass registries. A
10 // function pass registry (MachinePassRegistry) is auto filled by the static
11 // constructors of MachinePassRegistryNode. Further there is a command line
12 // parser (RegisterPassParser) which listens to each registry for additions
13 // and deletions, so that the appropriate command option is updated.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef LLVM_CODEGEN_MACHINEPASSREGISTRY_H
18 #define LLVM_CODEGEN_MACHINEPASSREGISTRY_H
19 
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/CodeGen/Passes.h"
23 
24 namespace llvm {
25 
26 //===----------------------------------------------------------------------===//
27 ///
28 /// MachinePassRegistryListener - Listener to adds and removals of nodes in
29 /// registration list.
30 ///
31 //===----------------------------------------------------------------------===//
32 template <class PassCtorTy> class MachinePassRegistryListener {
33  virtual void anchor() {}
34 
35 public:
36  MachinePassRegistryListener() = default;
37  virtual ~MachinePassRegistryListener() = default;
38 
39  virtual void NotifyAdd(StringRef N, PassCtorTy C, StringRef D) = 0;
40  virtual void NotifyRemove(StringRef N) = 0;
41 };
42 
43 //===----------------------------------------------------------------------===//
44 ///
45 /// MachinePassRegistryNode - Machine pass node stored in registration list.
46 ///
47 //===----------------------------------------------------------------------===//
48 template <typename PassCtorTy> class MachinePassRegistryNode {
49 private:
50  MachinePassRegistryNode *Next = nullptr; // Next function pass in list.
51  StringRef Name; // Name of function pass.
52  StringRef Description; // Description string.
53  PassCtorTy Ctor; // Pass creator.
54 
55 public:
56  MachinePassRegistryNode(const char *N, const char *D, PassCtorTy C)
57  : Name(N), Description(D), Ctor(C) {}
58 
59  // Accessors
60  MachinePassRegistryNode *getNext() const { return Next; }
62  StringRef getName() const { return Name; }
63  StringRef getDescription() const { return Description; }
64  PassCtorTy getCtor() const { return Ctor; }
65  void setNext(MachinePassRegistryNode *N) { Next = N; }
66 };
67 
68 //===----------------------------------------------------------------------===//
69 ///
70 /// MachinePassRegistry - Track the registration of machine passes.
71 ///
72 //===----------------------------------------------------------------------===//
73 template <typename PassCtorTy> class MachinePassRegistry {
74 private:
75  MachinePassRegistryNode<PassCtorTy> *List; // List of registry nodes.
76  PassCtorTy Default; // Default function pass creator.
78  *Listener; // Listener for list adds are removes.
79 
80 public:
81  // NO CONSTRUCTOR - we don't want static constructor ordering to mess
82  // with the registry.
83 
84  // Accessors.
85  //
87  PassCtorTy getDefault() { return Default; }
88  void setDefault(PassCtorTy C) { Default = C; }
89  /// setDefault - Set the default constructor by name.
91  PassCtorTy Ctor = nullptr;
92  for (MachinePassRegistryNode<PassCtorTy> *R = getList(); R;
93  R = R->getNext()) {
94  if (R->getName() == Name) {
95  Ctor = R->getCtor();
96  break;
97  }
98  }
99  assert(Ctor && "Unregistered pass name");
100  setDefault(Ctor);
101  }
103 
104  /// Add - Adds a function pass to the registration list.
105  ///
107  Node->setNext(List);
108  List = Node;
109  if (Listener)
110  Listener->NotifyAdd(Node->getName(), Node->getCtor(),
111  Node->getDescription());
112  }
113 
114  /// Remove - Removes a function pass from the registration list.
115  ///
117  for (MachinePassRegistryNode<PassCtorTy> **I = &List; *I;
118  I = (*I)->getNextAddress()) {
119  if (*I == Node) {
120  if (Listener)
121  Listener->NotifyRemove(Node->getName());
122  *I = (*I)->getNext();
123  break;
124  }
125  }
126  }
127 };
128 
129 //===----------------------------------------------------------------------===//
130 ///
131 /// RegisterPassParser class - Handle the addition of new machine passes.
132 ///
133 //===----------------------------------------------------------------------===//
134 template <class RegistryClass>
137  typename RegistryClass::FunctionPassCtor>,
138  public cl::parser<typename RegistryClass::FunctionPassCtor> {
139 public:
141  : cl::parser<typename RegistryClass::FunctionPassCtor>(O) {}
142  ~RegisterPassParser() override { RegistryClass::setListener(nullptr); }
143 
144  void initialize() {
146 
147  // Add existing passes to option.
148  for (RegistryClass *Node = RegistryClass::getList();
149  Node; Node = Node->getNext()) {
150  this->addLiteralOption(Node->getName(),
151  (typename RegistryClass::FunctionPassCtor)Node->getCtor(),
152  Node->getDescription());
153  }
154 
155  // Make sure we listen for list changes.
156  RegistryClass::setListener(this);
157  }
158 
159  // Implement the MachinePassRegistryListener callbacks.
160  void NotifyAdd(StringRef N, typename RegistryClass::FunctionPassCtor C,
161  StringRef D) override {
162  this->addLiteralOption(N, C, D);
163  }
164  void NotifyRemove(StringRef N) override {
165  this->removeLiteralOption(N);
166  }
167 };
168 
169 } // end namespace llvm
170 
171 #endif // LLVM_CODEGEN_MACHINEPASSREGISTRY_H
uint64_t CallInst * C
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void Remove(MachinePassRegistryNode< PassCtorTy > *Node)
Remove - Removes a function pass from the registration list.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
RegisterPassParser class - Handle the addition of new machine passes.
void setDefault(PassCtorTy C)
virtual void NotifyAdd(StringRef N, PassCtorTy C, StringRef D)=0
void setNext(MachinePassRegistryNode *N)
void NotifyAdd(StringRef N, typename RegistryClass::FunctionPassCtor C, StringRef D) override
void setListener(MachinePassRegistryListener< PassCtorTy > *L)
void NotifyRemove(StringRef N) override
MachinePassRegistry - Track the registration of machine passes.
void Add(MachinePassRegistryNode< PassCtorTy > *Node)
Add - Adds a function pass to the registration list.
virtual void NotifyRemove(StringRef N)=0
MachinePassRegistryNode< PassCtorTy > * getList()
virtual ~MachinePassRegistryListener()=default
MachinePassRegistryNode ** getNextAddress()
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
void setDefault(StringRef Name)
setDefault - Set the default constructor by name.
MachinePassRegistryListener - Listener to adds and removals of nodes in registration list...
MachinePassRegistryNode(const char *N, const char *D, PassCtorTy C)
MachinePassRegistryNode * getNext() const
const NodeList & List
Definition: RDFGraph.cpp:201
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
MachinePassRegistryNode - Machine pass node stored in registration list.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48