1 //===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===// 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 /// \brief Custom DAG lowering for SI 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifdef _MSC_VER 16 // Provide M_PI. 17 #define _USE_MATH_DEFINES 18 #include <cmath> 19 #endif 20 21 #include "AMDGPU.h" 22 #include "AMDGPUIntrinsicInfo.h" 23 #include "AMDGPUSubtarget.h" 24 #include "SIISelLowering.h" 25 #include "SIInstrInfo.h" 26 #include "SIMachineFunctionInfo.h" 27 #include "SIRegisterInfo.h" 28 #include "llvm/ADT/BitVector.h" 29 #include "llvm/ADT/StringSwitch.h" 30 #include "llvm/CodeGen/CallingConvLower.h" 31 #include "llvm/CodeGen/MachineInstrBuilder.h" 32 #include "llvm/CodeGen/MachineRegisterInfo.h" 33 #include "llvm/CodeGen/SelectionDAG.h" 34 #include "llvm/CodeGen/Analysis.h" 35 #include "llvm/IR/DiagnosticInfo.h" 36 #include "llvm/IR/Function.h" 37 38 using namespace llvm; 39 40 static cl::opt<bool> EnableVGPRIndexMode( 41 "amdgpu-vgpr-index-mode", 42 cl::desc("Use GPR indexing mode instead of movrel for vector indexing"), 43 cl::init(false)); 44 45 46 static unsigned findFirstFreeSGPR(CCState &CCInfo) { 47 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs(); 48 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) { 49 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) { 50 return AMDGPU::SGPR0 + Reg; 51 } 52 } 53 llvm_unreachable("Cannot allocate sgpr"); 54 } 55 56 SITargetLowering::SITargetLowering(const TargetMachine &TM, 57 const SISubtarget &STI) 58 : AMDGPUTargetLowering(TM, STI) { 59 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass); 60 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass); 61 62 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass); 63 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass); 64 65 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass); 66 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass); 67 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass); 68 69 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass); 70 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass); 71 72 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass); 73 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass); 74 75 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass); 76 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass); 77 78 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass); 79 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass); 80 81 computeRegisterProperties(STI.getRegisterInfo()); 82 83 // We need to custom lower vector stores from local memory 84 setOperationAction(ISD::LOAD, MVT::v2i32, Custom); 85 setOperationAction(ISD::LOAD, MVT::v4i32, Custom); 86 setOperationAction(ISD::LOAD, MVT::v8i32, Custom); 87 setOperationAction(ISD::LOAD, MVT::v16i32, Custom); 88 setOperationAction(ISD::LOAD, MVT::i1, Custom); 89 90 setOperationAction(ISD::STORE, MVT::v2i32, Custom); 91 setOperationAction(ISD::STORE, MVT::v4i32, Custom); 92 setOperationAction(ISD::STORE, MVT::v8i32, Custom); 93 setOperationAction(ISD::STORE, MVT::v16i32, Custom); 94 setOperationAction(ISD::STORE, MVT::i1, Custom); 95 96 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 97 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); 98 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand); 99 100 setOperationAction(ISD::SELECT, MVT::i1, Promote); 101 setOperationAction(ISD::SELECT, MVT::i64, Custom); 102 setOperationAction(ISD::SELECT, MVT::f64, Promote); 103 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64); 104 105 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); 106 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand); 107 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); 108 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); 109 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand); 110 111 setOperationAction(ISD::SETCC, MVT::i1, Promote); 112 setOperationAction(ISD::SETCC, MVT::v2i1, Expand); 113 setOperationAction(ISD::SETCC, MVT::v4i1, Expand); 114 115 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand); 116 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand); 117 118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom); 119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom); 120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom); 121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom); 122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom); 123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom); 124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom); 125 126 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom); 127 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom); 128 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom); 129 130 setOperationAction(ISD::BRCOND, MVT::Other, Custom); 131 setOperationAction(ISD::BR_CC, MVT::i1, Expand); 132 setOperationAction(ISD::BR_CC, MVT::i32, Expand); 133 setOperationAction(ISD::BR_CC, MVT::i64, Expand); 134 setOperationAction(ISD::BR_CC, MVT::f32, Expand); 135 setOperationAction(ISD::BR_CC, MVT::f64, Expand); 136 137 // We only support LOAD/STORE and vector manipulation ops for vectors 138 // with > 4 elements. 139 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) { 140 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) { 141 switch (Op) { 142 case ISD::LOAD: 143 case ISD::STORE: 144 case ISD::BUILD_VECTOR: 145 case ISD::BITCAST: 146 case ISD::EXTRACT_VECTOR_ELT: 147 case ISD::INSERT_VECTOR_ELT: 148 case ISD::INSERT_SUBVECTOR: 149 case ISD::EXTRACT_SUBVECTOR: 150 case ISD::SCALAR_TO_VECTOR: 151 break; 152 case ISD::CONCAT_VECTORS: 153 setOperationAction(Op, VT, Custom); 154 break; 155 default: 156 setOperationAction(Op, VT, Expand); 157 break; 158 } 159 } 160 } 161 162 // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that 163 // is expanded to avoid having two separate loops in case the index is a VGPR. 164 165 // Most operations are naturally 32-bit vector operations. We only support 166 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32. 167 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) { 168 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote); 169 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32); 170 171 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote); 172 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32); 173 174 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote); 175 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32); 176 177 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote); 178 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32); 179 } 180 181 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand); 182 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand); 183 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand); 184 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand); 185 186 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling, 187 // and output demarshalling 188 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom); 189 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom); 190 191 // We can't return success/failure, only the old value, 192 // let LLVM add the comparison 193 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand); 194 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand); 195 196 if (getSubtarget()->hasFlatAddressSpace()) { 197 setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom); 198 setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom); 199 } 200 201 setOperationAction(ISD::BSWAP, MVT::i32, Legal); 202 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal); 203 204 // On SI this is s_memtime and s_memrealtime on VI. 205 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal); 206 setOperationAction(ISD::TRAP, MVT::Other, Custom); 207 208 setOperationAction(ISD::FMINNUM, MVT::f64, Legal); 209 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal); 210 211 if (Subtarget->getGeneration() >= SISubtarget::SEA_ISLANDS) { 212 setOperationAction(ISD::FTRUNC, MVT::f64, Legal); 213 setOperationAction(ISD::FCEIL, MVT::f64, Legal); 214 setOperationAction(ISD::FRINT, MVT::f64, Legal); 215 } 216 217 setOperationAction(ISD::FFLOOR, MVT::f64, Legal); 218 219 setOperationAction(ISD::FSIN, MVT::f32, Custom); 220 setOperationAction(ISD::FCOS, MVT::f32, Custom); 221 setOperationAction(ISD::FDIV, MVT::f32, Custom); 222 setOperationAction(ISD::FDIV, MVT::f64, Custom); 223 224 setTargetDAGCombine(ISD::FADD); 225 setTargetDAGCombine(ISD::FSUB); 226 setTargetDAGCombine(ISD::FMINNUM); 227 setTargetDAGCombine(ISD::FMAXNUM); 228 setTargetDAGCombine(ISD::SMIN); 229 setTargetDAGCombine(ISD::SMAX); 230 setTargetDAGCombine(ISD::UMIN); 231 setTargetDAGCombine(ISD::UMAX); 232 setTargetDAGCombine(ISD::SETCC); 233 setTargetDAGCombine(ISD::AND); 234 setTargetDAGCombine(ISD::OR); 235 setTargetDAGCombine(ISD::XOR); 236 setTargetDAGCombine(ISD::UINT_TO_FP); 237 setTargetDAGCombine(ISD::FCANONICALIZE); 238 239 // All memory operations. Some folding on the pointer operand is done to help 240 // matching the constant offsets in the addressing modes. 241 setTargetDAGCombine(ISD::LOAD); 242 setTargetDAGCombine(ISD::STORE); 243 setTargetDAGCombine(ISD::ATOMIC_LOAD); 244 setTargetDAGCombine(ISD::ATOMIC_STORE); 245 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP); 246 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS); 247 setTargetDAGCombine(ISD::ATOMIC_SWAP); 248 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD); 249 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB); 250 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND); 251 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR); 252 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR); 253 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND); 254 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN); 255 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX); 256 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN); 257 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX); 258 259 setSchedulingPreference(Sched::RegPressure); 260 } 261 262 const SISubtarget *SITargetLowering::getSubtarget() const { 263 return static_cast<const SISubtarget *>(Subtarget); 264 } 265 266 //===----------------------------------------------------------------------===// 267 // TargetLowering queries 268 //===----------------------------------------------------------------------===// 269 270 bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 271 const CallInst &CI, 272 unsigned IntrID) const { 273 switch (IntrID) { 274 case Intrinsic::amdgcn_atomic_inc: 275 case Intrinsic::amdgcn_atomic_dec: 276 Info.opc = ISD::INTRINSIC_W_CHAIN; 277 Info.memVT = MVT::getVT(CI.getType()); 278 Info.ptrVal = CI.getOperand(0); 279 Info.align = 0; 280 Info.vol = false; 281 Info.readMem = true; 282 Info.writeMem = true; 283 return true; 284 default: 285 return false; 286 } 287 } 288 289 bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &, 290 EVT) const { 291 // SI has some legal vector types, but no legal vector operations. Say no 292 // shuffles are legal in order to prefer scalarizing some vector operations. 293 return false; 294 } 295 296 bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const { 297 // Flat instructions do not have offsets, and only have the register 298 // address. 299 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1); 300 } 301 302 bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const { 303 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and 304 // additionally can do r + r + i with addr64. 32-bit has more addressing 305 // mode options. Depending on the resource constant, it can also do 306 // (i64 r0) + (i32 r1) * (i14 i). 307 // 308 // Private arrays end up using a scratch buffer most of the time, so also 309 // assume those use MUBUF instructions. Scratch loads / stores are currently 310 // implemented as mubuf instructions with offen bit set, so slightly 311 // different than the normal addr64. 312 if (!isUInt<12>(AM.BaseOffs)) 313 return false; 314 315 // FIXME: Since we can split immediate into soffset and immediate offset, 316 // would it make sense to allow any immediate? 317 318 switch (AM.Scale) { 319 case 0: // r + i or just i, depending on HasBaseReg. 320 return true; 321 case 1: 322 return true; // We have r + r or r + i. 323 case 2: 324 if (AM.HasBaseReg) { 325 // Reject 2 * r + r. 326 return false; 327 } 328 329 // Allow 2 * r as r + r 330 // Or 2 * r + i is allowed as r + r + i. 331 return true; 332 default: // Don't allow n * r 333 return false; 334 } 335 } 336 337 bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL, 338 const AddrMode &AM, Type *Ty, 339 unsigned AS) const { 340 // No global is ever allowed as a base. 341 if (AM.BaseGV) 342 return false; 343 344 switch (AS) { 345 case AMDGPUAS::GLOBAL_ADDRESS: { 346 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 347 // Assume the we will use FLAT for all global memory accesses 348 // on VI. 349 // FIXME: This assumption is currently wrong. On VI we still use 350 // MUBUF instructions for the r + i addressing mode. As currently 351 // implemented, the MUBUF instructions only work on buffer < 4GB. 352 // It may be possible to support > 4GB buffers with MUBUF instructions, 353 // by setting the stride value in the resource descriptor which would 354 // increase the size limit to (stride * 4GB). However, this is risky, 355 // because it has never been validated. 356 return isLegalFlatAddressingMode(AM); 357 } 358 359 return isLegalMUBUFAddressingMode(AM); 360 } 361 case AMDGPUAS::CONSTANT_ADDRESS: { 362 // If the offset isn't a multiple of 4, it probably isn't going to be 363 // correctly aligned. 364 // FIXME: Can we get the real alignment here? 365 if (AM.BaseOffs % 4 != 0) 366 return isLegalMUBUFAddressingMode(AM); 367 368 // There are no SMRD extloads, so if we have to do a small type access we 369 // will use a MUBUF load. 370 // FIXME?: We also need to do this if unaligned, but we don't know the 371 // alignment here. 372 if (DL.getTypeStoreSize(Ty) < 4) 373 return isLegalMUBUFAddressingMode(AM); 374 375 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) { 376 // SMRD instructions have an 8-bit, dword offset on SI. 377 if (!isUInt<8>(AM.BaseOffs / 4)) 378 return false; 379 } else if (Subtarget->getGeneration() == SISubtarget::SEA_ISLANDS) { 380 // On CI+, this can also be a 32-bit literal constant offset. If it fits 381 // in 8-bits, it can use a smaller encoding. 382 if (!isUInt<32>(AM.BaseOffs / 4)) 383 return false; 384 } else if (Subtarget->getGeneration() == SISubtarget::VOLCANIC_ISLANDS) { 385 // On VI, these use the SMEM format and the offset is 20-bit in bytes. 386 if (!isUInt<20>(AM.BaseOffs)) 387 return false; 388 } else 389 llvm_unreachable("unhandled generation"); 390 391 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg. 392 return true; 393 394 if (AM.Scale == 1 && AM.HasBaseReg) 395 return true; 396 397 return false; 398 } 399 400 case AMDGPUAS::PRIVATE_ADDRESS: 401 return isLegalMUBUFAddressingMode(AM); 402 403 case AMDGPUAS::LOCAL_ADDRESS: 404 case AMDGPUAS::REGION_ADDRESS: { 405 // Basic, single offset DS instructions allow a 16-bit unsigned immediate 406 // field. 407 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have 408 // an 8-bit dword offset but we don't know the alignment here. 409 if (!isUInt<16>(AM.BaseOffs)) 410 return false; 411 412 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg. 413 return true; 414 415 if (AM.Scale == 1 && AM.HasBaseReg) 416 return true; 417 418 return false; 419 } 420 case AMDGPUAS::FLAT_ADDRESS: 421 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE: 422 // For an unknown address space, this usually means that this is for some 423 // reason being used for pure arithmetic, and not based on some addressing 424 // computation. We don't have instructions that compute pointers with any 425 // addressing modes, so treat them as having no offset like flat 426 // instructions. 427 return isLegalFlatAddressingMode(AM); 428 429 default: 430 llvm_unreachable("unhandled address space"); 431 } 432 } 433 434 bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT, 435 unsigned AddrSpace, 436 unsigned Align, 437 bool *IsFast) const { 438 if (IsFast) 439 *IsFast = false; 440 441 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96, 442 // which isn't a simple VT. 443 // Until MVT is extended to handle this, simply check for the size and 444 // rely on the condition below: allow accesses if the size is a multiple of 4. 445 if (VT == MVT::Other || (VT != MVT::Other && VT.getSizeInBits() > 1024 && 446 VT.getStoreSize() > 16)) { 447 return false; 448 } 449 450 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS || 451 AddrSpace == AMDGPUAS::REGION_ADDRESS) { 452 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte 453 // aligned, 8 byte access in a single operation using ds_read2/write2_b32 454 // with adjacent offsets. 455 bool AlignedBy4 = (Align % 4 == 0); 456 if (IsFast) 457 *IsFast = AlignedBy4; 458 459 return AlignedBy4; 460 } 461 462 if (Subtarget->hasUnalignedBufferAccess()) { 463 // If we have an uniform constant load, it still requires using a slow 464 // buffer instruction if unaligned. 465 if (IsFast) { 466 *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS) ? 467 (Align % 4 == 0) : true; 468 } 469 470 return true; 471 } 472 473 // Smaller than dword value must be aligned. 474 if (VT.bitsLT(MVT::i32)) 475 return false; 476 477 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the 478 // byte-address are ignored, thus forcing Dword alignment. 479 // This applies to private, global, and constant memory. 480 if (IsFast) 481 *IsFast = true; 482 483 return VT.bitsGT(MVT::i32) && Align % 4 == 0; 484 } 485 486 EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign, 487 unsigned SrcAlign, bool IsMemset, 488 bool ZeroMemset, 489 bool MemcpyStrSrc, 490 MachineFunction &MF) const { 491 // FIXME: Should account for address space here. 492 493 // The default fallback uses the private pointer size as a guess for a type to 494 // use. Make sure we switch these to 64-bit accesses. 495 496 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global 497 return MVT::v4i32; 498 499 if (Size >= 8 && DstAlign >= 4) 500 return MVT::v2i32; 501 502 // Use the default. 503 return MVT::Other; 504 } 505 506 static bool isFlatGlobalAddrSpace(unsigned AS) { 507 return AS == AMDGPUAS::GLOBAL_ADDRESS || 508 AS == AMDGPUAS::FLAT_ADDRESS || 509 AS == AMDGPUAS::CONSTANT_ADDRESS; 510 } 511 512 bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS, 513 unsigned DestAS) const { 514 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS); 515 } 516 517 bool SITargetLowering::isMemOpUniform(const SDNode *N) const { 518 const MemSDNode *MemNode = cast<MemSDNode>(N); 519 const Value *Ptr = MemNode->getMemOperand()->getValue(); 520 521 // UndefValue means this is a load of a kernel input. These are uniform. 522 // Sometimes LDS instructions have constant pointers. 523 // If Ptr is null, then that means this mem operand contains a 524 // PseudoSourceValue like GOT. 525 if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) || 526 isa<Constant>(Ptr) || isa<GlobalValue>(Ptr)) 527 return true; 528 529 const Instruction *I = dyn_cast<Instruction>(Ptr); 530 return I && I->getMetadata("amdgpu.uniform"); 531 } 532 533 TargetLoweringBase::LegalizeTypeAction 534 SITargetLowering::getPreferredVectorAction(EVT VT) const { 535 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16)) 536 return TypeSplitVector; 537 538 return TargetLoweringBase::getPreferredVectorAction(VT); 539 } 540 541 bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, 542 Type *Ty) const { 543 // FIXME: Could be smarter if called for vector constants. 544 return true; 545 } 546 547 bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const { 548 549 // i16 is not desirable unless it is a load or a store. 550 if (VT == MVT::i16 && Op != ISD::LOAD && Op != ISD::STORE) 551 return false; 552 553 // SimplifySetCC uses this function to determine whether or not it should 554 // create setcc with i1 operands. We don't have instructions for i1 setcc. 555 if (VT == MVT::i1 && Op == ISD::SETCC) 556 return false; 557 558 return TargetLowering::isTypeDesirableForOp(Op, VT); 559 } 560 561 SDValue SITargetLowering::LowerParameterPtr(SelectionDAG &DAG, 562 const SDLoc &SL, SDValue Chain, 563 unsigned Offset) const { 564 const DataLayout &DL = DAG.getDataLayout(); 565 MachineFunction &MF = DAG.getMachineFunction(); 566 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 567 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR); 568 569 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); 570 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS); 571 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL, 572 MRI.getLiveInVirtReg(InputPtrReg), PtrVT); 573 return DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr, 574 DAG.getConstant(Offset, SL, PtrVT)); 575 } 576 SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT, 577 const SDLoc &SL, SDValue Chain, 578 unsigned Offset, bool Signed) const { 579 const DataLayout &DL = DAG.getDataLayout(); 580 Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 581 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS); 582 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS); 583 SDValue PtrOffset = DAG.getUNDEF(PtrVT); 584 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); 585 586 unsigned Align = DL.getABITypeAlignment(Ty); 587 588 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD; 589 if (MemVT.isFloatingPoint()) 590 ExtTy = ISD::EXTLOAD; 591 592 SDValue Ptr = LowerParameterPtr(DAG, SL, Chain, Offset); 593 return DAG.getLoad(ISD::UNINDEXED, ExtTy, VT, SL, Chain, Ptr, PtrOffset, 594 PtrInfo, MemVT, Align, 595 MachineMemOperand::MONonTemporal | 596 MachineMemOperand::MODereferenceable | 597 MachineMemOperand::MOInvariant); 598 } 599 600 SDValue SITargetLowering::LowerFormalArguments( 601 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 602 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 603 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 604 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 605 606 MachineFunction &MF = DAG.getMachineFunction(); 607 FunctionType *FType = MF.getFunction()->getFunctionType(); 608 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 609 const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); 610 611 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) { 612 const Function *Fn = MF.getFunction(); 613 DiagnosticInfoUnsupported NoGraphicsHSA( 614 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc()); 615 DAG.getContext()->diagnose(NoGraphicsHSA); 616 return DAG.getEntryNode(); 617 } 618 619 // Create stack objects that are used for emitting debugger prologue if 620 // "amdgpu-debugger-emit-prologue" attribute was specified. 621 if (ST.debuggerEmitPrologue()) 622 createDebuggerPrologueStackObjects(MF); 623 624 SmallVector<ISD::InputArg, 16> Splits; 625 BitVector Skipped(Ins.size()); 626 627 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) { 628 const ISD::InputArg &Arg = Ins[i]; 629 630 // First check if it's a PS input addr 631 if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() && 632 !Arg.Flags.isByVal() && PSInputNum <= 15) { 633 634 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) { 635 // We can safely skip PS inputs 636 Skipped.set(i); 637 ++PSInputNum; 638 continue; 639 } 640 641 Info->markPSInputAllocated(PSInputNum); 642 if (Arg.Used) 643 Info->PSInputEna |= 1 << PSInputNum; 644 645 ++PSInputNum; 646 } 647 648 if (AMDGPU::isShader(CallConv)) { 649 // Second split vertices into their elements 650 if (Arg.VT.isVector()) { 651 ISD::InputArg NewArg = Arg; 652 NewArg.Flags.setSplit(); 653 NewArg.VT = Arg.VT.getVectorElementType(); 654 655 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a 656 // three or five element vertex only needs three or five registers, 657 // NOT four or eight. 658 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex()); 659 unsigned NumElements = ParamType->getVectorNumElements(); 660 661 for (unsigned j = 0; j != NumElements; ++j) { 662 Splits.push_back(NewArg); 663 NewArg.PartOffset += NewArg.VT.getStoreSize(); 664 } 665 } else { 666 Splits.push_back(Arg); 667 } 668 } 669 } 670 671 SmallVector<CCValAssign, 16> ArgLocs; 672 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 673 *DAG.getContext()); 674 675 // At least one interpolation mode must be enabled or else the GPU will hang. 676 // 677 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set 678 // PSInputAddr, the user wants to enable some bits after the compilation 679 // based on run-time states. Since we can't know what the final PSInputEna 680 // will look like, so we shouldn't do anything here and the user should take 681 // responsibility for the correct programming. 682 // 683 // Otherwise, the following restrictions apply: 684 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled. 685 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be 686 // enabled too. 687 if (CallConv == CallingConv::AMDGPU_PS && 688 ((Info->getPSInputAddr() & 0x7F) == 0 || 689 ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11)))) { 690 CCInfo.AllocateReg(AMDGPU::VGPR0); 691 CCInfo.AllocateReg(AMDGPU::VGPR1); 692 Info->markPSInputAllocated(0); 693 Info->PSInputEna |= 1; 694 } 695 696 if (!AMDGPU::isShader(CallConv)) { 697 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX()); 698 } else { 699 assert(!Info->hasPrivateSegmentBuffer() && !Info->hasDispatchPtr() && 700 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() && 701 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() && 702 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() && 703 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() && 704 !Info->hasWorkItemIDZ()); 705 } 706 707 // FIXME: How should these inputs interact with inreg / custom SGPR inputs? 708 if (Info->hasPrivateSegmentBuffer()) { 709 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI); 710 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass); 711 CCInfo.AllocateReg(PrivateSegmentBufferReg); 712 } 713 714 if (Info->hasDispatchPtr()) { 715 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI); 716 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass); 717 CCInfo.AllocateReg(DispatchPtrReg); 718 } 719 720 if (Info->hasQueuePtr()) { 721 unsigned QueuePtrReg = Info->addQueuePtr(*TRI); 722 MF.addLiveIn(QueuePtrReg, &AMDGPU::SReg_64RegClass); 723 CCInfo.AllocateReg(QueuePtrReg); 724 } 725 726 if (Info->hasKernargSegmentPtr()) { 727 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI); 728 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass); 729 CCInfo.AllocateReg(InputPtrReg); 730 } 731 732 if (Info->hasDispatchID()) { 733 unsigned DispatchIDReg = Info->addDispatchID(*TRI); 734 MF.addLiveIn(DispatchIDReg, &AMDGPU::SReg_64RegClass); 735 CCInfo.AllocateReg(DispatchIDReg); 736 } 737 738 if (Info->hasFlatScratchInit()) { 739 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI); 740 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SReg_64RegClass); 741 CCInfo.AllocateReg(FlatScratchInitReg); 742 } 743 744 if (!AMDGPU::isShader(CallConv)) 745 analyzeFormalArgumentsCompute(CCInfo, Ins); 746 else 747 AnalyzeFormalArguments(CCInfo, Splits); 748 749 SmallVector<SDValue, 16> Chains; 750 751 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) { 752 753 const ISD::InputArg &Arg = Ins[i]; 754 if (Skipped[i]) { 755 InVals.push_back(DAG.getUNDEF(Arg.VT)); 756 continue; 757 } 758 759 CCValAssign &VA = ArgLocs[ArgIdx++]; 760 MVT VT = VA.getLocVT(); 761 762 if (VA.isMemLoc()) { 763 VT = Ins[i].VT; 764 EVT MemVT = VA.getLocVT(); 765 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() + 766 VA.getLocMemOffset(); 767 // The first 36 bytes of the input buffer contains information about 768 // thread group and global sizes. 769 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain, 770 Offset, Ins[i].Flags.isSExt()); 771 Chains.push_back(Arg.getValue(1)); 772 773 auto *ParamTy = 774 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex())); 775 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS && 776 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) { 777 // On SI local pointers are just offsets into LDS, so they are always 778 // less than 16-bits. On CI and newer they could potentially be 779 // real pointers, so we can't guarantee their size. 780 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg, 781 DAG.getValueType(MVT::i16)); 782 } 783 784 InVals.push_back(Arg); 785 Info->setABIArgOffset(Offset + MemVT.getStoreSize()); 786 continue; 787 } 788 assert(VA.isRegLoc() && "Parameter must be in a register!"); 789 790 unsigned Reg = VA.getLocReg(); 791 792 if (VT == MVT::i64) { 793 // For now assume it is a pointer 794 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0, 795 &AMDGPU::SReg_64RegClass); 796 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass); 797 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT); 798 InVals.push_back(Copy); 799 continue; 800 } 801 802 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT); 803 804 Reg = MF.addLiveIn(Reg, RC); 805 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT); 806 807 if (Arg.VT.isVector()) { 808 809 // Build a vector from the registers 810 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex()); 811 unsigned NumElements = ParamType->getVectorNumElements(); 812 813 SmallVector<SDValue, 4> Regs; 814 Regs.push_back(Val); 815 for (unsigned j = 1; j != NumElements; ++j) { 816 Reg = ArgLocs[ArgIdx++].getLocReg(); 817 Reg = MF.addLiveIn(Reg, RC); 818 819 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT); 820 Regs.push_back(Copy); 821 } 822 823 // Fill up the missing vector elements 824 NumElements = Arg.VT.getVectorNumElements() - NumElements; 825 Regs.append(NumElements, DAG.getUNDEF(VT)); 826 827 InVals.push_back(DAG.getBuildVector(Arg.VT, DL, Regs)); 828 continue; 829 } 830 831 InVals.push_back(Val); 832 } 833 834 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read 835 // these from the dispatch pointer. 836 837 // Start adding system SGPRs. 838 if (Info->hasWorkGroupIDX()) { 839 unsigned Reg = Info->addWorkGroupIDX(); 840 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); 841 CCInfo.AllocateReg(Reg); 842 } 843 844 if (Info->hasWorkGroupIDY()) { 845 unsigned Reg = Info->addWorkGroupIDY(); 846 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); 847 CCInfo.AllocateReg(Reg); 848 } 849 850 if (Info->hasWorkGroupIDZ()) { 851 unsigned Reg = Info->addWorkGroupIDZ(); 852 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); 853 CCInfo.AllocateReg(Reg); 854 } 855 856 if (Info->hasWorkGroupInfo()) { 857 unsigned Reg = Info->addWorkGroupInfo(); 858 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); 859 CCInfo.AllocateReg(Reg); 860 } 861 862 if (Info->hasPrivateSegmentWaveByteOffset()) { 863 // Scratch wave offset passed in system SGPR. 864 unsigned PrivateSegmentWaveByteOffsetReg; 865 866 if (AMDGPU::isShader(CallConv)) { 867 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo); 868 Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg); 869 } else 870 PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset(); 871 872 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass); 873 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg); 874 } 875 876 // Now that we've figured out where the scratch register inputs are, see if 877 // should reserve the arguments and use them directly. 878 bool HasStackObjects = MF.getFrameInfo().hasStackObjects(); 879 // Record that we know we have non-spill stack objects so we don't need to 880 // check all stack objects later. 881 if (HasStackObjects) 882 Info->setHasNonSpillStackObjects(true); 883 884 // Everything live out of a block is spilled with fast regalloc, so it's 885 // almost certain that spilling will be required. 886 if (getTargetMachine().getOptLevel() == CodeGenOpt::None) 887 HasStackObjects = true; 888 889 if (ST.isAmdCodeObjectV2()) { 890 if (HasStackObjects) { 891 // If we have stack objects, we unquestionably need the private buffer 892 // resource. For the Code Object V2 ABI, this will be the first 4 user 893 // SGPR inputs. We can reserve those and use them directly. 894 895 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue( 896 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER); 897 Info->setScratchRSrcReg(PrivateSegmentBufferReg); 898 899 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue( 900 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); 901 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg); 902 } else { 903 unsigned ReservedBufferReg 904 = TRI->reservedPrivateSegmentBufferReg(MF); 905 unsigned ReservedOffsetReg 906 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); 907 908 // We tentatively reserve the last registers (skipping the last two 909 // which may contain VCC). After register allocation, we'll replace 910 // these with the ones immediately after those which were really 911 // allocated. In the prologue copies will be inserted from the argument 912 // to these reserved registers. 913 Info->setScratchRSrcReg(ReservedBufferReg); 914 Info->setScratchWaveOffsetReg(ReservedOffsetReg); 915 } 916 } else { 917 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF); 918 919 // Without HSA, relocations are used for the scratch pointer and the 920 // buffer resource setup is always inserted in the prologue. Scratch wave 921 // offset is still in an input SGPR. 922 Info->setScratchRSrcReg(ReservedBufferReg); 923 924 if (HasStackObjects) { 925 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue( 926 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); 927 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg); 928 } else { 929 unsigned ReservedOffsetReg 930 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); 931 Info->setScratchWaveOffsetReg(ReservedOffsetReg); 932 } 933 } 934 935 if (Info->hasWorkItemIDX()) { 936 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X); 937 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 938 CCInfo.AllocateReg(Reg); 939 } 940 941 if (Info->hasWorkItemIDY()) { 942 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y); 943 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 944 CCInfo.AllocateReg(Reg); 945 } 946 947 if (Info->hasWorkItemIDZ()) { 948 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z); 949 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 950 CCInfo.AllocateReg(Reg); 951 } 952 953 if (Chains.empty()) 954 return Chain; 955 956 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 957 } 958 959 SDValue 960 SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 961 bool isVarArg, 962 const SmallVectorImpl<ISD::OutputArg> &Outs, 963 const SmallVectorImpl<SDValue> &OutVals, 964 const SDLoc &DL, SelectionDAG &DAG) const { 965 MachineFunction &MF = DAG.getMachineFunction(); 966 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 967 968 if (!AMDGPU::isShader(CallConv)) 969 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs, 970 OutVals, DL, DAG); 971 972 Info->setIfReturnsVoid(Outs.size() == 0); 973 974 SmallVector<ISD::OutputArg, 48> Splits; 975 SmallVector<SDValue, 48> SplitVals; 976 977 // Split vectors into their elements. 978 for (unsigned i = 0, e = Outs.size(); i != e; ++i) { 979 const ISD::OutputArg &Out = Outs[i]; 980 981 if (Out.VT.isVector()) { 982 MVT VT = Out.VT.getVectorElementType(); 983 ISD::OutputArg NewOut = Out; 984 NewOut.Flags.setSplit(); 985 NewOut.VT = VT; 986 987 // We want the original number of vector elements here, e.g. 988 // three or five, not four or eight. 989 unsigned NumElements = Out.ArgVT.getVectorNumElements(); 990 991 for (unsigned j = 0; j != NumElements; ++j) { 992 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i], 993 DAG.getConstant(j, DL, MVT::i32)); 994 SplitVals.push_back(Elem); 995 Splits.push_back(NewOut); 996 NewOut.PartOffset += NewOut.VT.getStoreSize(); 997 } 998 } else { 999 SplitVals.push_back(OutVals[i]); 1000 Splits.push_back(Out); 1001 } 1002 } 1003 1004 // CCValAssign - represent the assignment of the return value to a location. 1005 SmallVector<CCValAssign, 48> RVLocs; 1006 1007 // CCState - Info about the registers and stack slots. 1008 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 1009 *DAG.getContext()); 1010 1011 // Analyze outgoing return values. 1012 AnalyzeReturn(CCInfo, Splits); 1013 1014 SDValue Flag; 1015 SmallVector<SDValue, 48> RetOps; 1016 RetOps.push_back(Chain); // Operand #0 = Chain (updated below) 1017 1018 // Copy the result values into the output registers. 1019 for (unsigned i = 0, realRVLocIdx = 0; 1020 i != RVLocs.size(); 1021 ++i, ++realRVLocIdx) { 1022 CCValAssign &VA = RVLocs[i]; 1023 assert(VA.isRegLoc() && "Can only return in registers!"); 1024 1025 SDValue Arg = SplitVals[realRVLocIdx]; 1026 1027 // Copied from other backends. 1028 switch (VA.getLocInfo()) { 1029 default: llvm_unreachable("Unknown loc info!"); 1030 case CCValAssign::Full: 1031 break; 1032 case CCValAssign::BCvt: 1033 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg); 1034 break; 1035 } 1036 1037 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag); 1038 Flag = Chain.getValue(1); 1039 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 1040 } 1041 1042 // Update chain and glue. 1043 RetOps[0] = Chain; 1044 if (Flag.getNode()) 1045 RetOps.push_back(Flag); 1046 1047 unsigned Opc = Info->returnsVoid() ? AMDGPUISD::ENDPGM : AMDGPUISD::RETURN; 1048 return DAG.getNode(Opc, DL, MVT::Other, RetOps); 1049 } 1050 1051 unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT, 1052 SelectionDAG &DAG) const { 1053 unsigned Reg = StringSwitch<unsigned>(RegName) 1054 .Case("m0", AMDGPU::M0) 1055 .Case("exec", AMDGPU::EXEC) 1056 .Case("exec_lo", AMDGPU::EXEC_LO) 1057 .Case("exec_hi", AMDGPU::EXEC_HI) 1058 .Case("flat_scratch", AMDGPU::FLAT_SCR) 1059 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO) 1060 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI) 1061 .Default(AMDGPU::NoRegister); 1062 1063 if (Reg == AMDGPU::NoRegister) { 1064 report_fatal_error(Twine("invalid register name \"" 1065 + StringRef(RegName) + "\".")); 1066 1067 } 1068 1069 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS && 1070 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) { 1071 report_fatal_error(Twine("invalid register \"" 1072 + StringRef(RegName) + "\" for subtarget.")); 1073 } 1074 1075 switch (Reg) { 1076 case AMDGPU::M0: 1077 case AMDGPU::EXEC_LO: 1078 case AMDGPU::EXEC_HI: 1079 case AMDGPU::FLAT_SCR_LO: 1080 case AMDGPU::FLAT_SCR_HI: 1081 if (VT.getSizeInBits() == 32) 1082 return Reg; 1083 break; 1084 case AMDGPU::EXEC: 1085 case AMDGPU::FLAT_SCR: 1086 if (VT.getSizeInBits() == 64) 1087 return Reg; 1088 break; 1089 default: 1090 llvm_unreachable("missing register type checking"); 1091 } 1092 1093 report_fatal_error(Twine("invalid type for register \"" 1094 + StringRef(RegName) + "\".")); 1095 } 1096 1097 // If kill is not the last instruction, split the block so kill is always a 1098 // proper terminator. 1099 MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI, 1100 MachineBasicBlock *BB) const { 1101 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 1102 1103 MachineBasicBlock::iterator SplitPoint(&MI); 1104 ++SplitPoint; 1105 1106 if (SplitPoint == BB->end()) { 1107 // Don't bother with a new block. 1108 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR)); 1109 return BB; 1110 } 1111 1112 MachineFunction *MF = BB->getParent(); 1113 MachineBasicBlock *SplitBB 1114 = MF->CreateMachineBasicBlock(BB->getBasicBlock()); 1115 1116 MF->insert(++MachineFunction::iterator(BB), SplitBB); 1117 SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end()); 1118 1119 SplitBB->transferSuccessorsAndUpdatePHIs(BB); 1120 BB->addSuccessor(SplitBB); 1121 1122 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR)); 1123 return SplitBB; 1124 } 1125 1126 // Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the 1127 // wavefront. If the value is uniform and just happens to be in a VGPR, this 1128 // will only do one iteration. In the worst case, this will loop 64 times. 1129 // 1130 // TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value. 1131 static MachineBasicBlock::iterator emitLoadM0FromVGPRLoop( 1132 const SIInstrInfo *TII, 1133 MachineRegisterInfo &MRI, 1134 MachineBasicBlock &OrigBB, 1135 MachineBasicBlock &LoopBB, 1136 const DebugLoc &DL, 1137 const MachineOperand &IdxReg, 1138 unsigned InitReg, 1139 unsigned ResultReg, 1140 unsigned PhiReg, 1141 unsigned InitSaveExecReg, 1142 int Offset, 1143 bool UseGPRIdxMode) { 1144 MachineBasicBlock::iterator I = LoopBB.begin(); 1145 1146 unsigned PhiExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1147 unsigned NewExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1148 unsigned CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 1149 unsigned CondReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1150 1151 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg) 1152 .addReg(InitReg) 1153 .addMBB(&OrigBB) 1154 .addReg(ResultReg) 1155 .addMBB(&LoopBB); 1156 1157 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec) 1158 .addReg(InitSaveExecReg) 1159 .addMBB(&OrigBB) 1160 .addReg(NewExec) 1161 .addMBB(&LoopBB); 1162 1163 // Read the next variant <- also loop target. 1164 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg) 1165 .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef())); 1166 1167 // Compare the just read M0 value to all possible Idx values. 1168 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg) 1169 .addReg(CurrentIdxReg) 1170 .addReg(IdxReg.getReg(), 0, IdxReg.getSubReg()); 1171 1172 if (UseGPRIdxMode) { 1173 unsigned IdxReg; 1174 if (Offset == 0) { 1175 IdxReg = CurrentIdxReg; 1176 } else { 1177 IdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 1178 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), IdxReg) 1179 .addReg(CurrentIdxReg, RegState::Kill) 1180 .addImm(Offset); 1181 } 1182 1183 MachineInstr *SetIdx = 1184 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_IDX)) 1185 .addReg(IdxReg, RegState::Kill); 1186 SetIdx->getOperand(2).setIsUndef(); 1187 } else { 1188 // Move index from VCC into M0 1189 if (Offset == 0) { 1190 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 1191 .addReg(CurrentIdxReg, RegState::Kill); 1192 } else { 1193 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0) 1194 .addReg(CurrentIdxReg, RegState::Kill) 1195 .addImm(Offset); 1196 } 1197 } 1198 1199 // Update EXEC, save the original EXEC value to VCC. 1200 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), NewExec) 1201 .addReg(CondReg, RegState::Kill); 1202 1203 MRI.setSimpleHint(NewExec, CondReg); 1204 1205 // Update EXEC, switch all done bits to 0 and all todo bits to 1. 1206 MachineInstr *InsertPt = 1207 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC) 1208 .addReg(AMDGPU::EXEC) 1209 .addReg(NewExec); 1210 1211 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use 1212 // s_cbranch_scc0? 1213 1214 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover. 1215 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ)) 1216 .addMBB(&LoopBB); 1217 1218 return InsertPt->getIterator(); 1219 } 1220 1221 // This has slightly sub-optimal regalloc when the source vector is killed by 1222 // the read. The register allocator does not understand that the kill is 1223 // per-workitem, so is kept alive for the whole loop so we end up not re-using a 1224 // subregister from it, using 1 more VGPR than necessary. This was saved when 1225 // this was expanded after register allocation. 1226 static MachineBasicBlock::iterator loadM0FromVGPR(const SIInstrInfo *TII, 1227 MachineBasicBlock &MBB, 1228 MachineInstr &MI, 1229 unsigned InitResultReg, 1230 unsigned PhiReg, 1231 int Offset, 1232 bool UseGPRIdxMode) { 1233 MachineFunction *MF = MBB.getParent(); 1234 MachineRegisterInfo &MRI = MF->getRegInfo(); 1235 const DebugLoc &DL = MI.getDebugLoc(); 1236 MachineBasicBlock::iterator I(&MI); 1237 1238 unsigned DstReg = MI.getOperand(0).getReg(); 1239 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1240 unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1241 1242 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec); 1243 1244 // Save the EXEC mask 1245 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec) 1246 .addReg(AMDGPU::EXEC); 1247 1248 // To insert the loop we need to split the block. Move everything after this 1249 // point to a new block, and insert a new empty block between the two. 1250 MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock(); 1251 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock(); 1252 MachineFunction::iterator MBBI(MBB); 1253 ++MBBI; 1254 1255 MF->insert(MBBI, LoopBB); 1256 MF->insert(MBBI, RemainderBB); 1257 1258 LoopBB->addSuccessor(LoopBB); 1259 LoopBB->addSuccessor(RemainderBB); 1260 1261 // Move the rest of the block into a new block. 1262 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB); 1263 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end()); 1264 1265 MBB.addSuccessor(LoopBB); 1266 1267 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 1268 1269 auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx, 1270 InitResultReg, DstReg, PhiReg, TmpExec, 1271 Offset, UseGPRIdxMode); 1272 1273 MachineBasicBlock::iterator First = RemainderBB->begin(); 1274 BuildMI(*RemainderBB, First, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC) 1275 .addReg(SaveExec); 1276 1277 MI.eraseFromParent(); 1278 1279 return InsPt; 1280 } 1281 1282 // Returns subreg index, offset 1283 static std::pair<unsigned, int> 1284 computeIndirectRegAndOffset(const SIRegisterInfo &TRI, 1285 const TargetRegisterClass *SuperRC, 1286 unsigned VecReg, 1287 int Offset) { 1288 int NumElts = SuperRC->getSize() / 4; 1289 1290 // Skip out of bounds offsets, or else we would end up using an undefined 1291 // register. 1292 if (Offset >= NumElts || Offset < 0) 1293 return std::make_pair(AMDGPU::sub0, Offset); 1294 1295 return std::make_pair(AMDGPU::sub0 + Offset, 0); 1296 } 1297 1298 // Return true if the index is an SGPR and was set. 1299 static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII, 1300 MachineRegisterInfo &MRI, 1301 MachineInstr &MI, 1302 int Offset, 1303 bool UseGPRIdxMode, 1304 bool IsIndirectSrc) { 1305 MachineBasicBlock *MBB = MI.getParent(); 1306 const DebugLoc &DL = MI.getDebugLoc(); 1307 MachineBasicBlock::iterator I(&MI); 1308 1309 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 1310 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg()); 1311 1312 assert(Idx->getReg() != AMDGPU::NoRegister); 1313 1314 if (!TII->getRegisterInfo().isSGPRClass(IdxRC)) 1315 return false; 1316 1317 if (UseGPRIdxMode) { 1318 unsigned IdxMode = IsIndirectSrc ? 1319 VGPRIndexMode::SRC0_ENABLE : VGPRIndexMode::DST_ENABLE; 1320 if (Offset == 0) { 1321 MachineInstr *SetOn = 1322 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 1323 .addOperand(*Idx) 1324 .addImm(IdxMode); 1325 1326 SetOn->getOperand(3).setIsUndef(); 1327 } else { 1328 unsigned Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 1329 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp) 1330 .addOperand(*Idx) 1331 .addImm(Offset); 1332 MachineInstr *SetOn = 1333 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 1334 .addReg(Tmp, RegState::Kill) 1335 .addImm(IdxMode); 1336 1337 SetOn->getOperand(3).setIsUndef(); 1338 } 1339 1340 return true; 1341 } 1342 1343 if (Offset == 0) { 1344 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 1345 .addOperand(*Idx); 1346 } else { 1347 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0) 1348 .addOperand(*Idx) 1349 .addImm(Offset); 1350 } 1351 1352 return true; 1353 } 1354 1355 // Control flow needs to be inserted if indexing with a VGPR. 1356 static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI, 1357 MachineBasicBlock &MBB, 1358 const SISubtarget &ST) { 1359 const SIInstrInfo *TII = ST.getInstrInfo(); 1360 const SIRegisterInfo &TRI = TII->getRegisterInfo(); 1361 MachineFunction *MF = MBB.getParent(); 1362 MachineRegisterInfo &MRI = MF->getRegInfo(); 1363 1364 unsigned Dst = MI.getOperand(0).getReg(); 1365 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src); 1366 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm(); 1367 1368 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg()); 1369 1370 unsigned SubReg; 1371 std::tie(SubReg, Offset) 1372 = computeIndirectRegAndOffset(TRI, VecRC, SrcVec->getReg(), Offset); 1373 1374 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode; 1375 1376 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, true)) { 1377 MachineBasicBlock::iterator I(&MI); 1378 const DebugLoc &DL = MI.getDebugLoc(); 1379 1380 if (UseGPRIdxMode) { 1381 // TODO: Look at the uses to avoid the copy. This may require rescheduling 1382 // to avoid interfering with other uses, so probably requires a new 1383 // optimization pass. 1384 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst) 1385 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) 1386 .addReg(SrcVec->getReg(), RegState::Implicit) 1387 .addReg(AMDGPU::M0, RegState::Implicit); 1388 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 1389 } else { 1390 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst) 1391 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) 1392 .addReg(SrcVec->getReg(), RegState::Implicit); 1393 } 1394 1395 MI.eraseFromParent(); 1396 1397 return &MBB; 1398 } 1399 1400 1401 const DebugLoc &DL = MI.getDebugLoc(); 1402 MachineBasicBlock::iterator I(&MI); 1403 1404 unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 1405 unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 1406 1407 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg); 1408 1409 if (UseGPRIdxMode) { 1410 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 1411 .addImm(0) // Reset inside loop. 1412 .addImm(VGPRIndexMode::SRC0_ENABLE); 1413 SetOn->getOperand(3).setIsUndef(); 1414 1415 1416 // Disable again after the loop. 1417 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 1418 } 1419 1420 auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, Offset, UseGPRIdxMode); 1421 MachineBasicBlock *LoopBB = InsPt->getParent(); 1422 1423 if (UseGPRIdxMode) { 1424 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst) 1425 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) 1426 .addReg(SrcVec->getReg(), RegState::Implicit) 1427 .addReg(AMDGPU::M0, RegState::Implicit); 1428 } else { 1429 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst) 1430 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) 1431 .addReg(SrcVec->getReg(), RegState::Implicit); 1432 } 1433 1434 return LoopBB; 1435 } 1436 1437 static MachineBasicBlock *emitIndirectDst(MachineInstr &MI, 1438 MachineBasicBlock &MBB, 1439 const SISubtarget &ST) { 1440 const SIInstrInfo *TII = ST.getInstrInfo(); 1441 const SIRegisterInfo &TRI = TII->getRegisterInfo(); 1442 MachineFunction *MF = MBB.getParent(); 1443 MachineRegisterInfo &MRI = MF->getRegInfo(); 1444 1445 unsigned Dst = MI.getOperand(0).getReg(); 1446 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src); 1447 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 1448 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val); 1449 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm(); 1450 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg()); 1451 1452 // This can be an immediate, but will be folded later. 1453 assert(Val->getReg()); 1454 1455 unsigned SubReg; 1456 std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC, 1457 SrcVec->getReg(), 1458 Offset); 1459 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode; 1460 1461 if (Idx->getReg() == AMDGPU::NoRegister) { 1462 MachineBasicBlock::iterator I(&MI); 1463 const DebugLoc &DL = MI.getDebugLoc(); 1464 1465 assert(Offset == 0); 1466 1467 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst) 1468 .addOperand(*SrcVec) 1469 .addOperand(*Val) 1470 .addImm(SubReg); 1471 1472 MI.eraseFromParent(); 1473 return &MBB; 1474 } 1475 1476 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, false)) { 1477 MachineBasicBlock::iterator I(&MI); 1478 const DebugLoc &DL = MI.getDebugLoc(); 1479 1480 if (UseGPRIdxMode) { 1481 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_indirect)) 1482 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst 1483 .addOperand(*Val) 1484 .addReg(Dst, RegState::ImplicitDefine) 1485 .addReg(SrcVec->getReg(), RegState::Implicit) 1486 .addReg(AMDGPU::M0, RegState::Implicit); 1487 1488 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 1489 } else { 1490 const MCInstrDesc &MovRelDesc = TII->get(AMDGPU::V_MOVRELD_B32_e32); 1491 1492 MachineInstr *MovRel = 1493 BuildMI(MBB, I, DL, MovRelDesc) 1494 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst 1495 .addOperand(*Val) 1496 .addReg(Dst, RegState::ImplicitDefine) 1497 .addReg(SrcVec->getReg(), RegState::Implicit); 1498 1499 const int ImpDefIdx = MovRelDesc.getNumOperands() + 1500 MovRelDesc.getNumImplicitUses(); 1501 const int ImpUseIdx = ImpDefIdx + 1; 1502 1503 MovRel->tieOperands(ImpDefIdx, ImpUseIdx); 1504 } 1505 1506 MI.eraseFromParent(); 1507 return &MBB; 1508 } 1509 1510 if (Val->isReg()) 1511 MRI.clearKillFlags(Val->getReg()); 1512 1513 const DebugLoc &DL = MI.getDebugLoc(); 1514 1515 if (UseGPRIdxMode) { 1516 MachineBasicBlock::iterator I(&MI); 1517 1518 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 1519 .addImm(0) // Reset inside loop. 1520 .addImm(VGPRIndexMode::DST_ENABLE); 1521 SetOn->getOperand(3).setIsUndef(); 1522 1523 // Disable again after the loop. 1524 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 1525 } 1526 1527 unsigned PhiReg = MRI.createVirtualRegister(VecRC); 1528 1529 auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg, 1530 Offset, UseGPRIdxMode); 1531 MachineBasicBlock *LoopBB = InsPt->getParent(); 1532 1533 if (UseGPRIdxMode) { 1534 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_indirect)) 1535 .addReg(PhiReg, RegState::Undef, SubReg) // vdst 1536 .addOperand(*Val) // src0 1537 .addReg(Dst, RegState::ImplicitDefine) 1538 .addReg(PhiReg, RegState::Implicit) 1539 .addReg(AMDGPU::M0, RegState::Implicit); 1540 } else { 1541 const MCInstrDesc &MovRelDesc = TII->get(AMDGPU::V_MOVRELD_B32_e32); 1542 // vdst is not actually read and just provides the base register index. 1543 MachineInstr *MovRel = 1544 BuildMI(*LoopBB, InsPt, DL, MovRelDesc) 1545 .addReg(PhiReg, RegState::Undef, SubReg) // vdst 1546 .addOperand(*Val) 1547 .addReg(Dst, RegState::ImplicitDefine) 1548 .addReg(PhiReg, RegState::Implicit); 1549 1550 const int ImpDefIdx = MovRelDesc.getNumOperands() + 1551 MovRelDesc.getNumImplicitUses(); 1552 const int ImpUseIdx = ImpDefIdx + 1; 1553 1554 MovRel->tieOperands(ImpDefIdx, ImpUseIdx); 1555 } 1556 1557 return LoopBB; 1558 } 1559 1560 MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter( 1561 MachineInstr &MI, MachineBasicBlock *BB) const { 1562 switch (MI.getOpcode()) { 1563 case AMDGPU::SI_INIT_M0: { 1564 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 1565 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(), 1566 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 1567 .addOperand(MI.getOperand(0)); 1568 MI.eraseFromParent(); 1569 return BB; 1570 } 1571 case AMDGPU::GET_GROUPSTATICSIZE: { 1572 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 1573 1574 MachineFunction *MF = BB->getParent(); 1575 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 1576 DebugLoc DL = MI.getDebugLoc(); 1577 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32)) 1578 .addOperand(MI.getOperand(0)) 1579 .addImm(MFI->getLDSSize()); 1580 MI.eraseFromParent(); 1581 return BB; 1582 } 1583 case AMDGPU::SI_INDIRECT_SRC_V1: 1584 case AMDGPU::SI_INDIRECT_SRC_V2: 1585 case AMDGPU::SI_INDIRECT_SRC_V4: 1586 case AMDGPU::SI_INDIRECT_SRC_V8: 1587 case AMDGPU::SI_INDIRECT_SRC_V16: 1588 return emitIndirectSrc(MI, *BB, *getSubtarget()); 1589 case AMDGPU::SI_INDIRECT_DST_V1: 1590 case AMDGPU::SI_INDIRECT_DST_V2: 1591 case AMDGPU::SI_INDIRECT_DST_V4: 1592 case AMDGPU::SI_INDIRECT_DST_V8: 1593 case AMDGPU::SI_INDIRECT_DST_V16: 1594 return emitIndirectDst(MI, *BB, *getSubtarget()); 1595 case AMDGPU::SI_KILL: 1596 return splitKillBlock(MI, BB); 1597 case AMDGPU::V_CNDMASK_B64_PSEUDO: { 1598 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 1599 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 1600 1601 unsigned Dst = MI.getOperand(0).getReg(); 1602 unsigned Src0 = MI.getOperand(1).getReg(); 1603 unsigned Src1 = MI.getOperand(2).getReg(); 1604 const DebugLoc &DL = MI.getDebugLoc(); 1605 unsigned SrcCond = MI.getOperand(3).getReg(); 1606 1607 unsigned DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 1608 unsigned DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 1609 1610 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo) 1611 .addReg(Src0, 0, AMDGPU::sub0) 1612 .addReg(Src1, 0, AMDGPU::sub0) 1613 .addReg(SrcCond); 1614 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi) 1615 .addReg(Src0, 0, AMDGPU::sub1) 1616 .addReg(Src1, 0, AMDGPU::sub1) 1617 .addReg(SrcCond); 1618 1619 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst) 1620 .addReg(DstLo) 1621 .addImm(AMDGPU::sub0) 1622 .addReg(DstHi) 1623 .addImm(AMDGPU::sub1); 1624 MI.eraseFromParent(); 1625 return BB; 1626 } 1627 default: 1628 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB); 1629 } 1630 } 1631 1632 bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const { 1633 // This currently forces unfolding various combinations of fsub into fma with 1634 // free fneg'd operands. As long as we have fast FMA (controlled by 1635 // isFMAFasterThanFMulAndFAdd), we should perform these. 1636 1637 // When fma is quarter rate, for f64 where add / sub are at best half rate, 1638 // most of these combines appear to be cycle neutral but save on instruction 1639 // count / code size. 1640 return true; 1641 } 1642 1643 EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx, 1644 EVT VT) const { 1645 if (!VT.isVector()) { 1646 return MVT::i1; 1647 } 1648 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements()); 1649 } 1650 1651 MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const { 1652 return MVT::i32; 1653 } 1654 1655 // Answering this is somewhat tricky and depends on the specific device which 1656 // have different rates for fma or all f64 operations. 1657 // 1658 // v_fma_f64 and v_mul_f64 always take the same number of cycles as each other 1659 // regardless of which device (although the number of cycles differs between 1660 // devices), so it is always profitable for f64. 1661 // 1662 // v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable 1663 // only on full rate devices. Normally, we should prefer selecting v_mad_f32 1664 // which we can always do even without fused FP ops since it returns the same 1665 // result as the separate operations and since it is always full 1666 // rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32 1667 // however does not support denormals, so we do report fma as faster if we have 1668 // a fast fma device and require denormals. 1669 // 1670 bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { 1671 VT = VT.getScalarType(); 1672 1673 if (!VT.isSimple()) 1674 return false; 1675 1676 switch (VT.getSimpleVT().SimpleTy) { 1677 case MVT::f32: 1678 // This is as fast on some subtargets. However, we always have full rate f32 1679 // mad available which returns the same result as the separate operations 1680 // which we should prefer over fma. We can't use this if we want to support 1681 // denormals, so only report this in these cases. 1682 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32(); 1683 case MVT::f64: 1684 return true; 1685 default: 1686 break; 1687 } 1688 1689 return false; 1690 } 1691 1692 //===----------------------------------------------------------------------===// 1693 // Custom DAG Lowering Operations 1694 //===----------------------------------------------------------------------===// 1695 1696 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 1697 switch (Op.getOpcode()) { 1698 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); 1699 case ISD::BRCOND: return LowerBRCOND(Op, DAG); 1700 case ISD::LOAD: { 1701 SDValue Result = LowerLOAD(Op, DAG); 1702 assert((!Result.getNode() || 1703 Result.getNode()->getNumValues() == 2) && 1704 "Load should return a value and a chain"); 1705 return Result; 1706 } 1707 1708 case ISD::FSIN: 1709 case ISD::FCOS: 1710 return LowerTrig(Op, DAG); 1711 case ISD::SELECT: return LowerSELECT(Op, DAG); 1712 case ISD::FDIV: return LowerFDIV(Op, DAG); 1713 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG); 1714 case ISD::STORE: return LowerSTORE(Op, DAG); 1715 case ISD::GlobalAddress: { 1716 MachineFunction &MF = DAG.getMachineFunction(); 1717 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 1718 return LowerGlobalAddress(MFI, Op, DAG); 1719 } 1720 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); 1721 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG); 1722 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG); 1723 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG); 1724 case ISD::TRAP: return lowerTRAP(Op, DAG); 1725 } 1726 return SDValue(); 1727 } 1728 1729 /// \brief Helper function for LowerBRCOND 1730 static SDNode *findUser(SDValue Value, unsigned Opcode) { 1731 1732 SDNode *Parent = Value.getNode(); 1733 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end(); 1734 I != E; ++I) { 1735 1736 if (I.getUse().get() != Value) 1737 continue; 1738 1739 if (I->getOpcode() == Opcode) 1740 return *I; 1741 } 1742 return nullptr; 1743 } 1744 1745 bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const { 1746 if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) { 1747 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) { 1748 case AMDGPUIntrinsic::amdgcn_if: 1749 case AMDGPUIntrinsic::amdgcn_else: 1750 case AMDGPUIntrinsic::amdgcn_end_cf: 1751 case AMDGPUIntrinsic::amdgcn_loop: 1752 return true; 1753 default: 1754 return false; 1755 } 1756 } 1757 1758 if (Intr->getOpcode() == ISD::INTRINSIC_WO_CHAIN) { 1759 switch (cast<ConstantSDNode>(Intr->getOperand(0))->getZExtValue()) { 1760 case AMDGPUIntrinsic::amdgcn_break: 1761 case AMDGPUIntrinsic::amdgcn_if_break: 1762 case AMDGPUIntrinsic::amdgcn_else_break: 1763 return true; 1764 default: 1765 return false; 1766 } 1767 } 1768 1769 return false; 1770 } 1771 1772 void SITargetLowering::createDebuggerPrologueStackObjects( 1773 MachineFunction &MF) const { 1774 // Create stack objects that are used for emitting debugger prologue. 1775 // 1776 // Debugger prologue writes work group IDs and work item IDs to scratch memory 1777 // at fixed location in the following format: 1778 // offset 0: work group ID x 1779 // offset 4: work group ID y 1780 // offset 8: work group ID z 1781 // offset 16: work item ID x 1782 // offset 20: work item ID y 1783 // offset 24: work item ID z 1784 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1785 int ObjectIdx = 0; 1786 1787 // For each dimension: 1788 for (unsigned i = 0; i < 3; ++i) { 1789 // Create fixed stack object for work group ID. 1790 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4, true); 1791 Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx); 1792 // Create fixed stack object for work item ID. 1793 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4 + 16, true); 1794 Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx); 1795 } 1796 } 1797 1798 /// This transforms the control flow intrinsics to get the branch destination as 1799 /// last parameter, also switches branch target with BR if the need arise 1800 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, 1801 SelectionDAG &DAG) const { 1802 1803 SDLoc DL(BRCOND); 1804 1805 SDNode *Intr = BRCOND.getOperand(1).getNode(); 1806 SDValue Target = BRCOND.getOperand(2); 1807 SDNode *BR = nullptr; 1808 SDNode *SetCC = nullptr; 1809 1810 if (Intr->getOpcode() == ISD::SETCC) { 1811 // As long as we negate the condition everything is fine 1812 SetCC = Intr; 1813 Intr = SetCC->getOperand(0).getNode(); 1814 1815 } else { 1816 // Get the target from BR if we don't negate the condition 1817 BR = findUser(BRCOND, ISD::BR); 1818 Target = BR->getOperand(1); 1819 } 1820 1821 // FIXME: This changes the types of the intrinsics instead of introducing new 1822 // nodes with the correct types. 1823 // e.g. llvm.amdgcn.loop 1824 1825 // eg: i1,ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3 1826 // => t9: ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3, BasicBlock:ch<bb1 0x7fee5286d088> 1827 1828 if (!isCFIntrinsic(Intr)) { 1829 // This is a uniform branch so we don't need to legalize. 1830 return BRCOND; 1831 } 1832 1833 bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID || 1834 Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN; 1835 1836 assert(!SetCC || 1837 (SetCC->getConstantOperandVal(1) == 1 && 1838 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() == 1839 ISD::SETNE)); 1840 1841 // operands of the new intrinsic call 1842 SmallVector<SDValue, 4> Ops; 1843 if (HaveChain) 1844 Ops.push_back(BRCOND.getOperand(0)); 1845 1846 Ops.append(Intr->op_begin() + (HaveChain ? 1 : 0), Intr->op_end()); 1847 Ops.push_back(Target); 1848 1849 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end()); 1850 1851 // build the new intrinsic call 1852 SDNode *Result = DAG.getNode( 1853 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL, 1854 DAG.getVTList(Res), Ops).getNode(); 1855 1856 if (!HaveChain) { 1857 SDValue Ops[] = { 1858 SDValue(Result, 0), 1859 BRCOND.getOperand(0) 1860 }; 1861 1862 Result = DAG.getMergeValues(Ops, DL).getNode(); 1863 } 1864 1865 if (BR) { 1866 // Give the branch instruction our target 1867 SDValue Ops[] = { 1868 BR->getOperand(0), 1869 BRCOND.getOperand(2) 1870 }; 1871 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops); 1872 DAG.ReplaceAllUsesWith(BR, NewBR.getNode()); 1873 BR = NewBR.getNode(); 1874 } 1875 1876 SDValue Chain = SDValue(Result, Result->getNumValues() - 1); 1877 1878 // Copy the intrinsic results to registers 1879 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) { 1880 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg); 1881 if (!CopyToReg) 1882 continue; 1883 1884 Chain = DAG.getCopyToReg( 1885 Chain, DL, 1886 CopyToReg->getOperand(1), 1887 SDValue(Result, i - 1), 1888 SDValue()); 1889 1890 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0)); 1891 } 1892 1893 // Remove the old intrinsic from the chain 1894 DAG.ReplaceAllUsesOfValueWith( 1895 SDValue(Intr, Intr->getNumValues() - 1), 1896 Intr->getOperand(0)); 1897 1898 return Chain; 1899 } 1900 1901 SDValue SITargetLowering::getSegmentAperture(unsigned AS, 1902 SelectionDAG &DAG) const { 1903 SDLoc SL; 1904 MachineFunction &MF = DAG.getMachineFunction(); 1905 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1906 unsigned UserSGPR = Info->getQueuePtrUserSGPR(); 1907 assert(UserSGPR != AMDGPU::NoRegister); 1908 1909 SDValue QueuePtr = CreateLiveInRegister( 1910 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64); 1911 1912 // Offset into amd_queue_t for group_segment_aperture_base_hi / 1913 // private_segment_aperture_base_hi. 1914 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44; 1915 1916 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr, 1917 DAG.getConstant(StructOffset, SL, MVT::i64)); 1918 1919 // TODO: Use custom target PseudoSourceValue. 1920 // TODO: We should use the value from the IR intrinsic call, but it might not 1921 // be available and how do we get it? 1922 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()), 1923 AMDGPUAS::CONSTANT_ADDRESS)); 1924 1925 MachinePointerInfo PtrInfo(V, StructOffset); 1926 return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr, PtrInfo, 1927 MinAlign(64, StructOffset), 1928 MachineMemOperand::MODereferenceable | 1929 MachineMemOperand::MOInvariant); 1930 } 1931 1932 SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op, 1933 SelectionDAG &DAG) const { 1934 SDLoc SL(Op); 1935 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op); 1936 1937 SDValue Src = ASC->getOperand(0); 1938 1939 // FIXME: Really support non-0 null pointers. 1940 SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32); 1941 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64); 1942 1943 // flat -> local/private 1944 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) { 1945 if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS || 1946 ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) { 1947 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE); 1948 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src); 1949 1950 return DAG.getNode(ISD::SELECT, SL, MVT::i32, 1951 NonNull, Ptr, SegmentNullPtr); 1952 } 1953 } 1954 1955 // local/private -> flat 1956 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) { 1957 if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS || 1958 ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) { 1959 SDValue NonNull 1960 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE); 1961 1962 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG); 1963 SDValue CvtPtr 1964 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture); 1965 1966 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull, 1967 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr), 1968 FlatNullPtr); 1969 } 1970 } 1971 1972 // global <-> flat are no-ops and never emitted. 1973 1974 const MachineFunction &MF = DAG.getMachineFunction(); 1975 DiagnosticInfoUnsupported InvalidAddrSpaceCast( 1976 *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc()); 1977 DAG.getContext()->diagnose(InvalidAddrSpaceCast); 1978 1979 return DAG.getUNDEF(ASC->getValueType(0)); 1980 } 1981 1982 static bool shouldEmitGOTReloc(const GlobalValue *GV, 1983 const TargetMachine &TM) { 1984 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS && 1985 !TM.shouldAssumeDSOLocal(*GV->getParent(), GV); 1986 } 1987 1988 bool 1989 SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 1990 // We can fold offsets for anything that doesn't require a GOT relocation. 1991 return GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS && 1992 !shouldEmitGOTReloc(GA->getGlobal(), getTargetMachine()); 1993 } 1994 1995 static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV, 1996 SDLoc DL, unsigned Offset, EVT PtrVT, 1997 unsigned GAFlags = SIInstrInfo::MO_NONE) { 1998 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is 1999 // lowered to the following code sequence: 2000 // s_getpc_b64 s[0:1] 2001 // s_add_u32 s0, s0, $symbol 2002 // s_addc_u32 s1, s1, 0 2003 // 2004 // s_getpc_b64 returns the address of the s_add_u32 instruction and then 2005 // a fixup or relocation is emitted to replace $symbol with a literal 2006 // constant, which is a pc-relative offset from the encoding of the $symbol 2007 // operand to the global variable. 2008 // 2009 // What we want here is an offset from the value returned by s_getpc 2010 // (which is the address of the s_add_u32 instruction) to the global 2011 // variable, but since the encoding of $symbol starts 4 bytes after the start 2012 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too 2013 // small. This requires us to add 4 to the global variable offset in order to 2014 // compute the correct address. 2015 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4, 2016 GAFlags); 2017 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, GA); 2018 } 2019 2020 SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI, 2021 SDValue Op, 2022 SelectionDAG &DAG) const { 2023 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op); 2024 2025 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS && 2026 GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS) 2027 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG); 2028 2029 SDLoc DL(GSD); 2030 const GlobalValue *GV = GSD->getGlobal(); 2031 EVT PtrVT = Op.getValueType(); 2032 2033 if (!shouldEmitGOTReloc(GV, getTargetMachine())) 2034 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT); 2035 2036 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT, 2037 SIInstrInfo::MO_GOTPCREL); 2038 2039 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext()); 2040 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS); 2041 const DataLayout &DataLayout = DAG.getDataLayout(); 2042 unsigned Align = DataLayout.getABITypeAlignment(PtrTy); 2043 // FIXME: Use a PseudoSourceValue once those can be assigned an address space. 2044 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); 2045 2046 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align, 2047 MachineMemOperand::MODereferenceable | 2048 MachineMemOperand::MOInvariant); 2049 } 2050 2051 SDValue SITargetLowering::lowerTRAP(SDValue Op, 2052 SelectionDAG &DAG) const { 2053 const MachineFunction &MF = DAG.getMachineFunction(); 2054 DiagnosticInfoUnsupported NoTrap(*MF.getFunction(), 2055 "trap handler not supported", 2056 Op.getDebugLoc(), 2057 DS_Warning); 2058 DAG.getContext()->diagnose(NoTrap); 2059 2060 // Emit s_endpgm. 2061 2062 // FIXME: This should really be selected to s_trap, but that requires 2063 // setting up the trap handler for it o do anything. 2064 return DAG.getNode(AMDGPUISD::ENDPGM, SDLoc(Op), MVT::Other, 2065 Op.getOperand(0)); 2066 } 2067 2068 SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, 2069 const SDLoc &DL, SDValue V) const { 2070 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as 2071 // the destination register. 2072 // 2073 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions, 2074 // so we will end up with redundant moves to m0. 2075 // 2076 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result. 2077 2078 // A Null SDValue creates a glue result. 2079 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue, 2080 V, Chain); 2081 return SDValue(M0, 0); 2082 } 2083 2084 SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG, 2085 SDValue Op, 2086 MVT VT, 2087 unsigned Offset) const { 2088 SDLoc SL(Op); 2089 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL, 2090 DAG.getEntryNode(), Offset, false); 2091 // The local size values will have the hi 16-bits as zero. 2092 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param, 2093 DAG.getValueType(VT)); 2094 } 2095 2096 static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) { 2097 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(), 2098 "non-hsa intrinsic with hsa target", 2099 DL.getDebugLoc()); 2100 DAG.getContext()->diagnose(BadIntrin); 2101 return DAG.getUNDEF(VT); 2102 } 2103 2104 static SDValue emitRemovedIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) { 2105 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(), 2106 "intrinsic not supported on subtarget", 2107 DL.getDebugLoc()); 2108 DAG.getContext()->diagnose(BadIntrin); 2109 return DAG.getUNDEF(VT); 2110 } 2111 2112 SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, 2113 SelectionDAG &DAG) const { 2114 MachineFunction &MF = DAG.getMachineFunction(); 2115 auto MFI = MF.getInfo<SIMachineFunctionInfo>(); 2116 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 2117 2118 EVT VT = Op.getValueType(); 2119 SDLoc DL(Op); 2120 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 2121 2122 // TODO: Should this propagate fast-math-flags? 2123 2124 switch (IntrinsicID) { 2125 case Intrinsic::amdgcn_dispatch_ptr: 2126 case Intrinsic::amdgcn_queue_ptr: { 2127 if (!Subtarget->isAmdCodeObjectV2()) { 2128 DiagnosticInfoUnsupported BadIntrin( 2129 *MF.getFunction(), "unsupported hsa intrinsic without hsa target", 2130 DL.getDebugLoc()); 2131 DAG.getContext()->diagnose(BadIntrin); 2132 return DAG.getUNDEF(VT); 2133 } 2134 2135 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ? 2136 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR; 2137 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, 2138 TRI->getPreloadedValue(MF, Reg), VT); 2139 } 2140 case Intrinsic::amdgcn_implicitarg_ptr: { 2141 unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT); 2142 return LowerParameterPtr(DAG, DL, DAG.getEntryNode(), offset); 2143 } 2144 case Intrinsic::amdgcn_kernarg_segment_ptr: { 2145 unsigned Reg 2146 = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR); 2147 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT); 2148 } 2149 case Intrinsic::amdgcn_dispatch_id: { 2150 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_ID); 2151 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT); 2152 } 2153 case Intrinsic::amdgcn_rcp: 2154 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1)); 2155 case Intrinsic::amdgcn_rsq: 2156 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name 2157 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); 2158 case Intrinsic::amdgcn_rsq_legacy: { 2159 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) 2160 return emitRemovedIntrinsicError(DAG, DL, VT); 2161 2162 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1)); 2163 } 2164 case Intrinsic::amdgcn_rcp_legacy: { 2165 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) 2166 return emitRemovedIntrinsicError(DAG, DL, VT); 2167 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1)); 2168 } 2169 case Intrinsic::amdgcn_rsq_clamp: { 2170 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS) 2171 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1)); 2172 2173 Type *Type = VT.getTypeForEVT(*DAG.getContext()); 2174 APFloat Max = APFloat::getLargest(Type->getFltSemantics()); 2175 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true); 2176 2177 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); 2178 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq, 2179 DAG.getConstantFP(Max, DL, VT)); 2180 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp, 2181 DAG.getConstantFP(Min, DL, VT)); 2182 } 2183 case Intrinsic::r600_read_ngroups_x: 2184 if (Subtarget->isAmdHsaOS()) 2185 return emitNonHSAIntrinsicError(DAG, DL, VT); 2186 2187 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2188 SI::KernelInputOffsets::NGROUPS_X, false); 2189 case Intrinsic::r600_read_ngroups_y: 2190 if (Subtarget->isAmdHsaOS()) 2191 return emitNonHSAIntrinsicError(DAG, DL, VT); 2192 2193 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2194 SI::KernelInputOffsets::NGROUPS_Y, false); 2195 case Intrinsic::r600_read_ngroups_z: 2196 if (Subtarget->isAmdHsaOS()) 2197 return emitNonHSAIntrinsicError(DAG, DL, VT); 2198 2199 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2200 SI::KernelInputOffsets::NGROUPS_Z, false); 2201 case Intrinsic::r600_read_global_size_x: 2202 if (Subtarget->isAmdHsaOS()) 2203 return emitNonHSAIntrinsicError(DAG, DL, VT); 2204 2205 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2206 SI::KernelInputOffsets::GLOBAL_SIZE_X, false); 2207 case Intrinsic::r600_read_global_size_y: 2208 if (Subtarget->isAmdHsaOS()) 2209 return emitNonHSAIntrinsicError(DAG, DL, VT); 2210 2211 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2212 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false); 2213 case Intrinsic::r600_read_global_size_z: 2214 if (Subtarget->isAmdHsaOS()) 2215 return emitNonHSAIntrinsicError(DAG, DL, VT); 2216 2217 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2218 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false); 2219 case Intrinsic::r600_read_local_size_x: 2220 if (Subtarget->isAmdHsaOS()) 2221 return emitNonHSAIntrinsicError(DAG, DL, VT); 2222 2223 return lowerImplicitZextParam(DAG, Op, MVT::i16, 2224 SI::KernelInputOffsets::LOCAL_SIZE_X); 2225 case Intrinsic::r600_read_local_size_y: 2226 if (Subtarget->isAmdHsaOS()) 2227 return emitNonHSAIntrinsicError(DAG, DL, VT); 2228 2229 return lowerImplicitZextParam(DAG, Op, MVT::i16, 2230 SI::KernelInputOffsets::LOCAL_SIZE_Y); 2231 case Intrinsic::r600_read_local_size_z: 2232 if (Subtarget->isAmdHsaOS()) 2233 return emitNonHSAIntrinsicError(DAG, DL, VT); 2234 2235 return lowerImplicitZextParam(DAG, Op, MVT::i16, 2236 SI::KernelInputOffsets::LOCAL_SIZE_Z); 2237 case Intrinsic::amdgcn_workgroup_id_x: 2238 case Intrinsic::r600_read_tgid_x: 2239 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, 2240 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT); 2241 case Intrinsic::amdgcn_workgroup_id_y: 2242 case Intrinsic::r600_read_tgid_y: 2243 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, 2244 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT); 2245 case Intrinsic::amdgcn_workgroup_id_z: 2246 case Intrinsic::r600_read_tgid_z: 2247 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, 2248 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT); 2249 case Intrinsic::amdgcn_workitem_id_x: 2250 case Intrinsic::r600_read_tidig_x: 2251 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, 2252 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT); 2253 case Intrinsic::amdgcn_workitem_id_y: 2254 case Intrinsic::r600_read_tidig_y: 2255 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, 2256 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT); 2257 case Intrinsic::amdgcn_workitem_id_z: 2258 case Intrinsic::r600_read_tidig_z: 2259 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, 2260 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT); 2261 case AMDGPUIntrinsic::SI_load_const: { 2262 SDValue Ops[] = { 2263 Op.getOperand(1), 2264 Op.getOperand(2) 2265 }; 2266 2267 MachineMemOperand *MMO = MF.getMachineMemOperand( 2268 MachinePointerInfo(), 2269 MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable | 2270 MachineMemOperand::MOInvariant, 2271 VT.getStoreSize(), 4); 2272 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL, 2273 Op->getVTList(), Ops, VT, MMO); 2274 } 2275 case AMDGPUIntrinsic::amdgcn_fdiv_fast: { 2276 return lowerFDIV_FAST(Op, DAG); 2277 } 2278 case AMDGPUIntrinsic::SI_vs_load_input: 2279 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT, 2280 Op.getOperand(1), 2281 Op.getOperand(2), 2282 Op.getOperand(3)); 2283 2284 case AMDGPUIntrinsic::SI_fs_constant: { 2285 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3)); 2286 SDValue Glue = M0.getValue(1); 2287 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32, 2288 DAG.getConstant(2, DL, MVT::i32), // P0 2289 Op.getOperand(1), Op.getOperand(2), Glue); 2290 } 2291 case AMDGPUIntrinsic::SI_packf16: 2292 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef()) 2293 return DAG.getUNDEF(MVT::i32); 2294 return Op; 2295 case AMDGPUIntrinsic::SI_fs_interp: { 2296 SDValue IJ = Op.getOperand(4); 2297 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ, 2298 DAG.getConstant(0, DL, MVT::i32)); 2299 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ, 2300 DAG.getConstant(1, DL, MVT::i32)); 2301 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3)); 2302 SDValue Glue = M0.getValue(1); 2303 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL, 2304 DAG.getVTList(MVT::f32, MVT::Glue), 2305 I, Op.getOperand(1), Op.getOperand(2), Glue); 2306 Glue = SDValue(P1.getNode(), 1); 2307 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J, 2308 Op.getOperand(1), Op.getOperand(2), Glue); 2309 } 2310 case Intrinsic::amdgcn_interp_p1: { 2311 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4)); 2312 SDValue Glue = M0.getValue(1); 2313 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1), 2314 Op.getOperand(2), Op.getOperand(3), Glue); 2315 } 2316 case Intrinsic::amdgcn_interp_p2: { 2317 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5)); 2318 SDValue Glue = SDValue(M0.getNode(), 1); 2319 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1), 2320 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4), 2321 Glue); 2322 } 2323 case Intrinsic::amdgcn_sin: 2324 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1)); 2325 2326 case Intrinsic::amdgcn_cos: 2327 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1)); 2328 2329 case Intrinsic::amdgcn_log_clamp: { 2330 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS) 2331 return SDValue(); 2332 2333 DiagnosticInfoUnsupported BadIntrin( 2334 *MF.getFunction(), "intrinsic not supported on subtarget", 2335 DL.getDebugLoc()); 2336 DAG.getContext()->diagnose(BadIntrin); 2337 return DAG.getUNDEF(VT); 2338 } 2339 case Intrinsic::amdgcn_ldexp: 2340 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT, 2341 Op.getOperand(1), Op.getOperand(2)); 2342 2343 case Intrinsic::amdgcn_fract: 2344 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1)); 2345 2346 case Intrinsic::amdgcn_class: 2347 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT, 2348 Op.getOperand(1), Op.getOperand(2)); 2349 case Intrinsic::amdgcn_div_fmas: 2350 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT, 2351 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3), 2352 Op.getOperand(4)); 2353 2354 case Intrinsic::amdgcn_div_fixup: 2355 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT, 2356 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 2357 2358 case Intrinsic::amdgcn_trig_preop: 2359 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT, 2360 Op.getOperand(1), Op.getOperand(2)); 2361 case Intrinsic::amdgcn_div_scale: { 2362 // 3rd parameter required to be a constant. 2363 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3)); 2364 if (!Param) 2365 return DAG.getUNDEF(VT); 2366 2367 // Translate to the operands expected by the machine instruction. The 2368 // first parameter must be the same as the first instruction. 2369 SDValue Numerator = Op.getOperand(1); 2370 SDValue Denominator = Op.getOperand(2); 2371 2372 // Note this order is opposite of the machine instruction's operations, 2373 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The 2374 // intrinsic has the numerator as the first operand to match a normal 2375 // division operation. 2376 2377 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator; 2378 2379 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0, 2380 Denominator, Numerator); 2381 } 2382 case Intrinsic::amdgcn_icmp: { 2383 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3)); 2384 int CondCode = CD->getSExtValue(); 2385 2386 if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE || 2387 CondCode >= ICmpInst::Predicate::BAD_ICMP_PREDICATE) 2388 return DAG.getUNDEF(VT); 2389 2390 ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode); 2391 ISD::CondCode CCOpcode = getICmpCondCode(IcInput); 2392 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1), 2393 Op.getOperand(2), DAG.getCondCode(CCOpcode)); 2394 } 2395 case Intrinsic::amdgcn_fcmp: { 2396 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3)); 2397 int CondCode = CD->getSExtValue(); 2398 2399 if (CondCode <= FCmpInst::Predicate::FCMP_FALSE || 2400 CondCode >= FCmpInst::Predicate::FCMP_TRUE) 2401 return DAG.getUNDEF(VT); 2402 2403 FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode); 2404 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput); 2405 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1), 2406 Op.getOperand(2), DAG.getCondCode(CCOpcode)); 2407 } 2408 case Intrinsic::amdgcn_fmul_legacy: 2409 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT, 2410 Op.getOperand(1), Op.getOperand(2)); 2411 case Intrinsic::amdgcn_sffbh: 2412 case AMDGPUIntrinsic::AMDGPU_flbit_i32: // Legacy name. 2413 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1)); 2414 default: 2415 return AMDGPUTargetLowering::LowerOperation(Op, DAG); 2416 } 2417 } 2418 2419 SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, 2420 SelectionDAG &DAG) const { 2421 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 2422 switch (IntrID) { 2423 case Intrinsic::amdgcn_atomic_inc: 2424 case Intrinsic::amdgcn_atomic_dec: { 2425 MemSDNode *M = cast<MemSDNode>(Op); 2426 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ? 2427 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC; 2428 SDValue Ops[] = { 2429 M->getOperand(0), // Chain 2430 M->getOperand(2), // Ptr 2431 M->getOperand(3) // Value 2432 }; 2433 2434 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops, 2435 M->getMemoryVT(), M->getMemOperand()); 2436 } 2437 default: 2438 return SDValue(); 2439 } 2440 } 2441 2442 SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op, 2443 SelectionDAG &DAG) const { 2444 MachineFunction &MF = DAG.getMachineFunction(); 2445 SDLoc DL(Op); 2446 SDValue Chain = Op.getOperand(0); 2447 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 2448 2449 switch (IntrinsicID) { 2450 case AMDGPUIntrinsic::SI_sendmsg: { 2451 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3)); 2452 SDValue Glue = Chain.getValue(1); 2453 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain, 2454 Op.getOperand(2), Glue); 2455 } 2456 case AMDGPUIntrinsic::SI_tbuffer_store: { 2457 SDValue Ops[] = { 2458 Chain, 2459 Op.getOperand(2), 2460 Op.getOperand(3), 2461 Op.getOperand(4), 2462 Op.getOperand(5), 2463 Op.getOperand(6), 2464 Op.getOperand(7), 2465 Op.getOperand(8), 2466 Op.getOperand(9), 2467 Op.getOperand(10), 2468 Op.getOperand(11), 2469 Op.getOperand(12), 2470 Op.getOperand(13), 2471 Op.getOperand(14) 2472 }; 2473 2474 EVT VT = Op.getOperand(3).getValueType(); 2475 2476 MachineMemOperand *MMO = MF.getMachineMemOperand( 2477 MachinePointerInfo(), 2478 MachineMemOperand::MOStore, 2479 VT.getStoreSize(), 4); 2480 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL, 2481 Op->getVTList(), Ops, VT, MMO); 2482 } 2483 case AMDGPUIntrinsic::AMDGPU_kill: { 2484 SDValue Src = Op.getOperand(2); 2485 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) { 2486 if (!K->isNegative()) 2487 return Chain; 2488 2489 SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32); 2490 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne); 2491 } 2492 2493 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src); 2494 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast); 2495 } 2496 default: 2497 return SDValue(); 2498 } 2499 } 2500 2501 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { 2502 SDLoc DL(Op); 2503 LoadSDNode *Load = cast<LoadSDNode>(Op); 2504 ISD::LoadExtType ExtType = Load->getExtensionType(); 2505 EVT MemVT = Load->getMemoryVT(); 2506 2507 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) { 2508 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected"); 2509 // FIXME: Copied from PPC 2510 // First, load into 32 bits, then truncate to 1 bit. 2511 2512 SDValue Chain = Load->getChain(); 2513 SDValue BasePtr = Load->getBasePtr(); 2514 MachineMemOperand *MMO = Load->getMemOperand(); 2515 2516 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain, 2517 BasePtr, MVT::i8, MMO); 2518 2519 SDValue Ops[] = { 2520 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD), 2521 NewLD.getValue(1) 2522 }; 2523 2524 return DAG.getMergeValues(Ops, DL); 2525 } 2526 2527 if (!MemVT.isVector()) 2528 return SDValue(); 2529 2530 assert(Op.getValueType().getVectorElementType() == MVT::i32 && 2531 "Custom lowering for non-i32 vectors hasn't been implemented."); 2532 2533 unsigned AS = Load->getAddressSpace(); 2534 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT, 2535 AS, Load->getAlignment())) { 2536 SDValue Ops[2]; 2537 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG); 2538 return DAG.getMergeValues(Ops, DL); 2539 } 2540 2541 unsigned NumElements = MemVT.getVectorNumElements(); 2542 switch (AS) { 2543 case AMDGPUAS::CONSTANT_ADDRESS: 2544 if (isMemOpUniform(Load)) 2545 return SDValue(); 2546 // Non-uniform loads will be selected to MUBUF instructions, so they 2547 // have the same legalization requires ments as global and private 2548 // loads. 2549 // 2550 LLVM_FALLTHROUGH; 2551 case AMDGPUAS::GLOBAL_ADDRESS: 2552 case AMDGPUAS::FLAT_ADDRESS: 2553 if (NumElements > 4) 2554 return SplitVectorLoad(Op, DAG); 2555 // v4 loads are supported for private and global memory. 2556 return SDValue(); 2557 case AMDGPUAS::PRIVATE_ADDRESS: { 2558 // Depending on the setting of the private_element_size field in the 2559 // resource descriptor, we can only make private accesses up to a certain 2560 // size. 2561 switch (Subtarget->getMaxPrivateElementSize()) { 2562 case 4: 2563 return scalarizeVectorLoad(Load, DAG); 2564 case 8: 2565 if (NumElements > 2) 2566 return SplitVectorLoad(Op, DAG); 2567 return SDValue(); 2568 case 16: 2569 // Same as global/flat 2570 if (NumElements > 4) 2571 return SplitVectorLoad(Op, DAG); 2572 return SDValue(); 2573 default: 2574 llvm_unreachable("unsupported private_element_size"); 2575 } 2576 } 2577 case AMDGPUAS::LOCAL_ADDRESS: { 2578 if (NumElements > 2) 2579 return SplitVectorLoad(Op, DAG); 2580 2581 if (NumElements == 2) 2582 return SDValue(); 2583 2584 // If properly aligned, if we split we might be able to use ds_read_b64. 2585 return SplitVectorLoad(Op, DAG); 2586 } 2587 default: 2588 return SDValue(); 2589 } 2590 } 2591 2592 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 2593 if (Op.getValueType() != MVT::i64) 2594 return SDValue(); 2595 2596 SDLoc DL(Op); 2597 SDValue Cond = Op.getOperand(0); 2598 2599 SDValue Zero = DAG.getConstant(0, DL, MVT::i32); 2600 SDValue One = DAG.getConstant(1, DL, MVT::i32); 2601 2602 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1)); 2603 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2)); 2604 2605 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero); 2606 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero); 2607 2608 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1); 2609 2610 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One); 2611 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One); 2612 2613 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1); 2614 2615 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi}); 2616 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res); 2617 } 2618 2619 // Catch division cases where we can use shortcuts with rcp and rsq 2620 // instructions. 2621 SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op, 2622 SelectionDAG &DAG) const { 2623 SDLoc SL(Op); 2624 SDValue LHS = Op.getOperand(0); 2625 SDValue RHS = Op.getOperand(1); 2626 EVT VT = Op.getValueType(); 2627 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath; 2628 2629 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) { 2630 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals()))) { 2631 2632 if (CLHS->isExactlyValue(1.0)) { 2633 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to 2634 // the CI documentation has a worst case error of 1 ulp. 2635 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to 2636 // use it as long as we aren't trying to use denormals. 2637 2638 // 1.0 / sqrt(x) -> rsq(x) 2639 // 2640 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP 2641 // error seems really high at 2^29 ULP. 2642 if (RHS.getOpcode() == ISD::FSQRT) 2643 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0)); 2644 2645 // 1.0 / x -> rcp(x) 2646 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); 2647 } 2648 2649 // Same as for 1.0, but expand the sign out of the constant. 2650 if (CLHS->isExactlyValue(-1.0)) { 2651 // -1.0 / x -> rcp (fneg x) 2652 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 2653 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS); 2654 } 2655 } 2656 } 2657 2658 const SDNodeFlags *Flags = Op->getFlags(); 2659 2660 if (Unsafe || Flags->hasAllowReciprocal()) { 2661 // Turn into multiply by the reciprocal. 2662 // x / y -> x * (1.0 / y) 2663 SDNodeFlags Flags; 2664 Flags.setUnsafeAlgebra(true); 2665 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); 2666 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags); 2667 } 2668 2669 return SDValue(); 2670 } 2671 2672 // Faster 2.5 ULP division that does not support denormals. 2673 SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const { 2674 SDLoc SL(Op); 2675 SDValue LHS = Op.getOperand(1); 2676 SDValue RHS = Op.getOperand(2); 2677 2678 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS); 2679 2680 const APFloat K0Val(BitsToFloat(0x6f800000)); 2681 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32); 2682 2683 const APFloat K1Val(BitsToFloat(0x2f800000)); 2684 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32); 2685 2686 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); 2687 2688 EVT SetCCVT = 2689 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32); 2690 2691 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT); 2692 2693 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One); 2694 2695 // TODO: Should this propagate fast-math-flags? 2696 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3); 2697 2698 // rcp does not support denormals. 2699 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1); 2700 2701 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0); 2702 2703 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul); 2704 } 2705 2706 SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const { 2707 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG)) 2708 return FastLowered; 2709 2710 SDLoc SL(Op); 2711 SDValue LHS = Op.getOperand(0); 2712 SDValue RHS = Op.getOperand(1); 2713 2714 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); 2715 2716 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1); 2717 2718 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, RHS, RHS, LHS); 2719 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, LHS, RHS, LHS); 2720 2721 // Denominator is scaled to not be denormal, so using rcp is ok. 2722 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, DenominatorScaled); 2723 2724 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, DenominatorScaled); 2725 2726 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, ApproxRcp, One); 2727 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, ApproxRcp); 2728 2729 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, NumeratorScaled, Fma1); 2730 2731 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, NumeratorScaled); 2732 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul); 2733 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, NumeratorScaled); 2734 2735 SDValue Scale = NumeratorScaled.getValue(1); 2736 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, Fma4, Fma1, Fma3, Scale); 2737 2738 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS); 2739 } 2740 2741 SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const { 2742 if (DAG.getTarget().Options.UnsafeFPMath) 2743 return lowerFastUnsafeFDIV(Op, DAG); 2744 2745 SDLoc SL(Op); 2746 SDValue X = Op.getOperand(0); 2747 SDValue Y = Op.getOperand(1); 2748 2749 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64); 2750 2751 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1); 2752 2753 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X); 2754 2755 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0); 2756 2757 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0); 2758 2759 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One); 2760 2761 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp); 2762 2763 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One); 2764 2765 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X); 2766 2767 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1); 2768 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3); 2769 2770 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64, 2771 NegDivScale0, Mul, DivScale1); 2772 2773 SDValue Scale; 2774 2775 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) { 2776 // Workaround a hardware bug on SI where the condition output from div_scale 2777 // is not usable. 2778 2779 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32); 2780 2781 // Figure out if the scale to use for div_fmas. 2782 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X); 2783 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y); 2784 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0); 2785 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1); 2786 2787 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi); 2788 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi); 2789 2790 SDValue Scale0Hi 2791 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi); 2792 SDValue Scale1Hi 2793 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi); 2794 2795 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ); 2796 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ); 2797 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen); 2798 } else { 2799 Scale = DivScale1.getValue(1); 2800 } 2801 2802 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64, 2803 Fma4, Fma3, Mul, Scale); 2804 2805 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X); 2806 } 2807 2808 SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const { 2809 EVT VT = Op.getValueType(); 2810 2811 if (VT == MVT::f32) 2812 return LowerFDIV32(Op, DAG); 2813 2814 if (VT == MVT::f64) 2815 return LowerFDIV64(Op, DAG); 2816 2817 llvm_unreachable("Unexpected type for fdiv"); 2818 } 2819 2820 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const { 2821 SDLoc DL(Op); 2822 StoreSDNode *Store = cast<StoreSDNode>(Op); 2823 EVT VT = Store->getMemoryVT(); 2824 2825 if (VT == MVT::i1) { 2826 return DAG.getTruncStore(Store->getChain(), DL, 2827 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32), 2828 Store->getBasePtr(), MVT::i1, Store->getMemOperand()); 2829 } 2830 2831 assert(VT.isVector() && 2832 Store->getValue().getValueType().getScalarType() == MVT::i32); 2833 2834 unsigned AS = Store->getAddressSpace(); 2835 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT, 2836 AS, Store->getAlignment())) { 2837 return expandUnalignedStore(Store, DAG); 2838 } 2839 2840 unsigned NumElements = VT.getVectorNumElements(); 2841 switch (AS) { 2842 case AMDGPUAS::GLOBAL_ADDRESS: 2843 case AMDGPUAS::FLAT_ADDRESS: 2844 if (NumElements > 4) 2845 return SplitVectorStore(Op, DAG); 2846 return SDValue(); 2847 case AMDGPUAS::PRIVATE_ADDRESS: { 2848 switch (Subtarget->getMaxPrivateElementSize()) { 2849 case 4: 2850 return scalarizeVectorStore(Store, DAG); 2851 case 8: 2852 if (NumElements > 2) 2853 return SplitVectorStore(Op, DAG); 2854 return SDValue(); 2855 case 16: 2856 if (NumElements > 4) 2857 return SplitVectorStore(Op, DAG); 2858 return SDValue(); 2859 default: 2860 llvm_unreachable("unsupported private_element_size"); 2861 } 2862 } 2863 case AMDGPUAS::LOCAL_ADDRESS: { 2864 if (NumElements > 2) 2865 return SplitVectorStore(Op, DAG); 2866 2867 if (NumElements == 2) 2868 return Op; 2869 2870 // If properly aligned, if we split we might be able to use ds_write_b64. 2871 return SplitVectorStore(Op, DAG); 2872 } 2873 default: 2874 llvm_unreachable("unhandled address space"); 2875 } 2876 } 2877 2878 SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const { 2879 SDLoc DL(Op); 2880 EVT VT = Op.getValueType(); 2881 SDValue Arg = Op.getOperand(0); 2882 // TODO: Should this propagate fast-math-flags? 2883 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT, 2884 DAG.getNode(ISD::FMUL, DL, VT, Arg, 2885 DAG.getConstantFP(0.5/M_PI, DL, 2886 VT))); 2887 2888 switch (Op.getOpcode()) { 2889 case ISD::FCOS: 2890 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart); 2891 case ISD::FSIN: 2892 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart); 2893 default: 2894 llvm_unreachable("Wrong trig opcode"); 2895 } 2896 } 2897 2898 SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const { 2899 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op); 2900 assert(AtomicNode->isCompareAndSwap()); 2901 unsigned AS = AtomicNode->getAddressSpace(); 2902 2903 // No custom lowering required for local address space 2904 if (!isFlatGlobalAddrSpace(AS)) 2905 return Op; 2906 2907 // Non-local address space requires custom lowering for atomic compare 2908 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2 2909 SDLoc DL(Op); 2910 SDValue ChainIn = Op.getOperand(0); 2911 SDValue Addr = Op.getOperand(1); 2912 SDValue Old = Op.getOperand(2); 2913 SDValue New = Op.getOperand(3); 2914 EVT VT = Op.getValueType(); 2915 MVT SimpleVT = VT.getSimpleVT(); 2916 MVT VecType = MVT::getVectorVT(SimpleVT, 2); 2917 2918 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old}); 2919 SDValue Ops[] = { ChainIn, Addr, NewOld }; 2920 2921 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(), 2922 Ops, VT, AtomicNode->getMemOperand()); 2923 } 2924 2925 //===----------------------------------------------------------------------===// 2926 // Custom DAG optimizations 2927 //===----------------------------------------------------------------------===// 2928 2929 SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N, 2930 DAGCombinerInfo &DCI) const { 2931 EVT VT = N->getValueType(0); 2932 EVT ScalarVT = VT.getScalarType(); 2933 if (ScalarVT != MVT::f32) 2934 return SDValue(); 2935 2936 SelectionDAG &DAG = DCI.DAG; 2937 SDLoc DL(N); 2938 2939 SDValue Src = N->getOperand(0); 2940 EVT SrcVT = Src.getValueType(); 2941 2942 // TODO: We could try to match extracting the higher bytes, which would be 2943 // easier if i8 vectors weren't promoted to i32 vectors, particularly after 2944 // types are legalized. v4i8 -> v4f32 is probably the only case to worry 2945 // about in practice. 2946 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) { 2947 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) { 2948 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src); 2949 DCI.AddToWorklist(Cvt.getNode()); 2950 return Cvt; 2951 } 2952 } 2953 2954 return SDValue(); 2955 } 2956 2957 /// \brief Return true if the given offset Size in bytes can be folded into 2958 /// the immediate offsets of a memory instruction for the given address space. 2959 static bool canFoldOffset(unsigned OffsetSize, unsigned AS, 2960 const SISubtarget &STI) { 2961 switch (AS) { 2962 case AMDGPUAS::GLOBAL_ADDRESS: { 2963 // MUBUF instructions a 12-bit offset in bytes. 2964 return isUInt<12>(OffsetSize); 2965 } 2966 case AMDGPUAS::CONSTANT_ADDRESS: { 2967 // SMRD instructions have an 8-bit offset in dwords on SI and 2968 // a 20-bit offset in bytes on VI. 2969 if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) 2970 return isUInt<20>(OffsetSize); 2971 else 2972 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4); 2973 } 2974 case AMDGPUAS::LOCAL_ADDRESS: 2975 case AMDGPUAS::REGION_ADDRESS: { 2976 // The single offset versions have a 16-bit offset in bytes. 2977 return isUInt<16>(OffsetSize); 2978 } 2979 case AMDGPUAS::PRIVATE_ADDRESS: 2980 // Indirect register addressing does not use any offsets. 2981 default: 2982 return 0; 2983 } 2984 } 2985 2986 // (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2) 2987 2988 // This is a variant of 2989 // (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2), 2990 // 2991 // The normal DAG combiner will do this, but only if the add has one use since 2992 // that would increase the number of instructions. 2993 // 2994 // This prevents us from seeing a constant offset that can be folded into a 2995 // memory instruction's addressing mode. If we know the resulting add offset of 2996 // a pointer can be folded into an addressing offset, we can replace the pointer 2997 // operand with the add of new constant offset. This eliminates one of the uses, 2998 // and may allow the remaining use to also be simplified. 2999 // 3000 SDValue SITargetLowering::performSHLPtrCombine(SDNode *N, 3001 unsigned AddrSpace, 3002 DAGCombinerInfo &DCI) const { 3003 SDValue N0 = N->getOperand(0); 3004 SDValue N1 = N->getOperand(1); 3005 3006 if (N0.getOpcode() != ISD::ADD) 3007 return SDValue(); 3008 3009 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1); 3010 if (!CN1) 3011 return SDValue(); 3012 3013 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 3014 if (!CAdd) 3015 return SDValue(); 3016 3017 // If the resulting offset is too large, we can't fold it into the addressing 3018 // mode offset. 3019 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue(); 3020 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget())) 3021 return SDValue(); 3022 3023 SelectionDAG &DAG = DCI.DAG; 3024 SDLoc SL(N); 3025 EVT VT = N->getValueType(0); 3026 3027 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1); 3028 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32); 3029 3030 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset); 3031 } 3032 3033 static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) { 3034 return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) || 3035 (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) || 3036 (Opc == ISD::XOR && Val == 0); 3037 } 3038 3039 // Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This 3040 // will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit 3041 // integer combine opportunities since most 64-bit operations are decomposed 3042 // this way. TODO: We won't want this for SALU especially if it is an inline 3043 // immediate. 3044 SDValue SITargetLowering::splitBinaryBitConstantOp( 3045 DAGCombinerInfo &DCI, 3046 const SDLoc &SL, 3047 unsigned Opc, SDValue LHS, 3048 const ConstantSDNode *CRHS) const { 3049 uint64_t Val = CRHS->getZExtValue(); 3050 uint32_t ValLo = Lo_32(Val); 3051 uint32_t ValHi = Hi_32(Val); 3052 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3053 3054 if ((bitOpWithConstantIsReducible(Opc, ValLo) || 3055 bitOpWithConstantIsReducible(Opc, ValHi)) || 3056 (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) { 3057 // If we need to materialize a 64-bit immediate, it will be split up later 3058 // anyway. Avoid creating the harder to understand 64-bit immediate 3059 // materialization. 3060 return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi); 3061 } 3062 3063 return SDValue(); 3064 } 3065 3066 SDValue SITargetLowering::performAndCombine(SDNode *N, 3067 DAGCombinerInfo &DCI) const { 3068 if (DCI.isBeforeLegalize()) 3069 return SDValue(); 3070 3071 SelectionDAG &DAG = DCI.DAG; 3072 EVT VT = N->getValueType(0); 3073 SDValue LHS = N->getOperand(0); 3074 SDValue RHS = N->getOperand(1); 3075 3076 3077 if (VT == MVT::i64) { 3078 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS); 3079 if (CRHS) { 3080 if (SDValue Split 3081 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS)) 3082 return Split; 3083 } 3084 } 3085 3086 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) -> 3087 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity) 3088 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) { 3089 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get(); 3090 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get(); 3091 3092 SDValue X = LHS.getOperand(0); 3093 SDValue Y = RHS.getOperand(0); 3094 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X) 3095 return SDValue(); 3096 3097 if (LCC == ISD::SETO) { 3098 if (X != LHS.getOperand(1)) 3099 return SDValue(); 3100 3101 if (RCC == ISD::SETUNE) { 3102 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1)); 3103 if (!C1 || !C1->isInfinity() || C1->isNegative()) 3104 return SDValue(); 3105 3106 const uint32_t Mask = SIInstrFlags::N_NORMAL | 3107 SIInstrFlags::N_SUBNORMAL | 3108 SIInstrFlags::N_ZERO | 3109 SIInstrFlags::P_ZERO | 3110 SIInstrFlags::P_SUBNORMAL | 3111 SIInstrFlags::P_NORMAL; 3112 3113 static_assert(((~(SIInstrFlags::S_NAN | 3114 SIInstrFlags::Q_NAN | 3115 SIInstrFlags::N_INFINITY | 3116 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask, 3117 "mask not equal"); 3118 3119 SDLoc DL(N); 3120 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, 3121 X, DAG.getConstant(Mask, DL, MVT::i32)); 3122 } 3123 } 3124 } 3125 3126 return SDValue(); 3127 } 3128 3129 SDValue SITargetLowering::performOrCombine(SDNode *N, 3130 DAGCombinerInfo &DCI) const { 3131 SelectionDAG &DAG = DCI.DAG; 3132 SDValue LHS = N->getOperand(0); 3133 SDValue RHS = N->getOperand(1); 3134 3135 EVT VT = N->getValueType(0); 3136 if (VT == MVT::i1) { 3137 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2) 3138 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS && 3139 RHS.getOpcode() == AMDGPUISD::FP_CLASS) { 3140 SDValue Src = LHS.getOperand(0); 3141 if (Src != RHS.getOperand(0)) 3142 return SDValue(); 3143 3144 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1)); 3145 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1)); 3146 if (!CLHS || !CRHS) 3147 return SDValue(); 3148 3149 // Only 10 bits are used. 3150 static const uint32_t MaxMask = 0x3ff; 3151 3152 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask; 3153 SDLoc DL(N); 3154 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, 3155 Src, DAG.getConstant(NewMask, DL, MVT::i32)); 3156 } 3157 3158 return SDValue(); 3159 } 3160 3161 if (VT != MVT::i64) 3162 return SDValue(); 3163 3164 // TODO: This could be a generic combine with a predicate for extracting the 3165 // high half of an integer being free. 3166 3167 // (or i64:x, (zero_extend i32:y)) -> 3168 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x))) 3169 if (LHS.getOpcode() == ISD::ZERO_EXTEND && 3170 RHS.getOpcode() != ISD::ZERO_EXTEND) 3171 std::swap(LHS, RHS); 3172 3173 if (RHS.getOpcode() == ISD::ZERO_EXTEND) { 3174 SDValue ExtSrc = RHS.getOperand(0); 3175 EVT SrcVT = ExtSrc.getValueType(); 3176 if (SrcVT == MVT::i32) { 3177 SDLoc SL(N); 3178 SDValue LowLHS, HiBits; 3179 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG); 3180 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc); 3181 3182 DCI.AddToWorklist(LowOr.getNode()); 3183 DCI.AddToWorklist(HiBits.getNode()); 3184 3185 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, 3186 LowOr, HiBits); 3187 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 3188 } 3189 } 3190 3191 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 3192 if (CRHS) { 3193 if (SDValue Split 3194 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS)) 3195 return Split; 3196 } 3197 3198 return SDValue(); 3199 } 3200 3201 SDValue SITargetLowering::performXorCombine(SDNode *N, 3202 DAGCombinerInfo &DCI) const { 3203 EVT VT = N->getValueType(0); 3204 if (VT != MVT::i64) 3205 return SDValue(); 3206 3207 SDValue LHS = N->getOperand(0); 3208 SDValue RHS = N->getOperand(1); 3209 3210 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS); 3211 if (CRHS) { 3212 if (SDValue Split 3213 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS)) 3214 return Split; 3215 } 3216 3217 return SDValue(); 3218 } 3219 3220 SDValue SITargetLowering::performClassCombine(SDNode *N, 3221 DAGCombinerInfo &DCI) const { 3222 SelectionDAG &DAG = DCI.DAG; 3223 SDValue Mask = N->getOperand(1); 3224 3225 // fp_class x, 0 -> false 3226 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) { 3227 if (CMask->isNullValue()) 3228 return DAG.getConstant(0, SDLoc(N), MVT::i1); 3229 } 3230 3231 if (N->getOperand(0).isUndef()) 3232 return DAG.getUNDEF(MVT::i1); 3233 3234 return SDValue(); 3235 } 3236 3237 // Constant fold canonicalize. 3238 SDValue SITargetLowering::performFCanonicalizeCombine( 3239 SDNode *N, 3240 DAGCombinerInfo &DCI) const { 3241 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); 3242 if (!CFP) 3243 return SDValue(); 3244 3245 SelectionDAG &DAG = DCI.DAG; 3246 const APFloat &C = CFP->getValueAPF(); 3247 3248 // Flush denormals to 0 if not enabled. 3249 if (C.isDenormal()) { 3250 EVT VT = N->getValueType(0); 3251 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals()) 3252 return DAG.getConstantFP(0.0, SDLoc(N), VT); 3253 3254 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals()) 3255 return DAG.getConstantFP(0.0, SDLoc(N), VT); 3256 } 3257 3258 if (C.isNaN()) { 3259 EVT VT = N->getValueType(0); 3260 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics()); 3261 if (C.isSignaling()) { 3262 // Quiet a signaling NaN. 3263 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT); 3264 } 3265 3266 // Make sure it is the canonical NaN bitpattern. 3267 // 3268 // TODO: Can we use -1 as the canonical NaN value since it's an inline 3269 // immediate? 3270 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt()) 3271 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT); 3272 } 3273 3274 return SDValue(CFP, 0); 3275 } 3276 3277 static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) { 3278 switch (Opc) { 3279 case ISD::FMAXNUM: 3280 return AMDGPUISD::FMAX3; 3281 case ISD::SMAX: 3282 return AMDGPUISD::SMAX3; 3283 case ISD::UMAX: 3284 return AMDGPUISD::UMAX3; 3285 case ISD::FMINNUM: 3286 return AMDGPUISD::FMIN3; 3287 case ISD::SMIN: 3288 return AMDGPUISD::SMIN3; 3289 case ISD::UMIN: 3290 return AMDGPUISD::UMIN3; 3291 default: 3292 llvm_unreachable("Not a min/max opcode"); 3293 } 3294 } 3295 3296 static SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL, 3297 SDValue Op0, SDValue Op1, bool Signed) { 3298 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1); 3299 if (!K1) 3300 return SDValue(); 3301 3302 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1)); 3303 if (!K0) 3304 return SDValue(); 3305 3306 if (Signed) { 3307 if (K0->getAPIntValue().sge(K1->getAPIntValue())) 3308 return SDValue(); 3309 } else { 3310 if (K0->getAPIntValue().uge(K1->getAPIntValue())) 3311 return SDValue(); 3312 } 3313 3314 EVT VT = K0->getValueType(0); 3315 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT, 3316 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0)); 3317 } 3318 3319 static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) { 3320 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions()) 3321 return true; 3322 3323 return DAG.isKnownNeverNaN(Op); 3324 } 3325 3326 static SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL, 3327 SDValue Op0, SDValue Op1) { 3328 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1); 3329 if (!K1) 3330 return SDValue(); 3331 3332 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1)); 3333 if (!K0) 3334 return SDValue(); 3335 3336 // Ordered >= (although NaN inputs should have folded away by now). 3337 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF()); 3338 if (Cmp == APFloat::cmpGreaterThan) 3339 return SDValue(); 3340 3341 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a 3342 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then 3343 // give the other result, which is different from med3 with a NaN input. 3344 SDValue Var = Op0.getOperand(0); 3345 if (!isKnownNeverSNan(DAG, Var)) 3346 return SDValue(); 3347 3348 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0), 3349 Var, SDValue(K0, 0), SDValue(K1, 0)); 3350 } 3351 3352 SDValue SITargetLowering::performMinMaxCombine(SDNode *N, 3353 DAGCombinerInfo &DCI) const { 3354 SelectionDAG &DAG = DCI.DAG; 3355 3356 unsigned Opc = N->getOpcode(); 3357 SDValue Op0 = N->getOperand(0); 3358 SDValue Op1 = N->getOperand(1); 3359 3360 // Only do this if the inner op has one use since this will just increases 3361 // register pressure for no benefit. 3362 3363 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) { 3364 // max(max(a, b), c) -> max3(a, b, c) 3365 // min(min(a, b), c) -> min3(a, b, c) 3366 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) { 3367 SDLoc DL(N); 3368 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), 3369 DL, 3370 N->getValueType(0), 3371 Op0.getOperand(0), 3372 Op0.getOperand(1), 3373 Op1); 3374 } 3375 3376 // Try commuted. 3377 // max(a, max(b, c)) -> max3(a, b, c) 3378 // min(a, min(b, c)) -> min3(a, b, c) 3379 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) { 3380 SDLoc DL(N); 3381 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), 3382 DL, 3383 N->getValueType(0), 3384 Op0, 3385 Op1.getOperand(0), 3386 Op1.getOperand(1)); 3387 } 3388 } 3389 3390 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1) 3391 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) { 3392 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true)) 3393 return Med3; 3394 } 3395 3396 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) { 3397 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false)) 3398 return Med3; 3399 } 3400 3401 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1) 3402 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) || 3403 (Opc == AMDGPUISD::FMIN_LEGACY && 3404 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) && 3405 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) { 3406 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1)) 3407 return Res; 3408 } 3409 3410 return SDValue(); 3411 } 3412 3413 SDValue SITargetLowering::performSetCCCombine(SDNode *N, 3414 DAGCombinerInfo &DCI) const { 3415 SelectionDAG &DAG = DCI.DAG; 3416 SDLoc SL(N); 3417 3418 SDValue LHS = N->getOperand(0); 3419 SDValue RHS = N->getOperand(1); 3420 EVT VT = LHS.getValueType(); 3421 3422 if (VT != MVT::f32 && VT != MVT::f64) 3423 return SDValue(); 3424 3425 // Match isinf pattern 3426 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity)) 3427 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); 3428 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) { 3429 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS); 3430 if (!CRHS) 3431 return SDValue(); 3432 3433 const APFloat &APF = CRHS->getValueAPF(); 3434 if (APF.isInfinity() && !APF.isNegative()) { 3435 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY; 3436 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0), 3437 DAG.getConstant(Mask, SL, MVT::i32)); 3438 } 3439 } 3440 3441 return SDValue(); 3442 } 3443 3444 SDValue SITargetLowering::PerformDAGCombine(SDNode *N, 3445 DAGCombinerInfo &DCI) const { 3446 SelectionDAG &DAG = DCI.DAG; 3447 SDLoc DL(N); 3448 3449 switch (N->getOpcode()) { 3450 default: 3451 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); 3452 case ISD::SETCC: 3453 return performSetCCCombine(N, DCI); 3454 case ISD::FMAXNUM: 3455 case ISD::FMINNUM: 3456 case ISD::SMAX: 3457 case ISD::SMIN: 3458 case ISD::UMAX: 3459 case ISD::UMIN: 3460 case AMDGPUISD::FMIN_LEGACY: 3461 case AMDGPUISD::FMAX_LEGACY: { 3462 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG && 3463 N->getValueType(0) != MVT::f64 && 3464 getTargetMachine().getOptLevel() > CodeGenOpt::None) 3465 return performMinMaxCombine(N, DCI); 3466 break; 3467 } 3468 3469 case AMDGPUISD::CVT_F32_UBYTE0: 3470 case AMDGPUISD::CVT_F32_UBYTE1: 3471 case AMDGPUISD::CVT_F32_UBYTE2: 3472 case AMDGPUISD::CVT_F32_UBYTE3: { 3473 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0; 3474 SDValue Src = N->getOperand(0); 3475 3476 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero. 3477 if (Src.getOpcode() == ISD::SRL) { 3478 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x 3479 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x 3480 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x 3481 3482 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(1))) { 3483 unsigned SrcOffset = C->getZExtValue() + 8 * Offset; 3484 if (SrcOffset < 32 && SrcOffset % 8 == 0) { 3485 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, DL, 3486 MVT::f32, Src.getOperand(0)); 3487 } 3488 } 3489 } 3490 3491 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8); 3492 3493 APInt KnownZero, KnownOne; 3494 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), 3495 !DCI.isBeforeLegalizeOps()); 3496 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3497 if (TLO.ShrinkDemandedConstant(Src, Demanded) || 3498 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) { 3499 DCI.CommitTargetLoweringOpt(TLO); 3500 } 3501 3502 break; 3503 } 3504 3505 case ISD::UINT_TO_FP: { 3506 return performUCharToFloatCombine(N, DCI); 3507 } 3508 case ISD::FADD: { 3509 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 3510 break; 3511 3512 EVT VT = N->getValueType(0); 3513 if (VT != MVT::f32) 3514 break; 3515 3516 // Only do this if we are not trying to support denormals. v_mad_f32 does 3517 // not support denormals ever. 3518 if (Subtarget->hasFP32Denormals()) 3519 break; 3520 3521 SDValue LHS = N->getOperand(0); 3522 SDValue RHS = N->getOperand(1); 3523 3524 // These should really be instruction patterns, but writing patterns with 3525 // source modiifiers is a pain. 3526 3527 // fadd (fadd (a, a), b) -> mad 2.0, a, b 3528 if (LHS.getOpcode() == ISD::FADD) { 3529 SDValue A = LHS.getOperand(0); 3530 if (A == LHS.getOperand(1)) { 3531 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); 3532 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS); 3533 } 3534 } 3535 3536 // fadd (b, fadd (a, a)) -> mad 2.0, a, b 3537 if (RHS.getOpcode() == ISD::FADD) { 3538 SDValue A = RHS.getOperand(0); 3539 if (A == RHS.getOperand(1)) { 3540 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); 3541 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS); 3542 } 3543 } 3544 3545 return SDValue(); 3546 } 3547 case ISD::FSUB: { 3548 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 3549 break; 3550 3551 EVT VT = N->getValueType(0); 3552 3553 // Try to get the fneg to fold into the source modifier. This undoes generic 3554 // DAG combines and folds them into the mad. 3555 // 3556 // Only do this if we are not trying to support denormals. v_mad_f32 does 3557 // not support denormals ever. 3558 if (VT == MVT::f32 && 3559 !Subtarget->hasFP32Denormals()) { 3560 SDValue LHS = N->getOperand(0); 3561 SDValue RHS = N->getOperand(1); 3562 if (LHS.getOpcode() == ISD::FADD) { 3563 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c) 3564 3565 SDValue A = LHS.getOperand(0); 3566 if (A == LHS.getOperand(1)) { 3567 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); 3568 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS); 3569 3570 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS); 3571 } 3572 } 3573 3574 if (RHS.getOpcode() == ISD::FADD) { 3575 // (fsub c, (fadd a, a)) -> mad -2.0, a, c 3576 3577 SDValue A = RHS.getOperand(0); 3578 if (A == RHS.getOperand(1)) { 3579 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32); 3580 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS); 3581 } 3582 } 3583 3584 return SDValue(); 3585 } 3586 3587 break; 3588 } 3589 case ISD::LOAD: 3590 case ISD::STORE: 3591 case ISD::ATOMIC_LOAD: 3592 case ISD::ATOMIC_STORE: 3593 case ISD::ATOMIC_CMP_SWAP: 3594 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: 3595 case ISD::ATOMIC_SWAP: 3596 case ISD::ATOMIC_LOAD_ADD: 3597 case ISD::ATOMIC_LOAD_SUB: 3598 case ISD::ATOMIC_LOAD_AND: 3599 case ISD::ATOMIC_LOAD_OR: 3600 case ISD::ATOMIC_LOAD_XOR: 3601 case ISD::ATOMIC_LOAD_NAND: 3602 case ISD::ATOMIC_LOAD_MIN: 3603 case ISD::ATOMIC_LOAD_MAX: 3604 case ISD::ATOMIC_LOAD_UMIN: 3605 case ISD::ATOMIC_LOAD_UMAX: 3606 case AMDGPUISD::ATOMIC_INC: 3607 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics. 3608 if (DCI.isBeforeLegalize()) 3609 break; 3610 3611 MemSDNode *MemNode = cast<MemSDNode>(N); 3612 SDValue Ptr = MemNode->getBasePtr(); 3613 3614 // TODO: We could also do this for multiplies. 3615 unsigned AS = MemNode->getAddressSpace(); 3616 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) { 3617 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI); 3618 if (NewPtr) { 3619 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end()); 3620 3621 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr; 3622 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0); 3623 } 3624 } 3625 break; 3626 } 3627 case ISD::AND: 3628 return performAndCombine(N, DCI); 3629 case ISD::OR: 3630 return performOrCombine(N, DCI); 3631 case ISD::XOR: 3632 return performXorCombine(N, DCI); 3633 case AMDGPUISD::FP_CLASS: 3634 return performClassCombine(N, DCI); 3635 case ISD::FCANONICALIZE: 3636 return performFCanonicalizeCombine(N, DCI); 3637 case AMDGPUISD::FRACT: 3638 case AMDGPUISD::RCP: 3639 case AMDGPUISD::RSQ: 3640 case AMDGPUISD::RCP_LEGACY: 3641 case AMDGPUISD::RSQ_LEGACY: 3642 case AMDGPUISD::RSQ_CLAMP: 3643 case AMDGPUISD::LDEXP: { 3644 SDValue Src = N->getOperand(0); 3645 if (Src.isUndef()) 3646 return Src; 3647 break; 3648 } 3649 } 3650 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); 3651 } 3652 3653 /// \brief Helper function for adjustWritemask 3654 static unsigned SubIdx2Lane(unsigned Idx) { 3655 switch (Idx) { 3656 default: return 0; 3657 case AMDGPU::sub0: return 0; 3658 case AMDGPU::sub1: return 1; 3659 case AMDGPU::sub2: return 2; 3660 case AMDGPU::sub3: return 3; 3661 } 3662 } 3663 3664 /// \brief Adjust the writemask of MIMG instructions 3665 void SITargetLowering::adjustWritemask(MachineSDNode *&Node, 3666 SelectionDAG &DAG) const { 3667 SDNode *Users[4] = { }; 3668 unsigned Lane = 0; 3669 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3; 3670 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx); 3671 unsigned NewDmask = 0; 3672 3673 // Try to figure out the used register components 3674 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end(); 3675 I != E; ++I) { 3676 3677 // Abort if we can't understand the usage 3678 if (!I->isMachineOpcode() || 3679 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG) 3680 return; 3681 3682 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used. 3683 // Note that subregs are packed, i.e. Lane==0 is the first bit set 3684 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit 3685 // set, etc. 3686 Lane = SubIdx2Lane(I->getConstantOperandVal(1)); 3687 3688 // Set which texture component corresponds to the lane. 3689 unsigned Comp; 3690 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) { 3691 assert(Dmask); 3692 Comp = countTrailingZeros(Dmask); 3693 Dmask &= ~(1 << Comp); 3694 } 3695 3696 // Abort if we have more than one user per component 3697 if (Users[Lane]) 3698 return; 3699 3700 Users[Lane] = *I; 3701 NewDmask |= 1 << Comp; 3702 } 3703 3704 // Abort if there's no change 3705 if (NewDmask == OldDmask) 3706 return; 3707 3708 // Adjust the writemask in the node 3709 std::vector<SDValue> Ops; 3710 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx); 3711 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32)); 3712 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end()); 3713 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops); 3714 3715 // If we only got one lane, replace it with a copy 3716 // (if NewDmask has only one bit set...) 3717 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) { 3718 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(), 3719 MVT::i32); 3720 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, 3721 SDLoc(), Users[Lane]->getValueType(0), 3722 SDValue(Node, 0), RC); 3723 DAG.ReplaceAllUsesWith(Users[Lane], Copy); 3724 return; 3725 } 3726 3727 // Update the users of the node with the new indices 3728 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) { 3729 3730 SDNode *User = Users[i]; 3731 if (!User) 3732 continue; 3733 3734 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32); 3735 DAG.UpdateNodeOperands(User, User->getOperand(0), Op); 3736 3737 switch (Idx) { 3738 default: break; 3739 case AMDGPU::sub0: Idx = AMDGPU::sub1; break; 3740 case AMDGPU::sub1: Idx = AMDGPU::sub2; break; 3741 case AMDGPU::sub2: Idx = AMDGPU::sub3; break; 3742 } 3743 } 3744 } 3745 3746 static bool isFrameIndexOp(SDValue Op) { 3747 if (Op.getOpcode() == ISD::AssertZext) 3748 Op = Op.getOperand(0); 3749 3750 return isa<FrameIndexSDNode>(Op); 3751 } 3752 3753 /// \brief Legalize target independent instructions (e.g. INSERT_SUBREG) 3754 /// with frame index operands. 3755 /// LLVM assumes that inputs are to these instructions are registers. 3756 void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node, 3757 SelectionDAG &DAG) const { 3758 3759 SmallVector<SDValue, 8> Ops; 3760 for (unsigned i = 0; i < Node->getNumOperands(); ++i) { 3761 if (!isFrameIndexOp(Node->getOperand(i))) { 3762 Ops.push_back(Node->getOperand(i)); 3763 continue; 3764 } 3765 3766 SDLoc DL(Node); 3767 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, 3768 Node->getOperand(i).getValueType(), 3769 Node->getOperand(i)), 0)); 3770 } 3771 3772 DAG.UpdateNodeOperands(Node, Ops); 3773 } 3774 3775 /// \brief Fold the instructions after selecting them. 3776 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node, 3777 SelectionDAG &DAG) const { 3778 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3779 unsigned Opcode = Node->getMachineOpcode(); 3780 3781 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() && 3782 !TII->isGather4(Opcode)) 3783 adjustWritemask(Node, DAG); 3784 3785 if (Opcode == AMDGPU::INSERT_SUBREG || 3786 Opcode == AMDGPU::REG_SEQUENCE) { 3787 legalizeTargetIndependentNode(Node, DAG); 3788 return Node; 3789 } 3790 return Node; 3791 } 3792 3793 /// \brief Assign the register class depending on the number of 3794 /// bits set in the writemask 3795 void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI, 3796 SDNode *Node) const { 3797 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3798 3799 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 3800 3801 if (TII->isVOP3(MI.getOpcode())) { 3802 // Make sure constant bus requirements are respected. 3803 TII->legalizeOperandsVOP3(MRI, MI); 3804 return; 3805 } 3806 3807 if (TII->isMIMG(MI)) { 3808 unsigned VReg = MI.getOperand(0).getReg(); 3809 unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4; 3810 unsigned Writemask = MI.getOperand(DmaskIdx).getImm(); 3811 unsigned BitsSet = 0; 3812 for (unsigned i = 0; i < 4; ++i) 3813 BitsSet += Writemask & (1 << i) ? 1 : 0; 3814 3815 const TargetRegisterClass *RC; 3816 switch (BitsSet) { 3817 default: return; 3818 case 1: RC = &AMDGPU::VGPR_32RegClass; break; 3819 case 2: RC = &AMDGPU::VReg_64RegClass; break; 3820 case 3: RC = &AMDGPU::VReg_96RegClass; break; 3821 } 3822 3823 unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet); 3824 MI.setDesc(TII->get(NewOpcode)); 3825 MRI.setRegClass(VReg, RC); 3826 return; 3827 } 3828 3829 // Replace unused atomics with the no return version. 3830 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode()); 3831 if (NoRetAtomicOp != -1) { 3832 if (!Node->hasAnyUseOfValue(0)) { 3833 MI.setDesc(TII->get(NoRetAtomicOp)); 3834 MI.RemoveOperand(0); 3835 return; 3836 } 3837 3838 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg 3839 // instruction, because the return type of these instructions is a vec2 of 3840 // the memory type, so it can be tied to the input operand. 3841 // This means these instructions always have a use, so we need to add a 3842 // special case to check if the atomic has only one extract_subreg use, 3843 // which itself has no uses. 3844 if ((Node->hasNUsesOfValue(1, 0) && 3845 Node->use_begin()->isMachineOpcode() && 3846 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG && 3847 !Node->use_begin()->hasAnyUseOfValue(0))) { 3848 unsigned Def = MI.getOperand(0).getReg(); 3849 3850 // Change this into a noret atomic. 3851 MI.setDesc(TII->get(NoRetAtomicOp)); 3852 MI.RemoveOperand(0); 3853 3854 // If we only remove the def operand from the atomic instruction, the 3855 // extract_subreg will be left with a use of a vreg without a def. 3856 // So we need to insert an implicit_def to avoid machine verifier 3857 // errors. 3858 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), 3859 TII->get(AMDGPU::IMPLICIT_DEF), Def); 3860 } 3861 return; 3862 } 3863 } 3864 3865 static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL, 3866 uint64_t Val) { 3867 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32); 3868 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0); 3869 } 3870 3871 MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG, 3872 const SDLoc &DL, 3873 SDValue Ptr) const { 3874 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3875 3876 // Build the half of the subregister with the constants before building the 3877 // full 128-bit register. If we are building multiple resource descriptors, 3878 // this will allow CSEing of the 2-component register. 3879 const SDValue Ops0[] = { 3880 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32), 3881 buildSMovImm32(DAG, DL, 0), 3882 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 3883 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32), 3884 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32) 3885 }; 3886 3887 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, 3888 MVT::v2i32, Ops0), 0); 3889 3890 // Combine the constants and the pointer. 3891 const SDValue Ops1[] = { 3892 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), 3893 Ptr, 3894 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32), 3895 SubRegHi, 3896 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32) 3897 }; 3898 3899 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1); 3900 } 3901 3902 /// \brief Return a resource descriptor with the 'Add TID' bit enabled 3903 /// The TID (Thread ID) is multiplied by the stride value (bits [61:48] 3904 /// of the resource descriptor) to create an offset, which is added to 3905 /// the resource pointer. 3906 MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL, 3907 SDValue Ptr, uint32_t RsrcDword1, 3908 uint64_t RsrcDword2And3) const { 3909 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr); 3910 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr); 3911 if (RsrcDword1) { 3912 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi, 3913 DAG.getConstant(RsrcDword1, DL, MVT::i32)), 3914 0); 3915 } 3916 3917 SDValue DataLo = buildSMovImm32(DAG, DL, 3918 RsrcDword2And3 & UINT64_C(0xFFFFFFFF)); 3919 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32); 3920 3921 const SDValue Ops[] = { 3922 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), 3923 PtrLo, 3924 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 3925 PtrHi, 3926 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32), 3927 DataLo, 3928 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32), 3929 DataHi, 3930 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32) 3931 }; 3932 3933 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops); 3934 } 3935 3936 SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG, 3937 const TargetRegisterClass *RC, 3938 unsigned Reg, EVT VT) const { 3939 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT); 3940 3941 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()), 3942 cast<RegisterSDNode>(VReg)->getReg(), VT); 3943 } 3944 3945 //===----------------------------------------------------------------------===// 3946 // SI Inline Assembly Support 3947 //===----------------------------------------------------------------------===// 3948 3949 std::pair<unsigned, const TargetRegisterClass *> 3950 SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 3951 StringRef Constraint, 3952 MVT VT) const { 3953 3954 if (Constraint.size() == 1) { 3955 switch (Constraint[0]) { 3956 case 's': 3957 case 'r': 3958 switch (VT.getSizeInBits()) { 3959 default: 3960 return std::make_pair(0U, nullptr); 3961 case 32: 3962 return std::make_pair(0U, &AMDGPU::SReg_32RegClass); 3963 case 64: 3964 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass); 3965 case 128: 3966 return std::make_pair(0U, &AMDGPU::SReg_128RegClass); 3967 case 256: 3968 return std::make_pair(0U, &AMDGPU::SReg_256RegClass); 3969 } 3970 3971 case 'v': 3972 switch (VT.getSizeInBits()) { 3973 default: 3974 return std::make_pair(0U, nullptr); 3975 case 32: 3976 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass); 3977 case 64: 3978 return std::make_pair(0U, &AMDGPU::VReg_64RegClass); 3979 case 96: 3980 return std::make_pair(0U, &AMDGPU::VReg_96RegClass); 3981 case 128: 3982 return std::make_pair(0U, &AMDGPU::VReg_128RegClass); 3983 case 256: 3984 return std::make_pair(0U, &AMDGPU::VReg_256RegClass); 3985 case 512: 3986 return std::make_pair(0U, &AMDGPU::VReg_512RegClass); 3987 } 3988 } 3989 } 3990 3991 if (Constraint.size() > 1) { 3992 const TargetRegisterClass *RC = nullptr; 3993 if (Constraint[1] == 'v') { 3994 RC = &AMDGPU::VGPR_32RegClass; 3995 } else if (Constraint[1] == 's') { 3996 RC = &AMDGPU::SGPR_32RegClass; 3997 } 3998 3999 if (RC) { 4000 uint32_t Idx; 4001 bool Failed = Constraint.substr(2).getAsInteger(10, Idx); 4002 if (!Failed && Idx < RC->getNumRegs()) 4003 return std::make_pair(RC->getRegister(Idx), RC); 4004 } 4005 } 4006 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 4007 } 4008 4009 SITargetLowering::ConstraintType 4010 SITargetLowering::getConstraintType(StringRef Constraint) const { 4011 if (Constraint.size() == 1) { 4012 switch (Constraint[0]) { 4013 default: break; 4014 case 's': 4015 case 'v': 4016 return C_RegisterClass; 4017 } 4018 } 4019 return TargetLowering::getConstraintType(Constraint); 4020 } 4021