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