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_AARCH64_VALIDBIT_OPT_H 17 #define MAPLEBE_INCLUDE_CG_AARCH64_VALIDBIT_OPT_H 18 19 #include "cg_validbit_opt.h" 20 #include "operand.h" 21 #include "aarch64_cgfunc.h" 22 23 namespace maplebe { 24 class AArch64ValidBitOpt : public ValidBitOpt { 25 public: AArch64ValidBitOpt(CGFunc & f,CGSSAInfo & info)26 AArch64ValidBitOpt(CGFunc &f, CGSSAInfo &info) : ValidBitOpt(f, info) {} 27 ~AArch64ValidBitOpt() override = default; 28 29 void DoOpt(BB &bb, Insn &insn) override; 30 void SetValidBits(Insn &insn) override; 31 bool SetPhiValidBits(Insn &insn) override; 32 }; 33 34 /* 35 * Example 1) 36 * def w9 def w9 37 * ... ... 38 * and w4, w9, #255 ===> mov w4, w9 39 * 40 * Example 2) 41 * and w6[16], w0[16], #FF00[16] mov w6, w0 42 * asr w6, w6[16], #8[4] ===> asr w6, w6 43 */ 44 class AndValidBitPattern : public ValidBitPattern { 45 public: AndValidBitPattern(CGFunc & cgFunc,CGSSAInfo & info)46 AndValidBitPattern(CGFunc &cgFunc, CGSSAInfo &info) : ValidBitPattern(cgFunc, info) {} ~AndValidBitPattern()47 ~AndValidBitPattern() override 48 { 49 desReg = nullptr; 50 srcReg = nullptr; 51 } 52 void Run(BB &bb, Insn &insn) override; 53 bool CheckCondition(Insn &insn) override; GetPatternName()54 std::string GetPatternName() override 55 { 56 return "AndValidBitPattern"; 57 } 58 59 private: 60 bool CheckImmValidBit(int64 andImm, uint32 andImmVB, int64 shiftImm) const; 61 MOperator newMop = MOP_undef; 62 RegOperand *desReg = nullptr; 63 RegOperand *srcReg = nullptr; 64 }; 65 66 /* 67 * Example 1) 68 * uxth w1[16], w2[16] / uxtb w1[8], w2[8] 69 * ===> 70 * mov w1, w2 71 * 72 * Example 2) 73 * ubfx w1, w2[16], #0, #16 / sbfx w1, w2[16], #0, #16 74 * ===> 75 * mov w1, w2 76 */ 77 class ExtValidBitPattern : public ValidBitPattern { 78 public: ExtValidBitPattern(CGFunc & cgFunc,CGSSAInfo & info)79 ExtValidBitPattern(CGFunc &cgFunc, CGSSAInfo &info) : ValidBitPattern(cgFunc, info) {} ~ExtValidBitPattern()80 ~ExtValidBitPattern() override 81 { 82 newDstOpnd = nullptr; 83 newSrcOpnd = nullptr; 84 } 85 void Run(BB &bb, Insn &insn) override; 86 bool CheckCondition(Insn &insn) override; GetPatternName()87 std::string GetPatternName() override 88 { 89 return "ExtValidBitPattern"; 90 } 91 92 private: 93 RegOperand *newDstOpnd = nullptr; 94 RegOperand *newSrcOpnd = nullptr; 95 MOperator newMop = MOP_undef; 96 }; 97 98 /* 99 * cmp w0, #0 100 * cset w1, NE --> mov w1, w0 101 * 102 * cmp w0, #0 103 * cset w1, EQ --> eor w1, w0, 1 104 * 105 * cmp w0, #1 106 * cset w1, NE --> eor w1, w0, 1 107 * 108 * cmp w0, #1 109 * cset w1, EQ --> mov w1, w0 110 * 111 * cmp w0, #0 112 * cset w0, NE -->null 113 * 114 * cmp w0, #1 115 * cset w0, EQ -->null 116 * 117 * condition: 118 * 1. the first operand of cmp instruction must has only one valid bit 119 * 2. the second operand of cmp instruction must be 0 or 1 120 * 3. flag register of cmp isntruction must not be used later 121 */ 122 class CmpCsetVBPattern : public ValidBitPattern { 123 public: CmpCsetVBPattern(CGFunc & cgFunc,CGSSAInfo & info)124 CmpCsetVBPattern(CGFunc &cgFunc, CGSSAInfo &info) : ValidBitPattern(cgFunc, info) {} ~CmpCsetVBPattern()125 ~CmpCsetVBPattern() override 126 { 127 cmpInsn = nullptr; 128 } 129 void Run(BB &bb, Insn &csetInsn) override; 130 bool CheckCondition(Insn &csetInsn) override; GetPatternName()131 std::string GetPatternName() override 132 { 133 return "CmpCsetPattern"; 134 }; 135 136 private: 137 bool IsContinuousCmpCset(const Insn &curInsn); 138 bool OpndDefByOneValidBit(const Insn &defInsn); 139 Insn *cmpInsn = nullptr; 140 int64 cmpConstVal = -1; 141 }; 142 143 /* 144 * cmp w0[16], #32768 145 * bge label ===> tbnz w0, #15, label 146 * 147 * bge / blt 148 */ 149 class CmpBranchesPattern : public ValidBitPattern { 150 public: CmpBranchesPattern(CGFunc & cgFunc,CGSSAInfo & info)151 CmpBranchesPattern(CGFunc &cgFunc, CGSSAInfo &info) : ValidBitPattern(cgFunc, info) {} ~CmpBranchesPattern()152 ~CmpBranchesPattern() override 153 { 154 prevCmpInsn = nullptr; 155 } 156 void Run(BB &bb, Insn &insn) override; 157 bool CheckCondition(Insn &insn) override; GetPatternName()158 std::string GetPatternName() override 159 { 160 return "CmpBranchesPattern"; 161 }; 162 163 private: 164 void SelectNewMop(MOperator mop); 165 Insn *prevCmpInsn = nullptr; 166 int64 newImmVal = -1; 167 MOperator newMop = MOP_undef; 168 bool is64Bit = false; 169 }; 170 } /* namespace maplebe */ 171 #endif /* MAPLEBE_INCLUDE_CG_AARCH64_VALIDBIT_OPT_H */ 172