14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_COMPILER_COMBINED_PASS_VISITOR_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_COMBINED_PASS_VISITOR_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include "ecmascript/compiler/circuit_builder.h" 204514f5e3Sopenharmony_ci#include "ecmascript/compiler/gate_accessor.h" 214514f5e3Sopenharmony_ci#include "ecmascript/mem/chunk_containers.h" 224514f5e3Sopenharmony_ci 234514f5e3Sopenharmony_cinamespace panda::ecmascript::kungfu { 244514f5e3Sopenharmony_ci 254514f5e3Sopenharmony_ciclass RPOVisitor { 264514f5e3Sopenharmony_cipublic: 274514f5e3Sopenharmony_ci virtual ~RPOVisitor() = default; 284514f5e3Sopenharmony_ci virtual int32_t GetGateOrder(GateRef gate) = 0; 294514f5e3Sopenharmony_ci virtual void SetGateOrder(GateRef gate, int32_t orderId) = 0; 304514f5e3Sopenharmony_ci virtual void Resize(int32_t size, int32_t num) = 0; 314514f5e3Sopenharmony_ci virtual void ReVisitGate(GateRef gate) = 0; 324514f5e3Sopenharmony_ci virtual void LogicallyReplaceGate(GateRef gate, GateRef replacement) = 0; 334514f5e3Sopenharmony_ci virtual void RelaxStateAndDepend(GateRef gate) = 0; 344514f5e3Sopenharmony_ci virtual void ReplaceGate(GateRef gate, GateRef replacement) = 0; 354514f5e3Sopenharmony_ci virtual void ReplaceGate(GateRef gate, StateDepend stateDepend, GateRef replacement) = 0; 364514f5e3Sopenharmony_ci}; 374514f5e3Sopenharmony_ci 384514f5e3Sopenharmony_ciclass PassVisitor { 394514f5e3Sopenharmony_cipublic: 404514f5e3Sopenharmony_ci PassVisitor(Circuit* circuit, Chunk* chunk, RPOVisitor* visitor) 414514f5e3Sopenharmony_ci : circuit_(circuit), acc_(circuit), chunk_(chunk), visitor_(visitor) {} 424514f5e3Sopenharmony_ci virtual ~PassVisitor() = default; 434514f5e3Sopenharmony_ci 444514f5e3Sopenharmony_ci virtual GateRef VisitGate(GateRef gate) = 0; 454514f5e3Sopenharmony_ci virtual void Initialize() {} 464514f5e3Sopenharmony_ci virtual void Finalize() {} 474514f5e3Sopenharmony_ciprotected: 484514f5e3Sopenharmony_ci void ReplaceGate(GateRef gate, GateRef replacement) 494514f5e3Sopenharmony_ci { 504514f5e3Sopenharmony_ci visitor_->ReplaceGate(gate, replacement); 514514f5e3Sopenharmony_ci } 524514f5e3Sopenharmony_ci void ReplaceGate(GateRef gate, StateDepend stateDepend, GateRef replacement) 534514f5e3Sopenharmony_ci { 544514f5e3Sopenharmony_ci visitor_->ReplaceGate(gate, stateDepend, replacement); 554514f5e3Sopenharmony_ci } 564514f5e3Sopenharmony_ci Circuit* circuit_ {nullptr}; 574514f5e3Sopenharmony_ci GateAccessor acc_; 584514f5e3Sopenharmony_ci Chunk* chunk_ {nullptr}; 594514f5e3Sopenharmony_ci RPOVisitor* visitor_; 604514f5e3Sopenharmony_ci}; 614514f5e3Sopenharmony_ci 624514f5e3Sopenharmony_ciclass CombinedPassVisitor : public RPOVisitor { 634514f5e3Sopenharmony_cipublic: 644514f5e3Sopenharmony_ci CombinedPassVisitor(Circuit* circuit, bool enableLog, const std::string& name, Chunk* chunk) 654514f5e3Sopenharmony_ci : enableLog_(enableLog), methodName_(name), circuit_(circuit), acc_(circuit), 664514f5e3Sopenharmony_ci chunk_(chunk), workList_(chunk), changedList_(chunk), orderList_(chunk), passList_(chunk) {} 674514f5e3Sopenharmony_ci virtual ~CombinedPassVisitor() = default; 684514f5e3Sopenharmony_ci void AddPass(PassVisitor* pass); 694514f5e3Sopenharmony_ci 704514f5e3Sopenharmony_ci int32_t GetGateOrder(GateRef gate) override; 714514f5e3Sopenharmony_ci void SetGateOrder(GateRef gate, int32_t orderId) override; 724514f5e3Sopenharmony_ci void Resize(int32_t size, int32_t num) override; 734514f5e3Sopenharmony_ci 744514f5e3Sopenharmony_ci void VisitGraph(); 754514f5e3Sopenharmony_ci GateRef VisitGate(GateRef gate); 764514f5e3Sopenharmony_ci void ReVisitGate(GateRef gate) override; 774514f5e3Sopenharmony_ci void LogicallyReplaceGate(GateRef gate, GateRef replacement) override; 784514f5e3Sopenharmony_ci void RelaxStateAndDepend(GateRef gate) override; 794514f5e3Sopenharmony_ci void ReplaceGate(GateRef gate, GateRef replacement) override; 804514f5e3Sopenharmony_ci void ReplaceGate(GateRef gate, StateDepend stateDepend, GateRef replacement) override; 814514f5e3Sopenharmony_ci void PrintLog(const std::string& phaseName); 824514f5e3Sopenharmony_ci void VistDependSelectorForLoop(GateRef gate); 834514f5e3Sopenharmony_ci 844514f5e3Sopenharmony_ciprotected: 854514f5e3Sopenharmony_ci 864514f5e3Sopenharmony_ci void VisitTopGate(Edge& current); 874514f5e3Sopenharmony_ci 884514f5e3Sopenharmony_ci void PushGate(GateRef gate, size_t index) 894514f5e3Sopenharmony_ci { 904514f5e3Sopenharmony_ci workList_.push_back(Edge{gate, index}); 914514f5e3Sopenharmony_ci acc_.SetMark(gate, MarkCode::VISITED); 924514f5e3Sopenharmony_ci } 934514f5e3Sopenharmony_ci 944514f5e3Sopenharmony_ci void PushChangedGate(GateRef gate) 954514f5e3Sopenharmony_ci { 964514f5e3Sopenharmony_ci changedList_.push_back(gate); 974514f5e3Sopenharmony_ci acc_.SetMark(gate, MarkCode::PREVISIT); 984514f5e3Sopenharmony_ci } 994514f5e3Sopenharmony_ci 1004514f5e3Sopenharmony_ci void PopGate(GateRef gate) 1014514f5e3Sopenharmony_ci { 1024514f5e3Sopenharmony_ci workList_.pop_back(); 1034514f5e3Sopenharmony_ci acc_.SetMark(gate, MarkCode::FINISHED); 1044514f5e3Sopenharmony_ci } 1054514f5e3Sopenharmony_ci 1064514f5e3Sopenharmony_ci Chunk *GetChunk() const 1074514f5e3Sopenharmony_ci { 1084514f5e3Sopenharmony_ci return chunk_; 1094514f5e3Sopenharmony_ci } 1104514f5e3Sopenharmony_ci void PrintStack(); 1114514f5e3Sopenharmony_ci 1124514f5e3Sopenharmony_ciprivate: 1134514f5e3Sopenharmony_ci bool enableLog_ {false}; 1144514f5e3Sopenharmony_ci std::string methodName_; 1154514f5e3Sopenharmony_ci Circuit* circuit_ {nullptr}; 1164514f5e3Sopenharmony_ci GateAccessor acc_; 1174514f5e3Sopenharmony_ci Chunk* chunk_ {nullptr}; 1184514f5e3Sopenharmony_ci ChunkDeque<Edge> workList_; 1194514f5e3Sopenharmony_ci ChunkDeque<GateRef> changedList_; 1204514f5e3Sopenharmony_ci ChunkVector<int32_t> orderList_; 1214514f5e3Sopenharmony_ci ChunkVector<PassVisitor*> passList_; 1224514f5e3Sopenharmony_ci uint32_t orderCount_ {0}; 1234514f5e3Sopenharmony_ci}; 1244514f5e3Sopenharmony_ci} // panda::ecmascript::kungfu 1254514f5e3Sopenharmony_ci#endif // ECMASCRIPT_COMPILER_COMBINED_PASS_VISITOR_H 126