blob: 4292575c6019ad021b5997e37a3225f24c66a3a5 [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"
26#include "llvm/CodeGen/MachineValueType.h"
27#include "llvm/CodeGen/ValueTypes.h"
28#include "llvm/IR/Argument.h"
29#include "llvm/IR/Attributes.h"
30#include "llvm/IR/BasicBlock.h"
31#include "llvm/IR/CallingConv.h"
32#include "llvm/IR/DataLayout.h"
33#include "llvm/IR/DerivedTypes.h"
34#include "llvm/IR/Function.h"
35#include "llvm/IR/Instruction.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000038#include "llvm/IR/Module.h"
Matt Arsenault376f1bd2017-08-31 05:47:00 +000039#include "llvm/IR/PatternMatch.h"
Eugene Zelenkod16eff82017-08-08 23:53:55 +000040#include "llvm/IR/Type.h"
41#include "llvm/IR/Value.h"
42#include "llvm/MC/SubtargetFeature.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/CommandLine.h"
Tom Stellard8b1e0212013-07-27 00:01:07 +000045#include "llvm/Support/Debug.h"
Eugene Zelenkod16eff82017-08-08 23:53:55 +000046#include "llvm/Support/ErrorHandling.h"
47#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
268 if (AddrSpace == AS.FLAT_ADDRESS)
Matt Arsenault0994bd52016-07-01 00:56:27 +0000269 return 128;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000270 if (AddrSpace == AS.LOCAL_ADDRESS ||
271 AddrSpace == AS.REGION_ADDRESS)
Matt Arsenault0994bd52016-07-01 00:56:27 +0000272 return 64;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000273 if (AddrSpace == AS.PRIVATE_ADDRESS)
Matt Arsenault0994bd52016-07-01 00:56:27 +0000274 return 8 * ST->getMaxPrivateElementSize();
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000275
276 if (ST->getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS &&
277 (AddrSpace == AS.PARAM_D_ADDRESS ||
278 AddrSpace == AS.PARAM_I_ADDRESS ||
Farhana Aleen89196642018-03-07 17:09:18 +0000279 (AddrSpace >= AS.CONSTANT_BUFFER_0 &&
280 AddrSpace <= AS.CONSTANT_BUFFER_15)))
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000281 return 128;
282 llvm_unreachable("unhandled address space");
Matt Arsenault0994bd52016-07-01 00:56:27 +0000283}
284
Matt Arsenaultf0a88db2017-02-23 03:58:53 +0000285bool AMDGPUTTIImpl::isLegalToVectorizeMemChain(unsigned ChainSizeInBytes,
286 unsigned Alignment,
287 unsigned AddrSpace) const {
288 // We allow vectorization of flat stores, even though we may need to decompose
289 // them later if they may access private memory. We don't have enough context
290 // here, and legalization can handle it.
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000291 if (AddrSpace == ST->getAMDGPUAS().PRIVATE_ADDRESS) {
Matt Arsenaultf0a88db2017-02-23 03:58:53 +0000292 return (Alignment >= 4 || ST->hasUnalignedScratchAccess()) &&
293 ChainSizeInBytes <= ST->getMaxPrivateElementSize();
294 }
295 return true;
296}
297
298bool AMDGPUTTIImpl::isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
299 unsigned Alignment,
300 unsigned AddrSpace) const {
301 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
302}
303
304bool AMDGPUTTIImpl::isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
305 unsigned Alignment,
306 unsigned AddrSpace) const {
307 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
308}
309
Wei Mi062c7442015-05-06 17:12:25 +0000310unsigned AMDGPUTTIImpl::getMaxInterleaveFactor(unsigned VF) {
Changpeng Fang1be9b9f2017-03-09 00:07:00 +0000311 // Disable unrolling if the loop is not vectorized.
Matt Arsenault67cd3472017-06-20 20:38:06 +0000312 // TODO: Enable this again.
Changpeng Fang1be9b9f2017-03-09 00:07:00 +0000313 if (VF == 1)
314 return 1;
315
Matt Arsenault67cd3472017-06-20 20:38:06 +0000316 return 8;
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000317}
Matt Arsenaulte830f542015-12-01 19:08:39 +0000318
Matt Arsenault3e268cc2017-12-11 21:38:43 +0000319bool AMDGPUTTIImpl::getTgtMemIntrinsic(IntrinsicInst *Inst,
320 MemIntrinsicInfo &Info) const {
321 switch (Inst->getIntrinsicID()) {
322 case Intrinsic::amdgcn_atomic_inc:
Daniil Fukalov6e1dc682018-01-26 11:09:38 +0000323 case Intrinsic::amdgcn_atomic_dec:
324 case Intrinsic::amdgcn_ds_fadd:
325 case Intrinsic::amdgcn_ds_fmin:
326 case Intrinsic::amdgcn_ds_fmax: {
Matt Arsenault3e268cc2017-12-11 21:38:43 +0000327 auto *Ordering = dyn_cast<ConstantInt>(Inst->getArgOperand(2));
328 auto *Volatile = dyn_cast<ConstantInt>(Inst->getArgOperand(4));
329 if (!Ordering || !Volatile)
330 return false; // Invalid.
331
332 unsigned OrderingVal = Ordering->getZExtValue();
333 if (OrderingVal > static_cast<unsigned>(AtomicOrdering::SequentiallyConsistent))
334 return false;
335
336 Info.PtrVal = Inst->getArgOperand(0);
337 Info.Ordering = static_cast<AtomicOrdering>(OrderingVal);
338 Info.ReadMem = true;
339 Info.WriteMem = true;
340 Info.IsVolatile = !Volatile->isNullValue();
341 return true;
342 }
343 default:
344 return false;
345 }
346}
347
Matt Arsenault96518132016-03-25 01:00:32 +0000348int AMDGPUTTIImpl::getArithmeticInstrCost(
349 unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info,
350 TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000351 TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args ) {
Matt Arsenault96518132016-03-25 01:00:32 +0000352 EVT OrigTy = TLI->getValueType(DL, Ty);
353 if (!OrigTy.isSimple()) {
354 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
355 Opd1PropInfo, Opd2PropInfo);
356 }
357
358 // Legalize the type.
359 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
360 int ISD = TLI->InstructionOpcodeToISD(Opcode);
361
362 // Because we don't have any legal vector operations, but the legal types, we
363 // need to account for split vectors.
364 unsigned NElts = LT.second.isVector() ?
365 LT.second.getVectorNumElements() : 1;
366
367 MVT::SimpleValueType SLT = LT.second.getScalarType().SimpleTy;
368
369 switch (ISD) {
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000370 case ISD::SHL:
371 case ISD::SRL:
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000372 case ISD::SRA:
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000373 if (SLT == MVT::i64)
374 return get64BitInstrCost() * LT.first * NElts;
375
376 // i32
377 return getFullRateInstrCost() * LT.first * NElts;
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000378 case ISD::ADD:
379 case ISD::SUB:
380 case ISD::AND:
381 case ISD::OR:
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000382 case ISD::XOR:
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000383 if (SLT == MVT::i64){
384 // and, or and xor are typically split into 2 VALU instructions.
385 return 2 * getFullRateInstrCost() * LT.first * NElts;
386 }
387
388 return LT.first * NElts * getFullRateInstrCost();
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000389 case ISD::MUL: {
390 const int QuarterRateCost = getQuarterRateInstrCost();
391 if (SLT == MVT::i64) {
392 const int FullRateCost = getFullRateInstrCost();
393 return (4 * QuarterRateCost + (2 * 2) * FullRateCost) * LT.first * NElts;
394 }
395
396 // i32
397 return QuarterRateCost * NElts * LT.first;
398 }
Matt Arsenault96518132016-03-25 01:00:32 +0000399 case ISD::FADD:
400 case ISD::FSUB:
401 case ISD::FMUL:
402 if (SLT == MVT::f64)
403 return LT.first * NElts * get64BitInstrCost();
404
405 if (SLT == MVT::f32 || SLT == MVT::f16)
406 return LT.first * NElts * getFullRateInstrCost();
407 break;
Matt Arsenault96518132016-03-25 01:00:32 +0000408 case ISD::FDIV:
409 case ISD::FREM:
410 // FIXME: frem should be handled separately. The fdiv in it is most of it,
411 // but the current lowering is also not entirely correct.
412 if (SLT == MVT::f64) {
413 int Cost = 4 * get64BitInstrCost() + 7 * getQuarterRateInstrCost();
Matt Arsenault96518132016-03-25 01:00:32 +0000414 // Add cost of workaround.
415 if (ST->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS)
416 Cost += 3 * getFullRateInstrCost();
417
418 return LT.first * Cost * NElts;
419 }
420
Matt Arsenault376f1bd2017-08-31 05:47:00 +0000421 if (!Args.empty() && match(Args[0], PatternMatch::m_FPOne())) {
422 // TODO: This is more complicated, unsafe flags etc.
423 if ((SLT == MVT::f32 && !ST->hasFP32Denormals()) ||
424 (SLT == MVT::f16 && ST->has16BitInsts())) {
425 return LT.first * getQuarterRateInstrCost() * NElts;
426 }
427 }
428
429 if (SLT == MVT::f16 && ST->has16BitInsts()) {
430 // 2 x v_cvt_f32_f16
431 // f32 rcp
432 // f32 fmul
433 // v_cvt_f16_f32
434 // f16 div_fixup
435 int Cost = 4 * getFullRateInstrCost() + 2 * getQuarterRateInstrCost();
436 return LT.first * Cost * NElts;
437 }
438
Matt Arsenault96518132016-03-25 01:00:32 +0000439 if (SLT == MVT::f32 || SLT == MVT::f16) {
Matt Arsenault96518132016-03-25 01:00:32 +0000440 int Cost = 7 * getFullRateInstrCost() + 1 * getQuarterRateInstrCost();
Matt Arsenault376f1bd2017-08-31 05:47:00 +0000441
442 if (!ST->hasFP32Denormals()) {
443 // FP mode switches.
444 Cost += 2 * getFullRateInstrCost();
445 }
446
Matt Arsenault96518132016-03-25 01:00:32 +0000447 return LT.first * NElts * Cost;
448 }
Matt Arsenault96518132016-03-25 01:00:32 +0000449 break;
450 default:
451 break;
452 }
453
454 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
455 Opd1PropInfo, Opd2PropInfo);
456}
457
Matt Arsenaulte05ff152015-12-16 18:37:19 +0000458unsigned AMDGPUTTIImpl::getCFInstrCost(unsigned Opcode) {
459 // XXX - For some reason this isn't called for switch.
460 switch (Opcode) {
461 case Instruction::Br:
462 case Instruction::Ret:
463 return 10;
464 default:
465 return BaseT::getCFInstrCost(Opcode);
466 }
467}
468
Matt Arsenaulte830f542015-12-01 19:08:39 +0000469int AMDGPUTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
470 unsigned Index) {
471 switch (Opcode) {
472 case Instruction::ExtractElement:
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000473 case Instruction::InsertElement: {
474 unsigned EltSize
475 = DL.getTypeSizeInBits(cast<VectorType>(ValTy)->getElementType());
476 if (EltSize < 32) {
477 if (EltSize == 16 && Index == 0 && ST->has16BitInsts())
478 return 0;
479 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
480 }
481
Matt Arsenault59767ce2016-03-25 00:14:11 +0000482 // Extracts are just reads of a subregister, so are free. Inserts are
483 // considered free because we don't want to have any cost for scalarizing
484 // operations, and we don't have to copy into a different register class.
485
Matt Arsenaulte830f542015-12-01 19:08:39 +0000486 // Dynamic indexing isn't free and is best avoided.
487 return Index == ~0u ? 2 : 0;
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000488 }
Matt Arsenaulte830f542015-12-01 19:08:39 +0000489 default:
490 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
491 }
492}
Tom Stellarddbe374b2015-12-15 18:04:38 +0000493
Alexander Timofeev2e5eece2018-03-05 15:12:21 +0000494
Tom Stellarddbe374b2015-12-15 18:04:38 +0000495
496static bool isArgPassedInSGPR(const Argument *A) {
497 const Function *F = A->getParent();
Tom Stellarddbe374b2015-12-15 18:04:38 +0000498
499 // Arguments to compute shaders are never a source of divergence.
Matt Arsenault4c1ecde2017-04-19 17:42:34 +0000500 CallingConv::ID CC = F->getCallingConv();
501 switch (CC) {
502 case CallingConv::AMDGPU_KERNEL:
503 case CallingConv::SPIR_KERNEL:
Tom Stellarddbe374b2015-12-15 18:04:38 +0000504 return true;
Matt Arsenault4c1ecde2017-04-19 17:42:34 +0000505 case CallingConv::AMDGPU_VS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000506 case CallingConv::AMDGPU_LS:
Marek Olsaka302a7362017-05-02 15:41:10 +0000507 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000508 case CallingConv::AMDGPU_ES:
Matt Arsenault4c1ecde2017-04-19 17:42:34 +0000509 case CallingConv::AMDGPU_GS:
510 case CallingConv::AMDGPU_PS:
511 case CallingConv::AMDGPU_CS:
512 // For non-compute shaders, SGPR inputs are marked with either inreg or byval.
513 // Everything else is in VGPRs.
514 return F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::InReg) ||
515 F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::ByVal);
516 default:
517 // TODO: Should calls support inreg for SGPR inputs?
518 return false;
519 }
Tom Stellarddbe374b2015-12-15 18:04:38 +0000520}
521
Tom Stellarddbe374b2015-12-15 18:04:38 +0000522/// \returns true if the result of the value could potentially be
523/// different across workitems in a wavefront.
524bool AMDGPUTTIImpl::isSourceOfDivergence(const Value *V) const {
Tom Stellarddbe374b2015-12-15 18:04:38 +0000525 if (const Argument *A = dyn_cast<Argument>(V))
526 return !isArgPassedInSGPR(A);
527
528 // Loads from the private address space are divergent, because threads
529 // can execute the load instruction with the same inputs and get different
530 // results.
531 //
532 // All other loads are not divergent, because if threads issue loads with the
533 // same arguments, they will always get the same result.
534 if (const LoadInst *Load = dyn_cast<LoadInst>(V))
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000535 return Load->getPointerAddressSpace() == ST->getAMDGPUAS().PRIVATE_ADDRESS;
Tom Stellarddbe374b2015-12-15 18:04:38 +0000536
Nicolai Haehnle79cad852016-03-17 16:21:59 +0000537 // Atomics are divergent because they are executed sequentially: when an
538 // atomic operation refers to the same address in each thread, then each
539 // thread after the first sees the value written by the previous thread as
540 // original value.
541 if (isa<AtomicRMWInst>(V) || isa<AtomicCmpXchgInst>(V))
542 return true;
543
Matt Arsenaultd2c8a332017-02-16 02:01:13 +0000544 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V))
Alexander Timofeev2e5eece2018-03-05 15:12:21 +0000545 return AMDGPU::isIntrinsicSourceOfDivergence(Intrinsic->getIntrinsicID());
Tom Stellarddbe374b2015-12-15 18:04:38 +0000546
547 // Assume all function calls are a source of divergence.
548 if (isa<CallInst>(V) || isa<InvokeInst>(V))
549 return true;
550
551 return false;
552}
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000553
Alexander Timofeev0f9c84c2017-06-15 19:33:10 +0000554bool AMDGPUTTIImpl::isAlwaysUniform(const Value *V) const {
555 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) {
556 switch (Intrinsic->getIntrinsicID()) {
557 default:
558 return false;
559 case Intrinsic::amdgcn_readfirstlane:
560 case Intrinsic::amdgcn_readlane:
561 return true;
562 }
563 }
564 return false;
565}
566
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000567unsigned AMDGPUTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
568 Type *SubTp) {
569 if (ST->hasVOP3PInsts()) {
570 VectorType *VT = cast<VectorType>(Tp);
571 if (VT->getNumElements() == 2 &&
572 DL.getTypeSizeInBits(VT->getElementType()) == 16) {
573 // With op_sel VOP3P instructions freely can access the low half or high
574 // half of a register, so any swizzle is free.
575
576 switch (Kind) {
577 case TTI::SK_Broadcast:
578 case TTI::SK_Reverse:
579 case TTI::SK_PermuteSingleSrc:
580 return 0;
581 default:
582 break;
583 }
584 }
585 }
586
587 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
588}
Matt Arsenaultaac47c12017-08-07 17:08:44 +0000589
590bool AMDGPUTTIImpl::areInlineCompatible(const Function *Caller,
591 const Function *Callee) const {
592 const TargetMachine &TM = getTLI()->getTargetMachine();
593 const FeatureBitset &CallerBits =
594 TM.getSubtargetImpl(*Caller)->getFeatureBits();
595 const FeatureBitset &CalleeBits =
596 TM.getSubtargetImpl(*Callee)->getFeatureBits();
597
598 FeatureBitset RealCallerBits = CallerBits & ~InlineFeatureIgnoreList;
599 FeatureBitset RealCalleeBits = CalleeBits & ~InlineFeatureIgnoreList;
600 return ((RealCallerBits & RealCalleeBits) == RealCalleeBits);
601}