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