blob: be88ebe9972e265fe3ed675adc566403f27bf37a [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Tom Stellard347ac792015-06-26 21:15:07 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H
10#define LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H
11
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000012#include "AMDGPU.h"
Tom Stellard347ac792015-06-26 21:15:07 +000013#include "AMDKernelCodeT.h"
Matt Arsenault4bd72362016-12-10 00:39:12 +000014#include "SIDefines.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000015#include "llvm/ADT/StringRef.h"
16#include "llvm/IR/CallingConv.h"
17#include "llvm/MC/MCInstrDesc.h"
Scott Linder1e8c2c72018-06-21 19:38:56 +000018#include "llvm/Support/AMDHSAKernelDescriptor.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000019#include "llvm/Support/Compiler.h"
20#include "llvm/Support/ErrorHandling.h"
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +000021#include "llvm/Support/TargetParser.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000022#include <cstdint>
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +000023#include <string>
Eugene Zelenkod96089b2017-02-14 00:33:36 +000024#include <utility>
Matt Arsenault4bd72362016-12-10 00:39:12 +000025
Tom Stellard347ac792015-06-26 21:15:07 +000026namespace llvm {
27
Matt Arsenault894e53d2017-07-26 20:39:42 +000028class Argument;
Tim Renouf4f703f52018-08-21 11:07:10 +000029class AMDGPUSubtarget;
Tom Stellard347ac792015-06-26 21:15:07 +000030class FeatureBitset;
Tom Stellardac00eb52015-12-15 16:26:16 +000031class Function;
Tim Renouf4f703f52018-08-21 11:07:10 +000032class GCNSubtarget;
Tom Stellarde3b5aea2015-12-02 17:00:42 +000033class GlobalValue;
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;
Scott Linder1e8c2c72018-06-21 19:38:56 +000039class MachineMemOperand;
Eugene Zelenkod96089b2017-02-14 00:33:36 +000040class Triple;
Tom Stellard347ac792015-06-26 21:15:07 +000041
42namespace AMDGPU {
Nicolai Haehnle0ab200b2018-06-21 13:36:44 +000043
44#define GET_MIMGBaseOpcode_DECL
Nicolai Haehnle7a9c03f2018-06-21 13:36:57 +000045#define GET_MIMGDim_DECL
Nicolai Haehnle0ab200b2018-06-21 13:36:44 +000046#define GET_MIMGEncoding_DECL
Ryan Taylor894c8fd2018-08-01 12:12:01 +000047#define GET_MIMGLZMapping_DECL
Piotr Sobczak9b11e932019-06-10 15:58:51 +000048#define GET_MIMGMIPMapping_DECL
Nicolai Haehnle0ab200b2018-06-21 13:36:44 +000049#include "AMDGPUGenSearchableTables.inc"
50
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000051namespace IsaInfo {
Sam Koltona3ec5c12016-10-07 14:46:06 +000052
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000053enum {
54 // The closed Vulkan driver sets 96, which limits the wave count to 8 but
55 // doesn't spill SGPRs as much as when 80 is set.
Konstantin Zhuravlyovc72ece62018-05-16 20:47:48 +000056 FIXED_NUM_SGPRS_FOR_INIT_BUG = 96,
57 TRAP_NUM_SGPRS = 16
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000058};
59
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000060/// Streams isa version string for given subtarget \p STI into \p Stream.
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +000061void streamIsaVersion(const MCSubtargetInfo *STI, raw_ostream &Stream);
62
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +000063/// \returns True if given subtarget \p STI supports code object version 3,
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +000064/// false otherwise.
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +000065bool hasCodeObjectV3(const MCSubtargetInfo *STI);
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +000066
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +000067/// \returns Wavefront size for given subtarget \p STI.
68unsigned getWavefrontSize(const MCSubtargetInfo *STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000069
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +000070/// \returns Local memory size in bytes for given subtarget \p STI.
71unsigned getLocalMemorySize(const MCSubtargetInfo *STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000072
73/// \returns Number of execution units per compute unit for given subtarget \p
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +000074/// STI.
75unsigned getEUsPerCU(const MCSubtargetInfo *STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000076
77/// \returns Maximum number of work groups per compute unit for given subtarget
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +000078/// \p STI and limited by given \p FlatWorkGroupSize.
79unsigned getMaxWorkGroupsPerCU(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000080 unsigned FlatWorkGroupSize);
81
82/// \returns Maximum number of waves per compute unit for given subtarget \p
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +000083/// STI without any kind of limitation.
84unsigned getMaxWavesPerCU(const MCSubtargetInfo *STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000085
86/// \returns Maximum number of waves per compute unit for given subtarget \p
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +000087/// STI and limited by given \p FlatWorkGroupSize.
88unsigned getMaxWavesPerCU(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000089 unsigned FlatWorkGroupSize);
90
91/// \returns Minimum number of waves per execution unit for given subtarget \p
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +000092/// STI.
93unsigned getMinWavesPerEU(const MCSubtargetInfo *STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000094
95/// \returns Maximum number of waves per execution unit for given subtarget \p
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +000096/// STI without any kind of limitation.
Tom Stellardc5a154d2018-06-28 23:47:12 +000097unsigned getMaxWavesPerEU();
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000098
99/// \returns Maximum number of waves per execution unit for given subtarget \p
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000100/// STI and limited by given \p FlatWorkGroupSize.
101unsigned getMaxWavesPerEU(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000102 unsigned FlatWorkGroupSize);
103
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000104/// \returns Minimum flat work group size for given subtarget \p STI.
105unsigned getMinFlatWorkGroupSize(const MCSubtargetInfo *STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000106
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000107/// \returns Maximum flat work group size for given subtarget \p STI.
108unsigned getMaxFlatWorkGroupSize(const MCSubtargetInfo *STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000109
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000110/// \returns Number of waves per work group for given subtarget \p STI and
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000111/// limited by given \p FlatWorkGroupSize.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000112unsigned getWavesPerWorkGroup(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000113 unsigned FlatWorkGroupSize);
114
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000115/// \returns SGPR allocation granularity for given subtarget \p STI.
116unsigned getSGPRAllocGranule(const MCSubtargetInfo *STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000117
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000118/// \returns SGPR encoding granularity for given subtarget \p STI.
119unsigned getSGPREncodingGranule(const MCSubtargetInfo *STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000120
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000121/// \returns Total number of SGPRs for given subtarget \p STI.
122unsigned getTotalNumSGPRs(const MCSubtargetInfo *STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000123
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000124/// \returns Addressable number of SGPRs for given subtarget \p STI.
125unsigned getAddressableNumSGPRs(const MCSubtargetInfo *STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000126
127/// \returns Minimum number of SGPRs that meets the given number of waves per
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000128/// execution unit requirement for given subtarget \p STI.
129unsigned getMinNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000130
131/// \returns Maximum number of SGPRs that meets the given number of waves per
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000132/// execution unit requirement for given subtarget \p STI.
133unsigned getMaxNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000134 bool Addressable);
135
Scott Linder1e8c2c72018-06-21 19:38:56 +0000136/// \returns Number of extra SGPRs implicitly required by given subtarget \p
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000137/// STI when the given special registers are used.
138unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed,
Scott Linder1e8c2c72018-06-21 19:38:56 +0000139 bool FlatScrUsed, bool XNACKUsed);
140
141/// \returns Number of extra SGPRs implicitly required by given subtarget \p
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000142/// STI when the given special registers are used. XNACK is inferred from
143/// \p STI.
144unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed,
Scott Linder1e8c2c72018-06-21 19:38:56 +0000145 bool FlatScrUsed);
146
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000147/// \returns Number of SGPR blocks needed for given subtarget \p STI when
Scott Linder1e8c2c72018-06-21 19:38:56 +0000148/// \p NumSGPRs are used. \p NumSGPRs should already include any special
149/// register counts.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000150unsigned getNumSGPRBlocks(const MCSubtargetInfo *STI, unsigned NumSGPRs);
Scott Linder1e8c2c72018-06-21 19:38:56 +0000151
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000152/// \returns VGPR allocation granularity for given subtarget \p STI.
Stanislav Mekhanoshin8bcc9bb2019-06-13 19:18:29 +0000153///
154/// For subtargets which support it, \p EnableWavefrontSize32 should match
155/// the ENABLE_WAVEFRONT_SIZE32 kernel descriptor field.
156unsigned getVGPRAllocGranule(const MCSubtargetInfo *STI,
157 Optional<bool> EnableWavefrontSize32 = None);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000158
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000159/// \returns VGPR encoding granularity for given subtarget \p STI.
Stanislav Mekhanoshin8bcc9bb2019-06-13 19:18:29 +0000160///
161/// For subtargets which support it, \p EnableWavefrontSize32 should match
162/// the ENABLE_WAVEFRONT_SIZE32 kernel descriptor field.
163unsigned getVGPREncodingGranule(const MCSubtargetInfo *STI,
164 Optional<bool> EnableWavefrontSize32 = None);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000165
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000166/// \returns Total number of VGPRs for given subtarget \p STI.
167unsigned getTotalNumVGPRs(const MCSubtargetInfo *STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000168
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000169/// \returns Addressable number of VGPRs for given subtarget \p STI.
170unsigned getAddressableNumVGPRs(const MCSubtargetInfo *STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000171
172/// \returns Minimum number of VGPRs that meets given number of waves per
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000173/// execution unit requirement for given subtarget \p STI.
174unsigned getMinNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000175
176/// \returns Maximum number of VGPRs that meets given number of waves per
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000177/// execution unit requirement for given subtarget \p STI.
178unsigned getMaxNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000179
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000180/// \returns Number of VGPR blocks needed for given subtarget \p STI when
Scott Linder1e8c2c72018-06-21 19:38:56 +0000181/// \p NumVGPRs are used.
Stanislav Mekhanoshin8bcc9bb2019-06-13 19:18:29 +0000182///
183/// For subtargets which support it, \p EnableWavefrontSize32 should match the
184/// ENABLE_WAVEFRONT_SIZE32 kernel descriptor field.
185unsigned getNumVGPRBlocks(const MCSubtargetInfo *STI, unsigned NumSGPRs,
186 Optional<bool> EnableWavefrontSize32 = None);
Scott Linder1e8c2c72018-06-21 19:38:56 +0000187
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000188} // end namespace IsaInfo
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000189
190LLVM_READONLY
191int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx);
192
Ryan Taylor9ab812d2019-06-26 17:34:57 +0000193LLVM_READONLY
194int getSOPPWithRelaxation(uint16_t Opcode);
195
Nicolai Haehnle7a9c03f2018-06-21 13:36:57 +0000196struct MIMGBaseOpcodeInfo {
197 MIMGBaseOpcode BaseOpcode;
198 bool Store;
199 bool Atomic;
200 bool AtomicX2;
201 bool Sampler;
David Stuttardf77079f2019-01-14 11:55:24 +0000202 bool Gather4;
Nicolai Haehnle7a9c03f2018-06-21 13:36:57 +0000203
204 uint8_t NumExtraArgs;
205 bool Gradients;
206 bool Coordinates;
207 bool LodOrClampOrMip;
208 bool HasD16;
209};
210
211LLVM_READONLY
212const MIMGBaseOpcodeInfo *getMIMGBaseOpcodeInfo(unsigned BaseOpcode);
213
214struct MIMGDimInfo {
215 MIMGDim Dim;
216 uint8_t NumCoords;
217 uint8_t NumGradients;
218 bool DA;
Stanislav Mekhanoshin692560d2019-05-01 16:32:58 +0000219 uint8_t Encoding;
220 const char *AsmSuffix;
Nicolai Haehnle7a9c03f2018-06-21 13:36:57 +0000221};
222
223LLVM_READONLY
Stanislav Mekhanoshin956b0be2019-04-25 18:53:41 +0000224const MIMGDimInfo *getMIMGDimInfo(unsigned DimEnum);
225
226LLVM_READONLY
227const MIMGDimInfo *getMIMGDimInfoByEncoding(uint8_t DimEnc);
228
229LLVM_READONLY
230const MIMGDimInfo *getMIMGDimInfoByAsmSuffix(StringRef AsmSuffix);
Nicolai Haehnle7a9c03f2018-06-21 13:36:57 +0000231
Ryan Taylor894c8fd2018-08-01 12:12:01 +0000232struct MIMGLZMappingInfo {
233 MIMGBaseOpcode L;
234 MIMGBaseOpcode LZ;
235};
236
Piotr Sobczak9b11e932019-06-10 15:58:51 +0000237struct MIMGMIPMappingInfo {
238 MIMGBaseOpcode MIP;
239 MIMGBaseOpcode NONMIP;
240};
241
Ryan Taylor894c8fd2018-08-01 12:12:01 +0000242LLVM_READONLY
243const MIMGLZMappingInfo *getMIMGLZMappingInfo(unsigned L);
244
Nicolai Haehnle7a9c03f2018-06-21 13:36:57 +0000245LLVM_READONLY
Piotr Sobczak9b11e932019-06-10 15:58:51 +0000246const MIMGMIPMappingInfo *getMIMGMIPMappingInfo(unsigned L);
247
248LLVM_READONLY
Nicolai Haehnle7a9c03f2018-06-21 13:36:57 +0000249int getMIMGOpcode(unsigned BaseOpcode, unsigned MIMGEncoding,
250 unsigned VDataDwords, unsigned VAddrDwords);
251
Matt Arsenaultcad7fa82017-12-13 21:07:51 +0000252LLVM_READONLY
Nicolai Haehnle0ab200b2018-06-21 13:36:44 +0000253int getMaskedMIMGOp(unsigned Opc, unsigned NewChannels);
Nicolai Haehnlef2674312018-06-21 13:36:01 +0000254
Stanislav Mekhanoshin956b0be2019-04-25 18:53:41 +0000255struct MIMGInfo {
256 uint16_t Opcode;
257 uint16_t BaseOpcode;
258 uint8_t MIMGEncoding;
259 uint8_t VDataDwords;
260 uint8_t VAddrDwords;
261};
262
263LLVM_READONLY
264const MIMGInfo *getMIMGInfo(unsigned Opc);
265
Nicolai Haehnlef2674312018-06-21 13:36:01 +0000266LLVM_READONLY
Neil Henning76504a42018-12-12 16:15:21 +0000267int getMUBUFBaseOpcode(unsigned Opc);
268
269LLVM_READONLY
270int getMUBUFOpcode(unsigned BaseOpc, unsigned Dwords);
271
272LLVM_READONLY
273int getMUBUFDwords(unsigned Opc);
274
275LLVM_READONLY
276bool getMUBUFHasVAddr(unsigned Opc);
277
278LLVM_READONLY
279bool getMUBUFHasSrsrc(unsigned Opc);
280
281LLVM_READONLY
282bool getMUBUFHasSoffset(unsigned Opc);
283
284LLVM_READONLY
Matt Arsenaultcad7fa82017-12-13 21:07:51 +0000285int getMCOpcode(uint16_t Opcode, unsigned Gen);
286
Tom Stellardff7416b2015-06-26 21:58:31 +0000287void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000288 const MCSubtargetInfo *STI);
Tom Stellard9760f032015-12-03 03:34:32 +0000289
Stanislav Mekhanoshincee607e2019-04-24 17:03:15 +0000290amdhsa::kernel_descriptor_t getDefaultAmdhsaKernelDescriptor(
291 const MCSubtargetInfo *STI);
Scott Linder1e8c2c72018-06-21 19:38:56 +0000292
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000293bool isGroupSegment(const GlobalValue *GV);
294bool isGlobalSegment(const GlobalValue *GV);
295bool isReadOnlySegment(const GlobalValue *GV);
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000296
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +0000297/// \returns True if constants should be emitted to .text section for given
298/// target triple \p TT, false otherwise.
299bool shouldEmitConstantsToTextSection(const Triple &TT);
300
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000301/// \returns Integer value requested using \p F's \p Name attribute.
302///
303/// \returns \p Default if attribute is not present.
304///
305/// \returns \p Default and emits error if requested value cannot be converted
306/// to integer.
Matt Arsenault83002722016-05-12 02:45:18 +0000307int getIntegerAttribute(const Function &F, StringRef Name, int Default);
308
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000309/// \returns A pair of integer values requested using \p F's \p Name attribute
310/// in "first[,second]" format ("second" is optional unless \p OnlyFirstRequired
311/// is false).
312///
313/// \returns \p Default if attribute is not present.
314///
315/// \returns \p Default and emits error if one of the requested values cannot be
316/// converted to integer, or \p OnlyFirstRequired is false and "second" value is
317/// not present.
318std::pair<int, int> getIntegerPairAttribute(const Function &F,
319 StringRef Name,
320 std::pair<int, int> Default,
321 bool OnlyFirstRequired = false);
322
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000323/// Represents the counter values to wait for in an s_waitcnt instruction.
324///
325/// Large values (including the maximum possible integer) can be used to
326/// represent "don't care" waits.
327struct Waitcnt {
328 unsigned VmCnt = ~0u;
329 unsigned ExpCnt = ~0u;
330 unsigned LgkmCnt = ~0u;
Stanislav Mekhanoshin956b0be2019-04-25 18:53:41 +0000331 unsigned VsCnt = ~0u;
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000332
333 Waitcnt() {}
Stanislav Mekhanoshin956b0be2019-04-25 18:53:41 +0000334 Waitcnt(unsigned VmCnt, unsigned ExpCnt, unsigned LgkmCnt, unsigned VsCnt)
335 : VmCnt(VmCnt), ExpCnt(ExpCnt), LgkmCnt(LgkmCnt), VsCnt(VsCnt) {}
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000336
Stanislav Mekhanoshin956b0be2019-04-25 18:53:41 +0000337 static Waitcnt allZero(const IsaVersion &Version) {
338 return Waitcnt(0, 0, 0, Version.Major >= 10 ? 0 : ~0u);
339 }
340 static Waitcnt allZeroExceptVsCnt() { return Waitcnt(0, 0, 0, ~0u); }
341
342 bool hasWait() const {
343 return VmCnt != ~0u || ExpCnt != ~0u || LgkmCnt != ~0u || VsCnt != ~0u;
344 }
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000345
346 bool dominates(const Waitcnt &Other) const {
347 return VmCnt <= Other.VmCnt && ExpCnt <= Other.ExpCnt &&
Stanislav Mekhanoshin956b0be2019-04-25 18:53:41 +0000348 LgkmCnt <= Other.LgkmCnt && VsCnt <= Other.VsCnt;
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000349 }
350
351 Waitcnt combined(const Waitcnt &Other) const {
352 return Waitcnt(std::min(VmCnt, Other.VmCnt), std::min(ExpCnt, Other.ExpCnt),
Stanislav Mekhanoshin956b0be2019-04-25 18:53:41 +0000353 std::min(LgkmCnt, Other.LgkmCnt),
354 std::min(VsCnt, Other.VsCnt));
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000355 }
356};
357
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000358/// \returns Vmcnt bit mask for given isa \p Version.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000359unsigned getVmcntBitMask(const IsaVersion &Version);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000360
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000361/// \returns Expcnt bit mask for given isa \p Version.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000362unsigned getExpcntBitMask(const IsaVersion &Version);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000363
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000364/// \returns Lgkmcnt bit mask for given isa \p Version.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000365unsigned getLgkmcntBitMask(const IsaVersion &Version);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000366
367/// \returns Waitcnt bit mask for given isa \p Version.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000368unsigned getWaitcntBitMask(const IsaVersion &Version);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000369
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000370/// \returns Decoded Vmcnt from given \p Waitcnt for given isa \p Version.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000371unsigned decodeVmcnt(const IsaVersion &Version, unsigned Waitcnt);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000372
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000373/// \returns Decoded Expcnt from given \p Waitcnt for given isa \p Version.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000374unsigned decodeExpcnt(const IsaVersion &Version, unsigned Waitcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000375
376/// \returns Decoded Lgkmcnt from given \p Waitcnt for given isa \p Version.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000377unsigned decodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000378
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000379/// Decodes Vmcnt, Expcnt and Lgkmcnt from given \p Waitcnt for given isa
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000380/// \p Version, and writes decoded values into \p Vmcnt, \p Expcnt and
381/// \p Lgkmcnt respectively.
382///
383/// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are decoded as follows:
Matt Arsenaulte823d922017-02-18 18:29:53 +0000384/// \p Vmcnt = \p Waitcnt[3:0] (pre-gfx9 only)
385/// \p Vmcnt = \p Waitcnt[3:0] | \p Waitcnt[15:14] (gfx9+ only)
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000386/// \p Expcnt = \p Waitcnt[6:4]
Stanislav Mekhanoshin956b0be2019-04-25 18:53:41 +0000387/// \p Lgkmcnt = \p Waitcnt[11:8] (pre-gfx10 only)
388/// \p Lgkmcnt = \p Waitcnt[13:8] (gfx10+ only)
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000389void decodeWaitcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000390 unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt);
391
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000392Waitcnt decodeWaitcnt(const IsaVersion &Version, unsigned Encoded);
393
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000394/// \returns \p Waitcnt with encoded \p Vmcnt for given isa \p Version.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000395unsigned encodeVmcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000396 unsigned Vmcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000397
398/// \returns \p Waitcnt with encoded \p Expcnt for given isa \p Version.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000399unsigned encodeExpcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000400 unsigned Expcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000401
402/// \returns \p Waitcnt with encoded \p Lgkmcnt for given isa \p Version.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000403unsigned encodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000404 unsigned Lgkmcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000405
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000406/// Encodes \p Vmcnt, \p Expcnt and \p Lgkmcnt into Waitcnt for given isa
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000407/// \p Version.
408///
409/// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are encoded as follows:
Matt Arsenaulte823d922017-02-18 18:29:53 +0000410/// Waitcnt[3:0] = \p Vmcnt (pre-gfx9 only)
411/// Waitcnt[3:0] = \p Vmcnt[3:0] (gfx9+ only)
412/// Waitcnt[6:4] = \p Expcnt
Stanislav Mekhanoshin956b0be2019-04-25 18:53:41 +0000413/// Waitcnt[11:8] = \p Lgkmcnt (pre-gfx10 only)
414/// Waitcnt[13:8] = \p Lgkmcnt (gfx10+ only)
Matt Arsenaulte823d922017-02-18 18:29:53 +0000415/// Waitcnt[15:14] = \p Vmcnt[5:4] (gfx9+ only)
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000416///
417/// \returns Waitcnt with encoded \p Vmcnt, \p Expcnt and \p Lgkmcnt for given
418/// isa \p Version.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000419unsigned encodeWaitcnt(const IsaVersion &Version,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000420 unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000421
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000422unsigned encodeWaitcnt(const IsaVersion &Version, const Waitcnt &Decoded);
423
Dmitry Preobrazhensky1fca3b12019-06-13 12:46:37 +0000424namespace Hwreg {
425
426LLVM_READONLY
427int64_t getHwregId(const StringRef Name);
428
429LLVM_READNONE
430bool isValidHwreg(int64_t Id, const MCSubtargetInfo &STI);
431
432LLVM_READNONE
433bool isValidHwreg(int64_t Id);
434
435LLVM_READNONE
436bool isValidHwregOffset(int64_t Offset);
437
438LLVM_READNONE
439bool isValidHwregWidth(int64_t Width);
440
441LLVM_READNONE
442int64_t encodeHwreg(int64_t Id, int64_t Offset, int64_t Width);
443
444LLVM_READNONE
445StringRef getHwreg(unsigned Id, const MCSubtargetInfo &STI);
446
447void decodeHwreg(unsigned Val, unsigned &Id, unsigned &Offset, unsigned &Width);
448
449} // namespace Hwreg
450
Dmitry Preobrazhensky1d572ce2019-06-28 14:14:02 +0000451namespace SendMsg {
452
453LLVM_READONLY
454int64_t getMsgId(const StringRef Name);
455
456LLVM_READONLY
457int64_t getMsgOpId(int64_t MsgId, const StringRef Name);
458
459LLVM_READNONE
460StringRef getMsgName(int64_t MsgId);
461
462LLVM_READNONE
463StringRef getMsgOpName(int64_t MsgId, int64_t OpId);
464
465LLVM_READNONE
466bool isValidMsgId(int64_t MsgId, const MCSubtargetInfo &STI, bool Strict = true);
467
468LLVM_READNONE
469bool isValidMsgOp(int64_t MsgId, int64_t OpId, bool Strict = true);
470
471LLVM_READNONE
472bool isValidMsgStream(int64_t MsgId, int64_t OpId, int64_t StreamId, bool Strict = true);
473
474LLVM_READNONE
475bool msgRequiresOp(int64_t MsgId);
476
477LLVM_READNONE
478bool msgSupportsStream(int64_t MsgId, int64_t OpId);
479
480void decodeMsg(unsigned Val,
481 uint16_t &MsgId,
482 uint16_t &OpId,
483 uint16_t &StreamId);
484
485LLVM_READNONE
486int64_t encodeMsg(int64_t MsgId,
487 int64_t OpId,
488 int64_t StreamId);
489
490} // namespace SendMsg
491
492
Marek Olsakfccabaf2016-01-13 11:45:36 +0000493unsigned getInitialPSInputAddr(const Function &F);
494
Matt Arsenaulte622dc32017-04-11 22:29:24 +0000495LLVM_READNONE
496bool isShader(CallingConv::ID CC);
497
498LLVM_READNONE
499bool isCompute(CallingConv::ID CC);
500
501LLVM_READNONE
502bool isEntryFunctionCC(CallingConv::ID CC);
503
Matt Arsenaultefa9f4b2017-04-11 22:29:28 +0000504// FIXME: Remove this when calling conventions cleaned up
505LLVM_READNONE
506inline bool isKernel(CallingConv::ID CC) {
507 switch (CC) {
Matt Arsenaultefa9f4b2017-04-11 22:29:28 +0000508 case CallingConv::AMDGPU_KERNEL:
509 case CallingConv::SPIR_KERNEL:
510 return true;
511 default:
512 return false;
513 }
514}
Tom Stellardac00eb52015-12-15 16:26:16 +0000515
Dmitry Preobrazhensky3afbd822018-01-10 14:22:19 +0000516bool hasXNACK(const MCSubtargetInfo &STI);
Konstantin Zhuravlyov108927b2018-11-05 22:44:19 +0000517bool hasSRAMECC(const MCSubtargetInfo &STI);
Dmitry Preobrazhenskye3271ae2018-02-05 12:45:43 +0000518bool hasMIMG_R128(const MCSubtargetInfo &STI);
Dmitry Preobrazhensky0a1ff462018-02-05 14:18:53 +0000519bool hasPackedD16(const MCSubtargetInfo &STI);
Dmitry Preobrazhenskye3271ae2018-02-05 12:45:43 +0000520
Tom Stellard2b65ed32015-12-21 18:44:27 +0000521bool isSI(const MCSubtargetInfo &STI);
522bool isCI(const MCSubtargetInfo &STI);
523bool isVI(const MCSubtargetInfo &STI);
Sam Koltonf7659d712017-05-23 10:08:55 +0000524bool isGFX9(const MCSubtargetInfo &STI);
Stanislav Mekhanoshincee607e2019-04-24 17:03:15 +0000525bool isGFX10(const MCSubtargetInfo &STI);
Sam Koltonf7659d712017-05-23 10:08:55 +0000526
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000527/// Is Reg - scalar register
Sam Koltonf7659d712017-05-23 10:08:55 +0000528bool isSGPR(unsigned Reg, const MCRegisterInfo* TRI);
Tom Stellard2b65ed32015-12-21 18:44:27 +0000529
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000530/// Is there any intersection between registers
Dmitry Preobrazhenskydc4ac822017-06-21 14:41:34 +0000531bool isRegIntersect(unsigned Reg0, unsigned Reg1, const MCRegisterInfo* TRI);
532
Tom Stellard2b65ed32015-12-21 18:44:27 +0000533/// If \p Reg is a pseudo reg, return the correct hardware register given
534/// \p STI otherwise return \p Reg.
535unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI);
536
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000537/// Convert hardware register \p Reg to a pseudo register
Dmitry Preobrazhensky03880f82017-03-03 14:31:06 +0000538LLVM_READNONE
539unsigned mc2PseudoReg(unsigned Reg);
540
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000541/// Can this operand also contain immediate values?
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000542bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo);
543
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000544/// Is this floating-point operand?
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000545bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo);
546
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000547/// Does this opearnd support only inlinable literals?
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000548bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo);
549
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000550/// Get the size in bits of a register from the register class \p RC.
Tom Stellardb133fbb2016-10-27 23:05:31 +0000551unsigned getRegBitWidth(unsigned RCID);
552
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000553/// Get the size in bits of a register from the register class \p RC.
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000554unsigned getRegBitWidth(const MCRegisterClass &RC);
555
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000556/// Get size of register operand
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000557unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
558 unsigned OpNo);
559
Matt Arsenault4bd72362016-12-10 00:39:12 +0000560LLVM_READNONE
561inline unsigned getOperandSize(const MCOperandInfo &OpInfo) {
562 switch (OpInfo.OperandType) {
563 case AMDGPU::OPERAND_REG_IMM_INT32:
564 case AMDGPU::OPERAND_REG_IMM_FP32:
565 case AMDGPU::OPERAND_REG_INLINE_C_INT32:
566 case AMDGPU::OPERAND_REG_INLINE_C_FP32:
567 return 4;
568
569 case AMDGPU::OPERAND_REG_IMM_INT64:
570 case AMDGPU::OPERAND_REG_IMM_FP64:
571 case AMDGPU::OPERAND_REG_INLINE_C_INT64:
572 case AMDGPU::OPERAND_REG_INLINE_C_FP64:
573 return 8;
574
575 case AMDGPU::OPERAND_REG_IMM_INT16:
576 case AMDGPU::OPERAND_REG_IMM_FP16:
577 case AMDGPU::OPERAND_REG_INLINE_C_INT16:
578 case AMDGPU::OPERAND_REG_INLINE_C_FP16:
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000579 case AMDGPU::OPERAND_REG_INLINE_C_V2INT16:
580 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
Stanislav Mekhanoshin956b0be2019-04-25 18:53:41 +0000581 case AMDGPU::OPERAND_REG_IMM_V2INT16:
582 case AMDGPU::OPERAND_REG_IMM_V2FP16:
Matt Arsenault4bd72362016-12-10 00:39:12 +0000583 return 2;
584
585 default:
586 llvm_unreachable("unhandled operand type");
587 }
588}
589
590LLVM_READNONE
591inline unsigned getOperandSize(const MCInstrDesc &Desc, unsigned OpNo) {
592 return getOperandSize(Desc.OpInfo[OpNo]);
593}
594
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000595/// Is this literal inlinable
Matt Arsenault26faed32016-12-05 22:26:17 +0000596LLVM_READNONE
597bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi);
598
599LLVM_READNONE
600bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi);
601
Matt Arsenault4bd72362016-12-10 00:39:12 +0000602LLVM_READNONE
603bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000604
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000605LLVM_READNONE
606bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi);
607
Matt Arsenault894e53d2017-07-26 20:39:42 +0000608bool isArgPassedInSGPR(const Argument *Arg);
Tom Stellard08efb7e2017-01-27 18:41:14 +0000609
610/// \returns The encoding that will be used for \p ByteOffset in the SMRD
611/// offset field.
612int64_t getSMRDEncodedOffset(const MCSubtargetInfo &ST, int64_t ByteOffset);
613
614/// \returns true if this offset is small enough to fit in the SMRD
615/// offset field. \p ByteOffset should be the offset in bytes and
616/// not the encoded offset.
617bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset);
618
Tim Renouf4f703f52018-08-21 11:07:10 +0000619bool splitMUBUFOffset(uint32_t Imm, uint32_t &SOffset, uint32_t &ImmOffset,
Nicolai Haehnlea7b00052018-11-30 22:55:38 +0000620 const GCNSubtarget *Subtarget, uint32_t Align = 4);
Tim Renouf4f703f52018-08-21 11:07:10 +0000621
Alexander Timofeev2e5eece2018-03-05 15:12:21 +0000622/// \returns true if the intrinsic is divergent
623bool isIntrinsicSourceOfDivergence(unsigned IntrID);
624
Matt Arsenault055e4dc2019-03-29 19:14:54 +0000625
626// Track defaults for fields in the MODE registser.
627struct SIModeRegisterDefaults {
628 /// Floating point opcodes that support exception flag gathering quiet and
629 /// propagate signaling NaN inputs per IEEE 754-2008. Min_dx10 and max_dx10
630 /// become IEEE 754- 2008 compliant due to signaling NaN propagation and
631 /// quieting.
632 bool IEEE : 1;
633
634 /// Used by the vector ALU to force DX10-style treatment of NaNs: when set,
635 /// clamp NaN to zero; otherwise, pass NaN through.
636 bool DX10Clamp : 1;
637
638 // TODO: FP mode fields
639
640 SIModeRegisterDefaults() :
641 IEEE(true),
642 DX10Clamp(true) {}
643
644 SIModeRegisterDefaults(const Function &F);
645
646 static SIModeRegisterDefaults getDefaultForCallingConv(CallingConv::ID CC) {
647 SIModeRegisterDefaults Mode;
648 Mode.DX10Clamp = true;
649 Mode.IEEE = AMDGPU::isCompute(CC);
650 return Mode;
651 }
652
653 bool operator ==(const SIModeRegisterDefaults Other) const {
654 return IEEE == Other.IEEE && DX10Clamp == Other.DX10Clamp;
655 }
656
657 // FIXME: Inlining should be OK for dx10-clamp, since the caller's mode should
658 // be able to override.
659 bool isInlineCompatible(SIModeRegisterDefaults CalleeMode) const {
660 return *this == CalleeMode;
661 }
662};
663
Tom Stellard347ac792015-06-26 21:15:07 +0000664} // end namespace AMDGPU
665} // end namespace llvm
666
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000667#endif // LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H