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