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/gate_accessor.h" 20 #include "ecmascript/ts_types/ts_manager.h" 21 #include "ecmascript/compiler/argument_accessor.h" 22 23 namespace panda::ecmascript::kungfu { 24 struct CollectedType; 25 class PGOTypeInfer { 26 public: PGOTypeInfer(Circuit * circuit,TSManager * tsManager,BytecodeCircuitBuilder * builder,const std::string & name,Chunk * chunk,bool enableLog)27 PGOTypeInfer(Circuit *circuit, TSManager *tsManager, BytecodeCircuitBuilder *builder, 28 const std::string &name, Chunk *chunk, bool enableLog) 29 : circuit_(circuit), acc_(circuit), argAcc_(circuit), tsManager_(tsManager), 30 builder_(builder), methodName_(name), chunk_(chunk), enableLog_(enableLog), profiler_(chunk) {} 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 NoNeedUpdate(const ChunkSet<GateType> & types)62 inline bool NoNeedUpdate(const ChunkSet<GateType> &types) const 63 { 64 return types.size() <= 1; 65 } 66 67 void RunTypeInfer(GateRef gate); 68 void InferLdObjByName(GateRef gate); 69 void InferStObjByName(GateRef gate, bool isThis); 70 void InferStOwnByName(GateRef gate); 71 void InferAccessObjByValue(GateRef gate); 72 73 void UpdateTypeForRWOp(GateRef gate, GateRef receiver, JSTaggedValue prop); 74 void CollectGateType(CollectedType &types, GateType tsType, PGORWOpType pgoTypes); 75 void UpdateType(CollectedType &types, JSTaggedValue prop); 76 void InferTypeForClass(ChunkSet<GateType> &types, JSTaggedValue prop); 77 void CheckAndInsert(CollectedType &types, GateType type); 78 void EliminateSubclassTypes(ChunkSet<GateType> &types); 79 void ComputeCommonSuperClassTypes(ChunkSet<GateType> &types, JSTaggedValue prop); 80 81 void Print() const; 82 void AddProfiler(GateRef gate, GateType tsType, PGORWOpType pgoType, ChunkSet<GateType>& inferTypes); 83 84 Circuit *circuit_ {nullptr}; 85 GateAccessor acc_; 86 ArgumentAccessor argAcc_; 87 TSManager *tsManager_ {nullptr}; 88 BytecodeCircuitBuilder *builder_ {nullptr}; 89 const std::string &methodName_; 90 Chunk *chunk_ {nullptr}; 91 bool enableLog_ {false}; 92 Profiler profiler_; 93 }; 94 } // panda::ecmascript::kungfu 95 #endif // ECMASCRIPT_COMPILER_TYPE_INFERENCE_PGO_TYPE_INFER_H 96