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_AARCH64_ICO_H 17 #define MAPLEBE_INCLUDE_CG_AARCH64_AARCH64_ICO_H 18 #include "ico.h" 19 #include "aarch64_isa.h" 20 #include "optimize_common.h" 21 #include "live.h" 22 23 namespace maplebe { 24 class AArch64IfConversionOptimizer : public IfConversionOptimizer { 25 public: AArch64IfConversionOptimizer(CGFunc & func,MemPool & memPool)26 AArch64IfConversionOptimizer(CGFunc &func, MemPool &memPool) : IfConversionOptimizer(func, memPool) {} 27 28 ~AArch64IfConversionOptimizer() override = default; 29 void InitOptimizePatterns() override; 30 }; 31 32 class AArch64ICOPattern : public ICOPattern { 33 public: AArch64ICOPattern(CGFunc & func)34 explicit AArch64ICOPattern(CGFunc &func) : ICOPattern(func) {} 35 ~AArch64ICOPattern() override = default; 36 37 protected: 38 ConditionCode Encode(MOperator mOp, bool inverse) const; 39 Insn *BuildCmpInsn(const Insn &condBr) const; 40 Insn *BuildCcmpInsn(ConditionCode ccCode, const Insn *cmpInsn) const; 41 Insn *BuildCondSet(const Insn &branch, RegOperand ®, bool inverse) const; 42 Insn *BuildCondSel(const Insn &branch, MOperator mOp, RegOperand &dst, RegOperand &src1, RegOperand &src2) const; 43 bool IsSetInsn(const Insn &insn, Operand *&dest, std::vector<Operand *> &src) const; 44 static uint32 GetNZCV(ConditionCode ccCode, bool inverse); 45 bool CheckMop(MOperator mOperator) const; 46 }; 47 48 /* If-Then-Else pattern */ 49 class AArch64ICOIfThenElsePattern : public AArch64ICOPattern { 50 public: AArch64ICOIfThenElsePattern(CGFunc & func)51 explicit AArch64ICOIfThenElsePattern(CGFunc &func) : AArch64ICOPattern(func) {} 52 ~AArch64ICOIfThenElsePattern() override = default; Optimize(BB & curBB)53 bool Optimize(BB &curBB) override 54 { 55 return true; 56 } 57 58 protected: 59 bool BuildCondMovInsn(BB &cmpBB, const BB &bb, const std::map<Operand *, std::vector<Operand *>> &ifDestSrcMap, 60 const std::map<Operand *, std::vector<Operand *>> &elseDestSrcMap, bool elseBBIsProcessed, 61 std::vector<Insn *> &generateInsn); 62 void GenerateInsnForImm(const Insn &branchInsn, Operand &ifDest, Operand &elseDest, RegOperand &destReg, 63 std::vector<Insn *> &generateInsn); 64 Operand *GetDestReg(const std::map<Operand *, std::vector<Operand *>> &destSrcMap, const RegOperand &destReg) const; 65 void GenerateInsnForReg(const Insn &branchInsn, Operand &ifDest, Operand &elseDest, RegOperand &destReg, 66 std::vector<Insn *> &generateInsn); 67 RegOperand *GenerateRegAndTempInsn(Operand &dest, const RegOperand &destReg, 68 std::vector<Insn *> &generateInsn) const; 69 bool CheckHasSameDest(std::vector<Insn *> &lInsn, std::vector<Insn *> &rInsn) const; 70 bool CheckCondMoveBB(BB *bb, std::map<Operand *, std::vector<Operand *>> &destSrcMap, 71 std::vector<Operand *> &destRegs, std::vector<Insn *> &setInsn, Operand *flagReg, 72 Insn *cmpInsn) const; 73 }; 74 75 /* If-Then MorePreds pattern 76 * 77 * .L.891__92: .L.891__92: 78 * cmp x4, w0, UXTW cmp x4, w0, UXTW 79 * bls .L.891__41 csel x0, x2, x0, LS 80 * .L.891__42: bls .L.891__94 81 * sub x0, x4, w0, UXTW =====> .L.891__42: 82 * cmp x0, x2 sub x0, x4, w0, UXTW 83 * bls .L.891__41 cmp x0, x2 84 * ...... csel x0, x2, x0, LS 85 * .L.891__41: bls .L.891__94 86 * mov x0, x2 87 * b .L.891__94 88 * */ 89 class AArch64ICOMorePredsPattern : public AArch64ICOPattern { 90 public: AArch64ICOMorePredsPattern(CGFunc & func)91 explicit AArch64ICOMorePredsPattern(CGFunc &func) : AArch64ICOPattern(func) {} 92 ~AArch64ICOMorePredsPattern() override = default; Optimize(BB & curBB)93 bool Optimize(BB &curBB) override 94 { 95 return true; 96 } 97 98 protected: 99 bool CheckGotoBB(BB &gotoBB, std::vector<Insn *> &movInsn) const; 100 bool MovToCsel(std::vector<Insn *> &movInsn, std::vector<Insn *> &cselInsn, const Insn &branchInsn) const; 101 }; 102 } /* namespace maplebe */ 103 104 #endif /* MAPLEBE_INCLUDE_CG_AARCH64_AARCH64_ICO_H */ 105