LLVM 24.0.0git
AMDGPUDisassembler.h
Go to the documentation of this file.
1//===- AMDGPUDisassembler.hpp - Disassembler for AMDGPU ISA -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10///
11/// This file contains declaration for AMDGPU ISA disassembler
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_TARGET_AMDGPU_DISASSEMBLER_AMDGPUDISASSEMBLER_H
16#define LLVM_LIB_TARGET_AMDGPU_DISASSEMBLER_AMDGPUDISASSEMBLER_H
17
18#include "SIDefines.h"
19#include "llvm/ADT/APInt.h"
22#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCInstrInfo.h"
25#include <memory>
26
27namespace llvm {
28
29class MCAsmInfo;
30class MCInst;
31class MCOperand;
32class MCSubtargetInfo;
33class Twine;
34
35//===----------------------------------------------------------------------===//
36// AMDGPUDisassembler
37//===----------------------------------------------------------------------===//
38
40private:
41 std::unique_ptr<MCInstrInfo const> const MCII;
42 const MCRegisterInfo &MRI;
43 const MCAsmInfo &MAI;
44 const unsigned HwModeRegClass;
45 const unsigned TargetMaxInstBytes;
46 mutable ArrayRef<uint8_t> Bytes;
47 mutable uint64_t Literal;
48 mutable bool HasLiteral;
49 mutable std::optional<bool> EnableWavefrontSize32;
50
51 // If the object's ELF e_flags enable xnack. TODO: Replace with TargetID
52 mutable bool XnackOnFromEFlags = false;
53 unsigned CodeObjectVersion;
54 const MCExpr *UCVersionW64Expr;
55 const MCExpr *UCVersionW32Expr;
56 const MCExpr *UCVersionMDPExpr;
57
58 const MCExpr *createConstantSymbolExpr(StringRef Id, int64_t Val);
59
60 bool decodeImmOperands(MCInst &MI, const MCInstrInfo &MCII) const;
61
62public:
64 MCInstrInfo const *MCII);
65 ~AMDGPUDisassembler() override = default;
66
67 void setABIVersion(unsigned Version) override;
68
69 void emitTargetIDIfSupported(raw_ostream &OS, unsigned EFlags) const override;
70
73 raw_ostream &CS) const override;
74
75 const char* getRegClassName(unsigned RegClassID) const;
76
78 MCOperand createRegOperand(unsigned RegClassID, unsigned Val) const;
79 MCOperand createSRegOperand(unsigned SRegClassID, unsigned Val) const;
80 MCOperand createVGPR16Operand(unsigned RegIdx, bool IsHi) const;
81
82 MCOperand errOperand(unsigned V, const Twine& ErrMsg) const;
83
84 template <typename InsnType>
85 DecodeStatus tryDecodeInst(const uint8_t *Table, MCInst &MI, InsnType Inst,
86 uint64_t Address, raw_ostream &Comments) const;
87 template <typename InsnType>
88 DecodeStatus tryDecodeInst(const uint8_t *Table1, const uint8_t *Table2,
89 MCInst &MI, InsnType Inst, uint64_t Address,
90 raw_ostream &Comments) const;
91
94 uint64_t Address) const override;
95
98 uint64_t KdAddress) const;
99
102 ArrayRef<uint8_t> Bytes,
103 raw_string_ostream &KdStream) const;
104
105 /// Decode as directives that handle COMPUTE_PGM_RSRC1.
106 /// \param FourByteBuffer - Bytes holding contents of COMPUTE_PGM_RSRC1.
107 /// \param KdStream - Stream to write the disassembled directives to.
108 // NOLINTNEXTLINE(readability-identifier-naming)
110 raw_string_ostream &KdStream) const;
111
112 /// Decode as directives that handle COMPUTE_PGM_RSRC2.
113 /// \param FourByteBuffer - Bytes holding contents of COMPUTE_PGM_RSRC2.
114 /// \param KdStream - Stream to write the disassembled directives to.
115 // NOLINTNEXTLINE(readability-identifier-naming)
117 raw_string_ostream &KdStream) const;
118
119 /// Decode as directives that handle COMPUTE_PGM_RSRC3.
120 /// \param FourByteBuffer - Bytes holding contents of COMPUTE_PGM_RSRC3.
121 /// \param KdStream - Stream to write the disassembled directives to.
122 // NOLINTNEXTLINE(readability-identifier-naming)
124 raw_string_ostream &KdStream) const;
125
126 void convertEXPInst(MCInst &MI) const;
127 void convertVINTERPInst(MCInst &MI) const;
128 void convertFMAanyK(MCInst &MI) const;
129 void convertSDWAInst(MCInst &MI) const;
130 bool convertMAIInst(MCInst &MI) const;
131 bool convertWMMAInst(MCInst &MI) const;
132 void convertDPP8Inst(MCInst &MI) const;
133 void convertMIMGInst(MCInst &MI) const;
134 void convertVOP3DPPInst(MCInst &MI) const;
135 void convertVOP3PDPPInst(MCInst &MI) const;
136 void convertVOPCDPPInst(MCInst &MI) const;
137 void convertVOPC64DPPInst(MCInst &MI) const;
138 void convertMacDPPInst(MCInst &MI) const;
139 void convertTrue16OpSel(MCInst &MI) const;
140
141 unsigned getVgprClassId(unsigned Width) const;
142 unsigned getAgprClassId(unsigned Width) const;
143
144 /// Return the SGPR/TTMP register class accepted by source decoding for
145 /// \p Width, or std::nullopt if that width has no supported encoding.
146 std::optional<unsigned> getSgprClassId(unsigned Width) const;
147 std::optional<unsigned> getTtmpClassId(unsigned Width) const;
148
149 static MCOperand decodeIntImmed(unsigned Imm);
150
154 const MCOperandInfo &OpDesc) const;
156
157 MCOperand decodeSrcOp(const MCInst &Inst, unsigned Width, unsigned Val) const;
158
159 MCOperand decodeNonVGPRSrcOp(const MCInst &Inst, unsigned Width,
160 unsigned Val) const;
161
162 MCOperand decodeVOPDDstYOp(MCInst &Inst, unsigned Val) const;
163 MCOperand decodeSpecialReg32(unsigned Val) const;
164 MCOperand decodeSpecialReg64(unsigned Val) const;
165 MCOperand decodeSpecialReg96Plus(unsigned Val) const;
166
167 MCOperand decodeSDWASrc(unsigned Width, unsigned Val) const;
168 MCOperand decodeSDWASrc16(unsigned Val) const;
169 MCOperand decodeSDWASrc32(unsigned Val) const;
170 MCOperand decodeSDWAVopcDst(unsigned Val) const;
171
172 MCOperand decodeBoolReg(const MCInst &Inst, unsigned Val) const;
173 MCOperand decodeSplitBarrier(const MCInst &Inst, unsigned Val) const;
174 MCOperand decodeDpp8FI(unsigned Val) const;
175
176 MCOperand decodeVersionImm(unsigned Imm) const;
177
178 int getTTmpIdx(unsigned Val) const;
179
180 const MCInstrInfo *getMCII() const { return MCII.get(); }
181
182 bool isVI() const;
183 bool isGFX9() const;
184 bool isGFX90A() const;
185 bool isGFX9Plus() const;
186 bool isGFX10() const;
187 bool isGFX10Plus() const;
188 bool isGFX11() const;
189 bool isGFX1170() const;
190 bool isGFX11Plus() const;
191 bool isGFX12() const;
192 bool isGFX12Plus() const;
193 bool isGFX1250() const;
194 bool isGFX1250Plus() const;
195 bool isGFX13() const;
196 bool isGFX13Plus() const;
197
198 bool hasArchitectedFlatScratch() const;
199 bool hasKernargPreload() const;
200
201 bool isMacDPP(MCInst &MI) const;
202
203 /// Check if the instruction is a buffer operation (MUBUF, MTBUF, or S_BUFFER)
204 bool isBufferInstruction(const MCInst &MI) const;
205};
206
207//===----------------------------------------------------------------------===//
208// AMDGPUSymbolizer
209//===----------------------------------------------------------------------===//
210
212private:
213 void *DisInfo;
214 std::vector<uint64_t> ReferencedAddresses;
215
216public:
217 AMDGPUSymbolizer(MCContext &Ctx, std::unique_ptr<MCRelocationInfo> &&RelInfo,
218 void *disInfo)
219 : MCSymbolizer(Ctx, std::move(RelInfo)), DisInfo(disInfo) {}
220
221 bool tryAddingSymbolicOperand(MCInst &Inst, raw_ostream &cStream,
222 int64_t Value, uint64_t Address, bool IsBranch,
223 uint64_t Offset, uint64_t OpSize,
224 uint64_t InstSize) override;
225
227 int64_t Value,
228 uint64_t Address) override;
229
231 return ReferencedAddresses;
232 }
233};
234
235} // end namespace llvm
236
237#endif // LLVM_LIB_TARGET_AMDGPU_DISASSEMBLER_AMDGPUDISASSEMBLER_H
unsigned Imm
unsigned uint64_t
This file implements a class to represent arbitrary precision integral constant values and operations...
IRTranslator LLVM IR MI
Register Reg
This file defines the SmallString class.
std::optional< unsigned > getSgprClassId(unsigned Width) const
Return the SGPR/TTMP register class accepted by source decoding for Width, or std::nullopt if that wi...
const MCInstrInfo * getMCII() const
MCOperand decodeNonVGPRSrcOp(const MCInst &Inst, unsigned Width, unsigned Val) const
MCOperand decodeLiteral64Constant() const
void convertVOPC64DPPInst(MCInst &MI) const
bool isBufferInstruction(const MCInst &MI) const
Check if the instruction is a buffer operation (MUBUF, MTBUF, or S_BUFFER)
void convertEXPInst(MCInst &MI) const
MCOperand decodeSpecialReg64(unsigned Val) const
const char * getRegClassName(unsigned RegClassID) const
Expected< bool > decodeCOMPUTE_PGM_RSRC1(uint32_t FourByteBuffer, raw_string_ostream &KdStream) const
Decode as directives that handle COMPUTE_PGM_RSRC1.
MCOperand decodeSplitBarrier(const MCInst &Inst, unsigned Val) const
Expected< bool > decodeKernelDescriptorDirective(DataExtractor::Cursor &Cursor, ArrayRef< uint8_t > Bytes, raw_string_ostream &KdStream) const
void convertVOPCDPPInst(MCInst &MI) const
MCOperand decodeSpecialReg96Plus(unsigned Val) const
MCOperand decodeSDWASrc32(unsigned Val) const
void setABIVersion(unsigned Version) override
ELF-specific, set the ABI version from the object header.
Expected< bool > decodeCOMPUTE_PGM_RSRC2(uint32_t FourByteBuffer, raw_string_ostream &KdStream) const
Decode as directives that handle COMPUTE_PGM_RSRC2.
unsigned getAgprClassId(unsigned Width) const
MCOperand decodeDpp8FI(unsigned Val) const
MCOperand decodeSDWASrc(unsigned Width, unsigned Val) const
void convertFMAanyK(MCInst &MI) const
DecodeStatus tryDecodeInst(const uint8_t *Table, MCInst &MI, InsnType Inst, uint64_t Address, raw_ostream &Comments) const
void convertMacDPPInst(MCInst &MI) const
MCOperand decodeVOPDDstYOp(MCInst &Inst, unsigned Val) const
void convertDPP8Inst(MCInst &MI) const
MCOperand createVGPR16Operand(unsigned RegIdx, bool IsHi) const
MCOperand errOperand(unsigned V, const Twine &ErrMsg) const
MCOperand decodeVersionImm(unsigned Imm) const
Expected< bool > decodeKernelDescriptor(StringRef KdName, ArrayRef< uint8_t > Bytes, uint64_t KdAddress) const
void convertVOP3DPPInst(MCInst &MI) const
void convertTrue16OpSel(MCInst &MI) const
MCOperand decodeSrcOp(const MCInst &Inst, unsigned Width, unsigned Val) const
bool convertMAIInst(MCInst &MI) const
f8f6f4 instructions have different pseudos depending on the used formats.
MCOperand decodeMandatoryLiteralConstant(unsigned Imm) const
MCOperand decodeLiteralConstant(const MCInstrDesc &Desc, const MCOperandInfo &OpDesc) const
Expected< bool > decodeCOMPUTE_PGM_RSRC3(uint32_t FourByteBuffer, raw_string_ostream &KdStream) const
Decode as directives that handle COMPUTE_PGM_RSRC3.
AMDGPUDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, MCInstrInfo const *MCII)
MCOperand decodeSpecialReg32(unsigned Val) const
MCOperand createRegOperand(MCRegister Reg) const
MCOperand decodeSDWAVopcDst(unsigned Val) const
~AMDGPUDisassembler() override=default
void convertVINTERPInst(MCInst &MI) const
void convertSDWAInst(MCInst &MI) const
static MCOperand decodeIntImmed(unsigned Imm)
MCOperand decodeBoolReg(const MCInst &Inst, unsigned Val) const
void emitTargetIDIfSupported(raw_ostream &OS, unsigned EFlags) const override
Emit something based on ELF's e_flags if the target needs to.
unsigned getVgprClassId(unsigned Width) const
DecodeStatus getInstruction(MCInst &MI, uint64_t &Size, ArrayRef< uint8_t > Bytes, uint64_t Address, raw_ostream &CS) const override
Returns the disassembly of a single instruction.
std::optional< unsigned > getTtmpClassId(unsigned Width) const
MCOperand decodeMandatoryLiteral64Constant(uint64_t Imm) const
void convertMIMGInst(MCInst &MI) const
bool isMacDPP(MCInst &MI) const
int getTTmpIdx(unsigned Val) const
void convertVOP3PDPPInst(MCInst &MI) const
bool convertWMMAInst(MCInst &MI) const
MCOperand createSRegOperand(unsigned SRegClassID, unsigned Val) const
MCOperand decodeSDWASrc16(unsigned Val) const
Expected< bool > onSymbolStart(SymbolInfoTy &Symbol, uint64_t &Size, ArrayRef< uint8_t > Bytes, uint64_t Address) const override
Used to perform separate target specific disassembly for a particular symbol.
ArrayRef< uint64_t > getReferencedAddresses() const override
Get the MCSymbolizer's list of addresses that were referenced by symbolizable operands but not resolv...
bool tryAddingSymbolicOperand(MCInst &Inst, raw_ostream &cStream, int64_t Value, uint64_t Address, bool IsBranch, uint64_t Offset, uint64_t OpSize, uint64_t InstSize) override
Try to add a symbolic operand instead of Value to the MCInst.
AMDGPUSymbolizer(MCContext &Ctx, std::unique_ptr< MCRelocationInfo > &&RelInfo, void *disInfo)
void tryAddingPcLoadReferenceComment(raw_ostream &cStream, int64_t Value, uint64_t Address) override
Try to add a comment on the PC-relative load.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Tagged union holding either a T or a Error.
Definition Error.h:485
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:67
Context object for machine code objects.
Definition MCContext.h:83
MCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
const MCSubtargetInfo & STI
DecodeStatus
Ternary decode status.
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
Describe properties that are true of each instruction in the target description file.
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition MCInstrInfo.h:89
This holds information about one operand of a machine instruction, indicating the register class for ...
Definition MCInstrDesc.h:86
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Generic base class for all target subtargets.
MCSymbolizer(MCContext &Ctx, std::unique_ptr< MCRelocationInfo > RelInfo)
Construct an MCSymbolizer, taking ownership of RelInfo.
std::unique_ptr< MCRelocationInfo > RelInfo
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
Op::Description Desc
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878