Jlm
TestOperations.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2025 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #ifndef JLM_RVSDG_TESTOPERATIONS_HPP
7 #define JLM_RVSDG_TESTOPERATIONS_HPP
8 
9 #include <jlm/rvsdg/binary.hpp>
10 #include <jlm/rvsdg/nullary.hpp>
11 #include <jlm/rvsdg/unary.hpp>
12 
13 namespace jlm::rvsdg
14 {
15 
17 {
18 public:
19  explicit TestNullaryOperation(const std::shared_ptr<const Type> & resultType)
20  : NullaryOperation(resultType)
21  {}
22 
23  bool
24  operator==(const Operation & other) const noexcept override
25  {
26  const auto nullaryOperation = dynamic_cast<const TestNullaryOperation *>(&other);
27  return nullaryOperation && *result(0) == *nullaryOperation->result(0);
28  }
29 
30  [[nodiscard]] std::string
31  debug_string() const override
32  {
33  return "NullaryOperation";
34  }
35 
36  [[nodiscard]] std::unique_ptr<Operation>
37  copy() const override
38  {
39  return std::make_unique<TestNullaryOperation>(this->result(0));
40  }
41 };
42 
43 class TestUnaryOperation final : public UnaryOperation
44 {
45 public:
46  ~TestUnaryOperation() noexcept override;
47 
49  std::shared_ptr<const Type> operandType,
50  std::shared_ptr<const Type> resultType) noexcept
51  : UnaryOperation(std::move(operandType), std::move(resultType))
52  {}
53 
54  bool
55  operator==(const Operation & other) const noexcept override;
56 
58  can_reduce_operand(const Output * operand) const noexcept override;
59 
60  Output *
61  reduce_operand(unop_reduction_path_t path, Output * operand) const override;
62 
63  [[nodiscard]] std::string
64  debug_string() const override;
65 
66  [[nodiscard]] std::unique_ptr<Operation>
67  copy() const override;
68 
69  static Node *
71  Region *,
72  std::shared_ptr<const Type> operandType,
73  Output * operand,
74  std::shared_ptr<const Type> resultType)
75  {
76  return &rvsdg::CreateOpNode<TestUnaryOperation>(
77  { operand },
78  std::move(operandType),
79  std::move(resultType));
80  }
81 
82  static Output *
84  std::shared_ptr<const Type> operandType,
85  Output * operand,
86  std::shared_ptr<const Type> resultType)
87  {
88  return rvsdg::CreateOpNode<TestUnaryOperation>(
89  { operand },
90  std::move(operandType),
91  std::move(resultType))
92  .output(0);
93  }
94 };
95 
97 {
98 public:
99  ~TestBinaryOperation() noexcept override;
100 
102  const std::shared_ptr<const Type> & operandType,
103  std::shared_ptr<const Type> resultType,
104  const enum BinaryOperation::flags & flags) noexcept
105  : BinaryOperation({ operandType, operandType }, std::move(resultType)),
106  flags_(flags)
107  {}
108 
109  bool
110  operator==(const Operation & other) const noexcept override;
111 
113  can_reduce_operand_pair(const Output * op1, const Output * op2) const noexcept override;
114 
115  Output *
116  reduce_operand_pair(unop_reduction_path_t path, Output * op1, Output * op2) const override;
117 
119  flags() const noexcept override;
120 
121  [[nodiscard]] std::string
122  debug_string() const override;
123 
124  [[nodiscard]] std::unique_ptr<Operation>
125  copy() const override;
126 
127  static Node *
129  const std::shared_ptr<const Type> & operandType,
130  std::shared_ptr<const Type> resultType,
131  Output * op1,
132  Output * op2)
133  {
134  return &rvsdg::CreateOpNode<TestBinaryOperation>(
135  { op1, op2 },
136  operandType,
137  std::move(resultType),
138  flags::none);
139  }
140 
141  static Output *
143  const std::shared_ptr<const Type> operandType,
144  std::shared_ptr<const Type> resultType,
145  Output * op1,
146  Output * op2)
147  {
148  return rvsdg::CreateOpNode<TestBinaryOperation>(
149  { op1, op2 },
150  operandType,
151  std::move(resultType),
152  flags::none)
153  .output(0);
154  }
155 
156 private:
158 };
159 
160 class TestOperation final : public SimpleOperation
161 {
162 public:
163  ~TestOperation() noexcept override;
164 
166  std::vector<std::shared_ptr<const Type>> operandTypes,
167  std::vector<std::shared_ptr<const Type>> resultTypes)
168  : SimpleOperation(std::move(operandTypes), std::move(resultTypes))
169  {}
170 
171  TestOperation(const TestOperation &) = default;
172 
173  bool
174  operator==(const Operation & other) const noexcept override;
175 
176  [[nodiscard]] std::string
177  debug_string() const override;
178 
179  [[nodiscard]] std::unique_ptr<Operation>
180  copy() const override;
181 
182  static std::unique_ptr<TestOperation>
184  std::vector<std::shared_ptr<const Type>> operandTypes,
185  std::vector<std::shared_ptr<const Type>> resultTypes)
186  {
187  return std::make_unique<TestOperation>(std::move(operandTypes), std::move(resultTypes));
188  }
189 
190  static SimpleNode *
192  Region * region,
193  const std::vector<Output *> & operands,
194  std::vector<std::shared_ptr<const Type>> resultTypes)
195  {
196  std::vector<std::shared_ptr<const Type>> operandTypes;
197  for (const auto & operand : operands)
198  operandTypes.push_back(operand->Type());
199 
200  return createNode(region, operandTypes, operands, std::move(resultTypes));
201  }
202 
203  static SimpleNode *
205  Region * region,
206  std::vector<std::shared_ptr<const Type>> operandTypes,
207  const std::vector<rvsdg::Output *> & operands,
208  std::vector<std::shared_ptr<const Type>> resultTypes)
209  {
210  return operands.empty() ? &rvsdg::CreateOpNode<TestOperation>(
211  *region,
212  std::move(operandTypes),
213  std::move(resultTypes))
214  : &rvsdg::CreateOpNode<TestOperation>(
215  { operands },
216  std::move(operandTypes),
217  std::move(resultTypes));
218  }
219 };
220 
221 }
222 
223 #endif
Nullary operator (operator taking no formal arguments)
Definition: nullary.hpp:22
Represent acyclic RVSDG subgraphs.
Definition: region.hpp:213
const std::shared_ptr< const rvsdg::Type > & result(size_t index) const noexcept
Definition: operation.cpp:36
binop_reduction_path_t can_reduce_operand_pair(const Output *op1, const Output *op2) const noexcept override
~TestBinaryOperation() noexcept override
std::unique_ptr< Operation > copy() const override
bool operator==(const Operation &other) const noexcept override
static Output * create_normalized(const std::shared_ptr< const Type > operandType, std::shared_ptr< const Type > resultType, Output *op1, Output *op2)
static Node * create(const std::shared_ptr< const Type > &operandType, std::shared_ptr< const Type > resultType, Output *op1, Output *op2)
enum BinaryOperation::flags flags_
Output * reduce_operand_pair(unop_reduction_path_t path, Output *op1, Output *op2) const override
enum BinaryOperation::flags flags() const noexcept override
std::string debug_string() const override
bool operator==(const Operation &other) const noexcept override
std::unique_ptr< Operation > copy() const override
TestNullaryOperation(const std::shared_ptr< const Type > &resultType)
std::string debug_string() const override
static std::unique_ptr< TestOperation > create(std::vector< std::shared_ptr< const Type >> operandTypes, std::vector< std::shared_ptr< const Type >> resultTypes)
static SimpleNode * createNode(Region *region, const std::vector< Output * > &operands, std::vector< std::shared_ptr< const Type >> resultTypes)
std::string debug_string() const override
std::unique_ptr< Operation > copy() const override
~TestOperation() noexcept override
TestOperation(const TestOperation &)=default
bool operator==(const Operation &other) const noexcept override
static SimpleNode * createNode(Region *region, std::vector< std::shared_ptr< const Type >> operandTypes, const std::vector< rvsdg::Output * > &operands, std::vector< std::shared_ptr< const Type >> resultTypes)
~TestUnaryOperation() noexcept override
bool operator==(const Operation &other) const noexcept override
static Node * create(Region *, std::shared_ptr< const Type > operandType, Output *operand, std::shared_ptr< const Type > resultType)
std::unique_ptr< Operation > copy() const override
Output * reduce_operand(unop_reduction_path_t path, Output *operand) const override
std::string debug_string() const override
unop_reduction_path_t can_reduce_operand(const Output *operand) const noexcept override
static Output * create_normalized(std::shared_ptr< const Type > operandType, Output *operand, std::shared_ptr< const Type > resultType)
Unary operator.
Definition: unary.hpp:26
size_t unop_reduction_path_t
Definition: unary.hpp:18
size_t binop_reduction_path_t
Definition: binary.hpp:19
static std::vector< jlm::rvsdg::Output * > operands(const Node *node)
Definition: node.hpp:1049