2012-12-12 05:25:42 +08:00
|
|
|
//===- AMDGPUMCInstLower.cpp - Lower AMDGPU MachineInstr to an MCInst -----===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-12-12 05:25:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Code to lower AMDGPU MachineInstrs to their corresponding MCInst.
|
2012-12-12 05:25:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "AMDGPUAsmPrinter.h"
|
2016-06-24 14:30:11 +08:00
|
|
|
#include "AMDGPUSubtarget.h"
|
2014-06-13 09:32:00 +08:00
|
|
|
#include "AMDGPUTargetMachine.h"
|
2013-10-12 13:02:51 +08:00
|
|
|
#include "InstPrinter/AMDGPUInstPrinter.h"
|
AMDGPU: Remove #include "MCTargetDesc/AMDGPUMCTargetDesc.h" from common headers
Summary:
MCTargetDesc/AMDGPUMCTargetDesc.h contains enums for all the instuction
and register defintions, which are huge so we only want to include
them where needed.
This will also make it easier if we want to split the R600 and GCN
definitions into separate tablegenerated files.
I was unable to remove AMDGPUMCTargetDesc.h from SIMachineFunctionInfo.h
because it uses some enums from the header to initialize default values
for the SIMachineFunction class, so I ended up having to remove includes of
SIMachineFunctionInfo.h from headers too.
Reviewers: arsenm, nhaehnle
Reviewed By: nhaehnle
Subscribers: MatzeB, kzhuravl, wdng, yaxunl, dstuttard, tpr, t-tye, javed.absar, llvm-commits
Differential Revision: https://reviews.llvm.org/D46272
llvm-svn: 332930
2018-05-22 10:03:23 +08:00
|
|
|
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
|
2018-05-25 04:02:01 +08:00
|
|
|
#include "R600AsmPrinter.h"
|
2014-05-17 04:56:47 +08:00
|
|
|
#include "SIInstrInfo.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2015-01-16 02:42:51 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2014-07-21 22:01:14 +08:00
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
2013-10-12 13:02:51 +08:00
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
2014-07-21 22:01:14 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2013-01-02 18:22:59 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
2013-10-12 13:02:51 +08:00
|
|
|
#include "llvm/MC/MCObjectStreamer.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2013-10-12 13:02:51 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include <algorithm>
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2018-05-25 12:57:02 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class AMDGPUMCInstLower {
|
|
|
|
MCContext &Ctx;
|
2018-05-30 01:41:59 +08:00
|
|
|
const TargetSubtargetInfo &ST;
|
2018-05-25 12:57:02 +08:00
|
|
|
const AsmPrinter &AP;
|
|
|
|
|
|
|
|
const MCExpr *getLongBranchBlockExpr(const MachineBasicBlock &SrcBB,
|
|
|
|
const MachineOperand &MO) const;
|
|
|
|
|
|
|
|
public:
|
2018-05-30 01:41:59 +08:00
|
|
|
AMDGPUMCInstLower(MCContext &ctx, const TargetSubtargetInfo &ST,
|
2018-05-25 12:57:02 +08:00
|
|
|
const AsmPrinter &AP);
|
|
|
|
|
|
|
|
bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
|
|
|
|
|
|
|
|
/// Lower a MachineInstr to an MCInst
|
|
|
|
void lower(const MachineInstr *MI, MCInst &OutMI) const;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-05-30 01:41:59 +08:00
|
|
|
class R600MCInstLower : public AMDGPUMCInstLower {
|
|
|
|
public:
|
|
|
|
R600MCInstLower(MCContext &ctx, const R600Subtarget &ST,
|
|
|
|
const AsmPrinter &AP);
|
|
|
|
|
|
|
|
/// Lower a MachineInstr to an MCInst
|
|
|
|
void lower(const MachineInstr *MI, MCInst &OutMI) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-05-25 12:57:02 +08:00
|
|
|
} // End anonymous namespace
|
|
|
|
|
2016-10-07 01:19:11 +08:00
|
|
|
#include "AMDGPUGenMCPseudoLowering.inc"
|
|
|
|
|
2018-05-30 01:41:59 +08:00
|
|
|
AMDGPUMCInstLower::AMDGPUMCInstLower(MCContext &ctx,
|
|
|
|
const TargetSubtargetInfo &st,
|
2016-09-27 01:29:25 +08:00
|
|
|
const AsmPrinter &ap):
|
|
|
|
Ctx(ctx), ST(st), AP(ap) { }
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2016-07-13 22:23:33 +08:00
|
|
|
static MCSymbolRefExpr::VariantKind getVariantKind(unsigned MOFlags) {
|
|
|
|
switch (MOFlags) {
|
2016-10-14 12:37:34 +08:00
|
|
|
default:
|
|
|
|
return MCSymbolRefExpr::VK_None;
|
|
|
|
case SIInstrInfo::MO_GOTPCREL:
|
|
|
|
return MCSymbolRefExpr::VK_GOTPCREL;
|
|
|
|
case SIInstrInfo::MO_GOTPCREL32_LO:
|
|
|
|
return MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_LO;
|
|
|
|
case SIInstrInfo::MO_GOTPCREL32_HI:
|
|
|
|
return MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_HI;
|
|
|
|
case SIInstrInfo::MO_REL32_LO:
|
|
|
|
return MCSymbolRefExpr::VK_AMDGPU_REL32_LO;
|
|
|
|
case SIInstrInfo::MO_REL32_HI:
|
|
|
|
return MCSymbolRefExpr::VK_AMDGPU_REL32_HI;
|
2016-07-13 22:23:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-07 00:20:41 +08:00
|
|
|
const MCExpr *AMDGPUMCInstLower::getLongBranchBlockExpr(
|
|
|
|
const MachineBasicBlock &SrcBB,
|
|
|
|
const MachineOperand &MO) const {
|
|
|
|
const MCExpr *DestBBSym
|
|
|
|
= MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx);
|
|
|
|
const MCExpr *SrcBBSym = MCSymbolRefExpr::create(SrcBB.getSymbol(), Ctx);
|
|
|
|
|
|
|
|
assert(SrcBB.front().getOpcode() == AMDGPU::S_GETPC_B64 &&
|
|
|
|
ST.getInstrInfo()->get(AMDGPU::S_GETPC_B64).Size == 4);
|
|
|
|
|
|
|
|
// s_getpc_b64 returns the address of next instruction.
|
|
|
|
const MCConstantExpr *One = MCConstantExpr::create(4, Ctx);
|
|
|
|
SrcBBSym = MCBinaryExpr::createAdd(SrcBBSym, One, Ctx);
|
|
|
|
|
|
|
|
if (MO.getTargetFlags() == AMDGPU::TF_LONG_BRANCH_FORWARD)
|
|
|
|
return MCBinaryExpr::createSub(DestBBSym, SrcBBSym, Ctx);
|
|
|
|
|
|
|
|
assert(MO.getTargetFlags() == AMDGPU::TF_LONG_BRANCH_BACKWARD);
|
|
|
|
return MCBinaryExpr::createSub(SrcBBSym, DestBBSym, Ctx);
|
|
|
|
}
|
|
|
|
|
2016-10-07 01:19:11 +08:00
|
|
|
bool AMDGPUMCInstLower::lowerOperand(const MachineOperand &MO,
|
|
|
|
MCOperand &MCOp) const {
|
|
|
|
switch (MO.getType()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown operand type");
|
|
|
|
case MachineOperand::MO_Immediate:
|
|
|
|
MCOp = MCOperand::createImm(MO.getImm());
|
|
|
|
return true;
|
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
MCOp = MCOperand::createReg(AMDGPU::getMCReg(MO.getReg(), ST));
|
|
|
|
return true;
|
|
|
|
case MachineOperand::MO_MachineBasicBlock: {
|
|
|
|
if (MO.getTargetFlags() != 0) {
|
|
|
|
MCOp = MCOperand::createExpr(
|
|
|
|
getLongBranchBlockExpr(*MO.getParent()->getParent(), MO));
|
|
|
|
} else {
|
|
|
|
MCOp = MCOperand::createExpr(
|
|
|
|
MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case MachineOperand::MO_GlobalAddress: {
|
|
|
|
const GlobalValue *GV = MO.getGlobal();
|
|
|
|
SmallString<128> SymbolName;
|
|
|
|
AP.getNameWithPrefix(SymbolName, GV);
|
|
|
|
MCSymbol *Sym = Ctx.getOrCreateSymbol(SymbolName);
|
|
|
|
const MCExpr *SymExpr =
|
|
|
|
MCSymbolRefExpr::create(Sym, getVariantKind(MO.getTargetFlags()),Ctx);
|
|
|
|
const MCExpr *Expr = MCBinaryExpr::createAdd(SymExpr,
|
|
|
|
MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
|
|
|
|
MCOp = MCOperand::createExpr(Expr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case MachineOperand::MO_ExternalSymbol: {
|
|
|
|
MCSymbol *Sym = Ctx.getOrCreateSymbol(StringRef(MO.getSymbolName()));
|
|
|
|
Sym->setExternal(true);
|
|
|
|
const MCSymbolRefExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
|
|
|
|
MCOp = MCOperand::createExpr(Expr);
|
|
|
|
return true;
|
|
|
|
}
|
2017-08-02 03:54:18 +08:00
|
|
|
case MachineOperand::MO_RegisterMask:
|
|
|
|
// Regmasks are like implicit defs.
|
|
|
|
return false;
|
2016-10-07 01:19:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 02:42:51 +08:00
|
|
|
void AMDGPUMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
|
2017-05-18 05:56:25 +08:00
|
|
|
unsigned Opcode = MI->getOpcode();
|
2018-05-30 01:41:59 +08:00
|
|
|
const auto *TII = static_cast<const SIInstrInfo*>(ST.getInstrInfo());
|
2014-05-17 04:56:47 +08:00
|
|
|
|
2017-05-18 05:56:25 +08:00
|
|
|
// FIXME: Should be able to handle this with emitPseudoExpansionLowering. We
|
|
|
|
// need to select it to the subtarget specific version, and there's no way to
|
|
|
|
// do that with a single pseudo source operation.
|
|
|
|
if (Opcode == AMDGPU::S_SETPC_B64_return)
|
|
|
|
Opcode = AMDGPU::S_SETPC_B64;
|
2017-08-02 09:31:28 +08:00
|
|
|
else if (Opcode == AMDGPU::SI_CALL) {
|
|
|
|
// SI_CALL is just S_SWAPPC_B64 with an additional operand to track the
|
2017-08-02 09:42:04 +08:00
|
|
|
// called function (which we need to remove here).
|
|
|
|
OutMI.setOpcode(TII->pseudoToMCOpcode(AMDGPU::S_SWAPPC_B64));
|
|
|
|
MCOperand Dest, Src;
|
|
|
|
lowerOperand(MI->getOperand(0), Dest);
|
|
|
|
lowerOperand(MI->getOperand(1), Src);
|
|
|
|
OutMI.addOperand(Dest);
|
|
|
|
OutMI.addOperand(Src);
|
|
|
|
return;
|
2017-08-12 04:42:08 +08:00
|
|
|
} else if (Opcode == AMDGPU::SI_TCRETURN) {
|
|
|
|
// TODO: How to use branch immediate and avoid register+add?
|
|
|
|
Opcode = AMDGPU::S_SETPC_B64;
|
2017-08-02 09:31:28 +08:00
|
|
|
}
|
2014-05-17 04:56:47 +08:00
|
|
|
|
2017-08-02 09:42:04 +08:00
|
|
|
int MCOpcode = TII->pseudoToMCOpcode(Opcode);
|
2015-01-16 02:42:51 +08:00
|
|
|
if (MCOpcode == -1) {
|
2017-12-16 06:22:58 +08:00
|
|
|
LLVMContext &C = MI->getParent()->getParent()->getFunction().getContext();
|
2015-01-16 02:42:51 +08:00
|
|
|
C.emitError("AMDGPUMCInstLower::lower - Pseudo instruction doesn't have "
|
|
|
|
"a target-specific version: " + Twine(MI->getOpcode()));
|
|
|
|
}
|
2014-05-17 04:56:47 +08:00
|
|
|
|
2015-01-16 02:42:51 +08:00
|
|
|
OutMI.setOpcode(MCOpcode);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2014-04-06 06:42:04 +08:00
|
|
|
for (const MachineOperand &MO : MI->explicit_operands()) {
|
2012-12-12 05:25:42 +08:00
|
|
|
MCOperand MCOp;
|
2016-10-07 01:19:11 +08:00
|
|
|
lowerOperand(MO, MCOp);
|
2012-12-12 05:25:42 +08:00
|
|
|
OutMI.addOperand(MCOp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-07 01:19:11 +08:00
|
|
|
bool AMDGPUAsmPrinter::lowerOperand(const MachineOperand &MO,
|
|
|
|
MCOperand &MCOp) const {
|
2018-07-12 04:59:01 +08:00
|
|
|
const GCNSubtarget &STI = MF->getSubtarget<GCNSubtarget>();
|
2016-10-07 01:19:11 +08:00
|
|
|
AMDGPUMCInstLower MCInstLowering(OutContext, STI, *this);
|
|
|
|
return MCInstLowering.lowerOperand(MO, MCOp);
|
|
|
|
}
|
|
|
|
|
2018-05-25 04:02:01 +08:00
|
|
|
static const MCExpr *lowerAddrSpaceCast(const TargetMachine &TM,
|
|
|
|
const Constant *CV,
|
|
|
|
MCContext &OutContext) {
|
2017-02-07 08:43:21 +08:00
|
|
|
// TargetMachine does not support llvm-style cast. Use C++-style cast.
|
|
|
|
// This is safe since TM is always of type AMDGPUTargetMachine or its
|
|
|
|
// derived class.
|
2018-05-25 04:02:01 +08:00
|
|
|
auto &AT = static_cast<const AMDGPUTargetMachine&>(TM);
|
2017-02-07 08:43:21 +08:00
|
|
|
auto *CE = dyn_cast<ConstantExpr>(CV);
|
|
|
|
|
|
|
|
// Lower null pointers in private and local address space.
|
|
|
|
// Clang generates addrspacecast for null pointers in private and local
|
|
|
|
// address space, which needs to be lowered.
|
|
|
|
if (CE && CE->getOpcode() == Instruction::AddrSpaceCast) {
|
|
|
|
auto Op = CE->getOperand(0);
|
|
|
|
auto SrcAddr = Op->getType()->getPointerAddressSpace();
|
2018-05-25 04:02:01 +08:00
|
|
|
if (Op->isNullValue() && AT.getNullPointerValue(SrcAddr) == 0) {
|
2017-02-07 08:43:21 +08:00
|
|
|
auto DstAddr = CE->getType()->getPointerAddressSpace();
|
2018-05-25 04:02:01 +08:00
|
|
|
return MCConstantExpr::create(AT.getNullPointerValue(DstAddr),
|
2017-02-07 08:43:21 +08:00
|
|
|
OutContext);
|
|
|
|
}
|
|
|
|
}
|
2018-05-25 04:02:01 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MCExpr *AMDGPUAsmPrinter::lowerConstant(const Constant *CV) {
|
|
|
|
if (const MCExpr *E = lowerAddrSpaceCast(TM, CV, OutContext))
|
|
|
|
return E;
|
2017-02-07 08:43:21 +08:00
|
|
|
return AsmPrinter::lowerConstant(CV);
|
|
|
|
}
|
|
|
|
|
2012-12-12 05:25:42 +08:00
|
|
|
void AMDGPUAsmPrinter::EmitInstruction(const MachineInstr *MI) {
|
2016-10-07 01:19:11 +08:00
|
|
|
if (emitPseudoExpansionLowering(*OutStreamer, MI))
|
|
|
|
return;
|
|
|
|
|
2018-07-12 04:59:01 +08:00
|
|
|
const GCNSubtarget &STI = MF->getSubtarget<GCNSubtarget>();
|
2016-09-27 01:29:25 +08:00
|
|
|
AMDGPUMCInstLower MCInstLowering(OutContext, STI, *this);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2014-03-01 05:36:41 +08:00
|
|
|
StringRef Err;
|
2016-06-30 08:01:54 +08:00
|
|
|
if (!STI.getInstrInfo()->verifyInstruction(*MI, Err)) {
|
2017-12-16 06:22:58 +08:00
|
|
|
LLVMContext &C = MI->getParent()->getParent()->getFunction().getContext();
|
2016-03-16 17:10:42 +08:00
|
|
|
C.emitError("Illegal instruction detected: " + Err);
|
2017-01-28 10:02:38 +08:00
|
|
|
MI->print(errs());
|
2014-03-01 05:36:41 +08:00
|
|
|
}
|
2016-03-16 17:10:42 +08:00
|
|
|
|
2012-12-12 05:25:42 +08:00
|
|
|
if (MI->isBundle()) {
|
|
|
|
const MachineBasicBlock *MBB = MI->getParent();
|
2016-02-23 04:49:58 +08:00
|
|
|
MachineBasicBlock::const_instr_iterator I = ++MI->getIterator();
|
2015-10-14 04:07:10 +08:00
|
|
|
while (I != MBB->instr_end() && I->isInsideBundle()) {
|
|
|
|
EmitInstruction(&*I);
|
2012-12-12 05:25:42 +08:00
|
|
|
++I;
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-22 06:18:10 +08:00
|
|
|
// We don't want SI_MASK_BRANCH/SI_RETURN_TO_EPILOG encoded. They are
|
|
|
|
// placeholder terminator instructions and should only be printed as
|
|
|
|
// comments.
|
2016-06-23 04:15:28 +08:00
|
|
|
if (MI->getOpcode() == AMDGPU::SI_MASK_BRANCH) {
|
|
|
|
if (isVerbose()) {
|
|
|
|
SmallVector<char, 16> BBStr;
|
|
|
|
raw_svector_ostream Str(BBStr);
|
|
|
|
|
2016-07-08 08:55:44 +08:00
|
|
|
const MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
|
2016-06-23 04:15:28 +08:00
|
|
|
const MCSymbolRefExpr *Expr
|
|
|
|
= MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
|
|
|
|
Expr->print(Str, MAI);
|
2017-10-12 07:53:36 +08:00
|
|
|
OutStreamer->emitRawComment(Twine(" mask branch ") + BBStr);
|
2016-06-23 04:15:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-22 06:18:10 +08:00
|
|
|
if (MI->getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG) {
|
2016-06-23 04:15:28 +08:00
|
|
|
if (isVerbose())
|
2017-03-22 06:18:10 +08:00
|
|
|
OutStreamer->emitRawComment(" return to shader part epilog");
|
2016-06-23 04:15:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-16 03:00:15 +08:00
|
|
|
if (MI->getOpcode() == AMDGPU::WAVE_BARRIER) {
|
|
|
|
if (isVerbose())
|
|
|
|
OutStreamer->emitRawComment(" wave barrier");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-22 03:32:02 +08:00
|
|
|
if (MI->getOpcode() == AMDGPU::SI_MASKED_UNREACHABLE) {
|
|
|
|
if (isVerbose())
|
|
|
|
OutStreamer->emitRawComment(" divergent unreachable");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-12 05:25:42 +08:00
|
|
|
MCInst TmpInst;
|
|
|
|
MCInstLowering.lower(MI, TmpInst);
|
2015-04-25 03:11:51 +08:00
|
|
|
EmitToStreamer(*OutStreamer, TmpInst);
|
2013-10-12 13:02:51 +08:00
|
|
|
|
2018-08-29 15:46:09 +08:00
|
|
|
#ifdef EXPENSIVE_CHECKS
|
|
|
|
// Sanity-check getInstSizeInBytes on explicitly specified CPUs (it cannot
|
|
|
|
// work correctly for the generic CPU).
|
|
|
|
//
|
|
|
|
// The isPseudo check really shouldn't be here, but unfortunately there are
|
|
|
|
// some negative lit tests that depend on being able to continue through
|
|
|
|
// here even when pseudo instructions haven't been lowered.
|
|
|
|
if (!MI->isPseudo() && STI.isCPUStringValid(STI.getCPU())) {
|
|
|
|
SmallVector<MCFixup, 4> Fixups;
|
|
|
|
SmallVector<char, 16> CodeBytes;
|
|
|
|
raw_svector_ostream CodeStream(CodeBytes);
|
|
|
|
|
|
|
|
std::unique_ptr<MCCodeEmitter> InstEmitter(createSIMCCodeEmitter(
|
|
|
|
*STI.getInstrInfo(), *OutContext.getRegisterInfo(), OutContext));
|
|
|
|
InstEmitter->encodeInstruction(TmpInst, CodeStream, Fixups, STI);
|
|
|
|
|
|
|
|
assert(CodeBytes.size() == STI.getInstrInfo()->getInstSizeInBytes(*MI));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-19 09:10:53 +08:00
|
|
|
if (STI.dumpCode()) {
|
2013-10-12 13:02:51 +08:00
|
|
|
// Disassemble instruction/operands to text.
|
|
|
|
DisasmLines.resize(DisasmLines.size() + 1);
|
|
|
|
std::string &DisasmLine = DisasmLines.back();
|
|
|
|
raw_string_ostream DisasmStream(DisasmLine);
|
|
|
|
|
2014-08-05 05:25:23 +08:00
|
|
|
AMDGPUInstPrinter InstPrinter(*TM.getMCAsmInfo(),
|
2016-06-24 14:30:11 +08:00
|
|
|
*STI.getInstrInfo(),
|
|
|
|
*STI.getRegisterInfo());
|
|
|
|
InstPrinter.printInst(&TmpInst, DisasmStream, StringRef(), STI);
|
2013-10-12 13:02:51 +08:00
|
|
|
|
|
|
|
// Disassemble instruction/operands to hex representation.
|
|
|
|
SmallVector<MCFixup, 4> Fixups;
|
|
|
|
SmallVector<char, 16> CodeBytes;
|
|
|
|
raw_svector_ostream CodeStream(CodeBytes);
|
|
|
|
|
2015-05-05 00:45:08 +08:00
|
|
|
auto &ObjStreamer = static_cast<MCObjectStreamer&>(*OutStreamer);
|
|
|
|
MCCodeEmitter &InstEmitter = ObjStreamer.getAssembler().getEmitter();
|
2015-05-16 03:13:16 +08:00
|
|
|
InstEmitter.encodeInstruction(TmpInst, CodeStream, Fixups,
|
2015-01-31 07:24:40 +08:00
|
|
|
MF->getSubtarget<MCSubtargetInfo>());
|
2013-10-12 13:02:51 +08:00
|
|
|
HexLines.resize(HexLines.size() + 1);
|
|
|
|
std::string &HexLine = HexLines.back();
|
|
|
|
raw_string_ostream HexStream(HexLine);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < CodeBytes.size(); i += 4) {
|
|
|
|
unsigned int CodeDWord = *(unsigned int *)&CodeBytes[i];
|
|
|
|
HexStream << format("%s%08X", (i > 0 ? " " : ""), CodeDWord);
|
|
|
|
}
|
|
|
|
|
|
|
|
DisasmStream.flush();
|
|
|
|
DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLine.size());
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-25 04:02:01 +08:00
|
|
|
|
2018-05-30 01:41:59 +08:00
|
|
|
R600MCInstLower::R600MCInstLower(MCContext &Ctx, const R600Subtarget &ST,
|
|
|
|
const AsmPrinter &AP) :
|
|
|
|
AMDGPUMCInstLower(Ctx, ST, AP) { }
|
|
|
|
|
|
|
|
void R600MCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
|
|
|
|
OutMI.setOpcode(MI->getOpcode());
|
|
|
|
for (const MachineOperand &MO : MI->explicit_operands()) {
|
|
|
|
MCOperand MCOp;
|
|
|
|
lowerOperand(MO, MCOp);
|
|
|
|
OutMI.addOperand(MCOp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 04:02:01 +08:00
|
|
|
void R600AsmPrinter::EmitInstruction(const MachineInstr *MI) {
|
|
|
|
const R600Subtarget &STI = MF->getSubtarget<R600Subtarget>();
|
2018-05-30 01:41:59 +08:00
|
|
|
R600MCInstLower MCInstLowering(OutContext, STI, *this);
|
2018-05-25 04:02:01 +08:00
|
|
|
|
|
|
|
StringRef Err;
|
|
|
|
if (!STI.getInstrInfo()->verifyInstruction(*MI, Err)) {
|
|
|
|
LLVMContext &C = MI->getParent()->getParent()->getFunction().getContext();
|
|
|
|
C.emitError("Illegal instruction detected: " + Err);
|
|
|
|
MI->print(errs());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MI->isBundle()) {
|
|
|
|
const MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
MachineBasicBlock::const_instr_iterator I = ++MI->getIterator();
|
|
|
|
while (I != MBB->instr_end() && I->isInsideBundle()) {
|
|
|
|
EmitInstruction(&*I);
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MCInst TmpInst;
|
|
|
|
MCInstLowering.lower(MI, TmpInst);
|
|
|
|
EmitToStreamer(*OutStreamer, TmpInst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const MCExpr *R600AsmPrinter::lowerConstant(const Constant *CV) {
|
|
|
|
if (const MCExpr *E = lowerAddrSpaceCast(TM, CV, OutContext))
|
|
|
|
return E;
|
|
|
|
return AsmPrinter::lowerConstant(CV);
|
|
|
|
}
|