Jlm
Loading...
Searching...
No Matches
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
11namespace jlm::rvsdg
12{
13
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 {
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
47const SimpleOperation &
52
53Node *
54SimpleNode::copy(Region * region, const std::vector<Output *> & operands) const
55{
56 return &Create(*region, GetOperation().copy(), operands);
57}
58
59Node *
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
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
78std::string
80{
81 return GetOperation().debug_string();
82}
83
84std::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
rvsdg::Region * region() const noexcept
Definition node.hpp:761
InputIteratorRange Inputs() noexcept
Definition node.hpp:622
NodeOutput * addOutput(std::unique_ptr< NodeOutput > output)
Definition node.hpp:732
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:420
TopNodeRange TopNodes() noexcept
Definition region.hpp:356
void notifyNodeCreate(Node *node)
Definition region.cpp:411
static SimpleNode & Create(Region &region, std::unique_ptr< Operation > operation, const std::vector< rvsdg::Output * > &operands)
SimpleNode(rvsdg::Region &region, std::unique_ptr< SimpleOperation > operation, const std::vector< jlm::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
size_t nresults() const noexcept
Definition operation.cpp:30
size_t narguments() const noexcept
Definition operation.cpp:17
#define JLM_ASSERT(x)
Definition common.hpp:16
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
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.
NodeType * TryGetOwnerNode(const rvsdg::Input &input) noexcept
Checks if this is an input to a node of specified type.
Definition node.hpp:872
static std::string strfmt(Args... args)
Definition strfmt.hpp:35