Jlm
SExtTests.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #include <gtest/gtest.h>
7 
9 #include <jlm/rvsdg/view.hpp>
10 
14 
15 TEST(SExtOperationTests, test_bitunary_reduction)
16 {
17  using namespace jlm::llvm;
18  using namespace jlm::rvsdg;
19 
20  // Arrange
21  Graph graph;
22  auto bitType32 = BitType::Create(32);
23 
24  auto x = &jlm::rvsdg::GraphImport::Create(graph, bitType32, "x");
25 
26  auto y = bitnot_op::create(32, x);
27  auto z = jlm::llvm::SExtOperation::create(64, y);
28 
29  auto & ex = GraphExport::Create(*z, "x");
30 
31  view(graph, stdout);
32 
33  // Act
34  ReduceNode<SExtOperation>(
36  *jlm::rvsdg::TryGetOwnerNode<jlm::rvsdg::SimpleNode>(*ex.origin()));
37  graph.PruneNodes();
38 
39  view(graph, stdout);
40 
41  // Assert
42  EXPECT_TRUE(is<bitnot_op>(jlm::rvsdg::TryGetOwnerNode<jlm::rvsdg::Node>(*ex.origin())));
43 }
44 
45 TEST(SExtOperationTests, test_bitbinary_reduction)
46 {
47  using namespace jlm::llvm;
48  using namespace jlm::rvsdg;
49 
50  // Arrange
51  Graph graph;
52  auto bt32 = BitType::Create(32);
53 
54  auto x = &jlm::rvsdg::GraphImport::Create(graph, bt32, "x");
55  auto y = &jlm::rvsdg::GraphImport::Create(graph, bt32, "y");
56 
57  auto z = bitadd_op::create(32, x, y);
58  auto w = SExtOperation::create(64, z);
59 
60  auto & ex = GraphExport::Create(*w, "x");
61 
62  view(graph, stdout);
63 
64  // Act
65  ReduceNode<SExtOperation>(
67  *jlm::rvsdg::TryGetOwnerNode<jlm::rvsdg::SimpleNode>(*ex.origin()));
68  graph.PruneNodes();
69 
70  view(graph, stdout);
71 
72  // Assert
73  EXPECT_TRUE(is<bitadd_op>(jlm::rvsdg::TryGetOwnerNode<jlm::rvsdg::Node>(*ex.origin())));
74 }
75 
76 TEST(SExtOperationTests, test_inverse_reduction)
77 {
78  using namespace jlm::llvm;
79  using namespace jlm::rvsdg;
80 
81  // Arrange
82  Graph graph;
83  auto bt64 = BitType::Create(64);
84 
85  auto x = &jlm::rvsdg::GraphImport::Create(graph, bt64, "x");
86 
87  auto y = TruncOperation::create(32, x);
88  auto z = SExtOperation::create(64, y);
89 
90  auto & ex = GraphExport::Create(*z, "x");
91 
92  view(graph, stdout);
93 
94  // Act
95  ReduceNode<SExtOperation>(
97  *jlm::rvsdg::TryGetOwnerNode<jlm::rvsdg::SimpleNode>(*ex.origin()));
98  graph.PruneNodes();
99 
100  view(graph, stdout);
101 
102  // Assert
103  EXPECT_EQ(ex.origin(), x);
104 }
TEST(SExtOperationTests, test_bitunary_reduction)
Definition: SExtTests.cpp:15
static std::unique_ptr< llvm::ThreeAddressCode > create(const Variable *operand, const std::shared_ptr< const rvsdg::Type > &type)
Definition: sext.hpp:75
static std::unique_ptr< llvm::ThreeAddressCode > create(const Variable *operand, const std::shared_ptr< const jlm::rvsdg::Type > &type)
Definition: operators.hpp:1745
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
Global memory state passed between functions.
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