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