1 /** 2 * Copyright (c) 2021-2022 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 COMPILER_OPTIMIZER_OPTIMIZATIONS_MEMORY_BARRIERS_H 17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_MEMORY_BARRIERS_H 18 19 #include "compiler_options.h" 20 #include "optimizer/ir/analysis.h" 21 #include "optimizer/ir/graph.h" 22 #include "optimizer/ir/graph_visitor.h" 23 #include "optimizer/pass.h" 24 25 namespace panda::compiler { 26 // NOLINTNEXTLINE(fuchsia-multiple-inheritance) 27 class OptimizeMemoryBarriers : public Optimization, public GraphVisitor { 28 public: OptimizeMemoryBarriers(Graph * graph)29 explicit OptimizeMemoryBarriers(Graph *graph) 30 : Optimization(graph), barriers_insts_(graph->GetLocalAllocator()->Adapter()) 31 { 32 } 33 NO_MOVE_SEMANTIC(OptimizeMemoryBarriers); 34 NO_COPY_SEMANTIC(OptimizeMemoryBarriers); 35 ~OptimizeMemoryBarriers() override = default; 36 37 bool RunImpl() override; 38 GetPassName()39 const char *GetPassName() const override 40 { 41 return "OptimizeMemoryBarriers"; 42 } 43 GetBlocksToVisit()44 const ArenaVector<BasicBlock *> &GetBlocksToVisit() const override 45 { 46 return GetGraph()->GetBlocksRPO(); 47 } 48 49 public: 50 void ApplyGraph(); IsApplied()51 bool IsApplied() 52 { 53 return is_applied_; 54 } 55 56 private: VisitDefault(Inst * inst)57 void VisitDefault([[maybe_unused]] Inst *inst) override 58 { 59 ASSERT_DO(!inst->IsCall() && !inst->IsStore(), 60 (std::cerr << "need to make a visitor for the instruction: " << *inst << std::endl)); 61 } 62 63 void CheckAllInputs(Inst *inst); 64 void CheckInput(Inst *input); 65 void CheckTwoInputs(Inst *input1, Inst *input2); 66 67 static void VisitCallStatic(GraphVisitor *v, Inst *inst); 68 static void VisitCallIndirect(GraphVisitor *v, Inst *inst); 69 static void VisitCall(GraphVisitor *v, Inst *inst); 70 static void VisitUnresolvedCallStatic(GraphVisitor *v, Inst *inst); 71 static void VisitCallVirtual(GraphVisitor *v, Inst *inst); 72 static void VisitUnresolvedCallVirtual(GraphVisitor *v, Inst *inst); 73 static void VisitCallDynamic(GraphVisitor *v, Inst *inst); 74 static void VisitSelect(GraphVisitor *v, Inst *inst); 75 static void VisitSelectImm(GraphVisitor *v, Inst *inst); 76 static void VisitStoreArray(GraphVisitor *v, Inst *inst); 77 static void VisitStoreArrayI(GraphVisitor *v, Inst *inst); 78 static void VisitStoreObject(GraphVisitor *v, Inst *inst); 79 static void VisitStoreStatic(GraphVisitor *v, Inst *inst); 80 static void VisitStore(GraphVisitor *v, Inst *inst); 81 static void VisitStoreI(GraphVisitor *v, Inst *inst); 82 static void VisitUnresolvedStoreObject(GraphVisitor *v, Inst *inst); 83 static void VisitUnresolvedStoreStatic(GraphVisitor *v, Inst *inst); 84 static void VisitStoreArrayPair(GraphVisitor *v, Inst *inst); 85 static void VisitStoreArrayPairI(GraphVisitor *v, Inst *inst); 86 static void VisitNullCheck(GraphVisitor *v, Inst *inst); 87 static void VisitBoundCheck(GraphVisitor *v, Inst *inst); VisitFillConstArray(GraphVisitor * v,Inst * inst)88 static void VisitFillConstArray([[maybe_unused]] GraphVisitor *v, [[maybe_unused]] Inst *inst) {} 89 void MergeBarriers(); 90 bool CheckInst(Inst *inst); 91 92 #include "optimizer/ir/visitor.inc" 93 94 private: 95 InstVector barriers_insts_; 96 bool is_applied_ {false}; 97 }; 98 } // namespace panda::compiler 99 100 #endif // COMPILER_OPTIMIZER_OPTIMIZATIONS_MEMORY_BARRIERS_H 101