Jlm
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 
13 namespace jlm::rvsdg
14 {
15 
16 class SimpleOperation;
17 
18 class SimpleNode final : public Node
19 {
20 public:
21  ~SimpleNode() override;
22 
23 private:
24  SimpleNode(
26  std::unique_ptr<SimpleOperation> operation,
27  const std::vector<jlm::rvsdg::Output *> & operands);
28 
29 public:
30  NodeInput *
31  input(size_t index) const noexcept;
32 
33  NodeOutput *
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 
62 private:
63  std::unique_ptr<SimpleOperation> Operation_;
64 };
65 
75 std::optional<std::vector<rvsdg::Output *>>
77  Region & region,
78  const SimpleOperation & operation,
79  const std::vector<rvsdg::Output *> & operands);
80 
81 inline NodeInput *
82 SimpleNode::input(size_t index) const noexcept
83 {
84  return Node::input(index);
85 }
86 
87 inline NodeOutput *
88 SimpleNode::output(size_t index) const noexcept
89 {
90  return Node::output(index);
91 }
92 
126 template<typename OperatorType, typename... OperatorArguments>
127 SimpleNode &
128 CreateOpNode(const std::vector<Output *> & operands, OperatorArguments... 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 
168 template<typename OperatorType, typename... OperatorArguments>
169 SimpleNode &
170 CreateOpNode(Region & region, OperatorArguments... operatorArguments)
171 {
172  return SimpleNode::Create(
173  region,
174  std::make_unique<OperatorType>(std::move(operatorArguments)...),
175  {});
176 }
177 
198 template<typename TOperation>
199 [[nodiscard]] std::pair<SimpleNode *, const TOperation *>
200 TryGetSimpleNodeAndOptionalOp(const Input & input) noexcept
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 
236 template<typename TOperation>
237 [[nodiscard]] std::pair<SimpleNode *, const TOperation *>
238 TryGetSimpleNodeAndOptionalOp(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 
273 template<typename TOperation>
274 [[nodiscard]] std::pair<const SimpleNode *, const TOperation *>
275 TryGetSimpleNodeAndOptionalOp(const Node & node) noexcept
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
rvsdg::Region * region() const noexcept
Definition: node.hpp:761
NodeInput * input(size_t index) const noexcept
Definition: node.hpp:615
NodeOutput * output(size_t index) const noexcept
Definition: node.hpp:650
Represent acyclic RVSDG subgraphs.
Definition: region.hpp:213
SimpleNode(rvsdg::Region &region, std::unique_ptr< SimpleOperation > operation, const std::vector< jlm::rvsdg::Output * > &operands)
Definition: simple-node.cpp:19
const SimpleOperation & GetOperation() const noexcept override
Definition: simple-node.cpp:48
std::unique_ptr< SimpleOperation > Operation_
Definition: simple-node.hpp:63
Node * copy(Region *region, const std::vector< Output * > &operands) const override
Definition: simple-node.cpp:54
std::string DebugString() const override
Definition: simple-node.cpp:79
NodeInput * input(size_t index) const noexcept
Definition: simple-node.hpp:82
NodeOutput * output(size_t index) const noexcept
Definition: simple-node.hpp:88
static SimpleNode & Create(Region &region, std::unique_ptr< Operation > operation, const std::vector< rvsdg::Output * > &operands)
Definition: simple-node.hpp:49
#define JLM_ASSERT(x)
Definition: common.hpp:16
SimpleNode & CreateOpNode(const std::vector< Output * > &operands, OperatorArguments... operatorArguments)
Creates a simple node characterized by its operator.
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.
Definition: simple-node.cpp:85
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