blob: 78776c11d75de1bbbf20742671d0cafe711845e5 [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 Stellard75aadc22012-12-11 21:25:42 +000017#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
18#include "MCTargetDesc/AMDGPUMCCodeEmitter.h"
Tom Stellard01825af2014-07-21 14:01:08 +000019#include "MCTargetDesc/AMDGPUFixupKinds.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000020#include "llvm/MC/MCCodeEmitter.h"
21#include "llvm/MC/MCContext.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000022#include "llvm/MC/MCFixup.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000023#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MC/MCRegisterInfo.h"
26#include "llvm/MC/MCSubtargetInfo.h"
27#include "llvm/Support/raw_ostream.h"
28
Tom Stellard75aadc22012-12-11 21:25:42 +000029using namespace llvm;
30
31namespace {
Christian Konigc756cb992013-02-16 11:28:22 +000032
33/// \brief Helper type used in encoding
34typedef union {
35 int32_t I;
36 float F;
37} IntFloatUnion;
38
Tom Stellard75aadc22012-12-11 21:25:42 +000039class SIMCCodeEmitter : public AMDGPUMCCodeEmitter {
David Blaikie772d4f72013-02-18 23:11:17 +000040 SIMCCodeEmitter(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
41 void operator=(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
Tom Stellard75aadc22012-12-11 21:25:42 +000042 const MCInstrInfo &MCII;
43 const MCRegisterInfo &MRI;
Tom Stellard067c8152014-07-21 14:01:14 +000044 MCContext &Ctx;
Tom Stellard75aadc22012-12-11 21:25:42 +000045
Christian Konigc756cb992013-02-16 11:28:22 +000046 /// \brief Can this operand also contain immediate values?
47 bool isSrcOperand(const MCInstrDesc &Desc, unsigned OpNo) const;
48
49 /// \brief Encode an fp or int literal
50 uint32_t getLitEncoding(const MCOperand &MO) const;
51
Tom Stellard75aadc22012-12-11 21:25:42 +000052public:
53 SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
David Woodhoused2cca112014-01-28 23:13:25 +000054 MCContext &ctx)
Tom Stellard067c8152014-07-21 14:01:14 +000055 : MCII(mcii), MRI(mri), Ctx(ctx) { }
Tom Stellard75aadc22012-12-11 21:25:42 +000056
57 ~SIMCCodeEmitter() { }
58
Alp Tokercb402912014-01-24 17:20:08 +000059 /// \brief Encode the instruction and write it to the OS.
Craig Topper5656db42014-04-29 07:57:24 +000060 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000061 SmallVectorImpl<MCFixup> &Fixups,
Craig Topper5656db42014-04-29 07:57:24 +000062 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000063
64 /// \returns the encoding for an MCOperand.
Craig Topper5656db42014-04-29 07:57:24 +000065 uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
66 SmallVectorImpl<MCFixup> &Fixups,
67 const MCSubtargetInfo &STI) const override;
Tom Stellard01825af2014-07-21 14:01:08 +000068
69 /// \brief Use a fixup to encode the simm16 field for SOPP branch
70 /// instructions.
71 unsigned getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
72 SmallVectorImpl<MCFixup> &Fixups,
73 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000074};
75
76} // End anonymous namespace
77
78MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
79 const MCRegisterInfo &MRI,
80 const MCSubtargetInfo &STI,
81 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000082 return new SIMCCodeEmitter(MCII, MRI, Ctx);
Tom Stellard75aadc22012-12-11 21:25:42 +000083}
84
Christian Konigc756cb992013-02-16 11:28:22 +000085bool SIMCCodeEmitter::isSrcOperand(const MCInstrDesc &Desc,
86 unsigned OpNo) const {
87
88 unsigned RegClass = Desc.OpInfo[OpNo].RegClass;
89 return (AMDGPU::SSrc_32RegClassID == RegClass) ||
90 (AMDGPU::SSrc_64RegClassID == RegClass) ||
91 (AMDGPU::VSrc_32RegClassID == RegClass) ||
92 (AMDGPU::VSrc_64RegClassID == RegClass);
93}
94
95uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO) const {
96
97 IntFloatUnion Imm;
98 if (MO.isImm())
99 Imm.I = MO.getImm();
100 else if (MO.isFPImm())
101 Imm.F = MO.getFPImm();
Tom Stellard067c8152014-07-21 14:01:14 +0000102 else if (MO.isExpr())
103 return 255;
Christian Konigc756cb992013-02-16 11:28:22 +0000104 else
105 return ~0;
106
107 if (Imm.I >= 0 && Imm.I <= 64)
108 return 128 + Imm.I;
109
110 if (Imm.I >= -16 && Imm.I <= -1)
111 return 192 + abs(Imm.I);
112
113 if (Imm.F == 0.5f)
114 return 240;
115
116 if (Imm.F == -0.5f)
117 return 241;
118
119 if (Imm.F == 1.0f)
120 return 242;
121
122 if (Imm.F == -1.0f)
123 return 243;
124
125 if (Imm.F == 2.0f)
126 return 244;
127
128 if (Imm.F == -2.0f)
129 return 245;
130
131 if (Imm.F == 4.0f)
132 return 246;
133
Christian Konigd76ed542013-02-26 17:51:57 +0000134 if (Imm.F == -4.0f)
Christian Konigc756cb992013-02-16 11:28:22 +0000135 return 247;
136
137 return 255;
138}
139
Tom Stellard75aadc22012-12-11 21:25:42 +0000140void SIMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000141 SmallVectorImpl<MCFixup> &Fixups,
142 const MCSubtargetInfo &STI) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000143
David Woodhouse3fa98a62014-01-28 23:13:18 +0000144 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups, STI);
Christian Konigc756cb992013-02-16 11:28:22 +0000145 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
146 unsigned bytes = Desc.getSize();
147
Tom Stellard75aadc22012-12-11 21:25:42 +0000148 for (unsigned i = 0; i < bytes; i++) {
149 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
150 }
Christian Konigc756cb992013-02-16 11:28:22 +0000151
152 if (bytes > 4)
153 return;
154
155 // Check for additional literals in SRC0/1/2 (Op 1/2/3)
156 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
157
158 // Check if this operand should be encoded as [SV]Src
159 if (!isSrcOperand(Desc, i))
160 continue;
161
162 // Is this operand a literal immediate?
163 const MCOperand &Op = MI.getOperand(i);
164 if (getLitEncoding(Op) != 255)
165 continue;
166
167 // Yes! Encode it
168 IntFloatUnion Imm;
169 if (Op.isImm())
170 Imm.I = Op.getImm();
Tom Stellard067c8152014-07-21 14:01:14 +0000171 else if (Op.isFPImm())
Christian Konigc756cb992013-02-16 11:28:22 +0000172 Imm.F = Op.getFPImm();
Tom Stellard067c8152014-07-21 14:01:14 +0000173 else {
174 assert(Op.isExpr());
175 // This will be replaced with a fixup value.
176 Imm.I = 0;
177 }
Christian Konigc756cb992013-02-16 11:28:22 +0000178
179 for (unsigned j = 0; j < 4; j++) {
180 OS.write((uint8_t) ((Imm.I >> (8 * j)) & 0xff));
181 }
182
183 // Only one literal value allowed
184 break;
185 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000186}
187
Tom Stellard01825af2014-07-21 14:01:08 +0000188unsigned SIMCCodeEmitter::getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
189 SmallVectorImpl<MCFixup> &Fixups,
190 const MCSubtargetInfo &STI) const {
191 const MCOperand &MO = MI.getOperand(OpNo);
192
193 if (MO.isExpr()) {
194 const MCExpr *Expr = MO.getExpr();
195 MCFixupKind Kind = (MCFixupKind)AMDGPU::fixup_si_sopp_br;
196 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
197 return 0;
198 }
199
200 return getMachineOpValue(MI, MO, Fixups, STI);
201}
202
Tom Stellard75aadc22012-12-11 21:25:42 +0000203uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
204 const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000205 SmallVectorImpl<MCFixup> &Fixups,
206 const MCSubtargetInfo &STI) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000207 if (MO.isReg())
Tom Stellard1c822a82013-02-07 19:39:45 +0000208 return MRI.getEncodingValue(MO.getReg());
Christian Konigc756cb992013-02-16 11:28:22 +0000209
Tom Stellard067c8152014-07-21 14:01:14 +0000210 if (MO.isExpr()) {
211 const MCSymbolRefExpr *Expr = cast<MCSymbolRefExpr>(MO.getExpr());
212 MCFixupKind Kind;
213 const MCSymbol *Sym =
214 Ctx.GetOrCreateSymbol(StringRef(END_OF_TEXT_LABEL_NAME));
215
216 if (&Expr->getSymbol() == Sym) {
217 // Add the offset to the beginning of the constant values.
218 Kind = (MCFixupKind)AMDGPU::fixup_si_end_of_text;
219 } else {
220 // This is used for constant data stored in .rodata.
221 Kind = (MCFixupKind)AMDGPU::fixup_si_rodata;
222 }
223 Fixups.push_back(MCFixup::Create(4, Expr, Kind, MI.getLoc()));
224 }
225
Christian Konigc756cb992013-02-16 11:28:22 +0000226 // Figure out the operand number, needed for isSrcOperand check
227 unsigned OpNo = 0;
228 for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
229 if (&MO == &MI.getOperand(OpNo))
230 break;
231 }
232
233 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
234 if (isSrcOperand(Desc, OpNo)) {
235 uint32_t Enc = getLitEncoding(MO);
236 if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
237 return Enc;
238
239 } else if (MO.isImm())
240 return MO.getImm();
241
242 llvm_unreachable("Encoding of this operand type is not supported yet.");
Tom Stellard75aadc22012-12-11 21:25:42 +0000243 return 0;
244}
245