blob: 0d9f3d814092a89a21c1e2fbe87384d643086bb4 [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"
18#include "llvm/MC/MCCodeEmitter.h"
19#include "llvm/MC/MCContext.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000020#include "llvm/MC/MCFixup.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "llvm/MC/MCInst.h"
22#include "llvm/MC/MCInstrInfo.h"
23#include "llvm/MC/MCRegisterInfo.h"
24#include "llvm/MC/MCSubtargetInfo.h"
25#include "llvm/Support/raw_ostream.h"
26
Tom Stellard75aadc22012-12-11 21:25:42 +000027using namespace llvm;
28
29namespace {
Christian Konigc756cb992013-02-16 11:28:22 +000030
31/// \brief Helper type used in encoding
32typedef union {
33 int32_t I;
34 float F;
35} IntFloatUnion;
36
Tom Stellard75aadc22012-12-11 21:25:42 +000037class SIMCCodeEmitter : public AMDGPUMCCodeEmitter {
38 SIMCCodeEmitter(const SIMCCodeEmitter &); // DO NOT IMPLEMENT
39 void operator=(const SIMCCodeEmitter &); // DO NOT IMPLEMENT
40 const MCInstrInfo &MCII;
41 const MCRegisterInfo &MRI;
42 const MCSubtargetInfo &STI;
43 MCContext &Ctx;
44
Christian Konigc756cb992013-02-16 11:28:22 +000045 /// \brief Encode a sequence of registers with the correct alignment.
46 unsigned GPRAlign(const MCInst &MI, unsigned OpNo, unsigned shift) const;
47
48 /// \brief Can this operand also contain immediate values?
49 bool isSrcOperand(const MCInstrDesc &Desc, unsigned OpNo) const;
50
51 /// \brief Encode an fp or int literal
52 uint32_t getLitEncoding(const MCOperand &MO) const;
53
Tom Stellard75aadc22012-12-11 21:25:42 +000054public:
55 SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
56 const MCSubtargetInfo &sti, MCContext &ctx)
57 : MCII(mcii), MRI(mri), STI(sti), Ctx(ctx) { }
58
59 ~SIMCCodeEmitter() { }
60
61 /// \breif Encode the instruction and write it to the OS.
62 virtual void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
63 SmallVectorImpl<MCFixup> &Fixups) const;
64
65 /// \returns the encoding for an MCOperand.
66 virtual uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
67 SmallVectorImpl<MCFixup> &Fixups) const;
68
Tom Stellard75aadc22012-12-11 21:25:42 +000069 /// \brief Encoding for when 2 consecutive registers are used
70 virtual unsigned GPR2AlignEncode(const MCInst &MI, unsigned OpNo,
71 SmallVectorImpl<MCFixup> &Fixup) const;
72
73 /// \brief Encoding for when 4 consectuive registers are used
74 virtual unsigned GPR4AlignEncode(const MCInst &MI, unsigned OpNo,
75 SmallVectorImpl<MCFixup> &Fixup) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000076};
77
78} // End anonymous namespace
79
80MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
81 const MCRegisterInfo &MRI,
82 const MCSubtargetInfo &STI,
83 MCContext &Ctx) {
84 return new SIMCCodeEmitter(MCII, MRI, STI, Ctx);
85}
86
Christian Konigc756cb992013-02-16 11:28:22 +000087bool SIMCCodeEmitter::isSrcOperand(const MCInstrDesc &Desc,
88 unsigned OpNo) const {
89
90 unsigned RegClass = Desc.OpInfo[OpNo].RegClass;
91 return (AMDGPU::SSrc_32RegClassID == RegClass) ||
92 (AMDGPU::SSrc_64RegClassID == RegClass) ||
93 (AMDGPU::VSrc_32RegClassID == RegClass) ||
94 (AMDGPU::VSrc_64RegClassID == RegClass);
95}
96
97uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO) const {
98
99 IntFloatUnion Imm;
100 if (MO.isImm())
101 Imm.I = MO.getImm();
102 else if (MO.isFPImm())
103 Imm.F = MO.getFPImm();
104 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
134 if (Imm.F == 4.0f)
135 return 247;
136
137 return 255;
138}
139
Tom Stellard75aadc22012-12-11 21:25:42 +0000140void SIMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
141 SmallVectorImpl<MCFixup> &Fixups) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000142
Tom Stellard75aadc22012-12-11 21:25:42 +0000143 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups);
Christian Konigc756cb992013-02-16 11:28:22 +0000144 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
145 unsigned bytes = Desc.getSize();
146
Tom Stellard75aadc22012-12-11 21:25:42 +0000147 for (unsigned i = 0; i < bytes; i++) {
148 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
149 }
Christian Konigc756cb992013-02-16 11:28:22 +0000150
151 if (bytes > 4)
152 return;
153
154 // Check for additional literals in SRC0/1/2 (Op 1/2/3)
155 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
156
157 // Check if this operand should be encoded as [SV]Src
158 if (!isSrcOperand(Desc, i))
159 continue;
160
161 // Is this operand a literal immediate?
162 const MCOperand &Op = MI.getOperand(i);
163 if (getLitEncoding(Op) != 255)
164 continue;
165
166 // Yes! Encode it
167 IntFloatUnion Imm;
168 if (Op.isImm())
169 Imm.I = Op.getImm();
170 else
171 Imm.F = Op.getFPImm();
172
173 for (unsigned j = 0; j < 4; j++) {
174 OS.write((uint8_t) ((Imm.I >> (8 * j)) & 0xff));
175 }
176
177 // Only one literal value allowed
178 break;
179 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000180}
181
182uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
183 const MCOperand &MO,
184 SmallVectorImpl<MCFixup> &Fixups) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000185 if (MO.isReg())
Tom Stellard1c822a82013-02-07 19:39:45 +0000186 return MRI.getEncodingValue(MO.getReg());
Christian Konigc756cb992013-02-16 11:28:22 +0000187
188 if (MO.isExpr()) {
Tom Stellard9e90b582012-12-17 15:14:54 +0000189 const MCExpr *Expr = MO.getExpr();
190 MCFixupKind Kind = MCFixupKind(FK_PCRel_4);
191 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
192 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000193 }
Christian Konigc756cb992013-02-16 11:28:22 +0000194
195 // Figure out the operand number, needed for isSrcOperand check
196 unsigned OpNo = 0;
197 for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
198 if (&MO == &MI.getOperand(OpNo))
199 break;
200 }
201
202 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
203 if (isSrcOperand(Desc, OpNo)) {
204 uint32_t Enc = getLitEncoding(MO);
205 if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
206 return Enc;
207
208 } else if (MO.isImm())
209 return MO.getImm();
210
211 llvm_unreachable("Encoding of this operand type is not supported yet.");
Tom Stellard75aadc22012-12-11 21:25:42 +0000212 return 0;
213}
214
215//===----------------------------------------------------------------------===//
216// Custom Operand Encodings
217//===----------------------------------------------------------------------===//
218
219unsigned SIMCCodeEmitter::GPRAlign(const MCInst &MI, unsigned OpNo,
220 unsigned shift) const {
Tom Stellard1c822a82013-02-07 19:39:45 +0000221 unsigned regCode = MRI.getEncodingValue(MI.getOperand(OpNo).getReg());
222 return (regCode & 0xff) >> shift;
Tom Stellard75aadc22012-12-11 21:25:42 +0000223}
Christian Konigc756cb992013-02-16 11:28:22 +0000224
Tom Stellard75aadc22012-12-11 21:25:42 +0000225unsigned SIMCCodeEmitter::GPR2AlignEncode(const MCInst &MI,
226 unsigned OpNo ,
227 SmallVectorImpl<MCFixup> &Fixup) const {
228 return GPRAlign(MI, OpNo, 1);
229}
230
231unsigned SIMCCodeEmitter::GPR4AlignEncode(const MCInst &MI,
232 unsigned OpNo,
233 SmallVectorImpl<MCFixup> &Fixup) const {
234 return GPRAlign(MI, OpNo, 2);
235}