LLVM  8.0.0svn
MachinePassRegistry.h
Go to the documentation of this file.
1 //===- llvm/CodeGen/MachinePassRegistry.h -----------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the mechanics for machine function pass registries. A
11 // function pass registry (MachinePassRegistry) is auto filled by the static
12 // constructors of MachinePassRegistryNode. Further there is a command line
13 // parser (RegisterPassParser) which listens to each registry for additions
14 // and deletions, so that the appropriate command option is updated.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef LLVM_CODEGEN_MACHINEPASSREGISTRY_H
19 #define LLVM_CODEGEN_MACHINEPASSREGISTRY_H
20 
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/CodeGen/Passes.h"
24 
25 namespace llvm {
26 
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 
36 public:
37  MachinePassRegistryListener() = default;
38  virtual ~MachinePassRegistryListener() = default;
39 
40  virtual void NotifyAdd(StringRef N, PassCtorTy C, StringRef D) = 0;
41  virtual void NotifyRemove(StringRef N) = 0;
42 };
43 
44 //===----------------------------------------------------------------------===//
45 ///
46 /// MachinePassRegistryNode - Machine pass node stored in registration list.
47 ///
48 //===----------------------------------------------------------------------===//
49 template <typename PassCtorTy> class MachinePassRegistryNode {
50 private:
51  MachinePassRegistryNode *Next = nullptr; // Next function pass in list.
52  StringRef Name; // Name of function pass.
53  StringRef Description; // Description string.
54  PassCtorTy Ctor; // Pass creator.
55 
56 public:
57  MachinePassRegistryNode(const char *N, const char *D, PassCtorTy C)
58  : Name(N), Description(D), Ctor(C) {}
59 
60  // Accessors
61  MachinePassRegistryNode *getNext() const { return Next; }
63  StringRef getName() const { return Name; }
64  StringRef getDescription() const { return Description; }
65  PassCtorTy getCtor() const { return Ctor; }
66  void setNext(MachinePassRegistryNode *N) { Next = N; }
67 };
68 
69 //===----------------------------------------------------------------------===//
70 ///
71 /// MachinePassRegistry - Track the registration of machine passes.
72 ///
73 //===----------------------------------------------------------------------===//
74 template <typename PassCtorTy> class MachinePassRegistry {
75 private:
76  MachinePassRegistryNode<PassCtorTy> *List; // List of registry nodes.
77  PassCtorTy Default; // Default function pass creator.
79  *Listener; // Listener for list adds are removes.
80 
81 public:
82  // NO CONSTRUCTOR - we don't want static constructor ordering to mess
83  // with the registry.
84 
85  // Accessors.
86  //
88  PassCtorTy getDefault() { return Default; }
89  void setDefault(PassCtorTy C) { Default = C; }
90  /// setDefault - Set the default constructor by name.
92  PassCtorTy Ctor = nullptr;
93  for (MachinePassRegistryNode<PassCtorTy> *R = getList(); R;
94  R = R->getNext()) {
95  if (R->getName() == Name) {
96  Ctor = R->getCtor();
97  break;
98  }
99  }
100  assert(Ctor && "Unregistered pass name");
101  setDefault(Ctor);
102  }
104 
105  /// Add - Adds a function pass to the registration list.
106  ///
108  Node->setNext(List);
109  List = Node;
110  if (Listener)
111  Listener->NotifyAdd(Node->getName(), Node->getCtor(),
112  Node->getDescription());
113  }
114 
115  /// Remove - Removes a function pass from the registration list.
116  ///
118  for (MachinePassRegistryNode<PassCtorTy> **I = &List; *I;
119  I = (*I)->getNextAddress()) {
120  if (*I == Node) {
121  if (Listener)
122  Listener->NotifyRemove(Node->getName());
123  *I = (*I)->getNext();
124  break;
125  }
126  }
127  }
128 };
129 
130 //===----------------------------------------------------------------------===//
131 ///
132 /// RegisterPassParser class - Handle the addition of new machine passes.
133 ///
134 //===----------------------------------------------------------------------===//
135 template <class RegistryClass>
138  typename RegistryClass::FunctionPassCtor>,
139  public cl::parser<typename RegistryClass::FunctionPassCtor> {
140 public:
142  : cl::parser<typename RegistryClass::FunctionPassCtor>(O) {}
143  ~RegisterPassParser() override { RegistryClass::setListener(nullptr); }
144 
145  void initialize() {
147 
148  // Add existing passes to option.
149  for (RegistryClass *Node = RegistryClass::getList();
150  Node; Node = Node->getNext()) {
151  this->addLiteralOption(Node->getName(),
152  (typename RegistryClass::FunctionPassCtor)Node->getCtor(),
153  Node->getDescription());
154  }
155 
156  // Make sure we listen for list changes.
157  RegistryClass::setListener(this);
158  }
159 
160  // Implement the MachinePassRegistryListener callbacks.
161  void NotifyAdd(StringRef N, typename RegistryClass::FunctionPassCtor C,
162  StringRef D) override {
163  this->addLiteralOption(N, C, D);
164  }
165  void NotifyRemove(StringRef N) override {
166  this->removeLiteralOption(N);
167  }
168 };
169 
170 } // end namespace llvm
171 
172 #endif // LLVM_CODEGEN_MACHINEPASSREGISTRY_H
uint64_t CallInst * C
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void Remove(MachinePassRegistryNode< PassCtorTy > *Node)
Remove - Removes a function pass from the registration list.
RegisterPassParser class - Handle the addition of new machine passes.
void setDefault(PassCtorTy C)
amdgpu Simplify well known AMD library false Value Value const Twine & Name
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:210
#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:49