1/*
2 * Copyright (c) 2021 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_SCHEDULER_H
17#define ECMASCRIPT_COMPILER_SCHEDULER_H
18
19#include "ecmascript/compiler/circuit.h"
20
21namespace panda::ecmascript::kungfu {
22class Scheduler {
23public:
24    using ControlFlowGraph = std::vector<std::vector<GateRef>>;
25
26    static void CalculateDominatorTree(const Circuit *circuit, std::vector<GateRef>& bbGatesList,
27                                       std::unordered_map<GateRef, size_t> &bbGatesAddrToIdx,
28                                       std::vector<size_t> &immDom);
29
30    static void Run(const Circuit *circuit, ControlFlowGraph &result,
31                    [[maybe_unused]] const std::string& methodName = "", bool enableLog = false);
32
33    static bool CalculateSchedulingUpperBound(const Circuit *circuit,
34                                              const std::unordered_map<GateRef, size_t> &bbGatesAddrToIdx,
35                                              const std::function<bool(size_t, size_t)> &isAncestor,
36                                              const std::vector<GateRef> &schedulableGatesList,
37                                              std::unordered_map<GateRef, size_t> &upperBound);
38
39    static void CalculateSchedulingLowerBound(const Circuit *circuit,
40                                              const std::unordered_map<GateRef, size_t> &bbGatesAddrToIdx,
41                                              const std::function<size_t(size_t, size_t)> &lowestCommonAncestor,
42                                              std::unordered_map<GateRef, size_t> &lowerBound,
43                                              std::vector<GateRef> *order = nullptr);
44
45    static void Print(const ControlFlowGraph *cfg, const Circuit *circuit);
46
47private:
48    static void PrintUpperBoundError(const Circuit *circuit, GateRef curGate,
49                                     GateRef predUpperBound, GateRef curUpperBound);
50    static void CalculateFixedGatesList(const Circuit *circuit,
51                                        const std::unordered_map<GateRef, size_t> &bbGatesAddrToIdx,
52                                        std::vector<GateRef> &bbAndFixedGatesList);
53};
54};  // namespace panda::ecmascript::kungfu
55
56#endif  // ECMASCRIPT_COMPILER_SCHEDULER_H
57