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 #include "aarch64_optimize_common.h"
17 #include "aarch64_cgfunc.h"
18
19 namespace maplebe {
ModifyJumpTarget(Operand & targetOperand,BB & bb)20 void AArch64InsnVisitor::ModifyJumpTarget(Operand &targetOperand, BB &bb)
21 {
22 if (bb.GetKind() == BB::kBBGoto) {
23 for (Insn *insn = bb.GetLastInsn(); insn != nullptr; insn = insn->GetPrev()) {
24 if (insn->GetMachineOpcode() == MOP_adrp_label) {
25 maple::LabelIdx labidx = static_cast<LabelOperand &>(targetOperand).GetLabelIndex();
26 LabelOperand &label = static_cast<AArch64CGFunc *>(GetCGFunc())->GetOrCreateLabelOperand(labidx);
27 insn->SetOperand(1, label);
28 break;
29 }
30 }
31 // fallthru below to patch the branch insn
32 }
33 CHECK_NULL_FATAL(bb.GetLastMachineInsn());
34 bb.GetLastMachineInsn()->SetOperand(AArch64isa::GetJumpTargetIdx(*bb.GetLastMachineInsn()), targetOperand);
35 }
36
ModifyJumpTarget(maple::LabelIdx targetLabel,BB & bb)37 void AArch64InsnVisitor::ModifyJumpTarget(maple::LabelIdx targetLabel, BB &bb)
38 {
39 ModifyJumpTarget(static_cast<AArch64CGFunc *>(GetCGFunc())->GetOrCreateLabelOperand(targetLabel), bb);
40 }
41
42 /*
43 * Precondition: The given insn is a jump instruction.
44 * Get the jump target label from the given instruction.
45 * Note: MOP_xbr is a branching instruction, but the target is unknown at compile time,
46 * because a register instead of label. So we don't take it as a branching instruction.
47 */
GetJumpLabel(const Insn & insn) const48 LabelIdx AArch64InsnVisitor::GetJumpLabel(const Insn &insn) const
49 {
50 uint32 operandIdx = AArch64isa::GetJumpTargetIdx(insn);
51 if (insn.GetOperand(operandIdx).IsLabelOpnd()) {
52 return static_cast<LabelOperand &>(insn.GetOperand(operandIdx)).GetLabelIndex();
53 }
54 DEBUG_ASSERT(false, "Operand is not label");
55 return 0;
56 }
57 } /* namespace maplebe */
58