blob: 5e8e6ceb7ca25941b6fe375de6a3797741d0aa15 [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 Emit(uint32_t value, raw_ostream &OS) const;
56 void Emit(uint64_t value, raw_ostream &OS) const;
57
Tom Stellard75aadc22012-12-11 21:25:42 +000058 unsigned getHWReg(unsigned regNo) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000059};
60
61} // End anonymous namespace
62
63enum RegElement {
64 ELEMENT_X = 0,
65 ELEMENT_Y,
66 ELEMENT_Z,
67 ELEMENT_W
68};
69
Tom Stellard75aadc22012-12-11 21:25:42 +000070enum FCInstr {
71 FC_IF_PREDICATE = 0,
72 FC_ELSE,
73 FC_ENDIF,
74 FC_BGNLOOP,
75 FC_ENDLOOP,
76 FC_BREAK_PREDICATE,
77 FC_CONTINUE
78};
79
Tom Stellard75aadc22012-12-11 21:25:42 +000080MCCodeEmitter *llvm::createR600MCCodeEmitter(const MCInstrInfo &MCII,
Eric Christopher501d5e92015-03-10 21:57:34 +000081 const MCRegisterInfo &MRI,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +000082 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000083 return new R600MCCodeEmitter(MCII, MRI);
Tom Stellard75aadc22012-12-11 21:25:42 +000084}
85
Jim Grosbach91df21f2015-05-15 19:13:16 +000086void R600MCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000087 SmallVectorImpl<MCFixup> &Fixups,
88 const MCSubtargetInfo &STI) const {
Tom Stellardd93cede2013-05-06 17:50:57 +000089 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
90 if (MI.getOpcode() == AMDGPU::RETURN ||
Vincent Lejeune3f1d1362013-04-30 00:13:53 +000091 MI.getOpcode() == AMDGPU::FETCH_CLAUSE ||
Vincent Lejeune3abdbf12013-04-30 00:14:38 +000092 MI.getOpcode() == AMDGPU::ALU_CLAUSE ||
Tom Stellard75aadc22012-12-11 21:25:42 +000093 MI.getOpcode() == AMDGPU::BUNDLE ||
94 MI.getOpcode() == AMDGPU::KILL) {
95 return;
Tom Stellardd93cede2013-05-06 17:50:57 +000096 } else if (IS_VTX(Desc)) {
David Woodhouse3fa98a62014-01-28 23:13:18 +000097 uint64_t InstWord01 = getBinaryCodeForInstr(MI, Fixups, STI);
Tom Stellardd93cede2013-05-06 17:50:57 +000098 uint32_t InstWord2 = MI.getOperand(2).getImm(); // Offset
Michael Kupersteindb0712f2015-05-26 10:47:10 +000099 if (!(STI.getFeatureBits()[AMDGPU::FeatureCaymanISA])) {
Tom Stellardecf9d862013-06-14 22:12:30 +0000100 InstWord2 |= 1 << 19; // Mega-Fetch bit
101 }
Tom Stellardd93cede2013-05-06 17:50:57 +0000102
103 Emit(InstWord01, OS);
104 Emit(InstWord2, OS);
Rafael Espindola525cf282013-05-22 01:36:19 +0000105 Emit((uint32_t) 0, OS);
Tom Stellardd93cede2013-05-06 17:50:57 +0000106 } else if (IS_TEX(Desc)) {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000107 int64_t Sampler = MI.getOperand(14).getImm();
Tom Stellardd93cede2013-05-06 17:50:57 +0000108
Rafael Espindola5986ce02013-05-17 22:45:52 +0000109 int64_t SrcSelect[4] = {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000110 MI.getOperand(2).getImm(),
111 MI.getOperand(3).getImm(),
112 MI.getOperand(4).getImm(),
113 MI.getOperand(5).getImm()
114 };
Rafael Espindola00345fa2013-05-23 13:22:30 +0000115 int64_t Offsets[3] = {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000116 MI.getOperand(6).getImm() & 0x1F,
117 MI.getOperand(7).getImm() & 0x1F,
118 MI.getOperand(8).getImm() & 0x1F
119 };
Tom Stellardd93cede2013-05-06 17:50:57 +0000120
David Woodhouse3fa98a62014-01-28 23:13:18 +0000121 uint64_t Word01 = getBinaryCodeForInstr(MI, Fixups, STI);
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000122 uint32_t Word2 = Sampler << 15 | SrcSelect[ELEMENT_X] << 20 |
123 SrcSelect[ELEMENT_Y] << 23 | SrcSelect[ELEMENT_Z] << 26 |
124 SrcSelect[ELEMENT_W] << 29 | Offsets[0] << 0 | Offsets[1] << 5 |
125 Offsets[2] << 10;
Tom Stellardd93cede2013-05-06 17:50:57 +0000126
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000127 Emit(Word01, OS);
128 Emit(Word2, OS);
Rafael Espindola525cf282013-05-22 01:36:19 +0000129 Emit((uint32_t) 0, OS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000130 } else {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000131 uint64_t Inst = getBinaryCodeForInstr(MI, Fixups, STI);
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000132 if ((STI.getFeatureBits()[AMDGPU::FeatureR600ALUInst]) &&
Tom Stellardecc2ad12013-05-17 15:23:21 +0000133 ((Desc.TSFlags & R600_InstFlag::OP1) ||
134 Desc.TSFlags & R600_InstFlag::OP2)) {
135 uint64_t ISAOpCode = Inst & (0x3FFULL << 39);
136 Inst &= ~(0x3FFULL << 39);
137 Inst |= ISAOpCode << 1;
138 }
Tom Stellardd93cede2013-05-06 17:50:57 +0000139 Emit(Inst, OS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000140 }
141}
142
Tom Stellard75aadc22012-12-11 21:25:42 +0000143void R600MCCodeEmitter::Emit(uint32_t Value, raw_ostream &OS) const {
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000144 support::endian::Writer<support::little>(OS).write(Value);
Tom Stellard75aadc22012-12-11 21:25:42 +0000145}
146
147void R600MCCodeEmitter::Emit(uint64_t Value, raw_ostream &OS) const {
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000148 support::endian::Writer<support::little>(OS).write(Value);
Tom Stellard75aadc22012-12-11 21:25:42 +0000149}
150
Tom Stellard75aadc22012-12-11 21:25:42 +0000151unsigned R600MCCodeEmitter::getHWReg(unsigned RegNo) const {
152 return MRI.getEncodingValue(RegNo) & HW_REG_MASK;
153}
154
155uint64_t R600MCCodeEmitter::getMachineOpValue(const MCInst &MI,
156 const MCOperand &MO,
Jan Veselya1f9fdf2016-05-13 20:39:26 +0000157 SmallVectorImpl<MCFixup> &Fixups,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000158 const MCSubtargetInfo &STI) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000159 if (MO.isReg()) {
Craig Topper35b2f752014-06-19 06:10:58 +0000160 if (HAS_NATIVE_OPERANDS(MCII.get(MI.getOpcode()).TSFlags))
Tom Stellard75aadc22012-12-11 21:25:42 +0000161 return MRI.getEncodingValue(MO.getReg());
Craig Topper35b2f752014-06-19 06:10:58 +0000162 return getHWReg(MO.getReg());
Tom Stellard75aadc22012-12-11 21:25:42 +0000163 }
Craig Topper35b2f752014-06-19 06:10:58 +0000164
Jan Veselya1f9fdf2016-05-13 20:39:26 +0000165 if (MO.isExpr()) {
Jan Veselya1f9fdf2016-05-13 20:39:26 +0000166 // We put rodata at the end of code section, then map the entire
167 // code secetion as vtx buf. Thus the section relative address is the
168 // correct one.
169 // Each R600 literal instruction has two operands
170 // We can't easily get the order of the current one, so compare against
171 // the first one and adjust offset.
172 const unsigned offset = (&MO == &MI.getOperand(0)) ? 0 : 4;
Jan Vesely3bc1af22016-06-25 18:24:16 +0000173 Fixups.push_back(MCFixup::create(offset, MO.getExpr(), FK_SecRel_4, MI.getLoc()));
Jan Veselya1f9fdf2016-05-13 20:39:26 +0000174 return 0;
175 }
176
Craig Topper35b2f752014-06-19 06:10:58 +0000177 assert(MO.isImm());
178 return MO.getImm();
Tom Stellard75aadc22012-12-11 21:25:42 +0000179}
180
Tom Stellard75aadc22012-12-11 21:25:42 +0000181#include "AMDGPUGenMCCodeEmitter.inc"