1 /* 2 * Copyright (c) 2021-2024 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 #ifndef ETS_BYTECODE_OPTIMIZER_TESTS_ETS_OPT_H 17 #define ETS_BYTECODE_OPTIMIZER_TESTS_ETS_OPT_H 18 19 #include <gtest/gtest.h> 20 #include <string> 21 #include <string_view> 22 23 #include "assembler/assembly-emitter.h" 24 #include "assembler/assembly-parser.h" 25 #include "assembler/assembly-program.h" 26 #include "assembler/extensions/extensions.h" 27 #include "bytecode_optimizer/canonicalization.h" 28 #include "compiler/compiler_options.h" 29 #include "compiler/optimizer/analysis/rpo.h" 30 #include "compiler/optimizer/ir/inst.h" 31 #include "compiler/optimizer/ir/ir_constructor.h" 32 #include "compiler/optimizer/optimizations/regalloc/reg_alloc_linear_scan.h" 33 #include "libpandabase/utils/logger.h" 34 #include "libpandabase/utils/utils.h" 35 #include "mem/arena_allocator.h" 36 #include "mem/pool_manager.h" 37 #include "optimizer/ir/inst.h" 38 #include "bytecode_optimizer/reg_acc_alloc.h" 39 #include "bytecode_optimizer/optimize_bytecode.h" 40 #include "bytecode_optimizer/panda_gen_options/generated/bytecodeopt_options_gen.h" 41 #include "parser/ETSparser.h" 42 43 namespace ark::bytecodeopt { 44 45 class EtsOptTest : public ::testing::Test { 46 public: EtsOptTest()47 EtsOptTest() 48 { 49 ark::compiler::g_options.SetCompilerUseSafepoint(false); 50 ark::compiler::g_options.SetCompilerSupportInitObjectInst(true); 51 52 // NOLINTNEXTLINE(readability-magic-numbers) 53 mem::MemConfig::Initialize(128_MB, 64_MB, 64_MB, 32_MB, 0, 0); 54 PoolManager::Initialize(); 55 allocator_ = new ArenaAllocator(SpaceType::SPACE_TYPE_INTERNAL); 56 localAllocator_ = new ArenaAllocator(SpaceType::SPACE_TYPE_INTERNAL); 57 builder_ = new compiler::IrConstructor(); 58 59 Logger::InitializeStdLogging(Logger::Level::ERROR, 60 ark::Logger::ComponentMask().set(Logger::Component::BYTECODE_OPTIMIZER)); 61 } ~EtsOptTest()62 ~EtsOptTest() override 63 { 64 delete allocator_; 65 delete localAllocator_; 66 delete builder_; 67 PoolManager::Finalize(); 68 mem::MemConfig::Finalize(); 69 70 Logger::Destroy(); 71 } 72 73 NO_COPY_SEMANTIC(EtsOptTest); 74 NO_MOVE_SEMANTIC(EtsOptTest); 75 GetAllocator()76 ArenaAllocator *GetAllocator() 77 { 78 return allocator_; 79 } GetLocalAllocator()80 ArenaAllocator *GetLocalAllocator() 81 { 82 return localAllocator_; 83 } 84 85 compiler::Graph *CreateEmptyGraph(bool isDynamic = false) 86 { 87 auto *graph = 88 GetAllocator()->New<compiler::Graph>(GetAllocator(), GetLocalAllocator(), Arch::NONE, isDynamic, true); 89 return graph; 90 } 91 GetGraph()92 compiler::Graph *GetGraph() 93 { 94 return graph_; 95 } 96 SetGraph(compiler::Graph * graph)97 void SetGraph(compiler::Graph *graph) 98 { 99 graph_ = graph; 100 } 101 FuncHasInst(pandasm::Function * func,pandasm::Opcode opcode)102 bool FuncHasInst(pandasm::Function *func, pandasm::Opcode opcode) 103 { 104 for (const auto &inst : func->ins) { 105 if (inst.opcode == opcode) { 106 return true; 107 } 108 } 109 return false; 110 } 111 112 protected: 113 // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) 114 compiler::IrConstructor *builder_; 115 116 private: 117 ArenaAllocator *allocator_; 118 ArenaAllocator *localAllocator_; 119 compiler::Graph *graph_ {nullptr}; 120 }; 121 122 } // namespace ark::bytecodeopt 123 124 #endif // ETS_BYTECODE_OPTIMIZER_TESTS_ETS_OPT_H