blob: f3c94182730f5b7b9712e0119302729ec610bdd5 [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);
Matt Arsenault5e2b0f52014-07-17 06:13:41 +000051 if (!GEP || GEP->getAddressSpace() != AMDGPUAS::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 {
Matt Arsenault0994bd52016-07-01 00:56:27 +0000111 switch (AddrSpace) {
112 case AMDGPUAS::GLOBAL_ADDRESS:
113 case AMDGPUAS::CONSTANT_ADDRESS:
114 case AMDGPUAS::FLAT_ADDRESS:
115 return 128;
116 case AMDGPUAS::LOCAL_ADDRESS:
117 case AMDGPUAS::REGION_ADDRESS:
118 return 64;
119 case AMDGPUAS::PRIVATE_ADDRESS:
120 return 8 * ST->getMaxPrivateElementSize();
121 default:
122 if (ST->getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS &&
123 (AddrSpace == AMDGPUAS::PARAM_D_ADDRESS ||
124 AddrSpace == AMDGPUAS::PARAM_I_ADDRESS ||
125 (AddrSpace >= AMDGPUAS::CONSTANT_BUFFER_0 &&
126 AddrSpace <= AMDGPUAS::CONSTANT_BUFFER_15)))
127 return 128;
128 llvm_unreachable("unhandled address space");
129 }
130}
131
Wei Mi062c7442015-05-06 17:12:25 +0000132unsigned AMDGPUTTIImpl::getMaxInterleaveFactor(unsigned VF) {
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000133 // Semi-arbitrary large amount.
134 return 64;
135}
Matt Arsenaulte830f542015-12-01 19:08:39 +0000136
Matt Arsenault96518132016-03-25 01:00:32 +0000137int AMDGPUTTIImpl::getArithmeticInstrCost(
138 unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info,
139 TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000140 TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args ) {
Matt Arsenault96518132016-03-25 01:00:32 +0000141
142 EVT OrigTy = TLI->getValueType(DL, Ty);
143 if (!OrigTy.isSimple()) {
144 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
145 Opd1PropInfo, Opd2PropInfo);
146 }
147
148 // Legalize the type.
149 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
150 int ISD = TLI->InstructionOpcodeToISD(Opcode);
151
152 // Because we don't have any legal vector operations, but the legal types, we
153 // need to account for split vectors.
154 unsigned NElts = LT.second.isVector() ?
155 LT.second.getVectorNumElements() : 1;
156
157 MVT::SimpleValueType SLT = LT.second.getScalarType().SimpleTy;
158
159 switch (ISD) {
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000160 case ISD::SHL:
161 case ISD::SRL:
162 case ISD::SRA: {
163 if (SLT == MVT::i64)
164 return get64BitInstrCost() * LT.first * NElts;
165
166 // i32
167 return getFullRateInstrCost() * LT.first * NElts;
168 }
169 case ISD::ADD:
170 case ISD::SUB:
171 case ISD::AND:
172 case ISD::OR:
173 case ISD::XOR: {
174 if (SLT == MVT::i64){
175 // and, or and xor are typically split into 2 VALU instructions.
176 return 2 * getFullRateInstrCost() * LT.first * NElts;
177 }
178
179 return LT.first * NElts * getFullRateInstrCost();
180 }
181 case ISD::MUL: {
182 const int QuarterRateCost = getQuarterRateInstrCost();
183 if (SLT == MVT::i64) {
184 const int FullRateCost = getFullRateInstrCost();
185 return (4 * QuarterRateCost + (2 * 2) * FullRateCost) * LT.first * NElts;
186 }
187
188 // i32
189 return QuarterRateCost * NElts * LT.first;
190 }
Matt Arsenault96518132016-03-25 01:00:32 +0000191 case ISD::FADD:
192 case ISD::FSUB:
193 case ISD::FMUL:
194 if (SLT == MVT::f64)
195 return LT.first * NElts * get64BitInstrCost();
196
197 if (SLT == MVT::f32 || SLT == MVT::f16)
198 return LT.first * NElts * getFullRateInstrCost();
199 break;
200
201 case ISD::FDIV:
202 case ISD::FREM:
203 // FIXME: frem should be handled separately. The fdiv in it is most of it,
204 // but the current lowering is also not entirely correct.
205 if (SLT == MVT::f64) {
206 int Cost = 4 * get64BitInstrCost() + 7 * getQuarterRateInstrCost();
207
208 // Add cost of workaround.
209 if (ST->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS)
210 Cost += 3 * getFullRateInstrCost();
211
212 return LT.first * Cost * NElts;
213 }
214
215 // Assuming no fp32 denormals lowering.
216 if (SLT == MVT::f32 || SLT == MVT::f16) {
217 assert(!ST->hasFP32Denormals() && "will change when supported");
218 int Cost = 7 * getFullRateInstrCost() + 1 * getQuarterRateInstrCost();
219 return LT.first * NElts * Cost;
220 }
221
222 break;
223 default:
224 break;
225 }
226
227 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
228 Opd1PropInfo, Opd2PropInfo);
229}
230
Matt Arsenaulte05ff152015-12-16 18:37:19 +0000231unsigned AMDGPUTTIImpl::getCFInstrCost(unsigned Opcode) {
232 // XXX - For some reason this isn't called for switch.
233 switch (Opcode) {
234 case Instruction::Br:
235 case Instruction::Ret:
236 return 10;
237 default:
238 return BaseT::getCFInstrCost(Opcode);
239 }
240}
241
Matt Arsenaulte830f542015-12-01 19:08:39 +0000242int AMDGPUTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
243 unsigned Index) {
244 switch (Opcode) {
245 case Instruction::ExtractElement:
Matt Arsenault59767ce2016-03-25 00:14:11 +0000246 case Instruction::InsertElement:
247 // Extracts are just reads of a subregister, so are free. Inserts are
248 // considered free because we don't want to have any cost for scalarizing
249 // operations, and we don't have to copy into a different register class.
250
Matt Arsenaulte830f542015-12-01 19:08:39 +0000251 // Dynamic indexing isn't free and is best avoided.
252 return Index == ~0u ? 2 : 0;
253 default:
254 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
255 }
256}
Tom Stellarddbe374b2015-12-15 18:04:38 +0000257
258static bool isIntrinsicSourceOfDivergence(const TargetIntrinsicInfo *TII,
259 const IntrinsicInst *I) {
260 switch (I->getIntrinsicID()) {
261 default:
262 return false;
263 case Intrinsic::not_intrinsic:
264 // This means we have an intrinsic that isn't defined in
265 // IntrinsicsAMDGPU.td
266 break;
267
Matt Arsenaultfe26def2016-02-11 05:32:51 +0000268 case Intrinsic::amdgcn_workitem_id_x:
269 case Intrinsic::amdgcn_workitem_id_y:
270 case Intrinsic::amdgcn_workitem_id_z:
Nicolai Haehnlef45ea4b2016-12-12 16:52:19 +0000271 case Intrinsic::amdgcn_interp_mov:
Tom Stellarddbe374b2015-12-15 18:04:38 +0000272 case Intrinsic::amdgcn_interp_p1:
273 case Intrinsic::amdgcn_interp_p2:
274 case Intrinsic::amdgcn_mbcnt_hi:
275 case Intrinsic::amdgcn_mbcnt_lo:
276 case Intrinsic::r600_read_tidig_x:
277 case Intrinsic::r600_read_tidig_y:
278 case Intrinsic::r600_read_tidig_z:
Matt Arsenault41c14992017-01-30 17:09:47 +0000279 case Intrinsic::amdgcn_atomic_inc:
280 case Intrinsic::amdgcn_atomic_dec:
Nicolai Haehnle74127fe82016-03-14 15:37:18 +0000281 case Intrinsic::amdgcn_image_atomic_swap:
282 case Intrinsic::amdgcn_image_atomic_add:
283 case Intrinsic::amdgcn_image_atomic_sub:
284 case Intrinsic::amdgcn_image_atomic_smin:
285 case Intrinsic::amdgcn_image_atomic_umin:
286 case Intrinsic::amdgcn_image_atomic_smax:
287 case Intrinsic::amdgcn_image_atomic_umax:
288 case Intrinsic::amdgcn_image_atomic_and:
289 case Intrinsic::amdgcn_image_atomic_or:
290 case Intrinsic::amdgcn_image_atomic_xor:
291 case Intrinsic::amdgcn_image_atomic_inc:
292 case Intrinsic::amdgcn_image_atomic_dec:
293 case Intrinsic::amdgcn_image_atomic_cmpswap:
Nicolai Haehnlead636382016-03-18 16:24:31 +0000294 case Intrinsic::amdgcn_buffer_atomic_swap:
295 case Intrinsic::amdgcn_buffer_atomic_add:
296 case Intrinsic::amdgcn_buffer_atomic_sub:
297 case Intrinsic::amdgcn_buffer_atomic_smin:
298 case Intrinsic::amdgcn_buffer_atomic_umin:
299 case Intrinsic::amdgcn_buffer_atomic_smax:
300 case Intrinsic::amdgcn_buffer_atomic_umax:
301 case Intrinsic::amdgcn_buffer_atomic_and:
302 case Intrinsic::amdgcn_buffer_atomic_or:
303 case Intrinsic::amdgcn_buffer_atomic_xor:
304 case Intrinsic::amdgcn_buffer_atomic_cmpswap:
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000305 case Intrinsic::amdgcn_ps_live:
Matt Arsenault41c14992017-01-30 17:09:47 +0000306 case Intrinsic::amdgcn_ds_swizzle:
Tom Stellarddbe374b2015-12-15 18:04:38 +0000307 return true;
308 }
309
310 StringRef Name = I->getCalledFunction()->getName();
311 switch (TII->lookupName((const char *)Name.bytes_begin(), Name.size())) {
312 default:
313 return false;
Tom Stellarddbe374b2015-12-15 18:04:38 +0000314 case AMDGPUIntrinsic::SI_fs_interp:
Nicolai Haehnle119d3d82016-05-02 17:37:01 +0000315 case AMDGPUIntrinsic::SI_fs_constant:
Tom Stellarddbe374b2015-12-15 18:04:38 +0000316 return true;
317 }
318}
319
320static bool isArgPassedInSGPR(const Argument *A) {
321 const Function *F = A->getParent();
Tom Stellarddbe374b2015-12-15 18:04:38 +0000322
323 // Arguments to compute shaders are never a source of divergence.
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000324 if (!AMDGPU::isShader(F->getCallingConv()))
Tom Stellarddbe374b2015-12-15 18:04:38 +0000325 return true;
326
Tom Stellardffc1a5a2015-12-19 02:54:15 +0000327 // For non-compute shaders, SGPR inputs are marked with either inreg or byval.
328 if (F->getAttributes().hasAttribute(A->getArgNo() + 1, Attribute::InReg) ||
329 F->getAttributes().hasAttribute(A->getArgNo() + 1, Attribute::ByVal))
Tom Stellarddbe374b2015-12-15 18:04:38 +0000330 return true;
331
Tom Stellardffc1a5a2015-12-19 02:54:15 +0000332 // Everything else is in VGPRs.
333 return false;
Tom Stellarddbe374b2015-12-15 18:04:38 +0000334}
335
336///
337/// \returns true if the result of the value could potentially be
338/// different across workitems in a wavefront.
339bool AMDGPUTTIImpl::isSourceOfDivergence(const Value *V) const {
340
341 if (const Argument *A = dyn_cast<Argument>(V))
342 return !isArgPassedInSGPR(A);
343
344 // Loads from the private address space are divergent, because threads
345 // can execute the load instruction with the same inputs and get different
346 // results.
347 //
348 // All other loads are not divergent, because if threads issue loads with the
349 // same arguments, they will always get the same result.
350 if (const LoadInst *Load = dyn_cast<LoadInst>(V))
351 return Load->getPointerAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS;
352
Nicolai Haehnle79cad852016-03-17 16:21:59 +0000353 // Atomics are divergent because they are executed sequentially: when an
354 // atomic operation refers to the same address in each thread, then each
355 // thread after the first sees the value written by the previous thread as
356 // original value.
357 if (isa<AtomicRMWInst>(V) || isa<AtomicCmpXchgInst>(V))
358 return true;
359
Tom Stellarddbe374b2015-12-15 18:04:38 +0000360 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) {
361 const TargetMachine &TM = getTLI()->getTargetMachine();
362 return isIntrinsicSourceOfDivergence(TM.getIntrinsicInfo(), Intrinsic);
363 }
364
365 // Assume all function calls are a source of divergence.
366 if (isa<CallInst>(V) || isa<InvokeInst>(V))
367 return true;
368
369 return false;
370}