blob: 4864b3e1b7cd883abac48fd59f6c298ebaf7c276 [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 {
David Blaikie772d4f72013-02-18 23:11:17 +000041 R600MCCodeEmitter(const R600MCCodeEmitter &) LLVM_DELETED_FUNCTION;
42 void operator=(const R600MCCodeEmitter &) LLVM_DELETED_FUNCTION;
Tom Stellard75aadc22012-12-11 21:25:42 +000043 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;
Tom Stellard365366f2013-01-23 02:09:06 +000066 void EmitSrcISA(const MCInst &MI, unsigned RegOpIdx, unsigned SelOpIdx,
67 raw_ostream &OS) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000068 void EmitDst(const MCInst &MI, raw_ostream &OS) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000069 void EmitFCInstr(const MCInst &MI, raw_ostream &OS) const;
70
71 void EmitNullBytes(unsigned int byteCount, raw_ostream &OS) const;
72
73 void EmitByte(unsigned int byte, raw_ostream &OS) const;
74
75 void EmitTwoBytes(uint32_t bytes, raw_ostream &OS) const;
76
77 void Emit(uint32_t value, raw_ostream &OS) const;
78 void Emit(uint64_t value, raw_ostream &OS) const;
79
80 unsigned getHWRegChan(unsigned reg) const;
81 unsigned getHWReg(unsigned regNo) const;
82
83 bool isFCOp(unsigned opcode) const;
84 bool isTexOp(unsigned opcode) const;
85 bool isFlagSet(const MCInst &MI, unsigned Operand, unsigned Flag) const;
86
87};
88
89} // End anonymous namespace
90
91enum RegElement {
92 ELEMENT_X = 0,
93 ELEMENT_Y,
94 ELEMENT_Z,
95 ELEMENT_W
96};
97
98enum InstrTypes {
99 INSTR_ALU = 0,
100 INSTR_TEX,
101 INSTR_FC,
102 INSTR_NATIVE,
103 INSTR_VTX,
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000104 INSTR_EXPORT,
105 INSTR_CFALU
Tom Stellard75aadc22012-12-11 21:25:42 +0000106};
107
108enum FCInstr {
109 FC_IF_PREDICATE = 0,
110 FC_ELSE,
111 FC_ENDIF,
112 FC_BGNLOOP,
113 FC_ENDLOOP,
114 FC_BREAK_PREDICATE,
115 FC_CONTINUE
116};
117
118enum TextureTypes {
119 TEXTURE_1D = 1,
120 TEXTURE_2D,
121 TEXTURE_3D,
122 TEXTURE_CUBE,
123 TEXTURE_RECT,
124 TEXTURE_SHADOW1D,
125 TEXTURE_SHADOW2D,
126 TEXTURE_SHADOWRECT,
127 TEXTURE_1D_ARRAY,
128 TEXTURE_2D_ARRAY,
129 TEXTURE_SHADOW1D_ARRAY,
130 TEXTURE_SHADOW2D_ARRAY
131};
132
133MCCodeEmitter *llvm::createR600MCCodeEmitter(const MCInstrInfo &MCII,
134 const MCRegisterInfo &MRI,
135 const MCSubtargetInfo &STI,
136 MCContext &Ctx) {
137 return new R600MCCodeEmitter(MCII, MRI, STI, Ctx);
138}
139
140void R600MCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
141 SmallVectorImpl<MCFixup> &Fixups) const {
Vincent Lejeune53f35252013-03-31 19:33:04 +0000142 if (isFCOp(MI.getOpcode())){
Tom Stellard75aadc22012-12-11 21:25:42 +0000143 EmitFCInstr(MI, OS);
144 } else if (MI.getOpcode() == AMDGPU::RETURN ||
145 MI.getOpcode() == AMDGPU::BUNDLE ||
146 MI.getOpcode() == AMDGPU::KILL) {
147 return;
148 } else {
149 switch(MI.getOpcode()) {
Vincent Lejeunebfaa63a62013-04-01 21:48:05 +0000150 case AMDGPU::STACK_SIZE: {
151 EmitByte(MI.getOperand(0).getImm(), OS);
152 break;
153 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000154 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
155 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
156 uint64_t inst = getBinaryCodeForInstr(MI, Fixups);
157 EmitByte(INSTR_NATIVE, OS);
158 Emit(inst, OS);
159 break;
160 }
161 case AMDGPU::CONSTANT_LOAD_eg:
162 case AMDGPU::VTX_READ_PARAM_8_eg:
163 case AMDGPU::VTX_READ_PARAM_16_eg:
164 case AMDGPU::VTX_READ_PARAM_32_eg:
Tom Stellard91da4e92013-02-13 22:05:20 +0000165 case AMDGPU::VTX_READ_PARAM_128_eg:
Tom Stellard75aadc22012-12-11 21:25:42 +0000166 case AMDGPU::VTX_READ_GLOBAL_8_eg:
167 case AMDGPU::VTX_READ_GLOBAL_32_eg:
Tom Stellard365366f2013-01-23 02:09:06 +0000168 case AMDGPU::VTX_READ_GLOBAL_128_eg:
Vincent Lejeune68501802013-02-18 14:11:19 +0000169 case AMDGPU::TEX_VTX_CONSTBUF:
170 case AMDGPU::TEX_VTX_TEXBUF : {
Tom Stellard75aadc22012-12-11 21:25:42 +0000171 uint64_t InstWord01 = getBinaryCodeForInstr(MI, Fixups);
172 uint32_t InstWord2 = MI.getOperand(2).getImm(); // Offset
173
174 EmitByte(INSTR_VTX, OS);
175 Emit(InstWord01, OS);
176 Emit(InstWord2, OS);
177 break;
178 }
Vincent Lejeune53f35252013-03-31 19:33:04 +0000179 case AMDGPU::TEX_LD:
180 case AMDGPU::TEX_GET_TEXTURE_RESINFO:
181 case AMDGPU::TEX_SAMPLE:
182 case AMDGPU::TEX_SAMPLE_C:
183 case AMDGPU::TEX_SAMPLE_L:
184 case AMDGPU::TEX_SAMPLE_C_L:
185 case AMDGPU::TEX_SAMPLE_LB:
186 case AMDGPU::TEX_SAMPLE_C_LB:
187 case AMDGPU::TEX_SAMPLE_G:
188 case AMDGPU::TEX_SAMPLE_C_G:
189 case AMDGPU::TEX_GET_GRADIENTS_H:
190 case AMDGPU::TEX_GET_GRADIENTS_V:
191 case AMDGPU::TEX_SET_GRADIENTS_H:
192 case AMDGPU::TEX_SET_GRADIENTS_V: {
193 unsigned Opcode = MI.getOpcode();
194 bool HasOffsets = (Opcode == AMDGPU::TEX_LD);
195 unsigned OpOffset = HasOffsets ? 3 : 0;
196 int64_t Sampler = MI.getOperand(OpOffset + 3).getImm();
197 int64_t TextureType = MI.getOperand(OpOffset + 4).getImm();
198
199 uint32_t SrcSelect[4] = {0, 1, 2, 3};
200 uint32_t Offsets[3] = {0, 0, 0};
201 uint64_t CoordType[4] = {1, 1, 1, 1};
202
203 if (HasOffsets)
Vincent Lejeunebcbb13d2013-04-04 14:00:09 +0000204 for (unsigned i = 0; i < 3; i++) {
205 int SignedOffset = MI.getOperand(i + 2).getImm();
206 Offsets[i] = (SignedOffset & 0x1F);
207 }
208
Vincent Lejeune53f35252013-03-31 19:33:04 +0000209
210 if (TextureType == TEXTURE_RECT ||
211 TextureType == TEXTURE_SHADOWRECT) {
212 CoordType[ELEMENT_X] = 0;
213 CoordType[ELEMENT_Y] = 0;
214 }
215
216 if (TextureType == TEXTURE_1D_ARRAY ||
217 TextureType == TEXTURE_SHADOW1D_ARRAY) {
218 if (Opcode == AMDGPU::TEX_SAMPLE_C_L ||
219 Opcode == AMDGPU::TEX_SAMPLE_C_LB) {
220 CoordType[ELEMENT_Y] = 0;
221 } else {
222 CoordType[ELEMENT_Z] = 0;
223 SrcSelect[ELEMENT_Z] = ELEMENT_Y;
224 }
225 } else if (TextureType == TEXTURE_2D_ARRAY ||
226 TextureType == TEXTURE_SHADOW2D_ARRAY) {
227 CoordType[ELEMENT_Z] = 0;
228 }
229
230
231 if ((TextureType == TEXTURE_SHADOW1D ||
232 TextureType == TEXTURE_SHADOW2D ||
233 TextureType == TEXTURE_SHADOWRECT ||
234 TextureType == TEXTURE_SHADOW1D_ARRAY) &&
235 Opcode != AMDGPU::TEX_SAMPLE_C_L &&
236 Opcode != AMDGPU::TEX_SAMPLE_C_LB) {
237 SrcSelect[ELEMENT_W] = ELEMENT_Z;
238 }
239
240 uint64_t Word01 = getBinaryCodeForInstr(MI, Fixups) |
241 CoordType[ELEMENT_X] << 60 | CoordType[ELEMENT_Y] << 61 |
242 CoordType[ELEMENT_Z] << 62 | CoordType[ELEMENT_W] << 63;
243 uint32_t Word2 = Sampler << 15 | SrcSelect[ELEMENT_X] << 20 |
244 SrcSelect[ELEMENT_Y] << 23 | SrcSelect[ELEMENT_Z] << 26 |
245 SrcSelect[ELEMENT_W] << 29 | Offsets[0] << 0 | Offsets[1] << 5 |
246 Offsets[2] << 10;
247
248 EmitByte(INSTR_TEX, OS);
249 Emit(Word01, OS);
250 Emit(Word2, OS);
251 break;
252 }
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000253 case AMDGPU::CF_ALU:
254 case AMDGPU::CF_ALU_PUSH_BEFORE: {
255 uint64_t Inst = getBinaryCodeForInstr(MI, Fixups);
256 EmitByte(INSTR_CFALU, OS);
257 Emit(Inst, OS);
258 break;
259 }
Vincent Lejeune5f11dd32013-04-08 13:05:49 +0000260 case AMDGPU::CF_TC_EG:
261 case AMDGPU::CF_VC_EG:
262 case AMDGPU::CF_CALL_FS_EG:
263 case AMDGPU::CF_TC_R600:
264 case AMDGPU::CF_VC_R600:
265 case AMDGPU::CF_CALL_FS_R600:
Vincent Lejeunebfaa63a62013-04-01 21:48:05 +0000266 return;
Vincent Lejeune5f11dd32013-04-08 13:05:49 +0000267 case AMDGPU::WHILE_LOOP_EG:
268 case AMDGPU::END_LOOP_EG:
269 case AMDGPU::LOOP_BREAK_EG:
270 case AMDGPU::CF_CONTINUE_EG:
271 case AMDGPU::CF_JUMP_EG:
272 case AMDGPU::CF_ELSE_EG:
273 case AMDGPU::POP_EG:
274 case AMDGPU::WHILE_LOOP_R600:
275 case AMDGPU::END_LOOP_R600:
276 case AMDGPU::LOOP_BREAK_R600:
277 case AMDGPU::CF_CONTINUE_R600:
278 case AMDGPU::CF_JUMP_R600:
279 case AMDGPU::CF_ELSE_R600:
Vincent Lejeune218093e2013-04-17 15:17:32 +0000280 case AMDGPU::POP_R600:
281 case AMDGPU::EG_ExportSwz:
282 case AMDGPU::R600_ExportSwz:
283 case AMDGPU::EG_ExportBuf:
Vincent Lejeuneb6bfe852013-04-23 17:34:00 +0000284 case AMDGPU::R600_ExportBuf:
285 case AMDGPU::PAD:
286 case AMDGPU::CF_END_R600:
287 case AMDGPU::CF_END_EG:
288 case AMDGPU::CF_END_CM: {
Vincent Lejeunebfaa63a62013-04-01 21:48:05 +0000289 uint64_t Inst = getBinaryCodeForInstr(MI, Fixups);
290 EmitByte(INSTR_NATIVE, OS);
291 Emit(Inst, OS);
292 break;
293 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000294 default:
295 EmitALUInstr(MI, Fixups, OS);
296 break;
297 }
298 }
299}
300
301void R600MCCodeEmitter::EmitALUInstr(const MCInst &MI,
302 SmallVectorImpl<MCFixup> &Fixups,
303 raw_ostream &OS) const {
304 const MCInstrDesc &MCDesc = MCII.get(MI.getOpcode());
Tom Stellard75aadc22012-12-11 21:25:42 +0000305
306 // Emit instruction type
307 EmitByte(INSTR_ALU, OS);
308
309 uint64_t InstWord01 = getBinaryCodeForInstr(MI, Fixups);
310
311 //older alu have different encoding for instructions with one or two src
312 //parameters.
313 if ((STI.getFeatureBits() & AMDGPU::FeatureR600ALUInst) &&
314 !(MCDesc.TSFlags & R600_InstFlag::OP3)) {
315 uint64_t ISAOpCode = InstWord01 & (0x3FFULL << 39);
316 InstWord01 &= ~(0x3FFULL << 39);
317 InstWord01 |= ISAOpCode << 1;
318 }
319
Tom Stellard365366f2013-01-23 02:09:06 +0000320 unsigned SrcNum = MCDesc.TSFlags & R600_InstFlag::OP3 ? 3 :
321 MCDesc.TSFlags & R600_InstFlag::OP2 ? 2 : 1;
Tom Stellard75aadc22012-12-11 21:25:42 +0000322
Tom Stellard365366f2013-01-23 02:09:06 +0000323 EmitByte(SrcNum, OS);
324
325 const unsigned SrcOps[3][2] = {
326 {R600Operands::SRC0, R600Operands::SRC0_SEL},
327 {R600Operands::SRC1, R600Operands::SRC1_SEL},
328 {R600Operands::SRC2, R600Operands::SRC2_SEL}
329 };
330
331 for (unsigned SrcIdx = 0; SrcIdx < SrcNum; ++SrcIdx) {
332 unsigned RegOpIdx = R600Operands::ALUOpTable[SrcNum-1][SrcOps[SrcIdx][0]];
333 unsigned SelOpIdx = R600Operands::ALUOpTable[SrcNum-1][SrcOps[SrcIdx][1]];
334 EmitSrcISA(MI, RegOpIdx, SelOpIdx, OS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000335 }
336
337 Emit(InstWord01, OS);
338 return;
339}
340
341void R600MCCodeEmitter::EmitSrc(const MCInst &MI, unsigned OpIdx,
342 raw_ostream &OS) const {
343 const MCOperand &MO = MI.getOperand(OpIdx);
344 union {
345 float f;
346 uint32_t i;
347 } Value;
348 Value.i = 0;
349 // Emit the source select (2 bytes). For GPRs, this is the register index.
350 // For other potential instruction operands, (e.g. constant registers) the
351 // value of the source select is defined in the r600isa docs.
352 if (MO.isReg()) {
353 unsigned reg = MO.getReg();
354 EmitTwoBytes(getHWReg(reg), OS);
355 if (reg == AMDGPU::ALU_LITERAL_X) {
356 unsigned ImmOpIndex = MI.getNumOperands() - 1;
357 MCOperand ImmOp = MI.getOperand(ImmOpIndex);
358 if (ImmOp.isFPImm()) {
359 Value.f = ImmOp.getFPImm();
360 } else {
361 assert(ImmOp.isImm());
362 Value.i = ImmOp.getImm();
363 }
364 }
365 } else {
366 // XXX: Handle other operand types.
367 EmitTwoBytes(0, OS);
368 }
369
370 // Emit the source channel (1 byte)
371 if (MO.isReg()) {
372 EmitByte(getHWRegChan(MO.getReg()), OS);
373 } else {
374 EmitByte(0, OS);
375 }
376
377 // XXX: Emit isNegated (1 byte)
378 if ((!(isFlagSet(MI, OpIdx, MO_FLAG_ABS)))
379 && (isFlagSet(MI, OpIdx, MO_FLAG_NEG) ||
380 (MO.isReg() &&
381 (MO.getReg() == AMDGPU::NEG_ONE || MO.getReg() == AMDGPU::NEG_HALF)))){
382 EmitByte(1, OS);
383 } else {
384 EmitByte(0, OS);
385 }
386
387 // Emit isAbsolute (1 byte)
388 if (isFlagSet(MI, OpIdx, MO_FLAG_ABS)) {
389 EmitByte(1, OS);
390 } else {
391 EmitByte(0, OS);
392 }
393
394 // XXX: Emit relative addressing mode (1 byte)
395 EmitByte(0, OS);
396
397 // Emit kc_bank, This will be adjusted later by r600_asm
398 EmitByte(0, OS);
399
400 // Emit the literal value, if applicable (4 bytes).
401 Emit(Value.i, OS);
402
403}
404
Tom Stellard365366f2013-01-23 02:09:06 +0000405void R600MCCodeEmitter::EmitSrcISA(const MCInst &MI, unsigned RegOpIdx,
406 unsigned SelOpIdx, raw_ostream &OS) const {
407 const MCOperand &RegMO = MI.getOperand(RegOpIdx);
408 const MCOperand &SelMO = MI.getOperand(SelOpIdx);
409
Tom Stellard75aadc22012-12-11 21:25:42 +0000410 union {
411 float f;
412 uint32_t i;
413 } InlineConstant;
414 InlineConstant.i = 0;
Tom Stellard365366f2013-01-23 02:09:06 +0000415 // Emit source type (1 byte) and source select (4 bytes). For GPRs type is 0
416 // and select is 0 (GPR index is encoded in the instr encoding. For constants
417 // type is 1 and select is the original const select passed from the driver.
418 unsigned Reg = RegMO.getReg();
419 if (Reg == AMDGPU::ALU_CONST) {
420 EmitByte(1, OS);
421 uint32_t Sel = SelMO.getImm();
422 Emit(Sel, OS);
423 } else {
424 EmitByte(0, OS);
425 Emit((uint32_t)0, OS);
426 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000427
Tom Stellard365366f2013-01-23 02:09:06 +0000428 if (Reg == AMDGPU::ALU_LITERAL_X) {
429 unsigned ImmOpIndex = MI.getNumOperands() - 1;
430 MCOperand ImmOp = MI.getOperand(ImmOpIndex);
431 if (ImmOp.isFPImm()) {
432 InlineConstant.f = ImmOp.getFPImm();
433 } else {
434 assert(ImmOp.isImm());
435 InlineConstant.i = ImmOp.getImm();
Tom Stellard75aadc22012-12-11 21:25:42 +0000436 }
437 }
438
439 // Emit the literal value, if applicable (4 bytes).
440 Emit(InlineConstant.i, OS);
441}
442
Tom Stellard75aadc22012-12-11 21:25:42 +0000443void R600MCCodeEmitter::EmitFCInstr(const MCInst &MI, raw_ostream &OS) const {
444
445 // Emit instruction type
446 EmitByte(INSTR_FC, OS);
447
448 // Emit SRC
449 unsigned NumOperands = MI.getNumOperands();
450 if (NumOperands > 0) {
451 assert(NumOperands == 1);
452 EmitSrc(MI, 0, OS);
453 } else {
454 EmitNullBytes(SRC_BYTE_COUNT, OS);
455 }
456
457 // Emit FC Instruction
458 enum FCInstr instr;
459 switch (MI.getOpcode()) {
460 case AMDGPU::PREDICATED_BREAK:
461 instr = FC_BREAK_PREDICATE;
462 break;
463 case AMDGPU::CONTINUE:
464 instr = FC_CONTINUE;
465 break;
466 case AMDGPU::IF_PREDICATE_SET:
467 instr = FC_IF_PREDICATE;
468 break;
469 case AMDGPU::ELSE:
470 instr = FC_ELSE;
471 break;
472 case AMDGPU::ENDIF:
473 instr = FC_ENDIF;
474 break;
475 case AMDGPU::ENDLOOP:
476 instr = FC_ENDLOOP;
477 break;
478 case AMDGPU::WHILELOOP:
479 instr = FC_BGNLOOP;
480 break;
481 default:
482 abort();
483 break;
484 }
485 EmitByte(instr, OS);
486}
487
488void R600MCCodeEmitter::EmitNullBytes(unsigned int ByteCount,
489 raw_ostream &OS) const {
490
491 for (unsigned int i = 0; i < ByteCount; i++) {
492 EmitByte(0, OS);
493 }
494}
495
496void R600MCCodeEmitter::EmitByte(unsigned int Byte, raw_ostream &OS) const {
497 OS.write((uint8_t) Byte & 0xff);
498}
499
500void R600MCCodeEmitter::EmitTwoBytes(unsigned int Bytes,
501 raw_ostream &OS) const {
502 OS.write((uint8_t) (Bytes & 0xff));
503 OS.write((uint8_t) ((Bytes >> 8) & 0xff));
504}
505
506void R600MCCodeEmitter::Emit(uint32_t Value, raw_ostream &OS) const {
507 for (unsigned i = 0; i < 4; i++) {
508 OS.write((uint8_t) ((Value >> (8 * i)) & 0xff));
509 }
510}
511
512void R600MCCodeEmitter::Emit(uint64_t Value, raw_ostream &OS) const {
513 for (unsigned i = 0; i < 8; i++) {
514 EmitByte((Value >> (8 * i)) & 0xff, OS);
515 }
516}
517
518unsigned R600MCCodeEmitter::getHWRegChan(unsigned reg) const {
519 return MRI.getEncodingValue(reg) >> HW_CHAN_SHIFT;
520}
521
522unsigned R600MCCodeEmitter::getHWReg(unsigned RegNo) const {
523 return MRI.getEncodingValue(RegNo) & HW_REG_MASK;
524}
525
526uint64_t R600MCCodeEmitter::getMachineOpValue(const MCInst &MI,
527 const MCOperand &MO,
528 SmallVectorImpl<MCFixup> &Fixup) const {
529 if (MO.isReg()) {
530 if (HAS_NATIVE_OPERANDS(MCII.get(MI.getOpcode()).TSFlags)) {
531 return MRI.getEncodingValue(MO.getReg());
532 } else {
533 return getHWReg(MO.getReg());
534 }
535 } else if (MO.isImm()) {
536 return MO.getImm();
537 } else {
538 assert(0);
539 return 0;
540 }
541}
542
543//===----------------------------------------------------------------------===//
544// Encoding helper functions
545//===----------------------------------------------------------------------===//
546
547bool R600MCCodeEmitter::isFCOp(unsigned opcode) const {
548 switch(opcode) {
549 default: return false;
550 case AMDGPU::PREDICATED_BREAK:
551 case AMDGPU::CONTINUE:
552 case AMDGPU::IF_PREDICATE_SET:
553 case AMDGPU::ELSE:
554 case AMDGPU::ENDIF:
555 case AMDGPU::ENDLOOP:
556 case AMDGPU::WHILELOOP:
557 return true;
558 }
559}
560
561bool R600MCCodeEmitter::isTexOp(unsigned opcode) const {
562 switch(opcode) {
563 default: return false;
564 case AMDGPU::TEX_LD:
565 case AMDGPU::TEX_GET_TEXTURE_RESINFO:
566 case AMDGPU::TEX_SAMPLE:
567 case AMDGPU::TEX_SAMPLE_C:
568 case AMDGPU::TEX_SAMPLE_L:
569 case AMDGPU::TEX_SAMPLE_C_L:
570 case AMDGPU::TEX_SAMPLE_LB:
571 case AMDGPU::TEX_SAMPLE_C_LB:
572 case AMDGPU::TEX_SAMPLE_G:
573 case AMDGPU::TEX_SAMPLE_C_G:
574 case AMDGPU::TEX_GET_GRADIENTS_H:
575 case AMDGPU::TEX_GET_GRADIENTS_V:
576 case AMDGPU::TEX_SET_GRADIENTS_H:
577 case AMDGPU::TEX_SET_GRADIENTS_V:
578 return true;
579 }
580}
581
582bool R600MCCodeEmitter::isFlagSet(const MCInst &MI, unsigned Operand,
583 unsigned Flag) const {
584 const MCInstrDesc &MCDesc = MCII.get(MI.getOpcode());
585 unsigned FlagIndex = GET_FLAG_OPERAND_IDX(MCDesc.TSFlags);
586 if (FlagIndex == 0) {
587 return false;
588 }
589 assert(MI.getOperand(FlagIndex).isImm());
590 return !!((MI.getOperand(FlagIndex).getImm() >>
591 (NUM_MO_FLAGS * Operand)) & Flag);
592}
593
594#include "AMDGPUGenMCCodeEmitter.inc"