1*9880d681SAndroid Build Coastguard Worker //===- AMDGPUMCInstLower.cpp - Lower AMDGPU MachineInstr to an MCInst -----===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker /// \file
11*9880d681SAndroid Build Coastguard Worker /// \brief Code to lower AMDGPU MachineInstrs to their corresponding MCInst.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Worker #include "AMDGPUMCInstLower.h"
17*9880d681SAndroid Build Coastguard Worker #include "AMDGPUAsmPrinter.h"
18*9880d681SAndroid Build Coastguard Worker #include "AMDGPUSubtarget.h"
19*9880d681SAndroid Build Coastguard Worker #include "AMDGPUTargetMachine.h"
20*9880d681SAndroid Build Coastguard Worker #include "InstPrinter/AMDGPUInstPrinter.h"
21*9880d681SAndroid Build Coastguard Worker #include "SIInstrInfo.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBasicBlock.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstr.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/GlobalVariable.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCCodeEmitter.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCContext.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCExpr.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCInst.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCObjectStreamer.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCStreamer.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Format.h"
35*9880d681SAndroid Build Coastguard Worker #include <algorithm>
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker using namespace llvm;
38*9880d681SAndroid Build Coastguard Worker
AMDGPUMCInstLower(MCContext & ctx,const AMDGPUSubtarget & st)39*9880d681SAndroid Build Coastguard Worker AMDGPUMCInstLower::AMDGPUMCInstLower(MCContext &ctx, const AMDGPUSubtarget &st):
40*9880d681SAndroid Build Coastguard Worker Ctx(ctx), ST(st) { }
41*9880d681SAndroid Build Coastguard Worker
getVariantKind(unsigned MOFlags)42*9880d681SAndroid Build Coastguard Worker static MCSymbolRefExpr::VariantKind getVariantKind(unsigned MOFlags) {
43*9880d681SAndroid Build Coastguard Worker switch (MOFlags) {
44*9880d681SAndroid Build Coastguard Worker default: return MCSymbolRefExpr::VK_None;
45*9880d681SAndroid Build Coastguard Worker case SIInstrInfo::MO_GOTPCREL: return MCSymbolRefExpr::VK_GOTPCREL;
46*9880d681SAndroid Build Coastguard Worker }
47*9880d681SAndroid Build Coastguard Worker }
48*9880d681SAndroid Build Coastguard Worker
lower(const MachineInstr * MI,MCInst & OutMI) const49*9880d681SAndroid Build Coastguard Worker void AMDGPUMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
50*9880d681SAndroid Build Coastguard Worker
51*9880d681SAndroid Build Coastguard Worker int MCOpcode = ST.getInstrInfo()->pseudoToMCOpcode(MI->getOpcode());
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker if (MCOpcode == -1) {
54*9880d681SAndroid Build Coastguard Worker LLVMContext &C = MI->getParent()->getParent()->getFunction()->getContext();
55*9880d681SAndroid Build Coastguard Worker C.emitError("AMDGPUMCInstLower::lower - Pseudo instruction doesn't have "
56*9880d681SAndroid Build Coastguard Worker "a target-specific version: " + Twine(MI->getOpcode()));
57*9880d681SAndroid Build Coastguard Worker }
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker OutMI.setOpcode(MCOpcode);
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : MI->explicit_operands()) {
62*9880d681SAndroid Build Coastguard Worker MCOperand MCOp;
63*9880d681SAndroid Build Coastguard Worker switch (MO.getType()) {
64*9880d681SAndroid Build Coastguard Worker default:
65*9880d681SAndroid Build Coastguard Worker llvm_unreachable("unknown operand type");
66*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_Immediate:
67*9880d681SAndroid Build Coastguard Worker MCOp = MCOperand::createImm(MO.getImm());
68*9880d681SAndroid Build Coastguard Worker break;
69*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_Register:
70*9880d681SAndroid Build Coastguard Worker MCOp = MCOperand::createReg(AMDGPU::getMCReg(MO.getReg(), ST));
71*9880d681SAndroid Build Coastguard Worker break;
72*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_MachineBasicBlock:
73*9880d681SAndroid Build Coastguard Worker MCOp = MCOperand::createExpr(MCSymbolRefExpr::create(
74*9880d681SAndroid Build Coastguard Worker MO.getMBB()->getSymbol(), Ctx));
75*9880d681SAndroid Build Coastguard Worker break;
76*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_GlobalAddress: {
77*9880d681SAndroid Build Coastguard Worker const GlobalValue *GV = MO.getGlobal();
78*9880d681SAndroid Build Coastguard Worker MCSymbol *Sym = Ctx.getOrCreateSymbol(StringRef(GV->getName()));
79*9880d681SAndroid Build Coastguard Worker const MCExpr *SymExpr =
80*9880d681SAndroid Build Coastguard Worker MCSymbolRefExpr::create(Sym, getVariantKind(MO.getTargetFlags()),Ctx);
81*9880d681SAndroid Build Coastguard Worker const MCExpr *Expr = MCBinaryExpr::createAdd(SymExpr,
82*9880d681SAndroid Build Coastguard Worker MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
83*9880d681SAndroid Build Coastguard Worker MCOp = MCOperand::createExpr(Expr);
84*9880d681SAndroid Build Coastguard Worker break;
85*9880d681SAndroid Build Coastguard Worker }
86*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_ExternalSymbol: {
87*9880d681SAndroid Build Coastguard Worker MCSymbol *Sym = Ctx.getOrCreateSymbol(StringRef(MO.getSymbolName()));
88*9880d681SAndroid Build Coastguard Worker Sym->setExternal(true);
89*9880d681SAndroid Build Coastguard Worker const MCSymbolRefExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
90*9880d681SAndroid Build Coastguard Worker MCOp = MCOperand::createExpr(Expr);
91*9880d681SAndroid Build Coastguard Worker break;
92*9880d681SAndroid Build Coastguard Worker }
93*9880d681SAndroid Build Coastguard Worker }
94*9880d681SAndroid Build Coastguard Worker OutMI.addOperand(MCOp);
95*9880d681SAndroid Build Coastguard Worker }
96*9880d681SAndroid Build Coastguard Worker }
97*9880d681SAndroid Build Coastguard Worker
EmitInstruction(const MachineInstr * MI)98*9880d681SAndroid Build Coastguard Worker void AMDGPUAsmPrinter::EmitInstruction(const MachineInstr *MI) {
99*9880d681SAndroid Build Coastguard Worker const AMDGPUSubtarget &STI = MF->getSubtarget<AMDGPUSubtarget>();
100*9880d681SAndroid Build Coastguard Worker AMDGPUMCInstLower MCInstLowering(OutContext, STI);
101*9880d681SAndroid Build Coastguard Worker
102*9880d681SAndroid Build Coastguard Worker StringRef Err;
103*9880d681SAndroid Build Coastguard Worker if (!STI.getInstrInfo()->verifyInstruction(*MI, Err)) {
104*9880d681SAndroid Build Coastguard Worker LLVMContext &C = MI->getParent()->getParent()->getFunction()->getContext();
105*9880d681SAndroid Build Coastguard Worker C.emitError("Illegal instruction detected: " + Err);
106*9880d681SAndroid Build Coastguard Worker MI->dump();
107*9880d681SAndroid Build Coastguard Worker }
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard Worker if (MI->isBundle()) {
110*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB = MI->getParent();
111*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_instr_iterator I = ++MI->getIterator();
112*9880d681SAndroid Build Coastguard Worker while (I != MBB->instr_end() && I->isInsideBundle()) {
113*9880d681SAndroid Build Coastguard Worker EmitInstruction(&*I);
114*9880d681SAndroid Build Coastguard Worker ++I;
115*9880d681SAndroid Build Coastguard Worker }
116*9880d681SAndroid Build Coastguard Worker } else {
117*9880d681SAndroid Build Coastguard Worker // We don't want SI_MASK_BRANCH/SI_RETURN encoded. They are placeholder
118*9880d681SAndroid Build Coastguard Worker // terminator instructions and should only be printed as comments.
119*9880d681SAndroid Build Coastguard Worker if (MI->getOpcode() == AMDGPU::SI_MASK_BRANCH) {
120*9880d681SAndroid Build Coastguard Worker if (isVerbose()) {
121*9880d681SAndroid Build Coastguard Worker SmallVector<char, 16> BBStr;
122*9880d681SAndroid Build Coastguard Worker raw_svector_ostream Str(BBStr);
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
125*9880d681SAndroid Build Coastguard Worker const MCSymbolRefExpr *Expr
126*9880d681SAndroid Build Coastguard Worker = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
127*9880d681SAndroid Build Coastguard Worker Expr->print(Str, MAI);
128*9880d681SAndroid Build Coastguard Worker OutStreamer->emitRawComment(" mask branch " + BBStr);
129*9880d681SAndroid Build Coastguard Worker }
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker return;
132*9880d681SAndroid Build Coastguard Worker }
133*9880d681SAndroid Build Coastguard Worker
134*9880d681SAndroid Build Coastguard Worker if (MI->getOpcode() == AMDGPU::SI_RETURN) {
135*9880d681SAndroid Build Coastguard Worker if (isVerbose())
136*9880d681SAndroid Build Coastguard Worker OutStreamer->emitRawComment(" return");
137*9880d681SAndroid Build Coastguard Worker return;
138*9880d681SAndroid Build Coastguard Worker }
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker MCInst TmpInst;
141*9880d681SAndroid Build Coastguard Worker MCInstLowering.lower(MI, TmpInst);
142*9880d681SAndroid Build Coastguard Worker EmitToStreamer(*OutStreamer, TmpInst);
143*9880d681SAndroid Build Coastguard Worker
144*9880d681SAndroid Build Coastguard Worker if (STI.dumpCode()) {
145*9880d681SAndroid Build Coastguard Worker // Disassemble instruction/operands to text.
146*9880d681SAndroid Build Coastguard Worker DisasmLines.resize(DisasmLines.size() + 1);
147*9880d681SAndroid Build Coastguard Worker std::string &DisasmLine = DisasmLines.back();
148*9880d681SAndroid Build Coastguard Worker raw_string_ostream DisasmStream(DisasmLine);
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard Worker AMDGPUInstPrinter InstPrinter(*TM.getMCAsmInfo(),
151*9880d681SAndroid Build Coastguard Worker *STI.getInstrInfo(),
152*9880d681SAndroid Build Coastguard Worker *STI.getRegisterInfo());
153*9880d681SAndroid Build Coastguard Worker InstPrinter.printInst(&TmpInst, DisasmStream, StringRef(), STI);
154*9880d681SAndroid Build Coastguard Worker
155*9880d681SAndroid Build Coastguard Worker // Disassemble instruction/operands to hex representation.
156*9880d681SAndroid Build Coastguard Worker SmallVector<MCFixup, 4> Fixups;
157*9880d681SAndroid Build Coastguard Worker SmallVector<char, 16> CodeBytes;
158*9880d681SAndroid Build Coastguard Worker raw_svector_ostream CodeStream(CodeBytes);
159*9880d681SAndroid Build Coastguard Worker
160*9880d681SAndroid Build Coastguard Worker auto &ObjStreamer = static_cast<MCObjectStreamer&>(*OutStreamer);
161*9880d681SAndroid Build Coastguard Worker MCCodeEmitter &InstEmitter = ObjStreamer.getAssembler().getEmitter();
162*9880d681SAndroid Build Coastguard Worker InstEmitter.encodeInstruction(TmpInst, CodeStream, Fixups,
163*9880d681SAndroid Build Coastguard Worker MF->getSubtarget<MCSubtargetInfo>());
164*9880d681SAndroid Build Coastguard Worker HexLines.resize(HexLines.size() + 1);
165*9880d681SAndroid Build Coastguard Worker std::string &HexLine = HexLines.back();
166*9880d681SAndroid Build Coastguard Worker raw_string_ostream HexStream(HexLine);
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Worker for (size_t i = 0; i < CodeBytes.size(); i += 4) {
169*9880d681SAndroid Build Coastguard Worker unsigned int CodeDWord = *(unsigned int *)&CodeBytes[i];
170*9880d681SAndroid Build Coastguard Worker HexStream << format("%s%08X", (i > 0 ? " " : ""), CodeDWord);
171*9880d681SAndroid Build Coastguard Worker }
172*9880d681SAndroid Build Coastguard Worker
173*9880d681SAndroid Build Coastguard Worker DisasmStream.flush();
174*9880d681SAndroid Build Coastguard Worker DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLine.size());
175*9880d681SAndroid Build Coastguard Worker }
176*9880d681SAndroid Build Coastguard Worker }
177*9880d681SAndroid Build Coastguard Worker }
178