1 /* 2 * Copyright (c) 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 COMPILER_OPTIMIZER_OPTIMIZATIONS_SAVESTATEOPTIMIZATION_H 17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_SAVESTATEOPTIMIZATION_H 18 19 #include "compiler_logger.h" 20 #include "optimizer/ir/graph.h" 21 #include "optimizer/pass.h" 22 #include "optimizer/analysis/bounds_analysis.h" 23 #include "optimizer/analysis/loop_analyzer.h" 24 #include "optimizer/ir/graph_visitor.h" 25 26 namespace ark::compiler { 27 // NOLINTNEXTLINE(fuchsia-multiple-inheritance) 28 class SaveStateOptimization : public Optimization, public GraphVisitor { 29 using Optimization::Optimization; 30 31 public: SaveStateOptimization(Graph * graph)32 explicit SaveStateOptimization(Graph *graph) : Optimization(graph) {} 33 34 NO_MOVE_SEMANTIC(SaveStateOptimization); 35 NO_COPY_SEMANTIC(SaveStateOptimization); 36 ~SaveStateOptimization() override = default; 37 38 bool RunImpl() override; 39 GetPassName()40 const char *GetPassName() const override 41 { 42 return "SaveStateOptimization"; 43 } 44 IsEnable()45 bool IsEnable() const override 46 { 47 return g_options.IsCompilerSaveStateElimination(); 48 } 49 50 void RemoveSafePoints(); 51 GetBlocksToVisit()52 const ArenaVector<BasicBlock *> &GetBlocksToVisit() const override 53 { 54 return GetGraph()->GetBlocksRPO(); 55 } 56 IsApplied()57 bool IsApplied() const 58 { 59 return isApplied_; 60 } 61 SetApplied()62 void SetApplied() 63 { 64 isApplied_ = true; 65 } 66 HaveCalls()67 bool HaveCalls() 68 { 69 return haveCalls_; 70 } 71 72 #include <savestate_optimization_call_visitors.inl> 73 static void VisitSaveState(GraphVisitor *v, Inst *inst); 74 static void VisitSaveStateDeoptimize(GraphVisitor *v, Inst *inst); 75 void VisitDefault(Inst *inst) override; 76 77 #include "optimizer/ir/visitor.inc" 78 79 private: SetHaveCalls()80 void SetHaveCalls() 81 { 82 haveCalls_ = true; 83 } 84 85 bool TryToRemoveRedundantSaveState(Inst *inst); 86 bool RequireRegMap(Inst *inst); 87 88 private: 89 bool isApplied_ {false}; 90 bool haveCalls_ {false}; 91 }; 92 } // namespace ark::compiler 93 94 #endif // COMPILER_OPTIMIZER_OPTIMIZATIONS_SAVESTATEOPTIMIZATION_H 95