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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
|
2021-09-07 16:21:04 +08:00
|
|
|
#include "AMDGPUMCInstLower.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "AMDGPUAsmPrinter.h"
|
2014-06-13 09:32:00 +08:00
|
|
|
#include "AMDGPUTargetMachine.h"
|
2019-05-11 08:03:35 +08:00
|
|
|
#include "MCTargetDesc/AMDGPUInstPrinter.h"
|
2021-01-20 20:48:02 +08:00
|
|
|
#include "MCTargetDesc/AMDGPUMCTargetDesc.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"
|
|
|
|
|
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;
|
2019-06-17 01:43:37 +08:00
|
|
|
case SIInstrInfo::MO_ABS32_LO:
|
|
|
|
return MCSymbolRefExpr::VK_AMDGPU_ABS32_LO;
|
|
|
|
case SIInstrInfo::MO_ABS32_HI:
|
|
|
|
return MCSymbolRefExpr::VK_AMDGPU_ABS32_HI;
|
2016-07-13 22:23:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-07 01:19:11 +08:00
|
|
|
bool AMDGPUMCInstLower::lowerOperand(const MachineOperand &MO,
|
|
|
|
MCOperand &MCOp) const {
|
|
|
|
switch (MO.getType()) {
|
|
|
|
default:
|
2021-07-19 01:45:52 +08:00
|
|
|
break;
|
2016-10-07 01:19:11 +08:00
|
|
|
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;
|
2021-07-19 01:45:52 +08:00
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
|
|
|
MCOp = MCOperand::createExpr(
|
2016-10-07 01:19:11 +08:00
|
|
|
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);
|
2019-06-17 01:43:37 +08:00
|
|
|
const MCExpr *Expr =
|
2016-10-07 01:19:11 +08:00
|
|
|
MCSymbolRefExpr::create(Sym, getVariantKind(MO.getTargetFlags()),Ctx);
|
2019-06-17 01:43:37 +08:00
|
|
|
int64_t Offset = MO.getOffset();
|
|
|
|
if (Offset != 0) {
|
|
|
|
Expr = MCBinaryExpr::createAdd(Expr,
|
|
|
|
MCConstantExpr::create(Offset, Ctx), Ctx);
|
|
|
|
}
|
2016-10-07 01:19:11 +08:00
|
|
|
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;
|
2021-07-19 01:45:52 +08:00
|
|
|
case MachineOperand::MO_MCSymbol:
|
|
|
|
if (MO.getTargetFlags() == SIInstrInfo::MO_FAR_BRANCH_OFFSET) {
|
|
|
|
MCSymbol *Sym = MO.getMCSymbol();
|
|
|
|
MCOp = MCOperand::createExpr(Sym->getVariableValue());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
2016-10-07 01:19:11 +08:00
|
|
|
}
|
2021-07-19 01:45:52 +08:00
|
|
|
llvm_unreachable("unknown operand type");
|
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);
|
|
|
|
}
|
2019-10-12 06:03:36 +08:00
|
|
|
|
|
|
|
int FIIdx = AMDGPU::getNamedOperandIdx(MCOpcode, AMDGPU::OpName::fi);
|
|
|
|
if (FIIdx >= (int)OutMI.getNumOperands())
|
|
|
|
OutMI.addOperand(MCOperand::createImm(0));
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-02-14 13:58:16 +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()) {
|
2020-02-14 13:58:16 +08:00
|
|
|
emitInstruction(&*I);
|
2012-12-12 05:25:42 +08:00
|
|
|
++I;
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-25 09:19:37 +08:00
|
|
|
// We don't want these pseudo instructions encoded. They are
|
2017-03-22 06:18:10 +08:00
|
|
|
// placeholder terminator instructions and should only be printed as
|
|
|
|
// comments.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-16 19:15:49 +08:00
|
|
|
if (MI->isMetaInstruction()) {
|
|
|
|
if (isVerbose())
|
|
|
|
OutStreamer->emitRawComment(" meta instruction");
|
|
|
|
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.
|
2020-10-23 08:08:59 +08:00
|
|
|
//
|
|
|
|
// We also overestimate branch sizes with the offset bug.
|
|
|
|
if (!MI->isPseudo() && STI.isCPUStringValid(STI.getCPU()) &&
|
|
|
|
(!STI.hasOffset3fBug() || !MI->isBranch())) {
|
2018-08-29 15:46:09 +08:00
|
|
|
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
|
|
|
|
|
2019-05-15 00:17:14 +08:00
|
|
|
if (DumpCodeInstEmitter) {
|
|
|
|
// Disassemble instruction/operands to text
|
2013-10-12 13:02:51 +08:00
|
|
|
DisasmLines.resize(DisasmLines.size() + 1);
|
|
|
|
std::string &DisasmLine = DisasmLines.back();
|
|
|
|
raw_string_ostream DisasmStream(DisasmLine);
|
|
|
|
|
2019-05-15 00:17:14 +08:00
|
|
|
AMDGPUInstPrinter InstPrinter(*TM.getMCAsmInfo(), *STI.getInstrInfo(),
|
2016-06-24 14:30:11 +08:00
|
|
|
*STI.getRegisterInfo());
|
2020-01-04 02:55:30 +08:00
|
|
|
InstPrinter.printInst(&TmpInst, 0, StringRef(), STI, DisasmStream);
|
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);
|
|
|
|
|
2019-05-15 00:17:14 +08:00
|
|
|
DumpCodeInstEmitter->encodeInstruction(
|
|
|
|
TmpInst, CodeStream, Fixups, 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
|
|
|
}
|
|
|
|
}
|