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_RANGE_ANALYSIS_H 17 #define ECMASCRIPT_COMPILER_RANGE_ANALYSIS_H 18 19 #include "ecmascript/compiler/circuit_builder.h" 20 #include "ecmascript/compiler/gate_accessor.h" 21 #include "ecmascript/compiler/gate_meta_data.h" 22 #include "ecmascript/compiler/graph_visitor.h" 23 #include "ecmascript/compiler/number_gate_info.h" 24 #include "ecmascript/mem/chunk_containers.h" 25 26 namespace panda::ecmascript::kungfu { 27 class RangeAnalysis : public GraphVisitor { 28 public: RangeAnalysis(Circuit * circuit,Chunk * chunk,ChunkVector<TypeInfo> & typeInfos,ChunkVector<RangeInfo> & rangeInfos)29 RangeAnalysis(Circuit *circuit, Chunk* chunk, ChunkVector<TypeInfo>& typeInfos, 30 ChunkVector<RangeInfo>& rangeInfos) 31 : GraphVisitor(circuit, chunk), acc_(circuit), builder_(circuit), 32 typeInfos_(typeInfos), rangeInfos_(rangeInfos) {} 33 void Run(); 34 GateRef VisitGate(GateRef gate); 35 void PrintRangeInfo() const; 36 37 private: 38 GateRef VisitPhi(GateRef gate); 39 GateRef VisitTypedBinaryOp(GateRef gate); 40 GateRef VisitTypedUnaryOp(GateRef gate); 41 GateRef VisitConstant(GateRef gate); 42 GateRef VisitOthers(GateRef gate); 43 GateRef VisitIndexCheck(GateRef gate); 44 GateRef VisitLoadArrayLength(GateRef gate); 45 GateRef VisitLoadTypedArrayLength(GateRef gate); 46 GateRef VisitRangeGuard(GateRef gate); 47 template<TypedBinOp Op> 48 RangeInfo GetRangeOfCalculate(GateRef gate); 49 template<TypedBinOp Op> 50 RangeInfo GetRangeOfShift(GateRef gate); 51 RangeInfo TryGetRangeOfBranch(GateRef state, GateRef value); 52 RangeInfo GetRangeOfCompare(GateRef gate, GateRef value, bool flag); 53 GateRef UpdateRange(GateRef gate, const RangeInfo& info); 54 RangeInfo GetRange(GateRef gate) const; 55 bool IsInt32Type(GateRef gate) const; 56 GateAccessor acc_; 57 CircuitBuilder builder_; 58 ChunkVector<TypeInfo>& typeInfos_; 59 ChunkVector<RangeInfo>& rangeInfos_; 60 }; 61 } // panda::ecmascript::kungfu 62 #endif // ECMASCRIPT_COMPILER_RANGE_ANALYSIS_H 63