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