blob: 6015ec190fd471d81858208add478f0179000a1e [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"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000023#include "llvm/MC/MCFixup.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000024#include "llvm/MC/MCInst.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000025#include "llvm/MC/MCInstrDesc.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000026#include "llvm/MC/MCInstrInfo.h"
27#include "llvm/MC/MCRegisterInfo.h"
28#include "llvm/MC/MCSubtargetInfo.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000029#include "llvm/Support/Endian.h"
Benjamin Kramer50e2a292015-06-04 15:03:02 +000030#include "llvm/Support/EndianStream.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000031#include "llvm/Support/raw_ostream.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000032#include <cassert>
33#include <cstdint>
Tom Stellard75aadc22012-12-11 21:25:42 +000034
Tom Stellard75aadc22012-12-11 21:25:42 +000035using namespace llvm;
36
37namespace {
38
39class R600MCCodeEmitter : public AMDGPUMCCodeEmitter {
Tom Stellard75aadc22012-12-11 21:25:42 +000040 const MCRegisterInfo &MRI;
Tom Stellard75aadc22012-12-11 21:25:42 +000041
42public:
David Woodhoused2cca112014-01-28 23:13:25 +000043 R600MCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri)
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000044 : AMDGPUMCCodeEmitter(mcii), MRI(mri) {}
45 R600MCCodeEmitter(const R600MCCodeEmitter &) = delete;
46 R600MCCodeEmitter &operator=(const R600MCCodeEmitter &) = delete;
Tom Stellard75aadc22012-12-11 21:25:42 +000047
48 /// \brief Encode the instruction and write it to the OS.
Jim Grosbach91df21f2015-05-15 19:13:16 +000049 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000050 SmallVectorImpl<MCFixup> &Fixups,
Craig Topper5656db42014-04-29 07:57:24 +000051 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000052
53 /// \returns the encoding for an MCOperand.
Craig Topper5656db42014-04-29 07:57:24 +000054 uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
55 SmallVectorImpl<MCFixup> &Fixups,
56 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000057
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +000058private:
Tom Stellard75aadc22012-12-11 21:25:42 +000059 void Emit(uint32_t value, raw_ostream &OS) const;
60 void Emit(uint64_t value, raw_ostream &OS) const;
61
Tom Stellard75aadc22012-12-11 21:25:42 +000062 unsigned getHWReg(unsigned regNo) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000063};
64
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000065} // end anonymous namespace
Tom Stellard75aadc22012-12-11 21:25:42 +000066
67enum RegElement {
68 ELEMENT_X = 0,
69 ELEMENT_Y,
70 ELEMENT_Z,
71 ELEMENT_W
72};
73
Tom Stellard75aadc22012-12-11 21:25:42 +000074enum FCInstr {
75 FC_IF_PREDICATE = 0,
76 FC_ELSE,
77 FC_ENDIF,
78 FC_BGNLOOP,
79 FC_ENDLOOP,
80 FC_BREAK_PREDICATE,
81 FC_CONTINUE
82};
83
Tom Stellard75aadc22012-12-11 21:25:42 +000084MCCodeEmitter *llvm::createR600MCCodeEmitter(const MCInstrInfo &MCII,
Eric Christopher501d5e92015-03-10 21:57:34 +000085 const MCRegisterInfo &MRI,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +000086 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000087 return new R600MCCodeEmitter(MCII, MRI);
Tom Stellard75aadc22012-12-11 21:25:42 +000088}
89
Jim Grosbach91df21f2015-05-15 19:13:16 +000090void R600MCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000091 SmallVectorImpl<MCFixup> &Fixups,
92 const MCSubtargetInfo &STI) const {
Daniel Sanders72db2a32016-11-19 13:05:44 +000093 verifyInstructionPredicates(MI,
94 computeAvailableFeatures(STI.getFeatureBits()));
95
Tom Stellardd93cede2013-05-06 17:50:57 +000096 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
97 if (MI.getOpcode() == AMDGPU::RETURN ||
Vincent Lejeune3f1d1362013-04-30 00:13:53 +000098 MI.getOpcode() == AMDGPU::FETCH_CLAUSE ||
Vincent Lejeune3abdbf12013-04-30 00:14:38 +000099 MI.getOpcode() == AMDGPU::ALU_CLAUSE ||
Tom Stellard75aadc22012-12-11 21:25:42 +0000100 MI.getOpcode() == AMDGPU::BUNDLE ||
101 MI.getOpcode() == AMDGPU::KILL) {
102 return;
Tom Stellardd93cede2013-05-06 17:50:57 +0000103 } else if (IS_VTX(Desc)) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000104 uint64_t InstWord01 = getBinaryCodeForInstr(MI, Fixups, STI);
Tom Stellardd93cede2013-05-06 17:50:57 +0000105 uint32_t InstWord2 = MI.getOperand(2).getImm(); // Offset
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000106 if (!(STI.getFeatureBits()[AMDGPU::FeatureCaymanISA])) {
Tom Stellardecf9d862013-06-14 22:12:30 +0000107 InstWord2 |= 1 << 19; // Mega-Fetch bit
108 }
Tom Stellardd93cede2013-05-06 17:50:57 +0000109
110 Emit(InstWord01, OS);
111 Emit(InstWord2, OS);
Rafael Espindola525cf282013-05-22 01:36:19 +0000112 Emit((uint32_t) 0, OS);
Tom Stellardd93cede2013-05-06 17:50:57 +0000113 } else if (IS_TEX(Desc)) {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000114 int64_t Sampler = MI.getOperand(14).getImm();
Tom Stellardd93cede2013-05-06 17:50:57 +0000115
Rafael Espindola5986ce02013-05-17 22:45:52 +0000116 int64_t SrcSelect[4] = {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000117 MI.getOperand(2).getImm(),
118 MI.getOperand(3).getImm(),
119 MI.getOperand(4).getImm(),
120 MI.getOperand(5).getImm()
121 };
Rafael Espindola00345fa2013-05-23 13:22:30 +0000122 int64_t Offsets[3] = {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000123 MI.getOperand(6).getImm() & 0x1F,
124 MI.getOperand(7).getImm() & 0x1F,
125 MI.getOperand(8).getImm() & 0x1F
126 };
Tom Stellardd93cede2013-05-06 17:50:57 +0000127
David Woodhouse3fa98a62014-01-28 23:13:18 +0000128 uint64_t Word01 = getBinaryCodeForInstr(MI, Fixups, STI);
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000129 uint32_t Word2 = Sampler << 15 | SrcSelect[ELEMENT_X] << 20 |
130 SrcSelect[ELEMENT_Y] << 23 | SrcSelect[ELEMENT_Z] << 26 |
131 SrcSelect[ELEMENT_W] << 29 | Offsets[0] << 0 | Offsets[1] << 5 |
132 Offsets[2] << 10;
Tom Stellardd93cede2013-05-06 17:50:57 +0000133
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000134 Emit(Word01, OS);
135 Emit(Word2, OS);
Rafael Espindola525cf282013-05-22 01:36:19 +0000136 Emit((uint32_t) 0, OS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000137 } else {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000138 uint64_t Inst = getBinaryCodeForInstr(MI, Fixups, STI);
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000139 if ((STI.getFeatureBits()[AMDGPU::FeatureR600ALUInst]) &&
Tom Stellardecc2ad12013-05-17 15:23:21 +0000140 ((Desc.TSFlags & R600_InstFlag::OP1) ||
141 Desc.TSFlags & R600_InstFlag::OP2)) {
142 uint64_t ISAOpCode = Inst & (0x3FFULL << 39);
143 Inst &= ~(0x3FFULL << 39);
144 Inst |= ISAOpCode << 1;
145 }
Tom Stellardd93cede2013-05-06 17:50:57 +0000146 Emit(Inst, OS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000147 }
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
Tom Stellard75aadc22012-12-11 21:25:42 +0000158unsigned R600MCCodeEmitter::getHWReg(unsigned RegNo) const {
159 return MRI.getEncodingValue(RegNo) & HW_REG_MASK;
160}
161
162uint64_t R600MCCodeEmitter::getMachineOpValue(const MCInst &MI,
163 const MCOperand &MO,
Jan Veselya1f9fdf2016-05-13 20:39:26 +0000164 SmallVectorImpl<MCFixup> &Fixups,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000165 const MCSubtargetInfo &STI) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000166 if (MO.isReg()) {
Craig Topper35b2f752014-06-19 06:10:58 +0000167 if (HAS_NATIVE_OPERANDS(MCII.get(MI.getOpcode()).TSFlags))
Tom Stellard75aadc22012-12-11 21:25:42 +0000168 return MRI.getEncodingValue(MO.getReg());
Craig Topper35b2f752014-06-19 06:10:58 +0000169 return getHWReg(MO.getReg());
Tom Stellard75aadc22012-12-11 21:25:42 +0000170 }
Craig Topper35b2f752014-06-19 06:10:58 +0000171
Jan Veselya1f9fdf2016-05-13 20:39:26 +0000172 if (MO.isExpr()) {
Jan Veselya1f9fdf2016-05-13 20:39:26 +0000173 // We put rodata at the end of code section, then map the entire
174 // code secetion as vtx buf. Thus the section relative address is the
175 // correct one.
176 // Each R600 literal instruction has two operands
177 // We can't easily get the order of the current one, so compare against
178 // the first one and adjust offset.
179 const unsigned offset = (&MO == &MI.getOperand(0)) ? 0 : 4;
Jan Vesely3bc1af22016-06-25 18:24:16 +0000180 Fixups.push_back(MCFixup::create(offset, MO.getExpr(), FK_SecRel_4, MI.getLoc()));
Jan Veselya1f9fdf2016-05-13 20:39:26 +0000181 return 0;
182 }
183
Craig Topper35b2f752014-06-19 06:10:58 +0000184 assert(MO.isImm());
185 return MO.getImm();
Tom Stellard75aadc22012-12-11 21:25:42 +0000186}
187
Daniel Sanders72db2a32016-11-19 13:05:44 +0000188#define ENABLE_INSTR_PREDICATE_VERIFIER
Tom Stellard75aadc22012-12-11 21:25:42 +0000189#include "AMDGPUGenMCCodeEmitter.inc"