• 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_TYPE_INFERENCE_PGO_TYPE_INFER_H
17 #define ECMASCRIPT_COMPILER_TYPE_INFERENCE_PGO_TYPE_INFER_H
18 
19 #include "ecmascript/compiler/argument_accessor.h"
20 #include "ecmascript/compiler/gate_accessor.h"
21 
22 namespace panda::ecmascript::kungfu {
23 struct CollectedType;
24 class PGOTypeInfer {
25 public:
PGOTypeInfer(Circuit * circuit,BytecodeCircuitBuilder * builder,const std::string & name,Chunk * chunk,bool enableLog)26     PGOTypeInfer(Circuit *circuit, BytecodeCircuitBuilder *builder, const std::string &name,
27                  Chunk *chunk, bool enableLog)
28         : circuit_(circuit), acc_(circuit), argAcc_(circuit), builder_(builder), methodName_(name),
29           enableLog_(enableLog), profiler_(chunk) {}
30     ~PGOTypeInfer() = default;
31 
32     void Run();
33 
34 private:
35     struct Profiler {
36         struct Value {
37             GateRef gate;
38             GateType tsType;
39             CVector<GateType> pgoTypes;
40             CVector<GateType> inferTypes;
41         };
ProfilerProfiler42         Profiler(Chunk *chunk) : datas(chunk) {}
43         ChunkVector<Value> datas;
44     };
45 
IsLogEnabled()46     inline bool IsLogEnabled() const
47     {
48         return enableLog_;
49     }
50 
GetMethodName()51     inline const std::string &GetMethodName() const
52     {
53         return methodName_;
54     }
55 
IsMonoTypes(const ChunkSet<GateType> & types)56     inline bool IsMonoTypes(const ChunkSet<GateType> &types) const
57     {
58         return types.size() == 1;
59     }
60 
IsMonoNumberType(const PGORWOpType & pgoTypes)61     inline bool IsMonoNumberType(const PGORWOpType &pgoTypes) const
62     {
63         // "ldobjbyvalue" will collect the type of the variable inside the square brackets while pgo collecting.
64         // If the type is "number", it will be marked as an "Element".
65         return pgoTypes.GetCount() == 1 && pgoTypes.GetObjectInfo(0).InElement();
66     }
67 
68     void RunTypeInfer(GateRef gate);
69     void InferLdObjByName(GateRef gate);
70     void InferStOwnByIndex(GateRef gate);
71     void InferAccessObjByValue(GateRef gate);
72     void InferCreateArray(GateRef gate);
73 
74     void TrySetElementsKind(GateRef gate);
75     void TrySetTransitionElementsKind(GateRef gate);
76     void TrySetPropKeyKind(GateRef gate, GateRef propKey);
77     void TrySetOnHeapMode(GateRef gate);
78 
79     Circuit *circuit_ {nullptr};
80     GateAccessor acc_;
81     ArgumentAccessor argAcc_;
82     BytecodeCircuitBuilder *builder_ {nullptr};
83     const std::string &methodName_;
84     bool enableLog_ {false};
85     Profiler profiler_;
86 };
87 }  // panda::ecmascript::kungfu
88 #endif  // ECMASCRIPT_COMPILER_TYPE_INFERENCE_PGO_TYPE_INFER_H
89