32#define DEBUG_TYPE "mccodeemitter"
34STATISTIC(MCNumEmitted,
"Number of MC instructions emitted");
40class LanaiMCCodeEmitter :
public MCCodeEmitter {
42 LanaiMCCodeEmitter(
const MCInstrInfo &MCII, MCContext &
C) {}
43 LanaiMCCodeEmitter(
const LanaiMCCodeEmitter &) =
delete;
44 void operator=(
const LanaiMCCodeEmitter &) =
delete;
45 ~LanaiMCCodeEmitter()
override =
default;
52 uint64_t getBinaryCodeForInstr(
const MCInst &Inst,
53 SmallVectorImpl<MCFixup> &Fixups,
54 const MCSubtargetInfo &SubtargetInfo)
const;
58 unsigned getMachineOpValue(
const MCInst &Inst,
const MCOperand &MCOp,
59 SmallVectorImpl<MCFixup> &Fixups,
60 const MCSubtargetInfo &SubtargetInfo)
const;
62 unsigned getRiMemoryOpValue(
const MCInst &Inst,
unsigned OpNo,
63 SmallVectorImpl<MCFixup> &Fixups,
64 const MCSubtargetInfo &SubtargetInfo)
const;
66 unsigned getRrMemoryOpValue(
const MCInst &Inst,
unsigned OpNo,
67 SmallVectorImpl<MCFixup> &Fixups,
68 const MCSubtargetInfo &SubtargetInfo)
const;
70 unsigned getSplsOpValue(
const MCInst &Inst,
unsigned OpNo,
71 SmallVectorImpl<MCFixup> &Fixups,
72 const MCSubtargetInfo &SubtargetInfo)
const;
75 SmallVectorImpl<MCFixup> &Fixups,
76 const MCSubtargetInfo &SubtargetInfo)
const;
78 void encodeInstruction(
const MCInst &Inst, SmallVectorImpl<char> &CB,
79 SmallVectorImpl<MCFixup> &Fixups,
80 const MCSubtargetInfo &SubtargetInfo)
const override;
82 unsigned adjustPqBitsRmAndRrm(
const MCInst &Inst,
unsigned Value,
83 const MCSubtargetInfo &STI)
const;
85 unsigned adjustPqBitsSpls(
const MCInst &Inst,
unsigned Value,
86 const MCSubtargetInfo &STI)
const;
92 if (isa<MCSymbolRefExpr>(Expr))
94 if (
const LanaiMCExpr *McExpr = dyn_cast<LanaiMCExpr>(Expr)) {
110unsigned LanaiMCCodeEmitter::getMachineOpValue(
111 const MCInst &Inst,
const MCOperand &MCOp, SmallVectorImpl<MCFixup> &Fixups,
112 const MCSubtargetInfo &SubtargetInfo)
const {
116 return static_cast<unsigned>(MCOp.getImm());
120 const MCExpr *Expr = MCOp.getExpr();
124 const MCBinaryExpr *
BinaryExpr =
static_cast<const MCBinaryExpr *
>(Expr);
137 unsigned PBitShift,
unsigned QBitShift) {
139 unsigned AluCode = AluOp.
getImm();
144 Value &= ~(1 << PBitShift);
148 Value |= (1 << PBitShift);
152 "Expected register operand.");
153 Value &= ~(1 << QBitShift);
156 Value |= (1 << QBitShift);
162LanaiMCCodeEmitter::adjustPqBitsRmAndRrm(
const MCInst &Inst,
unsigned Value,
163 const MCSubtargetInfo &STI)
const {
168LanaiMCCodeEmitter::adjustPqBitsSpls(
const MCInst &Inst,
unsigned Value,
169 const MCSubtargetInfo &STI)
const {
173void LanaiMCCodeEmitter::encodeInstruction(
174 const MCInst &Inst, SmallVectorImpl<char> &CB,
175 SmallVectorImpl<MCFixup> &Fixups,
176 const MCSubtargetInfo &SubtargetInfo)
const {
178 unsigned Value = getBinaryCodeForInstr(Inst, Fixups, SubtargetInfo);
185unsigned LanaiMCCodeEmitter::getRiMemoryOpValue(
186 const MCInst &Inst,
unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
187 const MCSubtargetInfo &SubtargetInfo)
const {
189 const MCOperand Op1 = Inst.getOperand(OpNo + 0);
190 const MCOperand Op2 = Inst.getOperand(OpNo + 1);
191 const MCOperand AluOp = Inst.getOperand(OpNo + 2);
193 assert(Op1.isReg() &&
"First operand is not register.");
194 assert((Op2.isImm() || Op2.isExpr()) &&
195 "Second operand is neither an immediate nor an expression.");
197 "Register immediate only supports addition operator");
201 assert(isInt<16>(Op2.getImm()) &&
202 "Constant value truncated (limited to 16-bit)");
204 Encoding |= (Op2.getImm() & 0xffff);
205 if (Op2.getImm() != 0) {
207 Encoding |= (0x3 << 16);
209 Encoding |= (0x1 << 16);
212 getMachineOpValue(Inst, Op2, Fixups, SubtargetInfo);
217unsigned LanaiMCCodeEmitter::getRrMemoryOpValue(
218 const MCInst &Inst,
unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
219 const MCSubtargetInfo &SubtargetInfo)
const {
221 const MCOperand Op1 = Inst.getOperand(OpNo + 0);
222 const MCOperand Op2 = Inst.getOperand(OpNo + 1);
223 const MCOperand AluMCOp = Inst.getOperand(OpNo + 2);
225 assert(Op1.isReg() &&
"First operand is not register.");
227 assert(Op2.isReg() &&
"Second operand is not register.");
230 assert(AluMCOp.isImm() &&
"Third operator is not immediate.");
232 unsigned AluOp = AluMCOp.getImm();
236 Encoding |= (0x3 << 8);
238 Encoding |= (0x1 << 8);
256LanaiMCCodeEmitter::getSplsOpValue(
const MCInst &Inst,
unsigned OpNo,
257 SmallVectorImpl<MCFixup> &Fixups,
258 const MCSubtargetInfo &SubtargetInfo)
const {
260 const MCOperand Op1 = Inst.getOperand(OpNo + 0);
261 const MCOperand Op2 = Inst.getOperand(OpNo + 1);
262 const MCOperand AluOp = Inst.getOperand(OpNo + 2);
264 assert(Op1.isReg() &&
"First operand is not register.");
265 assert((Op2.isImm() || Op2.isExpr()) &&
266 "Second operand is neither an immediate nor an expression.");
268 "Register immediate only supports addition operator");
272 assert(isInt<10>(Op2.getImm()) &&
273 "Constant value truncated (limited to 10-bit)");
275 Encoding |= (Op2.getImm() & 0x3ff);
276 if (Op2.getImm() != 0) {
278 Encoding |= (0x3 << 10);
280 Encoding |= (0x1 << 10);
283 getMachineOpValue(Inst, Op2, Fixups, SubtargetInfo);
288unsigned LanaiMCCodeEmitter::getBranchTargetOpValue(
289 const MCInst &Inst,
unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
290 const MCSubtargetInfo &SubtargetInfo)
const {
291 const MCOperand &MCOp = Inst.getOperand(OpNo);
292 if (MCOp.isReg() || MCOp.isImm())
293 return getMachineOpValue(Inst, MCOp, Fixups, SubtargetInfo);
301#include "LanaiGenMCCodeEmitter.inc"
308 return new LanaiMCCodeEmitter(InstrInfo, context);
static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, unsigned FixupKind, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI)
getBranchTargetOpValue - Helper function to get the branch target operand, which is either an immedia...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
MCCodeEmitter - Generic instruction encoding interface.
Context object for machine code objects.
Base class for the full range of assembler expressions which are needed for parsing.
@ SymbolRef
References to labels and assigned expressions.
@ Binary
Binary expressions.
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Instances of this class represent a single low-level machine instruction.
const MCOperand & getOperand(unsigned i) const
Interface to description of machine instruction set.
Instances of this class represent operands of the MCInst class.
unsigned getReg() const
Returns the register number.
LLVM Value Representation.
@ C
The default llvm calling convention, compatible with C.
static bool isPreOp(unsigned AluOp)
static unsigned getAluOp(unsigned AluOp)
static unsigned encodeLanaiAluCode(unsigned AluOp)
static bool isPostOp(unsigned AluOp)
static bool modifiesOp(unsigned AluOp)
This is an optimization pass for GlobalISel generic memory operations.
MCCodeEmitter * createLanaiMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)
MCFixupKind
Extensible enumeration to represent the type of a fixup.
static unsigned adjustPqBits(const MCInst &Inst, unsigned Value, unsigned PBitShift, unsigned QBitShift)
static unsigned getLanaiRegisterNumbering(unsigned Reg)