blob: 428248706bde5d6e592faa5c2296fd70b853815d [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 {
David Blaikie772d4f72013-02-18 23:11:17 +000035 SIMCCodeEmitter(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
36 void operator=(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
Tom Stellard75aadc22012-12-11 21:25:42 +000037 const MCInstrInfo &MCII;
38 const MCRegisterInfo &MRI;
Tom Stellard067c8152014-07-21 14:01:14 +000039 MCContext &Ctx;
Tom Stellard75aadc22012-12-11 21:25:42 +000040
Christian Konigc756cb992013-02-16 11:28:22 +000041 /// \brief Can this operand also contain immediate values?
42 bool isSrcOperand(const MCInstrDesc &Desc, unsigned OpNo) const;
43
44 /// \brief Encode an fp or int literal
Matt Arsenault11a4d672015-02-13 19:05:03 +000045 uint32_t getLitEncoding(const MCOperand &MO, unsigned OpSize) const;
Christian Konigc756cb992013-02-16 11:28:22 +000046
Tom Stellard75aadc22012-12-11 21:25:42 +000047public:
48 SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
David Woodhoused2cca112014-01-28 23:13:25 +000049 MCContext &ctx)
Tom Stellard067c8152014-07-21 14:01:14 +000050 : MCII(mcii), MRI(mri), Ctx(ctx) { }
Tom Stellard75aadc22012-12-11 21:25:42 +000051
52 ~SIMCCodeEmitter() { }
53
Alp Tokercb402912014-01-24 17:20:08 +000054 /// \brief Encode the instruction and write it to the OS.
Craig Topper5656db42014-04-29 07:57:24 +000055 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000056 SmallVectorImpl<MCFixup> &Fixups,
Craig Topper5656db42014-04-29 07:57:24 +000057 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000058
59 /// \returns the encoding for an MCOperand.
Craig Topper5656db42014-04-29 07:57:24 +000060 uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
61 SmallVectorImpl<MCFixup> &Fixups,
62 const MCSubtargetInfo &STI) const override;
Tom Stellard01825af2014-07-21 14:01:08 +000063
64 /// \brief Use a fixup to encode the simm16 field for SOPP branch
65 /// instructions.
66 unsigned getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
67 SmallVectorImpl<MCFixup> &Fixups,
68 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000069};
70
71} // End anonymous namespace
72
73MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
74 const MCRegisterInfo &MRI,
75 const MCSubtargetInfo &STI,
76 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000077 return new SIMCCodeEmitter(MCII, MRI, Ctx);
Tom Stellard75aadc22012-12-11 21:25:42 +000078}
79
Christian Konigc756cb992013-02-16 11:28:22 +000080bool SIMCCodeEmitter::isSrcOperand(const MCInstrDesc &Desc,
81 unsigned OpNo) const {
Tom Stellardb6550522015-01-12 19:33:18 +000082 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
83
84 return OpType == AMDGPU::OPERAND_REG_IMM32 ||
85 OpType == AMDGPU::OPERAND_REG_INLINE_C;
Christian Konigc756cb992013-02-16 11:28:22 +000086}
87
Matt Arsenault11a4d672015-02-13 19:05:03 +000088// Returns the encoding value to use if the given integer is an integer inline
89// immediate value, or 0 if it is not.
90template <typename IntTy>
91static uint32_t getIntInlineImmEncoding(IntTy Imm) {
92 if (Imm >= 0 && Imm <= 64)
93 return 128 + Imm;
Christian Konigc756cb992013-02-16 11:28:22 +000094
Matt Arsenault11a4d672015-02-13 19:05:03 +000095 if (Imm >= -16 && Imm <= -1)
96 return 192 + std::abs(Imm);
Christian Konigc756cb992013-02-16 11:28:22 +000097
Matt Arsenault11a4d672015-02-13 19:05:03 +000098 return 0;
99}
Christian Konigc756cb992013-02-16 11:28:22 +0000100
Matt Arsenault11a4d672015-02-13 19:05:03 +0000101static uint32_t getLit32Encoding(uint32_t Val) {
102 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int32_t>(Val));
103 if (IntImm != 0)
104 return IntImm;
Christian Konigc756cb992013-02-16 11:28:22 +0000105
Matt Arsenault11a4d672015-02-13 19:05:03 +0000106 if (Val == FloatToBits(0.5f))
Christian Konigc756cb992013-02-16 11:28:22 +0000107 return 240;
108
Matt Arsenault11a4d672015-02-13 19:05:03 +0000109 if (Val == FloatToBits(-0.5f))
Christian Konigc756cb992013-02-16 11:28:22 +0000110 return 241;
111
Matt Arsenault11a4d672015-02-13 19:05:03 +0000112 if (Val == FloatToBits(1.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000113 return 242;
114
Matt Arsenault11a4d672015-02-13 19:05:03 +0000115 if (Val == FloatToBits(-1.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000116 return 243;
117
Matt Arsenault11a4d672015-02-13 19:05:03 +0000118 if (Val == FloatToBits(2.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000119 return 244;
120
Matt Arsenault11a4d672015-02-13 19:05:03 +0000121 if (Val == FloatToBits(-2.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000122 return 245;
123
Matt Arsenault11a4d672015-02-13 19:05:03 +0000124 if (Val == FloatToBits(4.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000125 return 246;
126
Matt Arsenault11a4d672015-02-13 19:05:03 +0000127 if (Val == FloatToBits(-4.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000128 return 247;
129
130 return 255;
131}
132
Matt Arsenault11a4d672015-02-13 19:05:03 +0000133static uint32_t getLit64Encoding(uint64_t Val) {
134 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int64_t>(Val));
135 if (IntImm != 0)
136 return IntImm;
137
138 if (Val == DoubleToBits(0.5))
139 return 240;
140
141 if (Val == DoubleToBits(-0.5))
142 return 241;
143
144 if (Val == DoubleToBits(1.0))
145 return 242;
146
147 if (Val == DoubleToBits(-1.0))
148 return 243;
149
150 if (Val == DoubleToBits(2.0))
151 return 244;
152
153 if (Val == DoubleToBits(-2.0))
154 return 245;
155
156 if (Val == DoubleToBits(4.0))
157 return 246;
158
159 if (Val == DoubleToBits(-4.0))
160 return 247;
161
162 return 255;
163}
164
165uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO,
166 unsigned OpSize) const {
167 if (MO.isExpr())
168 return 255;
169
170 assert(!MO.isFPImm());
171
172 if (!MO.isImm())
173 return ~0;
174
175 if (OpSize == 4)
176 return getLit32Encoding(static_cast<uint32_t>(MO.getImm()));
177
178 assert(OpSize == 8);
179
180 return getLit64Encoding(static_cast<uint64_t>(MO.getImm()));
181}
182
Tom Stellard75aadc22012-12-11 21:25:42 +0000183void SIMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000184 SmallVectorImpl<MCFixup> &Fixups,
185 const MCSubtargetInfo &STI) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000186
David Woodhouse3fa98a62014-01-28 23:13:18 +0000187 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups, STI);
Christian Konigc756cb992013-02-16 11:28:22 +0000188 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
189 unsigned bytes = Desc.getSize();
190
Tom Stellard75aadc22012-12-11 21:25:42 +0000191 for (unsigned i = 0; i < bytes; i++) {
192 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
193 }
Christian Konigc756cb992013-02-16 11:28:22 +0000194
195 if (bytes > 4)
196 return;
197
198 // Check for additional literals in SRC0/1/2 (Op 1/2/3)
199 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
200
201 // Check if this operand should be encoded as [SV]Src
202 if (!isSrcOperand(Desc, i))
203 continue;
204
Matt Arsenault11a4d672015-02-13 19:05:03 +0000205 int RCID = Desc.OpInfo[i].RegClass;
206 const MCRegisterClass &RC = MRI.getRegClass(RCID);
207
Christian Konigc756cb992013-02-16 11:28:22 +0000208 // Is this operand a literal immediate?
209 const MCOperand &Op = MI.getOperand(i);
Matt Arsenault11a4d672015-02-13 19:05:03 +0000210 if (getLitEncoding(Op, RC.getSize()) != 255)
Christian Konigc756cb992013-02-16 11:28:22 +0000211 continue;
212
213 // Yes! Encode it
Matt Arsenault774e20b2015-02-13 19:05:07 +0000214 int64_t Imm = 0;
215
Christian Konigc756cb992013-02-16 11:28:22 +0000216 if (Op.isImm())
Matt Arsenault774e20b2015-02-13 19:05:07 +0000217 Imm = Op.getImm();
218 else if (!Op.isExpr()) // Exprs will be replaced with a fixup value.
219 llvm_unreachable("Must be immediate or expr");
Christian Konigc756cb992013-02-16 11:28:22 +0000220
221 for (unsigned j = 0; j < 4; j++) {
Matt Arsenault774e20b2015-02-13 19:05:07 +0000222 OS.write((uint8_t) ((Imm >> (8 * j)) & 0xff));
Christian Konigc756cb992013-02-16 11:28:22 +0000223 }
224
225 // Only one literal value allowed
226 break;
227 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000228}
229
Tom Stellard01825af2014-07-21 14:01:08 +0000230unsigned SIMCCodeEmitter::getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
231 SmallVectorImpl<MCFixup> &Fixups,
232 const MCSubtargetInfo &STI) const {
233 const MCOperand &MO = MI.getOperand(OpNo);
234
235 if (MO.isExpr()) {
236 const MCExpr *Expr = MO.getExpr();
237 MCFixupKind Kind = (MCFixupKind)AMDGPU::fixup_si_sopp_br;
238 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
239 return 0;
240 }
241
242 return getMachineOpValue(MI, MO, Fixups, STI);
243}
244
Tom Stellard75aadc22012-12-11 21:25:42 +0000245uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
246 const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000247 SmallVectorImpl<MCFixup> &Fixups,
248 const MCSubtargetInfo &STI) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000249 if (MO.isReg())
Tom Stellard1c822a82013-02-07 19:39:45 +0000250 return MRI.getEncodingValue(MO.getReg());
Christian Konigc756cb992013-02-16 11:28:22 +0000251
Tom Stellard067c8152014-07-21 14:01:14 +0000252 if (MO.isExpr()) {
253 const MCSymbolRefExpr *Expr = cast<MCSymbolRefExpr>(MO.getExpr());
254 MCFixupKind Kind;
255 const MCSymbol *Sym =
256 Ctx.GetOrCreateSymbol(StringRef(END_OF_TEXT_LABEL_NAME));
257
258 if (&Expr->getSymbol() == Sym) {
259 // Add the offset to the beginning of the constant values.
260 Kind = (MCFixupKind)AMDGPU::fixup_si_end_of_text;
261 } else {
262 // This is used for constant data stored in .rodata.
263 Kind = (MCFixupKind)AMDGPU::fixup_si_rodata;
264 }
265 Fixups.push_back(MCFixup::Create(4, Expr, Kind, MI.getLoc()));
266 }
267
Christian Konigc756cb992013-02-16 11:28:22 +0000268 // Figure out the operand number, needed for isSrcOperand check
269 unsigned OpNo = 0;
270 for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
271 if (&MO == &MI.getOperand(OpNo))
272 break;
273 }
274
275 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
276 if (isSrcOperand(Desc, OpNo)) {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000277 int RCID = Desc.OpInfo[OpNo].RegClass;
278 const MCRegisterClass &RC = MRI.getRegClass(RCID);
279
280 uint32_t Enc = getLitEncoding(MO, RC.getSize());
Christian Konigc756cb992013-02-16 11:28:22 +0000281 if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
282 return Enc;
283
284 } else if (MO.isImm())
285 return MO.getImm();
286
287 llvm_unreachable("Encoding of this operand type is not supported yet.");
Tom Stellard75aadc22012-12-11 21:25:42 +0000288 return 0;
289}
290