blob: ac9fab8c78adf557fcac2ede2bce697faf11f475 [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///
Tom Stellardcfe2ef82013-05-06 17:50:44 +000012/// \brief The R600 code emitter produces machine code that can be executed
13/// directly on the GPU device.
Tom Stellard75aadc22012-12-11 21:25:42 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "R600Defines.h"
Jan Veselya1f9fdf2016-05-13 20:39:26 +000018#include "MCTargetDesc/AMDGPUFixupKinds.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000019#include "MCTargetDesc/AMDGPUMCCodeEmitter.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000020#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "llvm/MC/MCCodeEmitter.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MC/MCRegisterInfo.h"
26#include "llvm/MC/MCSubtargetInfo.h"
Benjamin Kramer50e2a292015-06-04 15:03:02 +000027#include "llvm/Support/EndianStream.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000028#include "llvm/Support/raw_ostream.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000029
Tom Stellard75aadc22012-12-11 21:25:42 +000030using namespace llvm;
31
32namespace {
33
34class R600MCCodeEmitter : public AMDGPUMCCodeEmitter {
Aaron Ballmanf9a18972015-02-15 22:54:22 +000035 R600MCCodeEmitter(const R600MCCodeEmitter &) = delete;
36 void operator=(const R600MCCodeEmitter &) = delete;
Tom Stellard75aadc22012-12-11 21:25:42 +000037 const MCInstrInfo &MCII;
38 const MCRegisterInfo &MRI;
Tom Stellard75aadc22012-12-11 21:25:42 +000039
40public:
David Woodhoused2cca112014-01-28 23:13:25 +000041 R600MCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri)
42 : MCII(mcii), MRI(mri) { }
Tom Stellard75aadc22012-12-11 21:25:42 +000043
44 /// \brief Encode the instruction and write it to the OS.
Jim Grosbach91df21f2015-05-15 19:13:16 +000045 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000046 SmallVectorImpl<MCFixup> &Fixups,
Craig Topper5656db42014-04-29 07:57:24 +000047 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000048
49 /// \returns the encoding for an MCOperand.
Craig Topper5656db42014-04-29 07:57:24 +000050 uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
51 SmallVectorImpl<MCFixup> &Fixups,
52 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000053
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +000054private:
Tom Stellard75aadc22012-12-11 21:25:42 +000055 void EmitByte(unsigned int byte, raw_ostream &OS) const;
56
Tom Stellard75aadc22012-12-11 21:25:42 +000057 void Emit(uint32_t value, raw_ostream &OS) const;
58 void Emit(uint64_t value, raw_ostream &OS) const;
59
60 unsigned getHWRegChan(unsigned reg) const;
61 unsigned getHWReg(unsigned regNo) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000062};
63
64} // End anonymous namespace
65
66enum RegElement {
67 ELEMENT_X = 0,
68 ELEMENT_Y,
69 ELEMENT_Z,
70 ELEMENT_W
71};
72
Tom Stellard75aadc22012-12-11 21:25:42 +000073enum FCInstr {
74 FC_IF_PREDICATE = 0,
75 FC_ELSE,
76 FC_ENDIF,
77 FC_BGNLOOP,
78 FC_ENDLOOP,
79 FC_BREAK_PREDICATE,
80 FC_CONTINUE
81};
82
Tom Stellard75aadc22012-12-11 21:25:42 +000083MCCodeEmitter *llvm::createR600MCCodeEmitter(const MCInstrInfo &MCII,
Eric Christopher501d5e92015-03-10 21:57:34 +000084 const MCRegisterInfo &MRI,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +000085 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000086 return new R600MCCodeEmitter(MCII, MRI);
Tom Stellard75aadc22012-12-11 21:25:42 +000087}
88
Jim Grosbach91df21f2015-05-15 19:13:16 +000089void R600MCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000090 SmallVectorImpl<MCFixup> &Fixups,
91 const MCSubtargetInfo &STI) const {
Tom Stellardd93cede2013-05-06 17:50:57 +000092 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
93 if (MI.getOpcode() == AMDGPU::RETURN ||
Vincent Lejeune3f1d1362013-04-30 00:13:53 +000094 MI.getOpcode() == AMDGPU::FETCH_CLAUSE ||
Vincent Lejeune3abdbf12013-04-30 00:14:38 +000095 MI.getOpcode() == AMDGPU::ALU_CLAUSE ||
Tom Stellard75aadc22012-12-11 21:25:42 +000096 MI.getOpcode() == AMDGPU::BUNDLE ||
97 MI.getOpcode() == AMDGPU::KILL) {
98 return;
Tom Stellardd93cede2013-05-06 17:50:57 +000099 } else if (IS_VTX(Desc)) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000100 uint64_t InstWord01 = getBinaryCodeForInstr(MI, Fixups, STI);
Tom Stellardd93cede2013-05-06 17:50:57 +0000101 uint32_t InstWord2 = MI.getOperand(2).getImm(); // Offset
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000102 if (!(STI.getFeatureBits()[AMDGPU::FeatureCaymanISA])) {
Tom Stellardecf9d862013-06-14 22:12:30 +0000103 InstWord2 |= 1 << 19; // Mega-Fetch bit
104 }
Tom Stellardd93cede2013-05-06 17:50:57 +0000105
106 Emit(InstWord01, OS);
107 Emit(InstWord2, OS);
Rafael Espindola525cf282013-05-22 01:36:19 +0000108 Emit((uint32_t) 0, OS);
Tom Stellardd93cede2013-05-06 17:50:57 +0000109 } else if (IS_TEX(Desc)) {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000110 int64_t Sampler = MI.getOperand(14).getImm();
Tom Stellardd93cede2013-05-06 17:50:57 +0000111
Rafael Espindola5986ce02013-05-17 22:45:52 +0000112 int64_t SrcSelect[4] = {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000113 MI.getOperand(2).getImm(),
114 MI.getOperand(3).getImm(),
115 MI.getOperand(4).getImm(),
116 MI.getOperand(5).getImm()
117 };
Rafael Espindola00345fa2013-05-23 13:22:30 +0000118 int64_t Offsets[3] = {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000119 MI.getOperand(6).getImm() & 0x1F,
120 MI.getOperand(7).getImm() & 0x1F,
121 MI.getOperand(8).getImm() & 0x1F
122 };
Tom Stellardd93cede2013-05-06 17:50:57 +0000123
David Woodhouse3fa98a62014-01-28 23:13:18 +0000124 uint64_t Word01 = getBinaryCodeForInstr(MI, Fixups, STI);
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000125 uint32_t Word2 = Sampler << 15 | SrcSelect[ELEMENT_X] << 20 |
126 SrcSelect[ELEMENT_Y] << 23 | SrcSelect[ELEMENT_Z] << 26 |
127 SrcSelect[ELEMENT_W] << 29 | Offsets[0] << 0 | Offsets[1] << 5 |
128 Offsets[2] << 10;
Tom Stellardd93cede2013-05-06 17:50:57 +0000129
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000130 Emit(Word01, OS);
131 Emit(Word2, OS);
Rafael Espindola525cf282013-05-22 01:36:19 +0000132 Emit((uint32_t) 0, OS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000133 } else {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000134 uint64_t Inst = getBinaryCodeForInstr(MI, Fixups, STI);
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000135 if ((STI.getFeatureBits()[AMDGPU::FeatureR600ALUInst]) &&
Tom Stellardecc2ad12013-05-17 15:23:21 +0000136 ((Desc.TSFlags & R600_InstFlag::OP1) ||
137 Desc.TSFlags & R600_InstFlag::OP2)) {
138 uint64_t ISAOpCode = Inst & (0x3FFULL << 39);
139 Inst &= ~(0x3FFULL << 39);
140 Inst |= ISAOpCode << 1;
141 }
Tom Stellardd93cede2013-05-06 17:50:57 +0000142 Emit(Inst, OS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000143 }
144}
145
146void R600MCCodeEmitter::EmitByte(unsigned int Byte, raw_ostream &OS) const {
147 OS.write((uint8_t) Byte & 0xff);
148}
149
Tom Stellard75aadc22012-12-11 21:25:42 +0000150void R600MCCodeEmitter::Emit(uint32_t Value, raw_ostream &OS) const {
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000151 support::endian::Writer<support::little>(OS).write(Value);
Tom Stellard75aadc22012-12-11 21:25:42 +0000152}
153
154void R600MCCodeEmitter::Emit(uint64_t Value, raw_ostream &OS) const {
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000155 support::endian::Writer<support::little>(OS).write(Value);
Tom Stellard75aadc22012-12-11 21:25:42 +0000156}
157
158unsigned R600MCCodeEmitter::getHWRegChan(unsigned reg) const {
159 return MRI.getEncodingValue(reg) >> HW_CHAN_SHIFT;
160}
161
162unsigned R600MCCodeEmitter::getHWReg(unsigned RegNo) const {
163 return MRI.getEncodingValue(RegNo) & HW_REG_MASK;
164}
165
166uint64_t R600MCCodeEmitter::getMachineOpValue(const MCInst &MI,
167 const MCOperand &MO,
Jan Veselya1f9fdf2016-05-13 20:39:26 +0000168 SmallVectorImpl<MCFixup> &Fixups,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000169 const MCSubtargetInfo &STI) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000170 if (MO.isReg()) {
Craig Topper35b2f752014-06-19 06:10:58 +0000171 if (HAS_NATIVE_OPERANDS(MCII.get(MI.getOpcode()).TSFlags))
Tom Stellard75aadc22012-12-11 21:25:42 +0000172 return MRI.getEncodingValue(MO.getReg());
Craig Topper35b2f752014-06-19 06:10:58 +0000173 return getHWReg(MO.getReg());
Tom Stellard75aadc22012-12-11 21:25:42 +0000174 }
Craig Topper35b2f752014-06-19 06:10:58 +0000175
Jan Veselya1f9fdf2016-05-13 20:39:26 +0000176 if (MO.isExpr()) {
177 const MCSymbolRefExpr *Expr = cast<MCSymbolRefExpr>(MO.getExpr());
178 // We put rodata at the end of code section, then map the entire
179 // code secetion as vtx buf. Thus the section relative address is the
180 // correct one.
181 // Each R600 literal instruction has two operands
182 // We can't easily get the order of the current one, so compare against
183 // the first one and adjust offset.
184 const unsigned offset = (&MO == &MI.getOperand(0)) ? 0 : 4;
185 Fixups.push_back(MCFixup::create(offset, Expr, FK_SecRel_4, MI.getLoc()));
186 return 0;
187 }
188
Craig Topper35b2f752014-06-19 06:10:58 +0000189 assert(MO.isImm());
190 return MO.getImm();
Tom Stellard75aadc22012-12-11 21:25:42 +0000191}
192
Tom Stellard75aadc22012-12-11 21:25:42 +0000193#include "AMDGPUGenMCCodeEmitter.inc"