14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_COMPILER_PGO_BC_INFO_RECORDER_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_PGO_BC_INFO_RECORDER_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/method_literal.h" 204514f5e3Sopenharmony_ci#include "ecmascript/mem/c_containers.h" 214514f5e3Sopenharmony_ci#include "libpandafile/bytecode_instruction.h" 224514f5e3Sopenharmony_ci 234514f5e3Sopenharmony_cinamespace panda::ecmascript::kungfu { 244514f5e3Sopenharmony_ciclass PGOBCInfo { 254514f5e3Sopenharmony_cipublic: 264514f5e3Sopenharmony_ci enum Type { 274514f5e3Sopenharmony_ci OBJ_LITERAL = 0, 284514f5e3Sopenharmony_ci ARRAY_LITERAL, 294514f5e3Sopenharmony_ci EMPTY_ARRAY, 304514f5e3Sopenharmony_ci CALL_TARGET, 314514f5e3Sopenharmony_ci CLASS, 324514f5e3Sopenharmony_ci FUNCTION, 334514f5e3Sopenharmony_ci 344514f5e3Sopenharmony_ci TYPE_NUM, 354514f5e3Sopenharmony_ci TYPE_FIRST = OBJ_LITERAL, 364514f5e3Sopenharmony_ci TYPE_LAST = FUNCTION, 374514f5e3Sopenharmony_ci }; 384514f5e3Sopenharmony_ci 394514f5e3Sopenharmony_ci struct InfoDetail { 404514f5e3Sopenharmony_ci const CString &recordName; 414514f5e3Sopenharmony_ci uint32_t methodOffset; 424514f5e3Sopenharmony_ci uint32_t bcIndex; 434514f5e3Sopenharmony_ci uint32_t bcOffset; 444514f5e3Sopenharmony_ci uint32_t cpIndex; 454514f5e3Sopenharmony_ci }; 464514f5e3Sopenharmony_ci 474514f5e3Sopenharmony_ci class Info { 484514f5e3Sopenharmony_ci public: 494514f5e3Sopenharmony_ci void Record(const InfoDetail &detail); 504514f5e3Sopenharmony_ci 514514f5e3Sopenharmony_ci uint32_t GetPGOExtendGTCount(const CString &recordName) const; 524514f5e3Sopenharmony_ci 534514f5e3Sopenharmony_ci template <class Callback> 544514f5e3Sopenharmony_ci void IterateValByMethodOffset(uint32_t methodOffset, Type type, const Callback &cb) const 554514f5e3Sopenharmony_ci { 564514f5e3Sopenharmony_ci if (methodOffsetToValVec_.find(methodOffset) != methodOffsetToValVec_.end()) { 574514f5e3Sopenharmony_ci const ValVec &valVec = methodOffsetToValVec_.at(methodOffset); 584514f5e3Sopenharmony_ci for (auto val : valVec) { 594514f5e3Sopenharmony_ci cb(type, val.bcIndex, val.bcOffset, val.cpIndex); 604514f5e3Sopenharmony_ci } 614514f5e3Sopenharmony_ci } 624514f5e3Sopenharmony_ci } 634514f5e3Sopenharmony_ci 644514f5e3Sopenharmony_ci template <class Callback> 654514f5e3Sopenharmony_ci void IterateValByMethodOffsetAndType(uint32_t methodOffset, const Callback &cb) const 664514f5e3Sopenharmony_ci { 674514f5e3Sopenharmony_ci if (methodOffsetToValVec_.find(methodOffset) != methodOffsetToValVec_.end()) { 684514f5e3Sopenharmony_ci const ValVec &valVec = methodOffsetToValVec_.at(methodOffset); 694514f5e3Sopenharmony_ci for (auto val : valVec) { 704514f5e3Sopenharmony_ci cb(val.bcIndex, val.bcOffset, val.cpIndex); 714514f5e3Sopenharmony_ci } 724514f5e3Sopenharmony_ci } 734514f5e3Sopenharmony_ci } 744514f5e3Sopenharmony_ci 754514f5e3Sopenharmony_ci private: 764514f5e3Sopenharmony_ci struct Val { 774514f5e3Sopenharmony_ci uint32_t bcIndex; 784514f5e3Sopenharmony_ci uint32_t bcOffset; 794514f5e3Sopenharmony_ci uint32_t cpIndex; 804514f5e3Sopenharmony_ci }; 814514f5e3Sopenharmony_ci using ValVec = CVector<Val>; 824514f5e3Sopenharmony_ci CMap<CString, uint32_t> recordNameToValCount_; 834514f5e3Sopenharmony_ci CUnorderedMap<uint32_t, ValVec> methodOffsetToValVec_; 844514f5e3Sopenharmony_ci }; 854514f5e3Sopenharmony_ci 864514f5e3Sopenharmony_ci PGOBCInfo() : infos_(Type::TYPE_NUM, Info{}) {} 874514f5e3Sopenharmony_ci ~PGOBCInfo() = default; 884514f5e3Sopenharmony_ci 894514f5e3Sopenharmony_ci const Info& GetInfo(Type type) const; 904514f5e3Sopenharmony_ci 914514f5e3Sopenharmony_ci uint32_t GetPGOExtendGTCount(const CString &recordName) const; 924514f5e3Sopenharmony_ci 934514f5e3Sopenharmony_ci void PUBLIC_API Record(const BytecodeInstruction &bcIns, int32_t bcIndex, 944514f5e3Sopenharmony_ci const CString &recordName, const MethodLiteral *method); 954514f5e3Sopenharmony_ci 964514f5e3Sopenharmony_ci template <class Callback> 974514f5e3Sopenharmony_ci void IterateInfoAndType(uint32_t methodOffset, const Callback &cb) const 984514f5e3Sopenharmony_ci { 994514f5e3Sopenharmony_ci for (size_t idx = 0; idx < Type::TYPE_NUM; ++idx) { 1004514f5e3Sopenharmony_ci Type type = static_cast<Type>(idx); 1014514f5e3Sopenharmony_ci infos_[idx].IterateValByMethodOffset(methodOffset, type, cb); 1024514f5e3Sopenharmony_ci } 1034514f5e3Sopenharmony_ci } 1044514f5e3Sopenharmony_ci 1054514f5e3Sopenharmony_ci template <class Callback> 1064514f5e3Sopenharmony_ci void IterateInfoByType(uint32_t methodOffset, Type type, const Callback &cb) const 1074514f5e3Sopenharmony_ci { 1084514f5e3Sopenharmony_ci infos_[type].IterateValByMethodOffsetAndType(methodOffset, cb); 1094514f5e3Sopenharmony_ci } 1104514f5e3Sopenharmony_ciprivate: 1114514f5e3Sopenharmony_ci void Record(const InfoDetail &detail, Type type); 1124514f5e3Sopenharmony_ci 1134514f5e3Sopenharmony_ci CVector<Info> infos_; 1144514f5e3Sopenharmony_ci}; 1154514f5e3Sopenharmony_ci} // panda::ecmascript::kungfu 1164514f5e3Sopenharmony_ci#endif // ECMASCRIPT_COMPILER_PGO_BC_INFO_RECORDER_H 117