Jlm
Loading...
Searching...
No Matches
ControlOperationsTests.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2026 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/control.hpp>
12#include <jlm/rvsdg/view.hpp>
13
14namespace jlm::llvm
15{
16
17TEST(ControlOperationsTests, foldConstants)
18{
19 using namespace jlm::llvm;
20 using namespace jlm::rvsdg;
21
22 // Arrange
23 Graph graph;
24 auto bitType32 = BitType::Create(32);
25
26 auto & zeroNode =
28 auto & matchNode1 = MatchOperation::CreateNode(*zeroNode.output(0), { { 0, 0 } }, 1, 2);
29
30 auto & fourNode =
31 IntegerConstantOperation::Create(graph.GetRootRegion(), BitValueRepresentation(32, 4));
32 auto & matchNode2 = MatchOperation::CreateNode(*fourNode.output(0), { { 0, 0 } }, 1, 2);
33
34 auto & x1 = GraphExport::Create(*matchNode1.output(0), "x1");
35 auto & x2 = GraphExport::Create(*matchNode2.output(0), "x2");
36
37 view(graph, stdout);
38
39 // Act
40 ReduceNode<MatchOperation>(
41 foldMatchOperationWithConstant,
42 dynamic_cast<SimpleNode &>(matchNode1));
43
44 ReduceNode<MatchOperation>(
45 foldMatchOperationWithConstant,
46 dynamic_cast<SimpleNode &>(matchNode2));
47
48 graph.PruneNodes();
49
50 view(graph, stdout);
51
52 // Assert
53 {
54 auto [_, op] = TryGetSimpleNodeAndOptionalOp<ControlConstantOperation>(*x1.origin());
55 EXPECT_TRUE(op);
56 EXPECT_EQ(op->value().nalternatives(), 2u);
57 EXPECT_EQ(op->value().alternative(), 0u);
58 }
59
60 {
61 auto [_, op] = TryGetSimpleNodeAndOptionalOp<ControlConstantOperation>(*x2.origin());
62 EXPECT_TRUE(op);
63 EXPECT_EQ(op->value().nalternatives(), 2u);
64 EXPECT_EQ(op->value().alternative(), 1u);
65 }
66}
67
68}
static rvsdg::Node & Create(rvsdg::Region &region, IntegerValueRepresentation representation)
Region & GetRootRegion() const noexcept
Definition graph.hpp:99
Global memory state passed between functions.
TEST(ControlOperationsTests, foldConstants)