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 "x64_optimize_common.h"
17
18 namespace maplebe {
ModifyJumpTarget(Operand & targetOperand,BB & bb)19 void X64InsnVisitor::ModifyJumpTarget(Operand &targetOperand, BB &bb)
20 {
21 Insn *jmpInsn = bb.GetLastInsn();
22 jmpInsn->SetOperand(x64::GetJumpTargetIdx(*jmpInsn), targetOperand);
23 }
24
ModifyJumpTarget(LabelIdx targetLabel,BB & bb)25 void X64InsnVisitor::ModifyJumpTarget(LabelIdx targetLabel, BB &bb)
26 {
27 std::string lableName = ".L." + std::to_string(GetCGFunc()->GetUniqueID()) + "__" + std::to_string(targetLabel);
28 ModifyJumpTarget(GetCGFunc()->GetOpndBuilder()->CreateLabel(lableName.c_str(), targetLabel), bb);
29 }
30
31 /*
32 * Precondition: The given insn is a jump instruction.
33 * Get the jump target label operand index from the given instruction.
34 * Note: MOP_jmp_m, MOP_jmp_r is a jump instruction, but the target is unknown at compile time.
35 */
GetJumpLabel(const Insn & insn) const36 LabelIdx X64InsnVisitor::GetJumpLabel(const Insn &insn) const
37 {
38 uint32 operandIdx = x64::GetJumpTargetIdx(insn);
39 if (insn.GetOperand(operandIdx).IsLabelOpnd()) {
40 return static_cast<LabelOperand &>(insn.GetOperand(operandIdx)).GetLabelIndex();
41 }
42 DEBUG_ASSERT(false, "Operand is not label");
43 return 0;
44 }
45 } /* namespace maplebe */
46