1//===- HexagonMCInstLower.cpp - Convert Hexagon MachineInstr to an MCInst -===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains code to lower Hexagon MachineInstrs to their corresponding
10// MCInst records.
11//
12//===----------------------------------------------------------------------===//
13
14#include "HexagonAsmPrinter.h"
15#include "MCTargetDesc/HexagonMCExpr.h"
16#include "MCTargetDesc/HexagonMCInstrInfo.h"
17#include "MCTargetDesc/HexagonMCTargetDesc.h"
18#include "llvm/ADT/APFloat.h"
19#include "llvm/ADT/APInt.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
21#include "llvm/CodeGen/MachineInstr.h"
22#include "llvm/CodeGen/MachineOperand.h"
23#include "llvm/IR/Constants.h"
24#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCExpr.h"
26#include "llvm/MC/MCInst.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
29#include <cassert>
30
31using namespace llvm;
32
33namespace llvm {
34
35void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
36 MCInst &MCB, HexagonAsmPrinter &AP);
37
38} // end namespace llvm
39
40static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
41 HexagonAsmPrinter &Printer, bool MustExtend) {
42 MCContext &MC = Printer.OutContext;
43 const MCExpr *ME;
44
45 // Populate the relocation type based on Hexagon target flags
46 // set on an operand
47 HexagonMCExpr::VariantKind RelocationType;
48 switch (MO.getTargetFlags() & ~HexagonII::HMOTF_ConstExtended) {
49 default:
50 RelocationType = HexagonMCExpr::VK_None;
51 break;
52 case HexagonII::MO_PCREL:
53 RelocationType = HexagonMCExpr::VK_PCREL;
54 break;
55 case HexagonII::MO_GOT:
56 RelocationType = HexagonMCExpr::VK_GOT;
57 break;
58 case HexagonII::MO_LO16:
59 RelocationType = HexagonMCExpr::VK_LO16;
60 break;
61 case HexagonII::MO_HI16:
62 RelocationType = HexagonMCExpr::VK_HI16;
63 break;
64 case HexagonII::MO_GPREL:
65 RelocationType = HexagonMCExpr::VK_GPREL;
66 break;
67 case HexagonII::MO_GDGOT:
68 RelocationType = HexagonMCExpr::VK_GD_GOT;
69 break;
70 case HexagonII::MO_GDPLT:
71 RelocationType = HexagonMCExpr::VK_GD_PLT;
72 break;
73 case HexagonII::MO_IE:
74 RelocationType = HexagonMCExpr::VK_IE;
75 break;
76 case HexagonII::MO_IEGOT:
77 RelocationType = HexagonMCExpr::VK_IE_GOT;
78 break;
79 case HexagonII::MO_TPREL:
80 RelocationType = HexagonMCExpr::VK_TPREL;
81 break;
82 }
83
84 ME = MCSymbolRefExpr::create(Symbol, specifier: RelocationType, Ctx&: MC);
85
86 if (!MO.isJTI() && MO.getOffset())
87 ME = MCBinaryExpr::createAdd(LHS: ME, RHS: MCConstantExpr::create(Value: MO.getOffset(), Ctx&: MC),
88 Ctx&: MC);
89
90 ME = HexagonMCExpr::create(Expr: ME, Ctx&: MC);
91 HexagonMCInstrInfo::setMustExtend(Expr: *ME, Val: MustExtend);
92 return MCOperand::createExpr(Val: ME);
93}
94
95// Create an MCInst from a MachineInstr
96void llvm::HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
97 MCInst &MCB, HexagonAsmPrinter &AP) {
98 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
99 HexagonMCInstrInfo::setInnerLoop(MCB);
100 return;
101 }
102 if (MI->getOpcode() == Hexagon::ENDLOOP1) {
103 HexagonMCInstrInfo::setOuterLoop(MCB);
104 return;
105 }
106 if (MI->getOpcode() == Hexagon::PATCHABLE_FUNCTION_ENTER) {
107 AP.EmitSled(MI: *MI, Kind: HexagonAsmPrinter::SledKind::FUNCTION_ENTER);
108 return;
109 }
110 if (MI->getOpcode() == Hexagon::PATCHABLE_FUNCTION_EXIT) {
111 AP.EmitSled(MI: *MI, Kind: HexagonAsmPrinter::SledKind::FUNCTION_EXIT);
112 return;
113 }
114 if (MI->getOpcode() == Hexagon::PATCHABLE_TAIL_CALL) {
115 AP.EmitSled(MI: *MI, Kind: HexagonAsmPrinter::SledKind::TAIL_CALL);
116 return;
117 }
118 if (MI->getOpcode() == Hexagon::PATCHABLE_EVENT_CALL) {
119 AP.LowerPATCHABLE_EVENT_CALL(MI: *MI, Typed: false);
120 return;
121 }
122 if (MI->getOpcode() == Hexagon::PATCHABLE_TYPED_EVENT_CALL) {
123 AP.LowerPATCHABLE_EVENT_CALL(MI: *MI, Typed: true);
124 return;
125 }
126 if (MI->getOpcode() == Hexagon::KCFI_CHECK) {
127 AP.LowerKCFI_CHECK(MI: *MI);
128 return;
129 }
130
131 MCInst *MCI = AP.OutContext.createMCInst();
132 MCI->setOpcode(MI->getOpcode());
133 assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
134 "MCI opcode should have been set on construction");
135
136 for (const MachineOperand &MO : MI->operands()) {
137 MCOperand MCO;
138 bool MustExtend = MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended;
139
140 switch (MO.getType()) {
141 default:
142 MI->print(OS&: errs());
143 llvm_unreachable("unknown operand type");
144 case MachineOperand::MO_RegisterMask:
145 continue;
146 case MachineOperand::MO_Register:
147 // Ignore all implicit register operands.
148 if (MO.isImplicit())
149 continue;
150 MCO = MCOperand::createReg(Reg: MO.getReg());
151 break;
152 case MachineOperand::MO_FPImmediate: {
153 APFloat Val = MO.getFPImm()->getValueAPF();
154 // FP immediates are used only when setting GPRs, so they may be dealt
155 // with like regular immediates from this point on.
156 auto Expr = HexagonMCExpr::create(
157 Expr: MCConstantExpr::create(Value: *Val.bitcastToAPInt().getRawData(),
158 Ctx&: AP.OutContext),
159 Ctx&: AP.OutContext);
160 HexagonMCInstrInfo::setMustExtend(Expr: *Expr, Val: MustExtend);
161 MCO = MCOperand::createExpr(Val: Expr);
162 break;
163 }
164 case MachineOperand::MO_Immediate: {
165 auto Expr = HexagonMCExpr::create(
166 Expr: MCConstantExpr::create(Value: MO.getImm(), Ctx&: AP.OutContext), Ctx&: AP.OutContext);
167 HexagonMCInstrInfo::setMustExtend(Expr: *Expr, Val: MustExtend);
168 MCO = MCOperand::createExpr(Val: Expr);
169 break;
170 }
171 case MachineOperand::MO_MachineBasicBlock: {
172 MCExpr const *Expr = MCSymbolRefExpr::create(Symbol: MO.getMBB()->getSymbol(),
173 Ctx&: AP.OutContext);
174 Expr = HexagonMCExpr::create(Expr, Ctx&: AP.OutContext);
175 HexagonMCInstrInfo::setMustExtend(Expr: *Expr, Val: MustExtend);
176 MCO = MCOperand::createExpr(Val: Expr);
177 break;
178 }
179 case MachineOperand::MO_GlobalAddress:
180 MCO = GetSymbolRef(MO, Symbol: AP.getSymbol(GV: MO.getGlobal()), Printer&: AP, MustExtend);
181 break;
182 case MachineOperand::MO_ExternalSymbol:
183 MCO = GetSymbolRef(MO, Symbol: AP.GetExternalSymbolSymbol(Sym: MO.getSymbolName()),
184 Printer&: AP, MustExtend);
185 break;
186 case MachineOperand::MO_JumpTableIndex:
187 MCO = GetSymbolRef(MO, Symbol: AP.GetJTISymbol(JTID: MO.getIndex()), Printer&: AP, MustExtend);
188 break;
189 case MachineOperand::MO_ConstantPoolIndex:
190 MCO = GetSymbolRef(MO, Symbol: AP.GetCPISymbol(CPID: MO.getIndex()), Printer&: AP, MustExtend);
191 break;
192 case MachineOperand::MO_BlockAddress:
193 MCO = GetSymbolRef(MO, Symbol: AP.GetBlockAddressSymbol(BA: MO.getBlockAddress()), Printer&: AP,
194 MustExtend);
195 break;
196 }
197
198 MCI->addOperand(Op: MCO);
199 }
200 AP.HexagonProcessInstruction(Inst&: *MCI, MBB: *MI);
201 HexagonMCInstrInfo::extendIfNeeded(Context&: AP.OutContext, MCII, MCB, MCI: *MCI);
202 MCB.addOperand(Op: MCOperand::createInst(Val: MCI));
203}
204