1 //===- R600MCCodeEmitter.cpp - Code Emitter for R600->Cayman GPU families -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 /// This code emitter outputs bytecode that is understood by the r600g driver
13 /// in the Mesa [1] project. The bytecode is very similar to the hardware's ISA,
14 /// but it still needs to be run through a finalizer in order to be executed
17 /// [1] http://www.mesa3d.org/
19 //===----------------------------------------------------------------------===//
21 #include "R600Defines.h"
22 #include "MCTargetDesc/AMDGPUMCCodeEmitter.h"
23 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
24 #include "llvm/MC/MCCodeEmitter.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCInstrInfo.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCSubtargetInfo.h"
30 #include "llvm/Support/raw_ostream.h"
33 #define SRC_BYTE_COUNT 11
34 #define DST_BYTE_COUNT 5
40 class R600MCCodeEmitter : public AMDGPUMCCodeEmitter {
41 R600MCCodeEmitter(const R600MCCodeEmitter &); // DO NOT IMPLEMENT
42 void operator=(const R600MCCodeEmitter &); // DO NOT IMPLEMENT
43 const MCInstrInfo &MCII;
44 const MCRegisterInfo &MRI;
45 const MCSubtargetInfo &STI;
50 R600MCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
51 const MCSubtargetInfo &sti, MCContext &ctx)
52 : MCII(mcii), MRI(mri), STI(sti), Ctx(ctx) { }
54 /// \brief Encode the instruction and write it to the OS.
55 virtual void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
56 SmallVectorImpl<MCFixup> &Fixups) const;
58 /// \returns the encoding for an MCOperand.
59 virtual uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
60 SmallVectorImpl<MCFixup> &Fixups) const;
63 void EmitALUInstr(const MCInst &MI, SmallVectorImpl<MCFixup> &Fixups,
64 raw_ostream &OS) const;
65 void EmitSrc(const MCInst &MI, unsigned OpIdx, raw_ostream &OS) const;
66 void EmitSrcISA(const MCInst &MI, unsigned OpIdx, uint64_t &Value,
67 raw_ostream &OS) const;
68 void EmitDst(const MCInst &MI, raw_ostream &OS) const;
69 void EmitTexInstr(const MCInst &MI, SmallVectorImpl<MCFixup> &Fixups,
70 raw_ostream &OS) const;
71 void EmitFCInstr(const MCInst &MI, raw_ostream &OS) const;
73 void EmitNullBytes(unsigned int byteCount, raw_ostream &OS) const;
75 void EmitByte(unsigned int byte, raw_ostream &OS) const;
77 void EmitTwoBytes(uint32_t bytes, raw_ostream &OS) const;
79 void Emit(uint32_t value, raw_ostream &OS) const;
80 void Emit(uint64_t value, raw_ostream &OS) const;
82 unsigned getHWRegChan(unsigned reg) const;
83 unsigned getHWReg(unsigned regNo) const;
85 bool isFCOp(unsigned opcode) const;
86 bool isTexOp(unsigned opcode) const;
87 bool isFlagSet(const MCInst &MI, unsigned Operand, unsigned Flag) const;
91 } // End anonymous namespace
130 TEXTURE_SHADOW1D_ARRAY,
131 TEXTURE_SHADOW2D_ARRAY
134 MCCodeEmitter *llvm::createR600MCCodeEmitter(const MCInstrInfo &MCII,
135 const MCRegisterInfo &MRI,
136 const MCSubtargetInfo &STI,
138 return new R600MCCodeEmitter(MCII, MRI, STI, Ctx);
141 void R600MCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
142 SmallVectorImpl<MCFixup> &Fixups) const {
143 if (isTexOp(MI.getOpcode())) {
144 EmitTexInstr(MI, Fixups, OS);
145 } else if (isFCOp(MI.getOpcode())){
147 } else if (MI.getOpcode() == AMDGPU::RETURN ||
148 MI.getOpcode() == AMDGPU::BUNDLE ||
149 MI.getOpcode() == AMDGPU::KILL) {
152 switch(MI.getOpcode()) {
153 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
154 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
155 uint64_t inst = getBinaryCodeForInstr(MI, Fixups);
156 EmitByte(INSTR_NATIVE, OS);
160 case AMDGPU::CONSTANT_LOAD_eg:
161 case AMDGPU::VTX_READ_PARAM_8_eg:
162 case AMDGPU::VTX_READ_PARAM_16_eg:
163 case AMDGPU::VTX_READ_PARAM_32_eg:
164 case AMDGPU::VTX_READ_GLOBAL_8_eg:
165 case AMDGPU::VTX_READ_GLOBAL_32_eg:
166 case AMDGPU::VTX_READ_GLOBAL_128_eg: {
167 uint64_t InstWord01 = getBinaryCodeForInstr(MI, Fixups);
168 uint32_t InstWord2 = MI.getOperand(2).getImm(); // Offset
170 EmitByte(INSTR_VTX, OS);
171 Emit(InstWord01, OS);
175 case AMDGPU::EG_ExportSwz:
176 case AMDGPU::R600_ExportSwz:
177 case AMDGPU::EG_ExportBuf:
178 case AMDGPU::R600_ExportBuf: {
179 uint64_t Inst = getBinaryCodeForInstr(MI, Fixups);
180 EmitByte(INSTR_EXPORT, OS);
186 EmitALUInstr(MI, Fixups, OS);
192 void R600MCCodeEmitter::EmitALUInstr(const MCInst &MI,
193 SmallVectorImpl<MCFixup> &Fixups,
194 raw_ostream &OS) const {
195 const MCInstrDesc &MCDesc = MCII.get(MI.getOpcode());
196 unsigned NumOperands = MI.getNumOperands();
198 // Emit instruction type
199 EmitByte(INSTR_ALU, OS);
201 uint64_t InstWord01 = getBinaryCodeForInstr(MI, Fixups);
203 //older alu have different encoding for instructions with one or two src
205 if ((STI.getFeatureBits() & AMDGPU::FeatureR600ALUInst) &&
206 !(MCDesc.TSFlags & R600_InstFlag::OP3)) {
207 uint64_t ISAOpCode = InstWord01 & (0x3FFULL << 39);
208 InstWord01 &= ~(0x3FFULL << 39);
209 InstWord01 |= ISAOpCode << 1;
213 for (unsigned int OpIdx = 1; OpIdx < NumOperands; ++OpIdx) {
214 if (MI.getOperand(OpIdx).isImm() || MI.getOperand(OpIdx).isFPImm() ||
215 OpIdx == (unsigned)MCDesc.findFirstPredOperandIdx()) {
218 EmitSrcISA(MI, OpIdx, InstWord01, OS);
222 // Emit zeros for unused sources
223 for ( ; SrcIdx < 3; SrcIdx++) {
224 EmitNullBytes(SRC_BYTE_COUNT - 6, OS);
227 Emit(InstWord01, OS);
231 void R600MCCodeEmitter::EmitSrc(const MCInst &MI, unsigned OpIdx,
232 raw_ostream &OS) const {
233 const MCOperand &MO = MI.getOperand(OpIdx);
239 // Emit the source select (2 bytes). For GPRs, this is the register index.
240 // For other potential instruction operands, (e.g. constant registers) the
241 // value of the source select is defined in the r600isa docs.
243 unsigned reg = MO.getReg();
244 EmitTwoBytes(getHWReg(reg), OS);
245 if (reg == AMDGPU::ALU_LITERAL_X) {
246 unsigned ImmOpIndex = MI.getNumOperands() - 1;
247 MCOperand ImmOp = MI.getOperand(ImmOpIndex);
248 if (ImmOp.isFPImm()) {
249 Value.f = ImmOp.getFPImm();
251 assert(ImmOp.isImm());
252 Value.i = ImmOp.getImm();
256 // XXX: Handle other operand types.
260 // Emit the source channel (1 byte)
262 EmitByte(getHWRegChan(MO.getReg()), OS);
267 // XXX: Emit isNegated (1 byte)
268 if ((!(isFlagSet(MI, OpIdx, MO_FLAG_ABS)))
269 && (isFlagSet(MI, OpIdx, MO_FLAG_NEG) ||
271 (MO.getReg() == AMDGPU::NEG_ONE || MO.getReg() == AMDGPU::NEG_HALF)))){
277 // Emit isAbsolute (1 byte)
278 if (isFlagSet(MI, OpIdx, MO_FLAG_ABS)) {
284 // XXX: Emit relative addressing mode (1 byte)
287 // Emit kc_bank, This will be adjusted later by r600_asm
290 // Emit the literal value, if applicable (4 bytes).
295 void R600MCCodeEmitter::EmitSrcISA(const MCInst &MI, unsigned OpIdx,
296 uint64_t &Value, raw_ostream &OS) const {
297 const MCOperand &MO = MI.getOperand(OpIdx);
302 InlineConstant.i = 0;
303 // Emit the source select (2 bytes). For GPRs, this is the register index.
304 // For other potential instruction operands, (e.g. constant registers) the
305 // value of the source select is defined in the r600isa docs.
307 unsigned Reg = MO.getReg();
308 if (AMDGPUMCRegisterClasses[AMDGPU::R600_CReg32RegClassID].contains(Reg)) {
314 if (Reg == AMDGPU::ALU_LITERAL_X) {
315 unsigned ImmOpIndex = MI.getNumOperands() - 1;
316 MCOperand ImmOp = MI.getOperand(ImmOpIndex);
317 if (ImmOp.isFPImm()) {
318 InlineConstant.f = ImmOp.getFPImm();
320 assert(ImmOp.isImm());
321 InlineConstant.i = ImmOp.getImm();
326 // Emit the literal value, if applicable (4 bytes).
327 Emit(InlineConstant.i, OS);
330 void R600MCCodeEmitter::EmitTexInstr(const MCInst &MI,
331 SmallVectorImpl<MCFixup> &Fixups,
332 raw_ostream &OS) const {
334 unsigned Opcode = MI.getOpcode();
335 bool hasOffsets = (Opcode == AMDGPU::TEX_LD);
336 unsigned OpOffset = hasOffsets ? 3 : 0;
337 int64_t Resource = MI.getOperand(OpOffset + 2).getImm();
338 int64_t Sampler = MI.getOperand(OpOffset + 3).getImm();
339 int64_t TextureType = MI.getOperand(OpOffset + 4).getImm();
340 unsigned srcSelect[4] = {0, 1, 2, 3};
342 // Emit instruction type
346 EmitByte(getBinaryCodeForInstr(MI, Fixups), OS);
349 EmitByte(Resource, OS);
351 // Emit source register
352 EmitByte(getHWReg(MI.getOperand(1).getReg()), OS);
354 // XXX: Emit src isRelativeAddress
357 // Emit destination register
358 EmitByte(getHWReg(MI.getOperand(0).getReg()), OS);
360 // XXX: Emit dst isRealtiveAddress
363 // XXX: Emit dst select
364 EmitByte(0, OS); // X
365 EmitByte(1, OS); // Y
366 EmitByte(2, OS); // Z
367 EmitByte(3, OS); // W
369 // XXX: Emit lod bias
372 // XXX: Emit coord types
373 unsigned coordType[4] = {1, 1, 1, 1};
375 if (TextureType == TEXTURE_RECT
376 || TextureType == TEXTURE_SHADOWRECT) {
377 coordType[ELEMENT_X] = 0;
378 coordType[ELEMENT_Y] = 0;
381 if (TextureType == TEXTURE_1D_ARRAY
382 || TextureType == TEXTURE_SHADOW1D_ARRAY) {
383 if (Opcode == AMDGPU::TEX_SAMPLE_C_L || Opcode == AMDGPU::TEX_SAMPLE_C_LB) {
384 coordType[ELEMENT_Y] = 0;
386 coordType[ELEMENT_Z] = 0;
387 srcSelect[ELEMENT_Z] = ELEMENT_Y;
389 } else if (TextureType == TEXTURE_2D_ARRAY
390 || TextureType == TEXTURE_SHADOW2D_ARRAY) {
391 coordType[ELEMENT_Z] = 0;
394 for (unsigned i = 0; i < 4; i++) {
395 EmitByte(coordType[i], OS);
400 for (unsigned i = 2; i < 5; i++)
401 EmitByte(MI.getOperand(i).getImm()<<1, OS);
403 EmitNullBytes(3, OS);
406 EmitByte(Sampler, OS);
408 // XXX:Emit source select
409 if ((TextureType == TEXTURE_SHADOW1D
410 || TextureType == TEXTURE_SHADOW2D
411 || TextureType == TEXTURE_SHADOWRECT
412 || TextureType == TEXTURE_SHADOW1D_ARRAY)
413 && Opcode != AMDGPU::TEX_SAMPLE_C_L
414 && Opcode != AMDGPU::TEX_SAMPLE_C_LB) {
415 srcSelect[ELEMENT_W] = ELEMENT_Z;
418 for (unsigned i = 0; i < 4; i++) {
419 EmitByte(srcSelect[i], OS);
423 void R600MCCodeEmitter::EmitFCInstr(const MCInst &MI, raw_ostream &OS) const {
425 // Emit instruction type
426 EmitByte(INSTR_FC, OS);
429 unsigned NumOperands = MI.getNumOperands();
430 if (NumOperands > 0) {
431 assert(NumOperands == 1);
434 EmitNullBytes(SRC_BYTE_COUNT, OS);
437 // Emit FC Instruction
439 switch (MI.getOpcode()) {
440 case AMDGPU::PREDICATED_BREAK:
441 instr = FC_BREAK_PREDICATE;
443 case AMDGPU::CONTINUE:
446 case AMDGPU::IF_PREDICATE_SET:
447 instr = FC_IF_PREDICATE;
455 case AMDGPU::ENDLOOP:
458 case AMDGPU::WHILELOOP:
468 void R600MCCodeEmitter::EmitNullBytes(unsigned int ByteCount,
469 raw_ostream &OS) const {
471 for (unsigned int i = 0; i < ByteCount; i++) {
476 void R600MCCodeEmitter::EmitByte(unsigned int Byte, raw_ostream &OS) const {
477 OS.write((uint8_t) Byte & 0xff);
480 void R600MCCodeEmitter::EmitTwoBytes(unsigned int Bytes,
481 raw_ostream &OS) const {
482 OS.write((uint8_t) (Bytes & 0xff));
483 OS.write((uint8_t) ((Bytes >> 8) & 0xff));
486 void R600MCCodeEmitter::Emit(uint32_t Value, raw_ostream &OS) const {
487 for (unsigned i = 0; i < 4; i++) {
488 OS.write((uint8_t) ((Value >> (8 * i)) & 0xff));
492 void R600MCCodeEmitter::Emit(uint64_t Value, raw_ostream &OS) const {
493 for (unsigned i = 0; i < 8; i++) {
494 EmitByte((Value >> (8 * i)) & 0xff, OS);
498 unsigned R600MCCodeEmitter::getHWRegChan(unsigned reg) const {
499 return MRI.getEncodingValue(reg) >> HW_CHAN_SHIFT;
502 unsigned R600MCCodeEmitter::getHWReg(unsigned RegNo) const {
503 return MRI.getEncodingValue(RegNo) & HW_REG_MASK;
506 uint64_t R600MCCodeEmitter::getMachineOpValue(const MCInst &MI,
508 SmallVectorImpl<MCFixup> &Fixup) const {
510 if (HAS_NATIVE_OPERANDS(MCII.get(MI.getOpcode()).TSFlags)) {
511 return MRI.getEncodingValue(MO.getReg());
513 return getHWReg(MO.getReg());
515 } else if (MO.isImm()) {
523 //===----------------------------------------------------------------------===//
524 // Encoding helper functions
525 //===----------------------------------------------------------------------===//
527 bool R600MCCodeEmitter::isFCOp(unsigned opcode) const {
529 default: return false;
530 case AMDGPU::PREDICATED_BREAK:
531 case AMDGPU::CONTINUE:
532 case AMDGPU::IF_PREDICATE_SET:
535 case AMDGPU::ENDLOOP:
536 case AMDGPU::WHILELOOP:
541 bool R600MCCodeEmitter::isTexOp(unsigned opcode) const {
543 default: return false;
545 case AMDGPU::TEX_GET_TEXTURE_RESINFO:
546 case AMDGPU::TEX_SAMPLE:
547 case AMDGPU::TEX_SAMPLE_C:
548 case AMDGPU::TEX_SAMPLE_L:
549 case AMDGPU::TEX_SAMPLE_C_L:
550 case AMDGPU::TEX_SAMPLE_LB:
551 case AMDGPU::TEX_SAMPLE_C_LB:
552 case AMDGPU::TEX_SAMPLE_G:
553 case AMDGPU::TEX_SAMPLE_C_G:
554 case AMDGPU::TEX_GET_GRADIENTS_H:
555 case AMDGPU::TEX_GET_GRADIENTS_V:
556 case AMDGPU::TEX_SET_GRADIENTS_H:
557 case AMDGPU::TEX_SET_GRADIENTS_V:
562 bool R600MCCodeEmitter::isFlagSet(const MCInst &MI, unsigned Operand,
563 unsigned Flag) const {
564 const MCInstrDesc &MCDesc = MCII.get(MI.getOpcode());
565 unsigned FlagIndex = GET_FLAG_OPERAND_IDX(MCDesc.TSFlags);
566 if (FlagIndex == 0) {
569 assert(MI.getOperand(FlagIndex).isImm());
570 return !!((MI.getOperand(FlagIndex).getImm() >>
571 (NUM_MO_FLAGS * Operand)) & Flag);
574 #include "AMDGPUGenMCCodeEmitter.inc"