145bb48eaSTom Stellard //===- AMDGPUMCInstLower.cpp - Lower AMDGPU MachineInstr to an MCInst -----===//
245bb48eaSTom Stellard //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
645bb48eaSTom Stellard //
745bb48eaSTom Stellard //===----------------------------------------------------------------------===//
845bb48eaSTom Stellard //
945bb48eaSTom Stellard /// \file
105f8f34e4SAdrian Prantl /// Code to lower AMDGPU MachineInstrs to their corresponding MCInst.
1145bb48eaSTom Stellard //
1245bb48eaSTom Stellard //===----------------------------------------------------------------------===//
1345bb48eaSTom Stellard //
1445bb48eaSTom Stellard 
1545bb48eaSTom Stellard #include "AMDGPUAsmPrinter.h"
1643e92fe3SMatt Arsenault #include "AMDGPUSubtarget.h"
1745bb48eaSTom Stellard #include "AMDGPUTargetMachine.h"
18c0bd7bd4SRichard Trieu #include "MCTargetDesc/AMDGPUInstPrinter.h"
1944b30b45STom Stellard #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
20c5015010STom Stellard #include "R600AsmPrinter.h"
2145bb48eaSTom Stellard #include "SIInstrInfo.h"
2245bb48eaSTom Stellard #include "llvm/CodeGen/MachineBasicBlock.h"
2345bb48eaSTom Stellard #include "llvm/CodeGen/MachineInstr.h"
2445bb48eaSTom Stellard #include "llvm/IR/Constants.h"
2545bb48eaSTom Stellard #include "llvm/IR/Function.h"
2645bb48eaSTom Stellard #include "llvm/IR/GlobalVariable.h"
2745bb48eaSTom Stellard #include "llvm/MC/MCCodeEmitter.h"
2845bb48eaSTom Stellard #include "llvm/MC/MCContext.h"
2945bb48eaSTom Stellard #include "llvm/MC/MCExpr.h"
3045bb48eaSTom Stellard #include "llvm/MC/MCInst.h"
3145bb48eaSTom Stellard #include "llvm/MC/MCObjectStreamer.h"
3245bb48eaSTom Stellard #include "llvm/MC/MCStreamer.h"
3345bb48eaSTom Stellard #include "llvm/Support/ErrorHandling.h"
3445bb48eaSTom Stellard #include "llvm/Support/Format.h"
3545bb48eaSTom Stellard #include <algorithm>
3645bb48eaSTom Stellard 
3745bb48eaSTom Stellard using namespace llvm;
3845bb48eaSTom Stellard 
3979fffe35STom Stellard namespace {
4079fffe35STom Stellard 
4179fffe35STom Stellard class AMDGPUMCInstLower {
4279fffe35STom Stellard   MCContext &Ctx;
4357b9342cSTom Stellard   const TargetSubtargetInfo &ST;
4479fffe35STom Stellard   const AsmPrinter &AP;
4579fffe35STom Stellard 
4679fffe35STom Stellard   const MCExpr *getLongBranchBlockExpr(const MachineBasicBlock &SrcBB,
4779fffe35STom Stellard                                        const MachineOperand &MO) const;
4879fffe35STom Stellard 
4979fffe35STom Stellard public:
5057b9342cSTom Stellard   AMDGPUMCInstLower(MCContext &ctx, const TargetSubtargetInfo &ST,
5179fffe35STom Stellard                     const AsmPrinter &AP);
5279fffe35STom Stellard 
5379fffe35STom Stellard   bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
5479fffe35STom Stellard 
5579fffe35STom Stellard   /// Lower a MachineInstr to an MCInst
5679fffe35STom Stellard   void lower(const MachineInstr *MI, MCInst &OutMI) const;
5779fffe35STom Stellard 
5879fffe35STom Stellard };
5979fffe35STom Stellard 
6057b9342cSTom Stellard class R600MCInstLower : public AMDGPUMCInstLower {
6157b9342cSTom Stellard public:
6257b9342cSTom Stellard   R600MCInstLower(MCContext &ctx, const R600Subtarget &ST,
6357b9342cSTom Stellard                   const AsmPrinter &AP);
6457b9342cSTom Stellard 
6557b9342cSTom Stellard   /// Lower a MachineInstr to an MCInst
6657b9342cSTom Stellard   void lower(const MachineInstr *MI, MCInst &OutMI) const;
6757b9342cSTom Stellard };
6857b9342cSTom Stellard 
6957b9342cSTom Stellard 
7079fffe35STom Stellard } // End anonymous namespace
7179fffe35STom Stellard 
7211f74020SMatt Arsenault #include "AMDGPUGenMCPseudoLowering.inc"
7311f74020SMatt Arsenault 
7457b9342cSTom Stellard AMDGPUMCInstLower::AMDGPUMCInstLower(MCContext &ctx,
7557b9342cSTom Stellard                                      const TargetSubtargetInfo &st,
761b9748c6STom Stellard                                      const AsmPrinter &ap):
771b9748c6STom Stellard   Ctx(ctx), ST(st), AP(ap) { }
7845bb48eaSTom Stellard 
79418beb76STom Stellard static MCSymbolRefExpr::VariantKind getVariantKind(unsigned MOFlags) {
80418beb76STom Stellard   switch (MOFlags) {
81c96b5d70SKonstantin Zhuravlyov   default:
82c96b5d70SKonstantin Zhuravlyov     return MCSymbolRefExpr::VK_None;
83c96b5d70SKonstantin Zhuravlyov   case SIInstrInfo::MO_GOTPCREL:
84c96b5d70SKonstantin Zhuravlyov     return MCSymbolRefExpr::VK_GOTPCREL;
85c96b5d70SKonstantin Zhuravlyov   case SIInstrInfo::MO_GOTPCREL32_LO:
86c96b5d70SKonstantin Zhuravlyov     return MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_LO;
87c96b5d70SKonstantin Zhuravlyov   case SIInstrInfo::MO_GOTPCREL32_HI:
88c96b5d70SKonstantin Zhuravlyov     return MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_HI;
89c96b5d70SKonstantin Zhuravlyov   case SIInstrInfo::MO_REL32_LO:
90c96b5d70SKonstantin Zhuravlyov     return MCSymbolRefExpr::VK_AMDGPU_REL32_LO;
91c96b5d70SKonstantin Zhuravlyov   case SIInstrInfo::MO_REL32_HI:
92c96b5d70SKonstantin Zhuravlyov     return MCSymbolRefExpr::VK_AMDGPU_REL32_HI;
93418beb76STom Stellard   }
94418beb76STom Stellard }
95418beb76STom Stellard 
966bc43d86SMatt Arsenault const MCExpr *AMDGPUMCInstLower::getLongBranchBlockExpr(
976bc43d86SMatt Arsenault   const MachineBasicBlock &SrcBB,
986bc43d86SMatt Arsenault   const MachineOperand &MO) const {
996bc43d86SMatt Arsenault   const MCExpr *DestBBSym
1006bc43d86SMatt Arsenault     = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx);
1016bc43d86SMatt Arsenault   const MCExpr *SrcBBSym = MCSymbolRefExpr::create(SrcBB.getSymbol(), Ctx);
1026bc43d86SMatt Arsenault 
103f84ce75cSMatt Arsenault   // FIXME: The first half of this assert should be removed. This should
104f84ce75cSMatt Arsenault   // probably be PC relative instead of using the source block symbol, and
105f84ce75cSMatt Arsenault   // therefore the indirect branch expansion should use a bundle.
106f84ce75cSMatt Arsenault   assert(
107f84ce75cSMatt Arsenault       skipDebugInstructionsForward(SrcBB.begin(), SrcBB.end())->getOpcode() ==
108f84ce75cSMatt Arsenault           AMDGPU::S_GETPC_B64 &&
1096bc43d86SMatt Arsenault       ST.getInstrInfo()->get(AMDGPU::S_GETPC_B64).Size == 4);
1106bc43d86SMatt Arsenault 
1116bc43d86SMatt Arsenault   // s_getpc_b64 returns the address of next instruction.
1126bc43d86SMatt Arsenault   const MCConstantExpr *One = MCConstantExpr::create(4, Ctx);
1136bc43d86SMatt Arsenault   SrcBBSym = MCBinaryExpr::createAdd(SrcBBSym, One, Ctx);
1146bc43d86SMatt Arsenault 
115*0f8a764eSMatt Arsenault   if (MO.getTargetFlags() == SIInstrInfo::MO_LONG_BRANCH_FORWARD)
1166bc43d86SMatt Arsenault     return MCBinaryExpr::createSub(DestBBSym, SrcBBSym, Ctx);
1176bc43d86SMatt Arsenault 
118*0f8a764eSMatt Arsenault   assert(MO.getTargetFlags() == SIInstrInfo::MO_LONG_BRANCH_BACKWARD);
1196bc43d86SMatt Arsenault   return MCBinaryExpr::createSub(SrcBBSym, DestBBSym, Ctx);
1206bc43d86SMatt Arsenault }
1216bc43d86SMatt Arsenault 
12211f74020SMatt Arsenault bool AMDGPUMCInstLower::lowerOperand(const MachineOperand &MO,
12311f74020SMatt Arsenault                                      MCOperand &MCOp) const {
12411f74020SMatt Arsenault   switch (MO.getType()) {
12511f74020SMatt Arsenault   default:
12611f74020SMatt Arsenault     llvm_unreachable("unknown operand type");
12711f74020SMatt Arsenault   case MachineOperand::MO_Immediate:
12811f74020SMatt Arsenault     MCOp = MCOperand::createImm(MO.getImm());
12911f74020SMatt Arsenault     return true;
13011f74020SMatt Arsenault   case MachineOperand::MO_Register:
13111f74020SMatt Arsenault     MCOp = MCOperand::createReg(AMDGPU::getMCReg(MO.getReg(), ST));
13211f74020SMatt Arsenault     return true;
13311f74020SMatt Arsenault   case MachineOperand::MO_MachineBasicBlock: {
13411f74020SMatt Arsenault     if (MO.getTargetFlags() != 0) {
13511f74020SMatt Arsenault       MCOp = MCOperand::createExpr(
13611f74020SMatt Arsenault         getLongBranchBlockExpr(*MO.getParent()->getParent(), MO));
13711f74020SMatt Arsenault     } else {
13811f74020SMatt Arsenault       MCOp = MCOperand::createExpr(
13911f74020SMatt Arsenault         MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
14011f74020SMatt Arsenault     }
14111f74020SMatt Arsenault 
14211f74020SMatt Arsenault     return true;
14311f74020SMatt Arsenault   }
14411f74020SMatt Arsenault   case MachineOperand::MO_GlobalAddress: {
14511f74020SMatt Arsenault     const GlobalValue *GV = MO.getGlobal();
14611f74020SMatt Arsenault     SmallString<128> SymbolName;
14711f74020SMatt Arsenault     AP.getNameWithPrefix(SymbolName, GV);
14811f74020SMatt Arsenault     MCSymbol *Sym = Ctx.getOrCreateSymbol(SymbolName);
14911f74020SMatt Arsenault     const MCExpr *SymExpr =
15011f74020SMatt Arsenault       MCSymbolRefExpr::create(Sym, getVariantKind(MO.getTargetFlags()),Ctx);
15111f74020SMatt Arsenault     const MCExpr *Expr = MCBinaryExpr::createAdd(SymExpr,
15211f74020SMatt Arsenault       MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
15311f74020SMatt Arsenault     MCOp = MCOperand::createExpr(Expr);
15411f74020SMatt Arsenault     return true;
15511f74020SMatt Arsenault   }
15611f74020SMatt Arsenault   case MachineOperand::MO_ExternalSymbol: {
15711f74020SMatt Arsenault     MCSymbol *Sym = Ctx.getOrCreateSymbol(StringRef(MO.getSymbolName()));
15811f74020SMatt Arsenault     Sym->setExternal(true);
15911f74020SMatt Arsenault     const MCSymbolRefExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
16011f74020SMatt Arsenault     MCOp = MCOperand::createExpr(Expr);
16111f74020SMatt Arsenault     return true;
16211f74020SMatt Arsenault   }
163b62a4eb5SMatt Arsenault   case MachineOperand::MO_RegisterMask:
164b62a4eb5SMatt Arsenault     // Regmasks are like implicit defs.
165b62a4eb5SMatt Arsenault     return false;
16611f74020SMatt Arsenault   }
16711f74020SMatt Arsenault }
16811f74020SMatt Arsenault 
16945bb48eaSTom Stellard void AMDGPUMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
1702b1f9aa5SMatt Arsenault   unsigned Opcode = MI->getOpcode();
17157b9342cSTom Stellard   const auto *TII = static_cast<const SIInstrInfo*>(ST.getInstrInfo());
17245bb48eaSTom Stellard 
1732b1f9aa5SMatt Arsenault   // FIXME: Should be able to handle this with emitPseudoExpansionLowering. We
1742b1f9aa5SMatt Arsenault   // need to select it to the subtarget specific version, and there's no way to
1752b1f9aa5SMatt Arsenault   // do that with a single pseudo source operation.
1762b1f9aa5SMatt Arsenault   if (Opcode == AMDGPU::S_SETPC_B64_return)
1772b1f9aa5SMatt Arsenault     Opcode = AMDGPU::S_SETPC_B64;
1786ed7b9bfSMatt Arsenault   else if (Opcode == AMDGPU::SI_CALL) {
1796ed7b9bfSMatt Arsenault     // SI_CALL is just S_SWAPPC_B64 with an additional operand to track the
1801d6317c3SMatt Arsenault     // called function (which we need to remove here).
1811d6317c3SMatt Arsenault     OutMI.setOpcode(TII->pseudoToMCOpcode(AMDGPU::S_SWAPPC_B64));
1821d6317c3SMatt Arsenault     MCOperand Dest, Src;
1831d6317c3SMatt Arsenault     lowerOperand(MI->getOperand(0), Dest);
1841d6317c3SMatt Arsenault     lowerOperand(MI->getOperand(1), Src);
1851d6317c3SMatt Arsenault     OutMI.addOperand(Dest);
1861d6317c3SMatt Arsenault     OutMI.addOperand(Src);
1871d6317c3SMatt Arsenault     return;
18871bcbd45SMatt Arsenault   } else if (Opcode == AMDGPU::SI_TCRETURN) {
18971bcbd45SMatt Arsenault     // TODO: How to use branch immediate and avoid register+add?
19071bcbd45SMatt Arsenault     Opcode = AMDGPU::S_SETPC_B64;
1916ed7b9bfSMatt Arsenault   }
19245bb48eaSTom Stellard 
1931d6317c3SMatt Arsenault   int MCOpcode = TII->pseudoToMCOpcode(Opcode);
19445bb48eaSTom Stellard   if (MCOpcode == -1) {
195f1caa283SMatthias Braun     LLVMContext &C = MI->getParent()->getParent()->getFunction().getContext();
19645bb48eaSTom Stellard     C.emitError("AMDGPUMCInstLower::lower - Pseudo instruction doesn't have "
19745bb48eaSTom Stellard                 "a target-specific version: " + Twine(MI->getOpcode()));
19845bb48eaSTom Stellard   }
19945bb48eaSTom Stellard 
20045bb48eaSTom Stellard   OutMI.setOpcode(MCOpcode);
20145bb48eaSTom Stellard 
20245bb48eaSTom Stellard   for (const MachineOperand &MO : MI->explicit_operands()) {
20345bb48eaSTom Stellard     MCOperand MCOp;
20411f74020SMatt Arsenault     lowerOperand(MO, MCOp);
20545bb48eaSTom Stellard     OutMI.addOperand(MCOp);
20645bb48eaSTom Stellard   }
20745bb48eaSTom Stellard }
20845bb48eaSTom Stellard 
20911f74020SMatt Arsenault bool AMDGPUAsmPrinter::lowerOperand(const MachineOperand &MO,
21011f74020SMatt Arsenault                                     MCOperand &MCOp) const {
2115bfbae5cSTom Stellard   const GCNSubtarget &STI = MF->getSubtarget<GCNSubtarget>();
21211f74020SMatt Arsenault   AMDGPUMCInstLower MCInstLowering(OutContext, STI, *this);
21311f74020SMatt Arsenault   return MCInstLowering.lowerOperand(MO, MCOp);
21411f74020SMatt Arsenault }
21511f74020SMatt Arsenault 
216c5015010STom Stellard static const MCExpr *lowerAddrSpaceCast(const TargetMachine &TM,
217c5015010STom Stellard                                         const Constant *CV,
218c5015010STom Stellard                                         MCContext &OutContext) {
2198f844f39SYaxun Liu   // TargetMachine does not support llvm-style cast. Use C++-style cast.
2208f844f39SYaxun Liu   // This is safe since TM is always of type AMDGPUTargetMachine or its
2218f844f39SYaxun Liu   // derived class.
222c5015010STom Stellard   auto &AT = static_cast<const AMDGPUTargetMachine&>(TM);
2238f844f39SYaxun Liu   auto *CE = dyn_cast<ConstantExpr>(CV);
2248f844f39SYaxun Liu 
2258f844f39SYaxun Liu   // Lower null pointers in private and local address space.
2268f844f39SYaxun Liu   // Clang generates addrspacecast for null pointers in private and local
2278f844f39SYaxun Liu   // address space, which needs to be lowered.
2288f844f39SYaxun Liu   if (CE && CE->getOpcode() == Instruction::AddrSpaceCast) {
2298f844f39SYaxun Liu     auto Op = CE->getOperand(0);
2308f844f39SYaxun Liu     auto SrcAddr = Op->getType()->getPointerAddressSpace();
231c5015010STom Stellard     if (Op->isNullValue() && AT.getNullPointerValue(SrcAddr) == 0) {
2328f844f39SYaxun Liu       auto DstAddr = CE->getType()->getPointerAddressSpace();
233c5015010STom Stellard       return MCConstantExpr::create(AT.getNullPointerValue(DstAddr),
2348f844f39SYaxun Liu         OutContext);
2358f844f39SYaxun Liu     }
2368f844f39SYaxun Liu   }
237c5015010STom Stellard   return nullptr;
238c5015010STom Stellard }
239c5015010STom Stellard 
240c5015010STom Stellard const MCExpr *AMDGPUAsmPrinter::lowerConstant(const Constant *CV) {
241c5015010STom Stellard   if (const MCExpr *E = lowerAddrSpaceCast(TM, CV, OutContext))
242c5015010STom Stellard     return E;
2438f844f39SYaxun Liu   return AsmPrinter::lowerConstant(CV);
2448f844f39SYaxun Liu }
2458f844f39SYaxun Liu 
24645bb48eaSTom Stellard void AMDGPUAsmPrinter::EmitInstruction(const MachineInstr *MI) {
24711f74020SMatt Arsenault   if (emitPseudoExpansionLowering(*OutStreamer, MI))
24811f74020SMatt Arsenault     return;
24911f74020SMatt Arsenault 
2505bfbae5cSTom Stellard   const GCNSubtarget &STI = MF->getSubtarget<GCNSubtarget>();
2511b9748c6STom Stellard   AMDGPUMCInstLower MCInstLowering(OutContext, STI, *this);
25245bb48eaSTom Stellard 
25345bb48eaSTom Stellard   StringRef Err;
2549cfc75c2SDuncan P. N. Exon Smith   if (!STI.getInstrInfo()->verifyInstruction(*MI, Err)) {
255f1caa283SMatthias Braun     LLVMContext &C = MI->getParent()->getParent()->getFunction().getContext();
256302f83acSMichel Danzer     C.emitError("Illegal instruction detected: " + Err);
2578c209aa8SMatthias Braun     MI->print(errs());
25845bb48eaSTom Stellard   }
259302f83acSMichel Danzer 
26045bb48eaSTom Stellard   if (MI->isBundle()) {
26145bb48eaSTom Stellard     const MachineBasicBlock *MBB = MI->getParent();
262c5b668deSDuncan P. N. Exon Smith     MachineBasicBlock::const_instr_iterator I = ++MI->getIterator();
263a73371a9SDuncan P. N. Exon Smith     while (I != MBB->instr_end() && I->isInsideBundle()) {
264a73371a9SDuncan P. N. Exon Smith       EmitInstruction(&*I);
26545bb48eaSTom Stellard       ++I;
26645bb48eaSTom Stellard     }
26745bb48eaSTom Stellard   } else {
2685b20fbb7SMatt Arsenault     // We don't want SI_MASK_BRANCH/SI_RETURN_TO_EPILOG encoded. They are
2695b20fbb7SMatt Arsenault     // placeholder terminator instructions and should only be printed as
2705b20fbb7SMatt Arsenault     // comments.
2719babdf42SMatt Arsenault     if (MI->getOpcode() == AMDGPU::SI_MASK_BRANCH) {
2729babdf42SMatt Arsenault       if (isVerbose()) {
2739babdf42SMatt Arsenault         SmallVector<char, 16> BBStr;
2749babdf42SMatt Arsenault         raw_svector_ostream Str(BBStr);
2759babdf42SMatt Arsenault 
276a74374a8SMatt Arsenault         const MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
2779babdf42SMatt Arsenault         const MCSymbolRefExpr *Expr
2789babdf42SMatt Arsenault           = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
2799babdf42SMatt Arsenault         Expr->print(Str, MAI);
280c18c12e3SReid Kleckner         OutStreamer->emitRawComment(Twine(" mask branch ") + BBStr);
2819babdf42SMatt Arsenault       }
2829babdf42SMatt Arsenault 
2839babdf42SMatt Arsenault       return;
2849babdf42SMatt Arsenault     }
2859babdf42SMatt Arsenault 
2865b20fbb7SMatt Arsenault     if (MI->getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG) {
2879babdf42SMatt Arsenault       if (isVerbose())
2885b20fbb7SMatt Arsenault         OutStreamer->emitRawComment(" return to shader part epilog");
2899babdf42SMatt Arsenault       return;
2909babdf42SMatt Arsenault     }
2919babdf42SMatt Arsenault 
292ea91cca5SStanislav Mekhanoshin     if (MI->getOpcode() == AMDGPU::WAVE_BARRIER) {
293ea91cca5SStanislav Mekhanoshin       if (isVerbose())
294ea91cca5SStanislav Mekhanoshin         OutStreamer->emitRawComment(" wave barrier");
295ea91cca5SStanislav Mekhanoshin       return;
296ea91cca5SStanislav Mekhanoshin     }
297ea91cca5SStanislav Mekhanoshin 
29815a96b1dSYaxun Liu     if (MI->getOpcode() == AMDGPU::SI_MASKED_UNREACHABLE) {
29915a96b1dSYaxun Liu       if (isVerbose())
30015a96b1dSYaxun Liu         OutStreamer->emitRawComment(" divergent unreachable");
30115a96b1dSYaxun Liu       return;
30215a96b1dSYaxun Liu     }
30315a96b1dSYaxun Liu 
30445bb48eaSTom Stellard     MCInst TmpInst;
30545bb48eaSTom Stellard     MCInstLowering.lower(MI, TmpInst);
30645bb48eaSTom Stellard     EmitToStreamer(*OutStreamer, TmpInst);
30745bb48eaSTom Stellard 
308283b9950SNicolai Haehnle #ifdef EXPENSIVE_CHECKS
309283b9950SNicolai Haehnle     // Sanity-check getInstSizeInBytes on explicitly specified CPUs (it cannot
310283b9950SNicolai Haehnle     // work correctly for the generic CPU).
311283b9950SNicolai Haehnle     //
312283b9950SNicolai Haehnle     // The isPseudo check really shouldn't be here, but unfortunately there are
313283b9950SNicolai Haehnle     // some negative lit tests that depend on being able to continue through
314283b9950SNicolai Haehnle     // here even when pseudo instructions haven't been lowered.
315283b9950SNicolai Haehnle     if (!MI->isPseudo() && STI.isCPUStringValid(STI.getCPU())) {
316283b9950SNicolai Haehnle       SmallVector<MCFixup, 4> Fixups;
317283b9950SNicolai Haehnle       SmallVector<char, 16> CodeBytes;
318283b9950SNicolai Haehnle       raw_svector_ostream CodeStream(CodeBytes);
319283b9950SNicolai Haehnle 
320283b9950SNicolai Haehnle       std::unique_ptr<MCCodeEmitter> InstEmitter(createSIMCCodeEmitter(
321283b9950SNicolai Haehnle           *STI.getInstrInfo(), *OutContext.getRegisterInfo(), OutContext));
322283b9950SNicolai Haehnle       InstEmitter->encodeInstruction(TmpInst, CodeStream, Fixups, STI);
323283b9950SNicolai Haehnle 
324283b9950SNicolai Haehnle       assert(CodeBytes.size() == STI.getInstrInfo()->getInstSizeInBytes(*MI));
325283b9950SNicolai Haehnle     }
326283b9950SNicolai Haehnle #endif
327283b9950SNicolai Haehnle 
32833cb8f5bSTim Renouf     if (DumpCodeInstEmitter) {
32933cb8f5bSTim Renouf       // Disassemble instruction/operands to text
33045bb48eaSTom Stellard       DisasmLines.resize(DisasmLines.size() + 1);
33145bb48eaSTom Stellard       std::string &DisasmLine = DisasmLines.back();
33245bb48eaSTom Stellard       raw_string_ostream DisasmStream(DisasmLine);
33345bb48eaSTom Stellard 
33433cb8f5bSTim Renouf       AMDGPUInstPrinter InstPrinter(*TM.getMCAsmInfo(), *STI.getInstrInfo(),
33543e92fe3SMatt Arsenault                                     *STI.getRegisterInfo());
33643e92fe3SMatt Arsenault       InstPrinter.printInst(&TmpInst, DisasmStream, StringRef(), STI);
33745bb48eaSTom Stellard 
33845bb48eaSTom Stellard       // Disassemble instruction/operands to hex representation.
33945bb48eaSTom Stellard       SmallVector<MCFixup, 4> Fixups;
34045bb48eaSTom Stellard       SmallVector<char, 16> CodeBytes;
34145bb48eaSTom Stellard       raw_svector_ostream CodeStream(CodeBytes);
34245bb48eaSTom Stellard 
34333cb8f5bSTim Renouf       DumpCodeInstEmitter->encodeInstruction(
34433cb8f5bSTim Renouf           TmpInst, CodeStream, Fixups, MF->getSubtarget<MCSubtargetInfo>());
34545bb48eaSTom Stellard       HexLines.resize(HexLines.size() + 1);
34645bb48eaSTom Stellard       std::string &HexLine = HexLines.back();
34745bb48eaSTom Stellard       raw_string_ostream HexStream(HexLine);
34845bb48eaSTom Stellard 
34945bb48eaSTom Stellard       for (size_t i = 0; i < CodeBytes.size(); i += 4) {
35045bb48eaSTom Stellard         unsigned int CodeDWord = *(unsigned int *)&CodeBytes[i];
35145bb48eaSTom Stellard         HexStream << format("%s%08X", (i > 0 ? " " : ""), CodeDWord);
35245bb48eaSTom Stellard       }
35345bb48eaSTom Stellard 
35445bb48eaSTom Stellard       DisasmStream.flush();
35545bb48eaSTom Stellard       DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLine.size());
35645bb48eaSTom Stellard     }
35745bb48eaSTom Stellard   }
35845bb48eaSTom Stellard }
359c5015010STom Stellard 
36057b9342cSTom Stellard R600MCInstLower::R600MCInstLower(MCContext &Ctx, const R600Subtarget &ST,
36157b9342cSTom Stellard                                  const AsmPrinter &AP) :
36257b9342cSTom Stellard         AMDGPUMCInstLower(Ctx, ST, AP) { }
36357b9342cSTom Stellard 
36457b9342cSTom Stellard void R600MCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
36557b9342cSTom Stellard   OutMI.setOpcode(MI->getOpcode());
36657b9342cSTom Stellard   for (const MachineOperand &MO : MI->explicit_operands()) {
36757b9342cSTom Stellard     MCOperand MCOp;
36857b9342cSTom Stellard     lowerOperand(MO, MCOp);
36957b9342cSTom Stellard     OutMI.addOperand(MCOp);
37057b9342cSTom Stellard   }
37157b9342cSTom Stellard }
37257b9342cSTom Stellard 
373c5015010STom Stellard void R600AsmPrinter::EmitInstruction(const MachineInstr *MI) {
374c5015010STom Stellard   const R600Subtarget &STI = MF->getSubtarget<R600Subtarget>();
37557b9342cSTom Stellard   R600MCInstLower MCInstLowering(OutContext, STI, *this);
376c5015010STom Stellard 
377c5015010STom Stellard   StringRef Err;
378c5015010STom Stellard   if (!STI.getInstrInfo()->verifyInstruction(*MI, Err)) {
379c5015010STom Stellard     LLVMContext &C = MI->getParent()->getParent()->getFunction().getContext();
380c5015010STom Stellard     C.emitError("Illegal instruction detected: " + Err);
381c5015010STom Stellard     MI->print(errs());
382c5015010STom Stellard   }
383c5015010STom Stellard 
384c5015010STom Stellard   if (MI->isBundle()) {
385c5015010STom Stellard     const MachineBasicBlock *MBB = MI->getParent();
386c5015010STom Stellard     MachineBasicBlock::const_instr_iterator I = ++MI->getIterator();
387c5015010STom Stellard     while (I != MBB->instr_end() && I->isInsideBundle()) {
388c5015010STom Stellard       EmitInstruction(&*I);
389c5015010STom Stellard       ++I;
390c5015010STom Stellard     }
391c5015010STom Stellard   } else {
392c5015010STom Stellard     MCInst TmpInst;
393c5015010STom Stellard     MCInstLowering.lower(MI, TmpInst);
394c5015010STom Stellard     EmitToStreamer(*OutStreamer, TmpInst);
395c5015010STom Stellard  }
396c5015010STom Stellard }
397c5015010STom Stellard 
398c5015010STom Stellard const MCExpr *R600AsmPrinter::lowerConstant(const Constant *CV) {
399c5015010STom Stellard   if (const MCExpr *E = lowerAddrSpaceCast(TM, CV, OutContext))
400c5015010STom Stellard     return E;
401c5015010STom Stellard   return AsmPrinter::lowerConstant(CV);
402c5015010STom Stellard }
403