• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023 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 <iomanip>
17 #include <iostream>
18 #include <sstream>
19 #include <string>
20 #include <cstring>
21 #include <gtest/gtest.h>
22 #include <memory>
23 
24 #include "basicblock.h"
25 #include "bytecode_optimizer/reg_encoder.h"
26 #include "bytecode_optimizer/reg_acc_alloc.h"
27 #include "graph.h"
28 #include "graph_test.h"
29 #include "inst.h"
30 #include "locations.h"
31 #include "optimizer/optimizations/vn.h"
32 #include "optimizer/optimizations/cleanup.h"
33 #include "optimizer/optimizations/regalloc/reg_alloc_resolver.h"
34 #include "optimizer/optimizations/regalloc/reg_alloc.h"
35 #include "mem/pool_manager.h"
36 
37 using namespace testing::ext;
38 
39 namespace panda::compiler {
40 
41 class CompilerInstTest : public testing::Test {
42 public:
SetUpTestCase(void)43     static void SetUpTestCase(void) {}
TearDownTestCase(void)44     static void TearDownTestCase(void) {}
SetUp()45     void SetUp() {}
TearDown()46     void TearDown() {}
47 
48     GraphTest graph_test_;
49 };
50 
51 /**
52  * @tc.name: compiler_inst_test_001
53  * @tc.desc: Verify the GetInverseConditionCode function.
54  * @tc.type: FUNC
55  * @tc.require: issueNumber
56  */
57 HWTEST_F(CompilerInstTest, compiler_inst_test_001, TestSize.Level1)
58 {
59     auto cc = ConditionCode::CC_EQ;
60     auto data = ConditionCode::CC_NE;
61     auto code = GetInverseConditionCode(data);
62     EXPECT_EQ(code, cc);
63     auto code1 = GetInverseConditionCode(cc);
64     EXPECT_EQ(code1, data);
65 }
66 
67 /**
68  * @tc.name: compiler_inst_test_002
69  * @tc.desc: Verify the AllocateImmediates function.
70  * @tc.type: FUNC
71  * @tc.require: issueNumber
72  */
73 HWTEST_F(CompilerInstTest, compiler_inst_test_002, TestSize.Level1)
74 {
75     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
76     const char *test_method_name = "func_main_0";
77     bool status = false;
__anon7b7a1abf0102(Graph* graph, std::string &method_name) 78     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
79         if (test_method_name != method_name) {
80             return;
81         }
82 
83         EXPECT_NE(graph, nullptr);
84         size_t size = 5;  // 5: random number
85         for (auto bb : graph->GetBlocksRPO()) {
86             for (auto inst : bb->AllInsts()) {
87                 if (!inst->IsSaveState()) {
88                     continue;
89                 }
90                 status = true;
91                 auto save_state_inst = inst->CastToSaveState();
92                 save_state_inst->AllocateImmediates(graph->GetAllocator(), size);
93                 EXPECT_EQ(save_state_inst->GetImmediatesCount(), size);
94                 EXPECT_TRUE(save_state_inst->IsSaveState());
95             }
96         }
97     });
98     EXPECT_TRUE(status);
99 }
100 
101 /**
102  * @tc.name: compiler_inst_test_003
103  * @tc.desc: Verify the SwapOperandsConditionCode function.
104  * @tc.type: FUNC
105  * @tc.require: issueNumber
106  */
107 HWTEST_F(CompilerInstTest, compiler_inst_test_003, TestSize.Level1)
108 {
109     IfInst if_inst(ConditionCode::CC_EQ);
110     auto cc = if_inst.GetCc();
111     IfInst if_Inst(ConditionCode::CC_NE);
112     auto data = if_Inst.GetCc();
113     auto code = SwapOperandsConditionCode(data);
114     EXPECT_EQ(code, data);
115     auto code1 = SwapOperandsConditionCode(cc);
116     EXPECT_EQ(code1, cc);
117 }
118 
119 /**
120  * @tc.name: compiler_inst_test_004
121  * @tc.desc: Verify the Clone function.
122  * @tc.type: FUNC
123  * @tc.require: issueNumber
124  */
125 HWTEST_F(CompilerInstTest, compiler_inst_test_004, TestSize.Level1)
126 {
127     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
128     const char *test_method_name = "func5";
129     bool status = false;
__anon7b7a1abf0202(Graph* graph, std::string &method_name) 130     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
131         if (test_method_name != method_name) {
132             return;
133         }
134 
135         EXPECT_NE(graph, nullptr);
136         for (auto bb : graph->GetBlocksRPO()) {
137             for (auto inst : bb->AllInsts()) {
138                 if (inst->GetOpcode() != Opcode::Constant) {
139                     continue;
140                 }
141                 status = true;
142                 auto constInst = inst->Clone(graph);
143                 EXPECT_EQ(constInst->GetOpcode(), Opcode::Constant);
144                 inst->SetType(DataType::FLOAT32);
145                 constInst = inst->Clone(graph);
146                 EXPECT_EQ(constInst->GetType(), DataType::FLOAT32);
147                 inst->SetType(DataType::FLOAT64);
148                 constInst = inst->Clone(graph);
149                 EXPECT_EQ(constInst->GetType(), DataType::FLOAT64);
150                 inst->SetType(DataType::ANY);
151                 constInst = inst->Clone(graph);
152                 EXPECT_EQ(constInst->GetType(), DataType::ANY);
153             }
154         }
155     });
156     EXPECT_TRUE(status);
157 }
158 
159 /**
160  * @tc.name: compiler_inst_test_005
161  * @tc.desc: Verify the CastToParameter function.
162  * @tc.type: FUNC
163  * @tc.require: issueNumber
164  */
165 HWTEST_F(CompilerInstTest, compiler_inst_test_005, TestSize.Level1)
166 {
167     std::string pfile = GRAPH_TEST_ABC_DIR "moduleTryCatch.abc";
168     bool status = false;
__anon7b7a1abf0302(Graph* graph, std::string &method_name) 169     graph_test_.TestBuildGraphFromFile(pfile, [&](Graph* graph, std::string &method_name) {
170         auto constInst = graph->GetFirstConstInst();
171         auto same = constInst->InSameBlockOrDominate(constInst);
172         EXPECT_TRUE(same);
173         for (auto bb : graph->GetBlocksRPO()) {
174             for (auto inst : bb->AllInsts()) {
175                 if (inst->GetOpcode() != Opcode::Parameter) {
176                     continue;
177                 }
178                 status = true;
179                 auto parameterInst = inst->CastToParameter();
180                 EXPECT_EQ(parameterInst->Clone(graph)->GetOpcode(), Opcode::Parameter);
181             }
182         }
183     });
184     EXPECT_TRUE(true);
185 }
186 
187 /**
188  * @tc.name: compiler_inst_test_006
189  * @tc.desc: Verify the InSameBlockOrDominate function.
190  * @tc.type: FUNC
191  * @tc.require: issueNumber
192  */
193 HWTEST_F(CompilerInstTest, compiler_inst_test_006, TestSize.Level1)
194 {
195     std::string pfile = GRAPH_TEST_ABC_DIR "moduleTryCatch.abc";
196     bool status = false;
__anon7b7a1abf0402(Graph* graph, std::string &method_name) 197     graph_test_.TestBuildGraphFromFile(pfile, [&](Graph* graph, std::string &method_name) {
198         const auto constInst = graph->GetFirstConstInst();
199         auto same = constInst->InSameBlockOrDominate(constInst);
200         EXPECT_TRUE(same);
201         const auto startBlock = graph->GetStartBlock();
202         const auto firstInst = startBlock->GetFirstInst();
203 
204         for (auto bb : graph->GetBlocksRPO()) {
205             for (auto inst : bb->AllInsts()) {
206                 if (firstInst->GetBasicBlock() != inst->GetBasicBlock()) {
207                     continue;
208                 }
209                 status = true;
210                 EXPECT_TRUE(firstInst->InSameBlockOrDominate(inst));
211             }
212         }
213     });
214     EXPECT_TRUE(true);
215 }
216 
217 /**
218  * @tc.name: compiler_inst_test_007
219  * @tc.desc: Verify the GetImmediatesCount function.
220  * @tc.type: FUNC
221  * @tc.require: issueNumber
222  */
223 HWTEST_F(CompilerInstTest, compiler_inst_test_007, TestSize.Level1)
224 {
225     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
226     const char *test_method_name = "func_main_0";
227     bool status = false;
__anon7b7a1abf0502(Graph* graph, std::string &method_name) 228     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
229         if (test_method_name != method_name) {
230             return;
231         }
232 
233         uint64_t imm = 1;
234         uint16_t vreg = 2;
235         bool isAcc = true;
236         EXPECT_NE(graph, nullptr);
237 
238         for (auto bb : graph->GetBlocksRPO()) {
239             for (auto inst : bb->AllInsts()) {
240                 if (!inst->IsSaveState()) {
241                     continue;
242                 }
243                 status = true;
244                 auto save_state_inst = inst->CastToSaveState();
245                 save_state_inst->AppendImmediate(imm, vreg, DataType::Type::INT64, isAcc);
246                 EXPECT_EQ(save_state_inst->GetImmediatesCount(), 1);
247             }
248         }
249     });
250     EXPECT_TRUE(status);
251 }
252 
253 /**
254  * @tc.name: compiler_inst_test_008
255  * @tc.desc: Verify the SetVnObject function.
256  * @tc.type: FUNC
257  * @tc.require: issueNumber
258  */
259 HWTEST_F(CompilerInstTest, compiler_inst_test_008, TestSize.Level1)
260 {
261     IfInst if_inst(ConditionCode::CC_EQ);
262     VnObject vn_object;
263     if_inst.SetVnObject(&vn_object);
264     EXPECT_EQ(vn_object.GetSize(), 1);
265 }
266 
267 /**
268  * @tc.name: compiler_inst_test_009
269  * @tc.desc: Verify the SetVnObject function.
270  * @tc.type: FUNC
271  * @tc.require: issueNumber
272  */
273 HWTEST_F(CompilerInstTest, compiler_inst_test_009, TestSize.Level1)
274 {
275     CompareInst compareInst(ConditionCode::CC_EQ);
276     VnObject vn_object;
277     compareInst.SetVnObject(&vn_object);
278     EXPECT_EQ(vn_object.GetSize(), 1);
279 }
280 
281 /**
282  * @tc.name: compiler_inst_test_010
283  * @tc.desc: Verify the SetVnObject function.
284  * @tc.type: FUNC
285  * @tc.require: issueNumber
286  */
287 HWTEST_F(CompilerInstTest, compiler_inst_test_010, TestSize.Level1)
288 {
289     IfImmInst if_imm_inst(ConditionCode::CC_EQ);
290     VnObject vn_object;
291     if_imm_inst.SetVnObject(&vn_object);
292     EXPECT_EQ(vn_object.GetSize(), 1);
293 }
294 
295 /**
296  * @tc.name: compiler_inst_test_011
297  * @tc.desc: Verify the IsPrecedingInSameBlock function.
298  * @tc.type: FUNC
299  * @tc.require: issueNumber
300  */
301 HWTEST_F(CompilerInstTest, compiler_inst_test_011, TestSize.Level1)
302 {
303     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
304     const char *test_method_name = "func_main_0";
305     bool status = false;
__anon7b7a1abf0602(Graph* graph, std::string &method_name) 306     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
307         if (test_method_name != method_name) {
308             return;
309         }
310 
311         EXPECT_NE(graph, nullptr);
312         for (auto bb : graph->GetBlocksRPO()) {
313             for (auto inst : bb->AllInsts()) {
314                 if (!inst->IsSaveState()) {
315                     continue;
316                 }
317                 status = true;
318                 auto save_state_inst = inst->CastToSaveState();
319                 EXPECT_TRUE(save_state_inst->IsPrecedingInSameBlock(inst));
320             }
321         }
322     });
323     EXPECT_TRUE(status);
324 }
325 
326 /**
327  * @tc.name: compiler_inst_test_012
328  * @tc.desc: Verify the CastToCompare function.
329  * @tc.type: FUNC
330  * @tc.require: issueNumber
331  */
332 HWTEST_F(CompilerInstTest, compiler_inst_test_012, TestSize.Level1)
333 {
334     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
335     const char *test_method_name = "func_main_0";
336     bool status = false;
__anon7b7a1abf0702(Graph* graph, std::string &method_name) 337     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
338         if (test_method_name != method_name) {
339             return;
340         }
341 
342         EXPECT_NE(graph, nullptr);
343         for (auto bb : graph->GetBlocksRPO()) {
344             for (auto inst : bb->AllInsts()) {
345                 if (inst->GetOpcode() != Opcode::Compare) {
346                     continue;
347                 }
348                 status = true;
349                 auto compareInst = inst->CastToCompare();
350                 EXPECT_EQ(compareInst->Clone(graph)->GetOpcode(), Opcode::Compare);
351             }
352         }
353     });
354     EXPECT_TRUE(status);
355 }
356 
357 /**
358  * @tc.name: compiler_inst_test_013
359  * @tc.desc: Verify the CastToSpillFill function.
360  * @tc.type: FUNC
361  * @tc.require: issueNumber
362  */
363 HWTEST_F(CompilerInstTest, compiler_inst_test_013, TestSize.Level1)
364 {
365     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
366     const char *test_method_name = "func4";
367     bool status = false;
__anon7b7a1abf0802(Graph* graph, std::string &method_name) 368     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
369         if (test_method_name != method_name) {
370             return;
371         }
372 
373         EXPECT_NE(graph, nullptr);
374         EXPECT_TRUE(graph->RunPass<Cleanup>());
375         EXPECT_TRUE(graph->RunPass<bytecodeopt::RegAccAlloc>());
376         EXPECT_FALSE(graph->RunPass<Cleanup>());
377         EXPECT_TRUE(RegAlloc(graph));
378         EXPECT_FALSE(graph->RunPass<Cleanup>());
379         EXPECT_TRUE(graph->RunPass<bytecodeopt::RegEncoder>());
380         for (const auto &block : graph->GetVectorBlocks()) {
381             for (auto inst : block->AllInsts()) {
382                 if (inst->GetOpcode() != Opcode::SpillFill) {
383                     continue;
384                 }
385                 status = true;
386                 auto spillFill = inst->CastToSpillFill();
387                 EXPECT_EQ(spillFill->Clone(graph)->GetOpcode(), Opcode::SpillFill);
388             }
389         }
390     });
391     EXPECT_TRUE(status);
392 }
393 
394 /**
395  * @tc.name: compiler_inst_test_014
396  * @tc.desc: Verify the IsNativeCall function.
397  * @tc.type: FUNC
398  * @tc.require: issueNumber
399  */
400 HWTEST_F(CompilerInstTest, compiler_inst_test_014, TestSize.Level1)
401 {
402     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
403     const char *test_method_name = "func_main_0";
404     bool status = false;
__anon7b7a1abf0902(Graph* graph, std::string &method_name) 405     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
406         if (test_method_name != method_name) {
407             return;
408         }
409 
410         EXPECT_NE(graph, nullptr);
411         for (auto bb : graph->GetBlocksRPO()) {
412             for (auto inst : bb->AllInsts()) {
413                 if (!inst->IsIntrinsic()) {
414                     continue;
415                 }
416                 status = true;
417                 auto intrinsic_inst = inst->CastToIntrinsic();
418                 EXPECT_NE(intrinsic_inst->Clone(graph), nullptr);
419                 EXPECT_FALSE(intrinsic_inst->IsNativeCall());
420             }
421         }
422     });
423     EXPECT_TRUE(status);
424 }
425 
426 /**
427  * @tc.name: compiler_inst_test_015
428  * @tc.desc: Verify the GetThrowableInsts function.
429  * @tc.type: FUNC
430  * @tc.require: issueNumber
431  */
432 HWTEST_F(CompilerInstTest, compiler_inst_test_015, TestSize.Level1)
433 {
434     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
435     const char *test_method_name = "func_main_0";
436     bool status = false;
__anon7b7a1abf0a02(Graph* graph, std::string &method_name) 437     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
438         if (test_method_name != method_name) {
439             return;
440         }
441 
442         EXPECT_NE(graph, nullptr);
443         for (auto bb : graph->GetBlocksRPO()) {
444             for (auto inst : bb->AllInsts()) {
445                 if (inst->GetOpcode() != Opcode::CatchPhi) {
446                     continue;
447                 }
448                 status = true;
449                 auto catch_phi_inst =inst->CastToCatchPhi();
450                 catch_phi_inst->AppendThrowableInst(graph->GetFirstConstInst());
451 
452                 auto catch_inst = catch_phi_inst->GetThrowableInsts();
453                 auto it = std::find(catch_inst->begin(), catch_inst->end(), graph->GetFirstConstInst());
454                 EXPECT_NE(it, catch_inst->end());
455 
456                 catch_phi_inst->ReplaceThrowableInst(graph->GetFirstConstInst(), inst);
457                 auto it1 = std::find(catch_inst->begin(), catch_inst->end(), graph->GetFirstConstInst());
458                 EXPECT_EQ(it1, catch_inst->end());
459             }
460         }
461     });
462     EXPECT_TRUE(status);
463 }
464 
465 /**
466  * @tc.name: compiler_inst_test_016
467  * @tc.desc: Verify the Clone function.
468  * @tc.type: FUNC
469  * @tc.require: issueNumber
470  */
471 HWTEST_F(CompilerInstTest, compiler_inst_test_016, TestSize.Level1)
472 {
473     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
474     const char *test_method_name = "func_main_0";
475     bool status = false;
__anon7b7a1abf0b02(Graph* graph, std::string &method_name) 476     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
477         if (test_method_name != method_name) {
478             return;
479         }
480 
481         EXPECT_NE(graph, nullptr);
482 
483         for (auto bb : graph->GetBlocksRPO()) {
484             for (auto inst : bb->AllInsts()) {
485                 if (inst->GetOpcode() != Opcode::SaveState) {
486                     continue;
487                 }
488                 status = true;
489                 auto save_state = inst->CastToSaveState();
490                 EXPECT_EQ(save_state->Clone(graph)->GetOpcode(), Opcode::SaveState);
491             }
492         }
493     });
494     EXPECT_TRUE(status);
495 }
496 
497 /**
498  * @tc.name: compiler_inst_test_017
499  * @tc.desc: Verify the FindOrCreateConstant function.
500  * @tc.type: FUNC
501  * @tc.require: issueNumber
502  */
503 HWTEST_F(CompilerInstTest, compiler_inst_test_017, TestSize.Level1)
504 {
505     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
506     const char *test_method_name = "func_main_0";
507     bool status = false;
__anon7b7a1abf0c02(Graph* graph, std::string &method_name) 508     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
509         if (test_method_name != method_name) {
510             return;
511         }
512         status = true;
513         EXPECT_NE(graph, nullptr);
514         int32_t int32_const[3] = {-5, 0, 5};
515         int64_t int64_const[3] = {-5, 0, 5};
516 
517         auto start = graph->GetStartBlock();
518         for (auto i = 0; i < 3; i++) {
519             // add first int32 constant
520             int32_t val = int32_const[i];
521             auto const1 = graph->FindOrCreateConstant(val);
522             EXPECT_EQ(const1->GetType(), DataType::INT32);
523             EXPECT_EQ(const1->GetBasicBlock(), start);
524             uint64_t val1 = int64_const[i];
525             // add int64 constant, graph creates new constant
526             auto const2 = graph->FindOrCreateConstant(val1);
527             EXPECT_EQ(const2->GetType(), DataType::INT64);
528             EXPECT_EQ(const2->GetBasicBlock(), start);
529             EXPECT_EQ(const1->GetIntValue(), val1);
530             // add second int32 constant, graph doesn't create new constant
531             int32_t val2 = int32_const[i];
532             auto const3 = graph->FindOrCreateConstant(val2);
533             EXPECT_EQ(const3, const1);
534             EXPECT_EQ(const1->GetInt32Value(), val2);
535         }
536     });
537     EXPECT_TRUE(status);
538 }
539 
540 /**
541  * @tc.name: compiler_inst_test_018
542  * @tc.desc: Verify the AppendCatchTypeId function.
543  * @tc.type: FUNC
544  * @tc.require: issueNumber
545  */
546 HWTEST_F(CompilerInstTest, compiler_inst_test_018, TestSize.Level1)
547 {
548     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
549     const char *test_method_name = "func_main_0";
550     bool status = false;
__anon7b7a1abf0d02(Graph* graph, std::string &method_name) 551     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
552         if (test_method_name != method_name) {
553             return;
554         }
555 
556         EXPECT_NE(graph, nullptr);
557         uint32_t id = 7;
558         uint32_t catchIndex = 5;
559         for (auto bb : graph->GetBlocksRPO()) {
560             for (auto inst : bb->AllInsts()) {
561                 if (!inst->IsTry()) {
562                     continue;
563                 }
564                 status = true;
565                 TryInst *try_inst = inst->CastToTry();
566                 try_inst->AppendCatchTypeId(id, catchIndex);
567                 EXPECT_EQ(try_inst->Clone(graph)->GetOpcode(), Opcode::Try);
568             }
569         }
570     });
571     EXPECT_TRUE(status);
572 }
573 
574 /**
575  * @tc.name: compiler_inst_test_019
576  * @tc.desc: Verify the AppendCatchTypeId function.
577  * @tc.type: FUNC
578  * @tc.require: issueNumber
579  */
580 HWTEST_F(CompilerInstTest, compiler_inst_test_019, TestSize.Level1)
581 {
582     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
583     const char *test_method_name = "func_main_0";
584     bool status = false;
__anon7b7a1abf0e02(Graph* graph, std::string &method_name) 585     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
586         if (test_method_name != method_name) {
587             return;
588         }
589 
590         EXPECT_NE(graph, nullptr);
591         for (auto bb : graph->GetBlocksRPO()) {
592             for (auto inst : bb->AllInsts()) {
593                 if (!inst->IsTry()) {
594                     continue;
595                 }
596                 status = true;
597                 auto try_inst = inst->CastToTry();
598                 try_inst->AppendCatchTypeId(1, 1);
599                 auto catch_type_id = try_inst->GetCatchTypeIds();
600                 auto it1 = std::find(catch_type_id->begin(), catch_type_id->end(), 1);
601                 EXPECT_NE(it1, catch_type_id->end());
602             }
603         }
604     });
605     EXPECT_TRUE(status);
606 }
607 
608 /**
609  * @tc.name: compiler_inst_test_020
610  * @tc.desc: Verify the GetCommonType function.
611  * @tc.type: FUNC
612  * @tc.require: issueNumber
613  */
614 HWTEST_F(CompilerInstTest, compiler_inst_test_020, TestSize.Level1)
615 {
616     auto temp = DataType::Type::INT64;
617     auto type = DataType::Type::UINT8;
618     auto type1 = DataType::Type::UINT16;
619     auto str = DataType::Type::REFERENCE;
620     auto value = GetCommonType(type);
621     EXPECT_EQ(value, temp);
622 
623     auto value1 = GetCommonType(type1);
624     EXPECT_EQ(value1, temp);
625 
626     auto result = GetCommonType(str);
627     EXPECT_EQ(result, str);
628 }
629 
630 /**
631  * @tc.name: compiler_inst_test_021
632  * @tc.desc: Verify the IsInt32Bit function.
633  * @tc.type: FUNC
634  * @tc.require: issueNumber
635  */
636 HWTEST_F(CompilerInstTest, compiler_inst_test_021, TestSize.Level1)
637 {
638     auto type = DataType::Type::UINT8;
639     auto type1 = DataType::Type::UINT16;
640     auto str = DataType::Type::REFERENCE;
641     auto value = IsInt32Bit(type);
642     EXPECT_TRUE(value);
643 
644     auto value1 = IsInt32Bit(type1);
645     EXPECT_TRUE(value1);
646 
647     auto result = IsInt32Bit(str);
648     EXPECT_FALSE(result);
649 }
650 
651 /**
652  * @tc.name: compiler_basicblock_test_022
653  * @tc.desc: Verify the Is32Bits function.
654  * @tc.type: FUNC
655  * @tc.require: issueNumber
656  */
657 HWTEST_F(CompilerInstTest, compiler_inst_test_022, TestSize.Level1)
658 {
659     auto type = DataType::Type::UINT8;
660     auto type1 = DataType::Type::UINT16;
661     auto str = DataType::Type::REFERENCE;
662     auto str1 = DataType::Type::POINTER;
663     auto vi = DataType::Type::VOID;
664     auto value1 = Is32Bits(type, Arch::NONE);
665     EXPECT_TRUE(value1);
666 
667     auto value = Is32Bits(type1, Arch::NONE);
668     EXPECT_TRUE(value);
669 
670     auto result = Is32Bits(str, Arch::AARCH32);
671     EXPECT_TRUE(result);
672 
673     auto result1 = Is32Bits(str1, Arch::AARCH64);
674     EXPECT_FALSE(result1);
675 
676     auto result2 = Is32Bits(vi, Arch::AARCH64);
677     EXPECT_FALSE(result2);
678 }
679 
680 /**
681  * @tc.name: compiler_basicblock_test_023
682  * @tc.desc: Verify the IsTypeCollectable function.
683  * @tc.type: FUNC
684  * @tc.require: issueNumber
685  */
686 HWTEST_F(CompilerInstTest, compiler_inst_test_023, TestSize.Level1)
687 {
688     auto type = DataType::Type::REFERENCE;
689     auto value = IsTypeCollectable(type);
690     EXPECT_TRUE(value);
691 
692     auto type1 = DataType::Type::UINT16;
693     auto value1 = IsTypeCollectable(type1);
694     EXPECT_FALSE(value1);
695 }
696 
697 /**
698  * @tc.name: compiler_basicblock_test_024
699  * @tc.desc: Verify the GetIntTypeForReference function.
700  * @tc.type: FUNC
701  * @tc.require: issueNumber
702  */
703 HWTEST_F(CompilerInstTest, compiler_inst_test_024, TestSize.Level1)
704 {
705     auto type = DataType::Type::UINT32;
706     auto value = DataType::GetIntTypeForReference(Arch::AARCH32);
707     EXPECT_EQ(value, type);
708 }
709 
710 /**
711  * @tc.name: compiler_basicblock_test_025
712  * @tc.desc: Verify the IsTypeSigned function.
713  * @tc.type: FUNC
714  * @tc.require: issueNumber
715  */
716 HWTEST_F(CompilerInstTest, compiler_inst_test_025, TestSize.Level1)
717 {
718     auto type = DataType::Type::REFERENCE;
719     auto value = IsTypeSigned(type);
720     EXPECT_FALSE(value);
721 
722     auto type1 = DataType::Type::INT8;
723     auto value1 = IsTypeSigned(type1);
724     EXPECT_TRUE(value1);
725 }
726 
727 /**
728  * @tc.name: compiler_basicblock_test_026
729  * @tc.desc: Verify the GetPhiInputBb function.
730  * @tc.type: FUNC
731  * @tc.require: issueNumber
732  */
733 HWTEST_F(CompilerInstTest, compiler_inst_test_026, TestSize.Level1)
734 {
735     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
736     const char *test_method_name = "func_main_0";
737     bool status = false;
__anon7b7a1abf0f02(Graph* graph, std::string &method_name) 738     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
739         if (test_method_name != method_name) {
740             return;
741         }
742 
743         EXPECT_NE(graph, nullptr);
744         for (auto bb : graph->GetBlocksRPO()) {
745             for (auto inst : bb->PhiInsts()) {
746                 status = true;
747                 auto phi = inst->CastToPhi();
748                 auto block = phi->GetPhiInputBb(0);
749                 EXPECT_NE(phi->GetPhiInput(block), nullptr);
750             }
751         }
752     });
753     EXPECT_TRUE(status);
754 }
755 
756 /**
757  * @tc.name: compiler_basicblock_test_027
758  * @tc.desc: Verify the GetEdgeIfInputFalse function.
759  * @tc.type: FUNC
760  * @tc.require: issueNumber
761  */
762 HWTEST_F(CompilerInstTest, compiler_inst_test_027, TestSize.Level1)
763 {
764     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
765     const char *test_method_name = "func_main_0";
766     bool status = false;
__anon7b7a1abf1002(Graph* graph, std::string &method_name) 767     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
768         if (test_method_name != method_name) {
769             return;
770         }
771 
772         EXPECT_NE(graph, nullptr);
773         for (auto bb : graph->GetBlocksRPO()) {
774             for (auto inst : bb->AllInsts()) {
775                 if (inst->GetOpcode() != Opcode::IfImm) {
776                     continue;
777                 }
778                 status = true;
779                 auto if_imm_inst = inst->CastToIfImm();
780                 EXPECT_NE(if_imm_inst->GetEdgeIfInputTrue(), nullptr);
781                 EXPECT_NE(if_imm_inst->GetEdgeIfInputFalse(), nullptr);
782             }
783         }
784     });
785     EXPECT_TRUE(status);
786 }
787 
788 /**
789  * @tc.name: compiler_basicblock_test_028
790  * @tc.desc: Verify the GetSaveState function.
791  * @tc.type: FUNC
792  * @tc.require: issueNumber
793  */
794 HWTEST_F(CompilerInstTest, compiler_inst_test_028, TestSize.Level1)
795 {
796     SaveStateInst save_state1;
797     save_state1.SetFlag(inst_flags::Flags::CAN_THROW);
798     EXPECT_EQ(save_state1.GetSaveState(), nullptr);
799 
800     SaveStateInst save_state2;
801     save_state2.SetFlag(inst_flags::Flags::REQUIRE_STATE);
802     EXPECT_EQ(save_state2.GetSaveState(), nullptr);
803 }
804 
805 /**
806  * @tc.name: compiler_basicblock_test_029
807  * @tc.desc: Verify the ReplaceInput function.
808  * @tc.type: FUNC
809  * @tc.require: issueNumber
810  */
811 HWTEST_F(CompilerInstTest, compiler_inst_test_029, TestSize.Level1)
812 {
813     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
814     const char *test_method_name = "func_main_0";
815     bool status = false;
__anon7b7a1abf1102(Graph* graph, std::string &method_name) 816     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
817         if (test_method_name != method_name) {
818             return;
819         }
820 
821         EXPECT_NE(graph, nullptr);
822         for (auto bb : graph->GetBlocksRPO()) {
823             for (auto inst : bb->AllInsts()) {
824                 status = true;
825                 if (inst->GetInputsCount() != 0) {
826                     status = true;
827                     auto inst1 = graph->CreateInstTry(DataType::ANY, 0);
828                     inst->ReplaceInput(inst->GetInput(0).GetInst(), inst1);
829                 }
830             }
831         }
832     });
833     EXPECT_TRUE(status);
834 }
835 
836 /**
837  * @tc.name: compiler_basicblock_test_030
838  * @tc.desc: Verify the RemoveInputs function.
839  * @tc.type: FUNC
840  * @tc.require: issueNumber
841  */
842 HWTEST_F(CompilerInstTest, compiler_inst_test_030, TestSize.Level1)
843 {
844     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
845     const char *test_method_name = "func_main_0";
846     bool status = false;
__anon7b7a1abf1202(Graph* graph, std::string &method_name) 847     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
848         if (test_method_name != method_name) {
849             return;
850         }
851 
852         EXPECT_NE(graph, nullptr);
853         for (auto bb : graph->GetBlocksRPO()) {
854             for (auto inst : bb->AllInsts()) {
855                 status = true;
856                 inst->RemoveInputs();
857             }
858         }
859     });
860     EXPECT_TRUE(status);
861 }
862 
863 /**
864  * @tc.name: compiler_basicblock_test_031
865  * @tc.desc: Verify the IsInvalid function.
866  * @tc.type: FUNC
867  * @tc.require: issueNumber
868  */
869 HWTEST_F(CompilerInstTest, compiler_inst_test_031, TestSize.Level1)
870 {
871     IfInst if_inst(ConditionCode::CC_EQ);
872     Location location(Location::Kind::REGISTER, 253);  // 253: random number
873     if_inst.SetLikely();
874     EXPECT_TRUE(if_inst.IsLikely());
875     if_inst.InverseConditionCode();
876     IfInst if_inst1(ConditionCode::CC_NE);
877     if_inst1.SetUnlikely();
878     EXPECT_TRUE(if_inst.IsUnlikely());
879 }
880 
881 /**
882  * @tc.name: compiler_basicblock_test_032
883  * @tc.desc: Verify the AllocateInputTypes function.
884  * @tc.type: FUNC
885  * @tc.require: issueNumber
886  */
887 HWTEST_F(CompilerInstTest, compiler_inst_test_032, TestSize.Level1)
888 {
889     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
890     const char *test_method_name = "func_main_0";
891     bool status = false;
__anon7b7a1abf1302(Graph* graph, std::string &method_name) 892     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
893         if (test_method_name != method_name) {
894             return;
895         }
896         status = true;
897         size_t size = 2;  // 2: random number
898         EXPECT_NE(graph, nullptr);
899         InputTypesMixin input_types_mixin;
900         input_types_mixin.AllocateInputTypes(graph->GetAllocator(), size);
901     });
902     EXPECT_TRUE(status);
903 }
904 
905 /**
906  * @tc.name: compiler_basicblock_test_033
907  * @tc.desc: Verify the RequireRegMap function.
908  * @tc.type: FUNC
909  * @tc.require: issueNumber
910  */
911 HWTEST_F(CompilerInstTest, compiler_inst_test_033, TestSize.Level1)
912 {
913     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
914     const char *test_method_name = "func_main_0";
915     bool status = false;
__anon7b7a1abf1402(Graph* graph, std::string &method_name) 916     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
917         if (test_method_name != method_name) {
918             return;
919         }
920 
921         EXPECT_NE(graph, nullptr);
922         for (auto bb : graph->GetBlocksRPO()) {
923             if (!bb->IsTryBegin()) {
924                 continue;
925             }
926             for (auto inst : bb->AllInsts()) {
927                 if (!inst->IsTry()) {
928                     continue;
929                 }
930                 status = true;
931                 auto try_inst = GetTryBeginInst(bb);
932                 EXPECT_NE(try_inst, nullptr);
933                 EXPECT_FALSE(try_inst->RequireRegMap());
934             }
935         }
936     });
937     EXPECT_TRUE(status);
938 }
939 
940 /**
941  * @tc.name: compiler_basicblock_test_034
942  * @tc.desc: Verify the CloneTypes function.
943  * @tc.type: FUNC
944  * @tc.require: issueNumber
945  */
946 HWTEST_F(CompilerInstTest, compiler_inst_test_034, TestSize.Level1)
947 {
948     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
949     const char *test_method_name = "func_main_0";
950     bool status = false;
__anon7b7a1abf1502(Graph* graph, std::string &method_name) 951     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
952         if (test_method_name != method_name) {
953             return;
954         }
955         status = true;
956         EXPECT_NE(graph, nullptr);
957         InputTypesMixin input_types_mixin;
958         InputTypesMixin input_types_mixin1;
959         input_types_mixin1.AllocateInputTypes(graph->GetAllocator(), 0);
960         EXPECT_NE(input_types_mixin1.GetInputTypes(), nullptr);
961         input_types_mixin1.CloneTypes(graph->GetAllocator(), &input_types_mixin);
962         EXPECT_EQ(input_types_mixin1.GetInputTypes()->size(), 0);
963     });
964     EXPECT_TRUE(status);
965 }
966 
967 /**
968  * @tc.name: compiler_inst_test_035
969  * @tc.desc: Verify the HasInsTypeinfo function.
970  * @tc.type: FUNC
971  * @tc.require: issueNumber
972  */
973 HWTEST_F(CompilerInstTest, compiler_inst_test_035, TestSize.Level1)
974 {
975     std::string pfile = GRAPH_TEST_ABC_DIR "styleTryCatch.abc";
976     const char *test_method_name = "func_main_0";
977     bool status = false;
__anon7b7a1abf1602(Graph* graph, std::string &method_name) 978     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
979         if (test_method_name != method_name) {
980             return;
981         }
982         status = true;
983         EXPECT_NE(graph, nullptr);
984         auto method = graph->GetMethod();
985         RuntimeInterface interface;
986         EXPECT_EQ(interface.ResolveOffsetByIndex(method, 0), 0);
987         EXPECT_EQ(interface.GetBinaryFileForMethod(method), nullptr);
988         EXPECT_EQ(interface.GetMethodId(method), 0);
989         EXPECT_EQ(interface.GetMethodTotalArgumentsCount(method), 0);
990         EXPECT_EQ(interface.GetMethodArgumentsCount(method, 0), 0);
991         EXPECT_EQ(interface.GetMethodArgumentsCount(method), 0);
992         EXPECT_EQ(interface.GetMethodRegistersCount(method), 0);
993         EXPECT_EQ(interface.GetClassNameFromMethod(method), "UnknownClass");
994         EXPECT_EQ(interface.GetMethodName(method), "UnknownMethod");
995         EXPECT_FALSE(interface.SetTypeLiteralArrayKey("test"));
996         EXPECT_EQ(interface.GetMethodCode(method), nullptr);
997         EXPECT_EQ(interface.GetMethodCodeSize(method), 0);
998         EXPECT_EQ(interface.GetMethodSourceLanguage(method), SourceLanguage::PANDA_ASSEMBLY);
999         EXPECT_EQ(interface.GetClass(method), nullptr);
1000         EXPECT_EQ(interface.GetMethodFullName(method, true), "UnknownMethod");
1001         EXPECT_EQ(interface.GetClassIdForMethod(method), 0);
1002         EXPECT_EQ(interface.GetTypeInfoIndexByInstId(0), NO_EXPLICIT_TYPE);
1003         EXPECT_FALSE(interface.IsPcBindType(0));
1004         EXPECT_FALSE(interface.FillInstIdTypePairByPc(0, 0));
1005         EXPECT_EQ(interface.GetTypeLiteralArrayKey(), nullptr);
1006         EXPECT_FALSE(interface.HasInsTypeinfo());
1007     });
1008     EXPECT_TRUE(status);
1009 }
1010 
1011 /**
1012  * @tc.name: compiler_inst_test_036
1013  * @tc.desc: Verify the SetVirtualRegister function.
1014  * @tc.type: FUNC
1015  * @tc.require: issueNumber
1016  */
1017 HWTEST_F(CompilerInstTest, compiler_inst_test_036, TestSize.Level1)
1018 {
1019     User user(false, 0, 1);
1020     user.GetVirtualRegister();
1021     VirtualRegister reg;
1022     user.SetVirtualRegister(reg);
1023     EXPECT_TRUE(user.GetVirtualRegister().IsAccumulator());
1024     EXPECT_TRUE(user.IsDynamic());
1025 }
1026 }  // namespace panda::compiler
1027