LLVM  14.0.0git
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;
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  ///
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
llvm::MachinePassRegistryNode::getNextAddress
MachinePassRegistryNode ** getNextAddress()
Definition: MachinePassRegistry.h:61
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::MachinePassRegistryListener::NotifyAdd
virtual void NotifyAdd(StringRef N, PassCtorTy C, StringRef D)=0
llvm::MachinePassRegistry::setDefault
void setDefault(PassCtorTy C)
Definition: MachinePassRegistry.h:88
StringRef.h
llvm::MachinePassRegistry::Add
void Add(MachinePassRegistryNode< PassCtorTy > *Node)
Add - Adds a function pass to the registration list.
Definition: MachinePassRegistry.h:106
llvm::cl::parser< RegistryClass::FunctionPassCtor >::parser
parser(Option &O)
Definition: CommandLine.h:821
llvm::MachinePassRegistryListener
MachinePassRegistryListener - Listener to adds and removals of nodes in registration list.
Definition: MachinePassRegistry.h:32
llvm::MachinePassRegistryListener::MachinePassRegistryListener
MachinePassRegistryListener()=default
llvm::cl::generic_parser_base::initialize
void initialize()
Definition: CommandLine.h:765
llvm::MachinePassRegistryListener::~MachinePassRegistryListener
virtual ~MachinePassRegistryListener()=default
llvm::MachinePassRegistryNode::getCtor
PassCtorTy getCtor() const
Definition: MachinePassRegistry.h:64
FunctionPassCtor
CommandLine.h
llvm::RegisterPassParser::RegisterPassParser
RegisterPassParser(cl::Option &O)
Definition: MachinePassRegistry.h:140
llvm::cl::parser< RegistryClass::FunctionPassCtor >::removeLiteralOption
void removeLiteralOption(StringRef Name)
removeLiteralOption - Remove the specified option.
Definition: CommandLine.h:866
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::MachinePassRegistryNode::MachinePassRegistryNode
MachinePassRegistryNode(const char *N, const char *D, PassCtorTy C)
Definition: MachinePassRegistry.h:56
llvm::RegisterPassParser::NotifyAdd
void NotifyAdd(StringRef N, typename RegistryClass::FunctionPassCtor C, StringRef D) override
Definition: MachinePassRegistry.h:160
llvm::MachinePassRegistry::Remove
void Remove(MachinePassRegistryNode< PassCtorTy > *Node)
Remove - Removes a function pass from the registration list.
Definition: MachinePassRegistry.h:116
llvm::cl::parser
Definition: CommandLine.h:809
llvm::MachinePassRegistryNode::getDescription
StringRef getDescription() const
Definition: MachinePassRegistry.h:63
llvm::RegisterPassParser
RegisterPassParser class - Handle the addition of new machine passes.
Definition: MachinePassRegistry.h:135
llvm::MachinePassRegistryNode
MachinePassRegistryNode - Machine pass node stored in registration list.
Definition: MachinePassRegistry.h:48
Passes.h
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:197
llvm::cl::Option
Definition: CommandLine.h:249
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::RegisterPassParser::~RegisterPassParser
~RegisterPassParser() override
Definition: MachinePassRegistry.h:142
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::RegisterPassParser::initialize
void initialize()
Definition: MachinePassRegistry.h:144
llvm::MachinePassRegistryNode::setNext
void setNext(MachinePassRegistryNode *N)
Definition: MachinePassRegistry.h:65
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::cl::parser< RegistryClass::FunctionPassCtor >::addLiteralOption
void addLiteralOption(StringRef Name, const DT &V, StringRef HelpStr)
addLiteralOption - Add an entry to the mapping table.
Definition: CommandLine.h:857
llvm::MachinePassRegistry::setListener
void setListener(MachinePassRegistryListener< PassCtorTy > *L)
Definition: MachinePassRegistry.h:102
llvm::MachinePassRegistry::getDefault
PassCtorTy getDefault()
Definition: MachinePassRegistry.h:87
llvm::MachinePassRegistry
MachinePassRegistry - Track the registration of machine passes.
Definition: MachinePassRegistry.h:73
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
cl
http eax xorl edx cl sete al setne dl sall cl
Definition: README.txt:25
llvm::MachinePassRegistry::getList
MachinePassRegistryNode< PassCtorTy > * getList()
Definition: MachinePassRegistry.h:86
llvm::MachinePassRegistryListener::NotifyRemove
virtual void NotifyRemove(StringRef N)=0
Node
Definition: ItaniumDemangle.h:235
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::RegisterPassParser::NotifyRemove
void NotifyRemove(StringRef N) override
Definition: MachinePassRegistry.h:164
llvm::MachinePassRegistryNode::getName
StringRef getName() const
Definition: MachinePassRegistry.h:62
List
const NodeList & List
Definition: RDFGraph.cpp:201
N
#define N
llvm::MachinePassRegistry::setDefault
void setDefault(StringRef Name)
setDefault - Set the default constructor by name.
Definition: MachinePassRegistry.h:90
llvm::MachinePassRegistryNode::getNext
MachinePassRegistryNode * getNext() const
Definition: MachinePassRegistry.h:60