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