blob: 88245b01683a5b054373cfc82b7da1c836f14b7e [file] [log] [blame]
Tom Stellard8b1e0212013-07-27 00:01:07 +00001//===-- AMDGPUTargetTransformInfo.cpp - AMDGPU specific TTI pass ---------===//
2//
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//===----------------------------------------------------------------------===//
9//
10// \file
11// This file implements a TargetTransformInfo analysis pass specific to the
12// AMDGPU target machine. It uses the target's detailed information to provide
13// more precise answers to certain TTI queries, while letting the target
14// independent and default TTI implementations handle the rest.
15//
16//===----------------------------------------------------------------------===//
17
Chandler Carruth93dcdc42015-01-31 11:17:59 +000018#include "AMDGPUTargetTransformInfo.h"
Tom Stellard8cce9bd2014-01-23 18:49:28 +000019#include "llvm/Analysis/LoopInfo.h"
Tom Stellard8b1e0212013-07-27 00:01:07 +000020#include "llvm/Analysis/TargetTransformInfo.h"
Tom Stellard8cce9bd2014-01-23 18:49:28 +000021#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth705b1852015-01-31 03:43:40 +000022#include "llvm/CodeGen/BasicTTIImpl.h"
Tom Stellardbc4497b2016-02-12 23:45:29 +000023#include "llvm/IR/Intrinsics.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000024#include "llvm/IR/Module.h"
Tom Stellard8b1e0212013-07-27 00:01:07 +000025#include "llvm/Support/Debug.h"
Tom Stellard8b1e0212013-07-27 00:01:07 +000026#include "llvm/Target/CostTable.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000027#include "llvm/Target/TargetLowering.h"
Tom Stellard8b1e0212013-07-27 00:01:07 +000028using namespace llvm;
29
Chandler Carruth84e68b22014-04-22 02:41:26 +000030#define DEBUG_TYPE "AMDGPUtti"
31
Stanislav Mekhanoshinf29602d2017-02-03 02:20:05 +000032static cl::opt<unsigned> UnrollThresholdPrivate(
33 "amdgpu-unroll-threshold-private",
34 cl::desc("Unroll threshold for AMDGPU if private memory used in a loop"),
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +000035 cl::init(2500), cl::Hidden);
Matt Arsenault96518132016-03-25 01:00:32 +000036
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +000037static cl::opt<unsigned> UnrollThresholdLocal(
38 "amdgpu-unroll-threshold-local",
39 cl::desc("Unroll threshold for AMDGPU if local memory used in a loop"),
40 cl::init(1000), cl::Hidden);
41
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +000042static cl::opt<unsigned> UnrollThresholdIf(
43 "amdgpu-unroll-threshold-if",
44 cl::desc("Unroll threshold increment for AMDGPU for each if statement inside loop"),
45 cl::init(150), cl::Hidden);
46
47static bool dependsOnLocalPhi(const Loop *L, const Value *Cond,
48 unsigned Depth = 0) {
49 const Instruction *I = dyn_cast<Instruction>(Cond);
50 if (!I)
51 return false;
52
53 for (const Value *V : I->operand_values()) {
54 if (!L->contains(I))
55 continue;
56 if (const PHINode *PHI = dyn_cast<PHINode>(V)) {
57 if (none_of(L->getSubLoops(), [PHI](const Loop* SubLoop) {
58 return SubLoop->contains(PHI); }))
59 return true;
60 } else if (Depth < 10 && dependsOnLocalPhi(L, V, Depth+1))
61 return true;
62 }
63 return false;
64}
65
Chandler Carruthab5cb362015-02-01 14:31:23 +000066void AMDGPUTTIImpl::getUnrollingPreferences(Loop *L,
Chandler Carruth705b1852015-01-31 03:43:40 +000067 TTI::UnrollingPreferences &UP) {
Matt Arsenaultc8244582014-07-25 23:02:42 +000068 UP.Threshold = 300; // Twice the default.
Tom Stellardeea3f702015-02-05 15:32:18 +000069 UP.MaxCount = UINT_MAX;
Matt Arsenaultc8244582014-07-25 23:02:42 +000070 UP.Partial = true;
71
72 // TODO: Do we want runtime unrolling?
73
Stanislav Mekhanoshinf29602d2017-02-03 02:20:05 +000074 // Maximum alloca size than can fit registers. Reserve 16 registers.
75 const unsigned MaxAlloca = (256 - 16) * 4;
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +000076 unsigned ThresholdPrivate = UnrollThresholdPrivate;
77 unsigned ThresholdLocal = UnrollThresholdLocal;
78 unsigned MaxBoost = std::max(ThresholdPrivate, ThresholdLocal);
79 AMDGPUAS ASST = ST->getAMDGPUAS();
Matt Arsenaultac6e39c2014-07-17 06:19:06 +000080 for (const BasicBlock *BB : L->getBlocks()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +000081 const DataLayout &DL = BB->getModule()->getDataLayout();
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +000082 unsigned LocalGEPsSeen = 0;
83
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +000084 if (any_of(L->getSubLoops(), [BB](const Loop* SubLoop) {
85 return SubLoop->contains(BB); }))
86 continue; // Block belongs to an inner loop.
87
Matt Arsenaultac6e39c2014-07-17 06:19:06 +000088 for (const Instruction &I : *BB) {
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +000089
90 // Unroll a loop which contains an "if" statement whose condition
91 // defined by a PHI belonging to the loop. This may help to eliminate
92 // if region and potentially even PHI itself, saving on both divergence
93 // and registers used for the PHI.
94 // Add a small bonus for each of such "if" statements.
95 if (const BranchInst *Br = dyn_cast<BranchInst>(&I)) {
96 if (UP.Threshold < MaxBoost && Br->isConditional()) {
97 if (L->isLoopExiting(Br->getSuccessor(0)) ||
98 L->isLoopExiting(Br->getSuccessor(1)))
99 continue;
100 if (dependsOnLocalPhi(L, Br->getCondition())) {
101 UP.Threshold += UnrollThresholdIf;
102 DEBUG(dbgs() << "Set unroll threshold " << UP.Threshold
103 << " for loop:\n" << *L << " due to " << *Br << '\n');
104 if (UP.Threshold >= MaxBoost)
105 return;
106 }
107 }
108 continue;
109 }
110
Matt Arsenaultac6e39c2014-07-17 06:19:06 +0000111 const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I);
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000112 if (!GEP)
Tom Stellard8cce9bd2014-01-23 18:49:28 +0000113 continue;
Matt Arsenaultac6e39c2014-07-17 06:19:06 +0000114
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000115 unsigned AS = GEP->getAddressSpace();
116 unsigned Threshold = 0;
117 if (AS == ASST.PRIVATE_ADDRESS)
118 Threshold = ThresholdPrivate;
119 else if (AS == ASST.LOCAL_ADDRESS)
120 Threshold = ThresholdLocal;
121 else
122 continue;
123
124 if (UP.Threshold >= Threshold)
125 continue;
126
127 if (AS == ASST.PRIVATE_ADDRESS) {
128 const Value *Ptr = GEP->getPointerOperand();
129 const AllocaInst *Alloca =
130 dyn_cast<AllocaInst>(GetUnderlyingObject(Ptr, DL));
131 if (!Alloca || !Alloca->isStaticAlloca())
132 continue;
Stanislav Mekhanoshinf29602d2017-02-03 02:20:05 +0000133 Type *Ty = Alloca->getAllocatedType();
134 unsigned AllocaSize = Ty->isSized() ? DL.getTypeAllocSize(Ty) : 0;
135 if (AllocaSize > MaxAlloca)
136 continue;
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000137 } else if (AS == ASST.LOCAL_ADDRESS) {
138 LocalGEPsSeen++;
139 // Inhibit unroll for local memory if we have seen addressing not to
140 // a variable, most likely we will be unable to combine it.
141 // Do not unroll too deep inner loops for local memory to give a chance
142 // to unroll an outer loop for a more important reason.
143 if (LocalGEPsSeen > 1 || L->getLoopDepth() > 2 ||
144 (!isa<GlobalVariable>(GEP->getPointerOperand()) &&
145 !isa<Argument>(GEP->getPointerOperand())))
146 continue;
147 }
Stanislav Mekhanoshinf29602d2017-02-03 02:20:05 +0000148
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000149 // Check if GEP depends on a value defined by this loop itself.
150 bool HasLoopDef = false;
151 for (const Value *Op : GEP->operands()) {
152 const Instruction *Inst = dyn_cast<Instruction>(Op);
153 if (!Inst || L->isLoopInvariant(Op))
Stanislav Mekhanoshinf29602d2017-02-03 02:20:05 +0000154 continue;
155
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000156 if (any_of(L->getSubLoops(), [Inst](const Loop* SubLoop) {
157 return SubLoop->contains(Inst); }))
158 continue;
159 HasLoopDef = true;
160 break;
Tom Stellard8cce9bd2014-01-23 18:49:28 +0000161 }
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000162 if (!HasLoopDef)
163 continue;
164
165 // We want to do whatever we can to limit the number of alloca
166 // instructions that make it through to the code generator. allocas
167 // require us to use indirect addressing, which is slow and prone to
168 // compiler bugs. If this loop does an address calculation on an
169 // alloca ptr, then we want to use a higher than normal loop unroll
170 // threshold. This will give SROA a better chance to eliminate these
171 // allocas.
172 //
173 // We also want to have more unrolling for local memory to let ds
174 // instructions with different offsets combine.
175 //
176 // Don't use the maximum allowed value here as it will make some
177 // programs way too big.
178 UP.Threshold = Threshold;
179 DEBUG(dbgs() << "Set unroll threshold " << Threshold << " for loop:\n"
180 << *L << " due to " << *GEP << '\n');
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +0000181 if (UP.Threshold >= MaxBoost)
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000182 return;
Tom Stellard8cce9bd2014-01-23 18:49:28 +0000183 }
184 }
185}
Matt Arsenault3dd43fc2014-07-18 06:07:13 +0000186
Matt Arsenault67cd3472017-06-20 20:38:06 +0000187unsigned AMDGPUTTIImpl::getHardwareNumberOfRegisters(bool Vec) const {
188 // The concept of vector registers doesn't really exist. Some packed vector
189 // operations operate on the normal 32-bit registers.
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000190
191 // Number of VGPRs on SI.
192 if (ST->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS)
193 return 256;
194
195 return 4 * 128; // XXX - 4 channels. Should these count as vector instead?
196}
197
Matt Arsenault67cd3472017-06-20 20:38:06 +0000198unsigned AMDGPUTTIImpl::getNumberOfRegisters(bool Vec) const {
199 // This is really the number of registers to fill when vectorizing /
200 // interleaving loops, so we lie to avoid trying to use all registers.
201 return getHardwareNumberOfRegisters(Vec) >> 3;
202}
203
Daniel Neilsonc0112ae2017-06-12 14:22:21 +0000204unsigned AMDGPUTTIImpl::getRegisterBitWidth(bool Vector) const {
Matt Arsenault67cd3472017-06-20 20:38:06 +0000205 return 32;
206}
207
208unsigned AMDGPUTTIImpl::getMinVectorRegisterBitWidth() const {
209 return 32;
Matt Arsenault4339b3f2015-12-24 05:14:55 +0000210}
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000211
Volkan Keles1c386812016-10-03 10:31:34 +0000212unsigned AMDGPUTTIImpl::getLoadStoreVecRegBitWidth(unsigned AddrSpace) const {
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000213 AMDGPUAS AS = ST->getAMDGPUAS();
214 if (AddrSpace == AS.GLOBAL_ADDRESS ||
215 AddrSpace == AS.CONSTANT_ADDRESS ||
216 AddrSpace == AS.FLAT_ADDRESS)
Matt Arsenault0994bd52016-07-01 00:56:27 +0000217 return 128;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000218 if (AddrSpace == AS.LOCAL_ADDRESS ||
219 AddrSpace == AS.REGION_ADDRESS)
Matt Arsenault0994bd52016-07-01 00:56:27 +0000220 return 64;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000221 if (AddrSpace == AS.PRIVATE_ADDRESS)
Matt Arsenault0994bd52016-07-01 00:56:27 +0000222 return 8 * ST->getMaxPrivateElementSize();
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000223
224 if (ST->getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS &&
225 (AddrSpace == AS.PARAM_D_ADDRESS ||
226 AddrSpace == AS.PARAM_I_ADDRESS ||
227 (AddrSpace >= AS.CONSTANT_BUFFER_0 &&
228 AddrSpace <= AS.CONSTANT_BUFFER_15)))
229 return 128;
230 llvm_unreachable("unhandled address space");
Matt Arsenault0994bd52016-07-01 00:56:27 +0000231}
232
Matt Arsenaultf0a88db2017-02-23 03:58:53 +0000233bool AMDGPUTTIImpl::isLegalToVectorizeMemChain(unsigned ChainSizeInBytes,
234 unsigned Alignment,
235 unsigned AddrSpace) const {
236 // We allow vectorization of flat stores, even though we may need to decompose
237 // them later if they may access private memory. We don't have enough context
238 // here, and legalization can handle it.
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000239 if (AddrSpace == ST->getAMDGPUAS().PRIVATE_ADDRESS) {
Matt Arsenaultf0a88db2017-02-23 03:58:53 +0000240 return (Alignment >= 4 || ST->hasUnalignedScratchAccess()) &&
241 ChainSizeInBytes <= ST->getMaxPrivateElementSize();
242 }
243 return true;
244}
245
246bool AMDGPUTTIImpl::isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
247 unsigned Alignment,
248 unsigned AddrSpace) const {
249 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
250}
251
252bool AMDGPUTTIImpl::isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
253 unsigned Alignment,
254 unsigned AddrSpace) const {
255 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
256}
257
Wei Mi062c7442015-05-06 17:12:25 +0000258unsigned AMDGPUTTIImpl::getMaxInterleaveFactor(unsigned VF) {
Changpeng Fang1be9b9f2017-03-09 00:07:00 +0000259 // Disable unrolling if the loop is not vectorized.
Matt Arsenault67cd3472017-06-20 20:38:06 +0000260 // TODO: Enable this again.
Changpeng Fang1be9b9f2017-03-09 00:07:00 +0000261 if (VF == 1)
262 return 1;
263
Matt Arsenault67cd3472017-06-20 20:38:06 +0000264 return 8;
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000265}
Matt Arsenaulte830f542015-12-01 19:08:39 +0000266
Matt Arsenault96518132016-03-25 01:00:32 +0000267int AMDGPUTTIImpl::getArithmeticInstrCost(
268 unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info,
269 TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000270 TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args ) {
Matt Arsenault96518132016-03-25 01:00:32 +0000271
272 EVT OrigTy = TLI->getValueType(DL, Ty);
273 if (!OrigTy.isSimple()) {
274 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
275 Opd1PropInfo, Opd2PropInfo);
276 }
277
278 // Legalize the type.
279 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
280 int ISD = TLI->InstructionOpcodeToISD(Opcode);
281
282 // Because we don't have any legal vector operations, but the legal types, we
283 // need to account for split vectors.
284 unsigned NElts = LT.second.isVector() ?
285 LT.second.getVectorNumElements() : 1;
286
287 MVT::SimpleValueType SLT = LT.second.getScalarType().SimpleTy;
288
289 switch (ISD) {
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000290 case ISD::SHL:
291 case ISD::SRL:
292 case ISD::SRA: {
293 if (SLT == MVT::i64)
294 return get64BitInstrCost() * LT.first * NElts;
295
296 // i32
297 return getFullRateInstrCost() * LT.first * NElts;
298 }
299 case ISD::ADD:
300 case ISD::SUB:
301 case ISD::AND:
302 case ISD::OR:
303 case ISD::XOR: {
304 if (SLT == MVT::i64){
305 // and, or and xor are typically split into 2 VALU instructions.
306 return 2 * getFullRateInstrCost() * LT.first * NElts;
307 }
308
309 return LT.first * NElts * getFullRateInstrCost();
310 }
311 case ISD::MUL: {
312 const int QuarterRateCost = getQuarterRateInstrCost();
313 if (SLT == MVT::i64) {
314 const int FullRateCost = getFullRateInstrCost();
315 return (4 * QuarterRateCost + (2 * 2) * FullRateCost) * LT.first * NElts;
316 }
317
318 // i32
319 return QuarterRateCost * NElts * LT.first;
320 }
Matt Arsenault96518132016-03-25 01:00:32 +0000321 case ISD::FADD:
322 case ISD::FSUB:
323 case ISD::FMUL:
324 if (SLT == MVT::f64)
325 return LT.first * NElts * get64BitInstrCost();
326
327 if (SLT == MVT::f32 || SLT == MVT::f16)
328 return LT.first * NElts * getFullRateInstrCost();
329 break;
330
331 case ISD::FDIV:
332 case ISD::FREM:
333 // FIXME: frem should be handled separately. The fdiv in it is most of it,
334 // but the current lowering is also not entirely correct.
335 if (SLT == MVT::f64) {
336 int Cost = 4 * get64BitInstrCost() + 7 * getQuarterRateInstrCost();
337
338 // Add cost of workaround.
339 if (ST->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS)
340 Cost += 3 * getFullRateInstrCost();
341
342 return LT.first * Cost * NElts;
343 }
344
345 // Assuming no fp32 denormals lowering.
346 if (SLT == MVT::f32 || SLT == MVT::f16) {
347 assert(!ST->hasFP32Denormals() && "will change when supported");
348 int Cost = 7 * getFullRateInstrCost() + 1 * getQuarterRateInstrCost();
349 return LT.first * NElts * Cost;
350 }
351
352 break;
353 default:
354 break;
355 }
356
357 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
358 Opd1PropInfo, Opd2PropInfo);
359}
360
Matt Arsenaulte05ff152015-12-16 18:37:19 +0000361unsigned AMDGPUTTIImpl::getCFInstrCost(unsigned Opcode) {
362 // XXX - For some reason this isn't called for switch.
363 switch (Opcode) {
364 case Instruction::Br:
365 case Instruction::Ret:
366 return 10;
367 default:
368 return BaseT::getCFInstrCost(Opcode);
369 }
370}
371
Matt Arsenaulte830f542015-12-01 19:08:39 +0000372int AMDGPUTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
373 unsigned Index) {
374 switch (Opcode) {
375 case Instruction::ExtractElement:
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000376 case Instruction::InsertElement: {
377 unsigned EltSize
378 = DL.getTypeSizeInBits(cast<VectorType>(ValTy)->getElementType());
379 if (EltSize < 32) {
380 if (EltSize == 16 && Index == 0 && ST->has16BitInsts())
381 return 0;
382 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
383 }
384
Matt Arsenault59767ce2016-03-25 00:14:11 +0000385 // Extracts are just reads of a subregister, so are free. Inserts are
386 // considered free because we don't want to have any cost for scalarizing
387 // operations, and we don't have to copy into a different register class.
388
Matt Arsenaulte830f542015-12-01 19:08:39 +0000389 // Dynamic indexing isn't free and is best avoided.
390 return Index == ~0u ? 2 : 0;
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000391 }
Matt Arsenaulte830f542015-12-01 19:08:39 +0000392 default:
393 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
394 }
395}
Tom Stellarddbe374b2015-12-15 18:04:38 +0000396
Matt Arsenaultd2c8a332017-02-16 02:01:13 +0000397static bool isIntrinsicSourceOfDivergence(const IntrinsicInst *I) {
Tom Stellarddbe374b2015-12-15 18:04:38 +0000398 switch (I->getIntrinsicID()) {
Matt Arsenaultfe26def2016-02-11 05:32:51 +0000399 case Intrinsic::amdgcn_workitem_id_x:
400 case Intrinsic::amdgcn_workitem_id_y:
401 case Intrinsic::amdgcn_workitem_id_z:
Nicolai Haehnlef45ea4b2016-12-12 16:52:19 +0000402 case Intrinsic::amdgcn_interp_mov:
Tom Stellarddbe374b2015-12-15 18:04:38 +0000403 case Intrinsic::amdgcn_interp_p1:
404 case Intrinsic::amdgcn_interp_p2:
405 case Intrinsic::amdgcn_mbcnt_hi:
406 case Intrinsic::amdgcn_mbcnt_lo:
407 case Intrinsic::r600_read_tidig_x:
408 case Intrinsic::r600_read_tidig_y:
409 case Intrinsic::r600_read_tidig_z:
Matt Arsenault41c14992017-01-30 17:09:47 +0000410 case Intrinsic::amdgcn_atomic_inc:
411 case Intrinsic::amdgcn_atomic_dec:
Nicolai Haehnle74127fe82016-03-14 15:37:18 +0000412 case Intrinsic::amdgcn_image_atomic_swap:
413 case Intrinsic::amdgcn_image_atomic_add:
414 case Intrinsic::amdgcn_image_atomic_sub:
415 case Intrinsic::amdgcn_image_atomic_smin:
416 case Intrinsic::amdgcn_image_atomic_umin:
417 case Intrinsic::amdgcn_image_atomic_smax:
418 case Intrinsic::amdgcn_image_atomic_umax:
419 case Intrinsic::amdgcn_image_atomic_and:
420 case Intrinsic::amdgcn_image_atomic_or:
421 case Intrinsic::amdgcn_image_atomic_xor:
422 case Intrinsic::amdgcn_image_atomic_inc:
423 case Intrinsic::amdgcn_image_atomic_dec:
424 case Intrinsic::amdgcn_image_atomic_cmpswap:
Nicolai Haehnlead636382016-03-18 16:24:31 +0000425 case Intrinsic::amdgcn_buffer_atomic_swap:
426 case Intrinsic::amdgcn_buffer_atomic_add:
427 case Intrinsic::amdgcn_buffer_atomic_sub:
428 case Intrinsic::amdgcn_buffer_atomic_smin:
429 case Intrinsic::amdgcn_buffer_atomic_umin:
430 case Intrinsic::amdgcn_buffer_atomic_smax:
431 case Intrinsic::amdgcn_buffer_atomic_umax:
432 case Intrinsic::amdgcn_buffer_atomic_and:
433 case Intrinsic::amdgcn_buffer_atomic_or:
434 case Intrinsic::amdgcn_buffer_atomic_xor:
435 case Intrinsic::amdgcn_buffer_atomic_cmpswap:
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000436 case Intrinsic::amdgcn_ps_live:
Matt Arsenault41c14992017-01-30 17:09:47 +0000437 case Intrinsic::amdgcn_ds_swizzle:
Tom Stellarddbe374b2015-12-15 18:04:38 +0000438 return true;
Tom Stellarddbe374b2015-12-15 18:04:38 +0000439 default:
440 return false;
Tom Stellarddbe374b2015-12-15 18:04:38 +0000441 }
442}
443
444static bool isArgPassedInSGPR(const Argument *A) {
445 const Function *F = A->getParent();
Tom Stellarddbe374b2015-12-15 18:04:38 +0000446
447 // Arguments to compute shaders are never a source of divergence.
Matt Arsenault4c1ecde2017-04-19 17:42:34 +0000448 CallingConv::ID CC = F->getCallingConv();
449 switch (CC) {
450 case CallingConv::AMDGPU_KERNEL:
451 case CallingConv::SPIR_KERNEL:
Tom Stellarddbe374b2015-12-15 18:04:38 +0000452 return true;
Matt Arsenault4c1ecde2017-04-19 17:42:34 +0000453 case CallingConv::AMDGPU_VS:
Marek Olsaka302a7362017-05-02 15:41:10 +0000454 case CallingConv::AMDGPU_HS:
Matt Arsenault4c1ecde2017-04-19 17:42:34 +0000455 case CallingConv::AMDGPU_GS:
456 case CallingConv::AMDGPU_PS:
457 case CallingConv::AMDGPU_CS:
458 // For non-compute shaders, SGPR inputs are marked with either inreg or byval.
459 // Everything else is in VGPRs.
460 return F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::InReg) ||
461 F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::ByVal);
462 default:
463 // TODO: Should calls support inreg for SGPR inputs?
464 return false;
465 }
Tom Stellarddbe374b2015-12-15 18:04:38 +0000466}
467
468///
469/// \returns true if the result of the value could potentially be
470/// different across workitems in a wavefront.
471bool AMDGPUTTIImpl::isSourceOfDivergence(const Value *V) const {
472
473 if (const Argument *A = dyn_cast<Argument>(V))
474 return !isArgPassedInSGPR(A);
475
476 // Loads from the private address space are divergent, because threads
477 // can execute the load instruction with the same inputs and get different
478 // results.
479 //
480 // All other loads are not divergent, because if threads issue loads with the
481 // same arguments, they will always get the same result.
482 if (const LoadInst *Load = dyn_cast<LoadInst>(V))
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000483 return Load->getPointerAddressSpace() == ST->getAMDGPUAS().PRIVATE_ADDRESS;
Tom Stellarddbe374b2015-12-15 18:04:38 +0000484
Nicolai Haehnle79cad852016-03-17 16:21:59 +0000485 // Atomics are divergent because they are executed sequentially: when an
486 // atomic operation refers to the same address in each thread, then each
487 // thread after the first sees the value written by the previous thread as
488 // original value.
489 if (isa<AtomicRMWInst>(V) || isa<AtomicCmpXchgInst>(V))
490 return true;
491
Matt Arsenaultd2c8a332017-02-16 02:01:13 +0000492 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V))
493 return isIntrinsicSourceOfDivergence(Intrinsic);
Tom Stellarddbe374b2015-12-15 18:04:38 +0000494
495 // Assume all function calls are a source of divergence.
496 if (isa<CallInst>(V) || isa<InvokeInst>(V))
497 return true;
498
499 return false;
500}
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000501
Alexander Timofeev0f9c84c2017-06-15 19:33:10 +0000502bool AMDGPUTTIImpl::isAlwaysUniform(const Value *V) const {
503 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) {
504 switch (Intrinsic->getIntrinsicID()) {
505 default:
506 return false;
507 case Intrinsic::amdgcn_readfirstlane:
508 case Intrinsic::amdgcn_readlane:
509 return true;
510 }
511 }
512 return false;
513}
514
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000515unsigned AMDGPUTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
516 Type *SubTp) {
517 if (ST->hasVOP3PInsts()) {
518 VectorType *VT = cast<VectorType>(Tp);
519 if (VT->getNumElements() == 2 &&
520 DL.getTypeSizeInBits(VT->getElementType()) == 16) {
521 // With op_sel VOP3P instructions freely can access the low half or high
522 // half of a register, so any swizzle is free.
523
524 switch (Kind) {
525 case TTI::SK_Broadcast:
526 case TTI::SK_Reverse:
527 case TTI::SK_PermuteSingleSrc:
528 return 0;
529 default:
530 break;
531 }
532 }
533 }
534
535 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
536}