1 /* 2 * Copyright (c) 2021-2025 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 ark::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), barriersInsts_(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 IsEnable()44 bool IsEnable() const override 45 { 46 return g_options.IsCompilerOptimizeMemoryBarriers(); 47 } 48 GetBlocksToVisit()49 const ArenaVector<BasicBlock *> &GetBlocksToVisit() const override 50 { 51 return GetGraph()->GetBlocksRPO(); 52 } 53 54 public: 55 void ApplyGraph(); IsApplied()56 bool IsApplied() 57 { 58 return isApplied_; 59 } 60 61 private: VisitDefault(Inst * inst)62 void VisitDefault([[maybe_unused]] Inst *inst) override 63 { 64 ASSERT_DO(!inst->IsCall() && !inst->IsStore(), 65 (std::cerr << "need to make a visitor for the instruction: " << *inst << std::endl)); 66 } 67 68 void CheckAllInputs(Inst *inst); 69 void CheckInput(Inst *input); 70 void CheckTwoInputs(Inst *input1, Inst *input2); 71 72 static void VisitCallStatic(GraphVisitor *v, Inst *inst); 73 static void VisitCallIndirect(GraphVisitor *v, Inst *inst); 74 static void VisitCall(GraphVisitor *v, Inst *inst); 75 static void VisitCallResolvedStatic(GraphVisitor *v, Inst *inst); 76 static void VisitCallVirtual(GraphVisitor *v, Inst *inst); 77 static void VisitCallResolvedVirtual(GraphVisitor *v, Inst *inst); 78 static void VisitCallDynamic(GraphVisitor *v, Inst *inst); 79 static void VisitCallNative(GraphVisitor *v, Inst *inst); 80 static void VisitSelect(GraphVisitor *v, Inst *inst); 81 static void VisitSelectImm(GraphVisitor *v, Inst *inst); 82 static void VisitStoreArray(GraphVisitor *v, Inst *inst); 83 static void VisitStoreArrayI(GraphVisitor *v, Inst *inst); 84 static void VisitStoreObject(GraphVisitor *v, Inst *inst); 85 static void VisitStoreObjectPair(GraphVisitor *v, Inst *inst); 86 static void VisitStoreObjectDynamic(GraphVisitor *v, Inst *inst); 87 static void VisitStoreStatic(GraphVisitor *v, Inst *inst); 88 static void VisitStore(GraphVisitor *v, Inst *inst); 89 static void VisitStoreI(GraphVisitor *v, Inst *inst); 90 static void VisitStoreResolvedObjectField(GraphVisitor *v, Inst *inst); 91 static void VisitUnresolvedStoreStatic(GraphVisitor *v, Inst *inst); 92 static void VisitStoreResolvedObjectFieldStatic(GraphVisitor *v, Inst *inst); 93 static void VisitStoreArrayPair(GraphVisitor *v, Inst *inst); 94 static void VisitStoreArrayPairI(GraphVisitor *v, Inst *inst); 95 static void VisitNullCheck(GraphVisitor *v, Inst *inst); 96 static void VisitBoundCheck(GraphVisitor *v, Inst *inst); VisitFillConstArray(GraphVisitor * v,Inst * inst)97 static void VisitFillConstArray([[maybe_unused]] GraphVisitor *v, [[maybe_unused]] Inst *inst) {} 98 void MergeBarriers(); 99 bool CheckInst(Inst *inst); 100 101 #include "optimizer/ir/visitor.inc" 102 103 private: 104 InstVector barriersInsts_; 105 bool isApplied_ {false}; 106 }; 107 } // namespace ark::compiler 108 109 #endif // COMPILER_OPTIMIZER_OPTIMIZATIONS_MEMORY_BARRIERS_H 110