LLVM 22.0.0git
RISCVAsmPrinter.cpp
Go to the documentation of this file.
1//===-- RISCVAsmPrinter.cpp - RISC-V LLVM assembly writer -----------------===//
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 the RISC-V assembly language.
11//
12//===----------------------------------------------------------------------===//
13
19#include "RISCV.h"
22#include "RISCVRegisterInfo.h"
24#include "llvm/ADT/APInt.h"
25#include "llvm/ADT/Statistic.h"
31#include "llvm/IR/Module.h"
32#include "llvm/MC/MCAsmInfo.h"
33#include "llvm/MC/MCContext.h"
34#include "llvm/MC/MCInst.h"
38#include "llvm/MC/MCStreamer.h"
39#include "llvm/MC/MCSymbol.h"
45
46using namespace llvm;
47
48#define DEBUG_TYPE "asm-printer"
49
50STATISTIC(RISCVNumInstrsCompressed,
51 "Number of RISC-V Compressed instructions emitted");
52
53namespace llvm {
54extern const SubtargetFeatureKV RISCVFeatureKV[RISCV::NumSubtargetFeatures];
55} // namespace llvm
56
57namespace {
58class RISCVAsmPrinter : public AsmPrinter {
59public:
60 static char ID;
61
62private:
63 const RISCVSubtarget *STI;
64
65public:
66 explicit RISCVAsmPrinter(TargetMachine &TM,
67 std::unique_ptr<MCStreamer> Streamer)
68 : AsmPrinter(TM, std::move(Streamer), ID) {}
69
70 StringRef getPassName() const override { return "RISC-V Assembly Printer"; }
71
72 void LowerSTACKMAP(MCStreamer &OutStreamer, StackMaps &SM,
73 const MachineInstr &MI);
74
75 void LowerPATCHPOINT(MCStreamer &OutStreamer, StackMaps &SM,
76 const MachineInstr &MI);
77
78 void LowerSTATEPOINT(MCStreamer &OutStreamer, StackMaps &SM,
79 const MachineInstr &MI);
80
81 bool runOnMachineFunction(MachineFunction &MF) override;
82
83 void emitInstruction(const MachineInstr *MI) override;
84
85 void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) override;
86
87 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
88 const char *ExtraCode, raw_ostream &OS) override;
89 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
90 const char *ExtraCode, raw_ostream &OS) override;
91
92 // Returns whether Inst is compressed.
93 bool EmitToStreamer(MCStreamer &S, const MCInst &Inst,
94 const MCSubtargetInfo &SubtargetInfo);
95 bool EmitToStreamer(MCStreamer &S, const MCInst &Inst) {
96 return EmitToStreamer(S, Inst, *STI);
97 }
98
99 bool lowerPseudoInstExpansion(const MachineInstr *MI, MCInst &Inst);
100
101 typedef std::tuple<unsigned, uint32_t> HwasanMemaccessTuple;
102 std::map<HwasanMemaccessTuple, MCSymbol *> HwasanMemaccessSymbols;
103 void LowerHWASAN_CHECK_MEMACCESS(const MachineInstr &MI);
104 void LowerKCFI_CHECK(const MachineInstr &MI);
105 void EmitHwasanMemaccessSymbols(Module &M);
106
107 // Wrapper needed for tblgenned pseudo lowering.
108 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
109
110 void emitStartOfAsmFile(Module &M) override;
111 void emitEndOfAsmFile(Module &M) override;
112
113 void emitFunctionEntryLabel() override;
114 bool emitDirectiveOptionArch();
115
116 void emitNoteGnuProperty(const Module &M);
117
118private:
119 void emitAttributes(const MCSubtargetInfo &SubtargetInfo);
120
121 void emitNTLHint(const MachineInstr *MI);
122
123 // XRay Support
124 void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr *MI);
125 void LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr *MI);
126 void LowerPATCHABLE_TAIL_CALL(const MachineInstr *MI);
127 void emitSled(const MachineInstr *MI, SledKind Kind);
128
129 void lowerToMCInst(const MachineInstr *MI, MCInst &OutMI);
130};
131}
132
133void RISCVAsmPrinter::LowerSTACKMAP(MCStreamer &OutStreamer, StackMaps &SM,
134 const MachineInstr &MI) {
135 unsigned NOPBytes = STI->hasStdExtZca() ? 2 : 4;
136 unsigned NumNOPBytes = StackMapOpers(&MI).getNumPatchBytes();
137
138 auto &Ctx = OutStreamer.getContext();
139 MCSymbol *MILabel = Ctx.createTempSymbol();
140 OutStreamer.emitLabel(MILabel);
141
142 SM.recordStackMap(*MILabel, MI);
143 assert(NumNOPBytes % NOPBytes == 0 &&
144 "Invalid number of NOP bytes requested!");
145
146 // Scan ahead to trim the shadow.
147 const MachineBasicBlock &MBB = *MI.getParent();
149 ++MII;
150 while (NumNOPBytes > 0) {
151 if (MII == MBB.end() || MII->isCall() ||
152 MII->getOpcode() == RISCV::DBG_VALUE ||
153 MII->getOpcode() == TargetOpcode::PATCHPOINT ||
154 MII->getOpcode() == TargetOpcode::STACKMAP)
155 break;
156 ++MII;
157 NumNOPBytes -= 4;
158 }
159
160 // Emit nops.
161 emitNops(NumNOPBytes / NOPBytes);
162}
163
164// Lower a patchpoint of the form:
165// [<def>], <id>, <numBytes>, <target>, <numArgs>
166void RISCVAsmPrinter::LowerPATCHPOINT(MCStreamer &OutStreamer, StackMaps &SM,
167 const MachineInstr &MI) {
168 unsigned NOPBytes = STI->hasStdExtZca() ? 2 : 4;
169
170 auto &Ctx = OutStreamer.getContext();
171 MCSymbol *MILabel = Ctx.createTempSymbol();
172 OutStreamer.emitLabel(MILabel);
173 SM.recordPatchPoint(*MILabel, MI);
174
175 PatchPointOpers Opers(&MI);
176
177 const MachineOperand &CalleeMO = Opers.getCallTarget();
178 unsigned EncodedBytes = 0;
179
180 if (CalleeMO.isImm()) {
181 uint64_t CallTarget = CalleeMO.getImm();
182 if (CallTarget) {
183 assert((CallTarget & 0xFFFF'FFFF'FFFF) == CallTarget &&
184 "High 16 bits of call target should be zero.");
185 // Materialize the jump address:
187 RISCVMatInt::generateMCInstSeq(CallTarget, *STI, RISCV::X1, Seq);
188 for (MCInst &Inst : Seq) {
189 bool Compressed = EmitToStreamer(OutStreamer, Inst);
190 EncodedBytes += Compressed ? 2 : 4;
191 }
192 bool Compressed = EmitToStreamer(OutStreamer, MCInstBuilder(RISCV::JALR)
193 .addReg(RISCV::X1)
194 .addReg(RISCV::X1)
195 .addImm(0));
196 EncodedBytes += Compressed ? 2 : 4;
197 }
198 } else if (CalleeMO.isGlobal()) {
199 MCOperand CallTargetMCOp;
200 lowerOperand(CalleeMO, CallTargetMCOp);
201 EmitToStreamer(OutStreamer,
202 MCInstBuilder(RISCV::PseudoCALL).addOperand(CallTargetMCOp));
203 EncodedBytes += 8;
204 }
205
206 // Emit padding.
207 unsigned NumBytes = Opers.getNumPatchBytes();
208 assert(NumBytes >= EncodedBytes &&
209 "Patchpoint can't request size less than the length of a call.");
210 assert((NumBytes - EncodedBytes) % NOPBytes == 0 &&
211 "Invalid number of NOP bytes requested!");
212 emitNops((NumBytes - EncodedBytes) / NOPBytes);
213}
214
215void RISCVAsmPrinter::LowerSTATEPOINT(MCStreamer &OutStreamer, StackMaps &SM,
216 const MachineInstr &MI) {
217 unsigned NOPBytes = STI->hasStdExtZca() ? 2 : 4;
218
219 StatepointOpers SOpers(&MI);
220 if (unsigned PatchBytes = SOpers.getNumPatchBytes()) {
221 assert(PatchBytes % NOPBytes == 0 &&
222 "Invalid number of NOP bytes requested!");
223 emitNops(PatchBytes / NOPBytes);
224 } else {
225 // Lower call target and choose correct opcode
226 const MachineOperand &CallTarget = SOpers.getCallTarget();
227 MCOperand CallTargetMCOp;
228 switch (CallTarget.getType()) {
231 lowerOperand(CallTarget, CallTargetMCOp);
232 EmitToStreamer(
233 OutStreamer,
234 MCInstBuilder(RISCV::PseudoCALL).addOperand(CallTargetMCOp));
235 break;
237 CallTargetMCOp = MCOperand::createImm(CallTarget.getImm());
238 EmitToStreamer(OutStreamer, MCInstBuilder(RISCV::JAL)
239 .addReg(RISCV::X1)
240 .addOperand(CallTargetMCOp));
241 break;
243 CallTargetMCOp = MCOperand::createReg(CallTarget.getReg());
244 EmitToStreamer(OutStreamer, MCInstBuilder(RISCV::JALR)
245 .addReg(RISCV::X1)
246 .addOperand(CallTargetMCOp)
247 .addImm(0));
248 break;
249 default:
250 llvm_unreachable("Unsupported operand type in statepoint call target");
251 break;
252 }
253 }
254
255 auto &Ctx = OutStreamer.getContext();
256 MCSymbol *MILabel = Ctx.createTempSymbol();
257 OutStreamer.emitLabel(MILabel);
258 SM.recordStatepoint(*MILabel, MI);
259}
260
261bool RISCVAsmPrinter::EmitToStreamer(MCStreamer &S, const MCInst &Inst,
262 const MCSubtargetInfo &SubtargetInfo) {
263 MCInst CInst;
264 bool Res = RISCVRVC::compress(CInst, Inst, SubtargetInfo);
265 if (Res)
266 ++RISCVNumInstrsCompressed;
267 S.emitInstruction(Res ? CInst : Inst, SubtargetInfo);
268 return Res;
269}
270
271// Simple pseudo-instructions have their lowering (with expansion to real
272// instructions) auto-generated.
273#include "RISCVGenMCPseudoLowering.inc"
274
275// If the target supports Zihintntl and the instruction has a nontemporal
276// MachineMemOperand, emit an NTLH hint instruction before it.
277void RISCVAsmPrinter::emitNTLHint(const MachineInstr *MI) {
278 if (!STI->hasStdExtZihintntl())
279 return;
280
281 if (MI->memoperands_empty())
282 return;
283
284 MachineMemOperand *MMO = *(MI->memoperands_begin());
285 if (!MMO->isNonTemporal())
286 return;
287
288 unsigned NontemporalMode = 0;
289 if (MMO->getFlags() & MONontemporalBit0)
290 NontemporalMode += 0b1;
291 if (MMO->getFlags() & MONontemporalBit1)
292 NontemporalMode += 0b10;
293
294 MCInst Hint;
295 if (STI->hasStdExtZca())
296 Hint.setOpcode(RISCV::C_ADD);
297 else
298 Hint.setOpcode(RISCV::ADD);
299
300 Hint.addOperand(MCOperand::createReg(RISCV::X0));
301 Hint.addOperand(MCOperand::createReg(RISCV::X0));
302 Hint.addOperand(MCOperand::createReg(RISCV::X2 + NontemporalMode));
303
304 EmitToStreamer(*OutStreamer, Hint);
305}
306
307void RISCVAsmPrinter::emitInstruction(const MachineInstr *MI) {
308 RISCV_MC::verifyInstructionPredicates(MI->getOpcode(), STI->getFeatureBits());
309
310 emitNTLHint(MI);
311
312 // Do any auto-generated pseudo lowerings.
313 if (MCInst OutInst; lowerPseudoInstExpansion(MI, OutInst)) {
314 EmitToStreamer(*OutStreamer, OutInst);
315 return;
316 }
317
318 switch (MI->getOpcode()) {
319 case RISCV::HWASAN_CHECK_MEMACCESS_SHORTGRANULES:
320 LowerHWASAN_CHECK_MEMACCESS(*MI);
321 return;
322 case RISCV::KCFI_CHECK:
323 LowerKCFI_CHECK(*MI);
324 return;
325 case TargetOpcode::STACKMAP:
326 return LowerSTACKMAP(*OutStreamer, SM, *MI);
327 case TargetOpcode::PATCHPOINT:
328 return LowerPATCHPOINT(*OutStreamer, SM, *MI);
329 case TargetOpcode::STATEPOINT:
330 return LowerSTATEPOINT(*OutStreamer, SM, *MI);
331 case TargetOpcode::PATCHABLE_FUNCTION_ENTER: {
332 const Function &F = MI->getParent()->getParent()->getFunction();
333 if (F.hasFnAttribute("patchable-function-entry")) {
334 unsigned Num;
335 [[maybe_unused]] bool Result =
336 F.getFnAttribute("patchable-function-entry")
337 .getValueAsString()
338 .getAsInteger(10, Num);
339 assert(!Result && "Enforced by the verifier");
340 emitNops(Num);
341 return;
342 }
343 LowerPATCHABLE_FUNCTION_ENTER(MI);
344 return;
345 }
346 case TargetOpcode::PATCHABLE_FUNCTION_EXIT:
347 LowerPATCHABLE_FUNCTION_EXIT(MI);
348 return;
349 case TargetOpcode::PATCHABLE_TAIL_CALL:
350 LowerPATCHABLE_TAIL_CALL(MI);
351 return;
352 }
353
354 MCInst OutInst;
355 lowerToMCInst(MI, OutInst);
356 EmitToStreamer(*OutStreamer, OutInst);
357}
358
359bool RISCVAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
360 const char *ExtraCode, raw_ostream &OS) {
361 // First try the generic code, which knows about modifiers like 'c' and 'n'.
362 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS))
363 return false;
364
365 const MachineOperand &MO = MI->getOperand(OpNo);
366 if (ExtraCode && ExtraCode[0]) {
367 if (ExtraCode[1] != 0)
368 return true; // Unknown modifier.
369
370 switch (ExtraCode[0]) {
371 default:
372 return true; // Unknown modifier.
373 case 'z': // Print zero register if zero, regular printing otherwise.
374 if (MO.isImm() && MO.getImm() == 0) {
375 OS << RISCVInstPrinter::getRegisterName(RISCV::X0);
376 return false;
377 }
378 break;
379 case 'i': // Literal 'i' if operand is not a register.
380 if (!MO.isReg())
381 OS << 'i';
382 return false;
383 case 'N': // Print the register encoding as an integer (0-31)
384 if (!MO.isReg())
385 return true;
386
387 const RISCVRegisterInfo *TRI = STI->getRegisterInfo();
388 OS << TRI->getEncodingValue(MO.getReg());
389 return false;
390 }
391 }
392
393 switch (MO.getType()) {
395 OS << MO.getImm();
396 return false;
399 return false;
401 PrintSymbolOperand(MO, OS);
402 return false;
404 MCSymbol *Sym = GetBlockAddressSymbol(MO.getBlockAddress());
405 Sym->print(OS, MAI);
406 return false;
407 }
408 default:
409 break;
410 }
411
412 return true;
413}
414
415bool RISCVAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
416 unsigned OpNo,
417 const char *ExtraCode,
418 raw_ostream &OS) {
419 if (ExtraCode)
420 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, ExtraCode, OS);
421
422 const MachineOperand &AddrReg = MI->getOperand(OpNo);
423 assert(MI->getNumOperands() > OpNo + 1 && "Expected additional operand");
424 const MachineOperand &Offset = MI->getOperand(OpNo + 1);
425 // All memory operands should have a register and an immediate operand (see
426 // RISCVDAGToDAGISel::SelectInlineAsmMemoryOperand).
427 if (!AddrReg.isReg())
428 return true;
429 if (!Offset.isImm() && !Offset.isGlobal() && !Offset.isBlockAddress() &&
430 !Offset.isMCSymbol())
431 return true;
432
433 MCOperand MCO;
434 if (!lowerOperand(Offset, MCO))
435 return true;
436
437 if (Offset.isImm())
438 OS << MCO.getImm();
439 else if (Offset.isGlobal() || Offset.isBlockAddress() || Offset.isMCSymbol())
440 MAI->printExpr(OS, *MCO.getExpr());
441
442 if (Offset.isMCSymbol())
443 MMI->getContext().registerInlineAsmLabel(Offset.getMCSymbol());
444 if (Offset.isBlockAddress()) {
445 const BlockAddress *BA = Offset.getBlockAddress();
446 MCSymbol *Sym = GetBlockAddressSymbol(BA);
447 MMI->getContext().registerInlineAsmLabel(Sym);
448 }
449
450 OS << "(" << RISCVInstPrinter::getRegisterName(AddrReg.getReg()) << ")";
451 return false;
452}
453
454bool RISCVAsmPrinter::emitDirectiveOptionArch() {
455 RISCVTargetStreamer &RTS =
456 static_cast<RISCVTargetStreamer &>(*OutStreamer->getTargetStreamer());
457 SmallVector<RISCVOptionArchArg> NeedEmitStdOptionArgs;
458 const MCSubtargetInfo &MCSTI = *TM.getMCSubtargetInfo();
459 for (const auto &Feature : RISCVFeatureKV) {
460 if (STI->hasFeature(Feature.Value) == MCSTI.hasFeature(Feature.Value))
461 continue;
462
464 continue;
465
466 auto Delta = STI->hasFeature(Feature.Value) ? RISCVOptionArchArgType::Plus
467 : RISCVOptionArchArgType::Minus;
468 NeedEmitStdOptionArgs.emplace_back(Delta, Feature.Key);
469 }
470 if (!NeedEmitStdOptionArgs.empty()) {
472 RTS.emitDirectiveOptionArch(NeedEmitStdOptionArgs);
473 return true;
474 }
475
476 return false;
477}
478
479bool RISCVAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
480 STI = &MF.getSubtarget<RISCVSubtarget>();
481 RISCVTargetStreamer &RTS =
482 static_cast<RISCVTargetStreamer &>(*OutStreamer->getTargetStreamer());
483
484 bool EmittedOptionArch = emitDirectiveOptionArch();
485
486 SetupMachineFunction(MF);
487 emitFunctionBody();
488
489 // Emit the XRay table
490 emitXRayTable();
491
492 if (EmittedOptionArch)
493 RTS.emitDirectiveOptionPop();
494 return false;
495}
496
497void RISCVAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr *MI) {
498 emitSled(MI, SledKind::FUNCTION_ENTER);
499}
500
501void RISCVAsmPrinter::LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr *MI) {
502 emitSled(MI, SledKind::FUNCTION_EXIT);
503}
504
505void RISCVAsmPrinter::LowerPATCHABLE_TAIL_CALL(const MachineInstr *MI) {
506 emitSled(MI, SledKind::TAIL_CALL);
507}
508
509void RISCVAsmPrinter::emitSled(const MachineInstr *MI, SledKind Kind) {
510 // We want to emit the jump instruction and the nops constituting the sled.
511 // The format is as follows:
512 // .Lxray_sled_N
513 // ALIGN
514 // J .tmpN
515 // 21 or 33 C.NOP instructions
516 // .tmpN
517
518 // The following variable holds the count of the number of NOPs to be patched
519 // in for XRay instrumentation during compilation.
520 // Note that RV64 and RV32 each has a sled of 68 and 44 bytes, respectively.
521 // Assuming we're using JAL to jump to .tmpN, then we only need
522 // (68 - 4)/2 = 32 NOPs for RV64 and (44 - 4)/2 = 20 for RV32. However, there
523 // is a chance that we'll use C.JAL instead, so an additional NOP is needed.
524 const uint8_t NoopsInSledCount = STI->is64Bit() ? 33 : 21;
525
526 OutStreamer->emitCodeAlignment(Align(4), STI);
527 auto CurSled = OutContext.createTempSymbol("xray_sled_", true);
528 OutStreamer->emitLabel(CurSled);
529 auto Target = OutContext.createTempSymbol();
530
531 const MCExpr *TargetExpr = MCSymbolRefExpr::create(Target, OutContext);
532
533 // Emit "J bytes" instruction, which jumps over the nop sled to the actual
534 // start of function.
535 EmitToStreamer(
536 *OutStreamer,
537 MCInstBuilder(RISCV::JAL).addReg(RISCV::X0).addExpr(TargetExpr));
538
539 // Emit NOP instructions
540 for (int8_t I = 0; I < NoopsInSledCount; ++I)
541 EmitToStreamer(*OutStreamer, MCInstBuilder(RISCV::ADDI)
542 .addReg(RISCV::X0)
543 .addReg(RISCV::X0)
544 .addImm(0));
545
546 OutStreamer->emitLabel(Target);
547 recordSled(CurSled, *MI, Kind, 2);
548}
549
550void RISCVAsmPrinter::emitStartOfAsmFile(Module &M) {
551 RISCVTargetStreamer &RTS =
552 static_cast<RISCVTargetStreamer &>(*OutStreamer->getTargetStreamer());
553 if (const MDString *ModuleTargetABI =
554 dyn_cast_or_null<MDString>(M.getModuleFlag("target-abi")))
555 RTS.setTargetABI(RISCVABI::getTargetABI(ModuleTargetABI->getString()));
556
557 MCSubtargetInfo SubtargetInfo = *TM.getMCSubtargetInfo();
558
559 // Use module flag to update feature bits.
560 if (auto *MD = dyn_cast_or_null<MDNode>(M.getModuleFlag("riscv-isa"))) {
561 for (auto &ISA : MD->operands()) {
562 if (auto *ISAString = dyn_cast_or_null<MDString>(ISA)) {
563 auto ParseResult = llvm::RISCVISAInfo::parseArchString(
564 ISAString->getString(), /*EnableExperimentalExtension=*/true,
565 /*ExperimentalExtensionVersionCheck=*/true);
566 if (!errorToBool(ParseResult.takeError())) {
567 auto &ISAInfo = *ParseResult;
568 for (const auto &Feature : RISCVFeatureKV) {
569 if (ISAInfo->hasExtension(Feature.Key) &&
570 !SubtargetInfo.hasFeature(Feature.Value))
571 SubtargetInfo.ToggleFeature(Feature.Key);
572 }
573 }
574 }
575 }
576
577 RTS.setFlagsFromFeatures(SubtargetInfo);
578 }
579
580 if (TM.getTargetTriple().isOSBinFormatELF())
581 emitAttributes(SubtargetInfo);
582}
583
584void RISCVAsmPrinter::emitEndOfAsmFile(Module &M) {
585 RISCVTargetStreamer &RTS =
586 static_cast<RISCVTargetStreamer &>(*OutStreamer->getTargetStreamer());
587
588 if (TM.getTargetTriple().isOSBinFormatELF()) {
590 emitNoteGnuProperty(M);
591 }
592 EmitHwasanMemaccessSymbols(M);
593}
594
595void RISCVAsmPrinter::emitAttributes(const MCSubtargetInfo &SubtargetInfo) {
596 RISCVTargetStreamer &RTS =
597 static_cast<RISCVTargetStreamer &>(*OutStreamer->getTargetStreamer());
598 // Use MCSubtargetInfo from TargetMachine. Individual functions may have
599 // attributes that differ from other functions in the module and we have no
600 // way to know which function is correct.
601 RTS.emitTargetAttributes(SubtargetInfo, /*EmitStackAlign*/ true);
602}
603
604void RISCVAsmPrinter::emitFunctionEntryLabel() {
605 const auto *RMFI = MF->getInfo<RISCVMachineFunctionInfo>();
606 if (RMFI->isVectorCall()) {
607 auto &RTS =
608 static_cast<RISCVTargetStreamer &>(*OutStreamer->getTargetStreamer());
609 RTS.emitDirectiveVariantCC(*CurrentFnSym);
610 }
612}
613
614// Force static initialization.
622
623void RISCVAsmPrinter::LowerHWASAN_CHECK_MEMACCESS(const MachineInstr &MI) {
624 Register Reg = MI.getOperand(0).getReg();
625 uint32_t AccessInfo = MI.getOperand(1).getImm();
626 MCSymbol *&Sym =
627 HwasanMemaccessSymbols[HwasanMemaccessTuple(Reg, AccessInfo)];
628 if (!Sym) {
629 // FIXME: Make this work on non-ELF.
630 if (!TM.getTargetTriple().isOSBinFormatELF())
631 report_fatal_error("llvm.hwasan.check.memaccess only supported on ELF");
632
633 std::string SymName = "__hwasan_check_x" + utostr(Reg - RISCV::X0) + "_" +
634 utostr(AccessInfo) + "_short";
635 Sym = OutContext.getOrCreateSymbol(SymName);
636 }
637 auto Res = MCSymbolRefExpr::create(Sym, OutContext);
638 auto Expr = MCSpecifierExpr::create(Res, ELF::R_RISCV_CALL_PLT, OutContext);
639
640 EmitToStreamer(*OutStreamer, MCInstBuilder(RISCV::PseudoCALL).addExpr(Expr));
641}
642
643void RISCVAsmPrinter::LowerKCFI_CHECK(const MachineInstr &MI) {
644 Register AddrReg = MI.getOperand(0).getReg();
645 assert(std::next(MI.getIterator())->isCall() &&
646 "KCFI_CHECK not followed by a call instruction");
647 assert(std::next(MI.getIterator())->getOperand(0).getReg() == AddrReg &&
648 "KCFI_CHECK call target doesn't match call operand");
649
650 // Temporary registers for comparing the hashes. If a register is used
651 // for the call target, or reserved by the user, we can clobber another
652 // temporary register as the check is immediately followed by the
653 // call. The check defaults to X6/X7, but can fall back to X28-X31 if
654 // needed.
655 unsigned ScratchRegs[] = {RISCV::X6, RISCV::X7};
656 unsigned NextReg = RISCV::X28;
657 auto isRegAvailable = [&](unsigned Reg) {
658 return Reg != AddrReg && !STI->isRegisterReservedByUser(Reg);
659 };
660 for (auto &Reg : ScratchRegs) {
661 if (isRegAvailable(Reg))
662 continue;
663 while (!isRegAvailable(NextReg))
664 ++NextReg;
665 Reg = NextReg++;
666 if (Reg > RISCV::X31)
667 report_fatal_error("Unable to find scratch registers for KCFI_CHECK");
668 }
669
670 if (AddrReg == RISCV::X0) {
671 // Checking X0 makes no sense. Instead of emitting a load, zero
672 // ScratchRegs[0].
673 EmitToStreamer(*OutStreamer, MCInstBuilder(RISCV::ADDI)
674 .addReg(ScratchRegs[0])
675 .addReg(RISCV::X0)
676 .addImm(0));
677 } else {
678 // Adjust the offset for patchable-function-prefix. This assumes that
679 // patchable-function-prefix is the same for all functions.
680 int NopSize = STI->hasStdExtZca() ? 2 : 4;
681 int64_t PrefixNops = 0;
682 (void)MI.getMF()
683 ->getFunction()
684 .getFnAttribute("patchable-function-prefix")
685 .getValueAsString()
686 .getAsInteger(10, PrefixNops);
687
688 // Load the target function type hash.
689 EmitToStreamer(*OutStreamer, MCInstBuilder(RISCV::LW)
690 .addReg(ScratchRegs[0])
691 .addReg(AddrReg)
692 .addImm(-(PrefixNops * NopSize + 4)));
693 }
694
695 // Load the expected 32-bit type hash.
696 const int64_t Type = MI.getOperand(1).getImm();
697 const int64_t Hi20 = ((Type + 0x800) >> 12) & 0xFFFFF;
698 const int64_t Lo12 = SignExtend64<12>(Type);
699 if (Hi20) {
700 EmitToStreamer(
701 *OutStreamer,
702 MCInstBuilder(RISCV::LUI).addReg(ScratchRegs[1]).addImm(Hi20));
703 }
704 if (Lo12 || Hi20 == 0) {
705 EmitToStreamer(*OutStreamer,
706 MCInstBuilder((STI->hasFeature(RISCV::Feature64Bit) && Hi20)
707 ? RISCV::ADDIW
708 : RISCV::ADDI)
709 .addReg(ScratchRegs[1])
710 .addReg(ScratchRegs[1])
711 .addImm(Lo12));
712 }
713
714 // Compare the hashes and trap if there's a mismatch.
715 MCSymbol *Pass = OutContext.createTempSymbol();
716 EmitToStreamer(*OutStreamer,
717 MCInstBuilder(RISCV::BEQ)
718 .addReg(ScratchRegs[0])
719 .addReg(ScratchRegs[1])
720 .addExpr(MCSymbolRefExpr::create(Pass, OutContext)));
721
722 MCSymbol *Trap = OutContext.createTempSymbol();
723 OutStreamer->emitLabel(Trap);
724 EmitToStreamer(*OutStreamer, MCInstBuilder(RISCV::EBREAK));
725 emitKCFITrapEntry(*MI.getMF(), Trap);
726 OutStreamer->emitLabel(Pass);
727}
728
729void RISCVAsmPrinter::EmitHwasanMemaccessSymbols(Module &M) {
730 if (HwasanMemaccessSymbols.empty())
731 return;
732
733 assert(TM.getTargetTriple().isOSBinFormatELF());
734 // Use MCSubtargetInfo from TargetMachine. Individual functions may have
735 // attributes that differ from other functions in the module and we have no
736 // way to know which function is correct.
737 const MCSubtargetInfo &MCSTI = *TM.getMCSubtargetInfo();
738
739 MCSymbol *HwasanTagMismatchV2Sym =
740 OutContext.getOrCreateSymbol("__hwasan_tag_mismatch_v2");
741 // Annotate symbol as one having incompatible calling convention, so
742 // run-time linkers can instead eagerly bind this function.
743 auto &RTS =
744 static_cast<RISCVTargetStreamer &>(*OutStreamer->getTargetStreamer());
745 RTS.emitDirectiveVariantCC(*HwasanTagMismatchV2Sym);
746
747 const MCSymbolRefExpr *HwasanTagMismatchV2Ref =
748 MCSymbolRefExpr::create(HwasanTagMismatchV2Sym, OutContext);
749 auto Expr = MCSpecifierExpr::create(HwasanTagMismatchV2Ref,
750 ELF::R_RISCV_CALL_PLT, OutContext);
751
752 for (auto &P : HwasanMemaccessSymbols) {
753 unsigned Reg = std::get<0>(P.first);
754 uint32_t AccessInfo = std::get<1>(P.first);
755 MCSymbol *Sym = P.second;
756
757 unsigned Size =
758 1 << ((AccessInfo >> HWASanAccessInfo::AccessSizeShift) & 0xf);
759 OutStreamer->switchSection(OutContext.getELFSection(
760 ".text.hot", ELF::SHT_PROGBITS,
762 /*IsComdat=*/true));
763
765 OutStreamer->emitSymbolAttribute(Sym, MCSA_Weak);
766 OutStreamer->emitSymbolAttribute(Sym, MCSA_Hidden);
767 OutStreamer->emitLabel(Sym);
768
769 // Extract shadow offset from ptr
770 EmitToStreamer(
771 *OutStreamer,
772 MCInstBuilder(RISCV::SLLI).addReg(RISCV::X6).addReg(Reg).addImm(8),
773 MCSTI);
774 EmitToStreamer(*OutStreamer,
775 MCInstBuilder(RISCV::SRLI)
776 .addReg(RISCV::X6)
777 .addReg(RISCV::X6)
778 .addImm(12),
779 MCSTI);
780 // load shadow tag in X6, X5 contains shadow base
781 EmitToStreamer(*OutStreamer,
782 MCInstBuilder(RISCV::ADD)
783 .addReg(RISCV::X6)
784 .addReg(RISCV::X5)
785 .addReg(RISCV::X6),
786 MCSTI);
787 EmitToStreamer(
788 *OutStreamer,
789 MCInstBuilder(RISCV::LBU).addReg(RISCV::X6).addReg(RISCV::X6).addImm(0),
790 MCSTI);
791 // Extract tag from pointer and compare it with loaded tag from shadow
792 EmitToStreamer(
793 *OutStreamer,
794 MCInstBuilder(RISCV::SRLI).addReg(RISCV::X7).addReg(Reg).addImm(56),
795 MCSTI);
796 MCSymbol *HandleMismatchOrPartialSym = OutContext.createTempSymbol();
797 // X7 contains tag from the pointer, while X6 contains tag from memory
798 EmitToStreamer(*OutStreamer,
799 MCInstBuilder(RISCV::BNE)
800 .addReg(RISCV::X7)
801 .addReg(RISCV::X6)
803 HandleMismatchOrPartialSym, OutContext)),
804 MCSTI);
805 MCSymbol *ReturnSym = OutContext.createTempSymbol();
806 OutStreamer->emitLabel(ReturnSym);
807 EmitToStreamer(*OutStreamer,
808 MCInstBuilder(RISCV::JALR)
809 .addReg(RISCV::X0)
810 .addReg(RISCV::X1)
811 .addImm(0),
812 MCSTI);
813 OutStreamer->emitLabel(HandleMismatchOrPartialSym);
814
815 EmitToStreamer(*OutStreamer,
816 MCInstBuilder(RISCV::ADDI)
817 .addReg(RISCV::X28)
818 .addReg(RISCV::X0)
819 .addImm(16),
820 MCSTI);
821 MCSymbol *HandleMismatchSym = OutContext.createTempSymbol();
822 EmitToStreamer(
823 *OutStreamer,
824 MCInstBuilder(RISCV::BGEU)
825 .addReg(RISCV::X6)
826 .addReg(RISCV::X28)
827 .addExpr(MCSymbolRefExpr::create(HandleMismatchSym, OutContext)),
828 MCSTI);
829
830 EmitToStreamer(
831 *OutStreamer,
832 MCInstBuilder(RISCV::ANDI).addReg(RISCV::X28).addReg(Reg).addImm(0xF),
833 MCSTI);
834
835 if (Size != 1)
836 EmitToStreamer(*OutStreamer,
837 MCInstBuilder(RISCV::ADDI)
838 .addReg(RISCV::X28)
839 .addReg(RISCV::X28)
840 .addImm(Size - 1),
841 MCSTI);
842 EmitToStreamer(
843 *OutStreamer,
844 MCInstBuilder(RISCV::BGE)
845 .addReg(RISCV::X28)
846 .addReg(RISCV::X6)
847 .addExpr(MCSymbolRefExpr::create(HandleMismatchSym, OutContext)),
848 MCSTI);
849
850 EmitToStreamer(
851 *OutStreamer,
852 MCInstBuilder(RISCV::ORI).addReg(RISCV::X6).addReg(Reg).addImm(0xF),
853 MCSTI);
854 EmitToStreamer(
855 *OutStreamer,
856 MCInstBuilder(RISCV::LBU).addReg(RISCV::X6).addReg(RISCV::X6).addImm(0),
857 MCSTI);
858 EmitToStreamer(*OutStreamer,
859 MCInstBuilder(RISCV::BEQ)
860 .addReg(RISCV::X6)
861 .addReg(RISCV::X7)
862 .addExpr(MCSymbolRefExpr::create(ReturnSym, OutContext)),
863 MCSTI);
864
865 OutStreamer->emitLabel(HandleMismatchSym);
866
867 // | Previous stack frames... |
868 // +=================================+ <-- [SP + 256]
869 // | ... |
870 // | |
871 // | Stack frame space for x12 - x31.|
872 // | |
873 // | ... |
874 // +---------------------------------+ <-- [SP + 96]
875 // | Saved x11(arg1), as |
876 // | __hwasan_check_* clobbers it. |
877 // +---------------------------------+ <-- [SP + 88]
878 // | Saved x10(arg0), as |
879 // | __hwasan_check_* clobbers it. |
880 // +---------------------------------+ <-- [SP + 80]
881 // | |
882 // | Stack frame space for x9. |
883 // +---------------------------------+ <-- [SP + 72]
884 // | |
885 // | Saved x8(fp), as |
886 // | __hwasan_check_* clobbers it. |
887 // +---------------------------------+ <-- [SP + 64]
888 // | ... |
889 // | |
890 // | Stack frame space for x2 - x7. |
891 // | |
892 // | ... |
893 // +---------------------------------+ <-- [SP + 16]
894 // | Return address (x1) for caller |
895 // | of __hwasan_check_*. |
896 // +---------------------------------+ <-- [SP + 8]
897 // | Reserved place for x0, possibly |
898 // | junk, since we don't save it. |
899 // +---------------------------------+ <-- [x2 / SP]
900
901 // Adjust sp
902 EmitToStreamer(*OutStreamer,
903 MCInstBuilder(RISCV::ADDI)
904 .addReg(RISCV::X2)
905 .addReg(RISCV::X2)
906 .addImm(-256),
907 MCSTI);
908
909 // store x10(arg0) by new sp
910 EmitToStreamer(*OutStreamer,
911 MCInstBuilder(RISCV::SD)
912 .addReg(RISCV::X10)
913 .addReg(RISCV::X2)
914 .addImm(8 * 10),
915 MCSTI);
916 // store x11(arg1) by new sp
917 EmitToStreamer(*OutStreamer,
918 MCInstBuilder(RISCV::SD)
919 .addReg(RISCV::X11)
920 .addReg(RISCV::X2)
921 .addImm(8 * 11),
922 MCSTI);
923
924 // store x8(fp) by new sp
925 EmitToStreamer(
926 *OutStreamer,
927 MCInstBuilder(RISCV::SD).addReg(RISCV::X8).addReg(RISCV::X2).addImm(8 *
928 8),
929 MCSTI);
930 // store x1(ra) by new sp
931 EmitToStreamer(
932 *OutStreamer,
933 MCInstBuilder(RISCV::SD).addReg(RISCV::X1).addReg(RISCV::X2).addImm(1 *
934 8),
935 MCSTI);
936 if (Reg != RISCV::X10)
937 EmitToStreamer(
938 *OutStreamer,
939 MCInstBuilder(RISCV::ADDI).addReg(RISCV::X10).addReg(Reg).addImm(0),
940 MCSTI);
941 EmitToStreamer(*OutStreamer,
942 MCInstBuilder(RISCV::ADDI)
943 .addReg(RISCV::X11)
944 .addReg(RISCV::X0)
945 .addImm(AccessInfo & HWASanAccessInfo::RuntimeMask),
946 MCSTI);
947
948 EmitToStreamer(*OutStreamer, MCInstBuilder(RISCV::PseudoCALL).addExpr(Expr),
949 MCSTI);
950 }
951}
952
953void RISCVAsmPrinter::emitNoteGnuProperty(const Module &M) {
954 if (const Metadata *const Flag = M.getModuleFlag("cf-protection-return");
955 Flag && !mdconst::extract<ConstantInt>(Flag)->isZero()) {
956 RISCVTargetStreamer &RTS =
957 static_cast<RISCVTargetStreamer &>(*OutStreamer->getTargetStreamer());
959 }
960}
961
963 const AsmPrinter &AP) {
964 MCContext &Ctx = AP.OutContext;
965 RISCV::Specifier Kind;
966
967 switch (MO.getTargetFlags()) {
968 default:
969 llvm_unreachable("Unknown target flag on GV operand");
970 case RISCVII::MO_None:
971 Kind = RISCV::S_None;
972 break;
973 case RISCVII::MO_CALL:
974 Kind = ELF::R_RISCV_CALL_PLT;
975 break;
976 case RISCVII::MO_LO:
977 Kind = RISCV::S_LO;
978 break;
979 case RISCVII::MO_HI:
980 Kind = ELF::R_RISCV_HI20;
981 break;
983 Kind = RISCV::S_PCREL_LO;
984 break;
986 Kind = ELF::R_RISCV_PCREL_HI20;
987 break;
989 Kind = ELF::R_RISCV_GOT_HI20;
990 break;
992 Kind = RISCV::S_TPREL_LO;
993 break;
995 Kind = ELF::R_RISCV_TPREL_HI20;
996 break;
998 Kind = ELF::R_RISCV_TPREL_ADD;
999 break;
1001 Kind = ELF::R_RISCV_TLS_GOT_HI20;
1002 break;
1004 Kind = ELF::R_RISCV_TLS_GD_HI20;
1005 break;
1007 Kind = ELF::R_RISCV_TLSDESC_HI20;
1008 break;
1010 Kind = ELF::R_RISCV_TLSDESC_LOAD_LO12;
1011 break;
1013 Kind = ELF::R_RISCV_TLSDESC_ADD_LO12;
1014 break;
1016 Kind = ELF::R_RISCV_TLSDESC_CALL;
1017 break;
1018 }
1019
1020 const MCExpr *ME = MCSymbolRefExpr::create(Sym, Ctx);
1021
1022 if (!MO.isJTI() && !MO.isMBB() && MO.getOffset())
1024 ME, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
1025
1026 if (Kind != RISCV::S_None)
1027 ME = MCSpecifierExpr::create(ME, Kind, Ctx);
1028 return MCOperand::createExpr(ME);
1029}
1030
1031bool RISCVAsmPrinter::lowerOperand(const MachineOperand &MO,
1032 MCOperand &MCOp) const {
1033 switch (MO.getType()) {
1034 default:
1035 report_fatal_error("lowerOperand: unknown operand type");
1037 // Ignore all implicit register operands.
1038 if (MO.isImplicit())
1039 return false;
1040 MCOp = MCOperand::createReg(MO.getReg());
1041 break;
1043 // Regmasks are like implicit defs.
1044 return false;
1046 MCOp = MCOperand::createImm(MO.getImm());
1047 break;
1049 MCOp = lowerSymbolOperand(MO, MO.getMBB()->getSymbol(), *this);
1050 break;
1052 MCOp = lowerSymbolOperand(MO, getSymbolPreferLocal(*MO.getGlobal()), *this);
1053 break;
1055 MCOp = lowerSymbolOperand(MO, GetBlockAddressSymbol(MO.getBlockAddress()),
1056 *this);
1057 break;
1059 MCOp = lowerSymbolOperand(MO, GetExternalSymbolSymbol(MO.getSymbolName()),
1060 *this);
1061 break;
1063 MCOp = lowerSymbolOperand(MO, GetCPISymbol(MO.getIndex()), *this);
1064 break;
1066 MCOp = lowerSymbolOperand(MO, GetJTISymbol(MO.getIndex()), *this);
1067 break;
1069 MCOp = lowerSymbolOperand(MO, MO.getMCSymbol(), *this);
1070 break;
1071 }
1072 return true;
1073}
1074
1076 MCInst &OutMI,
1077 const RISCVSubtarget *STI) {
1079 RISCVVPseudosTable::getPseudoInfo(MI->getOpcode());
1080 if (!RVV)
1081 return false;
1082
1083 OutMI.setOpcode(RVV->BaseInstr);
1084
1085 const TargetInstrInfo *TII = STI->getInstrInfo();
1086 const TargetRegisterInfo *TRI = STI->getRegisterInfo();
1087 assert(TRI && "TargetRegisterInfo expected");
1088
1089 const MCInstrDesc &MCID = MI->getDesc();
1090 uint64_t TSFlags = MCID.TSFlags;
1091 unsigned NumOps = MI->getNumExplicitOperands();
1092
1093 // Skip policy, SEW, VL, VXRM/FRM operands which are the last operands if
1094 // present.
1095 if (RISCVII::hasVecPolicyOp(TSFlags))
1096 --NumOps;
1097 if (RISCVII::hasSEWOp(TSFlags))
1098 --NumOps;
1099 if (RISCVII::hasVLOp(TSFlags))
1100 --NumOps;
1101 if (RISCVII::hasRoundModeOp(TSFlags))
1102 --NumOps;
1103
1104 bool hasVLOutput = RISCVInstrInfo::isFaultOnlyFirstLoad(*MI);
1105 for (unsigned OpNo = 0; OpNo != NumOps; ++OpNo) {
1106 const MachineOperand &MO = MI->getOperand(OpNo);
1107 // Skip vl output. It should be the second output.
1108 if (hasVLOutput && OpNo == 1)
1109 continue;
1110
1111 // Skip passthru op. It should be the first operand after the defs.
1112 if (OpNo == MI->getNumExplicitDefs() && MO.isReg() && MO.isTied()) {
1113 assert(MCID.getOperandConstraint(OpNo, MCOI::TIED_TO) == 0 &&
1114 "Expected tied to first def.");
1115 const MCInstrDesc &OutMCID = TII->get(OutMI.getOpcode());
1116 // Skip if the next operand in OutMI is not supposed to be tied. Unless it
1117 // is a _TIED instruction.
1118 if (OutMCID.getOperandConstraint(OutMI.getNumOperands(), MCOI::TIED_TO) <
1119 0 &&
1120 !RISCVII::isTiedPseudo(TSFlags))
1121 continue;
1122 }
1123
1124 MCOperand MCOp;
1125 switch (MO.getType()) {
1126 default:
1127 llvm_unreachable("Unknown operand type");
1129 Register Reg = MO.getReg();
1130
1131 if (RISCV::VRM2RegClass.contains(Reg) ||
1132 RISCV::VRM4RegClass.contains(Reg) ||
1133 RISCV::VRM8RegClass.contains(Reg)) {
1134 Reg = TRI->getSubReg(Reg, RISCV::sub_vrm1_0);
1135 assert(Reg && "Subregister does not exist");
1136 } else if (RISCV::FPR16RegClass.contains(Reg)) {
1137 Reg =
1138 TRI->getMatchingSuperReg(Reg, RISCV::sub_16, &RISCV::FPR32RegClass);
1139 assert(Reg && "Subregister does not exist");
1140 } else if (RISCV::FPR64RegClass.contains(Reg)) {
1141 Reg = TRI->getSubReg(Reg, RISCV::sub_32);
1142 assert(Reg && "Superregister does not exist");
1143 } else if (RISCV::VRN2M1RegClass.contains(Reg) ||
1144 RISCV::VRN2M2RegClass.contains(Reg) ||
1145 RISCV::VRN2M4RegClass.contains(Reg) ||
1146 RISCV::VRN3M1RegClass.contains(Reg) ||
1147 RISCV::VRN3M2RegClass.contains(Reg) ||
1148 RISCV::VRN4M1RegClass.contains(Reg) ||
1149 RISCV::VRN4M2RegClass.contains(Reg) ||
1150 RISCV::VRN5M1RegClass.contains(Reg) ||
1151 RISCV::VRN6M1RegClass.contains(Reg) ||
1152 RISCV::VRN7M1RegClass.contains(Reg) ||
1153 RISCV::VRN8M1RegClass.contains(Reg)) {
1154 Reg = TRI->getSubReg(Reg, RISCV::sub_vrm1_0);
1155 assert(Reg && "Subregister does not exist");
1156 }
1157
1158 MCOp = MCOperand::createReg(Reg);
1159 break;
1160 }
1162 MCOp = MCOperand::createImm(MO.getImm());
1163 break;
1164 }
1165 OutMI.addOperand(MCOp);
1166 }
1167
1168 // Unmasked pseudo instructions need to append dummy mask operand to
1169 // V instructions. All V instructions are modeled as the masked version.
1170 const MCInstrDesc &OutMCID = TII->get(OutMI.getOpcode());
1171 if (OutMI.getNumOperands() < OutMCID.getNumOperands()) {
1172 assert(OutMCID.operands()[OutMI.getNumOperands()].RegClass ==
1173 RISCV::VMV0RegClassID &&
1174 "Expected only mask operand to be missing");
1175 OutMI.addOperand(MCOperand::createReg(RISCV::NoRegister));
1176 }
1177
1178 assert(OutMI.getNumOperands() == OutMCID.getNumOperands());
1179 return true;
1180}
1181
1182void RISCVAsmPrinter::lowerToMCInst(const MachineInstr *MI, MCInst &OutMI) {
1183 if (lowerRISCVVMachineInstrToMCInst(MI, OutMI, STI))
1184 return;
1185
1186 OutMI.setOpcode(MI->getOpcode());
1187
1188 for (const MachineOperand &MO : MI->operands()) {
1189 MCOperand MCOp;
1190 if (lowerOperand(MO, MCOp))
1191 OutMI.addOperand(MCOp);
1192 }
1193}
1194
1195void RISCVAsmPrinter::emitMachineConstantPoolValue(
1196 MachineConstantPoolValue *MCPV) {
1197 auto *RCPV = static_cast<RISCVConstantPoolValue *>(MCPV);
1198 MCSymbol *MCSym;
1199
1200 if (RCPV->isGlobalValue()) {
1201 auto *GV = RCPV->getGlobalValue();
1202 MCSym = getSymbol(GV);
1203 } else {
1204 assert(RCPV->isExtSymbol() && "unrecognized constant pool type");
1205 auto Sym = RCPV->getSymbol();
1206 MCSym = GetExternalSymbolSymbol(Sym);
1207 }
1208
1209 const MCExpr *Expr = MCSymbolRefExpr::create(MCSym, OutContext);
1210 uint64_t Size = getDataLayout().getTypeAllocSize(RCPV->getType());
1211 OutStreamer->emitValue(Expr, Size);
1212}
1213
1214char RISCVAsmPrinter::ID = 0;
1215
1216INITIALIZE_PASS(RISCVAsmPrinter, "riscv-asm-printer", "RISC-V Assembly Printer",
1217 false, false)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static MCDisassembler::DecodeStatus addOperand(MCInst &Inst, const MCOperand &Opnd)
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock & MBB
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
dxil translate DXIL Translate Metadata
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
static MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym, const AsmPrinter &AP)
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
Machine Check Debug Module
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define P(N)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool lowerRISCVVMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, const RISCVSubtarget *STI)
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVAsmPrinter()
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:480
Shrink Wrap Pass
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
This class is intended to be used as a driving class for all asm writers.
Definition AsmPrinter.h:90
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition AsmPrinter.h:100
virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant as...
virtual void emitFunctionEntryLabel()
EmitFunctionEntryLabel - Emit the label that is the entrypoint for the function.
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:343
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
unsigned getNumOperands() const
Definition MCInst.h:212
unsigned getOpcode() const
Definition MCInst.h:202
void addOperand(const MCOperand Op)
Definition MCInst.h:215
void setOpcode(unsigned Op)
Definition MCInst.h:201
Describe properties that are true of each instruction in the target description file.
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
ArrayRef< MCOperandInfo > operands() const
int getOperandConstraint(unsigned OpNum, MCOI::OperandConstraint Constraint) const
Returns the value of the specified operand constraint if it is present.
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:166
int64_t getImm() const
Definition MCInst.h:84
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
const MCExpr * getExpr() const
Definition MCInst.h:118
static const MCSpecifierExpr * create(const MCExpr *Expr, Spec S, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:743
Streaming machine code generation interface.
Definition MCStreamer.h:220
virtual void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI)
Emit the given Instruction into the current section.
virtual bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute)=0
Add the given Attribute to Symbol.
MCContext & getContext() const
Definition MCStreamer.h:314
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
MCTargetStreamer * getTargetStreamer()
Definition MCStreamer.h:324
virtual void emitCodeAlignment(Align Alignment, const MCSubtargetInfo *STI, unsigned MaxBytesToEmit=0)
Emit nops until the byte alignment ByteAlignment is reached.
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
bool hasFeature(unsigned Feature) const
FeatureBitset ToggleFeature(uint64_t FB)
Toggle a feature and return the re-computed feature bits.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:214
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition MCSymbol.cpp:59
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
MachineInstrBundleIterator< const MachineInstr > const_iterator
LLVM_ABI MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Representation of each machine instruction.
Flags getFlags() const
Return the raw flags of the source value,.
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
const BlockAddress * getBlockAddress() const
unsigned getTargetFlags() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
MCSymbol * getMCSymbol() const
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_MCSymbol
MCSymbol reference (for debug/eh info)
@ MO_GlobalAddress
Address of a global value.
@ MO_RegisterMask
Mask of preserved registers.
@ MO_BlockAddress
Address of a basic block.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
int64_t getOffset() const
Return the offset from the symbol in this operand.
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
static LLVM_ABI bool isSupportedExtensionFeature(StringRef Ext)
static LLVM_ABI llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseArchString(StringRef Arch, bool EnableExperimentalExtension, bool ExperimentalExtensionVersionCheck=true)
Parse RISC-V ISA info from arch string.
static const char * getRegisterName(MCRegister Reg)
bool isRegisterReservedByUser(Register i) const override
const RISCVRegisterInfo * getRegisterInfo() const override
const RISCVInstrInfo * getInstrInfo() const override
void emitNoteGnuPropertySection(const uint32_t Feature1And)
virtual void emitDirectiveVariantCC(MCSymbol &Symbol)
void emitTargetAttributes(const MCSubtargetInfo &STI, bool EmitStackAlign)
void setFlagsFromFeatures(const MCSubtargetInfo &STI)
void setTargetABI(RISCVABI::ABI ABI)
virtual void emitDirectiveOptionArch(ArrayRef< RISCVOptionArchArg > Args)
Wrapper class representing virtual and physical registers.
Definition Register.h:19
reference emplace_back(ArgTypes &&... Args)
LLVM_ABI void recordStatepoint(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a statepoint instruction.
LLVM_ABI void recordPatchPoint(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a patchpoint instruction.
LLVM_ABI void recordStackMap(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a stackmap instruction.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ SHT_PROGBITS
Definition ELF.h:1143
@ SHF_ALLOC
Definition ELF.h:1243
@ SHF_GROUP
Definition ELF.h:1265
@ SHF_EXECINSTR
Definition ELF.h:1246
@ GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS
Definition ELF.h:1908
ABI getTargetABI(StringRef ABIName)
static bool hasRoundModeOp(uint64_t TSFlags)
static bool isTiedPseudo(uint64_t TSFlags)
static bool hasVLOp(uint64_t TSFlags)
static bool hasVecPolicyOp(uint64_t TSFlags)
static bool hasSEWOp(uint64_t TSFlags)
void generateMCInstSeq(int64_t Val, const MCSubtargetInfo &STI, MCRegister DestReg, SmallVectorImpl< MCInst > &Insts)
bool compress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI)
uint16_t Specifier
void emitInstruction(MCObjectStreamer &, const MCInst &Inst, const MCSubtargetInfo &STI)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:666
This is an optimization pass for GlobalISel generic memory operations.
bool errorToBool(Error Err)
Helper for converting an Error to a bool.
Definition Error.h:1113
@ Offset
Definition DWP.cpp:477
static const MachineMemOperand::Flags MONontemporalBit1
Target & getTheRISCV32Target()
static const MachineMemOperand::Flags MONontemporalBit0
std::string utostr(uint64_t X, bool isNeg=false)
Target & getTheRISCV64beTarget()
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:759
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
Target & getTheRISCV64Target()
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1847
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:583
const SubtargetFeatureKV RISCVFeatureKV[RISCV::NumSubtargetFeatures]
@ MCSA_Weak
.weak
@ MCSA_ELF_TypeFunction
.type _foo, STT_FUNC # aka @function
@ MCSA_Hidden
.hidden (ELF)
Target & getTheRISCV32beTarget()
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...
Used to provide key value pairs for feature and CPU bit flags.