1 /* 2 * Copyright (c) 2023-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 PANDA_CONST_ARRAY_RESOLVER_H 17 #define PANDA_CONST_ARRAY_RESOLVER_H 18 19 #include "assembler/assembly-function.h" 20 #include "bytecodeopt_options.h" 21 #include "ir_interface.h" 22 #include "compiler/optimizer/ir/graph.h" 23 #include "compiler/optimizer/pass.h" 24 #include "compiler/optimizer/ir/inst.h" 25 #include "compiler/optimizer/optimizations/const_folding.h" 26 27 namespace ark::bytecodeopt { 28 using ark::compiler::Inst; 29 using ark::compiler::Opcode; 30 // NOLINTNEXTLINE(google-build-using-namespace) 31 using namespace ark::compiler::DataType; 32 class ConstArrayResolver : public compiler::Optimization { 33 public: ConstArrayResolver(compiler::Graph * graph,BytecodeOptIrInterface * iface)34 explicit ConstArrayResolver(compiler::Graph *graph, BytecodeOptIrInterface *iface) 35 : compiler::Optimization(graph), 36 irInterface_(iface), 37 constArraysInit_(graph->GetLocalAllocator()->Adapter()), 38 constArraysFill_(graph->GetLocalAllocator()->Adapter()) 39 { 40 } 41 42 ~ConstArrayResolver() override = default; 43 NO_COPY_SEMANTIC(ConstArrayResolver); 44 NO_MOVE_SEMANTIC(ConstArrayResolver); 45 46 bool RunImpl() override; 47 GetPassName()48 const char *GetPassName() const override 49 { 50 return "ConstArrayResolver"; 51 } 52 IsEnable()53 bool IsEnable() const override 54 { 55 return g_options.IsConstArrayResolver(); 56 } 57 58 private: 59 bool FindConstantArrays(); 60 void RemoveArraysFill(); 61 void InsertLoadConstArrayInsts(); 62 std::optional<std::vector<pandasm::LiteralArray::Literal>> FillLiteralArray(Inst *inst, size_t size); 63 bool FillLiteral(compiler::StoreInst *storeArrayInst, pandasm::LiteralArray::Literal *literal); 64 void AddIntroLiterals(pandasm::LiteralArray *ltAr); 65 bool IsMultidimensionalArray(compiler::NewArrayInst *inst); 66 67 private: 68 BytecodeOptIrInterface *irInterface_ {nullptr}; 69 ArenaMap<uint32_t, compiler::NewArrayInst *> constArraysInit_; // const_arrays_[literalarray_id] = new_array_inst 70 ArenaMap<Inst *, std::vector<Inst *>> 71 constArraysFill_; // const_arrays_[new_array_inst] = {store_array_inst_1, ... , store_array_inst_n} 72 }; 73 } // namespace ark::bytecodeopt 74 75 #endif // PANDA_CONST_ARRAY_RESOLVER_H 76