6#include <gtest/gtest.h>
19TEST(ConversionOperationsTests, SExtConstantFolding)
34 auto & x1 = GraphExport::Create(*node1.output(0),
"x1");
35 auto & x2 = GraphExport::Create(*node2.output(0),
"x2");
49 auto [_, op] = TryGetSimpleNodeAndOptionalOp<IntegerConstantOperation>(*x1.origin());
51 EXPECT_EQ(op->Representation().to_uint(), 0u);
52 EXPECT_EQ(op->Representation().nbits(), 32u);
56 auto [_, op] = TryGetSimpleNodeAndOptionalOp<IntegerConstantOperation>(*x2.origin());
58 EXPECT_EQ(op->Representation().to_int(), -1);
59 EXPECT_EQ(op->Representation().nbits(), 32u);
63TEST(ConversionOperationsTests, ZExtConstantFolding)
78 auto & x1 = GraphExport::Create(*node1.output(0),
"x1");
79 auto & x2 = GraphExport::Create(*node2.output(0),
"x2");
93 auto [_, op] = TryGetSimpleNodeAndOptionalOp<IntegerConstantOperation>(*x1.origin());
95 EXPECT_EQ(op->Representation().to_uint(), 0u);
96 EXPECT_EQ(op->Representation().nbits(), 32u);
100 auto [_, op] = TryGetSimpleNodeAndOptionalOp<IntegerConstantOperation>(*x2.origin());
102 EXPECT_EQ(op->Representation().to_int(), 1);
103 EXPECT_EQ(op->Representation().nbits(), 32u);
107TEST(ConversionOperationsTests, TruncConstantFolding)
112 auto i1Type = BitType::Create(1);
124 auto & x1 = GraphExport::Create(*node1.output(0),
"x1");
125 auto & x2 = GraphExport::Create(*node2.output(0),
"x2");
139 auto [_, op] = TryGetSimpleNodeAndOptionalOp<IntegerConstantOperation>(*x1.origin());
141 EXPECT_EQ(op->Representation().to_uint(), 0u);
142 EXPECT_EQ(op->Representation().nbits(), 1u);
146 auto [_, op] = TryGetSimpleNodeAndOptionalOp<IntegerConstantOperation>(*x2.origin());
148 EXPECT_EQ(op->Representation().to_uint(), 1u);
149 EXPECT_EQ(op->Representation().nbits(), 1u);
153TEST(ConversionOperationsTests, fpExtConstantFolding)
169 auto & x1 = GraphExport::Create(*node1.output(0),
"x1");
170 auto & x2 = GraphExport::Create(*node2.output(0),
"x2");
184 auto [_, op] = TryGetSimpleNodeAndOptionalOp<ConstantFP>(*x1.origin());
186 EXPECT_EQ(&op->constant().getSemantics(), &::llvm::APFloat::IEEEdouble());
187 EXPECT_EQ(op->constant().convertToDouble(), 8.0);
191 auto [_, op] = TryGetSimpleNodeAndOptionalOp<ConstantFP>(*x2.origin());
193 EXPECT_EQ(&op->constant().getSemantics(), &::llvm::APFloat::IEEEsingle());
194 EXPECT_EQ(op->constant().convertToFloat(), 1.0);
198TEST(ConversionOperationsTests, fpTruncConstantFolding)
211 auto & x1 = GraphExport::Create(*node1.output(0),
"x1");
224 auto [_, op] = TryGetSimpleNodeAndOptionalOp<ConstantFP>(*x1.origin());
226 EXPECT_EQ(&op->constant().getSemantics(), &::llvm::APFloat::IEEEsingle());
227 EXPECT_EQ(op->constant().convertToDouble(), 8.0);
231TEST(ConversionOperationsTests, FunctionToPointerInversion)
236 auto valueType = TestType::createValueType();
238 auto functionType = FunctionType::Create({ valueType }, { valueType });
242 auto & i0 = GraphImport::Create(graph, pointerType,
"i0");
243 auto & i1 = GraphImport::Create(graph, functionType,
"i1");
247 auto & fnToPtrNode1 =
252 auto & x1 = GraphExport::Create(*fnToPtrNode1.output(0),
"x1");
253 auto & x2 = GraphExport::Create(*fnToPtrNode2.output(0),
"x2");
258 ReduceNode<FunctionToPointerOperation>(
262 ReduceNode<FunctionToPointerOperation>(
273 EXPECT_EQ(x1.origin(), &i0);
278 auto [_, op] = TryGetSimpleNodeAndOptionalOp<FunctionToPointerOperation>(*x2.origin());
279 EXPECT_NE(op,
nullptr);
283TEST(ConversionOperationsTests, PointerToFunctionInversion)
288 auto stateType = TestType::createStateType();
289 auto valueType = TestType::createValueType();
291 auto functionType1 = FunctionType::Create({ valueType }, { valueType });
292 auto functionType2 = FunctionType::Create({ stateType }, { stateType });
296 auto & i0 = GraphImport::Create(graph, functionType1,
"i0");
300 auto & ptrToFnNode1 =
303 auto & ptrToFnNode2 =
306 auto & x1 = GraphExport::Create(*ptrToFnNode1.output(0),
"x1");
307 auto & x2 = GraphExport::Create(*ptrToFnNode2.output(0),
"x2");
312 ReduceNode<PointerToFunctionOperation>(
316 ReduceNode<PointerToFunctionOperation>(
327 EXPECT_EQ(x1.origin(), &i0);
333 auto [_, op] = TryGetSimpleNodeAndOptionalOp<PointerToFunctionOperation>(*x2.origin());
334 EXPECT_NE(op,
nullptr);
static rvsdg::Node & createNode(rvsdg::Region ®ion, fpsize size, const ::llvm::APFloat &constant)
static std::optional< std::vector< rvsdg::Output * > > foldConstant(const FPExtOperation &operation, const std::vector< rvsdg::Output * > &operands)
static rvsdg::SimpleNode & createNode(rvsdg::Output &operand, const std::shared_ptr< const rvsdg::Type > &resultType)
static std::optional< std::vector< rvsdg::Output * > > foldConstant(const FPTruncOperation &operation, const std::vector< rvsdg::Output * > &operands)
static rvsdg::SimpleNode & createNode(rvsdg::Output &operand, const std::shared_ptr< const rvsdg::Type > &resultType)
static std::shared_ptr< const FloatingPointType > Create(fpsize size)
static std::optional< std::vector< rvsdg::Output * > > invertFunctionToPointer(const FunctionToPointerOperation &operation, const std::vector< rvsdg::Output * > &operands)
static rvsdg::Node & Create(rvsdg::Region ®ion, IntegerValueRepresentation representation)
static std::optional< std::vector< rvsdg::Output * > > invertPointerToFunction(const PointerToFunctionOperation &operation, const std::vector< rvsdg::Output * > &operands)
static std::shared_ptr< const PointerType > Create()
static std::optional< std::vector< rvsdg::Output * > > foldConstant(const SExtOperation &operation, const std::vector< rvsdg::Output * > &operands)
static rvsdg::SimpleNode & createNode(const size_t numResultBits, rvsdg::Output &operand)
static rvsdg::SimpleNode & createNode(rvsdg::Output &operand, std::shared_ptr< const rvsdg::Type > resultType)
static std::optional< std::vector< rvsdg::Output * > > foldConstant(const TruncOperation &operation, const std::vector< rvsdg::Output * > &operands)
static std::optional< std::vector< rvsdg::Output * > > foldConstant(const ZExtOperation &operation, const std::vector< rvsdg::Output * > &operands)
static rvsdg::SimpleNode & createNode(const size_t numResultBits, rvsdg::Output &operand)
Region & GetRootRegion() const noexcept
Global memory state passed between functions.
TEST(ControlOperationsTests, foldConstants)
NodeType * TryGetOwnerNode(const rvsdg::Input &input) noexcept
Checks if this is an input to a node of specified type.