• 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), builder_(builder), methodName_(name),
29           enableLog_(enableLog), profiler_(chunk) {}
30 
31     ~PGOTypeInfer() = default;
32 
33     void Run();
34 
35 private:
36     struct Profiler {
37         struct Value {
38             GateRef gate;
39             GateType tsType;
40             CVector<GateType> pgoTypes;
41             CVector<GateType> inferTypes;
42         };
ProfilerProfiler43         Profiler(Chunk *chunk) : datas(chunk) {}
44         ChunkVector<Value> datas;
45     };
46 
IsLogEnabled()47     inline bool IsLogEnabled() const
48     {
49         return enableLog_;
50     }
51 
GetMethodName()52     inline const std::string &GetMethodName() const
53     {
54         return methodName_;
55     }
56 
IsMonoTypes(const ChunkSet<GateType> & types)57     inline bool IsMonoTypes(const ChunkSet<GateType> &types) const
58     {
59         return types.size() == 1;
60     }
61 
IsMonoNumberType(const PGORWOpType & pgoTypes)62     inline bool IsMonoNumberType(const PGORWOpType &pgoTypes) const
63     {
64         // "ldobjbyvalue" will collect the type of the variable inside the square brackets while pgo collecting.
65         // If the type is "number", it will be marked as an "Element".
66         return pgoTypes.GetCount() == 1 && pgoTypes.GetObjectInfo(0).InElement();
67     }
68 
69     void RunTypeInfer(GateRef gate);
70     void InferLdObjByName(GateRef gate);
71     void InferStOwnByIndex(GateRef gate);
72     void InferAccessObjByValue(GateRef gate);
73     void InferCreateArray(GateRef gate);
74 
75     void TrySetElementsKind(GateRef gate);
76     void TrySetTransitionElementsKind(GateRef gate);
77     void TrySetPropKeyKind(GateRef gate, GateRef propKey);
78     void TrySetOnHeapMode(GateRef gate);
79 
80     Circuit *circuit_ {nullptr};
81     GateAccessor acc_;
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