2012-12-12 05:25:42 +08:00
|
|
|
//===- AMDGPUMCInstLower.cpp - Lower AMDGPU MachineInstr to an MCInst -----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
/// \brief Code to lower AMDGPU MachineInstrs to their corresponding MCInst.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "AMDGPUMCInstLower.h"
|
|
|
|
#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"
|
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;
|
|
|
|
|
2016-10-07 01:19:11 +08:00
|
|
|
#include "AMDGPUGenMCPseudoLowering.inc"
|
|
|
|
|
2016-09-27 01:29:25 +08:00
|
|
|
AMDGPUMCInstLower::AMDGPUMCInstLower(MCContext &ctx, const AMDGPUSubtarget &st,
|
|
|
|
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();
|
2017-08-02 09:42:04 +08:00
|
|
|
const auto *TII = 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 {
|
|
|
|
const AMDGPUSubtarget &STI = MF->getSubtarget<AMDGPUSubtarget>();
|
|
|
|
AMDGPUMCInstLower MCInstLowering(OutContext, STI, *this);
|
|
|
|
return MCInstLowering.lowerOperand(MO, MCOp);
|
|
|
|
}
|
|
|
|
|
2017-02-07 08:43:21 +08:00
|
|
|
const MCExpr *AMDGPUAsmPrinter::lowerConstant(const Constant *CV) {
|
|
|
|
// 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.
|
|
|
|
auto *AT = static_cast<AMDGPUTargetMachine*>(&TM);
|
|
|
|
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();
|
|
|
|
if (Op->isNullValue() && AT->getNullPointerValue(SrcAddr) == 0) {
|
|
|
|
auto DstAddr = CE->getType()->getPointerAddressSpace();
|
|
|
|
return MCConstantExpr::create(AT->getNullPointerValue(DstAddr),
|
|
|
|
OutContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2015-02-19 09:10:53 +08:00
|
|
|
const AMDGPUSubtarget &STI = MF->getSubtarget<AMDGPUSubtarget>();
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|