2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __LOCO_IR_NODE_H__
18 #define __LOCO_IR_NODE_H__
20 #include "loco/ADT/AnnotatedItem.h"
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"
36 * @brief Extensible Node Metadata
40 virtual ~NodeAnnotation() = default;
43 enum class SubstQualifier
45 Default, // Replace all the occurrences as "Use" (by default)
48 template <SubstQualifier Q> class Subst;
51 * @brief Logical unit of computation
53 class Node : public AnnotatedItem<NodeAnnotation>
57 friend class Subst<SubstQualifier::Default>;
58 friend class NodePool;
59 friend std::set<Node *> succs(const Node *node);
64 Node(const Node &) = delete;
65 Node(Node &&) = delete;
70 Graph *graph(void) { return _graph; }
71 const Graph *graph(void) const { return _graph; }
75 * @brief Set associated "Graph"
77 * @note Only "NodePool" class is permitted to invoke this private method.
79 void graph(Graph *g) { _graph = g; }
83 * @brief Return "Dialect" identifier that this node belongs to
85 * dialect() SHOULD return a valid pointer.
87 virtual const Dialect *dialect(void) const = 0;
89 virtual uint32_t opnum(void) const = 0;
92 /// @brief Return the number of arguments
93 virtual uint32_t arity(void) const = 0;
95 /// @brief Access N-th argument node
96 virtual Node *arg(uint32_t N) const = 0;
99 * @brief Drop all the reference of arguments
101 * arg(n) SHOULD return nullptr for every valid n after drop() call.
103 virtual void drop(void) = 0;
107 * @brief Associated Graph
109 * May be nullptr if no associated Graph exists.
111 Graph *_graph = nullptr;
114 * @brief The edges to a node that uses this node as its argument
116 * @note "succs" function below accesses this private field.
118 std::set<Use *> _uses;
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);
127 * @brief A helper for below "replace" helper
129 template <> class Subst<SubstQualifier::Default>
132 friend Subst<SubstQualifier::Default> replace(Node *node);
135 explicit Subst(Node *from);
138 void with(Node *into) const;
144 Subst<SubstQualifier::Default> replace(Node *node);
147 * @brief A helper dynamic_cast that throws when failed
149 template <typename T> T must_cast(Node *node) { return _must_cast<T, Node *>(node); }
151 template <typename T> T must_cast(const Node *node) { return _must_cast<T, const Node *>(node); }
155 #endif // __LOCO_IR_NODE_H__