1 //===- AMDGPUMCInstLower.cpp - Lower AMDGPU MachineInstr to an MCInst -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Code to lower AMDGPU MachineInstrs to their corresponding MCInst.
13 //===----------------------------------------------------------------------===//
16 #include "AMDGPUMCInstLower.h"
17 #include "AMDGPUAsmPrinter.h"
18 #include "AMDGPUSubtarget.h"
19 #include "AMDGPUTargetMachine.h"
20 #include "InstPrinter/AMDGPUInstPrinter.h"
21 #include "SIInstrInfo.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/MC/MCCodeEmitter.h"
28 #include "llvm/MC/MCContext.h"
29 #include "llvm/MC/MCExpr.h"
30 #include "llvm/MC/MCInst.h"
31 #include "llvm/MC/MCObjectStreamer.h"
32 #include "llvm/MC/MCStreamer.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/Format.h"
39 #include "AMDGPUGenMCPseudoLowering.inc"
42 AMDGPUMCInstLower::AMDGPUMCInstLower(MCContext &ctx, const AMDGPUSubtarget &st,
43 const AsmPrinter &ap):
44 Ctx(ctx), ST(st), AP(ap) { }
46 static MCSymbolRefExpr::VariantKind getVariantKind(unsigned MOFlags) {
49 return MCSymbolRefExpr::VK_None;
50 case SIInstrInfo::MO_GOTPCREL:
51 return MCSymbolRefExpr::VK_GOTPCREL;
52 case SIInstrInfo::MO_GOTPCREL32_LO:
53 return MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_LO;
54 case SIInstrInfo::MO_GOTPCREL32_HI:
55 return MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_HI;
56 case SIInstrInfo::MO_REL32_LO:
57 return MCSymbolRefExpr::VK_AMDGPU_REL32_LO;
58 case SIInstrInfo::MO_REL32_HI:
59 return MCSymbolRefExpr::VK_AMDGPU_REL32_HI;
63 const MCExpr *AMDGPUMCInstLower::getLongBranchBlockExpr(
64 const MachineBasicBlock &SrcBB,
65 const MachineOperand &MO) const {
66 const MCExpr *DestBBSym
67 = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx);
68 const MCExpr *SrcBBSym = MCSymbolRefExpr::create(SrcBB.getSymbol(), Ctx);
70 assert(SrcBB.front().getOpcode() == AMDGPU::S_GETPC_B64 &&
71 ST.getInstrInfo()->get(AMDGPU::S_GETPC_B64).Size == 4);
73 // s_getpc_b64 returns the address of next instruction.
74 const MCConstantExpr *One = MCConstantExpr::create(4, Ctx);
75 SrcBBSym = MCBinaryExpr::createAdd(SrcBBSym, One, Ctx);
77 if (MO.getTargetFlags() == AMDGPU::TF_LONG_BRANCH_FORWARD)
78 return MCBinaryExpr::createSub(DestBBSym, SrcBBSym, Ctx);
80 assert(MO.getTargetFlags() == AMDGPU::TF_LONG_BRANCH_BACKWARD);
81 return MCBinaryExpr::createSub(SrcBBSym, DestBBSym, Ctx);
84 bool AMDGPUMCInstLower::lowerOperand(const MachineOperand &MO,
85 MCOperand &MCOp) const {
86 switch (MO.getType()) {
88 llvm_unreachable("unknown operand type");
89 case MachineOperand::MO_Immediate:
90 MCOp = MCOperand::createImm(MO.getImm());
92 case MachineOperand::MO_Register:
93 MCOp = MCOperand::createReg(AMDGPU::getMCReg(MO.getReg(), ST));
95 case MachineOperand::MO_MachineBasicBlock: {
96 if (MO.getTargetFlags() != 0) {
97 MCOp = MCOperand::createExpr(
98 getLongBranchBlockExpr(*MO.getParent()->getParent(), MO));
100 MCOp = MCOperand::createExpr(
101 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
106 case MachineOperand::MO_GlobalAddress: {
107 const GlobalValue *GV = MO.getGlobal();
108 SmallString<128> SymbolName;
109 AP.getNameWithPrefix(SymbolName, GV);
110 MCSymbol *Sym = Ctx.getOrCreateSymbol(SymbolName);
111 const MCExpr *SymExpr =
112 MCSymbolRefExpr::create(Sym, getVariantKind(MO.getTargetFlags()),Ctx);
113 const MCExpr *Expr = MCBinaryExpr::createAdd(SymExpr,
114 MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
115 MCOp = MCOperand::createExpr(Expr);
118 case MachineOperand::MO_ExternalSymbol: {
119 MCSymbol *Sym = Ctx.getOrCreateSymbol(StringRef(MO.getSymbolName()));
120 Sym->setExternal(true);
121 const MCSymbolRefExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
122 MCOp = MCOperand::createExpr(Expr);
128 void AMDGPUMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
130 int MCOpcode = ST.getInstrInfo()->pseudoToMCOpcode(MI->getOpcode());
132 if (MCOpcode == -1) {
133 LLVMContext &C = MI->getParent()->getParent()->getFunction()->getContext();
134 C.emitError("AMDGPUMCInstLower::lower - Pseudo instruction doesn't have "
135 "a target-specific version: " + Twine(MI->getOpcode()));
138 OutMI.setOpcode(MCOpcode);
140 for (const MachineOperand &MO : MI->explicit_operands()) {
142 lowerOperand(MO, MCOp);
143 OutMI.addOperand(MCOp);
147 bool AMDGPUAsmPrinter::lowerOperand(const MachineOperand &MO,
148 MCOperand &MCOp) const {
149 const AMDGPUSubtarget &STI = MF->getSubtarget<AMDGPUSubtarget>();
150 AMDGPUMCInstLower MCInstLowering(OutContext, STI, *this);
151 return MCInstLowering.lowerOperand(MO, MCOp);
154 void AMDGPUAsmPrinter::EmitInstruction(const MachineInstr *MI) {
155 if (emitPseudoExpansionLowering(*OutStreamer, MI))
158 const AMDGPUSubtarget &STI = MF->getSubtarget<AMDGPUSubtarget>();
159 AMDGPUMCInstLower MCInstLowering(OutContext, STI, *this);
162 if (!STI.getInstrInfo()->verifyInstruction(*MI, Err)) {
163 LLVMContext &C = MI->getParent()->getParent()->getFunction()->getContext();
164 C.emitError("Illegal instruction detected: " + Err);
168 if (MI->isBundle()) {
169 const MachineBasicBlock *MBB = MI->getParent();
170 MachineBasicBlock::const_instr_iterator I = ++MI->getIterator();
171 while (I != MBB->instr_end() && I->isInsideBundle()) {
172 EmitInstruction(&*I);
176 // We don't want SI_MASK_BRANCH/SI_RETURN encoded. They are placeholder
177 // terminator instructions and should only be printed as comments.
178 if (MI->getOpcode() == AMDGPU::SI_MASK_BRANCH) {
180 SmallVector<char, 16> BBStr;
181 raw_svector_ostream Str(BBStr);
183 const MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
184 const MCSymbolRefExpr *Expr
185 = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
186 Expr->print(Str, MAI);
187 OutStreamer->emitRawComment(" mask branch " + BBStr);
193 if (MI->getOpcode() == AMDGPU::SI_RETURN) {
195 OutStreamer->emitRawComment(" return");
199 if (MI->getOpcode() == AMDGPU::WAVE_BARRIER) {
201 OutStreamer->emitRawComment(" wave barrier");
206 MCInstLowering.lower(MI, TmpInst);
207 EmitToStreamer(*OutStreamer, TmpInst);
209 if (STI.dumpCode()) {
210 // Disassemble instruction/operands to text.
211 DisasmLines.resize(DisasmLines.size() + 1);
212 std::string &DisasmLine = DisasmLines.back();
213 raw_string_ostream DisasmStream(DisasmLine);
215 AMDGPUInstPrinter InstPrinter(*TM.getMCAsmInfo(),
217 *STI.getRegisterInfo());
218 InstPrinter.printInst(&TmpInst, DisasmStream, StringRef(), STI);
220 // Disassemble instruction/operands to hex representation.
221 SmallVector<MCFixup, 4> Fixups;
222 SmallVector<char, 16> CodeBytes;
223 raw_svector_ostream CodeStream(CodeBytes);
225 auto &ObjStreamer = static_cast<MCObjectStreamer&>(*OutStreamer);
226 MCCodeEmitter &InstEmitter = ObjStreamer.getAssembler().getEmitter();
227 InstEmitter.encodeInstruction(TmpInst, CodeStream, Fixups,
228 MF->getSubtarget<MCSubtargetInfo>());
229 HexLines.resize(HexLines.size() + 1);
230 std::string &HexLine = HexLines.back();
231 raw_string_ostream HexStream(HexLine);
233 for (size_t i = 0; i < CodeBytes.size(); i += 4) {
234 unsigned int CodeDWord = *(unsigned int *)&CodeBytes[i];
235 HexStream << format("%s%08X", (i > 0 ? " " : ""), CodeDWord);
238 DisasmStream.flush();
239 DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLine.size());