• 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_NUMBER_SPECULATIVE_RETYPE_H
17 #define ECMASCRIPT_COMPILER_NUMBER_SPECULATIVE_RETYPE_H
18 
19 #include "ecmascript/compiler/circuit_builder.h"
20 #include "ecmascript/compiler/combined_pass_visitor.h"
21 #include "ecmascript/compiler/gate_accessor.h"
22 #include "ecmascript/compiler/share_gate_meta_data.h"
23 #include "ecmascript/compiler/number_gate_info.h"
24 #include "ecmascript/compiler/type.h"
25 #include "ecmascript/mem/chunk_containers.h"
26 
27 namespace panda::ecmascript::kungfu {
28 class NumberSpeculativeRetype {
29 public:
30     enum class State {
31         Retype,
32         Convert,
33     };
NumberSpeculativeRetype(Circuit * circuit,Chunk * chunk,ChunkVector<TypeInfo> & typeInfos)34     NumberSpeculativeRetype(Circuit* circuit, Chunk* chunk, ChunkVector<TypeInfo>& typeInfos)
35         : circuit_(circuit), acc_(circuit), builder_(circuit),
36           typeInfos_(typeInfos), chunk_(chunk) {}
37     GateRef VisitGate(GateRef gate);
38     void setState(NumberSpeculativeRetype::State state);
39 
40 private:
41 
42     enum class OpType {
43         NORMAL,
44         SHIFT_AND_LOGICAL,
45     };
46 
47     enum class ConvertToNumber {
48         DISABLE,
49         BOOL_ONLY,
50         ALL
51     };
52 
ToConvertSupport(ConvertToNumber convert)53     ConvertSupport ToConvertSupport(ConvertToNumber convert)
54     {
55         return convert == ConvertToNumber::DISABLE ? ConvertSupport::DISABLE : ConvertSupport::ENABLE;
56     }
57 
IsRetype()58     bool IsRetype() const
59     {
60         return state_ == State::Retype;
61     }
62 
IsConvert()63     bool IsConvert() const
64     {
65         return state_ == State::Convert;
66     }
67 
68     GateRef SetOutputType(GateRef gate, GateType type);
69     GateRef SetOutputType(GateRef gate, ParamType type);
70     GateRef SetOutputType(GateRef gate, Representation rep);
71     GateRef SetOutputType(GateRef gate, TypeInfo type);
72     TypeInfo GetNumberTypeInfo(GateRef gate);
73     GateRef VisitPhi(GateRef gate);
74     GateRef VisitArrayFindOrFindIndex(GateRef gate);
75     GateRef VisitConstant(GateRef gate);
76     GateRef VisitTypedBinaryOp(GateRef gate);
77     GateRef VisitNumberBinaryOp(GateRef gate);
78     GateRef VisitStringBinaryOp(GateRef gate);
79     GateRef VisitUndefinedEqualCompareOrUndefinedNotEqualCompare(GateRef gate);
80     GateRef VisitTypedUnaryOp(GateRef gate);
81 
82     GateRef VisitNumberMonocular(GateRef gate);
83     GateRef VisitNumberNot(GateRef gate);
84     GateRef VisitTypedConditionJump(GateRef gate);
85 
86     GateRef VisitIntMonocular(GateRef gate);
87     GateRef VisitDoubleMonocular(GateRef gate);
88 
89     GateRef VisitNumberCalculate(GateRef gate);
90     GateRef VisitNumberCompare(GateRef gate);
91     GateRef VisitNumberShiftAndLogical(GateRef gate);
92     GateRef VisitNumberToString(GateRef gate);
93     GateRef VisitNumberParseFloat(GateRef gate);
94     GateRef VisitNumberParseInt(GateRef gate);
95     GateRef VisitMathDoubleParamsBuiltin(GateRef gate);
96     const GateMetaData *GetNewMeta(OpCode op, TypeInfo type);
97     void UpdateMeta(GateRef gate, TypeInfo newType, const GateMetaData *meta);
98     GateRef VisitMathTaggedNumberParamsBuiltin(GateRef gate);
99     GateRef VisitClz32Builtin(GateRef gate);
100     GateRef VisitMathTrunc(GateRef gate);
101     GateRef VisitMathImul(GateRef gate);
102     template <bool IS_NAN>
103     GateRef VisitNumberOrGlobalBuiltin(GateRef gate);
104     GateRef VisitNumberIsInteger(GateRef gate);
105     GateRef VisitBigIntAsIntN(GateRef gate);
106     GateRef VisitBooleanJump(GateRef gate);
107     GateRef VisitRangeCheckPredicate(GateRef gate);
108     GateRef VisitIndexCheck(GateRef gate);
109     GateRef VisitLoadArrayLength(GateRef gate);
110     GateRef VisitLoadStringLength(GateRef gate);
111     GateRef VisitLoadMapSize(GateRef gate);
112     GateRef VisitLoadElement(GateRef gate);
113     GateRef VisitStoreElement(GateRef gate);
114     GateRef VisitStoreProperty(GateRef gate);
115     GateRef VisitLoadProperty(GateRef gate);
116     GateRef VisitNumberRelated(GateRef gate, ParamType paramType);
117     GateRef VisitDataViewGet(GateRef gate);
118     GateRef VisitDataViewSet(GateRef gate);
119     GateRef VisitOthers(GateRef gate, GateType outputType = GateType::AnyType());
120     GateRef VisitOthersWithoutConvert(GateRef gate, GateType outputType = GateType::AnyType());
121     GateRef VisitBigIntConstructor(GateRef gate);
122     GateRef VisitTypeConvert(GateRef gate);
123     GateRef VisitFrameState(GateRef gate);
124     GateRef VisitIsTrueOrFalse(GateRef gate);
125     GateRef VisitWithConstantValue(GateRef gate, size_t ignoreIndex);
126     GateRef VisitIntermediateValue(GateRef gate);
127     GateRef VisitEqualCompareOrNotEqualCompare(GateRef gate);
128 
129     GateRef VisitStringCompare(GateRef gate);
130     GateRef VisitStringAdd(GateRef gate);
131     GateRef VisitMonoLoadPropertyOnProto(GateRef gate);
132     GateRef VisitMonoCallGetterOnProto(GateRef gate);
133     GateRef VisitMonoStoreProperty(GateRef gate);
134     GateRef VisitDateGetTime(GateRef gate);
135     GateRef VisitDateNow(GateRef gate);
136     GateRef VisitArrayIncludesIndexOf(GateRef gate);
137 
138     void ConvertForNumberBinaryOp(GateRef gate);
139     void ConvertForNumberCompareOp(GateRef gate);
140     void ConvertForNumberShiftAndLogicalOperator(GateRef gate);
141 
142     void ConvertForIntOperator(GateRef gate, GateType leftType, GateType rightType);
143     void ConvertForShiftAndLogicalOperator(GateRef gate, GateType leftType, GateType rightType);
144     void ConvertForDoubleOperator(GateRef gate, GateType leftType, GateType rightType);
145 
146     template<bool BOOL_AS_INT = true>
147     TypeInfo GetNumberInputTypeInfo(GateRef gate, bool skipTagged = false);
148     void SetNewInputForMathImul(GateRef gate, int idx, Label *exit);
149     double GetDoubleValueFromConst(GateRef gate);
150 
151     GateRef CheckAndConvertToInt32(GateRef gate, GateType gateType, ConvertSupport support = ConvertSupport::ENABLE,
152                                    OpType type = OpType::NORMAL);
153     GateRef CheckTaggedAndConvertToInt32(GateRef gate, GateType gateType, OpType type);
154     GateRef CheckBoundAndConvertToInt32(GateRef gate,
155                                         ConvertSupport support = ConvertSupport::ENABLE,
156                                         OpType type = OpType::NORMAL);
157     GateRef CheckAndConvertToFloat64(GateRef gate, GateType gateType,
158                                     ConvertToNumber convert = ConvertToNumber::BOOL_ONLY);
159     GateRef CheckAndConvertToTagged(GateRef gate, GateType gateType, ConvertToNumber convert);
160     GateRef CheckAndConvertToBool(GateRef gate, GateType gateType);
161     GateRef ConvertToTagged(GateRef gate);
162     GateRef TryConvertConstant(GateRef gate, bool needInt32);
163     GateRef TryConvertConstantToInt32(GateRef gate);
164     GateRef ConvertTaggedToNJSValue(GateRef gate, TypeInfo output);
165     TypeInfo GetOutputForPhi(GateRef gate, bool ignoreConstant);
166 
GetOutputTypeInfo(GateRef gate)167     TypeInfo GetOutputTypeInfo(GateRef gate) const
168     {
169         auto index = acc_.GetId(gate);
170         ASSERT(index < typeInfos_.size());
171         return typeInfos_[index];
172     }
173 
SetOutputTypeInfo(GateRef gate,TypeInfo info)174     void SetOutputTypeInfo(GateRef gate, TypeInfo info)
175     {
176         auto index = acc_.GetId(gate);
177         ASSERT(index < typeInfos_.size());
178         typeInfos_[index] = info;
179     }
180 
ResizeAndSetTypeInfo(GateRef gate,TypeInfo info)181     void ResizeAndSetTypeInfo(GateRef gate, TypeInfo info)
182     {
183         auto index = acc_.GetId(gate);
184         if (index >= typeInfos_.size()) {
185             // +1: for size
186             typeInfos_.resize(index + 1, TypeInfo::NONE);
187         }
188         typeInfos_[index] = info;
189     }
190 
191     static constexpr size_t PROPERTY_LOOKUP_RESULT_INDEX = 1;
192     static constexpr size_t HCLASS_INDEX = 2;
193     Circuit *circuit_ {nullptr};
194     GateAccessor acc_;
195     CircuitBuilder builder_;
196     ChunkVector<TypeInfo>& typeInfos_;
197     State state_ {0};
198     [[maybe_unused]] Chunk *chunk_ {nullptr};
199 };
200 
201 class NumberSpeculativeRetypeManager : public PassVisitor {
202 public:
NumberSpeculativeRetypeManager(Circuit * circuit,RPOVisitor * visitor,Chunk * chunk,NumberSpeculativeRetype * retype,NumberSpeculativeRetype::State state)203     NumberSpeculativeRetypeManager(Circuit* circuit, RPOVisitor* visitor, Chunk* chunk,
204                                    NumberSpeculativeRetype* retype, NumberSpeculativeRetype::State state)
205         : PassVisitor(circuit, chunk, visitor), retype_(retype), state_(state) {}
206     GateRef VisitGate(GateRef gate) override;
207 private:
208     NumberSpeculativeRetype* retype_;
209     NumberSpeculativeRetype::State state_;
210 };
211 
212 }  // panda::ecmascript::kungfu
213 #endif  // ECMASCRIPT_COMPILER_NUMBER_SPECULATIVE_RETYPE_H
214