36 const MCRegisterInfo &MRI;
37 const MCInstrInfo &MCII;
40 AMDGPUMCCodeEmitter(
const MCInstrInfo &MCII,
const MCRegisterInfo &MRI)
41 : MRI(MRI), MCII(MCII) {}
44 void encodeInstruction(
const MCInst &
MI, SmallVectorImpl<char> &CB,
45 SmallVectorImpl<MCFixup> &Fixups,
46 const MCSubtargetInfo &STI)
const override;
48 void getMachineOpValue(
const MCInst &
MI,
const MCOperand &MO, APInt &
Op,
49 SmallVectorImpl<MCFixup> &Fixups,
50 const MCSubtargetInfo &STI)
const;
52 void getMachineOpValueT16(
const MCInst &
MI,
unsigned OpNo, APInt &
Op,
53 SmallVectorImpl<MCFixup> &Fixups,
54 const MCSubtargetInfo &STI)
const;
56 void getMachineOpValueT16Lo128(
const MCInst &
MI,
unsigned OpNo, APInt &
Op,
57 SmallVectorImpl<MCFixup> &Fixups,
58 const MCSubtargetInfo &STI)
const;
60 void getMachineOpValueRsrcRegOp(
const MCInst &
MI,
unsigned OpNo, APInt &
Op,
61 SmallVectorImpl<MCFixup> &Fixups,
62 const MCSubtargetInfo &STI)
const;
66 void getSOPPBrEncoding(
const MCInst &
MI,
unsigned OpNo, APInt &
Op,
67 SmallVectorImpl<MCFixup> &Fixups,
68 const MCSubtargetInfo &STI)
const;
70 void getSMEMOffsetEncoding(
const MCInst &
MI,
unsigned OpNo, APInt &
Op,
71 SmallVectorImpl<MCFixup> &Fixups,
72 const MCSubtargetInfo &STI)
const;
75 void getSMEMOffsetPCRelEncoding(
const MCInst &
MI,
unsigned OpNo, APInt &
Op,
76 SmallVectorImpl<MCFixup> &Fixups,
77 const MCSubtargetInfo &STI)
const;
79 void getSDWASrcEncoding(
const MCInst &
MI,
unsigned OpNo, APInt &
Op,
80 SmallVectorImpl<MCFixup> &Fixups,
81 const MCSubtargetInfo &STI)
const;
83 void getSDWAVopcDstEncoding(
const MCInst &
MI,
unsigned OpNo, APInt &
Op,
84 SmallVectorImpl<MCFixup> &Fixups,
85 const MCSubtargetInfo &STI)
const;
87 void getAVOperandEncoding(
const MCInst &
MI,
unsigned OpNo, APInt &
Op,
88 SmallVectorImpl<MCFixup> &Fixups,
89 const MCSubtargetInfo &STI)
const;
92 uint64_t getImplicitOpSelHiEncoding(
int Opcode)
const;
93 void getMachineOpValueCommon(
const MCInst &
MI,
const MCOperand &MO,
94 unsigned OpNo, APInt &
Op,
95 SmallVectorImpl<MCFixup> &Fixups,
96 const MCSubtargetInfo &STI)
const;
99 std::optional<uint64_t>
100 getLitEncoding(
const MCInstrDesc &
Desc,
const MCOperand &MO,
unsigned OpNo,
101 const MCSubtargetInfo &STI,
102 bool HasMandatoryLiteral =
false)
const;
104 void getBinaryCodeForInstr(
const MCInst &
MI, SmallVectorImpl<MCFixup> &Fixups,
105 APInt &Inst, APInt &Scratch,
106 const MCSubtargetInfo &STI)
const;
108 template <
bool HasSrc0,
bool HasSrc1,
bool HasSrc2>
109 APInt postEncodeVOP3(
const MCInst &
MI, APInt EncodedValue,
110 const MCSubtargetInfo &STI)
const;
112 APInt postEncodeVOPCX(
const MCInst &
MI, APInt EncodedValue,
113 const MCSubtargetInfo &STI)
const;
120 return new AMDGPUMCCodeEmitter(MCII, *Ctx.getRegisterInfo());
130template <
typename IntTy>
132 if (
Imm >= 0 &&
Imm <= 64)
135 if (
Imm >= -16 &&
Imm <= -1)
136 return 192 + std::abs(
Imm);
171 STI.
hasFeature(AMDGPU::FeatureInv2PiInlineImm))
184 case 0x3F00:
return 240;
185 case 0xBF00:
return 241;
186 case 0x3F80:
return 242;
187 case 0xBF80:
return 243;
188 case 0x4000:
return 244;
189 case 0xC000:
return 245;
190 case 0x4080:
return 246;
191 case 0xC080:
return 247;
192 case 0x3E22:
return 248;
227 if (Val == 0x3e22f983 &&
228 STI.
hasFeature(AMDGPU::FeatureInv2PiInlineImm))
268 if (Val == 0x3fc45f306dc9c882 &&
269 STI.
hasFeature(AMDGPU::FeatureInv2PiInlineImm))
274 bool CanUse64BitLiterals = STI.
hasFeature(AMDGPU::Feature64BitLiterals) &&
277 return CanUse64BitLiterals &&
Lo_32(Val) ? 254 : 255;
284std::optional<uint64_t> AMDGPUMCCodeEmitter::getLitEncoding(
287 const MCOperandInfo &OpInfo =
Desc.operands()[OpNo];
296 if (STI.
hasFeature(AMDGPU::Feature64BitLiterals) &&
335 return (HasMandatoryLiteral && Enc == 255) ? 254 : Enc;
392uint64_t AMDGPUMCCodeEmitter::getImplicitOpSelHiEncoding(
int Opcode)
const {
393 using namespace AMDGPU::VOP3PEncoding;
406void AMDGPUMCCodeEmitter::encodeInstruction(
const MCInst &
MI,
407 SmallVectorImpl<char> &CB,
408 SmallVectorImpl<MCFixup> &Fixups,
409 const MCSubtargetInfo &STI)
const {
410 int Opcode =
MI.getOpcode();
411 APInt Encoding, Scratch;
412 getBinaryCodeForInstr(
MI, Fixups, Encoding, Scratch, STI);
413 const MCInstrDesc &
Desc = MCII.
get(
MI.getOpcode());
414 unsigned bytes =
Desc.getSize();
419 Opcode == AMDGPU::V_ACCVGPR_WRITE_B32_vi) &&
429 Encoding |= getImplicitOpSelHiEncoding(Opcode);
432 for (
unsigned i = 0; i < bytes; i++) {
438 int vaddr0 = AMDGPU::getNamedOperandIdx(
MI.getOpcode(),
439 AMDGPU::OpName::vaddr0);
440 int srsrc = AMDGPU::getNamedOperandIdx(
MI.getOpcode(),
441 AMDGPU::OpName::srsrc);
442 assert(vaddr0 >= 0 && srsrc > vaddr0);
443 unsigned NumExtraAddrs = srsrc - vaddr0 - 1;
444 unsigned NumPadding = ~(NumExtraAddrs - 1u) & 3;
446 for (
unsigned i = 0; i < NumExtraAddrs; ++i) {
447 getMachineOpValue(
MI,
MI.getOperand(vaddr0 + 1 + i), Encoding, Fixups,
454 if ((bytes > 8 && STI.
hasFeature(AMDGPU::FeatureVOP3Literal)) ||
455 (bytes > 4 && !STI.
hasFeature(AMDGPU::FeatureVOP3Literal)))
463 for (
unsigned i = 0, e =
Desc.getNumOperands(); i < e; ++i) {
470 const MCOperand &
Op =
MI.getOperand(i);
471 auto Enc = getLitEncoding(
Desc,
Op, i, STI);
472 if (!Enc || (*Enc != 255 && *Enc != 254))
481 else if (
Op.isExpr()) {
506void AMDGPUMCCodeEmitter::getSOPPBrEncoding(
const MCInst &
MI,
unsigned OpNo,
508 SmallVectorImpl<MCFixup> &Fixups,
509 const MCSubtargetInfo &STI)
const {
510 const MCOperand &MO =
MI.getOperand(OpNo);
513 const MCExpr *Expr = MO.
getExpr();
517 getMachineOpValue(
MI, MO,
Op, Fixups, STI);
521void AMDGPUMCCodeEmitter::getSMEMOffsetEncoding(
522 const MCInst &
MI,
unsigned OpNo, APInt &
Op,
523 SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI)
const {
530void AMDGPUMCCodeEmitter::getSMEMOffsetPCRelEncoding(
531 const MCInst &
MI,
unsigned OpNo, APInt &
Op,
532 SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI)
const {
533 const MCOperand &MO =
MI.getOperand(OpNo);
544 getSMEMOffsetEncoding(
MI, OpNo,
Op, Fixups, STI);
547void AMDGPUMCCodeEmitter::getSDWASrcEncoding(
const MCInst &
MI,
unsigned OpNo,
549 SmallVectorImpl<MCFixup> &Fixups,
550 const MCSubtargetInfo &STI)
const {
551 using namespace AMDGPU::SDWA;
555 const MCOperand &MO =
MI.getOperand(OpNo);
559 RegEnc |=
MRI.getEncodingValue(
Reg);
560 RegEnc &= SDWA9EncValues::SRC_VGPR_MASK;
562 RegEnc |= SDWA9EncValues::SRC_SGPR_MASK;
567 const MCInstrDesc &
Desc = MCII.
get(
MI.getOpcode());
568 auto Enc = getLitEncoding(
Desc, MO, OpNo, STI);
569 if (Enc && *Enc != 255) {
570 Op = *Enc | SDWA9EncValues::SRC_SGPR_MASK;
578void AMDGPUMCCodeEmitter::getSDWAVopcDstEncoding(
579 const MCInst &
MI,
unsigned OpNo, APInt &
Op,
580 SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI)
const {
581 using namespace AMDGPU::SDWA;
585 const MCOperand &MO =
MI.getOperand(OpNo);
588 if (
Reg != AMDGPU::VCC &&
Reg != AMDGPU::VCC_LO) {
589 RegEnc |=
MRI.getEncodingValue(
Reg);
590 RegEnc &= SDWA9EncValues::VOPC_DST_SGPR_MASK;
591 RegEnc |= SDWA9EncValues::VOPC_DST_VCC_MASK;
596void AMDGPUMCCodeEmitter::getAVOperandEncoding(
597 const MCInst &
MI,
unsigned OpNo, APInt &
Op,
598 SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI)
const {
599 MCRegister
Reg =
MI.getOperand(OpNo).getReg();
600 unsigned Enc =
MRI.getEncodingValue(
Reg);
610 Op =
Idx | (IsVGPROrAGPR << 8) | (IsAGPR << 9);
637void AMDGPUMCCodeEmitter::getMachineOpValue(
const MCInst &
MI,
638 const MCOperand &MO, APInt &
Op,
639 SmallVectorImpl<MCFixup> &Fixups,
640 const MCSubtargetInfo &STI)
const {
642 unsigned Enc =
MRI.getEncodingValue(MO.
getReg());
646 Op =
Idx | (IsVGPROrAGPR << 8);
649 unsigned OpNo = &MO -
MI.begin();
650 getMachineOpValueCommon(
MI, MO, OpNo,
Op, Fixups, STI);
653void AMDGPUMCCodeEmitter::getMachineOpValueT16(
654 const MCInst &
MI,
unsigned OpNo, APInt &
Op,
655 SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI)
const {
656 const MCOperand &MO =
MI.getOperand(OpNo);
658 unsigned Enc =
MRI.getEncodingValue(MO.
getReg());
661 Op =
Idx | (IsVGPR << 8);
664 getMachineOpValueCommon(
MI, MO, OpNo,
Op, Fixups, STI);
671 if ((
int)OpNo == AMDGPU::getNamedOperandIdx(
MI.getOpcode(),
672 AMDGPU::OpName::src0_modifiers)) {
673 SrcMOIdx = AMDGPU::getNamedOperandIdx(
MI.getOpcode(), AMDGPU::OpName::src0);
675 AMDGPU::getNamedOperandIdx(
MI.getOpcode(), AMDGPU::OpName::vdst);
676 if (VDstMOIdx != -1) {
677 auto DstReg =
MI.getOperand(VDstMOIdx).getReg();
681 }
else if ((
int)OpNo == AMDGPU::getNamedOperandIdx(
682 MI.getOpcode(), AMDGPU::OpName::src1_modifiers))
683 SrcMOIdx = AMDGPU::getNamedOperandIdx(
MI.getOpcode(), AMDGPU::OpName::src1);
684 else if ((
int)OpNo == AMDGPU::getNamedOperandIdx(
685 MI.getOpcode(), AMDGPU::OpName::src2_modifiers))
686 SrcMOIdx = AMDGPU::getNamedOperandIdx(
MI.getOpcode(), AMDGPU::OpName::src2);
690 const MCOperand &SrcMO =
MI.getOperand(SrcMOIdx);
693 auto SrcReg = SrcMO.
getReg();
700void AMDGPUMCCodeEmitter::getMachineOpValueT16Lo128(
701 const MCInst &
MI,
unsigned OpNo, APInt &
Op,
702 SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI)
const {
703 const MCOperand &MO =
MI.getOperand(OpNo);
705 uint16_t Encoding =
MRI.getEncodingValue(MO.
getReg());
710 Op = (IsVGPR ? 0x100 : 0) | (IsHi ? 0x80 : 0) | RegIdx;
713 getMachineOpValueCommon(
MI, MO, OpNo,
Op, Fixups, STI);
719void AMDGPUMCCodeEmitter::getMachineOpValueRsrcRegOp(
720 const MCInst &
MI,
unsigned OpNo, APInt &
Op,
721 SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI)
const {
722 const MCOperand &MO =
MI.getOperand(OpNo);
725 unsigned Enc =
MRI.getEncodingValue(MO.
getReg());
729 Op =
Idx | IsVGPROrAGPR << 8 | IsSReg32 << 7;
732void AMDGPUMCCodeEmitter::getMachineOpValueCommon(
733 const MCInst &
MI,
const MCOperand &MO,
unsigned OpNo, APInt &
Op,
734 SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI)
const {
735 bool isLikeImm =
false;
741 }
else if (MO.
isExpr() && MO.
getExpr()->evaluateAsAbsolute(Val)) {
755 const MCInstrDesc &
Desc = MCII.
get(
MI.getOpcode());
763 const MCInstrDesc &
Desc = MCII.
get(
MI.getOpcode());
765 bool HasMandatoryLiteral =
767 if (
auto Enc = getLitEncoding(
Desc, MO, OpNo, STI, HasMandatoryLiteral)) {
783template <
bool HasSrc0,
bool HasSrc1,
bool HasSrc2>
784APInt AMDGPUMCCodeEmitter::postEncodeVOP3(
const MCInst &
MI, APInt EncodedValue,
785 const MCSubtargetInfo &STI)
const {
790 constexpr uint64_t InlineImmediate0 = 0x80;
792 EncodedValue |= InlineImmediate0 << 32;
794 EncodedValue |= InlineImmediate0 << 41;
796 EncodedValue |= InlineImmediate0 << 50;
800APInt AMDGPUMCCodeEmitter::postEncodeVOPCX(
const MCInst &
MI, APInt EncodedValue,
801 const MCSubtargetInfo &STI)
const {
810 EncodedValue |=
MRI.getEncodingValue(AMDGPU::EXEC_LO) &
812 return postEncodeVOP3<true, true, false>(
MI, EncodedValue, STI);
815#include "AMDGPUGenMCCodeEmitter.inc"
static void addFixup(SmallVectorImpl< MCFixup > &Fixups, uint32_t Offset, const MCExpr *Value, uint16_t Kind, bool PCRel=false)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static uint32_t getLit64Encoding(const MCInstrDesc &Desc, uint64_t Val, const MCSubtargetInfo &STI, bool IsFP)
static uint32_t getLit16IntEncoding(uint32_t Val, const MCSubtargetInfo &STI)
static void addFixup(SmallVectorImpl< MCFixup > &Fixups, uint32_t Offset, const MCExpr *Value, uint16_t Kind, bool PCRel=false)
static uint32_t getLitBF16Encoding(uint16_t Val)
static uint32_t getLit16Encoding(uint16_t Val, const MCSubtargetInfo &STI)
static uint32_t getIntInlineImmEncoding(IntTy Imm)
static bool needsPCRel(const MCExpr *Expr)
static uint32_t getLit32Encoding(uint32_t Val, const MCSubtargetInfo &STI)
Provides AMDGPU specific target descriptions.
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
MachineRegisterInfo & MRI
LLVM_ABI uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
MCCodeEmitter - Generic instruction encoding interface.
Context object for machine code objects.
Base class for the full range of assembler expressions which are needed for parsing.
@ Unary
Unary expressions.
@ Constant
Constant expressions.
@ SymbolRef
References to labels and assigned expressions.
@ Target
Target specific expression.
@ Specifier
Expression with a relocation specifier.
@ Binary
Binary expressions.
static MCFixupKind getDataKindForSize(unsigned Size)
Return the generic fixup kind for a value with the given size.
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
Describe properties that are true of each instruction in the target description file.
LLVM_ABI bool hasImplicitDefOfPhysReg(MCRegister Reg, const MCRegisterInfo *MRI=nullptr) const
Return true if this instruction implicitly defines the specified physical register.
Interface to description of machine instruction set.
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
uint8_t OperandType
Information about the type of the operand.
Instances of this class represent operands of the MCInst class.
MCRegister getReg() const
Returns the register number.
const MCExpr * getExpr() const
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
LLVM Value Representation.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isSGPR(MCRegister Reg, const MCRegisterInfo *TRI)
Is Reg - scalar register.
bool isHi16Reg(MCRegister Reg, const MCRegisterInfo &MRI)
static AMDGPUMCExpr::Specifier getSpecifier(const MCSymbolRefExpr *SRE)
LLVM_READONLY bool isLitExpr(const MCExpr *Expr)
@ fixup_si_sopp_br
16-bit PC relative fixup for SOPP branch instructions.
@ fixup_si_smem_pcrel
24-bit PC-relative byte offset for s_prefetch_inst_pc_rel.
LLVM_READONLY bool hasNamedOperand(uint32_t Opcode, OpName NamedIdx)
bool hasNoOpSelHi(unsigned Opc)
Returns true if this VOP3P opcode has no op_sel_hi field, in which case those bits are encoded as zer...
constexpr bool isSISrcOperand(const MCOperandInfo &OpInfo)
Is this an AMDGPU specific source operand?
LLVM_READONLY AMDGPUMCExpr::VariantKind getExprKind(const MCExpr *Expr)
LLVM_READONLY int64_t getLitValue(const MCExpr *Expr)
bool isGFX11Plus(const MCSubtargetInfo &STI)
std::optional< unsigned > getInlineEncodingV2F16(uint32_t Literal)
bool isGFX10Plus(const MCSubtargetInfo &STI)
int64_t encode32BitLiteral(int64_t Imm, OperandType Type, bool IsLit)
@ OPERAND_KIMM32
Operand with 32-bit immediate that uses the constant bus.
@ OPERAND_REG_INLINE_C_FP64
@ OPERAND_REG_IMM_NOINLINE_FP16
@ OPERAND_REG_INLINE_C_BF16
@ OPERAND_REG_INLINE_C_V2BF16
@ OPERAND_REG_IMM_V2INT64
@ OPERAND_REG_IMM_V2INT16
@ OPERAND_REG_IMM_INT32
Operands with register, 32-bit, or 64-bit immediate.
@ OPERAND_REG_IMM_V2FP16_SPLAT
@ OPERAND_REG_INLINE_C_INT64
@ OPERAND_REG_INLINE_C_INT16
Operands with register or inline constant.
@ OPERAND_REG_IMM_NOINLINE_V2FP16
@ OPERAND_REG_INLINE_C_V2FP16
@ OPERAND_REG_INLINE_AC_INT32
Operands with an AccVGPR register or inline constant.
@ OPERAND_REG_INLINE_AC_FP32
@ OPERAND_REG_IMM_V2INT32
@ OPERAND_REG_INLINE_C_FP32
@ OPERAND_REG_INLINE_C_INT32
@ OPERAND_REG_INLINE_C_V2INT16
@ OPERAND_REG_INLINE_AC_FP64
@ OPERAND_REG_INLINE_C_FP16
@ OPERAND_INLINE_SPLIT_BARRIER_INT32
std::optional< unsigned > getPKFMACF16InlineEncoding(uint32_t Literal, bool IsGFX11Plus)
std::optional< unsigned > getInlineEncodingV2I16(uint32_t Literal)
bool isVI(const MCSubtargetInfo &STI)
MCRegister mc2PseudoReg(MCRegister Reg)
Convert hardware register Reg to a pseudo register.
std::optional< unsigned > getInlineEncodingV2BF16(uint32_t Literal)
LLVM_READNONE unsigned getOperandSize(const MCOperandInfo &OpInfo)
constexpr bool isVOP3(const T &...O)
constexpr bool isVOP3P(const T &...O)
constexpr bool isVOP3Like(const T &...O)
constexpr bool isMIMG(const T &...O)
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
This is an optimization pass for GlobalISel generic memory operations.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
To bit_cast(const From &from) noexcept
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
MCCodeEmitter * createAMDGPUMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)