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_FIXSHORTBRANCH_H 17 #define MAPLEBE_INCLUDE_CG_AARCH64_AARCH64_FIXSHORTBRANCH_H 18 19 #include "aarch64_cg.h" 20 #include "optimize_common.h" 21 #include "mir_builder.h" 22 23 namespace maplebe { 24 class AArch64FixShortBranch { 25 public: AArch64FixShortBranch(CGFunc * cf)26 explicit AArch64FixShortBranch(CGFunc *cf) : cgFunc(cf) {} 27 ~AArch64FixShortBranch() = default; 28 void FixShortBranches() const; 29 void FixShortBranchesForSplitting(); 30 // for long branch which exceeds size of imm19, we need to insert pad. 31 // see InsertJumpPad to know how we do this. 32 void PatchLongBranch(); 33 void FixLdr(); 34 35 private: 36 CGFunc *cgFunc = nullptr; 37 BB *boundaryBB = nullptr; 38 BB *lastBB = nullptr; 39 // For long branch caused by cold-hot bb splitting , 40 // insert an unconditional branch at the end section in order to minimize the negative impact 41 // From To 42 // cond_br target_label cond_br new_label 43 // fallthruBB fallthruBB 44 // [section end] 45 // new_label 46 // unconditional br target_label 47 void InsertJmpPadAtSecEnd(Insn &insn, uint32 targetLabelIdx, BB &targetBB); 48 void InitSecEnd(); 49 uint32 CalculateAlignRange(const BB &bb, uint32 addr) const; 50 uint32 CalculateIfBBNum() const; 51 void SetInsnId() const; 52 bool CheckFunctionSize(uint32 maxSize) const; 53 }; /* class AArch64ShortBranch */ 54 55 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgFixShortBranch, maplebe::CGFunc) 56 MAPLE_FUNC_PHASE_DECLARE_END 57 } /* namespace maplebe */ 58 #endif /* MAPLEBE_INCLUDE_CG_AARCH64_AARCH64_FIXSHORTBRANCH_H */ 59