• 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 #ifndef ECMASCRIPT_COMPILER_PGO_TYPE_PGO_TYPE_MANAGER_H
16 #define ECMASCRIPT_COMPILER_PGO_TYPE_PGO_TYPE_MANAGER_H
17 
18 #include "ecmascript/compiler/aot_snapshot/aot_snapshot.h"
19 #include "ecmascript/compiler/pgo_type/pgo_type_location.h"
20 
21 namespace panda::ecmascript::kungfu {
22 class PGOTypeManager {
23 public:
PGOTypeManager(EcmaVM * vm)24     PGOTypeManager(EcmaVM *vm)
25         : thread_(vm->GetJSThread()), aotSnapshot_(vm) {}
26     ~PGOTypeManager() = default;
27 
28     static int32_t GetConstantPoolIDByMethodOffset(const JSPandaFile *jsPandaFile, uint32_t methodOffset);
29 
30     void Iterate(const RootVisitor &v);
31 
32     // common
GetJSThread()33     JSThread* GetJSThread()
34     {
35         return thread_;
36     }
37 
38     void SetCurConstantPool(const JSPandaFile *jsPandaFile, uint32_t methodOffset);
39 
GetCurConstantPool()40     JSHandle<JSTaggedValue> GetCurConstantPool() const
41     {
42         return JSHandle<JSTaggedValue>(uintptr_t(&curCP_));
43     }
44 
45     // snapshot
46     void InitAOTSnapshot(uint32_t compileFilesCount);
47     void GenArrayInfo();
48     void GenConstantIndexInfo();
49 
GetAOTSnapshot()50     AOTSnapshot& GetAOTSnapshot()
51     {
52         return aotSnapshot_;
53     }
54 
GetArrayInfo()55     CVector<JSTaggedType>& GetArrayInfo()
56     {
57         return arrayData_;
58     }
59 
60     // array
61     void RecordElements(panda_file::File::EntityId id, JSTaggedValue elements);
62 
63     void RecordConstantIndex(uint32_t bcAbsoluteOffset, uint32_t index);
64 
65     int GetElementsIndexByEntityId(panda_file::File::EntityId id);
66 
67     // hclass
68     void RecordHClass(ProfileType rootType, ProfileType childType, JSTaggedType hclass);
69 
70     uint32_t GetHClassIndexByProfileType(ProfileTyper type) const;
71 
72     JSTaggedValue QueryHClass(ProfileType rootType, ProfileType childType) const;
73     ElementsKind QueryElementKind(ProfileType rootType);
74 
GetRootIdByLocation(const PGOTypeLocation & loc)75     inline ProfileType GetRootIdByLocation(const PGOTypeLocation &loc)
76     {
77         auto it = locToRootIdMap_.find(loc);
78         if (it != locToRootIdMap_.end()) {
79             return it->second;
80         }
81         return ProfileType::PROFILE_TYPE_NONE;
82     }
83 
GetElementsKindByLocation(PGOTypeLocation loc)84     inline ElementsKind GetElementsKindByLocation(PGOTypeLocation loc)
85     {
86         auto it = locToElmsKindMap_.find(loc);
87         if (it != locToElmsKindMap_.end()) {
88             return it->second;
89         }
90         return ElementsKind::GENERIC;
91     }
92 
RecordLocationToRootType(const PGOTypeLocation & loc,ProfileType rootType)93     inline void RecordLocationToRootType(const PGOTypeLocation &loc, ProfileType rootType)
94     {
95         locToRootIdMap_.emplace(loc, rootType);
96     }
97 
RecordLocationToElementsKind(PGOTypeLocation loc,ElementsKind kind)98     inline void RecordLocationToElementsKind(PGOTypeLocation loc, ElementsKind kind)
99     {
100         locToElmsKindMap_.emplace(loc, kind);
101     }
102 
103 private:
104     // snapshot
105     void GenHClassInfo();
106 
107     // opt to std::unordered_map
108     using TransIdToHClass = std::map<ProfileType, JSTaggedType>;
109     using RootIdToTransMap = std::map<ProfileType, TransIdToHClass>;
110 
111     JSThread *thread_;
112     RootIdToTransMap hcData_;
113     CVector<JSTaggedType> arrayData_ {};
114     CVector<uint32_t> constantIndexData_ {};
115     CUnorderedMap<PGOTypeLocation, ProfileType, HashPGOTypeLocation> locToRootIdMap_ {};
116     CUnorderedMap<PGOTypeLocation, ElementsKind, HashPGOTypeLocation> locToElmsKindMap_ {};
117     CMap<ProfileTyper, uint32_t> profileTyperToHClassIndex_ {};
118     std::map<panda_file::File::EntityId, uint32_t> idElmsIdxMap_ {};
119     AOTSnapshot aotSnapshot_;
120 
121     // When the passmanager iterates each method, the curCP_ and curCPID_ should be updated,
122     // so that subsequent passes (type_infer, ts_hcr_lowering) can obtain the correct constpool.
123     JSTaggedValue curCP_ {JSTaggedValue::Hole()};
124     int32_t curCPID_ {0};
125 };
126 }  // panda::ecmascript::kungfu
127 #endif // ECMASCRIPT_COMPILER_PGO_TYPE_PGO_TYPE_MANAGER_H
128