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"
|
2014-06-13 09:32:00 +08:00
|
|
|
#include "AMDGPUTargetMachine.h"
|
2013-10-12 13:02:51 +08:00
|
|
|
#include "InstPrinter/AMDGPUInstPrinter.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "R600InstrInfo.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;
|
|
|
|
|
2014-05-17 04:56:47 +08:00
|
|
|
AMDGPUMCInstLower::AMDGPUMCInstLower(MCContext &ctx, const AMDGPUSubtarget &st):
|
|
|
|
Ctx(ctx), ST(st)
|
2012-12-17 23:14:54 +08:00
|
|
|
{ }
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2015-01-16 02:42:51 +08:00
|
|
|
void AMDGPUMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
|
2014-05-17 04:56:47 +08:00
|
|
|
|
2015-01-16 02:42:51 +08:00
|
|
|
int MCOpcode = ST.getInstrInfo()->pseudoToMCOpcode(MI->getOpcode());
|
2014-05-17 04:56:47 +08:00
|
|
|
|
2015-01-16 02:42:51 +08:00
|
|
|
if (MCOpcode == -1) {
|
|
|
|
LLVMContext &C = MI->getParent()->getParent()->getFunction()->getContext();
|
|
|
|
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;
|
|
|
|
switch (MO.getType()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown operand type");
|
|
|
|
case MachineOperand::MO_Immediate:
|
2015-05-14 02:37:00 +08:00
|
|
|
MCOp = MCOperand::createImm(MO.getImm());
|
2012-12-12 05:25:42 +08:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_Register:
|
2015-05-14 02:37:00 +08:00
|
|
|
MCOp = MCOperand::createReg(MO.getReg());
|
2012-12-12 05:25:42 +08:00
|
|
|
break;
|
2012-12-17 23:14:54 +08:00
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
2015-05-30 09:25:56 +08:00
|
|
|
MCOp = MCOperand::createExpr(MCSymbolRefExpr::create(
|
2012-12-17 23:14:54 +08:00
|
|
|
MO.getMBB()->getSymbol(), Ctx));
|
2014-07-21 22:01:14 +08:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_GlobalAddress: {
|
|
|
|
const GlobalValue *GV = MO.getGlobal();
|
2015-05-19 02:43:14 +08:00
|
|
|
MCSymbol *Sym = Ctx.getOrCreateSymbol(StringRef(GV->getName()));
|
2015-05-30 09:25:56 +08:00
|
|
|
MCOp = MCOperand::createExpr(MCSymbolRefExpr::create(Sym, Ctx));
|
2014-07-21 22:01:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MachineOperand::MO_TargetIndex: {
|
|
|
|
assert(MO.getIndex() == AMDGPU::TI_CONSTDATA_START);
|
2015-05-19 02:43:14 +08:00
|
|
|
MCSymbol *Sym = Ctx.getOrCreateSymbol(StringRef(END_OF_TEXT_LABEL_NAME));
|
2015-05-30 09:25:56 +08:00
|
|
|
const MCSymbolRefExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
|
2015-05-14 02:37:00 +08:00
|
|
|
MCOp = MCOperand::createExpr(Expr);
|
2014-07-21 22:01:14 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-01-21 01:49:47 +08:00
|
|
|
case MachineOperand::MO_ExternalSymbol: {
|
2015-05-19 02:43:14 +08:00
|
|
|
MCSymbol *Sym = Ctx.getOrCreateSymbol(StringRef(MO.getSymbolName()));
|
2015-05-30 09:25:56 +08:00
|
|
|
const MCSymbolRefExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
|
2015-05-14 02:37:00 +08:00
|
|
|
MCOp = MCOperand::createExpr(Expr);
|
2015-01-21 01:49:47 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
OutMI.addOperand(MCOp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AMDGPUAsmPrinter::EmitInstruction(const MachineInstr *MI) {
|
2015-02-19 09:10:53 +08:00
|
|
|
const AMDGPUSubtarget &STI = MF->getSubtarget<AMDGPUSubtarget>();
|
|
|
|
AMDGPUMCInstLower MCInstLowering(OutContext, STI);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2014-03-01 05:36:41 +08:00
|
|
|
#ifdef _DEBUG
|
|
|
|
StringRef Err;
|
2015-02-19 09:10:53 +08:00
|
|
|
if (!STI.getInstrInfo()->verifyInstruction(MI, Err)) {
|
2014-03-01 05:36:41 +08:00
|
|
|
errs() << "Warning: Illegal instruction detected: " << Err << "\n";
|
|
|
|
MI->dump();
|
|
|
|
}
|
|
|
|
#endif
|
2012-12-12 05:25:42 +08:00
|
|
|
if (MI->isBundle()) {
|
|
|
|
const MachineBasicBlock *MBB = MI->getParent();
|
2015-10-14 04:07:10 +08:00
|
|
|
MachineBasicBlock::const_instr_iterator I = ++MI->getIterator();
|
|
|
|
while (I != MBB->instr_end() && I->isInsideBundle()) {
|
|
|
|
EmitInstruction(&*I);
|
2012-12-12 05:25:42 +08:00
|
|
|
++I;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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(),
|
2015-01-31 07:24:40 +08:00
|
|
|
*MF->getSubtarget().getInstrInfo(),
|
|
|
|
*MF->getSubtarget().getRegisterInfo());
|
2015-03-28 04:36:02 +08:00
|
|
|
InstPrinter.printInst(&TmpInst, DisasmStream, StringRef(),
|
|
|
|
MF->getSubtarget());
|
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
|
|
|
}
|
|
|
|
}
|