Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 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" |
Tom Stellard | 2e59a45 | 2014-06-13 01:32:00 +0000 | [diff] [blame^] | 18 | #include "AMDGPUTargetMachine.h" |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 19 | #include "InstPrinter/AMDGPUInstPrinter.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 20 | #include "R600InstrInfo.h" |
Tom Stellard | c721a23 | 2014-05-16 20:56:47 +0000 | [diff] [blame] | 21 | #include "SIInstrInfo.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 23 | #include "llvm/CodeGen/MachineInstr.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCCodeEmitter.h" |
Chandler Carruth | be81023 | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCExpr.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCInst.h" |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCObjectStreamer.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCStreamer.h" |
| 30 | #include "llvm/Support/ErrorHandling.h" |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Format.h" |
| 32 | #include <algorithm> |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace llvm; |
| 35 | |
Tom Stellard | c721a23 | 2014-05-16 20:56:47 +0000 | [diff] [blame] | 36 | AMDGPUMCInstLower::AMDGPUMCInstLower(MCContext &ctx, const AMDGPUSubtarget &st): |
| 37 | Ctx(ctx), ST(st) |
Tom Stellard | 9e90b58 | 2012-12-17 15:14:54 +0000 | [diff] [blame] | 38 | { } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 39 | |
Tom Stellard | c721a23 | 2014-05-16 20:56:47 +0000 | [diff] [blame] | 40 | enum AMDGPUMCInstLower::SISubtarget |
Aaron Ballman | 0dfed53 | 2014-05-19 14:29:04 +0000 | [diff] [blame] | 41 | AMDGPUMCInstLower::AMDGPUSubtargetToSISubtarget(unsigned) const { |
| 42 | return AMDGPUMCInstLower::SI; |
Tom Stellard | c721a23 | 2014-05-16 20:56:47 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | unsigned AMDGPUMCInstLower::getMCOpcode(unsigned MIOpcode) const { |
| 46 | |
| 47 | int MCOpcode = AMDGPU::getMCOpcode(MIOpcode, |
| 48 | AMDGPUSubtargetToSISubtarget(ST.getGeneration())); |
| 49 | if (MCOpcode == -1) |
| 50 | MCOpcode = MIOpcode; |
| 51 | |
| 52 | return MCOpcode; |
| 53 | } |
| 54 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 55 | void AMDGPUMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const { |
Tom Stellard | c721a23 | 2014-05-16 20:56:47 +0000 | [diff] [blame] | 56 | |
| 57 | OutMI.setOpcode(getMCOpcode(MI->getOpcode())); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 58 | |
David Blaikie | 2f77112 | 2014-04-05 22:42:04 +0000 | [diff] [blame] | 59 | for (const MachineOperand &MO : MI->explicit_operands()) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 60 | MCOperand MCOp; |
| 61 | switch (MO.getType()) { |
| 62 | default: |
| 63 | llvm_unreachable("unknown operand type"); |
| 64 | case MachineOperand::MO_FPImmediate: { |
| 65 | const APFloat &FloatValue = MO.getFPImm()->getValueAPF(); |
| 66 | assert(&FloatValue.getSemantics() == &APFloat::IEEEsingle && |
| 67 | "Only floating point immediates are supported at the moment."); |
| 68 | MCOp = MCOperand::CreateFPImm(FloatValue.convertToFloat()); |
| 69 | break; |
| 70 | } |
| 71 | case MachineOperand::MO_Immediate: |
| 72 | MCOp = MCOperand::CreateImm(MO.getImm()); |
| 73 | break; |
| 74 | case MachineOperand::MO_Register: |
| 75 | MCOp = MCOperand::CreateReg(MO.getReg()); |
| 76 | break; |
Tom Stellard | 9e90b58 | 2012-12-17 15:14:54 +0000 | [diff] [blame] | 77 | case MachineOperand::MO_MachineBasicBlock: |
| 78 | MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create( |
| 79 | MO.getMBB()->getSymbol(), Ctx)); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 80 | } |
| 81 | OutMI.addOperand(MCOp); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void AMDGPUAsmPrinter::EmitInstruction(const MachineInstr *MI) { |
Tom Stellard | c721a23 | 2014-05-16 20:56:47 +0000 | [diff] [blame] | 86 | AMDGPUMCInstLower MCInstLowering(OutContext, |
| 87 | MF->getTarget().getSubtarget<AMDGPUSubtarget>()); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 88 | |
Tom Stellard | 9b9e926 | 2014-02-28 21:36:41 +0000 | [diff] [blame] | 89 | #ifdef _DEBUG |
| 90 | StringRef Err; |
| 91 | if (!TM.getInstrInfo()->verifyInstruction(MI, Err)) { |
| 92 | errs() << "Warning: Illegal instruction detected: " << Err << "\n"; |
| 93 | MI->dump(); |
| 94 | } |
| 95 | #endif |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 96 | if (MI->isBundle()) { |
| 97 | const MachineBasicBlock *MBB = MI->getParent(); |
| 98 | MachineBasicBlock::const_instr_iterator I = MI; |
| 99 | ++I; |
| 100 | while (I != MBB->end() && I->isInsideBundle()) { |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 101 | EmitInstruction(I); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 102 | ++I; |
| 103 | } |
| 104 | } else { |
| 105 | MCInst TmpInst; |
| 106 | MCInstLowering.lower(MI, TmpInst); |
David Woodhouse | e6c13e4 | 2014-01-28 23:12:42 +0000 | [diff] [blame] | 107 | EmitToStreamer(OutStreamer, TmpInst); |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 108 | |
| 109 | if (DisasmEnabled) { |
| 110 | // Disassemble instruction/operands to text. |
| 111 | DisasmLines.resize(DisasmLines.size() + 1); |
| 112 | std::string &DisasmLine = DisasmLines.back(); |
| 113 | raw_string_ostream DisasmStream(DisasmLine); |
| 114 | |
| 115 | AMDGPUInstPrinter InstPrinter(*TM.getMCAsmInfo(), *TM.getInstrInfo(), |
| 116 | *TM.getRegisterInfo()); |
| 117 | InstPrinter.printInst(&TmpInst, DisasmStream, StringRef()); |
| 118 | |
| 119 | // Disassemble instruction/operands to hex representation. |
| 120 | SmallVector<MCFixup, 4> Fixups; |
| 121 | SmallVector<char, 16> CodeBytes; |
| 122 | raw_svector_ostream CodeStream(CodeBytes); |
| 123 | |
| 124 | MCObjectStreamer &ObjStreamer = (MCObjectStreamer &)OutStreamer; |
| 125 | MCCodeEmitter &InstEmitter = ObjStreamer.getAssembler().getEmitter(); |
David Woodhouse | 9784cef | 2014-01-28 23:13:07 +0000 | [diff] [blame] | 126 | InstEmitter.EncodeInstruction(TmpInst, CodeStream, Fixups, |
| 127 | TM.getSubtarget<MCSubtargetInfo>()); |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 128 | CodeStream.flush(); |
| 129 | |
| 130 | HexLines.resize(HexLines.size() + 1); |
| 131 | std::string &HexLine = HexLines.back(); |
| 132 | raw_string_ostream HexStream(HexLine); |
| 133 | |
| 134 | for (size_t i = 0; i < CodeBytes.size(); i += 4) { |
| 135 | unsigned int CodeDWord = *(unsigned int *)&CodeBytes[i]; |
| 136 | HexStream << format("%s%08X", (i > 0 ? " " : ""), CodeDWord); |
| 137 | } |
| 138 | |
| 139 | DisasmStream.flush(); |
| 140 | DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLine.size()); |
| 141 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 142 | } |
| 143 | } |