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/AMDGPUMCTargetDesc.h"
18 #include "MCTargetDesc/AMDGPUMCCodeEmitter.h"
19 #include "MCTargetDesc/AMDGPUFixupKinds.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCFixup.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCRegisterInfo.h"
26 #include "llvm/MC/MCSubtargetInfo.h"
27 #include "llvm/Support/raw_ostream.h"
33 /// \brief Helper type used in encoding
39 class SIMCCodeEmitter : public AMDGPUMCCodeEmitter {
40 SIMCCodeEmitter(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
41 void operator=(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
42 const MCInstrInfo &MCII;
43 const MCRegisterInfo &MRI;
46 /// \brief Can this operand also contain immediate values?
47 bool isSrcOperand(const MCInstrDesc &Desc, unsigned OpNo) const;
49 /// \brief Encode an fp or int literal
50 uint32_t getLitEncoding(const MCOperand &MO) const;
53 SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
55 : MCII(mcii), MRI(mri), Ctx(ctx) { }
57 ~SIMCCodeEmitter() { }
59 /// \brief Encode the instruction and write it to the OS.
60 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
61 SmallVectorImpl<MCFixup> &Fixups,
62 const MCSubtargetInfo &STI) const override;
64 /// \returns the encoding for an MCOperand.
65 uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
66 SmallVectorImpl<MCFixup> &Fixups,
67 const MCSubtargetInfo &STI) const override;
69 /// \brief Use a fixup to encode the simm16 field for SOPP branch
71 unsigned getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
72 SmallVectorImpl<MCFixup> &Fixups,
73 const MCSubtargetInfo &STI) const override;
76 } // End anonymous namespace
78 MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
79 const MCRegisterInfo &MRI,
80 const MCSubtargetInfo &STI,
82 return new SIMCCodeEmitter(MCII, MRI, Ctx);
85 bool SIMCCodeEmitter::isSrcOperand(const MCInstrDesc &Desc,
86 unsigned OpNo) const {
88 unsigned RegClass = Desc.OpInfo[OpNo].RegClass;
89 return (AMDGPU::SSrc_32RegClassID == RegClass) ||
90 (AMDGPU::SSrc_64RegClassID == RegClass) ||
91 (AMDGPU::VSrc_32RegClassID == RegClass) ||
92 (AMDGPU::VSrc_64RegClassID == RegClass);
95 uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO) const {
100 else if (MO.isFPImm())
101 Imm.F = MO.getFPImm();
102 else if (MO.isExpr())
107 if (Imm.I >= 0 && Imm.I <= 64)
110 if (Imm.I >= -16 && Imm.I <= -1)
111 return 192 + abs(Imm.I);
140 void SIMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
141 SmallVectorImpl<MCFixup> &Fixups,
142 const MCSubtargetInfo &STI) const {
144 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups, STI);
145 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
146 unsigned bytes = Desc.getSize();
148 for (unsigned i = 0; i < bytes; i++) {
149 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
155 // Check for additional literals in SRC0/1/2 (Op 1/2/3)
156 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
158 // Check if this operand should be encoded as [SV]Src
159 if (!isSrcOperand(Desc, i))
162 // Is this operand a literal immediate?
163 const MCOperand &Op = MI.getOperand(i);
164 if (getLitEncoding(Op) != 255)
171 else if (Op.isFPImm())
172 Imm.F = Op.getFPImm();
175 // This will be replaced with a fixup value.
179 for (unsigned j = 0; j < 4; j++) {
180 OS.write((uint8_t) ((Imm.I >> (8 * j)) & 0xff));
183 // Only one literal value allowed
188 unsigned SIMCCodeEmitter::getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
189 SmallVectorImpl<MCFixup> &Fixups,
190 const MCSubtargetInfo &STI) const {
191 const MCOperand &MO = MI.getOperand(OpNo);
194 const MCExpr *Expr = MO.getExpr();
195 MCFixupKind Kind = (MCFixupKind)AMDGPU::fixup_si_sopp_br;
196 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
200 return getMachineOpValue(MI, MO, Fixups, STI);
203 uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
205 SmallVectorImpl<MCFixup> &Fixups,
206 const MCSubtargetInfo &STI) const {
208 return MRI.getEncodingValue(MO.getReg());
211 const MCSymbolRefExpr *Expr = cast<MCSymbolRefExpr>(MO.getExpr());
213 const MCSymbol *Sym =
214 Ctx.GetOrCreateSymbol(StringRef(END_OF_TEXT_LABEL_NAME));
216 if (&Expr->getSymbol() == Sym) {
217 // Add the offset to the beginning of the constant values.
218 Kind = (MCFixupKind)AMDGPU::fixup_si_end_of_text;
220 // This is used for constant data stored in .rodata.
221 Kind = (MCFixupKind)AMDGPU::fixup_si_rodata;
223 Fixups.push_back(MCFixup::Create(4, Expr, Kind, MI.getLoc()));
226 // Figure out the operand number, needed for isSrcOperand check
228 for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
229 if (&MO == &MI.getOperand(OpNo))
233 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
234 if (isSrcOperand(Desc, OpNo)) {
235 uint32_t Enc = getLitEncoding(MO);
236 if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
239 } else if (MO.isImm())
242 llvm_unreachable("Encoding of this operand type is not supported yet.");