• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "common.h"
17 #include "codegen.h"
18 #include "optimize_bytecode.h"
19 #include "mangling.h"
20 
21 namespace panda::bytecodeopt::test {
22 
TEST_F(AsmTest,BitopsBitwiseAnd)23 TEST_F(AsmTest, BitopsBitwiseAnd)
24 {
25     // naive translation of bitops-bitwise-and benchmark
26     auto source = R"(
27     .function u1 main() {
28     movi.64 v0, 0x100000000
29     mov.64 v4, v0
30     movi v0, 0x1
31     mov v6, v0
32     label_1: mov v0, v6
33     movi v1, 0x5f5e100
34     lda v0
35     jge v1, label_0
36     mov.64 v0, v4
37     mov v2, v6
38     lda v2
39     i32toi64
40     sta.64 v2
41     lda.64 v0
42     and2.64 v2
43     sta.64 v0
44     mov.64 v4, v0
45     inci v6, 0x1
46     jmp label_1
47     label_0: mov.64 v0, v4
48     mov.64 v6, v0
49     movi.64 v0, 0x0
50     mov.64 v3, v0
51     mov.64 v0, v6
52     lda.64 v0
53     cmp.64 v3
54     sta v0
55     lda v0
56     jeqz label_2
57     movi v0, 0x1
58     lda v0
59     return
60     label_2: movi v0, 0x2
61     lda v0
62     return
63     }
64     )";
65 
66     pandasm::Parser parser;
67     auto res = parser.Parse(source);
68     ASSERT_TRUE(res);
69     auto &program = res.Value();
70 
71     ASSERT_TRUE(ParseToGraph(&program, "main"));
72 
73     EXPECT_TRUE(RunOptimizations(GetGraph()));
74 
75     auto expected = CreateEmptyGraph();
76     GRAPH(expected)
77     {
78         using namespace compiler::DataType;
79 
80         BASIC_BLOCK(2, 3)
81         {
82             CONSTANT(23, 0x5f5e100).s32();
83             CONSTANT(2, 1).s32();
84             CONSTANT(1, 0x100000000).s64();
85             INST(0, Opcode::SaveStateDeoptimize).NoVregs();
86             INST(26, Opcode::SpillFill);
87         }
88         BASIC_BLOCK(3, 5, 4)
89         {
90             INST(4, Opcode::Phi).s64().Inputs(1, 10);
91             INST(5, Opcode::Phi).s32().Inputs(2, 20);
92             INST(19, Opcode::If).CC(compiler::CC_GE).SrcType(INT32).Inputs(5, 23);
93         }
94         BASIC_BLOCK(4, 3)
95         {
96             INST(9, Opcode::Cast).s64().SrcType(INT32).Inputs(5);
97             INST(10, Opcode::And).s64().Inputs(9, 4);
98             INST(20, Opcode::AddI).s32().Inputs(5).Imm(1);
99         }
100         BASIC_BLOCK(5, 7, 6)
101         {
102             CONSTANT(22, 0).s64();
103             INST(13, Opcode::Cmp).s32().Inputs(4, 22);
104             INST(15, Opcode::IfImm).SrcType(INT32).CC(compiler::CC_EQ).Imm(0).Inputs(13);
105         }
106         BASIC_BLOCK(6, -1)
107         {
108             INST(16, Opcode::Return).b().Inputs(2);
109         }
110         BASIC_BLOCK(7, -1)
111         {
112             CONSTANT(21, 2).s32();
113             INST(18, Opcode::Return).b().Inputs(21);
114         }
115     }
116     EXPECT_TRUE(GraphComparator().Compare(GetGraph(), expected));
117 
118     const auto sig_main = pandasm::GetFunctionSignatureFromName("main", {});
119 
120     auto &function = program.function_table.at(sig_main);
121     EXPECT_TRUE(GetGraph()->RunPass<BytecodeGen>(&function, GetIrInterface()));
122 }
123 
124 }  // namespace panda::bytecodeopt::test
125