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 class SIMCCodeEmitter : public AMDGPUMCCodeEmitter {
35 SIMCCodeEmitter(const SIMCCodeEmitter &) = delete;
36 void operator=(const SIMCCodeEmitter &) = delete;
37 const MCInstrInfo &MCII;
38 const MCRegisterInfo &MRI;
41 /// \brief Can this operand also contain immediate values?
42 bool isSrcOperand(const MCInstrDesc &Desc, unsigned OpNo) const;
44 /// \brief Encode an fp or int literal
45 uint32_t getLitEncoding(const MCOperand &MO, unsigned OpSize) const;
48 SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
50 : MCII(mcii), MRI(mri), Ctx(ctx) { }
52 ~SIMCCodeEmitter() override {}
54 /// \brief Encode the instruction and write it to the OS.
55 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
56 SmallVectorImpl<MCFixup> &Fixups,
57 const MCSubtargetInfo &STI) const override;
59 /// \returns the encoding for an MCOperand.
60 uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
61 SmallVectorImpl<MCFixup> &Fixups,
62 const MCSubtargetInfo &STI) const override;
64 /// \brief Use a fixup to encode the simm16 field for SOPP branch
66 unsigned getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
67 SmallVectorImpl<MCFixup> &Fixups,
68 const MCSubtargetInfo &STI) const override;
71 } // End anonymous namespace
73 MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
74 const MCRegisterInfo &MRI,
76 return new SIMCCodeEmitter(MCII, MRI, Ctx);
79 bool SIMCCodeEmitter::isSrcOperand(const MCInstrDesc &Desc,
80 unsigned OpNo) const {
81 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
83 return OpType == AMDGPU::OPERAND_REG_IMM32 ||
84 OpType == AMDGPU::OPERAND_REG_INLINE_C;
87 // Returns the encoding value to use if the given integer is an integer inline
88 // immediate value, or 0 if it is not.
89 template <typename IntTy>
90 static uint32_t getIntInlineImmEncoding(IntTy Imm) {
91 if (Imm >= 0 && Imm <= 64)
94 if (Imm >= -16 && Imm <= -1)
95 return 192 + std::abs(Imm);
100 static uint32_t getLit32Encoding(uint32_t Val) {
101 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int32_t>(Val));
105 if (Val == FloatToBits(0.5f))
108 if (Val == FloatToBits(-0.5f))
111 if (Val == FloatToBits(1.0f))
114 if (Val == FloatToBits(-1.0f))
117 if (Val == FloatToBits(2.0f))
120 if (Val == FloatToBits(-2.0f))
123 if (Val == FloatToBits(4.0f))
126 if (Val == FloatToBits(-4.0f))
132 static uint32_t getLit64Encoding(uint64_t Val) {
133 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int64_t>(Val));
137 if (Val == DoubleToBits(0.5))
140 if (Val == DoubleToBits(-0.5))
143 if (Val == DoubleToBits(1.0))
146 if (Val == DoubleToBits(-1.0))
149 if (Val == DoubleToBits(2.0))
152 if (Val == DoubleToBits(-2.0))
155 if (Val == DoubleToBits(4.0))
158 if (Val == DoubleToBits(-4.0))
164 uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO,
165 unsigned OpSize) const {
169 assert(!MO.isFPImm());
175 return getLit32Encoding(static_cast<uint32_t>(MO.getImm()));
179 return getLit64Encoding(static_cast<uint64_t>(MO.getImm()));
182 void SIMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
183 SmallVectorImpl<MCFixup> &Fixups,
184 const MCSubtargetInfo &STI) const {
186 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups, STI);
187 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
188 unsigned bytes = Desc.getSize();
190 for (unsigned i = 0; i < bytes; i++) {
191 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
197 // Check for additional literals in SRC0/1/2 (Op 1/2/3)
198 for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
200 // Check if this operand should be encoded as [SV]Src
201 if (!isSrcOperand(Desc, i))
204 int RCID = Desc.OpInfo[i].RegClass;
205 const MCRegisterClass &RC = MRI.getRegClass(RCID);
207 // Is this operand a literal immediate?
208 const MCOperand &Op = MI.getOperand(i);
209 if (getLitEncoding(Op, RC.getSize()) != 255)
217 else if (!Op.isExpr()) // Exprs will be replaced with a fixup value.
218 llvm_unreachable("Must be immediate or expr");
220 for (unsigned j = 0; j < 4; j++) {
221 OS.write((uint8_t) ((Imm >> (8 * j)) & 0xff));
224 // Only one literal value allowed
229 unsigned SIMCCodeEmitter::getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
230 SmallVectorImpl<MCFixup> &Fixups,
231 const MCSubtargetInfo &STI) const {
232 const MCOperand &MO = MI.getOperand(OpNo);
235 const MCExpr *Expr = MO.getExpr();
236 MCFixupKind Kind = (MCFixupKind)AMDGPU::fixup_si_sopp_br;
237 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
241 return getMachineOpValue(MI, MO, Fixups, STI);
244 uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
246 SmallVectorImpl<MCFixup> &Fixups,
247 const MCSubtargetInfo &STI) const {
249 return MRI.getEncodingValue(MO.getReg());
252 const MCSymbolRefExpr *Expr = cast<MCSymbolRefExpr>(MO.getExpr());
253 MCFixupKind Kind = (MCFixupKind)AMDGPU::fixup_si_rodata;
254 Fixups.push_back(MCFixup::create(4, Expr, Kind, MI.getLoc()));
257 // Figure out the operand number, needed for isSrcOperand check
259 for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
260 if (&MO == &MI.getOperand(OpNo))
264 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
265 if (isSrcOperand(Desc, OpNo)) {
266 int RCID = Desc.OpInfo[OpNo].RegClass;
267 const MCRegisterClass &RC = MRI.getRegClass(RCID);
269 uint32_t Enc = getLitEncoding(MO, RC.getSize());
270 if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
273 } else if (MO.isImm())
276 llvm_unreachable("Encoding of this operand type is not supported yet.");