blob: fc1e71299c502f4e9f538275e94505893829224f [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
Eugene Zelenkod96089b2017-02-14 00:33:36 +000010#include "AMDGPUBaseInfo.h"
Alexander Timofeev2e5eece2018-03-05 15:12:21 +000011#include "AMDGPUTargetTransformInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000012#include "AMDGPU.h"
Sam Kolton1eeb11b2016-09-09 14:44:04 +000013#include "SIDefines.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000014#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/Triple.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000016#include "llvm/BinaryFormat/ELF.h"
Tom Stellard08efb7e2017-01-27 18:41:14 +000017#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000018#include "llvm/IR/Attributes.h"
Tom Stellard08efb7e2017-01-27 18:41:14 +000019#include "llvm/IR/Constants.h"
Tom Stellardac00eb52015-12-15 16:26:16 +000020#include "llvm/IR/Function.h"
Tom Stellarde3b5aea2015-12-02 17:00:42 +000021#include "llvm/IR/GlobalValue.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000022#include "llvm/IR/Instruction.h"
Tom Stellardca166212017-01-30 21:56:46 +000023#include "llvm/IR/LLVMContext.h"
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000024#include "llvm/IR/Module.h"
Tom Stellarde135ffd2015-09-25 21:41:28 +000025#include "llvm/MC/MCContext.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000026#include "llvm/MC/MCInstrDesc.h"
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000027#include "llvm/MC/MCInstrInfo.h"
Sam Kolton1eeb11b2016-09-09 14:44:04 +000028#include "llvm/MC/MCRegisterInfo.h"
Tom Stellarde135ffd2015-09-25 21:41:28 +000029#include "llvm/MC/MCSectionELF.h"
Tom Stellard2b65ed32015-12-21 18:44:27 +000030#include "llvm/MC/MCSubtargetInfo.h"
Tom Stellard347ac792015-06-26 21:15:07 +000031#include "llvm/MC/SubtargetFeature.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000032#include "llvm/Support/Casting.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000033#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/MathExtras.h"
35#include <algorithm>
36#include <cassert>
37#include <cstdint>
38#include <cstring>
39#include <utility>
Tom Stellard347ac792015-06-26 21:15:07 +000040
Matt Arsenault678e1112017-04-10 17:58:06 +000041#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Tom Stellard347ac792015-06-26 21:15:07 +000042
Sam Koltona3ec5c12016-10-07 14:46:06 +000043#define GET_INSTRINFO_NAMED_OPS
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000044#define GET_INSTRMAP_INFO
Sam Koltona3ec5c12016-10-07 14:46:06 +000045#include "AMDGPUGenInstrInfo.inc"
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000046#undef GET_INSTRMAP_INFO
Sam Koltona3ec5c12016-10-07 14:46:06 +000047#undef GET_INSTRINFO_NAMED_OPS
Sam Koltona3ec5c12016-10-07 14:46:06 +000048
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000049namespace {
50
51/// \returns Bit mask for given bit \p Shift and bit \p Width.
52unsigned getBitMask(unsigned Shift, unsigned Width) {
53 return ((1 << Width) - 1) << Shift;
54}
55
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000056/// Packs \p Src into \p Dst for given bit \p Shift and bit \p Width.
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000057///
58/// \returns Packed \p Dst.
59unsigned packBits(unsigned Src, unsigned Dst, unsigned Shift, unsigned Width) {
60 Dst &= ~(1 << Shift) & ~getBitMask(Shift, Width);
61 Dst |= (Src << Shift) & getBitMask(Shift, Width);
62 return Dst;
63}
64
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000065/// Unpacks bits from \p Src for given bit \p Shift and bit \p Width.
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000066///
67/// \returns Unpacked bits.
68unsigned unpackBits(unsigned Src, unsigned Shift, unsigned Width) {
69 return (Src & getBitMask(Shift, Width)) >> Shift;
70}
71
Matt Arsenaulte823d922017-02-18 18:29:53 +000072/// \returns Vmcnt bit shift (lower bits).
73unsigned getVmcntBitShiftLo() { return 0; }
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000074
Matt Arsenaulte823d922017-02-18 18:29:53 +000075/// \returns Vmcnt bit width (lower bits).
76unsigned getVmcntBitWidthLo() { return 4; }
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000077
78/// \returns Expcnt bit shift.
79unsigned getExpcntBitShift() { return 4; }
80
81/// \returns Expcnt bit width.
82unsigned getExpcntBitWidth() { return 3; }
83
84/// \returns Lgkmcnt bit shift.
85unsigned getLgkmcntBitShift() { return 8; }
86
87/// \returns Lgkmcnt bit width.
88unsigned getLgkmcntBitWidth() { return 4; }
89
Matt Arsenaulte823d922017-02-18 18:29:53 +000090/// \returns Vmcnt bit shift (higher bits).
91unsigned getVmcntBitShiftHi() { return 14; }
92
93/// \returns Vmcnt bit width (higher bits).
94unsigned getVmcntBitWidthHi() { return 2; }
95
Eugene Zelenkod96089b2017-02-14 00:33:36 +000096} // end namespace anonymous
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000097
Tom Stellard347ac792015-06-26 21:15:07 +000098namespace llvm {
Konstantin Zhuravlyov3d1cc882017-04-21 19:45:22 +000099
Tom Stellard347ac792015-06-26 21:15:07 +0000100namespace AMDGPU {
101
Nicolai Haehnle0ab200b2018-06-21 13:36:44 +0000102struct MIMGInfo {
103 uint16_t Opcode;
104 uint16_t BaseOpcode;
105 uint8_t MIMGEncoding;
106 uint8_t VDataDwords;
107 uint8_t VAddrDwords;
108};
Matt Arsenaultcad7fa82017-12-13 21:07:51 +0000109
Nicolai Haehnle7a9c03f2018-06-21 13:36:57 +0000110#define GET_MIMGBaseOpcodesTable_IMPL
111#define GET_MIMGDimInfoTable_IMPL
Nicolai Haehnle0ab200b2018-06-21 13:36:44 +0000112#define GET_MIMGInfoTable_IMPL
Ryan Taylor894c8fd2018-08-01 12:12:01 +0000113#define GET_MIMGLZMappingTable_IMPL
Nicolai Haehnle0ab200b2018-06-21 13:36:44 +0000114#include "AMDGPUGenSearchableTables.inc"
Matt Arsenaultcad7fa82017-12-13 21:07:51 +0000115
Nicolai Haehnle7a9c03f2018-06-21 13:36:57 +0000116int getMIMGOpcode(unsigned BaseOpcode, unsigned MIMGEncoding,
117 unsigned VDataDwords, unsigned VAddrDwords) {
118 const MIMGInfo *Info = getMIMGOpcodeHelper(BaseOpcode, MIMGEncoding,
119 VDataDwords, VAddrDwords);
120 return Info ? Info->Opcode : -1;
121}
122
Nicolai Haehnle0ab200b2018-06-21 13:36:44 +0000123int getMaskedMIMGOp(unsigned Opc, unsigned NewChannels) {
124 const MIMGInfo *OrigInfo = getMIMGInfo(Opc);
125 const MIMGInfo *NewInfo =
126 getMIMGOpcodeHelper(OrigInfo->BaseOpcode, OrigInfo->MIMGEncoding,
127 NewChannels, OrigInfo->VAddrDwords);
128 return NewInfo ? NewInfo->Opcode : -1;
Dmitry Preobrazhensky0b4eb1e2018-01-26 15:43:29 +0000129}
130
Matt Arsenaultcad7fa82017-12-13 21:07:51 +0000131// Wrapper for Tablegen'd function. enum Subtarget is not defined in any
132// header files, so we need to wrap it in a function that takes unsigned
133// instead.
134int getMCOpcode(uint16_t Opcode, unsigned Gen) {
135 return getMCOpcodeGen(Opcode, static_cast<Subtarget>(Gen));
136}
137
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000138namespace IsaInfo {
Tom Stellard347ac792015-06-26 21:15:07 +0000139
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +0000140void streamIsaVersion(const MCSubtargetInfo *STI, raw_ostream &Stream) {
141 auto TargetTriple = STI->getTargetTriple();
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000142 auto Version = getIsaVersion(STI->getCPU());
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +0000143
144 Stream << TargetTriple.getArchName() << '-'
145 << TargetTriple.getVendorName() << '-'
146 << TargetTriple.getOSName() << '-'
147 << TargetTriple.getEnvironmentName() << '-'
148 << "gfx"
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000149 << Version.Major
150 << Version.Minor
151 << Version.Stepping;
Scott Linder1e8c2c72018-06-21 19:38:56 +0000152
153 if (hasXNACK(*STI))
154 Stream << "+xnack";
Konstantin Zhuravlyov108927b2018-11-05 22:44:19 +0000155 if (hasSRAMECC(*STI))
156 Stream << "+sram-ecc";
Scott Linder1e8c2c72018-06-21 19:38:56 +0000157
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +0000158 Stream.flush();
159}
160
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000161bool hasCodeObjectV3(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyovaf7b5d72018-11-15 23:14:23 +0000162 return STI->getTargetTriple().getOS() == Triple::AMDHSA &&
163 STI->getFeatureBits().test(FeatureCodeObjectV3);
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000164}
165
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000166unsigned getWavefrontSize(const MCSubtargetInfo *STI) {
167 if (STI->getFeatureBits().test(FeatureWavefrontSize16))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000168 return 16;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000169 if (STI->getFeatureBits().test(FeatureWavefrontSize32))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000170 return 32;
171
172 return 64;
173}
174
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000175unsigned getLocalMemorySize(const MCSubtargetInfo *STI) {
176 if (STI->getFeatureBits().test(FeatureLocalMemorySize32768))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000177 return 32768;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000178 if (STI->getFeatureBits().test(FeatureLocalMemorySize65536))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000179 return 65536;
180
181 return 0;
182}
183
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000184unsigned getEUsPerCU(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000185 return 4;
186}
187
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000188unsigned getMaxWorkGroupsPerCU(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000189 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000190 if (!STI->getFeatureBits().test(FeatureGCN))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000191 return 8;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000192 unsigned N = getWavesPerWorkGroup(STI, FlatWorkGroupSize);
Stanislav Mekhanoshin19f98c62017-02-15 01:03:59 +0000193 if (N == 1)
194 return 40;
195 N = 40 / N;
196 return std::min(N, 16u);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000197}
198
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000199unsigned getMaxWavesPerCU(const MCSubtargetInfo *STI) {
200 return getMaxWavesPerEU() * getEUsPerCU(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000201}
202
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000203unsigned getMaxWavesPerCU(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000204 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000205 return getWavesPerWorkGroup(STI, FlatWorkGroupSize);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000206}
207
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000208unsigned getMinWavesPerEU(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000209 return 1;
210}
211
Tom Stellardc5a154d2018-06-28 23:47:12 +0000212unsigned getMaxWavesPerEU() {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000213 // FIXME: Need to take scratch memory into account.
214 return 10;
215}
216
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000217unsigned getMaxWavesPerEU(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000218 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000219 return alignTo(getMaxWavesPerCU(STI, FlatWorkGroupSize),
220 getEUsPerCU(STI)) / getEUsPerCU(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000221}
222
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000223unsigned getMinFlatWorkGroupSize(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000224 return 1;
225}
226
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000227unsigned getMaxFlatWorkGroupSize(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000228 return 2048;
229}
230
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000231unsigned getWavesPerWorkGroup(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000232 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000233 return alignTo(FlatWorkGroupSize, getWavefrontSize(STI)) /
234 getWavefrontSize(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000235}
236
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000237unsigned getSGPRAllocGranule(const MCSubtargetInfo *STI) {
238 IsaVersion Version = getIsaVersion(STI->getCPU());
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000239 if (Version.Major >= 8)
240 return 16;
241 return 8;
242}
243
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000244unsigned getSGPREncodingGranule(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000245 return 8;
246}
247
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000248unsigned getTotalNumSGPRs(const MCSubtargetInfo *STI) {
249 IsaVersion Version = getIsaVersion(STI->getCPU());
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000250 if (Version.Major >= 8)
251 return 800;
252 return 512;
253}
254
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000255unsigned getAddressableNumSGPRs(const MCSubtargetInfo *STI) {
256 if (STI->getFeatureBits().test(FeatureSGPRInitBug))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000257 return FIXED_NUM_SGPRS_FOR_INIT_BUG;
258
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000259 IsaVersion Version = getIsaVersion(STI->getCPU());
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000260 if (Version.Major >= 8)
261 return 102;
262 return 104;
263}
264
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000265unsigned getMinNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000266 assert(WavesPerEU != 0);
267
Tom Stellardc5a154d2018-06-28 23:47:12 +0000268 if (WavesPerEU >= getMaxWavesPerEU())
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000269 return 0;
Konstantin Zhuravlyovc72ece62018-05-16 20:47:48 +0000270
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000271 unsigned MinNumSGPRs = getTotalNumSGPRs(STI) / (WavesPerEU + 1);
272 if (STI->getFeatureBits().test(FeatureTrapHandler))
Konstantin Zhuravlyovc72ece62018-05-16 20:47:48 +0000273 MinNumSGPRs -= std::min(MinNumSGPRs, (unsigned)TRAP_NUM_SGPRS);
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000274 MinNumSGPRs = alignDown(MinNumSGPRs, getSGPRAllocGranule(STI)) + 1;
275 return std::min(MinNumSGPRs, getAddressableNumSGPRs(STI));
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000276}
277
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000278unsigned getMaxNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000279 bool Addressable) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000280 assert(WavesPerEU != 0);
281
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000282 IsaVersion Version = getIsaVersion(STI->getCPU());
283 unsigned AddressableNumSGPRs = getAddressableNumSGPRs(STI);
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000284 if (Version.Major >= 8 && !Addressable)
285 AddressableNumSGPRs = 112;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000286 unsigned MaxNumSGPRs = getTotalNumSGPRs(STI) / WavesPerEU;
287 if (STI->getFeatureBits().test(FeatureTrapHandler))
Konstantin Zhuravlyovc72ece62018-05-16 20:47:48 +0000288 MaxNumSGPRs -= std::min(MaxNumSGPRs, (unsigned)TRAP_NUM_SGPRS);
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000289 MaxNumSGPRs = alignDown(MaxNumSGPRs, getSGPRAllocGranule(STI));
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000290 return std::min(MaxNumSGPRs, AddressableNumSGPRs);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000291}
292
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000293unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed,
Scott Linder1e8c2c72018-06-21 19:38:56 +0000294 bool FlatScrUsed, bool XNACKUsed) {
295 unsigned ExtraSGPRs = 0;
296 if (VCCUsed)
297 ExtraSGPRs = 2;
298
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000299 IsaVersion Version = getIsaVersion(STI->getCPU());
Scott Linder1e8c2c72018-06-21 19:38:56 +0000300 if (Version.Major < 8) {
301 if (FlatScrUsed)
302 ExtraSGPRs = 4;
303 } else {
304 if (XNACKUsed)
305 ExtraSGPRs = 4;
306
307 if (FlatScrUsed)
308 ExtraSGPRs = 6;
309 }
310
311 return ExtraSGPRs;
312}
313
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000314unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed,
Scott Linder1e8c2c72018-06-21 19:38:56 +0000315 bool FlatScrUsed) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000316 return getNumExtraSGPRs(STI, VCCUsed, FlatScrUsed,
317 STI->getFeatureBits().test(AMDGPU::FeatureXNACK));
Scott Linder1e8c2c72018-06-21 19:38:56 +0000318}
319
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000320unsigned getNumSGPRBlocks(const MCSubtargetInfo *STI, unsigned NumSGPRs) {
321 NumSGPRs = alignTo(std::max(1u, NumSGPRs), getSGPREncodingGranule(STI));
Scott Linder1e8c2c72018-06-21 19:38:56 +0000322 // SGPRBlocks is actual number of SGPR blocks minus 1.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000323 return NumSGPRs / getSGPREncodingGranule(STI) - 1;
Scott Linder1e8c2c72018-06-21 19:38:56 +0000324}
325
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000326unsigned getVGPRAllocGranule(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000327 return 4;
328}
329
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000330unsigned getVGPREncodingGranule(const MCSubtargetInfo *STI) {
331 return getVGPRAllocGranule(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000332}
333
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000334unsigned getTotalNumVGPRs(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000335 return 256;
336}
337
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000338unsigned getAddressableNumVGPRs(const MCSubtargetInfo *STI) {
339 return getTotalNumVGPRs(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000340}
341
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000342unsigned getMinNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000343 assert(WavesPerEU != 0);
344
Tom Stellardc5a154d2018-06-28 23:47:12 +0000345 if (WavesPerEU >= getMaxWavesPerEU())
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000346 return 0;
347 unsigned MinNumVGPRs =
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000348 alignDown(getTotalNumVGPRs(STI) / (WavesPerEU + 1),
349 getVGPRAllocGranule(STI)) + 1;
350 return std::min(MinNumVGPRs, getAddressableNumVGPRs(STI));
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000351}
352
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000353unsigned getMaxNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000354 assert(WavesPerEU != 0);
355
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000356 unsigned MaxNumVGPRs = alignDown(getTotalNumVGPRs(STI) / WavesPerEU,
357 getVGPRAllocGranule(STI));
358 unsigned AddressableNumVGPRs = getAddressableNumVGPRs(STI);
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000359 return std::min(MaxNumVGPRs, AddressableNumVGPRs);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000360}
361
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000362unsigned getNumVGPRBlocks(const MCSubtargetInfo *STI, unsigned NumVGPRs) {
363 NumVGPRs = alignTo(std::max(1u, NumVGPRs), getVGPREncodingGranule(STI));
Scott Linder1e8c2c72018-06-21 19:38:56 +0000364 // VGPRBlocks is actual number of VGPR blocks minus 1.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000365 return NumVGPRs / getVGPREncodingGranule(STI) - 1;
Scott Linder1e8c2c72018-06-21 19:38:56 +0000366}
367
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000368} // end namespace IsaInfo
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000369
Tom Stellardff7416b2015-06-26 21:58:31 +0000370void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000371 const MCSubtargetInfo *STI) {
372 IsaVersion Version = getIsaVersion(STI->getCPU());
Tom Stellardff7416b2015-06-26 21:58:31 +0000373
374 memset(&Header, 0, sizeof(Header));
375
376 Header.amd_kernel_code_version_major = 1;
Konstantin Zhuravlyov61830652018-04-09 20:47:22 +0000377 Header.amd_kernel_code_version_minor = 2;
Tom Stellardff7416b2015-06-26 21:58:31 +0000378 Header.amd_machine_kind = 1; // AMD_MACHINE_KIND_AMDGPU
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000379 Header.amd_machine_version_major = Version.Major;
380 Header.amd_machine_version_minor = Version.Minor;
381 Header.amd_machine_version_stepping = Version.Stepping;
Tom Stellardff7416b2015-06-26 21:58:31 +0000382 Header.kernel_code_entry_byte_offset = sizeof(Header);
383 // wavefront_size is specified as a power of 2: 2^6 = 64 threads.
384 Header.wavefront_size = 6;
Matt Arsenault5d910192017-01-25 20:21:57 +0000385
386 // If the code object does not support indirect functions, then the value must
387 // be 0xffffffff.
388 Header.call_convention = -1;
389
Tom Stellardff7416b2015-06-26 21:58:31 +0000390 // These alignment values are specified in powers of two, so alignment =
391 // 2^n. The minimum alignment is 2^4 = 16.
392 Header.kernarg_segment_alignment = 4;
393 Header.group_segment_alignment = 4;
394 Header.private_segment_alignment = 4;
395}
396
Scott Linder1e8c2c72018-06-21 19:38:56 +0000397amdhsa::kernel_descriptor_t getDefaultAmdhsaKernelDescriptor() {
398 amdhsa::kernel_descriptor_t KD;
399 memset(&KD, 0, sizeof(KD));
400 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1,
401 amdhsa::COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_16_64,
402 amdhsa::FLOAT_DENORM_MODE_FLUSH_NONE);
403 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1,
404 amdhsa::COMPUTE_PGM_RSRC1_ENABLE_DX10_CLAMP, 1);
405 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1,
406 amdhsa::COMPUTE_PGM_RSRC1_ENABLE_IEEE_MODE, 1);
407 AMDHSA_BITS_SET(KD.compute_pgm_rsrc2,
408 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_X, 1);
409 return KD;
410}
411
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000412bool isGroupSegment(const GlobalValue *GV) {
413 return GV->getType()->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000414}
415
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000416bool isGlobalSegment(const GlobalValue *GV) {
417 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
Tom Stellard00f2f912015-12-02 19:47:57 +0000418}
419
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000420bool isReadOnlySegment(const GlobalValue *GV) {
Matt Arsenault923712b2018-02-09 16:57:57 +0000421 return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS ||
422 GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT;
Tom Stellard00f2f912015-12-02 19:47:57 +0000423}
424
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +0000425bool shouldEmitConstantsToTextSection(const Triple &TT) {
426 return TT.getOS() != Triple::AMDHSA;
427}
428
Matt Arsenault83002722016-05-12 02:45:18 +0000429int getIntegerAttribute(const Function &F, StringRef Name, int Default) {
Marek Olsakfccabaf2016-01-13 11:45:36 +0000430 Attribute A = F.getFnAttribute(Name);
Matt Arsenault83002722016-05-12 02:45:18 +0000431 int Result = Default;
Tom Stellardac00eb52015-12-15 16:26:16 +0000432
433 if (A.isStringAttribute()) {
434 StringRef Str = A.getValueAsString();
Marek Olsakfccabaf2016-01-13 11:45:36 +0000435 if (Str.getAsInteger(0, Result)) {
Tom Stellardac00eb52015-12-15 16:26:16 +0000436 LLVMContext &Ctx = F.getContext();
Matt Arsenault83002722016-05-12 02:45:18 +0000437 Ctx.emitError("can't parse integer attribute " + Name);
Tom Stellardac00eb52015-12-15 16:26:16 +0000438 }
439 }
Matt Arsenault83002722016-05-12 02:45:18 +0000440
Marek Olsakfccabaf2016-01-13 11:45:36 +0000441 return Result;
442}
443
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000444std::pair<int, int> getIntegerPairAttribute(const Function &F,
445 StringRef Name,
446 std::pair<int, int> Default,
447 bool OnlyFirstRequired) {
448 Attribute A = F.getFnAttribute(Name);
449 if (!A.isStringAttribute())
450 return Default;
451
452 LLVMContext &Ctx = F.getContext();
453 std::pair<int, int> Ints = Default;
454 std::pair<StringRef, StringRef> Strs = A.getValueAsString().split(',');
455 if (Strs.first.trim().getAsInteger(0, Ints.first)) {
456 Ctx.emitError("can't parse first integer attribute " + Name);
457 return Default;
458 }
459 if (Strs.second.trim().getAsInteger(0, Ints.second)) {
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000460 if (!OnlyFirstRequired || !Strs.second.trim().empty()) {
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000461 Ctx.emitError("can't parse second integer attribute " + Name);
462 return Default;
463 }
464 }
465
466 return Ints;
Tom Stellard79a1fd72016-04-14 16:27:07 +0000467}
468
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000469unsigned getVmcntBitMask(const IsaVersion &Version) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000470 unsigned VmcntLo = (1 << getVmcntBitWidthLo()) - 1;
471 if (Version.Major < 9)
472 return VmcntLo;
473
474 unsigned VmcntHi = ((1 << getVmcntBitWidthHi()) - 1) << getVmcntBitWidthLo();
475 return VmcntLo | VmcntHi;
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000476}
477
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000478unsigned getExpcntBitMask(const IsaVersion &Version) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000479 return (1 << getExpcntBitWidth()) - 1;
480}
481
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000482unsigned getLgkmcntBitMask(const IsaVersion &Version) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000483 return (1 << getLgkmcntBitWidth()) - 1;
484}
485
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000486unsigned getWaitcntBitMask(const IsaVersion &Version) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000487 unsigned VmcntLo = getBitMask(getVmcntBitShiftLo(), getVmcntBitWidthLo());
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000488 unsigned Expcnt = getBitMask(getExpcntBitShift(), getExpcntBitWidth());
489 unsigned Lgkmcnt = getBitMask(getLgkmcntBitShift(), getLgkmcntBitWidth());
Matt Arsenaulte823d922017-02-18 18:29:53 +0000490 unsigned Waitcnt = VmcntLo | Expcnt | Lgkmcnt;
491 if (Version.Major < 9)
492 return Waitcnt;
493
494 unsigned VmcntHi = getBitMask(getVmcntBitShiftHi(), getVmcntBitWidthHi());
495 return Waitcnt | VmcntHi;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000496}
497
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000498unsigned decodeVmcnt(const IsaVersion &Version, unsigned Waitcnt) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000499 unsigned VmcntLo =
500 unpackBits(Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo());
501 if (Version.Major < 9)
502 return VmcntLo;
503
504 unsigned VmcntHi =
505 unpackBits(Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi());
506 VmcntHi <<= getVmcntBitWidthLo();
507 return VmcntLo | VmcntHi;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000508}
509
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000510unsigned decodeExpcnt(const IsaVersion &Version, unsigned Waitcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000511 return unpackBits(Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
512}
513
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000514unsigned decodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000515 return unpackBits(Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
516}
517
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000518void decodeWaitcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000519 unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt) {
520 Vmcnt = decodeVmcnt(Version, Waitcnt);
521 Expcnt = decodeExpcnt(Version, Waitcnt);
522 Lgkmcnt = decodeLgkmcnt(Version, Waitcnt);
523}
524
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000525unsigned encodeVmcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000526 unsigned Vmcnt) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000527 Waitcnt =
528 packBits(Vmcnt, Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo());
529 if (Version.Major < 9)
530 return Waitcnt;
531
532 Vmcnt >>= getVmcntBitWidthLo();
533 return packBits(Vmcnt, Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi());
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000534}
535
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000536unsigned encodeExpcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000537 unsigned Expcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000538 return packBits(Expcnt, Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
539}
540
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000541unsigned encodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000542 unsigned Lgkmcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000543 return packBits(Lgkmcnt, Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
544}
545
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000546unsigned encodeWaitcnt(const IsaVersion &Version,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000547 unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt) {
Konstantin Zhuravlyov31dbb032017-01-06 17:23:21 +0000548 unsigned Waitcnt = getWaitcntBitMask(Version);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000549 Waitcnt = encodeVmcnt(Version, Waitcnt, Vmcnt);
550 Waitcnt = encodeExpcnt(Version, Waitcnt, Expcnt);
551 Waitcnt = encodeLgkmcnt(Version, Waitcnt, Lgkmcnt);
552 return Waitcnt;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000553}
554
Marek Olsakfccabaf2016-01-13 11:45:36 +0000555unsigned getInitialPSInputAddr(const Function &F) {
556 return getIntegerAttribute(F, "InitialPSInputAddr", 0);
Tom Stellardac00eb52015-12-15 16:26:16 +0000557}
558
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000559bool isShader(CallingConv::ID cc) {
560 switch(cc) {
561 case CallingConv::AMDGPU_VS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000562 case CallingConv::AMDGPU_LS:
Marek Olsaka302a7362017-05-02 15:41:10 +0000563 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000564 case CallingConv::AMDGPU_ES:
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000565 case CallingConv::AMDGPU_GS:
566 case CallingConv::AMDGPU_PS:
567 case CallingConv::AMDGPU_CS:
568 return true;
569 default:
570 return false;
571 }
572}
573
574bool isCompute(CallingConv::ID cc) {
575 return !isShader(cc) || cc == CallingConv::AMDGPU_CS;
576}
577
Matt Arsenaulte622dc32017-04-11 22:29:24 +0000578bool isEntryFunctionCC(CallingConv::ID CC) {
Matt Arsenault2b1f9aa2017-05-17 21:56:25 +0000579 switch (CC) {
580 case CallingConv::AMDGPU_KERNEL:
581 case CallingConv::SPIR_KERNEL:
582 case CallingConv::AMDGPU_VS:
583 case CallingConv::AMDGPU_GS:
584 case CallingConv::AMDGPU_PS:
585 case CallingConv::AMDGPU_CS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000586 case CallingConv::AMDGPU_ES:
Matt Arsenault2b1f9aa2017-05-17 21:56:25 +0000587 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000588 case CallingConv::AMDGPU_LS:
Matt Arsenault2b1f9aa2017-05-17 21:56:25 +0000589 return true;
590 default:
591 return false;
592 }
Matt Arsenaulte622dc32017-04-11 22:29:24 +0000593}
594
Dmitry Preobrazhensky3afbd822018-01-10 14:22:19 +0000595bool hasXNACK(const MCSubtargetInfo &STI) {
596 return STI.getFeatureBits()[AMDGPU::FeatureXNACK];
597}
598
Konstantin Zhuravlyov108927b2018-11-05 22:44:19 +0000599bool hasSRAMECC(const MCSubtargetInfo &STI) {
600 return STI.getFeatureBits()[AMDGPU::FeatureSRAMECC];
601}
602
Dmitry Preobrazhenskye3271ae2018-02-05 12:45:43 +0000603bool hasMIMG_R128(const MCSubtargetInfo &STI) {
604 return STI.getFeatureBits()[AMDGPU::FeatureMIMG_R128];
605}
606
Dmitry Preobrazhensky0a1ff462018-02-05 14:18:53 +0000607bool hasPackedD16(const MCSubtargetInfo &STI) {
608 return !STI.getFeatureBits()[AMDGPU::FeatureUnpackedD16VMem];
609}
610
Tom Stellard2b65ed32015-12-21 18:44:27 +0000611bool isSI(const MCSubtargetInfo &STI) {
612 return STI.getFeatureBits()[AMDGPU::FeatureSouthernIslands];
613}
614
615bool isCI(const MCSubtargetInfo &STI) {
616 return STI.getFeatureBits()[AMDGPU::FeatureSeaIslands];
617}
618
619bool isVI(const MCSubtargetInfo &STI) {
620 return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
621}
622
Sam Koltonf7659d712017-05-23 10:08:55 +0000623bool isGFX9(const MCSubtargetInfo &STI) {
624 return STI.getFeatureBits()[AMDGPU::FeatureGFX9];
625}
626
Matt Arsenault8728c5f2017-08-07 14:58:04 +0000627bool isGCN3Encoding(const MCSubtargetInfo &STI) {
628 return STI.getFeatureBits()[AMDGPU::FeatureGCN3Encoding];
629}
630
Sam Koltonf7659d712017-05-23 10:08:55 +0000631bool isSGPR(unsigned Reg, const MCRegisterInfo* TRI) {
632 const MCRegisterClass SGPRClass = TRI->getRegClass(AMDGPU::SReg_32RegClassID);
633 const unsigned FirstSubReg = TRI->getSubReg(Reg, 1);
634 return SGPRClass.contains(FirstSubReg != 0 ? FirstSubReg : Reg) ||
635 Reg == AMDGPU::SCC;
636}
637
Dmitry Preobrazhenskydc4ac822017-06-21 14:41:34 +0000638bool isRegIntersect(unsigned Reg0, unsigned Reg1, const MCRegisterInfo* TRI) {
Dmitry Preobrazhensky00deef82017-07-18 11:14:02 +0000639 for (MCRegAliasIterator R(Reg0, TRI, true); R.isValid(); ++R) {
640 if (*R == Reg1) return true;
Dmitry Preobrazhenskydc4ac822017-06-21 14:41:34 +0000641 }
Dmitry Preobrazhenskydc4ac822017-06-21 14:41:34 +0000642 return false;
643}
644
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000645#define MAP_REG2REG \
646 using namespace AMDGPU; \
647 switch(Reg) { \
648 default: return Reg; \
649 CASE_CI_VI(FLAT_SCR) \
650 CASE_CI_VI(FLAT_SCR_LO) \
651 CASE_CI_VI(FLAT_SCR_HI) \
652 CASE_VI_GFX9(TTMP0) \
653 CASE_VI_GFX9(TTMP1) \
654 CASE_VI_GFX9(TTMP2) \
655 CASE_VI_GFX9(TTMP3) \
656 CASE_VI_GFX9(TTMP4) \
657 CASE_VI_GFX9(TTMP5) \
658 CASE_VI_GFX9(TTMP6) \
659 CASE_VI_GFX9(TTMP7) \
660 CASE_VI_GFX9(TTMP8) \
661 CASE_VI_GFX9(TTMP9) \
662 CASE_VI_GFX9(TTMP10) \
663 CASE_VI_GFX9(TTMP11) \
664 CASE_VI_GFX9(TTMP12) \
665 CASE_VI_GFX9(TTMP13) \
666 CASE_VI_GFX9(TTMP14) \
667 CASE_VI_GFX9(TTMP15) \
668 CASE_VI_GFX9(TTMP0_TTMP1) \
669 CASE_VI_GFX9(TTMP2_TTMP3) \
670 CASE_VI_GFX9(TTMP4_TTMP5) \
671 CASE_VI_GFX9(TTMP6_TTMP7) \
672 CASE_VI_GFX9(TTMP8_TTMP9) \
673 CASE_VI_GFX9(TTMP10_TTMP11) \
674 CASE_VI_GFX9(TTMP12_TTMP13) \
675 CASE_VI_GFX9(TTMP14_TTMP15) \
676 CASE_VI_GFX9(TTMP0_TTMP1_TTMP2_TTMP3) \
677 CASE_VI_GFX9(TTMP4_TTMP5_TTMP6_TTMP7) \
678 CASE_VI_GFX9(TTMP8_TTMP9_TTMP10_TTMP11) \
679 CASE_VI_GFX9(TTMP12_TTMP13_TTMP14_TTMP15) \
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000680 CASE_VI_GFX9(TTMP0_TTMP1_TTMP2_TTMP3_TTMP4_TTMP5_TTMP6_TTMP7) \
681 CASE_VI_GFX9(TTMP4_TTMP5_TTMP6_TTMP7_TTMP8_TTMP9_TTMP10_TTMP11) \
682 CASE_VI_GFX9(TTMP8_TTMP9_TTMP10_TTMP11_TTMP12_TTMP13_TTMP14_TTMP15) \
683 CASE_VI_GFX9(TTMP0_TTMP1_TTMP2_TTMP3_TTMP4_TTMP5_TTMP6_TTMP7_TTMP8_TTMP9_TTMP10_TTMP11_TTMP12_TTMP13_TTMP14_TTMP15) \
Tom Stellard2b65ed32015-12-21 18:44:27 +0000684 }
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000685
686#define CASE_CI_VI(node) \
687 assert(!isSI(STI)); \
688 case node: return isCI(STI) ? node##_ci : node##_vi;
689
690#define CASE_VI_GFX9(node) \
691 case node: return isGFX9(STI) ? node##_gfx9 : node##_vi;
692
693unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000694 if (STI.getTargetTriple().getArch() == Triple::r600)
695 return Reg;
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000696 MAP_REG2REG
Tom Stellard2b65ed32015-12-21 18:44:27 +0000697}
698
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000699#undef CASE_CI_VI
700#undef CASE_VI_GFX9
701
702#define CASE_CI_VI(node) case node##_ci: case node##_vi: return node;
703#define CASE_VI_GFX9(node) case node##_vi: case node##_gfx9: return node;
704
Dmitry Preobrazhensky03880f82017-03-03 14:31:06 +0000705unsigned mc2PseudoReg(unsigned Reg) {
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000706 MAP_REG2REG
Dmitry Preobrazhensky03880f82017-03-03 14:31:06 +0000707}
708
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000709#undef CASE_CI_VI
710#undef CASE_VI_GFX9
711#undef MAP_REG2REG
712
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000713bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000714 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000715 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000716 return OpType >= AMDGPU::OPERAND_SRC_FIRST &&
717 OpType <= AMDGPU::OPERAND_SRC_LAST;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000718}
719
720bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000721 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000722 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000723 switch (OpType) {
724 case AMDGPU::OPERAND_REG_IMM_FP32:
725 case AMDGPU::OPERAND_REG_IMM_FP64:
726 case AMDGPU::OPERAND_REG_IMM_FP16:
727 case AMDGPU::OPERAND_REG_INLINE_C_FP32:
728 case AMDGPU::OPERAND_REG_INLINE_C_FP64:
729 case AMDGPU::OPERAND_REG_INLINE_C_FP16:
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000730 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
Matt Arsenault4bd72362016-12-10 00:39:12 +0000731 return true;
732 default:
733 return false;
734 }
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000735}
736
737bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000738 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000739 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000740 return OpType >= AMDGPU::OPERAND_REG_INLINE_C_FIRST &&
741 OpType <= AMDGPU::OPERAND_REG_INLINE_C_LAST;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000742}
743
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000744// Avoid using MCRegisterClass::getSize, since that function will go away
745// (move from MC* level to Target* level). Return size in bits.
Tom Stellardb133fbb2016-10-27 23:05:31 +0000746unsigned getRegBitWidth(unsigned RCID) {
747 switch (RCID) {
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000748 case AMDGPU::SGPR_32RegClassID:
749 case AMDGPU::VGPR_32RegClassID:
750 case AMDGPU::VS_32RegClassID:
751 case AMDGPU::SReg_32RegClassID:
752 case AMDGPU::SReg_32_XM0RegClassID:
753 return 32;
754 case AMDGPU::SGPR_64RegClassID:
755 case AMDGPU::VS_64RegClassID:
756 case AMDGPU::SReg_64RegClassID:
757 case AMDGPU::VReg_64RegClassID:
758 return 64;
759 case AMDGPU::VReg_96RegClassID:
760 return 96;
761 case AMDGPU::SGPR_128RegClassID:
762 case AMDGPU::SReg_128RegClassID:
763 case AMDGPU::VReg_128RegClassID:
764 return 128;
765 case AMDGPU::SReg_256RegClassID:
766 case AMDGPU::VReg_256RegClassID:
767 return 256;
768 case AMDGPU::SReg_512RegClassID:
769 case AMDGPU::VReg_512RegClassID:
770 return 512;
771 default:
772 llvm_unreachable("Unexpected register class");
773 }
774}
775
Tom Stellardb133fbb2016-10-27 23:05:31 +0000776unsigned getRegBitWidth(const MCRegisterClass &RC) {
777 return getRegBitWidth(RC.getID());
778}
779
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000780unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
781 unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000782 assert(OpNo < Desc.NumOperands);
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000783 unsigned RCID = Desc.OpInfo[OpNo].RegClass;
784 return getRegBitWidth(MRI->getRegClass(RCID)) / 8;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000785}
786
Matt Arsenault26faed32016-12-05 22:26:17 +0000787bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi) {
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000788 if (Literal >= -16 && Literal <= 64)
789 return true;
790
Matt Arsenault26faed32016-12-05 22:26:17 +0000791 uint64_t Val = static_cast<uint64_t>(Literal);
792 return (Val == DoubleToBits(0.0)) ||
793 (Val == DoubleToBits(1.0)) ||
794 (Val == DoubleToBits(-1.0)) ||
795 (Val == DoubleToBits(0.5)) ||
796 (Val == DoubleToBits(-0.5)) ||
797 (Val == DoubleToBits(2.0)) ||
798 (Val == DoubleToBits(-2.0)) ||
799 (Val == DoubleToBits(4.0)) ||
800 (Val == DoubleToBits(-4.0)) ||
801 (Val == 0x3fc45f306dc9c882 && HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000802}
803
Matt Arsenault26faed32016-12-05 22:26:17 +0000804bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi) {
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000805 if (Literal >= -16 && Literal <= 64)
806 return true;
807
Matt Arsenault4bd72362016-12-10 00:39:12 +0000808 // The actual type of the operand does not seem to matter as long
809 // as the bits match one of the inline immediate values. For example:
810 //
811 // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal,
812 // so it is a legal inline immediate.
813 //
814 // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in
815 // floating-point, so it is a legal inline immediate.
816
Matt Arsenault26faed32016-12-05 22:26:17 +0000817 uint32_t Val = static_cast<uint32_t>(Literal);
818 return (Val == FloatToBits(0.0f)) ||
819 (Val == FloatToBits(1.0f)) ||
820 (Val == FloatToBits(-1.0f)) ||
821 (Val == FloatToBits(0.5f)) ||
822 (Val == FloatToBits(-0.5f)) ||
823 (Val == FloatToBits(2.0f)) ||
824 (Val == FloatToBits(-2.0f)) ||
825 (Val == FloatToBits(4.0f)) ||
826 (Val == FloatToBits(-4.0f)) ||
827 (Val == 0x3e22f983 && HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000828}
829
Matt Arsenault4bd72362016-12-10 00:39:12 +0000830bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi) {
Sam Kolton9dffada2017-01-17 15:26:02 +0000831 if (!HasInv2Pi)
832 return false;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000833
834 if (Literal >= -16 && Literal <= 64)
835 return true;
836
837 uint16_t Val = static_cast<uint16_t>(Literal);
838 return Val == 0x3C00 || // 1.0
839 Val == 0xBC00 || // -1.0
840 Val == 0x3800 || // 0.5
841 Val == 0xB800 || // -0.5
842 Val == 0x4000 || // 2.0
843 Val == 0xC000 || // -2.0
844 Val == 0x4400 || // 4.0
845 Val == 0xC400 || // -4.0
846 Val == 0x3118; // 1/2pi
847}
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000848
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000849bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi) {
850 assert(HasInv2Pi);
851
852 int16_t Lo16 = static_cast<int16_t>(Literal);
853 int16_t Hi16 = static_cast<int16_t>(Literal >> 16);
854 return Lo16 == Hi16 && isInlinableLiteral16(Lo16, HasInv2Pi);
855}
856
Matt Arsenault894e53d2017-07-26 20:39:42 +0000857bool isArgPassedInSGPR(const Argument *A) {
858 const Function *F = A->getParent();
859
860 // Arguments to compute shaders are never a source of divergence.
861 CallingConv::ID CC = F->getCallingConv();
862 switch (CC) {
863 case CallingConv::AMDGPU_KERNEL:
864 case CallingConv::SPIR_KERNEL:
865 return true;
866 case CallingConv::AMDGPU_VS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000867 case CallingConv::AMDGPU_LS:
Matt Arsenault894e53d2017-07-26 20:39:42 +0000868 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000869 case CallingConv::AMDGPU_ES:
Matt Arsenault894e53d2017-07-26 20:39:42 +0000870 case CallingConv::AMDGPU_GS:
871 case CallingConv::AMDGPU_PS:
872 case CallingConv::AMDGPU_CS:
873 // For non-compute shaders, SGPR inputs are marked with either inreg or byval.
874 // Everything else is in VGPRs.
875 return F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::InReg) ||
876 F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::ByVal);
877 default:
878 // TODO: Should calls support inreg for SGPR inputs?
879 return false;
880 }
881}
882
Tom Stellard08efb7e2017-01-27 18:41:14 +0000883int64_t getSMRDEncodedOffset(const MCSubtargetInfo &ST, int64_t ByteOffset) {
Matt Arsenault8728c5f2017-08-07 14:58:04 +0000884 if (isGCN3Encoding(ST))
885 return ByteOffset;
886 return ByteOffset >> 2;
Tom Stellard08efb7e2017-01-27 18:41:14 +0000887}
888
889bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset) {
890 int64_t EncodedOffset = getSMRDEncodedOffset(ST, ByteOffset);
Matt Arsenault8728c5f2017-08-07 14:58:04 +0000891 return isGCN3Encoding(ST) ?
892 isUInt<20>(EncodedOffset) : isUInt<8>(EncodedOffset);
Tom Stellard08efb7e2017-01-27 18:41:14 +0000893}
Matt Arsenaultcad7fa82017-12-13 21:07:51 +0000894
Tim Renouf4f703f52018-08-21 11:07:10 +0000895// Given Imm, split it into the values to put into the SOffset and ImmOffset
896// fields in an MUBUF instruction. Return false if it is not possible (due to a
897// hardware bug needing a workaround).
898bool splitMUBUFOffset(uint32_t Imm, uint32_t &SOffset, uint32_t &ImmOffset,
Nicolai Haehnlebc233f52018-11-07 21:53:43 +0000899 const GCNSubtarget *Subtarget) {
900 const uint32_t Align = 4;
Tim Renouf4f703f52018-08-21 11:07:10 +0000901 const uint32_t MaxImm = alignDown(4095, Align);
902 uint32_t Overflow = 0;
903
904 if (Imm > MaxImm) {
905 if (Imm <= MaxImm + 64) {
906 // Use an SOffset inline constant for 4..64
907 Overflow = Imm - MaxImm;
908 Imm = MaxImm;
909 } else {
910 // Try to keep the same value in SOffset for adjacent loads, so that
911 // the corresponding register contents can be re-used.
912 //
913 // Load values with all low-bits (except for alignment bits) set into
914 // SOffset, so that a larger range of values can be covered using
915 // s_movk_i32.
916 //
917 // Atomic operations fail to work correctly when individual address
918 // components are unaligned, even if their sum is aligned.
919 uint32_t High = (Imm + Align) & ~4095;
920 uint32_t Low = (Imm + Align) & 4095;
921 Imm = Low;
922 Overflow = High - Align;
923 }
924 }
925
926 // There is a hardware bug in SI and CI which prevents address clamping in
927 // MUBUF instructions from working correctly with SOffsets. The immediate
928 // offset is unaffected.
929 if (Overflow > 0 &&
930 Subtarget->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS)
931 return false;
932
933 ImmOffset = Imm;
934 SOffset = Overflow;
935 return true;
936}
937
Nicolai Haehnle4254d452018-04-01 17:09:14 +0000938namespace {
939
940struct SourceOfDivergence {
941 unsigned Intr;
942};
Nicolai Haehnlee741d7e2018-06-21 13:36:33 +0000943const SourceOfDivergence *lookupSourceOfDivergence(unsigned Intr);
Nicolai Haehnle4254d452018-04-01 17:09:14 +0000944
Nicolai Haehnlee741d7e2018-06-21 13:36:33 +0000945#define GET_SourcesOfDivergence_IMPL
Nicolai Haehnle4254d452018-04-01 17:09:14 +0000946#include "AMDGPUGenSearchableTables.inc"
947
948} // end anonymous namespace
949
Alexander Timofeev2e5eece2018-03-05 15:12:21 +0000950bool isIntrinsicSourceOfDivergence(unsigned IntrID) {
Nicolai Haehnlee741d7e2018-06-21 13:36:33 +0000951 return lookupSourceOfDivergence(IntrID);
Alexander Timofeev2e5eece2018-03-05 15:12:21 +0000952}
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000953} // namespace AMDGPU
954} // namespace llvm