blob: 39be33e4a2cf6af837fe4d3e441ccbd4c2d1f838 [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
Chandler Carruthab5cb362015-02-01 14:31:23 +000032void AMDGPUTTIImpl::getUnrollingPreferences(Loop *L,
Chandler Carruth705b1852015-01-31 03:43:40 +000033 TTI::UnrollingPreferences &UP) {
Matt Arsenaultc8244582014-07-25 23:02:42 +000034 UP.Threshold = 300; // Twice the default.
Tom Stellardeea3f702015-02-05 15:32:18 +000035 UP.MaxCount = UINT_MAX;
Matt Arsenaultc8244582014-07-25 23:02:42 +000036 UP.Partial = true;
37
38 // TODO: Do we want runtime unrolling?
39
Matt Arsenaultac6e39c2014-07-17 06:19:06 +000040 for (const BasicBlock *BB : L->getBlocks()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +000041 const DataLayout &DL = BB->getModule()->getDataLayout();
Matt Arsenaultac6e39c2014-07-17 06:19:06 +000042 for (const Instruction &I : *BB) {
43 const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I);
Matt Arsenault5e2b0f52014-07-17 06:13:41 +000044 if (!GEP || GEP->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
Tom Stellard8cce9bd2014-01-23 18:49:28 +000045 continue;
Matt Arsenaultac6e39c2014-07-17 06:19:06 +000046
Tom Stellard8cce9bd2014-01-23 18:49:28 +000047 const Value *Ptr = GEP->getPointerOperand();
Mehdi Aminia28d91d2015-03-10 02:37:25 +000048 const AllocaInst *Alloca =
49 dyn_cast<AllocaInst>(GetUnderlyingObject(Ptr, DL));
Tom Stellard8cce9bd2014-01-23 18:49:28 +000050 if (Alloca) {
51 // We want to do whatever we can to limit the number of alloca
52 // instructions that make it through to the code generator. allocas
53 // require us to use indirect addressing, which is slow and prone to
54 // compiler bugs. If this loop does an address calculation on an
Tom Stellardfd0d86c2014-02-25 21:36:21 +000055 // alloca ptr, then we want to use a higher than normal loop unroll
Matt Arsenault5e1e4312014-04-04 20:13:08 +000056 // threshold. This will give SROA a better chance to eliminate these
57 // allocas.
58 //
59 // Don't use the maximum allowed value here as it will make some
60 // programs way too big.
Matt Arsenaultc8244582014-07-25 23:02:42 +000061 UP.Threshold = 800;
Tom Stellard8cce9bd2014-01-23 18:49:28 +000062 }
63 }
64 }
65}
Matt Arsenault3dd43fc2014-07-18 06:07:13 +000066
Chandler Carruth705b1852015-01-31 03:43:40 +000067unsigned AMDGPUTTIImpl::getNumberOfRegisters(bool Vec) {
Matt Arsenaulta93441f2014-07-19 18:15:16 +000068 if (Vec)
69 return 0;
70
71 // Number of VGPRs on SI.
72 if (ST->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS)
73 return 256;
74
75 return 4 * 128; // XXX - 4 channels. Should these count as vector instead?
76}
77
Matt Arsenault4339b3f2015-12-24 05:14:55 +000078unsigned AMDGPUTTIImpl::getRegisterBitWidth(bool Vector) {
79 return Vector ? 0 : 32;
80}
Matt Arsenaulta93441f2014-07-19 18:15:16 +000081
Wei Mi062c7442015-05-06 17:12:25 +000082unsigned AMDGPUTTIImpl::getMaxInterleaveFactor(unsigned VF) {
Matt Arsenaulta93441f2014-07-19 18:15:16 +000083 // Semi-arbitrary large amount.
84 return 64;
85}
Matt Arsenaulte830f542015-12-01 19:08:39 +000086
Matt Arsenaulte05ff152015-12-16 18:37:19 +000087unsigned AMDGPUTTIImpl::getCFInstrCost(unsigned Opcode) {
88 // XXX - For some reason this isn't called for switch.
89 switch (Opcode) {
90 case Instruction::Br:
91 case Instruction::Ret:
92 return 10;
93 default:
94 return BaseT::getCFInstrCost(Opcode);
95 }
96}
97
Matt Arsenaulte830f542015-12-01 19:08:39 +000098int AMDGPUTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
99 unsigned Index) {
100 switch (Opcode) {
101 case Instruction::ExtractElement:
Matt Arsenault59767ce2016-03-25 00:14:11 +0000102 case Instruction::InsertElement:
103 // Extracts are just reads of a subregister, so are free. Inserts are
104 // considered free because we don't want to have any cost for scalarizing
105 // operations, and we don't have to copy into a different register class.
106
Matt Arsenaulte830f542015-12-01 19:08:39 +0000107 // Dynamic indexing isn't free and is best avoided.
108 return Index == ~0u ? 2 : 0;
109 default:
110 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
111 }
112}
Tom Stellarddbe374b2015-12-15 18:04:38 +0000113
114static bool isIntrinsicSourceOfDivergence(const TargetIntrinsicInfo *TII,
115 const IntrinsicInst *I) {
116 switch (I->getIntrinsicID()) {
117 default:
118 return false;
119 case Intrinsic::not_intrinsic:
120 // This means we have an intrinsic that isn't defined in
121 // IntrinsicsAMDGPU.td
122 break;
123
Matt Arsenaultfe26def2016-02-11 05:32:51 +0000124 case Intrinsic::amdgcn_workitem_id_x:
125 case Intrinsic::amdgcn_workitem_id_y:
126 case Intrinsic::amdgcn_workitem_id_z:
Tom Stellarddbe374b2015-12-15 18:04:38 +0000127 case Intrinsic::amdgcn_interp_p1:
128 case Intrinsic::amdgcn_interp_p2:
129 case Intrinsic::amdgcn_mbcnt_hi:
130 case Intrinsic::amdgcn_mbcnt_lo:
131 case Intrinsic::r600_read_tidig_x:
132 case Intrinsic::r600_read_tidig_y:
133 case Intrinsic::r600_read_tidig_z:
Nicolai Haehnle74127fe82016-03-14 15:37:18 +0000134 case Intrinsic::amdgcn_image_atomic_swap:
135 case Intrinsic::amdgcn_image_atomic_add:
136 case Intrinsic::amdgcn_image_atomic_sub:
137 case Intrinsic::amdgcn_image_atomic_smin:
138 case Intrinsic::amdgcn_image_atomic_umin:
139 case Intrinsic::amdgcn_image_atomic_smax:
140 case Intrinsic::amdgcn_image_atomic_umax:
141 case Intrinsic::amdgcn_image_atomic_and:
142 case Intrinsic::amdgcn_image_atomic_or:
143 case Intrinsic::amdgcn_image_atomic_xor:
144 case Intrinsic::amdgcn_image_atomic_inc:
145 case Intrinsic::amdgcn_image_atomic_dec:
146 case Intrinsic::amdgcn_image_atomic_cmpswap:
Nicolai Haehnlead636382016-03-18 16:24:31 +0000147 case Intrinsic::amdgcn_buffer_atomic_swap:
148 case Intrinsic::amdgcn_buffer_atomic_add:
149 case Intrinsic::amdgcn_buffer_atomic_sub:
150 case Intrinsic::amdgcn_buffer_atomic_smin:
151 case Intrinsic::amdgcn_buffer_atomic_umin:
152 case Intrinsic::amdgcn_buffer_atomic_smax:
153 case Intrinsic::amdgcn_buffer_atomic_umax:
154 case Intrinsic::amdgcn_buffer_atomic_and:
155 case Intrinsic::amdgcn_buffer_atomic_or:
156 case Intrinsic::amdgcn_buffer_atomic_xor:
157 case Intrinsic::amdgcn_buffer_atomic_cmpswap:
Tom Stellarddbe374b2015-12-15 18:04:38 +0000158 return true;
159 }
160
161 StringRef Name = I->getCalledFunction()->getName();
162 switch (TII->lookupName((const char *)Name.bytes_begin(), Name.size())) {
163 default:
164 return false;
165 case AMDGPUIntrinsic::SI_tid:
166 case AMDGPUIntrinsic::SI_fs_interp:
167 return true;
168 }
169}
170
171static bool isArgPassedInSGPR(const Argument *A) {
172 const Function *F = A->getParent();
173 unsigned ShaderType = AMDGPU::getShaderType(*F);
174
175 // Arguments to compute shaders are never a source of divergence.
176 if (ShaderType == ShaderType::COMPUTE)
177 return true;
178
Tom Stellardffc1a5a2015-12-19 02:54:15 +0000179 // For non-compute shaders, SGPR inputs are marked with either inreg or byval.
180 if (F->getAttributes().hasAttribute(A->getArgNo() + 1, Attribute::InReg) ||
181 F->getAttributes().hasAttribute(A->getArgNo() + 1, Attribute::ByVal))
Tom Stellarddbe374b2015-12-15 18:04:38 +0000182 return true;
183
Tom Stellardffc1a5a2015-12-19 02:54:15 +0000184 // Everything else is in VGPRs.
185 return false;
Tom Stellarddbe374b2015-12-15 18:04:38 +0000186}
187
188///
189/// \returns true if the result of the value could potentially be
190/// different across workitems in a wavefront.
191bool AMDGPUTTIImpl::isSourceOfDivergence(const Value *V) const {
192
193 if (const Argument *A = dyn_cast<Argument>(V))
194 return !isArgPassedInSGPR(A);
195
196 // Loads from the private address space are divergent, because threads
197 // can execute the load instruction with the same inputs and get different
198 // results.
199 //
200 // All other loads are not divergent, because if threads issue loads with the
201 // same arguments, they will always get the same result.
202 if (const LoadInst *Load = dyn_cast<LoadInst>(V))
203 return Load->getPointerAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS;
204
Nicolai Haehnle79cad852016-03-17 16:21:59 +0000205 // Atomics are divergent because they are executed sequentially: when an
206 // atomic operation refers to the same address in each thread, then each
207 // thread after the first sees the value written by the previous thread as
208 // original value.
209 if (isa<AtomicRMWInst>(V) || isa<AtomicCmpXchgInst>(V))
210 return true;
211
Tom Stellarddbe374b2015-12-15 18:04:38 +0000212 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) {
213 const TargetMachine &TM = getTLI()->getTargetMachine();
214 return isIntrinsicSourceOfDivergence(TM.getIntrinsicInfo(), Intrinsic);
215 }
216
217 // Assume all function calls are a source of divergence.
218 if (isa<CallInst>(V) || isa<InvokeInst>(V))
219 return true;
220
221 return false;
222}