blob: ae4c32c258a7493a240a3521b87ef7f8bdf4edd9 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===- AMDGPUMCInstLower.cpp - Lower AMDGPU MachineInstr to an MCInst -----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tom Stellard75aadc22012-12-11 21:25:42 +00006//
7//===----------------------------------------------------------------------===//
8//
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// Code to lower AMDGPU MachineInstrs to their corresponding MCInst.
Tom Stellard75aadc22012-12-11 21:25:42 +000011//
12//===----------------------------------------------------------------------===//
13//
14
Tom Stellard75aadc22012-12-11 21:25:42 +000015#include "AMDGPUAsmPrinter.h"
Matt Arsenault43e92fe2016-06-24 06:30:11 +000016#include "AMDGPUSubtarget.h"
Tom Stellard2e59a452014-06-13 01:32:00 +000017#include "AMDGPUTargetMachine.h"
Richard Trieuc0bd7bd2019-05-11 00:03:35 +000018#include "MCTargetDesc/AMDGPUInstPrinter.h"
Tom Stellard44b30b42018-05-22 02:03:23 +000019#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Tom Stellardc5015012018-05-24 20:02:01 +000020#include "R600AsmPrinter.h"
Tom Stellardc721a232014-05-16 20:56:47 +000021#include "SIInstrInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
23#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
Marek Olsaka93603d2015-01-15 18:42:51 +000025#include "llvm/IR/Function.h"
Tom Stellard067c8152014-07-21 14:01:14 +000026#include "llvm/IR/GlobalVariable.h"
Tom Stellarded699252013-10-12 05:02:51 +000027#include "llvm/MC/MCCodeEmitter.h"
Tom Stellard067c8152014-07-21 14:01:14 +000028#include "llvm/MC/MCContext.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000029#include "llvm/MC/MCExpr.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000030#include "llvm/MC/MCInst.h"
Tom Stellarded699252013-10-12 05:02:51 +000031#include "llvm/MC/MCObjectStreamer.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000032#include "llvm/MC/MCStreamer.h"
33#include "llvm/Support/ErrorHandling.h"
Tom Stellarded699252013-10-12 05:02:51 +000034#include "llvm/Support/Format.h"
35#include <algorithm>
Tom Stellard75aadc22012-12-11 21:25:42 +000036
37using namespace llvm;
38
Tom Stellard79fffe32018-05-25 04:57:02 +000039namespace {
40
41class AMDGPUMCInstLower {
42 MCContext &Ctx;
Tom Stellard57b93422018-05-29 17:41:59 +000043 const TargetSubtargetInfo &ST;
Tom Stellard79fffe32018-05-25 04:57:02 +000044 const AsmPrinter &AP;
45
46 const MCExpr *getLongBranchBlockExpr(const MachineBasicBlock &SrcBB,
47 const MachineOperand &MO) const;
48
49public:
Tom Stellard57b93422018-05-29 17:41:59 +000050 AMDGPUMCInstLower(MCContext &ctx, const TargetSubtargetInfo &ST,
Tom Stellard79fffe32018-05-25 04:57:02 +000051 const AsmPrinter &AP);
52
53 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
54
55 /// Lower a MachineInstr to an MCInst
56 void lower(const MachineInstr *MI, MCInst &OutMI) const;
57
58};
59
Tom Stellard57b93422018-05-29 17:41:59 +000060class R600MCInstLower : public AMDGPUMCInstLower {
61public:
62 R600MCInstLower(MCContext &ctx, const R600Subtarget &ST,
63 const AsmPrinter &AP);
64
65 /// Lower a MachineInstr to an MCInst
66 void lower(const MachineInstr *MI, MCInst &OutMI) const;
67};
68
69
Tom Stellard79fffe32018-05-25 04:57:02 +000070} // End anonymous namespace
71
Matt Arsenault11f74022016-10-06 17:19:11 +000072#include "AMDGPUGenMCPseudoLowering.inc"
73
Tom Stellard57b93422018-05-29 17:41:59 +000074AMDGPUMCInstLower::AMDGPUMCInstLower(MCContext &ctx,
75 const TargetSubtargetInfo &st,
Tom Stellard1b9748c2016-09-26 17:29:25 +000076 const AsmPrinter &ap):
77 Ctx(ctx), ST(st), AP(ap) { }
Tom Stellard75aadc22012-12-11 21:25:42 +000078
Tom Stellard418beb72016-07-13 14:23:33 +000079static MCSymbolRefExpr::VariantKind getVariantKind(unsigned MOFlags) {
80 switch (MOFlags) {
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +000081 default:
82 return MCSymbolRefExpr::VK_None;
83 case SIInstrInfo::MO_GOTPCREL:
84 return MCSymbolRefExpr::VK_GOTPCREL;
85 case SIInstrInfo::MO_GOTPCREL32_LO:
86 return MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_LO;
87 case SIInstrInfo::MO_GOTPCREL32_HI:
88 return MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_HI;
89 case SIInstrInfo::MO_REL32_LO:
90 return MCSymbolRefExpr::VK_AMDGPU_REL32_LO;
91 case SIInstrInfo::MO_REL32_HI:
92 return MCSymbolRefExpr::VK_AMDGPU_REL32_HI;
Nicolai Haehnle41abf272019-06-16 17:43:37 +000093 case SIInstrInfo::MO_ABS32_LO:
94 return MCSymbolRefExpr::VK_AMDGPU_ABS32_LO;
95 case SIInstrInfo::MO_ABS32_HI:
96 return MCSymbolRefExpr::VK_AMDGPU_ABS32_HI;
Tom Stellard418beb72016-07-13 14:23:33 +000097 }
98}
99
Matt Arsenault6bc43d82016-10-06 16:20:41 +0000100const MCExpr *AMDGPUMCInstLower::getLongBranchBlockExpr(
101 const MachineBasicBlock &SrcBB,
102 const MachineOperand &MO) const {
103 const MCExpr *DestBBSym
104 = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx);
105 const MCExpr *SrcBBSym = MCSymbolRefExpr::create(SrcBB.getSymbol(), Ctx);
106
Matt Arsenaultf84ce752019-04-22 19:14:26 +0000107 // FIXME: The first half of this assert should be removed. This should
108 // probably be PC relative instead of using the source block symbol, and
109 // therefore the indirect branch expansion should use a bundle.
110 assert(
111 skipDebugInstructionsForward(SrcBB.begin(), SrcBB.end())->getOpcode() ==
112 AMDGPU::S_GETPC_B64 &&
113 ST.getInstrInfo()->get(AMDGPU::S_GETPC_B64).Size == 4);
Matt Arsenault6bc43d82016-10-06 16:20:41 +0000114
115 // s_getpc_b64 returns the address of next instruction.
116 const MCConstantExpr *One = MCConstantExpr::create(4, Ctx);
117 SrcBBSym = MCBinaryExpr::createAdd(SrcBBSym, One, Ctx);
118
Matt Arsenault0f8a7642019-06-05 20:32:25 +0000119 if (MO.getTargetFlags() == SIInstrInfo::MO_LONG_BRANCH_FORWARD)
Matt Arsenault6bc43d82016-10-06 16:20:41 +0000120 return MCBinaryExpr::createSub(DestBBSym, SrcBBSym, Ctx);
121
Matt Arsenault0f8a7642019-06-05 20:32:25 +0000122 assert(MO.getTargetFlags() == SIInstrInfo::MO_LONG_BRANCH_BACKWARD);
Matt Arsenault6bc43d82016-10-06 16:20:41 +0000123 return MCBinaryExpr::createSub(SrcBBSym, DestBBSym, Ctx);
124}
125
Matt Arsenault11f74022016-10-06 17:19:11 +0000126bool AMDGPUMCInstLower::lowerOperand(const MachineOperand &MO,
127 MCOperand &MCOp) const {
128 switch (MO.getType()) {
129 default:
130 llvm_unreachable("unknown operand type");
131 case MachineOperand::MO_Immediate:
132 MCOp = MCOperand::createImm(MO.getImm());
133 return true;
134 case MachineOperand::MO_Register:
135 MCOp = MCOperand::createReg(AMDGPU::getMCReg(MO.getReg(), ST));
136 return true;
137 case MachineOperand::MO_MachineBasicBlock: {
138 if (MO.getTargetFlags() != 0) {
139 MCOp = MCOperand::createExpr(
140 getLongBranchBlockExpr(*MO.getParent()->getParent(), MO));
141 } else {
142 MCOp = MCOperand::createExpr(
143 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
144 }
145
146 return true;
147 }
148 case MachineOperand::MO_GlobalAddress: {
149 const GlobalValue *GV = MO.getGlobal();
150 SmallString<128> SymbolName;
151 AP.getNameWithPrefix(SymbolName, GV);
152 MCSymbol *Sym = Ctx.getOrCreateSymbol(SymbolName);
Nicolai Haehnle41abf272019-06-16 17:43:37 +0000153 const MCExpr *Expr =
Matt Arsenault11f74022016-10-06 17:19:11 +0000154 MCSymbolRefExpr::create(Sym, getVariantKind(MO.getTargetFlags()),Ctx);
Nicolai Haehnle41abf272019-06-16 17:43:37 +0000155 int64_t Offset = MO.getOffset();
156 if (Offset != 0) {
157 Expr = MCBinaryExpr::createAdd(Expr,
158 MCConstantExpr::create(Offset, Ctx), Ctx);
159 }
Matt Arsenault11f74022016-10-06 17:19:11 +0000160 MCOp = MCOperand::createExpr(Expr);
161 return true;
162 }
163 case MachineOperand::MO_ExternalSymbol: {
164 MCSymbol *Sym = Ctx.getOrCreateSymbol(StringRef(MO.getSymbolName()));
165 Sym->setExternal(true);
166 const MCSymbolRefExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
167 MCOp = MCOperand::createExpr(Expr);
168 return true;
169 }
Matt Arsenaultb62a4eb2017-08-01 19:54:18 +0000170 case MachineOperand::MO_RegisterMask:
171 // Regmasks are like implicit defs.
172 return false;
Matt Arsenault11f74022016-10-06 17:19:11 +0000173 }
174}
175
Tom Stellard75aadc22012-12-11 21:25:42 +0000176void AMDGPUMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
Matt Arsenault2b1f9aa2017-05-17 21:56:25 +0000177 unsigned Opcode = MI->getOpcode();
Tom Stellard57b93422018-05-29 17:41:59 +0000178 const auto *TII = static_cast<const SIInstrInfo*>(ST.getInstrInfo());
Tom Stellardc721a232014-05-16 20:56:47 +0000179
Matt Arsenault2b1f9aa2017-05-17 21:56:25 +0000180 // FIXME: Should be able to handle this with emitPseudoExpansionLowering. We
181 // need to select it to the subtarget specific version, and there's no way to
182 // do that with a single pseudo source operation.
183 if (Opcode == AMDGPU::S_SETPC_B64_return)
184 Opcode = AMDGPU::S_SETPC_B64;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000185 else if (Opcode == AMDGPU::SI_CALL) {
186 // SI_CALL is just S_SWAPPC_B64 with an additional operand to track the
Matt Arsenault1d6317c2017-08-02 01:42:04 +0000187 // called function (which we need to remove here).
188 OutMI.setOpcode(TII->pseudoToMCOpcode(AMDGPU::S_SWAPPC_B64));
189 MCOperand Dest, Src;
190 lowerOperand(MI->getOperand(0), Dest);
191 lowerOperand(MI->getOperand(1), Src);
192 OutMI.addOperand(Dest);
193 OutMI.addOperand(Src);
194 return;
Matt Arsenault71bcbd42017-08-11 20:42:08 +0000195 } else if (Opcode == AMDGPU::SI_TCRETURN) {
196 // TODO: How to use branch immediate and avoid register+add?
197 Opcode = AMDGPU::S_SETPC_B64;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000198 }
Marek Olsaka93603d2015-01-15 18:42:51 +0000199
Matt Arsenault1d6317c2017-08-02 01:42:04 +0000200 int MCOpcode = TII->pseudoToMCOpcode(Opcode);
Marek Olsaka93603d2015-01-15 18:42:51 +0000201 if (MCOpcode == -1) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000202 LLVMContext &C = MI->getParent()->getParent()->getFunction().getContext();
Marek Olsaka93603d2015-01-15 18:42:51 +0000203 C.emitError("AMDGPUMCInstLower::lower - Pseudo instruction doesn't have "
204 "a target-specific version: " + Twine(MI->getOpcode()));
205 }
206
207 OutMI.setOpcode(MCOpcode);
Tom Stellard75aadc22012-12-11 21:25:42 +0000208
David Blaikie2f771122014-04-05 22:42:04 +0000209 for (const MachineOperand &MO : MI->explicit_operands()) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000210 MCOperand MCOp;
Matt Arsenault11f74022016-10-06 17:19:11 +0000211 lowerOperand(MO, MCOp);
Tom Stellard75aadc22012-12-11 21:25:42 +0000212 OutMI.addOperand(MCOp);
213 }
214}
215
Matt Arsenault11f74022016-10-06 17:19:11 +0000216bool AMDGPUAsmPrinter::lowerOperand(const MachineOperand &MO,
217 MCOperand &MCOp) const {
Tom Stellard5bfbae52018-07-11 20:59:01 +0000218 const GCNSubtarget &STI = MF->getSubtarget<GCNSubtarget>();
Matt Arsenault11f74022016-10-06 17:19:11 +0000219 AMDGPUMCInstLower MCInstLowering(OutContext, STI, *this);
220 return MCInstLowering.lowerOperand(MO, MCOp);
221}
222
Tom Stellardc5015012018-05-24 20:02:01 +0000223static const MCExpr *lowerAddrSpaceCast(const TargetMachine &TM,
224 const Constant *CV,
225 MCContext &OutContext) {
Yaxun Liu8f844f32017-02-07 00:43:21 +0000226 // TargetMachine does not support llvm-style cast. Use C++-style cast.
227 // This is safe since TM is always of type AMDGPUTargetMachine or its
228 // derived class.
Tom Stellardc5015012018-05-24 20:02:01 +0000229 auto &AT = static_cast<const AMDGPUTargetMachine&>(TM);
Yaxun Liu8f844f32017-02-07 00:43:21 +0000230 auto *CE = dyn_cast<ConstantExpr>(CV);
231
232 // Lower null pointers in private and local address space.
233 // Clang generates addrspacecast for null pointers in private and local
234 // address space, which needs to be lowered.
235 if (CE && CE->getOpcode() == Instruction::AddrSpaceCast) {
236 auto Op = CE->getOperand(0);
237 auto SrcAddr = Op->getType()->getPointerAddressSpace();
Tom Stellardc5015012018-05-24 20:02:01 +0000238 if (Op->isNullValue() && AT.getNullPointerValue(SrcAddr) == 0) {
Yaxun Liu8f844f32017-02-07 00:43:21 +0000239 auto DstAddr = CE->getType()->getPointerAddressSpace();
Tom Stellardc5015012018-05-24 20:02:01 +0000240 return MCConstantExpr::create(AT.getNullPointerValue(DstAddr),
Yaxun Liu8f844f32017-02-07 00:43:21 +0000241 OutContext);
242 }
243 }
Tom Stellardc5015012018-05-24 20:02:01 +0000244 return nullptr;
245}
246
247const MCExpr *AMDGPUAsmPrinter::lowerConstant(const Constant *CV) {
248 if (const MCExpr *E = lowerAddrSpaceCast(TM, CV, OutContext))
249 return E;
Yaxun Liu8f844f32017-02-07 00:43:21 +0000250 return AsmPrinter::lowerConstant(CV);
251}
252
Tom Stellard75aadc22012-12-11 21:25:42 +0000253void AMDGPUAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Matt Arsenault11f74022016-10-06 17:19:11 +0000254 if (emitPseudoExpansionLowering(*OutStreamer, MI))
255 return;
256
Tom Stellard5bfbae52018-07-11 20:59:01 +0000257 const GCNSubtarget &STI = MF->getSubtarget<GCNSubtarget>();
Tom Stellard1b9748c2016-09-26 17:29:25 +0000258 AMDGPUMCInstLower MCInstLowering(OutContext, STI, *this);
Tom Stellard75aadc22012-12-11 21:25:42 +0000259
Tom Stellard9b9e9262014-02-28 21:36:41 +0000260 StringRef Err;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000261 if (!STI.getInstrInfo()->verifyInstruction(*MI, Err)) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000262 LLVMContext &C = MI->getParent()->getParent()->getFunction().getContext();
Michel Danzer302f83a2016-03-16 09:10:42 +0000263 C.emitError("Illegal instruction detected: " + Err);
Matthias Braun8c209aa2017-01-28 02:02:38 +0000264 MI->print(errs());
Tom Stellard9b9e9262014-02-28 21:36:41 +0000265 }
Michel Danzer302f83a2016-03-16 09:10:42 +0000266
Tom Stellard75aadc22012-12-11 21:25:42 +0000267 if (MI->isBundle()) {
268 const MachineBasicBlock *MBB = MI->getParent();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000269 MachineBasicBlock::const_instr_iterator I = ++MI->getIterator();
Duncan P. N. Exon Smitha73371a2015-10-13 20:07:10 +0000270 while (I != MBB->instr_end() && I->isInsideBundle()) {
271 EmitInstruction(&*I);
Tom Stellard75aadc22012-12-11 21:25:42 +0000272 ++I;
273 }
274 } else {
Matt Arsenault5b20fbb2017-03-21 22:18:10 +0000275 // We don't want SI_MASK_BRANCH/SI_RETURN_TO_EPILOG encoded. They are
276 // placeholder terminator instructions and should only be printed as
277 // comments.
Matt Arsenault9babdf42016-06-22 20:15:28 +0000278 if (MI->getOpcode() == AMDGPU::SI_MASK_BRANCH) {
279 if (isVerbose()) {
280 SmallVector<char, 16> BBStr;
281 raw_svector_ostream Str(BBStr);
282
Matt Arsenaulta74374a2016-07-08 00:55:44 +0000283 const MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
Matt Arsenault9babdf42016-06-22 20:15:28 +0000284 const MCSymbolRefExpr *Expr
285 = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
286 Expr->print(Str, MAI);
Reid Klecknerc18c12e2017-10-11 23:53:36 +0000287 OutStreamer->emitRawComment(Twine(" mask branch ") + BBStr);
Matt Arsenault9babdf42016-06-22 20:15:28 +0000288 }
289
290 return;
291 }
292
Matt Arsenault5b20fbb2017-03-21 22:18:10 +0000293 if (MI->getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG) {
Matt Arsenault9babdf42016-06-22 20:15:28 +0000294 if (isVerbose())
Matt Arsenault5b20fbb2017-03-21 22:18:10 +0000295 OutStreamer->emitRawComment(" return to shader part epilog");
Matt Arsenault9babdf42016-06-22 20:15:28 +0000296 return;
297 }
298
Stanislav Mekhanoshinea91cca2016-11-15 19:00:15 +0000299 if (MI->getOpcode() == AMDGPU::WAVE_BARRIER) {
300 if (isVerbose())
301 OutStreamer->emitRawComment(" wave barrier");
302 return;
303 }
304
Yaxun Liu15a96b12017-04-21 19:32:02 +0000305 if (MI->getOpcode() == AMDGPU::SI_MASKED_UNREACHABLE) {
306 if (isVerbose())
307 OutStreamer->emitRawComment(" divergent unreachable");
308 return;
309 }
310
Tom Stellard75aadc22012-12-11 21:25:42 +0000311 MCInst TmpInst;
312 MCInstLowering.lower(MI, TmpInst);
Lang Hames9ff69c82015-04-24 19:11:51 +0000313 EmitToStreamer(*OutStreamer, TmpInst);
Tom Stellarded699252013-10-12 05:02:51 +0000314
Nicolai Haehnle283b9952018-08-29 07:46:09 +0000315#ifdef EXPENSIVE_CHECKS
316 // Sanity-check getInstSizeInBytes on explicitly specified CPUs (it cannot
317 // work correctly for the generic CPU).
318 //
319 // The isPseudo check really shouldn't be here, but unfortunately there are
320 // some negative lit tests that depend on being able to continue through
321 // here even when pseudo instructions haven't been lowered.
322 if (!MI->isPseudo() && STI.isCPUStringValid(STI.getCPU())) {
323 SmallVector<MCFixup, 4> Fixups;
324 SmallVector<char, 16> CodeBytes;
325 raw_svector_ostream CodeStream(CodeBytes);
326
327 std::unique_ptr<MCCodeEmitter> InstEmitter(createSIMCCodeEmitter(
328 *STI.getInstrInfo(), *OutContext.getRegisterInfo(), OutContext));
329 InstEmitter->encodeInstruction(TmpInst, CodeStream, Fixups, STI);
330
331 assert(CodeBytes.size() == STI.getInstrInfo()->getInstSizeInBytes(*MI));
332 }
333#endif
334
Tim Renouf33cb8f52019-05-14 16:17:14 +0000335 if (DumpCodeInstEmitter) {
336 // Disassemble instruction/operands to text
Tom Stellarded699252013-10-12 05:02:51 +0000337 DisasmLines.resize(DisasmLines.size() + 1);
338 std::string &DisasmLine = DisasmLines.back();
339 raw_string_ostream DisasmStream(DisasmLine);
340
Tim Renouf33cb8f52019-05-14 16:17:14 +0000341 AMDGPUInstPrinter InstPrinter(*TM.getMCAsmInfo(), *STI.getInstrInfo(),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000342 *STI.getRegisterInfo());
343 InstPrinter.printInst(&TmpInst, DisasmStream, StringRef(), STI);
Tom Stellarded699252013-10-12 05:02:51 +0000344
345 // Disassemble instruction/operands to hex representation.
346 SmallVector<MCFixup, 4> Fixups;
347 SmallVector<char, 16> CodeBytes;
348 raw_svector_ostream CodeStream(CodeBytes);
349
Tim Renouf33cb8f52019-05-14 16:17:14 +0000350 DumpCodeInstEmitter->encodeInstruction(
351 TmpInst, CodeStream, Fixups, MF->getSubtarget<MCSubtargetInfo>());
Tom Stellarded699252013-10-12 05:02:51 +0000352 HexLines.resize(HexLines.size() + 1);
353 std::string &HexLine = HexLines.back();
354 raw_string_ostream HexStream(HexLine);
355
356 for (size_t i = 0; i < CodeBytes.size(); i += 4) {
357 unsigned int CodeDWord = *(unsigned int *)&CodeBytes[i];
358 HexStream << format("%s%08X", (i > 0 ? " " : ""), CodeDWord);
359 }
360
361 DisasmStream.flush();
362 DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLine.size());
363 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000364 }
365}
Tom Stellardc5015012018-05-24 20:02:01 +0000366
Tom Stellard57b93422018-05-29 17:41:59 +0000367R600MCInstLower::R600MCInstLower(MCContext &Ctx, const R600Subtarget &ST,
368 const AsmPrinter &AP) :
369 AMDGPUMCInstLower(Ctx, ST, AP) { }
370
371void R600MCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
372 OutMI.setOpcode(MI->getOpcode());
373 for (const MachineOperand &MO : MI->explicit_operands()) {
374 MCOperand MCOp;
375 lowerOperand(MO, MCOp);
376 OutMI.addOperand(MCOp);
377 }
378}
379
Tom Stellardc5015012018-05-24 20:02:01 +0000380void R600AsmPrinter::EmitInstruction(const MachineInstr *MI) {
381 const R600Subtarget &STI = MF->getSubtarget<R600Subtarget>();
Tom Stellard57b93422018-05-29 17:41:59 +0000382 R600MCInstLower MCInstLowering(OutContext, STI, *this);
Tom Stellardc5015012018-05-24 20:02:01 +0000383
384 StringRef Err;
385 if (!STI.getInstrInfo()->verifyInstruction(*MI, Err)) {
386 LLVMContext &C = MI->getParent()->getParent()->getFunction().getContext();
387 C.emitError("Illegal instruction detected: " + Err);
388 MI->print(errs());
389 }
390
391 if (MI->isBundle()) {
392 const MachineBasicBlock *MBB = MI->getParent();
393 MachineBasicBlock::const_instr_iterator I = ++MI->getIterator();
394 while (I != MBB->instr_end() && I->isInsideBundle()) {
395 EmitInstruction(&*I);
396 ++I;
397 }
398 } else {
399 MCInst TmpInst;
400 MCInstLowering.lower(MI, TmpInst);
401 EmitToStreamer(*OutStreamer, TmpInst);
402 }
403}
404
405const MCExpr *R600AsmPrinter::lowerConstant(const Constant *CV) {
406 if (const MCExpr *E = lowerAddrSpaceCast(TM, CV, OutContext))
407 return E;
408 return AsmPrinter::lowerConstant(CV);
409}