47 auto & node1 = Operation::createNode(32, *
c1.output(0), *
c2.output(0));
49 auto & x1 = GraphExport::Create(*node1.output(0),
"x1");
54 ReduceNode<Operation>(Operation::foldConstants,
dynamic_cast<SimpleNode &
>(node1));
62 auto [_, op] = TryGetSimpleNodeAndOptionalOp<IntegerConstantOperation>(*x1.origin());
64 EXPECT_EQ(op->Representation().to_int(), input.expected);
65 EXPECT_EQ(op->Representation().nbits(),
static_cast<size_t>(input.numBitsExpected));
76 auto iType = BitType::Create(32);
80 auto & i0 = GraphImport::Create(graph, iType,
"i0");
81 auto & i1 = GraphImport::Create(graph, iType,
"i1");
83 auto & node1 = Operation::createNode(32, i0, i0);
84 auto & node2 = Operation::createNode(32, i0, i1);
86 auto & x1 = GraphExport::Create(*node1.output(0),
"x1");
87 auto & x2 = GraphExport::Create(*node2.output(0),
"x2");
90 ReduceNode<Operation>(Operation::normalizeIdenticalOperands,
dynamic_cast<SimpleNode &
>(node1));
91 ReduceNode<Operation>(Operation::normalizeIdenticalOperands,
dynamic_cast<SimpleNode &
>(node2));
97 auto [_, op] = TryGetSimpleNodeAndOptionalOp<IntegerConstantOperation>(*x1.origin());
99 EXPECT_EQ(op->Representation().nbits(), 1u);
100 EXPECT_EQ(op->Representation().to_uint(),
expected);
104 auto [node, op] = TryGetSimpleNodeAndOptionalOp<Operation>(*x2.origin());
106 EXPECT_EQ(node, &node2);
110TEST(IntegerEqOperationTest, foldConstants)
112 TestFoldConstants<IntegerEqOperation>({ 4, 32, 4, 32, -1, 1 });
113 TestFoldConstants<IntegerEqOperation>({ 4, 32, -4, 32, 0, 1 });
116TEST(IntegerNeOperationTest, foldConstants)
118 TestFoldConstants<IntegerNeOperation>({ 4, 32, 4, 32, 0, 1 });
119 TestFoldConstants<IntegerNeOperation>({ 4, 32, -4, 32, -1, 1 });
122TEST(IntegerSgeOperationTest, foldConstants)
124 TestFoldConstants<IntegerSgeOperation>({ 4, 32, 4, 32, -1, 1 });
125 TestFoldConstants<IntegerSgeOperation>({ 4, 32, -4, 32, -1, 1 });
128TEST(IntegerSgtOperationTest, foldConstants)
130 TestFoldConstants<IntegerSgtOperation>({ 4, 32, 4, 32, 0, 1 });
131 TestFoldConstants<IntegerSgtOperation>({ 4, 32, -4, 32, -1, 1 });
134TEST(IntegerSleOperationTest, foldConstants)
136 TestFoldConstants<IntegerSleOperation>({ 4, 32, 4, 32, -1, 1 });
137 TestFoldConstants<IntegerSleOperation>({ 4, 32, -4, 32, 0, 1 });
140TEST(IntegerSltOperationTest, foldConstants)
142 TestFoldConstants<IntegerSltOperation>({ 4, 32, 4, 32, 0, 1 });
143 TestFoldConstants<IntegerSltOperation>({ 4, 32, -4, 32, 0, 1 });
146TEST(IntegerUgeOperationTest, foldConstants)
148 TestFoldConstants<IntegerUgeOperation>({ 4, 32, 4, 32, -1, 1 });
149 TestFoldConstants<IntegerUgeOperation>({ 4, 32, -4, 32, 0, 1 });
152TEST(IntegerUgtOperationTest, foldConstants)
154 TestFoldConstants<IntegerUgtOperation>({ 4, 32, 4, 32, 0, 1 });
155 TestFoldConstants<IntegerUgtOperation>({ 4, 32, -4, 32, 0, 1 });
158TEST(IntegerUleOperationTest, foldConstants)
160 TestFoldConstants<IntegerUleOperation>({ 4, 32, 4, 32, -1, 1 });
161 TestFoldConstants<IntegerUleOperation>({ 4, 32, -4, 32, -1, 1 });
164TEST(IntegerUltOperationTest, foldConstants)
166 TestFoldConstants<IntegerUltOperation>({ 4, 32, 4, 32, 0, 1 });
167 TestFoldConstants<IntegerUltOperation>({ 4, 32, -4, 32, -1, 1 });
170TEST(IntegerOrOperationTest, foldConstants)
172 TestFoldConstants<IntegerOrOperation>({ 4, 32, 4, 32, 4, 32 });
173 TestFoldConstants<IntegerOrOperation>({ 0, 32, -1, 32, -1, 32 });
174 TestFoldConstants<IntegerOrOperation>({ 1, 32, 2, 32, 3, 32 });
177TEST(IntegerOrOperationTest, normalizeIdempotent)
182 auto i32Type = BitType::Create(32);
186 auto & i0 = GraphImport::Create(graph, i32Type,
"i0");
196 auto & x1 = GraphExport::Create(*subNode1.output(0),
"x1");
197 auto & x2 = GraphExport::Create(*subNode2.output(0),
"x2");
198 auto & x3 = GraphExport::Create(*subNode3.output(0),
"x3");
199 auto & x4 = GraphExport::Create(*subNode4.output(0),
"x4");
202 ReduceNode<IntegerOrOperation>(
205 ReduceNode<IntegerOrOperation>(
208 ReduceNode<IntegerOrOperation>(
211 ReduceNode<IntegerOrOperation>(
220 EXPECT_EQ(x1.origin(), &i0);
221 EXPECT_EQ(x2.origin(), &i0);
222 EXPECT_EQ(x3.origin(), subNode3.output(0));
223 EXPECT_EQ(x4.origin(), subNode4.output(0));
226TEST(IntegerAndOperationTest, foldConstants)
228 TestFoldConstants<IntegerAndOperation>({ 4, 32, 4, 32, 4, 32 });
229 TestFoldConstants<IntegerAndOperation>({ 0, 32, -1, 32, 0, 32 });
232TEST(IntegerXorOperationTest, foldConstants)
234 TestFoldConstants<IntegerXorOperation>({ 4, 32, 4, 32, 0, 32 });
235 TestFoldConstants<IntegerXorOperation>({ 0, 32, -1, 32, -1, 32 });
238TEST(IntegerAddOperationTest, foldConstants)
240 TestFoldConstants<IntegerAddOperation>({ 12, 32, 7, 32, 19, 32 });
241 TestFoldConstants<IntegerAddOperation>({ -1, 32, 2, 32, 1, 32 });
244TEST(IntegerSubOperationTest, foldConstants)
246 TestFoldConstants<IntegerSubOperation>({ 12, 32, 7, 32, 5, 32 });
247 TestFoldConstants<IntegerSubOperation>({ 0, 32, 1, 32, -1, 32 });
250TEST(IntegerMulOperationTest, foldConstants)
252 TestFoldConstants<IntegerMulOperation>({ 6, 32, 7, 32, 42, 32 });
253 TestFoldConstants<IntegerMulOperation>({ -6, 32, 7, 32, -42, 32 });
256TEST(IntegerSDivOperationTest, foldConstants)
258 TestFoldConstants<IntegerSDivOperation>({ -13, 32, 3, 32, -4, 32 });
259 TestFoldConstants<IntegerSDivOperation>({ 13, 32, -3, 32, -4, 32 });
262TEST(IntegerUDivOperationTest, foldConstants)
264 TestFoldConstants<IntegerUDivOperation>({ 13, 32, 3, 32, 4, 32 });
265 TestFoldConstants<IntegerUDivOperation>({ -1, 32, 2, 32, 2147483647, 32 });
268TEST(IntegerSRemOperationTest, foldConstants)
270 TestFoldConstants<IntegerSRemOperation>({ -13, 32, 3, 32, -1, 32 });
271 TestFoldConstants<IntegerSRemOperation>({ 13, 32, -3, 32, 1, 32 });
274TEST(IntegerURemOperationTest, foldConstants)
276 TestFoldConstants<IntegerURemOperation>({ 13, 32, 3, 32, 1, 32 });
277 TestFoldConstants<IntegerURemOperation>({ -1, 32, 10, 32, 5, 32 });
280TEST(IntegerAShrOperationTest, foldConstants)
282 TestFoldConstants<IntegerAShrOperation>({ -0x10, 32, 2, 32, -0x4, 32 });
283 TestFoldConstants<IntegerAShrOperation>({ 0x10, 32, 2, 32, 0x4, 32 });
286TEST(IntegerShlOperationTest, foldConstants)
288 TestFoldConstants<IntegerShlOperation>({ 0x3, 32, 2, 32, 0xc, 32 });
289 TestFoldConstants<IntegerShlOperation>({ 0x400, 32, 2, 32, 0x1000, 32 });
292TEST(IntegerLShrOperationTest, foldConstants)
294 TestFoldConstants<IntegerLShrOperation>({ -0x1, 32, 2, 32, 0x3fffffff, 32 });
295 TestFoldConstants<IntegerLShrOperation>({ 0x10, 32, 2, 32, 0x4, 32 });
298TEST(IntegerSubOperationTests, normalizeAdditiveInverse)
303 auto i32Type = BitType::Create(32);
307 auto & i0 = GraphImport::Create(graph, i32Type,
"i0");
309 auto structuralNode = TestStructuralNode::create(&graph.
GetRootRegion(), 1);
310 auto inputVar = structuralNode->addInputWithArguments(i0);
315 auto outputVar = structuralNode->addOutputWithResults({ subNode.output(0) });
317 GraphExport::Create(*outputVar.output,
"x0");
320 ReduceNode<IntegerSubOperation>(
331 TryGetSimpleNodeAndOptionalOp<IntegerConstantOperation>(*outputVar.result[0]->origin());
333 EXPECT_EQ(op->Representation().to_int(), 0u);
334 EXPECT_EQ(op->Representation().nbits(), 32u);