• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ECMASCRIPT_COMPILER_PGO_BC_INFO_RECORDER_H
17 #define ECMASCRIPT_COMPILER_PGO_BC_INFO_RECORDER_H
18 
19 #include "ecmascript/jspandafile/method_literal.h"
20 #include "ecmascript/mem/c_containers.h"
21 
22 namespace panda::ecmascript::kungfu {
23 class PGOBCInfo {
24 public:
25     enum Type {
26         OBJ_LITERAL = 0,
27         ARRAY_LITERAL,
28         CALL_TARGET,
29         TYPE_NUM,
30         TYPE_FIRST = OBJ_LITERAL,
31         TYPE_LAST = CALL_TARGET,
32     };
33 
34     struct InfoDetail {
35         const CString &recordName;
36         uint32_t methodOffset;
37         uint32_t bcIndex;
38         uint32_t bcOffset;
39         uint32_t cpIndex;
40     };
41 
42     class Info {
43     public:
44         void Record(const InfoDetail &detail);
45 
46         uint32_t GetPGOExtendGTCount(const CString &recordName) const;
47 
48         template <class Callback>
IterateValByMethodOffset(uint32_t methodOffset,Type type,const Callback & cb)49         void IterateValByMethodOffset(uint32_t methodOffset, Type type, const Callback &cb) const
50         {
51             if (methodOffsetToValVec_.find(methodOffset) != methodOffsetToValVec_.end()) {
52                 const ValVec &valVec = methodOffsetToValVec_.at(methodOffset);
53                 for (auto val : valVec) {
54                     cb(type, val.bcIndex, val.bcOffset, val.cpIndex);
55                 }
56             }
57         }
58     private:
59         struct Val {
60             uint32_t bcIndex;
61             uint32_t bcOffset;
62             uint32_t cpIndex;
63         };
64         using ValVec = CVector<Val>;
65         CMap<CString, uint32_t> recordNameToValCount_;
66         CUnorderedMap<uint32_t, ValVec> methodOffsetToValVec_;
67     };
68 
PGOBCInfo()69     PGOBCInfo() : infos_(Type::TYPE_NUM, Info{}) {}
70     ~PGOBCInfo() = default;
71 
72     const Info& GetInfo(Type type) const;
73 
74     uint32_t GetPGOExtendGTCount(const CString &recordName) const;
75 
76     void Record(const BytecodeInstruction &bcIns, int32_t bcIndex,
77                 const CString &recordName, const MethodLiteral *method);
78 
79     template <class Callback>
IterateInfoAndType(uint32_t methodOffset,const Callback & cb)80     void IterateInfoAndType(uint32_t methodOffset, const Callback &cb) const
81     {
82         for (size_t idx = 0; idx < Type::TYPE_NUM; ++idx) {
83             Type type = static_cast<Type>(idx);
84             infos_[idx].IterateValByMethodOffset(methodOffset, type, cb);
85         }
86     }
87 private:
88     void Record(const InfoDetail &detail, Type type);
89 
90     CVector<Info> infos_;
91 };
92 }  // panda::ecmascript::kungfu
93 #endif  // ECMASCRIPT_COMPILER_PGO_BC_INFO_RECORDER_H
94