blob: abb489b88c5c579dc1524e4ad9fec44c6b669e04 [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 {
96namespace AMDGPU {
97
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000098namespace IsaInfo {
Tom Stellard347ac792015-06-26 21:15:07 +000099
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000100IsaVersion getIsaVersion(const FeatureBitset &Features) {
101 // CI.
Tom Stellard347ac792015-06-26 21:15:07 +0000102 if (Features.test(FeatureISAVersion7_0_0))
103 return {7, 0, 0};
Tom Stellard347ac792015-06-26 21:15:07 +0000104 if (Features.test(FeatureISAVersion7_0_1))
105 return {7, 0, 1};
Yaxun Liu94add852016-10-26 16:37:56 +0000106 if (Features.test(FeatureISAVersion7_0_2))
107 return {7, 0, 2};
108
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000109 // VI.
Tom Stellard347ac792015-06-26 21:15:07 +0000110 if (Features.test(FeatureISAVersion8_0_0))
111 return {8, 0, 0};
Tom Stellard347ac792015-06-26 21:15:07 +0000112 if (Features.test(FeatureISAVersion8_0_1))
113 return {8, 0, 1};
Changpeng Fang98317d22016-10-11 16:00:47 +0000114 if (Features.test(FeatureISAVersion8_0_2))
115 return {8, 0, 2};
Changpeng Fangc16be002016-01-13 20:39:25 +0000116 if (Features.test(FeatureISAVersion8_0_3))
117 return {8, 0, 3};
Yaxun Liu94add852016-10-26 16:37:56 +0000118 if (Features.test(FeatureISAVersion8_0_4))
119 return {8, 0, 4};
Yaxun Liu94add852016-10-26 16:37:56 +0000120 if (Features.test(FeatureISAVersion8_1_0))
121 return {8, 1, 0};
122
Matt Arsenaulte823d922017-02-18 18:29:53 +0000123 // GFX9.
124 if (Features.test(FeatureISAVersion9_0_0))
125 return {9, 0, 0};
126 if (Features.test(FeatureISAVersion9_0_1))
127 return {9, 0, 1};
128
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000129 if (!Features.test(FeatureGCN) || Features.test(FeatureSouthernIslands))
130 return {0, 0, 0};
131 return {7, 0, 0};
Tom Stellard347ac792015-06-26 21:15:07 +0000132}
133
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000134unsigned getWavefrontSize(const FeatureBitset &Features) {
135 if (Features.test(FeatureWavefrontSize16))
136 return 16;
137 if (Features.test(FeatureWavefrontSize32))
138 return 32;
139
140 return 64;
141}
142
143unsigned getLocalMemorySize(const FeatureBitset &Features) {
144 if (Features.test(FeatureLocalMemorySize32768))
145 return 32768;
146 if (Features.test(FeatureLocalMemorySize65536))
147 return 65536;
148
149 return 0;
150}
151
152unsigned getEUsPerCU(const FeatureBitset &Features) {
153 return 4;
154}
155
156unsigned getMaxWorkGroupsPerCU(const FeatureBitset &Features,
157 unsigned FlatWorkGroupSize) {
158 if (!Features.test(FeatureGCN))
159 return 8;
Stanislav Mekhanoshin19f98c62017-02-15 01:03:59 +0000160 unsigned N = getWavesPerWorkGroup(Features, FlatWorkGroupSize);
161 if (N == 1)
162 return 40;
163 N = 40 / N;
164 return std::min(N, 16u);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000165}
166
167unsigned getMaxWavesPerCU(const FeatureBitset &Features) {
168 return getMaxWavesPerEU(Features) * getEUsPerCU(Features);
169}
170
171unsigned getMaxWavesPerCU(const FeatureBitset &Features,
172 unsigned FlatWorkGroupSize) {
173 return getWavesPerWorkGroup(Features, FlatWorkGroupSize);
174}
175
176unsigned getMinWavesPerEU(const FeatureBitset &Features) {
177 return 1;
178}
179
180unsigned getMaxWavesPerEU(const FeatureBitset &Features) {
181 if (!Features.test(FeatureGCN))
182 return 8;
183 // FIXME: Need to take scratch memory into account.
184 return 10;
185}
186
187unsigned getMaxWavesPerEU(const FeatureBitset &Features,
188 unsigned FlatWorkGroupSize) {
189 return alignTo(getMaxWavesPerCU(Features, FlatWorkGroupSize),
190 getEUsPerCU(Features)) / getEUsPerCU(Features);
191}
192
193unsigned getMinFlatWorkGroupSize(const FeatureBitset &Features) {
194 return 1;
195}
196
197unsigned getMaxFlatWorkGroupSize(const FeatureBitset &Features) {
198 return 2048;
199}
200
201unsigned getWavesPerWorkGroup(const FeatureBitset &Features,
202 unsigned FlatWorkGroupSize) {
203 return alignTo(FlatWorkGroupSize, getWavefrontSize(Features)) /
204 getWavefrontSize(Features);
205}
206
207unsigned getSGPRAllocGranule(const FeatureBitset &Features) {
208 IsaVersion Version = getIsaVersion(Features);
209 if (Version.Major >= 8)
210 return 16;
211 return 8;
212}
213
214unsigned getSGPREncodingGranule(const FeatureBitset &Features) {
215 return 8;
216}
217
218unsigned getTotalNumSGPRs(const FeatureBitset &Features) {
219 IsaVersion Version = getIsaVersion(Features);
220 if (Version.Major >= 8)
221 return 800;
222 return 512;
223}
224
225unsigned getAddressableNumSGPRs(const FeatureBitset &Features) {
226 if (Features.test(FeatureSGPRInitBug))
227 return FIXED_NUM_SGPRS_FOR_INIT_BUG;
228
229 IsaVersion Version = getIsaVersion(Features);
230 if (Version.Major >= 8)
231 return 102;
232 return 104;
233}
234
235unsigned getMinNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000236 assert(WavesPerEU != 0);
237
238 if (WavesPerEU >= getMaxWavesPerEU(Features))
239 return 0;
240 unsigned MinNumSGPRs =
241 alignDown(getTotalNumSGPRs(Features) / (WavesPerEU + 1),
242 getSGPRAllocGranule(Features)) + 1;
243 return std::min(MinNumSGPRs, getAddressableNumSGPRs(Features));
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000244}
245
246unsigned getMaxNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU,
247 bool Addressable) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000248 assert(WavesPerEU != 0);
249
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000250 IsaVersion Version = getIsaVersion(Features);
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000251 unsigned MaxNumSGPRs = alignDown(getTotalNumSGPRs(Features) / WavesPerEU,
252 getSGPRAllocGranule(Features));
253 unsigned AddressableNumSGPRs = getAddressableNumSGPRs(Features);
254 if (Version.Major >= 8 && !Addressable)
255 AddressableNumSGPRs = 112;
256 return std::min(MaxNumSGPRs, AddressableNumSGPRs);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000257}
258
259unsigned getVGPRAllocGranule(const FeatureBitset &Features) {
260 return 4;
261}
262
263unsigned getVGPREncodingGranule(const FeatureBitset &Features) {
264 return getVGPRAllocGranule(Features);
265}
266
267unsigned getTotalNumVGPRs(const FeatureBitset &Features) {
268 return 256;
269}
270
271unsigned getAddressableNumVGPRs(const FeatureBitset &Features) {
272 return getTotalNumVGPRs(Features);
273}
274
275unsigned getMinNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000276 assert(WavesPerEU != 0);
277
278 if (WavesPerEU >= getMaxWavesPerEU(Features))
279 return 0;
280 unsigned MinNumVGPRs =
281 alignDown(getTotalNumVGPRs(Features) / (WavesPerEU + 1),
282 getVGPRAllocGranule(Features)) + 1;
283 return std::min(MinNumVGPRs, getAddressableNumVGPRs(Features));
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000284}
285
286unsigned getMaxNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000287 assert(WavesPerEU != 0);
288
289 unsigned MaxNumVGPRs = alignDown(getTotalNumVGPRs(Features) / WavesPerEU,
290 getVGPRAllocGranule(Features));
291 unsigned AddressableNumVGPRs = getAddressableNumVGPRs(Features);
292 return std::min(MaxNumVGPRs, AddressableNumVGPRs);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000293}
294
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000295} // end namespace IsaInfo
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000296
Tom Stellardff7416b2015-06-26 21:58:31 +0000297void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
298 const FeatureBitset &Features) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000299 IsaInfo::IsaVersion ISA = IsaInfo::getIsaVersion(Features);
Tom Stellardff7416b2015-06-26 21:58:31 +0000300
301 memset(&Header, 0, sizeof(Header));
302
303 Header.amd_kernel_code_version_major = 1;
Konstantin Zhuravlyov182e9cc2017-02-28 17:17:52 +0000304 Header.amd_kernel_code_version_minor = 1;
Tom Stellardff7416b2015-06-26 21:58:31 +0000305 Header.amd_machine_kind = 1; // AMD_MACHINE_KIND_AMDGPU
306 Header.amd_machine_version_major = ISA.Major;
307 Header.amd_machine_version_minor = ISA.Minor;
308 Header.amd_machine_version_stepping = ISA.Stepping;
309 Header.kernel_code_entry_byte_offset = sizeof(Header);
310 // wavefront_size is specified as a power of 2: 2^6 = 64 threads.
311 Header.wavefront_size = 6;
Matt Arsenault5d910192017-01-25 20:21:57 +0000312
313 // If the code object does not support indirect functions, then the value must
314 // be 0xffffffff.
315 Header.call_convention = -1;
316
Tom Stellardff7416b2015-06-26 21:58:31 +0000317 // These alignment values are specified in powers of two, so alignment =
318 // 2^n. The minimum alignment is 2^4 = 16.
319 Header.kernarg_segment_alignment = 4;
320 Header.group_segment_alignment = 4;
321 Header.private_segment_alignment = 4;
322}
323
Tom Stellarde135ffd2015-09-25 21:41:28 +0000324MCSection *getHSATextSection(MCContext &Ctx) {
325 return Ctx.getELFSection(".hsatext", ELF::SHT_PROGBITS,
326 ELF::SHF_ALLOC | ELF::SHF_WRITE |
327 ELF::SHF_EXECINSTR |
328 ELF::SHF_AMDGPU_HSA_AGENT |
329 ELF::SHF_AMDGPU_HSA_CODE);
330}
331
Tom Stellard00f2f912015-12-02 19:47:57 +0000332MCSection *getHSADataGlobalAgentSection(MCContext &Ctx) {
333 return Ctx.getELFSection(".hsadata_global_agent", ELF::SHT_PROGBITS,
334 ELF::SHF_ALLOC | ELF::SHF_WRITE |
335 ELF::SHF_AMDGPU_HSA_GLOBAL |
336 ELF::SHF_AMDGPU_HSA_AGENT);
337}
338
339MCSection *getHSADataGlobalProgramSection(MCContext &Ctx) {
340 return Ctx.getELFSection(".hsadata_global_program", ELF::SHT_PROGBITS,
341 ELF::SHF_ALLOC | ELF::SHF_WRITE |
342 ELF::SHF_AMDGPU_HSA_GLOBAL);
343}
344
Tom Stellard9760f032015-12-03 03:34:32 +0000345MCSection *getHSARodataReadonlyAgentSection(MCContext &Ctx) {
346 return Ctx.getELFSection(".hsarodata_readonly_agent", ELF::SHT_PROGBITS,
347 ELF::SHF_ALLOC | ELF::SHF_AMDGPU_HSA_READONLY |
348 ELF::SHF_AMDGPU_HSA_AGENT);
349}
350
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000351bool isGroupSegment(const GlobalValue *GV, AMDGPUAS AS) {
352 return GV->getType()->getAddressSpace() == AS.LOCAL_ADDRESS;
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000353}
354
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000355bool isGlobalSegment(const GlobalValue *GV, AMDGPUAS AS) {
356 return GV->getType()->getAddressSpace() == AS.GLOBAL_ADDRESS;
Tom Stellard00f2f912015-12-02 19:47:57 +0000357}
358
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000359bool isReadOnlySegment(const GlobalValue *GV, AMDGPUAS AS) {
360 return GV->getType()->getAddressSpace() == AS.CONSTANT_ADDRESS;
Tom Stellard00f2f912015-12-02 19:47:57 +0000361}
362
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +0000363bool shouldEmitConstantsToTextSection(const Triple &TT) {
364 return TT.getOS() != Triple::AMDHSA;
365}
366
Matt Arsenault83002722016-05-12 02:45:18 +0000367int getIntegerAttribute(const Function &F, StringRef Name, int Default) {
Marek Olsakfccabaf2016-01-13 11:45:36 +0000368 Attribute A = F.getFnAttribute(Name);
Matt Arsenault83002722016-05-12 02:45:18 +0000369 int Result = Default;
Tom Stellardac00eb52015-12-15 16:26:16 +0000370
371 if (A.isStringAttribute()) {
372 StringRef Str = A.getValueAsString();
Marek Olsakfccabaf2016-01-13 11:45:36 +0000373 if (Str.getAsInteger(0, Result)) {
Tom Stellardac00eb52015-12-15 16:26:16 +0000374 LLVMContext &Ctx = F.getContext();
Matt Arsenault83002722016-05-12 02:45:18 +0000375 Ctx.emitError("can't parse integer attribute " + Name);
Tom Stellardac00eb52015-12-15 16:26:16 +0000376 }
377 }
Matt Arsenault83002722016-05-12 02:45:18 +0000378
Marek Olsakfccabaf2016-01-13 11:45:36 +0000379 return Result;
380}
381
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000382std::pair<int, int> getIntegerPairAttribute(const Function &F,
383 StringRef Name,
384 std::pair<int, int> Default,
385 bool OnlyFirstRequired) {
386 Attribute A = F.getFnAttribute(Name);
387 if (!A.isStringAttribute())
388 return Default;
389
390 LLVMContext &Ctx = F.getContext();
391 std::pair<int, int> Ints = Default;
392 std::pair<StringRef, StringRef> Strs = A.getValueAsString().split(',');
393 if (Strs.first.trim().getAsInteger(0, Ints.first)) {
394 Ctx.emitError("can't parse first integer attribute " + Name);
395 return Default;
396 }
397 if (Strs.second.trim().getAsInteger(0, Ints.second)) {
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000398 if (!OnlyFirstRequired || !Strs.second.trim().empty()) {
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000399 Ctx.emitError("can't parse second integer attribute " + Name);
400 return Default;
401 }
402 }
403
404 return Ints;
Tom Stellard79a1fd72016-04-14 16:27:07 +0000405}
406
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000407unsigned getVmcntBitMask(const IsaInfo::IsaVersion &Version) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000408 unsigned VmcntLo = (1 << getVmcntBitWidthLo()) - 1;
409 if (Version.Major < 9)
410 return VmcntLo;
411
412 unsigned VmcntHi = ((1 << getVmcntBitWidthHi()) - 1) << getVmcntBitWidthLo();
413 return VmcntLo | VmcntHi;
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000414}
415
416unsigned getExpcntBitMask(const IsaInfo::IsaVersion &Version) {
417 return (1 << getExpcntBitWidth()) - 1;
418}
419
420unsigned getLgkmcntBitMask(const IsaInfo::IsaVersion &Version) {
421 return (1 << getLgkmcntBitWidth()) - 1;
422}
423
424unsigned getWaitcntBitMask(const IsaInfo::IsaVersion &Version) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000425 unsigned VmcntLo = getBitMask(getVmcntBitShiftLo(), getVmcntBitWidthLo());
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000426 unsigned Expcnt = getBitMask(getExpcntBitShift(), getExpcntBitWidth());
427 unsigned Lgkmcnt = getBitMask(getLgkmcntBitShift(), getLgkmcntBitWidth());
Matt Arsenaulte823d922017-02-18 18:29:53 +0000428 unsigned Waitcnt = VmcntLo | Expcnt | Lgkmcnt;
429 if (Version.Major < 9)
430 return Waitcnt;
431
432 unsigned VmcntHi = getBitMask(getVmcntBitShiftHi(), getVmcntBitWidthHi());
433 return Waitcnt | VmcntHi;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000434}
435
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000436unsigned decodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000437 unsigned VmcntLo =
438 unpackBits(Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo());
439 if (Version.Major < 9)
440 return VmcntLo;
441
442 unsigned VmcntHi =
443 unpackBits(Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi());
444 VmcntHi <<= getVmcntBitWidthLo();
445 return VmcntLo | VmcntHi;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000446}
447
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000448unsigned decodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000449 return unpackBits(Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
450}
451
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000452unsigned decodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000453 return unpackBits(Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
454}
455
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000456void decodeWaitcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000457 unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt) {
458 Vmcnt = decodeVmcnt(Version, Waitcnt);
459 Expcnt = decodeExpcnt(Version, Waitcnt);
460 Lgkmcnt = decodeLgkmcnt(Version, Waitcnt);
461}
462
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000463unsigned encodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
464 unsigned Vmcnt) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000465 Waitcnt =
466 packBits(Vmcnt, Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo());
467 if (Version.Major < 9)
468 return Waitcnt;
469
470 Vmcnt >>= getVmcntBitWidthLo();
471 return packBits(Vmcnt, Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi());
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000472}
473
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000474unsigned encodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
475 unsigned Expcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000476 return packBits(Expcnt, Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
477}
478
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000479unsigned encodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
480 unsigned Lgkmcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000481 return packBits(Lgkmcnt, Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
482}
483
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000484unsigned encodeWaitcnt(const IsaInfo::IsaVersion &Version,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000485 unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt) {
Konstantin Zhuravlyov31dbb032017-01-06 17:23:21 +0000486 unsigned Waitcnt = getWaitcntBitMask(Version);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000487 Waitcnt = encodeVmcnt(Version, Waitcnt, Vmcnt);
488 Waitcnt = encodeExpcnt(Version, Waitcnt, Expcnt);
489 Waitcnt = encodeLgkmcnt(Version, Waitcnt, Lgkmcnt);
490 return Waitcnt;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000491}
492
Marek Olsakfccabaf2016-01-13 11:45:36 +0000493unsigned getInitialPSInputAddr(const Function &F) {
494 return getIntegerAttribute(F, "InitialPSInputAddr", 0);
Tom Stellardac00eb52015-12-15 16:26:16 +0000495}
496
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000497bool isShader(CallingConv::ID cc) {
498 switch(cc) {
499 case CallingConv::AMDGPU_VS:
500 case CallingConv::AMDGPU_GS:
501 case CallingConv::AMDGPU_PS:
502 case CallingConv::AMDGPU_CS:
503 return true;
504 default:
505 return false;
506 }
507}
508
509bool isCompute(CallingConv::ID cc) {
510 return !isShader(cc) || cc == CallingConv::AMDGPU_CS;
511}
512
Tom Stellard2b65ed32015-12-21 18:44:27 +0000513bool isSI(const MCSubtargetInfo &STI) {
514 return STI.getFeatureBits()[AMDGPU::FeatureSouthernIslands];
515}
516
517bool isCI(const MCSubtargetInfo &STI) {
518 return STI.getFeatureBits()[AMDGPU::FeatureSeaIslands];
519}
520
521bool isVI(const MCSubtargetInfo &STI) {
522 return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
523}
524
525unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI) {
526
527 switch(Reg) {
528 default: break;
529 case AMDGPU::FLAT_SCR:
530 assert(!isSI(STI));
531 return isCI(STI) ? AMDGPU::FLAT_SCR_ci : AMDGPU::FLAT_SCR_vi;
532
533 case AMDGPU::FLAT_SCR_LO:
534 assert(!isSI(STI));
535 return isCI(STI) ? AMDGPU::FLAT_SCR_LO_ci : AMDGPU::FLAT_SCR_LO_vi;
536
537 case AMDGPU::FLAT_SCR_HI:
538 assert(!isSI(STI));
539 return isCI(STI) ? AMDGPU::FLAT_SCR_HI_ci : AMDGPU::FLAT_SCR_HI_vi;
540 }
541 return Reg;
542}
543
Dmitry Preobrazhensky03880f82017-03-03 14:31:06 +0000544unsigned mc2PseudoReg(unsigned Reg) {
545 switch (Reg) {
546 case AMDGPU::FLAT_SCR_ci:
547 case AMDGPU::FLAT_SCR_vi:
548 return FLAT_SCR;
549
550 case AMDGPU::FLAT_SCR_LO_ci:
551 case AMDGPU::FLAT_SCR_LO_vi:
552 return AMDGPU::FLAT_SCR_LO;
553
554 case AMDGPU::FLAT_SCR_HI_ci:
555 case AMDGPU::FLAT_SCR_HI_vi:
556 return AMDGPU::FLAT_SCR_HI;
557
558 default:
559 return Reg;
560 }
561}
562
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000563bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000564 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000565 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000566 return OpType >= AMDGPU::OPERAND_SRC_FIRST &&
567 OpType <= AMDGPU::OPERAND_SRC_LAST;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000568}
569
570bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000571 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000572 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000573 switch (OpType) {
574 case AMDGPU::OPERAND_REG_IMM_FP32:
575 case AMDGPU::OPERAND_REG_IMM_FP64:
576 case AMDGPU::OPERAND_REG_IMM_FP16:
577 case AMDGPU::OPERAND_REG_INLINE_C_FP32:
578 case AMDGPU::OPERAND_REG_INLINE_C_FP64:
579 case AMDGPU::OPERAND_REG_INLINE_C_FP16:
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000580 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
Matt Arsenault4bd72362016-12-10 00:39:12 +0000581 return true;
582 default:
583 return false;
584 }
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000585}
586
587bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000588 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000589 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000590 return OpType >= AMDGPU::OPERAND_REG_INLINE_C_FIRST &&
591 OpType <= AMDGPU::OPERAND_REG_INLINE_C_LAST;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000592}
593
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000594// Avoid using MCRegisterClass::getSize, since that function will go away
595// (move from MC* level to Target* level). Return size in bits.
Tom Stellardb133fbb2016-10-27 23:05:31 +0000596unsigned getRegBitWidth(unsigned RCID) {
597 switch (RCID) {
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000598 case AMDGPU::SGPR_32RegClassID:
599 case AMDGPU::VGPR_32RegClassID:
600 case AMDGPU::VS_32RegClassID:
601 case AMDGPU::SReg_32RegClassID:
602 case AMDGPU::SReg_32_XM0RegClassID:
603 return 32;
604 case AMDGPU::SGPR_64RegClassID:
605 case AMDGPU::VS_64RegClassID:
606 case AMDGPU::SReg_64RegClassID:
607 case AMDGPU::VReg_64RegClassID:
608 return 64;
609 case AMDGPU::VReg_96RegClassID:
610 return 96;
611 case AMDGPU::SGPR_128RegClassID:
612 case AMDGPU::SReg_128RegClassID:
613 case AMDGPU::VReg_128RegClassID:
614 return 128;
615 case AMDGPU::SReg_256RegClassID:
616 case AMDGPU::VReg_256RegClassID:
617 return 256;
618 case AMDGPU::SReg_512RegClassID:
619 case AMDGPU::VReg_512RegClassID:
620 return 512;
621 default:
622 llvm_unreachable("Unexpected register class");
623 }
624}
625
Tom Stellardb133fbb2016-10-27 23:05:31 +0000626unsigned getRegBitWidth(const MCRegisterClass &RC) {
627 return getRegBitWidth(RC.getID());
628}
629
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000630unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
631 unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000632 assert(OpNo < Desc.NumOperands);
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000633 unsigned RCID = Desc.OpInfo[OpNo].RegClass;
634 return getRegBitWidth(MRI->getRegClass(RCID)) / 8;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000635}
636
Matt Arsenault26faed32016-12-05 22:26:17 +0000637bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi) {
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000638 if (Literal >= -16 && Literal <= 64)
639 return true;
640
Matt Arsenault26faed32016-12-05 22:26:17 +0000641 uint64_t Val = static_cast<uint64_t>(Literal);
642 return (Val == DoubleToBits(0.0)) ||
643 (Val == DoubleToBits(1.0)) ||
644 (Val == DoubleToBits(-1.0)) ||
645 (Val == DoubleToBits(0.5)) ||
646 (Val == DoubleToBits(-0.5)) ||
647 (Val == DoubleToBits(2.0)) ||
648 (Val == DoubleToBits(-2.0)) ||
649 (Val == DoubleToBits(4.0)) ||
650 (Val == DoubleToBits(-4.0)) ||
651 (Val == 0x3fc45f306dc9c882 && HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000652}
653
Matt Arsenault26faed32016-12-05 22:26:17 +0000654bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi) {
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000655 if (Literal >= -16 && Literal <= 64)
656 return true;
657
Matt Arsenault4bd72362016-12-10 00:39:12 +0000658 // The actual type of the operand does not seem to matter as long
659 // as the bits match one of the inline immediate values. For example:
660 //
661 // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal,
662 // so it is a legal inline immediate.
663 //
664 // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in
665 // floating-point, so it is a legal inline immediate.
666
Matt Arsenault26faed32016-12-05 22:26:17 +0000667 uint32_t Val = static_cast<uint32_t>(Literal);
668 return (Val == FloatToBits(0.0f)) ||
669 (Val == FloatToBits(1.0f)) ||
670 (Val == FloatToBits(-1.0f)) ||
671 (Val == FloatToBits(0.5f)) ||
672 (Val == FloatToBits(-0.5f)) ||
673 (Val == FloatToBits(2.0f)) ||
674 (Val == FloatToBits(-2.0f)) ||
675 (Val == FloatToBits(4.0f)) ||
676 (Val == FloatToBits(-4.0f)) ||
677 (Val == 0x3e22f983 && HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000678}
679
Matt Arsenault4bd72362016-12-10 00:39:12 +0000680bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi) {
Sam Kolton9dffada2017-01-17 15:26:02 +0000681 if (!HasInv2Pi)
682 return false;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000683
684 if (Literal >= -16 && Literal <= 64)
685 return true;
686
687 uint16_t Val = static_cast<uint16_t>(Literal);
688 return Val == 0x3C00 || // 1.0
689 Val == 0xBC00 || // -1.0
690 Val == 0x3800 || // 0.5
691 Val == 0xB800 || // -0.5
692 Val == 0x4000 || // 2.0
693 Val == 0xC000 || // -2.0
694 Val == 0x4400 || // 4.0
695 Val == 0xC400 || // -4.0
696 Val == 0x3118; // 1/2pi
697}
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000698
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000699bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi) {
700 assert(HasInv2Pi);
701
702 int16_t Lo16 = static_cast<int16_t>(Literal);
703 int16_t Hi16 = static_cast<int16_t>(Literal >> 16);
704 return Lo16 == Hi16 && isInlinableLiteral16(Lo16, HasInv2Pi);
705}
706
Tom Stellard08efb7e2017-01-27 18:41:14 +0000707bool isUniformMMO(const MachineMemOperand *MMO) {
708 const Value *Ptr = MMO->getValue();
709 // UndefValue means this is a load of a kernel input. These are uniform.
710 // Sometimes LDS instructions have constant pointers.
711 // If Ptr is null, then that means this mem operand contains a
712 // PseudoSourceValue like GOT.
713 if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) ||
714 isa<Constant>(Ptr) || isa<GlobalValue>(Ptr))
715 return true;
716
717 const Instruction *I = dyn_cast<Instruction>(Ptr);
718 return I && I->getMetadata("amdgpu.uniform");
719}
720
721int64_t getSMRDEncodedOffset(const MCSubtargetInfo &ST, int64_t ByteOffset) {
722 if (isSI(ST) || isCI(ST))
723 return ByteOffset >> 2;
724
725 return ByteOffset;
726}
727
728bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset) {
729 int64_t EncodedOffset = getSMRDEncodedOffset(ST, ByteOffset);
730 return isSI(ST) || isCI(ST) ? isUInt<8>(EncodedOffset) :
731 isUInt<20>(EncodedOffset);
732}
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000733} // end namespace AMDGPU
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000734
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000735} // end namespace llvm
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000736
737const unsigned AMDGPUAS::MAX_COMMON_ADDRESS;
738const unsigned AMDGPUAS::GLOBAL_ADDRESS;
739const unsigned AMDGPUAS::LOCAL_ADDRESS;
740const unsigned AMDGPUAS::PARAM_D_ADDRESS;
741const unsigned AMDGPUAS::PARAM_I_ADDRESS;
742const unsigned AMDGPUAS::CONSTANT_BUFFER_0;
743const unsigned AMDGPUAS::CONSTANT_BUFFER_1;
744const unsigned AMDGPUAS::CONSTANT_BUFFER_2;
745const unsigned AMDGPUAS::CONSTANT_BUFFER_3;
746const unsigned AMDGPUAS::CONSTANT_BUFFER_4;
747const unsigned AMDGPUAS::CONSTANT_BUFFER_5;
748const unsigned AMDGPUAS::CONSTANT_BUFFER_6;
749const unsigned AMDGPUAS::CONSTANT_BUFFER_7;
750const unsigned AMDGPUAS::CONSTANT_BUFFER_8;
751const unsigned AMDGPUAS::CONSTANT_BUFFER_9;
752const unsigned AMDGPUAS::CONSTANT_BUFFER_10;
753const unsigned AMDGPUAS::CONSTANT_BUFFER_11;
754const unsigned AMDGPUAS::CONSTANT_BUFFER_12;
755const unsigned AMDGPUAS::CONSTANT_BUFFER_13;
756const unsigned AMDGPUAS::CONSTANT_BUFFER_14;
757const unsigned AMDGPUAS::CONSTANT_BUFFER_15;
758const unsigned AMDGPUAS::UNKNOWN_ADDRESS_SPACE;
759
760namespace llvm {
761namespace AMDGPU {
762
763AMDGPUAS getAMDGPUAS(Triple T) {
764 auto Env = T.getEnvironmentName();
765 AMDGPUAS AS;
766 if (Env == "amdgiz" || Env == "amdgizcl") {
767 AS.FLAT_ADDRESS = 0;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000768 AS.PRIVATE_ADDRESS = 5;
Yaxun Liu76ae47c2017-04-06 19:17:32 +0000769 AS.REGION_ADDRESS = 4;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000770 }
771 else {
772 AS.FLAT_ADDRESS = 4;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000773 AS.PRIVATE_ADDRESS = 0;
774 AS.REGION_ADDRESS = 5;
775 }
776 return AS;
777}
778
779AMDGPUAS getAMDGPUAS(const TargetMachine &M) {
780 return getAMDGPUAS(M.getTargetTriple());
781}
782
783AMDGPUAS getAMDGPUAS(const Module &M) {
784 return getAMDGPUAS(Triple(M.getTargetTriple()));
785}
786} // namespace AMDGPU
787} // namespace llvm