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_VALIDBIT_OPT_H 17 #define MAPLEBE_INCLUDE_CG_VALIDBIT_OPT_H 18 19 #include "cg.h" 20 #include "cgfunc.h" 21 #include "bb.h" 22 #include "insn.h" 23 #include "cg_ssa.h" 24 25 namespace maplebe { 26 #define CG_VALIDBIT_OPT_DUMP CG_DEBUG_FUNC(*cgFunc) 27 class ValidBitPattern { 28 public: ValidBitPattern(CGFunc & f,CGSSAInfo & info)29 ValidBitPattern(CGFunc &f, CGSSAInfo &info) : cgFunc(&f), ssaInfo(&info) {} ~ValidBitPattern()30 virtual ~ValidBitPattern() 31 { 32 cgFunc = nullptr; 33 ssaInfo = nullptr; 34 } PhaseName()35 std::string PhaseName() const 36 { 37 return "cgvalidbitopt"; 38 } 39 40 virtual std::string GetPatternName() = 0; 41 virtual bool CheckCondition(Insn &insn) = 0; 42 virtual void Run(BB &bb, Insn &insn) = 0; 43 Insn *GetDefInsn(const RegOperand &useReg); 44 InsnSet GetAllUseInsn(const RegOperand &defReg); 45 void DumpAfterPattern(std::vector<Insn *> &prevInsns, const Insn *replacedInsn, const Insn *newInsn); 46 47 protected: 48 CGFunc *cgFunc; 49 CGSSAInfo *ssaInfo; 50 }; 51 52 class ValidBitOpt { 53 public: ValidBitOpt(CGFunc & f,CGSSAInfo & info)54 ValidBitOpt(CGFunc &f, CGSSAInfo &info) : cgFunc(&f), ssaInfo(&info) {} ~ValidBitOpt()55 virtual ~ValidBitOpt() 56 { 57 cgFunc = nullptr; 58 ssaInfo = nullptr; 59 } 60 void Run(); GetImmValidBit(int64 value,uint32 size)61 static uint32 GetImmValidBit(int64 value, uint32 size) 62 { 63 if (value < 0) { 64 return size; 65 } else if (value == 0) { 66 return k1BitSize; 67 } 68 uint32 pos = 0; 69 constexpr int64 mask = 1; 70 for (uint32 i = 0; i <= k8BitSize * sizeof(int64); ++i, value >>= 1) { 71 if ((value & mask) == mask) { 72 pos = i + 1; 73 } 74 } 75 return pos; 76 } 77 GetLogValueAtBase2(int64 val)78 static int64 GetLogValueAtBase2(int64 val) 79 { 80 return (__builtin_popcountll(static_cast<uint64>(val)) == 1) ? (__builtin_ffsll(val) - 1) : -1; 81 } 82 83 template <typename VBOpt> Optimize(BB & bb,Insn & insn)84 void Optimize(BB &bb, Insn &insn) 85 { 86 VBOpt opt(*cgFunc, *ssaInfo); 87 opt.Run(bb, insn); 88 } 89 virtual void DoOpt(BB &bb, Insn &insn) = 0; 90 void RectifyValidBitNum(); 91 void RecoverValidBitNum(); 92 virtual void SetValidBits(Insn &insn) = 0; 93 virtual bool SetPhiValidBits(Insn &insn) = 0; 94 95 protected: 96 CGFunc *cgFunc; 97 CGSSAInfo *ssaInfo; 98 }; 99 MAPLE_FUNC_PHASE_DECLARE(CgValidBitOpt, maplebe::CGFunc) 100 } /* namespace maplebe */ 101 #endif /* MAPLEBE_INCLUDE_CG_VALIDBIT_OPT_H */ 102