1 /**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "unit_test.h"
17 #include "optimizer/ir/graph_cloner.h"
18 #include "optimizer/optimizations/loop_unroll.h"
19 #include "optimizer/optimizations/loop_peeling.h"
20
21 namespace panda::compiler {
22 class GraphCreationTest : public CommonTest {
23 };
24
TEST_F(GraphCreationTest,CreateEmptyGraph)25 TEST_F(GraphCreationTest, CreateEmptyGraph)
26 {
27 Graph *graph = CreateEmptyGraph();
28 EXPECT_NE(graph, nullptr);
29 }
30
TEST_F(GraphCreationTest,CreateGraphStartEndBlocks)31 TEST_F(GraphCreationTest, CreateGraphStartEndBlocks)
32 {
33 Graph *graph = CreateGraphStartEndBlocks();
34 EXPECT_NE(graph, nullptr);
35 EXPECT_NE(graph->GetStartBlock(), nullptr);
36 EXPECT_NE(graph->GetEndBlock(), nullptr);
37 EXPECT_EQ(graph->GetAliveBlocksCount(), 2U);
38 }
39
TEST_F(GraphCreationTest,OsrModeGraph)40 TEST_F(GraphCreationTest, OsrModeGraph)
41 {
42 auto graph = CreateEmptyGraph();
43 GRAPH(graph)
44 {
45 PARAMETER(0, 0).u64();
46 PARAMETER(1, 1).u64();
47 PARAMETER(2, 2).u64();
48 BASIC_BLOCK(2, 3, 4)
49 {
50 INST(3, Opcode::Phi).u64().Inputs(1, 5);
51 INST(4, Opcode::Phi).u64().Inputs(2, 10);
52 INST(5, Opcode::Sub).u64().Inputs(3, 2);
53 INST(6, Opcode::SafePoint).Inputs(0, 3, 4).SrcVregs({0, 1, 2});
54 INST(7, Opcode::Compare).CC(CC_EQ).b().Inputs(5, 0);
55 INST(8, Opcode::IfImm).SrcType(DataType::BOOL).CC(CC_NE).Imm(0).Inputs(7);
56 }
57 BASIC_BLOCK(3, 2)
58 {
59 INST(9, Opcode::And).u64().Inputs(4, 5);
60 INST(10, Opcode::Add).u64().Inputs(9, 4);
61 }
62 BASIC_BLOCK(4, -1)
63 {
64 INST(11, Opcode::Return).u64().Inputs(4);
65 }
66 }
67 BB(2).SetOsrEntry(true);
68 auto clone_graph = GraphCloner(graph, graph->GetAllocator(), graph->GetLocalAllocator()).CloneGraph();
69
70 graph->RunPass<LoopPeeling>();
71 graph->RunPass<LoopUnroll>(1000, 4);
72 EXPECT_TRUE(GraphComparator().Compare(graph, clone_graph));
73 }
74 } // namespace panda::compiler
75