blob: 0ce90284d6731f1ff06b2027d148c641fc71fd74 [file] [log] [blame]
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001//===- AMDGPUBaseInfo.h - Top level definitions for AMDGPU ------*- C++ -*-===//
Tom Stellard347ac792015-06-26 21:15:07 +00002//
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#ifndef LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H
11#define LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H
12
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000013#include "AMDGPU.h"
Tom Stellard347ac792015-06-26 21:15:07 +000014#include "AMDKernelCodeT.h"
Matt Arsenault4bd72362016-12-10 00:39:12 +000015#include "SIDefines.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000016#include "llvm/ADT/StringRef.h"
17#include "llvm/IR/CallingConv.h"
18#include "llvm/MC/MCInstrDesc.h"
19#include "llvm/Support/Compiler.h"
20#include "llvm/Support/ErrorHandling.h"
21#include <cstdint>
22#include <utility>
Matt Arsenault4bd72362016-12-10 00:39:12 +000023
Sam Koltona3ec5c12016-10-07 14:46:06 +000024#define GET_INSTRINFO_OPERAND_ENUM
25#include "AMDGPUGenInstrInfo.inc"
26#undef GET_INSTRINFO_OPERAND_ENUM
27
Tom Stellard347ac792015-06-26 21:15:07 +000028namespace llvm {
29
30class FeatureBitset;
Tom Stellardac00eb52015-12-15 16:26:16 +000031class Function;
Tom Stellarde3b5aea2015-12-02 17:00:42 +000032class GlobalValue;
Tom Stellard08efb7e2017-01-27 18:41:14 +000033class MachineMemOperand;
Tom Stellarde135ffd2015-09-25 21:41:28 +000034class MCContext;
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +000035class MCRegisterClass;
Sam Kolton1eeb11b2016-09-09 14:44:04 +000036class MCRegisterInfo;
Tom Stellarde135ffd2015-09-25 21:41:28 +000037class MCSection;
Tom Stellard2b65ed32015-12-21 18:44:27 +000038class MCSubtargetInfo;
Eugene Zelenkod96089b2017-02-14 00:33:36 +000039class Triple;
Tom Stellard347ac792015-06-26 21:15:07 +000040
41namespace AMDGPU {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000042namespace IsaInfo {
Sam Koltona3ec5c12016-10-07 14:46:06 +000043
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000044enum {
45 // The closed Vulkan driver sets 96, which limits the wave count to 8 but
46 // doesn't spill SGPRs as much as when 80 is set.
47 FIXED_NUM_SGPRS_FOR_INIT_BUG = 96
48};
49
50/// \brief Instruction set architecture version.
Tom Stellard347ac792015-06-26 21:15:07 +000051struct IsaVersion {
52 unsigned Major;
53 unsigned Minor;
54 unsigned Stepping;
55};
56
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000057/// \returns Isa version for given subtarget \p Features.
Tom Stellard347ac792015-06-26 21:15:07 +000058IsaVersion getIsaVersion(const FeatureBitset &Features);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000059
60/// \returns Wavefront size for given subtarget \p Features.
61unsigned getWavefrontSize(const FeatureBitset &Features);
62
63/// \returns Local memory size in bytes for given subtarget \p Features.
64unsigned getLocalMemorySize(const FeatureBitset &Features);
65
66/// \returns Number of execution units per compute unit for given subtarget \p
67/// Features.
68unsigned getEUsPerCU(const FeatureBitset &Features);
69
70/// \returns Maximum number of work groups per compute unit for given subtarget
71/// \p Features and limited by given \p FlatWorkGroupSize.
72unsigned getMaxWorkGroupsPerCU(const FeatureBitset &Features,
73 unsigned FlatWorkGroupSize);
74
75/// \returns Maximum number of waves per compute unit for given subtarget \p
76/// Features without any kind of limitation.
77unsigned getMaxWavesPerCU(const FeatureBitset &Features);
78
79/// \returns Maximum number of waves per compute unit for given subtarget \p
80/// Features and limited by given \p FlatWorkGroupSize.
81unsigned getMaxWavesPerCU(const FeatureBitset &Features,
82 unsigned FlatWorkGroupSize);
83
84/// \returns Minimum number of waves per execution unit for given subtarget \p
85/// Features.
86unsigned getMinWavesPerEU(const FeatureBitset &Features);
87
88/// \returns Maximum number of waves per execution unit for given subtarget \p
89/// Features without any kind of limitation.
90unsigned getMaxWavesPerEU(const FeatureBitset &Features);
91
92/// \returns Maximum number of waves per execution unit for given subtarget \p
93/// Features and limited by given \p FlatWorkGroupSize.
94unsigned getMaxWavesPerEU(const FeatureBitset &Features,
95 unsigned FlatWorkGroupSize);
96
97/// \returns Minimum flat work group size for given subtarget \p Features.
98unsigned getMinFlatWorkGroupSize(const FeatureBitset &Features);
99
100/// \returns Maximum flat work group size for given subtarget \p Features.
101unsigned getMaxFlatWorkGroupSize(const FeatureBitset &Features);
102
103/// \returns Number of waves per work group for given subtarget \p Features and
104/// limited by given \p FlatWorkGroupSize.
105unsigned getWavesPerWorkGroup(const FeatureBitset &Features,
106 unsigned FlatWorkGroupSize);
107
108/// \returns SGPR allocation granularity for given subtarget \p Features.
109unsigned getSGPRAllocGranule(const FeatureBitset &Features);
110
111/// \returns SGPR encoding granularity for given subtarget \p Features.
112unsigned getSGPREncodingGranule(const FeatureBitset &Features);
113
114/// \returns Total number of SGPRs for given subtarget \p Features.
115unsigned getTotalNumSGPRs(const FeatureBitset &Features);
116
117/// \returns Addressable number of SGPRs for given subtarget \p Features.
118unsigned getAddressableNumSGPRs(const FeatureBitset &Features);
119
120/// \returns Minimum number of SGPRs that meets the given number of waves per
121/// execution unit requirement for given subtarget \p Features.
122unsigned getMinNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
123
124/// \returns Maximum number of SGPRs that meets the given number of waves per
125/// execution unit requirement for given subtarget \p Features.
126unsigned getMaxNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU,
127 bool Addressable);
128
129/// \returns VGPR allocation granularity for given subtarget \p Features.
130unsigned getVGPRAllocGranule(const FeatureBitset &Features);
131
132/// \returns VGPR encoding granularity for given subtarget \p Features.
133unsigned getVGPREncodingGranule(const FeatureBitset &Features);
134
135/// \returns Total number of VGPRs for given subtarget \p Features.
136unsigned getTotalNumVGPRs(const FeatureBitset &Features);
137
138/// \returns Addressable number of VGPRs for given subtarget \p Features.
139unsigned getAddressableNumVGPRs(const FeatureBitset &Features);
140
141/// \returns Minimum number of VGPRs that meets given number of waves per
142/// execution unit requirement for given subtarget \p Features.
143unsigned getMinNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
144
145/// \returns Maximum number of VGPRs that meets given number of waves per
146/// execution unit requirement for given subtarget \p Features.
147unsigned getMaxNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
148
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000149} // end namespace IsaInfo
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000150
151LLVM_READONLY
152int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx);
153
Tom Stellardff7416b2015-06-26 21:58:31 +0000154void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
155 const FeatureBitset &Features);
Tom Stellarde135ffd2015-09-25 21:41:28 +0000156MCSection *getHSATextSection(MCContext &Ctx);
Tom Stellard347ac792015-06-26 21:15:07 +0000157
Tom Stellard00f2f912015-12-02 19:47:57 +0000158MCSection *getHSADataGlobalAgentSection(MCContext &Ctx);
159
160MCSection *getHSADataGlobalProgramSection(MCContext &Ctx);
161
Tom Stellard9760f032015-12-03 03:34:32 +0000162MCSection *getHSARodataReadonlyAgentSection(MCContext &Ctx);
163
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000164bool isGroupSegment(const GlobalValue *GV, AMDGPUAS AS);
165bool isGlobalSegment(const GlobalValue *GV, AMDGPUAS AS);
166bool isReadOnlySegment(const GlobalValue *GV, AMDGPUAS AS);
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000167
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +0000168/// \returns True if constants should be emitted to .text section for given
169/// target triple \p TT, false otherwise.
170bool shouldEmitConstantsToTextSection(const Triple &TT);
171
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000172/// \returns Integer value requested using \p F's \p Name attribute.
173///
174/// \returns \p Default if attribute is not present.
175///
176/// \returns \p Default and emits error if requested value cannot be converted
177/// to integer.
Matt Arsenault83002722016-05-12 02:45:18 +0000178int getIntegerAttribute(const Function &F, StringRef Name, int Default);
179
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000180/// \returns A pair of integer values requested using \p F's \p Name attribute
181/// in "first[,second]" format ("second" is optional unless \p OnlyFirstRequired
182/// is false).
183///
184/// \returns \p Default if attribute is not present.
185///
186/// \returns \p Default and emits error if one of the requested values cannot be
187/// converted to integer, or \p OnlyFirstRequired is false and "second" value is
188/// not present.
189std::pair<int, int> getIntegerPairAttribute(const Function &F,
190 StringRef Name,
191 std::pair<int, int> Default,
192 bool OnlyFirstRequired = false);
193
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000194/// \returns Vmcnt bit mask for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000195unsigned getVmcntBitMask(const IsaInfo::IsaVersion &Version);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000196
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000197/// \returns Expcnt bit mask for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000198unsigned getExpcntBitMask(const IsaInfo::IsaVersion &Version);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000199
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000200/// \returns Lgkmcnt bit mask for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000201unsigned getLgkmcntBitMask(const IsaInfo::IsaVersion &Version);
202
203/// \returns Waitcnt bit mask for given isa \p Version.
204unsigned getWaitcntBitMask(const IsaInfo::IsaVersion &Version);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000205
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000206/// \returns Decoded Vmcnt from given \p Waitcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000207unsigned decodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000208
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000209/// \returns Decoded Expcnt from given \p Waitcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000210unsigned decodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000211
212/// \returns Decoded Lgkmcnt from given \p Waitcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000213unsigned decodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000214
215/// \brief Decodes Vmcnt, Expcnt and Lgkmcnt from given \p Waitcnt for given isa
216/// \p Version, and writes decoded values into \p Vmcnt, \p Expcnt and
217/// \p Lgkmcnt respectively.
218///
219/// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are decoded as follows:
Matt Arsenaulte823d922017-02-18 18:29:53 +0000220/// \p Vmcnt = \p Waitcnt[3:0] (pre-gfx9 only)
221/// \p Vmcnt = \p Waitcnt[3:0] | \p Waitcnt[15:14] (gfx9+ only)
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000222/// \p Expcnt = \p Waitcnt[6:4]
223/// \p Lgkmcnt = \p Waitcnt[11:8]
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000224void decodeWaitcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000225 unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt);
226
227/// \returns \p Waitcnt with encoded \p Vmcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000228unsigned encodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
229 unsigned Vmcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000230
231/// \returns \p Waitcnt with encoded \p Expcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000232unsigned encodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
233 unsigned Expcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000234
235/// \returns \p Waitcnt with encoded \p Lgkmcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000236unsigned encodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
237 unsigned Lgkmcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000238
239/// \brief Encodes \p Vmcnt, \p Expcnt and \p Lgkmcnt into Waitcnt for given isa
240/// \p Version.
241///
242/// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are encoded as follows:
Matt Arsenaulte823d922017-02-18 18:29:53 +0000243/// Waitcnt[3:0] = \p Vmcnt (pre-gfx9 only)
244/// Waitcnt[3:0] = \p Vmcnt[3:0] (gfx9+ only)
245/// Waitcnt[6:4] = \p Expcnt
246/// Waitcnt[11:8] = \p Lgkmcnt
247/// Waitcnt[15:14] = \p Vmcnt[5:4] (gfx9+ only)
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000248///
249/// \returns Waitcnt with encoded \p Vmcnt, \p Expcnt and \p Lgkmcnt for given
250/// isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000251unsigned encodeWaitcnt(const IsaInfo::IsaVersion &Version,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000252 unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000253
Marek Olsakfccabaf2016-01-13 11:45:36 +0000254unsigned getInitialPSInputAddr(const Function &F);
255
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000256bool isShader(CallingConv::ID cc);
257bool isCompute(CallingConv::ID cc);
Tom Stellardac00eb52015-12-15 16:26:16 +0000258
Tom Stellard2b65ed32015-12-21 18:44:27 +0000259bool isSI(const MCSubtargetInfo &STI);
260bool isCI(const MCSubtargetInfo &STI);
261bool isVI(const MCSubtargetInfo &STI);
262
263/// If \p Reg is a pseudo reg, return the correct hardware register given
264/// \p STI otherwise return \p Reg.
265unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI);
266
Dmitry Preobrazhensky03880f82017-03-03 14:31:06 +0000267/// \brief Convert hardware register \p Reg to a pseudo register
268LLVM_READNONE
269unsigned mc2PseudoReg(unsigned Reg);
270
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000271/// \brief Can this operand also contain immediate values?
272bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo);
273
274/// \brief Is this floating-point operand?
275bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo);
276
277/// \brief Does this opearnd support only inlinable literals?
278bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo);
279
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000280/// \brief Get the size in bits of a register from the register class \p RC.
Tom Stellardb133fbb2016-10-27 23:05:31 +0000281unsigned getRegBitWidth(unsigned RCID);
282
283/// \brief Get the size in bits of a register from the register class \p RC.
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000284unsigned getRegBitWidth(const MCRegisterClass &RC);
285
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000286/// \brief Get size of register operand
287unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
288 unsigned OpNo);
289
Matt Arsenault4bd72362016-12-10 00:39:12 +0000290LLVM_READNONE
291inline unsigned getOperandSize(const MCOperandInfo &OpInfo) {
292 switch (OpInfo.OperandType) {
293 case AMDGPU::OPERAND_REG_IMM_INT32:
294 case AMDGPU::OPERAND_REG_IMM_FP32:
295 case AMDGPU::OPERAND_REG_INLINE_C_INT32:
296 case AMDGPU::OPERAND_REG_INLINE_C_FP32:
297 return 4;
298
299 case AMDGPU::OPERAND_REG_IMM_INT64:
300 case AMDGPU::OPERAND_REG_IMM_FP64:
301 case AMDGPU::OPERAND_REG_INLINE_C_INT64:
302 case AMDGPU::OPERAND_REG_INLINE_C_FP64:
303 return 8;
304
305 case AMDGPU::OPERAND_REG_IMM_INT16:
306 case AMDGPU::OPERAND_REG_IMM_FP16:
307 case AMDGPU::OPERAND_REG_INLINE_C_INT16:
308 case AMDGPU::OPERAND_REG_INLINE_C_FP16:
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000309 case AMDGPU::OPERAND_REG_INLINE_C_V2INT16:
310 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
Matt Arsenault4bd72362016-12-10 00:39:12 +0000311 return 2;
312
313 default:
314 llvm_unreachable("unhandled operand type");
315 }
316}
317
318LLVM_READNONE
319inline unsigned getOperandSize(const MCInstrDesc &Desc, unsigned OpNo) {
320 return getOperandSize(Desc.OpInfo[OpNo]);
321}
322
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000323/// \brief Is this literal inlinable
Matt Arsenault26faed32016-12-05 22:26:17 +0000324LLVM_READNONE
325bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi);
326
327LLVM_READNONE
328bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi);
329
Matt Arsenault4bd72362016-12-10 00:39:12 +0000330LLVM_READNONE
331bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000332
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000333LLVM_READNONE
334bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi);
335
Tom Stellard08efb7e2017-01-27 18:41:14 +0000336bool isUniformMMO(const MachineMemOperand *MMO);
337
338/// \returns The encoding that will be used for \p ByteOffset in the SMRD
339/// offset field.
340int64_t getSMRDEncodedOffset(const MCSubtargetInfo &ST, int64_t ByteOffset);
341
342/// \returns true if this offset is small enough to fit in the SMRD
343/// offset field. \p ByteOffset should be the offset in bytes and
344/// not the encoded offset.
345bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset);
346
Tom Stellard347ac792015-06-26 21:15:07 +0000347} // end namespace AMDGPU
348} // end namespace llvm
349
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000350#endif // LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H