1 //===-- SIMCCodeEmitter.cpp - SI Code Emitter -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief The SI code emitter produces machine code that can be executed
12 /// directly on the GPU device.
14 //===----------------------------------------------------------------------===//
17 #include "MCTargetDesc/AMDGPUFixupKinds.h"
18 #include "MCTargetDesc/AMDGPUMCCodeEmitter.h"
19 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
20 #include "SIDefines.h"
21 #include "llvm/MC/MCCodeEmitter.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCFixup.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCInstrInfo.h"
26 #include "llvm/MC/MCRegisterInfo.h"
27 #include "llvm/MC/MCSubtargetInfo.h"
28 #include "llvm/Support/raw_ostream.h"
34 /// \brief Helper type used in encoding
40 class SIMCCodeEmitter : public AMDGPUMCCodeEmitter {
41 SIMCCodeEmitter(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
42 void operator=(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
43 const MCInstrInfo &MCII;
44 const MCRegisterInfo &MRI;
47 /// \brief Can this operand also contain immediate values?
48 bool isSrcOperand(const MCInstrDesc &Desc, unsigned OpNo) const;
50 /// \brief Encode an fp or int literal
51 uint32_t getLitEncoding(const MCOperand &MO, unsigned OpSize) const;
54 SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
56 : MCII(mcii), MRI(mri), Ctx(ctx) { }
58 ~SIMCCodeEmitter() { }
60 /// \brief Encode the instruction and write it to the OS.
61 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
62 SmallVectorImpl<MCFixup> &Fixups,
63 const MCSubtargetInfo &STI) const override;
65 /// \returns the encoding for an MCOperand.
66 uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
67 SmallVectorImpl<MCFixup> &Fixups,
68 const MCSubtargetInfo &STI) const override;
70 /// \brief Use a fixup to encode the simm16 field for SOPP branch
72 unsigned getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
73 SmallVectorImpl<MCFixup> &Fixups,
74 const MCSubtargetInfo &STI) const override;
77 } // End anonymous namespace
79 MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
80 const MCRegisterInfo &MRI,
81 const MCSubtargetInfo &STI,
83 return new SIMCCodeEmitter(MCII, MRI, Ctx);
86 bool SIMCCodeEmitter::isSrcOperand(const MCInstrDesc &Desc,
87 unsigned OpNo) const {
88 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
90 return OpType == AMDGPU::OPERAND_REG_IMM32 ||
91 OpType == AMDGPU::OPERAND_REG_INLINE_C;
94 // Returns the encoding value to use if the given integer is an integer inline
95 // immediate value, or 0 if it is not.
96 template <typename IntTy>
97 static uint32_t getIntInlineImmEncoding(IntTy Imm) {
98 if (Imm >= 0 && Imm <= 64)
101 if (Imm >= -16 && Imm <= -1)
102 return 192 + std::abs(Imm);
107 static uint32_t getLit32Encoding(uint32_t Val) {
108 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int32_t>(Val));
112 if (Val == FloatToBits(0.5f))
115 if (Val == FloatToBits(-0.5f))
118 if (Val == FloatToBits(1.0f))
121 if (Val == FloatToBits(-1.0f))
124 if (Val == FloatToBits(2.0f))
127 if (Val == FloatToBits(-2.0f))
130 if (Val == FloatToBits(4.0f))
133 if (Val == FloatToBits(-4.0f))
139 static uint32_t getLit64Encoding(uint64_t Val) {
140 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int64_t>(Val));
144 if (Val == DoubleToBits(0.5))
147 if (Val == DoubleToBits(-0.5))
150 if (Val == DoubleToBits(1.0))
153 if (Val == DoubleToBits(-1.0))
156 if (Val == DoubleToBits(2.0))
159 if (Val == DoubleToBits(-2.0))
162 if (Val == DoubleToBits(4.0))
165 if (Val == DoubleToBits(-4.0))
171 uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO,
172 unsigned OpSize) const {
176 assert(!MO.isFPImm());
182 return getLit32Encoding(static_cast<uint32_t>(MO.getImm()));
186 return getLit64Encoding(static_cast<uint64_t>(MO.getImm()));
189 void SIMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
190 SmallVectorImpl<MCFixup> &Fixups,
191 const MCSubtargetInfo &STI) const {
193 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups, STI);
194 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
195 unsigned bytes = Desc.getSize();
197 for (unsigned i = 0; i < bytes; i++) {
198 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
204 // Check for additional literals in SRC0/1/2 (Op 1/2/3)
205 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
207 // Check if this operand should be encoded as [SV]Src
208 if (!isSrcOperand(Desc, i))
211 int RCID = Desc.OpInfo[i].RegClass;
212 const MCRegisterClass &RC = MRI.getRegClass(RCID);
214 // Is this operand a literal immediate?
215 const MCOperand &Op = MI.getOperand(i);
216 if (getLitEncoding(Op, RC.getSize()) != 255)
223 else if (Op.isFPImm())
224 Imm.F = Op.getFPImm();
227 // This will be replaced with a fixup value.
231 for (unsigned j = 0; j < 4; j++) {
232 OS.write((uint8_t) ((Imm.I >> (8 * j)) & 0xff));
235 // Only one literal value allowed
240 unsigned SIMCCodeEmitter::getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
241 SmallVectorImpl<MCFixup> &Fixups,
242 const MCSubtargetInfo &STI) const {
243 const MCOperand &MO = MI.getOperand(OpNo);
246 const MCExpr *Expr = MO.getExpr();
247 MCFixupKind Kind = (MCFixupKind)AMDGPU::fixup_si_sopp_br;
248 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
252 return getMachineOpValue(MI, MO, Fixups, STI);
255 uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
257 SmallVectorImpl<MCFixup> &Fixups,
258 const MCSubtargetInfo &STI) const {
260 return MRI.getEncodingValue(MO.getReg());
263 const MCSymbolRefExpr *Expr = cast<MCSymbolRefExpr>(MO.getExpr());
265 const MCSymbol *Sym =
266 Ctx.GetOrCreateSymbol(StringRef(END_OF_TEXT_LABEL_NAME));
268 if (&Expr->getSymbol() == Sym) {
269 // Add the offset to the beginning of the constant values.
270 Kind = (MCFixupKind)AMDGPU::fixup_si_end_of_text;
272 // This is used for constant data stored in .rodata.
273 Kind = (MCFixupKind)AMDGPU::fixup_si_rodata;
275 Fixups.push_back(MCFixup::Create(4, Expr, Kind, MI.getLoc()));
278 // Figure out the operand number, needed for isSrcOperand check
280 for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
281 if (&MO == &MI.getOperand(OpNo))
285 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
286 if (isSrcOperand(Desc, OpNo)) {
287 int RCID = Desc.OpInfo[OpNo].RegClass;
288 const MCRegisterClass &RC = MRI.getRegClass(RCID);
290 uint32_t Enc = getLitEncoding(MO, RC.getSize());
291 if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
294 } else if (MO.isImm())
297 llvm_unreachable("Encoding of this operand type is not supported yet.");