Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / loco / include / loco / IR / Node.h
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __LOCO_IR_NODE_H__
18 #define __LOCO_IR_NODE_H__
19
20 #include "loco/ADT/AnnotatedItem.h"
21
22 #include "loco/IR/Use.h"
23 #include "loco/IR/Dialect.h"
24 #include "loco/IR/NodePool.forward.h"
25 #include "loco/IR/Graph.forward.h"
26 #include "loco/IR/CastHelpers.h"
27
28 #include <array>
29 #include <memory>
30 #include <set>
31
32 namespace loco
33 {
34
35 /**
36  * @brief Extensible Node Metadata
37  */
38 struct NodeAnnotation
39 {
40   virtual ~NodeAnnotation() = default;
41 };
42
43 enum class SubstQualifier
44 {
45   Default, // Replace all the occurrences as "Use" (by default)
46 };
47
48 template <SubstQualifier Q> class Subst;
49
50 /**
51  * @brief Logical unit of computation
52  */
53 class Node : public AnnotatedItem<NodeAnnotation>
54 {
55 public:
56   friend class Use;
57   friend class Subst<SubstQualifier::Default>;
58   friend class NodePool;
59   friend std::set<Node *> succs(const Node *node);
60
61 public:
62   Node() = default;
63
64   Node(const Node &) = delete;
65   Node(Node &&) = delete;
66
67   virtual ~Node();
68
69 public:
70   Graph *graph(void) { return _graph; }
71   const Graph *graph(void) const { return _graph; }
72
73 private:
74   /**
75    * @brief Set associated "Graph"
76    *
77    * @note Only "NodePool" class is permitted to invoke this private method.
78    */
79   void graph(Graph *g) { _graph = g; }
80
81 public:
82   /**
83    * @brief Return "Dialect" identifier that this node belongs to
84    *
85    * dialect() SHOULD return a valid pointer.
86    */
87   virtual const Dialect *dialect(void) const = 0;
88
89   virtual uint32_t opnum(void) const = 0;
90
91 public:
92   /// @brief Return the number of arguments
93   virtual uint32_t arity(void) const = 0;
94
95   /// @brief Access N-th argument node
96   virtual Node *arg(uint32_t N) const = 0;
97
98   /**
99    * @brief Drop all the reference of arguments
100    *
101    * arg(n) SHOULD return nullptr for every valid n after drop() call.
102    */
103   virtual void drop(void) = 0;
104
105 private:
106   /**
107    * @brief Associated Graph
108    *
109    * May be nullptr if no associated Graph exists.
110    */
111   Graph *_graph = nullptr;
112
113   /**
114    * @brief The edges to a node that uses this node as its argument
115    *
116    * @note "succs" function below accesses this private field.
117    */
118   std::set<Use *> _uses;
119 };
120
121 /// @brief Enumerate all the predecessors of a given node
122 std::set<Node *> preds(const Node *node);
123 /// @brief Enumerate all the successors of a given node
124 std::set<Node *> succs(const Node *node);
125
126 /**
127  * @brief A helper for below "replace" helper
128  */
129 template <> class Subst<SubstQualifier::Default>
130 {
131 public:
132   friend Subst<SubstQualifier::Default> replace(Node *node);
133
134 private:
135   explicit Subst(Node *from);
136
137 public:
138   void with(Node *into) const;
139
140 private:
141   Node *_from;
142 };
143
144 Subst<SubstQualifier::Default> replace(Node *node);
145
146 /**
147  * @brief A helper dynamic_cast that throws when failed
148  */
149 template <typename T> T must_cast(Node *node) { return _must_cast<T, Node *>(node); }
150
151 template <typename T> T must_cast(const Node *node) { return _must_cast<T, const Node *>(node); }
152
153 } // namespace loco
154
155 #endif // __LOCO_IR_NODE_H__