LLVM  8.0.1
AMDGPUTargetTransformInfo.cpp
Go to the documentation of this file.
1 //===- AMDGPUTargetTransformInfo.cpp - AMDGPU specific TTI pass -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // \file
11 // This file implements a TargetTransformInfo analysis pass specific to the
12 // AMDGPU target machine. It uses the target's detailed information to provide
13 // more precise answers to certain TTI queries, while letting the target
14 // independent and default TTI implementations handle the rest.
15 //
16 //===----------------------------------------------------------------------===//
17 
19 #include "AMDGPUSubtarget.h"
20 #include "Utils/AMDGPUBaseInfo.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/Analysis/LoopInfo.h"
27 #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"
37 #include "llvm/IR/Module.h"
38 #include "llvm/IR/PatternMatch.h"
39 #include "llvm/IR/Type.h"
40 #include "llvm/IR/Value.h"
42 #include "llvm/Support/Casting.h"
44 #include "llvm/Support/Debug.h"
49 #include <algorithm>
50 #include <cassert>
51 #include <limits>
52 #include <utility>
53 
54 using namespace llvm;
55 
56 #define DEBUG_TYPE "AMDGPUtti"
57 
59  "amdgpu-unroll-threshold-private",
60  cl::desc("Unroll threshold for AMDGPU if private memory used in a loop"),
61  cl::init(2500), cl::Hidden);
62 
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 
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 
73 static 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)) {
83  if (llvm::none_of(L->getSubLoops(), [PHI](const Loop* SubLoop) {
84  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 
94  UP.Threshold = 300; // Twice the default.
96  UP.Partial = true;
97 
98  // TODO: Do we want runtime unrolling?
99 
100  // Maximum alloca size than can fit registers. Reserve 16 registers.
101  const unsigned MaxAlloca = (256 - 16) * 4;
102  unsigned ThresholdPrivate = UnrollThresholdPrivate;
103  unsigned ThresholdLocal = UnrollThresholdLocal;
104  unsigned MaxBoost = std::max(ThresholdPrivate, ThresholdLocal);
105  for (const BasicBlock *BB : L->getBlocks()) {
106  const DataLayout &DL = BB->getModule()->getDataLayout();
107  unsigned LocalGEPsSeen = 0;
108 
109  if (llvm::any_of(L->getSubLoops(), [BB](const Loop* SubLoop) {
110  return SubLoop->contains(BB); }))
111  continue; // Block belongs to an inner loop.
112 
113  for (const Instruction &I : *BB) {
114  // 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())) {
126  LLVM_DEBUG(dbgs() << "Set unroll threshold " << UP.Threshold
127  << " for loop:\n"
128  << *L << " due to " << *Br << '\n');
129  if (UP.Threshold >= MaxBoost)
130  return;
131  }
132  }
133  continue;
134  }
135 
137  if (!GEP)
138  continue;
139 
140  unsigned AS = GEP->getAddressSpace();
141  unsigned Threshold = 0;
142  if (AS == AMDGPUAS::PRIVATE_ADDRESS)
143  Threshold = ThresholdPrivate;
144  else if (AS == AMDGPUAS::LOCAL_ADDRESS)
145  Threshold = ThresholdLocal;
146  else
147  continue;
148 
149  if (UP.Threshold >= Threshold)
150  continue;
151 
152  if (AS == AMDGPUAS::PRIVATE_ADDRESS) {
153  const Value *Ptr = GEP->getPointerOperand();
154  const AllocaInst *Alloca =
156  if (!Alloca || !Alloca->isStaticAlloca())
157  continue;
158  Type *Ty = Alloca->getAllocatedType();
159  unsigned AllocaSize = Ty->isSized() ? DL.getTypeAllocSize(Ty) : 0;
160  if (AllocaSize > MaxAlloca)
161  continue;
162  } else if (AS == AMDGPUAS::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  }
173 
174  // 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))
179  continue;
180 
181  if (llvm::any_of(L->getSubLoops(), [Inst](const Loop* SubLoop) {
182  return SubLoop->contains(Inst); }))
183  continue;
184  HasLoopDef = true;
185  break;
186  }
187  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  LLVM_DEBUG(dbgs() << "Set unroll threshold " << Threshold
205  << " for loop:\n"
206  << *L << " due to " << *GEP << '\n');
207  if (UP.Threshold >= MaxBoost)
208  return;
209  }
210  }
211 }
212 
214  // The concept of vector registers doesn't really exist. Some packed vector
215  // operations operate on the normal 32-bit registers.
216  return 256;
217 }
218 
219 unsigned GCNTTIImpl::getNumberOfRegisters(bool Vec) const {
220  // 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 
225 unsigned GCNTTIImpl::getRegisterBitWidth(bool Vector) const {
226  return 32;
227 }
228 
230  return 32;
231 }
232 
233 unsigned GCNTTIImpl::getLoadVectorFactor(unsigned VF, unsigned LoadSize,
234  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 
244 unsigned GCNTTIImpl::getStoreVectorFactor(unsigned VF, unsigned StoreSize,
245  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 
254 unsigned GCNTTIImpl::getLoadStoreVecRegBitWidth(unsigned AddrSpace) const {
255  if (AddrSpace == AMDGPUAS::GLOBAL_ADDRESS ||
256  AddrSpace == AMDGPUAS::CONSTANT_ADDRESS ||
257  AddrSpace == AMDGPUAS::CONSTANT_ADDRESS_32BIT) {
258  return 512;
259  }
260 
261  if (AddrSpace == AMDGPUAS::FLAT_ADDRESS ||
262  AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
263  AddrSpace == AMDGPUAS::REGION_ADDRESS)
264  return 128;
265 
266  if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS)
267  return 8 * ST->getMaxPrivateElementSize();
268 
269  llvm_unreachable("unhandled address space");
270 }
271 
272 bool GCNTTIImpl::isLegalToVectorizeMemChain(unsigned ChainSizeInBytes,
273  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.
278  if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) {
279  return (Alignment >= 4 || ST->hasUnalignedScratchAccess()) &&
280  ChainSizeInBytes <= ST->getMaxPrivateElementSize();
281  }
282  return true;
283 }
284 
285 bool GCNTTIImpl::isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
286  unsigned Alignment,
287  unsigned AddrSpace) const {
288  return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
289 }
290 
291 bool GCNTTIImpl::isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
292  unsigned Alignment,
293  unsigned AddrSpace) const {
294  return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
295 }
296 
297 unsigned GCNTTIImpl::getMaxInterleaveFactor(unsigned VF) {
298  // Disable unrolling if the loop is not vectorized.
299  // TODO: Enable this again.
300  if (VF == 1)
301  return 1;
302 
303  return 8;
304 }
305 
307  MemIntrinsicInfo &Info) const {
308  switch (Inst->getIntrinsicID()) {
316  auto *Ordering = dyn_cast<ConstantInt>(Inst->getArgOperand(2));
317  auto *Volatile = dyn_cast<ConstantInt>(Inst->getArgOperand(4));
318  if (!Ordering || !Volatile)
319  return false; // Invalid.
320 
321  unsigned OrderingVal = Ordering->getZExtValue();
322  if (OrderingVal > static_cast<unsigned>(AtomicOrdering::SequentiallyConsistent))
323  return false;
324 
325  Info.PtrVal = Inst->getArgOperand(0);
326  Info.Ordering = static_cast<AtomicOrdering>(OrderingVal);
327  Info.ReadMem = true;
328  Info.WriteMem = true;
329  Info.IsVolatile = !Volatile->isNullValue();
330  return true;
331  }
332  default:
333  return false;
334  }
335 }
336 
338  unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info,
339  TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo,
341  EVT OrigTy = TLI->getValueType(DL, Ty);
342  if (!OrigTy.isSimple()) {
343  return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
344  Opd1PropInfo, Opd2PropInfo);
345  }
346 
347  // Legalize the type.
348  std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
349  int ISD = TLI->InstructionOpcodeToISD(Opcode);
350 
351  // Because we don't have any legal vector operations, but the legal types, we
352  // need to account for split vectors.
353  unsigned NElts = LT.second.isVector() ?
354  LT.second.getVectorNumElements() : 1;
355 
356  MVT::SimpleValueType SLT = LT.second.getScalarType().SimpleTy;
357 
358  switch (ISD) {
359  case ISD::SHL:
360  case ISD::SRL:
361  case ISD::SRA:
362  if (SLT == MVT::i64)
363  return get64BitInstrCost() * LT.first * NElts;
364 
365  // i32
366  return getFullRateInstrCost() * LT.first * NElts;
367  case ISD::ADD:
368  case ISD::SUB:
369  case ISD::AND:
370  case ISD::OR:
371  case ISD::XOR:
372  if (SLT == MVT::i64){
373  // and, or and xor are typically split into 2 VALU instructions.
374  return 2 * getFullRateInstrCost() * LT.first * NElts;
375  }
376 
377  return LT.first * NElts * getFullRateInstrCost();
378  case ISD::MUL: {
379  const int QuarterRateCost = getQuarterRateInstrCost();
380  if (SLT == MVT::i64) {
381  const int FullRateCost = getFullRateInstrCost();
382  return (4 * QuarterRateCost + (2 * 2) * FullRateCost) * LT.first * NElts;
383  }
384 
385  // i32
386  return QuarterRateCost * NElts * LT.first;
387  }
388  case ISD::FADD:
389  case ISD::FSUB:
390  case ISD::FMUL:
391  if (SLT == MVT::f64)
392  return LT.first * NElts * get64BitInstrCost();
393 
394  if (SLT == MVT::f32 || SLT == MVT::f16)
395  return LT.first * NElts * getFullRateInstrCost();
396  break;
397  case ISD::FDIV:
398  case ISD::FREM:
399  // FIXME: frem should be handled separately. The fdiv in it is most of it,
400  // but the current lowering is also not entirely correct.
401  if (SLT == MVT::f64) {
402  int Cost = 4 * get64BitInstrCost() + 7 * getQuarterRateInstrCost();
403  // Add cost of workaround.
404  if (ST->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS)
405  Cost += 3 * getFullRateInstrCost();
406 
407  return LT.first * Cost * NElts;
408  }
409 
410  if (!Args.empty() && match(Args[0], PatternMatch::m_FPOne())) {
411  // TODO: This is more complicated, unsafe flags etc.
412  if ((SLT == MVT::f32 && !ST->hasFP32Denormals()) ||
413  (SLT == MVT::f16 && ST->has16BitInsts())) {
414  return LT.first * getQuarterRateInstrCost() * NElts;
415  }
416  }
417 
418  if (SLT == MVT::f16 && ST->has16BitInsts()) {
419  // 2 x v_cvt_f32_f16
420  // f32 rcp
421  // f32 fmul
422  // v_cvt_f16_f32
423  // f16 div_fixup
424  int Cost = 4 * getFullRateInstrCost() + 2 * getQuarterRateInstrCost();
425  return LT.first * Cost * NElts;
426  }
427 
428  if (SLT == MVT::f32 || SLT == MVT::f16) {
429  int Cost = 7 * getFullRateInstrCost() + 1 * getQuarterRateInstrCost();
430 
431  if (!ST->hasFP32Denormals()) {
432  // FP mode switches.
433  Cost += 2 * getFullRateInstrCost();
434  }
435 
436  return LT.first * NElts * Cost;
437  }
438  break;
439  default:
440  break;
441  }
442 
443  return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
444  Opd1PropInfo, Opd2PropInfo);
445 }
446 
447 unsigned GCNTTIImpl::getCFInstrCost(unsigned Opcode) {
448  // XXX - For some reason this isn't called for switch.
449  switch (Opcode) {
450  case Instruction::Br:
451  case Instruction::Ret:
452  return 10;
453  default:
454  return BaseT::getCFInstrCost(Opcode);
455  }
456 }
457 
459  bool IsPairwise) {
460  EVT OrigTy = TLI->getValueType(DL, Ty);
461 
462  // Computes cost on targets that have packed math instructions(which support
463  // 16-bit types only).
464  if (IsPairwise ||
465  !ST->hasVOP3PInsts() ||
466  OrigTy.getScalarSizeInBits() != 16)
467  return BaseT::getArithmeticReductionCost(Opcode, Ty, IsPairwise);
468 
469  std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
470  return LT.first * getFullRateInstrCost();
471 }
472 
474  bool IsPairwise,
475  bool IsUnsigned) {
476  EVT OrigTy = TLI->getValueType(DL, Ty);
477 
478  // Computes cost on targets that have packed math instructions(which support
479  // 16-bit types only).
480  if (IsPairwise ||
481  !ST->hasVOP3PInsts() ||
482  OrigTy.getScalarSizeInBits() != 16)
483  return BaseT::getMinMaxReductionCost(Ty, CondTy, IsPairwise, IsUnsigned);
484 
485  std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
486  return LT.first * getHalfRateInstrCost();
487 }
488 
489 int GCNTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
490  unsigned Index) {
491  switch (Opcode) {
492  case Instruction::ExtractElement:
493  case Instruction::InsertElement: {
494  unsigned EltSize
495  = DL.getTypeSizeInBits(cast<VectorType>(ValTy)->getElementType());
496  if (EltSize < 32) {
497  if (EltSize == 16 && Index == 0 && ST->has16BitInsts())
498  return 0;
499  return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
500  }
501 
502  // Extracts are just reads of a subregister, so are free. Inserts are
503  // considered free because we don't want to have any cost for scalarizing
504  // operations, and we don't have to copy into a different register class.
505 
506  // Dynamic indexing isn't free and is best avoided.
507  return Index == ~0u ? 2 : 0;
508  }
509  default:
510  return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
511  }
512 }
513 
514 
515 
516 static bool isArgPassedInSGPR(const Argument *A) {
517  const Function *F = A->getParent();
518 
519  // Arguments to compute shaders are never a source of divergence.
521  switch (CC) {
524  return true;
532  // For non-compute shaders, SGPR inputs are marked with either inreg or byval.
533  // Everything else is in VGPRs.
536  default:
537  // TODO: Should calls support inreg for SGPR inputs?
538  return false;
539  }
540 }
541 
542 /// \returns true if the result of the value could potentially be
543 /// different across workitems in a wavefront.
545  if (const Argument *A = dyn_cast<Argument>(V))
546  return !isArgPassedInSGPR(A);
547 
548  // Loads from the private and flat address spaces are divergent, because
549  // threads can execute the load instruction with the same inputs and get
550  // different results.
551  //
552  // All other loads are not divergent, because if threads issue loads with the
553  // same arguments, they will always get the same result.
554  if (const LoadInst *Load = dyn_cast<LoadInst>(V))
555  return Load->getPointerAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
556  Load->getPointerAddressSpace() == AMDGPUAS::FLAT_ADDRESS;
557 
558  // Atomics are divergent because they are executed sequentially: when an
559  // atomic operation refers to the same address in each thread, then each
560  // thread after the first sees the value written by the previous thread as
561  // original value.
562  if (isa<AtomicRMWInst>(V) || isa<AtomicCmpXchgInst>(V))
563  return true;
564 
565  if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V))
566  return AMDGPU::isIntrinsicSourceOfDivergence(Intrinsic->getIntrinsicID());
567 
568  // Assume all function calls are a source of divergence.
569  if (isa<CallInst>(V) || isa<InvokeInst>(V))
570  return true;
571 
572  return false;
573 }
574 
575 bool GCNTTIImpl::isAlwaysUniform(const Value *V) const {
576  if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) {
577  switch (Intrinsic->getIntrinsicID()) {
578  default:
579  return false;
582  return true;
583  }
584  }
585  return false;
586 }
587 
589  Type *SubTp) {
590  if (ST->hasVOP3PInsts()) {
591  VectorType *VT = cast<VectorType>(Tp);
592  if (VT->getNumElements() == 2 &&
593  DL.getTypeSizeInBits(VT->getElementType()) == 16) {
594  // With op_sel VOP3P instructions freely can access the low half or high
595  // half of a register, so any swizzle is free.
596 
597  switch (Kind) {
598  case TTI::SK_Broadcast:
599  case TTI::SK_Reverse:
601  return 0;
602  default:
603  break;
604  }
605  }
606  }
607 
608  return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
609 }
610 
612  const Function *Callee) const {
613  const TargetMachine &TM = getTLI()->getTargetMachine();
614  const FeatureBitset &CallerBits =
615  TM.getSubtargetImpl(*Caller)->getFeatureBits();
616  const FeatureBitset &CalleeBits =
617  TM.getSubtargetImpl(*Callee)->getFeatureBits();
618 
619  FeatureBitset RealCallerBits = CallerBits & ~InlineFeatureIgnoreList;
620  FeatureBitset RealCalleeBits = CalleeBits & ~InlineFeatureIgnoreList;
621  return ((RealCallerBits & RealCalleeBits) == RealCalleeBits);
622 }
623 
626  CommonTTI.getUnrollingPreferences(L, SE, UP);
627 }
628 
630  return 4 * 128; // XXX - 4 channels. Should these count as vector instead?
631 }
632 
633 unsigned R600TTIImpl::getNumberOfRegisters(bool Vec) const {
634  return getHardwareNumberOfRegisters(Vec);
635 }
636 
637 unsigned R600TTIImpl::getRegisterBitWidth(bool Vector) const {
638  return 32;
639 }
640 
642  return 32;
643 }
644 
645 unsigned R600TTIImpl::getLoadStoreVecRegBitWidth(unsigned AddrSpace) const {
646  if (AddrSpace == AMDGPUAS::GLOBAL_ADDRESS ||
647  AddrSpace == AMDGPUAS::CONSTANT_ADDRESS)
648  return 128;
649  if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
650  AddrSpace == AMDGPUAS::REGION_ADDRESS)
651  return 64;
652  if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS)
653  return 32;
654 
655  if ((AddrSpace == AMDGPUAS::PARAM_D_ADDRESS ||
656  AddrSpace == AMDGPUAS::PARAM_I_ADDRESS ||
657  (AddrSpace >= AMDGPUAS::CONSTANT_BUFFER_0 &&
658  AddrSpace <= AMDGPUAS::CONSTANT_BUFFER_15)))
659  return 128;
660  llvm_unreachable("unhandled address space");
661 }
662 
663 bool R600TTIImpl::isLegalToVectorizeMemChain(unsigned ChainSizeInBytes,
664  unsigned Alignment,
665  unsigned AddrSpace) const {
666  // We allow vectorization of flat stores, even though we may need to decompose
667  // them later if they may access private memory. We don't have enough context
668  // here, and legalization can handle it.
669  return (AddrSpace != AMDGPUAS::PRIVATE_ADDRESS);
670 }
671 
672 bool R600TTIImpl::isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
673  unsigned Alignment,
674  unsigned AddrSpace) const {
675  return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
676 }
677 
678 bool R600TTIImpl::isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
679  unsigned Alignment,
680  unsigned AddrSpace) const {
681  return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace);
682 }
683 
684 unsigned R600TTIImpl::getMaxInterleaveFactor(unsigned VF) {
685  // Disable unrolling if the loop is not vectorized.
686  // TODO: Enable this again.
687  if (VF == 1)
688  return 1;
689 
690  return 8;
691 }
692 
693 unsigned R600TTIImpl::getCFInstrCost(unsigned Opcode) {
694  // XXX - For some reason this isn't called for switch.
695  switch (Opcode) {
696  case Instruction::Br:
697  case Instruction::Ret:
698  return 10;
699  default:
700  return BaseT::getCFInstrCost(Opcode);
701  }
702 }
703 
704 int R600TTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
705  unsigned Index) {
706  switch (Opcode) {
707  case Instruction::ExtractElement:
708  case Instruction::InsertElement: {
709  unsigned EltSize
710  = DL.getTypeSizeInBits(cast<VectorType>(ValTy)->getElementType());
711  if (EltSize < 32) {
712  return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
713  }
714 
715  // Extracts are just reads of a subregister, so are free. Inserts are
716  // considered free because we don't want to have any cost for scalarizing
717  // operations, and we don't have to copy into a different register class.
718 
719  // Dynamic indexing isn't free and is best avoided.
720  return Index == ~0u ? 2 : 0;
721  }
722  default:
723  return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
724  }
725 }
726 
729  CommonTTI.getUnrollingPreferences(L, SE, UP);
730 }
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
bool Partial
Allow partial unrolling (unrolling of loops to expand the size of the loop body, not only to eliminat...
unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info=TTI::OK_AnyValue, TTI::OperandValueKind Opd2Info=TTI::OK_AnyValue, TTI::OperandValueProperties Opd1PropInfo=TTI::OP_None, TTI::OperandValueProperties Opd2PropInfo=TTI::OP_None, ArrayRef< const Value * > Args=ArrayRef< const Value * >())
Definition: BasicTTIImpl.h:568
bool isLegalToVectorizeMemChain(unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const
bool isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const
This file a TargetTransformInfo::Concept conforming object specific to the AMDGPU target machine...
Address space for direct addressible parameter memory (CONST0)
Definition: AMDGPU.h:266
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents an incoming formal argument to a Function.
Definition: Argument.h:30
AMDGPU specific subclass of TargetSubtarget.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:265
static cl::opt< unsigned > UnrollThresholdIf("amdgpu-unroll-threshold-if", cl::desc("Unroll threshold increment for AMDGPU for each if statement inside loop"), cl::init(150), cl::Hidden)
unsigned getCFInstrCost(unsigned Opcode)
unsigned getLoopDepth() const
Return the nesting level of this loop.
Definition: LoopInfo.h:92
The main scalar evolution driver.
bool isLegalToVectorizeMemChain(unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const
int getVectorInstrCost(unsigned Opcode, Type *ValTy, unsigned Index)
Address space for 32-bit constant memory.
Definition: AMDGPU.h:263
unsigned getMinVectorRegisterBitWidth() const
Address space for private memory.
Definition: AMDGPU.h:261
void getUnrollingPreferences(Loop *L, ScalarEvolution &SE, TTI::UnrollingPreferences &UP)
F(f)
An instruction for reading from memory.
Definition: Instructions.h:168
int getArithmeticReductionCost(unsigned Opcode, Type *Ty, bool IsPairwise)
Hexagon Common GEP
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1135
Calling convention used for Mesa/AMDPAL geometry shaders.
Definition: CallingConv.h:192
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:48
unsigned getLoadStoreVecRegBitWidth(unsigned AddrSpace) const
Address space for constant memory (VTX2)
Definition: AMDGPU.h:259
Calling convention used for Mesa/AMDPAL compute shaders.
Definition: CallingConv.h:198
const FeatureBitset & getFeatureBits() const
bool isAlwaysUniform(const Value *V) const
Shift and rotation operations.
Definition: ISDOpcodes.h:410
unsigned getNumberOfRegisters(bool Vector) const
bool isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const
SPIR_KERNEL - Calling convention for SPIR kernel functions.
Definition: CallingConv.h:137
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1200
This file contains the simple types necessary to represent the attributes associated with functions a...
unsigned getArithmeticReductionCost(unsigned Opcode, Type *Ty, bool IsPairwise)
Try to calculate arithmetic and shuffle op costs for reduction operations.
uint64_t getNumElements() const
Definition: DerivedTypes.h:359
AtomicOrdering
Atomic ordering for LLVM&#39;s memory model.
unsigned getScalarSizeInBits() const
Definition: ValueTypes.h:298
Calling convention used for AMDPAL shader stage before geometry shader if geometry is in use...
Definition: CallingConv.h:221
bool getTgtMemIntrinsic(IntrinsicInst *Inst, MemIntrinsicInfo &Info) const
bool isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:201
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
unsigned getRegisterBitWidth(bool Vector) const
unsigned getMaxInterleaveFactor(unsigned VF)
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:224
static bool dependsOnLocalPhi(const Loop *L, const Value *Cond, unsigned Depth=0)
Reverse the order of the vector.
Calling convention used for Mesa vertex shaders, or AMDPAL last shader stage before rasterization (ve...
Definition: CallingConv.h:189
Analysis containing CSE Info
Definition: CSEInfo.cpp:21
unsigned getAddressSpace() const
Returns the address space of this instruction&#39;s pointer type.
Definition: Instructions.h:963
unsigned getCFInstrCost(unsigned Opcode)
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Definition: Instructions.h:854
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:149
Container class for subtarget features.
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
bool isLoopExiting(const BlockT *BB) const
True if terminator in the block can branch to another block that is outside of the current loop...
Definition: LoopInfo.h:203
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
Simple binary floating point operators.
Definition: ISDOpcodes.h:283
Conditional or Unconditional Branch instruction.
Address space for flat memory.
Definition: AMDGPU.h:255
unsigned getMinMaxReductionCost(Type *Ty, Type *CondTy, bool IsPairwise, bool)
Try to calculate op costs for min/max reduction operations.
bool isIntrinsicSourceOfDivergence(unsigned IntrID)
unsigned getStoreVectorFactor(unsigned VF, unsigned StoreSize, unsigned ChainSizeInBytes, VectorType *VecTy) const
Address space for indirect addressible parameter memory (VTX1)
Definition: AMDGPU.h:268
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1193
Address space for local memory.
Definition: AMDGPU.h:260
unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index, Type *SubTp)
Calling convention used for AMDPAL vertex shader if tessellation is in use.
Definition: CallingConv.h:216
unsigned getNumberOfRegisters(bool Vec) const
op_range operands()
Definition: User.h:238
static bool isArgPassedInSGPR(const Argument *A)
Extended Value Type.
Definition: ValueTypes.h:34
Address space for global memory (RAT0, VTX0).
Definition: AMDGPU.h:256
unsigned getHardwareNumberOfRegisters(bool Vec) const
Calling convention used for Mesa/AMDPAL pixel shaders.
Definition: CallingConv.h:195
unsigned getLoadStoreVecRegBitWidth(unsigned AddrSpace) const
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Definition: Instructions.h:106
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
Definition: LoopInfo.cpp:58
OperandValueProperties
Additional properties of an operand&#39;s values.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:51
unsigned getCFInstrCost(unsigned Opcode)
Definition: BasicTTIImpl.h:767
unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index, Type *SubTp)
Definition: BasicTTIImpl.h:615
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:110
unsigned getLoadVectorFactor(unsigned VF, unsigned LoadSize, unsigned ChainSizeInBytes, VectorType *VecTy) const
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
bool isSourceOfDivergence(const Value *V) const
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
Definition: Type.cpp:130
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:213
Module.h This file contains the declarations for the Module class.
int getArithmeticInstrCost(unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info=TTI::OK_AnyValue, TTI::OperandValueKind Opd2Info=TTI::OK_AnyValue, TTI::OperandValueProperties Opd1PropInfo=TTI::OP_None, TTI::OperandValueProperties Opd2PropInfo=TTI::OP_None, ArrayRef< const Value *> Args=ArrayRef< const Value *>())
int getMinMaxReductionCost(Type *Ty, Type *CondTy, bool IsPairwiseForm, bool IsUnsigned)
bool isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const
virtual const TargetSubtargetInfo * getSubtargetImpl(const Function &) const
Virtual method implemented by subclasses that returns a reference to that target&#39;s TargetSubtargetInf...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
unsigned getMaxInterleaveFactor(unsigned VF)
Class to represent vector types.
Definition: DerivedTypes.h:393
bool areInlineCompatible(const Function *Caller, const Function *Callee) const
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
Definition: Argument.h:48
static cl::opt< unsigned > Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
int getVectorInstrCost(unsigned Opcode, Type *ValTy, unsigned Index)
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:568
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:436
const std::vector< LoopT * > & getSubLoops() const
Return the loops contained entirely within this loop.
Definition: LoopInfo.h:131
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
Definition: PatternMatch.h:543
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:387
unsigned Threshold
The cost threshold for the unrolled loop.
const Function * getParent() const
Definition: Argument.h:42
unsigned getHardwareNumberOfRegisters(bool Vector) const
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:465
ArrayRef< BlockT * > getBlocks() const
Get a list of the basic blocks which make up this loop.
Definition: LoopInfo.h:149
Parameters that control the generic loop unrolling transformation.
Calling convention used for Mesa/AMDPAL hull shaders (= tessellation control shaders).
Definition: CallingConv.h:208
void getUnrollingPreferences(Loop *L, ScalarEvolution &SE, TTI::UnrollingPreferences &UP)
#define I(x, y, z)
Definition: MD5.cpp:58
iterator_range< value_op_iterator > operand_values()
Definition: User.h:262
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index)
Definition: BasicTTIImpl.h:812
static cl::opt< unsigned > UnrollThresholdLocal("amdgpu-unroll-threshold-local", cl::desc("Unroll threshold for AMDGPU if local memory used in a loop"), cl::init(1000), cl::Hidden)
const unsigned Kind
LLVM Value Representation.
Definition: Value.h:73
static cl::opt< unsigned > UnrollThresholdPrivate("amdgpu-unroll-threshold-private", cl::desc("Unroll threshold for AMDGPU if private memory used in a loop"), cl::init(2500), cl::Hidden)
Address space for region memory. (GDS)
Definition: AMDGPU.h:257
Broadcast element 0 to all other elements.
unsigned getMinVectorRegisterBitWidth() const
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
Type * getElementType() const
Definition: DerivedTypes.h:360
Value * PtrVal
This is the pointer that the intrinsic is loading from or storing to.
OperandValueKind
Additional information about an operand&#39;s possible values.
This pass exposes codegen information to IR-level passes.
bool isStaticAlloca() const
Return true if this alloca is in the entry block of the function and is a constant size...
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition: ValueTypes.h:126
void getUnrollingPreferences(Loop *L, ScalarEvolution &SE, TTI::UnrollingPreferences &UP)
#define LLVM_DEBUG(X)
Definition: Debug.h:123
Information about a load/store intrinsic defined by the target.
Calling convention for AMDGPU code object kernels.
Definition: CallingConv.h:201
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
unsigned getRegisterBitWidth(bool Vector) const
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44
an instruction to allocate memory on the stack
Definition: Instructions.h:60
ShuffleKind
The various kinds of shuffle patterns for vector queries.
Shuffle elements of single source vector with any shuffle mask.