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_LICM_H 17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_LICM_H 18 19 #include "optimizer/ir/graph.h" 20 #include "optimizer/pass.h" 21 #include "compiler_options.h" 22 #include "optimizer/ir/analysis.h" 23 24 namespace ark::compiler { 25 class PANDA_PUBLIC_API Licm : public Optimization { 26 public: 27 explicit Licm(Graph *graph, uint32_t hoistLimit = std::numeric_limits<uint32_t>::max()); 28 NO_MOVE_SEMANTIC(Licm); 29 NO_COPY_SEMANTIC(Licm); 30 ~Licm() override = default; 31 32 bool RunImpl() override; 33 IsEnable()34 bool IsEnable() const override 35 { 36 return g_options.IsCompilerLicm(); 37 } 38 GetPassName()39 const char *GetPassName() const override 40 { 41 return "LICM"; 42 } 43 44 void InvalidateAnalyses() override; 45 46 bool IsBlockLoopExit(BasicBlock *block); 47 48 private: 49 bool IsLoopVisited(const Loop &loop) const; 50 void VisitLoop(Loop *loop); 51 bool IsInstHoistable(Inst *inst); 52 void LoopSearchDFS(Loop *loop); 53 bool InstDominatesLoopExits(Inst *inst); 54 bool InstInputDominatesPreheader(Inst *inst); 55 Inst *FindSaveStateForHoist(Inst *hoisted, const BasicBlock *preHeader, Inst **insertBefore); 56 void TryAppendHoistableInst(Inst *inst, BasicBlock *block, Loop *loop); 57 void UnmarkHoistUsers(Inst *inst); 58 void MoveInstructions(BasicBlock *preHeader, Loop *loop); 59 60 private: 61 const uint32_t hoistLimit_ {0}; 62 uint32_t hoistedInstCount_ {0}; 63 Marker markerLoopExit_ {UNDEF_MARKER}; 64 Marker markerHoistInst_ {UNDEF_MARKER}; 65 SaveStateBridgesBuilder ssb_; 66 InstVector hoistableInstructions_; 67 }; 68 } // namespace ark::compiler 69 70 #endif // COMPILER_OPTIMIZER_OPTIMIZATIONS_LICM_H 71