blob: 469a8ad0477b881c80008a1637bb3ab28b569534 [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 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000253 case AMDGPU::EG_ExportSwz:
254 case AMDGPU::R600_ExportSwz:
255 case AMDGPU::EG_ExportBuf:
256 case AMDGPU::R600_ExportBuf: {
257 uint64_t Inst = getBinaryCodeForInstr(MI, Fixups);
258 EmitByte(INSTR_EXPORT, OS);
259 Emit(Inst, OS);
260 break;
261 }
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000262 case AMDGPU::CF_ALU:
263 case AMDGPU::CF_ALU_PUSH_BEFORE: {
264 uint64_t Inst = getBinaryCodeForInstr(MI, Fixups);
265 EmitByte(INSTR_CFALU, OS);
266 Emit(Inst, OS);
267 break;
268 }
Vincent Lejeune5f11dd32013-04-08 13:05:49 +0000269 case AMDGPU::CF_TC_EG:
270 case AMDGPU::CF_VC_EG:
271 case AMDGPU::CF_CALL_FS_EG:
272 case AMDGPU::CF_TC_R600:
273 case AMDGPU::CF_VC_R600:
274 case AMDGPU::CF_CALL_FS_R600:
Vincent Lejeunebfaa63a62013-04-01 21:48:05 +0000275 return;
Vincent Lejeune5f11dd32013-04-08 13:05:49 +0000276 case AMDGPU::WHILE_LOOP_EG:
277 case AMDGPU::END_LOOP_EG:
278 case AMDGPU::LOOP_BREAK_EG:
279 case AMDGPU::CF_CONTINUE_EG:
280 case AMDGPU::CF_JUMP_EG:
281 case AMDGPU::CF_ELSE_EG:
282 case AMDGPU::POP_EG:
283 case AMDGPU::WHILE_LOOP_R600:
284 case AMDGPU::END_LOOP_R600:
285 case AMDGPU::LOOP_BREAK_R600:
286 case AMDGPU::CF_CONTINUE_R600:
287 case AMDGPU::CF_JUMP_R600:
288 case AMDGPU::CF_ELSE_R600:
289 case AMDGPU::POP_R600: {
Vincent Lejeunebfaa63a62013-04-01 21:48:05 +0000290 uint64_t Inst = getBinaryCodeForInstr(MI, Fixups);
291 EmitByte(INSTR_NATIVE, OS);
292 Emit(Inst, OS);
293 break;
294 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000295 default:
296 EmitALUInstr(MI, Fixups, OS);
297 break;
298 }
299 }
300}
301
302void R600MCCodeEmitter::EmitALUInstr(const MCInst &MI,
303 SmallVectorImpl<MCFixup> &Fixups,
304 raw_ostream &OS) const {
305 const MCInstrDesc &MCDesc = MCII.get(MI.getOpcode());
Tom Stellard75aadc22012-12-11 21:25:42 +0000306
307 // Emit instruction type
308 EmitByte(INSTR_ALU, OS);
309
310 uint64_t InstWord01 = getBinaryCodeForInstr(MI, Fixups);
311
312 //older alu have different encoding for instructions with one or two src
313 //parameters.
314 if ((STI.getFeatureBits() & AMDGPU::FeatureR600ALUInst) &&
315 !(MCDesc.TSFlags & R600_InstFlag::OP3)) {
316 uint64_t ISAOpCode = InstWord01 & (0x3FFULL << 39);
317 InstWord01 &= ~(0x3FFULL << 39);
318 InstWord01 |= ISAOpCode << 1;
319 }
320
Tom Stellard365366f2013-01-23 02:09:06 +0000321 unsigned SrcNum = MCDesc.TSFlags & R600_InstFlag::OP3 ? 3 :
322 MCDesc.TSFlags & R600_InstFlag::OP2 ? 2 : 1;
Tom Stellard75aadc22012-12-11 21:25:42 +0000323
Tom Stellard365366f2013-01-23 02:09:06 +0000324 EmitByte(SrcNum, OS);
325
326 const unsigned SrcOps[3][2] = {
327 {R600Operands::SRC0, R600Operands::SRC0_SEL},
328 {R600Operands::SRC1, R600Operands::SRC1_SEL},
329 {R600Operands::SRC2, R600Operands::SRC2_SEL}
330 };
331
332 for (unsigned SrcIdx = 0; SrcIdx < SrcNum; ++SrcIdx) {
333 unsigned RegOpIdx = R600Operands::ALUOpTable[SrcNum-1][SrcOps[SrcIdx][0]];
334 unsigned SelOpIdx = R600Operands::ALUOpTable[SrcNum-1][SrcOps[SrcIdx][1]];
335 EmitSrcISA(MI, RegOpIdx, SelOpIdx, OS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000336 }
337
338 Emit(InstWord01, OS);
339 return;
340}
341
342void R600MCCodeEmitter::EmitSrc(const MCInst &MI, unsigned OpIdx,
343 raw_ostream &OS) const {
344 const MCOperand &MO = MI.getOperand(OpIdx);
345 union {
346 float f;
347 uint32_t i;
348 } Value;
349 Value.i = 0;
350 // Emit the source select (2 bytes). For GPRs, this is the register index.
351 // For other potential instruction operands, (e.g. constant registers) the
352 // value of the source select is defined in the r600isa docs.
353 if (MO.isReg()) {
354 unsigned reg = MO.getReg();
355 EmitTwoBytes(getHWReg(reg), OS);
356 if (reg == AMDGPU::ALU_LITERAL_X) {
357 unsigned ImmOpIndex = MI.getNumOperands() - 1;
358 MCOperand ImmOp = MI.getOperand(ImmOpIndex);
359 if (ImmOp.isFPImm()) {
360 Value.f = ImmOp.getFPImm();
361 } else {
362 assert(ImmOp.isImm());
363 Value.i = ImmOp.getImm();
364 }
365 }
366 } else {
367 // XXX: Handle other operand types.
368 EmitTwoBytes(0, OS);
369 }
370
371 // Emit the source channel (1 byte)
372 if (MO.isReg()) {
373 EmitByte(getHWRegChan(MO.getReg()), OS);
374 } else {
375 EmitByte(0, OS);
376 }
377
378 // XXX: Emit isNegated (1 byte)
379 if ((!(isFlagSet(MI, OpIdx, MO_FLAG_ABS)))
380 && (isFlagSet(MI, OpIdx, MO_FLAG_NEG) ||
381 (MO.isReg() &&
382 (MO.getReg() == AMDGPU::NEG_ONE || MO.getReg() == AMDGPU::NEG_HALF)))){
383 EmitByte(1, OS);
384 } else {
385 EmitByte(0, OS);
386 }
387
388 // Emit isAbsolute (1 byte)
389 if (isFlagSet(MI, OpIdx, MO_FLAG_ABS)) {
390 EmitByte(1, OS);
391 } else {
392 EmitByte(0, OS);
393 }
394
395 // XXX: Emit relative addressing mode (1 byte)
396 EmitByte(0, OS);
397
398 // Emit kc_bank, This will be adjusted later by r600_asm
399 EmitByte(0, OS);
400
401 // Emit the literal value, if applicable (4 bytes).
402 Emit(Value.i, OS);
403
404}
405
Tom Stellard365366f2013-01-23 02:09:06 +0000406void R600MCCodeEmitter::EmitSrcISA(const MCInst &MI, unsigned RegOpIdx,
407 unsigned SelOpIdx, raw_ostream &OS) const {
408 const MCOperand &RegMO = MI.getOperand(RegOpIdx);
409 const MCOperand &SelMO = MI.getOperand(SelOpIdx);
410
Tom Stellard75aadc22012-12-11 21:25:42 +0000411 union {
412 float f;
413 uint32_t i;
414 } InlineConstant;
415 InlineConstant.i = 0;
Tom Stellard365366f2013-01-23 02:09:06 +0000416 // Emit source type (1 byte) and source select (4 bytes). For GPRs type is 0
417 // and select is 0 (GPR index is encoded in the instr encoding. For constants
418 // type is 1 and select is the original const select passed from the driver.
419 unsigned Reg = RegMO.getReg();
420 if (Reg == AMDGPU::ALU_CONST) {
421 EmitByte(1, OS);
422 uint32_t Sel = SelMO.getImm();
423 Emit(Sel, OS);
424 } else {
425 EmitByte(0, OS);
426 Emit((uint32_t)0, OS);
427 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000428
Tom Stellard365366f2013-01-23 02:09:06 +0000429 if (Reg == AMDGPU::ALU_LITERAL_X) {
430 unsigned ImmOpIndex = MI.getNumOperands() - 1;
431 MCOperand ImmOp = MI.getOperand(ImmOpIndex);
432 if (ImmOp.isFPImm()) {
433 InlineConstant.f = ImmOp.getFPImm();
434 } else {
435 assert(ImmOp.isImm());
436 InlineConstant.i = ImmOp.getImm();
Tom Stellard75aadc22012-12-11 21:25:42 +0000437 }
438 }
439
440 // Emit the literal value, if applicable (4 bytes).
441 Emit(InlineConstant.i, OS);
442}
443
Tom Stellard75aadc22012-12-11 21:25:42 +0000444void R600MCCodeEmitter::EmitFCInstr(const MCInst &MI, raw_ostream &OS) const {
445
446 // Emit instruction type
447 EmitByte(INSTR_FC, OS);
448
449 // Emit SRC
450 unsigned NumOperands = MI.getNumOperands();
451 if (NumOperands > 0) {
452 assert(NumOperands == 1);
453 EmitSrc(MI, 0, OS);
454 } else {
455 EmitNullBytes(SRC_BYTE_COUNT, OS);
456 }
457
458 // Emit FC Instruction
459 enum FCInstr instr;
460 switch (MI.getOpcode()) {
461 case AMDGPU::PREDICATED_BREAK:
462 instr = FC_BREAK_PREDICATE;
463 break;
464 case AMDGPU::CONTINUE:
465 instr = FC_CONTINUE;
466 break;
467 case AMDGPU::IF_PREDICATE_SET:
468 instr = FC_IF_PREDICATE;
469 break;
470 case AMDGPU::ELSE:
471 instr = FC_ELSE;
472 break;
473 case AMDGPU::ENDIF:
474 instr = FC_ENDIF;
475 break;
476 case AMDGPU::ENDLOOP:
477 instr = FC_ENDLOOP;
478 break;
479 case AMDGPU::WHILELOOP:
480 instr = FC_BGNLOOP;
481 break;
482 default:
483 abort();
484 break;
485 }
486 EmitByte(instr, OS);
487}
488
489void R600MCCodeEmitter::EmitNullBytes(unsigned int ByteCount,
490 raw_ostream &OS) const {
491
492 for (unsigned int i = 0; i < ByteCount; i++) {
493 EmitByte(0, OS);
494 }
495}
496
497void R600MCCodeEmitter::EmitByte(unsigned int Byte, raw_ostream &OS) const {
498 OS.write((uint8_t) Byte & 0xff);
499}
500
501void R600MCCodeEmitter::EmitTwoBytes(unsigned int Bytes,
502 raw_ostream &OS) const {
503 OS.write((uint8_t) (Bytes & 0xff));
504 OS.write((uint8_t) ((Bytes >> 8) & 0xff));
505}
506
507void R600MCCodeEmitter::Emit(uint32_t Value, raw_ostream &OS) const {
508 for (unsigned i = 0; i < 4; i++) {
509 OS.write((uint8_t) ((Value >> (8 * i)) & 0xff));
510 }
511}
512
513void R600MCCodeEmitter::Emit(uint64_t Value, raw_ostream &OS) const {
514 for (unsigned i = 0; i < 8; i++) {
515 EmitByte((Value >> (8 * i)) & 0xff, OS);
516 }
517}
518
519unsigned R600MCCodeEmitter::getHWRegChan(unsigned reg) const {
520 return MRI.getEncodingValue(reg) >> HW_CHAN_SHIFT;
521}
522
523unsigned R600MCCodeEmitter::getHWReg(unsigned RegNo) const {
524 return MRI.getEncodingValue(RegNo) & HW_REG_MASK;
525}
526
527uint64_t R600MCCodeEmitter::getMachineOpValue(const MCInst &MI,
528 const MCOperand &MO,
529 SmallVectorImpl<MCFixup> &Fixup) const {
530 if (MO.isReg()) {
531 if (HAS_NATIVE_OPERANDS(MCII.get(MI.getOpcode()).TSFlags)) {
532 return MRI.getEncodingValue(MO.getReg());
533 } else {
534 return getHWReg(MO.getReg());
535 }
536 } else if (MO.isImm()) {
537 return MO.getImm();
538 } else {
539 assert(0);
540 return 0;
541 }
542}
543
544//===----------------------------------------------------------------------===//
545// Encoding helper functions
546//===----------------------------------------------------------------------===//
547
548bool R600MCCodeEmitter::isFCOp(unsigned opcode) const {
549 switch(opcode) {
550 default: return false;
551 case AMDGPU::PREDICATED_BREAK:
552 case AMDGPU::CONTINUE:
553 case AMDGPU::IF_PREDICATE_SET:
554 case AMDGPU::ELSE:
555 case AMDGPU::ENDIF:
556 case AMDGPU::ENDLOOP:
557 case AMDGPU::WHILELOOP:
558 return true;
559 }
560}
561
562bool R600MCCodeEmitter::isTexOp(unsigned opcode) const {
563 switch(opcode) {
564 default: return false;
565 case AMDGPU::TEX_LD:
566 case AMDGPU::TEX_GET_TEXTURE_RESINFO:
567 case AMDGPU::TEX_SAMPLE:
568 case AMDGPU::TEX_SAMPLE_C:
569 case AMDGPU::TEX_SAMPLE_L:
570 case AMDGPU::TEX_SAMPLE_C_L:
571 case AMDGPU::TEX_SAMPLE_LB:
572 case AMDGPU::TEX_SAMPLE_C_LB:
573 case AMDGPU::TEX_SAMPLE_G:
574 case AMDGPU::TEX_SAMPLE_C_G:
575 case AMDGPU::TEX_GET_GRADIENTS_H:
576 case AMDGPU::TEX_GET_GRADIENTS_V:
577 case AMDGPU::TEX_SET_GRADIENTS_H:
578 case AMDGPU::TEX_SET_GRADIENTS_V:
579 return true;
580 }
581}
582
583bool R600MCCodeEmitter::isFlagSet(const MCInst &MI, unsigned Operand,
584 unsigned Flag) const {
585 const MCInstrDesc &MCDesc = MCII.get(MI.getOpcode());
586 unsigned FlagIndex = GET_FLAG_OPERAND_IDX(MCDesc.TSFlags);
587 if (FlagIndex == 0) {
588 return false;
589 }
590 assert(MI.getOperand(FlagIndex).isImm());
591 return !!((MI.getOperand(FlagIndex).getImm() >>
592 (NUM_MO_FLAGS * Operand)) & Flag);
593}
594
595#include "AMDGPUGenMCCodeEmitter.inc"