summaryrefslogtreecommitdiff
path: root/compiler/loco/include/loco/IR/Node.h
blob: ef0bf238dbbec402479a24b03b7587226f9a1e79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __LOCO_IR_NODE_H__
#define __LOCO_IR_NODE_H__

#include "loco/ADT/AnnotatedItem.h"

#include "loco/IR/Use.h"
#include "loco/IR/Dialect.h"
#include "loco/IR/NodePool.forward.h"
#include "loco/IR/Graph.forward.h"

#include <array>
#include <memory>
#include <set>

namespace loco
{

/**
 * @brief Extensible Node Metadata
 */
struct NodeAnnotation
{
  virtual ~NodeAnnotation() = default;
};

enum class SubstQualifier
{
  Default, // Replace all the occurrences as "Use" (by default)
};

template <SubstQualifier Q> class Subst;

/**
 * @brief Logical unit of computation
 */
class Node : public AnnotatedItem<NodeAnnotation>
{
public:
  friend class Use;
  friend class Subst<SubstQualifier::Default>;
  friend class NodePool;
  friend std::set<Node *> succs(const Node *node);

public:
  Node() = default;

  Node(const Node &) = delete;
  Node(Node &&) = delete;

  virtual ~Node();

public:
  Graph *graph(void) { return _graph; }
  const Graph *graph(void) const { return _graph; }

private:
  /**
   * @brief Set associated "Graph"
   *
   * @note Only "NodePool" class is permitted to invoke this private method.
   */
  void graph(Graph *g) { _graph = g; }

public:
  /**
   * @brief Return "Dialect" identifier that this node belongs to
   *
   * dialect() SHOULD return a valid pointer.
   */
  virtual const Dialect *dialect(void) const = 0;

  virtual uint32_t opnum(void) const = 0;

public:
  /// @brief Return the number of arguments
  virtual uint32_t arity(void) const = 0;

  /// @brief Access N-th argument node
  virtual Node *arg(uint32_t N) const = 0;

  /**
   * @brief Drop all the reference of arguments
   *
   * arg(n) SHOULD return nullptr for every valid n after drop() call.
   */
  virtual void drop(void) = 0;

private:
  /**
   * @brief Associated Graph
   *
   * May be nullptr if no associated Graph exists.
   */
  Graph *_graph = nullptr;

  /**
   * @brief The edges to a node that uses this node as its argument
   *
   * @note "succs" function below accesses this private field.
   */
  std::set<Use *> _uses;
};

/// @brief Enumerate all the predecessors of a given node
std::set<Node *> preds(const Node *node);
/// @brief Enumerate all the successors of a given node
std::set<Node *> succs(const Node *node);

/**
 * @brief A helper for below "replace" helper
 */
template <> class Subst<SubstQualifier::Default>
{
public:
  friend Subst<SubstQualifier::Default> replace(Node *node);

private:
  explicit Subst(Node *from);

public:
  void with(Node *into) const;

private:
  Node *_from;
};

Subst<SubstQualifier::Default> replace(Node *node);

} // namespace loco

#endif // __LOCO_IR_NODE_H__