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; 9341abf276SNicolai Haehnle case SIInstrInfo::MO_ABS32_LO: 9441abf276SNicolai Haehnle return MCSymbolRefExpr::VK_AMDGPU_ABS32_LO; 9541abf276SNicolai Haehnle case SIInstrInfo::MO_ABS32_HI: 9641abf276SNicolai Haehnle return MCSymbolRefExpr::VK_AMDGPU_ABS32_HI; 97418beb76STom Stellard } 98418beb76STom Stellard } 99418beb76STom Stellard 1006bc43d86SMatt Arsenault const MCExpr *AMDGPUMCInstLower::getLongBranchBlockExpr( 1016bc43d86SMatt Arsenault const MachineBasicBlock &SrcBB, 1026bc43d86SMatt Arsenault const MachineOperand &MO) const { 1036bc43d86SMatt Arsenault const MCExpr *DestBBSym 1046bc43d86SMatt Arsenault = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx); 1056bc43d86SMatt Arsenault const MCExpr *SrcBBSym = MCSymbolRefExpr::create(SrcBB.getSymbol(), Ctx); 1066bc43d86SMatt Arsenault 107f84ce75cSMatt Arsenault // FIXME: The first half of this assert should be removed. This should 108f84ce75cSMatt Arsenault // probably be PC relative instead of using the source block symbol, and 109f84ce75cSMatt Arsenault // therefore the indirect branch expansion should use a bundle. 110f84ce75cSMatt Arsenault assert( 111f84ce75cSMatt Arsenault skipDebugInstructionsForward(SrcBB.begin(), SrcBB.end())->getOpcode() == 112f84ce75cSMatt Arsenault AMDGPU::S_GETPC_B64 && 1136bc43d86SMatt Arsenault ST.getInstrInfo()->get(AMDGPU::S_GETPC_B64).Size == 4); 1146bc43d86SMatt Arsenault 1156bc43d86SMatt Arsenault // s_getpc_b64 returns the address of next instruction. 1166bc43d86SMatt Arsenault const MCConstantExpr *One = MCConstantExpr::create(4, Ctx); 1176bc43d86SMatt Arsenault SrcBBSym = MCBinaryExpr::createAdd(SrcBBSym, One, Ctx); 1186bc43d86SMatt Arsenault 1190f8a764eSMatt Arsenault if (MO.getTargetFlags() == SIInstrInfo::MO_LONG_BRANCH_FORWARD) 1206bc43d86SMatt Arsenault return MCBinaryExpr::createSub(DestBBSym, SrcBBSym, Ctx); 1216bc43d86SMatt Arsenault 1220f8a764eSMatt Arsenault assert(MO.getTargetFlags() == SIInstrInfo::MO_LONG_BRANCH_BACKWARD); 1236bc43d86SMatt Arsenault return MCBinaryExpr::createSub(SrcBBSym, DestBBSym, Ctx); 1246bc43d86SMatt Arsenault } 1256bc43d86SMatt Arsenault 12611f74020SMatt Arsenault bool AMDGPUMCInstLower::lowerOperand(const MachineOperand &MO, 12711f74020SMatt Arsenault MCOperand &MCOp) const { 12811f74020SMatt Arsenault switch (MO.getType()) { 12911f74020SMatt Arsenault default: 13011f74020SMatt Arsenault llvm_unreachable("unknown operand type"); 13111f74020SMatt Arsenault case MachineOperand::MO_Immediate: 13211f74020SMatt Arsenault MCOp = MCOperand::createImm(MO.getImm()); 13311f74020SMatt Arsenault return true; 13411f74020SMatt Arsenault case MachineOperand::MO_Register: 13511f74020SMatt Arsenault MCOp = MCOperand::createReg(AMDGPU::getMCReg(MO.getReg(), ST)); 13611f74020SMatt Arsenault return true; 13711f74020SMatt Arsenault case MachineOperand::MO_MachineBasicBlock: { 13811f74020SMatt Arsenault if (MO.getTargetFlags() != 0) { 13911f74020SMatt Arsenault MCOp = MCOperand::createExpr( 14011f74020SMatt Arsenault getLongBranchBlockExpr(*MO.getParent()->getParent(), MO)); 14111f74020SMatt Arsenault } else { 14211f74020SMatt Arsenault MCOp = MCOperand::createExpr( 14311f74020SMatt Arsenault MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx)); 14411f74020SMatt Arsenault } 14511f74020SMatt Arsenault 14611f74020SMatt Arsenault return true; 14711f74020SMatt Arsenault } 14811f74020SMatt Arsenault case MachineOperand::MO_GlobalAddress: { 14911f74020SMatt Arsenault const GlobalValue *GV = MO.getGlobal(); 15011f74020SMatt Arsenault SmallString<128> SymbolName; 15111f74020SMatt Arsenault AP.getNameWithPrefix(SymbolName, GV); 15211f74020SMatt Arsenault MCSymbol *Sym = Ctx.getOrCreateSymbol(SymbolName); 15341abf276SNicolai Haehnle const MCExpr *Expr = 15411f74020SMatt Arsenault MCSymbolRefExpr::create(Sym, getVariantKind(MO.getTargetFlags()),Ctx); 15541abf276SNicolai Haehnle int64_t Offset = MO.getOffset(); 15641abf276SNicolai Haehnle if (Offset != 0) { 15741abf276SNicolai Haehnle Expr = MCBinaryExpr::createAdd(Expr, 15841abf276SNicolai Haehnle MCConstantExpr::create(Offset, Ctx), Ctx); 15941abf276SNicolai Haehnle } 16011f74020SMatt Arsenault MCOp = MCOperand::createExpr(Expr); 16111f74020SMatt Arsenault return true; 16211f74020SMatt Arsenault } 16311f74020SMatt Arsenault case MachineOperand::MO_ExternalSymbol: { 16411f74020SMatt Arsenault MCSymbol *Sym = Ctx.getOrCreateSymbol(StringRef(MO.getSymbolName())); 16511f74020SMatt Arsenault Sym->setExternal(true); 16611f74020SMatt Arsenault const MCSymbolRefExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx); 16711f74020SMatt Arsenault MCOp = MCOperand::createExpr(Expr); 16811f74020SMatt Arsenault return true; 16911f74020SMatt Arsenault } 170b62a4eb5SMatt Arsenault case MachineOperand::MO_RegisterMask: 171b62a4eb5SMatt Arsenault // Regmasks are like implicit defs. 172b62a4eb5SMatt Arsenault return false; 17311f74020SMatt Arsenault } 17411f74020SMatt Arsenault } 17511f74020SMatt Arsenault 17645bb48eaSTom Stellard void AMDGPUMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const { 1772b1f9aa5SMatt Arsenault unsigned Opcode = MI->getOpcode(); 17857b9342cSTom Stellard const auto *TII = static_cast<const SIInstrInfo*>(ST.getInstrInfo()); 17945bb48eaSTom Stellard 1802b1f9aa5SMatt Arsenault // FIXME: Should be able to handle this with emitPseudoExpansionLowering. We 1812b1f9aa5SMatt Arsenault // need to select it to the subtarget specific version, and there's no way to 1822b1f9aa5SMatt Arsenault // do that with a single pseudo source operation. 1832b1f9aa5SMatt Arsenault if (Opcode == AMDGPU::S_SETPC_B64_return) 1842b1f9aa5SMatt Arsenault Opcode = AMDGPU::S_SETPC_B64; 1856ed7b9bfSMatt Arsenault else if (Opcode == AMDGPU::SI_CALL) { 1866ed7b9bfSMatt Arsenault // SI_CALL is just S_SWAPPC_B64 with an additional operand to track the 1871d6317c3SMatt Arsenault // called function (which we need to remove here). 1881d6317c3SMatt Arsenault OutMI.setOpcode(TII->pseudoToMCOpcode(AMDGPU::S_SWAPPC_B64)); 1891d6317c3SMatt Arsenault MCOperand Dest, Src; 1901d6317c3SMatt Arsenault lowerOperand(MI->getOperand(0), Dest); 1911d6317c3SMatt Arsenault lowerOperand(MI->getOperand(1), Src); 1921d6317c3SMatt Arsenault OutMI.addOperand(Dest); 1931d6317c3SMatt Arsenault OutMI.addOperand(Src); 1941d6317c3SMatt Arsenault return; 19571bcbd45SMatt Arsenault } else if (Opcode == AMDGPU::SI_TCRETURN) { 19671bcbd45SMatt Arsenault // TODO: How to use branch immediate and avoid register+add? 19771bcbd45SMatt Arsenault Opcode = AMDGPU::S_SETPC_B64; 1986ed7b9bfSMatt Arsenault } 19945bb48eaSTom Stellard 2001d6317c3SMatt Arsenault int MCOpcode = TII->pseudoToMCOpcode(Opcode); 20145bb48eaSTom Stellard if (MCOpcode == -1) { 202f1caa283SMatthias Braun LLVMContext &C = MI->getParent()->getParent()->getFunction().getContext(); 20345bb48eaSTom Stellard C.emitError("AMDGPUMCInstLower::lower - Pseudo instruction doesn't have " 20445bb48eaSTom Stellard "a target-specific version: " + Twine(MI->getOpcode())); 20545bb48eaSTom Stellard } 20645bb48eaSTom Stellard 20745bb48eaSTom Stellard OutMI.setOpcode(MCOpcode); 20845bb48eaSTom Stellard 20945bb48eaSTom Stellard for (const MachineOperand &MO : MI->explicit_operands()) { 21045bb48eaSTom Stellard MCOperand MCOp; 21111f74020SMatt Arsenault lowerOperand(MO, MCOp); 21245bb48eaSTom Stellard OutMI.addOperand(MCOp); 21345bb48eaSTom Stellard } 214*e2d104f6SStanislav Mekhanoshin 215*e2d104f6SStanislav Mekhanoshin int FIIdx = AMDGPU::getNamedOperandIdx(MCOpcode, AMDGPU::OpName::fi); 216*e2d104f6SStanislav Mekhanoshin if (FIIdx >= (int)OutMI.getNumOperands()) 217*e2d104f6SStanislav Mekhanoshin OutMI.addOperand(MCOperand::createImm(0)); 21845bb48eaSTom Stellard } 21945bb48eaSTom Stellard 22011f74020SMatt Arsenault bool AMDGPUAsmPrinter::lowerOperand(const MachineOperand &MO, 22111f74020SMatt Arsenault MCOperand &MCOp) const { 2225bfbae5cSTom Stellard const GCNSubtarget &STI = MF->getSubtarget<GCNSubtarget>(); 22311f74020SMatt Arsenault AMDGPUMCInstLower MCInstLowering(OutContext, STI, *this); 22411f74020SMatt Arsenault return MCInstLowering.lowerOperand(MO, MCOp); 22511f74020SMatt Arsenault } 22611f74020SMatt Arsenault 227c5015010STom Stellard static const MCExpr *lowerAddrSpaceCast(const TargetMachine &TM, 228c5015010STom Stellard const Constant *CV, 229c5015010STom Stellard MCContext &OutContext) { 2308f844f39SYaxun Liu // TargetMachine does not support llvm-style cast. Use C++-style cast. 2318f844f39SYaxun Liu // This is safe since TM is always of type AMDGPUTargetMachine or its 2328f844f39SYaxun Liu // derived class. 233c5015010STom Stellard auto &AT = static_cast<const AMDGPUTargetMachine&>(TM); 2348f844f39SYaxun Liu auto *CE = dyn_cast<ConstantExpr>(CV); 2358f844f39SYaxun Liu 2368f844f39SYaxun Liu // Lower null pointers in private and local address space. 2378f844f39SYaxun Liu // Clang generates addrspacecast for null pointers in private and local 2388f844f39SYaxun Liu // address space, which needs to be lowered. 2398f844f39SYaxun Liu if (CE && CE->getOpcode() == Instruction::AddrSpaceCast) { 2408f844f39SYaxun Liu auto Op = CE->getOperand(0); 2418f844f39SYaxun Liu auto SrcAddr = Op->getType()->getPointerAddressSpace(); 242c5015010STom Stellard if (Op->isNullValue() && AT.getNullPointerValue(SrcAddr) == 0) { 2438f844f39SYaxun Liu auto DstAddr = CE->getType()->getPointerAddressSpace(); 244c5015010STom Stellard return MCConstantExpr::create(AT.getNullPointerValue(DstAddr), 2458f844f39SYaxun Liu OutContext); 2468f844f39SYaxun Liu } 2478f844f39SYaxun Liu } 248c5015010STom Stellard return nullptr; 249c5015010STom Stellard } 250c5015010STom Stellard 251c5015010STom Stellard const MCExpr *AMDGPUAsmPrinter::lowerConstant(const Constant *CV) { 252c5015010STom Stellard if (const MCExpr *E = lowerAddrSpaceCast(TM, CV, OutContext)) 253c5015010STom Stellard return E; 2548f844f39SYaxun Liu return AsmPrinter::lowerConstant(CV); 2558f844f39SYaxun Liu } 2568f844f39SYaxun Liu 25745bb48eaSTom Stellard void AMDGPUAsmPrinter::EmitInstruction(const MachineInstr *MI) { 25811f74020SMatt Arsenault if (emitPseudoExpansionLowering(*OutStreamer, MI)) 25911f74020SMatt Arsenault return; 26011f74020SMatt Arsenault 2615bfbae5cSTom Stellard const GCNSubtarget &STI = MF->getSubtarget<GCNSubtarget>(); 2621b9748c6STom Stellard AMDGPUMCInstLower MCInstLowering(OutContext, STI, *this); 26345bb48eaSTom Stellard 26445bb48eaSTom Stellard StringRef Err; 2659cfc75c2SDuncan P. N. Exon Smith if (!STI.getInstrInfo()->verifyInstruction(*MI, Err)) { 266f1caa283SMatthias Braun LLVMContext &C = MI->getParent()->getParent()->getFunction().getContext(); 267302f83acSMichel Danzer C.emitError("Illegal instruction detected: " + Err); 2688c209aa8SMatthias Braun MI->print(errs()); 26945bb48eaSTom Stellard } 270302f83acSMichel Danzer 27145bb48eaSTom Stellard if (MI->isBundle()) { 27245bb48eaSTom Stellard const MachineBasicBlock *MBB = MI->getParent(); 273c5b668deSDuncan P. N. Exon Smith MachineBasicBlock::const_instr_iterator I = ++MI->getIterator(); 274a73371a9SDuncan P. N. Exon Smith while (I != MBB->instr_end() && I->isInsideBundle()) { 275a73371a9SDuncan P. N. Exon Smith EmitInstruction(&*I); 27645bb48eaSTom Stellard ++I; 27745bb48eaSTom Stellard } 27845bb48eaSTom Stellard } else { 2795b20fbb7SMatt Arsenault // We don't want SI_MASK_BRANCH/SI_RETURN_TO_EPILOG encoded. They are 2805b20fbb7SMatt Arsenault // placeholder terminator instructions and should only be printed as 2815b20fbb7SMatt Arsenault // comments. 2829babdf42SMatt Arsenault if (MI->getOpcode() == AMDGPU::SI_MASK_BRANCH) { 2839babdf42SMatt Arsenault if (isVerbose()) { 2849babdf42SMatt Arsenault SmallVector<char, 16> BBStr; 2859babdf42SMatt Arsenault raw_svector_ostream Str(BBStr); 2869babdf42SMatt Arsenault 287a74374a8SMatt Arsenault const MachineBasicBlock *MBB = MI->getOperand(0).getMBB(); 2889babdf42SMatt Arsenault const MCSymbolRefExpr *Expr 2899babdf42SMatt Arsenault = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext); 2909babdf42SMatt Arsenault Expr->print(Str, MAI); 291c18c12e3SReid Kleckner OutStreamer->emitRawComment(Twine(" mask branch ") + BBStr); 2929babdf42SMatt Arsenault } 2939babdf42SMatt Arsenault 2949babdf42SMatt Arsenault return; 2959babdf42SMatt Arsenault } 2969babdf42SMatt Arsenault 2975b20fbb7SMatt Arsenault if (MI->getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG) { 2989babdf42SMatt Arsenault if (isVerbose()) 2995b20fbb7SMatt Arsenault OutStreamer->emitRawComment(" return to shader part epilog"); 3009babdf42SMatt Arsenault return; 3019babdf42SMatt Arsenault } 3029babdf42SMatt Arsenault 303ea91cca5SStanislav Mekhanoshin if (MI->getOpcode() == AMDGPU::WAVE_BARRIER) { 304ea91cca5SStanislav Mekhanoshin if (isVerbose()) 305ea91cca5SStanislav Mekhanoshin OutStreamer->emitRawComment(" wave barrier"); 306ea91cca5SStanislav Mekhanoshin return; 307ea91cca5SStanislav Mekhanoshin } 308ea91cca5SStanislav Mekhanoshin 30915a96b1dSYaxun Liu if (MI->getOpcode() == AMDGPU::SI_MASKED_UNREACHABLE) { 31015a96b1dSYaxun Liu if (isVerbose()) 31115a96b1dSYaxun Liu OutStreamer->emitRawComment(" divergent unreachable"); 31215a96b1dSYaxun Liu return; 31315a96b1dSYaxun Liu } 31415a96b1dSYaxun Liu 31545bb48eaSTom Stellard MCInst TmpInst; 31645bb48eaSTom Stellard MCInstLowering.lower(MI, TmpInst); 31745bb48eaSTom Stellard EmitToStreamer(*OutStreamer, TmpInst); 31845bb48eaSTom Stellard 319283b9950SNicolai Haehnle #ifdef EXPENSIVE_CHECKS 320283b9950SNicolai Haehnle // Sanity-check getInstSizeInBytes on explicitly specified CPUs (it cannot 321283b9950SNicolai Haehnle // work correctly for the generic CPU). 322283b9950SNicolai Haehnle // 323283b9950SNicolai Haehnle // The isPseudo check really shouldn't be here, but unfortunately there are 324283b9950SNicolai Haehnle // some negative lit tests that depend on being able to continue through 325283b9950SNicolai Haehnle // here even when pseudo instructions haven't been lowered. 326283b9950SNicolai Haehnle if (!MI->isPseudo() && STI.isCPUStringValid(STI.getCPU())) { 327283b9950SNicolai Haehnle SmallVector<MCFixup, 4> Fixups; 328283b9950SNicolai Haehnle SmallVector<char, 16> CodeBytes; 329283b9950SNicolai Haehnle raw_svector_ostream CodeStream(CodeBytes); 330283b9950SNicolai Haehnle 331283b9950SNicolai Haehnle std::unique_ptr<MCCodeEmitter> InstEmitter(createSIMCCodeEmitter( 332283b9950SNicolai Haehnle *STI.getInstrInfo(), *OutContext.getRegisterInfo(), OutContext)); 333283b9950SNicolai Haehnle InstEmitter->encodeInstruction(TmpInst, CodeStream, Fixups, STI); 334283b9950SNicolai Haehnle 335283b9950SNicolai Haehnle assert(CodeBytes.size() == STI.getInstrInfo()->getInstSizeInBytes(*MI)); 336283b9950SNicolai Haehnle } 337283b9950SNicolai Haehnle #endif 338283b9950SNicolai Haehnle 33933cb8f5bSTim Renouf if (DumpCodeInstEmitter) { 34033cb8f5bSTim Renouf // Disassemble instruction/operands to text 34145bb48eaSTom Stellard DisasmLines.resize(DisasmLines.size() + 1); 34245bb48eaSTom Stellard std::string &DisasmLine = DisasmLines.back(); 34345bb48eaSTom Stellard raw_string_ostream DisasmStream(DisasmLine); 34445bb48eaSTom Stellard 34533cb8f5bSTim Renouf AMDGPUInstPrinter InstPrinter(*TM.getMCAsmInfo(), *STI.getInstrInfo(), 34643e92fe3SMatt Arsenault *STI.getRegisterInfo()); 34743e92fe3SMatt Arsenault InstPrinter.printInst(&TmpInst, DisasmStream, StringRef(), STI); 34845bb48eaSTom Stellard 34945bb48eaSTom Stellard // Disassemble instruction/operands to hex representation. 35045bb48eaSTom Stellard SmallVector<MCFixup, 4> Fixups; 35145bb48eaSTom Stellard SmallVector<char, 16> CodeBytes; 35245bb48eaSTom Stellard raw_svector_ostream CodeStream(CodeBytes); 35345bb48eaSTom Stellard 35433cb8f5bSTim Renouf DumpCodeInstEmitter->encodeInstruction( 35533cb8f5bSTim Renouf TmpInst, CodeStream, Fixups, MF->getSubtarget<MCSubtargetInfo>()); 35645bb48eaSTom Stellard HexLines.resize(HexLines.size() + 1); 35745bb48eaSTom Stellard std::string &HexLine = HexLines.back(); 35845bb48eaSTom Stellard raw_string_ostream HexStream(HexLine); 35945bb48eaSTom Stellard 36045bb48eaSTom Stellard for (size_t i = 0; i < CodeBytes.size(); i += 4) { 36145bb48eaSTom Stellard unsigned int CodeDWord = *(unsigned int *)&CodeBytes[i]; 36245bb48eaSTom Stellard HexStream << format("%s%08X", (i > 0 ? " " : ""), CodeDWord); 36345bb48eaSTom Stellard } 36445bb48eaSTom Stellard 36545bb48eaSTom Stellard DisasmStream.flush(); 36645bb48eaSTom Stellard DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLine.size()); 36745bb48eaSTom Stellard } 36845bb48eaSTom Stellard } 36945bb48eaSTom Stellard } 370c5015010STom Stellard 37157b9342cSTom Stellard R600MCInstLower::R600MCInstLower(MCContext &Ctx, const R600Subtarget &ST, 37257b9342cSTom Stellard const AsmPrinter &AP) : 37357b9342cSTom Stellard AMDGPUMCInstLower(Ctx, ST, AP) { } 37457b9342cSTom Stellard 37557b9342cSTom Stellard void R600MCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const { 37657b9342cSTom Stellard OutMI.setOpcode(MI->getOpcode()); 37757b9342cSTom Stellard for (const MachineOperand &MO : MI->explicit_operands()) { 37857b9342cSTom Stellard MCOperand MCOp; 37957b9342cSTom Stellard lowerOperand(MO, MCOp); 38057b9342cSTom Stellard OutMI.addOperand(MCOp); 38157b9342cSTom Stellard } 38257b9342cSTom Stellard } 38357b9342cSTom Stellard 384c5015010STom Stellard void R600AsmPrinter::EmitInstruction(const MachineInstr *MI) { 385c5015010STom Stellard const R600Subtarget &STI = MF->getSubtarget<R600Subtarget>(); 38657b9342cSTom Stellard R600MCInstLower MCInstLowering(OutContext, STI, *this); 387c5015010STom Stellard 388c5015010STom Stellard StringRef Err; 389c5015010STom Stellard if (!STI.getInstrInfo()->verifyInstruction(*MI, Err)) { 390c5015010STom Stellard LLVMContext &C = MI->getParent()->getParent()->getFunction().getContext(); 391c5015010STom Stellard C.emitError("Illegal instruction detected: " + Err); 392c5015010STom Stellard MI->print(errs()); 393c5015010STom Stellard } 394c5015010STom Stellard 395c5015010STom Stellard if (MI->isBundle()) { 396c5015010STom Stellard const MachineBasicBlock *MBB = MI->getParent(); 397c5015010STom Stellard MachineBasicBlock::const_instr_iterator I = ++MI->getIterator(); 398c5015010STom Stellard while (I != MBB->instr_end() && I->isInsideBundle()) { 399c5015010STom Stellard EmitInstruction(&*I); 400c5015010STom Stellard ++I; 401c5015010STom Stellard } 402c5015010STom Stellard } else { 403c5015010STom Stellard MCInst TmpInst; 404c5015010STom Stellard MCInstLowering.lower(MI, TmpInst); 405c5015010STom Stellard EmitToStreamer(*OutStreamer, TmpInst); 406c5015010STom Stellard } 407c5015010STom Stellard } 408c5015010STom Stellard 409c5015010STom Stellard const MCExpr *R600AsmPrinter::lowerConstant(const Constant *CV) { 410c5015010STom Stellard if (const MCExpr *E = lowerAddrSpaceCast(TM, CV, OutContext)) 411c5015010STom Stellard return E; 412c5015010STom Stellard return AsmPrinter::lowerConstant(CV); 413c5015010STom Stellard } 414