blob: 12aaaa7c7aadc52523d7630d4a330aae6db1c6a1 [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
34/// \brief Helper type used in encoding
35typedef union {
Matt Arsenault11a4d672015-02-13 19:05:03 +000036 int64_t I;
37 double F;
Christian Konigc756cb992013-02-16 11:28:22 +000038} IntFloatUnion;
39
Tom Stellard75aadc22012-12-11 21:25:42 +000040class SIMCCodeEmitter : public AMDGPUMCCodeEmitter {
David Blaikie772d4f72013-02-18 23:11:17 +000041 SIMCCodeEmitter(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
42 void operator=(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
Tom Stellard75aadc22012-12-11 21:25:42 +000043 const MCInstrInfo &MCII;
44 const MCRegisterInfo &MRI;
Tom Stellard067c8152014-07-21 14:01:14 +000045 MCContext &Ctx;
Tom Stellard75aadc22012-12-11 21:25:42 +000046
Christian Konigc756cb992013-02-16 11:28:22 +000047 /// \brief Can this operand also contain immediate values?
48 bool isSrcOperand(const MCInstrDesc &Desc, unsigned OpNo) const;
49
50 /// \brief Encode an fp or int literal
Matt Arsenault11a4d672015-02-13 19:05:03 +000051 uint32_t getLitEncoding(const MCOperand &MO, unsigned OpSize) const;
Christian Konigc756cb992013-02-16 11:28:22 +000052
Tom Stellard75aadc22012-12-11 21:25:42 +000053public:
54 SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
David Woodhoused2cca112014-01-28 23:13:25 +000055 MCContext &ctx)
Tom Stellard067c8152014-07-21 14:01:14 +000056 : MCII(mcii), MRI(mri), Ctx(ctx) { }
Tom Stellard75aadc22012-12-11 21:25:42 +000057
58 ~SIMCCodeEmitter() { }
59
Alp Tokercb402912014-01-24 17:20:08 +000060 /// \brief Encode the instruction and write it to the OS.
Craig Topper5656db42014-04-29 07:57:24 +000061 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000062 SmallVectorImpl<MCFixup> &Fixups,
Craig Topper5656db42014-04-29 07:57:24 +000063 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000064
65 /// \returns the encoding for an MCOperand.
Craig Topper5656db42014-04-29 07:57:24 +000066 uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
67 SmallVectorImpl<MCFixup> &Fixups,
68 const MCSubtargetInfo &STI) const override;
Tom Stellard01825af2014-07-21 14:01:08 +000069
70 /// \brief Use a fixup to encode the simm16 field for SOPP branch
71 /// instructions.
72 unsigned getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
73 SmallVectorImpl<MCFixup> &Fixups,
74 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000075};
76
77} // End anonymous namespace
78
79MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
80 const MCRegisterInfo &MRI,
81 const MCSubtargetInfo &STI,
82 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000083 return new SIMCCodeEmitter(MCII, MRI, Ctx);
Tom Stellard75aadc22012-12-11 21:25:42 +000084}
85
Christian Konigc756cb992013-02-16 11:28:22 +000086bool SIMCCodeEmitter::isSrcOperand(const MCInstrDesc &Desc,
87 unsigned OpNo) const {
Tom Stellardb6550522015-01-12 19:33:18 +000088 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
89
90 return OpType == AMDGPU::OPERAND_REG_IMM32 ||
91 OpType == AMDGPU::OPERAND_REG_INLINE_C;
Christian Konigc756cb992013-02-16 11:28:22 +000092}
93
Matt Arsenault11a4d672015-02-13 19:05:03 +000094// Returns the encoding value to use if the given integer is an integer inline
95// immediate value, or 0 if it is not.
96template <typename IntTy>
97static uint32_t getIntInlineImmEncoding(IntTy Imm) {
98 if (Imm >= 0 && Imm <= 64)
99 return 128 + Imm;
Christian Konigc756cb992013-02-16 11:28:22 +0000100
Matt Arsenault11a4d672015-02-13 19:05:03 +0000101 if (Imm >= -16 && Imm <= -1)
102 return 192 + std::abs(Imm);
Christian Konigc756cb992013-02-16 11:28:22 +0000103
Matt Arsenault11a4d672015-02-13 19:05:03 +0000104 return 0;
105}
Christian Konigc756cb992013-02-16 11:28:22 +0000106
Matt Arsenault11a4d672015-02-13 19:05:03 +0000107static uint32_t getLit32Encoding(uint32_t Val) {
108 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int32_t>(Val));
109 if (IntImm != 0)
110 return IntImm;
Christian Konigc756cb992013-02-16 11:28:22 +0000111
Matt Arsenault11a4d672015-02-13 19:05:03 +0000112 if (Val == FloatToBits(0.5f))
Christian Konigc756cb992013-02-16 11:28:22 +0000113 return 240;
114
Matt Arsenault11a4d672015-02-13 19:05:03 +0000115 if (Val == FloatToBits(-0.5f))
Christian Konigc756cb992013-02-16 11:28:22 +0000116 return 241;
117
Matt Arsenault11a4d672015-02-13 19:05:03 +0000118 if (Val == FloatToBits(1.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000119 return 242;
120
Matt Arsenault11a4d672015-02-13 19:05:03 +0000121 if (Val == FloatToBits(-1.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000122 return 243;
123
Matt Arsenault11a4d672015-02-13 19:05:03 +0000124 if (Val == FloatToBits(2.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000125 return 244;
126
Matt Arsenault11a4d672015-02-13 19:05:03 +0000127 if (Val == FloatToBits(-2.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000128 return 245;
129
Matt Arsenault11a4d672015-02-13 19:05:03 +0000130 if (Val == FloatToBits(4.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000131 return 246;
132
Matt Arsenault11a4d672015-02-13 19:05:03 +0000133 if (Val == FloatToBits(-4.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000134 return 247;
135
136 return 255;
137}
138
Matt Arsenault11a4d672015-02-13 19:05:03 +0000139static uint32_t getLit64Encoding(uint64_t Val) {
140 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int64_t>(Val));
141 if (IntImm != 0)
142 return IntImm;
143
144 if (Val == DoubleToBits(0.5))
145 return 240;
146
147 if (Val == DoubleToBits(-0.5))
148 return 241;
149
150 if (Val == DoubleToBits(1.0))
151 return 242;
152
153 if (Val == DoubleToBits(-1.0))
154 return 243;
155
156 if (Val == DoubleToBits(2.0))
157 return 244;
158
159 if (Val == DoubleToBits(-2.0))
160 return 245;
161
162 if (Val == DoubleToBits(4.0))
163 return 246;
164
165 if (Val == DoubleToBits(-4.0))
166 return 247;
167
168 return 255;
169}
170
171uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO,
172 unsigned OpSize) const {
173 if (MO.isExpr())
174 return 255;
175
176 assert(!MO.isFPImm());
177
178 if (!MO.isImm())
179 return ~0;
180
181 if (OpSize == 4)
182 return getLit32Encoding(static_cast<uint32_t>(MO.getImm()));
183
184 assert(OpSize == 8);
185
186 return getLit64Encoding(static_cast<uint64_t>(MO.getImm()));
187}
188
Tom Stellard75aadc22012-12-11 21:25:42 +0000189void SIMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000190 SmallVectorImpl<MCFixup> &Fixups,
191 const MCSubtargetInfo &STI) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000192
David Woodhouse3fa98a62014-01-28 23:13:18 +0000193 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups, STI);
Christian Konigc756cb992013-02-16 11:28:22 +0000194 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
195 unsigned bytes = Desc.getSize();
196
Tom Stellard75aadc22012-12-11 21:25:42 +0000197 for (unsigned i = 0; i < bytes; i++) {
198 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
199 }
Christian Konigc756cb992013-02-16 11:28:22 +0000200
201 if (bytes > 4)
202 return;
203
204 // Check for additional literals in SRC0/1/2 (Op 1/2/3)
205 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
206
207 // Check if this operand should be encoded as [SV]Src
208 if (!isSrcOperand(Desc, i))
209 continue;
210
Matt Arsenault11a4d672015-02-13 19:05:03 +0000211 int RCID = Desc.OpInfo[i].RegClass;
212 const MCRegisterClass &RC = MRI.getRegClass(RCID);
213
Christian Konigc756cb992013-02-16 11:28:22 +0000214 // Is this operand a literal immediate?
215 const MCOperand &Op = MI.getOperand(i);
Matt Arsenault11a4d672015-02-13 19:05:03 +0000216 if (getLitEncoding(Op, RC.getSize()) != 255)
Christian Konigc756cb992013-02-16 11:28:22 +0000217 continue;
218
219 // Yes! Encode it
220 IntFloatUnion Imm;
221 if (Op.isImm())
222 Imm.I = Op.getImm();
Tom Stellard067c8152014-07-21 14:01:14 +0000223 else if (Op.isFPImm())
Christian Konigc756cb992013-02-16 11:28:22 +0000224 Imm.F = Op.getFPImm();
Tom Stellard067c8152014-07-21 14:01:14 +0000225 else {
226 assert(Op.isExpr());
227 // This will be replaced with a fixup value.
228 Imm.I = 0;
229 }
Christian Konigc756cb992013-02-16 11:28:22 +0000230
231 for (unsigned j = 0; j < 4; j++) {
232 OS.write((uint8_t) ((Imm.I >> (8 * j)) & 0xff));
233 }
234
235 // Only one literal value allowed
236 break;
237 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000238}
239
Tom Stellard01825af2014-07-21 14:01:08 +0000240unsigned SIMCCodeEmitter::getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
241 SmallVectorImpl<MCFixup> &Fixups,
242 const MCSubtargetInfo &STI) const {
243 const MCOperand &MO = MI.getOperand(OpNo);
244
245 if (MO.isExpr()) {
246 const MCExpr *Expr = MO.getExpr();
247 MCFixupKind Kind = (MCFixupKind)AMDGPU::fixup_si_sopp_br;
248 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
249 return 0;
250 }
251
252 return getMachineOpValue(MI, MO, Fixups, STI);
253}
254
Tom Stellard75aadc22012-12-11 21:25:42 +0000255uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
256 const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000257 SmallVectorImpl<MCFixup> &Fixups,
258 const MCSubtargetInfo &STI) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000259 if (MO.isReg())
Tom Stellard1c822a82013-02-07 19:39:45 +0000260 return MRI.getEncodingValue(MO.getReg());
Christian Konigc756cb992013-02-16 11:28:22 +0000261
Tom Stellard067c8152014-07-21 14:01:14 +0000262 if (MO.isExpr()) {
263 const MCSymbolRefExpr *Expr = cast<MCSymbolRefExpr>(MO.getExpr());
264 MCFixupKind Kind;
265 const MCSymbol *Sym =
266 Ctx.GetOrCreateSymbol(StringRef(END_OF_TEXT_LABEL_NAME));
267
268 if (&Expr->getSymbol() == Sym) {
269 // Add the offset to the beginning of the constant values.
270 Kind = (MCFixupKind)AMDGPU::fixup_si_end_of_text;
271 } else {
272 // This is used for constant data stored in .rodata.
273 Kind = (MCFixupKind)AMDGPU::fixup_si_rodata;
274 }
275 Fixups.push_back(MCFixup::Create(4, Expr, Kind, MI.getLoc()));
276 }
277
Christian Konigc756cb992013-02-16 11:28:22 +0000278 // Figure out the operand number, needed for isSrcOperand check
279 unsigned OpNo = 0;
280 for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
281 if (&MO == &MI.getOperand(OpNo))
282 break;
283 }
284
285 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
286 if (isSrcOperand(Desc, OpNo)) {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000287 int RCID = Desc.OpInfo[OpNo].RegClass;
288 const MCRegisterClass &RC = MRI.getRegClass(RCID);
289
290 uint32_t Enc = getLitEncoding(MO, RC.getSize());
Christian Konigc756cb992013-02-16 11:28:22 +0000291 if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
292 return Enc;
293
294 } else if (MO.isImm())
295 return MO.getImm();
296
297 llvm_unreachable("Encoding of this operand type is not supported yet.");
Tom Stellard75aadc22012-12-11 21:25:42 +0000298 return 0;
299}
300