blob: 5e674d6394de9618c3c6c255003762113cdc114d [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
16#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
17#include "MCTargetDesc/AMDGPUMCCodeEmitter.h"
Tom Stellard01825af2014-07-21 14:01:08 +000018#include "MCTargetDesc/AMDGPUFixupKinds.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000019#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCContext.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000021#include "llvm/MC/MCFixup.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000022#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/MC/MCRegisterInfo.h"
25#include "llvm/MC/MCSubtargetInfo.h"
26#include "llvm/Support/raw_ostream.h"
27
Tom Stellard75aadc22012-12-11 21:25:42 +000028using namespace llvm;
29
30namespace {
Christian Konigc756cb992013-02-16 11:28:22 +000031
32/// \brief Helper type used in encoding
33typedef union {
34 int32_t I;
35 float F;
36} IntFloatUnion;
37
Tom Stellard75aadc22012-12-11 21:25:42 +000038class SIMCCodeEmitter : public AMDGPUMCCodeEmitter {
David Blaikie772d4f72013-02-18 23:11:17 +000039 SIMCCodeEmitter(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
40 void operator=(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
Tom Stellard75aadc22012-12-11 21:25:42 +000041 const MCInstrInfo &MCII;
42 const MCRegisterInfo &MRI;
Tom Stellard75aadc22012-12-11 21:25:42 +000043
Christian Konigc756cb992013-02-16 11:28:22 +000044 /// \brief Can this operand also contain immediate values?
45 bool isSrcOperand(const MCInstrDesc &Desc, unsigned OpNo) const;
46
47 /// \brief Encode an fp or int literal
48 uint32_t getLitEncoding(const MCOperand &MO) const;
49
Tom Stellard75aadc22012-12-11 21:25:42 +000050public:
51 SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
David Woodhoused2cca112014-01-28 23:13:25 +000052 MCContext &ctx)
NAKAMURA Takumi32341782013-03-26 19:42:48 +000053 : MCII(mcii), MRI(mri) { }
Tom Stellard75aadc22012-12-11 21:25:42 +000054
55 ~SIMCCodeEmitter() { }
56
Alp Tokercb402912014-01-24 17:20:08 +000057 /// \brief Encode the instruction and write it to the OS.
Craig Topper5656db42014-04-29 07:57:24 +000058 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000059 SmallVectorImpl<MCFixup> &Fixups,
Craig Topper5656db42014-04-29 07:57:24 +000060 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000061
62 /// \returns the encoding for an MCOperand.
Craig Topper5656db42014-04-29 07:57:24 +000063 uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
64 SmallVectorImpl<MCFixup> &Fixups,
65 const MCSubtargetInfo &STI) const override;
Tom Stellard01825af2014-07-21 14:01:08 +000066
67 /// \brief Use a fixup to encode the simm16 field for SOPP branch
68 /// instructions.
69 unsigned getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
70 SmallVectorImpl<MCFixup> &Fixups,
71 const MCSubtargetInfo &STI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000072};
73
74} // End anonymous namespace
75
76MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
77 const MCRegisterInfo &MRI,
78 const MCSubtargetInfo &STI,
79 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000080 return new SIMCCodeEmitter(MCII, MRI, Ctx);
Tom Stellard75aadc22012-12-11 21:25:42 +000081}
82
Christian Konigc756cb992013-02-16 11:28:22 +000083bool SIMCCodeEmitter::isSrcOperand(const MCInstrDesc &Desc,
84 unsigned OpNo) const {
85
86 unsigned RegClass = Desc.OpInfo[OpNo].RegClass;
87 return (AMDGPU::SSrc_32RegClassID == RegClass) ||
88 (AMDGPU::SSrc_64RegClassID == RegClass) ||
89 (AMDGPU::VSrc_32RegClassID == RegClass) ||
90 (AMDGPU::VSrc_64RegClassID == RegClass);
91}
92
93uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO) const {
94
95 IntFloatUnion Imm;
96 if (MO.isImm())
97 Imm.I = MO.getImm();
98 else if (MO.isFPImm())
99 Imm.F = MO.getFPImm();
100 else
101 return ~0;
102
103 if (Imm.I >= 0 && Imm.I <= 64)
104 return 128 + Imm.I;
105
106 if (Imm.I >= -16 && Imm.I <= -1)
107 return 192 + abs(Imm.I);
108
109 if (Imm.F == 0.5f)
110 return 240;
111
112 if (Imm.F == -0.5f)
113 return 241;
114
115 if (Imm.F == 1.0f)
116 return 242;
117
118 if (Imm.F == -1.0f)
119 return 243;
120
121 if (Imm.F == 2.0f)
122 return 244;
123
124 if (Imm.F == -2.0f)
125 return 245;
126
127 if (Imm.F == 4.0f)
128 return 246;
129
Christian Konigd76ed542013-02-26 17:51:57 +0000130 if (Imm.F == -4.0f)
Christian Konigc756cb992013-02-16 11:28:22 +0000131 return 247;
132
133 return 255;
134}
135
Tom Stellard75aadc22012-12-11 21:25:42 +0000136void SIMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000137 SmallVectorImpl<MCFixup> &Fixups,
138 const MCSubtargetInfo &STI) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000139
David Woodhouse3fa98a62014-01-28 23:13:18 +0000140 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups, STI);
Christian Konigc756cb992013-02-16 11:28:22 +0000141 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
142 unsigned bytes = Desc.getSize();
143
Tom Stellard75aadc22012-12-11 21:25:42 +0000144 for (unsigned i = 0; i < bytes; i++) {
145 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
146 }
Christian Konigc756cb992013-02-16 11:28:22 +0000147
148 if (bytes > 4)
149 return;
150
151 // Check for additional literals in SRC0/1/2 (Op 1/2/3)
152 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
153
154 // Check if this operand should be encoded as [SV]Src
155 if (!isSrcOperand(Desc, i))
156 continue;
157
158 // Is this operand a literal immediate?
159 const MCOperand &Op = MI.getOperand(i);
160 if (getLitEncoding(Op) != 255)
161 continue;
162
163 // Yes! Encode it
164 IntFloatUnion Imm;
165 if (Op.isImm())
166 Imm.I = Op.getImm();
167 else
168 Imm.F = Op.getFPImm();
169
170 for (unsigned j = 0; j < 4; j++) {
171 OS.write((uint8_t) ((Imm.I >> (8 * j)) & 0xff));
172 }
173
174 // Only one literal value allowed
175 break;
176 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000177}
178
Tom Stellard01825af2014-07-21 14:01:08 +0000179unsigned SIMCCodeEmitter::getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
180 SmallVectorImpl<MCFixup> &Fixups,
181 const MCSubtargetInfo &STI) const {
182 const MCOperand &MO = MI.getOperand(OpNo);
183
184 if (MO.isExpr()) {
185 const MCExpr *Expr = MO.getExpr();
186 MCFixupKind Kind = (MCFixupKind)AMDGPU::fixup_si_sopp_br;
187 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
188 return 0;
189 }
190
191 return getMachineOpValue(MI, MO, Fixups, STI);
192}
193
Tom Stellard75aadc22012-12-11 21:25:42 +0000194uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
195 const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000196 SmallVectorImpl<MCFixup> &Fixups,
197 const MCSubtargetInfo &STI) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000198 if (MO.isReg())
Tom Stellard1c822a82013-02-07 19:39:45 +0000199 return MRI.getEncodingValue(MO.getReg());
Christian Konigc756cb992013-02-16 11:28:22 +0000200
Christian Konigc756cb992013-02-16 11:28:22 +0000201 // Figure out the operand number, needed for isSrcOperand check
202 unsigned OpNo = 0;
203 for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
204 if (&MO == &MI.getOperand(OpNo))
205 break;
206 }
207
208 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
209 if (isSrcOperand(Desc, OpNo)) {
210 uint32_t Enc = getLitEncoding(MO);
211 if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
212 return Enc;
213
214 } else if (MO.isImm())
215 return MO.getImm();
216
217 llvm_unreachable("Encoding of this operand type is not supported yet.");
Tom Stellard75aadc22012-12-11 21:25:42 +0000218 return 0;
219}
220