blob: 4a046acfabbcb3fbbef669d3dbcd5cf1228f4a9f [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"
Sam Kolton1eeb11b2016-09-09 14:44:04 +000021#include "Utils/AMDGPUBaseInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000022#include "llvm/MC/MCCodeEmitter.h"
23#include "llvm/MC/MCContext.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000024#include "llvm/MC/MCFixup.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025#include "llvm/MC/MCInst.h"
26#include "llvm/MC/MCInstrInfo.h"
27#include "llvm/MC/MCRegisterInfo.h"
28#include "llvm/MC/MCSubtargetInfo.h"
Reid Klecknera5b1eef2016-08-26 17:58:37 +000029#include "llvm/MC/MCSymbol.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000030#include "llvm/Support/raw_ostream.h"
31
Tom Stellard75aadc22012-12-11 21:25:42 +000032using namespace llvm;
33
34namespace {
Christian Konigc756cb992013-02-16 11:28:22 +000035
Tom Stellard75aadc22012-12-11 21:25:42 +000036class SIMCCodeEmitter : public AMDGPUMCCodeEmitter {
Aaron Ballmanf9a18972015-02-15 22:54:22 +000037 SIMCCodeEmitter(const SIMCCodeEmitter &) = delete;
38 void operator=(const SIMCCodeEmitter &) = delete;
Tom Stellard75aadc22012-12-11 21:25:42 +000039 const MCRegisterInfo &MRI;
Tom Stellard75aadc22012-12-11 21:25:42 +000040
Christian Konigc756cb992013-02-16 11:28:22 +000041 /// \brief Encode an fp or int literal
Matt Arsenault4bd72362016-12-10 00:39:12 +000042 uint32_t getLitEncoding(const MCOperand &MO, const MCOperandInfo &OpInfo,
Sam Kolton1eeb11b2016-09-09 14:44:04 +000043 const MCSubtargetInfo &STI) const;
Christian Konigc756cb992013-02-16 11:28:22 +000044
Tom Stellard75aadc22012-12-11 21:25:42 +000045public:
46 SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
David Woodhoused2cca112014-01-28 23:13:25 +000047 MCContext &ctx)
Daniel Sanders72db2a32016-11-19 13:05:44 +000048 : AMDGPUMCCodeEmitter(mcii), MRI(mri) {}
Tom Stellard75aadc22012-12-11 21:25:42 +000049
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000050 ~SIMCCodeEmitter() override {}
Tom Stellard75aadc22012-12-11 21:25:42 +000051
Alp Tokercb402912014-01-24 17:20:08 +000052 /// \brief Encode the instruction and write it to the OS.
Jim Grosbach91df21f2015-05-15 19:13:16 +000053 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000054 SmallVectorImpl<MCFixup> &Fixups,
Craig Topper5656db42014-04-29 07:57:24 +000055 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000056
57 /// \returns the encoding for an MCOperand.
Craig Topper5656db42014-04-29 07:57:24 +000058 uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
59 SmallVectorImpl<MCFixup> &Fixups,
60 const MCSubtargetInfo &STI) const override;
Tom Stellard01825af2014-07-21 14:01:08 +000061
62 /// \brief Use a fixup to encode the simm16 field for SOPP branch
63 /// instructions.
64 unsigned getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
65 SmallVectorImpl<MCFixup> &Fixups,
66 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000067};
68
69} // End anonymous namespace
70
71MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
72 const MCRegisterInfo &MRI,
Tom Stellard75aadc22012-12-11 21:25:42 +000073 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000074 return new SIMCCodeEmitter(MCII, MRI, Ctx);
Tom Stellard75aadc22012-12-11 21:25:42 +000075}
76
Matt Arsenault11a4d672015-02-13 19:05:03 +000077// Returns the encoding value to use if the given integer is an integer inline
78// immediate value, or 0 if it is not.
79template <typename IntTy>
80static uint32_t getIntInlineImmEncoding(IntTy Imm) {
81 if (Imm >= 0 && Imm <= 64)
82 return 128 + Imm;
Christian Konigc756cb992013-02-16 11:28:22 +000083
Matt Arsenault11a4d672015-02-13 19:05:03 +000084 if (Imm >= -16 && Imm <= -1)
85 return 192 + std::abs(Imm);
Christian Konigc756cb992013-02-16 11:28:22 +000086
Matt Arsenault11a4d672015-02-13 19:05:03 +000087 return 0;
88}
Christian Konigc756cb992013-02-16 11:28:22 +000089
Matt Arsenault4bd72362016-12-10 00:39:12 +000090static uint32_t getLit16Encoding(uint16_t Val, const MCSubtargetInfo &STI) {
91 uint16_t IntImm = getIntInlineImmEncoding(static_cast<int16_t>(Val));
92 if (IntImm != 0)
93 return IntImm;
94
95 if (Val == 0x3800) // 0.5
96 return 240;
97
98 if (Val == 0xB800) // -0.5
99 return 241;
100
101 if (Val == 0x3C00) // 1.0
102 return 242;
103
104 if (Val == 0xBC00) // -1.0
105 return 243;
106
107 if (Val == 0x4000) // 2.0
108 return 244;
109
110 if (Val == 0xC000) // -2.0
111 return 245;
112
113 if (Val == 0x4400) // 4.0
114 return 246;
115
116 if (Val == 0xC400) // -4.0
117 return 247;
118
119 if (Val == 0x3118 && // 1.0 / (2.0 * pi)
120 STI.getFeatureBits()[AMDGPU::FeatureInv2PiInlineImm])
121 return 248;
122
123 return 255;
124}
125
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000126static uint32_t getLit32Encoding(uint32_t Val, const MCSubtargetInfo &STI) {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000127 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int32_t>(Val));
128 if (IntImm != 0)
129 return IntImm;
Christian Konigc756cb992013-02-16 11:28:22 +0000130
Matt Arsenault11a4d672015-02-13 19:05:03 +0000131 if (Val == FloatToBits(0.5f))
Christian Konigc756cb992013-02-16 11:28:22 +0000132 return 240;
133
Matt Arsenault11a4d672015-02-13 19:05:03 +0000134 if (Val == FloatToBits(-0.5f))
Christian Konigc756cb992013-02-16 11:28:22 +0000135 return 241;
136
Matt Arsenault11a4d672015-02-13 19:05:03 +0000137 if (Val == FloatToBits(1.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000138 return 242;
139
Matt Arsenault11a4d672015-02-13 19:05:03 +0000140 if (Val == FloatToBits(-1.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000141 return 243;
142
Matt Arsenault11a4d672015-02-13 19:05:03 +0000143 if (Val == FloatToBits(2.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000144 return 244;
145
Matt Arsenault11a4d672015-02-13 19:05:03 +0000146 if (Val == FloatToBits(-2.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000147 return 245;
148
Matt Arsenault11a4d672015-02-13 19:05:03 +0000149 if (Val == FloatToBits(4.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000150 return 246;
151
Matt Arsenault11a4d672015-02-13 19:05:03 +0000152 if (Val == FloatToBits(-4.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000153 return 247;
154
Matt Arsenaultc88ba362016-10-29 04:05:06 +0000155 if (Val == 0x3e22f983 && // 1.0 / (2.0 * pi)
156 STI.getFeatureBits()[AMDGPU::FeatureInv2PiInlineImm])
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000157 return 248;
158
Christian Konigc756cb992013-02-16 11:28:22 +0000159 return 255;
160}
161
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000162static uint32_t getLit64Encoding(uint64_t Val, const MCSubtargetInfo &STI) {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000163 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int64_t>(Val));
164 if (IntImm != 0)
165 return IntImm;
166
167 if (Val == DoubleToBits(0.5))
168 return 240;
169
170 if (Val == DoubleToBits(-0.5))
171 return 241;
172
173 if (Val == DoubleToBits(1.0))
174 return 242;
175
176 if (Val == DoubleToBits(-1.0))
177 return 243;
178
179 if (Val == DoubleToBits(2.0))
180 return 244;
181
182 if (Val == DoubleToBits(-2.0))
183 return 245;
184
185 if (Val == DoubleToBits(4.0))
186 return 246;
187
188 if (Val == DoubleToBits(-4.0))
189 return 247;
190
Matt Arsenaultc88ba362016-10-29 04:05:06 +0000191 if (Val == 0x3fc45f306dc9c882 && // 1.0 / (2.0 * pi)
192 STI.getFeatureBits()[AMDGPU::FeatureInv2PiInlineImm])
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000193 return 248;
194
Matt Arsenault11a4d672015-02-13 19:05:03 +0000195 return 255;
196}
197
198uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO,
Matt Arsenault4bd72362016-12-10 00:39:12 +0000199 const MCOperandInfo &OpInfo,
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000200 const MCSubtargetInfo &STI) const {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000201
Tom Stellard82785e92016-06-15 03:09:39 +0000202 int64_t Imm;
203 if (MO.isExpr()) {
204 const MCConstantExpr *C = dyn_cast<MCConstantExpr>(MO.getExpr());
205 if (!C)
206 return 255;
Matt Arsenault11a4d672015-02-13 19:05:03 +0000207
Tom Stellard82785e92016-06-15 03:09:39 +0000208 Imm = C->getValue();
209 } else {
210
211 assert(!MO.isFPImm());
212
213 if (!MO.isImm())
214 return ~0;
215
216 Imm = MO.getImm();
217 }
Matt Arsenault11a4d672015-02-13 19:05:03 +0000218
Matt Arsenault4bd72362016-12-10 00:39:12 +0000219 switch (AMDGPU::getOperandSize(OpInfo)) {
220 case 4:
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000221 return getLit32Encoding(static_cast<uint32_t>(Imm), STI);
Matt Arsenault4bd72362016-12-10 00:39:12 +0000222 case 8:
223 return getLit64Encoding(static_cast<uint64_t>(Imm), STI);
224 case 2:
225 return getLit16Encoding(static_cast<uint16_t>(Imm), STI);
226 default:
227 llvm_unreachable("invalid operand size");
228 }
Matt Arsenault11a4d672015-02-13 19:05:03 +0000229}
230
Jim Grosbach91df21f2015-05-15 19:13:16 +0000231void SIMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000232 SmallVectorImpl<MCFixup> &Fixups,
233 const MCSubtargetInfo &STI) const {
Daniel Sanders72db2a32016-11-19 13:05:44 +0000234 verifyInstructionPredicates(MI,
235 computeAvailableFeatures(STI.getFeatureBits()));
Christian Konigc756cb992013-02-16 11:28:22 +0000236
David Woodhouse3fa98a62014-01-28 23:13:18 +0000237 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups, STI);
Christian Konigc756cb992013-02-16 11:28:22 +0000238 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
239 unsigned bytes = Desc.getSize();
240
Tom Stellard75aadc22012-12-11 21:25:42 +0000241 for (unsigned i = 0; i < bytes; i++) {
242 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
243 }
Christian Konigc756cb992013-02-16 11:28:22 +0000244
245 if (bytes > 4)
246 return;
247
248 // Check for additional literals in SRC0/1/2 (Op 1/2/3)
249 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
250
251 // Check if this operand should be encoded as [SV]Src
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000252 if (!AMDGPU::isSISrcOperand(Desc, i))
Christian Konigc756cb992013-02-16 11:28:22 +0000253 continue;
254
255 // Is this operand a literal immediate?
256 const MCOperand &Op = MI.getOperand(i);
Matt Arsenault4bd72362016-12-10 00:39:12 +0000257 if (getLitEncoding(Op, Desc.OpInfo[i], STI) != 255)
Christian Konigc756cb992013-02-16 11:28:22 +0000258 continue;
259
260 // Yes! Encode it
Matt Arsenault774e20b2015-02-13 19:05:07 +0000261 int64_t Imm = 0;
262
Christian Konigc756cb992013-02-16 11:28:22 +0000263 if (Op.isImm())
Matt Arsenault774e20b2015-02-13 19:05:07 +0000264 Imm = Op.getImm();
Tom Stellard82785e92016-06-15 03:09:39 +0000265 else if (Op.isExpr()) {
266 if (const MCConstantExpr *C = dyn_cast<MCConstantExpr>(Op.getExpr()))
267 Imm = C->getValue();
268
269 } else if (!Op.isExpr()) // Exprs will be replaced with a fixup value.
Matt Arsenault774e20b2015-02-13 19:05:07 +0000270 llvm_unreachable("Must be immediate or expr");
Christian Konigc756cb992013-02-16 11:28:22 +0000271
272 for (unsigned j = 0; j < 4; j++) {
Matt Arsenault774e20b2015-02-13 19:05:07 +0000273 OS.write((uint8_t) ((Imm >> (8 * j)) & 0xff));
Christian Konigc756cb992013-02-16 11:28:22 +0000274 }
275
276 // Only one literal value allowed
277 break;
278 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000279}
280
Tom Stellard01825af2014-07-21 14:01:08 +0000281unsigned SIMCCodeEmitter::getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
282 SmallVectorImpl<MCFixup> &Fixups,
283 const MCSubtargetInfo &STI) const {
284 const MCOperand &MO = MI.getOperand(OpNo);
285
286 if (MO.isExpr()) {
287 const MCExpr *Expr = MO.getExpr();
288 MCFixupKind Kind = (MCFixupKind)AMDGPU::fixup_si_sopp_br;
Jim Grosbach63661f82015-05-15 19:13:05 +0000289 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
Tom Stellard01825af2014-07-21 14:01:08 +0000290 return 0;
291 }
292
293 return getMachineOpValue(MI, MO, Fixups, STI);
294}
295
Tom Stellard75aadc22012-12-11 21:25:42 +0000296uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
297 const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000298 SmallVectorImpl<MCFixup> &Fixups,
299 const MCSubtargetInfo &STI) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000300 if (MO.isReg())
Tom Stellard1c822a82013-02-07 19:39:45 +0000301 return MRI.getEncodingValue(MO.getReg());
Christian Konigc756cb992013-02-16 11:28:22 +0000302
Tom Stellard82785e92016-06-15 03:09:39 +0000303 if (MO.isExpr() && MO.getExpr()->getKind() != MCExpr::Constant) {
Tom Stellardbf3e6e52016-06-14 20:29:59 +0000304 const MCSymbolRefExpr *Expr = dyn_cast<MCSymbolRefExpr>(MO.getExpr());
Tom Stellardf3af8412016-06-10 19:26:38 +0000305 MCFixupKind Kind;
Tom Stellardbf3e6e52016-06-14 20:29:59 +0000306 if (Expr && Expr->getSymbol().isExternal())
Tom Stellardf3af8412016-06-10 19:26:38 +0000307 Kind = FK_Data_4;
308 else
Tom Stellardbf3e6e52016-06-14 20:29:59 +0000309 Kind = FK_PCRel_4;
310 Fixups.push_back(MCFixup::create(4, MO.getExpr(), Kind, MI.getLoc()));
Tom Stellard067c8152014-07-21 14:01:14 +0000311 }
312
Christian Konigc756cb992013-02-16 11:28:22 +0000313 // Figure out the operand number, needed for isSrcOperand check
314 unsigned OpNo = 0;
315 for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
316 if (&MO == &MI.getOperand(OpNo))
317 break;
318 }
319
320 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000321 if (AMDGPU::isSISrcOperand(Desc, OpNo)) {
Matt Arsenault4bd72362016-12-10 00:39:12 +0000322 uint32_t Enc = getLitEncoding(MO, Desc.OpInfo[OpNo], STI);
Christian Konigc756cb992013-02-16 11:28:22 +0000323 if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
324 return Enc;
325
326 } else if (MO.isImm())
327 return MO.getImm();
328
329 llvm_unreachable("Encoding of this operand type is not supported yet.");
Tom Stellard75aadc22012-12-11 21:25:42 +0000330 return 0;
331}
332