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 //===----------------------------------------------------------------------===//
16 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
17 #include "MCTargetDesc/AMDGPUMCCodeEmitter.h"
18 #include "llvm/MC/MCCodeEmitter.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCFixup.h"
21 #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"
31 /// \brief Helper type used in encoding
37 class SIMCCodeEmitter : public AMDGPUMCCodeEmitter {
38 SIMCCodeEmitter(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
39 void operator=(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
40 const MCInstrInfo &MCII;
41 const MCRegisterInfo &MRI;
43 /// \brief Can this operand also contain immediate values?
44 bool isSrcOperand(const MCInstrDesc &Desc, unsigned OpNo) const;
46 /// \brief Encode an fp or int literal
47 uint32_t getLitEncoding(const MCOperand &MO) const;
50 SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
52 : MCII(mcii), MRI(mri) { }
54 ~SIMCCodeEmitter() { }
56 /// \brief Encode the instruction and write it to the OS.
57 virtual void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
58 SmallVectorImpl<MCFixup> &Fixups,
59 const MCSubtargetInfo &STI) const;
61 /// \returns the encoding for an MCOperand.
62 virtual uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
63 SmallVectorImpl<MCFixup> &Fixups,
64 const MCSubtargetInfo &STI) const;
67 } // End anonymous namespace
69 MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
70 const MCRegisterInfo &MRI,
71 const MCSubtargetInfo &STI,
73 return new SIMCCodeEmitter(MCII, MRI, Ctx);
76 bool SIMCCodeEmitter::isSrcOperand(const MCInstrDesc &Desc,
77 unsigned OpNo) const {
79 unsigned RegClass = Desc.OpInfo[OpNo].RegClass;
80 return (AMDGPU::SSrc_32RegClassID == RegClass) ||
81 (AMDGPU::SSrc_64RegClassID == RegClass) ||
82 (AMDGPU::VSrc_32RegClassID == RegClass) ||
83 (AMDGPU::VSrc_64RegClassID == RegClass);
86 uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO) const {
91 else if (MO.isFPImm())
92 Imm.F = MO.getFPImm();
96 if (Imm.I >= 0 && Imm.I <= 64)
99 if (Imm.I >= -16 && Imm.I <= -1)
100 return 192 + abs(Imm.I);
129 void SIMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
130 SmallVectorImpl<MCFixup> &Fixups,
131 const MCSubtargetInfo &STI) const {
133 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups, STI);
134 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
135 unsigned bytes = Desc.getSize();
137 for (unsigned i = 0; i < bytes; i++) {
138 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
144 // Check for additional literals in SRC0/1/2 (Op 1/2/3)
145 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
147 // Check if this operand should be encoded as [SV]Src
148 if (!isSrcOperand(Desc, i))
151 // Is this operand a literal immediate?
152 const MCOperand &Op = MI.getOperand(i);
153 if (getLitEncoding(Op) != 255)
161 Imm.F = Op.getFPImm();
163 for (unsigned j = 0; j < 4; j++) {
164 OS.write((uint8_t) ((Imm.I >> (8 * j)) & 0xff));
167 // Only one literal value allowed
172 uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
174 SmallVectorImpl<MCFixup> &Fixups,
175 const MCSubtargetInfo &STI) const {
177 return MRI.getEncodingValue(MO.getReg());
180 const MCExpr *Expr = MO.getExpr();
181 MCFixupKind Kind = MCFixupKind(FK_PCRel_4);
182 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
186 // Figure out the operand number, needed for isSrcOperand check
188 for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
189 if (&MO == &MI.getOperand(OpNo))
193 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
194 if (isSrcOperand(Desc, OpNo)) {
195 uint32_t Enc = getLitEncoding(MO);
196 if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
199 } else if (MO.isImm())
202 llvm_unreachable("Encoding of this operand type is not supported yet.");