Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1 | //===-- SIMCCodeEmitter.cpp - SI Code Emitter -------------------------------===// |
| 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 The SI code emitter produces machine code that can be executed |
| 12 | /// directly on the GPU device. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 16 | #include "AMDGPU.h" |
Tom Stellard | 01825af | 2014-07-21 14:01:08 +0000 | [diff] [blame] | 17 | #include "MCTargetDesc/AMDGPUFixupKinds.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame^] | 18 | #include "MCTargetDesc/AMDGPUMCCodeEmitter.h" |
| 19 | #include "MCTargetDesc/AMDGPUMCTargetDesc.h" |
| 20 | #include "SIDefines.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCCodeEmitter.h" |
| 22 | #include "llvm/MC/MCContext.h" |
Chandler Carruth | be81023 | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCFixup.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCInst.h" |
| 25 | #include "llvm/MC/MCInstrInfo.h" |
| 26 | #include "llvm/MC/MCRegisterInfo.h" |
| 27 | #include "llvm/MC/MCSubtargetInfo.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
Christian Konig | c756cb99 | 2013-02-16 11:28:22 +0000 | [diff] [blame] | 33 | |
| 34 | /// \brief Helper type used in encoding |
| 35 | typedef union { |
| 36 | int32_t I; |
| 37 | float F; |
| 38 | } IntFloatUnion; |
| 39 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 40 | class SIMCCodeEmitter : public AMDGPUMCCodeEmitter { |
David Blaikie | 772d4f7 | 2013-02-18 23:11:17 +0000 | [diff] [blame] | 41 | SIMCCodeEmitter(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION; |
| 42 | void operator=(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 43 | const MCInstrInfo &MCII; |
| 44 | const MCRegisterInfo &MRI; |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 45 | MCContext &Ctx; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 46 | |
Christian Konig | c756cb99 | 2013-02-16 11:28:22 +0000 | [diff] [blame] | 47 | /// \brief Can this operand also contain immediate values? |
| 48 | bool isSrcOperand(const MCInstrDesc &Desc, unsigned OpNo) const; |
| 49 | |
| 50 | /// \brief Encode an fp or int literal |
| 51 | uint32_t getLitEncoding(const MCOperand &MO) const; |
| 52 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 53 | public: |
| 54 | SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri, |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 55 | MCContext &ctx) |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 56 | : MCII(mcii), MRI(mri), Ctx(ctx) { } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 57 | |
| 58 | ~SIMCCodeEmitter() { } |
| 59 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 60 | /// \brief Encode the instruction and write it to the OS. |
Craig Topper | 5656db4 | 2014-04-29 07:57:24 +0000 | [diff] [blame] | 61 | void EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
David Woodhouse | 9784cef | 2014-01-28 23:13:07 +0000 | [diff] [blame] | 62 | SmallVectorImpl<MCFixup> &Fixups, |
Craig Topper | 5656db4 | 2014-04-29 07:57:24 +0000 | [diff] [blame] | 63 | const MCSubtargetInfo &STI) const override; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 64 | |
| 65 | /// \returns the encoding for an MCOperand. |
Craig Topper | 5656db4 | 2014-04-29 07:57:24 +0000 | [diff] [blame] | 66 | uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO, |
| 67 | SmallVectorImpl<MCFixup> &Fixups, |
| 68 | const MCSubtargetInfo &STI) const override; |
Tom Stellard | 01825af | 2014-07-21 14:01:08 +0000 | [diff] [blame] | 69 | |
| 70 | /// \brief Use a fixup to encode the simm16 field for SOPP branch |
| 71 | /// instructions. |
| 72 | unsigned getSOPPBrEncoding(const MCInst &MI, unsigned OpNo, |
| 73 | SmallVectorImpl<MCFixup> &Fixups, |
| 74 | const MCSubtargetInfo &STI) const override; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | } // End anonymous namespace |
| 78 | |
| 79 | MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII, |
| 80 | const MCRegisterInfo &MRI, |
| 81 | const MCSubtargetInfo &STI, |
| 82 | MCContext &Ctx) { |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 83 | return new SIMCCodeEmitter(MCII, MRI, Ctx); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Christian Konig | c756cb99 | 2013-02-16 11:28:22 +0000 | [diff] [blame] | 86 | bool SIMCCodeEmitter::isSrcOperand(const MCInstrDesc &Desc, |
| 87 | unsigned OpNo) const { |
Tom Stellard | b655052 | 2015-01-12 19:33:18 +0000 | [diff] [blame] | 88 | unsigned OpType = Desc.OpInfo[OpNo].OperandType; |
| 89 | |
| 90 | return OpType == AMDGPU::OPERAND_REG_IMM32 || |
| 91 | OpType == AMDGPU::OPERAND_REG_INLINE_C; |
Christian Konig | c756cb99 | 2013-02-16 11:28:22 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO) const { |
| 95 | |
| 96 | IntFloatUnion Imm; |
| 97 | if (MO.isImm()) |
| 98 | Imm.I = MO.getImm(); |
| 99 | else if (MO.isFPImm()) |
| 100 | Imm.F = MO.getFPImm(); |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 101 | else if (MO.isExpr()) |
| 102 | return 255; |
Christian Konig | c756cb99 | 2013-02-16 11:28:22 +0000 | [diff] [blame] | 103 | else |
| 104 | return ~0; |
| 105 | |
| 106 | if (Imm.I >= 0 && Imm.I <= 64) |
| 107 | return 128 + Imm.I; |
| 108 | |
| 109 | if (Imm.I >= -16 && Imm.I <= -1) |
| 110 | return 192 + abs(Imm.I); |
| 111 | |
| 112 | if (Imm.F == 0.5f) |
| 113 | return 240; |
| 114 | |
| 115 | if (Imm.F == -0.5f) |
| 116 | return 241; |
| 117 | |
| 118 | if (Imm.F == 1.0f) |
| 119 | return 242; |
| 120 | |
| 121 | if (Imm.F == -1.0f) |
| 122 | return 243; |
| 123 | |
| 124 | if (Imm.F == 2.0f) |
| 125 | return 244; |
| 126 | |
| 127 | if (Imm.F == -2.0f) |
| 128 | return 245; |
| 129 | |
| 130 | if (Imm.F == 4.0f) |
| 131 | return 246; |
| 132 | |
Christian Konig | d76ed54 | 2013-02-26 17:51:57 +0000 | [diff] [blame] | 133 | if (Imm.F == -4.0f) |
Christian Konig | c756cb99 | 2013-02-16 11:28:22 +0000 | [diff] [blame] | 134 | return 247; |
| 135 | |
| 136 | return 255; |
| 137 | } |
| 138 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 139 | void SIMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
David Woodhouse | 9784cef | 2014-01-28 23:13:07 +0000 | [diff] [blame] | 140 | SmallVectorImpl<MCFixup> &Fixups, |
| 141 | const MCSubtargetInfo &STI) const { |
Christian Konig | c756cb99 | 2013-02-16 11:28:22 +0000 | [diff] [blame] | 142 | |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 143 | uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups, STI); |
Christian Konig | c756cb99 | 2013-02-16 11:28:22 +0000 | [diff] [blame] | 144 | const MCInstrDesc &Desc = MCII.get(MI.getOpcode()); |
| 145 | unsigned bytes = Desc.getSize(); |
| 146 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 147 | for (unsigned i = 0; i < bytes; i++) { |
| 148 | OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff)); |
| 149 | } |
Christian Konig | c756cb99 | 2013-02-16 11:28:22 +0000 | [diff] [blame] | 150 | |
| 151 | if (bytes > 4) |
| 152 | return; |
| 153 | |
| 154 | // Check for additional literals in SRC0/1/2 (Op 1/2/3) |
| 155 | for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) { |
| 156 | |
| 157 | // Check if this operand should be encoded as [SV]Src |
| 158 | if (!isSrcOperand(Desc, i)) |
| 159 | continue; |
| 160 | |
| 161 | // Is this operand a literal immediate? |
| 162 | const MCOperand &Op = MI.getOperand(i); |
| 163 | if (getLitEncoding(Op) != 255) |
| 164 | continue; |
| 165 | |
| 166 | // Yes! Encode it |
| 167 | IntFloatUnion Imm; |
| 168 | if (Op.isImm()) |
| 169 | Imm.I = Op.getImm(); |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 170 | else if (Op.isFPImm()) |
Christian Konig | c756cb99 | 2013-02-16 11:28:22 +0000 | [diff] [blame] | 171 | Imm.F = Op.getFPImm(); |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 172 | else { |
| 173 | assert(Op.isExpr()); |
| 174 | // This will be replaced with a fixup value. |
| 175 | Imm.I = 0; |
| 176 | } |
Christian Konig | c756cb99 | 2013-02-16 11:28:22 +0000 | [diff] [blame] | 177 | |
| 178 | for (unsigned j = 0; j < 4; j++) { |
| 179 | OS.write((uint8_t) ((Imm.I >> (8 * j)) & 0xff)); |
| 180 | } |
| 181 | |
| 182 | // Only one literal value allowed |
| 183 | break; |
| 184 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Tom Stellard | 01825af | 2014-07-21 14:01:08 +0000 | [diff] [blame] | 187 | unsigned SIMCCodeEmitter::getSOPPBrEncoding(const MCInst &MI, unsigned OpNo, |
| 188 | SmallVectorImpl<MCFixup> &Fixups, |
| 189 | const MCSubtargetInfo &STI) const { |
| 190 | const MCOperand &MO = MI.getOperand(OpNo); |
| 191 | |
| 192 | if (MO.isExpr()) { |
| 193 | const MCExpr *Expr = MO.getExpr(); |
| 194 | MCFixupKind Kind = (MCFixupKind)AMDGPU::fixup_si_sopp_br; |
| 195 | Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc())); |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | return getMachineOpValue(MI, MO, Fixups, STI); |
| 200 | } |
| 201 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 202 | uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI, |
| 203 | const MCOperand &MO, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 204 | SmallVectorImpl<MCFixup> &Fixups, |
| 205 | const MCSubtargetInfo &STI) const { |
Christian Konig | c756cb99 | 2013-02-16 11:28:22 +0000 | [diff] [blame] | 206 | if (MO.isReg()) |
Tom Stellard | 1c822a8 | 2013-02-07 19:39:45 +0000 | [diff] [blame] | 207 | return MRI.getEncodingValue(MO.getReg()); |
Christian Konig | c756cb99 | 2013-02-16 11:28:22 +0000 | [diff] [blame] | 208 | |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 209 | if (MO.isExpr()) { |
| 210 | const MCSymbolRefExpr *Expr = cast<MCSymbolRefExpr>(MO.getExpr()); |
| 211 | MCFixupKind Kind; |
| 212 | const MCSymbol *Sym = |
| 213 | Ctx.GetOrCreateSymbol(StringRef(END_OF_TEXT_LABEL_NAME)); |
| 214 | |
| 215 | if (&Expr->getSymbol() == Sym) { |
| 216 | // Add the offset to the beginning of the constant values. |
| 217 | Kind = (MCFixupKind)AMDGPU::fixup_si_end_of_text; |
| 218 | } else { |
| 219 | // This is used for constant data stored in .rodata. |
| 220 | Kind = (MCFixupKind)AMDGPU::fixup_si_rodata; |
| 221 | } |
| 222 | Fixups.push_back(MCFixup::Create(4, Expr, Kind, MI.getLoc())); |
| 223 | } |
| 224 | |
Christian Konig | c756cb99 | 2013-02-16 11:28:22 +0000 | [diff] [blame] | 225 | // Figure out the operand number, needed for isSrcOperand check |
| 226 | unsigned OpNo = 0; |
| 227 | for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) { |
| 228 | if (&MO == &MI.getOperand(OpNo)) |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | const MCInstrDesc &Desc = MCII.get(MI.getOpcode()); |
| 233 | if (isSrcOperand(Desc, OpNo)) { |
| 234 | uint32_t Enc = getLitEncoding(MO); |
| 235 | if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4)) |
| 236 | return Enc; |
| 237 | |
| 238 | } else if (MO.isImm()) |
| 239 | return MO.getImm(); |
| 240 | |
| 241 | llvm_unreachable("Encoding of this operand type is not supported yet."); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |