Jlm
UnaryOperationTests.cpp
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 #include <gtest/gtest.h>
7 
10 #include <jlm/rvsdg/TestType.hpp>
11 #include <jlm/rvsdg/unary.hpp>
12 #include <jlm/rvsdg/view.hpp>
13 
15 {
16 public:
18  const std::shared_ptr<const jlm::rvsdg::Type> & operandType,
19  const std::shared_ptr<const jlm::rvsdg::Type> & resultType)
20  : jlm::rvsdg::UnaryOperation(operandType, resultType)
21  {}
22 
24  can_reduce_operand(const jlm::rvsdg::Output * operand) const noexcept override
25  {
26 
27  if (const auto node = jlm::rvsdg::TryGetOwnerNode<jlm::rvsdg::SimpleNode>(*operand);
28  node && jlm::rvsdg::is<jlm::rvsdg::TestNullaryOperation>(node->GetOperation()))
29  {
31  }
32 
34  }
35 
38  const override
39  {
41  {
42  return operand;
43  }
44 
45  return nullptr;
46  }
47 
48  bool
49  operator==(const Operation &) const noexcept override
50  {
51  JLM_UNREACHABLE("Not implemented.");
52  }
53 
54  [[nodiscard]] std::string
55  debug_string() const override
56  {
57  return "UnaryOperation";
58  }
59 
60  [[nodiscard]] std::unique_ptr<Operation>
61  copy() const override
62  {
63  return std::make_unique<UnaryOperation>(this->argument(0), this->result(0));
64  }
65 };
66 
67 TEST(ArgumentTests, NormalizeUnaryOperation_Success)
68 {
69  using namespace jlm::rvsdg;
70 
71  // Arrange
72  Graph graph;
73  const auto valueType = TestType::createValueType();
74 
75  const auto nullaryNode = &CreateOpNode<TestNullaryOperation>(graph.GetRootRegion(), valueType);
76 
77  const auto unaryNode =
78  &CreateOpNode<::UnaryOperation>({ nullaryNode->output(0) }, valueType, valueType);
79 
80  auto & ex = GraphExport::Create(*unaryNode->output(0), "o2");
81 
82  view(graph, stdout);
83 
84  // Act
85  const auto success = ReduceNode<::UnaryOperation>(NormalizeUnaryOperation, *unaryNode);
86  view(graph, stdout);
87 
88  // Assert
89  EXPECT_TRUE(success);
90 
91  graph.PruneNodes();
92  EXPECT_EQ(graph.GetRootRegion().numNodes(), 1u);
93 
94  const auto node = TryGetOwnerNode<SimpleNode>(*ex.origin());
95  EXPECT_EQ(node, nullaryNode);
96 }
97 
98 TEST(ArgumentTests, NormalizeUnaryOperation_Failure)
99 {
100  using namespace jlm::rvsdg;
101 
102  // Arrange
103  const auto valueType = TestType::createValueType();
104 
105  Graph graph;
106  auto i0 = &GraphImport::Create(graph, valueType, "i0");
107 
108  const auto unaryNode = &CreateOpNode<::UnaryOperation>({ i0 }, valueType, valueType);
109 
110  auto & ex = GraphExport::Create(*unaryNode->output(0), "o2");
111 
112  view(graph, stdout);
113 
114  // Act
115  const auto success = ReduceNode<::UnaryOperation>(NormalizeUnaryOperation, *unaryNode);
116  view(graph, stdout);
117 
118  // Assert
119  EXPECT_FALSE(success);
120 
121  graph.PruneNodes();
122  EXPECT_EQ(graph.GetRootRegion().numNodes(), 1u);
123 
124  const auto node = TryGetOwnerNode<SimpleNode>(*ex.origin());
125  EXPECT_EQ(node, unaryNode);
126 }
TEST(ArgumentTests, NormalizeUnaryOperation_Success)
jlm::rvsdg::unop_reduction_path_t can_reduce_operand(const jlm::rvsdg::Output *operand) const noexcept override
std::string debug_string() const override
std::unique_ptr< Operation > copy() const override
bool operator==(const Operation &) const noexcept override
jlm::rvsdg::Output * reduce_operand(jlm::rvsdg::unop_reduction_path_t path, jlm::rvsdg::Output *operand) const override
UnaryOperation(const std::shared_ptr< const jlm::rvsdg::Type > &operandType, const std::shared_ptr< const jlm::rvsdg::Type > &resultType)
static GraphExport & Create(Output &origin, std::string name)
Definition: graph.cpp:62
static GraphImport & Create(Graph &graph, std::shared_ptr< const rvsdg::Type > type, std::string name)
Definition: graph.cpp:36
void PruneNodes()
Definition: graph.hpp:116
Region & GetRootRegion() const noexcept
Definition: graph.hpp:99
size_t numNodes() const noexcept
Definition: region.hpp:481
const std::shared_ptr< const rvsdg::Type > & argument(size_t index) const noexcept
Definition: operation.cpp:23
const std::shared_ptr< const rvsdg::Type > & result(size_t index) const noexcept
Definition: operation.cpp:36
static std::shared_ptr< const TestType > createValueType()
Definition: TestType.cpp:67
Unary operator.
Definition: unary.hpp:26
#define JLM_UNREACHABLE(msg)
Definition: common.hpp:43
size_t unop_reduction_path_t
Definition: unary.hpp:18
static const unop_reduction_path_t unop_reduction_constant
Definition: unary.hpp:45
std::string view(const rvsdg::Region *region)
Definition: view.cpp:142
std::optional< std::vector< rvsdg::Output * > > NormalizeUnaryOperation(const UnaryOperation &operation, const std::vector< rvsdg::Output * > &operands)
Applies the reductions implemented in the unary operations reduction functions.
Definition: unary.cpp:17
static const unop_reduction_path_t unop_reduction_none
Definition: unary.hpp:43