54#define DEBUG_TYPE "asm-printer"
57 "Number of RISC-V Compressed instructions emitted");
65 const RISCVMCOptions &CLOpts;
70 std::unique_ptr<MCStreamer>
Streamer)
74 StringRef getPassName()
const override {
return "RISC-V Assembly Printer"; }
76 RISCVTargetStreamer &getTargetStreamer()
const {
77 return static_cast<RISCVTargetStreamer &
>(
78 *OutStreamer->getTargetStreamer());
81 void LowerSTACKMAP(MCStreamer &OutStreamer, StackMaps &SM,
82 const MachineInstr &
MI);
84 void LowerPATCHPOINT(MCStreamer &OutStreamer, StackMaps &SM,
85 const MachineInstr &
MI);
87 void LowerSTATEPOINT(MCStreamer &OutStreamer, StackMaps &SM,
88 const MachineInstr &
MI);
94 void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV)
override;
96 bool PrintAsmOperand(
const MachineInstr *
MI,
unsigned OpNo,
97 const char *ExtraCode, raw_ostream &OS)
override;
98 bool PrintAsmMemoryOperand(
const MachineInstr *
MI,
unsigned OpNo,
99 const char *ExtraCode, raw_ostream &OS)
override;
102 bool EmitToStreamer(MCStreamer &S,
const MCInst &Inst,
103 const MCSubtargetInfo &SubtargetInfo);
104 bool EmitToStreamer(MCStreamer &S,
const MCInst &Inst) {
105 return EmitToStreamer(S, Inst, *STI);
108 bool lowerPseudoInstExpansion(
const MachineInstr *
MI, MCInst &Inst);
110 typedef std::tuple<unsigned, uint32_t> HwasanMemaccessTuple;
111 std::map<HwasanMemaccessTuple, MCSymbol *> HwasanMemaccessSymbols;
112 void LowerHWASAN_CHECK_MEMACCESS(
const MachineInstr &
MI);
113 void LowerKCFI_CHECK(
const MachineInstr &
MI);
114 void EmitHwasanMemaccessSymbols(
Module &M);
117 bool lowerOperand(
const MachineOperand &MO, MCOperand &MCOp)
const;
119 void emitStartOfAsmFile(
Module &M)
override;
120 void emitEndOfAsmFile(
Module &M)
override;
122 void emitFunctionEntryLabel()
override;
123 bool emitTargetFeaturePush(
const MCSubtargetInfo &STI)
override;
124 void emitTargetFeaturePop(
const MCSubtargetInfo &STI,
bool DidPush)
override;
126 void emitNoteGnuProperty(
const Module &M);
129 void emitNTLHint(
const MachineInstr *
MI);
131 void emitLpadAlignedCall(
const MachineInstr &
MI);
134 void LowerPATCHABLE_FUNCTION_ENTER(
const MachineInstr *
MI);
135 void LowerPATCHABLE_FUNCTION_EXIT(
const MachineInstr *
MI);
136 void LowerPATCHABLE_TAIL_CALL(
const MachineInstr *
MI);
137 void emitSled(
const MachineInstr *
MI, SledKind Kind);
139 void lowerToMCInst(
const MachineInstr *
MI, MCInst &OutMI);
142 getRequiredGlobalAlignmentGranule(
const GlobalVariable &GV)
override;
148 unsigned NOPBytes = STI->hasStdExtZca() ? 2 : 4;
149 unsigned NumNOPBytes = StackMapOpers(&
MI).getNumPatchBytes();
152 MCSymbol *MILabel = Ctx.createTempSymbol();
156 assert(NumNOPBytes % NOPBytes == 0 &&
157 "Invalid number of NOP bytes requested!");
163 while (NumNOPBytes > 0) {
164 if (MII !=
MBB.
end() && MII->isDebugInstr()) {
168 if (MII ==
MBB.
end() || MII->isCall() ||
169 MII->getOpcode() == TargetOpcode::PATCHPOINT ||
170 MII->getOpcode() == TargetOpcode::STACKMAP)
173 NumNOPBytes -= NOPBytes;
177 emitNops(NumNOPBytes / NOPBytes);
182void RISCVAsmPrinter::LowerPATCHPOINT(MCStreamer &OutStreamer, StackMaps &SM,
183 const MachineInstr &
MI) {
184 unsigned NOPBytes = STI->hasStdExtZca() ? 2 : 4;
187 MCSymbol *MILabel = Ctx.createTempSymbol();
191 PatchPointOpers Opers(&
MI);
193 const MachineOperand &CalleeMO = Opers.getCallTarget();
194 unsigned EncodedBytes = 0;
196 if (CalleeMO.
isImm()) {
199 assert((CallTarget & 0xFFFF'FFFF'FFFF) == CallTarget &&
200 "High 16 bits of call target should be zero.");
204 for (MCInst &Inst : Seq) {
205 bool Compressed = EmitToStreamer(OutStreamer, Inst);
206 EncodedBytes += Compressed ? 2 : 4;
208 bool Compressed = EmitToStreamer(OutStreamer, MCInstBuilder(RISCV::JALR)
212 EncodedBytes += Compressed ? 2 : 4;
215 MCOperand CallTargetMCOp;
216 lowerOperand(CalleeMO, CallTargetMCOp);
217 EmitToStreamer(OutStreamer,
218 MCInstBuilder(RISCV::PseudoCALL).
addOperand(CallTargetMCOp));
223 unsigned NumBytes = Opers.getNumPatchBytes();
224 assert(NumBytes >= EncodedBytes &&
225 "Patchpoint can't request size less than the length of a call.");
226 assert((NumBytes - EncodedBytes) % NOPBytes == 0 &&
227 "Invalid number of NOP bytes requested!");
228 emitNops((NumBytes - EncodedBytes) / NOPBytes);
231void RISCVAsmPrinter::LowerSTATEPOINT(MCStreamer &OutStreamer, StackMaps &SM,
232 const MachineInstr &
MI) {
233 unsigned NOPBytes = STI->hasStdExtZca() ? 2 : 4;
235 StatepointOpers SOpers(&
MI);
236 if (
unsigned PatchBytes = SOpers.getNumPatchBytes()) {
237 assert(PatchBytes % NOPBytes == 0 &&
238 "Invalid number of NOP bytes requested!");
239 emitNops(PatchBytes / NOPBytes);
242 const MachineOperand &CallTarget = SOpers.getCallTarget();
243 MCOperand CallTargetMCOp;
244 switch (CallTarget.
getType()) {
247 lowerOperand(CallTarget, CallTargetMCOp);
250 MCInstBuilder(RISCV::PseudoCALL).
addOperand(CallTargetMCOp));
254 EmitToStreamer(OutStreamer, MCInstBuilder(RISCV::JAL)
260 EmitToStreamer(OutStreamer, MCInstBuilder(RISCV::JALR)
272 MCSymbol *MILabel = Ctx.createTempSymbol();
277bool RISCVAsmPrinter::EmitToStreamer(MCStreamer &S,
const MCInst &Inst,
278 const MCSubtargetInfo &SubtargetInfo) {
282 ++RISCVNumInstrsCompressed;
289#include "RISCVGenMCPseudoLowering.inc"
296void RISCVAsmPrinter::emitLpadAlignedCall(
const MachineInstr &
MI) {
297 const MCSubtargetInfo &MCSTI = getSubtargetInfo();
298 const bool IsIndirect =
MI.getOpcode() == RISCV::PseudoCALLIndirectLpadAlign,
299 HasZca = MCSTI.
hasFeature(RISCV::FeatureStdExtZca),
300 HasRelax = MCSTI.
hasFeature(RISCV::FeatureRelax);
308 RISCVTargetStreamer &RTS = getTargetStreamer();
309 if (HasZca && HasRelax) {
317 lowerOperand(
MI.getOperand(0), MCOp);
318 CallInst = MCInstBuilder(RISCV::PseudoCALL).
addOperand(MCOp);
320 CallInst = MCInstBuilder(RISCV::JALR)
322 .addReg(
MI.getOperand(0).getReg())
326 if (HasZca && HasRelax) {
327 MCSubtargetInfo NoRelaxSTI(MCSTI);
328 NoRelaxSTI.ToggleFeature(RISCV::FeatureRelax);
329 EmitToStreamer(*OutStreamer, CallInst, NoRelaxSTI);
332 EmitToStreamer(*OutStreamer, CallInst, MCSTI);
336 MCInst LpadInst = MCInstBuilder(RISCV::AUIPC)
338 .addImm(
MI.getOperand(1).getImm());
339 EmitToStreamer(*OutStreamer, LpadInst, MCSTI);
348 lowerOperand(
MI.getOperand(0), MCOp);
354 EmitToStreamer(*OutStreamer, TmpInst, MCSTI);
362void RISCVAsmPrinter::emitNTLHint(
const MachineInstr *
MI) {
368 MachineMemOperand *MMO = *(
MI->memoperands_begin());
372 unsigned NontemporalMode = 0;
374 NontemporalMode += 0b1;
376 NontemporalMode += 0b10;
379 if (STI->hasStdExtZca())
380 Hint.setOpcode(RISCV::C_ADD);
382 Hint.setOpcode(RISCV::ADD);
388 EmitToStreamer(*OutStreamer, Hint);
391void RISCVAsmPrinter::emitInstruction(
const MachineInstr *
MI) {
392 RISCV_MC::verifyInstructionPredicates(
MI->getOpcode(), STI->getFeatureBits());
397 if (MCInst OutInst; lowerPseudoInstExpansion(
MI, OutInst)) {
398 EmitToStreamer(*OutStreamer, OutInst);
402 switch (
MI->getOpcode()) {
403 case RISCV::PseudoTAILX7: {
406 lowerOperand(
MI->getOperand(0), SymOp);
411 EmitToStreamer(*OutStreamer, TmpInst);
414 case RISCV::HWASAN_CHECK_MEMACCESS_SHORTGRANULES:
415 LowerHWASAN_CHECK_MEMACCESS(*
MI);
417 case RISCV::KCFI_CHECK:
418 LowerKCFI_CHECK(*
MI);
420 case TargetOpcode::STACKMAP:
421 return LowerSTACKMAP(*OutStreamer, SM, *
MI);
422 case TargetOpcode::PATCHPOINT:
423 return LowerPATCHPOINT(*OutStreamer, SM, *
MI);
424 case TargetOpcode::STATEPOINT:
425 return LowerSTATEPOINT(*OutStreamer, SM, *
MI);
426 case TargetOpcode::PATCHABLE_FUNCTION_ENTER: {
427 const Function &
F =
MI->getParent()->getParent()->getFunction();
428 if (
F.hasFnAttribute(
"patchable-function-entry")) {
430 F.getFnAttributeAsParsedInteger(
"patchable-function-entry");
434 LowerPATCHABLE_FUNCTION_ENTER(
MI);
437 case TargetOpcode::PATCHABLE_FUNCTION_EXIT:
438 LowerPATCHABLE_FUNCTION_EXIT(
MI);
440 case TargetOpcode::PATCHABLE_TAIL_CALL:
441 LowerPATCHABLE_TAIL_CALL(
MI);
443 case RISCV::PseudoCALLLpadAlign:
444 case RISCV::PseudoCALLIndirectLpadAlign:
445 emitLpadAlignedCall(*
MI);
450 lowerToMCInst(
MI, OutInst);
451 EmitToStreamer(*OutStreamer, OutInst);
454bool RISCVAsmPrinter::PrintAsmOperand(
const MachineInstr *
MI,
unsigned OpNo,
455 const char *ExtraCode, raw_ostream &OS) {
460 const MachineOperand &MO =
MI->getOperand(OpNo);
461 if (ExtraCode && ExtraCode[0]) {
462 if (ExtraCode[1] != 0)
465 switch (ExtraCode[0]) {
483 OS <<
TRI->getEncodingValue(MO.
getReg());
497 PrintSymbolOperand(MO, OS);
511bool RISCVAsmPrinter::PrintAsmMemoryOperand(
const MachineInstr *
MI,
513 const char *ExtraCode,
518 const MachineOperand &AddrReg =
MI->getOperand(OpNo);
519 assert(
MI->getNumOperands() > OpNo + 1 &&
"Expected additional operand");
520 const MachineOperand &
Offset =
MI->getOperand(OpNo + 1);
523 if (!AddrReg.
isReg())
530 if (!lowerOperand(
Offset, MCO))
536 MAI.printExpr(OS, *MCO.
getExpr());
539 MMI->getContext().registerInlineAsmLabel(
Offset.getMCSymbol());
540 if (
Offset.isBlockAddress()) {
542 MCSymbol *Sym = GetBlockAddressSymbol(BA);
543 MMI->getContext().registerInlineAsmLabel(Sym);
551bool RISCVAsmPrinter::emitTargetFeaturePush(
const MCSubtargetInfo &STI) {
552 RISCVTargetStreamer &RTS = getTargetStreamer();
554 const MCSubtargetInfo &MCSTI = TM.getMCSubtargetInfo();
562 auto Delta = STI.
hasFeature(Feature.Value) ? RISCVOptionArchArgType::Plus
563 : RISCVOptionArchArgType::Minus;
564 StringRef ExtName = Feature.key();
568 if (!NeedEmitStdOptionArgs.
empty()) {
579void RISCVAsmPrinter::emitTargetFeaturePop(
const MCSubtargetInfo &STI,
582 getTargetStreamer().emitDirectiveOptionPop();
588 bool EmittedOptionArch = emitTargetFeaturePush(*STI);
590 SetupMachineFunction(MF);
596 emitTargetFeaturePop(*STI, EmittedOptionArch);
600void RISCVAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(
const MachineInstr *
MI) {
601 emitSled(
MI, SledKind::FUNCTION_ENTER);
604void RISCVAsmPrinter::LowerPATCHABLE_FUNCTION_EXIT(
const MachineInstr *
MI) {
605 emitSled(
MI, SledKind::FUNCTION_EXIT);
608void RISCVAsmPrinter::LowerPATCHABLE_TAIL_CALL(
const MachineInstr *
MI) {
609 emitSled(
MI, SledKind::TAIL_CALL);
612void RISCVAsmPrinter::emitSled(
const MachineInstr *
MI, SledKind Kind) {
627 const uint8_t NoopsInSledCount = STI->is64Bit() ? 33 : 21;
630 auto CurSled = OutContext.createTempSymbol(
"xray_sled_",
true);
632 auto Target = OutContext.createTempSymbol();
640 MCInstBuilder(RISCV::JAL).addReg(RISCV::X0).addExpr(TargetExpr));
643 for (int8_t
I = 0;
I < NoopsInSledCount; ++
I)
644 EmitToStreamer(*OutStreamer, MCInstBuilder(RISCV::ADDI)
650 recordSled(CurSled, *
MI, Kind, 2);
653void RISCVAsmPrinter::emitStartOfAsmFile(
Module &M) {
655 "target streamer is uninitialized");
656 RISCVTargetStreamer &RTS = getTargetStreamer();
657 StringRef ABIName =
M.getTargetABIFromMD();
658 if (!ABIName.
empty()) {
660 if (ABI == RISCVABI::ABI::Unknown) {
661 M.getContext().emitError(Twine(
'\'') + ABIName +
662 "' is not a recognized ABI for this target");
671 MCSubtargetInfo SubtargetInfo = TM.getMCSubtargetInfo();
675 for (
auto &ISA : MD->operands()) {
678 ISAString->getString(),
true,
681 auto &ISAInfo = *ParseResult;
683 if (ISAInfo->hasExtension(Feature.key()) &&
694 if (
M.getTargetTriple().isOSBinFormatELF())
698void RISCVAsmPrinter::emitEndOfAsmFile(
Module &M) {
699 RISCVTargetStreamer &RTS = getTargetStreamer();
701 if (
M.getTargetTriple().isOSBinFormatELF()) {
703 emitNoteGnuProperty(M);
705 EmitHwasanMemaccessSymbols(M);
708void RISCVAsmPrinter::emitFunctionEntryLabel() {
709 const auto *RMFI = MF->getInfo<RISCVMachineFunctionInfo>();
710 if (RMFI->isVectorCall()) {
711 RISCVTargetStreamer &RTS = getTargetStreamer();
726void RISCVAsmPrinter::LowerHWASAN_CHECK_MEMACCESS(
const MachineInstr &
MI) {
728 uint32_t AccessInfo =
MI.getOperand(1).getImm();
730 HwasanMemaccessSymbols[HwasanMemaccessTuple(
Reg, AccessInfo)];
733 if (!TM.getTargetTriple().isOSBinFormatELF())
736 std::string SymName =
"__hwasan_check_x" +
utostr(
Reg - RISCV::X0) +
"_" +
737 utostr(AccessInfo) +
"_short";
738 Sym = OutContext.getOrCreateSymbol(SymName);
743 EmitToStreamer(*OutStreamer, MCInstBuilder(RISCV::PseudoCALL).addExpr(Expr));
746void RISCVAsmPrinter::LowerKCFI_CHECK(
const MachineInstr &
MI) {
748 assert(std::next(
MI.getIterator())->isCall() &&
749 "KCFI_CHECK not followed by a call instruction");
750 assert(std::next(
MI.getIterator())->getOperand(0).getReg() == AddrReg &&
751 "KCFI_CHECK call target doesn't match call operand");
758 MCRegister ScratchRegs[] = {RISCV::X6, RISCV::X7};
759 static const MCPhysReg FallbackRegArray[] = {RISCV::X28, RISCV::X29,
760 RISCV::X30, RISCV::X31};
762 auto isRegAvailable = [&](MCRegister
Reg) {
765 if (
Reg == RISCV::X7 &&
MI.getMF()
766 ->getInfo<RISCVMachineFunctionInfo>()
767 ->hasCFProtectionBranch())
769 return AddrReg !=
Reg && !STI->isRegisterReservedByUser(
Reg);
771 for (
auto &
Reg : ScratchRegs) {
772 if (isRegAvailable(
Reg))
775 if (It == FallbackRegs.end())
779 FallbackRegs = FallbackRegs.drop_front(It - FallbackRegs.begin() + 1);
782 if (AddrReg == RISCV::X0) {
785 EmitToStreamer(*OutStreamer, MCInstBuilder(RISCV::ADDI)
786 .addReg(ScratchRegs[0])
792 int NopSize = STI->hasStdExtZca() ? 2 : 4;
794 MI.getMF()->getFunction().getFnAttributeAsParsedInteger(
795 "patchable-function-prefix");
798 EmitToStreamer(*OutStreamer, MCInstBuilder(RISCV::LW)
799 .addReg(ScratchRegs[0])
801 .addImm(-(PrefixNops * NopSize + 4)));
805 const int64_t
Type =
MI.getOperand(1).getImm();
806 const int64_t Hi20 = ((
Type + 0x800) >> 12) & 0xFFFFF;
811 MCInstBuilder(RISCV::LUI).addReg(ScratchRegs[1]).addImm(Hi20));
813 if (Lo12 || Hi20 == 0) {
814 EmitToStreamer(*OutStreamer,
815 MCInstBuilder((STI->
hasFeature(RISCV::Feature64Bit) && Hi20)
818 .addReg(ScratchRegs[1])
819 .addReg(ScratchRegs[1])
825 EmitToStreamer(*OutStreamer,
826 MCInstBuilder(RISCV::BEQ)
827 .addReg(ScratchRegs[0])
828 .addReg(ScratchRegs[1])
833 EmitToStreamer(*OutStreamer, MCInstBuilder(RISCV::EBREAK));
834 emitKCFITrapEntry(*
MI.getMF(),
Trap);
838void RISCVAsmPrinter::EmitHwasanMemaccessSymbols(
Module &M) {
839 if (HwasanMemaccessSymbols.empty())
842 assert(
M.getTargetTriple().isOSBinFormatELF());
846 const MCSubtargetInfo &MCSTI = TM.getMCSubtargetInfo();
849 OutContext.getOrCreateSymbol(
"__hwasan_tag_mismatch_v2");
852 RISCVTargetStreamer &RTS = getTargetStreamer();
855 const MCSymbolRefExpr *HwasanTagMismatchV2Ref =
860 for (
auto &
P : HwasanMemaccessSymbols) {
861 unsigned Reg = std::get<0>(
P.first);
862 uint32_t AccessInfo = std::get<1>(
P.first);
880 MCInstBuilder(RISCV::SLLI).addReg(RISCV::X6).addReg(
Reg).addImm(8),
882 EmitToStreamer(*OutStreamer,
883 MCInstBuilder(RISCV::SRLI)
889 EmitToStreamer(*OutStreamer,
890 MCInstBuilder(RISCV::ADD)
897 MCInstBuilder(RISCV::LBU).addReg(RISCV::X6).addReg(RISCV::X6).addImm(0),
902 MCInstBuilder(RISCV::SRLI).addReg(RISCV::X7).addReg(
Reg).addImm(56),
904 MCSymbol *HandleMismatchOrPartialSym = OutContext.createTempSymbol();
906 EmitToStreamer(*OutStreamer,
907 MCInstBuilder(RISCV::BNE)
911 HandleMismatchOrPartialSym, OutContext)),
913 MCSymbol *ReturnSym = OutContext.createTempSymbol();
915 EmitToStreamer(*OutStreamer,
916 MCInstBuilder(RISCV::JALR)
921 OutStreamer->
emitLabel(HandleMismatchOrPartialSym);
923 EmitToStreamer(*OutStreamer,
924 MCInstBuilder(RISCV::ADDI)
929 MCSymbol *HandleMismatchSym = OutContext.createTempSymbol();
932 MCInstBuilder(RISCV::BGEU)
940 MCInstBuilder(RISCV::ANDI).addReg(RISCV::X28).addReg(
Reg).addImm(0xF),
944 EmitToStreamer(*OutStreamer,
945 MCInstBuilder(RISCV::ADDI)
952 MCInstBuilder(RISCV::BGE)
960 MCInstBuilder(RISCV::ORI).addReg(RISCV::X6).addReg(
Reg).addImm(0xF),
964 MCInstBuilder(RISCV::LBU).addReg(RISCV::X6).addReg(RISCV::X6).addImm(0),
966 EmitToStreamer(*OutStreamer,
967 MCInstBuilder(RISCV::BEQ)
973 OutStreamer->
emitLabel(HandleMismatchSym);
1010 EmitToStreamer(*OutStreamer,
1011 MCInstBuilder(RISCV::ADDI)
1018 EmitToStreamer(*OutStreamer,
1019 MCInstBuilder(RISCV::SD)
1025 EmitToStreamer(*OutStreamer,
1026 MCInstBuilder(RISCV::SD)
1035 MCInstBuilder(RISCV::SD).addReg(RISCV::X8).addReg(RISCV::X2).addImm(8 *
1041 MCInstBuilder(RISCV::SD).addReg(RISCV::X1).addReg(RISCV::X2).addImm(1 *
1044 if (
Reg != RISCV::X10)
1047 MCInstBuilder(RISCV::ADDI).addReg(RISCV::X10).addReg(
Reg).addImm(0),
1049 EmitToStreamer(*OutStreamer,
1050 MCInstBuilder(RISCV::ADDI)
1056 EmitToStreamer(*OutStreamer, MCInstBuilder(RISCV::PseudoCALL).addExpr(Expr),
1061void RISCVAsmPrinter::emitNoteGnuProperty(
const Module &M) {
1062 assert(
M.getTargetTriple().isOSBinFormatELF() &&
"invalid binary format");
1063 uint32_t GnuProps = 0;
1064 if (
const Metadata *
const Flag =
M.getModuleFlag(
"cf-protection-return");
1068 if (
const Metadata *
const Flag =
M.getModuleFlag(
"cf-protection-branch");
1070 using namespace llvm::RISCVISAUtils;
1071 const Metadata *
const CFBranchLabelSchemeFlag =
1072 M.getModuleFlag(
"cf-branch-label-scheme");
1073 assert(CFBranchLabelSchemeFlag &&
1074 "cf-protection=branch should come with cf-branch-label-scheme=... "
1075 "on RISC-V targets");
1076 const StringRef CFBranchLabelScheme =
1095 auto &RTS =
static_cast<RISCVTargetELFStreamer &
>(getTargetStreamer());
1096 RTS.emitNoteGnuPropertySection(GnuProps);
1117 Kind = ELF::R_RISCV_HI20;
1132 Kind = ELF::R_RISCV_TPREL_HI20;
1135 Kind = ELF::R_RISCV_TPREL_ADD;
1138 Kind = ELF::R_RISCV_TLS_GOT_HI20;
1141 Kind = ELF::R_RISCV_TLS_GD_HI20;
1144 Kind = ELF::R_RISCV_TLSDESC_HI20;
1147 Kind = ELF::R_RISCV_TLSDESC_LOAD_LO12;
1150 Kind = ELF::R_RISCV_TLSDESC_ADD_LO12;
1153 Kind = ELF::R_RISCV_TLSDESC_CALL;
1171bool RISCVAsmPrinter::lowerOperand(
const MachineOperand &MO,
1172 MCOperand &MCOp)
const {
1219 RISCVVPseudosTable::getPseudoInfo(
MI->getOpcode());
1227 assert(
TRI &&
"TargetRegisterInfo expected");
1231 unsigned NumOps =
MI->getNumExplicitOperands();
1250 bool hasVLOutput = RISCVInstrInfo::isFaultOnlyFirstLoad(*
MI);
1251 for (
unsigned OpNo = 0; OpNo !=
NumOps; ++OpNo) {
1254 if (hasVLOutput && OpNo == 1)
1258 if (OpNo ==
MI->getNumExplicitDefs() && MO.
isReg() && MO.
isTied()) {
1260 "Expected tied to first def.");
1280 Reg =
TRI->getSubReg(
Reg, RISCV::sub_vrm1_0);
1281 assert(
Reg &&
"Subregister does not exist");
1284 TRI->getMatchingSuperReg(
Reg, RISCV::sub_16, &RISCV::FPR32RegClass);
1285 assert(
Reg &&
"Subregister does not exist");
1287 Reg =
TRI->getSubReg(
Reg, RISCV::sub_32);
1288 assert(
Reg &&
"Superregister does not exist");
1300 Reg =
TRI->getSubReg(
Reg, RISCV::sub_vrm1_0);
1301 assert(
Reg &&
"Subregister does not exist");
1320 "Expected only mask operand to be missing");
1328void RISCVAsmPrinter::lowerToMCInst(
const MachineInstr *
MI, MCInst &OutMI) {
1334 for (
const MachineOperand &MO :
MI->operands()) {
1336 if (lowerOperand(MO, MCOp))
1341void RISCVAsmPrinter::emitMachineConstantPoolValue(
1342 MachineConstantPoolValue *MCPV) {
1343 auto *RCPV =
static_cast<RISCVConstantPoolValue *
>(MCPV);
1346 if (RCPV->isGlobalValue()) {
1347 auto *GV = RCPV->getGlobalValue();
1348 MCSym = getSymbol(GV);
1350 assert(RCPV->isExtSymbol() &&
"unrecognized constant pool type");
1351 auto Sym = RCPV->getSymbol();
1352 MCSym = GetExternalSymbolSymbol(Sym);
1356 uint64_t Size = getDataLayout().getTypeAllocSize(RCPV->getType());
1361RISCVAsmPrinter::getRequiredGlobalAlignmentGranule(
const GlobalVariable &GV) {
1362 const MCSubtargetInfo &MCSTI = TM.getMCSubtargetInfo();
1364 return std::nullopt;
1371 return std::nullopt;
1374 if (MCSTI.
hasFeature(RISCV::FeatureVendorXCheriot))
1375 return CHERIoTCapabilityFormat::getRequiredAlignment(
Size);
1377 if (MCSTI.
hasFeature(RISCV::FeatureStdExtY)) {
1384 return std::nullopt;
1387char RISCVAsmPrinter::ID = 0;
1394 RISCVAsmPrinter &
AsmPrinter =
static_cast<RISCVAsmPrinter &
>(
1404 RISCVAsmPrinter &
AsmPrinter =
static_cast<RISCVAsmPrinter &
>(
1415 RISCVAsmPrinter &
AsmPrinter =
static_cast<RISCVAsmPrinter &
>(
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...
static const Function * getParent(const Value *V)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_EXTERNAL_VISIBILITY
const HexagonInstrInfo * TII
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)
print mir2vec MIR2Vec Vocabulary Printer Pass
Machine Check Debug Module
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register const TargetRegisterInfo * TRI
Promote Memory to Register
ModuleAnalysisManager MAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
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)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
std::unique_ptr< MCStreamer > && Streamer
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
This class is intended to be used as a driving class for all asm writers.
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
MCContext & OutContext
This is the context for the output file that we are streaming.
bool doFinalization(Module &M) override
Shut down the asmprinter.
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
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.
bool hasSection() const
Check if this global has a custom object file section.
Type * getValueType() const
LLVM_ABI uint64_t getGlobalSize(const DataLayout &DL) const
Get the size of this global variable in bytes.
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Context object for machine code objects.
Base class for the full range of assembler expressions which are needed for parsing.
Instances of this class represent a single low-level machine instruction.
unsigned getNumOperands() const
unsigned getOpcode() const
void addOperand(const MCOperand Op)
void setOpcode(unsigned Op)
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.
static MCOperand createExpr(const MCExpr *Val)
static MCOperand createReg(MCRegister Reg)
static MCOperand createImm(int64_t Val)
const MCExpr * getExpr() const
static const MCSpecifierExpr * create(const MCExpr *Expr, Spec S, MCContext &Ctx, SMLoc Loc=SMLoc())
Streaming machine code generation interface.
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.
virtual void emitCodeAlignment(Align Alignment, const MCSubtargetInfo &STI, unsigned MaxBytesToEmit=0)
Emit nops until the byte alignment ByteAlignment is reached.
virtual bool hasRawTextSupport() const
Return true if this asm streamer supports emitting unformatted text to the .s file with EmitRawText.
MCContext & getContext() const
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()
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
const FeatureBitset & ToggleFeature(uint64_t FB)
Toggle a feature and return the re-computed feature bits.
ArrayRef< SubtargetFeatureKV > getAllProcessorFeatures() const
Return processor features.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
StringRef getName() const
getName - Get the symbol name.
MachineInstrBundleIterator< const MachineInstr > const_iterator
LLVM_ABI MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
bool isNonTemporal() const
Flags getFlags() const
Return the raw flags of the source value,.
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() 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.
A Module instance is used to store all the information related to an LLVM module.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
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(const RISCVMCOptions &CLOpts, MCRegister Reg)
bool requiresNTLHint(const MachineInstr &MI) const
Return true if the instruction requires an NTL hint to be emitted.
const RISCVRegisterInfo * getRegisterInfo() const override
const RISCVInstrInfo * getInstrInfo() const override
virtual void setArchString(StringRef Arch)
virtual void emitDirectiveVariantCC(MCSymbol &Symbol)
void emitTargetAttributes(const MCSubtargetInfo &STI, bool EmitStackAlign)
virtual void setFlagsFromFeatures(const MCSubtargetInfo &STI)
virtual void emitDirectiveOptionExact()
virtual void emitDirectiveOptionPop()
void setTargetABI(RISCVABI::ABI ABI)
virtual void emitDirectiveOptionArch(ArrayRef< RISCVOptionArchArg > Args)
virtual void finishAttributeSection()
bool hasTargetABI() const
virtual void emitDirectiveOptionPush()
Wrapper class representing virtual and physical registers.
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.
std::string str() const
Get the contents as an std::string.
constexpr bool empty() const
Check if the string is empty.
bool consume_front(char Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
TargetInstrInfo - Interface to description of machine instruction set.
Primary interface to the complete machine description for the target machine.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool isSized() const
Return true if it makes sense to take the size of this type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED
@ GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS
ABI getTargetABI(StringRef ABIName)
Expected< ABI > computeTargetABI(const MCSubtargetInfo &STI, StringRef ABIName)
ZicfilpLabelSchemeKind getZicfilpLabelScheme(const StringRef CFBranchLabelScheme)
llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseFeatureBits(const MCSubtargetInfo &STI)
static bool hasRoundModeOp(uint64_t TSFlags)
static bool hasTWidenOp(uint64_t TSFlags)
static bool isTiedPseudo(uint64_t TSFlags)
static bool hasTKOp(uint64_t TSFlags)
static bool hasVLOp(uint64_t TSFlags)
static bool hasTMOp(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)
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.
This is an optimization pass for GlobalISel generic memory operations.
bool errorToBool(Error Err)
Helper for converting an Error to a bool.
static const MachineMemOperand::Flags MONontemporalBit1
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
Target & getTheRISCV32Target()
static const MachineMemOperand::Flags MONontemporalBit0
std::string utostr(uint64_t X, bool isNeg=false)
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
Target & getTheRISCV64beTarget()
LLVM_ABI void reportFatalInternalError(Error Err)
Report a fatal error that indicates a bug in LLVM.
auto dyn_cast_or_null(const Y &Val)
LLVM_ABI void setupModuleAsmPrinter(Module &M, ModuleAnalysisManager &MAM, AsmPrinter &AsmPrinter)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Target & getTheRISCV64Target()
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI void setupMachineFunctionAsmPrinter(MachineFunctionAnalysisManager &MFAM, MachineFunction &MF, AsmPrinter &AsmPrinter)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
@ MCSA_ELF_TypeFunction
.type _foo, STT_FUNC # aka @function
@ MCSA_Hidden
.hidden (ELF)
Target & getTheRISCV32beTarget()
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Implement std::hash so that hash_code can be used in STL containers.
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...