blob: 533a54c1a8b60473e771fdd06fb15efa49372a7e [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIMCCodeEmitter.cpp - SI Code Emitter -------------------------------===//
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/// \brief The SI code emitter produces machine code that can be executed
12/// directly on the GPU device.
13//
14//===----------------------------------------------------------------------===//
15
Tom Stellard067c8152014-07-21 14:01:14 +000016#include "AMDGPU.h"
Tom Stellard01825af2014-07-21 14:01:08 +000017#include "MCTargetDesc/AMDGPUFixupKinds.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "MCTargetDesc/AMDGPUMCCodeEmitter.h"
19#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
20#include "SIDefines.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "llvm/MC/MCCodeEmitter.h"
22#include "llvm/MC/MCContext.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000023#include "llvm/MC/MCFixup.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000024#include "llvm/MC/MCInst.h"
25#include "llvm/MC/MCInstrInfo.h"
26#include "llvm/MC/MCRegisterInfo.h"
27#include "llvm/MC/MCSubtargetInfo.h"
28#include "llvm/Support/raw_ostream.h"
29
Tom Stellard75aadc22012-12-11 21:25:42 +000030using namespace llvm;
31
32namespace {
Christian Konigc756cb992013-02-16 11:28:22 +000033
Tom Stellard75aadc22012-12-11 21:25:42 +000034class SIMCCodeEmitter : public AMDGPUMCCodeEmitter {
Aaron Ballmanf9a18972015-02-15 22:54:22 +000035 SIMCCodeEmitter(const SIMCCodeEmitter &) = delete;
36 void operator=(const SIMCCodeEmitter &) = delete;
Tom Stellard75aadc22012-12-11 21:25:42 +000037 const MCInstrInfo &MCII;
38 const MCRegisterInfo &MRI;
Tom Stellard75aadc22012-12-11 21:25:42 +000039
Christian Konigc756cb992013-02-16 11:28:22 +000040 /// \brief Can this operand also contain immediate values?
41 bool isSrcOperand(const MCInstrDesc &Desc, unsigned OpNo) const;
42
43 /// \brief Encode an fp or int literal
Matt Arsenault11a4d672015-02-13 19:05:03 +000044 uint32_t getLitEncoding(const MCOperand &MO, unsigned OpSize) const;
Christian Konigc756cb992013-02-16 11:28:22 +000045
Tom Stellard75aadc22012-12-11 21:25:42 +000046public:
47 SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
David Woodhoused2cca112014-01-28 23:13:25 +000048 MCContext &ctx)
Tom Stellardc2d65432015-12-10 03:10:46 +000049 : MCII(mcii), MRI(mri) { }
Tom Stellard75aadc22012-12-11 21:25:42 +000050
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000051 ~SIMCCodeEmitter() override {}
Tom Stellard75aadc22012-12-11 21:25:42 +000052
Alp Tokercb402912014-01-24 17:20:08 +000053 /// \brief Encode the instruction and write it to the OS.
Jim Grosbach91df21f2015-05-15 19:13:16 +000054 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000055 SmallVectorImpl<MCFixup> &Fixups,
Craig Topper5656db42014-04-29 07:57:24 +000056 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000057
58 /// \returns the encoding for an MCOperand.
Craig Topper5656db42014-04-29 07:57:24 +000059 uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
60 SmallVectorImpl<MCFixup> &Fixups,
61 const MCSubtargetInfo &STI) const override;
Tom Stellard01825af2014-07-21 14:01:08 +000062
63 /// \brief Use a fixup to encode the simm16 field for SOPP branch
64 /// instructions.
65 unsigned getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
66 SmallVectorImpl<MCFixup> &Fixups,
67 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000068};
69
70} // End anonymous namespace
71
72MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
73 const MCRegisterInfo &MRI,
Tom Stellard75aadc22012-12-11 21:25:42 +000074 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000075 return new SIMCCodeEmitter(MCII, MRI, Ctx);
Tom Stellard75aadc22012-12-11 21:25:42 +000076}
77
Christian Konigc756cb992013-02-16 11:28:22 +000078bool SIMCCodeEmitter::isSrcOperand(const MCInstrDesc &Desc,
79 unsigned OpNo) const {
Tom Stellardb6550522015-01-12 19:33:18 +000080 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
81
82 return OpType == AMDGPU::OPERAND_REG_IMM32 ||
83 OpType == AMDGPU::OPERAND_REG_INLINE_C;
Christian Konigc756cb992013-02-16 11:28:22 +000084}
85
Matt Arsenault11a4d672015-02-13 19:05:03 +000086// Returns the encoding value to use if the given integer is an integer inline
87// immediate value, or 0 if it is not.
88template <typename IntTy>
89static uint32_t getIntInlineImmEncoding(IntTy Imm) {
90 if (Imm >= 0 && Imm <= 64)
91 return 128 + Imm;
Christian Konigc756cb992013-02-16 11:28:22 +000092
Matt Arsenault11a4d672015-02-13 19:05:03 +000093 if (Imm >= -16 && Imm <= -1)
94 return 192 + std::abs(Imm);
Christian Konigc756cb992013-02-16 11:28:22 +000095
Matt Arsenault11a4d672015-02-13 19:05:03 +000096 return 0;
97}
Christian Konigc756cb992013-02-16 11:28:22 +000098
Matt Arsenault11a4d672015-02-13 19:05:03 +000099static uint32_t getLit32Encoding(uint32_t Val) {
100 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int32_t>(Val));
101 if (IntImm != 0)
102 return IntImm;
Christian Konigc756cb992013-02-16 11:28:22 +0000103
Matt Arsenault11a4d672015-02-13 19:05:03 +0000104 if (Val == FloatToBits(0.5f))
Christian Konigc756cb992013-02-16 11:28:22 +0000105 return 240;
106
Matt Arsenault11a4d672015-02-13 19:05:03 +0000107 if (Val == FloatToBits(-0.5f))
Christian Konigc756cb992013-02-16 11:28:22 +0000108 return 241;
109
Matt Arsenault11a4d672015-02-13 19:05:03 +0000110 if (Val == FloatToBits(1.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000111 return 242;
112
Matt Arsenault11a4d672015-02-13 19:05:03 +0000113 if (Val == FloatToBits(-1.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000114 return 243;
115
Matt Arsenault11a4d672015-02-13 19:05:03 +0000116 if (Val == FloatToBits(2.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000117 return 244;
118
Matt Arsenault11a4d672015-02-13 19:05:03 +0000119 if (Val == FloatToBits(-2.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000120 return 245;
121
Matt Arsenault11a4d672015-02-13 19:05:03 +0000122 if (Val == FloatToBits(4.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000123 return 246;
124
Matt Arsenault11a4d672015-02-13 19:05:03 +0000125 if (Val == FloatToBits(-4.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000126 return 247;
127
128 return 255;
129}
130
Matt Arsenault11a4d672015-02-13 19:05:03 +0000131static uint32_t getLit64Encoding(uint64_t Val) {
132 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int64_t>(Val));
133 if (IntImm != 0)
134 return IntImm;
135
136 if (Val == DoubleToBits(0.5))
137 return 240;
138
139 if (Val == DoubleToBits(-0.5))
140 return 241;
141
142 if (Val == DoubleToBits(1.0))
143 return 242;
144
145 if (Val == DoubleToBits(-1.0))
146 return 243;
147
148 if (Val == DoubleToBits(2.0))
149 return 244;
150
151 if (Val == DoubleToBits(-2.0))
152 return 245;
153
154 if (Val == DoubleToBits(4.0))
155 return 246;
156
157 if (Val == DoubleToBits(-4.0))
158 return 247;
159
160 return 255;
161}
162
163uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO,
164 unsigned OpSize) const {
165 if (MO.isExpr())
166 return 255;
167
168 assert(!MO.isFPImm());
169
170 if (!MO.isImm())
171 return ~0;
172
173 if (OpSize == 4)
174 return getLit32Encoding(static_cast<uint32_t>(MO.getImm()));
175
176 assert(OpSize == 8);
177
178 return getLit64Encoding(static_cast<uint64_t>(MO.getImm()));
179}
180
Jim Grosbach91df21f2015-05-15 19:13:16 +0000181void SIMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000182 SmallVectorImpl<MCFixup> &Fixups,
183 const MCSubtargetInfo &STI) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000184
David Woodhouse3fa98a62014-01-28 23:13:18 +0000185 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups, STI);
Christian Konigc756cb992013-02-16 11:28:22 +0000186 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
187 unsigned bytes = Desc.getSize();
188
Tom Stellard75aadc22012-12-11 21:25:42 +0000189 for (unsigned i = 0; i < bytes; i++) {
190 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
191 }
Christian Konigc756cb992013-02-16 11:28:22 +0000192
193 if (bytes > 4)
194 return;
195
196 // Check for additional literals in SRC0/1/2 (Op 1/2/3)
197 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
198
199 // Check if this operand should be encoded as [SV]Src
200 if (!isSrcOperand(Desc, i))
201 continue;
202
Matt Arsenault11a4d672015-02-13 19:05:03 +0000203 int RCID = Desc.OpInfo[i].RegClass;
204 const MCRegisterClass &RC = MRI.getRegClass(RCID);
205
Christian Konigc756cb992013-02-16 11:28:22 +0000206 // Is this operand a literal immediate?
207 const MCOperand &Op = MI.getOperand(i);
Matt Arsenault11a4d672015-02-13 19:05:03 +0000208 if (getLitEncoding(Op, RC.getSize()) != 255)
Christian Konigc756cb992013-02-16 11:28:22 +0000209 continue;
210
211 // Yes! Encode it
Matt Arsenault774e20b2015-02-13 19:05:07 +0000212 int64_t Imm = 0;
213
Christian Konigc756cb992013-02-16 11:28:22 +0000214 if (Op.isImm())
Matt Arsenault774e20b2015-02-13 19:05:07 +0000215 Imm = Op.getImm();
216 else if (!Op.isExpr()) // Exprs will be replaced with a fixup value.
217 llvm_unreachable("Must be immediate or expr");
Christian Konigc756cb992013-02-16 11:28:22 +0000218
219 for (unsigned j = 0; j < 4; j++) {
Matt Arsenault774e20b2015-02-13 19:05:07 +0000220 OS.write((uint8_t) ((Imm >> (8 * j)) & 0xff));
Christian Konigc756cb992013-02-16 11:28:22 +0000221 }
222
223 // Only one literal value allowed
224 break;
225 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000226}
227
Tom Stellard01825af2014-07-21 14:01:08 +0000228unsigned SIMCCodeEmitter::getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
229 SmallVectorImpl<MCFixup> &Fixups,
230 const MCSubtargetInfo &STI) const {
231 const MCOperand &MO = MI.getOperand(OpNo);
232
233 if (MO.isExpr()) {
234 const MCExpr *Expr = MO.getExpr();
235 MCFixupKind Kind = (MCFixupKind)AMDGPU::fixup_si_sopp_br;
Jim Grosbach63661f82015-05-15 19:13:05 +0000236 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
Tom Stellard01825af2014-07-21 14:01:08 +0000237 return 0;
238 }
239
240 return getMachineOpValue(MI, MO, Fixups, STI);
241}
242
Tom Stellard75aadc22012-12-11 21:25:42 +0000243uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
244 const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000245 SmallVectorImpl<MCFixup> &Fixups,
246 const MCSubtargetInfo &STI) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000247 if (MO.isReg())
Tom Stellard1c822a82013-02-07 19:39:45 +0000248 return MRI.getEncodingValue(MO.getReg());
Christian Konigc756cb992013-02-16 11:28:22 +0000249
Tom Stellard067c8152014-07-21 14:01:14 +0000250 if (MO.isExpr()) {
251 const MCSymbolRefExpr *Expr = cast<MCSymbolRefExpr>(MO.getExpr());
Tom Stellardf3af8412016-06-10 19:26:38 +0000252 const MCSymbol &Sym = Expr->getSymbol();
253 MCFixupKind Kind;
254 if (Sym.isExternal())
255 Kind = FK_Data_4;
256 else
257 Kind = (MCFixupKind)AMDGPU::fixup_si_rodata;
Jim Grosbach63661f82015-05-15 19:13:05 +0000258 Fixups.push_back(MCFixup::create(4, Expr, Kind, MI.getLoc()));
Tom Stellard067c8152014-07-21 14:01:14 +0000259 }
260
Christian Konigc756cb992013-02-16 11:28:22 +0000261 // Figure out the operand number, needed for isSrcOperand check
262 unsigned OpNo = 0;
263 for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
264 if (&MO == &MI.getOperand(OpNo))
265 break;
266 }
267
268 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
269 if (isSrcOperand(Desc, OpNo)) {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000270 int RCID = Desc.OpInfo[OpNo].RegClass;
271 const MCRegisterClass &RC = MRI.getRegClass(RCID);
272
273 uint32_t Enc = getLitEncoding(MO, RC.getSize());
Christian Konigc756cb992013-02-16 11:28:22 +0000274 if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
275 return Enc;
276
277 } else if (MO.isImm())
278 return MO.getImm();
279
280 llvm_unreachable("Encoding of this operand type is not supported yet.");
Tom Stellard75aadc22012-12-11 21:25:42 +0000281 return 0;
282}
283