blob: bf560a94b310cee956531805968432d86011b6dc [file] [log] [blame]
Eugene Zelenkod16eff82017-08-08 23:53:55 +00001//===- AMDGPUTargetTransformInfo.cpp - AMDGPU specific TTI pass -----------===//
Tom Stellard8b1e0212013-07-27 00:01:07 +00002//
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"
Eugene Zelenkod16eff82017-08-08 23:53:55 +000019#include "AMDGPUSubtarget.h"
Alexander Timofeev2e5eece2018-03-05 15:12:21 +000020#include "Utils/AMDGPUBaseInfo.h"
Eugene Zelenkod16eff82017-08-08 23:53:55 +000021#include "llvm/ADT/STLExtras.h"
Tom Stellard8cce9bd2014-01-23 18:49:28 +000022#include "llvm/Analysis/LoopInfo.h"
Tom Stellard8b1e0212013-07-27 00:01:07 +000023#include "llvm/Analysis/TargetTransformInfo.h"
Tom Stellard8cce9bd2014-01-23 18:49:28 +000024#include "llvm/Analysis/ValueTracking.h"
Eugene Zelenkod16eff82017-08-08 23:53:55 +000025#include "llvm/CodeGen/ISDOpcodes.h"
Craig Topper2fa14362018-03-29 17:21:10 +000026#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenkod16eff82017-08-08 23:53:55 +000027#include "llvm/IR/Argument.h"
28#include "llvm/IR/Attributes.h"
29#include "llvm/IR/BasicBlock.h"
30#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/DerivedTypes.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/Instruction.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000037#include "llvm/IR/Module.h"
Matt Arsenault376f1bd2017-08-31 05:47:00 +000038#include "llvm/IR/PatternMatch.h"
Eugene Zelenkod16eff82017-08-08 23:53:55 +000039#include "llvm/IR/Type.h"
40#include "llvm/IR/Value.h"
41#include "llvm/MC/SubtargetFeature.h"
42#include "llvm/Support/Casting.h"
43#include "llvm/Support/CommandLine.h"
Tom Stellard8b1e0212013-07-27 00:01:07 +000044#include "llvm/Support/Debug.h"
Eugene Zelenkod16eff82017-08-08 23:53:55 +000045#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000046#include "llvm/Support/MachineValueType.h"
Eugene Zelenkod16eff82017-08-08 23:53:55 +000047#include "llvm/Support/raw_ostream.h"
48#include "llvm/Target/TargetMachine.h"
49#include <algorithm>
50#include <cassert>
51#include <limits>
52#include <utility>
53
Tom Stellard8b1e0212013-07-27 00:01:07 +000054using namespace llvm;
55
Chandler Carruth84e68b22014-04-22 02:41:26 +000056#define DEBUG_TYPE "AMDGPUtti"
57
Stanislav Mekhanoshinf29602d2017-02-03 02:20:05 +000058static cl::opt<unsigned> UnrollThresholdPrivate(
59 "amdgpu-unroll-threshold-private",
60 cl::desc("Unroll threshold for AMDGPU if private memory used in a loop"),
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +000061 cl::init(2500), cl::Hidden);
Matt Arsenault96518132016-03-25 01:00:32 +000062
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +000063static cl::opt<unsigned> UnrollThresholdLocal(
64 "amdgpu-unroll-threshold-local",
65 cl::desc("Unroll threshold for AMDGPU if local memory used in a loop"),
66 cl::init(1000), cl::Hidden);
67
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +000068static cl::opt<unsigned> UnrollThresholdIf(
69 "amdgpu-unroll-threshold-if",
70 cl::desc("Unroll threshold increment for AMDGPU for each if statement inside loop"),
71 cl::init(150), cl::Hidden);
72
73static bool dependsOnLocalPhi(const Loop *L, const Value *Cond,
74 unsigned Depth = 0) {
75 const Instruction *I = dyn_cast<Instruction>(Cond);
76 if (!I)
77 return false;
78
79 for (const Value *V : I->operand_values()) {
80 if (!L->contains(I))
81 continue;
82 if (const PHINode *PHI = dyn_cast<PHINode>(V)) {
Eugene Zelenkod16eff82017-08-08 23:53:55 +000083 if (llvm::none_of(L->getSubLoops(), [PHI](const Loop* SubLoop) {
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +000084 return SubLoop->contains(PHI); }))
85 return true;
86 } else if (Depth < 10 && dependsOnLocalPhi(L, V, Depth+1))
87 return true;
88 }
89 return false;
90}
91
Geoff Berry66d9bdb2017-06-28 15:53:17 +000092void AMDGPUTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
Chandler Carruth705b1852015-01-31 03:43:40 +000093 TTI::UnrollingPreferences &UP) {
Matt Arsenaultc8244582014-07-25 23:02:42 +000094 UP.Threshold = 300; // Twice the default.
Eugene Zelenkod16eff82017-08-08 23:53:55 +000095 UP.MaxCount = std::numeric_limits<unsigned>::max();
Matt Arsenaultc8244582014-07-25 23:02:42 +000096 UP.Partial = true;
97
98 // TODO: Do we want runtime unrolling?
99
Stanislav Mekhanoshinf29602d2017-02-03 02:20:05 +0000100 // Maximum alloca size than can fit registers. Reserve 16 registers.
101 const unsigned MaxAlloca = (256 - 16) * 4;
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000102 unsigned ThresholdPrivate = UnrollThresholdPrivate;
103 unsigned ThresholdLocal = UnrollThresholdLocal;
104 unsigned MaxBoost = std::max(ThresholdPrivate, ThresholdLocal);
105 AMDGPUAS ASST = ST->getAMDGPUAS();
Matt Arsenaultac6e39c2014-07-17 06:19:06 +0000106 for (const BasicBlock *BB : L->getBlocks()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000107 const DataLayout &DL = BB->getModule()->getDataLayout();
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000108 unsigned LocalGEPsSeen = 0;
109
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000110 if (llvm::any_of(L->getSubLoops(), [BB](const Loop* SubLoop) {
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +0000111 return SubLoop->contains(BB); }))
112 continue; // Block belongs to an inner loop.
113
Matt Arsenaultac6e39c2014-07-17 06:19:06 +0000114 for (const Instruction &I : *BB) {
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +0000115 // Unroll a loop which contains an "if" statement whose condition
116 // defined by a PHI belonging to the loop. This may help to eliminate
117 // if region and potentially even PHI itself, saving on both divergence
118 // and registers used for the PHI.
119 // Add a small bonus for each of such "if" statements.
120 if (const BranchInst *Br = dyn_cast<BranchInst>(&I)) {
121 if (UP.Threshold < MaxBoost && Br->isConditional()) {
122 if (L->isLoopExiting(Br->getSuccessor(0)) ||
123 L->isLoopExiting(Br->getSuccessor(1)))
124 continue;
125 if (dependsOnLocalPhi(L, Br->getCondition())) {
126 UP.Threshold += UnrollThresholdIf;
127 DEBUG(dbgs() << "Set unroll threshold " << UP.Threshold
128 << " for loop:\n" << *L << " due to " << *Br << '\n');
129 if (UP.Threshold >= MaxBoost)
130 return;
131 }
132 }
133 continue;
134 }
135
Matt Arsenaultac6e39c2014-07-17 06:19:06 +0000136 const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I);
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000137 if (!GEP)
Tom Stellard8cce9bd2014-01-23 18:49:28 +0000138 continue;
Matt Arsenaultac6e39c2014-07-17 06:19:06 +0000139
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000140 unsigned AS = GEP->getAddressSpace();
141 unsigned Threshold = 0;
142 if (AS == ASST.PRIVATE_ADDRESS)
143 Threshold = ThresholdPrivate;
144 else if (AS == ASST.LOCAL_ADDRESS)
145 Threshold = ThresholdLocal;
146 else
147 continue;
148
149 if (UP.Threshold >= Threshold)
150 continue;
151
152 if (AS == ASST.PRIVATE_ADDRESS) {
153 const Value *Ptr = GEP->getPointerOperand();
154 const AllocaInst *Alloca =
155 dyn_cast<AllocaInst>(GetUnderlyingObject(Ptr, DL));
156 if (!Alloca || !Alloca->isStaticAlloca())
157 continue;
Stanislav Mekhanoshinf29602d2017-02-03 02:20:05 +0000158 Type *Ty = Alloca->getAllocatedType();
159 unsigned AllocaSize = Ty->isSized() ? DL.getTypeAllocSize(Ty) : 0;
160 if (AllocaSize > MaxAlloca)
161 continue;
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000162 } else if (AS == ASST.LOCAL_ADDRESS) {
163 LocalGEPsSeen++;
164 // Inhibit unroll for local memory if we have seen addressing not to
165 // a variable, most likely we will be unable to combine it.
166 // Do not unroll too deep inner loops for local memory to give a chance
167 // to unroll an outer loop for a more important reason.
168 if (LocalGEPsSeen > 1 || L->getLoopDepth() > 2 ||
169 (!isa<GlobalVariable>(GEP->getPointerOperand()) &&
170 !isa<Argument>(GEP->getPointerOperand())))
171 continue;
172 }
Stanislav Mekhanoshinf29602d2017-02-03 02:20:05 +0000173
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000174 // Check if GEP depends on a value defined by this loop itself.
175 bool HasLoopDef = false;
176 for (const Value *Op : GEP->operands()) {
177 const Instruction *Inst = dyn_cast<Instruction>(Op);
178 if (!Inst || L->isLoopInvariant(Op))
Stanislav Mekhanoshinf29602d2017-02-03 02:20:05 +0000179 continue;
180
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000181 if (llvm::any_of(L->getSubLoops(), [Inst](const Loop* SubLoop) {
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000182 return SubLoop->contains(Inst); }))
183 continue;
184 HasLoopDef = true;
185 break;
Tom Stellard8cce9bd2014-01-23 18:49:28 +0000186 }
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000187 if (!HasLoopDef)
188 continue;
189
190 // We want to do whatever we can to limit the number of alloca
191 // instructions that make it through to the code generator. allocas
192 // require us to use indirect addressing, which is slow and prone to
193 // compiler bugs. If this loop does an address calculation on an
194 // alloca ptr, then we want to use a higher than normal loop unroll
195 // threshold. This will give SROA a better chance to eliminate these
196 // allocas.
197 //
198 // We also want to have more unrolling for local memory to let ds
199 // instructions with different offsets combine.
200 //
201 // Don't use the maximum allowed value here as it will make some
202 // programs way too big.
203 UP.Threshold = Threshold;
204 DEBUG(dbgs() << "Set unroll threshold " << Threshold << " for loop:\n"
205 << *L << " due to " << *GEP << '\n');
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +0000206 if (UP.Threshold >= MaxBoost)
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000207 return;
Tom Stellard8cce9bd2014-01-23 18:49:28 +0000208 }
209 }
210}
Matt Arsenault3dd43fc2014-07-18 06:07:13 +0000211
Matt Arsenault67cd3472017-06-20 20:38:06 +0000212unsigned AMDGPUTTIImpl::getHardwareNumberOfRegisters(bool Vec) const {
213 // The concept of vector registers doesn't really exist. Some packed vector
214 // operations operate on the normal 32-bit registers.
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000215
216 // Number of VGPRs on SI.
217 if (ST->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS)
218 return 256;
219
220 return 4 * 128; // XXX - 4 channels. Should these count as vector instead?
221}
222
Matt Arsenault67cd3472017-06-20 20:38:06 +0000223unsigned AMDGPUTTIImpl::getNumberOfRegisters(bool Vec) const {
224 // This is really the number of registers to fill when vectorizing /
225 // interleaving loops, so we lie to avoid trying to use all registers.
226 return getHardwareNumberOfRegisters(Vec) >> 3;
227}
228
Daniel Neilsonc0112ae2017-06-12 14:22:21 +0000229unsigned AMDGPUTTIImpl::getRegisterBitWidth(bool Vector) const {
Matt Arsenault67cd3472017-06-20 20:38:06 +0000230 return 32;
231}
232
233unsigned AMDGPUTTIImpl::getMinVectorRegisterBitWidth() const {
234 return 32;
Matt Arsenault4339b3f2015-12-24 05:14:55 +0000235}
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000236
Farhana Aleen89196642018-03-07 17:09:18 +0000237unsigned AMDGPUTTIImpl::getLoadVectorFactor(unsigned VF, unsigned LoadSize,
238 unsigned ChainSizeInBytes,
239 VectorType *VecTy) const {
240 unsigned VecRegBitWidth = VF * LoadSize;
241 if (VecRegBitWidth > 128 && VecTy->getScalarSizeInBits() < 32)
242 // TODO: Support element-size less than 32bit?
243 return 128 / LoadSize;
244
245 return VF;
246}
247
248unsigned AMDGPUTTIImpl::getStoreVectorFactor(unsigned VF, unsigned StoreSize,
249 unsigned ChainSizeInBytes,
250 VectorType *VecTy) const {
251 unsigned VecRegBitWidth = VF * StoreSize;
252 if (VecRegBitWidth > 128)
253 return 128 / StoreSize;
254
255 return VF;
256}
257
Volkan Keles1c386812016-10-03 10:31:34 +0000258unsigned AMDGPUTTIImpl::getLoadStoreVecRegBitWidth(unsigned AddrSpace) const {
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000259 AMDGPUAS AS = ST->getAMDGPUAS();
260 if (AddrSpace == AS.GLOBAL_ADDRESS ||
261 AddrSpace == AS.CONSTANT_ADDRESS ||
Farhana Aleen89196642018-03-07 17:09:18 +0000262 AddrSpace == AS.CONSTANT_ADDRESS_32BIT) {
263 if (ST->getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
264 return 128;
265 return 512;
266 }
267
Marek Olsak52b033b2018-04-09 16:56:32 +0000268 if (AddrSpace == AS.FLAT_ADDRESS)
Farhana Aleena7cb3112018-03-09 17:41:39 +0000269 return 128;
270
Marek Olsak52b033b2018-04-09 16:56:32 +0000271 if (AddrSpace == AS.LOCAL_ADDRESS ||
272 AddrSpace == AS.REGION_ADDRESS)
273 return ST->useDS128() ? 128 : 64;
274
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000275 if (AddrSpace == AS.PRIVATE_ADDRESS)
Matt Arsenault0994bd52016-07-01 00:56:27 +0000276 return 8 * ST->getMaxPrivateElementSize();
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000277
278 if (ST->getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS &&
279 (AddrSpace == AS.PARAM_D_ADDRESS ||
280 AddrSpace == AS.PARAM_I_ADDRESS ||
Farhana Aleen89196642018-03-07 17:09:18 +0000281 (AddrSpace >= AS.CONSTANT_BUFFER_0 &&
282 AddrSpace <= AS.CONSTANT_BUFFER_15)))
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000283 return 128;
284 llvm_unreachable("unhandled address space");
Matt Arsenault0994bd52016-07-01 00:56:27 +0000285}
286
Matt Arsenaultf0a88db2017-02-23 03:58:53 +0000287bool AMDGPUTTIImpl::isLegalToVectorizeMemChain(unsigned ChainSizeInBytes,
288 unsigned Alignment,
289 unsigned AddrSpace) const {
290 // We allow vectorization of flat stores, even though we may need to decompose
291 // them later if they may access private memory. We don't have enough context
292 // here, and legalization can handle it.
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000293 if (AddrSpace == ST->getAMDGPUAS().PRIVATE_ADDRESS) {
Matt Arsenaultf0a88db2017-02-23 03:58:53 +0000294 return (Alignment >= 4 || ST->hasUnalignedScratchAccess()) &&
295 ChainSizeInBytes <= ST->getMaxPrivateElementSize();
296 }
297 return true;
298}
299
300bool AMDGPUTTIImpl::isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
301 unsigned Alignment,
302 unsigned AddrSpace) const {
303 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
304}
305
306bool AMDGPUTTIImpl::isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
307 unsigned Alignment,
308 unsigned AddrSpace) const {
309 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
310}
311
Wei Mi062c7442015-05-06 17:12:25 +0000312unsigned AMDGPUTTIImpl::getMaxInterleaveFactor(unsigned VF) {
Changpeng Fang1be9b9f2017-03-09 00:07:00 +0000313 // Disable unrolling if the loop is not vectorized.
Matt Arsenault67cd3472017-06-20 20:38:06 +0000314 // TODO: Enable this again.
Changpeng Fang1be9b9f2017-03-09 00:07:00 +0000315 if (VF == 1)
316 return 1;
317
Matt Arsenault67cd3472017-06-20 20:38:06 +0000318 return 8;
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000319}
Matt Arsenaulte830f542015-12-01 19:08:39 +0000320
Matt Arsenault3e268cc2017-12-11 21:38:43 +0000321bool AMDGPUTTIImpl::getTgtMemIntrinsic(IntrinsicInst *Inst,
322 MemIntrinsicInfo &Info) const {
323 switch (Inst->getIntrinsicID()) {
324 case Intrinsic::amdgcn_atomic_inc:
Daniil Fukalov6e1dc682018-01-26 11:09:38 +0000325 case Intrinsic::amdgcn_atomic_dec:
326 case Intrinsic::amdgcn_ds_fadd:
327 case Intrinsic::amdgcn_ds_fmin:
328 case Intrinsic::amdgcn_ds_fmax: {
Matt Arsenault3e268cc2017-12-11 21:38:43 +0000329 auto *Ordering = dyn_cast<ConstantInt>(Inst->getArgOperand(2));
330 auto *Volatile = dyn_cast<ConstantInt>(Inst->getArgOperand(4));
331 if (!Ordering || !Volatile)
332 return false; // Invalid.
333
334 unsigned OrderingVal = Ordering->getZExtValue();
335 if (OrderingVal > static_cast<unsigned>(AtomicOrdering::SequentiallyConsistent))
336 return false;
337
338 Info.PtrVal = Inst->getArgOperand(0);
339 Info.Ordering = static_cast<AtomicOrdering>(OrderingVal);
340 Info.ReadMem = true;
341 Info.WriteMem = true;
342 Info.IsVolatile = !Volatile->isNullValue();
343 return true;
344 }
345 default:
346 return false;
347 }
348}
349
Matt Arsenault96518132016-03-25 01:00:32 +0000350int AMDGPUTTIImpl::getArithmeticInstrCost(
351 unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info,
352 TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000353 TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args ) {
Matt Arsenault96518132016-03-25 01:00:32 +0000354 EVT OrigTy = TLI->getValueType(DL, Ty);
355 if (!OrigTy.isSimple()) {
356 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
357 Opd1PropInfo, Opd2PropInfo);
358 }
359
360 // Legalize the type.
361 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
362 int ISD = TLI->InstructionOpcodeToISD(Opcode);
363
364 // Because we don't have any legal vector operations, but the legal types, we
365 // need to account for split vectors.
366 unsigned NElts = LT.second.isVector() ?
367 LT.second.getVectorNumElements() : 1;
368
369 MVT::SimpleValueType SLT = LT.second.getScalarType().SimpleTy;
370
371 switch (ISD) {
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000372 case ISD::SHL:
373 case ISD::SRL:
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000374 case ISD::SRA:
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000375 if (SLT == MVT::i64)
376 return get64BitInstrCost() * LT.first * NElts;
377
378 // i32
379 return getFullRateInstrCost() * LT.first * NElts;
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000380 case ISD::ADD:
381 case ISD::SUB:
382 case ISD::AND:
383 case ISD::OR:
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000384 case ISD::XOR:
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000385 if (SLT == MVT::i64){
386 // and, or and xor are typically split into 2 VALU instructions.
387 return 2 * getFullRateInstrCost() * LT.first * NElts;
388 }
389
390 return LT.first * NElts * getFullRateInstrCost();
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000391 case ISD::MUL: {
392 const int QuarterRateCost = getQuarterRateInstrCost();
393 if (SLT == MVT::i64) {
394 const int FullRateCost = getFullRateInstrCost();
395 return (4 * QuarterRateCost + (2 * 2) * FullRateCost) * LT.first * NElts;
396 }
397
398 // i32
399 return QuarterRateCost * NElts * LT.first;
400 }
Matt Arsenault96518132016-03-25 01:00:32 +0000401 case ISD::FADD:
402 case ISD::FSUB:
403 case ISD::FMUL:
404 if (SLT == MVT::f64)
405 return LT.first * NElts * get64BitInstrCost();
406
407 if (SLT == MVT::f32 || SLT == MVT::f16)
408 return LT.first * NElts * getFullRateInstrCost();
409 break;
Matt Arsenault96518132016-03-25 01:00:32 +0000410 case ISD::FDIV:
411 case ISD::FREM:
412 // FIXME: frem should be handled separately. The fdiv in it is most of it,
413 // but the current lowering is also not entirely correct.
414 if (SLT == MVT::f64) {
415 int Cost = 4 * get64BitInstrCost() + 7 * getQuarterRateInstrCost();
Matt Arsenault96518132016-03-25 01:00:32 +0000416 // Add cost of workaround.
417 if (ST->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS)
418 Cost += 3 * getFullRateInstrCost();
419
420 return LT.first * Cost * NElts;
421 }
422
Matt Arsenault376f1bd2017-08-31 05:47:00 +0000423 if (!Args.empty() && match(Args[0], PatternMatch::m_FPOne())) {
424 // TODO: This is more complicated, unsafe flags etc.
425 if ((SLT == MVT::f32 && !ST->hasFP32Denormals()) ||
426 (SLT == MVT::f16 && ST->has16BitInsts())) {
427 return LT.first * getQuarterRateInstrCost() * NElts;
428 }
429 }
430
431 if (SLT == MVT::f16 && ST->has16BitInsts()) {
432 // 2 x v_cvt_f32_f16
433 // f32 rcp
434 // f32 fmul
435 // v_cvt_f16_f32
436 // f16 div_fixup
437 int Cost = 4 * getFullRateInstrCost() + 2 * getQuarterRateInstrCost();
438 return LT.first * Cost * NElts;
439 }
440
Matt Arsenault96518132016-03-25 01:00:32 +0000441 if (SLT == MVT::f32 || SLT == MVT::f16) {
Matt Arsenault96518132016-03-25 01:00:32 +0000442 int Cost = 7 * getFullRateInstrCost() + 1 * getQuarterRateInstrCost();
Matt Arsenault376f1bd2017-08-31 05:47:00 +0000443
444 if (!ST->hasFP32Denormals()) {
445 // FP mode switches.
446 Cost += 2 * getFullRateInstrCost();
447 }
448
Matt Arsenault96518132016-03-25 01:00:32 +0000449 return LT.first * NElts * Cost;
450 }
Matt Arsenault96518132016-03-25 01:00:32 +0000451 break;
452 default:
453 break;
454 }
455
456 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
457 Opd1PropInfo, Opd2PropInfo);
458}
459
Matt Arsenaulte05ff152015-12-16 18:37:19 +0000460unsigned AMDGPUTTIImpl::getCFInstrCost(unsigned Opcode) {
461 // XXX - For some reason this isn't called for switch.
462 switch (Opcode) {
463 case Instruction::Br:
464 case Instruction::Ret:
465 return 10;
466 default:
467 return BaseT::getCFInstrCost(Opcode);
468 }
469}
470
Matt Arsenaulte830f542015-12-01 19:08:39 +0000471int AMDGPUTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
472 unsigned Index) {
473 switch (Opcode) {
474 case Instruction::ExtractElement:
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000475 case Instruction::InsertElement: {
476 unsigned EltSize
477 = DL.getTypeSizeInBits(cast<VectorType>(ValTy)->getElementType());
478 if (EltSize < 32) {
479 if (EltSize == 16 && Index == 0 && ST->has16BitInsts())
480 return 0;
481 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
482 }
483
Matt Arsenault59767ce2016-03-25 00:14:11 +0000484 // Extracts are just reads of a subregister, so are free. Inserts are
485 // considered free because we don't want to have any cost for scalarizing
486 // operations, and we don't have to copy into a different register class.
487
Matt Arsenaulte830f542015-12-01 19:08:39 +0000488 // Dynamic indexing isn't free and is best avoided.
489 return Index == ~0u ? 2 : 0;
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000490 }
Matt Arsenaulte830f542015-12-01 19:08:39 +0000491 default:
492 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
493 }
494}
Tom Stellarddbe374b2015-12-15 18:04:38 +0000495
Alexander Timofeev2e5eece2018-03-05 15:12:21 +0000496
Tom Stellarddbe374b2015-12-15 18:04:38 +0000497
498static bool isArgPassedInSGPR(const Argument *A) {
499 const Function *F = A->getParent();
Tom Stellarddbe374b2015-12-15 18:04:38 +0000500
501 // Arguments to compute shaders are never a source of divergence.
Matt Arsenault4c1ecde2017-04-19 17:42:34 +0000502 CallingConv::ID CC = F->getCallingConv();
503 switch (CC) {
504 case CallingConv::AMDGPU_KERNEL:
505 case CallingConv::SPIR_KERNEL:
Tom Stellarddbe374b2015-12-15 18:04:38 +0000506 return true;
Matt Arsenault4c1ecde2017-04-19 17:42:34 +0000507 case CallingConv::AMDGPU_VS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000508 case CallingConv::AMDGPU_LS:
Marek Olsaka302a7362017-05-02 15:41:10 +0000509 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000510 case CallingConv::AMDGPU_ES:
Matt Arsenault4c1ecde2017-04-19 17:42:34 +0000511 case CallingConv::AMDGPU_GS:
512 case CallingConv::AMDGPU_PS:
513 case CallingConv::AMDGPU_CS:
514 // For non-compute shaders, SGPR inputs are marked with either inreg or byval.
515 // Everything else is in VGPRs.
516 return F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::InReg) ||
517 F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::ByVal);
518 default:
519 // TODO: Should calls support inreg for SGPR inputs?
520 return false;
521 }
Tom Stellarddbe374b2015-12-15 18:04:38 +0000522}
523
Tom Stellarddbe374b2015-12-15 18:04:38 +0000524/// \returns true if the result of the value could potentially be
525/// different across workitems in a wavefront.
526bool AMDGPUTTIImpl::isSourceOfDivergence(const Value *V) const {
Tom Stellarddbe374b2015-12-15 18:04:38 +0000527 if (const Argument *A = dyn_cast<Argument>(V))
528 return !isArgPassedInSGPR(A);
529
530 // Loads from the private address space are divergent, because threads
531 // can execute the load instruction with the same inputs and get different
532 // results.
533 //
534 // All other loads are not divergent, because if threads issue loads with the
535 // same arguments, they will always get the same result.
536 if (const LoadInst *Load = dyn_cast<LoadInst>(V))
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000537 return Load->getPointerAddressSpace() == ST->getAMDGPUAS().PRIVATE_ADDRESS;
Tom Stellarddbe374b2015-12-15 18:04:38 +0000538
Nicolai Haehnle79cad852016-03-17 16:21:59 +0000539 // Atomics are divergent because they are executed sequentially: when an
540 // atomic operation refers to the same address in each thread, then each
541 // thread after the first sees the value written by the previous thread as
542 // original value.
543 if (isa<AtomicRMWInst>(V) || isa<AtomicCmpXchgInst>(V))
544 return true;
545
Matt Arsenaultd2c8a332017-02-16 02:01:13 +0000546 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V))
Alexander Timofeev2e5eece2018-03-05 15:12:21 +0000547 return AMDGPU::isIntrinsicSourceOfDivergence(Intrinsic->getIntrinsicID());
Tom Stellarddbe374b2015-12-15 18:04:38 +0000548
549 // Assume all function calls are a source of divergence.
550 if (isa<CallInst>(V) || isa<InvokeInst>(V))
551 return true;
552
553 return false;
554}
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000555
Alexander Timofeev0f9c84c2017-06-15 19:33:10 +0000556bool AMDGPUTTIImpl::isAlwaysUniform(const Value *V) const {
557 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) {
558 switch (Intrinsic->getIntrinsicID()) {
559 default:
560 return false;
561 case Intrinsic::amdgcn_readfirstlane:
562 case Intrinsic::amdgcn_readlane:
563 return true;
564 }
565 }
566 return false;
567}
568
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000569unsigned AMDGPUTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
570 Type *SubTp) {
571 if (ST->hasVOP3PInsts()) {
572 VectorType *VT = cast<VectorType>(Tp);
573 if (VT->getNumElements() == 2 &&
574 DL.getTypeSizeInBits(VT->getElementType()) == 16) {
575 // With op_sel VOP3P instructions freely can access the low half or high
576 // half of a register, so any swizzle is free.
577
578 switch (Kind) {
579 case TTI::SK_Broadcast:
580 case TTI::SK_Reverse:
581 case TTI::SK_PermuteSingleSrc:
582 return 0;
583 default:
584 break;
585 }
586 }
587 }
588
589 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
590}
Matt Arsenaultaac47c12017-08-07 17:08:44 +0000591
592bool AMDGPUTTIImpl::areInlineCompatible(const Function *Caller,
593 const Function *Callee) const {
594 const TargetMachine &TM = getTLI()->getTargetMachine();
595 const FeatureBitset &CallerBits =
596 TM.getSubtargetImpl(*Caller)->getFeatureBits();
597 const FeatureBitset &CalleeBits =
598 TM.getSubtargetImpl(*Callee)->getFeatureBits();
599
600 FeatureBitset RealCallerBits = CallerBits & ~InlineFeatureIgnoreList;
601 FeatureBitset RealCalleeBits = CalleeBits & ~InlineFeatureIgnoreList;
602 return ((RealCallerBits & RealCalleeBits) == RealCalleeBits);
603}