2012-12-11 22:25:42 +01:00
|
|
|
//===-- SIMCCodeEmitter.cpp - SI Code Emitter -------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
/// \brief The SI code emitter produces machine code that can be executed
|
|
|
|
/// directly on the GPU device.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
|
|
|
|
#include "MCTargetDesc/AMDGPUMCCodeEmitter.h"
|
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2013-01-02 11:22:59 +01:00
|
|
|
#include "llvm/MC/MCFixup.h"
|
2012-12-11 22:25:42 +01:00
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2013-02-16 12:28:22 +01:00
|
|
|
|
|
|
|
/// \brief Helper type used in encoding
|
|
|
|
typedef union {
|
|
|
|
int32_t I;
|
|
|
|
float F;
|
|
|
|
} IntFloatUnion;
|
|
|
|
|
2012-12-11 22:25:42 +01:00
|
|
|
class SIMCCodeEmitter : public AMDGPUMCCodeEmitter {
|
2013-02-19 00:11:17 +01:00
|
|
|
SIMCCodeEmitter(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
|
|
|
|
void operator=(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
|
2012-12-11 22:25:42 +01:00
|
|
|
const MCInstrInfo &MCII;
|
|
|
|
const MCRegisterInfo &MRI;
|
|
|
|
|
2013-02-16 12:28:22 +01:00
|
|
|
/// \brief Can this operand also contain immediate values?
|
|
|
|
bool isSrcOperand(const MCInstrDesc &Desc, unsigned OpNo) const;
|
|
|
|
|
|
|
|
/// \brief Encode an fp or int literal
|
|
|
|
uint32_t getLitEncoding(const MCOperand &MO) const;
|
|
|
|
|
2012-12-11 22:25:42 +01:00
|
|
|
public:
|
|
|
|
SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
|
|
|
|
const MCSubtargetInfo &sti, MCContext &ctx)
|
2013-03-26 20:42:48 +01:00
|
|
|
: MCII(mcii), MRI(mri) { }
|
2012-12-11 22:25:42 +01:00
|
|
|
|
|
|
|
~SIMCCodeEmitter() { }
|
|
|
|
|
|
|
|
/// \breif Encode the instruction and write it to the OS.
|
|
|
|
virtual void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
|
|
|
|
/// \returns the encoding for an MCOperand.
|
|
|
|
virtual uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // End anonymous namespace
|
|
|
|
|
|
|
|
MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
|
|
|
|
const MCRegisterInfo &MRI,
|
|
|
|
const MCSubtargetInfo &STI,
|
|
|
|
MCContext &Ctx) {
|
|
|
|
return new SIMCCodeEmitter(MCII, MRI, STI, Ctx);
|
|
|
|
}
|
|
|
|
|
2013-02-16 12:28:22 +01:00
|
|
|
bool SIMCCodeEmitter::isSrcOperand(const MCInstrDesc &Desc,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
|
|
|
|
unsigned RegClass = Desc.OpInfo[OpNo].RegClass;
|
|
|
|
return (AMDGPU::SSrc_32RegClassID == RegClass) ||
|
|
|
|
(AMDGPU::SSrc_64RegClassID == RegClass) ||
|
|
|
|
(AMDGPU::VSrc_32RegClassID == RegClass) ||
|
|
|
|
(AMDGPU::VSrc_64RegClassID == RegClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO) const {
|
|
|
|
|
|
|
|
IntFloatUnion Imm;
|
|
|
|
if (MO.isImm())
|
|
|
|
Imm.I = MO.getImm();
|
|
|
|
else if (MO.isFPImm())
|
|
|
|
Imm.F = MO.getFPImm();
|
|
|
|
else
|
|
|
|
return ~0;
|
|
|
|
|
|
|
|
if (Imm.I >= 0 && Imm.I <= 64)
|
|
|
|
return 128 + Imm.I;
|
|
|
|
|
|
|
|
if (Imm.I >= -16 && Imm.I <= -1)
|
|
|
|
return 192 + abs(Imm.I);
|
|
|
|
|
|
|
|
if (Imm.F == 0.5f)
|
|
|
|
return 240;
|
|
|
|
|
|
|
|
if (Imm.F == -0.5f)
|
|
|
|
return 241;
|
|
|
|
|
|
|
|
if (Imm.F == 1.0f)
|
|
|
|
return 242;
|
|
|
|
|
|
|
|
if (Imm.F == -1.0f)
|
|
|
|
return 243;
|
|
|
|
|
|
|
|
if (Imm.F == 2.0f)
|
|
|
|
return 244;
|
|
|
|
|
|
|
|
if (Imm.F == -2.0f)
|
|
|
|
return 245;
|
|
|
|
|
|
|
|
if (Imm.F == 4.0f)
|
|
|
|
return 246;
|
|
|
|
|
2013-02-26 18:51:57 +01:00
|
|
|
if (Imm.F == -4.0f)
|
2013-02-16 12:28:22 +01:00
|
|
|
return 247;
|
|
|
|
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
2012-12-11 22:25:42 +01:00
|
|
|
void SIMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
2013-02-16 12:28:22 +01:00
|
|
|
|
2012-12-11 22:25:42 +01:00
|
|
|
uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups);
|
2013-02-16 12:28:22 +01:00
|
|
|
const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
|
|
|
|
unsigned bytes = Desc.getSize();
|
|
|
|
|
2012-12-11 22:25:42 +01:00
|
|
|
for (unsigned i = 0; i < bytes; i++) {
|
|
|
|
OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
|
|
|
|
}
|
2013-02-16 12:28:22 +01:00
|
|
|
|
|
|
|
if (bytes > 4)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check for additional literals in SRC0/1/2 (Op 1/2/3)
|
|
|
|
for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
|
|
|
|
|
|
|
|
// Check if this operand should be encoded as [SV]Src
|
|
|
|
if (!isSrcOperand(Desc, i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Is this operand a literal immediate?
|
|
|
|
const MCOperand &Op = MI.getOperand(i);
|
|
|
|
if (getLitEncoding(Op) != 255)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Yes! Encode it
|
|
|
|
IntFloatUnion Imm;
|
|
|
|
if (Op.isImm())
|
|
|
|
Imm.I = Op.getImm();
|
|
|
|
else
|
|
|
|
Imm.F = Op.getFPImm();
|
|
|
|
|
|
|
|
for (unsigned j = 0; j < 4; j++) {
|
|
|
|
OS.write((uint8_t) ((Imm.I >> (8 * j)) & 0xff));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only one literal value allowed
|
|
|
|
break;
|
|
|
|
}
|
2012-12-11 22:25:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
|
|
|
|
const MCOperand &MO,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
2013-02-16 12:28:22 +01:00
|
|
|
if (MO.isReg())
|
2013-02-07 20:39:45 +01:00
|
|
|
return MRI.getEncodingValue(MO.getReg());
|
2013-02-16 12:28:22 +01:00
|
|
|
|
|
|
|
if (MO.isExpr()) {
|
2012-12-17 16:14:54 +01:00
|
|
|
const MCExpr *Expr = MO.getExpr();
|
|
|
|
MCFixupKind Kind = MCFixupKind(FK_PCRel_4);
|
|
|
|
Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
|
|
|
|
return 0;
|
2012-12-11 22:25:42 +01:00
|
|
|
}
|
2013-02-16 12:28:22 +01:00
|
|
|
|
|
|
|
// Figure out the operand number, needed for isSrcOperand check
|
|
|
|
unsigned OpNo = 0;
|
|
|
|
for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
|
|
|
|
if (&MO == &MI.getOperand(OpNo))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
|
|
|
|
if (isSrcOperand(Desc, OpNo)) {
|
|
|
|
uint32_t Enc = getLitEncoding(MO);
|
|
|
|
if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
|
|
|
|
return Enc;
|
|
|
|
|
|
|
|
} else if (MO.isImm())
|
|
|
|
return MO.getImm();
|
|
|
|
|
|
|
|
llvm_unreachable("Encoding of this operand type is not supported yet.");
|
2012-12-11 22:25:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|