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_POST_SCHEDULE_H
17#define ECMASCRIPT_COMPILER_POST_SCHEDULE_H
18
19#include "ecmascript/compiler/circuit.h"
20#include "ecmascript/compiler/circuit_builder.h"
21#include "ecmascript/compiler/gate_accessor.h"
22#include "ecmascript/mem/chunk_containers.h"
23
24namespace panda::ecmascript::kungfu {
25class PostSchedule {
26public:
27    using ControlFlowGraph = std::vector<std::vector<GateRef>>;
28
29    PostSchedule(Circuit *circuit, bool enableLog, const std::string &name, Chunk *chunk, bool fastBarrier = false)
30        : enableLog_(enableLog), methodName_(name), chunk_(chunk), circuit_(circuit), builder_(circuit), acc_(circuit),
31          fastBarrier_(fastBarrier)
32    {
33    }
34
35    void Run(ControlFlowGraph &result);
36
37private:
38    bool IsLogEnabled() const
39    {
40        return enableLog_;
41    }
42
43    const std::string& GetMethodName() const
44    {
45        return methodName_;
46    }
47
48    void GenerateExtraBB(ControlFlowGraph &cfg);
49
50    bool VisitHeapAlloc(GateRef gate, ControlFlowGraph &cfg, size_t bbIdx, size_t instIdx);
51    bool VisitStore(GateRef gate, ControlFlowGraph &cfg, size_t bbIdx, size_t instIdx);
52
53    void ReplaceGateDirectly(std::vector<GateRef> &gates, ControlFlowGraph &cfg, size_t bbIdx, size_t instIdx);
54    void ScheduleEndBB(std::vector<GateRef> &gates, ControlFlowGraph &cfg, size_t bbIdx, size_t instIdx);
55    void ScheduleNewBB(std::vector<GateRef> &gates, ControlFlowGraph &cfg, size_t bbIdx);
56    void ScheduleCurrentBB(const std::vector<GateRef> &gates, ControlFlowGraph &cfg, size_t bbIdx, size_t instIdx);
57
58    void LoweringHeapAllocAndPrepareScheduleGate(GateRef gate,
59                                                 std::vector<GateRef> &currentBBGates,
60                                                 std::vector<GateRef> &successBBGates,
61                                                 std::vector<GateRef> &failBBGates,
62                                                 std::vector<GateRef> &endBBGates,
63                                                 int64_t flag);
64    void LoweringHeapAllocate(GateRef gate,
65                              std::vector<GateRef> &currentBBGates,
66                              std::vector<GateRef> &successBBGates,
67                              std::vector<GateRef> &failBBGates,
68                              std::vector<GateRef> &endBBGates,
69                              int64_t flag);
70
71    void LoweringStoreNoBarrierAndPrepareScheduleGate(GateRef gate, std::vector<GateRef> &currentBBGates);
72    void LoweringStoreWithBarrierAndPrepareScheduleGate(GateRef gate, std::vector<GateRef> &currentBBGates);
73    void LoweringStoreUnknownBarrierAndPrepareScheduleGate(GateRef gate,
74                                                           std::vector<GateRef> &currentBBGates,
75                                                           std::vector<GateRef> &barrierBBGates,
76                                                           std::vector<GateRef> &endBBGates);
77
78    void PrepareToScheduleNewGate(GateRef gate, std::vector<GateRef> &gates);
79    MemoryAttribute::Barrier GetWriteBarrierKind(GateRef gate);
80    void ReplaceBBState(ControlFlowGraph &cfg, size_t bbIdx, std::vector<GateRef> &currentBBGates,
81                        std::vector<GateRef> &endBBGates);
82    MemoryAttribute::ShareFlag GetShareKind(panda::ecmascript::kungfu::GateRef gate);
83
84    int SelectBarrier(MemoryAttribute::ShareFlag share, const CallSignature*& cs, std::string_view& comment);
85
86    void PrintGraph(const char* title, ControlFlowGraph &cfg);
87
88    bool enableLog_ {false};
89    std::string methodName_;
90    [[maybe_unused]]Chunk* chunk_ {nullptr};
91    Circuit* circuit_ {nullptr};
92    CircuitBuilder builder_;
93    GateAccessor acc_;
94    bool fastBarrier_ {false};
95};
96};  // namespace panda::ecmascript::kungfu
97
98#endif  // ECMASCRIPT_COMPILER_POST_SCHEDULE_H
99