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_VALUE_NUMBERING_H
17#define ECMASCRIPT_COMPILER_VALUE_NUMBERING_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/mem/chunk_containers.h"
23
24namespace panda::ecmascript::kungfu {
25class ValueNumbering : public PassVisitor {
26public:
27    ValueNumbering(Circuit *circuit, RPOVisitor *visitor, Chunk* chunk, bool useNewGVN, bool enableLog)
28        : PassVisitor(circuit, chunk, visitor), entries_(nullptr), useNewGVN_(useNewGVN),
29          enableLog_(enableLog) {}
30
31    ~ValueNumbering() = default;
32
33    GateRef VisitGate(GateRef gate) override;
34    bool CheckReplacement(GateRef lhs, GateRef rhs);
35    int GetoptimizedGateCount()
36    {
37        return optimizedGateCount;
38    }
39
40private:
41    void Grow();
42    void EnsureCapacity();
43    void InitEntries(size_t initSize);
44    size_t HashCode(GateRef gate);
45    void SetEntry(size_t hash, GateRef gate)
46    {
47        ASSERT(entriesLength_ > 0);
48        entries_[hash & (entriesLength_ - 1)] = gate;
49    }
50    static const uint32_t CACHE_LENGTH_BIT = 8;
51    static const uint32_t CACHE_LENGTH = (1U << CACHE_LENGTH_BIT);
52    const uint8_t LOAD_FACTOR_THRESHOLD = 4;
53    uint32_t entriesLength_ = (1U << CACHE_LENGTH_BIT);
54    uint32_t entriesSize_ = 0;
55    GateRef* entries_;
56    bool useNewGVN_;
57    int optimizedGateCount = 0;
58    bool enableLog_ = false;
59};
60}  // panda::ecmascript::kungfu
61#endif  // ECMASCRIPT_COMPILER_VALUE_NUMBERING_H