LLVM API Documentation
00001 //===- AMDGPUMCInstLower.cpp - Lower AMDGPU MachineInstr to an MCInst -----===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 /// \file 00011 /// \brief Code to lower AMDGPU MachineInstrs to their corresponding MCInst. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 // 00015 00016 #include "AMDGPUMCInstLower.h" 00017 #include "AMDGPUAsmPrinter.h" 00018 #include "AMDGPUTargetMachine.h" 00019 #include "InstPrinter/AMDGPUInstPrinter.h" 00020 #include "R600InstrInfo.h" 00021 #include "SIInstrInfo.h" 00022 #include "llvm/CodeGen/MachineBasicBlock.h" 00023 #include "llvm/CodeGen/MachineInstr.h" 00024 #include "llvm/IR/Constants.h" 00025 #include "llvm/IR/GlobalVariable.h" 00026 #include "llvm/MC/MCCodeEmitter.h" 00027 #include "llvm/MC/MCContext.h" 00028 #include "llvm/MC/MCExpr.h" 00029 #include "llvm/MC/MCInst.h" 00030 #include "llvm/MC/MCObjectStreamer.h" 00031 #include "llvm/MC/MCStreamer.h" 00032 #include "llvm/Support/ErrorHandling.h" 00033 #include "llvm/Support/Format.h" 00034 #include <algorithm> 00035 00036 using namespace llvm; 00037 00038 AMDGPUMCInstLower::AMDGPUMCInstLower(MCContext &ctx, const AMDGPUSubtarget &st): 00039 Ctx(ctx), ST(st) 00040 { } 00041 00042 enum AMDGPUMCInstLower::SISubtarget 00043 AMDGPUMCInstLower::AMDGPUSubtargetToSISubtarget(unsigned) const { 00044 return AMDGPUMCInstLower::SI; 00045 } 00046 00047 unsigned AMDGPUMCInstLower::getMCOpcode(unsigned MIOpcode) const { 00048 00049 int MCOpcode = AMDGPU::getMCOpcode(MIOpcode, 00050 AMDGPUSubtargetToSISubtarget(ST.getGeneration())); 00051 if (MCOpcode == -1) 00052 MCOpcode = MIOpcode; 00053 00054 return MCOpcode; 00055 } 00056 00057 void AMDGPUMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const { 00058 00059 OutMI.setOpcode(getMCOpcode(MI->getOpcode())); 00060 00061 for (const MachineOperand &MO : MI->explicit_operands()) { 00062 MCOperand MCOp; 00063 switch (MO.getType()) { 00064 default: 00065 llvm_unreachable("unknown operand type"); 00066 case MachineOperand::MO_FPImmediate: { 00067 const APFloat &FloatValue = MO.getFPImm()->getValueAPF(); 00068 assert(&FloatValue.getSemantics() == &APFloat::IEEEsingle && 00069 "Only floating point immediates are supported at the moment."); 00070 MCOp = MCOperand::CreateFPImm(FloatValue.convertToFloat()); 00071 break; 00072 } 00073 case MachineOperand::MO_Immediate: 00074 MCOp = MCOperand::CreateImm(MO.getImm()); 00075 break; 00076 case MachineOperand::MO_Register: 00077 MCOp = MCOperand::CreateReg(MO.getReg()); 00078 break; 00079 case MachineOperand::MO_MachineBasicBlock: 00080 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create( 00081 MO.getMBB()->getSymbol(), Ctx)); 00082 break; 00083 case MachineOperand::MO_GlobalAddress: { 00084 const GlobalValue *GV = MO.getGlobal(); 00085 MCSymbol *Sym = Ctx.GetOrCreateSymbol(StringRef(GV->getName())); 00086 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(Sym, Ctx)); 00087 break; 00088 } 00089 case MachineOperand::MO_TargetIndex: { 00090 assert(MO.getIndex() == AMDGPU::TI_CONSTDATA_START); 00091 MCSymbol *Sym = Ctx.GetOrCreateSymbol(StringRef(END_OF_TEXT_LABEL_NAME)); 00092 const MCSymbolRefExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx); 00093 MCOp = MCOperand::CreateExpr(Expr); 00094 break; 00095 } 00096 } 00097 OutMI.addOperand(MCOp); 00098 } 00099 } 00100 00101 void AMDGPUAsmPrinter::EmitInstruction(const MachineInstr *MI) { 00102 AMDGPUMCInstLower MCInstLowering(OutContext, 00103 MF->getTarget().getSubtarget<AMDGPUSubtarget>()); 00104 00105 #ifdef _DEBUG 00106 StringRef Err; 00107 if (!TM.getSubtargetImpl()->getInstrInfo()->verifyInstruction(MI, Err)) { 00108 errs() << "Warning: Illegal instruction detected: " << Err << "\n"; 00109 MI->dump(); 00110 } 00111 #endif 00112 if (MI->isBundle()) { 00113 const MachineBasicBlock *MBB = MI->getParent(); 00114 MachineBasicBlock::const_instr_iterator I = MI; 00115 ++I; 00116 while (I != MBB->end() && I->isInsideBundle()) { 00117 EmitInstruction(I); 00118 ++I; 00119 } 00120 } else { 00121 MCInst TmpInst; 00122 MCInstLowering.lower(MI, TmpInst); 00123 EmitToStreamer(OutStreamer, TmpInst); 00124 00125 if (DisasmEnabled) { 00126 // Disassemble instruction/operands to text. 00127 DisasmLines.resize(DisasmLines.size() + 1); 00128 std::string &DisasmLine = DisasmLines.back(); 00129 raw_string_ostream DisasmStream(DisasmLine); 00130 00131 AMDGPUInstPrinter InstPrinter(*TM.getMCAsmInfo(), 00132 *TM.getSubtargetImpl()->getInstrInfo(), 00133 *TM.getSubtargetImpl()->getRegisterInfo()); 00134 InstPrinter.printInst(&TmpInst, DisasmStream, StringRef()); 00135 00136 // Disassemble instruction/operands to hex representation. 00137 SmallVector<MCFixup, 4> Fixups; 00138 SmallVector<char, 16> CodeBytes; 00139 raw_svector_ostream CodeStream(CodeBytes); 00140 00141 MCObjectStreamer &ObjStreamer = (MCObjectStreamer &)OutStreamer; 00142 MCCodeEmitter &InstEmitter = ObjStreamer.getAssembler().getEmitter(); 00143 InstEmitter.EncodeInstruction(TmpInst, CodeStream, Fixups, 00144 TM.getSubtarget<MCSubtargetInfo>()); 00145 CodeStream.flush(); 00146 00147 HexLines.resize(HexLines.size() + 1); 00148 std::string &HexLine = HexLines.back(); 00149 raw_string_ostream HexStream(HexLine); 00150 00151 for (size_t i = 0; i < CodeBytes.size(); i += 4) { 00152 unsigned int CodeDWord = *(unsigned int *)&CodeBytes[i]; 00153 HexStream << format("%s%08X", (i > 0 ? " " : ""), CodeDWord); 00154 } 00155 00156 DisasmStream.flush(); 00157 DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLine.size()); 00158 } 00159 } 00160 }