1 //===- AMDGPUTargetTransformInfo.cpp - AMDGPU specific TTI pass -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // \file
10 // This file implements a TargetTransformInfo analysis pass specific to the
11 // AMDGPU target machine. It uses the target's detailed information to provide
12 // more precise answers to certain TTI queries, while letting the target
13 // independent and default TTI implementations handle the rest.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "AMDGPUTargetTransformInfo.h"
18 #include "AMDGPUTargetMachine.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/ValueTracking.h"
21 #include "llvm/IR/IntrinsicsAMDGPU.h"
22 #include "llvm/IR/PatternMatch.h"
23 #include "llvm/Support/KnownBits.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "AMDGPUtti"
28 
29 static cl::opt<unsigned> UnrollThresholdPrivate(
30   "amdgpu-unroll-threshold-private",
31   cl::desc("Unroll threshold for AMDGPU if private memory used in a loop"),
32   cl::init(2700), cl::Hidden);
33 
34 static cl::opt<unsigned> UnrollThresholdLocal(
35   "amdgpu-unroll-threshold-local",
36   cl::desc("Unroll threshold for AMDGPU if local memory used in a loop"),
37   cl::init(1000), cl::Hidden);
38 
39 static cl::opt<unsigned> UnrollThresholdIf(
40   "amdgpu-unroll-threshold-if",
41   cl::desc("Unroll threshold increment for AMDGPU for each if statement inside loop"),
42   cl::init(150), cl::Hidden);
43 
44 static cl::opt<bool> UnrollRuntimeLocal(
45   "amdgpu-unroll-runtime-local",
46   cl::desc("Allow runtime unroll for AMDGPU if local memory used in a loop"),
47   cl::init(true), cl::Hidden);
48 
49 static cl::opt<bool> UseLegacyDA(
50   "amdgpu-use-legacy-divergence-analysis",
51   cl::desc("Enable legacy divergence analysis for AMDGPU"),
52   cl::init(false), cl::Hidden);
53 
54 static cl::opt<unsigned> UnrollMaxBlockToAnalyze(
55     "amdgpu-unroll-max-block-to-analyze",
56     cl::desc("Inner loop block size threshold to analyze in unroll for AMDGPU"),
57     cl::init(32), cl::Hidden);
58 
59 static cl::opt<unsigned> ArgAllocaCost("amdgpu-inline-arg-alloca-cost",
60                                        cl::Hidden, cl::init(4000),
61                                        cl::desc("Cost of alloca argument"));
62 
63 // If the amount of scratch memory to eliminate exceeds our ability to allocate
64 // it into registers we gain nothing by aggressively inlining functions for that
65 // heuristic.
66 static cl::opt<unsigned>
67     ArgAllocaCutoff("amdgpu-inline-arg-alloca-cutoff", cl::Hidden,
68                     cl::init(256),
69                     cl::desc("Maximum alloca size to use for inline cost"));
70 
71 // Inliner constraint to achieve reasonable compilation time.
72 static cl::opt<size_t> InlineMaxBB(
73     "amdgpu-inline-max-bb", cl::Hidden, cl::init(1100),
74     cl::desc("Maximum number of BBs allowed in a function after inlining"
75              " (compile time constraint)"));
76 
77 static bool dependsOnLocalPhi(const Loop *L, const Value *Cond,
78                               unsigned Depth = 0) {
79   const Instruction *I = dyn_cast<Instruction>(Cond);
80   if (!I)
81     return false;
82 
83   for (const Value *V : I->operand_values()) {
84     if (!L->contains(I))
85       continue;
86     if (const PHINode *PHI = dyn_cast<PHINode>(V)) {
87       if (llvm::none_of(L->getSubLoops(), [PHI](const Loop* SubLoop) {
88                   return SubLoop->contains(PHI); }))
89         return true;
90     } else if (Depth < 10 && dependsOnLocalPhi(L, V, Depth+1))
91       return true;
92   }
93   return false;
94 }
95 
96 AMDGPUTTIImpl::AMDGPUTTIImpl(const AMDGPUTargetMachine *TM, const Function &F)
97     : BaseT(TM, F.getParent()->getDataLayout()),
98       TargetTriple(TM->getTargetTriple()),
99       ST(static_cast<const GCNSubtarget *>(TM->getSubtargetImpl(F))),
100       TLI(ST->getTargetLowering()) {}
101 
102 void AMDGPUTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
103                                             TTI::UnrollingPreferences &UP) {
104   const Function &F = *L->getHeader()->getParent();
105   UP.Threshold = AMDGPU::getIntegerAttribute(F, "amdgpu-unroll-threshold", 300);
106   UP.MaxCount = std::numeric_limits<unsigned>::max();
107   UP.Partial = true;
108 
109   // TODO: Do we want runtime unrolling?
110 
111   // Maximum alloca size than can fit registers. Reserve 16 registers.
112   const unsigned MaxAlloca = (256 - 16) * 4;
113   unsigned ThresholdPrivate = UnrollThresholdPrivate;
114   unsigned ThresholdLocal = UnrollThresholdLocal;
115 
116   // If this loop has the amdgpu.loop.unroll.threshold metadata we will use the
117   // provided threshold value as the default for Threshold
118   if (MDNode *LoopUnrollThreshold =
119           findOptionMDForLoop(L, "amdgpu.loop.unroll.threshold")) {
120     if (LoopUnrollThreshold->getNumOperands() == 2) {
121       ConstantInt *MetaThresholdValue = mdconst::extract_or_null<ConstantInt>(
122           LoopUnrollThreshold->getOperand(1));
123       if (MetaThresholdValue) {
124         // We will also use the supplied value for PartialThreshold for now.
125         // We may introduce additional metadata if it becomes necessary in the
126         // future.
127         UP.Threshold = MetaThresholdValue->getSExtValue();
128         UP.PartialThreshold = UP.Threshold;
129         ThresholdPrivate = std::min(ThresholdPrivate, UP.Threshold);
130         ThresholdLocal = std::min(ThresholdLocal, UP.Threshold);
131       }
132     }
133   }
134 
135   unsigned MaxBoost = std::max(ThresholdPrivate, ThresholdLocal);
136   for (const BasicBlock *BB : L->getBlocks()) {
137     const DataLayout &DL = BB->getModule()->getDataLayout();
138     unsigned LocalGEPsSeen = 0;
139 
140     if (llvm::any_of(L->getSubLoops(), [BB](const Loop* SubLoop) {
141                return SubLoop->contains(BB); }))
142         continue; // Block belongs to an inner loop.
143 
144     for (const Instruction &I : *BB) {
145       // Unroll a loop which contains an "if" statement whose condition
146       // defined by a PHI belonging to the loop. This may help to eliminate
147       // if region and potentially even PHI itself, saving on both divergence
148       // and registers used for the PHI.
149       // Add a small bonus for each of such "if" statements.
150       if (const BranchInst *Br = dyn_cast<BranchInst>(&I)) {
151         if (UP.Threshold < MaxBoost && Br->isConditional()) {
152           BasicBlock *Succ0 = Br->getSuccessor(0);
153           BasicBlock *Succ1 = Br->getSuccessor(1);
154           if ((L->contains(Succ0) && L->isLoopExiting(Succ0)) ||
155               (L->contains(Succ1) && L->isLoopExiting(Succ1)))
156             continue;
157           if (dependsOnLocalPhi(L, Br->getCondition())) {
158             UP.Threshold += UnrollThresholdIf;
159             LLVM_DEBUG(dbgs() << "Set unroll threshold " << UP.Threshold
160                               << " for loop:\n"
161                               << *L << " due to " << *Br << '\n');
162             if (UP.Threshold >= MaxBoost)
163               return;
164           }
165         }
166         continue;
167       }
168 
169       const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I);
170       if (!GEP)
171         continue;
172 
173       unsigned AS = GEP->getAddressSpace();
174       unsigned Threshold = 0;
175       if (AS == AMDGPUAS::PRIVATE_ADDRESS)
176         Threshold = ThresholdPrivate;
177       else if (AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::REGION_ADDRESS)
178         Threshold = ThresholdLocal;
179       else
180         continue;
181 
182       if (UP.Threshold >= Threshold)
183         continue;
184 
185       if (AS == AMDGPUAS::PRIVATE_ADDRESS) {
186         const Value *Ptr = GEP->getPointerOperand();
187         const AllocaInst *Alloca =
188             dyn_cast<AllocaInst>(getUnderlyingObject(Ptr));
189         if (!Alloca || !Alloca->isStaticAlloca())
190           continue;
191         Type *Ty = Alloca->getAllocatedType();
192         unsigned AllocaSize = Ty->isSized() ? DL.getTypeAllocSize(Ty) : 0;
193         if (AllocaSize > MaxAlloca)
194           continue;
195       } else if (AS == AMDGPUAS::LOCAL_ADDRESS ||
196                  AS == AMDGPUAS::REGION_ADDRESS) {
197         LocalGEPsSeen++;
198         // Inhibit unroll for local memory if we have seen addressing not to
199         // a variable, most likely we will be unable to combine it.
200         // Do not unroll too deep inner loops for local memory to give a chance
201         // to unroll an outer loop for a more important reason.
202         if (LocalGEPsSeen > 1 || L->getLoopDepth() > 2 ||
203             (!isa<GlobalVariable>(GEP->getPointerOperand()) &&
204              !isa<Argument>(GEP->getPointerOperand())))
205           continue;
206         LLVM_DEBUG(dbgs() << "Allow unroll runtime for loop:\n"
207                           << *L << " due to LDS use.\n");
208         UP.Runtime = UnrollRuntimeLocal;
209       }
210 
211       // Check if GEP depends on a value defined by this loop itself.
212       bool HasLoopDef = false;
213       for (const Value *Op : GEP->operands()) {
214         const Instruction *Inst = dyn_cast<Instruction>(Op);
215         if (!Inst || L->isLoopInvariant(Op))
216           continue;
217 
218         if (llvm::any_of(L->getSubLoops(), [Inst](const Loop* SubLoop) {
219              return SubLoop->contains(Inst); }))
220           continue;
221         HasLoopDef = true;
222         break;
223       }
224       if (!HasLoopDef)
225         continue;
226 
227       // We want to do whatever we can to limit the number of alloca
228       // instructions that make it through to the code generator.  allocas
229       // require us to use indirect addressing, which is slow and prone to
230       // compiler bugs.  If this loop does an address calculation on an
231       // alloca ptr, then we want to use a higher than normal loop unroll
232       // threshold. This will give SROA a better chance to eliminate these
233       // allocas.
234       //
235       // We also want to have more unrolling for local memory to let ds
236       // instructions with different offsets combine.
237       //
238       // Don't use the maximum allowed value here as it will make some
239       // programs way too big.
240       UP.Threshold = Threshold;
241       LLVM_DEBUG(dbgs() << "Set unroll threshold " << Threshold
242                         << " for loop:\n"
243                         << *L << " due to " << *GEP << '\n');
244       if (UP.Threshold >= MaxBoost)
245         return;
246     }
247 
248     // If we got a GEP in a small BB from inner loop then increase max trip
249     // count to analyze for better estimation cost in unroll
250     if (L->isInnermost() && BB->size() < UnrollMaxBlockToAnalyze)
251       UP.MaxIterationsCountToAnalyze = 32;
252   }
253 }
254 
255 void AMDGPUTTIImpl::getPeelingPreferences(Loop *L, ScalarEvolution &SE,
256                                           TTI::PeelingPreferences &PP) {
257   BaseT::getPeelingPreferences(L, SE, PP);
258 }
259 
260 const FeatureBitset GCNTTIImpl::InlineFeatureIgnoreList = {
261     // Codegen control options which don't matter.
262     AMDGPU::FeatureEnableLoadStoreOpt, AMDGPU::FeatureEnableSIScheduler,
263     AMDGPU::FeatureEnableUnsafeDSOffsetFolding, AMDGPU::FeatureFlatForGlobal,
264     AMDGPU::FeaturePromoteAlloca, AMDGPU::FeatureUnalignedScratchAccess,
265     AMDGPU::FeatureUnalignedAccessMode,
266 
267     AMDGPU::FeatureAutoWaitcntBeforeBarrier,
268 
269     // Property of the kernel/environment which can't actually differ.
270     AMDGPU::FeatureSGPRInitBug, AMDGPU::FeatureXNACK,
271     AMDGPU::FeatureTrapHandler,
272 
273     // The default assumption needs to be ecc is enabled, but no directly
274     // exposed operations depend on it, so it can be safely inlined.
275     AMDGPU::FeatureSRAMECC,
276 
277     // Perf-tuning features
278     AMDGPU::FeatureFastFMAF32, AMDGPU::HalfRate64Ops};
279 
280 GCNTTIImpl::GCNTTIImpl(const AMDGPUTargetMachine *TM, const Function &F)
281     : BaseT(TM, F.getParent()->getDataLayout()),
282       ST(static_cast<const GCNSubtarget *>(TM->getSubtargetImpl(F))),
283       TLI(ST->getTargetLowering()), CommonTTI(TM, F),
284       IsGraphics(AMDGPU::isGraphics(F.getCallingConv())),
285       MaxVGPRs(ST->getMaxNumVGPRs(
286           std::max(ST->getWavesPerEU(F).first,
287                    ST->getWavesPerEUForWorkGroup(
288                        ST->getFlatWorkGroupSizes(F).second)))) {
289   AMDGPU::SIModeRegisterDefaults Mode(F);
290   HasFP32Denormals = Mode.allFP32Denormals();
291   HasFP64FP16Denormals = Mode.allFP64FP16Denormals();
292 }
293 
294 unsigned GCNTTIImpl::getHardwareNumberOfRegisters(bool Vec) const {
295   // The concept of vector registers doesn't really exist. Some packed vector
296   // operations operate on the normal 32-bit registers.
297   return MaxVGPRs;
298 }
299 
300 unsigned GCNTTIImpl::getNumberOfRegisters(bool Vec) const {
301   // This is really the number of registers to fill when vectorizing /
302   // interleaving loops, so we lie to avoid trying to use all registers.
303   return getHardwareNumberOfRegisters(Vec) >> 3;
304 }
305 
306 unsigned GCNTTIImpl::getNumberOfRegisters(unsigned RCID) const {
307   const SIRegisterInfo *TRI = ST->getRegisterInfo();
308   const TargetRegisterClass *RC = TRI->getRegClass(RCID);
309   unsigned NumVGPRs = (TRI->getRegSizeInBits(*RC) + 31) / 32;
310   return getHardwareNumberOfRegisters(false) / NumVGPRs;
311 }
312 
313 unsigned GCNTTIImpl::getRegisterBitWidth(bool Vector) const {
314   return 32;
315 }
316 
317 unsigned GCNTTIImpl::getMinVectorRegisterBitWidth() const {
318   return 32;
319 }
320 
321 unsigned GCNTTIImpl::getMaximumVF(unsigned ElemWidth, unsigned Opcode) const {
322   if (Opcode == Instruction::Load || Opcode == Instruction::Store)
323     return 32 * 4 / ElemWidth;
324   return (ElemWidth == 16 && ST->has16BitInsts()) ? 2 : 1;
325 }
326 
327 unsigned GCNTTIImpl::getLoadVectorFactor(unsigned VF, unsigned LoadSize,
328                                          unsigned ChainSizeInBytes,
329                                          VectorType *VecTy) const {
330   unsigned VecRegBitWidth = VF * LoadSize;
331   if (VecRegBitWidth > 128 && VecTy->getScalarSizeInBits() < 32)
332     // TODO: Support element-size less than 32bit?
333     return 128 / LoadSize;
334 
335   return VF;
336 }
337 
338 unsigned GCNTTIImpl::getStoreVectorFactor(unsigned VF, unsigned StoreSize,
339                                              unsigned ChainSizeInBytes,
340                                              VectorType *VecTy) const {
341   unsigned VecRegBitWidth = VF * StoreSize;
342   if (VecRegBitWidth > 128)
343     return 128 / StoreSize;
344 
345   return VF;
346 }
347 
348 unsigned GCNTTIImpl::getLoadStoreVecRegBitWidth(unsigned AddrSpace) const {
349   if (AddrSpace == AMDGPUAS::GLOBAL_ADDRESS ||
350       AddrSpace == AMDGPUAS::CONSTANT_ADDRESS ||
351       AddrSpace == AMDGPUAS::CONSTANT_ADDRESS_32BIT ||
352       AddrSpace == AMDGPUAS::BUFFER_FAT_POINTER) {
353     return 512;
354   }
355 
356   if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS)
357     return 8 * ST->getMaxPrivateElementSize();
358 
359   // Common to flat, global, local and region. Assume for unknown addrspace.
360   return 128;
361 }
362 
363 bool GCNTTIImpl::isLegalToVectorizeMemChain(unsigned ChainSizeInBytes,
364                                             Align Alignment,
365                                             unsigned AddrSpace) const {
366   // We allow vectorization of flat stores, even though we may need to decompose
367   // them later if they may access private memory. We don't have enough context
368   // here, and legalization can handle it.
369   if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) {
370     return (Alignment >= 4 || ST->hasUnalignedScratchAccess()) &&
371       ChainSizeInBytes <= ST->getMaxPrivateElementSize();
372   }
373   return true;
374 }
375 
376 bool GCNTTIImpl::isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
377                                              Align Alignment,
378                                              unsigned AddrSpace) const {
379   return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
380 }
381 
382 bool GCNTTIImpl::isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
383                                               Align Alignment,
384                                               unsigned AddrSpace) const {
385   return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
386 }
387 
388 // FIXME: Really we would like to issue multiple 128-bit loads and stores per
389 // iteration. Should we report a larger size and let it legalize?
390 //
391 // FIXME: Should we use narrower types for local/region, or account for when
392 // unaligned access is legal?
393 //
394 // FIXME: This could use fine tuning and microbenchmarks.
395 Type *GCNTTIImpl::getMemcpyLoopLoweringType(LLVMContext &Context, Value *Length,
396                                             unsigned SrcAddrSpace,
397                                             unsigned DestAddrSpace,
398                                             unsigned SrcAlign,
399                                             unsigned DestAlign) const {
400   unsigned MinAlign = std::min(SrcAlign, DestAlign);
401 
402   // A (multi-)dword access at an address == 2 (mod 4) will be decomposed by the
403   // hardware into byte accesses. If you assume all alignments are equally
404   // probable, it's more efficient on average to use short accesses for this
405   // case.
406   if (MinAlign == 2)
407     return Type::getInt16Ty(Context);
408 
409   // Not all subtargets have 128-bit DS instructions, and we currently don't
410   // form them by default.
411   if (SrcAddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
412       SrcAddrSpace == AMDGPUAS::REGION_ADDRESS ||
413       DestAddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
414       DestAddrSpace == AMDGPUAS::REGION_ADDRESS) {
415     return FixedVectorType::get(Type::getInt32Ty(Context), 2);
416   }
417 
418   // Global memory works best with 16-byte accesses. Private memory will also
419   // hit this, although they'll be decomposed.
420   return FixedVectorType::get(Type::getInt32Ty(Context), 4);
421 }
422 
423 void GCNTTIImpl::getMemcpyLoopResidualLoweringType(
424   SmallVectorImpl<Type *> &OpsOut, LLVMContext &Context,
425   unsigned RemainingBytes, unsigned SrcAddrSpace, unsigned DestAddrSpace,
426   unsigned SrcAlign, unsigned DestAlign) const {
427   assert(RemainingBytes < 16);
428 
429   unsigned MinAlign = std::min(SrcAlign, DestAlign);
430 
431   if (MinAlign != 2) {
432     Type *I64Ty = Type::getInt64Ty(Context);
433     while (RemainingBytes >= 8) {
434       OpsOut.push_back(I64Ty);
435       RemainingBytes -= 8;
436     }
437 
438     Type *I32Ty = Type::getInt32Ty(Context);
439     while (RemainingBytes >= 4) {
440       OpsOut.push_back(I32Ty);
441       RemainingBytes -= 4;
442     }
443   }
444 
445   Type *I16Ty = Type::getInt16Ty(Context);
446   while (RemainingBytes >= 2) {
447     OpsOut.push_back(I16Ty);
448     RemainingBytes -= 2;
449   }
450 
451   Type *I8Ty = Type::getInt8Ty(Context);
452   while (RemainingBytes) {
453     OpsOut.push_back(I8Ty);
454     --RemainingBytes;
455   }
456 }
457 
458 unsigned GCNTTIImpl::getMaxInterleaveFactor(unsigned VF) {
459   // Disable unrolling if the loop is not vectorized.
460   // TODO: Enable this again.
461   if (VF == 1)
462     return 1;
463 
464   return 8;
465 }
466 
467 bool GCNTTIImpl::getTgtMemIntrinsic(IntrinsicInst *Inst,
468                                        MemIntrinsicInfo &Info) const {
469   switch (Inst->getIntrinsicID()) {
470   case Intrinsic::amdgcn_atomic_inc:
471   case Intrinsic::amdgcn_atomic_dec:
472   case Intrinsic::amdgcn_ds_ordered_add:
473   case Intrinsic::amdgcn_ds_ordered_swap:
474   case Intrinsic::amdgcn_ds_fadd:
475   case Intrinsic::amdgcn_ds_fmin:
476   case Intrinsic::amdgcn_ds_fmax: {
477     auto *Ordering = dyn_cast<ConstantInt>(Inst->getArgOperand(2));
478     auto *Volatile = dyn_cast<ConstantInt>(Inst->getArgOperand(4));
479     if (!Ordering || !Volatile)
480       return false; // Invalid.
481 
482     unsigned OrderingVal = Ordering->getZExtValue();
483     if (OrderingVal > static_cast<unsigned>(AtomicOrdering::SequentiallyConsistent))
484       return false;
485 
486     Info.PtrVal = Inst->getArgOperand(0);
487     Info.Ordering = static_cast<AtomicOrdering>(OrderingVal);
488     Info.ReadMem = true;
489     Info.WriteMem = true;
490     Info.IsVolatile = !Volatile->isNullValue();
491     return true;
492   }
493   default:
494     return false;
495   }
496 }
497 
498 int GCNTTIImpl::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
499                                        TTI::TargetCostKind CostKind,
500                                        TTI::OperandValueKind Opd1Info,
501                                        TTI::OperandValueKind Opd2Info,
502                                        TTI::OperandValueProperties Opd1PropInfo,
503                                        TTI::OperandValueProperties Opd2PropInfo,
504                                        ArrayRef<const Value *> Args,
505                                        const Instruction *CxtI) {
506   EVT OrigTy = TLI->getValueType(DL, Ty);
507   if (!OrigTy.isSimple()) {
508     // FIXME: We're having to query the throughput cost so that the basic
509     // implementation tries to generate legalize and scalarization costs. Maybe
510     // we could hoist the scalarization code here?
511     if (CostKind != TTI::TCK_CodeSize)
512       return BaseT::getArithmeticInstrCost(Opcode, Ty, TTI::TCK_RecipThroughput,
513                                            Opd1Info, Opd2Info, Opd1PropInfo,
514                                            Opd2PropInfo, Args, CxtI);
515     // Scalarization
516 
517     // Check if any of the operands are vector operands.
518     int ISD = TLI->InstructionOpcodeToISD(Opcode);
519     assert(ISD && "Invalid opcode");
520 
521     std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
522 
523     bool IsFloat = Ty->isFPOrFPVectorTy();
524     // Assume that floating point arithmetic operations cost twice as much as
525     // integer operations.
526     unsigned OpCost = (IsFloat ? 2 : 1);
527 
528     if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
529       // The operation is legal. Assume it costs 1.
530       // TODO: Once we have extract/insert subvector cost we need to use them.
531       return LT.first * OpCost;
532     }
533 
534     if (!TLI->isOperationExpand(ISD, LT.second)) {
535       // If the operation is custom lowered, then assume that the code is twice
536       // as expensive.
537       return LT.first * 2 * OpCost;
538     }
539 
540     // Else, assume that we need to scalarize this op.
541     // TODO: If one of the types get legalized by splitting, handle this
542     // similarly to what getCastInstrCost() does.
543     if (auto *VTy = dyn_cast<VectorType>(Ty)) {
544       unsigned Num = cast<FixedVectorType>(VTy)->getNumElements();
545       unsigned Cost = getArithmeticInstrCost(
546           Opcode, VTy->getScalarType(), CostKind, Opd1Info, Opd2Info,
547           Opd1PropInfo, Opd2PropInfo, Args, CxtI);
548       // Return the cost of multiple scalar invocation plus the cost of
549       // inserting and extracting the values.
550       return getScalarizationOverhead(VTy, Args) + Num * Cost;
551     }
552 
553     // We don't know anything about this scalar instruction.
554     return OpCost;
555   }
556 
557   // Legalize the type.
558   std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
559   int ISD = TLI->InstructionOpcodeToISD(Opcode);
560 
561   // Because we don't have any legal vector operations, but the legal types, we
562   // need to account for split vectors.
563   unsigned NElts = LT.second.isVector() ?
564     LT.second.getVectorNumElements() : 1;
565 
566   MVT::SimpleValueType SLT = LT.second.getScalarType().SimpleTy;
567 
568   switch (ISD) {
569   case ISD::SHL:
570   case ISD::SRL:
571   case ISD::SRA:
572     if (SLT == MVT::i64)
573       return get64BitInstrCost(CostKind) * LT.first * NElts;
574 
575     if (ST->has16BitInsts() && SLT == MVT::i16)
576       NElts = (NElts + 1) / 2;
577 
578     // i32
579     return getFullRateInstrCost() * LT.first * NElts;
580   case ISD::ADD:
581   case ISD::SUB:
582   case ISD::AND:
583   case ISD::OR:
584   case ISD::XOR:
585     if (SLT == MVT::i64) {
586       // and, or and xor are typically split into 2 VALU instructions.
587       return 2 * getFullRateInstrCost() * LT.first * NElts;
588     }
589 
590     if (ST->has16BitInsts() && SLT == MVT::i16)
591       NElts = (NElts + 1) / 2;
592 
593     return LT.first * NElts * getFullRateInstrCost();
594   case ISD::MUL: {
595     const int QuarterRateCost = getQuarterRateInstrCost(CostKind);
596     if (SLT == MVT::i64) {
597       const int FullRateCost = getFullRateInstrCost();
598       return (4 * QuarterRateCost + (2 * 2) * FullRateCost) * LT.first * NElts;
599     }
600 
601     if (ST->has16BitInsts() && SLT == MVT::i16)
602       NElts = (NElts + 1) / 2;
603 
604     // i32
605     return QuarterRateCost * NElts * LT.first;
606   }
607   case ISD::FMUL:
608     // Check possible fuse {fadd|fsub}(a,fmul(b,c)) and return zero cost for
609     // fmul(b,c) supposing the fadd|fsub will get estimated cost for the whole
610     // fused operation.
611     if (CxtI && CxtI->hasOneUse())
612       if (const auto *FAdd = dyn_cast<BinaryOperator>(*CxtI->user_begin())) {
613         const int OPC = TLI->InstructionOpcodeToISD(FAdd->getOpcode());
614         if (OPC == ISD::FADD || OPC == ISD::FSUB) {
615           if (ST->hasMadMacF32Insts() && SLT == MVT::f32 && !HasFP32Denormals)
616             return TargetTransformInfo::TCC_Free;
617           if (ST->has16BitInsts() && SLT == MVT::f16 && !HasFP64FP16Denormals)
618             return TargetTransformInfo::TCC_Free;
619 
620           // Estimate all types may be fused with contract/unsafe flags
621           const TargetOptions &Options = TLI->getTargetMachine().Options;
622           if (Options.AllowFPOpFusion == FPOpFusion::Fast ||
623               Options.UnsafeFPMath ||
624               (FAdd->hasAllowContract() && CxtI->hasAllowContract()))
625             return TargetTransformInfo::TCC_Free;
626         }
627       }
628     LLVM_FALLTHROUGH;
629   case ISD::FADD:
630   case ISD::FSUB:
631     if (SLT == MVT::f64)
632       return LT.first * NElts * get64BitInstrCost(CostKind);
633 
634     if (ST->has16BitInsts() && SLT == MVT::f16)
635       NElts = (NElts + 1) / 2;
636 
637     if (SLT == MVT::f32 || SLT == MVT::f16)
638       return LT.first * NElts * getFullRateInstrCost();
639     break;
640   case ISD::FDIV:
641   case ISD::FREM:
642     // FIXME: frem should be handled separately. The fdiv in it is most of it,
643     // but the current lowering is also not entirely correct.
644     if (SLT == MVT::f64) {
645       int Cost = 7 * get64BitInstrCost(CostKind) +
646                  getQuarterRateInstrCost(CostKind) +
647                  3 * getHalfRateInstrCost(CostKind);
648       // Add cost of workaround.
649       if (!ST->hasUsableDivScaleConditionOutput())
650         Cost += 3 * getFullRateInstrCost();
651 
652       return LT.first * Cost * NElts;
653     }
654 
655     if (!Args.empty() && match(Args[0], PatternMatch::m_FPOne())) {
656       // TODO: This is more complicated, unsafe flags etc.
657       if ((SLT == MVT::f32 && !HasFP32Denormals) ||
658           (SLT == MVT::f16 && ST->has16BitInsts())) {
659         return LT.first * getQuarterRateInstrCost(CostKind) * NElts;
660       }
661     }
662 
663     if (SLT == MVT::f16 && ST->has16BitInsts()) {
664       // 2 x v_cvt_f32_f16
665       // f32 rcp
666       // f32 fmul
667       // v_cvt_f16_f32
668       // f16 div_fixup
669       int Cost =
670           4 * getFullRateInstrCost() + 2 * getQuarterRateInstrCost(CostKind);
671       return LT.first * Cost * NElts;
672     }
673 
674     if (SLT == MVT::f32 || SLT == MVT::f16) {
675       // 4 more v_cvt_* insts without f16 insts support
676       int Cost = (SLT == MVT::f16 ? 14 : 10) * getFullRateInstrCost() +
677                  1 * getQuarterRateInstrCost(CostKind);
678 
679       if (!HasFP32Denormals) {
680         // FP mode switches.
681         Cost += 2 * getFullRateInstrCost();
682       }
683 
684       return LT.first * NElts * Cost;
685     }
686     break;
687   case ISD::FNEG:
688     // Use the backend' estimation. If fneg is not free each element will cost
689     // one additional instruction.
690     return TLI->isFNegFree(SLT) ? 0 : NElts;
691   default:
692     break;
693   }
694 
695   return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info, Opd2Info,
696                                        Opd1PropInfo, Opd2PropInfo, Args, CxtI);
697 }
698 
699 // Return true if there's a potential benefit from using v2f16/v2i16
700 // instructions for an intrinsic, even if it requires nontrivial legalization.
701 static bool intrinsicHasPackedVectorBenefit(Intrinsic::ID ID) {
702   switch (ID) {
703   case Intrinsic::fma: // TODO: fmuladd
704   // There's a small benefit to using vector ops in the legalized code.
705   case Intrinsic::round:
706   case Intrinsic::uadd_sat:
707   case Intrinsic::usub_sat:
708   case Intrinsic::sadd_sat:
709   case Intrinsic::ssub_sat:
710     return true;
711   default:
712     return false;
713   }
714 }
715 
716 int GCNTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
717                                       TTI::TargetCostKind CostKind) {
718   if (ICA.getID() == Intrinsic::fabs)
719     return 0;
720 
721   if (!intrinsicHasPackedVectorBenefit(ICA.getID()))
722     return BaseT::getIntrinsicInstrCost(ICA, CostKind);
723 
724   Type *RetTy = ICA.getReturnType();
725   EVT OrigTy = TLI->getValueType(DL, RetTy);
726   if (!OrigTy.isSimple()) {
727     if (CostKind != TTI::TCK_CodeSize)
728       return BaseT::getIntrinsicInstrCost(ICA, CostKind);
729 
730     // TODO: Combine these two logic paths.
731     if (ICA.isTypeBasedOnly())
732       return getTypeBasedIntrinsicInstrCost(ICA, CostKind);
733 
734     unsigned RetVF =
735         (RetTy->isVectorTy() ? cast<FixedVectorType>(RetTy)->getNumElements()
736                              : 1);
737     const IntrinsicInst *I = ICA.getInst();
738     const SmallVectorImpl<const Value *> &Args = ICA.getArgs();
739     FastMathFlags FMF = ICA.getFlags();
740     // Assume that we need to scalarize this intrinsic.
741 
742     // Compute the scalarization overhead based on Args for a vector
743     // intrinsic. A vectorizer will pass a scalar RetTy and VF > 1, while
744     // CostModel will pass a vector RetTy and VF is 1.
745     unsigned ScalarizationCost = std::numeric_limits<unsigned>::max();
746     if (RetVF > 1) {
747       ScalarizationCost = 0;
748       if (!RetTy->isVoidTy())
749         ScalarizationCost +=
750             getScalarizationOverhead(cast<VectorType>(RetTy), true, false);
751       ScalarizationCost += getOperandsScalarizationOverhead(Args, RetVF);
752     }
753 
754     IntrinsicCostAttributes Attrs(ICA.getID(), RetTy, ICA.getArgTypes(), FMF, I,
755                                   ScalarizationCost);
756     return getIntrinsicInstrCost(Attrs, CostKind);
757   }
758 
759   // Legalize the type.
760   std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, RetTy);
761 
762   unsigned NElts = LT.second.isVector() ?
763     LT.second.getVectorNumElements() : 1;
764 
765   MVT::SimpleValueType SLT = LT.second.getScalarType().SimpleTy;
766 
767   if (SLT == MVT::f64)
768     return LT.first * NElts * get64BitInstrCost(CostKind);
769 
770   if (ST->has16BitInsts() && SLT == MVT::f16)
771     NElts = (NElts + 1) / 2;
772 
773   // TODO: Get more refined intrinsic costs?
774   unsigned InstRate = getQuarterRateInstrCost(CostKind);
775 
776   switch (ICA.getID()) {
777   case Intrinsic::fma:
778     InstRate = ST->hasFastFMAF32() ? getHalfRateInstrCost(CostKind)
779                                    : getQuarterRateInstrCost(CostKind);
780     break;
781   case Intrinsic::uadd_sat:
782   case Intrinsic::usub_sat:
783   case Intrinsic::sadd_sat:
784   case Intrinsic::ssub_sat:
785     static const auto ValidSatTys = {MVT::v2i16, MVT::v4i16};
786     if (any_of(ValidSatTys, [&LT](MVT M) { return M == LT.second; }))
787       NElts = 1;
788     break;
789   }
790 
791   return LT.first * NElts * InstRate;
792 }
793 
794 unsigned GCNTTIImpl::getCFInstrCost(unsigned Opcode,
795                                     TTI::TargetCostKind CostKind) {
796   if (CostKind == TTI::TCK_CodeSize || CostKind == TTI::TCK_SizeAndLatency)
797     return Opcode == Instruction::PHI ? 0 : 1;
798 
799   // XXX - For some reason this isn't called for switch.
800   switch (Opcode) {
801   case Instruction::Br:
802   case Instruction::Ret:
803     return 10;
804   default:
805     return BaseT::getCFInstrCost(Opcode, CostKind);
806   }
807 }
808 
809 int GCNTTIImpl::getArithmeticReductionCost(unsigned Opcode, VectorType *Ty,
810                                            bool IsPairwise,
811                                            TTI::TargetCostKind CostKind) {
812   EVT OrigTy = TLI->getValueType(DL, Ty);
813 
814   // Computes cost on targets that have packed math instructions(which support
815   // 16-bit types only).
816   if (IsPairwise ||
817       !ST->hasVOP3PInsts() ||
818       OrigTy.getScalarSizeInBits() != 16)
819     return BaseT::getArithmeticReductionCost(Opcode, Ty, IsPairwise, CostKind);
820 
821   std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
822   return LT.first * getFullRateInstrCost();
823 }
824 
825 int GCNTTIImpl::getMinMaxReductionCost(VectorType *Ty, VectorType *CondTy,
826                                        bool IsPairwise, bool IsUnsigned,
827                                        TTI::TargetCostKind CostKind) {
828   EVT OrigTy = TLI->getValueType(DL, Ty);
829 
830   // Computes cost on targets that have packed math instructions(which support
831   // 16-bit types only).
832   if (IsPairwise ||
833       !ST->hasVOP3PInsts() ||
834       OrigTy.getScalarSizeInBits() != 16)
835     return BaseT::getMinMaxReductionCost(Ty, CondTy, IsPairwise, IsUnsigned,
836                                          CostKind);
837 
838   std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
839   return LT.first * getHalfRateInstrCost(CostKind);
840 }
841 
842 int GCNTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
843                                       unsigned Index) {
844   switch (Opcode) {
845   case Instruction::ExtractElement:
846   case Instruction::InsertElement: {
847     unsigned EltSize
848       = DL.getTypeSizeInBits(cast<VectorType>(ValTy)->getElementType());
849     if (EltSize < 32) {
850       if (EltSize == 16 && Index == 0 && ST->has16BitInsts())
851         return 0;
852       return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
853     }
854 
855     // Extracts are just reads of a subregister, so are free. Inserts are
856     // considered free because we don't want to have any cost for scalarizing
857     // operations, and we don't have to copy into a different register class.
858 
859     // Dynamic indexing isn't free and is best avoided.
860     return Index == ~0u ? 2 : 0;
861   }
862   default:
863     return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
864   }
865 }
866 
867 /// Analyze if the results of inline asm are divergent. If \p Indices is empty,
868 /// this is analyzing the collective result of all output registers. Otherwise,
869 /// this is only querying a specific result index if this returns multiple
870 /// registers in a struct.
871 bool GCNTTIImpl::isInlineAsmSourceOfDivergence(
872   const CallInst *CI, ArrayRef<unsigned> Indices) const {
873   // TODO: Handle complex extract indices
874   if (Indices.size() > 1)
875     return true;
876 
877   const DataLayout &DL = CI->getModule()->getDataLayout();
878   const SIRegisterInfo *TRI = ST->getRegisterInfo();
879   TargetLowering::AsmOperandInfoVector TargetConstraints =
880       TLI->ParseConstraints(DL, ST->getRegisterInfo(), *CI);
881 
882   const int TargetOutputIdx = Indices.empty() ? -1 : Indices[0];
883 
884   int OutputIdx = 0;
885   for (auto &TC : TargetConstraints) {
886     if (TC.Type != InlineAsm::isOutput)
887       continue;
888 
889     // Skip outputs we don't care about.
890     if (TargetOutputIdx != -1 && TargetOutputIdx != OutputIdx++)
891       continue;
892 
893     TLI->ComputeConstraintToUse(TC, SDValue());
894 
895     Register AssignedReg;
896     const TargetRegisterClass *RC;
897     std::tie(AssignedReg, RC) = TLI->getRegForInlineAsmConstraint(
898       TRI, TC.ConstraintCode, TC.ConstraintVT);
899     if (AssignedReg) {
900       // FIXME: This is a workaround for getRegForInlineAsmConstraint
901       // returning VS_32
902       RC = TRI->getPhysRegClass(AssignedReg);
903     }
904 
905     // For AGPR constraints null is returned on subtargets without AGPRs, so
906     // assume divergent for null.
907     if (!RC || !TRI->isSGPRClass(RC))
908       return true;
909   }
910 
911   return false;
912 }
913 
914 /// \returns true if the new GPU divergence analysis is enabled.
915 bool GCNTTIImpl::useGPUDivergenceAnalysis() const {
916   return !UseLegacyDA;
917 }
918 
919 /// \returns true if the result of the value could potentially be
920 /// different across workitems in a wavefront.
921 bool GCNTTIImpl::isSourceOfDivergence(const Value *V) const {
922   if (const Argument *A = dyn_cast<Argument>(V))
923     return !AMDGPU::isArgPassedInSGPR(A);
924 
925   // Loads from the private and flat address spaces are divergent, because
926   // threads can execute the load instruction with the same inputs and get
927   // different results.
928   //
929   // All other loads are not divergent, because if threads issue loads with the
930   // same arguments, they will always get the same result.
931   if (const LoadInst *Load = dyn_cast<LoadInst>(V))
932     return Load->getPointerAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
933            Load->getPointerAddressSpace() == AMDGPUAS::FLAT_ADDRESS;
934 
935   // Atomics are divergent because they are executed sequentially: when an
936   // atomic operation refers to the same address in each thread, then each
937   // thread after the first sees the value written by the previous thread as
938   // original value.
939   if (isa<AtomicRMWInst>(V) || isa<AtomicCmpXchgInst>(V))
940     return true;
941 
942   if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V))
943     return AMDGPU::isIntrinsicSourceOfDivergence(Intrinsic->getIntrinsicID());
944 
945   // Assume all function calls are a source of divergence.
946   if (const CallInst *CI = dyn_cast<CallInst>(V)) {
947     if (CI->isInlineAsm())
948       return isInlineAsmSourceOfDivergence(CI);
949     return true;
950   }
951 
952   // Assume all function calls are a source of divergence.
953   if (isa<InvokeInst>(V))
954     return true;
955 
956   return false;
957 }
958 
959 bool GCNTTIImpl::isAlwaysUniform(const Value *V) const {
960   if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) {
961     switch (Intrinsic->getIntrinsicID()) {
962     default:
963       return false;
964     case Intrinsic::amdgcn_readfirstlane:
965     case Intrinsic::amdgcn_readlane:
966     case Intrinsic::amdgcn_icmp:
967     case Intrinsic::amdgcn_fcmp:
968     case Intrinsic::amdgcn_ballot:
969     case Intrinsic::amdgcn_if_break:
970       return true;
971     }
972   }
973 
974   if (const CallInst *CI = dyn_cast<CallInst>(V)) {
975     if (CI->isInlineAsm())
976       return !isInlineAsmSourceOfDivergence(CI);
977     return false;
978   }
979 
980   const ExtractValueInst *ExtValue = dyn_cast<ExtractValueInst>(V);
981   if (!ExtValue)
982     return false;
983 
984   const CallInst *CI = dyn_cast<CallInst>(ExtValue->getOperand(0));
985   if (!CI)
986     return false;
987 
988   if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(CI)) {
989     switch (Intrinsic->getIntrinsicID()) {
990     default:
991       return false;
992     case Intrinsic::amdgcn_if:
993     case Intrinsic::amdgcn_else: {
994       ArrayRef<unsigned> Indices = ExtValue->getIndices();
995       return Indices.size() == 1 && Indices[0] == 1;
996     }
997     }
998   }
999 
1000   // If we have inline asm returning mixed SGPR and VGPR results, we inferred
1001   // divergent for the overall struct return. We need to override it in the
1002   // case we're extracting an SGPR component here.
1003   if (CI->isInlineAsm())
1004     return !isInlineAsmSourceOfDivergence(CI, ExtValue->getIndices());
1005 
1006   return false;
1007 }
1008 
1009 bool GCNTTIImpl::collectFlatAddressOperands(SmallVectorImpl<int> &OpIndexes,
1010                                             Intrinsic::ID IID) const {
1011   switch (IID) {
1012   case Intrinsic::amdgcn_atomic_inc:
1013   case Intrinsic::amdgcn_atomic_dec:
1014   case Intrinsic::amdgcn_ds_fadd:
1015   case Intrinsic::amdgcn_ds_fmin:
1016   case Intrinsic::amdgcn_ds_fmax:
1017   case Intrinsic::amdgcn_is_shared:
1018   case Intrinsic::amdgcn_is_private:
1019     OpIndexes.push_back(0);
1020     return true;
1021   default:
1022     return false;
1023   }
1024 }
1025 
1026 Value *GCNTTIImpl::rewriteIntrinsicWithAddressSpace(IntrinsicInst *II,
1027                                                     Value *OldV,
1028                                                     Value *NewV) const {
1029   auto IntrID = II->getIntrinsicID();
1030   switch (IntrID) {
1031   case Intrinsic::amdgcn_atomic_inc:
1032   case Intrinsic::amdgcn_atomic_dec:
1033   case Intrinsic::amdgcn_ds_fadd:
1034   case Intrinsic::amdgcn_ds_fmin:
1035   case Intrinsic::amdgcn_ds_fmax: {
1036     const ConstantInt *IsVolatile = cast<ConstantInt>(II->getArgOperand(4));
1037     if (!IsVolatile->isZero())
1038       return nullptr;
1039     Module *M = II->getParent()->getParent()->getParent();
1040     Type *DestTy = II->getType();
1041     Type *SrcTy = NewV->getType();
1042     Function *NewDecl =
1043         Intrinsic::getDeclaration(M, II->getIntrinsicID(), {DestTy, SrcTy});
1044     II->setArgOperand(0, NewV);
1045     II->setCalledFunction(NewDecl);
1046     return II;
1047   }
1048   case Intrinsic::amdgcn_is_shared:
1049   case Intrinsic::amdgcn_is_private: {
1050     unsigned TrueAS = IntrID == Intrinsic::amdgcn_is_shared ?
1051       AMDGPUAS::LOCAL_ADDRESS : AMDGPUAS::PRIVATE_ADDRESS;
1052     unsigned NewAS = NewV->getType()->getPointerAddressSpace();
1053     LLVMContext &Ctx = NewV->getType()->getContext();
1054     ConstantInt *NewVal = (TrueAS == NewAS) ?
1055       ConstantInt::getTrue(Ctx) : ConstantInt::getFalse(Ctx);
1056     return NewVal;
1057   }
1058   case Intrinsic::ptrmask: {
1059     unsigned OldAS = OldV->getType()->getPointerAddressSpace();
1060     unsigned NewAS = NewV->getType()->getPointerAddressSpace();
1061     Value *MaskOp = II->getArgOperand(1);
1062     Type *MaskTy = MaskOp->getType();
1063 
1064     bool DoTruncate = false;
1065 
1066     const GCNTargetMachine &TM =
1067         static_cast<const GCNTargetMachine &>(getTLI()->getTargetMachine());
1068     if (!TM.isNoopAddrSpaceCast(OldAS, NewAS)) {
1069       // All valid 64-bit to 32-bit casts work by chopping off the high
1070       // bits. Any masking only clearing the low bits will also apply in the new
1071       // address space.
1072       if (DL.getPointerSizeInBits(OldAS) != 64 ||
1073           DL.getPointerSizeInBits(NewAS) != 32)
1074         return nullptr;
1075 
1076       // TODO: Do we need to thread more context in here?
1077       KnownBits Known = computeKnownBits(MaskOp, DL, 0, nullptr, II);
1078       if (Known.countMinLeadingOnes() < 32)
1079         return nullptr;
1080 
1081       DoTruncate = true;
1082     }
1083 
1084     IRBuilder<> B(II);
1085     if (DoTruncate) {
1086       MaskTy = B.getInt32Ty();
1087       MaskOp = B.CreateTrunc(MaskOp, MaskTy);
1088     }
1089 
1090     return B.CreateIntrinsic(Intrinsic::ptrmask, {NewV->getType(), MaskTy},
1091                              {NewV, MaskOp});
1092   }
1093   default:
1094     return nullptr;
1095   }
1096 }
1097 
1098 unsigned GCNTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, VectorType *VT,
1099                                     int Index, VectorType *SubTp) {
1100   if (ST->hasVOP3PInsts()) {
1101     if (cast<FixedVectorType>(VT)->getNumElements() == 2 &&
1102         DL.getTypeSizeInBits(VT->getElementType()) == 16) {
1103       // With op_sel VOP3P instructions freely can access the low half or high
1104       // half of a register, so any swizzle is free.
1105 
1106       switch (Kind) {
1107       case TTI::SK_Broadcast:
1108       case TTI::SK_Reverse:
1109       case TTI::SK_PermuteSingleSrc:
1110         return 0;
1111       default:
1112         break;
1113       }
1114     }
1115   }
1116 
1117   return BaseT::getShuffleCost(Kind, VT, Index, SubTp);
1118 }
1119 
1120 bool GCNTTIImpl::areInlineCompatible(const Function *Caller,
1121                                      const Function *Callee) const {
1122   const TargetMachine &TM = getTLI()->getTargetMachine();
1123   const GCNSubtarget *CallerST
1124     = static_cast<const GCNSubtarget *>(TM.getSubtargetImpl(*Caller));
1125   const GCNSubtarget *CalleeST
1126     = static_cast<const GCNSubtarget *>(TM.getSubtargetImpl(*Callee));
1127 
1128   const FeatureBitset &CallerBits = CallerST->getFeatureBits();
1129   const FeatureBitset &CalleeBits = CalleeST->getFeatureBits();
1130 
1131   FeatureBitset RealCallerBits = CallerBits & ~InlineFeatureIgnoreList;
1132   FeatureBitset RealCalleeBits = CalleeBits & ~InlineFeatureIgnoreList;
1133   if ((RealCallerBits & RealCalleeBits) != RealCalleeBits)
1134     return false;
1135 
1136   // FIXME: dx10_clamp can just take the caller setting, but there seems to be
1137   // no way to support merge for backend defined attributes.
1138   AMDGPU::SIModeRegisterDefaults CallerMode(*Caller);
1139   AMDGPU::SIModeRegisterDefaults CalleeMode(*Callee);
1140   if (!CallerMode.isInlineCompatible(CalleeMode))
1141     return false;
1142 
1143   // Hack to make compile times reasonable.
1144   if (InlineMaxBB && !Callee->hasFnAttribute(Attribute::InlineHint)) {
1145     // Single BB does not increase total BB amount, thus subtract 1.
1146     size_t BBSize = Caller->size() + Callee->size() - 1;
1147     return BBSize <= InlineMaxBB;
1148   }
1149 
1150   return true;
1151 }
1152 
1153 unsigned GCNTTIImpl::adjustInliningThreshold(const CallBase *CB) const {
1154   // If we have a pointer to private array passed into a function
1155   // it will not be optimized out, leaving scratch usage.
1156   // Increase the inline threshold to allow inlining in this case.
1157   uint64_t AllocaSize = 0;
1158   SmallPtrSet<const AllocaInst *, 8> AIVisited;
1159   for (Value *PtrArg : CB->args()) {
1160     PointerType *Ty = dyn_cast<PointerType>(PtrArg->getType());
1161     if (!Ty || (Ty->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS &&
1162                 Ty->getAddressSpace() != AMDGPUAS::FLAT_ADDRESS))
1163       continue;
1164 
1165     PtrArg = getUnderlyingObject(PtrArg);
1166     if (const AllocaInst *AI = dyn_cast<AllocaInst>(PtrArg)) {
1167       if (!AI->isStaticAlloca() || !AIVisited.insert(AI).second)
1168         continue;
1169       AllocaSize += DL.getTypeAllocSize(AI->getAllocatedType());
1170       // If the amount of stack memory is excessive we will not be able
1171       // to get rid of the scratch anyway, bail out.
1172       if (AllocaSize > ArgAllocaCutoff) {
1173         AllocaSize = 0;
1174         break;
1175       }
1176     }
1177   }
1178   if (AllocaSize)
1179     return ArgAllocaCost;
1180   return 0;
1181 }
1182 
1183 void GCNTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
1184                                          TTI::UnrollingPreferences &UP) {
1185   CommonTTI.getUnrollingPreferences(L, SE, UP);
1186 }
1187 
1188 void GCNTTIImpl::getPeelingPreferences(Loop *L, ScalarEvolution &SE,
1189                                        TTI::PeelingPreferences &PP) {
1190   CommonTTI.getPeelingPreferences(L, SE, PP);
1191 }
1192 
1193 int GCNTTIImpl::get64BitInstrCost(TTI::TargetCostKind CostKind) const {
1194   return ST->hasHalfRate64Ops() ? getHalfRateInstrCost(CostKind)
1195                                 : getQuarterRateInstrCost(CostKind);
1196 }
1197 
1198 R600TTIImpl::R600TTIImpl(const AMDGPUTargetMachine *TM, const Function &F)
1199     : BaseT(TM, F.getParent()->getDataLayout()),
1200       ST(static_cast<const R600Subtarget *>(TM->getSubtargetImpl(F))),
1201       TLI(ST->getTargetLowering()), CommonTTI(TM, F) {}
1202 
1203 unsigned R600TTIImpl::getHardwareNumberOfRegisters(bool Vec) const {
1204   return 4 * 128; // XXX - 4 channels. Should these count as vector instead?
1205 }
1206 
1207 unsigned R600TTIImpl::getNumberOfRegisters(bool Vec) const {
1208   return getHardwareNumberOfRegisters(Vec);
1209 }
1210 
1211 unsigned R600TTIImpl::getRegisterBitWidth(bool Vector) const {
1212   return 32;
1213 }
1214 
1215 unsigned R600TTIImpl::getMinVectorRegisterBitWidth() const {
1216   return 32;
1217 }
1218 
1219 unsigned R600TTIImpl::getLoadStoreVecRegBitWidth(unsigned AddrSpace) const {
1220   if (AddrSpace == AMDGPUAS::GLOBAL_ADDRESS ||
1221       AddrSpace == AMDGPUAS::CONSTANT_ADDRESS)
1222     return 128;
1223   if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
1224       AddrSpace == AMDGPUAS::REGION_ADDRESS)
1225     return 64;
1226   if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS)
1227     return 32;
1228 
1229   if ((AddrSpace == AMDGPUAS::PARAM_D_ADDRESS ||
1230       AddrSpace == AMDGPUAS::PARAM_I_ADDRESS ||
1231       (AddrSpace >= AMDGPUAS::CONSTANT_BUFFER_0 &&
1232       AddrSpace <= AMDGPUAS::CONSTANT_BUFFER_15)))
1233     return 128;
1234   llvm_unreachable("unhandled address space");
1235 }
1236 
1237 bool R600TTIImpl::isLegalToVectorizeMemChain(unsigned ChainSizeInBytes,
1238                                              Align Alignment,
1239                                              unsigned AddrSpace) const {
1240   // We allow vectorization of flat stores, even though we may need to decompose
1241   // them later if they may access private memory. We don't have enough context
1242   // here, and legalization can handle it.
1243   return (AddrSpace != AMDGPUAS::PRIVATE_ADDRESS);
1244 }
1245 
1246 bool R600TTIImpl::isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
1247                                               Align Alignment,
1248                                               unsigned AddrSpace) const {
1249   return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
1250 }
1251 
1252 bool R600TTIImpl::isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
1253                                                Align Alignment,
1254                                                unsigned AddrSpace) const {
1255   return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
1256 }
1257 
1258 unsigned R600TTIImpl::getMaxInterleaveFactor(unsigned VF) {
1259   // Disable unrolling if the loop is not vectorized.
1260   // TODO: Enable this again.
1261   if (VF == 1)
1262     return 1;
1263 
1264   return 8;
1265 }
1266 
1267 unsigned R600TTIImpl::getCFInstrCost(unsigned Opcode,
1268                                      TTI::TargetCostKind CostKind) {
1269   if (CostKind == TTI::TCK_CodeSize || CostKind == TTI::TCK_SizeAndLatency)
1270     return Opcode == Instruction::PHI ? 0 : 1;
1271 
1272   // XXX - For some reason this isn't called for switch.
1273   switch (Opcode) {
1274   case Instruction::Br:
1275   case Instruction::Ret:
1276     return 10;
1277   default:
1278     return BaseT::getCFInstrCost(Opcode, CostKind);
1279   }
1280 }
1281 
1282 int R600TTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
1283                                     unsigned Index) {
1284   switch (Opcode) {
1285   case Instruction::ExtractElement:
1286   case Instruction::InsertElement: {
1287     unsigned EltSize
1288       = DL.getTypeSizeInBits(cast<VectorType>(ValTy)->getElementType());
1289     if (EltSize < 32) {
1290       return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
1291     }
1292 
1293     // Extracts are just reads of a subregister, so are free. Inserts are
1294     // considered free because we don't want to have any cost for scalarizing
1295     // operations, and we don't have to copy into a different register class.
1296 
1297     // Dynamic indexing isn't free and is best avoided.
1298     return Index == ~0u ? 2 : 0;
1299   }
1300   default:
1301     return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
1302   }
1303 }
1304 
1305 void R600TTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
1306                                           TTI::UnrollingPreferences &UP) {
1307   CommonTTI.getUnrollingPreferences(L, SE, UP);
1308 }
1309 
1310 void R600TTIImpl::getPeelingPreferences(Loop *L, ScalarEvolution &SE,
1311                                         TTI::PeelingPreferences &PP) {
1312   CommonTTI.getPeelingPreferences(L, SE, PP);
1313 }
1314