1//===- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly ---===//
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 a printer that converts from our internal representation
10// of machine-dependent LLVM code to Hexagon assembly language. This printer is
11// the output mechanism used by `llc'.
12//
13//===----------------------------------------------------------------------===//
14
15#include "HexagonAsmPrinter.h"
16#include "HexagonInstrInfo.h"
17#include "HexagonRegisterInfo.h"
18#include "HexagonSubtarget.h"
19#include "MCTargetDesc/HexagonInstPrinter.h"
20#include "MCTargetDesc/HexagonMCExpr.h"
21#include "MCTargetDesc/HexagonMCInstrInfo.h"
22#include "MCTargetDesc/HexagonMCTargetDesc.h"
23#include "MCTargetDesc/HexagonTargetStreamer.h"
24#include "TargetInfo/HexagonTargetInfo.h"
25#include "llvm/ADT/StringExtras.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/ADT/Twine.h"
28#include "llvm/BinaryFormat/ELF.h"
29#include "llvm/CodeGen/AsmPrinter.h"
30#include "llvm/CodeGen/MachineBasicBlock.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstr.h"
33#include "llvm/CodeGen/MachineOperand.h"
34#include "llvm/CodeGen/TargetRegisterInfo.h"
35#include "llvm/CodeGen/TargetSubtargetInfo.h"
36#include "llvm/MC/MCContext.h"
37#include "llvm/MC/MCDirectives.h"
38#include "llvm/MC/MCExpr.h"
39#include "llvm/MC/MCInst.h"
40#include "llvm/MC/MCRegisterInfo.h"
41#include "llvm/MC/MCSectionELF.h"
42#include "llvm/MC/MCStreamer.h"
43#include "llvm/MC/MCSymbol.h"
44#include "llvm/MC/TargetRegistry.h"
45#include "llvm/Support/Casting.h"
46#include "llvm/Support/Compiler.h"
47#include "llvm/Support/ErrorHandling.h"
48#include "llvm/Support/raw_ostream.h"
49#include "llvm/Target/TargetMachine.h"
50#include <cassert>
51#include <cstdint>
52#include <string>
53
54using namespace llvm;
55
56namespace llvm {
57
58void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
59 MCInst &MCB, HexagonAsmPrinter &AP);
60
61} // end namespace llvm
62
63#define DEBUG_TYPE "asm-printer"
64
65// Given a scalar register return its pair.
66inline static unsigned getHexagonRegisterPair(unsigned Reg,
67 const MCRegisterInfo *RI) {
68 assert(Hexagon::IntRegsRegClass.contains(Reg));
69 unsigned Pair = *RI->superregs(Reg).begin();
70 assert(Hexagon::DoubleRegsRegClass.contains(Pair));
71 return Pair;
72}
73
74void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
75 raw_ostream &O) {
76 const MachineOperand &MO = MI->getOperand(i: OpNo);
77
78 switch (MO.getType()) {
79 default:
80 llvm_unreachable ("<unknown operand type>");
81 case MachineOperand::MO_Register:
82 O << HexagonInstPrinter::getRegisterName(Reg: MO.getReg());
83 return;
84 case MachineOperand::MO_Immediate:
85 O << MO.getImm();
86 return;
87 case MachineOperand::MO_MachineBasicBlock:
88 MO.getMBB()->getSymbol()->print(OS&: O, MAI);
89 return;
90 case MachineOperand::MO_ConstantPoolIndex:
91 GetCPISymbol(CPID: MO.getIndex())->print(OS&: O, MAI);
92 return;
93 case MachineOperand::MO_GlobalAddress:
94 PrintSymbolOperand(MO, OS&: O);
95 return;
96 }
97}
98
99// isBlockOnlyReachableByFallthrough - We need to override this since the
100// default AsmPrinter does not print labels for any basic block that
101// is only reachable by a fall through. That works for all cases except
102// for the case in which the basic block is reachable by a fall through but
103// through an indirect from a jump table. In this case, the jump table
104// will contain a label not defined by AsmPrinter.
105bool HexagonAsmPrinter::isBlockOnlyReachableByFallthrough(
106 const MachineBasicBlock *MBB) const {
107 if (MBB->hasAddressTaken())
108 return false;
109 return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
110}
111
112/// PrintAsmOperand - Print out an operand for an inline asm expression.
113bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
114 const char *ExtraCode,
115 raw_ostream &OS) {
116 // Does this asm operand have a single letter operand modifier?
117 if (ExtraCode && ExtraCode[0]) {
118 if (ExtraCode[1] != 0)
119 return true; // Unknown modifier.
120
121 switch (ExtraCode[0]) {
122 default:
123 // See if this is a generic print operand
124 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS);
125 case 'L':
126 case 'H': { // The highest-numbered register of a pair.
127 const MachineOperand &MO = MI->getOperand(i: OpNo);
128 const MachineFunction &MF = *MI->getParent()->getParent();
129 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
130 if (!MO.isReg())
131 return true;
132 Register RegNumber = MO.getReg();
133 // This should be an assert in the frontend.
134 if (Hexagon::DoubleRegsRegClass.contains(Reg: RegNumber))
135 RegNumber = TRI->getSubReg(Reg: RegNumber, Idx: ExtraCode[0] == 'L' ?
136 Hexagon::isub_lo :
137 Hexagon::isub_hi);
138 OS << HexagonInstPrinter::getRegisterName(Reg: RegNumber);
139 return false;
140 }
141 case 'I':
142 // Write 'i' if an integer constant, otherwise nothing. Used to print
143 // addi vs add, etc.
144 if (MI->getOperand(i: OpNo).isImm())
145 OS << "i";
146 return false;
147 }
148 }
149
150 printOperand(MI, OpNo, O&: OS);
151 return false;
152}
153
154bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
155 unsigned OpNo,
156 const char *ExtraCode,
157 raw_ostream &O) {
158 if (ExtraCode && ExtraCode[0])
159 return true; // Unknown modifier.
160
161 const MachineOperand &Base = MI->getOperand(i: OpNo);
162 const MachineOperand &Offset = MI->getOperand(i: OpNo+1);
163
164 if (Base.isReg())
165 printOperand(MI, OpNo, O);
166 else
167 llvm_unreachable("Unimplemented");
168
169 if (Offset.isImm()) {
170 if (Offset.getImm())
171 O << "+#" << Offset.getImm();
172 } else {
173 llvm_unreachable("Unimplemented");
174 }
175
176 return false;
177}
178
179static MCSymbol *smallData(AsmPrinter &AP, const MachineInstr &MI,
180 MCStreamer &OutStreamer, const MCOperand &Imm,
181 int AlignSize, const MCSubtargetInfo& STI) {
182 MCSymbol *Sym;
183 int64_t Value;
184 if (Imm.getExpr()->evaluateAsAbsolute(Res&: Value)) {
185 StringRef sectionPrefix;
186 std::string ImmString;
187 StringRef Name;
188 if (AlignSize == 8) {
189 Name = ".CONST_0000000000000000";
190 sectionPrefix = ".gnu.linkonce.l8";
191 ImmString = utohexstr(X: Value);
192 } else {
193 Name = ".CONST_00000000";
194 sectionPrefix = ".gnu.linkonce.l4";
195 ImmString = utohexstr(X: static_cast<uint32_t>(Value));
196 }
197
198 std::string symbolName = // Yes, leading zeros are kept.
199 Name.drop_back(N: ImmString.size()).str() + ImmString;
200 std::string sectionName = sectionPrefix.str() + symbolName;
201
202 MCSectionELF *Section = OutStreamer.getContext().getELFSection(
203 Section: sectionName, Type: ELF::SHT_PROGBITS, Flags: ELF::SHF_WRITE | ELF::SHF_ALLOC);
204 OutStreamer.switchSection(Section);
205
206 Sym = AP.OutContext.getOrCreateSymbol(Name: Twine(symbolName));
207 if (Sym->isUndefined()) {
208 OutStreamer.emitLabel(Symbol: Sym);
209 OutStreamer.emitSymbolAttribute(Symbol: Sym, Attribute: MCSA_Global);
210 OutStreamer.emitIntValue(Value, Size: AlignSize);
211 OutStreamer.emitCodeAlignment(Alignment: Align(AlignSize), STI);
212 }
213 } else {
214 assert(Imm.isExpr() && "Expected expression and found none");
215 const MachineOperand &MO = MI.getOperand(i: 1);
216 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI());
217 MCSymbol *MOSymbol = nullptr;
218 if (MO.isGlobal())
219 MOSymbol = AP.getSymbol(GV: MO.getGlobal());
220 else if (MO.isCPI())
221 MOSymbol = AP.GetCPISymbol(CPID: MO.getIndex());
222 else if (MO.isJTI())
223 MOSymbol = AP.GetJTISymbol(JTID: MO.getIndex());
224 else
225 llvm_unreachable("Unknown operand type!");
226
227 StringRef SymbolName = MOSymbol->getName();
228 std::string LitaName = ".CONST_" + SymbolName.str();
229
230 MCSectionELF *Section = OutStreamer.getContext().getELFSection(
231 Section: ".lita", Type: ELF::SHT_PROGBITS, Flags: ELF::SHF_WRITE | ELF::SHF_ALLOC);
232
233 OutStreamer.switchSection(Section);
234 Sym = AP.OutContext.getOrCreateSymbol(Name: Twine(LitaName));
235 if (Sym->isUndefined()) {
236 OutStreamer.emitLabel(Symbol: Sym);
237 OutStreamer.emitSymbolAttribute(Symbol: Sym, Attribute: MCSA_Local);
238 OutStreamer.emitValue(Value: Imm.getExpr(), Size: AlignSize);
239 OutStreamer.emitCodeAlignment(Alignment: Align(AlignSize), STI);
240 }
241 }
242 return Sym;
243}
244
245static MCInst ScaleVectorOffset(MCInst &Inst, unsigned OpNo,
246 unsigned VectorSize, MCContext &Ctx) {
247 MCInst T;
248 T.setOpcode(Inst.getOpcode());
249 for (unsigned i = 0, n = Inst.getNumOperands(); i != n; ++i) {
250 if (i != OpNo) {
251 T.addOperand(Op: Inst.getOperand(i));
252 continue;
253 }
254 MCOperand &ImmOp = Inst.getOperand(i);
255 const auto *HE = static_cast<const HexagonMCExpr*>(ImmOp.getExpr());
256 int32_t V = cast<MCConstantExpr>(Val: HE->getExpr())->getValue();
257 auto *NewCE = MCConstantExpr::create(Value: V / int32_t(VectorSize), Ctx);
258 auto *NewHE = HexagonMCExpr::create(Expr: NewCE, Ctx);
259 T.addOperand(Op: MCOperand::createExpr(Val: NewHE));
260 }
261 return T;
262}
263
264void HexagonAsmPrinter::HexagonProcessInstruction(MCInst &Inst,
265 const MachineInstr &MI) {
266 MCInst &MappedInst = static_cast <MCInst &>(Inst);
267 const MCRegisterInfo *RI = OutStreamer->getContext().getRegisterInfo();
268 const MachineFunction &MF = *MI.getParent()->getParent();
269 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
270 unsigned VectorSize = HRI.getRegSizeInBits(RC: Hexagon::HvxVRRegClass) / 8;
271
272 switch (Inst.getOpcode()) {
273 default:
274 return;
275
276 case Hexagon::A2_iconst: {
277 Inst.setOpcode(Hexagon::A2_addi);
278 MCOperand Reg = Inst.getOperand(i: 0);
279 MCOperand S16 = Inst.getOperand(i: 1);
280 HexagonMCInstrInfo::setMustNotExtend(Expr: *S16.getExpr());
281 HexagonMCInstrInfo::setS27_2_reloc(Expr: *S16.getExpr());
282 Inst.clear();
283 Inst.addOperand(Op: Reg);
284 Inst.addOperand(Op: MCOperand::createReg(Reg: Hexagon::R0));
285 Inst.addOperand(Op: S16);
286 break;
287 }
288
289 case Hexagon::A2_tfrf: {
290 const MCConstantExpr *Zero = MCConstantExpr::create(Value: 0, Ctx&: OutContext);
291 Inst.setOpcode(Hexagon::A2_paddif);
292 Inst.addOperand(Op: MCOperand::createExpr(Val: Zero));
293 break;
294 }
295
296 case Hexagon::A2_tfrt: {
297 const MCConstantExpr *Zero = MCConstantExpr::create(Value: 0, Ctx&: OutContext);
298 Inst.setOpcode(Hexagon::A2_paddit);
299 Inst.addOperand(Op: MCOperand::createExpr(Val: Zero));
300 break;
301 }
302
303 case Hexagon::A2_tfrfnew: {
304 const MCConstantExpr *Zero = MCConstantExpr::create(Value: 0, Ctx&: OutContext);
305 Inst.setOpcode(Hexagon::A2_paddifnew);
306 Inst.addOperand(Op: MCOperand::createExpr(Val: Zero));
307 break;
308 }
309
310 case Hexagon::A2_tfrtnew: {
311 const MCConstantExpr *Zero = MCConstantExpr::create(Value: 0, Ctx&: OutContext);
312 Inst.setOpcode(Hexagon::A2_padditnew);
313 Inst.addOperand(Op: MCOperand::createExpr(Val: Zero));
314 break;
315 }
316
317 case Hexagon::A2_zxtb: {
318 const MCConstantExpr *C255 = MCConstantExpr::create(Value: 255, Ctx&: OutContext);
319 Inst.setOpcode(Hexagon::A2_andir);
320 Inst.addOperand(Op: MCOperand::createExpr(Val: C255));
321 break;
322 }
323
324 // "$dst = CONST64(#$src1)",
325 case Hexagon::CONST64:
326 if (!OutStreamer->hasRawTextSupport()) {
327 const MCOperand &Imm = MappedInst.getOperand(i: 1);
328 MCSectionSubPair Current = OutStreamer->getCurrentSection();
329
330 MCSymbol *Sym =
331 smallData(AP&: *this, MI, OutStreamer&: *OutStreamer, Imm, AlignSize: 8, STI: getSubtargetInfo());
332
333 OutStreamer->switchSection(Section: Current.first, Subsec: Current.second);
334 MCInst TmpInst;
335 MCOperand &Reg = MappedInst.getOperand(i: 0);
336 TmpInst.setOpcode(Hexagon::L2_loadrdgp);
337 TmpInst.addOperand(Op: Reg);
338 TmpInst.addOperand(Op: MCOperand::createExpr(
339 Val: MCSymbolRefExpr::create(Symbol: Sym, Ctx&: OutContext)));
340 MappedInst = TmpInst;
341
342 }
343 break;
344 case Hexagon::CONST32:
345 if (!OutStreamer->hasRawTextSupport()) {
346 MCOperand &Imm = MappedInst.getOperand(i: 1);
347 MCSectionSubPair Current = OutStreamer->getCurrentSection();
348 MCSymbol *Sym =
349 smallData(AP&: *this, MI, OutStreamer&: *OutStreamer, Imm, AlignSize: 4, STI: getSubtargetInfo());
350 OutStreamer->switchSection(Section: Current.first, Subsec: Current.second);
351 MCInst TmpInst;
352 MCOperand &Reg = MappedInst.getOperand(i: 0);
353 TmpInst.setOpcode(Hexagon::L2_loadrigp);
354 TmpInst.addOperand(Op: Reg);
355 TmpInst.addOperand(Op: MCOperand::createExpr(Val: HexagonMCExpr::create(
356 Expr: MCSymbolRefExpr::create(Symbol: Sym, Ctx&: OutContext), Ctx&: OutContext)));
357 MappedInst = TmpInst;
358 }
359 break;
360
361 // C2_pxfer_map maps to C2_or instruction. Though, it's possible to use
362 // C2_or during instruction selection itself but it results
363 // into suboptimal code.
364 case Hexagon::C2_pxfer_map: {
365 MCOperand &Ps = Inst.getOperand(i: 1);
366 MappedInst.setOpcode(Hexagon::C2_or);
367 MappedInst.addOperand(Op: Ps);
368 return;
369 }
370
371 // Vector reduce complex multiply by scalar, Rt & 1 map to :hi else :lo
372 // The insn is mapped from the 4 operand to the 3 operand raw form taking
373 // 3 register pairs.
374 case Hexagon::M2_vrcmpys_acc_s1: {
375 MCOperand &Rt = Inst.getOperand(i: 3);
376 assert(Rt.isReg() && "Expected register and none was found");
377 unsigned Reg = RI->getEncodingValue(Reg: Rt.getReg());
378 if (Reg & 1)
379 MappedInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_h);
380 else
381 MappedInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_l);
382 Rt.setReg(getHexagonRegisterPair(Reg: Rt.getReg(), RI));
383 return;
384 }
385 case Hexagon::M2_vrcmpys_s1: {
386 MCOperand &Rt = Inst.getOperand(i: 2);
387 assert(Rt.isReg() && "Expected register and none was found");
388 unsigned Reg = RI->getEncodingValue(Reg: Rt.getReg());
389 if (Reg & 1)
390 MappedInst.setOpcode(Hexagon::M2_vrcmpys_s1_h);
391 else
392 MappedInst.setOpcode(Hexagon::M2_vrcmpys_s1_l);
393 Rt.setReg(getHexagonRegisterPair(Reg: Rt.getReg(), RI));
394 return;
395 }
396
397 case Hexagon::M2_vrcmpys_s1rp: {
398 MCOperand &Rt = Inst.getOperand(i: 2);
399 assert(Rt.isReg() && "Expected register and none was found");
400 unsigned Reg = RI->getEncodingValue(Reg: Rt.getReg());
401 if (Reg & 1)
402 MappedInst.setOpcode(Hexagon::M2_vrcmpys_s1rp_h);
403 else
404 MappedInst.setOpcode(Hexagon::M2_vrcmpys_s1rp_l);
405 Rt.setReg(getHexagonRegisterPair(Reg: Rt.getReg(), RI));
406 return;
407 }
408
409 case Hexagon::A4_boundscheck: {
410 MCOperand &Rs = Inst.getOperand(i: 1);
411 assert(Rs.isReg() && "Expected register and none was found");
412 unsigned Reg = RI->getEncodingValue(Reg: Rs.getReg());
413 if (Reg & 1) // Odd mapped to raw:hi, regpair is rodd:odd-1, like r3:2
414 MappedInst.setOpcode(Hexagon::A4_boundscheck_hi);
415 else // raw:lo
416 MappedInst.setOpcode(Hexagon::A4_boundscheck_lo);
417 Rs.setReg(getHexagonRegisterPair(Reg: Rs.getReg(), RI));
418 return;
419 }
420
421 case Hexagon::PS_call_nr:
422 Inst.setOpcode(Hexagon::J2_call);
423 break;
424
425 case Hexagon::PS_readcr:
426 Inst.setOpcode(Hexagon::A2_tfrcrr);
427 break;
428
429 case Hexagon::PS_readcr64:
430 Inst.setOpcode(Hexagon::A4_tfrcpp);
431 break;
432
433 case Hexagon::S5_asrhub_rnd_sat_goodsyntax: {
434 MCOperand &MO = MappedInst.getOperand(i: 2);
435 int64_t Imm;
436 MCExpr const *Expr = MO.getExpr();
437 bool Success = Expr->evaluateAsAbsolute(Res&: Imm);
438 assert(Success && "Expected immediate and none was found");
439 (void)Success;
440 MCInst TmpInst;
441 if (Imm == 0) {
442 TmpInst.setOpcode(Hexagon::S2_vsathub);
443 TmpInst.addOperand(Op: MappedInst.getOperand(i: 0));
444 TmpInst.addOperand(Op: MappedInst.getOperand(i: 1));
445 MappedInst = TmpInst;
446 return;
447 }
448 TmpInst.setOpcode(Hexagon::S5_asrhub_rnd_sat);
449 TmpInst.addOperand(Op: MappedInst.getOperand(i: 0));
450 TmpInst.addOperand(Op: MappedInst.getOperand(i: 1));
451 const MCExpr *One = MCConstantExpr::create(Value: 1, Ctx&: OutContext);
452 const MCExpr *Sub = MCBinaryExpr::createSub(LHS: Expr, RHS: One, Ctx&: OutContext);
453 TmpInst.addOperand(
454 Op: MCOperand::createExpr(Val: HexagonMCExpr::create(Expr: Sub, Ctx&: OutContext)));
455 MappedInst = TmpInst;
456 return;
457 }
458
459 case Hexagon::S5_vasrhrnd_goodsyntax:
460 case Hexagon::S2_asr_i_p_rnd_goodsyntax: {
461 MCOperand &MO2 = MappedInst.getOperand(i: 2);
462 MCExpr const *Expr = MO2.getExpr();
463 int64_t Imm;
464 bool Success = Expr->evaluateAsAbsolute(Res&: Imm);
465 assert(Success && "Expected immediate and none was found");
466 (void)Success;
467 MCInst TmpInst;
468 if (Imm == 0) {
469 TmpInst.setOpcode(Hexagon::A2_combinew);
470 TmpInst.addOperand(Op: MappedInst.getOperand(i: 0));
471 MCOperand &MO1 = MappedInst.getOperand(i: 1);
472 MCRegister High = RI->getSubReg(Reg: MO1.getReg(), Idx: Hexagon::isub_hi);
473 MCRegister Low = RI->getSubReg(Reg: MO1.getReg(), Idx: Hexagon::isub_lo);
474 // Add a new operand for the second register in the pair.
475 TmpInst.addOperand(Op: MCOperand::createReg(Reg: High));
476 TmpInst.addOperand(Op: MCOperand::createReg(Reg: Low));
477 MappedInst = TmpInst;
478 return;
479 }
480
481 if (Inst.getOpcode() == Hexagon::S2_asr_i_p_rnd_goodsyntax)
482 TmpInst.setOpcode(Hexagon::S2_asr_i_p_rnd);
483 else
484 TmpInst.setOpcode(Hexagon::S5_vasrhrnd);
485 TmpInst.addOperand(Op: MappedInst.getOperand(i: 0));
486 TmpInst.addOperand(Op: MappedInst.getOperand(i: 1));
487 const MCExpr *One = MCConstantExpr::create(Value: 1, Ctx&: OutContext);
488 const MCExpr *Sub = MCBinaryExpr::createSub(LHS: Expr, RHS: One, Ctx&: OutContext);
489 TmpInst.addOperand(
490 Op: MCOperand::createExpr(Val: HexagonMCExpr::create(Expr: Sub, Ctx&: OutContext)));
491 MappedInst = TmpInst;
492 return;
493 }
494
495 // if ("#u5==0") Assembler mapped to: "Rd=Rs"; else Rd=asr(Rs,#u5-1):rnd
496 case Hexagon::S2_asr_i_r_rnd_goodsyntax: {
497 MCOperand &MO = Inst.getOperand(i: 2);
498 MCExpr const *Expr = MO.getExpr();
499 int64_t Imm;
500 bool Success = Expr->evaluateAsAbsolute(Res&: Imm);
501 assert(Success && "Expected immediate and none was found");
502 (void)Success;
503 MCInst TmpInst;
504 if (Imm == 0) {
505 TmpInst.setOpcode(Hexagon::A2_tfr);
506 TmpInst.addOperand(Op: MappedInst.getOperand(i: 0));
507 TmpInst.addOperand(Op: MappedInst.getOperand(i: 1));
508 MappedInst = TmpInst;
509 return;
510 }
511 TmpInst.setOpcode(Hexagon::S2_asr_i_r_rnd);
512 TmpInst.addOperand(Op: MappedInst.getOperand(i: 0));
513 TmpInst.addOperand(Op: MappedInst.getOperand(i: 1));
514 const MCExpr *One = MCConstantExpr::create(Value: 1, Ctx&: OutContext);
515 const MCExpr *Sub = MCBinaryExpr::createSub(LHS: Expr, RHS: One, Ctx&: OutContext);
516 TmpInst.addOperand(
517 Op: MCOperand::createExpr(Val: HexagonMCExpr::create(Expr: Sub, Ctx&: OutContext)));
518 MappedInst = TmpInst;
519 return;
520 }
521
522 // Translate a "$Rdd = #imm" to "$Rdd = combine(#[-1,0], #imm)"
523 case Hexagon::A2_tfrpi: {
524 MCInst TmpInst;
525 MCOperand &Rdd = MappedInst.getOperand(i: 0);
526 MCOperand &MO = MappedInst.getOperand(i: 1);
527
528 TmpInst.setOpcode(Hexagon::A2_combineii);
529 TmpInst.addOperand(Op: Rdd);
530 int64_t Imm;
531 bool Success = MO.getExpr()->evaluateAsAbsolute(Res&: Imm);
532 if (Success && Imm < 0) {
533 const MCExpr *MOne = MCConstantExpr::create(Value: -1, Ctx&: OutContext);
534 const HexagonMCExpr *E = HexagonMCExpr::create(Expr: MOne, Ctx&: OutContext);
535 TmpInst.addOperand(Op: MCOperand::createExpr(Val: E));
536 } else {
537 const MCExpr *Zero = MCConstantExpr::create(Value: 0, Ctx&: OutContext);
538 const HexagonMCExpr *E = HexagonMCExpr::create(Expr: Zero, Ctx&: OutContext);
539 TmpInst.addOperand(Op: MCOperand::createExpr(Val: E));
540 }
541 TmpInst.addOperand(Op: MO);
542 MappedInst = TmpInst;
543 return;
544 }
545
546 // Translate a "$Rdd = $Rss" to "$Rdd = combine($Rs, $Rt)"
547 case Hexagon::A2_tfrp: {
548 MCOperand &MO = MappedInst.getOperand(i: 1);
549 MCRegister High = RI->getSubReg(Reg: MO.getReg(), Idx: Hexagon::isub_hi);
550 MCRegister Low = RI->getSubReg(Reg: MO.getReg(), Idx: Hexagon::isub_lo);
551 MO.setReg(High);
552 // Add a new operand for the second register in the pair.
553 MappedInst.addOperand(Op: MCOperand::createReg(Reg: Low));
554 MappedInst.setOpcode(Hexagon::A2_combinew);
555 return;
556 }
557
558 case Hexagon::A2_tfrpt:
559 case Hexagon::A2_tfrpf: {
560 MCOperand &MO = MappedInst.getOperand(i: 2);
561 MCRegister High = RI->getSubReg(Reg: MO.getReg(), Idx: Hexagon::isub_hi);
562 MCRegister Low = RI->getSubReg(Reg: MO.getReg(), Idx: Hexagon::isub_lo);
563 MO.setReg(High);
564 // Add a new operand for the second register in the pair.
565 MappedInst.addOperand(Op: MCOperand::createReg(Reg: Low));
566 MappedInst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrpt)
567 ? Hexagon::C2_ccombinewt
568 : Hexagon::C2_ccombinewf);
569 return;
570 }
571
572 case Hexagon::A2_tfrptnew:
573 case Hexagon::A2_tfrpfnew: {
574 MCOperand &MO = MappedInst.getOperand(i: 2);
575 MCRegister High = RI->getSubReg(Reg: MO.getReg(), Idx: Hexagon::isub_hi);
576 MCRegister Low = RI->getSubReg(Reg: MO.getReg(), Idx: Hexagon::isub_lo);
577 MO.setReg(High);
578 // Add a new operand for the second register in the pair.
579 MappedInst.addOperand(Op: MCOperand::createReg(Reg: Low));
580 MappedInst.setOpcode(Inst.getOpcode() == Hexagon::A2_tfrptnew
581 ? Hexagon::C2_ccombinewnewt
582 : Hexagon::C2_ccombinewnewf);
583 return;
584 }
585
586 case Hexagon::M2_mpysmi: {
587 MCOperand &Imm = MappedInst.getOperand(i: 2);
588 MCExpr const *Expr = Imm.getExpr();
589 int64_t Value;
590 bool Success = Expr->evaluateAsAbsolute(Res&: Value);
591 assert(Success);
592 (void)Success;
593 if (Value < 0 && Value > -256) {
594 MappedInst.setOpcode(Hexagon::M2_mpysin);
595 Imm.setExpr(HexagonMCExpr::create(
596 Expr: MCUnaryExpr::createMinus(Expr, Ctx&: OutContext), Ctx&: OutContext));
597 } else
598 MappedInst.setOpcode(Hexagon::M2_mpysip);
599 return;
600 }
601
602 case Hexagon::A2_addsp: {
603 MCOperand &Rt = Inst.getOperand(i: 1);
604 assert(Rt.isReg() && "Expected register and none was found");
605 unsigned Reg = RI->getEncodingValue(Reg: Rt.getReg());
606 if (Reg & 1)
607 MappedInst.setOpcode(Hexagon::A2_addsph);
608 else
609 MappedInst.setOpcode(Hexagon::A2_addspl);
610 Rt.setReg(getHexagonRegisterPair(Reg: Rt.getReg(), RI));
611 return;
612 }
613
614 case Hexagon::V6_vd0: {
615 MCInst TmpInst;
616 assert(Inst.getOperand(0).isReg() &&
617 "Expected register and none was found");
618
619 TmpInst.setOpcode(Hexagon::V6_vxor);
620 TmpInst.addOperand(Op: Inst.getOperand(i: 0));
621 TmpInst.addOperand(Op: Inst.getOperand(i: 0));
622 TmpInst.addOperand(Op: Inst.getOperand(i: 0));
623 MappedInst = TmpInst;
624 return;
625 }
626
627 case Hexagon::V6_vdd0: {
628 MCInst TmpInst;
629 assert (Inst.getOperand(0).isReg() &&
630 "Expected register and none was found");
631
632 TmpInst.setOpcode(Hexagon::V6_vsubw_dv);
633 TmpInst.addOperand(Op: Inst.getOperand(i: 0));
634 TmpInst.addOperand(Op: Inst.getOperand(i: 0));
635 TmpInst.addOperand(Op: Inst.getOperand(i: 0));
636 MappedInst = TmpInst;
637 return;
638 }
639
640 case Hexagon::V6_vL32Ub_pi:
641 case Hexagon::V6_vL32b_cur_pi:
642 case Hexagon::V6_vL32b_nt_cur_pi:
643 case Hexagon::V6_vL32b_pi:
644 case Hexagon::V6_vL32b_nt_pi:
645 case Hexagon::V6_vL32b_nt_tmp_pi:
646 case Hexagon::V6_vL32b_tmp_pi:
647 MappedInst = ScaleVectorOffset(Inst, OpNo: 3, VectorSize, Ctx&: OutContext);
648 return;
649
650 case Hexagon::V6_vL32Ub_ai:
651 case Hexagon::V6_vL32b_ai:
652 case Hexagon::V6_vL32b_cur_ai:
653 case Hexagon::V6_vL32b_nt_ai:
654 case Hexagon::V6_vL32b_nt_cur_ai:
655 case Hexagon::V6_vL32b_nt_tmp_ai:
656 case Hexagon::V6_vL32b_tmp_ai:
657 MappedInst = ScaleVectorOffset(Inst, OpNo: 2, VectorSize, Ctx&: OutContext);
658 return;
659
660 case Hexagon::V6_vS32Ub_pi:
661 case Hexagon::V6_vS32b_new_pi:
662 case Hexagon::V6_vS32b_nt_new_pi:
663 case Hexagon::V6_vS32b_nt_pi:
664 case Hexagon::V6_vS32b_pi:
665 MappedInst = ScaleVectorOffset(Inst, OpNo: 2, VectorSize, Ctx&: OutContext);
666 return;
667
668 case Hexagon::V6_vS32Ub_ai:
669 case Hexagon::V6_vS32b_ai:
670 case Hexagon::V6_vS32b_new_ai:
671 case Hexagon::V6_vS32b_nt_ai:
672 case Hexagon::V6_vS32b_nt_new_ai:
673 MappedInst = ScaleVectorOffset(Inst, OpNo: 1, VectorSize, Ctx&: OutContext);
674 return;
675
676 case Hexagon::V6_vL32b_cur_npred_pi:
677 case Hexagon::V6_vL32b_cur_pred_pi:
678 case Hexagon::V6_vL32b_npred_pi:
679 case Hexagon::V6_vL32b_nt_cur_npred_pi:
680 case Hexagon::V6_vL32b_nt_cur_pred_pi:
681 case Hexagon::V6_vL32b_nt_npred_pi:
682 case Hexagon::V6_vL32b_nt_pred_pi:
683 case Hexagon::V6_vL32b_nt_tmp_npred_pi:
684 case Hexagon::V6_vL32b_nt_tmp_pred_pi:
685 case Hexagon::V6_vL32b_pred_pi:
686 case Hexagon::V6_vL32b_tmp_npred_pi:
687 case Hexagon::V6_vL32b_tmp_pred_pi:
688 MappedInst = ScaleVectorOffset(Inst, OpNo: 4, VectorSize, Ctx&: OutContext);
689 return;
690
691 case Hexagon::V6_vL32b_cur_npred_ai:
692 case Hexagon::V6_vL32b_cur_pred_ai:
693 case Hexagon::V6_vL32b_npred_ai:
694 case Hexagon::V6_vL32b_nt_cur_npred_ai:
695 case Hexagon::V6_vL32b_nt_cur_pred_ai:
696 case Hexagon::V6_vL32b_nt_npred_ai:
697 case Hexagon::V6_vL32b_nt_pred_ai:
698 case Hexagon::V6_vL32b_nt_tmp_npred_ai:
699 case Hexagon::V6_vL32b_nt_tmp_pred_ai:
700 case Hexagon::V6_vL32b_pred_ai:
701 case Hexagon::V6_vL32b_tmp_npred_ai:
702 case Hexagon::V6_vL32b_tmp_pred_ai:
703 MappedInst = ScaleVectorOffset(Inst, OpNo: 3, VectorSize, Ctx&: OutContext);
704 return;
705
706 case Hexagon::V6_vS32Ub_npred_pi:
707 case Hexagon::V6_vS32Ub_pred_pi:
708 case Hexagon::V6_vS32b_new_npred_pi:
709 case Hexagon::V6_vS32b_new_pred_pi:
710 case Hexagon::V6_vS32b_npred_pi:
711 case Hexagon::V6_vS32b_nqpred_pi:
712 case Hexagon::V6_vS32b_nt_new_npred_pi:
713 case Hexagon::V6_vS32b_nt_new_pred_pi:
714 case Hexagon::V6_vS32b_nt_npred_pi:
715 case Hexagon::V6_vS32b_nt_nqpred_pi:
716 case Hexagon::V6_vS32b_nt_pred_pi:
717 case Hexagon::V6_vS32b_nt_qpred_pi:
718 case Hexagon::V6_vS32b_pred_pi:
719 case Hexagon::V6_vS32b_qpred_pi:
720 MappedInst = ScaleVectorOffset(Inst, OpNo: 3, VectorSize, Ctx&: OutContext);
721 return;
722
723 case Hexagon::V6_vS32Ub_npred_ai:
724 case Hexagon::V6_vS32Ub_pred_ai:
725 case Hexagon::V6_vS32b_new_npred_ai:
726 case Hexagon::V6_vS32b_new_pred_ai:
727 case Hexagon::V6_vS32b_npred_ai:
728 case Hexagon::V6_vS32b_nqpred_ai:
729 case Hexagon::V6_vS32b_nt_new_npred_ai:
730 case Hexagon::V6_vS32b_nt_new_pred_ai:
731 case Hexagon::V6_vS32b_nt_npred_ai:
732 case Hexagon::V6_vS32b_nt_nqpred_ai:
733 case Hexagon::V6_vS32b_nt_pred_ai:
734 case Hexagon::V6_vS32b_nt_qpred_ai:
735 case Hexagon::V6_vS32b_pred_ai:
736 case Hexagon::V6_vS32b_qpred_ai:
737 MappedInst = ScaleVectorOffset(Inst, OpNo: 2, VectorSize, Ctx&: OutContext);
738 return;
739
740 // V65+
741 case Hexagon::V6_vS32b_srls_ai:
742 MappedInst = ScaleVectorOffset(Inst, OpNo: 1, VectorSize, Ctx&: OutContext);
743 return;
744
745 case Hexagon::V6_vS32b_srls_pi:
746 MappedInst = ScaleVectorOffset(Inst, OpNo: 2, VectorSize, Ctx&: OutContext);
747 return;
748 }
749}
750
751/// Print out a single Hexagon MI to the current output stream.
752void HexagonAsmPrinter::emitInstruction(const MachineInstr *MI) {
753 Hexagon_MC::verifyInstructionPredicates(Opcode: MI->getOpcode(),
754 Features: getSubtargetInfo().getFeatureBits());
755
756 MCInst MCB;
757 MCB.setOpcode(Hexagon::BUNDLE);
758 MCB.addOperand(Op: MCOperand::createImm(Val: 0));
759 const MCInstrInfo &MCII = *Subtarget->getInstrInfo();
760
761 if (MI->isBundle()) {
762 const MachineBasicBlock* MBB = MI->getParent();
763 MachineBasicBlock::const_instr_iterator MII = MI->getIterator();
764
765 for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII)
766 if (!MII->isDebugInstr() && !MII->isImplicitDef())
767 HexagonLowerToMC(MCII, MI: &*MII, MCB, AP&: *this);
768 } else {
769 HexagonLowerToMC(MCII, MI, MCB, AP&: *this);
770 }
771
772 const MachineFunction &MF = *MI->getParent()->getParent();
773 const auto &HII = *MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
774 if (MI->isBundle() && HII.getBundleNoShuf(MIB: *MI))
775 HexagonMCInstrInfo::setMemReorderDisabled(MCB);
776
777 MCContext &Ctx = OutStreamer->getContext();
778 bool Ok = HexagonMCInstrInfo::canonicalizePacket(MCII, STI: *Subtarget, Context&: Ctx,
779 MCB, Checker: nullptr);
780 assert(Ok); (void)Ok;
781 if (HexagonMCInstrInfo::bundleSize(MCI: MCB) == 0)
782 return;
783 OutStreamer->emitInstruction(Inst: MCB, STI: getSubtargetInfo());
784}
785
786void HexagonAsmPrinter::emitStartOfAsmFile(Module &M) {
787 if (TM.getTargetTriple().isOSBinFormatELF())
788 emitAttributes();
789}
790
791void HexagonAsmPrinter::emitEndOfAsmFile(Module &M) {
792 HexagonTargetStreamer &HTS =
793 static_cast<HexagonTargetStreamer &>(*OutStreamer->getTargetStreamer());
794 if (TM.getTargetTriple().isOSBinFormatELF())
795 HTS.finishAttributeSection();
796}
797
798void HexagonAsmPrinter::emitAttributes() {
799 HexagonTargetStreamer &HTS =
800 static_cast<HexagonTargetStreamer &>(*OutStreamer->getTargetStreamer());
801 HTS.emitTargetAttributes(STI: TM.getMCSubtargetInfo());
802}
803
804void HexagonAsmPrinter::LowerPATCHABLE_EVENT_CALL(const MachineInstr &MI,
805 bool Typed) {
806 auto &O = *OutStreamer;
807 MCSymbol *CurSled = OutContext.createTempSymbol(Name: "xray_sled_", AlwaysAddSuffix: true);
808 O.emitLabel(Symbol: CurSled);
809
810 auto *Sym = MCSymbolRefExpr::create(
811 Symbol: OutContext.getOrCreateSymbol(Name: Typed ? "__xray_TypedEvent"
812 : "__xray_CustomEvent"),
813 Ctx&: OutContext);
814
815 // The sled structure:
816 // .Lxray_sled_N:
817 // { jump .Lend } -- disabled (patched to nop when enabled)
818 // <save args, move operands, call handler, restore args>
819 // .Lend:
820
821 MCSymbol *EndSled = OutContext.createTempSymbol();
822
823 // Packet 1: jump over the sled (disabled state).
824 MCInst *JumpInst = OutContext.createMCInst();
825 JumpInst->setOpcode(Hexagon::J2_jump);
826 JumpInst->addOperand(Op: MCOperand::createExpr(Val: HexagonMCExpr::create(
827 Expr: MCSymbolRefExpr::create(Symbol: EndSled, Ctx&: OutContext), Ctx&: OutContext)));
828
829 MCInst JumpPacket;
830 JumpPacket.setOpcode(Hexagon::BUNDLE);
831 JumpPacket.addOperand(Op: MCOperand::createImm(Val: 0));
832 JumpPacket.addOperand(Op: MCOperand::createInst(Val: JumpInst));
833 EmitToStreamer(S&: O, Inst: JumpPacket);
834
835 // Packet 2: allocframe to save LR:FP.
836 MCInst *AllocInst = OutContext.createMCInst();
837 AllocInst->setOpcode(Hexagon::S2_allocframe);
838 AllocInst->addOperand(Op: MCOperand::createReg(Reg: Hexagon::R29));
839 AllocInst->addOperand(Op: MCOperand::createReg(Reg: Hexagon::R30));
840 AllocInst->addOperand(Op: MCOperand::createExpr(Val: HexagonMCExpr::create(
841 Expr: MCConstantExpr::create(Value: 0, Ctx&: OutContext), Ctx&: OutContext)));
842
843 MCInst AllocPacket;
844 AllocPacket.setOpcode(Hexagon::BUNDLE);
845 AllocPacket.addOperand(Op: MCOperand::createImm(Val: 0));
846 AllocPacket.addOperand(Op: MCOperand::createInst(Val: AllocInst));
847 EmitToStreamer(S&: O, Inst: AllocPacket);
848
849 // Save argument registers and set up call arguments.
850 // Custom event: 2 operands (ptr, size) in MI operands 0,1 -> r0, r1
851 // Typed event: 3 operands (type, ptr, size) in MI operands 0,1,2 ->
852 // r0,r1,r2
853 unsigned NumArgs = Typed ? 3 : 2;
854
855 // Save the original argument registers onto the stack.
856 // Packet 3: Allocate space and save r0.
857 MCInst *SubSpInst = OutContext.createMCInst();
858 SubSpInst->setOpcode(Hexagon::A2_addi);
859 SubSpInst->addOperand(Op: MCOperand::createReg(Reg: Hexagon::R29));
860 SubSpInst->addOperand(Op: MCOperand::createReg(Reg: Hexagon::R29));
861 SubSpInst->addOperand(Op: MCOperand::createExpr(Val: HexagonMCExpr::create(
862 Expr: MCConstantExpr::create(Value: -(int64_t)(NumArgs * 4), Ctx&: OutContext),
863 Ctx&: OutContext)));
864
865 MCInst SubSpPacket;
866 SubSpPacket.setOpcode(Hexagon::BUNDLE);
867 SubSpPacket.addOperand(Op: MCOperand::createImm(Val: 0));
868 SubSpPacket.addOperand(Op: MCOperand::createInst(Val: SubSpInst));
869 EmitToStreamer(S&: O, Inst: SubSpPacket);
870
871 // Save each argument register.
872 for (unsigned I = 0; I < NumArgs; ++I) {
873 MCInst *StoreInst = OutContext.createMCInst();
874 StoreInst->setOpcode(Hexagon::S2_storeri_io);
875 StoreInst->addOperand(Op: MCOperand::createReg(Reg: Hexagon::R29));
876 StoreInst->addOperand(Op: MCOperand::createExpr(Val: HexagonMCExpr::create(
877 Expr: MCConstantExpr::create(Value: I * 4, Ctx&: OutContext), Ctx&: OutContext)));
878 StoreInst->addOperand(Op: MCOperand::createReg(Reg: Hexagon::R0 + I));
879
880 MCInst StorePacket;
881 StorePacket.setOpcode(Hexagon::BUNDLE);
882 StorePacket.addOperand(Op: MCOperand::createImm(Val: 0));
883 StorePacket.addOperand(Op: MCOperand::createInst(Val: StoreInst));
884 EmitToStreamer(S&: O, Inst: StorePacket);
885 }
886
887 // Move operands into argument registers (r0, r1, [r2]).
888 // The XRay intrinsic uses i64 for size (and type) parameters. On 32-bit
889 // Hexagon these are in DoubleRegs (register pairs). The runtime handler
890 // expects 32-bit arguments, so extract the low sub-register.
891 //
892 // NOTE: Moves are always emitted (even identity moves like r0 = r0) so that
893 // the sled has a fixed size. The runtime patching code relies on the sled
894 // being a known number of words to encode the correct jump offset for the
895 // disabled state.
896 //
897 // NOTE: When source registers alias destination registers in a conflicting
898 // order (e.g., src0 in r1 and src1 in r0), the sequential moves can produce
899 // incorrect results. This is the same limitation as AArch64's implementation
900 // and is unlikely in practice since the register allocator rarely produces
901 // such assignments for XRay event intrinsics.
902 const auto &HRI = *MF->getSubtarget<HexagonSubtarget>().getRegisterInfo();
903 for (unsigned I = 0; I < NumArgs; ++I) {
904 Register SrcReg = MI.getOperand(i: I).getReg();
905 if (Hexagon::DoubleRegsRegClass.contains(Reg: SrcReg))
906 SrcReg = HRI.getSubReg(Reg: SrcReg, Idx: Hexagon::isub_lo);
907
908 MCInst *MovInst = OutContext.createMCInst();
909 MovInst->setOpcode(Hexagon::A2_tfr);
910 MovInst->addOperand(Op: MCOperand::createReg(Reg: Hexagon::R0 + I));
911 MovInst->addOperand(Op: MCOperand::createReg(Reg: SrcReg));
912
913 MCInst MovPacket;
914 MovPacket.setOpcode(Hexagon::BUNDLE);
915 MovPacket.addOperand(Op: MCOperand::createImm(Val: 0));
916 MovPacket.addOperand(Op: MCOperand::createInst(Val: MovInst));
917 EmitToStreamer(S&: O, Inst: MovPacket);
918 }
919
920 // Call the handler.
921 MCInst *CallInst = OutContext.createMCInst();
922 CallInst->setOpcode(Hexagon::J2_call);
923 CallInst->addOperand(
924 Op: MCOperand::createExpr(Val: HexagonMCExpr::create(Expr: Sym, Ctx&: OutContext)));
925
926 MCInst CallPacket;
927 CallPacket.setOpcode(Hexagon::BUNDLE);
928 CallPacket.addOperand(Op: MCOperand::createImm(Val: 0));
929 CallPacket.addOperand(Op: MCOperand::createInst(Val: CallInst));
930 EmitToStreamer(S&: O, Inst: CallPacket);
931
932 // Restore argument registers.
933 for (unsigned I = 0; I < NumArgs; ++I) {
934 MCInst *LoadInst = OutContext.createMCInst();
935 LoadInst->setOpcode(Hexagon::L2_loadri_io);
936 LoadInst->addOperand(Op: MCOperand::createReg(Reg: Hexagon::R0 + I));
937 LoadInst->addOperand(Op: MCOperand::createReg(Reg: Hexagon::R29));
938 LoadInst->addOperand(Op: MCOperand::createExpr(Val: HexagonMCExpr::create(
939 Expr: MCConstantExpr::create(Value: I * 4, Ctx&: OutContext), Ctx&: OutContext)));
940
941 MCInst LoadPacket;
942 LoadPacket.setOpcode(Hexagon::BUNDLE);
943 LoadPacket.addOperand(Op: MCOperand::createImm(Val: 0));
944 LoadPacket.addOperand(Op: MCOperand::createInst(Val: LoadInst));
945 EmitToStreamer(S&: O, Inst: LoadPacket);
946 }
947
948 // Deallocate saved argument space.
949 MCInst *AddSpInst = OutContext.createMCInst();
950 AddSpInst->setOpcode(Hexagon::A2_addi);
951 AddSpInst->addOperand(Op: MCOperand::createReg(Reg: Hexagon::R29));
952 AddSpInst->addOperand(Op: MCOperand::createReg(Reg: Hexagon::R29));
953 AddSpInst->addOperand(Op: MCOperand::createExpr(Val: HexagonMCExpr::create(
954 Expr: MCConstantExpr::create(Value: NumArgs * 4, Ctx&: OutContext), Ctx&: OutContext)));
955
956 MCInst AddSpPacket;
957 AddSpPacket.setOpcode(Hexagon::BUNDLE);
958 AddSpPacket.addOperand(Op: MCOperand::createImm(Val: 0));
959 AddSpPacket.addOperand(Op: MCOperand::createInst(Val: AddSpInst));
960 EmitToStreamer(S&: O, Inst: AddSpPacket);
961
962 // Deallocframe to restore LR:FP.
963 MCInst *DeallocInst = OutContext.createMCInst();
964 DeallocInst->setOpcode(Hexagon::L2_deallocframe);
965 DeallocInst->addOperand(Op: MCOperand::createReg(Reg: Hexagon::D15));
966 DeallocInst->addOperand(Op: MCOperand::createReg(Reg: Hexagon::R30));
967
968 MCInst DeallocPacket;
969 DeallocPacket.setOpcode(Hexagon::BUNDLE);
970 DeallocPacket.addOperand(Op: MCOperand::createImm(Val: 0));
971 DeallocPacket.addOperand(Op: MCOperand::createInst(Val: DeallocInst));
972 EmitToStreamer(S&: O, Inst: DeallocPacket);
973
974 OutStreamer->emitLabel(Symbol: EndSled);
975 recordSled(Sled: CurSled, MI,
976 Kind: Typed ? SledKind::TYPED_EVENT : SledKind::CUSTOM_EVENT, Version: 2);
977}
978
979void HexagonAsmPrinter::LowerKCFI_CHECK(const MachineInstr &MI) {
980 Register AddrReg = MI.getOperand(i: 0).getReg();
981 const int64_t Type = MI.getOperand(i: 1).getImm();
982 [[maybe_unused]] MachineBasicBlock::const_instr_iterator NextI =
983 std::next(x: MI.getIterator());
984 assert(NextI != MI.getParent()->instr_end() && NextI->isCall() &&
985 "KCFI_CHECK not followed by a call instruction");
986 assert(NextI->getOperand(0).getReg() == AddrReg &&
987 "KCFI_CHECK call target doesn't match call operand");
988
989 // Scratch registers for the compare. Default to R6/R7 (caller-saved,
990 // in GeneralSubRegs for potential compounding). If AddrReg conflicts,
991 // fall back through other caller-saved registers.
992 unsigned ScratchRegs[] = {Hexagon::R6, Hexagon::R7};
993 unsigned NextReg = Hexagon::R8;
994 for (auto &Reg : ScratchRegs) {
995 if (Reg != AddrReg)
996 continue;
997 if (NextReg == AddrReg)
998 ++NextReg;
999 Reg = NextReg++;
1000 }
1001 unsigned LoadReg = ScratchRegs[0];
1002 unsigned TypeReg = ScratchRegs[1];
1003 unsigned PredReg = Hexagon::P0;
1004
1005 // Adjust for patchable-function-prefix (nop padding before the function).
1006 int64_t PrefixNops = MI.getMF()->getFunction().getFnAttributeAsParsedInteger(
1007 Kind: "patchable-function-prefix");
1008 int64_t Offset = -(PrefixNops * 4 + 4);
1009
1010 // Emit the KCFI check sequence.
1011 //
1012 // Packet 1: Load the type hash and materialize the expected hash together.
1013 // The load offset fits in the native instruction field for any
1014 // patchable-function-prefix count, so it never requires a constant
1015 // extender. This lets the extender for ##hash share the same packet,
1016 // saving one packet compared to emitting them separately.
1017 // { r_load = memw(r_addr + #offset); r_type = ##expected_hash }
1018 MCInst *LoadInst = OutContext.createMCInst();
1019 LoadInst->setOpcode(Hexagon::L2_loadri_io);
1020 LoadInst->addOperand(Op: MCOperand::createReg(Reg: LoadReg));
1021 LoadInst->addOperand(Op: MCOperand::createReg(Reg: AddrReg));
1022 LoadInst->addOperand(Op: MCOperand::createExpr(Val: HexagonMCExpr::create(
1023 Expr: MCConstantExpr::create(Value: Offset, Ctx&: OutContext), Ctx&: OutContext)));
1024
1025 MCInst *TypeInst = OutContext.createMCInst();
1026 TypeInst->setOpcode(Hexagon::A2_tfrsi);
1027 TypeInst->addOperand(Op: MCOperand::createReg(Reg: TypeReg));
1028 auto *TypeExpr = HexagonMCExpr::create(
1029 Expr: MCConstantExpr::create(Value: Type, Ctx&: OutContext), Ctx&: OutContext);
1030 HexagonMCInstrInfo::setMustExtend(Expr: *TypeExpr, Val: true);
1031 TypeInst->addOperand(Op: MCOperand::createExpr(Val: TypeExpr));
1032
1033 MCInst LoadTypePacket;
1034 LoadTypePacket.setOpcode(Hexagon::BUNDLE);
1035 LoadTypePacket.addOperand(Op: MCOperand::createImm(Val: 0));
1036 LoadTypePacket.addOperand(Op: MCOperand::createInst(Val: LoadInst));
1037 LoadTypePacket.addOperand(Op: MCOperand::createInst(Val: TypeInst));
1038 EmitToStreamer(S&: *OutStreamer, Inst: LoadTypePacket);
1039
1040 // Packet 3: Compare and branch if equal.
1041 // { p0 = cmp.eq(r_load, r_type); if (p0.new) jump:t .Lpass }
1042 MCSymbol *Pass = OutContext.createTempSymbol();
1043
1044 MCInst *CmpInst = OutContext.createMCInst();
1045 CmpInst->setOpcode(Hexagon::C2_cmpeq);
1046 CmpInst->addOperand(Op: MCOperand::createReg(Reg: PredReg));
1047 CmpInst->addOperand(Op: MCOperand::createReg(Reg: LoadReg));
1048 CmpInst->addOperand(Op: MCOperand::createReg(Reg: TypeReg));
1049
1050 MCInst *JumpInst = OutContext.createMCInst();
1051 JumpInst->setOpcode(Hexagon::J2_jumptnewpt);
1052 JumpInst->addOperand(Op: MCOperand::createReg(Reg: PredReg));
1053 JumpInst->addOperand(Op: MCOperand::createExpr(Val: HexagonMCExpr::create(
1054 Expr: MCSymbolRefExpr::create(Symbol: Pass, Ctx&: OutContext), Ctx&: OutContext)));
1055
1056 MCInst CmpJmpPacket;
1057 CmpJmpPacket.setOpcode(Hexagon::BUNDLE);
1058 CmpJmpPacket.addOperand(Op: MCOperand::createImm(Val: 0));
1059 CmpJmpPacket.addOperand(Op: MCOperand::createInst(Val: CmpInst));
1060 CmpJmpPacket.addOperand(Op: MCOperand::createInst(Val: JumpInst));
1061 EmitToStreamer(S&: *OutStreamer, Inst: CmpJmpPacket);
1062
1063 // Packet 4: Crash on mismatch via misaligned load.
1064 // Use the same mechanism as llvm.trap (PS_crash): a doubleword load from
1065 // a misaligned address is guaranteed to fault in all execution modes,
1066 // including kernel/monitor mode where trap0 may not generate a useful
1067 // exception.
1068 MCSymbol *TrapLabel = OutContext.createTempSymbol();
1069 OutStreamer->emitLabel(Symbol: TrapLabel);
1070
1071 MCInst *CrashInst = OutContext.createMCInst();
1072 CrashInst->setOpcode(Hexagon::PS_loadrdabs);
1073 CrashInst->addOperand(Op: MCOperand::createReg(Reg: Hexagon::D13));
1074 auto *CrashExpr = HexagonMCExpr::create(
1075 Expr: MCConstantExpr::create(Value: 0xBADC0FEE, Ctx&: OutContext), Ctx&: OutContext);
1076 HexagonMCInstrInfo::setMustExtend(Expr: *CrashExpr, Val: true);
1077 CrashInst->addOperand(Op: MCOperand::createExpr(Val: CrashExpr));
1078
1079 MCInst CrashPacket;
1080 CrashPacket.setOpcode(Hexagon::BUNDLE);
1081 CrashPacket.addOperand(Op: MCOperand::createImm(Val: 0));
1082 CrashPacket.addOperand(Op: MCOperand::createInst(Val: CrashInst));
1083 EmitToStreamer(S&: *OutStreamer, Inst: CrashPacket);
1084
1085 emitKCFITrapEntry(MF: *MI.getMF(), Symbol: TrapLabel);
1086 OutStreamer->emitLabel(Symbol: Pass);
1087}
1088
1089void HexagonAsmPrinter::EmitSled(const MachineInstr &MI, SledKind Kind) {
1090 static const int8_t NoopsInSledCount = 6;
1091 // We want to emit the following pattern:
1092 //
1093 // .L_xray_sled_N:
1094 // <xray_sled_base>:
1095 // { jump .Ltmp0 }
1096 // { nop }
1097 // { nop }
1098 // { nop }
1099 // { nop }
1100 // { nop }
1101 // { nop }
1102 // .Ltmp0:
1103 //
1104 // We need the 6 nop words because at runtime, we'd be patching over the
1105 // full 7 words with the following pattern:
1106 //
1107 // <xray_sled_n>:
1108 // { allocframe(#0) }
1109 // { immext(#...) // upper 26-bits of func id
1110 // r7 = ##... // lower 6-bits of func id
1111 // immext(#...) // upper 26-bits of trampoline
1112 // r6 = ##... } // lower 6-bits of trampoline
1113 // { callr r6 }
1114 // { deallocframe }
1115 //
1116 // allocframe saves r31:30 (LR:FP) before the call, and deallocframe
1117 // restores them after the trampoline returns, ensuring the caller's
1118 // return address in r31 is preserved across the sled.
1119 //
1120 auto CurSled = OutContext.createTempSymbol(Name: "xray_sled_", AlwaysAddSuffix: true);
1121 OutStreamer->emitLabel(Symbol: CurSled);
1122
1123 MCInst *SledJump = new (OutContext) MCInst();
1124 SledJump->setOpcode(Hexagon::J2_jump);
1125 auto PostSled = OutContext.createTempSymbol();
1126 SledJump->addOperand(Op: MCOperand::createExpr(Val: HexagonMCExpr::create(
1127 Expr: MCSymbolRefExpr::create(Symbol: PostSled, Ctx&: OutContext), Ctx&: OutContext)));
1128
1129 // Emit "jump PostSled" instruction, which jumps over the nop series.
1130 MCInst SledJumpPacket;
1131 SledJumpPacket.setOpcode(Hexagon::BUNDLE);
1132 SledJumpPacket.addOperand(Op: MCOperand::createImm(Val: 0));
1133 SledJumpPacket.addOperand(Op: MCOperand::createInst(Val: SledJump));
1134
1135 EmitToStreamer(S&: *OutStreamer, Inst: SledJumpPacket);
1136
1137 // FIXME: this will emit individual packets, we should
1138 // special-case this and combine them into a single packet.
1139 emitNops(N: NoopsInSledCount);
1140
1141 OutStreamer->emitLabel(Symbol: PostSled);
1142 recordSled(Sled: CurSled, MI, Kind, Version: 2);
1143}
1144
1145void HexagonAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI) {
1146 EmitSled(MI, Kind: SledKind::FUNCTION_ENTER);
1147}
1148
1149void HexagonAsmPrinter::LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr &MI) {
1150 EmitSled(MI, Kind: SledKind::FUNCTION_EXIT);
1151}
1152
1153void HexagonAsmPrinter::LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI) {
1154 EmitSled(MI, Kind: SledKind::TAIL_CALL);
1155}
1156
1157char HexagonAsmPrinter::ID = 0;
1158
1159INITIALIZE_PASS(HexagonAsmPrinter, "hexagon-asm-printer",
1160 "Hexagon Assembly Printer", false, false)
1161
1162extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
1163LLVMInitializeHexagonAsmPrinter() {
1164 RegisterAsmPrinter<HexagonAsmPrinter> X(getTheHexagonTarget());
1165}
1166