LLVM 23.0.0git
AMDGPUMCCodeEmitter.cpp
Go to the documentation of this file.
1//===-- AMDGPUMCCodeEmitter.cpp - AMDGPU Code Emitter ---------------------===//
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/// \file
10/// The AMDGPU code emitter produces machine code that can be executed
11/// directly on the GPU device.
12//
13//===----------------------------------------------------------------------===//
14
18#include "SIDefines.h"
20#include "llvm/ADT/APInt.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCInstrInfo.h"
29#include <optional>
30
31using namespace llvm;
32
33namespace {
34
35class AMDGPUMCCodeEmitter : public MCCodeEmitter {
36 const MCRegisterInfo &MRI;
37 const MCInstrInfo &MCII;
38
39public:
40 AMDGPUMCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI)
41 : MRI(MRI), MCII(MCII) {}
42
43 /// Encode the instruction and write it to the OS.
44 void encodeInstruction(const MCInst &MI, SmallVectorImpl<char> &CB,
45 SmallVectorImpl<MCFixup> &Fixups,
46 const MCSubtargetInfo &STI) const override;
47
48 void getMachineOpValue(const MCInst &MI, const MCOperand &MO, APInt &Op,
49 SmallVectorImpl<MCFixup> &Fixups,
50 const MCSubtargetInfo &STI) const;
51
52 void getMachineOpValueT16(const MCInst &MI, unsigned OpNo, APInt &Op,
53 SmallVectorImpl<MCFixup> &Fixups,
54 const MCSubtargetInfo &STI) const;
55
56 void getMachineOpValueT16Lo128(const MCInst &MI, unsigned OpNo, APInt &Op,
57 SmallVectorImpl<MCFixup> &Fixups,
58 const MCSubtargetInfo &STI) const;
59
60 /// Use a fixup to encode the simm16 field for SOPP branch
61 /// instructions.
62 void getSOPPBrEncoding(const MCInst &MI, unsigned OpNo, APInt &Op,
63 SmallVectorImpl<MCFixup> &Fixups,
64 const MCSubtargetInfo &STI) const;
65
66 void getSMEMOffsetEncoding(const MCInst &MI, unsigned OpNo, APInt &Op,
67 SmallVectorImpl<MCFixup> &Fixups,
68 const MCSubtargetInfo &STI) const;
69
70 void getSDWASrcEncoding(const MCInst &MI, unsigned OpNo, APInt &Op,
71 SmallVectorImpl<MCFixup> &Fixups,
72 const MCSubtargetInfo &STI) const;
73
74 void getSDWAVopcDstEncoding(const MCInst &MI, unsigned OpNo, APInt &Op,
75 SmallVectorImpl<MCFixup> &Fixups,
76 const MCSubtargetInfo &STI) const;
77
78 void getAVOperandEncoding(const MCInst &MI, unsigned OpNo, APInt &Op,
79 SmallVectorImpl<MCFixup> &Fixups,
80 const MCSubtargetInfo &STI) const;
81
82private:
83 uint64_t getImplicitOpSelHiEncoding(int Opcode) const;
84 void getMachineOpValueCommon(const MCInst &MI, const MCOperand &MO,
85 unsigned OpNo, APInt &Op,
86 SmallVectorImpl<MCFixup> &Fixups,
87 const MCSubtargetInfo &STI) const;
88
89 /// Encode an fp or int literal.
90 std::optional<uint64_t>
91 getLitEncoding(const MCInstrDesc &Desc, const MCOperand &MO, unsigned OpNo,
92 const MCSubtargetInfo &STI,
93 bool HasMandatoryLiteral = false) const;
94
95 void getBinaryCodeForInstr(const MCInst &MI, SmallVectorImpl<MCFixup> &Fixups,
96 APInt &Inst, APInt &Scratch,
97 const MCSubtargetInfo &STI) const;
98
99 template <bool HasSrc0, bool HasSrc1, bool HasSrc2>
100 APInt postEncodeVOP3(const MCInst &MI, APInt EncodedValue,
101 const MCSubtargetInfo &STI) const;
102
103 APInt postEncodeVOPCX(const MCInst &MI, APInt EncodedValue,
104 const MCSubtargetInfo &STI) const;
105
106 APInt postEncodeLdScale(const MCInst &MI, APInt EncodedValue,
107 const MCSubtargetInfo &STI) const;
108};
109
110} // end anonymous namespace
111
113 MCContext &Ctx) {
114 return new AMDGPUMCCodeEmitter(MCII, *Ctx.getRegisterInfo());
115}
116
118 const MCExpr *Value, uint16_t Kind, bool PCRel = false) {
119 Fixups.push_back(MCFixup::create(Offset, Value, Kind, PCRel));
120}
121
122// Returns the encoding value to use if the given integer is an integer inline
123// immediate value, or 0 if it is not.
124template <typename IntTy>
126 if (Imm >= 0 && Imm <= 64)
127 return 128 + Imm;
128
129 if (Imm >= -16 && Imm <= -1)
130 return 192 + std::abs(Imm);
131
132 return 0;
133}
134
136 uint16_t IntImm = getIntInlineImmEncoding(static_cast<int16_t>(Val));
137 if (IntImm != 0)
138 return IntImm;
139
140 if (Val == 0x3800) // 0.5
141 return 240;
142
143 if (Val == 0xB800) // -0.5
144 return 241;
145
146 if (Val == 0x3C00) // 1.0
147 return 242;
148
149 if (Val == 0xBC00) // -1.0
150 return 243;
151
152 if (Val == 0x4000) // 2.0
153 return 244;
154
155 if (Val == 0xC000) // -2.0
156 return 245;
157
158 if (Val == 0x4400) // 4.0
159 return 246;
160
161 if (Val == 0xC400) // -4.0
162 return 247;
163
164 if (Val == 0x3118 && // 1.0 / (2.0 * pi)
165 STI.hasFeature(AMDGPU::FeatureInv2PiInlineImm))
166 return 248;
167
168 return 255;
169}
170
172 uint16_t IntImm = getIntInlineImmEncoding(static_cast<int16_t>(Val));
173 if (IntImm != 0)
174 return IntImm;
175
176 // clang-format off
177 switch (Val) {
178 case 0x3F00: return 240; // 0.5
179 case 0xBF00: return 241; // -0.5
180 case 0x3F80: return 242; // 1.0
181 case 0xBF80: return 243; // -1.0
182 case 0x4000: return 244; // 2.0
183 case 0xC000: return 245; // -2.0
184 case 0x4080: return 246; // 4.0
185 case 0xC080: return 247; // -4.0
186 case 0x3E22: return 248; // 1.0 / (2.0 * pi)
187 default: return 255;
188 }
189 // clang-format on
190}
191
193 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int32_t>(Val));
194 if (IntImm != 0)
195 return IntImm;
196
197 if (Val == llvm::bit_cast<uint32_t>(0.5f))
198 return 240;
199
200 if (Val == llvm::bit_cast<uint32_t>(-0.5f))
201 return 241;
202
203 if (Val == llvm::bit_cast<uint32_t>(1.0f))
204 return 242;
205
206 if (Val == llvm::bit_cast<uint32_t>(-1.0f))
207 return 243;
208
209 if (Val == llvm::bit_cast<uint32_t>(2.0f))
210 return 244;
211
212 if (Val == llvm::bit_cast<uint32_t>(-2.0f))
213 return 245;
214
215 if (Val == llvm::bit_cast<uint32_t>(4.0f))
216 return 246;
217
218 if (Val == llvm::bit_cast<uint32_t>(-4.0f))
219 return 247;
220
221 if (Val == 0x3e22f983 && // 1.0 / (2.0 * pi)
222 STI.hasFeature(AMDGPU::FeatureInv2PiInlineImm))
223 return 248;
224
225 return 255;
226}
227
229 return getLit32Encoding(Val, STI);
230}
231
233 const MCSubtargetInfo &STI, bool IsFP) {
234 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int64_t>(Val));
235 if (IntImm != 0)
236 return IntImm;
237
238 if (Val == llvm::bit_cast<uint64_t>(0.5))
239 return 240;
240
241 if (Val == llvm::bit_cast<uint64_t>(-0.5))
242 return 241;
243
244 if (Val == llvm::bit_cast<uint64_t>(1.0))
245 return 242;
246
247 if (Val == llvm::bit_cast<uint64_t>(-1.0))
248 return 243;
249
250 if (Val == llvm::bit_cast<uint64_t>(2.0))
251 return 244;
252
253 if (Val == llvm::bit_cast<uint64_t>(-2.0))
254 return 245;
255
256 if (Val == llvm::bit_cast<uint64_t>(4.0))
257 return 246;
258
259 if (Val == llvm::bit_cast<uint64_t>(-4.0))
260 return 247;
261
262 if (Val == 0x3fc45f306dc9c882 && // 1.0 / (2.0 * pi)
263 STI.hasFeature(AMDGPU::FeatureInv2PiInlineImm))
264 return 248;
265
266 // The rest part needs to align with AMDGPUInstPrinter::printLiteral64.
267
268 bool CanUse64BitLiterals =
269 STI.hasFeature(AMDGPU::Feature64BitLiterals) &&
271 if (IsFP) {
272 return CanUse64BitLiterals && Lo_32(Val) ? 254 : 255;
273 }
274
275 return CanUse64BitLiterals && (!isInt<32>(Val) || !isUInt<32>(Val)) ? 254
276 : 255;
277}
278
279std::optional<uint64_t> AMDGPUMCCodeEmitter::getLitEncoding(
280 const MCInstrDesc &Desc, const MCOperand &MO, unsigned OpNo,
281 const MCSubtargetInfo &STI, bool HasMandatoryLiteral) const {
282 const MCOperandInfo &OpInfo = Desc.operands()[OpNo];
283 int64_t Imm = 0;
284 if (MO.isExpr()) {
285 if (!MO.getExpr()->evaluateAsAbsolute(Imm) ||
287 if (OpInfo.OperandType == AMDGPU::OPERAND_KIMM16 ||
290 return Imm;
291 if (STI.hasFeature(AMDGPU::Feature64BitLiterals) &&
292 AMDGPU::getOperandSize(OpInfo) == 8)
293 return 254;
294 return 255;
295 }
296 } else {
297 assert(!MO.isDFPImm());
298
299 if (!MO.isImm())
300 return {};
301
302 Imm = MO.getImm();
303 }
304
305 switch (OpInfo.OperandType) {
315 return getLit32Encoding(static_cast<uint32_t>(Imm), STI);
316
319 return getLit64Encoding(Desc, static_cast<uint64_t>(Imm), STI, false);
320
323 return getLit64Encoding(Desc, static_cast<uint64_t>(Imm), STI, true);
324
326 auto Enc = getLit64Encoding(Desc, static_cast<uint64_t>(Imm), STI, true);
327 return (HasMandatoryLiteral && Enc == 255) ? 254 : Enc;
328 }
329
332 return getLit16IntEncoding(static_cast<uint32_t>(Imm), STI);
333
336 // FIXME Is this correct? What do inline immediates do on SI for f16 src
337 // which does not have f16 support?
338 return getLit16Encoding(static_cast<uint16_t>(Imm), STI);
339
342 // We don't actually need to check Inv2Pi here because BF16 instructions can
343 // only be emitted for targets that already support the feature.
344 return getLitBF16Encoding(static_cast<uint16_t>(Imm));
345
348 return AMDGPU::getInlineEncodingV2I16(static_cast<uint32_t>(Imm))
349 .value_or(255);
350
353 return AMDGPU::getInlineEncodingV2F16(static_cast<uint32_t>(Imm))
354 .value_or(255);
355
358 return AMDGPU::getInlineEncodingV2BF16(static_cast<uint32_t>(Imm))
359 .value_or(255);
360
362 return 255;
363
367 return Imm;
368 default:
369 llvm_unreachable("invalid operand size");
370 }
371}
372
373uint64_t AMDGPUMCCodeEmitter::getImplicitOpSelHiEncoding(int Opcode) const {
374 using namespace AMDGPU::VOP3PEncoding;
375
376 if (AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::op_sel_hi)) {
377 if (AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::src2))
378 return 0;
379 if (AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::src1))
380 return OP_SEL_HI_2;
381 if (AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::src0))
382 return OP_SEL_HI_1 | OP_SEL_HI_2;
383 }
385}
386
387void AMDGPUMCCodeEmitter::encodeInstruction(const MCInst &MI,
388 SmallVectorImpl<char> &CB,
389 SmallVectorImpl<MCFixup> &Fixups,
390 const MCSubtargetInfo &STI) const {
391 int Opcode = MI.getOpcode();
392 APInt Encoding, Scratch;
393 getBinaryCodeForInstr(MI, Fixups, Encoding, Scratch, STI);
394 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
395 unsigned bytes = Desc.getSize();
396
397 // Set unused op_sel_hi bits to 1 for VOP3P and MAI instructions.
398 // Note that accvgpr_read/write are MAI, have src0, but do not use op_sel.
399 if (((Desc.TSFlags & SIInstrFlags::VOP3P) ||
400 Opcode == AMDGPU::V_ACCVGPR_READ_B32_vi ||
401 Opcode == AMDGPU::V_ACCVGPR_WRITE_B32_vi) &&
402 // Matrix B format operand reuses op_sel_hi.
403 !AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::matrix_b_fmt) &&
404 // Matrix B scale operand reuses op_sel_hi.
405 !AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::matrix_b_scale) &&
406 // Matrix B reuse operand reuses op_sel_hi.
407 !AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::matrix_b_reuse)) {
408 Encoding |= getImplicitOpSelHiEncoding(Opcode);
409 }
410
411 for (unsigned i = 0; i < bytes; i++) {
412 CB.push_back((uint8_t)Encoding.extractBitsAsZExtValue(8, 8 * i));
413 }
414
415 // NSA encoding.
416 if (AMDGPU::isGFX10Plus(STI) && Desc.TSFlags & SIInstrFlags::MIMG) {
417 int vaddr0 = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
418 AMDGPU::OpName::vaddr0);
419 int srsrc = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
420 AMDGPU::OpName::srsrc);
421 assert(vaddr0 >= 0 && srsrc > vaddr0);
422 unsigned NumExtraAddrs = srsrc - vaddr0 - 1;
423 unsigned NumPadding = (-NumExtraAddrs) & 3;
424
425 for (unsigned i = 0; i < NumExtraAddrs; ++i) {
426 getMachineOpValue(MI, MI.getOperand(vaddr0 + 1 + i), Encoding, Fixups,
427 STI);
428 CB.push_back((uint8_t)Encoding.getLimitedValue());
429 }
430 CB.append(NumPadding, 0);
431 }
432
433 if ((bytes > 8 && STI.hasFeature(AMDGPU::FeatureVOP3Literal)) ||
434 (bytes > 4 && !STI.hasFeature(AMDGPU::FeatureVOP3Literal)))
435 return;
436
437 // Do not print literals from SISrc Operands for insts with mandatory literals
438 if (AMDGPU::hasNamedOperand(MI.getOpcode(), AMDGPU::OpName::imm))
439 return;
440
441 // Check for additional literals
442 for (unsigned i = 0, e = Desc.getNumOperands(); i < e; ++i) {
443
444 // Check if this operand should be encoded as [SV]Src
446 continue;
447
448 // Is this operand a literal immediate?
449 const MCOperand &Op = MI.getOperand(i);
450 auto Enc = getLitEncoding(Desc, Op, i, STI);
451 if (!Enc || (*Enc != 255 && *Enc != 254))
452 continue;
453
454 // Yes! Encode it
455 int64_t Imm = 0;
456
457 bool IsLit = false;
458 if (Op.isImm())
459 Imm = Op.getImm();
460 else if (Op.isExpr()) {
461 if (const auto *C = dyn_cast<MCConstantExpr>(Op.getExpr())) {
462 Imm = C->getValue();
463 } else if (AMDGPU::isLitExpr(Op.getExpr())) {
464 IsLit = true;
465 Imm = AMDGPU::getLitValue(Op.getExpr());
466 }
467 } else // Exprs will be replaced with a fixup value.
468 llvm_unreachable("Must be immediate or expr");
469
470 if (*Enc == 254) {
471 assert(STI.hasFeature(AMDGPU::Feature64BitLiterals));
473 } else {
474 auto OpType =
475 static_cast<AMDGPU::OperandType>(Desc.operands()[i].OperandType);
476 Imm = AMDGPU::encode32BitLiteral(Imm, OpType, IsLit);
478 }
479
480 // Only one literal value allowed
481 break;
482 }
483}
484
485void AMDGPUMCCodeEmitter::getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
486 APInt &Op,
487 SmallVectorImpl<MCFixup> &Fixups,
488 const MCSubtargetInfo &STI) const {
489 const MCOperand &MO = MI.getOperand(OpNo);
490
491 if (MO.isExpr()) {
492 const MCExpr *Expr = MO.getExpr();
493 addFixup(Fixups, 0, Expr, AMDGPU::fixup_si_sopp_br, true);
494 Op = APInt::getZero(96);
495 } else {
496 getMachineOpValue(MI, MO, Op, Fixups, STI);
497 }
498}
499
500void AMDGPUMCCodeEmitter::getSMEMOffsetEncoding(
501 const MCInst &MI, unsigned OpNo, APInt &Op,
502 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
503 auto Offset = MI.getOperand(OpNo).getImm();
504 // VI only supports 20-bit unsigned offsets.
506 Op = Offset;
507}
508
509void AMDGPUMCCodeEmitter::getSDWASrcEncoding(const MCInst &MI, unsigned OpNo,
510 APInt &Op,
511 SmallVectorImpl<MCFixup> &Fixups,
512 const MCSubtargetInfo &STI) const {
513 using namespace AMDGPU::SDWA;
514
515 uint64_t RegEnc = 0;
516
517 const MCOperand &MO = MI.getOperand(OpNo);
518
519 if (MO.isReg()) {
520 MCRegister Reg = MO.getReg();
521 RegEnc |= MRI.getEncodingValue(Reg);
522 RegEnc &= SDWA9EncValues::SRC_VGPR_MASK;
524 RegEnc |= SDWA9EncValues::SRC_SGPR_MASK;
525 }
526 Op = RegEnc;
527 return;
528 } else {
529 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
530 auto Enc = getLitEncoding(Desc, MO, OpNo, STI);
531 if (Enc && *Enc != 255) {
532 Op = *Enc | SDWA9EncValues::SRC_SGPR_MASK;
533 return;
534 }
535 }
536
537 llvm_unreachable("Unsupported operand kind");
538}
539
540void AMDGPUMCCodeEmitter::getSDWAVopcDstEncoding(
541 const MCInst &MI, unsigned OpNo, APInt &Op,
542 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
543 using namespace AMDGPU::SDWA;
544
545 uint64_t RegEnc = 0;
546
547 const MCOperand &MO = MI.getOperand(OpNo);
548
549 MCRegister Reg = MO.getReg();
550 if (Reg != AMDGPU::VCC && Reg != AMDGPU::VCC_LO) {
551 RegEnc |= MRI.getEncodingValue(Reg);
552 RegEnc &= SDWA9EncValues::VOPC_DST_SGPR_MASK;
553 RegEnc |= SDWA9EncValues::VOPC_DST_VCC_MASK;
554 }
555 Op = RegEnc;
556}
557
558void AMDGPUMCCodeEmitter::getAVOperandEncoding(
559 const MCInst &MI, unsigned OpNo, APInt &Op,
560 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
561 MCRegister Reg = MI.getOperand(OpNo).getReg();
562 unsigned Enc = MRI.getEncodingValue(Reg);
563 unsigned Idx = Enc & AMDGPU::HWEncoding::LO256_REG_IDX_MASK;
564 bool IsVGPROrAGPR =
566
567 // VGPR and AGPR have the same encoding, but SrcA and SrcB operands of mfma
568 // instructions use acc[0:1] modifier bits to distinguish. These bits are
569 // encoded as a virtual 9th bit of the register for these operands.
570 bool IsAGPR = Enc & AMDGPU::HWEncoding::IS_AGPR;
571
572 Op = Idx | (IsVGPROrAGPR << 8) | (IsAGPR << 9);
573}
574
575static bool needsPCRel(const MCExpr *Expr) {
576 switch (Expr->getKind()) {
577 case MCExpr::SymbolRef: {
578 auto *SE = cast<MCSymbolRefExpr>(Expr);
579 auto Spec = AMDGPU::getSpecifier(SE);
580 return Spec != AMDGPUMCExpr::S_ABS32_LO &&
582 }
583 case MCExpr::Binary: {
584 auto *BE = cast<MCBinaryExpr>(Expr);
585 if (BE->getOpcode() == MCBinaryExpr::Sub)
586 return false;
587 return needsPCRel(BE->getLHS()) || needsPCRel(BE->getRHS());
588 }
589 case MCExpr::Unary:
590 return needsPCRel(cast<MCUnaryExpr>(Expr)->getSubExpr());
592 case MCExpr::Target:
593 case MCExpr::Constant:
594 return false;
595 }
596 llvm_unreachable("invalid kind");
597}
598
599void AMDGPUMCCodeEmitter::getMachineOpValue(const MCInst &MI,
600 const MCOperand &MO, APInt &Op,
601 SmallVectorImpl<MCFixup> &Fixups,
602 const MCSubtargetInfo &STI) const {
603 if (MO.isReg()){
604 unsigned Enc = MRI.getEncodingValue(MO.getReg());
605 unsigned Idx = Enc & AMDGPU::HWEncoding::LO256_REG_IDX_MASK;
606 bool IsVGPROrAGPR =
608 Op = Idx | (IsVGPROrAGPR << 8);
609 return;
610 }
611 unsigned OpNo = &MO - MI.begin();
612 getMachineOpValueCommon(MI, MO, OpNo, Op, Fixups, STI);
613}
614
615void AMDGPUMCCodeEmitter::getMachineOpValueT16(
616 const MCInst &MI, unsigned OpNo, APInt &Op,
617 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
618 const MCOperand &MO = MI.getOperand(OpNo);
619 if (MO.isReg()) {
620 unsigned Enc = MRI.getEncodingValue(MO.getReg());
621 unsigned Idx = Enc & AMDGPU::HWEncoding::REG_IDX_MASK;
622 bool IsVGPR = Enc & AMDGPU::HWEncoding::IS_VGPR;
623 Op = Idx | (IsVGPR << 8);
624 return;
625 }
626 getMachineOpValueCommon(MI, MO, OpNo, Op, Fixups, STI);
627 // VGPRs include the suffix/op_sel bit in the register encoding, but
628 // immediates and SGPRs include it in src_modifiers. Therefore, copy the
629 // op_sel bit from the src operands into src_modifier operands if Op is
630 // src_modifiers and the corresponding src is a VGPR
631 int SrcMOIdx = -1;
632 assert(OpNo < INT_MAX);
633 if ((int)OpNo == AMDGPU::getNamedOperandIdx(MI.getOpcode(),
634 AMDGPU::OpName::src0_modifiers)) {
635 SrcMOIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
636 int VDstMOIdx =
637 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdst);
638 if (VDstMOIdx != -1) {
639 auto DstReg = MI.getOperand(VDstMOIdx).getReg();
640 if (AMDGPU::isHi16Reg(DstReg, MRI))
642 }
643 } else if ((int)OpNo == AMDGPU::getNamedOperandIdx(
644 MI.getOpcode(), AMDGPU::OpName::src1_modifiers))
645 SrcMOIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src1);
646 else if ((int)OpNo == AMDGPU::getNamedOperandIdx(
647 MI.getOpcode(), AMDGPU::OpName::src2_modifiers))
648 SrcMOIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src2);
649 if (SrcMOIdx == -1)
650 return;
651
652 const MCOperand &SrcMO = MI.getOperand(SrcMOIdx);
653 if (!SrcMO.isReg())
654 return;
655 auto SrcReg = SrcMO.getReg();
656 if (AMDGPU::isSGPR(SrcReg, &MRI))
657 return;
658 if (AMDGPU::isHi16Reg(SrcReg, MRI))
660}
661
662void AMDGPUMCCodeEmitter::getMachineOpValueT16Lo128(
663 const MCInst &MI, unsigned OpNo, APInt &Op,
664 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
665 const MCOperand &MO = MI.getOperand(OpNo);
666 if (MO.isReg()) {
667 uint16_t Encoding = MRI.getEncodingValue(MO.getReg());
668 unsigned RegIdx = Encoding & AMDGPU::HWEncoding::LO256_REG_IDX_MASK;
669 bool IsHi = Encoding & AMDGPU::HWEncoding::IS_HI16;
670 bool IsVGPR = Encoding & AMDGPU::HWEncoding::IS_VGPR;
671 assert((!IsVGPR || isUInt<7>(RegIdx)) && "VGPR0-VGPR127 expected!");
672 Op = (IsVGPR ? 0x100 : 0) | (IsHi ? 0x80 : 0) | RegIdx;
673 return;
674 }
675 getMachineOpValueCommon(MI, MO, OpNo, Op, Fixups, STI);
676}
677
678void AMDGPUMCCodeEmitter::getMachineOpValueCommon(
679 const MCInst &MI, const MCOperand &MO, unsigned OpNo, APInt &Op,
680 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
681 bool isLikeImm = false;
682 int64_t Val;
683
684 if (MO.isImm()) {
685 Val = MO.getImm();
686 isLikeImm = true;
687 } else if (MO.isExpr() && MO.getExpr()->evaluateAsAbsolute(Val)) {
688 isLikeImm = true;
689 } else if (MO.isExpr()) {
690 // FIXME: If this is expression is PCRel or not should not depend on what
691 // the expression looks like. Given that this is just a general expression,
692 // it should probably be FK_Data_4 and whatever is producing
693 //
694 // s_add_u32 s2, s2, (extern_const_addrspace+16
695 //
696 // And expecting a PCRel should instead produce
697 //
698 // .Ltmp1:
699 // s_add_u32 s2, s2, (extern_const_addrspace+16)-.Ltmp1
700 bool PCRel = needsPCRel(MO.getExpr());
701 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
702 uint32_t Offset = Desc.getSize();
703 assert(Offset == 4 || Offset == 8);
704 unsigned Size = AMDGPU::getOperandSize(Desc, OpNo);
706 addFixup(Fixups, Offset, MO.getExpr(), Kind, PCRel);
707 }
708
709 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
710 if (AMDGPU::isSISrcOperand(Desc, OpNo)) {
711 bool HasMandatoryLiteral =
712 AMDGPU::hasNamedOperand(MI.getOpcode(), AMDGPU::OpName::imm);
713 if (auto Enc = getLitEncoding(Desc, MO, OpNo, STI, HasMandatoryLiteral)) {
714 Op = *Enc;
715 return;
716 }
717
718 llvm_unreachable("Operand not supported for SISrc");
719 }
720
721 if (isLikeImm) {
722 Op = Val;
723 return;
724 }
725
726 llvm_unreachable("Encoding of this operand type is not supported yet.");
727}
728
729template <bool HasSrc0, bool HasSrc1, bool HasSrc2>
730APInt AMDGPUMCCodeEmitter::postEncodeVOP3(const MCInst &MI, APInt EncodedValue,
731 const MCSubtargetInfo &STI) const {
732 if (!AMDGPU::isGFX10Plus(STI))
733 return EncodedValue;
734 // Set unused source fields in VOP3 encodings to inline immediate 0 to avoid
735 // hardware conservatively assuming the instruction reads SGPRs.
736 constexpr uint64_t InlineImmediate0 = 0x80;
737 if (!HasSrc0)
738 EncodedValue |= InlineImmediate0 << 32;
739 if (!HasSrc1)
740 EncodedValue |= InlineImmediate0 << 41;
741 if (!HasSrc2)
742 EncodedValue |= InlineImmediate0 << 50;
743 return EncodedValue;
744}
745
746APInt AMDGPUMCCodeEmitter::postEncodeVOPCX(const MCInst &MI, APInt EncodedValue,
747 const MCSubtargetInfo &STI) const {
748 // GFX10+ v_cmpx opcodes promoted to VOP3 have implied dst=EXEC.
749 // Documentation requires dst to be encoded as EXEC (0x7E),
750 // but it looks like the actual value encoded for dst operand
751 // is ignored by HW. It was decided to define dst as "do not care"
752 // in td files to allow disassembler accept any dst value.
753 // However, dst is encoded as EXEC for compatibility with SP3.
754 [[maybe_unused]] const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
755 assert((Desc.TSFlags & SIInstrFlags::VOP3) &&
756 Desc.hasImplicitDefOfPhysReg(AMDGPU::EXEC));
757 EncodedValue |= MRI.getEncodingValue(AMDGPU::EXEC_LO) &
759 return postEncodeVOP3<true, true, false>(MI, EncodedValue, STI);
760}
761
762APInt AMDGPUMCCodeEmitter::postEncodeLdScale(const MCInst &MI,
763 APInt EncodedValue,
764 const MCSubtargetInfo &STI) const {
765 // Set unused scale_src2 field to VGPR0 to avoid hardware conservatively
766 // assuming the instruction reads SGPRs.
767 constexpr uint64_t Vgpr0 = 0x100;
768 EncodedValue |= Vgpr0 << 50;
769 return EncodedValue;
770}
771
772#include "AMDGPUGenMCCodeEmitter.inc"
unsigned const MachineRegisterInfo * MRI
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...
IRTranslator LLVM IR MI
Register Reg
LLVM_ABI uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const
Definition APInt.cpp:520
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.
Definition APInt.h:476
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
@ Sub
Subtraction.
Definition MCExpr.h:324
MCCodeEmitter - Generic instruction encoding interface.
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
@ Unary
Unary expressions.
Definition MCExpr.h:44
@ Constant
Constant expressions.
Definition MCExpr.h:42
@ SymbolRef
References to labels and assigned expressions.
Definition MCExpr.h:43
@ Target
Target specific expression.
Definition MCExpr.h:46
@ Specifier
Expression with a relocation specifier.
Definition MCExpr.h:45
@ Binary
Binary expressions.
Definition MCExpr.h:41
ExprKind getKind() const
Definition MCExpr.h:85
static MCFixupKind getDataKindForSize(unsigned Size)
Return the generic fixup kind for a value with the given size.
Definition MCFixup.h:110
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
Definition MCFixup.h:86
Describe properties that are true of each instruction in the target description file.
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition MCInstrInfo.h:90
uint8_t OperandType
Information about the type of the operand.
Definition MCInstrDesc.h:98
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
int64_t getImm() const
Definition MCInst.h:84
bool isImm() const
Definition MCInst.h:66
bool isReg() const
Definition MCInst.h:65
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:73
bool isDFPImm() const
Definition MCInst.h:68
const MCExpr * getExpr() const
Definition MCInst.h:118
bool isExpr() const
Definition MCInst.h:69
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
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.
Definition Value.h:75
#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.
LLVM_READONLY bool hasNamedOperand(uint64_t Opcode, OpName NamedIdx)
constexpr bool isSISrcOperand(const MCOperandInfo &OpInfo)
Is this an AMDGPU specific source operand?
LLVM_READONLY int64_t getLitValue(const MCExpr *Expr)
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.
Definition SIDefines.h:231
@ OPERAND_REG_IMM_INT64
Definition SIDefines.h:202
@ OPERAND_REG_IMM_V2FP16
Definition SIDefines.h:209
@ OPERAND_REG_INLINE_C_FP64
Definition SIDefines.h:222
@ OPERAND_REG_INLINE_C_BF16
Definition SIDefines.h:219
@ OPERAND_REG_INLINE_C_V2BF16
Definition SIDefines.h:224
@ OPERAND_REG_IMM_V2INT16
Definition SIDefines.h:210
@ OPERAND_REG_IMM_BF16
Definition SIDefines.h:206
@ OPERAND_REG_IMM_INT32
Operands with register, 32-bit, or 64-bit immediate.
Definition SIDefines.h:201
@ OPERAND_REG_IMM_V2BF16
Definition SIDefines.h:208
@ OPERAND_REG_IMM_FP16
Definition SIDefines.h:207
@ OPERAND_REG_INLINE_C_INT64
Definition SIDefines.h:218
@ OPERAND_REG_INLINE_C_INT16
Operands with register or inline constant.
Definition SIDefines.h:216
@ OPERAND_REG_IMM_NOINLINE_V2FP16
Definition SIDefines.h:211
@ OPERAND_REG_IMM_FP64
Definition SIDefines.h:205
@ OPERAND_REG_INLINE_C_V2FP16
Definition SIDefines.h:225
@ OPERAND_REG_INLINE_AC_INT32
Operands with an AccVGPR register or inline constant.
Definition SIDefines.h:236
@ OPERAND_REG_INLINE_AC_FP32
Definition SIDefines.h:237
@ OPERAND_REG_IMM_V2INT32
Definition SIDefines.h:212
@ OPERAND_REG_IMM_FP32
Definition SIDefines.h:204
@ OPERAND_REG_INLINE_C_FP32
Definition SIDefines.h:221
@ OPERAND_REG_INLINE_C_INT32
Definition SIDefines.h:217
@ OPERAND_REG_INLINE_C_V2INT16
Definition SIDefines.h:223
@ OPERAND_REG_IMM_V2FP32
Definition SIDefines.h:213
@ OPERAND_REG_INLINE_AC_FP64
Definition SIDefines.h:238
@ OPERAND_REG_INLINE_C_FP16
Definition SIDefines.h:220
@ OPERAND_REG_IMM_INT16
Definition SIDefines.h:203
@ OPERAND_INLINE_SPLIT_BARRIER_INT32
Definition SIDefines.h:228
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)
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition Endian.h:96
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
Op::Description Desc
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
Definition MathExtras.h:155
To bit_cast(const From &from) noexcept
Definition bit.h:90
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
MCCodeEmitter * createAMDGPUMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)