blob: eb9e837d2914f99a10873756774d5abfe8be911d [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 Stellard8b1e0212013-07-27 00:01:07 +000024#include "llvm/Support/Debug.h"
Tom Stellard8b1e0212013-07-27 00:01:07 +000025#include "llvm/Target/CostTable.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000026#include "llvm/Target/TargetLowering.h"
Tom Stellard8b1e0212013-07-27 00:01:07 +000027using namespace llvm;
28
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "AMDGPUtti"
30
Chandler Carruthab5cb362015-02-01 14:31:23 +000031void AMDGPUTTIImpl::getUnrollingPreferences(Loop *L,
Chandler Carruth705b1852015-01-31 03:43:40 +000032 TTI::UnrollingPreferences &UP) {
Matt Arsenaultc8244582014-07-25 23:02:42 +000033 UP.Threshold = 300; // Twice the default.
Tom Stellardeea3f702015-02-05 15:32:18 +000034 UP.MaxCount = UINT_MAX;
Matt Arsenaultc8244582014-07-25 23:02:42 +000035 UP.Partial = true;
36
37 // TODO: Do we want runtime unrolling?
38
Matt Arsenaultac6e39c2014-07-17 06:19:06 +000039 for (const BasicBlock *BB : L->getBlocks()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +000040 const DataLayout &DL = BB->getModule()->getDataLayout();
Matt Arsenaultac6e39c2014-07-17 06:19:06 +000041 for (const Instruction &I : *BB) {
42 const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I);
Matt Arsenault5e2b0f52014-07-17 06:13:41 +000043 if (!GEP || GEP->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
Tom Stellard8cce9bd2014-01-23 18:49:28 +000044 continue;
Matt Arsenaultac6e39c2014-07-17 06:19:06 +000045
Tom Stellard8cce9bd2014-01-23 18:49:28 +000046 const Value *Ptr = GEP->getPointerOperand();
Mehdi Aminia28d91d2015-03-10 02:37:25 +000047 const AllocaInst *Alloca =
48 dyn_cast<AllocaInst>(GetUnderlyingObject(Ptr, DL));
Tom Stellard8cce9bd2014-01-23 18:49:28 +000049 if (Alloca) {
50 // We want to do whatever we can to limit the number of alloca
51 // instructions that make it through to the code generator. allocas
52 // require us to use indirect addressing, which is slow and prone to
53 // compiler bugs. If this loop does an address calculation on an
Tom Stellardfd0d86c2014-02-25 21:36:21 +000054 // alloca ptr, then we want to use a higher than normal loop unroll
Matt Arsenault5e1e4312014-04-04 20:13:08 +000055 // threshold. This will give SROA a better chance to eliminate these
56 // allocas.
57 //
58 // Don't use the maximum allowed value here as it will make some
59 // programs way too big.
Matt Arsenaultc8244582014-07-25 23:02:42 +000060 UP.Threshold = 800;
Tom Stellard8cce9bd2014-01-23 18:49:28 +000061 }
62 }
63 }
64}
Matt Arsenault3dd43fc2014-07-18 06:07:13 +000065
Chandler Carruth705b1852015-01-31 03:43:40 +000066unsigned AMDGPUTTIImpl::getNumberOfRegisters(bool Vec) {
Matt Arsenaulta93441f2014-07-19 18:15:16 +000067 if (Vec)
68 return 0;
69
70 // Number of VGPRs on SI.
71 if (ST->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS)
72 return 256;
73
74 return 4 * 128; // XXX - 4 channels. Should these count as vector instead?
75}
76
Chandler Carruth705b1852015-01-31 03:43:40 +000077unsigned AMDGPUTTIImpl::getRegisterBitWidth(bool) { return 32; }
Matt Arsenaulta93441f2014-07-19 18:15:16 +000078
Wei Mi062c7442015-05-06 17:12:25 +000079unsigned AMDGPUTTIImpl::getMaxInterleaveFactor(unsigned VF) {
Matt Arsenaulta93441f2014-07-19 18:15:16 +000080 // Semi-arbitrary large amount.
81 return 64;
82}
Matt Arsenaulte830f542015-12-01 19:08:39 +000083
Matt Arsenaulte05ff152015-12-16 18:37:19 +000084unsigned AMDGPUTTIImpl::getCFInstrCost(unsigned Opcode) {
85 // XXX - For some reason this isn't called for switch.
86 switch (Opcode) {
87 case Instruction::Br:
88 case Instruction::Ret:
89 return 10;
90 default:
91 return BaseT::getCFInstrCost(Opcode);
92 }
93}
94
Matt Arsenaulte830f542015-12-01 19:08:39 +000095int AMDGPUTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
96 unsigned Index) {
97 switch (Opcode) {
98 case Instruction::ExtractElement:
99 // Dynamic indexing isn't free and is best avoided.
100 return Index == ~0u ? 2 : 0;
101 default:
102 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
103 }
104}
Tom Stellarddbe374b2015-12-15 18:04:38 +0000105
106static bool isIntrinsicSourceOfDivergence(const TargetIntrinsicInfo *TII,
107 const IntrinsicInst *I) {
108 switch (I->getIntrinsicID()) {
109 default:
110 return false;
111 case Intrinsic::not_intrinsic:
112 // This means we have an intrinsic that isn't defined in
113 // IntrinsicsAMDGPU.td
114 break;
115
116 case Intrinsic::amdgcn_interp_p1:
117 case Intrinsic::amdgcn_interp_p2:
118 case Intrinsic::amdgcn_mbcnt_hi:
119 case Intrinsic::amdgcn_mbcnt_lo:
120 case Intrinsic::r600_read_tidig_x:
121 case Intrinsic::r600_read_tidig_y:
122 case Intrinsic::r600_read_tidig_z:
123 return true;
124 }
125
126 StringRef Name = I->getCalledFunction()->getName();
127 switch (TII->lookupName((const char *)Name.bytes_begin(), Name.size())) {
128 default:
129 return false;
130 case AMDGPUIntrinsic::SI_tid:
131 case AMDGPUIntrinsic::SI_fs_interp:
132 return true;
133 }
134}
135
136static bool isArgPassedInSGPR(const Argument *A) {
137 const Function *F = A->getParent();
138 unsigned ShaderType = AMDGPU::getShaderType(*F);
139
140 // Arguments to compute shaders are never a source of divergence.
141 if (ShaderType == ShaderType::COMPUTE)
142 return true;
143
144 // For non-compute shaders, the inreg attribute is used to mark inputs,
145 // which pre-loaded into SGPRs.
146 if (F->getAttributes().hasAttribute(A->getArgNo(), Attribute::InReg))
147 return true;
148
149 // For non-compute shaders, 32-bit values are pre-loaded into vgprs, all
150 // other value types use SGPRS.
151 return !A->getType()->isIntegerTy(32) && !A->getType()->isFloatTy();
152}
153
154///
155/// \returns true if the result of the value could potentially be
156/// different across workitems in a wavefront.
157bool AMDGPUTTIImpl::isSourceOfDivergence(const Value *V) const {
158
159 if (const Argument *A = dyn_cast<Argument>(V))
160 return !isArgPassedInSGPR(A);
161
162 // Loads from the private address space are divergent, because threads
163 // can execute the load instruction with the same inputs and get different
164 // results.
165 //
166 // All other loads are not divergent, because if threads issue loads with the
167 // same arguments, they will always get the same result.
168 if (const LoadInst *Load = dyn_cast<LoadInst>(V))
169 return Load->getPointerAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS;
170
171 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) {
172 const TargetMachine &TM = getTLI()->getTargetMachine();
173 return isIntrinsicSourceOfDivergence(TM.getIntrinsicInfo(), Intrinsic);
174 }
175
176 // Assume all function calls are a source of divergence.
177 if (isa<CallInst>(V) || isa<InvokeInst>(V))
178 return true;
179
180 return false;
181}