blob: cd16fe0d0db66fff145125a69888b3499806df51 [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 MCInstrInfo &MCII;
40 const MCRegisterInfo &MRI;
Tom Stellard75aadc22012-12-11 21:25:42 +000041
Christian Konigc756cb992013-02-16 11:28:22 +000042 /// \brief Encode an fp or int literal
Sam Kolton1eeb11b2016-09-09 14:44:04 +000043 uint32_t getLitEncoding(const MCOperand &MO, unsigned OpSize,
44 const MCSubtargetInfo &STI) 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
Matt Arsenault11a4d672015-02-13 19:05:03 +000078// Returns the encoding value to use if the given integer is an integer inline
79// immediate value, or 0 if it is not.
80template <typename IntTy>
81static uint32_t getIntInlineImmEncoding(IntTy Imm) {
82 if (Imm >= 0 && Imm <= 64)
83 return 128 + Imm;
Christian Konigc756cb992013-02-16 11:28:22 +000084
Matt Arsenault11a4d672015-02-13 19:05:03 +000085 if (Imm >= -16 && Imm <= -1)
86 return 192 + std::abs(Imm);
Christian Konigc756cb992013-02-16 11:28:22 +000087
Matt Arsenault11a4d672015-02-13 19:05:03 +000088 return 0;
89}
Christian Konigc756cb992013-02-16 11:28:22 +000090
Sam Kolton1eeb11b2016-09-09 14:44:04 +000091static uint32_t getLit32Encoding(uint32_t Val, const MCSubtargetInfo &STI) {
Matt Arsenault11a4d672015-02-13 19:05:03 +000092 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int32_t>(Val));
93 if (IntImm != 0)
94 return IntImm;
Christian Konigc756cb992013-02-16 11:28:22 +000095
Matt Arsenault11a4d672015-02-13 19:05:03 +000096 if (Val == FloatToBits(0.5f))
Christian Konigc756cb992013-02-16 11:28:22 +000097 return 240;
98
Matt Arsenault11a4d672015-02-13 19:05:03 +000099 if (Val == FloatToBits(-0.5f))
Christian Konigc756cb992013-02-16 11:28:22 +0000100 return 241;
101
Matt Arsenault11a4d672015-02-13 19:05:03 +0000102 if (Val == FloatToBits(1.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000103 return 242;
104
Matt Arsenault11a4d672015-02-13 19:05:03 +0000105 if (Val == FloatToBits(-1.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000106 return 243;
107
Matt Arsenault11a4d672015-02-13 19:05:03 +0000108 if (Val == FloatToBits(2.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000109 return 244;
110
Matt Arsenault11a4d672015-02-13 19:05:03 +0000111 if (Val == FloatToBits(-2.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000112 return 245;
113
Matt Arsenault11a4d672015-02-13 19:05:03 +0000114 if (Val == FloatToBits(4.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000115 return 246;
116
Matt Arsenault11a4d672015-02-13 19:05:03 +0000117 if (Val == FloatToBits(-4.0f))
Christian Konigc756cb992013-02-16 11:28:22 +0000118 return 247;
119
Matt Arsenaultc88ba362016-10-29 04:05:06 +0000120 if (Val == 0x3e22f983 && // 1.0 / (2.0 * pi)
121 STI.getFeatureBits()[AMDGPU::FeatureInv2PiInlineImm])
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000122 return 248;
123
Christian Konigc756cb992013-02-16 11:28:22 +0000124 return 255;
125}
126
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000127static uint32_t getLit64Encoding(uint64_t Val, const MCSubtargetInfo &STI) {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000128 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int64_t>(Val));
129 if (IntImm != 0)
130 return IntImm;
131
132 if (Val == DoubleToBits(0.5))
133 return 240;
134
135 if (Val == DoubleToBits(-0.5))
136 return 241;
137
138 if (Val == DoubleToBits(1.0))
139 return 242;
140
141 if (Val == DoubleToBits(-1.0))
142 return 243;
143
144 if (Val == DoubleToBits(2.0))
145 return 244;
146
147 if (Val == DoubleToBits(-2.0))
148 return 245;
149
150 if (Val == DoubleToBits(4.0))
151 return 246;
152
153 if (Val == DoubleToBits(-4.0))
154 return 247;
155
Matt Arsenaultc88ba362016-10-29 04:05:06 +0000156 if (Val == 0x3fc45f306dc9c882 && // 1.0 / (2.0 * pi)
157 STI.getFeatureBits()[AMDGPU::FeatureInv2PiInlineImm])
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000158 return 248;
159
Matt Arsenault11a4d672015-02-13 19:05:03 +0000160 return 255;
161}
162
163uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO,
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000164 unsigned OpSize,
165 const MCSubtargetInfo &STI) const {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000166
Tom Stellard82785e92016-06-15 03:09:39 +0000167 int64_t Imm;
168 if (MO.isExpr()) {
169 const MCConstantExpr *C = dyn_cast<MCConstantExpr>(MO.getExpr());
170 if (!C)
171 return 255;
Matt Arsenault11a4d672015-02-13 19:05:03 +0000172
Tom Stellard82785e92016-06-15 03:09:39 +0000173 Imm = C->getValue();
174 } else {
175
176 assert(!MO.isFPImm());
177
178 if (!MO.isImm())
179 return ~0;
180
181 Imm = MO.getImm();
182 }
Matt Arsenault11a4d672015-02-13 19:05:03 +0000183
184 if (OpSize == 4)
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000185 return getLit32Encoding(static_cast<uint32_t>(Imm), STI);
Matt Arsenault11a4d672015-02-13 19:05:03 +0000186
187 assert(OpSize == 8);
188
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000189 return getLit64Encoding(static_cast<uint64_t>(Imm), STI);
Matt Arsenault11a4d672015-02-13 19:05:03 +0000190}
191
Jim Grosbach91df21f2015-05-15 19:13:16 +0000192void SIMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000193 SmallVectorImpl<MCFixup> &Fixups,
194 const MCSubtargetInfo &STI) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000195
David Woodhouse3fa98a62014-01-28 23:13:18 +0000196 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups, STI);
Christian Konigc756cb992013-02-16 11:28:22 +0000197 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
198 unsigned bytes = Desc.getSize();
199
Tom Stellard75aadc22012-12-11 21:25:42 +0000200 for (unsigned i = 0; i < bytes; i++) {
201 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
202 }
Christian Konigc756cb992013-02-16 11:28:22 +0000203
204 if (bytes > 4)
205 return;
206
207 // Check for additional literals in SRC0/1/2 (Op 1/2/3)
208 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
209
210 // Check if this operand should be encoded as [SV]Src
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000211 if (!AMDGPU::isSISrcOperand(Desc, i))
Christian Konigc756cb992013-02-16 11:28:22 +0000212 continue;
213
Matt Arsenault11a4d672015-02-13 19:05:03 +0000214 int RCID = Desc.OpInfo[i].RegClass;
215 const MCRegisterClass &RC = MRI.getRegClass(RCID);
216
Christian Konigc756cb992013-02-16 11:28:22 +0000217 // Is this operand a literal immediate?
218 const MCOperand &Op = MI.getOperand(i);
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000219 if (getLitEncoding(Op, AMDGPU::getRegBitWidth(RC) / 8, STI) != 255)
Christian Konigc756cb992013-02-16 11:28:22 +0000220 continue;
221
222 // Yes! Encode it
Matt Arsenault774e20b2015-02-13 19:05:07 +0000223 int64_t Imm = 0;
224
Christian Konigc756cb992013-02-16 11:28:22 +0000225 if (Op.isImm())
Matt Arsenault774e20b2015-02-13 19:05:07 +0000226 Imm = Op.getImm();
Tom Stellard82785e92016-06-15 03:09:39 +0000227 else if (Op.isExpr()) {
228 if (const MCConstantExpr *C = dyn_cast<MCConstantExpr>(Op.getExpr()))
229 Imm = C->getValue();
230
231 } else if (!Op.isExpr()) // Exprs will be replaced with a fixup value.
Matt Arsenault774e20b2015-02-13 19:05:07 +0000232 llvm_unreachable("Must be immediate or expr");
Christian Konigc756cb992013-02-16 11:28:22 +0000233
234 for (unsigned j = 0; j < 4; j++) {
Matt Arsenault774e20b2015-02-13 19:05:07 +0000235 OS.write((uint8_t) ((Imm >> (8 * j)) & 0xff));
Christian Konigc756cb992013-02-16 11:28:22 +0000236 }
237
238 // Only one literal value allowed
239 break;
240 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000241}
242
Tom Stellard01825af2014-07-21 14:01:08 +0000243unsigned SIMCCodeEmitter::getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
244 SmallVectorImpl<MCFixup> &Fixups,
245 const MCSubtargetInfo &STI) const {
246 const MCOperand &MO = MI.getOperand(OpNo);
247
248 if (MO.isExpr()) {
249 const MCExpr *Expr = MO.getExpr();
250 MCFixupKind Kind = (MCFixupKind)AMDGPU::fixup_si_sopp_br;
Jim Grosbach63661f82015-05-15 19:13:05 +0000251 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
Tom Stellard01825af2014-07-21 14:01:08 +0000252 return 0;
253 }
254
255 return getMachineOpValue(MI, MO, Fixups, STI);
256}
257
Tom Stellard75aadc22012-12-11 21:25:42 +0000258uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
259 const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000260 SmallVectorImpl<MCFixup> &Fixups,
261 const MCSubtargetInfo &STI) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000262 if (MO.isReg())
Tom Stellard1c822a82013-02-07 19:39:45 +0000263 return MRI.getEncodingValue(MO.getReg());
Christian Konigc756cb992013-02-16 11:28:22 +0000264
Tom Stellard82785e92016-06-15 03:09:39 +0000265 if (MO.isExpr() && MO.getExpr()->getKind() != MCExpr::Constant) {
Tom Stellardbf3e6e52016-06-14 20:29:59 +0000266 const MCSymbolRefExpr *Expr = dyn_cast<MCSymbolRefExpr>(MO.getExpr());
Tom Stellardf3af8412016-06-10 19:26:38 +0000267 MCFixupKind Kind;
Tom Stellardbf3e6e52016-06-14 20:29:59 +0000268 if (Expr && Expr->getSymbol().isExternal())
Tom Stellardf3af8412016-06-10 19:26:38 +0000269 Kind = FK_Data_4;
270 else
Tom Stellardbf3e6e52016-06-14 20:29:59 +0000271 Kind = FK_PCRel_4;
272 Fixups.push_back(MCFixup::create(4, MO.getExpr(), Kind, MI.getLoc()));
Tom Stellard067c8152014-07-21 14:01:14 +0000273 }
274
Christian Konigc756cb992013-02-16 11:28:22 +0000275 // Figure out the operand number, needed for isSrcOperand check
276 unsigned OpNo = 0;
277 for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
278 if (&MO == &MI.getOperand(OpNo))
279 break;
280 }
281
282 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000283 if (AMDGPU::isSISrcOperand(Desc, OpNo)) {
284 uint32_t Enc = getLitEncoding(MO,
285 AMDGPU::getRegOperandSize(&MRI, Desc, OpNo),
286 STI);
Christian Konigc756cb992013-02-16 11:28:22 +0000287 if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
288 return Enc;
289
290 } else if (MO.isImm())
291 return MO.getImm();
292
293 llvm_unreachable("Encoding of this operand type is not supported yet.");
Tom Stellard75aadc22012-12-11 21:25:42 +0000294 return 0;
295}
296