1 //===-- AMDGPUInstPrinter.cpp - AMDGPU MC Inst -> ASM ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #include "AMDGPUInstPrinter.h"
12 #include "SIDefines.h"
14 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCRegisterInfo.h"
18 #include "llvm/Support/MathExtras.h"
22 void AMDGPUInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
25 printInstruction(MI, OS);
27 printAnnotation(OS, Annot);
30 void AMDGPUInstPrinter::printU8ImmOperand(const MCInst *MI, unsigned OpNo,
32 O << formatHex(MI->getOperand(OpNo).getImm() & 0xff);
35 void AMDGPUInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
37 O << formatHex(MI->getOperand(OpNo).getImm() & 0xffff);
40 void AMDGPUInstPrinter::printU32ImmOperand(const MCInst *MI, unsigned OpNo,
42 O << formatHex(MI->getOperand(OpNo).getImm() & 0xffffffff);
45 void AMDGPUInstPrinter::printU8ImmDecOperand(const MCInst *MI, unsigned OpNo,
47 O << formatDec(MI->getOperand(OpNo).getImm() & 0xff);
50 void AMDGPUInstPrinter::printU16ImmDecOperand(const MCInst *MI, unsigned OpNo,
52 O << formatDec(MI->getOperand(OpNo).getImm() & 0xffff);
55 void AMDGPUInstPrinter::printOffen(const MCInst *MI, unsigned OpNo,
57 if (MI->getOperand(OpNo).getImm())
61 void AMDGPUInstPrinter::printIdxen(const MCInst *MI, unsigned OpNo,
63 if (MI->getOperand(OpNo).getImm())
67 void AMDGPUInstPrinter::printAddr64(const MCInst *MI, unsigned OpNo,
69 if (MI->getOperand(OpNo).getImm())
73 void AMDGPUInstPrinter::printMBUFOffset(const MCInst *MI, unsigned OpNo,
75 if (MI->getOperand(OpNo).getImm()) {
77 printU16ImmOperand(MI, OpNo, O);
81 void AMDGPUInstPrinter::printDSOffset(const MCInst *MI, unsigned OpNo,
83 uint16_t Imm = MI->getOperand(OpNo).getImm();
86 printU16ImmDecOperand(MI, OpNo, O);
90 void AMDGPUInstPrinter::printDSOffset0(const MCInst *MI, unsigned OpNo,
93 printU8ImmDecOperand(MI, OpNo, O);
96 void AMDGPUInstPrinter::printDSOffset1(const MCInst *MI, unsigned OpNo,
99 printU8ImmDecOperand(MI, OpNo, O);
102 void AMDGPUInstPrinter::printGLC(const MCInst *MI, unsigned OpNo,
104 if (MI->getOperand(OpNo).getImm())
108 void AMDGPUInstPrinter::printSLC(const MCInst *MI, unsigned OpNo,
110 if (MI->getOperand(OpNo).getImm())
114 void AMDGPUInstPrinter::printTFE(const MCInst *MI, unsigned OpNo,
116 if (MI->getOperand(OpNo).getImm())
120 void AMDGPUInstPrinter::printRegOperand(unsigned reg, raw_ostream &O) {
134 case AMDGPU::FLAT_SCR:
143 case AMDGPU::EXEC_LO:
146 case AMDGPU::EXEC_HI:
149 case AMDGPU::FLAT_SCR_LO:
150 O << "flat_scratch_lo";
152 case AMDGPU::FLAT_SCR_HI:
153 O << "flat_scratch_hi";
162 if (MRI.getRegClass(AMDGPU::VGPR_32RegClassID).contains(reg)) {
165 } else if (MRI.getRegClass(AMDGPU::SGPR_32RegClassID).contains(reg)) {
168 } else if (MRI.getRegClass(AMDGPU::VReg_64RegClassID).contains(reg)) {
171 } else if (MRI.getRegClass(AMDGPU::SReg_64RegClassID).contains(reg)) {
174 } else if (MRI.getRegClass(AMDGPU::VReg_128RegClassID).contains(reg)) {
177 } else if (MRI.getRegClass(AMDGPU::SReg_128RegClassID).contains(reg)) {
180 } else if (MRI.getRegClass(AMDGPU::VReg_96RegClassID).contains(reg)) {
183 } else if (MRI.getRegClass(AMDGPU::VReg_256RegClassID).contains(reg)) {
186 } else if (MRI.getRegClass(AMDGPU::SReg_256RegClassID).contains(reg)) {
189 } else if (MRI.getRegClass(AMDGPU::VReg_512RegClassID).contains(reg)) {
192 } else if (MRI.getRegClass(AMDGPU::SReg_512RegClassID).contains(reg)) {
196 O << getRegisterName(reg);
200 // The low 8 bits of the encoding value is the register index, for both VGPRs
202 unsigned RegIdx = MRI.getEncodingValue(reg) & ((1 << 8) - 1);
208 O << Type << '[' << RegIdx << ':' << (RegIdx + NumRegs - 1) << ']';
211 void AMDGPUInstPrinter::printImmediate(uint32_t Imm, raw_ostream &O) {
212 int32_t SImm = static_cast<int32_t>(Imm);
213 if (SImm >= -16 && SImm <= 64) {
218 if (Imm == FloatToBits(0.0f))
220 else if (Imm == FloatToBits(1.0f))
222 else if (Imm == FloatToBits(-1.0f))
224 else if (Imm == FloatToBits(0.5f))
226 else if (Imm == FloatToBits(-0.5f))
228 else if (Imm == FloatToBits(2.0f))
230 else if (Imm == FloatToBits(-2.0f))
232 else if (Imm == FloatToBits(4.0f))
234 else if (Imm == FloatToBits(-4.0f))
237 O << formatHex(static_cast<uint64_t>(Imm));
241 void AMDGPUInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
244 const MCOperand &Op = MI->getOperand(OpNo);
246 switch (Op.getReg()) {
247 // This is the default predicate state, so we don't need to print it.
248 case AMDGPU::PRED_SEL_OFF:
252 printRegOperand(Op.getReg(), O);
255 } else if (Op.isImm()) {
256 printImmediate(Op.getImm(), O);
257 } else if (Op.isFPImm()) {
259 // We special case 0.0 because otherwise it will be printed as an integer.
260 if (Op.getFPImm() == 0.0)
263 printImmediate(FloatToBits(Op.getFPImm()), O);
264 } else if (Op.isExpr()) {
265 const MCExpr *Exp = Op.getExpr();
268 llvm_unreachable("unknown operand type in printOperand");
272 void AMDGPUInstPrinter::printOperandAndMods(const MCInst *MI, unsigned OpNo,
274 unsigned InputModifiers = MI->getOperand(OpNo).getImm();
275 if (InputModifiers & SISrcMods::NEG)
277 if (InputModifiers & SISrcMods::ABS)
279 printOperand(MI, OpNo + 1, O);
280 if (InputModifiers & SISrcMods::ABS)
284 void AMDGPUInstPrinter::printInterpSlot(const MCInst *MI, unsigned OpNum,
286 unsigned Imm = MI->getOperand(OpNum).getImm();
290 } else if (Imm == 1) {
292 } else if (Imm == 0) {
295 llvm_unreachable("Invalid interpolation parameter slot");
299 void AMDGPUInstPrinter::printMemOperand(const MCInst *MI, unsigned OpNo,
301 printOperand(MI, OpNo, O);
303 printOperand(MI, OpNo + 1, O);
306 void AMDGPUInstPrinter::printIfSet(const MCInst *MI, unsigned OpNo,
307 raw_ostream &O, StringRef Asm,
309 const MCOperand &Op = MI->getOperand(OpNo);
311 if (Op.getImm() == 1) {
318 void AMDGPUInstPrinter::printAbs(const MCInst *MI, unsigned OpNo,
320 printIfSet(MI, OpNo, O, "|");
323 void AMDGPUInstPrinter::printClamp(const MCInst *MI, unsigned OpNo,
325 printIfSet(MI, OpNo, O, "_SAT");
328 void AMDGPUInstPrinter::printClampSI(const MCInst *MI, unsigned OpNo,
330 if (MI->getOperand(OpNo).getImm())
334 void AMDGPUInstPrinter::printOModSI(const MCInst *MI, unsigned OpNo,
336 int Imm = MI->getOperand(OpNo).getImm();
337 if (Imm == SIOutMods::MUL2)
339 else if (Imm == SIOutMods::MUL4)
341 else if (Imm == SIOutMods::DIV2)
345 void AMDGPUInstPrinter::printLiteral(const MCInst *MI, unsigned OpNo,
347 int32_t Imm = MI->getOperand(OpNo).getImm();
348 O << Imm << '(' << BitsToFloat(Imm) << ')';
351 void AMDGPUInstPrinter::printLast(const MCInst *MI, unsigned OpNo,
353 printIfSet(MI, OpNo, O.indent(25 - O.GetNumBytesInBuffer()), "*", " ");
356 void AMDGPUInstPrinter::printNeg(const MCInst *MI, unsigned OpNo,
358 printIfSet(MI, OpNo, O, "-");
361 void AMDGPUInstPrinter::printOMOD(const MCInst *MI, unsigned OpNo,
363 switch (MI->getOperand(OpNo).getImm()) {
377 void AMDGPUInstPrinter::printRel(const MCInst *MI, unsigned OpNo,
379 printIfSet(MI, OpNo, O, "+");
382 void AMDGPUInstPrinter::printUpdateExecMask(const MCInst *MI, unsigned OpNo,
384 printIfSet(MI, OpNo, O, "ExecMask,");
387 void AMDGPUInstPrinter::printUpdatePred(const MCInst *MI, unsigned OpNo,
389 printIfSet(MI, OpNo, O, "Pred,");
392 void AMDGPUInstPrinter::printWrite(const MCInst *MI, unsigned OpNo,
394 const MCOperand &Op = MI->getOperand(OpNo);
395 if (Op.getImm() == 0) {
400 void AMDGPUInstPrinter::printSel(const MCInst *MI, unsigned OpNo,
402 const char * chans = "XYZW";
403 int sel = MI->getOperand(OpNo).getImm();
412 O << cb << '[' << sel << ']';
413 } else if (sel >= 448) {
416 } else if (sel >= 0){
421 O << '.' << chans[chan];
424 void AMDGPUInstPrinter::printBankSwizzle(const MCInst *MI, unsigned OpNo,
426 int BankSwizzle = MI->getOperand(OpNo).getImm();
427 switch (BankSwizzle) {
429 O << "BS:VEC_021/SCL_122";
432 O << "BS:VEC_120/SCL_212";
435 O << "BS:VEC_102/SCL_221";
449 void AMDGPUInstPrinter::printRSel(const MCInst *MI, unsigned OpNo,
451 unsigned Sel = MI->getOperand(OpNo).getImm();
479 void AMDGPUInstPrinter::printCT(const MCInst *MI, unsigned OpNo,
481 unsigned CT = MI->getOperand(OpNo).getImm();
494 void AMDGPUInstPrinter::printKCache(const MCInst *MI, unsigned OpNo,
496 int KCacheMode = MI->getOperand(OpNo).getImm();
497 if (KCacheMode > 0) {
498 int KCacheBank = MI->getOperand(OpNo - 2).getImm();
499 O << "CB" << KCacheBank << ':';
500 int KCacheAddr = MI->getOperand(OpNo + 2).getImm();
501 int LineSize = (KCacheMode == 1) ? 16 : 32;
502 O << KCacheAddr * 16 << '-' << KCacheAddr * 16 + LineSize;
506 void AMDGPUInstPrinter::printSendMsg(const MCInst *MI, unsigned OpNo,
508 unsigned SImm16 = MI->getOperand(OpNo).getImm();
509 unsigned Msg = SImm16 & 0xF;
510 if (Msg == 2 || Msg == 3) {
511 unsigned Op = (SImm16 >> 4) & 0xF;
519 unsigned Stream = (SImm16 >> 8) & 0x3;
526 O << " stream " << Stream;
534 O << "unknown(" << Msg << ") ";
537 void AMDGPUInstPrinter::printWaitFlag(const MCInst *MI, unsigned OpNo,
539 // Note: Mask values are taken from SIInsertWaits.cpp and not from ISA docs
540 // SIInsertWaits.cpp bits usage does not match ISA docs description but it
541 // works so it might be a misprint in docs.
542 unsigned SImm16 = MI->getOperand(OpNo).getImm();
543 unsigned Vmcnt = SImm16 & 0xF;
544 unsigned Expcnt = (SImm16 >> 4) & 0xF;
545 unsigned Lgkmcnt = (SImm16 >> 8) & 0xF;
547 bool NeedSpace = false;
550 O << "vmcnt(" << Vmcnt << ')';
557 O << "expcnt(" << Expcnt << ')';
561 if (Lgkmcnt != 0x7) {
564 O << "lgkmcnt(" << Lgkmcnt << ')';
568 #include "AMDGPUGenAsmWriter.inc"