10b57cec5SDimitry Andric //===- R600MCCodeEmitter.cpp - Code Emitter for R600->Cayman GPU families -===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric ///
110b57cec5SDimitry Andric /// The R600 code emitter produces machine code that can be executed
120b57cec5SDimitry Andric /// directly on the GPU device.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
150b57cec5SDimitry Andric
16349cc55cSDimitry Andric #include "MCTargetDesc/R600MCTargetDesc.h"
170b57cec5SDimitry Andric #include "R600Defines.h"
180b57cec5SDimitry Andric #include "llvm/MC/MCCodeEmitter.h"
190b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
200b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
210b57cec5SDimitry Andric #include "llvm/MC/MCInstrInfo.h"
220b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
2381ad6265SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
240b57cec5SDimitry Andric #include "llvm/Support/EndianStream.h"
2506c3fb27SDimitry Andric #include "llvm/TargetParser/SubtargetFeature.h"
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric using namespace llvm;
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric namespace {
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric class R600MCCodeEmitter : public MCCodeEmitter {
320b57cec5SDimitry Andric const MCRegisterInfo &MRI;
330b57cec5SDimitry Andric const MCInstrInfo &MCII;
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric public:
R600MCCodeEmitter(const MCInstrInfo & mcii,const MCRegisterInfo & mri)360b57cec5SDimitry Andric R600MCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri)
370b57cec5SDimitry Andric : MRI(mri), MCII(mcii) {}
380b57cec5SDimitry Andric R600MCCodeEmitter(const R600MCCodeEmitter &) = delete;
390b57cec5SDimitry Andric R600MCCodeEmitter &operator=(const R600MCCodeEmitter &) = delete;
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric /// Encode the instruction and write it to the OS.
4206c3fb27SDimitry Andric void encodeInstruction(const MCInst &MI, SmallVectorImpl<char> &CB,
430b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
445ffd83dbSDimitry Andric const MCSubtargetInfo &STI) const override;
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric /// \returns the encoding for an MCOperand.
470b57cec5SDimitry Andric uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
480b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
490b57cec5SDimitry Andric const MCSubtargetInfo &STI) const;
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric private:
5206c3fb27SDimitry Andric void emit(uint32_t value, SmallVectorImpl<char> &CB) const;
5306c3fb27SDimitry Andric void emit(uint64_t value, SmallVectorImpl<char> &CB) const;
540b57cec5SDimitry Andric
550b57cec5SDimitry Andric unsigned getHWReg(unsigned regNo) const;
560b57cec5SDimitry Andric
570b57cec5SDimitry Andric uint64_t getBinaryCodeForInstr(const MCInst &MI,
580b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
590b57cec5SDimitry Andric const MCSubtargetInfo &STI) const;
600b57cec5SDimitry Andric };
610b57cec5SDimitry Andric
620b57cec5SDimitry Andric } // end anonymous namespace
630b57cec5SDimitry Andric
640b57cec5SDimitry Andric enum RegElement {
650b57cec5SDimitry Andric ELEMENT_X = 0,
660b57cec5SDimitry Andric ELEMENT_Y,
670b57cec5SDimitry Andric ELEMENT_Z,
680b57cec5SDimitry Andric ELEMENT_W
690b57cec5SDimitry Andric };
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric enum FCInstr {
720b57cec5SDimitry Andric FC_IF_PREDICATE = 0,
730b57cec5SDimitry Andric FC_ELSE,
740b57cec5SDimitry Andric FC_ENDIF,
750b57cec5SDimitry Andric FC_BGNLOOP,
760b57cec5SDimitry Andric FC_ENDLOOP,
770b57cec5SDimitry Andric FC_BREAK_PREDICATE,
780b57cec5SDimitry Andric FC_CONTINUE
790b57cec5SDimitry Andric };
800b57cec5SDimitry Andric
createR600MCCodeEmitter(const MCInstrInfo & MCII,MCContext & Ctx)810b57cec5SDimitry Andric MCCodeEmitter *llvm::createR600MCCodeEmitter(const MCInstrInfo &MCII,
820b57cec5SDimitry Andric MCContext &Ctx) {
8381ad6265SDimitry Andric return new R600MCCodeEmitter(MCII, *Ctx.getRegisterInfo());
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric
encodeInstruction(const MCInst & MI,SmallVectorImpl<char> & CB,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const8606c3fb27SDimitry Andric void R600MCCodeEmitter::encodeInstruction(const MCInst &MI,
8706c3fb27SDimitry Andric SmallVectorImpl<char> &CB,
880b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
890b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
900b57cec5SDimitry Andric const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
910b57cec5SDimitry Andric if (MI.getOpcode() == R600::RETURN ||
920b57cec5SDimitry Andric MI.getOpcode() == R600::FETCH_CLAUSE ||
930b57cec5SDimitry Andric MI.getOpcode() == R600::ALU_CLAUSE ||
940b57cec5SDimitry Andric MI.getOpcode() == R600::BUNDLE ||
950b57cec5SDimitry Andric MI.getOpcode() == R600::KILL) {
960b57cec5SDimitry Andric return;
97*0fca6ea1SDimitry Andric }
98*0fca6ea1SDimitry Andric if (IS_VTX(Desc)) {
990b57cec5SDimitry Andric uint64_t InstWord01 = getBinaryCodeForInstr(MI, Fixups, STI);
1000b57cec5SDimitry Andric uint32_t InstWord2 = MI.getOperand(2).getImm(); // Offset
10106c3fb27SDimitry Andric if (!(STI.hasFeature(R600::FeatureCaymanISA))) {
1020b57cec5SDimitry Andric InstWord2 |= 1 << 19; // Mega-Fetch bit
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric
10506c3fb27SDimitry Andric emit(InstWord01, CB);
10606c3fb27SDimitry Andric emit(InstWord2, CB);
10706c3fb27SDimitry Andric emit((uint32_t)0, CB);
1080b57cec5SDimitry Andric } else if (IS_TEX(Desc)) {
1090b57cec5SDimitry Andric int64_t Sampler = MI.getOperand(14).getImm();
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric int64_t SrcSelect[4] = {
112*0fca6ea1SDimitry Andric MI.getOperand(2).getImm(), MI.getOperand(3).getImm(),
113*0fca6ea1SDimitry Andric MI.getOperand(4).getImm(), MI.getOperand(5).getImm()};
114*0fca6ea1SDimitry Andric int64_t Offsets[3] = {MI.getOperand(6).getImm() & 0x1F,
1150b57cec5SDimitry Andric MI.getOperand(7).getImm() & 0x1F,
116*0fca6ea1SDimitry Andric MI.getOperand(8).getImm() & 0x1F};
1170b57cec5SDimitry Andric
1180b57cec5SDimitry Andric uint64_t Word01 = getBinaryCodeForInstr(MI, Fixups, STI);
1190b57cec5SDimitry Andric uint32_t Word2 = Sampler << 15 | SrcSelect[ELEMENT_X] << 20 |
1200b57cec5SDimitry Andric SrcSelect[ELEMENT_Y] << 23 | SrcSelect[ELEMENT_Z] << 26 |
121*0fca6ea1SDimitry Andric SrcSelect[ELEMENT_W] << 29 | Offsets[0] << 0 |
122*0fca6ea1SDimitry Andric Offsets[1] << 5 | Offsets[2] << 10;
1230b57cec5SDimitry Andric
12406c3fb27SDimitry Andric emit(Word01, CB);
12506c3fb27SDimitry Andric emit(Word2, CB);
12606c3fb27SDimitry Andric emit((uint32_t)0, CB);
1270b57cec5SDimitry Andric } else {
1280b57cec5SDimitry Andric uint64_t Inst = getBinaryCodeForInstr(MI, Fixups, STI);
12906c3fb27SDimitry Andric if ((STI.hasFeature(R600::FeatureR600ALUInst)) &&
1300b57cec5SDimitry Andric ((Desc.TSFlags & R600_InstFlag::OP1) ||
1310b57cec5SDimitry Andric Desc.TSFlags & R600_InstFlag::OP2)) {
1320b57cec5SDimitry Andric uint64_t ISAOpCode = Inst & (0x3FFULL << 39);
1330b57cec5SDimitry Andric Inst &= ~(0x3FFULL << 39);
1340b57cec5SDimitry Andric Inst |= ISAOpCode << 1;
1350b57cec5SDimitry Andric }
13606c3fb27SDimitry Andric emit(Inst, CB);
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric
emit(uint32_t Value,SmallVectorImpl<char> & CB) const14006c3fb27SDimitry Andric void R600MCCodeEmitter::emit(uint32_t Value, SmallVectorImpl<char> &CB) const {
1415f757f3fSDimitry Andric support::endian::write(CB, Value, llvm::endianness::little);
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric
emit(uint64_t Value,SmallVectorImpl<char> & CB) const14406c3fb27SDimitry Andric void R600MCCodeEmitter::emit(uint64_t Value, SmallVectorImpl<char> &CB) const {
1455f757f3fSDimitry Andric support::endian::write(CB, Value, llvm::endianness::little);
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric
getHWReg(unsigned RegNo) const1480b57cec5SDimitry Andric unsigned R600MCCodeEmitter::getHWReg(unsigned RegNo) const {
1490b57cec5SDimitry Andric return MRI.getEncodingValue(RegNo) & HW_REG_MASK;
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric
getMachineOpValue(const MCInst & MI,const MCOperand & MO,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const1520b57cec5SDimitry Andric uint64_t R600MCCodeEmitter::getMachineOpValue(const MCInst &MI,
1530b57cec5SDimitry Andric const MCOperand &MO,
1540b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
1550b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
1560b57cec5SDimitry Andric if (MO.isReg()) {
1570b57cec5SDimitry Andric if (HAS_NATIVE_OPERANDS(MCII.get(MI.getOpcode()).TSFlags))
1580b57cec5SDimitry Andric return MRI.getEncodingValue(MO.getReg());
1590b57cec5SDimitry Andric return getHWReg(MO.getReg());
1600b57cec5SDimitry Andric }
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andric if (MO.isExpr()) {
1630b57cec5SDimitry Andric // We put rodata at the end of code section, then map the entire
1640b57cec5SDimitry Andric // code secetion as vtx buf. Thus the section relative address is the
1650b57cec5SDimitry Andric // correct one.
1660b57cec5SDimitry Andric // Each R600 literal instruction has two operands
1670b57cec5SDimitry Andric // We can't easily get the order of the current one, so compare against
1680b57cec5SDimitry Andric // the first one and adjust offset.
1690b57cec5SDimitry Andric const unsigned offset = (&MO == &MI.getOperand(0)) ? 0 : 4;
1700b57cec5SDimitry Andric Fixups.push_back(MCFixup::create(offset, MO.getExpr(), FK_SecRel_4, MI.getLoc()));
1710b57cec5SDimitry Andric return 0;
1720b57cec5SDimitry Andric }
1730b57cec5SDimitry Andric
1740b57cec5SDimitry Andric assert(MO.isImm());
1750b57cec5SDimitry Andric return MO.getImm();
1760b57cec5SDimitry Andric }
1770b57cec5SDimitry Andric
1780b57cec5SDimitry Andric #include "R600GenMCCodeEmitter.inc"
179