blob: d565c84bfedaa5073f0aec539686a42601fc0d6c [file] [log] [blame]
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001//===- AMDGPUBaseInfo.cpp - AMDGPU Base encoding information --------------===//
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//===----------------------------------------------------------------------===//
Eugene Zelenkod96089b2017-02-14 00:33:36 +00009
Tom Stellarde3b5aea2015-12-02 17:00:42 +000010#include "AMDGPU.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000011#include "AMDGPUBaseInfo.h"
Sam Kolton1eeb11b2016-09-09 14:44:04 +000012#include "SIDefines.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000013#include "llvm/ADT/StringRef.h"
14#include "llvm/ADT/Triple.h"
Tom Stellard08efb7e2017-01-27 18:41:14 +000015#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000016#include "llvm/IR/Attributes.h"
Tom Stellard08efb7e2017-01-27 18:41:14 +000017#include "llvm/IR/Constants.h"
Tom Stellardac00eb52015-12-15 16:26:16 +000018#include "llvm/IR/Function.h"
Tom Stellarde3b5aea2015-12-02 17:00:42 +000019#include "llvm/IR/GlobalValue.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000020#include "llvm/IR/Instruction.h"
Tom Stellardca166212017-01-30 21:56:46 +000021#include "llvm/IR/LLVMContext.h"
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000022#include "llvm/IR/Module.h"
Tom Stellarde135ffd2015-09-25 21:41:28 +000023#include "llvm/MC/MCContext.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000024#include "llvm/MC/MCInstrDesc.h"
Sam Kolton1eeb11b2016-09-09 14:44:04 +000025#include "llvm/MC/MCRegisterInfo.h"
Tom Stellarde135ffd2015-09-25 21:41:28 +000026#include "llvm/MC/MCSectionELF.h"
Tom Stellard2b65ed32015-12-21 18:44:27 +000027#include "llvm/MC/MCSubtargetInfo.h"
Tom Stellard347ac792015-06-26 21:15:07 +000028#include "llvm/MC/SubtargetFeature.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000029#include "llvm/Support/Casting.h"
30#include "llvm/Support/ELF.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/MathExtras.h"
33#include <algorithm>
34#include <cassert>
35#include <cstdint>
36#include <cstring>
37#include <utility>
Tom Stellard347ac792015-06-26 21:15:07 +000038
Matt Arsenault678e1112017-04-10 17:58:06 +000039#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Tom Stellard347ac792015-06-26 21:15:07 +000040
Tom Stellard2b65ed32015-12-21 18:44:27 +000041
Sam Koltona3ec5c12016-10-07 14:46:06 +000042#define GET_INSTRINFO_NAMED_OPS
Sam Koltona3ec5c12016-10-07 14:46:06 +000043#include "AMDGPUGenInstrInfo.inc"
44#undef GET_INSTRINFO_NAMED_OPS
Sam Koltona3ec5c12016-10-07 14:46:06 +000045
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000046namespace {
47
48/// \returns Bit mask for given bit \p Shift and bit \p Width.
49unsigned getBitMask(unsigned Shift, unsigned Width) {
50 return ((1 << Width) - 1) << Shift;
51}
52
53/// \brief Packs \p Src into \p Dst for given bit \p Shift and bit \p Width.
54///
55/// \returns Packed \p Dst.
56unsigned packBits(unsigned Src, unsigned Dst, unsigned Shift, unsigned Width) {
57 Dst &= ~(1 << Shift) & ~getBitMask(Shift, Width);
58 Dst |= (Src << Shift) & getBitMask(Shift, Width);
59 return Dst;
60}
61
62/// \brief Unpacks bits from \p Src for given bit \p Shift and bit \p Width.
63///
64/// \returns Unpacked bits.
65unsigned unpackBits(unsigned Src, unsigned Shift, unsigned Width) {
66 return (Src & getBitMask(Shift, Width)) >> Shift;
67}
68
Matt Arsenaulte823d922017-02-18 18:29:53 +000069/// \returns Vmcnt bit shift (lower bits).
70unsigned getVmcntBitShiftLo() { return 0; }
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000071
Matt Arsenaulte823d922017-02-18 18:29:53 +000072/// \returns Vmcnt bit width (lower bits).
73unsigned getVmcntBitWidthLo() { return 4; }
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000074
75/// \returns Expcnt bit shift.
76unsigned getExpcntBitShift() { return 4; }
77
78/// \returns Expcnt bit width.
79unsigned getExpcntBitWidth() { return 3; }
80
81/// \returns Lgkmcnt bit shift.
82unsigned getLgkmcntBitShift() { return 8; }
83
84/// \returns Lgkmcnt bit width.
85unsigned getLgkmcntBitWidth() { return 4; }
86
Matt Arsenaulte823d922017-02-18 18:29:53 +000087/// \returns Vmcnt bit shift (higher bits).
88unsigned getVmcntBitShiftHi() { return 14; }
89
90/// \returns Vmcnt bit width (higher bits).
91unsigned getVmcntBitWidthHi() { return 2; }
92
Eugene Zelenkod96089b2017-02-14 00:33:36 +000093} // end namespace anonymous
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000094
Tom Stellard347ac792015-06-26 21:15:07 +000095namespace llvm {
Konstantin Zhuravlyov3d1cc882017-04-21 19:45:22 +000096
97static cl::opt<bool> EnablePackedInlinableLiterals(
98 "enable-packed-inlinable-literals",
99 cl::desc("Enable packed inlinable literals (v2f16, v2i16)"),
100 cl::init(false));
101
Tom Stellard347ac792015-06-26 21:15:07 +0000102namespace AMDGPU {
103
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000104namespace IsaInfo {
Tom Stellard347ac792015-06-26 21:15:07 +0000105
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000106IsaVersion getIsaVersion(const FeatureBitset &Features) {
107 // CI.
Tom Stellard347ac792015-06-26 21:15:07 +0000108 if (Features.test(FeatureISAVersion7_0_0))
109 return {7, 0, 0};
Tom Stellard347ac792015-06-26 21:15:07 +0000110 if (Features.test(FeatureISAVersion7_0_1))
111 return {7, 0, 1};
Yaxun Liu94add852016-10-26 16:37:56 +0000112 if (Features.test(FeatureISAVersion7_0_2))
113 return {7, 0, 2};
114
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000115 // VI.
Tom Stellard347ac792015-06-26 21:15:07 +0000116 if (Features.test(FeatureISAVersion8_0_0))
117 return {8, 0, 0};
Tom Stellard347ac792015-06-26 21:15:07 +0000118 if (Features.test(FeatureISAVersion8_0_1))
119 return {8, 0, 1};
Changpeng Fang98317d22016-10-11 16:00:47 +0000120 if (Features.test(FeatureISAVersion8_0_2))
121 return {8, 0, 2};
Changpeng Fangc16be002016-01-13 20:39:25 +0000122 if (Features.test(FeatureISAVersion8_0_3))
123 return {8, 0, 3};
Yaxun Liu94add852016-10-26 16:37:56 +0000124 if (Features.test(FeatureISAVersion8_0_4))
125 return {8, 0, 4};
Yaxun Liu94add852016-10-26 16:37:56 +0000126 if (Features.test(FeatureISAVersion8_1_0))
127 return {8, 1, 0};
128
Matt Arsenaulte823d922017-02-18 18:29:53 +0000129 // GFX9.
130 if (Features.test(FeatureISAVersion9_0_0))
131 return {9, 0, 0};
132 if (Features.test(FeatureISAVersion9_0_1))
133 return {9, 0, 1};
134
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000135 if (!Features.test(FeatureGCN) || Features.test(FeatureSouthernIslands))
136 return {0, 0, 0};
137 return {7, 0, 0};
Tom Stellard347ac792015-06-26 21:15:07 +0000138}
139
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000140unsigned getWavefrontSize(const FeatureBitset &Features) {
141 if (Features.test(FeatureWavefrontSize16))
142 return 16;
143 if (Features.test(FeatureWavefrontSize32))
144 return 32;
145
146 return 64;
147}
148
149unsigned getLocalMemorySize(const FeatureBitset &Features) {
150 if (Features.test(FeatureLocalMemorySize32768))
151 return 32768;
152 if (Features.test(FeatureLocalMemorySize65536))
153 return 65536;
154
155 return 0;
156}
157
158unsigned getEUsPerCU(const FeatureBitset &Features) {
159 return 4;
160}
161
162unsigned getMaxWorkGroupsPerCU(const FeatureBitset &Features,
163 unsigned FlatWorkGroupSize) {
164 if (!Features.test(FeatureGCN))
165 return 8;
Stanislav Mekhanoshin19f98c62017-02-15 01:03:59 +0000166 unsigned N = getWavesPerWorkGroup(Features, FlatWorkGroupSize);
167 if (N == 1)
168 return 40;
169 N = 40 / N;
170 return std::min(N, 16u);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000171}
172
173unsigned getMaxWavesPerCU(const FeatureBitset &Features) {
174 return getMaxWavesPerEU(Features) * getEUsPerCU(Features);
175}
176
177unsigned getMaxWavesPerCU(const FeatureBitset &Features,
178 unsigned FlatWorkGroupSize) {
179 return getWavesPerWorkGroup(Features, FlatWorkGroupSize);
180}
181
182unsigned getMinWavesPerEU(const FeatureBitset &Features) {
183 return 1;
184}
185
186unsigned getMaxWavesPerEU(const FeatureBitset &Features) {
187 if (!Features.test(FeatureGCN))
188 return 8;
189 // FIXME: Need to take scratch memory into account.
190 return 10;
191}
192
193unsigned getMaxWavesPerEU(const FeatureBitset &Features,
194 unsigned FlatWorkGroupSize) {
195 return alignTo(getMaxWavesPerCU(Features, FlatWorkGroupSize),
196 getEUsPerCU(Features)) / getEUsPerCU(Features);
197}
198
199unsigned getMinFlatWorkGroupSize(const FeatureBitset &Features) {
200 return 1;
201}
202
203unsigned getMaxFlatWorkGroupSize(const FeatureBitset &Features) {
204 return 2048;
205}
206
207unsigned getWavesPerWorkGroup(const FeatureBitset &Features,
208 unsigned FlatWorkGroupSize) {
209 return alignTo(FlatWorkGroupSize, getWavefrontSize(Features)) /
210 getWavefrontSize(Features);
211}
212
213unsigned getSGPRAllocGranule(const FeatureBitset &Features) {
214 IsaVersion Version = getIsaVersion(Features);
215 if (Version.Major >= 8)
216 return 16;
217 return 8;
218}
219
220unsigned getSGPREncodingGranule(const FeatureBitset &Features) {
221 return 8;
222}
223
224unsigned getTotalNumSGPRs(const FeatureBitset &Features) {
225 IsaVersion Version = getIsaVersion(Features);
226 if (Version.Major >= 8)
227 return 800;
228 return 512;
229}
230
231unsigned getAddressableNumSGPRs(const FeatureBitset &Features) {
232 if (Features.test(FeatureSGPRInitBug))
233 return FIXED_NUM_SGPRS_FOR_INIT_BUG;
234
235 IsaVersion Version = getIsaVersion(Features);
236 if (Version.Major >= 8)
237 return 102;
238 return 104;
239}
240
241unsigned getMinNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000242 assert(WavesPerEU != 0);
243
244 if (WavesPerEU >= getMaxWavesPerEU(Features))
245 return 0;
246 unsigned MinNumSGPRs =
247 alignDown(getTotalNumSGPRs(Features) / (WavesPerEU + 1),
248 getSGPRAllocGranule(Features)) + 1;
249 return std::min(MinNumSGPRs, getAddressableNumSGPRs(Features));
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000250}
251
252unsigned getMaxNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU,
253 bool Addressable) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000254 assert(WavesPerEU != 0);
255
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000256 IsaVersion Version = getIsaVersion(Features);
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000257 unsigned MaxNumSGPRs = alignDown(getTotalNumSGPRs(Features) / WavesPerEU,
258 getSGPRAllocGranule(Features));
259 unsigned AddressableNumSGPRs = getAddressableNumSGPRs(Features);
260 if (Version.Major >= 8 && !Addressable)
261 AddressableNumSGPRs = 112;
262 return std::min(MaxNumSGPRs, AddressableNumSGPRs);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000263}
264
265unsigned getVGPRAllocGranule(const FeatureBitset &Features) {
266 return 4;
267}
268
269unsigned getVGPREncodingGranule(const FeatureBitset &Features) {
270 return getVGPRAllocGranule(Features);
271}
272
273unsigned getTotalNumVGPRs(const FeatureBitset &Features) {
274 return 256;
275}
276
277unsigned getAddressableNumVGPRs(const FeatureBitset &Features) {
278 return getTotalNumVGPRs(Features);
279}
280
281unsigned getMinNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000282 assert(WavesPerEU != 0);
283
284 if (WavesPerEU >= getMaxWavesPerEU(Features))
285 return 0;
286 unsigned MinNumVGPRs =
287 alignDown(getTotalNumVGPRs(Features) / (WavesPerEU + 1),
288 getVGPRAllocGranule(Features)) + 1;
289 return std::min(MinNumVGPRs, getAddressableNumVGPRs(Features));
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000290}
291
292unsigned getMaxNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000293 assert(WavesPerEU != 0);
294
295 unsigned MaxNumVGPRs = alignDown(getTotalNumVGPRs(Features) / WavesPerEU,
296 getVGPRAllocGranule(Features));
297 unsigned AddressableNumVGPRs = getAddressableNumVGPRs(Features);
298 return std::min(MaxNumVGPRs, AddressableNumVGPRs);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000299}
300
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000301} // end namespace IsaInfo
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000302
Tom Stellardff7416b2015-06-26 21:58:31 +0000303void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
304 const FeatureBitset &Features) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000305 IsaInfo::IsaVersion ISA = IsaInfo::getIsaVersion(Features);
Tom Stellardff7416b2015-06-26 21:58:31 +0000306
307 memset(&Header, 0, sizeof(Header));
308
309 Header.amd_kernel_code_version_major = 1;
Konstantin Zhuravlyov182e9cc2017-02-28 17:17:52 +0000310 Header.amd_kernel_code_version_minor = 1;
Tom Stellardff7416b2015-06-26 21:58:31 +0000311 Header.amd_machine_kind = 1; // AMD_MACHINE_KIND_AMDGPU
312 Header.amd_machine_version_major = ISA.Major;
313 Header.amd_machine_version_minor = ISA.Minor;
314 Header.amd_machine_version_stepping = ISA.Stepping;
315 Header.kernel_code_entry_byte_offset = sizeof(Header);
316 // wavefront_size is specified as a power of 2: 2^6 = 64 threads.
317 Header.wavefront_size = 6;
Matt Arsenault5d910192017-01-25 20:21:57 +0000318
319 // If the code object does not support indirect functions, then the value must
320 // be 0xffffffff.
321 Header.call_convention = -1;
322
Tom Stellardff7416b2015-06-26 21:58:31 +0000323 // These alignment values are specified in powers of two, so alignment =
324 // 2^n. The minimum alignment is 2^4 = 16.
325 Header.kernarg_segment_alignment = 4;
326 Header.group_segment_alignment = 4;
327 Header.private_segment_alignment = 4;
328}
329
Tom Stellarde135ffd2015-09-25 21:41:28 +0000330MCSection *getHSATextSection(MCContext &Ctx) {
331 return Ctx.getELFSection(".hsatext", ELF::SHT_PROGBITS,
332 ELF::SHF_ALLOC | ELF::SHF_WRITE |
333 ELF::SHF_EXECINSTR |
334 ELF::SHF_AMDGPU_HSA_AGENT |
335 ELF::SHF_AMDGPU_HSA_CODE);
336}
337
Tom Stellard00f2f912015-12-02 19:47:57 +0000338MCSection *getHSADataGlobalAgentSection(MCContext &Ctx) {
339 return Ctx.getELFSection(".hsadata_global_agent", ELF::SHT_PROGBITS,
340 ELF::SHF_ALLOC | ELF::SHF_WRITE |
341 ELF::SHF_AMDGPU_HSA_GLOBAL |
342 ELF::SHF_AMDGPU_HSA_AGENT);
343}
344
345MCSection *getHSADataGlobalProgramSection(MCContext &Ctx) {
346 return Ctx.getELFSection(".hsadata_global_program", ELF::SHT_PROGBITS,
347 ELF::SHF_ALLOC | ELF::SHF_WRITE |
348 ELF::SHF_AMDGPU_HSA_GLOBAL);
349}
350
Tom Stellard9760f032015-12-03 03:34:32 +0000351MCSection *getHSARodataReadonlyAgentSection(MCContext &Ctx) {
352 return Ctx.getELFSection(".hsarodata_readonly_agent", ELF::SHT_PROGBITS,
353 ELF::SHF_ALLOC | ELF::SHF_AMDGPU_HSA_READONLY |
354 ELF::SHF_AMDGPU_HSA_AGENT);
355}
356
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000357bool isGroupSegment(const GlobalValue *GV, AMDGPUAS AS) {
358 return GV->getType()->getAddressSpace() == AS.LOCAL_ADDRESS;
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000359}
360
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000361bool isGlobalSegment(const GlobalValue *GV, AMDGPUAS AS) {
362 return GV->getType()->getAddressSpace() == AS.GLOBAL_ADDRESS;
Tom Stellard00f2f912015-12-02 19:47:57 +0000363}
364
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000365bool isReadOnlySegment(const GlobalValue *GV, AMDGPUAS AS) {
366 return GV->getType()->getAddressSpace() == AS.CONSTANT_ADDRESS;
Tom Stellard00f2f912015-12-02 19:47:57 +0000367}
368
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +0000369bool shouldEmitConstantsToTextSection(const Triple &TT) {
370 return TT.getOS() != Triple::AMDHSA;
371}
372
Matt Arsenault83002722016-05-12 02:45:18 +0000373int getIntegerAttribute(const Function &F, StringRef Name, int Default) {
Marek Olsakfccabaf2016-01-13 11:45:36 +0000374 Attribute A = F.getFnAttribute(Name);
Matt Arsenault83002722016-05-12 02:45:18 +0000375 int Result = Default;
Tom Stellardac00eb52015-12-15 16:26:16 +0000376
377 if (A.isStringAttribute()) {
378 StringRef Str = A.getValueAsString();
Marek Olsakfccabaf2016-01-13 11:45:36 +0000379 if (Str.getAsInteger(0, Result)) {
Tom Stellardac00eb52015-12-15 16:26:16 +0000380 LLVMContext &Ctx = F.getContext();
Matt Arsenault83002722016-05-12 02:45:18 +0000381 Ctx.emitError("can't parse integer attribute " + Name);
Tom Stellardac00eb52015-12-15 16:26:16 +0000382 }
383 }
Matt Arsenault83002722016-05-12 02:45:18 +0000384
Marek Olsakfccabaf2016-01-13 11:45:36 +0000385 return Result;
386}
387
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000388std::pair<int, int> getIntegerPairAttribute(const Function &F,
389 StringRef Name,
390 std::pair<int, int> Default,
391 bool OnlyFirstRequired) {
392 Attribute A = F.getFnAttribute(Name);
393 if (!A.isStringAttribute())
394 return Default;
395
396 LLVMContext &Ctx = F.getContext();
397 std::pair<int, int> Ints = Default;
398 std::pair<StringRef, StringRef> Strs = A.getValueAsString().split(',');
399 if (Strs.first.trim().getAsInteger(0, Ints.first)) {
400 Ctx.emitError("can't parse first integer attribute " + Name);
401 return Default;
402 }
403 if (Strs.second.trim().getAsInteger(0, Ints.second)) {
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000404 if (!OnlyFirstRequired || !Strs.second.trim().empty()) {
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000405 Ctx.emitError("can't parse second integer attribute " + Name);
406 return Default;
407 }
408 }
409
410 return Ints;
Tom Stellard79a1fd72016-04-14 16:27:07 +0000411}
412
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000413unsigned getVmcntBitMask(const IsaInfo::IsaVersion &Version) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000414 unsigned VmcntLo = (1 << getVmcntBitWidthLo()) - 1;
415 if (Version.Major < 9)
416 return VmcntLo;
417
418 unsigned VmcntHi = ((1 << getVmcntBitWidthHi()) - 1) << getVmcntBitWidthLo();
419 return VmcntLo | VmcntHi;
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000420}
421
422unsigned getExpcntBitMask(const IsaInfo::IsaVersion &Version) {
423 return (1 << getExpcntBitWidth()) - 1;
424}
425
426unsigned getLgkmcntBitMask(const IsaInfo::IsaVersion &Version) {
427 return (1 << getLgkmcntBitWidth()) - 1;
428}
429
430unsigned getWaitcntBitMask(const IsaInfo::IsaVersion &Version) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000431 unsigned VmcntLo = getBitMask(getVmcntBitShiftLo(), getVmcntBitWidthLo());
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000432 unsigned Expcnt = getBitMask(getExpcntBitShift(), getExpcntBitWidth());
433 unsigned Lgkmcnt = getBitMask(getLgkmcntBitShift(), getLgkmcntBitWidth());
Matt Arsenaulte823d922017-02-18 18:29:53 +0000434 unsigned Waitcnt = VmcntLo | Expcnt | Lgkmcnt;
435 if (Version.Major < 9)
436 return Waitcnt;
437
438 unsigned VmcntHi = getBitMask(getVmcntBitShiftHi(), getVmcntBitWidthHi());
439 return Waitcnt | VmcntHi;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000440}
441
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000442unsigned decodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000443 unsigned VmcntLo =
444 unpackBits(Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo());
445 if (Version.Major < 9)
446 return VmcntLo;
447
448 unsigned VmcntHi =
449 unpackBits(Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi());
450 VmcntHi <<= getVmcntBitWidthLo();
451 return VmcntLo | VmcntHi;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000452}
453
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000454unsigned decodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000455 return unpackBits(Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
456}
457
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000458unsigned decodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000459 return unpackBits(Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
460}
461
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000462void decodeWaitcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000463 unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt) {
464 Vmcnt = decodeVmcnt(Version, Waitcnt);
465 Expcnt = decodeExpcnt(Version, Waitcnt);
466 Lgkmcnt = decodeLgkmcnt(Version, Waitcnt);
467}
468
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000469unsigned encodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
470 unsigned Vmcnt) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000471 Waitcnt =
472 packBits(Vmcnt, Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo());
473 if (Version.Major < 9)
474 return Waitcnt;
475
476 Vmcnt >>= getVmcntBitWidthLo();
477 return packBits(Vmcnt, Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi());
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000478}
479
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000480unsigned encodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
481 unsigned Expcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000482 return packBits(Expcnt, Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
483}
484
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000485unsigned encodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
486 unsigned Lgkmcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000487 return packBits(Lgkmcnt, Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
488}
489
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000490unsigned encodeWaitcnt(const IsaInfo::IsaVersion &Version,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000491 unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt) {
Konstantin Zhuravlyov31dbb032017-01-06 17:23:21 +0000492 unsigned Waitcnt = getWaitcntBitMask(Version);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000493 Waitcnt = encodeVmcnt(Version, Waitcnt, Vmcnt);
494 Waitcnt = encodeExpcnt(Version, Waitcnt, Expcnt);
495 Waitcnt = encodeLgkmcnt(Version, Waitcnt, Lgkmcnt);
496 return Waitcnt;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000497}
498
Marek Olsakfccabaf2016-01-13 11:45:36 +0000499unsigned getInitialPSInputAddr(const Function &F) {
500 return getIntegerAttribute(F, "InitialPSInputAddr", 0);
Tom Stellardac00eb52015-12-15 16:26:16 +0000501}
502
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000503bool isShader(CallingConv::ID cc) {
504 switch(cc) {
505 case CallingConv::AMDGPU_VS:
Marek Olsaka302a7362017-05-02 15:41:10 +0000506 case CallingConv::AMDGPU_HS:
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000507 case CallingConv::AMDGPU_GS:
508 case CallingConv::AMDGPU_PS:
509 case CallingConv::AMDGPU_CS:
510 return true;
511 default:
512 return false;
513 }
514}
515
516bool isCompute(CallingConv::ID cc) {
517 return !isShader(cc) || cc == CallingConv::AMDGPU_CS;
518}
519
Matt Arsenaulte622dc32017-04-11 22:29:24 +0000520bool isEntryFunctionCC(CallingConv::ID CC) {
521 return true;
522}
523
Tom Stellard2b65ed32015-12-21 18:44:27 +0000524bool isSI(const MCSubtargetInfo &STI) {
525 return STI.getFeatureBits()[AMDGPU::FeatureSouthernIslands];
526}
527
528bool isCI(const MCSubtargetInfo &STI) {
529 return STI.getFeatureBits()[AMDGPU::FeatureSeaIslands];
530}
531
532bool isVI(const MCSubtargetInfo &STI) {
533 return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
534}
535
536unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI) {
537
538 switch(Reg) {
539 default: break;
540 case AMDGPU::FLAT_SCR:
541 assert(!isSI(STI));
542 return isCI(STI) ? AMDGPU::FLAT_SCR_ci : AMDGPU::FLAT_SCR_vi;
543
544 case AMDGPU::FLAT_SCR_LO:
545 assert(!isSI(STI));
546 return isCI(STI) ? AMDGPU::FLAT_SCR_LO_ci : AMDGPU::FLAT_SCR_LO_vi;
547
548 case AMDGPU::FLAT_SCR_HI:
549 assert(!isSI(STI));
550 return isCI(STI) ? AMDGPU::FLAT_SCR_HI_ci : AMDGPU::FLAT_SCR_HI_vi;
551 }
552 return Reg;
553}
554
Dmitry Preobrazhensky03880f82017-03-03 14:31:06 +0000555unsigned mc2PseudoReg(unsigned Reg) {
556 switch (Reg) {
557 case AMDGPU::FLAT_SCR_ci:
558 case AMDGPU::FLAT_SCR_vi:
559 return FLAT_SCR;
560
561 case AMDGPU::FLAT_SCR_LO_ci:
562 case AMDGPU::FLAT_SCR_LO_vi:
563 return AMDGPU::FLAT_SCR_LO;
564
565 case AMDGPU::FLAT_SCR_HI_ci:
566 case AMDGPU::FLAT_SCR_HI_vi:
567 return AMDGPU::FLAT_SCR_HI;
568
569 default:
570 return Reg;
571 }
572}
573
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000574bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000575 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000576 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000577 return OpType >= AMDGPU::OPERAND_SRC_FIRST &&
578 OpType <= AMDGPU::OPERAND_SRC_LAST;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000579}
580
581bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000582 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000583 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000584 switch (OpType) {
585 case AMDGPU::OPERAND_REG_IMM_FP32:
586 case AMDGPU::OPERAND_REG_IMM_FP64:
587 case AMDGPU::OPERAND_REG_IMM_FP16:
588 case AMDGPU::OPERAND_REG_INLINE_C_FP32:
589 case AMDGPU::OPERAND_REG_INLINE_C_FP64:
590 case AMDGPU::OPERAND_REG_INLINE_C_FP16:
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000591 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
Matt Arsenault4bd72362016-12-10 00:39:12 +0000592 return true;
593 default:
594 return false;
595 }
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000596}
597
598bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000599 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000600 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000601 return OpType >= AMDGPU::OPERAND_REG_INLINE_C_FIRST &&
602 OpType <= AMDGPU::OPERAND_REG_INLINE_C_LAST;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000603}
604
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000605// Avoid using MCRegisterClass::getSize, since that function will go away
606// (move from MC* level to Target* level). Return size in bits.
Tom Stellardb133fbb2016-10-27 23:05:31 +0000607unsigned getRegBitWidth(unsigned RCID) {
608 switch (RCID) {
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000609 case AMDGPU::SGPR_32RegClassID:
610 case AMDGPU::VGPR_32RegClassID:
611 case AMDGPU::VS_32RegClassID:
612 case AMDGPU::SReg_32RegClassID:
613 case AMDGPU::SReg_32_XM0RegClassID:
614 return 32;
615 case AMDGPU::SGPR_64RegClassID:
616 case AMDGPU::VS_64RegClassID:
617 case AMDGPU::SReg_64RegClassID:
618 case AMDGPU::VReg_64RegClassID:
619 return 64;
620 case AMDGPU::VReg_96RegClassID:
621 return 96;
622 case AMDGPU::SGPR_128RegClassID:
623 case AMDGPU::SReg_128RegClassID:
624 case AMDGPU::VReg_128RegClassID:
625 return 128;
626 case AMDGPU::SReg_256RegClassID:
627 case AMDGPU::VReg_256RegClassID:
628 return 256;
629 case AMDGPU::SReg_512RegClassID:
630 case AMDGPU::VReg_512RegClassID:
631 return 512;
632 default:
633 llvm_unreachable("Unexpected register class");
634 }
635}
636
Tom Stellardb133fbb2016-10-27 23:05:31 +0000637unsigned getRegBitWidth(const MCRegisterClass &RC) {
638 return getRegBitWidth(RC.getID());
639}
640
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000641unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
642 unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000643 assert(OpNo < Desc.NumOperands);
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000644 unsigned RCID = Desc.OpInfo[OpNo].RegClass;
645 return getRegBitWidth(MRI->getRegClass(RCID)) / 8;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000646}
647
Matt Arsenault26faed32016-12-05 22:26:17 +0000648bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi) {
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000649 if (Literal >= -16 && Literal <= 64)
650 return true;
651
Matt Arsenault26faed32016-12-05 22:26:17 +0000652 uint64_t Val = static_cast<uint64_t>(Literal);
653 return (Val == DoubleToBits(0.0)) ||
654 (Val == DoubleToBits(1.0)) ||
655 (Val == DoubleToBits(-1.0)) ||
656 (Val == DoubleToBits(0.5)) ||
657 (Val == DoubleToBits(-0.5)) ||
658 (Val == DoubleToBits(2.0)) ||
659 (Val == DoubleToBits(-2.0)) ||
660 (Val == DoubleToBits(4.0)) ||
661 (Val == DoubleToBits(-4.0)) ||
662 (Val == 0x3fc45f306dc9c882 && HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000663}
664
Matt Arsenault26faed32016-12-05 22:26:17 +0000665bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi) {
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000666 if (Literal >= -16 && Literal <= 64)
667 return true;
668
Matt Arsenault4bd72362016-12-10 00:39:12 +0000669 // The actual type of the operand does not seem to matter as long
670 // as the bits match one of the inline immediate values. For example:
671 //
672 // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal,
673 // so it is a legal inline immediate.
674 //
675 // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in
676 // floating-point, so it is a legal inline immediate.
677
Matt Arsenault26faed32016-12-05 22:26:17 +0000678 uint32_t Val = static_cast<uint32_t>(Literal);
679 return (Val == FloatToBits(0.0f)) ||
680 (Val == FloatToBits(1.0f)) ||
681 (Val == FloatToBits(-1.0f)) ||
682 (Val == FloatToBits(0.5f)) ||
683 (Val == FloatToBits(-0.5f)) ||
684 (Val == FloatToBits(2.0f)) ||
685 (Val == FloatToBits(-2.0f)) ||
686 (Val == FloatToBits(4.0f)) ||
687 (Val == FloatToBits(-4.0f)) ||
688 (Val == 0x3e22f983 && HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000689}
690
Matt Arsenault4bd72362016-12-10 00:39:12 +0000691bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi) {
Sam Kolton9dffada2017-01-17 15:26:02 +0000692 if (!HasInv2Pi)
693 return false;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000694
695 if (Literal >= -16 && Literal <= 64)
696 return true;
697
698 uint16_t Val = static_cast<uint16_t>(Literal);
699 return Val == 0x3C00 || // 1.0
700 Val == 0xBC00 || // -1.0
701 Val == 0x3800 || // 0.5
702 Val == 0xB800 || // -0.5
703 Val == 0x4000 || // 2.0
704 Val == 0xC000 || // -2.0
705 Val == 0x4400 || // 4.0
706 Val == 0xC400 || // -4.0
707 Val == 0x3118; // 1/2pi
708}
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000709
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000710bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi) {
711 assert(HasInv2Pi);
712
Konstantin Zhuravlyov3d1cc882017-04-21 19:45:22 +0000713 if (!EnablePackedInlinableLiterals)
714 return false;
715
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000716 int16_t Lo16 = static_cast<int16_t>(Literal);
717 int16_t Hi16 = static_cast<int16_t>(Literal >> 16);
718 return Lo16 == Hi16 && isInlinableLiteral16(Lo16, HasInv2Pi);
719}
720
Tom Stellard08efb7e2017-01-27 18:41:14 +0000721bool isUniformMMO(const MachineMemOperand *MMO) {
722 const Value *Ptr = MMO->getValue();
723 // UndefValue means this is a load of a kernel input. These are uniform.
724 // Sometimes LDS instructions have constant pointers.
725 // If Ptr is null, then that means this mem operand contains a
726 // PseudoSourceValue like GOT.
727 if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) ||
728 isa<Constant>(Ptr) || isa<GlobalValue>(Ptr))
729 return true;
730
731 const Instruction *I = dyn_cast<Instruction>(Ptr);
732 return I && I->getMetadata("amdgpu.uniform");
733}
734
735int64_t getSMRDEncodedOffset(const MCSubtargetInfo &ST, int64_t ByteOffset) {
736 if (isSI(ST) || isCI(ST))
737 return ByteOffset >> 2;
738
739 return ByteOffset;
740}
741
742bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset) {
743 int64_t EncodedOffset = getSMRDEncodedOffset(ST, ByteOffset);
744 return isSI(ST) || isCI(ST) ? isUInt<8>(EncodedOffset) :
745 isUInt<20>(EncodedOffset);
746}
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000747} // end namespace AMDGPU
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000748
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000749} // end namespace llvm
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000750
751const unsigned AMDGPUAS::MAX_COMMON_ADDRESS;
752const unsigned AMDGPUAS::GLOBAL_ADDRESS;
753const unsigned AMDGPUAS::LOCAL_ADDRESS;
754const unsigned AMDGPUAS::PARAM_D_ADDRESS;
755const unsigned AMDGPUAS::PARAM_I_ADDRESS;
756const unsigned AMDGPUAS::CONSTANT_BUFFER_0;
757const unsigned AMDGPUAS::CONSTANT_BUFFER_1;
758const unsigned AMDGPUAS::CONSTANT_BUFFER_2;
759const unsigned AMDGPUAS::CONSTANT_BUFFER_3;
760const unsigned AMDGPUAS::CONSTANT_BUFFER_4;
761const unsigned AMDGPUAS::CONSTANT_BUFFER_5;
762const unsigned AMDGPUAS::CONSTANT_BUFFER_6;
763const unsigned AMDGPUAS::CONSTANT_BUFFER_7;
764const unsigned AMDGPUAS::CONSTANT_BUFFER_8;
765const unsigned AMDGPUAS::CONSTANT_BUFFER_9;
766const unsigned AMDGPUAS::CONSTANT_BUFFER_10;
767const unsigned AMDGPUAS::CONSTANT_BUFFER_11;
768const unsigned AMDGPUAS::CONSTANT_BUFFER_12;
769const unsigned AMDGPUAS::CONSTANT_BUFFER_13;
770const unsigned AMDGPUAS::CONSTANT_BUFFER_14;
771const unsigned AMDGPUAS::CONSTANT_BUFFER_15;
772const unsigned AMDGPUAS::UNKNOWN_ADDRESS_SPACE;
773
774namespace llvm {
775namespace AMDGPU {
776
777AMDGPUAS getAMDGPUAS(Triple T) {
778 auto Env = T.getEnvironmentName();
779 AMDGPUAS AS;
780 if (Env == "amdgiz" || Env == "amdgizcl") {
781 AS.FLAT_ADDRESS = 0;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000782 AS.PRIVATE_ADDRESS = 5;
Yaxun Liu76ae47c2017-04-06 19:17:32 +0000783 AS.REGION_ADDRESS = 4;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000784 }
785 else {
786 AS.FLAT_ADDRESS = 4;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000787 AS.PRIVATE_ADDRESS = 0;
788 AS.REGION_ADDRESS = 5;
789 }
790 return AS;
791}
792
793AMDGPUAS getAMDGPUAS(const TargetMachine &M) {
794 return getAMDGPUAS(M.getTargetTriple());
795}
796
797AMDGPUAS getAMDGPUAS(const Module &M) {
798 return getAMDGPUAS(Triple(M.getTargetTriple()));
799}
800} // namespace AMDGPU
801} // namespace llvm