1 /* 2 * Copyright (c) 2021-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_REDUNDANTLOOPELIMINATIONS_H 17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_REDUNDANTLOOPELIMINATIONS_H 18 19 #include "optimizer/pass.h" 20 #include "optimizer/optimizations/loop_transform.h" 21 22 namespace ark::compiler { 23 class RedundantLoopElimination : public LoopTransform<LoopExitPoint::ALL_LOOP> { 24 public: RedundantLoopElimination(Graph * graph)25 explicit RedundantLoopElimination(Graph *graph) : LoopTransform(graph) {} 26 27 NO_MOVE_SEMANTIC(RedundantLoopElimination); 28 NO_COPY_SEMANTIC(RedundantLoopElimination); 29 ~RedundantLoopElimination() override = default; 30 31 bool RunImpl() override; 32 GetPassName()33 const char *GetPassName() const override 34 { 35 return "RedundantLoopElimination"; 36 } 37 IsEnable()38 bool IsEnable() const override 39 { 40 return g_options.IsCompilerRedundantLoopElimination(); 41 } 42 IsApplied()43 bool IsApplied() const 44 { 45 return isApplied_; 46 } 47 48 void InvalidateAnalyses() override; 49 50 private: 51 bool TransformLoop(Loop *loop) override; 52 BasicBlock *IsRedundant(Loop *loop); 53 void DeleteLoop(Loop *loop, BasicBlock *outsideSucc) const; 54 55 BasicBlock *loopExit_ {nullptr}; 56 bool isApplied_ {false}; 57 }; 58 } // namespace ark::compiler 59 60 #endif // COMPILER_OPTIMIZER_OPTIMIZATIONS_REDUNDANTLOOPELIMINATIONS_H 61