blob: 9d567579d71b168efb9a2f9bfcd98c7fbfe05e31 [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) {
162 return STI->getFeatureBits().test(FeatureCodeObjectV3);
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000163}
164
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000165unsigned getWavefrontSize(const MCSubtargetInfo *STI) {
166 if (STI->getFeatureBits().test(FeatureWavefrontSize16))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000167 return 16;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000168 if (STI->getFeatureBits().test(FeatureWavefrontSize32))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000169 return 32;
170
171 return 64;
172}
173
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000174unsigned getLocalMemorySize(const MCSubtargetInfo *STI) {
175 if (STI->getFeatureBits().test(FeatureLocalMemorySize32768))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000176 return 32768;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000177 if (STI->getFeatureBits().test(FeatureLocalMemorySize65536))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000178 return 65536;
179
180 return 0;
181}
182
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000183unsigned getEUsPerCU(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000184 return 4;
185}
186
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000187unsigned getMaxWorkGroupsPerCU(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000188 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000189 if (!STI->getFeatureBits().test(FeatureGCN))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000190 return 8;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000191 unsigned N = getWavesPerWorkGroup(STI, FlatWorkGroupSize);
Stanislav Mekhanoshin19f98c62017-02-15 01:03:59 +0000192 if (N == 1)
193 return 40;
194 N = 40 / N;
195 return std::min(N, 16u);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000196}
197
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000198unsigned getMaxWavesPerCU(const MCSubtargetInfo *STI) {
199 return getMaxWavesPerEU() * getEUsPerCU(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000200}
201
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000202unsigned getMaxWavesPerCU(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000203 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000204 return getWavesPerWorkGroup(STI, FlatWorkGroupSize);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000205}
206
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000207unsigned getMinWavesPerEU(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000208 return 1;
209}
210
Tom Stellardc5a154d2018-06-28 23:47:12 +0000211unsigned getMaxWavesPerEU() {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000212 // FIXME: Need to take scratch memory into account.
213 return 10;
214}
215
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000216unsigned getMaxWavesPerEU(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000217 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000218 return alignTo(getMaxWavesPerCU(STI, FlatWorkGroupSize),
219 getEUsPerCU(STI)) / getEUsPerCU(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000220}
221
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000222unsigned getMinFlatWorkGroupSize(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000223 return 1;
224}
225
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000226unsigned getMaxFlatWorkGroupSize(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000227 return 2048;
228}
229
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000230unsigned getWavesPerWorkGroup(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000231 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000232 return alignTo(FlatWorkGroupSize, getWavefrontSize(STI)) /
233 getWavefrontSize(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000234}
235
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000236unsigned getSGPRAllocGranule(const MCSubtargetInfo *STI) {
237 IsaVersion Version = getIsaVersion(STI->getCPU());
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000238 if (Version.Major >= 8)
239 return 16;
240 return 8;
241}
242
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000243unsigned getSGPREncodingGranule(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000244 return 8;
245}
246
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000247unsigned getTotalNumSGPRs(const MCSubtargetInfo *STI) {
248 IsaVersion Version = getIsaVersion(STI->getCPU());
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000249 if (Version.Major >= 8)
250 return 800;
251 return 512;
252}
253
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000254unsigned getAddressableNumSGPRs(const MCSubtargetInfo *STI) {
255 if (STI->getFeatureBits().test(FeatureSGPRInitBug))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000256 return FIXED_NUM_SGPRS_FOR_INIT_BUG;
257
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000258 IsaVersion Version = getIsaVersion(STI->getCPU());
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000259 if (Version.Major >= 8)
260 return 102;
261 return 104;
262}
263
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000264unsigned getMinNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000265 assert(WavesPerEU != 0);
266
Tom Stellardc5a154d2018-06-28 23:47:12 +0000267 if (WavesPerEU >= getMaxWavesPerEU())
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000268 return 0;
Konstantin Zhuravlyovc72ece62018-05-16 20:47:48 +0000269
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000270 unsigned MinNumSGPRs = getTotalNumSGPRs(STI) / (WavesPerEU + 1);
271 if (STI->getFeatureBits().test(FeatureTrapHandler))
Konstantin Zhuravlyovc72ece62018-05-16 20:47:48 +0000272 MinNumSGPRs -= std::min(MinNumSGPRs, (unsigned)TRAP_NUM_SGPRS);
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000273 MinNumSGPRs = alignDown(MinNumSGPRs, getSGPRAllocGranule(STI)) + 1;
274 return std::min(MinNumSGPRs, getAddressableNumSGPRs(STI));
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000275}
276
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000277unsigned getMaxNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000278 bool Addressable) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000279 assert(WavesPerEU != 0);
280
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000281 IsaVersion Version = getIsaVersion(STI->getCPU());
282 unsigned AddressableNumSGPRs = getAddressableNumSGPRs(STI);
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000283 if (Version.Major >= 8 && !Addressable)
284 AddressableNumSGPRs = 112;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000285 unsigned MaxNumSGPRs = getTotalNumSGPRs(STI) / WavesPerEU;
286 if (STI->getFeatureBits().test(FeatureTrapHandler))
Konstantin Zhuravlyovc72ece62018-05-16 20:47:48 +0000287 MaxNumSGPRs -= std::min(MaxNumSGPRs, (unsigned)TRAP_NUM_SGPRS);
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000288 MaxNumSGPRs = alignDown(MaxNumSGPRs, getSGPRAllocGranule(STI));
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000289 return std::min(MaxNumSGPRs, AddressableNumSGPRs);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000290}
291
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000292unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed,
Scott Linder1e8c2c72018-06-21 19:38:56 +0000293 bool FlatScrUsed, bool XNACKUsed) {
294 unsigned ExtraSGPRs = 0;
295 if (VCCUsed)
296 ExtraSGPRs = 2;
297
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000298 IsaVersion Version = getIsaVersion(STI->getCPU());
Scott Linder1e8c2c72018-06-21 19:38:56 +0000299 if (Version.Major < 8) {
300 if (FlatScrUsed)
301 ExtraSGPRs = 4;
302 } else {
303 if (XNACKUsed)
304 ExtraSGPRs = 4;
305
306 if (FlatScrUsed)
307 ExtraSGPRs = 6;
308 }
309
310 return ExtraSGPRs;
311}
312
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000313unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed,
Scott Linder1e8c2c72018-06-21 19:38:56 +0000314 bool FlatScrUsed) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000315 return getNumExtraSGPRs(STI, VCCUsed, FlatScrUsed,
316 STI->getFeatureBits().test(AMDGPU::FeatureXNACK));
Scott Linder1e8c2c72018-06-21 19:38:56 +0000317}
318
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000319unsigned getNumSGPRBlocks(const MCSubtargetInfo *STI, unsigned NumSGPRs) {
320 NumSGPRs = alignTo(std::max(1u, NumSGPRs), getSGPREncodingGranule(STI));
Scott Linder1e8c2c72018-06-21 19:38:56 +0000321 // SGPRBlocks is actual number of SGPR blocks minus 1.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000322 return NumSGPRs / getSGPREncodingGranule(STI) - 1;
Scott Linder1e8c2c72018-06-21 19:38:56 +0000323}
324
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000325unsigned getVGPRAllocGranule(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000326 return 4;
327}
328
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000329unsigned getVGPREncodingGranule(const MCSubtargetInfo *STI) {
330 return getVGPRAllocGranule(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000331}
332
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000333unsigned getTotalNumVGPRs(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000334 return 256;
335}
336
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000337unsigned getAddressableNumVGPRs(const MCSubtargetInfo *STI) {
338 return getTotalNumVGPRs(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000339}
340
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000341unsigned getMinNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000342 assert(WavesPerEU != 0);
343
Tom Stellardc5a154d2018-06-28 23:47:12 +0000344 if (WavesPerEU >= getMaxWavesPerEU())
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000345 return 0;
346 unsigned MinNumVGPRs =
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000347 alignDown(getTotalNumVGPRs(STI) / (WavesPerEU + 1),
348 getVGPRAllocGranule(STI)) + 1;
349 return std::min(MinNumVGPRs, getAddressableNumVGPRs(STI));
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000350}
351
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000352unsigned getMaxNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000353 assert(WavesPerEU != 0);
354
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000355 unsigned MaxNumVGPRs = alignDown(getTotalNumVGPRs(STI) / WavesPerEU,
356 getVGPRAllocGranule(STI));
357 unsigned AddressableNumVGPRs = getAddressableNumVGPRs(STI);
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000358 return std::min(MaxNumVGPRs, AddressableNumVGPRs);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000359}
360
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000361unsigned getNumVGPRBlocks(const MCSubtargetInfo *STI, unsigned NumVGPRs) {
362 NumVGPRs = alignTo(std::max(1u, NumVGPRs), getVGPREncodingGranule(STI));
Scott Linder1e8c2c72018-06-21 19:38:56 +0000363 // VGPRBlocks is actual number of VGPR blocks minus 1.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000364 return NumVGPRs / getVGPREncodingGranule(STI) - 1;
Scott Linder1e8c2c72018-06-21 19:38:56 +0000365}
366
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000367} // end namespace IsaInfo
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000368
Tom Stellardff7416b2015-06-26 21:58:31 +0000369void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000370 const MCSubtargetInfo *STI) {
371 IsaVersion Version = getIsaVersion(STI->getCPU());
Tom Stellardff7416b2015-06-26 21:58:31 +0000372
373 memset(&Header, 0, sizeof(Header));
374
375 Header.amd_kernel_code_version_major = 1;
Konstantin Zhuravlyov61830652018-04-09 20:47:22 +0000376 Header.amd_kernel_code_version_minor = 2;
Tom Stellardff7416b2015-06-26 21:58:31 +0000377 Header.amd_machine_kind = 1; // AMD_MACHINE_KIND_AMDGPU
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000378 Header.amd_machine_version_major = Version.Major;
379 Header.amd_machine_version_minor = Version.Minor;
380 Header.amd_machine_version_stepping = Version.Stepping;
Tom Stellardff7416b2015-06-26 21:58:31 +0000381 Header.kernel_code_entry_byte_offset = sizeof(Header);
382 // wavefront_size is specified as a power of 2: 2^6 = 64 threads.
383 Header.wavefront_size = 6;
Matt Arsenault5d910192017-01-25 20:21:57 +0000384
385 // If the code object does not support indirect functions, then the value must
386 // be 0xffffffff.
387 Header.call_convention = -1;
388
Tom Stellardff7416b2015-06-26 21:58:31 +0000389 // These alignment values are specified in powers of two, so alignment =
390 // 2^n. The minimum alignment is 2^4 = 16.
391 Header.kernarg_segment_alignment = 4;
392 Header.group_segment_alignment = 4;
393 Header.private_segment_alignment = 4;
394}
395
Scott Linder1e8c2c72018-06-21 19:38:56 +0000396amdhsa::kernel_descriptor_t getDefaultAmdhsaKernelDescriptor() {
397 amdhsa::kernel_descriptor_t KD;
398 memset(&KD, 0, sizeof(KD));
399 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1,
400 amdhsa::COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_16_64,
401 amdhsa::FLOAT_DENORM_MODE_FLUSH_NONE);
402 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1,
403 amdhsa::COMPUTE_PGM_RSRC1_ENABLE_DX10_CLAMP, 1);
404 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1,
405 amdhsa::COMPUTE_PGM_RSRC1_ENABLE_IEEE_MODE, 1);
406 AMDHSA_BITS_SET(KD.compute_pgm_rsrc2,
407 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_X, 1);
408 return KD;
409}
410
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000411bool isGroupSegment(const GlobalValue *GV) {
412 return GV->getType()->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000413}
414
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000415bool isGlobalSegment(const GlobalValue *GV) {
416 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
Tom Stellard00f2f912015-12-02 19:47:57 +0000417}
418
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000419bool isReadOnlySegment(const GlobalValue *GV) {
Matt Arsenault923712b2018-02-09 16:57:57 +0000420 return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS ||
421 GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT;
Tom Stellard00f2f912015-12-02 19:47:57 +0000422}
423
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +0000424bool shouldEmitConstantsToTextSection(const Triple &TT) {
425 return TT.getOS() != Triple::AMDHSA;
426}
427
Matt Arsenault83002722016-05-12 02:45:18 +0000428int getIntegerAttribute(const Function &F, StringRef Name, int Default) {
Marek Olsakfccabaf2016-01-13 11:45:36 +0000429 Attribute A = F.getFnAttribute(Name);
Matt Arsenault83002722016-05-12 02:45:18 +0000430 int Result = Default;
Tom Stellardac00eb52015-12-15 16:26:16 +0000431
432 if (A.isStringAttribute()) {
433 StringRef Str = A.getValueAsString();
Marek Olsakfccabaf2016-01-13 11:45:36 +0000434 if (Str.getAsInteger(0, Result)) {
Tom Stellardac00eb52015-12-15 16:26:16 +0000435 LLVMContext &Ctx = F.getContext();
Matt Arsenault83002722016-05-12 02:45:18 +0000436 Ctx.emitError("can't parse integer attribute " + Name);
Tom Stellardac00eb52015-12-15 16:26:16 +0000437 }
438 }
Matt Arsenault83002722016-05-12 02:45:18 +0000439
Marek Olsakfccabaf2016-01-13 11:45:36 +0000440 return Result;
441}
442
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000443std::pair<int, int> getIntegerPairAttribute(const Function &F,
444 StringRef Name,
445 std::pair<int, int> Default,
446 bool OnlyFirstRequired) {
447 Attribute A = F.getFnAttribute(Name);
448 if (!A.isStringAttribute())
449 return Default;
450
451 LLVMContext &Ctx = F.getContext();
452 std::pair<int, int> Ints = Default;
453 std::pair<StringRef, StringRef> Strs = A.getValueAsString().split(',');
454 if (Strs.first.trim().getAsInteger(0, Ints.first)) {
455 Ctx.emitError("can't parse first integer attribute " + Name);
456 return Default;
457 }
458 if (Strs.second.trim().getAsInteger(0, Ints.second)) {
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000459 if (!OnlyFirstRequired || !Strs.second.trim().empty()) {
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000460 Ctx.emitError("can't parse second integer attribute " + Name);
461 return Default;
462 }
463 }
464
465 return Ints;
Tom Stellard79a1fd72016-04-14 16:27:07 +0000466}
467
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000468unsigned getVmcntBitMask(const IsaVersion &Version) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000469 unsigned VmcntLo = (1 << getVmcntBitWidthLo()) - 1;
470 if (Version.Major < 9)
471 return VmcntLo;
472
473 unsigned VmcntHi = ((1 << getVmcntBitWidthHi()) - 1) << getVmcntBitWidthLo();
474 return VmcntLo | VmcntHi;
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000475}
476
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000477unsigned getExpcntBitMask(const IsaVersion &Version) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000478 return (1 << getExpcntBitWidth()) - 1;
479}
480
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000481unsigned getLgkmcntBitMask(const IsaVersion &Version) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000482 return (1 << getLgkmcntBitWidth()) - 1;
483}
484
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000485unsigned getWaitcntBitMask(const IsaVersion &Version) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000486 unsigned VmcntLo = getBitMask(getVmcntBitShiftLo(), getVmcntBitWidthLo());
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000487 unsigned Expcnt = getBitMask(getExpcntBitShift(), getExpcntBitWidth());
488 unsigned Lgkmcnt = getBitMask(getLgkmcntBitShift(), getLgkmcntBitWidth());
Matt Arsenaulte823d922017-02-18 18:29:53 +0000489 unsigned Waitcnt = VmcntLo | Expcnt | Lgkmcnt;
490 if (Version.Major < 9)
491 return Waitcnt;
492
493 unsigned VmcntHi = getBitMask(getVmcntBitShiftHi(), getVmcntBitWidthHi());
494 return Waitcnt | VmcntHi;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000495}
496
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000497unsigned decodeVmcnt(const IsaVersion &Version, unsigned Waitcnt) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000498 unsigned VmcntLo =
499 unpackBits(Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo());
500 if (Version.Major < 9)
501 return VmcntLo;
502
503 unsigned VmcntHi =
504 unpackBits(Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi());
505 VmcntHi <<= getVmcntBitWidthLo();
506 return VmcntLo | VmcntHi;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000507}
508
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000509unsigned decodeExpcnt(const IsaVersion &Version, unsigned Waitcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000510 return unpackBits(Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
511}
512
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000513unsigned decodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000514 return unpackBits(Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
515}
516
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000517void decodeWaitcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000518 unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt) {
519 Vmcnt = decodeVmcnt(Version, Waitcnt);
520 Expcnt = decodeExpcnt(Version, Waitcnt);
521 Lgkmcnt = decodeLgkmcnt(Version, Waitcnt);
522}
523
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000524unsigned encodeVmcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000525 unsigned Vmcnt) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000526 Waitcnt =
527 packBits(Vmcnt, Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo());
528 if (Version.Major < 9)
529 return Waitcnt;
530
531 Vmcnt >>= getVmcntBitWidthLo();
532 return packBits(Vmcnt, Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi());
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000533}
534
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000535unsigned encodeExpcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000536 unsigned Expcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000537 return packBits(Expcnt, Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
538}
539
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000540unsigned encodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000541 unsigned Lgkmcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000542 return packBits(Lgkmcnt, Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
543}
544
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000545unsigned encodeWaitcnt(const IsaVersion &Version,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000546 unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt) {
Konstantin Zhuravlyov31dbb032017-01-06 17:23:21 +0000547 unsigned Waitcnt = getWaitcntBitMask(Version);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000548 Waitcnt = encodeVmcnt(Version, Waitcnt, Vmcnt);
549 Waitcnt = encodeExpcnt(Version, Waitcnt, Expcnt);
550 Waitcnt = encodeLgkmcnt(Version, Waitcnt, Lgkmcnt);
551 return Waitcnt;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000552}
553
Marek Olsakfccabaf2016-01-13 11:45:36 +0000554unsigned getInitialPSInputAddr(const Function &F) {
555 return getIntegerAttribute(F, "InitialPSInputAddr", 0);
Tom Stellardac00eb52015-12-15 16:26:16 +0000556}
557
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000558bool isShader(CallingConv::ID cc) {
559 switch(cc) {
560 case CallingConv::AMDGPU_VS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000561 case CallingConv::AMDGPU_LS:
Marek Olsaka302a7362017-05-02 15:41:10 +0000562 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000563 case CallingConv::AMDGPU_ES:
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000564 case CallingConv::AMDGPU_GS:
565 case CallingConv::AMDGPU_PS:
566 case CallingConv::AMDGPU_CS:
567 return true;
568 default:
569 return false;
570 }
571}
572
573bool isCompute(CallingConv::ID cc) {
574 return !isShader(cc) || cc == CallingConv::AMDGPU_CS;
575}
576
Matt Arsenaulte622dc32017-04-11 22:29:24 +0000577bool isEntryFunctionCC(CallingConv::ID CC) {
Matt Arsenault2b1f9aa2017-05-17 21:56:25 +0000578 switch (CC) {
579 case CallingConv::AMDGPU_KERNEL:
580 case CallingConv::SPIR_KERNEL:
581 case CallingConv::AMDGPU_VS:
582 case CallingConv::AMDGPU_GS:
583 case CallingConv::AMDGPU_PS:
584 case CallingConv::AMDGPU_CS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000585 case CallingConv::AMDGPU_ES:
Matt Arsenault2b1f9aa2017-05-17 21:56:25 +0000586 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000587 case CallingConv::AMDGPU_LS:
Matt Arsenault2b1f9aa2017-05-17 21:56:25 +0000588 return true;
589 default:
590 return false;
591 }
Matt Arsenaulte622dc32017-04-11 22:29:24 +0000592}
593
Dmitry Preobrazhensky3afbd822018-01-10 14:22:19 +0000594bool hasXNACK(const MCSubtargetInfo &STI) {
595 return STI.getFeatureBits()[AMDGPU::FeatureXNACK];
596}
597
Konstantin Zhuravlyov108927b2018-11-05 22:44:19 +0000598bool hasSRAMECC(const MCSubtargetInfo &STI) {
599 return STI.getFeatureBits()[AMDGPU::FeatureSRAMECC];
600}
601
Dmitry Preobrazhenskye3271ae2018-02-05 12:45:43 +0000602bool hasMIMG_R128(const MCSubtargetInfo &STI) {
603 return STI.getFeatureBits()[AMDGPU::FeatureMIMG_R128];
604}
605
Dmitry Preobrazhensky0a1ff462018-02-05 14:18:53 +0000606bool hasPackedD16(const MCSubtargetInfo &STI) {
607 return !STI.getFeatureBits()[AMDGPU::FeatureUnpackedD16VMem];
608}
609
Tom Stellard2b65ed32015-12-21 18:44:27 +0000610bool isSI(const MCSubtargetInfo &STI) {
611 return STI.getFeatureBits()[AMDGPU::FeatureSouthernIslands];
612}
613
614bool isCI(const MCSubtargetInfo &STI) {
615 return STI.getFeatureBits()[AMDGPU::FeatureSeaIslands];
616}
617
618bool isVI(const MCSubtargetInfo &STI) {
619 return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
620}
621
Sam Koltonf7659d712017-05-23 10:08:55 +0000622bool isGFX9(const MCSubtargetInfo &STI) {
623 return STI.getFeatureBits()[AMDGPU::FeatureGFX9];
624}
625
Matt Arsenault8728c5f2017-08-07 14:58:04 +0000626bool isGCN3Encoding(const MCSubtargetInfo &STI) {
627 return STI.getFeatureBits()[AMDGPU::FeatureGCN3Encoding];
628}
629
Sam Koltonf7659d712017-05-23 10:08:55 +0000630bool isSGPR(unsigned Reg, const MCRegisterInfo* TRI) {
631 const MCRegisterClass SGPRClass = TRI->getRegClass(AMDGPU::SReg_32RegClassID);
632 const unsigned FirstSubReg = TRI->getSubReg(Reg, 1);
633 return SGPRClass.contains(FirstSubReg != 0 ? FirstSubReg : Reg) ||
634 Reg == AMDGPU::SCC;
635}
636
Dmitry Preobrazhenskydc4ac822017-06-21 14:41:34 +0000637bool isRegIntersect(unsigned Reg0, unsigned Reg1, const MCRegisterInfo* TRI) {
Dmitry Preobrazhensky00deef82017-07-18 11:14:02 +0000638 for (MCRegAliasIterator R(Reg0, TRI, true); R.isValid(); ++R) {
639 if (*R == Reg1) return true;
Dmitry Preobrazhenskydc4ac822017-06-21 14:41:34 +0000640 }
Dmitry Preobrazhenskydc4ac822017-06-21 14:41:34 +0000641 return false;
642}
643
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000644#define MAP_REG2REG \
645 using namespace AMDGPU; \
646 switch(Reg) { \
647 default: return Reg; \
648 CASE_CI_VI(FLAT_SCR) \
649 CASE_CI_VI(FLAT_SCR_LO) \
650 CASE_CI_VI(FLAT_SCR_HI) \
651 CASE_VI_GFX9(TTMP0) \
652 CASE_VI_GFX9(TTMP1) \
653 CASE_VI_GFX9(TTMP2) \
654 CASE_VI_GFX9(TTMP3) \
655 CASE_VI_GFX9(TTMP4) \
656 CASE_VI_GFX9(TTMP5) \
657 CASE_VI_GFX9(TTMP6) \
658 CASE_VI_GFX9(TTMP7) \
659 CASE_VI_GFX9(TTMP8) \
660 CASE_VI_GFX9(TTMP9) \
661 CASE_VI_GFX9(TTMP10) \
662 CASE_VI_GFX9(TTMP11) \
663 CASE_VI_GFX9(TTMP12) \
664 CASE_VI_GFX9(TTMP13) \
665 CASE_VI_GFX9(TTMP14) \
666 CASE_VI_GFX9(TTMP15) \
667 CASE_VI_GFX9(TTMP0_TTMP1) \
668 CASE_VI_GFX9(TTMP2_TTMP3) \
669 CASE_VI_GFX9(TTMP4_TTMP5) \
670 CASE_VI_GFX9(TTMP6_TTMP7) \
671 CASE_VI_GFX9(TTMP8_TTMP9) \
672 CASE_VI_GFX9(TTMP10_TTMP11) \
673 CASE_VI_GFX9(TTMP12_TTMP13) \
674 CASE_VI_GFX9(TTMP14_TTMP15) \
675 CASE_VI_GFX9(TTMP0_TTMP1_TTMP2_TTMP3) \
676 CASE_VI_GFX9(TTMP4_TTMP5_TTMP6_TTMP7) \
677 CASE_VI_GFX9(TTMP8_TTMP9_TTMP10_TTMP11) \
678 CASE_VI_GFX9(TTMP12_TTMP13_TTMP14_TTMP15) \
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000679 CASE_VI_GFX9(TTMP0_TTMP1_TTMP2_TTMP3_TTMP4_TTMP5_TTMP6_TTMP7) \
680 CASE_VI_GFX9(TTMP4_TTMP5_TTMP6_TTMP7_TTMP8_TTMP9_TTMP10_TTMP11) \
681 CASE_VI_GFX9(TTMP8_TTMP9_TTMP10_TTMP11_TTMP12_TTMP13_TTMP14_TTMP15) \
682 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 +0000683 }
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000684
685#define CASE_CI_VI(node) \
686 assert(!isSI(STI)); \
687 case node: return isCI(STI) ? node##_ci : node##_vi;
688
689#define CASE_VI_GFX9(node) \
690 case node: return isGFX9(STI) ? node##_gfx9 : node##_vi;
691
692unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000693 if (STI.getTargetTriple().getArch() == Triple::r600)
694 return Reg;
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000695 MAP_REG2REG
Tom Stellard2b65ed32015-12-21 18:44:27 +0000696}
697
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000698#undef CASE_CI_VI
699#undef CASE_VI_GFX9
700
701#define CASE_CI_VI(node) case node##_ci: case node##_vi: return node;
702#define CASE_VI_GFX9(node) case node##_vi: case node##_gfx9: return node;
703
Dmitry Preobrazhensky03880f82017-03-03 14:31:06 +0000704unsigned mc2PseudoReg(unsigned Reg) {
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000705 MAP_REG2REG
Dmitry Preobrazhensky03880f82017-03-03 14:31:06 +0000706}
707
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000708#undef CASE_CI_VI
709#undef CASE_VI_GFX9
710#undef MAP_REG2REG
711
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000712bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000713 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000714 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000715 return OpType >= AMDGPU::OPERAND_SRC_FIRST &&
716 OpType <= AMDGPU::OPERAND_SRC_LAST;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000717}
718
719bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000720 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000721 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000722 switch (OpType) {
723 case AMDGPU::OPERAND_REG_IMM_FP32:
724 case AMDGPU::OPERAND_REG_IMM_FP64:
725 case AMDGPU::OPERAND_REG_IMM_FP16:
726 case AMDGPU::OPERAND_REG_INLINE_C_FP32:
727 case AMDGPU::OPERAND_REG_INLINE_C_FP64:
728 case AMDGPU::OPERAND_REG_INLINE_C_FP16:
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000729 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
Matt Arsenault4bd72362016-12-10 00:39:12 +0000730 return true;
731 default:
732 return false;
733 }
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000734}
735
736bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000737 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000738 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000739 return OpType >= AMDGPU::OPERAND_REG_INLINE_C_FIRST &&
740 OpType <= AMDGPU::OPERAND_REG_INLINE_C_LAST;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000741}
742
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000743// Avoid using MCRegisterClass::getSize, since that function will go away
744// (move from MC* level to Target* level). Return size in bits.
Tom Stellardb133fbb2016-10-27 23:05:31 +0000745unsigned getRegBitWidth(unsigned RCID) {
746 switch (RCID) {
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000747 case AMDGPU::SGPR_32RegClassID:
748 case AMDGPU::VGPR_32RegClassID:
749 case AMDGPU::VS_32RegClassID:
750 case AMDGPU::SReg_32RegClassID:
751 case AMDGPU::SReg_32_XM0RegClassID:
752 return 32;
753 case AMDGPU::SGPR_64RegClassID:
754 case AMDGPU::VS_64RegClassID:
755 case AMDGPU::SReg_64RegClassID:
756 case AMDGPU::VReg_64RegClassID:
757 return 64;
758 case AMDGPU::VReg_96RegClassID:
759 return 96;
760 case AMDGPU::SGPR_128RegClassID:
761 case AMDGPU::SReg_128RegClassID:
762 case AMDGPU::VReg_128RegClassID:
763 return 128;
764 case AMDGPU::SReg_256RegClassID:
765 case AMDGPU::VReg_256RegClassID:
766 return 256;
767 case AMDGPU::SReg_512RegClassID:
768 case AMDGPU::VReg_512RegClassID:
769 return 512;
770 default:
771 llvm_unreachable("Unexpected register class");
772 }
773}
774
Tom Stellardb133fbb2016-10-27 23:05:31 +0000775unsigned getRegBitWidth(const MCRegisterClass &RC) {
776 return getRegBitWidth(RC.getID());
777}
778
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000779unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
780 unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000781 assert(OpNo < Desc.NumOperands);
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000782 unsigned RCID = Desc.OpInfo[OpNo].RegClass;
783 return getRegBitWidth(MRI->getRegClass(RCID)) / 8;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000784}
785
Matt Arsenault26faed32016-12-05 22:26:17 +0000786bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi) {
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000787 if (Literal >= -16 && Literal <= 64)
788 return true;
789
Matt Arsenault26faed32016-12-05 22:26:17 +0000790 uint64_t Val = static_cast<uint64_t>(Literal);
791 return (Val == DoubleToBits(0.0)) ||
792 (Val == DoubleToBits(1.0)) ||
793 (Val == DoubleToBits(-1.0)) ||
794 (Val == DoubleToBits(0.5)) ||
795 (Val == DoubleToBits(-0.5)) ||
796 (Val == DoubleToBits(2.0)) ||
797 (Val == DoubleToBits(-2.0)) ||
798 (Val == DoubleToBits(4.0)) ||
799 (Val == DoubleToBits(-4.0)) ||
800 (Val == 0x3fc45f306dc9c882 && HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000801}
802
Matt Arsenault26faed32016-12-05 22:26:17 +0000803bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi) {
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000804 if (Literal >= -16 && Literal <= 64)
805 return true;
806
Matt Arsenault4bd72362016-12-10 00:39:12 +0000807 // The actual type of the operand does not seem to matter as long
808 // as the bits match one of the inline immediate values. For example:
809 //
810 // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal,
811 // so it is a legal inline immediate.
812 //
813 // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in
814 // floating-point, so it is a legal inline immediate.
815
Matt Arsenault26faed32016-12-05 22:26:17 +0000816 uint32_t Val = static_cast<uint32_t>(Literal);
817 return (Val == FloatToBits(0.0f)) ||
818 (Val == FloatToBits(1.0f)) ||
819 (Val == FloatToBits(-1.0f)) ||
820 (Val == FloatToBits(0.5f)) ||
821 (Val == FloatToBits(-0.5f)) ||
822 (Val == FloatToBits(2.0f)) ||
823 (Val == FloatToBits(-2.0f)) ||
824 (Val == FloatToBits(4.0f)) ||
825 (Val == FloatToBits(-4.0f)) ||
826 (Val == 0x3e22f983 && HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000827}
828
Matt Arsenault4bd72362016-12-10 00:39:12 +0000829bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi) {
Sam Kolton9dffada2017-01-17 15:26:02 +0000830 if (!HasInv2Pi)
831 return false;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000832
833 if (Literal >= -16 && Literal <= 64)
834 return true;
835
836 uint16_t Val = static_cast<uint16_t>(Literal);
837 return Val == 0x3C00 || // 1.0
838 Val == 0xBC00 || // -1.0
839 Val == 0x3800 || // 0.5
840 Val == 0xB800 || // -0.5
841 Val == 0x4000 || // 2.0
842 Val == 0xC000 || // -2.0
843 Val == 0x4400 || // 4.0
844 Val == 0xC400 || // -4.0
845 Val == 0x3118; // 1/2pi
846}
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000847
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000848bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi) {
849 assert(HasInv2Pi);
850
851 int16_t Lo16 = static_cast<int16_t>(Literal);
852 int16_t Hi16 = static_cast<int16_t>(Literal >> 16);
853 return Lo16 == Hi16 && isInlinableLiteral16(Lo16, HasInv2Pi);
854}
855
Matt Arsenault894e53d2017-07-26 20:39:42 +0000856bool isArgPassedInSGPR(const Argument *A) {
857 const Function *F = A->getParent();
858
859 // Arguments to compute shaders are never a source of divergence.
860 CallingConv::ID CC = F->getCallingConv();
861 switch (CC) {
862 case CallingConv::AMDGPU_KERNEL:
863 case CallingConv::SPIR_KERNEL:
864 return true;
865 case CallingConv::AMDGPU_VS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000866 case CallingConv::AMDGPU_LS:
Matt Arsenault894e53d2017-07-26 20:39:42 +0000867 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000868 case CallingConv::AMDGPU_ES:
Matt Arsenault894e53d2017-07-26 20:39:42 +0000869 case CallingConv::AMDGPU_GS:
870 case CallingConv::AMDGPU_PS:
871 case CallingConv::AMDGPU_CS:
872 // For non-compute shaders, SGPR inputs are marked with either inreg or byval.
873 // Everything else is in VGPRs.
874 return F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::InReg) ||
875 F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::ByVal);
876 default:
877 // TODO: Should calls support inreg for SGPR inputs?
878 return false;
879 }
880}
881
Tom Stellard08efb7e2017-01-27 18:41:14 +0000882int64_t getSMRDEncodedOffset(const MCSubtargetInfo &ST, int64_t ByteOffset) {
Matt Arsenault8728c5f2017-08-07 14:58:04 +0000883 if (isGCN3Encoding(ST))
884 return ByteOffset;
885 return ByteOffset >> 2;
Tom Stellard08efb7e2017-01-27 18:41:14 +0000886}
887
888bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset) {
889 int64_t EncodedOffset = getSMRDEncodedOffset(ST, ByteOffset);
Matt Arsenault8728c5f2017-08-07 14:58:04 +0000890 return isGCN3Encoding(ST) ?
891 isUInt<20>(EncodedOffset) : isUInt<8>(EncodedOffset);
Tom Stellard08efb7e2017-01-27 18:41:14 +0000892}
Matt Arsenaultcad7fa82017-12-13 21:07:51 +0000893
Tim Renouf4f703f52018-08-21 11:07:10 +0000894// Given Imm, split it into the values to put into the SOffset and ImmOffset
895// fields in an MUBUF instruction. Return false if it is not possible (due to a
896// hardware bug needing a workaround).
Nicolai Haehnlec4a2ff02018-10-17 15:37:30 +0000897//
898// The required alignment ensures that individual address components remain
899// aligned if they are aligned to begin with. It also ensures that additional
900// offsets within the given alignment can be added to the resulting ImmOffset.
Tim Renouf4f703f52018-08-21 11:07:10 +0000901bool splitMUBUFOffset(uint32_t Imm, uint32_t &SOffset, uint32_t &ImmOffset,
Nicolai Haehnlec4a2ff02018-10-17 15:37:30 +0000902 const GCNSubtarget *Subtarget, uint32_t Align) {
Tim Renouf4f703f52018-08-21 11:07:10 +0000903 const uint32_t MaxImm = alignDown(4095, Align);
904 uint32_t Overflow = 0;
905
906 if (Imm > MaxImm) {
907 if (Imm <= MaxImm + 64) {
908 // Use an SOffset inline constant for 4..64
909 Overflow = Imm - MaxImm;
910 Imm = MaxImm;
911 } else {
912 // Try to keep the same value in SOffset for adjacent loads, so that
913 // the corresponding register contents can be re-used.
914 //
915 // Load values with all low-bits (except for alignment bits) set into
916 // SOffset, so that a larger range of values can be covered using
917 // s_movk_i32.
918 //
919 // Atomic operations fail to work correctly when individual address
920 // components are unaligned, even if their sum is aligned.
921 uint32_t High = (Imm + Align) & ~4095;
922 uint32_t Low = (Imm + Align) & 4095;
923 Imm = Low;
924 Overflow = High - Align;
925 }
926 }
927
928 // There is a hardware bug in SI and CI which prevents address clamping in
929 // MUBUF instructions from working correctly with SOffsets. The immediate
930 // offset is unaffected.
931 if (Overflow > 0 &&
932 Subtarget->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS)
933 return false;
934
935 ImmOffset = Imm;
936 SOffset = Overflow;
937 return true;
938}
939
Nicolai Haehnle4254d452018-04-01 17:09:14 +0000940namespace {
941
942struct SourceOfDivergence {
943 unsigned Intr;
944};
Nicolai Haehnlee741d7e2018-06-21 13:36:33 +0000945const SourceOfDivergence *lookupSourceOfDivergence(unsigned Intr);
Nicolai Haehnle4254d452018-04-01 17:09:14 +0000946
Nicolai Haehnlee741d7e2018-06-21 13:36:33 +0000947#define GET_SourcesOfDivergence_IMPL
Nicolai Haehnle4254d452018-04-01 17:09:14 +0000948#include "AMDGPUGenSearchableTables.inc"
949
950} // end anonymous namespace
951
Alexander Timofeev2e5eece2018-03-05 15:12:21 +0000952bool isIntrinsicSourceOfDivergence(unsigned IntrID) {
Nicolai Haehnlee741d7e2018-06-21 13:36:33 +0000953 return lookupSourceOfDivergence(IntrID);
Alexander Timofeev2e5eece2018-03-05 15:12:21 +0000954}
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000955} // namespace AMDGPU
956} // namespace llvm