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_GRAPH_EDITOR_H
17#define ECMASCRIPT_COMPILER_GRAPH_EDITOR_H
18
19#include "ecmascript/compiler/circuit_builder.h"
20#include "ecmascript/compiler/gate_accessor.h"
21#include "ecmascript/mem/chunk_containers.h"
22
23namespace panda::ecmascript::kungfu {
24
25class GraphEditor {
26public:
27    GraphEditor(Circuit *circuit)
28        : circuit_(circuit), acc_(circuit),
29        chunk_(circuit->chunk()), workList_(circuit->chunk()) {}
30
31    ~GraphEditor() = default;
32
33    static void RemoveDeadState(Circuit* circuit, GateRef gate);
34    static void EliminateRedundantPhi(Circuit* circuit, bool enableLog, const std::string& methodName);
35private:
36    void ReplaceGate(GateRef gate);
37    void RemoveGate();
38    void PropagateGate(const Edge& edge);
39    void PropagateMerge(const Edge& edge);
40    void EliminatePhi();
41    bool HasOsrDeoptUse(GateRef gate);
42
43    Circuit *circuit_ {nullptr};
44    GateAccessor acc_;
45    Chunk* chunk_ {nullptr};
46    ChunkVector<Edge> workList_;
47};
48}  // panda::ecmascript::kungfu
49#endif  // ECMASCRIPT_COMPILER_GRAPH_EDITOR_H
50