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/CodeGen/Passes.h"
22#include "llvm/Support/CommandLine.h"
23
24namespace llvm {
25
26typedef void *(*MachinePassCtor)();
27
28
29//===----------------------------------------------------------------------===//
30///
31/// MachinePassRegistryListener - Listener to adds and removals of nodes in
32/// registration list.
33///
34//===----------------------------------------------------------------------===//
35class MachinePassRegistryListener {
36  virtual void anchor();
37public:
38  MachinePassRegistryListener() {}
39  virtual ~MachinePassRegistryListener() {}
40  virtual void NotifyAdd(const char *N, MachinePassCtor C, const char *D) = 0;
41  virtual void NotifyRemove(const char *N) = 0;
42};
43
44
45//===----------------------------------------------------------------------===//
46///
47/// MachinePassRegistryNode - Machine pass node stored in registration list.
48///
49//===----------------------------------------------------------------------===//
50class MachinePassRegistryNode {
51
52private:
53
54  MachinePassRegistryNode *Next;        // Next function pass in list.
55  const char *Name;                     // Name of function pass.
56  const char *Description;              // Description string.
57  MachinePassCtor Ctor;                 // Function pass creator.
58
59public:
60
61  MachinePassRegistryNode(const char *N, const char *D, MachinePassCtor C)
62  : Next(nullptr)
63  , Name(N)
64  , Description(D)
65  , Ctor(C)
66  {}
67
68  // Accessors
69  MachinePassRegistryNode *getNext()      const { return Next; }
70  MachinePassRegistryNode **getNextAddress()    { return &Next; }
71  const char *getName()                   const { return Name; }
72  const char *getDescription()            const { return Description; }
73  MachinePassCtor getCtor()               const { return Ctor; }
74  void setNext(MachinePassRegistryNode *N)      { Next = N; }
75
76};
77
78
79//===----------------------------------------------------------------------===//
80///
81/// MachinePassRegistry - Track the registration of machine passes.
82///
83//===----------------------------------------------------------------------===//
84class MachinePassRegistry {
85
86private:
87
88  MachinePassRegistryNode *List;        // List of registry nodes.
89  MachinePassCtor Default;              // Default function pass creator.
90  MachinePassRegistryListener* Listener;// Listener for list adds are removes.
91
92public:
93
94  // NO CONSTRUCTOR - we don't want static constructor ordering to mess
95  // with the registry.
96
97  // Accessors.
98  //
99  MachinePassRegistryNode *getList()                    { return List; }
100  MachinePassCtor getDefault()                          { return Default; }
101  void setDefault(MachinePassCtor C)                    { Default = C; }
102  void setDefault(StringRef Name);
103  void setListener(MachinePassRegistryListener *L)      { Listener = L; }
104
105  /// Add - Adds a function pass to the registration list.
106  ///
107  void Add(MachinePassRegistryNode *Node);
108
109  /// Remove - Removes a function pass from the registration list.
110  ///
111  void Remove(MachinePassRegistryNode *Node);
112
113};
114
115
116//===----------------------------------------------------------------------===//
117///
118/// RegisterPassParser class - Handle the addition of new machine passes.
119///
120//===----------------------------------------------------------------------===//
121template<class RegistryClass>
122class RegisterPassParser : public MachinePassRegistryListener,
123                   public cl::parser<typename RegistryClass::FunctionPassCtor> {
124public:
125  RegisterPassParser() {}
126  ~RegisterPassParser() { RegistryClass::setListener(nullptr); }
127
128  void initialize(cl::Option &O) {
129    cl::parser<typename RegistryClass::FunctionPassCtor>::initialize(O);
130
131    // Add existing passes to option.
132    for (RegistryClass *Node = RegistryClass::getList();
133         Node; Node = Node->getNext()) {
134      this->addLiteralOption(Node->getName(),
135                      (typename RegistryClass::FunctionPassCtor)Node->getCtor(),
136                             Node->getDescription());
137    }
138
139    // Make sure we listen for list changes.
140    RegistryClass::setListener(this);
141  }
142
143  // Implement the MachinePassRegistryListener callbacks.
144  //
145  void NotifyAdd(const char *N, MachinePassCtor C, const char *D) override {
146    this->addLiteralOption(N, (typename RegistryClass::FunctionPassCtor)C, D);
147  }
148  void NotifyRemove(const char *N) override {
149    this->removeLiteralOption(N);
150  }
151};
152
153
154} // end namespace llvm
155
156#endif
157