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#include "ecmascript/compiler/pgo_bc_info.h" 17#include "ecmascript/compiler/bytecodes.h" 18 19namespace panda::ecmascript::kungfu { 20void PGOBCInfo::Info::Record(const InfoDetail &detail) 21{ 22 auto it = methodOffsetToValVec_.find(detail.methodOffset); 23 if (it == methodOffsetToValVec_.end()) { 24 methodOffsetToValVec_[detail.methodOffset] = 25 ValVec { Val { detail.bcIndex, detail.bcOffset, detail.cpIndex} }; 26 } else { 27 it->second.emplace_back(Val{ detail.bcIndex, detail.bcOffset, detail.cpIndex }); 28 } 29 recordNameToValCount_[detail.recordName]++; 30} 31 32uint32_t PGOBCInfo::Info::GetPGOExtendGTCount(const CString &recordName) const 33{ 34 auto it = recordNameToValCount_.find(recordName); 35 if (it != recordNameToValCount_.end()) { 36 return it->second; 37 } 38 return 0; 39} 40 41const PGOBCInfo::Info& PGOBCInfo::GetInfo(Type type) const 42{ 43 ASSERT(Type::TYPE_FIRST <= type && type <= Type::TYPE_LAST); 44 return infos_[type]; 45} 46 47uint32_t PGOBCInfo::GetPGOExtendGTCount(const CString &recordName) const 48{ 49 uint32_t count = 0; 50 for (const Info &info : infos_) { 51 count += info.GetPGOExtendGTCount(recordName); 52 } 53 return count; 54} 55 56void PGOBCInfo::Record(const InfoDetail &detail, Type type) 57{ 58 ASSERT(Type::TYPE_FIRST <= type && type <= Type::TYPE_LAST); 59 Info &info = infos_[type]; 60 info.Record(detail); 61} 62 63void PGOBCInfo::Record(const BytecodeInstruction &bcIns, int32_t bcIndex, 64 const CString &recordName, const MethodLiteral *method) 65{ 66 BytecodeInstruction::Opcode opcode = static_cast<BytecodeInstruction::Opcode>(bcIns.GetOpcode()); 67 uint32_t methodOffset = method->GetMethodId().GetOffset(); 68 uint32_t bcOffset = bcIns.GetAddress() - method->GetBytecodeArray(); 69 if (Bytecodes::IsCreateObjectWithBufferOp(opcode)) { 70 auto cpIndex = bcIns.GetId().AsRawValue(); 71 Record(InfoDetail {recordName, methodOffset, bcIndex, bcOffset, cpIndex}, Type::OBJ_LITERAL); 72 } else if (Bytecodes::IsCreateArrayWithBufferOp(opcode)) { 73 auto cpIndex = bcIns.GetId().AsRawValue(); 74 Record(InfoDetail {recordName, methodOffset, bcIndex, bcOffset, cpIndex}, Type::ARRAY_LITERAL); 75 } else if (Bytecodes::IsCreateEmptyArrayOp(opcode)) { 76 Record(InfoDetail {recordName, methodOffset, bcIndex, bcOffset, 0}, Type::EMPTY_ARRAY); 77 } else if (Bytecodes::IsCallOp(opcode)) { 78 Record(InfoDetail {recordName, methodOffset, bcIndex, bcOffset, 0}, Type::CALL_TARGET); 79 } else if (Bytecodes::IsDefineClassWithBufferOp(opcode)) { 80 Record(InfoDetail {recordName, methodOffset, bcIndex, bcOffset, 0}, Type::CLASS); 81 } else if (Bytecodes::IsDefineFunc(opcode)) { 82 Record(InfoDetail {recordName, methodOffset, bcIndex, bcOffset, 0}, Type::FUNCTION); 83 } 84} 85} // namespace panda::ecmascript 86