blob: 11e4ba4b5010dc3655d1014dfd81596241488845 [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);
Matt Arsenaultac6e39c2014-07-17 06:19:06 +0000105 for (const BasicBlock *BB : L->getBlocks()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000106 const DataLayout &DL = BB->getModule()->getDataLayout();
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000107 unsigned LocalGEPsSeen = 0;
108
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000109 if (llvm::any_of(L->getSubLoops(), [BB](const Loop* SubLoop) {
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +0000110 return SubLoop->contains(BB); }))
111 continue; // Block belongs to an inner loop.
112
Matt Arsenaultac6e39c2014-07-17 06:19:06 +0000113 for (const Instruction &I : *BB) {
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +0000114 // Unroll a loop which contains an "if" statement whose condition
115 // defined by a PHI belonging to the loop. This may help to eliminate
116 // if region and potentially even PHI itself, saving on both divergence
117 // and registers used for the PHI.
118 // Add a small bonus for each of such "if" statements.
119 if (const BranchInst *Br = dyn_cast<BranchInst>(&I)) {
120 if (UP.Threshold < MaxBoost && Br->isConditional()) {
121 if (L->isLoopExiting(Br->getSuccessor(0)) ||
122 L->isLoopExiting(Br->getSuccessor(1)))
123 continue;
124 if (dependsOnLocalPhi(L, Br->getCondition())) {
125 UP.Threshold += UnrollThresholdIf;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000126 LLVM_DEBUG(dbgs() << "Set unroll threshold " << UP.Threshold
127 << " for loop:\n"
128 << *L << " due to " << *Br << '\n');
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +0000129 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;
Matt Arsenault0da63502018-08-31 05:49:54 +0000142 if (AS == AMDGPUAS::PRIVATE_ADDRESS)
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000143 Threshold = ThresholdPrivate;
Matt Arsenault0da63502018-08-31 05:49:54 +0000144 else if (AS == AMDGPUAS::LOCAL_ADDRESS)
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000145 Threshold = ThresholdLocal;
146 else
147 continue;
148
149 if (UP.Threshold >= Threshold)
150 continue;
151
Matt Arsenault0da63502018-08-31 05:49:54 +0000152 if (AS == AMDGPUAS::PRIVATE_ADDRESS) {
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000153 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;
Matt Arsenault0da63502018-08-31 05:49:54 +0000162 } else if (AS == AMDGPUAS::LOCAL_ADDRESS) {
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000163 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;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000204 LLVM_DEBUG(dbgs() << "Set unroll threshold " << Threshold
205 << " for loop:\n"
206 << *L << " due to " << *GEP << '\n');
Stanislav Mekhanoshin478b8192017-04-07 16:26:28 +0000207 if (UP.Threshold >= MaxBoost)
Stanislav Mekhanoshinbaf31ac2017-03-28 22:13:51 +0000208 return;
Tom Stellard8cce9bd2014-01-23 18:49:28 +0000209 }
210 }
211}
Matt Arsenault3dd43fc2014-07-18 06:07:13 +0000212
Tom Stellardc7624312018-05-30 22:55:35 +0000213unsigned GCNTTIImpl::getHardwareNumberOfRegisters(bool Vec) const {
Matt Arsenault67cd3472017-06-20 20:38:06 +0000214 // The concept of vector registers doesn't really exist. Some packed vector
215 // operations operate on the normal 32-bit registers.
Tom Stellardc7624312018-05-30 22:55:35 +0000216 return 256;
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000217}
218
Tom Stellardc7624312018-05-30 22:55:35 +0000219unsigned GCNTTIImpl::getNumberOfRegisters(bool Vec) const {
Matt Arsenault67cd3472017-06-20 20:38:06 +0000220 // This is really the number of registers to fill when vectorizing /
221 // interleaving loops, so we lie to avoid trying to use all registers.
222 return getHardwareNumberOfRegisters(Vec) >> 3;
223}
224
Tom Stellardc7624312018-05-30 22:55:35 +0000225unsigned GCNTTIImpl::getRegisterBitWidth(bool Vector) const {
Matt Arsenault67cd3472017-06-20 20:38:06 +0000226 return 32;
227}
228
Tom Stellardc7624312018-05-30 22:55:35 +0000229unsigned GCNTTIImpl::getMinVectorRegisterBitWidth() const {
Matt Arsenault67cd3472017-06-20 20:38:06 +0000230 return 32;
Matt Arsenault4339b3f2015-12-24 05:14:55 +0000231}
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000232
Tom Stellardc7624312018-05-30 22:55:35 +0000233unsigned GCNTTIImpl::getLoadVectorFactor(unsigned VF, unsigned LoadSize,
Farhana Aleen89196642018-03-07 17:09:18 +0000234 unsigned ChainSizeInBytes,
235 VectorType *VecTy) const {
236 unsigned VecRegBitWidth = VF * LoadSize;
237 if (VecRegBitWidth > 128 && VecTy->getScalarSizeInBits() < 32)
238 // TODO: Support element-size less than 32bit?
239 return 128 / LoadSize;
240
241 return VF;
242}
243
Tom Stellardc7624312018-05-30 22:55:35 +0000244unsigned GCNTTIImpl::getStoreVectorFactor(unsigned VF, unsigned StoreSize,
Farhana Aleen89196642018-03-07 17:09:18 +0000245 unsigned ChainSizeInBytes,
246 VectorType *VecTy) const {
247 unsigned VecRegBitWidth = VF * StoreSize;
248 if (VecRegBitWidth > 128)
249 return 128 / StoreSize;
250
251 return VF;
252}
253
Tom Stellardc7624312018-05-30 22:55:35 +0000254unsigned GCNTTIImpl::getLoadStoreVecRegBitWidth(unsigned AddrSpace) const {
Matt Arsenault0da63502018-08-31 05:49:54 +0000255 if (AddrSpace == AMDGPUAS::GLOBAL_ADDRESS ||
256 AddrSpace == AMDGPUAS::CONSTANT_ADDRESS ||
257 AddrSpace == AMDGPUAS::CONSTANT_ADDRESS_32BIT) {
Farhana Aleen89196642018-03-07 17:09:18 +0000258 return 512;
259 }
260
Matt Arsenault0da63502018-08-31 05:49:54 +0000261 if (AddrSpace == AMDGPUAS::FLAT_ADDRESS ||
262 AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
263 AddrSpace == AMDGPUAS::REGION_ADDRESS)
Farhana Aleeneacb1022018-05-28 18:15:11 +0000264 return 128;
Marek Olsaka9a58fa2018-04-10 22:48:23 +0000265
Matt Arsenault0da63502018-08-31 05:49:54 +0000266 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS)
Matt Arsenault0994bd52016-07-01 00:56:27 +0000267 return 8 * ST->getMaxPrivateElementSize();
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000268
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000269 llvm_unreachable("unhandled address space");
Matt Arsenault0994bd52016-07-01 00:56:27 +0000270}
271
Tom Stellardc7624312018-05-30 22:55:35 +0000272bool GCNTTIImpl::isLegalToVectorizeMemChain(unsigned ChainSizeInBytes,
Matt Arsenaultf0a88db2017-02-23 03:58:53 +0000273 unsigned Alignment,
274 unsigned AddrSpace) const {
275 // We allow vectorization of flat stores, even though we may need to decompose
276 // them later if they may access private memory. We don't have enough context
277 // here, and legalization can handle it.
Matt Arsenault0da63502018-08-31 05:49:54 +0000278 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) {
Matt Arsenaultf0a88db2017-02-23 03:58:53 +0000279 return (Alignment >= 4 || ST->hasUnalignedScratchAccess()) &&
280 ChainSizeInBytes <= ST->getMaxPrivateElementSize();
281 }
282 return true;
283}
284
Tom Stellardc7624312018-05-30 22:55:35 +0000285bool GCNTTIImpl::isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
Matt Arsenaultf0a88db2017-02-23 03:58:53 +0000286 unsigned Alignment,
287 unsigned AddrSpace) const {
288 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
289}
290
Tom Stellardc7624312018-05-30 22:55:35 +0000291bool GCNTTIImpl::isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
Matt Arsenaultf0a88db2017-02-23 03:58:53 +0000292 unsigned Alignment,
293 unsigned AddrSpace) const {
294 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
295}
296
Tom Stellardc7624312018-05-30 22:55:35 +0000297unsigned GCNTTIImpl::getMaxInterleaveFactor(unsigned VF) {
Changpeng Fang1be9b9f2017-03-09 00:07:00 +0000298 // Disable unrolling if the loop is not vectorized.
Matt Arsenault67cd3472017-06-20 20:38:06 +0000299 // TODO: Enable this again.
Changpeng Fang1be9b9f2017-03-09 00:07:00 +0000300 if (VF == 1)
301 return 1;
302
Matt Arsenault67cd3472017-06-20 20:38:06 +0000303 return 8;
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000304}
Matt Arsenaulte830f542015-12-01 19:08:39 +0000305
Tom Stellardc7624312018-05-30 22:55:35 +0000306bool GCNTTIImpl::getTgtMemIntrinsic(IntrinsicInst *Inst,
Matt Arsenault3e268cc2017-12-11 21:38:43 +0000307 MemIntrinsicInfo &Info) const {
308 switch (Inst->getIntrinsicID()) {
309 case Intrinsic::amdgcn_atomic_inc:
Daniil Fukalov6e1dc682018-01-26 11:09:38 +0000310 case Intrinsic::amdgcn_atomic_dec:
311 case Intrinsic::amdgcn_ds_fadd:
312 case Intrinsic::amdgcn_ds_fmin:
313 case Intrinsic::amdgcn_ds_fmax: {
Matt Arsenault3e268cc2017-12-11 21:38:43 +0000314 auto *Ordering = dyn_cast<ConstantInt>(Inst->getArgOperand(2));
315 auto *Volatile = dyn_cast<ConstantInt>(Inst->getArgOperand(4));
316 if (!Ordering || !Volatile)
317 return false; // Invalid.
318
319 unsigned OrderingVal = Ordering->getZExtValue();
320 if (OrderingVal > static_cast<unsigned>(AtomicOrdering::SequentiallyConsistent))
321 return false;
322
323 Info.PtrVal = Inst->getArgOperand(0);
324 Info.Ordering = static_cast<AtomicOrdering>(OrderingVal);
325 Info.ReadMem = true;
326 Info.WriteMem = true;
327 Info.IsVolatile = !Volatile->isNullValue();
328 return true;
329 }
330 default:
331 return false;
332 }
333}
334
Tom Stellardc7624312018-05-30 22:55:35 +0000335int GCNTTIImpl::getArithmeticInstrCost(
Matt Arsenault96518132016-03-25 01:00:32 +0000336 unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info,
337 TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000338 TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args ) {
Matt Arsenault96518132016-03-25 01:00:32 +0000339 EVT OrigTy = TLI->getValueType(DL, Ty);
340 if (!OrigTy.isSimple()) {
341 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
342 Opd1PropInfo, Opd2PropInfo);
343 }
344
345 // Legalize the type.
346 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
347 int ISD = TLI->InstructionOpcodeToISD(Opcode);
348
349 // Because we don't have any legal vector operations, but the legal types, we
350 // need to account for split vectors.
351 unsigned NElts = LT.second.isVector() ?
352 LT.second.getVectorNumElements() : 1;
353
354 MVT::SimpleValueType SLT = LT.second.getScalarType().SimpleTy;
355
356 switch (ISD) {
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000357 case ISD::SHL:
358 case ISD::SRL:
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000359 case ISD::SRA:
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000360 if (SLT == MVT::i64)
361 return get64BitInstrCost() * LT.first * NElts;
362
363 // i32
364 return getFullRateInstrCost() * LT.first * NElts;
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000365 case ISD::ADD:
366 case ISD::SUB:
367 case ISD::AND:
368 case ISD::OR:
Eugene Zelenkod16eff82017-08-08 23:53:55 +0000369 case ISD::XOR:
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000370 if (SLT == MVT::i64){
371 // and, or and xor are typically split into 2 VALU instructions.
372 return 2 * getFullRateInstrCost() * LT.first * NElts;
373 }
374
375 return LT.first * NElts * getFullRateInstrCost();
Matt Arsenault8c8fcb22016-03-25 01:16:40 +0000376 case ISD::MUL: {
377 const int QuarterRateCost = getQuarterRateInstrCost();
378 if (SLT == MVT::i64) {
379 const int FullRateCost = getFullRateInstrCost();
380 return (4 * QuarterRateCost + (2 * 2) * FullRateCost) * LT.first * NElts;
381 }
382
383 // i32
384 return QuarterRateCost * NElts * LT.first;
385 }
Matt Arsenault96518132016-03-25 01:00:32 +0000386 case ISD::FADD:
387 case ISD::FSUB:
388 case ISD::FMUL:
389 if (SLT == MVT::f64)
390 return LT.first * NElts * get64BitInstrCost();
391
392 if (SLT == MVT::f32 || SLT == MVT::f16)
393 return LT.first * NElts * getFullRateInstrCost();
394 break;
Matt Arsenault96518132016-03-25 01:00:32 +0000395 case ISD::FDIV:
396 case ISD::FREM:
397 // FIXME: frem should be handled separately. The fdiv in it is most of it,
398 // but the current lowering is also not entirely correct.
399 if (SLT == MVT::f64) {
400 int Cost = 4 * get64BitInstrCost() + 7 * getQuarterRateInstrCost();
Matt Arsenault96518132016-03-25 01:00:32 +0000401 // Add cost of workaround.
402 if (ST->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS)
403 Cost += 3 * getFullRateInstrCost();
404
405 return LT.first * Cost * NElts;
406 }
407
Matt Arsenault376f1bd2017-08-31 05:47:00 +0000408 if (!Args.empty() && match(Args[0], PatternMatch::m_FPOne())) {
409 // TODO: This is more complicated, unsafe flags etc.
410 if ((SLT == MVT::f32 && !ST->hasFP32Denormals()) ||
411 (SLT == MVT::f16 && ST->has16BitInsts())) {
412 return LT.first * getQuarterRateInstrCost() * NElts;
413 }
414 }
415
416 if (SLT == MVT::f16 && ST->has16BitInsts()) {
417 // 2 x v_cvt_f32_f16
418 // f32 rcp
419 // f32 fmul
420 // v_cvt_f16_f32
421 // f16 div_fixup
422 int Cost = 4 * getFullRateInstrCost() + 2 * getQuarterRateInstrCost();
423 return LT.first * Cost * NElts;
424 }
425
Matt Arsenault96518132016-03-25 01:00:32 +0000426 if (SLT == MVT::f32 || SLT == MVT::f16) {
Matt Arsenault96518132016-03-25 01:00:32 +0000427 int Cost = 7 * getFullRateInstrCost() + 1 * getQuarterRateInstrCost();
Matt Arsenault376f1bd2017-08-31 05:47:00 +0000428
429 if (!ST->hasFP32Denormals()) {
430 // FP mode switches.
431 Cost += 2 * getFullRateInstrCost();
432 }
433
Matt Arsenault96518132016-03-25 01:00:32 +0000434 return LT.first * NElts * Cost;
435 }
Matt Arsenault96518132016-03-25 01:00:32 +0000436 break;
437 default:
438 break;
439 }
440
441 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
442 Opd1PropInfo, Opd2PropInfo);
443}
444
Tom Stellardc7624312018-05-30 22:55:35 +0000445unsigned GCNTTIImpl::getCFInstrCost(unsigned Opcode) {
Matt Arsenaulte05ff152015-12-16 18:37:19 +0000446 // XXX - For some reason this isn't called for switch.
447 switch (Opcode) {
448 case Instruction::Br:
449 case Instruction::Ret:
450 return 10;
451 default:
452 return BaseT::getCFInstrCost(Opcode);
453 }
454}
455
Tom Stellardc7624312018-05-30 22:55:35 +0000456int GCNTTIImpl::getArithmeticReductionCost(unsigned Opcode, Type *Ty,
Farhana Aleene2dfe8a2018-05-01 21:41:12 +0000457 bool IsPairwise) {
458 EVT OrigTy = TLI->getValueType(DL, Ty);
459
460 // Computes cost on targets that have packed math instructions(which support
461 // 16-bit types only).
462 if (IsPairwise ||
463 !ST->hasVOP3PInsts() ||
464 OrigTy.getScalarSizeInBits() != 16)
465 return BaseT::getArithmeticReductionCost(Opcode, Ty, IsPairwise);
466
467 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
468 return LT.first * getFullRateInstrCost();
469}
470
Tom Stellardc7624312018-05-30 22:55:35 +0000471int GCNTTIImpl::getMinMaxReductionCost(Type *Ty, Type *CondTy,
Farhana Aleene24f3ff2018-05-09 21:18:34 +0000472 bool IsPairwise,
473 bool IsUnsigned) {
474 EVT OrigTy = TLI->getValueType(DL, Ty);
475
476 // Computes cost on targets that have packed math instructions(which support
477 // 16-bit types only).
478 if (IsPairwise ||
479 !ST->hasVOP3PInsts() ||
480 OrigTy.getScalarSizeInBits() != 16)
481 return BaseT::getMinMaxReductionCost(Ty, CondTy, IsPairwise, IsUnsigned);
482
483 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
484 return LT.first * getHalfRateInstrCost();
485}
486
Tom Stellardc7624312018-05-30 22:55:35 +0000487int GCNTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
Matt Arsenaulte830f542015-12-01 19:08:39 +0000488 unsigned Index) {
489 switch (Opcode) {
490 case Instruction::ExtractElement:
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000491 case Instruction::InsertElement: {
492 unsigned EltSize
493 = DL.getTypeSizeInBits(cast<VectorType>(ValTy)->getElementType());
494 if (EltSize < 32) {
495 if (EltSize == 16 && Index == 0 && ST->has16BitInsts())
496 return 0;
497 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
498 }
499
Matt Arsenault59767ce2016-03-25 00:14:11 +0000500 // Extracts are just reads of a subregister, so are free. Inserts are
501 // considered free because we don't want to have any cost for scalarizing
502 // operations, and we don't have to copy into a different register class.
503
Matt Arsenaulte830f542015-12-01 19:08:39 +0000504 // Dynamic indexing isn't free and is best avoided.
505 return Index == ~0u ? 2 : 0;
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000506 }
Matt Arsenaulte830f542015-12-01 19:08:39 +0000507 default:
508 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
509 }
510}
Tom Stellarddbe374b2015-12-15 18:04:38 +0000511
Alexander Timofeev2e5eece2018-03-05 15:12:21 +0000512
Tom Stellarddbe374b2015-12-15 18:04:38 +0000513
514static bool isArgPassedInSGPR(const Argument *A) {
515 const Function *F = A->getParent();
Tom Stellarddbe374b2015-12-15 18:04:38 +0000516
517 // Arguments to compute shaders are never a source of divergence.
Matt Arsenault4c1ecde2017-04-19 17:42:34 +0000518 CallingConv::ID CC = F->getCallingConv();
519 switch (CC) {
520 case CallingConv::AMDGPU_KERNEL:
521 case CallingConv::SPIR_KERNEL:
Tom Stellarddbe374b2015-12-15 18:04:38 +0000522 return true;
Matt Arsenault4c1ecde2017-04-19 17:42:34 +0000523 case CallingConv::AMDGPU_VS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000524 case CallingConv::AMDGPU_LS:
Marek Olsaka302a7362017-05-02 15:41:10 +0000525 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000526 case CallingConv::AMDGPU_ES:
Matt Arsenault4c1ecde2017-04-19 17:42:34 +0000527 case CallingConv::AMDGPU_GS:
528 case CallingConv::AMDGPU_PS:
529 case CallingConv::AMDGPU_CS:
530 // For non-compute shaders, SGPR inputs are marked with either inreg or byval.
531 // Everything else is in VGPRs.
532 return F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::InReg) ||
533 F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::ByVal);
534 default:
535 // TODO: Should calls support inreg for SGPR inputs?
536 return false;
537 }
Tom Stellarddbe374b2015-12-15 18:04:38 +0000538}
539
Tom Stellarddbe374b2015-12-15 18:04:38 +0000540/// \returns true if the result of the value could potentially be
541/// different across workitems in a wavefront.
Tom Stellardc7624312018-05-30 22:55:35 +0000542bool GCNTTIImpl::isSourceOfDivergence(const Value *V) const {
Tom Stellarddbe374b2015-12-15 18:04:38 +0000543 if (const Argument *A = dyn_cast<Argument>(V))
544 return !isArgPassedInSGPR(A);
545
Scott Linder72855e32018-08-21 21:24:31 +0000546 // Loads from the private and flat address spaces are divergent, because
547 // threads can execute the load instruction with the same inputs and get
548 // different results.
Tom Stellarddbe374b2015-12-15 18:04:38 +0000549 //
550 // All other loads are not divergent, because if threads issue loads with the
551 // same arguments, they will always get the same result.
552 if (const LoadInst *Load = dyn_cast<LoadInst>(V))
Matt Arsenault0da63502018-08-31 05:49:54 +0000553 return Load->getPointerAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
554 Load->getPointerAddressSpace() == AMDGPUAS::FLAT_ADDRESS;
Tom Stellarddbe374b2015-12-15 18:04:38 +0000555
Nicolai Haehnle79cad852016-03-17 16:21:59 +0000556 // Atomics are divergent because they are executed sequentially: when an
557 // atomic operation refers to the same address in each thread, then each
558 // thread after the first sees the value written by the previous thread as
559 // original value.
560 if (isa<AtomicRMWInst>(V) || isa<AtomicCmpXchgInst>(V))
561 return true;
562
Matt Arsenaultd2c8a332017-02-16 02:01:13 +0000563 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V))
Alexander Timofeev2e5eece2018-03-05 15:12:21 +0000564 return AMDGPU::isIntrinsicSourceOfDivergence(Intrinsic->getIntrinsicID());
Tom Stellarddbe374b2015-12-15 18:04:38 +0000565
566 // Assume all function calls are a source of divergence.
567 if (isa<CallInst>(V) || isa<InvokeInst>(V))
568 return true;
569
570 return false;
571}
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000572
Tom Stellardc7624312018-05-30 22:55:35 +0000573bool GCNTTIImpl::isAlwaysUniform(const Value *V) const {
Alexander Timofeev0f9c84c2017-06-15 19:33:10 +0000574 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) {
575 switch (Intrinsic->getIntrinsicID()) {
576 default:
577 return false;
578 case Intrinsic::amdgcn_readfirstlane:
579 case Intrinsic::amdgcn_readlane:
580 return true;
581 }
582 }
583 return false;
584}
585
Tom Stellardc7624312018-05-30 22:55:35 +0000586unsigned GCNTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
Matt Arsenault3c5e4232017-05-10 21:29:33 +0000587 Type *SubTp) {
588 if (ST->hasVOP3PInsts()) {
589 VectorType *VT = cast<VectorType>(Tp);
590 if (VT->getNumElements() == 2 &&
591 DL.getTypeSizeInBits(VT->getElementType()) == 16) {
592 // With op_sel VOP3P instructions freely can access the low half or high
593 // half of a register, so any swizzle is free.
594
595 switch (Kind) {
596 case TTI::SK_Broadcast:
597 case TTI::SK_Reverse:
598 case TTI::SK_PermuteSingleSrc:
599 return 0;
600 default:
601 break;
602 }
603 }
604 }
605
606 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
607}
Matt Arsenaultaac47c12017-08-07 17:08:44 +0000608
Tom Stellardc7624312018-05-30 22:55:35 +0000609bool GCNTTIImpl::areInlineCompatible(const Function *Caller,
Matt Arsenaultaac47c12017-08-07 17:08:44 +0000610 const Function *Callee) const {
611 const TargetMachine &TM = getTLI()->getTargetMachine();
612 const FeatureBitset &CallerBits =
613 TM.getSubtargetImpl(*Caller)->getFeatureBits();
614 const FeatureBitset &CalleeBits =
615 TM.getSubtargetImpl(*Callee)->getFeatureBits();
616
617 FeatureBitset RealCallerBits = CallerBits & ~InlineFeatureIgnoreList;
618 FeatureBitset RealCalleeBits = CalleeBits & ~InlineFeatureIgnoreList;
619 return ((RealCallerBits & RealCalleeBits) == RealCalleeBits);
620}
Tom Stellardc7624312018-05-30 22:55:35 +0000621
622void GCNTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
623 TTI::UnrollingPreferences &UP) {
624 CommonTTI.getUnrollingPreferences(L, SE, UP);
625}
626
627unsigned R600TTIImpl::getHardwareNumberOfRegisters(bool Vec) const {
628 return 4 * 128; // XXX - 4 channels. Should these count as vector instead?
629}
630
631unsigned R600TTIImpl::getNumberOfRegisters(bool Vec) const {
632 return getHardwareNumberOfRegisters(Vec);
633}
634
635unsigned R600TTIImpl::getRegisterBitWidth(bool Vector) const {
636 return 32;
637}
638
639unsigned R600TTIImpl::getMinVectorRegisterBitWidth() const {
640 return 32;
641}
642
643unsigned R600TTIImpl::getLoadStoreVecRegBitWidth(unsigned AddrSpace) const {
Matt Arsenault0da63502018-08-31 05:49:54 +0000644 if (AddrSpace == AMDGPUAS::GLOBAL_ADDRESS ||
645 AddrSpace == AMDGPUAS::CONSTANT_ADDRESS)
Tom Stellardc7624312018-05-30 22:55:35 +0000646 return 128;
Matt Arsenault0da63502018-08-31 05:49:54 +0000647 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
648 AddrSpace == AMDGPUAS::REGION_ADDRESS)
Tom Stellardc7624312018-05-30 22:55:35 +0000649 return 64;
Matt Arsenault0da63502018-08-31 05:49:54 +0000650 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS)
Tom Stellardc7624312018-05-30 22:55:35 +0000651 return 32;
652
Matt Arsenault0da63502018-08-31 05:49:54 +0000653 if ((AddrSpace == AMDGPUAS::PARAM_D_ADDRESS ||
654 AddrSpace == AMDGPUAS::PARAM_I_ADDRESS ||
655 (AddrSpace >= AMDGPUAS::CONSTANT_BUFFER_0 &&
656 AddrSpace <= AMDGPUAS::CONSTANT_BUFFER_15)))
Tom Stellardc7624312018-05-30 22:55:35 +0000657 return 128;
658 llvm_unreachable("unhandled address space");
659}
660
661bool R600TTIImpl::isLegalToVectorizeMemChain(unsigned ChainSizeInBytes,
662 unsigned Alignment,
663 unsigned AddrSpace) const {
664 // We allow vectorization of flat stores, even though we may need to decompose
665 // them later if they may access private memory. We don't have enough context
666 // here, and legalization can handle it.
Matt Arsenault0da63502018-08-31 05:49:54 +0000667 return (AddrSpace != AMDGPUAS::PRIVATE_ADDRESS);
Tom Stellardc7624312018-05-30 22:55:35 +0000668}
669
670bool R600TTIImpl::isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
671 unsigned Alignment,
672 unsigned AddrSpace) const {
673 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
674}
675
676bool R600TTIImpl::isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
677 unsigned Alignment,
678 unsigned AddrSpace) const {
679 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
680}
681
682unsigned R600TTIImpl::getMaxInterleaveFactor(unsigned VF) {
683 // Disable unrolling if the loop is not vectorized.
684 // TODO: Enable this again.
685 if (VF == 1)
686 return 1;
687
688 return 8;
689}
690
691unsigned R600TTIImpl::getCFInstrCost(unsigned Opcode) {
692 // XXX - For some reason this isn't called for switch.
693 switch (Opcode) {
694 case Instruction::Br:
695 case Instruction::Ret:
696 return 10;
697 default:
698 return BaseT::getCFInstrCost(Opcode);
699 }
700}
701
702int R600TTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
703 unsigned Index) {
704 switch (Opcode) {
705 case Instruction::ExtractElement:
706 case Instruction::InsertElement: {
707 unsigned EltSize
708 = DL.getTypeSizeInBits(cast<VectorType>(ValTy)->getElementType());
709 if (EltSize < 32) {
710 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
711 }
712
713 // Extracts are just reads of a subregister, so are free. Inserts are
714 // considered free because we don't want to have any cost for scalarizing
715 // operations, and we don't have to copy into a different register class.
716
717 // Dynamic indexing isn't free and is best avoided.
718 return Index == ~0u ? 2 : 0;
719 }
720 default:
721 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
722 }
723}
724
725void R600TTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
726 TTI::UnrollingPreferences &UP) {
727 CommonTTI.getUnrollingPreferences(L, SE, UP);
728}