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_AARCH64RAOPT_H 17 #define MAPLEBE_INCLUDE_CG_AARCH64RAOPT_H 18 19 #include "cg.h" 20 #include "ra_opt.h" 21 #include "aarch64_cg.h" 22 #include "aarch64_insn.h" 23 #include "aarch64_operand.h" 24 25 namespace maplebe { 26 class X0OptInfo { 27 public: X0OptInfo()28 X0OptInfo() : movSrc(nullptr), replaceReg(0), renameInsn(nullptr), renameOpnd(nullptr), renameReg(0) {} 29 ~X0OptInfo() = default; 30 GetMovSrc()31 inline RegOperand *GetMovSrc() const 32 { 33 return movSrc; 34 } 35 GetReplaceReg()36 inline regno_t GetReplaceReg() const 37 { 38 return replaceReg; 39 } 40 GetRenameInsn()41 inline Insn *GetRenameInsn() const 42 { 43 return renameInsn; 44 } 45 GetRenameOpnd()46 inline Operand *GetRenameOpnd() const 47 { 48 return renameOpnd; 49 } 50 GetRenameReg()51 inline regno_t GetRenameReg() const 52 { 53 return renameReg; 54 } 55 SetMovSrc(RegOperand * srcReg)56 inline void SetMovSrc(RegOperand *srcReg) 57 { 58 movSrc = srcReg; 59 } 60 SetReplaceReg(regno_t regno)61 inline void SetReplaceReg(regno_t regno) 62 { 63 replaceReg = regno; 64 } 65 SetRenameInsn(Insn * insn)66 inline void SetRenameInsn(Insn *insn) 67 { 68 renameInsn = insn; 69 } 70 ResetRenameInsn()71 inline void ResetRenameInsn() 72 { 73 renameInsn = nullptr; 74 } 75 SetRenameOpnd(Operand * opnd)76 inline void SetRenameOpnd(Operand *opnd) 77 { 78 renameOpnd = opnd; 79 } 80 SetRenameReg(regno_t regno)81 inline void SetRenameReg(regno_t regno) 82 { 83 renameReg = regno; 84 } 85 86 private: 87 RegOperand *movSrc; 88 regno_t replaceReg; 89 Insn *renameInsn; 90 Operand *renameOpnd; 91 regno_t renameReg; 92 }; 93 94 class RaX0Opt { 95 public: RaX0Opt(CGFunc * func)96 explicit RaX0Opt(CGFunc *func) : cgFunc(func) {} 97 ~RaX0Opt() = default; 98 99 bool PropagateX0CanReplace(Operand *opnd, regno_t replaceReg) const; 100 bool PropagateRenameReg(Insn *insn, const X0OptInfo &optVal) const; 101 bool PropagateX0DetectX0(const Insn *insn, X0OptInfo &optVal) const; 102 bool PropagateX0DetectRedefine(const InsnDesc *md, const Insn *ninsn, const X0OptInfo &optVal, uint32 index) const; 103 bool PropagateX0Optimize(const BB *bb, const Insn *insn, X0OptInfo &optVal); 104 bool PropagateX0ForCurrBb(BB *bb, const X0OptInfo &optVal); 105 void PropagateX0ForNextBb(BB *nextBb, const X0OptInfo &optVal); 106 void PropagateX0(); 107 108 private: 109 CGFunc *cgFunc; 110 }; 111 112 class VregRenameInfo { 113 public: 114 VregRenameInfo() = default; 115 116 ~VregRenameInfo() = default; 117 118 BB *firstBBLevelSeen = nullptr; 119 BB *lastBBLevelSeen = nullptr; 120 uint32 numDefs = 0; 121 uint32 numUses = 0; 122 uint32 numInnerDefs = 0; 123 uint32 numInnerUses = 0; 124 uint32 largestUnusedDistance = 0; 125 uint8 innerMostloopLevelSeen = 0; 126 }; 127 128 class VregRename { 129 public: VregRename(CGFunc * func,MemPool * pool,LoopAnalysis & loop)130 VregRename(CGFunc *func, MemPool *pool, LoopAnalysis &loop) 131 : cgFunc(func), memPool(pool), alloc(pool), loopInfo(loop), renameInfo(alloc.Adapter()) 132 { 133 renameInfo.resize(cgFunc->GetMaxRegNum()); 134 ccRegno = static_cast<RegOperand *>(&cgFunc->GetOrCreateRflag())->GetRegisterNumber(); 135 }; 136 ~VregRename() = default; 137 138 void PrintRenameInfo(regno_t regno) const; 139 void PrintAllRenameInfo() const; 140 141 void RenameFindLoopVregs(const LoopDesc &loop); 142 void RenameFindVregsToRename(const LoopDesc &loop); 143 bool IsProfitableToRename(const VregRenameInfo *info) const; 144 void RenameProfitableVreg(RegOperand &ropnd, const LoopDesc &loop); 145 void RenameGetFuncVregInfo(); 146 void UpdateVregInfo(regno_t reg, BB *bb, bool isInner, bool isDef); 147 void VregLongLiveRename(); 148 149 CGFunc *cgFunc; 150 MemPool *memPool; 151 MapleAllocator alloc; 152 LoopAnalysis &loopInfo; 153 Bfs *bfs = nullptr; 154 MapleVector<VregRenameInfo *> renameInfo; 155 uint32 maxRegnoSeen = 0; 156 regno_t ccRegno; 157 }; 158 159 class AArch64RaOpt : public RaOpt { 160 public: AArch64RaOpt(CGFunc & func,MemPool & pool,DomAnalysis & dom,LoopAnalysis & loop)161 AArch64RaOpt(CGFunc &func, MemPool &pool, DomAnalysis &dom, LoopAnalysis &loop) : RaOpt(func, pool, dom, loop) {} 162 ~AArch64RaOpt() override = default; 163 void Run() override; 164 165 private: 166 }; 167 } /* namespace maplebe */ 168 169 #endif /* MAPLEBE_INCLUDE_CG_AARCH64RAOPT_H */ 170