1 //===- AMDGPUMCInstLower.cpp - Lower AMDGPU MachineInstr to an MCInst -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// \brief Code to lower AMDGPU MachineInstrs to their corresponding MCInst.
12 //
13 //===----------------------------------------------------------------------===//
14 //
15 
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"
35 #include <algorithm>
36 
37 using namespace llvm;
38 
39 #include "AMDGPUGenMCPseudoLowering.inc"
40 
41 
42 AMDGPUMCInstLower::AMDGPUMCInstLower(MCContext &ctx, const AMDGPUSubtarget &st,
43                                      const AsmPrinter &ap):
44   Ctx(ctx), ST(st), AP(ap) { }
45 
46 static MCSymbolRefExpr::VariantKind getVariantKind(unsigned MOFlags) {
47   switch (MOFlags) {
48   default:
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;
60   }
61 }
62 
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);
69 
70   assert(SrcBB.front().getOpcode() == AMDGPU::S_GETPC_B64 &&
71          ST.getInstrInfo()->get(AMDGPU::S_GETPC_B64).Size == 4);
72 
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);
76 
77   if (MO.getTargetFlags() == AMDGPU::TF_LONG_BRANCH_FORWARD)
78     return MCBinaryExpr::createSub(DestBBSym, SrcBBSym, Ctx);
79 
80   assert(MO.getTargetFlags() == AMDGPU::TF_LONG_BRANCH_BACKWARD);
81   return MCBinaryExpr::createSub(SrcBBSym, DestBBSym, Ctx);
82 }
83 
84 bool AMDGPUMCInstLower::lowerOperand(const MachineOperand &MO,
85                                      MCOperand &MCOp) const {
86   switch (MO.getType()) {
87   default:
88     llvm_unreachable("unknown operand type");
89   case MachineOperand::MO_Immediate:
90     MCOp = MCOperand::createImm(MO.getImm());
91     return true;
92   case MachineOperand::MO_Register:
93     MCOp = MCOperand::createReg(AMDGPU::getMCReg(MO.getReg(), ST));
94     return true;
95   case MachineOperand::MO_MachineBasicBlock: {
96     if (MO.getTargetFlags() != 0) {
97       MCOp = MCOperand::createExpr(
98         getLongBranchBlockExpr(*MO.getParent()->getParent(), MO));
99     } else {
100       MCOp = MCOperand::createExpr(
101         MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
102     }
103 
104     return true;
105   }
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);
116     return true;
117   }
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);
123     return true;
124   }
125   }
126 }
127 
128 void AMDGPUMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
129 
130   int MCOpcode = ST.getInstrInfo()->pseudoToMCOpcode(MI->getOpcode());
131 
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()));
136   }
137 
138   OutMI.setOpcode(MCOpcode);
139 
140   for (const MachineOperand &MO : MI->explicit_operands()) {
141     MCOperand MCOp;
142     lowerOperand(MO, MCOp);
143     OutMI.addOperand(MCOp);
144   }
145 }
146 
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);
152 }
153 
154 void AMDGPUAsmPrinter::EmitInstruction(const MachineInstr *MI) {
155   if (emitPseudoExpansionLowering(*OutStreamer, MI))
156     return;
157 
158   const AMDGPUSubtarget &STI = MF->getSubtarget<AMDGPUSubtarget>();
159   AMDGPUMCInstLower MCInstLowering(OutContext, STI, *this);
160 
161   StringRef Err;
162   if (!STI.getInstrInfo()->verifyInstruction(*MI, Err)) {
163     LLVMContext &C = MI->getParent()->getParent()->getFunction()->getContext();
164     C.emitError("Illegal instruction detected: " + Err);
165     MI->dump();
166   }
167 
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);
173       ++I;
174     }
175   } else {
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) {
179       if (isVerbose()) {
180         SmallVector<char, 16> BBStr;
181         raw_svector_ostream Str(BBStr);
182 
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);
188       }
189 
190       return;
191     }
192 
193     if (MI->getOpcode() == AMDGPU::SI_RETURN) {
194       if (isVerbose())
195         OutStreamer->emitRawComment(" return");
196       return;
197     }
198 
199     MCInst TmpInst;
200     MCInstLowering.lower(MI, TmpInst);
201     EmitToStreamer(*OutStreamer, TmpInst);
202 
203     if (STI.dumpCode()) {
204       // Disassemble instruction/operands to text.
205       DisasmLines.resize(DisasmLines.size() + 1);
206       std::string &DisasmLine = DisasmLines.back();
207       raw_string_ostream DisasmStream(DisasmLine);
208 
209       AMDGPUInstPrinter InstPrinter(*TM.getMCAsmInfo(),
210                                     *STI.getInstrInfo(),
211                                     *STI.getRegisterInfo());
212       InstPrinter.printInst(&TmpInst, DisasmStream, StringRef(), STI);
213 
214       // Disassemble instruction/operands to hex representation.
215       SmallVector<MCFixup, 4> Fixups;
216       SmallVector<char, 16> CodeBytes;
217       raw_svector_ostream CodeStream(CodeBytes);
218 
219       auto &ObjStreamer = static_cast<MCObjectStreamer&>(*OutStreamer);
220       MCCodeEmitter &InstEmitter = ObjStreamer.getAssembler().getEmitter();
221       InstEmitter.encodeInstruction(TmpInst, CodeStream, Fixups,
222                                     MF->getSubtarget<MCSubtargetInfo>());
223       HexLines.resize(HexLines.size() + 1);
224       std::string &HexLine = HexLines.back();
225       raw_string_ostream HexStream(HexLine);
226 
227       for (size_t i = 0; i < CodeBytes.size(); i += 4) {
228         unsigned int CodeDWord = *(unsigned int *)&CodeBytes[i];
229         HexStream << format("%s%08X", (i > 0 ? " " : ""), CodeDWord);
230       }
231 
232       DisasmStream.flush();
233       DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLine.size());
234     }
235   }
236 }
237