blob: 36deae9c0aba84ef028455c9a63f94d1aed9b616 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===- R600MCCodeEmitter.cpp - Code Emitter for R600->Cayman GPU families -===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11///
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
15/// by the GPU.
16///
17/// [1] http://www.mesa3d.org/
18//
19//===----------------------------------------------------------------------===//
20
21#include "R600Defines.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000022#include "MCTargetDesc/AMDGPUMCCodeEmitter.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000023#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000024#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"
Tom Stellard75aadc22012-12-11 21:25:42 +000031#include <stdio.h>
32
33#define SRC_BYTE_COUNT 11
34#define DST_BYTE_COUNT 5
35
36using namespace llvm;
37
38namespace {
39
40class 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;
46 MCContext &Ctx;
47
48public:
49
50 R600MCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
51 const MCSubtargetInfo &sti, MCContext &ctx)
52 : MCII(mcii), MRI(mri), STI(sti), Ctx(ctx) { }
53
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;
57
58 /// \returns the encoding for an MCOperand.
59 virtual uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
60 SmallVectorImpl<MCFixup> &Fixups) const;
61private:
62
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;
72
73 void EmitNullBytes(unsigned int byteCount, raw_ostream &OS) const;
74
75 void EmitByte(unsigned int byte, raw_ostream &OS) const;
76
77 void EmitTwoBytes(uint32_t bytes, raw_ostream &OS) const;
78
79 void Emit(uint32_t value, raw_ostream &OS) const;
80 void Emit(uint64_t value, raw_ostream &OS) const;
81
82 unsigned getHWRegChan(unsigned reg) const;
83 unsigned getHWReg(unsigned regNo) const;
84
85 bool isFCOp(unsigned opcode) const;
86 bool isTexOp(unsigned opcode) const;
87 bool isFlagSet(const MCInst &MI, unsigned Operand, unsigned Flag) const;
88
89};
90
91} // End anonymous namespace
92
93enum RegElement {
94 ELEMENT_X = 0,
95 ELEMENT_Y,
96 ELEMENT_Z,
97 ELEMENT_W
98};
99
100enum InstrTypes {
101 INSTR_ALU = 0,
102 INSTR_TEX,
103 INSTR_FC,
104 INSTR_NATIVE,
105 INSTR_VTX,
106 INSTR_EXPORT
107};
108
109enum FCInstr {
110 FC_IF_PREDICATE = 0,
111 FC_ELSE,
112 FC_ENDIF,
113 FC_BGNLOOP,
114 FC_ENDLOOP,
115 FC_BREAK_PREDICATE,
116 FC_CONTINUE
117};
118
119enum TextureTypes {
120 TEXTURE_1D = 1,
121 TEXTURE_2D,
122 TEXTURE_3D,
123 TEXTURE_CUBE,
124 TEXTURE_RECT,
125 TEXTURE_SHADOW1D,
126 TEXTURE_SHADOW2D,
127 TEXTURE_SHADOWRECT,
128 TEXTURE_1D_ARRAY,
129 TEXTURE_2D_ARRAY,
130 TEXTURE_SHADOW1D_ARRAY,
131 TEXTURE_SHADOW2D_ARRAY
132};
133
134MCCodeEmitter *llvm::createR600MCCodeEmitter(const MCInstrInfo &MCII,
135 const MCRegisterInfo &MRI,
136 const MCSubtargetInfo &STI,
137 MCContext &Ctx) {
138 return new R600MCCodeEmitter(MCII, MRI, STI, Ctx);
139}
140
141void 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())){
146 EmitFCInstr(MI, OS);
147 } else if (MI.getOpcode() == AMDGPU::RETURN ||
148 MI.getOpcode() == AMDGPU::BUNDLE ||
149 MI.getOpcode() == AMDGPU::KILL) {
150 return;
151 } else {
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);
157 Emit(inst, OS);
158 break;
159 }
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
169
170 EmitByte(INSTR_VTX, OS);
171 Emit(InstWord01, OS);
172 Emit(InstWord2, OS);
173 break;
174 }
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);
181 Emit(Inst, OS);
182 break;
183 }
184
185 default:
186 EmitALUInstr(MI, Fixups, OS);
187 break;
188 }
189 }
190}
191
192void 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();
197
198 // Emit instruction type
199 EmitByte(INSTR_ALU, OS);
200
201 uint64_t InstWord01 = getBinaryCodeForInstr(MI, Fixups);
202
203 //older alu have different encoding for instructions with one or two src
204 //parameters.
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;
210 }
211
212 unsigned SrcIdx = 0;
213 for (unsigned int OpIdx = 1; OpIdx < NumOperands; ++OpIdx) {
214 if (MI.getOperand(OpIdx).isImm() || MI.getOperand(OpIdx).isFPImm() ||
215 OpIdx == (unsigned)MCDesc.findFirstPredOperandIdx()) {
216 continue;
217 }
218 EmitSrcISA(MI, OpIdx, InstWord01, OS);
219 SrcIdx++;
220 }
221
222 // Emit zeros for unused sources
223 for ( ; SrcIdx < 3; SrcIdx++) {
224 EmitNullBytes(SRC_BYTE_COUNT - 6, OS);
225 }
226
227 Emit(InstWord01, OS);
228 return;
229}
230
231void R600MCCodeEmitter::EmitSrc(const MCInst &MI, unsigned OpIdx,
232 raw_ostream &OS) const {
233 const MCOperand &MO = MI.getOperand(OpIdx);
234 union {
235 float f;
236 uint32_t i;
237 } Value;
238 Value.i = 0;
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.
242 if (MO.isReg()) {
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();
250 } else {
251 assert(ImmOp.isImm());
252 Value.i = ImmOp.getImm();
253 }
254 }
255 } else {
256 // XXX: Handle other operand types.
257 EmitTwoBytes(0, OS);
258 }
259
260 // Emit the source channel (1 byte)
261 if (MO.isReg()) {
262 EmitByte(getHWRegChan(MO.getReg()), OS);
263 } else {
264 EmitByte(0, OS);
265 }
266
267 // XXX: Emit isNegated (1 byte)
268 if ((!(isFlagSet(MI, OpIdx, MO_FLAG_ABS)))
269 && (isFlagSet(MI, OpIdx, MO_FLAG_NEG) ||
270 (MO.isReg() &&
271 (MO.getReg() == AMDGPU::NEG_ONE || MO.getReg() == AMDGPU::NEG_HALF)))){
272 EmitByte(1, OS);
273 } else {
274 EmitByte(0, OS);
275 }
276
277 // Emit isAbsolute (1 byte)
278 if (isFlagSet(MI, OpIdx, MO_FLAG_ABS)) {
279 EmitByte(1, OS);
280 } else {
281 EmitByte(0, OS);
282 }
283
284 // XXX: Emit relative addressing mode (1 byte)
285 EmitByte(0, OS);
286
287 // Emit kc_bank, This will be adjusted later by r600_asm
288 EmitByte(0, OS);
289
290 // Emit the literal value, if applicable (4 bytes).
291 Emit(Value.i, OS);
292
293}
294
295void R600MCCodeEmitter::EmitSrcISA(const MCInst &MI, unsigned OpIdx,
296 uint64_t &Value, raw_ostream &OS) const {
297 const MCOperand &MO = MI.getOperand(OpIdx);
298 union {
299 float f;
300 uint32_t i;
301 } InlineConstant;
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.
306 if (MO.isReg()) {
307 unsigned Reg = MO.getReg();
308 if (AMDGPUMCRegisterClasses[AMDGPU::R600_CReg32RegClassID].contains(Reg)) {
309 EmitByte(1, OS);
310 } else {
311 EmitByte(0, OS);
312 }
313
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();
319 } else {
320 assert(ImmOp.isImm());
321 InlineConstant.i = ImmOp.getImm();
322 }
323 }
324 }
325
326 // Emit the literal value, if applicable (4 bytes).
327 Emit(InlineConstant.i, OS);
328}
329
330void R600MCCodeEmitter::EmitTexInstr(const MCInst &MI,
331 SmallVectorImpl<MCFixup> &Fixups,
332 raw_ostream &OS) const {
333
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};
341
342 // Emit instruction type
343 EmitByte(1, OS);
344
345 // Emit instruction
346 EmitByte(getBinaryCodeForInstr(MI, Fixups), OS);
347
348 // Emit resource id
349 EmitByte(Resource, OS);
350
351 // Emit source register
352 EmitByte(getHWReg(MI.getOperand(1).getReg()), OS);
353
354 // XXX: Emit src isRelativeAddress
355 EmitByte(0, OS);
356
357 // Emit destination register
358 EmitByte(getHWReg(MI.getOperand(0).getReg()), OS);
359
360 // XXX: Emit dst isRealtiveAddress
361 EmitByte(0, OS);
362
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
368
369 // XXX: Emit lod bias
370 EmitByte(0, OS);
371
372 // XXX: Emit coord types
373 unsigned coordType[4] = {1, 1, 1, 1};
374
375 if (TextureType == TEXTURE_RECT
376 || TextureType == TEXTURE_SHADOWRECT) {
377 coordType[ELEMENT_X] = 0;
378 coordType[ELEMENT_Y] = 0;
379 }
380
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;
385 } else {
386 coordType[ELEMENT_Z] = 0;
387 srcSelect[ELEMENT_Z] = ELEMENT_Y;
388 }
389 } else if (TextureType == TEXTURE_2D_ARRAY
390 || TextureType == TEXTURE_SHADOW2D_ARRAY) {
391 coordType[ELEMENT_Z] = 0;
392 }
393
394 for (unsigned i = 0; i < 4; i++) {
395 EmitByte(coordType[i], OS);
396 }
397
398 // XXX: Emit offsets
399 if (hasOffsets)
400 for (unsigned i = 2; i < 5; i++)
401 EmitByte(MI.getOperand(i).getImm()<<1, OS);
402 else
403 EmitNullBytes(3, OS);
404
405 // Emit sampler id
406 EmitByte(Sampler, OS);
407
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;
416 }
417
418 for (unsigned i = 0; i < 4; i++) {
419 EmitByte(srcSelect[i], OS);
420 }
421}
422
423void R600MCCodeEmitter::EmitFCInstr(const MCInst &MI, raw_ostream &OS) const {
424
425 // Emit instruction type
426 EmitByte(INSTR_FC, OS);
427
428 // Emit SRC
429 unsigned NumOperands = MI.getNumOperands();
430 if (NumOperands > 0) {
431 assert(NumOperands == 1);
432 EmitSrc(MI, 0, OS);
433 } else {
434 EmitNullBytes(SRC_BYTE_COUNT, OS);
435 }
436
437 // Emit FC Instruction
438 enum FCInstr instr;
439 switch (MI.getOpcode()) {
440 case AMDGPU::PREDICATED_BREAK:
441 instr = FC_BREAK_PREDICATE;
442 break;
443 case AMDGPU::CONTINUE:
444 instr = FC_CONTINUE;
445 break;
446 case AMDGPU::IF_PREDICATE_SET:
447 instr = FC_IF_PREDICATE;
448 break;
449 case AMDGPU::ELSE:
450 instr = FC_ELSE;
451 break;
452 case AMDGPU::ENDIF:
453 instr = FC_ENDIF;
454 break;
455 case AMDGPU::ENDLOOP:
456 instr = FC_ENDLOOP;
457 break;
458 case AMDGPU::WHILELOOP:
459 instr = FC_BGNLOOP;
460 break;
461 default:
462 abort();
463 break;
464 }
465 EmitByte(instr, OS);
466}
467
468void R600MCCodeEmitter::EmitNullBytes(unsigned int ByteCount,
469 raw_ostream &OS) const {
470
471 for (unsigned int i = 0; i < ByteCount; i++) {
472 EmitByte(0, OS);
473 }
474}
475
476void R600MCCodeEmitter::EmitByte(unsigned int Byte, raw_ostream &OS) const {
477 OS.write((uint8_t) Byte & 0xff);
478}
479
480void 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));
484}
485
486void 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));
489 }
490}
491
492void R600MCCodeEmitter::Emit(uint64_t Value, raw_ostream &OS) const {
493 for (unsigned i = 0; i < 8; i++) {
494 EmitByte((Value >> (8 * i)) & 0xff, OS);
495 }
496}
497
498unsigned R600MCCodeEmitter::getHWRegChan(unsigned reg) const {
499 return MRI.getEncodingValue(reg) >> HW_CHAN_SHIFT;
500}
501
502unsigned R600MCCodeEmitter::getHWReg(unsigned RegNo) const {
503 return MRI.getEncodingValue(RegNo) & HW_REG_MASK;
504}
505
506uint64_t R600MCCodeEmitter::getMachineOpValue(const MCInst &MI,
507 const MCOperand &MO,
508 SmallVectorImpl<MCFixup> &Fixup) const {
509 if (MO.isReg()) {
510 if (HAS_NATIVE_OPERANDS(MCII.get(MI.getOpcode()).TSFlags)) {
511 return MRI.getEncodingValue(MO.getReg());
512 } else {
513 return getHWReg(MO.getReg());
514 }
515 } else if (MO.isImm()) {
516 return MO.getImm();
517 } else {
518 assert(0);
519 return 0;
520 }
521}
522
523//===----------------------------------------------------------------------===//
524// Encoding helper functions
525//===----------------------------------------------------------------------===//
526
527bool R600MCCodeEmitter::isFCOp(unsigned opcode) const {
528 switch(opcode) {
529 default: return false;
530 case AMDGPU::PREDICATED_BREAK:
531 case AMDGPU::CONTINUE:
532 case AMDGPU::IF_PREDICATE_SET:
533 case AMDGPU::ELSE:
534 case AMDGPU::ENDIF:
535 case AMDGPU::ENDLOOP:
536 case AMDGPU::WHILELOOP:
537 return true;
538 }
539}
540
541bool R600MCCodeEmitter::isTexOp(unsigned opcode) const {
542 switch(opcode) {
543 default: return false;
544 case AMDGPU::TEX_LD:
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:
558 return true;
559 }
560}
561
562bool 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) {
567 return false;
568 }
569 assert(MI.getOperand(FlagIndex).isImm());
570 return !!((MI.getOperand(FlagIndex).getImm() >>
571 (NUM_MO_FLAGS * Operand)) & Flag);
572}
573
574#include "AMDGPUGenMCCodeEmitter.inc"