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