blob: 53dad5a08ca5735be72e2f4831190c4f657c9595 [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 {
David Blaikie772d4f72013-02-18 23:11:17 +000038 SIMCCodeEmitter(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
39 void operator=(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
Tom Stellard75aadc22012-12-11 21:25:42 +000040 const MCInstrInfo &MCII;
41 const MCRegisterInfo &MRI;
Tom Stellard75aadc22012-12-11 21:25:42 +000042
Christian Konigc756cb992013-02-16 11:28:22 +000043 /// \brief Can this operand also contain immediate values?
44 bool isSrcOperand(const MCInstrDesc &Desc, unsigned OpNo) const;
45
46 /// \brief Encode an fp or int literal
47 uint32_t getLitEncoding(const MCOperand &MO) const;
48
Tom Stellard75aadc22012-12-11 21:25:42 +000049public:
50 SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
51 const MCSubtargetInfo &sti, MCContext &ctx)
NAKAMURA Takumi32341782013-03-26 19:42:48 +000052 : MCII(mcii), MRI(mri) { }
Tom Stellard75aadc22012-12-11 21:25:42 +000053
54 ~SIMCCodeEmitter() { }
55
Alp Tokercb402912014-01-24 17:20:08 +000056 /// \brief Encode the instruction and write it to the OS.
Tom Stellard75aadc22012-12-11 21:25:42 +000057 virtual void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000058 SmallVectorImpl<MCFixup> &Fixups,
59 const MCSubtargetInfo &STI) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000060
61 /// \returns the encoding for an MCOperand.
62 virtual uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
63 SmallVectorImpl<MCFixup> &Fixups) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000064};
65
66} // End anonymous namespace
67
68MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
69 const MCRegisterInfo &MRI,
70 const MCSubtargetInfo &STI,
71 MCContext &Ctx) {
72 return new SIMCCodeEmitter(MCII, MRI, STI, Ctx);
73}
74
Christian Konigc756cb992013-02-16 11:28:22 +000075bool SIMCCodeEmitter::isSrcOperand(const MCInstrDesc &Desc,
76 unsigned OpNo) const {
77
78 unsigned RegClass = Desc.OpInfo[OpNo].RegClass;
79 return (AMDGPU::SSrc_32RegClassID == RegClass) ||
80 (AMDGPU::SSrc_64RegClassID == RegClass) ||
81 (AMDGPU::VSrc_32RegClassID == RegClass) ||
82 (AMDGPU::VSrc_64RegClassID == RegClass);
83}
84
85uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO) const {
86
87 IntFloatUnion Imm;
88 if (MO.isImm())
89 Imm.I = MO.getImm();
90 else if (MO.isFPImm())
91 Imm.F = MO.getFPImm();
92 else
93 return ~0;
94
95 if (Imm.I >= 0 && Imm.I <= 64)
96 return 128 + Imm.I;
97
98 if (Imm.I >= -16 && Imm.I <= -1)
99 return 192 + abs(Imm.I);
100
101 if (Imm.F == 0.5f)
102 return 240;
103
104 if (Imm.F == -0.5f)
105 return 241;
106
107 if (Imm.F == 1.0f)
108 return 242;
109
110 if (Imm.F == -1.0f)
111 return 243;
112
113 if (Imm.F == 2.0f)
114 return 244;
115
116 if (Imm.F == -2.0f)
117 return 245;
118
119 if (Imm.F == 4.0f)
120 return 246;
121
Christian Konigd76ed542013-02-26 17:51:57 +0000122 if (Imm.F == -4.0f)
Christian Konigc756cb992013-02-16 11:28:22 +0000123 return 247;
124
125 return 255;
126}
127
Tom Stellard75aadc22012-12-11 21:25:42 +0000128void SIMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000129 SmallVectorImpl<MCFixup> &Fixups,
130 const MCSubtargetInfo &STI) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000131
Tom Stellard75aadc22012-12-11 21:25:42 +0000132 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups);
Christian Konigc756cb992013-02-16 11:28:22 +0000133 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
134 unsigned bytes = Desc.getSize();
135
Tom Stellard75aadc22012-12-11 21:25:42 +0000136 for (unsigned i = 0; i < bytes; i++) {
137 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
138 }
Christian Konigc756cb992013-02-16 11:28:22 +0000139
140 if (bytes > 4)
141 return;
142
143 // Check for additional literals in SRC0/1/2 (Op 1/2/3)
144 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
145
146 // Check if this operand should be encoded as [SV]Src
147 if (!isSrcOperand(Desc, i))
148 continue;
149
150 // Is this operand a literal immediate?
151 const MCOperand &Op = MI.getOperand(i);
152 if (getLitEncoding(Op) != 255)
153 continue;
154
155 // Yes! Encode it
156 IntFloatUnion Imm;
157 if (Op.isImm())
158 Imm.I = Op.getImm();
159 else
160 Imm.F = Op.getFPImm();
161
162 for (unsigned j = 0; j < 4; j++) {
163 OS.write((uint8_t) ((Imm.I >> (8 * j)) & 0xff));
164 }
165
166 // Only one literal value allowed
167 break;
168 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000169}
170
171uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
172 const MCOperand &MO,
173 SmallVectorImpl<MCFixup> &Fixups) const {
Christian Konigc756cb992013-02-16 11:28:22 +0000174 if (MO.isReg())
Tom Stellard1c822a82013-02-07 19:39:45 +0000175 return MRI.getEncodingValue(MO.getReg());
Christian Konigc756cb992013-02-16 11:28:22 +0000176
177 if (MO.isExpr()) {
Tom Stellard9e90b582012-12-17 15:14:54 +0000178 const MCExpr *Expr = MO.getExpr();
179 MCFixupKind Kind = MCFixupKind(FK_PCRel_4);
180 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
181 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000182 }
Christian Konigc756cb992013-02-16 11:28:22 +0000183
184 // Figure out the operand number, needed for isSrcOperand check
185 unsigned OpNo = 0;
186 for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
187 if (&MO == &MI.getOperand(OpNo))
188 break;
189 }
190
191 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
192 if (isSrcOperand(Desc, OpNo)) {
193 uint32_t Enc = getLitEncoding(MO);
194 if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
195 return Enc;
196
197 } else if (MO.isImm())
198 return MO.getImm();
199
200 llvm_unreachable("Encoding of this operand type is not supported yet.");
Tom Stellard75aadc22012-12-11 21:25:42 +0000201 return 0;
202}
203