Jlm
simple-node.cpp
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 #include <jlm/rvsdg/graph.hpp>
9 #include <jlm/util/strfmt.hpp>
10 
11 namespace jlm::rvsdg
12 {
13 
15 {
16  region()->notifyNodeDestroy(this);
17 }
18 
20  rvsdg::Region & region,
21  std::unique_ptr<SimpleOperation> operation,
22  const std::vector<jlm::rvsdg::Output *> & operands)
23  : Node(&region),
24  Operation_(std::move(operation))
25 {
26  if (GetOperation().narguments() != operands.size())
28  "Argument error - expected ",
29  SimpleNode::GetOperation().narguments(),
30  ", received ",
31  operands.size(),
32  " arguments."));
33 
34  for (size_t n = 0; n < SimpleNode::GetOperation().narguments(); n++)
35  {
36  addInput(
37  std::make_unique<NodeInput>(operands[n], this, SimpleNode::GetOperation().argument(n)),
38  false);
39  }
40 
41  for (size_t n = 0; n < SimpleNode::GetOperation().nresults(); n++)
42  addOutput(std::make_unique<NodeOutput>(this, SimpleNode::GetOperation().result(n)));
43 
45 }
46 
47 const SimpleOperation &
48 SimpleNode::GetOperation() const noexcept
49 {
50  return *Operation_;
51 }
52 
53 Node *
54 SimpleNode::copy(Region * region, const std::vector<Output *> & operands) const
55 {
56  return &Create(*region, GetOperation().copy(), operands);
57 }
58 
59 Node *
60 SimpleNode::copy(Region * region, SubstitutionMap & smap) const
61 {
62  std::vector<Output *> operands;
63  for (auto & input : Inputs())
64  {
65  auto & operand = smap.lookup(*input.origin());
66  operands.push_back(&operand);
67  }
68 
69  auto copiedNode = copy(region, operands);
70 
71  JLM_ASSERT(copiedNode->noutputs() == noutputs());
72  for (size_t n = 0; n < copiedNode->noutputs(); n++)
73  smap.insert(output(n), copiedNode->output(n));
74 
75  return copiedNode;
76 }
77 
78 std::string
80 {
81  return GetOperation().debug_string();
82 }
83 
84 std::optional<std::vector<rvsdg::Output *>>
86  Region & region,
87  const SimpleOperation & operation,
88  const std::vector<rvsdg::Output *> & operands)
89 {
90  auto isCongruent = [&](const Node & node)
91  {
92  auto simpleNode = dynamic_cast<const SimpleNode *>(&node);
93  return simpleNode && simpleNode->GetOperation() == operation
94  && operands == rvsdg::operands(&node) && &simpleNode->GetOperation() != &operation;
95  };
96 
97  if (operands.empty())
98  {
99  for (auto & node : region.TopNodes())
100  {
101  if (isCongruent(node))
102  {
103  return outputs(&node);
104  }
105  }
106  }
107  else
108  {
109  for (const auto & user : operands[0]->Users())
110  {
111  if (const auto node = TryGetOwnerNode<SimpleNode>(user))
112  {
113  if (isCongruent(*node))
114  {
115  return outputs(node);
116  }
117  }
118  }
119  }
120 
121  return std::nullopt;
122 }
123 
124 }
Output * origin() const noexcept
Definition: node.hpp:58
NodeOutput * addOutput(std::unique_ptr< NodeOutput > output)
Definition: node.hpp:732
rvsdg::Region * region() const noexcept
Definition: node.hpp:761
InputIteratorRange Inputs() noexcept
Definition: node.hpp:622
size_t noutputs() const noexcept
Definition: node.hpp:644
NodeInput * addInput(std::unique_ptr< NodeInput > input, bool notifyRegion)
Definition: node.cpp:288
virtual std::string debug_string() const =0
Represent acyclic RVSDG subgraphs.
Definition: region.hpp:213
void notifyNodeDestroy(Node *node)
Definition: region.cpp:417
TopNodeRange TopNodes() noexcept
Definition: region.hpp:309
void notifyNodeCreate(Node *node)
Definition: region.cpp:408
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
size_t nresults() const noexcept
Definition: operation.cpp:30
size_t narguments() const noexcept
Definition: operation.cpp:17
void insert(const Output *original, Output *substitute)
Output & lookup(const Output &original) const
#define JLM_ASSERT(x)
Definition: common.hpp:16
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
static std::vector< jlm::rvsdg::Output * > operands(const Node *node)
Definition: node.hpp:1049
static std::vector< jlm::rvsdg::Output * > outputs(const Node *node)
Definition: node.hpp:1058
static std::string strfmt(Args... args)
Definition: strfmt.hpp:35