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 "AMDGPUSubtarget.h" 19 #include "Utils/AMDGPUBaseInfo.h" 20 #include "llvm/ADT/STLExtras.h" 21 #include "llvm/Analysis/LoopInfo.h" 22 #include "llvm/Analysis/TargetTransformInfo.h" 23 #include "llvm/Analysis/ValueTracking.h" 24 #include "llvm/CodeGen/ISDOpcodes.h" 25 #include "llvm/CodeGen/ValueTypes.h" 26 #include "llvm/IR/Argument.h" 27 #include "llvm/IR/Attributes.h" 28 #include "llvm/IR/BasicBlock.h" 29 #include "llvm/IR/CallingConv.h" 30 #include "llvm/IR/DataLayout.h" 31 #include "llvm/IR/DerivedTypes.h" 32 #include "llvm/IR/Function.h" 33 #include "llvm/IR/Instruction.h" 34 #include "llvm/IR/Instructions.h" 35 #include "llvm/IR/IntrinsicInst.h" 36 #include "llvm/IR/Module.h" 37 #include "llvm/IR/PatternMatch.h" 38 #include "llvm/IR/Type.h" 39 #include "llvm/IR/Value.h" 40 #include "llvm/MC/SubtargetFeature.h" 41 #include "llvm/Support/Casting.h" 42 #include "llvm/Support/CommandLine.h" 43 #include "llvm/Support/Debug.h" 44 #include "llvm/Support/ErrorHandling.h" 45 #include "llvm/Support/MachineValueType.h" 46 #include "llvm/Support/raw_ostream.h" 47 #include "llvm/Target/TargetMachine.h" 48 #include <algorithm> 49 #include <cassert> 50 #include <limits> 51 #include <utility> 52 53 using namespace llvm; 54 55 #define DEBUG_TYPE "AMDGPUtti" 56 57 static cl::opt<unsigned> UnrollThresholdPrivate( 58 "amdgpu-unroll-threshold-private", 59 cl::desc("Unroll threshold for AMDGPU if private memory used in a loop"), 60 cl::init(2500), cl::Hidden); 61 62 static cl::opt<unsigned> UnrollThresholdLocal( 63 "amdgpu-unroll-threshold-local", 64 cl::desc("Unroll threshold for AMDGPU if local memory used in a loop"), 65 cl::init(1000), cl::Hidden); 66 67 static cl::opt<unsigned> UnrollThresholdIf( 68 "amdgpu-unroll-threshold-if", 69 cl::desc("Unroll threshold increment for AMDGPU for each if statement inside loop"), 70 cl::init(150), cl::Hidden); 71 72 static bool dependsOnLocalPhi(const Loop *L, const Value *Cond, 73 unsigned Depth = 0) { 74 const Instruction *I = dyn_cast<Instruction>(Cond); 75 if (!I) 76 return false; 77 78 for (const Value *V : I->operand_values()) { 79 if (!L->contains(I)) 80 continue; 81 if (const PHINode *PHI = dyn_cast<PHINode>(V)) { 82 if (llvm::none_of(L->getSubLoops(), [PHI](const Loop* SubLoop) { 83 return SubLoop->contains(PHI); })) 84 return true; 85 } else if (Depth < 10 && dependsOnLocalPhi(L, V, Depth+1)) 86 return true; 87 } 88 return false; 89 } 90 91 void AMDGPUTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE, 92 TTI::UnrollingPreferences &UP) { 93 UP.Threshold = 300; // Twice the default. 94 UP.MaxCount = std::numeric_limits<unsigned>::max(); 95 UP.Partial = true; 96 97 // TODO: Do we want runtime unrolling? 98 99 // Maximum alloca size than can fit registers. Reserve 16 registers. 100 const unsigned MaxAlloca = (256 - 16) * 4; 101 unsigned ThresholdPrivate = UnrollThresholdPrivate; 102 unsigned ThresholdLocal = UnrollThresholdLocal; 103 unsigned MaxBoost = std::max(ThresholdPrivate, ThresholdLocal); 104 for (const BasicBlock *BB : L->getBlocks()) { 105 const DataLayout &DL = BB->getModule()->getDataLayout(); 106 unsigned LocalGEPsSeen = 0; 107 108 if (llvm::any_of(L->getSubLoops(), [BB](const Loop* SubLoop) { 109 return SubLoop->contains(BB); })) 110 continue; // Block belongs to an inner loop. 111 112 for (const Instruction &I : *BB) { 113 // Unroll a loop which contains an "if" statement whose condition 114 // defined by a PHI belonging to the loop. This may help to eliminate 115 // if region and potentially even PHI itself, saving on both divergence 116 // and registers used for the PHI. 117 // Add a small bonus for each of such "if" statements. 118 if (const BranchInst *Br = dyn_cast<BranchInst>(&I)) { 119 if (UP.Threshold < MaxBoost && Br->isConditional()) { 120 if (L->isLoopExiting(Br->getSuccessor(0)) || 121 L->isLoopExiting(Br->getSuccessor(1))) 122 continue; 123 if (dependsOnLocalPhi(L, Br->getCondition())) { 124 UP.Threshold += UnrollThresholdIf; 125 LLVM_DEBUG(dbgs() << "Set unroll threshold " << UP.Threshold 126 << " for loop:\n" 127 << *L << " due to " << *Br << '\n'); 128 if (UP.Threshold >= MaxBoost) 129 return; 130 } 131 } 132 continue; 133 } 134 135 const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I); 136 if (!GEP) 137 continue; 138 139 unsigned AS = GEP->getAddressSpace(); 140 unsigned Threshold = 0; 141 if (AS == AMDGPUAS::PRIVATE_ADDRESS) 142 Threshold = ThresholdPrivate; 143 else if (AS == AMDGPUAS::LOCAL_ADDRESS) 144 Threshold = ThresholdLocal; 145 else 146 continue; 147 148 if (UP.Threshold >= Threshold) 149 continue; 150 151 if (AS == AMDGPUAS::PRIVATE_ADDRESS) { 152 const Value *Ptr = GEP->getPointerOperand(); 153 const AllocaInst *Alloca = 154 dyn_cast<AllocaInst>(GetUnderlyingObject(Ptr, DL)); 155 if (!Alloca || !Alloca->isStaticAlloca()) 156 continue; 157 Type *Ty = Alloca->getAllocatedType(); 158 unsigned AllocaSize = Ty->isSized() ? DL.getTypeAllocSize(Ty) : 0; 159 if (AllocaSize > MaxAlloca) 160 continue; 161 } else if (AS == AMDGPUAS::LOCAL_ADDRESS) { 162 LocalGEPsSeen++; 163 // Inhibit unroll for local memory if we have seen addressing not to 164 // a variable, most likely we will be unable to combine it. 165 // Do not unroll too deep inner loops for local memory to give a chance 166 // to unroll an outer loop for a more important reason. 167 if (LocalGEPsSeen > 1 || L->getLoopDepth() > 2 || 168 (!isa<GlobalVariable>(GEP->getPointerOperand()) && 169 !isa<Argument>(GEP->getPointerOperand()))) 170 continue; 171 } 172 173 // Check if GEP depends on a value defined by this loop itself. 174 bool HasLoopDef = false; 175 for (const Value *Op : GEP->operands()) { 176 const Instruction *Inst = dyn_cast<Instruction>(Op); 177 if (!Inst || L->isLoopInvariant(Op)) 178 continue; 179 180 if (llvm::any_of(L->getSubLoops(), [Inst](const Loop* SubLoop) { 181 return SubLoop->contains(Inst); })) 182 continue; 183 HasLoopDef = true; 184 break; 185 } 186 if (!HasLoopDef) 187 continue; 188 189 // We want to do whatever we can to limit the number of alloca 190 // instructions that make it through to the code generator. allocas 191 // require us to use indirect addressing, which is slow and prone to 192 // compiler bugs. If this loop does an address calculation on an 193 // alloca ptr, then we want to use a higher than normal loop unroll 194 // threshold. This will give SROA a better chance to eliminate these 195 // allocas. 196 // 197 // We also want to have more unrolling for local memory to let ds 198 // instructions with different offsets combine. 199 // 200 // Don't use the maximum allowed value here as it will make some 201 // programs way too big. 202 UP.Threshold = Threshold; 203 LLVM_DEBUG(dbgs() << "Set unroll threshold " << Threshold 204 << " for loop:\n" 205 << *L << " due to " << *GEP << '\n'); 206 if (UP.Threshold >= MaxBoost) 207 return; 208 } 209 } 210 } 211 212 unsigned GCNTTIImpl::getHardwareNumberOfRegisters(bool Vec) const { 213 // The concept of vector registers doesn't really exist. Some packed vector 214 // operations operate on the normal 32-bit registers. 215 return 256; 216 } 217 218 unsigned GCNTTIImpl::getNumberOfRegisters(bool Vec) const { 219 // This is really the number of registers to fill when vectorizing / 220 // interleaving loops, so we lie to avoid trying to use all registers. 221 return getHardwareNumberOfRegisters(Vec) >> 3; 222 } 223 224 unsigned GCNTTIImpl::getRegisterBitWidth(bool Vector) const { 225 return 32; 226 } 227 228 unsigned GCNTTIImpl::getMinVectorRegisterBitWidth() const { 229 return 32; 230 } 231 232 unsigned GCNTTIImpl::getLoadVectorFactor(unsigned VF, unsigned LoadSize, 233 unsigned ChainSizeInBytes, 234 VectorType *VecTy) const { 235 unsigned VecRegBitWidth = VF * LoadSize; 236 if (VecRegBitWidth > 128 && VecTy->getScalarSizeInBits() < 32) 237 // TODO: Support element-size less than 32bit? 238 return 128 / LoadSize; 239 240 return VF; 241 } 242 243 unsigned GCNTTIImpl::getStoreVectorFactor(unsigned VF, unsigned StoreSize, 244 unsigned ChainSizeInBytes, 245 VectorType *VecTy) const { 246 unsigned VecRegBitWidth = VF * StoreSize; 247 if (VecRegBitWidth > 128) 248 return 128 / StoreSize; 249 250 return VF; 251 } 252 253 unsigned GCNTTIImpl::getLoadStoreVecRegBitWidth(unsigned AddrSpace) const { 254 if (AddrSpace == AMDGPUAS::GLOBAL_ADDRESS || 255 AddrSpace == AMDGPUAS::CONSTANT_ADDRESS || 256 AddrSpace == AMDGPUAS::CONSTANT_ADDRESS_32BIT || 257 AddrSpace == AMDGPUAS::BUFFER_FAT_POINTER) { 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 306 bool GCNTTIImpl::getTgtMemIntrinsic(IntrinsicInst *Inst, 307 MemIntrinsicInfo &Info) const { 308 switch (Inst->getIntrinsicID()) { 309 case Intrinsic::amdgcn_atomic_inc: 310 case Intrinsic::amdgcn_atomic_dec: 311 case Intrinsic::amdgcn_ds_ordered_add: 312 case Intrinsic::amdgcn_ds_ordered_swap: 313 case Intrinsic::amdgcn_ds_fadd: 314 case Intrinsic::amdgcn_ds_fmin: 315 case Intrinsic::amdgcn_ds_fmax: { 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 337 int GCNTTIImpl::getArithmeticInstrCost( 338 unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info, 339 TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo, 340 TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args ) { 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 458 int GCNTTIImpl::getArithmeticReductionCost(unsigned Opcode, Type *Ty, 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 473 int GCNTTIImpl::getMinMaxReductionCost(Type *Ty, Type *CondTy, 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. 520 CallingConv::ID CC = F->getCallingConv(); 521 switch (CC) { 522 case CallingConv::AMDGPU_KERNEL: 523 case CallingConv::SPIR_KERNEL: 524 return true; 525 case CallingConv::AMDGPU_VS: 526 case CallingConv::AMDGPU_LS: 527 case CallingConv::AMDGPU_HS: 528 case CallingConv::AMDGPU_ES: 529 case CallingConv::AMDGPU_GS: 530 case CallingConv::AMDGPU_PS: 531 case CallingConv::AMDGPU_CS: 532 // For non-compute shaders, SGPR inputs are marked with either inreg or byval. 533 // Everything else is in VGPRs. 534 return F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::InReg) || 535 F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::ByVal); 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. 544 bool GCNTTIImpl::isSourceOfDivergence(const Value *V) const { 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; 580 case Intrinsic::amdgcn_readfirstlane: 581 case Intrinsic::amdgcn_readlane: 582 case Intrinsic::amdgcn_icmp: 583 case Intrinsic::amdgcn_fcmp: 584 return true; 585 } 586 } 587 return false; 588 } 589 590 unsigned GCNTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index, 591 Type *SubTp) { 592 if (ST->hasVOP3PInsts()) { 593 VectorType *VT = cast<VectorType>(Tp); 594 if (VT->getNumElements() == 2 && 595 DL.getTypeSizeInBits(VT->getElementType()) == 16) { 596 // With op_sel VOP3P instructions freely can access the low half or high 597 // half of a register, so any swizzle is free. 598 599 switch (Kind) { 600 case TTI::SK_Broadcast: 601 case TTI::SK_Reverse: 602 case TTI::SK_PermuteSingleSrc: 603 return 0; 604 default: 605 break; 606 } 607 } 608 } 609 610 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); 611 } 612 613 bool GCNTTIImpl::areInlineCompatible(const Function *Caller, 614 const Function *Callee) const { 615 const TargetMachine &TM = getTLI()->getTargetMachine(); 616 const FeatureBitset &CallerBits = 617 TM.getSubtargetImpl(*Caller)->getFeatureBits(); 618 const FeatureBitset &CalleeBits = 619 TM.getSubtargetImpl(*Callee)->getFeatureBits(); 620 621 FeatureBitset RealCallerBits = CallerBits & ~InlineFeatureIgnoreList; 622 FeatureBitset RealCalleeBits = CalleeBits & ~InlineFeatureIgnoreList; 623 if ((RealCallerBits & RealCalleeBits) != RealCalleeBits) 624 return false; 625 626 // FIXME: dx10_clamp can just take the caller setting, but there seems to be 627 // no way to support merge for backend defined attributes. 628 AMDGPU::SIModeRegisterDefaults CallerMode(*Caller); 629 AMDGPU::SIModeRegisterDefaults CalleeMode(*Callee); 630 return CallerMode.isInlineCompatible(CalleeMode); 631 } 632 633 void GCNTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE, 634 TTI::UnrollingPreferences &UP) { 635 CommonTTI.getUnrollingPreferences(L, SE, UP); 636 } 637 638 unsigned R600TTIImpl::getHardwareNumberOfRegisters(bool Vec) const { 639 return 4 * 128; // XXX - 4 channels. Should these count as vector instead? 640 } 641 642 unsigned R600TTIImpl::getNumberOfRegisters(bool Vec) const { 643 return getHardwareNumberOfRegisters(Vec); 644 } 645 646 unsigned R600TTIImpl::getRegisterBitWidth(bool Vector) const { 647 return 32; 648 } 649 650 unsigned R600TTIImpl::getMinVectorRegisterBitWidth() const { 651 return 32; 652 } 653 654 unsigned R600TTIImpl::getLoadStoreVecRegBitWidth(unsigned AddrSpace) const { 655 if (AddrSpace == AMDGPUAS::GLOBAL_ADDRESS || 656 AddrSpace == AMDGPUAS::CONSTANT_ADDRESS) 657 return 128; 658 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS || 659 AddrSpace == AMDGPUAS::REGION_ADDRESS) 660 return 64; 661 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) 662 return 32; 663 664 if ((AddrSpace == AMDGPUAS::PARAM_D_ADDRESS || 665 AddrSpace == AMDGPUAS::PARAM_I_ADDRESS || 666 (AddrSpace >= AMDGPUAS::CONSTANT_BUFFER_0 && 667 AddrSpace <= AMDGPUAS::CONSTANT_BUFFER_15))) 668 return 128; 669 llvm_unreachable("unhandled address space"); 670 } 671 672 bool R600TTIImpl::isLegalToVectorizeMemChain(unsigned ChainSizeInBytes, 673 unsigned Alignment, 674 unsigned AddrSpace) const { 675 // We allow vectorization of flat stores, even though we may need to decompose 676 // them later if they may access private memory. We don't have enough context 677 // here, and legalization can handle it. 678 return (AddrSpace != AMDGPUAS::PRIVATE_ADDRESS); 679 } 680 681 bool R600TTIImpl::isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes, 682 unsigned Alignment, 683 unsigned AddrSpace) const { 684 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace); 685 } 686 687 bool R600TTIImpl::isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes, 688 unsigned Alignment, 689 unsigned AddrSpace) const { 690 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace); 691 } 692 693 unsigned R600TTIImpl::getMaxInterleaveFactor(unsigned VF) { 694 // Disable unrolling if the loop is not vectorized. 695 // TODO: Enable this again. 696 if (VF == 1) 697 return 1; 698 699 return 8; 700 } 701 702 unsigned R600TTIImpl::getCFInstrCost(unsigned Opcode) { 703 // XXX - For some reason this isn't called for switch. 704 switch (Opcode) { 705 case Instruction::Br: 706 case Instruction::Ret: 707 return 10; 708 default: 709 return BaseT::getCFInstrCost(Opcode); 710 } 711 } 712 713 int R600TTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy, 714 unsigned Index) { 715 switch (Opcode) { 716 case Instruction::ExtractElement: 717 case Instruction::InsertElement: { 718 unsigned EltSize 719 = DL.getTypeSizeInBits(cast<VectorType>(ValTy)->getElementType()); 720 if (EltSize < 32) { 721 return BaseT::getVectorInstrCost(Opcode, ValTy, Index); 722 } 723 724 // Extracts are just reads of a subregister, so are free. Inserts are 725 // considered free because we don't want to have any cost for scalarizing 726 // operations, and we don't have to copy into a different register class. 727 728 // Dynamic indexing isn't free and is best avoided. 729 return Index == ~0u ? 2 : 0; 730 } 731 default: 732 return BaseT::getVectorInstrCost(Opcode, ValTy, Index); 733 } 734 } 735 736 void R600TTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE, 737 TTI::UnrollingPreferences &UP) { 738 CommonTTI.getUnrollingPreferences(L, SE, UP); 739 } 740