1 /* 2 * Copyright (c) 2023 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 MAPLEBE_INCLUDE_CG_LOCALO_H 17 #define MAPLEBE_INCLUDE_CG_LOCALO_H 18 19 #include "cg_phase.h" 20 #include "cgbb.h" 21 #include "live.h" 22 #include "loop.h" 23 #include "cg.h" 24 25 namespace maplebe { 26 class LocalOpt { 27 public: LocalOpt(MemPool & memPool,CGFunc & func,ReachingDefinition & rd)28 LocalOpt(MemPool &memPool, CGFunc &func, ReachingDefinition &rd) 29 : localoMp(&memPool), cgFunc(&func), reachindDef(&rd) 30 { 31 } 32 33 virtual ~LocalOpt() = default; 34 35 void DoLocalCopyPropOptmize(); 36 37 protected: GetRDInfo()38 ReachingDefinition *GetRDInfo() 39 { 40 return reachindDef; 41 } 42 MemPool *localoMp; 43 CGFunc *cgFunc; 44 ReachingDefinition *reachindDef; 45 46 private: 47 virtual void DoLocalCopyProp() = 0; 48 }; 49 50 class LocalOptimizeManager { 51 public: LocalOptimizeManager(CGFunc & cgFunc,ReachingDefinition & rd)52 LocalOptimizeManager(CGFunc &cgFunc, ReachingDefinition &rd) : cgFunc(cgFunc), reachingDef(&rd) {} 53 ~LocalOptimizeManager() = default; 54 template <typename LocalPropOptimizePattern> Optimize()55 void Optimize() 56 { 57 LocalPropOptimizePattern optPattern(cgFunc, *reachingDef); 58 optPattern.Run(); 59 } 60 61 private: 62 CGFunc &cgFunc; 63 ReachingDefinition *reachingDef; 64 }; 65 66 class LocalPropOptimizePattern { 67 public: LocalPropOptimizePattern(CGFunc & cgFunc,ReachingDefinition & rd)68 LocalPropOptimizePattern(CGFunc &cgFunc, ReachingDefinition &rd) : cgFunc(cgFunc), reachingDef(&rd) {} 69 virtual ~LocalPropOptimizePattern() = default; 70 virtual bool CheckCondition(Insn &insn) = 0; 71 virtual void Optimize(BB &bb, Insn &insn) = 0; 72 void Run(); 73 74 protected: PhaseName()75 std::string PhaseName() const 76 { 77 return "localopt"; 78 } 79 CGFunc &cgFunc; 80 ReachingDefinition *reachingDef; 81 }; 82 83 class RedundantDefRemove : public LocalPropOptimizePattern { 84 public: RedundantDefRemove(CGFunc & cgFunc,ReachingDefinition & rd)85 RedundantDefRemove(CGFunc &cgFunc, ReachingDefinition &rd) : LocalPropOptimizePattern(cgFunc, rd) {} 86 ~RedundantDefRemove() override = default; 87 bool CheckCondition(Insn &insn) final; 88 }; 89 90 MAPLE_FUNC_PHASE_DECLARE(LocalCopyProp, maplebe::CGFunc) 91 } /* namespace maplebe */ 92 #endif /* MAPLEBE_INCLUDE_CG_LOCALO_H */ 93