1 /* 2 * Copyright (c) 2023-2024 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 PANDA_BYTECODE_OPTIMIZER_BYTECODEOPT_PEEPHOLES_H 17 #define PANDA_BYTECODE_OPTIMIZER_BYTECODEOPT_PEEPHOLES_H 18 19 #include "bytecodeopt_options.h" 20 #include "compiler/optimizer/pass.h" 21 #include "compiler/optimizer/ir/basicblock.h" 22 #include "compiler/optimizer/ir/graph.h" 23 #include "compiler/optimizer/ir/graph_visitor.h" 24 #include "compiler/optimizer/ir/inst.h" 25 #include "libpandabase/utils/arena_containers.h" 26 #include "runtime_adapter.h" 27 28 /* 29 * BytecodeOptPeepholes 30 * 31 * BytecodeOptPeepholes includes now only transformation of NewObject and related instructions into 32 * InitObject 33 */ 34 35 namespace ark::bytecodeopt { 36 37 using compiler::BasicBlock; 38 using compiler::CallInst; 39 using compiler::Inst; 40 using compiler::Opcode; 41 42 // NOLINTNEXTLINE(fuchsia-multiple-inheritance) 43 class BytecodeOptPeepholes : public compiler::Optimization, public compiler::GraphVisitor { 44 public: BytecodeOptPeepholes(compiler::Graph * graph)45 explicit BytecodeOptPeepholes(compiler::Graph *graph) : compiler::Optimization(graph) {} 46 ~BytecodeOptPeepholes() override = default; 47 NO_COPY_SEMANTIC(BytecodeOptPeepholes); 48 NO_MOVE_SEMANTIC(BytecodeOptPeepholes); 49 50 bool RunImpl() override; 51 GetPassName()52 const char *GetPassName() const override 53 { 54 return "BytecodeOptPeepholes"; 55 } 56 IsEnable()57 bool IsEnable() const override 58 { 59 return g_options.IsBytecodeOptPeepholes(); 60 } 61 62 /* This visitor replaces combination of NewObject, SaveState, 63 * NullCheck and CallStatic with InitObject. It is used in order to replace newobj, sta and call 64 * in some ark bytecode with one instruction initobj. 65 */ GetBlocksToVisit()66 const ArenaVector<BasicBlock *> &GetBlocksToVisit() const override 67 { 68 return GetGraph()->GetBlocksRPO(); 69 } 70 71 static void VisitNewObject(GraphVisitor *v, Inst *inst); 72 73 public: IsApplied()74 bool IsApplied() 75 { 76 return isApplied_; 77 } 78 79 #include "compiler/optimizer/ir/visitor.inc" 80 81 private: SetIsApplied()82 void SetIsApplied() 83 { 84 isApplied_ = true; 85 } 86 87 bool isApplied_ {false}; 88 }; 89 90 } // namespace ark::bytecodeopt 91 92 #endif // PANDA_BYTECODE_OPTIMIZER_BYTECODEOPT_PEEPHOLES_H 93