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 // for long branch which exceeds size of imm19, we need to insert pad. 30 // see InsertJumpPad to know how we do this. 31 void PatchLongBranch(); 32 void FixLdr(); 33 34 private: 35 CGFunc *cgFunc = nullptr; 36 // For long branch caused by cold-hot bb splitting , 37 // insert an unconditional branch at the end section in order to minimize the negative impact 38 // From To 39 // cond_br target_label cond_br new_label 40 // fallthruBB fallthruBB 41 // [section end] 42 // new_label 43 // unconditional br target_label 44 uint32 CalculateAlignRange(const BB &bb, uint32 addr) const; 45 uint32 CalculateIfBBNum() const; 46 void SetInsnId() const; 47 bool CheckFunctionSize(uint32 maxSize) const; 48 }; /* class AArch64ShortBranch */ 49 50 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgFixShortBranch, maplebe::CGFunc) 51 MAPLE_FUNC_PHASE_DECLARE_END 52 } /* namespace maplebe */ 53 #endif /* MAPLEBE_INCLUDE_CG_AARCH64_AARCH64_FIXSHORTBRANCH_H */ 54