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 "SIDefines.h"
18 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
19 #include "MCTargetDesc/AMDGPUMCCodeEmitter.h"
20 #include "MCTargetDesc/AMDGPUFixupKinds.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) 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 uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO) const {
99 else if (MO.isFPImm())
100 Imm.F = MO.getFPImm();
101 else if (MO.isExpr())
106 if (Imm.I >= 0 && Imm.I <= 64)
109 if (Imm.I >= -16 && Imm.I <= -1)
110 return 192 + abs(Imm.I);
139 void SIMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
140 SmallVectorImpl<MCFixup> &Fixups,
141 const MCSubtargetInfo &STI) const {
143 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups, STI);
144 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
145 unsigned bytes = Desc.getSize();
147 for (unsigned i = 0; i < bytes; i++) {
148 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
154 // Check for additional literals in SRC0/1/2 (Op 1/2/3)
155 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
157 // Check if this operand should be encoded as [SV]Src
158 if (!isSrcOperand(Desc, i))
161 // Is this operand a literal immediate?
162 const MCOperand &Op = MI.getOperand(i);
163 if (getLitEncoding(Op) != 255)
170 else if (Op.isFPImm())
171 Imm.F = Op.getFPImm();
174 // This will be replaced with a fixup value.
178 for (unsigned j = 0; j < 4; j++) {
179 OS.write((uint8_t) ((Imm.I >> (8 * j)) & 0xff));
182 // Only one literal value allowed
187 unsigned SIMCCodeEmitter::getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
188 SmallVectorImpl<MCFixup> &Fixups,
189 const MCSubtargetInfo &STI) const {
190 const MCOperand &MO = MI.getOperand(OpNo);
193 const MCExpr *Expr = MO.getExpr();
194 MCFixupKind Kind = (MCFixupKind)AMDGPU::fixup_si_sopp_br;
195 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
199 return getMachineOpValue(MI, MO, Fixups, STI);
202 uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
204 SmallVectorImpl<MCFixup> &Fixups,
205 const MCSubtargetInfo &STI) const {
207 return MRI.getEncodingValue(MO.getReg());
210 const MCSymbolRefExpr *Expr = cast<MCSymbolRefExpr>(MO.getExpr());
212 const MCSymbol *Sym =
213 Ctx.GetOrCreateSymbol(StringRef(END_OF_TEXT_LABEL_NAME));
215 if (&Expr->getSymbol() == Sym) {
216 // Add the offset to the beginning of the constant values.
217 Kind = (MCFixupKind)AMDGPU::fixup_si_end_of_text;
219 // This is used for constant data stored in .rodata.
220 Kind = (MCFixupKind)AMDGPU::fixup_si_rodata;
222 Fixups.push_back(MCFixup::Create(4, Expr, Kind, MI.getLoc()));
225 // Figure out the operand number, needed for isSrcOperand check
227 for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
228 if (&MO == &MI.getOperand(OpNo))
232 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
233 if (isSrcOperand(Desc, OpNo)) {
234 uint32_t Enc = getLitEncoding(MO);
235 if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
238 } else if (MO.isImm())
241 llvm_unreachable("Encoding of this operand type is not supported yet.");