blob: 43a2136dada46518edd589a686f9205fb5ef7d81 [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 MCRegisterInfo &MRI;
Tom Stellard75aadc22012-12-11 21:25:42 +000038
39public:
David Woodhoused2cca112014-01-28 23:13:25 +000040 R600MCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri)
Daniel Sanders72db2a32016-11-19 13:05:44 +000041 : AMDGPUMCCodeEmitter(mcii), MRI(mri) { }
Tom Stellard75aadc22012-12-11 21:25:42 +000042
43 /// \brief Encode the instruction and write it to the OS.
Jim Grosbach91df21f2015-05-15 19:13:16 +000044 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000045 SmallVectorImpl<MCFixup> &Fixups,
Craig Topper5656db42014-04-29 07:57:24 +000046 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000047
48 /// \returns the encoding for an MCOperand.
Craig Topper5656db42014-04-29 07:57:24 +000049 uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
50 SmallVectorImpl<MCFixup> &Fixups,
51 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000052
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +000053private:
Tom Stellard75aadc22012-12-11 21:25:42 +000054 void Emit(uint32_t value, raw_ostream &OS) const;
55 void Emit(uint64_t value, raw_ostream &OS) const;
56
Tom Stellard75aadc22012-12-11 21:25:42 +000057 unsigned getHWReg(unsigned regNo) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000058};
59
60} // End anonymous namespace
61
62enum RegElement {
63 ELEMENT_X = 0,
64 ELEMENT_Y,
65 ELEMENT_Z,
66 ELEMENT_W
67};
68
Tom Stellard75aadc22012-12-11 21:25:42 +000069enum FCInstr {
70 FC_IF_PREDICATE = 0,
71 FC_ELSE,
72 FC_ENDIF,
73 FC_BGNLOOP,
74 FC_ENDLOOP,
75 FC_BREAK_PREDICATE,
76 FC_CONTINUE
77};
78
Tom Stellard75aadc22012-12-11 21:25:42 +000079MCCodeEmitter *llvm::createR600MCCodeEmitter(const MCInstrInfo &MCII,
Eric Christopher501d5e92015-03-10 21:57:34 +000080 const MCRegisterInfo &MRI,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +000081 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000082 return new R600MCCodeEmitter(MCII, MRI);
Tom Stellard75aadc22012-12-11 21:25:42 +000083}
84
Jim Grosbach91df21f2015-05-15 19:13:16 +000085void R600MCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000086 SmallVectorImpl<MCFixup> &Fixups,
87 const MCSubtargetInfo &STI) const {
Daniel Sanders72db2a32016-11-19 13:05:44 +000088 verifyInstructionPredicates(MI,
89 computeAvailableFeatures(STI.getFeatureBits()));
90
Tom Stellardd93cede2013-05-06 17:50:57 +000091 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
92 if (MI.getOpcode() == AMDGPU::RETURN ||
Vincent Lejeune3f1d1362013-04-30 00:13:53 +000093 MI.getOpcode() == AMDGPU::FETCH_CLAUSE ||
Vincent Lejeune3abdbf12013-04-30 00:14:38 +000094 MI.getOpcode() == AMDGPU::ALU_CLAUSE ||
Tom Stellard75aadc22012-12-11 21:25:42 +000095 MI.getOpcode() == AMDGPU::BUNDLE ||
96 MI.getOpcode() == AMDGPU::KILL) {
97 return;
Tom Stellardd93cede2013-05-06 17:50:57 +000098 } else if (IS_VTX(Desc)) {
David Woodhouse3fa98a62014-01-28 23:13:18 +000099 uint64_t InstWord01 = getBinaryCodeForInstr(MI, Fixups, STI);
Tom Stellardd93cede2013-05-06 17:50:57 +0000100 uint32_t InstWord2 = MI.getOperand(2).getImm(); // Offset
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000101 if (!(STI.getFeatureBits()[AMDGPU::FeatureCaymanISA])) {
Tom Stellardecf9d862013-06-14 22:12:30 +0000102 InstWord2 |= 1 << 19; // Mega-Fetch bit
103 }
Tom Stellardd93cede2013-05-06 17:50:57 +0000104
105 Emit(InstWord01, OS);
106 Emit(InstWord2, OS);
Rafael Espindola525cf282013-05-22 01:36:19 +0000107 Emit((uint32_t) 0, OS);
Tom Stellardd93cede2013-05-06 17:50:57 +0000108 } else if (IS_TEX(Desc)) {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000109 int64_t Sampler = MI.getOperand(14).getImm();
Tom Stellardd93cede2013-05-06 17:50:57 +0000110
Rafael Espindola5986ce02013-05-17 22:45:52 +0000111 int64_t SrcSelect[4] = {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000112 MI.getOperand(2).getImm(),
113 MI.getOperand(3).getImm(),
114 MI.getOperand(4).getImm(),
115 MI.getOperand(5).getImm()
116 };
Rafael Espindola00345fa2013-05-23 13:22:30 +0000117 int64_t Offsets[3] = {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000118 MI.getOperand(6).getImm() & 0x1F,
119 MI.getOperand(7).getImm() & 0x1F,
120 MI.getOperand(8).getImm() & 0x1F
121 };
Tom Stellardd93cede2013-05-06 17:50:57 +0000122
David Woodhouse3fa98a62014-01-28 23:13:18 +0000123 uint64_t Word01 = getBinaryCodeForInstr(MI, Fixups, STI);
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000124 uint32_t Word2 = Sampler << 15 | SrcSelect[ELEMENT_X] << 20 |
125 SrcSelect[ELEMENT_Y] << 23 | SrcSelect[ELEMENT_Z] << 26 |
126 SrcSelect[ELEMENT_W] << 29 | Offsets[0] << 0 | Offsets[1] << 5 |
127 Offsets[2] << 10;
Tom Stellardd93cede2013-05-06 17:50:57 +0000128
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000129 Emit(Word01, OS);
130 Emit(Word2, OS);
Rafael Espindola525cf282013-05-22 01:36:19 +0000131 Emit((uint32_t) 0, OS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000132 } else {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000133 uint64_t Inst = getBinaryCodeForInstr(MI, Fixups, STI);
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000134 if ((STI.getFeatureBits()[AMDGPU::FeatureR600ALUInst]) &&
Tom Stellardecc2ad12013-05-17 15:23:21 +0000135 ((Desc.TSFlags & R600_InstFlag::OP1) ||
136 Desc.TSFlags & R600_InstFlag::OP2)) {
137 uint64_t ISAOpCode = Inst & (0x3FFULL << 39);
138 Inst &= ~(0x3FFULL << 39);
139 Inst |= ISAOpCode << 1;
140 }
Tom Stellardd93cede2013-05-06 17:50:57 +0000141 Emit(Inst, OS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000142 }
143}
144
Tom Stellard75aadc22012-12-11 21:25:42 +0000145void R600MCCodeEmitter::Emit(uint32_t Value, raw_ostream &OS) const {
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000146 support::endian::Writer<support::little>(OS).write(Value);
Tom Stellard75aadc22012-12-11 21:25:42 +0000147}
148
149void R600MCCodeEmitter::Emit(uint64_t Value, raw_ostream &OS) const {
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000150 support::endian::Writer<support::little>(OS).write(Value);
Tom Stellard75aadc22012-12-11 21:25:42 +0000151}
152
Tom Stellard75aadc22012-12-11 21:25:42 +0000153unsigned R600MCCodeEmitter::getHWReg(unsigned RegNo) const {
154 return MRI.getEncodingValue(RegNo) & HW_REG_MASK;
155}
156
157uint64_t R600MCCodeEmitter::getMachineOpValue(const MCInst &MI,
158 const MCOperand &MO,
Jan Veselya1f9fdf2016-05-13 20:39:26 +0000159 SmallVectorImpl<MCFixup> &Fixups,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000160 const MCSubtargetInfo &STI) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000161 if (MO.isReg()) {
Craig Topper35b2f752014-06-19 06:10:58 +0000162 if (HAS_NATIVE_OPERANDS(MCII.get(MI.getOpcode()).TSFlags))
Tom Stellard75aadc22012-12-11 21:25:42 +0000163 return MRI.getEncodingValue(MO.getReg());
Craig Topper35b2f752014-06-19 06:10:58 +0000164 return getHWReg(MO.getReg());
Tom Stellard75aadc22012-12-11 21:25:42 +0000165 }
Craig Topper35b2f752014-06-19 06:10:58 +0000166
Jan Veselya1f9fdf2016-05-13 20:39:26 +0000167 if (MO.isExpr()) {
Jan Veselya1f9fdf2016-05-13 20:39:26 +0000168 // We put rodata at the end of code section, then map the entire
169 // code secetion as vtx buf. Thus the section relative address is the
170 // correct one.
171 // Each R600 literal instruction has two operands
172 // We can't easily get the order of the current one, so compare against
173 // the first one and adjust offset.
174 const unsigned offset = (&MO == &MI.getOperand(0)) ? 0 : 4;
Jan Vesely3bc1af22016-06-25 18:24:16 +0000175 Fixups.push_back(MCFixup::create(offset, MO.getExpr(), FK_SecRel_4, MI.getLoc()));
Jan Veselya1f9fdf2016-05-13 20:39:26 +0000176 return 0;
177 }
178
Craig Topper35b2f752014-06-19 06:10:58 +0000179 assert(MO.isImm());
180 return MO.getImm();
Tom Stellard75aadc22012-12-11 21:25:42 +0000181}
182
Daniel Sanders72db2a32016-11-19 13:05:44 +0000183#define ENABLE_INSTR_PREDICATE_VERIFIER
Tom Stellard75aadc22012-12-11 21:25:42 +0000184#include "AMDGPUGenMCCodeEmitter.inc"