blob: c5b7086dd48e39b3ab75ca49c51e3ebf94f5f6a2 [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"
Mehdi Aminia28d91d2015-03-10 02:37:25 +000023#include "llvm/IR/Module.h"
Tom Stellardbc4497b2016-02-12 23:45:29 +000024#include "llvm/IR/Intrinsics.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 Mekhanoshin81db5312017-02-03 20:08:29 +000035 cl::init(2000), cl::Hidden);
Matt Arsenault96518132016-03-25 01:00:32 +000036
Chandler Carruthab5cb362015-02-01 14:31:23 +000037void AMDGPUTTIImpl::getUnrollingPreferences(Loop *L,
Chandler Carruth705b1852015-01-31 03:43:40 +000038 TTI::UnrollingPreferences &UP) {
Matt Arsenaultc8244582014-07-25 23:02:42 +000039 UP.Threshold = 300; // Twice the default.
Tom Stellardeea3f702015-02-05 15:32:18 +000040 UP.MaxCount = UINT_MAX;
Matt Arsenaultc8244582014-07-25 23:02:42 +000041 UP.Partial = true;
42
43 // TODO: Do we want runtime unrolling?
44
Stanislav Mekhanoshinf29602d2017-02-03 02:20:05 +000045 // Maximum alloca size than can fit registers. Reserve 16 registers.
46 const unsigned MaxAlloca = (256 - 16) * 4;
Matt Arsenaultac6e39c2014-07-17 06:19:06 +000047 for (const BasicBlock *BB : L->getBlocks()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +000048 const DataLayout &DL = BB->getModule()->getDataLayout();
Matt Arsenaultac6e39c2014-07-17 06:19:06 +000049 for (const Instruction &I : *BB) {
50 const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I);
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000051 if (!GEP || GEP->getAddressSpace() != ST->getAMDGPUAS().PRIVATE_ADDRESS)
Tom Stellard8cce9bd2014-01-23 18:49:28 +000052 continue;
Matt Arsenaultac6e39c2014-07-17 06:19:06 +000053
Tom Stellard8cce9bd2014-01-23 18:49:28 +000054 const Value *Ptr = GEP->getPointerOperand();
Mehdi Aminia28d91d2015-03-10 02:37:25 +000055 const AllocaInst *Alloca =
56 dyn_cast<AllocaInst>(GetUnderlyingObject(Ptr, DL));
Matt Arsenaultd9cd7362017-02-03 19:36:00 +000057 if (Alloca && Alloca->isStaticAlloca()) {
Stanislav Mekhanoshinf29602d2017-02-03 02:20:05 +000058 Type *Ty = Alloca->getAllocatedType();
59 unsigned AllocaSize = Ty->isSized() ? DL.getTypeAllocSize(Ty) : 0;
60 if (AllocaSize > MaxAlloca)
61 continue;
62
63 // Check if GEP depends on a value defined by this loop itself.
64 bool HasLoopDef = false;
65 for (const Value *Op : GEP->operands()) {
66 const Instruction *Inst = dyn_cast<Instruction>(Op);
67 if (!Inst || L->isLoopInvariant(Op))
68 continue;
69 if (any_of(L->getSubLoops(), [Inst](const Loop* SubLoop) {
70 return SubLoop->contains(Inst); }))
71 continue;
72 HasLoopDef = true;
73 break;
74 }
75 if (!HasLoopDef)
76 continue;
77
Tom Stellard8cce9bd2014-01-23 18:49:28 +000078 // We want to do whatever we can to limit the number of alloca
79 // instructions that make it through to the code generator. allocas
80 // require us to use indirect addressing, which is slow and prone to
81 // compiler bugs. If this loop does an address calculation on an
Tom Stellardfd0d86c2014-02-25 21:36:21 +000082 // alloca ptr, then we want to use a higher than normal loop unroll
Matt Arsenault5e1e4312014-04-04 20:13:08 +000083 // threshold. This will give SROA a better chance to eliminate these
84 // allocas.
85 //
86 // Don't use the maximum allowed value here as it will make some
87 // programs way too big.
Stanislav Mekhanoshinf29602d2017-02-03 02:20:05 +000088 UP.Threshold = UnrollThresholdPrivate;
89 return;
Tom Stellard8cce9bd2014-01-23 18:49:28 +000090 }
91 }
92 }
93}
Matt Arsenault3dd43fc2014-07-18 06:07:13 +000094
Chandler Carruth705b1852015-01-31 03:43:40 +000095unsigned AMDGPUTTIImpl::getNumberOfRegisters(bool Vec) {
Matt Arsenaulta93441f2014-07-19 18:15:16 +000096 if (Vec)
97 return 0;
98
99 // Number of VGPRs on SI.
100 if (ST->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS)
101 return 256;
102
103 return 4 * 128; // XXX - 4 channels. Should these count as vector instead?
104}
105
Matt Arsenault4339b3f2015-12-24 05:14:55 +0000106unsigned AMDGPUTTIImpl::getRegisterBitWidth(bool Vector) {
107 return Vector ? 0 : 32;
108}
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000109
Volkan Keles1c386812016-10-03 10:31:34 +0000110unsigned AMDGPUTTIImpl::getLoadStoreVecRegBitWidth(unsigned AddrSpace) const {
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000111 AMDGPUAS AS = ST->getAMDGPUAS();
112 if (AddrSpace == AS.GLOBAL_ADDRESS ||
113 AddrSpace == AS.CONSTANT_ADDRESS ||
114 AddrSpace == AS.FLAT_ADDRESS)
Matt Arsenault0994bd52016-07-01 00:56:27 +0000115 return 128;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000116 if (AddrSpace == AS.LOCAL_ADDRESS ||
117 AddrSpace == AS.REGION_ADDRESS)
Matt Arsenault0994bd52016-07-01 00:56:27 +0000118 return 64;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000119 if (AddrSpace == AS.PRIVATE_ADDRESS)
Matt Arsenault0994bd52016-07-01 00:56:27 +0000120 return 8 * ST->getMaxPrivateElementSize();
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000121
122 if (ST->getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS &&
123 (AddrSpace == AS.PARAM_D_ADDRESS ||
124 AddrSpace == AS.PARAM_I_ADDRESS ||
125 (AddrSpace >= AS.CONSTANT_BUFFER_0 &&
126 AddrSpace <= AS.CONSTANT_BUFFER_15)))
127 return 128;
128 llvm_unreachable("unhandled address space");
Matt Arsenault0994bd52016-07-01 00:56:27 +0000129}
130
Matt Arsenaultf0a88db2017-02-23 03:58:53 +0000131bool AMDGPUTTIImpl::isLegalToVectorizeMemChain(unsigned ChainSizeInBytes,
132 unsigned Alignment,
133 unsigned AddrSpace) const {
134 // We allow vectorization of flat stores, even though we may need to decompose
135 // them later if they may access private memory. We don't have enough context
136 // here, and legalization can handle it.
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000137 if (AddrSpace == ST->getAMDGPUAS().PRIVATE_ADDRESS) {
Matt Arsenaultf0a88db2017-02-23 03:58:53 +0000138 return (Alignment >= 4 || ST->hasUnalignedScratchAccess()) &&
139 ChainSizeInBytes <= ST->getMaxPrivateElementSize();
140 }
141 return true;
142}
143
144bool AMDGPUTTIImpl::isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
145 unsigned Alignment,
146 unsigned AddrSpace) const {
147 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
148}
149
150bool AMDGPUTTIImpl::isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
151 unsigned Alignment,
152 unsigned AddrSpace) const {
153 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
154}
155
Wei Mi062c7442015-05-06 17:12:25 +0000156unsigned AMDGPUTTIImpl::getMaxInterleaveFactor(unsigned VF) {
Changpeng Fang1be9b9f2017-03-09 00:07:00 +0000157 // Disable unrolling if the loop is not vectorized.
158 if (VF == 1)
159 return 1;
160
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000161 // Semi-arbitrary large amount.
162 return 64;
163}
Matt Arsenaulte830f542015-12-01 19:08:39 +0000164
Matt Arsenault96518132016-03-25 01:00:32 +0000165int AMDGPUTTIImpl::getArithmeticInstrCost(
166 unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info,
167 TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000168 TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args ) {
Matt Arsenault96518132016-03-25 01:00:32 +0000169
170 EVT OrigTy = TLI->getValueType(DL, Ty);
171 if (!OrigTy.isSimple()) {
172 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
173 Opd1PropInfo, Opd2PropInfo);
174 }
175
176 // Legalize the type.
177 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
178 int ISD = TLI->InstructionOpcodeToISD(Opcode);
179
180 // Because we don't have any legal vector operations, but the legal types, we
181 // need to account for split vectors.
182 unsigned NElts = LT.second.isVector() ?
183 LT.second.getVectorNumElements() : 1;
184
185 MVT::SimpleValueType SLT = LT.second.getScalarType().SimpleTy;
186
187 switch (ISD) {
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000188 case ISD::SHL:
189 case ISD::SRL:
190 case ISD::SRA: {
191 if (SLT == MVT::i64)
192 return get64BitInstrCost() * LT.first * NElts;
193
194 // i32
195 return getFullRateInstrCost() * LT.first * NElts;
196 }
197 case ISD::ADD:
198 case ISD::SUB:
199 case ISD::AND:
200 case ISD::OR:
201 case ISD::XOR: {
202 if (SLT == MVT::i64){
203 // and, or and xor are typically split into 2 VALU instructions.
204 return 2 * getFullRateInstrCost() * LT.first * NElts;
205 }
206
207 return LT.first * NElts * getFullRateInstrCost();
208 }
209 case ISD::MUL: {
210 const int QuarterRateCost = getQuarterRateInstrCost();
211 if (SLT == MVT::i64) {
212 const int FullRateCost = getFullRateInstrCost();
213 return (4 * QuarterRateCost + (2 * 2) * FullRateCost) * LT.first * NElts;
214 }
215
216 // i32
217 return QuarterRateCost * NElts * LT.first;
218 }
Matt Arsenault96518132016-03-25 01:00:32 +0000219 case ISD::FADD:
220 case ISD::FSUB:
221 case ISD::FMUL:
222 if (SLT == MVT::f64)
223 return LT.first * NElts * get64BitInstrCost();
224
225 if (SLT == MVT::f32 || SLT == MVT::f16)
226 return LT.first * NElts * getFullRateInstrCost();
227 break;
228
229 case ISD::FDIV:
230 case ISD::FREM:
231 // FIXME: frem should be handled separately. The fdiv in it is most of it,
232 // but the current lowering is also not entirely correct.
233 if (SLT == MVT::f64) {
234 int Cost = 4 * get64BitInstrCost() + 7 * getQuarterRateInstrCost();
235
236 // Add cost of workaround.
237 if (ST->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS)
238 Cost += 3 * getFullRateInstrCost();
239
240 return LT.first * Cost * NElts;
241 }
242
243 // Assuming no fp32 denormals lowering.
244 if (SLT == MVT::f32 || SLT == MVT::f16) {
245 assert(!ST->hasFP32Denormals() && "will change when supported");
246 int Cost = 7 * getFullRateInstrCost() + 1 * getQuarterRateInstrCost();
247 return LT.first * NElts * Cost;
248 }
249
250 break;
251 default:
252 break;
253 }
254
255 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
256 Opd1PropInfo, Opd2PropInfo);
257}
258
Matt Arsenaulte05ff152015-12-16 18:37:19 +0000259unsigned AMDGPUTTIImpl::getCFInstrCost(unsigned Opcode) {
260 // XXX - For some reason this isn't called for switch.
261 switch (Opcode) {
262 case Instruction::Br:
263 case Instruction::Ret:
264 return 10;
265 default:
266 return BaseT::getCFInstrCost(Opcode);
267 }
268}
269
Matt Arsenaulte830f542015-12-01 19:08:39 +0000270int AMDGPUTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
271 unsigned Index) {
272 switch (Opcode) {
273 case Instruction::ExtractElement:
Matt Arsenault59767ce2016-03-25 00:14:11 +0000274 case Instruction::InsertElement:
275 // Extracts are just reads of a subregister, so are free. Inserts are
276 // considered free because we don't want to have any cost for scalarizing
277 // operations, and we don't have to copy into a different register class.
278
Matt Arsenaulte830f542015-12-01 19:08:39 +0000279 // Dynamic indexing isn't free and is best avoided.
280 return Index == ~0u ? 2 : 0;
281 default:
282 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
283 }
284}
Tom Stellarddbe374b2015-12-15 18:04:38 +0000285
Matt Arsenaultd2c8a332017-02-16 02:01:13 +0000286static bool isIntrinsicSourceOfDivergence(const IntrinsicInst *I) {
Tom Stellarddbe374b2015-12-15 18:04:38 +0000287 switch (I->getIntrinsicID()) {
Matt Arsenaultfe26def2016-02-11 05:32:51 +0000288 case Intrinsic::amdgcn_workitem_id_x:
289 case Intrinsic::amdgcn_workitem_id_y:
290 case Intrinsic::amdgcn_workitem_id_z:
Nicolai Haehnlef45ea4b2016-12-12 16:52:19 +0000291 case Intrinsic::amdgcn_interp_mov:
Tom Stellarddbe374b2015-12-15 18:04:38 +0000292 case Intrinsic::amdgcn_interp_p1:
293 case Intrinsic::amdgcn_interp_p2:
294 case Intrinsic::amdgcn_mbcnt_hi:
295 case Intrinsic::amdgcn_mbcnt_lo:
296 case Intrinsic::r600_read_tidig_x:
297 case Intrinsic::r600_read_tidig_y:
298 case Intrinsic::r600_read_tidig_z:
Matt Arsenault41c14992017-01-30 17:09:47 +0000299 case Intrinsic::amdgcn_atomic_inc:
300 case Intrinsic::amdgcn_atomic_dec:
Nicolai Haehnle74127fe82016-03-14 15:37:18 +0000301 case Intrinsic::amdgcn_image_atomic_swap:
302 case Intrinsic::amdgcn_image_atomic_add:
303 case Intrinsic::amdgcn_image_atomic_sub:
304 case Intrinsic::amdgcn_image_atomic_smin:
305 case Intrinsic::amdgcn_image_atomic_umin:
306 case Intrinsic::amdgcn_image_atomic_smax:
307 case Intrinsic::amdgcn_image_atomic_umax:
308 case Intrinsic::amdgcn_image_atomic_and:
309 case Intrinsic::amdgcn_image_atomic_or:
310 case Intrinsic::amdgcn_image_atomic_xor:
311 case Intrinsic::amdgcn_image_atomic_inc:
312 case Intrinsic::amdgcn_image_atomic_dec:
313 case Intrinsic::amdgcn_image_atomic_cmpswap:
Nicolai Haehnlead636382016-03-18 16:24:31 +0000314 case Intrinsic::amdgcn_buffer_atomic_swap:
315 case Intrinsic::amdgcn_buffer_atomic_add:
316 case Intrinsic::amdgcn_buffer_atomic_sub:
317 case Intrinsic::amdgcn_buffer_atomic_smin:
318 case Intrinsic::amdgcn_buffer_atomic_umin:
319 case Intrinsic::amdgcn_buffer_atomic_smax:
320 case Intrinsic::amdgcn_buffer_atomic_umax:
321 case Intrinsic::amdgcn_buffer_atomic_and:
322 case Intrinsic::amdgcn_buffer_atomic_or:
323 case Intrinsic::amdgcn_buffer_atomic_xor:
324 case Intrinsic::amdgcn_buffer_atomic_cmpswap:
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000325 case Intrinsic::amdgcn_ps_live:
Matt Arsenault41c14992017-01-30 17:09:47 +0000326 case Intrinsic::amdgcn_ds_swizzle:
Tom Stellarddbe374b2015-12-15 18:04:38 +0000327 return true;
Tom Stellarddbe374b2015-12-15 18:04:38 +0000328 default:
329 return false;
Tom Stellarddbe374b2015-12-15 18:04:38 +0000330 }
331}
332
333static bool isArgPassedInSGPR(const Argument *A) {
334 const Function *F = A->getParent();
Tom Stellarddbe374b2015-12-15 18:04:38 +0000335
336 // Arguments to compute shaders are never a source of divergence.
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000337 if (!AMDGPU::isShader(F->getCallingConv()))
Tom Stellarddbe374b2015-12-15 18:04:38 +0000338 return true;
339
Tom Stellardffc1a5a2015-12-19 02:54:15 +0000340 // For non-compute shaders, SGPR inputs are marked with either inreg or byval.
341 if (F->getAttributes().hasAttribute(A->getArgNo() + 1, Attribute::InReg) ||
342 F->getAttributes().hasAttribute(A->getArgNo() + 1, Attribute::ByVal))
Tom Stellarddbe374b2015-12-15 18:04:38 +0000343 return true;
344
Tom Stellardffc1a5a2015-12-19 02:54:15 +0000345 // Everything else is in VGPRs.
346 return false;
Tom Stellarddbe374b2015-12-15 18:04:38 +0000347}
348
349///
350/// \returns true if the result of the value could potentially be
351/// different across workitems in a wavefront.
352bool AMDGPUTTIImpl::isSourceOfDivergence(const Value *V) const {
353
354 if (const Argument *A = dyn_cast<Argument>(V))
355 return !isArgPassedInSGPR(A);
356
357 // Loads from the private address space are divergent, because threads
358 // can execute the load instruction with the same inputs and get different
359 // results.
360 //
361 // All other loads are not divergent, because if threads issue loads with the
362 // same arguments, they will always get the same result.
363 if (const LoadInst *Load = dyn_cast<LoadInst>(V))
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000364 return Load->getPointerAddressSpace() == ST->getAMDGPUAS().PRIVATE_ADDRESS;
Tom Stellarddbe374b2015-12-15 18:04:38 +0000365
Nicolai Haehnle79cad852016-03-17 16:21:59 +0000366 // Atomics are divergent because they are executed sequentially: when an
367 // atomic operation refers to the same address in each thread, then each
368 // thread after the first sees the value written by the previous thread as
369 // original value.
370 if (isa<AtomicRMWInst>(V) || isa<AtomicCmpXchgInst>(V))
371 return true;
372
Matt Arsenaultd2c8a332017-02-16 02:01:13 +0000373 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V))
374 return isIntrinsicSourceOfDivergence(Intrinsic);
Tom Stellarddbe374b2015-12-15 18:04:38 +0000375
376 // Assume all function calls are a source of divergence.
377 if (isa<CallInst>(V) || isa<InvokeInst>(V))
378 return true;
379
380 return false;
381}