1 /**
2 * Copyright (c) 2024-2025 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 "libabckit/include/c/abckit.h"
17 #include "libabckit/include/c/metadata_core.h"
18 #include "logger.h"
19 #include "libabckit/include/c/ir_core.h"
20 #include "libabckit/include/c/isa/isa_static.h"
21
22 #include "helpers/helpers.h"
23 #include "helpers/helpers_runtime.h"
24
25 #include <gtest/gtest.h>
26
27 namespace libabckit::test {
28
29 namespace {
30
31 auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35 auto g_statG = AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36
IsCall(AbckitInst * cond,AbckitInst ** ifInstr,AbckitInst * curInst)37 bool IsCall(AbckitInst *cond, AbckitInst **ifInstr, AbckitInst *curInst)
38 {
39 if (!g_implG->iCheckIsCall(cond)) {
40 return false;
41 }
42 AbckitCoreFunction *method = g_implG->iGetFunction(cond);
43 AbckitString *methodNameStr = g_implI->functionGetName(method);
44 auto name = helpers::GetCropFuncName(helpers::AbckitStringToString(methodNameStr).data());
45 if (name == "IsDebug") {
46 *ifInstr = curInst;
47 return true;
48 }
49 return false;
50 }
51
__anoncb8892340202(AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) 52 auto g_libAbcKitTestStaticBranchEliminationLambda0 = [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) {
53 AbckitInst *ifInstr = nullptr;
54
55 auto checkIfArg = [&ifInstr](AbckitFile *, AbckitBasicBlock *bb) -> bool {
56 auto *curInst = g_implG->bbGetFirstInst(bb);
57 while (curInst != nullptr) {
58 AbckitIsaApiStaticOpcode op = g_statG->iGetOpcode(curInst);
59 if (op != ABCKIT_ISA_API_STATIC_OPCODE_IF) {
60 curInst = g_implG->iGetNext(curInst);
61 continue;
62 }
63 auto *cond = g_implG->iGetInput(curInst, 0);
64 if (IsCall(cond, &ifInstr, curInst)) {
65 return true;
66 }
67 curInst = g_implG->iGetNext(curInst);
68 }
69 return false;
70 };
71
72 std::vector<AbckitBasicBlock *> bbs;
73 g_implG->gVisitBlocksRpo(graph, &bbs, [](AbckitBasicBlock *bb, void *data) {
74 reinterpret_cast<std::vector<AbckitBasicBlock *> *>(data)->emplace_back(bb);
75 return true;
76 });
77
78 for (auto i = bbs.rbegin(), j = bbs.rend(); i != j; ++i) {
79 if (checkIfArg(file, *i)) {
80 break;
81 }
82 }
83
84 EXPECT_NE(ifInstr, nullptr);
85 auto *ifBB = g_implG->iGetBasicBlock(ifInstr);
86 EXPECT_NE(ifBB, nullptr);
87 auto *falseBB = g_implG->bbGetFalseBranch(ifBB);
88 EXPECT_NE(falseBB, nullptr);
89 g_implG->bbDisconnectSuccBlock(ifBB, ABCKIT_FALSE_SUCC_IDX);
90 g_implG->iRemove(ifInstr);
91 };
92
93 } // namespace
94
95 class AbckitScenarioTest : public ::testing::Test {};
96
97 // Test: test-kind=scenario, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(AbckitScenarioTest,LibAbcKitTestStaticBranchElimination)98 TEST_F(AbckitScenarioTest, LibAbcKitTestStaticBranchElimination)
99 {
100 constexpr auto INPUT_PATH = ABCKIT_ABC_DIR "scenarios/static_branch_elimination/static_branch_elimination.abc";
101 auto output = helpers::ExecuteStaticAbc(INPUT_PATH, "static_branch_elimination/ETSGLOBAL", "main");
102 EXPECT_TRUE(helpers::Match(output,
103 "MyFunc start...\n"
104 "MyFunc start...\n"
105 "Debug buisness logic...\n"
106 "Debug buisness logic...\n"
107 "MyFunc end...\n"
108 "MyFunc end...\n"));
109
110 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
111 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
112 // Transform method
113 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
114 helpers::TransformMethod(file, "MyFunc", g_libAbcKitTestStaticBranchEliminationLambda0);
115 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
116
117 constexpr auto OUTPUT_PATH =
118 ABCKIT_ABC_DIR "scenarios/static_branch_elimination/static_branch_elimination_transformed.abc";
119 // Write output file
120 g_impl->writeAbc(file, OUTPUT_PATH, strlen(OUTPUT_PATH));
121 g_impl->closeFile(file);
122 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
123 output = helpers::ExecuteStaticAbc(OUTPUT_PATH, "static_branch_elimination/ETSGLOBAL", "main");
124 EXPECT_TRUE(helpers::Match(output,
125 "MyFunc start...\n"
126 "MyFunc start...\n"
127 "Release buisness logic...\n"
128 "Release buisness logic...\n"
129 "MyFunc end...\n"
130 "MyFunc end...\n"));
131 }
132
133 } // namespace libabckit::test
134