• 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         EMPTY_ARRAY,
29         CALL_TARGET,
30         CLASS,
31         FUNCTION,
32 
33         TYPE_NUM,
34         TYPE_FIRST = OBJ_LITERAL,
35         TYPE_LAST = FUNCTION,
36     };
37 
38     struct InfoDetail {
39         const CString &recordName;
40         uint32_t methodOffset;
41         uint32_t bcIndex;
42         uint32_t bcOffset;
43         uint32_t cpIndex;
44     };
45 
46     class Info {
47     public:
48         void Record(const InfoDetail &detail);
49 
50         uint32_t GetPGOExtendGTCount(const CString &recordName) const;
51 
52         template <class Callback>
IterateValByMethodOffset(uint32_t methodOffset,Type type,const Callback & cb)53         void IterateValByMethodOffset(uint32_t methodOffset, Type type, const Callback &cb) const
54         {
55             if (methodOffsetToValVec_.find(methodOffset) != methodOffsetToValVec_.end()) {
56                 const ValVec &valVec = methodOffsetToValVec_.at(methodOffset);
57                 for (auto val : valVec) {
58                     cb(type, val.bcIndex, val.bcOffset, val.cpIndex);
59                 }
60             }
61         }
62 
63         template <class Callback>
IterateValByMethodOffsetAndType(uint32_t methodOffset,const Callback & cb)64         void IterateValByMethodOffsetAndType(uint32_t methodOffset, const Callback &cb) const
65         {
66             if (methodOffsetToValVec_.find(methodOffset) != methodOffsetToValVec_.end()) {
67                 const ValVec &valVec = methodOffsetToValVec_.at(methodOffset);
68                 for (auto val : valVec) {
69                     cb(val.bcIndex, val.bcOffset, val.cpIndex);
70                 }
71             }
72         }
73 
74     private:
75         struct Val {
76             uint32_t bcIndex;
77             uint32_t bcOffset;
78             uint32_t cpIndex;
79         };
80         using ValVec = CVector<Val>;
81         CMap<CString, uint32_t> recordNameToValCount_;
82         CUnorderedMap<uint32_t, ValVec> methodOffsetToValVec_;
83     };
84 
PGOBCInfo()85     PGOBCInfo() : infos_(Type::TYPE_NUM, Info{}) {}
86     ~PGOBCInfo() = default;
87 
88     const Info& GetInfo(Type type) const;
89 
90     uint32_t GetPGOExtendGTCount(const CString &recordName) const;
91 
92     void Record(const BytecodeInstruction &bcIns, int32_t bcIndex,
93                 const CString &recordName, const MethodLiteral *method);
94 
95     template <class Callback>
IterateInfoAndType(uint32_t methodOffset,const Callback & cb)96     void IterateInfoAndType(uint32_t methodOffset, const Callback &cb) const
97     {
98         for (size_t idx = 0; idx < Type::TYPE_NUM; ++idx) {
99             Type type = static_cast<Type>(idx);
100             infos_[idx].IterateValByMethodOffset(methodOffset, type, cb);
101         }
102     }
103 
104     template <class Callback>
IterateInfoByType(uint32_t methodOffset,Type type,const Callback & cb)105     void IterateInfoByType(uint32_t methodOffset, Type type, const Callback &cb) const
106     {
107         infos_[type].IterateValByMethodOffsetAndType(methodOffset, cb);
108     }
109 private:
110     void Record(const InfoDetail &detail, Type type);
111 
112     CVector<Info> infos_;
113 };
114 }  // panda::ecmascript::kungfu
115 #endif  // ECMASCRIPT_COMPILER_PGO_BC_INFO_RECORDER_H
116