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 "AMDGPUTargetMachine.h"
19 #include "InstPrinter/AMDGPUInstPrinter.h"
20 #include "R600InstrInfo.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 AMDGPUMCInstLower::AMDGPUMCInstLower(MCContext &ctx, const AMDGPUSubtarget &st):
43 void AMDGPUMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
45 int MCOpcode = ST.getInstrInfo()->pseudoToMCOpcode(MI->getOpcode());
48 LLVMContext &C = MI->getParent()->getParent()->getFunction()->getContext();
49 C.emitError("AMDGPUMCInstLower::lower - Pseudo instruction doesn't have "
50 "a target-specific version: " + Twine(MI->getOpcode()));
53 OutMI.setOpcode(MCOpcode);
55 for (const MachineOperand &MO : MI->explicit_operands()) {
57 switch (MO.getType()) {
59 llvm_unreachable("unknown operand type");
60 case MachineOperand::MO_Immediate:
61 MCOp = MCOperand::createImm(MO.getImm());
63 case MachineOperand::MO_Register:
64 MCOp = MCOperand::createReg(MO.getReg());
66 case MachineOperand::MO_MachineBasicBlock:
67 MCOp = MCOperand::createExpr(MCSymbolRefExpr::create(
68 MO.getMBB()->getSymbol(), Ctx));
70 case MachineOperand::MO_GlobalAddress: {
71 const GlobalValue *GV = MO.getGlobal();
72 MCSymbol *Sym = Ctx.getOrCreateSymbol(StringRef(GV->getName()));
73 MCOp = MCOperand::createExpr(MCSymbolRefExpr::create(Sym, Ctx));
76 case MachineOperand::MO_ExternalSymbol: {
77 MCSymbol *Sym = Ctx.getOrCreateSymbol(StringRef(MO.getSymbolName()));
78 const MCSymbolRefExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
79 MCOp = MCOperand::createExpr(Expr);
83 OutMI.addOperand(MCOp);
87 void AMDGPUAsmPrinter::EmitInstruction(const MachineInstr *MI) {
88 const AMDGPUSubtarget &STI = MF->getSubtarget<AMDGPUSubtarget>();
89 AMDGPUMCInstLower MCInstLowering(OutContext, STI);
93 if (!STI.getInstrInfo()->verifyInstruction(MI, Err)) {
94 errs() << "Warning: Illegal instruction detected: " << Err << "\n";
99 const MachineBasicBlock *MBB = MI->getParent();
100 MachineBasicBlock::const_instr_iterator I = ++MI->getIterator();
101 while (I != MBB->instr_end() && I->isInsideBundle()) {
102 EmitInstruction(&*I);
107 MCInstLowering.lower(MI, TmpInst);
108 EmitToStreamer(*OutStreamer, TmpInst);
110 if (STI.dumpCode()) {
111 // Disassemble instruction/operands to text.
112 DisasmLines.resize(DisasmLines.size() + 1);
113 std::string &DisasmLine = DisasmLines.back();
114 raw_string_ostream DisasmStream(DisasmLine);
116 AMDGPUInstPrinter InstPrinter(*TM.getMCAsmInfo(),
117 *MF->getSubtarget().getInstrInfo(),
118 *MF->getSubtarget().getRegisterInfo());
119 InstPrinter.printInst(&TmpInst, DisasmStream, StringRef(),
122 // Disassemble instruction/operands to hex representation.
123 SmallVector<MCFixup, 4> Fixups;
124 SmallVector<char, 16> CodeBytes;
125 raw_svector_ostream CodeStream(CodeBytes);
127 auto &ObjStreamer = static_cast<MCObjectStreamer&>(*OutStreamer);
128 MCCodeEmitter &InstEmitter = ObjStreamer.getAssembler().getEmitter();
129 InstEmitter.encodeInstruction(TmpInst, CodeStream, Fixups,
130 MF->getSubtarget<MCSubtargetInfo>());
131 HexLines.resize(HexLines.size() + 1);
132 std::string &HexLine = HexLines.back();
133 raw_string_ostream HexStream(HexLine);
135 for (size_t i = 0; i < CodeBytes.size(); i += 4) {
136 unsigned int CodeDWord = *(unsigned int *)&CodeBytes[i];
137 HexStream << format("%s%08X", (i > 0 ? " " : ""), CodeDWord);
140 DisasmStream.flush();
141 DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLine.size());