Jlm
Loading...
Searching...
No Matches
simple-node.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 Nico Reißmann <nico.reissmann@gmail.com>
3 * See COPYING for terms of redistribution.
4 */
5
6#ifndef JLM_RVSDG_SIMPLE_NODE_HPP
7#define JLM_RVSDG_SIMPLE_NODE_HPP
8
9#include <jlm/rvsdg/node.hpp>
10
11#include <optional>
12
13namespace jlm::rvsdg
14{
15
16class SimpleOperation;
17
18class SimpleNode final : public Node
19{
20public:
21 ~SimpleNode() override;
22
23private:
26 std::unique_ptr<SimpleOperation> operation,
27 const std::vector<jlm::rvsdg::Output *> & operands);
28
29public:
30 NodeInput *
31 input(size_t index) const noexcept;
32
34 output(size_t index) const noexcept;
35
36 [[nodiscard]] const SimpleOperation &
37 GetOperation() const noexcept override;
38
39 Node *
40 copy(Region * region, const std::vector<Output *> & operands) const override;
41
42 Node *
43 copy(Region * region, SubstitutionMap & smap) const override;
44
45 std::string
46 DebugString() const override;
47
48 static SimpleNode &
50 Region & region,
51 std::unique_ptr<Operation> operation,
52 const std::vector<rvsdg::Output *> & operands)
53 {
54 if (!is<SimpleOperation>(*operation))
55 throw util::Error("Expected operation derived from SimpleOperation");
56
57 std::unique_ptr<SimpleOperation> simpleOperation(
58 util::assertedCast<SimpleOperation>(operation.release()));
59 return *new SimpleNode(region, std::move(simpleOperation), operands);
60 }
61
62private:
63 std::unique_ptr<SimpleOperation> Operation_;
64};
65
75std::optional<std::vector<rvsdg::Output *>>
77 Region & region,
78 const SimpleOperation & operation,
79 const std::vector<rvsdg::Output *> & operands);
80
81inline NodeInput *
82SimpleNode::input(size_t index) const noexcept
83{
84 return Node::input(index);
85}
86
87inline NodeOutput *
88SimpleNode::output(size_t index) const noexcept
89{
90 return Node::output(index);
91}
92
126template<typename OperatorType, typename... OperatorArguments>
129{
130 JLM_ASSERT(!operands.empty());
131 return SimpleNode::Create(
132 *operands[0]->region(),
133 std::make_unique<OperatorType>(std::move(operatorArguments)...),
134 operands);
135}
136
168template<typename OperatorType, typename... OperatorArguments>
169SimpleNode &
171{
172 return SimpleNode::Create(
173 region,
174 std::make_unique<OperatorType>(std::move(operatorArguments)...),
175 {});
176}
177
198template<typename TOperation>
199[[nodiscard]] std::pair<SimpleNode *, const TOperation *>
201{
202 const auto simpleNode = TryGetOwnerNode<SimpleNode>(input);
203 if (!simpleNode)
204 {
205 return std::make_pair(nullptr, nullptr);
206 }
207
208 if (auto operation = dynamic_cast<const TOperation *>(&simpleNode->GetOperation()))
209 {
210 return std::make_pair(simpleNode, operation);
211 }
212
213 return std::make_pair(simpleNode, nullptr);
214}
215
236template<typename TOperation>
237[[nodiscard]] std::pair<SimpleNode *, const TOperation *>
238TryGetSimpleNodeAndOptionalOp(const Output & output) noexcept
239{
240 const auto simpleNode = TryGetOwnerNode<SimpleNode>(output);
241 if (!simpleNode)
242 {
243 return std::make_pair(nullptr, nullptr);
244 }
245
246 if (auto operation = dynamic_cast<const TOperation *>(&simpleNode->GetOperation()))
247 {
248 return std::make_pair(simpleNode, operation);
249 }
250
251 return std::make_pair(simpleNode, nullptr);
252}
253
273template<typename TOperation>
274[[nodiscard]] std::pair<const SimpleNode *, const TOperation *>
276{
277 const auto simpleNode = dynamic_cast<const SimpleNode *>(&node);
278 if (!simpleNode)
279 {
280 return std::make_pair(nullptr, nullptr);
281 }
282 if (auto operation = dynamic_cast<const TOperation *>(&simpleNode->GetOperation()))
283 {
284 return std::make_pair(simpleNode, operation);
285 }
286 return std::make_pair(simpleNode, nullptr);
287}
288
289}
290
291#endif
NodeInput * input(size_t index) const noexcept
Definition node.hpp:615
NodeOutput * output(size_t index) const noexcept
Definition node.hpp:650
rvsdg::Region * region() const noexcept
Definition node.hpp:761
Represent acyclic RVSDG subgraphs.
Definition region.hpp:213
static SimpleNode & Create(Region &region, std::unique_ptr< Operation > operation, const std::vector< rvsdg::Output * > &operands)
const SimpleOperation & GetOperation() const noexcept override
std::unique_ptr< SimpleOperation > Operation_
Node * copy(Region *region, const std::vector< Output * > &operands) const override
std::string DebugString() const override
NodeInput * input(size_t index) const noexcept
NodeOutput * output(size_t index) const noexcept
#define JLM_ASSERT(x)
Definition common.hpp:16
std::pair< SimpleNode *, const TOperation * > TryGetSimpleNodeAndOptionalOp(const Input &input) noexcept
Checks if this is an input to a SimpleNode and of specified operation type.
static std::vector< jlm::rvsdg::Output * > operands(const Node *node)
Definition node.hpp:1049
std::optional< std::vector< rvsdg::Output * > > NormalizeSimpleOperationCommonNodeElimination(Region &region, const SimpleOperation &operation, const std::vector< rvsdg::Output * > &operands)
Performs common node elimination for a given operation and operands in a region.
SimpleNode & CreateOpNode(const std::vector< Output * > &operands, OperatorArguments... operatorArguments)
Creates a simple node characterized by its operator.
NodeType * TryGetOwnerNode(const rvsdg::Input &input) noexcept
Checks if this is an input to a node of specified type.
Definition node.hpp:872