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 if (ST.isAmdCodeObjectV2()) { 885 // TODO: Assume we will spill without optimizations. 886 if (HasStackObjects) { 887 // If we have stack objects, we unquestionably need the private buffer 888 // resource. For the Code Object V2 ABI, this will be the first 4 user 889 // SGPR inputs. We can reserve those and use them directly. 890 891 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue( 892 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER); 893 Info->setScratchRSrcReg(PrivateSegmentBufferReg); 894 895 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue( 896 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); 897 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg); 898 } else { 899 unsigned ReservedBufferReg 900 = TRI->reservedPrivateSegmentBufferReg(MF); 901 unsigned ReservedOffsetReg 902 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); 903 904 // We tentatively reserve the last registers (skipping the last two 905 // which may contain VCC). After register allocation, we'll replace 906 // these with the ones immediately after those which were really 907 // allocated. In the prologue copies will be inserted from the argument 908 // to these reserved registers. 909 Info->setScratchRSrcReg(ReservedBufferReg); 910 Info->setScratchWaveOffsetReg(ReservedOffsetReg); 911 } 912 } else { 913 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF); 914 915 // Without HSA, relocations are used for the scratch pointer and the 916 // buffer resource setup is always inserted in the prologue. Scratch wave 917 // offset is still in an input SGPR. 918 Info->setScratchRSrcReg(ReservedBufferReg); 919 920 if (HasStackObjects) { 921 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue( 922 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); 923 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg); 924 } else { 925 unsigned ReservedOffsetReg 926 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); 927 Info->setScratchWaveOffsetReg(ReservedOffsetReg); 928 } 929 } 930 931 if (Info->hasWorkItemIDX()) { 932 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X); 933 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 934 CCInfo.AllocateReg(Reg); 935 } 936 937 if (Info->hasWorkItemIDY()) { 938 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y); 939 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 940 CCInfo.AllocateReg(Reg); 941 } 942 943 if (Info->hasWorkItemIDZ()) { 944 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z); 945 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 946 CCInfo.AllocateReg(Reg); 947 } 948 949 if (Chains.empty()) 950 return Chain; 951 952 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 953 } 954 955 SDValue 956 SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 957 bool isVarArg, 958 const SmallVectorImpl<ISD::OutputArg> &Outs, 959 const SmallVectorImpl<SDValue> &OutVals, 960 const SDLoc &DL, SelectionDAG &DAG) const { 961 MachineFunction &MF = DAG.getMachineFunction(); 962 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 963 964 if (!AMDGPU::isShader(CallConv)) 965 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs, 966 OutVals, DL, DAG); 967 968 Info->setIfReturnsVoid(Outs.size() == 0); 969 970 SmallVector<ISD::OutputArg, 48> Splits; 971 SmallVector<SDValue, 48> SplitVals; 972 973 // Split vectors into their elements. 974 for (unsigned i = 0, e = Outs.size(); i != e; ++i) { 975 const ISD::OutputArg &Out = Outs[i]; 976 977 if (Out.VT.isVector()) { 978 MVT VT = Out.VT.getVectorElementType(); 979 ISD::OutputArg NewOut = Out; 980 NewOut.Flags.setSplit(); 981 NewOut.VT = VT; 982 983 // We want the original number of vector elements here, e.g. 984 // three or five, not four or eight. 985 unsigned NumElements = Out.ArgVT.getVectorNumElements(); 986 987 for (unsigned j = 0; j != NumElements; ++j) { 988 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i], 989 DAG.getConstant(j, DL, MVT::i32)); 990 SplitVals.push_back(Elem); 991 Splits.push_back(NewOut); 992 NewOut.PartOffset += NewOut.VT.getStoreSize(); 993 } 994 } else { 995 SplitVals.push_back(OutVals[i]); 996 Splits.push_back(Out); 997 } 998 } 999 1000 // CCValAssign - represent the assignment of the return value to a location. 1001 SmallVector<CCValAssign, 48> RVLocs; 1002 1003 // CCState - Info about the registers and stack slots. 1004 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 1005 *DAG.getContext()); 1006 1007 // Analyze outgoing return values. 1008 AnalyzeReturn(CCInfo, Splits); 1009 1010 SDValue Flag; 1011 SmallVector<SDValue, 48> RetOps; 1012 RetOps.push_back(Chain); // Operand #0 = Chain (updated below) 1013 1014 // Copy the result values into the output registers. 1015 for (unsigned i = 0, realRVLocIdx = 0; 1016 i != RVLocs.size(); 1017 ++i, ++realRVLocIdx) { 1018 CCValAssign &VA = RVLocs[i]; 1019 assert(VA.isRegLoc() && "Can only return in registers!"); 1020 1021 SDValue Arg = SplitVals[realRVLocIdx]; 1022 1023 // Copied from other backends. 1024 switch (VA.getLocInfo()) { 1025 default: llvm_unreachable("Unknown loc info!"); 1026 case CCValAssign::Full: 1027 break; 1028 case CCValAssign::BCvt: 1029 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg); 1030 break; 1031 } 1032 1033 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag); 1034 Flag = Chain.getValue(1); 1035 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 1036 } 1037 1038 // Update chain and glue. 1039 RetOps[0] = Chain; 1040 if (Flag.getNode()) 1041 RetOps.push_back(Flag); 1042 1043 unsigned Opc = Info->returnsVoid() ? AMDGPUISD::ENDPGM : AMDGPUISD::RETURN; 1044 return DAG.getNode(Opc, DL, MVT::Other, RetOps); 1045 } 1046 1047 unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT, 1048 SelectionDAG &DAG) const { 1049 unsigned Reg = StringSwitch<unsigned>(RegName) 1050 .Case("m0", AMDGPU::M0) 1051 .Case("exec", AMDGPU::EXEC) 1052 .Case("exec_lo", AMDGPU::EXEC_LO) 1053 .Case("exec_hi", AMDGPU::EXEC_HI) 1054 .Case("flat_scratch", AMDGPU::FLAT_SCR) 1055 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO) 1056 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI) 1057 .Default(AMDGPU::NoRegister); 1058 1059 if (Reg == AMDGPU::NoRegister) { 1060 report_fatal_error(Twine("invalid register name \"" 1061 + StringRef(RegName) + "\".")); 1062 1063 } 1064 1065 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS && 1066 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) { 1067 report_fatal_error(Twine("invalid register \"" 1068 + StringRef(RegName) + "\" for subtarget.")); 1069 } 1070 1071 switch (Reg) { 1072 case AMDGPU::M0: 1073 case AMDGPU::EXEC_LO: 1074 case AMDGPU::EXEC_HI: 1075 case AMDGPU::FLAT_SCR_LO: 1076 case AMDGPU::FLAT_SCR_HI: 1077 if (VT.getSizeInBits() == 32) 1078 return Reg; 1079 break; 1080 case AMDGPU::EXEC: 1081 case AMDGPU::FLAT_SCR: 1082 if (VT.getSizeInBits() == 64) 1083 return Reg; 1084 break; 1085 default: 1086 llvm_unreachable("missing register type checking"); 1087 } 1088 1089 report_fatal_error(Twine("invalid type for register \"" 1090 + StringRef(RegName) + "\".")); 1091 } 1092 1093 // If kill is not the last instruction, split the block so kill is always a 1094 // proper terminator. 1095 MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI, 1096 MachineBasicBlock *BB) const { 1097 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 1098 1099 MachineBasicBlock::iterator SplitPoint(&MI); 1100 ++SplitPoint; 1101 1102 if (SplitPoint == BB->end()) { 1103 // Don't bother with a new block. 1104 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR)); 1105 return BB; 1106 } 1107 1108 MachineFunction *MF = BB->getParent(); 1109 MachineBasicBlock *SplitBB 1110 = MF->CreateMachineBasicBlock(BB->getBasicBlock()); 1111 1112 MF->insert(++MachineFunction::iterator(BB), SplitBB); 1113 SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end()); 1114 1115 SplitBB->transferSuccessorsAndUpdatePHIs(BB); 1116 BB->addSuccessor(SplitBB); 1117 1118 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR)); 1119 return SplitBB; 1120 } 1121 1122 // Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the 1123 // wavefront. If the value is uniform and just happens to be in a VGPR, this 1124 // will only do one iteration. In the worst case, this will loop 64 times. 1125 // 1126 // TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value. 1127 static MachineBasicBlock::iterator emitLoadM0FromVGPRLoop( 1128 const SIInstrInfo *TII, 1129 MachineRegisterInfo &MRI, 1130 MachineBasicBlock &OrigBB, 1131 MachineBasicBlock &LoopBB, 1132 const DebugLoc &DL, 1133 const MachineOperand &IdxReg, 1134 unsigned InitReg, 1135 unsigned ResultReg, 1136 unsigned PhiReg, 1137 unsigned InitSaveExecReg, 1138 int Offset, 1139 bool UseGPRIdxMode) { 1140 MachineBasicBlock::iterator I = LoopBB.begin(); 1141 1142 unsigned PhiExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1143 unsigned NewExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1144 unsigned CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 1145 unsigned CondReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1146 1147 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg) 1148 .addReg(InitReg) 1149 .addMBB(&OrigBB) 1150 .addReg(ResultReg) 1151 .addMBB(&LoopBB); 1152 1153 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec) 1154 .addReg(InitSaveExecReg) 1155 .addMBB(&OrigBB) 1156 .addReg(NewExec) 1157 .addMBB(&LoopBB); 1158 1159 // Read the next variant <- also loop target. 1160 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg) 1161 .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef())); 1162 1163 // Compare the just read M0 value to all possible Idx values. 1164 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg) 1165 .addReg(CurrentIdxReg) 1166 .addReg(IdxReg.getReg(), 0, IdxReg.getSubReg()); 1167 1168 if (UseGPRIdxMode) { 1169 unsigned IdxReg; 1170 if (Offset == 0) { 1171 IdxReg = CurrentIdxReg; 1172 } else { 1173 IdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 1174 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), IdxReg) 1175 .addReg(CurrentIdxReg, RegState::Kill) 1176 .addImm(Offset); 1177 } 1178 1179 MachineInstr *SetIdx = 1180 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_IDX)) 1181 .addReg(IdxReg, RegState::Kill); 1182 SetIdx->getOperand(2).setIsUndef(true); 1183 } else { 1184 // Move index from VCC into M0 1185 if (Offset == 0) { 1186 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 1187 .addReg(CurrentIdxReg, RegState::Kill); 1188 } else { 1189 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0) 1190 .addReg(CurrentIdxReg, RegState::Kill) 1191 .addImm(Offset); 1192 } 1193 } 1194 1195 // Update EXEC, save the original EXEC value to VCC. 1196 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), NewExec) 1197 .addReg(CondReg, RegState::Kill); 1198 1199 MRI.setSimpleHint(NewExec, CondReg); 1200 1201 // Update EXEC, switch all done bits to 0 and all todo bits to 1. 1202 MachineInstr *InsertPt = 1203 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC) 1204 .addReg(AMDGPU::EXEC) 1205 .addReg(NewExec); 1206 1207 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use 1208 // s_cbranch_scc0? 1209 1210 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover. 1211 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ)) 1212 .addMBB(&LoopBB); 1213 1214 return InsertPt->getIterator(); 1215 } 1216 1217 // This has slightly sub-optimal regalloc when the source vector is killed by 1218 // the read. The register allocator does not understand that the kill is 1219 // per-workitem, so is kept alive for the whole loop so we end up not re-using a 1220 // subregister from it, using 1 more VGPR than necessary. This was saved when 1221 // this was expanded after register allocation. 1222 static MachineBasicBlock::iterator loadM0FromVGPR(const SIInstrInfo *TII, 1223 MachineBasicBlock &MBB, 1224 MachineInstr &MI, 1225 unsigned InitResultReg, 1226 unsigned PhiReg, 1227 int Offset, 1228 bool UseGPRIdxMode) { 1229 MachineFunction *MF = MBB.getParent(); 1230 MachineRegisterInfo &MRI = MF->getRegInfo(); 1231 const DebugLoc &DL = MI.getDebugLoc(); 1232 MachineBasicBlock::iterator I(&MI); 1233 1234 unsigned DstReg = MI.getOperand(0).getReg(); 1235 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1236 unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1237 1238 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec); 1239 1240 // Save the EXEC mask 1241 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec) 1242 .addReg(AMDGPU::EXEC); 1243 1244 // To insert the loop we need to split the block. Move everything after this 1245 // point to a new block, and insert a new empty block between the two. 1246 MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock(); 1247 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock(); 1248 MachineFunction::iterator MBBI(MBB); 1249 ++MBBI; 1250 1251 MF->insert(MBBI, LoopBB); 1252 MF->insert(MBBI, RemainderBB); 1253 1254 LoopBB->addSuccessor(LoopBB); 1255 LoopBB->addSuccessor(RemainderBB); 1256 1257 // Move the rest of the block into a new block. 1258 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB); 1259 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end()); 1260 1261 MBB.addSuccessor(LoopBB); 1262 1263 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 1264 1265 auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx, 1266 InitResultReg, DstReg, PhiReg, TmpExec, 1267 Offset, UseGPRIdxMode); 1268 1269 MachineBasicBlock::iterator First = RemainderBB->begin(); 1270 BuildMI(*RemainderBB, First, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC) 1271 .addReg(SaveExec); 1272 1273 MI.eraseFromParent(); 1274 1275 return InsPt; 1276 } 1277 1278 // Returns subreg index, offset 1279 static std::pair<unsigned, int> 1280 computeIndirectRegAndOffset(const SIRegisterInfo &TRI, 1281 const TargetRegisterClass *SuperRC, 1282 unsigned VecReg, 1283 int Offset) { 1284 int NumElts = SuperRC->getSize() / 4; 1285 1286 // Skip out of bounds offsets, or else we would end up using an undefined 1287 // register. 1288 if (Offset >= NumElts || Offset < 0) 1289 return std::make_pair(AMDGPU::sub0, Offset); 1290 1291 return std::make_pair(AMDGPU::sub0 + Offset, 0); 1292 } 1293 1294 // Return true if the index is an SGPR and was set. 1295 static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII, 1296 MachineRegisterInfo &MRI, 1297 MachineInstr &MI, 1298 int Offset, 1299 bool UseGPRIdxMode, 1300 bool IsIndirectSrc) { 1301 MachineBasicBlock *MBB = MI.getParent(); 1302 const DebugLoc &DL = MI.getDebugLoc(); 1303 MachineBasicBlock::iterator I(&MI); 1304 1305 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 1306 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg()); 1307 1308 assert(Idx->getReg() != AMDGPU::NoRegister); 1309 1310 if (!TII->getRegisterInfo().isSGPRClass(IdxRC)) 1311 return false; 1312 1313 if (UseGPRIdxMode) { 1314 unsigned IdxMode = IsIndirectSrc ? 1315 VGPRIndexMode::SRC0_ENABLE : VGPRIndexMode::DST_ENABLE; 1316 if (Offset == 0) { 1317 MachineInstr *SetOn = 1318 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 1319 .addOperand(*Idx) 1320 .addImm(IdxMode); 1321 1322 SetOn->getOperand(3).setIsUndef(AMDGPU::M0); 1323 } else { 1324 unsigned Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 1325 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp) 1326 .addOperand(*Idx) 1327 .addImm(Offset); 1328 MachineInstr *SetOn = 1329 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 1330 .addReg(Tmp, RegState::Kill) 1331 .addImm(IdxMode); 1332 1333 SetOn->getOperand(3).setIsUndef(AMDGPU::M0); 1334 } 1335 1336 return true; 1337 } 1338 1339 if (Offset == 0) { 1340 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 1341 .addOperand(*Idx); 1342 } else { 1343 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0) 1344 .addOperand(*Idx) 1345 .addImm(Offset); 1346 } 1347 1348 return true; 1349 } 1350 1351 // Control flow needs to be inserted if indexing with a VGPR. 1352 static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI, 1353 MachineBasicBlock &MBB, 1354 const SISubtarget &ST) { 1355 const SIInstrInfo *TII = ST.getInstrInfo(); 1356 const SIRegisterInfo &TRI = TII->getRegisterInfo(); 1357 MachineFunction *MF = MBB.getParent(); 1358 MachineRegisterInfo &MRI = MF->getRegInfo(); 1359 1360 unsigned Dst = MI.getOperand(0).getReg(); 1361 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src); 1362 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm(); 1363 1364 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg()); 1365 1366 unsigned SubReg; 1367 std::tie(SubReg, Offset) 1368 = computeIndirectRegAndOffset(TRI, VecRC, SrcVec->getReg(), Offset); 1369 1370 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode; 1371 1372 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, true)) { 1373 MachineBasicBlock::iterator I(&MI); 1374 const DebugLoc &DL = MI.getDebugLoc(); 1375 1376 if (UseGPRIdxMode) { 1377 // TODO: Look at the uses to avoid the copy. This may require rescheduling 1378 // to avoid interfering with other uses, so probably requires a new 1379 // optimization pass. 1380 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst) 1381 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) 1382 .addReg(SrcVec->getReg(), RegState::Implicit) 1383 .addReg(AMDGPU::M0, RegState::Implicit); 1384 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 1385 } else { 1386 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst) 1387 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) 1388 .addReg(SrcVec->getReg(), RegState::Implicit); 1389 } 1390 1391 MI.eraseFromParent(); 1392 1393 return &MBB; 1394 } 1395 1396 1397 const DebugLoc &DL = MI.getDebugLoc(); 1398 MachineBasicBlock::iterator I(&MI); 1399 1400 unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 1401 unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 1402 1403 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg); 1404 1405 if (UseGPRIdxMode) { 1406 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 1407 .addImm(0) // Reset inside loop. 1408 .addImm(VGPRIndexMode::SRC0_ENABLE); 1409 SetOn->getOperand(3).setIsUndef(AMDGPU::M0); 1410 1411 1412 // Disable again after the loop. 1413 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 1414 } 1415 1416 auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, Offset, UseGPRIdxMode); 1417 MachineBasicBlock *LoopBB = InsPt->getParent(); 1418 1419 if (UseGPRIdxMode) { 1420 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst) 1421 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) 1422 .addReg(SrcVec->getReg(), RegState::Implicit) 1423 .addReg(AMDGPU::M0, RegState::Implicit); 1424 } else { 1425 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst) 1426 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) 1427 .addReg(SrcVec->getReg(), RegState::Implicit); 1428 } 1429 1430 return LoopBB; 1431 } 1432 1433 static MachineBasicBlock *emitIndirectDst(MachineInstr &MI, 1434 MachineBasicBlock &MBB, 1435 const SISubtarget &ST) { 1436 const SIInstrInfo *TII = ST.getInstrInfo(); 1437 const SIRegisterInfo &TRI = TII->getRegisterInfo(); 1438 MachineFunction *MF = MBB.getParent(); 1439 MachineRegisterInfo &MRI = MF->getRegInfo(); 1440 1441 unsigned Dst = MI.getOperand(0).getReg(); 1442 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src); 1443 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 1444 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val); 1445 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm(); 1446 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg()); 1447 1448 // This can be an immediate, but will be folded later. 1449 assert(Val->getReg()); 1450 1451 unsigned SubReg; 1452 std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC, 1453 SrcVec->getReg(), 1454 Offset); 1455 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode; 1456 1457 if (Idx->getReg() == AMDGPU::NoRegister) { 1458 MachineBasicBlock::iterator I(&MI); 1459 const DebugLoc &DL = MI.getDebugLoc(); 1460 1461 assert(Offset == 0); 1462 1463 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst) 1464 .addOperand(*SrcVec) 1465 .addOperand(*Val) 1466 .addImm(SubReg); 1467 1468 MI.eraseFromParent(); 1469 return &MBB; 1470 } 1471 1472 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, false)) { 1473 MachineBasicBlock::iterator I(&MI); 1474 const DebugLoc &DL = MI.getDebugLoc(); 1475 1476 if (UseGPRIdxMode) { 1477 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_indirect)) 1478 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst 1479 .addOperand(*Val) 1480 .addReg(Dst, RegState::ImplicitDefine) 1481 .addReg(SrcVec->getReg(), RegState::Implicit) 1482 .addReg(AMDGPU::M0, RegState::Implicit); 1483 1484 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 1485 } else { 1486 const MCInstrDesc &MovRelDesc = TII->get(AMDGPU::V_MOVRELD_B32_e32); 1487 1488 MachineInstr *MovRel = 1489 BuildMI(MBB, I, DL, MovRelDesc) 1490 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst 1491 .addOperand(*Val) 1492 .addReg(Dst, RegState::ImplicitDefine) 1493 .addReg(SrcVec->getReg(), RegState::Implicit); 1494 1495 const int ImpDefIdx = MovRelDesc.getNumOperands() + 1496 MovRelDesc.getNumImplicitUses(); 1497 const int ImpUseIdx = ImpDefIdx + 1; 1498 1499 MovRel->tieOperands(ImpDefIdx, ImpUseIdx); 1500 } 1501 1502 MI.eraseFromParent(); 1503 return &MBB; 1504 } 1505 1506 if (Val->isReg()) 1507 MRI.clearKillFlags(Val->getReg()); 1508 1509 const DebugLoc &DL = MI.getDebugLoc(); 1510 1511 if (UseGPRIdxMode) { 1512 MachineBasicBlock::iterator I(&MI); 1513 1514 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 1515 .addImm(0) // Reset inside loop. 1516 .addImm(VGPRIndexMode::DST_ENABLE); 1517 SetOn->getOperand(3).setIsUndef(AMDGPU::M0); 1518 1519 // Disable again after the loop. 1520 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 1521 } 1522 1523 unsigned PhiReg = MRI.createVirtualRegister(VecRC); 1524 1525 auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg, 1526 Offset, UseGPRIdxMode); 1527 MachineBasicBlock *LoopBB = InsPt->getParent(); 1528 1529 if (UseGPRIdxMode) { 1530 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_indirect)) 1531 .addReg(PhiReg, RegState::Undef, SubReg) // vdst 1532 .addOperand(*Val) // src0 1533 .addReg(Dst, RegState::ImplicitDefine) 1534 .addReg(PhiReg, RegState::Implicit) 1535 .addReg(AMDGPU::M0, RegState::Implicit); 1536 } else { 1537 const MCInstrDesc &MovRelDesc = TII->get(AMDGPU::V_MOVRELD_B32_e32); 1538 // vdst is not actually read and just provides the base register index. 1539 MachineInstr *MovRel = 1540 BuildMI(*LoopBB, InsPt, DL, MovRelDesc) 1541 .addReg(PhiReg, RegState::Undef, SubReg) // vdst 1542 .addOperand(*Val) 1543 .addReg(Dst, RegState::ImplicitDefine) 1544 .addReg(PhiReg, RegState::Implicit); 1545 1546 const int ImpDefIdx = MovRelDesc.getNumOperands() + 1547 MovRelDesc.getNumImplicitUses(); 1548 const int ImpUseIdx = ImpDefIdx + 1; 1549 1550 MovRel->tieOperands(ImpDefIdx, ImpUseIdx); 1551 } 1552 1553 return LoopBB; 1554 } 1555 1556 MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter( 1557 MachineInstr &MI, MachineBasicBlock *BB) const { 1558 switch (MI.getOpcode()) { 1559 case AMDGPU::SI_INIT_M0: { 1560 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 1561 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(), 1562 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 1563 .addOperand(MI.getOperand(0)); 1564 MI.eraseFromParent(); 1565 return BB; 1566 } 1567 case AMDGPU::GET_GROUPSTATICSIZE: { 1568 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 1569 1570 MachineFunction *MF = BB->getParent(); 1571 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 1572 DebugLoc DL = MI.getDebugLoc(); 1573 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32)) 1574 .addOperand(MI.getOperand(0)) 1575 .addImm(MFI->getLDSSize()); 1576 MI.eraseFromParent(); 1577 return BB; 1578 } 1579 case AMDGPU::SI_INDIRECT_SRC_V1: 1580 case AMDGPU::SI_INDIRECT_SRC_V2: 1581 case AMDGPU::SI_INDIRECT_SRC_V4: 1582 case AMDGPU::SI_INDIRECT_SRC_V8: 1583 case AMDGPU::SI_INDIRECT_SRC_V16: 1584 return emitIndirectSrc(MI, *BB, *getSubtarget()); 1585 case AMDGPU::SI_INDIRECT_DST_V1: 1586 case AMDGPU::SI_INDIRECT_DST_V2: 1587 case AMDGPU::SI_INDIRECT_DST_V4: 1588 case AMDGPU::SI_INDIRECT_DST_V8: 1589 case AMDGPU::SI_INDIRECT_DST_V16: 1590 return emitIndirectDst(MI, *BB, *getSubtarget()); 1591 case AMDGPU::SI_KILL: 1592 return splitKillBlock(MI, BB); 1593 case AMDGPU::V_CNDMASK_B64_PSEUDO: { 1594 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 1595 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 1596 1597 unsigned Dst = MI.getOperand(0).getReg(); 1598 unsigned Src0 = MI.getOperand(1).getReg(); 1599 unsigned Src1 = MI.getOperand(2).getReg(); 1600 const DebugLoc &DL = MI.getDebugLoc(); 1601 unsigned SrcCond = MI.getOperand(3).getReg(); 1602 1603 unsigned DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 1604 unsigned DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 1605 1606 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo) 1607 .addReg(Src0, 0, AMDGPU::sub0) 1608 .addReg(Src1, 0, AMDGPU::sub0) 1609 .addReg(SrcCond); 1610 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi) 1611 .addReg(Src0, 0, AMDGPU::sub1) 1612 .addReg(Src1, 0, AMDGPU::sub1) 1613 .addReg(SrcCond); 1614 1615 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst) 1616 .addReg(DstLo) 1617 .addImm(AMDGPU::sub0) 1618 .addReg(DstHi) 1619 .addImm(AMDGPU::sub1); 1620 MI.eraseFromParent(); 1621 return BB; 1622 } 1623 default: 1624 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB); 1625 } 1626 } 1627 1628 bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const { 1629 // This currently forces unfolding various combinations of fsub into fma with 1630 // free fneg'd operands. As long as we have fast FMA (controlled by 1631 // isFMAFasterThanFMulAndFAdd), we should perform these. 1632 1633 // When fma is quarter rate, for f64 where add / sub are at best half rate, 1634 // most of these combines appear to be cycle neutral but save on instruction 1635 // count / code size. 1636 return true; 1637 } 1638 1639 EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx, 1640 EVT VT) const { 1641 if (!VT.isVector()) { 1642 return MVT::i1; 1643 } 1644 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements()); 1645 } 1646 1647 MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const { 1648 return MVT::i32; 1649 } 1650 1651 // Answering this is somewhat tricky and depends on the specific device which 1652 // have different rates for fma or all f64 operations. 1653 // 1654 // v_fma_f64 and v_mul_f64 always take the same number of cycles as each other 1655 // regardless of which device (although the number of cycles differs between 1656 // devices), so it is always profitable for f64. 1657 // 1658 // v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable 1659 // only on full rate devices. Normally, we should prefer selecting v_mad_f32 1660 // which we can always do even without fused FP ops since it returns the same 1661 // result as the separate operations and since it is always full 1662 // rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32 1663 // however does not support denormals, so we do report fma as faster if we have 1664 // a fast fma device and require denormals. 1665 // 1666 bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { 1667 VT = VT.getScalarType(); 1668 1669 if (!VT.isSimple()) 1670 return false; 1671 1672 switch (VT.getSimpleVT().SimpleTy) { 1673 case MVT::f32: 1674 // This is as fast on some subtargets. However, we always have full rate f32 1675 // mad available which returns the same result as the separate operations 1676 // which we should prefer over fma. We can't use this if we want to support 1677 // denormals, so only report this in these cases. 1678 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32(); 1679 case MVT::f64: 1680 return true; 1681 default: 1682 break; 1683 } 1684 1685 return false; 1686 } 1687 1688 //===----------------------------------------------------------------------===// 1689 // Custom DAG Lowering Operations 1690 //===----------------------------------------------------------------------===// 1691 1692 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 1693 switch (Op.getOpcode()) { 1694 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); 1695 case ISD::BRCOND: return LowerBRCOND(Op, DAG); 1696 case ISD::LOAD: { 1697 SDValue Result = LowerLOAD(Op, DAG); 1698 assert((!Result.getNode() || 1699 Result.getNode()->getNumValues() == 2) && 1700 "Load should return a value and a chain"); 1701 return Result; 1702 } 1703 1704 case ISD::FSIN: 1705 case ISD::FCOS: 1706 return LowerTrig(Op, DAG); 1707 case ISD::SELECT: return LowerSELECT(Op, DAG); 1708 case ISD::FDIV: return LowerFDIV(Op, DAG); 1709 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG); 1710 case ISD::STORE: return LowerSTORE(Op, DAG); 1711 case ISD::GlobalAddress: { 1712 MachineFunction &MF = DAG.getMachineFunction(); 1713 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 1714 return LowerGlobalAddress(MFI, Op, DAG); 1715 } 1716 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); 1717 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG); 1718 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG); 1719 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG); 1720 case ISD::TRAP: return lowerTRAP(Op, DAG); 1721 } 1722 return SDValue(); 1723 } 1724 1725 /// \brief Helper function for LowerBRCOND 1726 static SDNode *findUser(SDValue Value, unsigned Opcode) { 1727 1728 SDNode *Parent = Value.getNode(); 1729 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end(); 1730 I != E; ++I) { 1731 1732 if (I.getUse().get() != Value) 1733 continue; 1734 1735 if (I->getOpcode() == Opcode) 1736 return *I; 1737 } 1738 return nullptr; 1739 } 1740 1741 bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const { 1742 if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) { 1743 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) { 1744 case AMDGPUIntrinsic::amdgcn_if: 1745 case AMDGPUIntrinsic::amdgcn_else: 1746 case AMDGPUIntrinsic::amdgcn_end_cf: 1747 case AMDGPUIntrinsic::amdgcn_loop: 1748 return true; 1749 default: 1750 return false; 1751 } 1752 } 1753 1754 if (Intr->getOpcode() == ISD::INTRINSIC_WO_CHAIN) { 1755 switch (cast<ConstantSDNode>(Intr->getOperand(0))->getZExtValue()) { 1756 case AMDGPUIntrinsic::amdgcn_break: 1757 case AMDGPUIntrinsic::amdgcn_if_break: 1758 case AMDGPUIntrinsic::amdgcn_else_break: 1759 return true; 1760 default: 1761 return false; 1762 } 1763 } 1764 1765 return false; 1766 } 1767 1768 void SITargetLowering::createDebuggerPrologueStackObjects( 1769 MachineFunction &MF) const { 1770 // Create stack objects that are used for emitting debugger prologue. 1771 // 1772 // Debugger prologue writes work group IDs and work item IDs to scratch memory 1773 // at fixed location in the following format: 1774 // offset 0: work group ID x 1775 // offset 4: work group ID y 1776 // offset 8: work group ID z 1777 // offset 16: work item ID x 1778 // offset 20: work item ID y 1779 // offset 24: work item ID z 1780 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1781 int ObjectIdx = 0; 1782 1783 // For each dimension: 1784 for (unsigned i = 0; i < 3; ++i) { 1785 // Create fixed stack object for work group ID. 1786 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4, true); 1787 Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx); 1788 // Create fixed stack object for work item ID. 1789 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4 + 16, true); 1790 Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx); 1791 } 1792 } 1793 1794 /// This transforms the control flow intrinsics to get the branch destination as 1795 /// last parameter, also switches branch target with BR if the need arise 1796 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, 1797 SelectionDAG &DAG) const { 1798 1799 SDLoc DL(BRCOND); 1800 1801 SDNode *Intr = BRCOND.getOperand(1).getNode(); 1802 SDValue Target = BRCOND.getOperand(2); 1803 SDNode *BR = nullptr; 1804 SDNode *SetCC = nullptr; 1805 1806 if (Intr->getOpcode() == ISD::SETCC) { 1807 // As long as we negate the condition everything is fine 1808 SetCC = Intr; 1809 Intr = SetCC->getOperand(0).getNode(); 1810 1811 } else { 1812 // Get the target from BR if we don't negate the condition 1813 BR = findUser(BRCOND, ISD::BR); 1814 Target = BR->getOperand(1); 1815 } 1816 1817 // FIXME: This changes the types of the intrinsics instead of introducing new 1818 // nodes with the correct types. 1819 // e.g. llvm.amdgcn.loop 1820 1821 // eg: i1,ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3 1822 // => t9: ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3, BasicBlock:ch<bb1 0x7fee5286d088> 1823 1824 if (!isCFIntrinsic(Intr)) { 1825 // This is a uniform branch so we don't need to legalize. 1826 return BRCOND; 1827 } 1828 1829 bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID || 1830 Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN; 1831 1832 assert(!SetCC || 1833 (SetCC->getConstantOperandVal(1) == 1 && 1834 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() == 1835 ISD::SETNE)); 1836 1837 // operands of the new intrinsic call 1838 SmallVector<SDValue, 4> Ops; 1839 if (HaveChain) 1840 Ops.push_back(BRCOND.getOperand(0)); 1841 1842 Ops.append(Intr->op_begin() + (HaveChain ? 1 : 0), Intr->op_end()); 1843 Ops.push_back(Target); 1844 1845 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end()); 1846 1847 // build the new intrinsic call 1848 SDNode *Result = DAG.getNode( 1849 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL, 1850 DAG.getVTList(Res), Ops).getNode(); 1851 1852 if (!HaveChain) { 1853 SDValue Ops[] = { 1854 SDValue(Result, 0), 1855 BRCOND.getOperand(0) 1856 }; 1857 1858 Result = DAG.getMergeValues(Ops, DL).getNode(); 1859 } 1860 1861 if (BR) { 1862 // Give the branch instruction our target 1863 SDValue Ops[] = { 1864 BR->getOperand(0), 1865 BRCOND.getOperand(2) 1866 }; 1867 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops); 1868 DAG.ReplaceAllUsesWith(BR, NewBR.getNode()); 1869 BR = NewBR.getNode(); 1870 } 1871 1872 SDValue Chain = SDValue(Result, Result->getNumValues() - 1); 1873 1874 // Copy the intrinsic results to registers 1875 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) { 1876 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg); 1877 if (!CopyToReg) 1878 continue; 1879 1880 Chain = DAG.getCopyToReg( 1881 Chain, DL, 1882 CopyToReg->getOperand(1), 1883 SDValue(Result, i - 1), 1884 SDValue()); 1885 1886 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0)); 1887 } 1888 1889 // Remove the old intrinsic from the chain 1890 DAG.ReplaceAllUsesOfValueWith( 1891 SDValue(Intr, Intr->getNumValues() - 1), 1892 Intr->getOperand(0)); 1893 1894 return Chain; 1895 } 1896 1897 SDValue SITargetLowering::getSegmentAperture(unsigned AS, 1898 SelectionDAG &DAG) const { 1899 SDLoc SL; 1900 MachineFunction &MF = DAG.getMachineFunction(); 1901 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1902 unsigned UserSGPR = Info->getQueuePtrUserSGPR(); 1903 assert(UserSGPR != AMDGPU::NoRegister); 1904 1905 SDValue QueuePtr = CreateLiveInRegister( 1906 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64); 1907 1908 // Offset into amd_queue_t for group_segment_aperture_base_hi / 1909 // private_segment_aperture_base_hi. 1910 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44; 1911 1912 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr, 1913 DAG.getConstant(StructOffset, SL, MVT::i64)); 1914 1915 // TODO: Use custom target PseudoSourceValue. 1916 // TODO: We should use the value from the IR intrinsic call, but it might not 1917 // be available and how do we get it? 1918 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()), 1919 AMDGPUAS::CONSTANT_ADDRESS)); 1920 1921 MachinePointerInfo PtrInfo(V, StructOffset); 1922 return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr, PtrInfo, 1923 MinAlign(64, StructOffset), 1924 MachineMemOperand::MODereferenceable | 1925 MachineMemOperand::MOInvariant); 1926 } 1927 1928 SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op, 1929 SelectionDAG &DAG) const { 1930 SDLoc SL(Op); 1931 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op); 1932 1933 SDValue Src = ASC->getOperand(0); 1934 1935 // FIXME: Really support non-0 null pointers. 1936 SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32); 1937 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64); 1938 1939 // flat -> local/private 1940 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) { 1941 if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS || 1942 ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) { 1943 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE); 1944 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src); 1945 1946 return DAG.getNode(ISD::SELECT, SL, MVT::i32, 1947 NonNull, Ptr, SegmentNullPtr); 1948 } 1949 } 1950 1951 // local/private -> flat 1952 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) { 1953 if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS || 1954 ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) { 1955 SDValue NonNull 1956 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE); 1957 1958 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG); 1959 SDValue CvtPtr 1960 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture); 1961 1962 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull, 1963 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr), 1964 FlatNullPtr); 1965 } 1966 } 1967 1968 // global <-> flat are no-ops and never emitted. 1969 1970 const MachineFunction &MF = DAG.getMachineFunction(); 1971 DiagnosticInfoUnsupported InvalidAddrSpaceCast( 1972 *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc()); 1973 DAG.getContext()->diagnose(InvalidAddrSpaceCast); 1974 1975 return DAG.getUNDEF(ASC->getValueType(0)); 1976 } 1977 1978 static bool shouldEmitGOTReloc(const GlobalValue *GV, 1979 const TargetMachine &TM) { 1980 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS && 1981 !TM.shouldAssumeDSOLocal(*GV->getParent(), GV); 1982 } 1983 1984 bool 1985 SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 1986 // We can fold offsets for anything that doesn't require a GOT relocation. 1987 return GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS && 1988 !shouldEmitGOTReloc(GA->getGlobal(), getTargetMachine()); 1989 } 1990 1991 static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV, 1992 SDLoc DL, unsigned Offset, EVT PtrVT, 1993 unsigned GAFlags = SIInstrInfo::MO_NONE) { 1994 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is 1995 // lowered to the following code sequence: 1996 // s_getpc_b64 s[0:1] 1997 // s_add_u32 s0, s0, $symbol 1998 // s_addc_u32 s1, s1, 0 1999 // 2000 // s_getpc_b64 returns the address of the s_add_u32 instruction and then 2001 // a fixup or relocation is emitted to replace $symbol with a literal 2002 // constant, which is a pc-relative offset from the encoding of the $symbol 2003 // operand to the global variable. 2004 // 2005 // What we want here is an offset from the value returned by s_getpc 2006 // (which is the address of the s_add_u32 instruction) to the global 2007 // variable, but since the encoding of $symbol starts 4 bytes after the start 2008 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too 2009 // small. This requires us to add 4 to the global variable offset in order to 2010 // compute the correct address. 2011 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4, 2012 GAFlags); 2013 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, GA); 2014 } 2015 2016 SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI, 2017 SDValue Op, 2018 SelectionDAG &DAG) const { 2019 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op); 2020 2021 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS && 2022 GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS) 2023 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG); 2024 2025 SDLoc DL(GSD); 2026 const GlobalValue *GV = GSD->getGlobal(); 2027 EVT PtrVT = Op.getValueType(); 2028 2029 if (!shouldEmitGOTReloc(GV, getTargetMachine())) 2030 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT); 2031 2032 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT, 2033 SIInstrInfo::MO_GOTPCREL); 2034 2035 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext()); 2036 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS); 2037 const DataLayout &DataLayout = DAG.getDataLayout(); 2038 unsigned Align = DataLayout.getABITypeAlignment(PtrTy); 2039 // FIXME: Use a PseudoSourceValue once those can be assigned an address space. 2040 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); 2041 2042 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align, 2043 MachineMemOperand::MODereferenceable | 2044 MachineMemOperand::MOInvariant); 2045 } 2046 2047 SDValue SITargetLowering::lowerTRAP(SDValue Op, 2048 SelectionDAG &DAG) const { 2049 const MachineFunction &MF = DAG.getMachineFunction(); 2050 DiagnosticInfoUnsupported NoTrap(*MF.getFunction(), 2051 "trap handler not supported", 2052 Op.getDebugLoc(), 2053 DS_Warning); 2054 DAG.getContext()->diagnose(NoTrap); 2055 2056 // Emit s_endpgm. 2057 2058 // FIXME: This should really be selected to s_trap, but that requires 2059 // setting up the trap handler for it o do anything. 2060 return DAG.getNode(AMDGPUISD::ENDPGM, SDLoc(Op), MVT::Other, 2061 Op.getOperand(0)); 2062 } 2063 2064 SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, 2065 const SDLoc &DL, SDValue V) const { 2066 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as 2067 // the destination register. 2068 // 2069 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions, 2070 // so we will end up with redundant moves to m0. 2071 // 2072 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result. 2073 2074 // A Null SDValue creates a glue result. 2075 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue, 2076 V, Chain); 2077 return SDValue(M0, 0); 2078 } 2079 2080 SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG, 2081 SDValue Op, 2082 MVT VT, 2083 unsigned Offset) const { 2084 SDLoc SL(Op); 2085 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL, 2086 DAG.getEntryNode(), Offset, false); 2087 // The local size values will have the hi 16-bits as zero. 2088 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param, 2089 DAG.getValueType(VT)); 2090 } 2091 2092 static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) { 2093 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(), 2094 "non-hsa intrinsic with hsa target", 2095 DL.getDebugLoc()); 2096 DAG.getContext()->diagnose(BadIntrin); 2097 return DAG.getUNDEF(VT); 2098 } 2099 2100 static SDValue emitRemovedIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) { 2101 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(), 2102 "intrinsic not supported on subtarget", 2103 DL.getDebugLoc()); 2104 DAG.getContext()->diagnose(BadIntrin); 2105 return DAG.getUNDEF(VT); 2106 } 2107 2108 SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, 2109 SelectionDAG &DAG) const { 2110 MachineFunction &MF = DAG.getMachineFunction(); 2111 auto MFI = MF.getInfo<SIMachineFunctionInfo>(); 2112 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 2113 2114 EVT VT = Op.getValueType(); 2115 SDLoc DL(Op); 2116 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 2117 2118 // TODO: Should this propagate fast-math-flags? 2119 2120 switch (IntrinsicID) { 2121 case Intrinsic::amdgcn_dispatch_ptr: 2122 case Intrinsic::amdgcn_queue_ptr: { 2123 if (!Subtarget->isAmdCodeObjectV2()) { 2124 DiagnosticInfoUnsupported BadIntrin( 2125 *MF.getFunction(), "unsupported hsa intrinsic without hsa target", 2126 DL.getDebugLoc()); 2127 DAG.getContext()->diagnose(BadIntrin); 2128 return DAG.getUNDEF(VT); 2129 } 2130 2131 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ? 2132 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR; 2133 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, 2134 TRI->getPreloadedValue(MF, Reg), VT); 2135 } 2136 case Intrinsic::amdgcn_implicitarg_ptr: { 2137 unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT); 2138 return LowerParameterPtr(DAG, DL, DAG.getEntryNode(), offset); 2139 } 2140 case Intrinsic::amdgcn_kernarg_segment_ptr: { 2141 unsigned Reg 2142 = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR); 2143 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT); 2144 } 2145 case Intrinsic::amdgcn_dispatch_id: { 2146 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_ID); 2147 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT); 2148 } 2149 case Intrinsic::amdgcn_rcp: 2150 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1)); 2151 case Intrinsic::amdgcn_rsq: 2152 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name 2153 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); 2154 case Intrinsic::amdgcn_rsq_legacy: { 2155 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) 2156 return emitRemovedIntrinsicError(DAG, DL, VT); 2157 2158 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1)); 2159 } 2160 case Intrinsic::amdgcn_rcp_legacy: { 2161 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) 2162 return emitRemovedIntrinsicError(DAG, DL, VT); 2163 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1)); 2164 } 2165 case Intrinsic::amdgcn_rsq_clamp: { 2166 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS) 2167 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1)); 2168 2169 Type *Type = VT.getTypeForEVT(*DAG.getContext()); 2170 APFloat Max = APFloat::getLargest(Type->getFltSemantics()); 2171 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true); 2172 2173 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); 2174 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq, 2175 DAG.getConstantFP(Max, DL, VT)); 2176 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp, 2177 DAG.getConstantFP(Min, DL, VT)); 2178 } 2179 case Intrinsic::r600_read_ngroups_x: 2180 if (Subtarget->isAmdHsaOS()) 2181 return emitNonHSAIntrinsicError(DAG, DL, VT); 2182 2183 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2184 SI::KernelInputOffsets::NGROUPS_X, false); 2185 case Intrinsic::r600_read_ngroups_y: 2186 if (Subtarget->isAmdHsaOS()) 2187 return emitNonHSAIntrinsicError(DAG, DL, VT); 2188 2189 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2190 SI::KernelInputOffsets::NGROUPS_Y, false); 2191 case Intrinsic::r600_read_ngroups_z: 2192 if (Subtarget->isAmdHsaOS()) 2193 return emitNonHSAIntrinsicError(DAG, DL, VT); 2194 2195 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2196 SI::KernelInputOffsets::NGROUPS_Z, false); 2197 case Intrinsic::r600_read_global_size_x: 2198 if (Subtarget->isAmdHsaOS()) 2199 return emitNonHSAIntrinsicError(DAG, DL, VT); 2200 2201 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2202 SI::KernelInputOffsets::GLOBAL_SIZE_X, false); 2203 case Intrinsic::r600_read_global_size_y: 2204 if (Subtarget->isAmdHsaOS()) 2205 return emitNonHSAIntrinsicError(DAG, DL, VT); 2206 2207 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2208 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false); 2209 case Intrinsic::r600_read_global_size_z: 2210 if (Subtarget->isAmdHsaOS()) 2211 return emitNonHSAIntrinsicError(DAG, DL, VT); 2212 2213 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2214 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false); 2215 case Intrinsic::r600_read_local_size_x: 2216 if (Subtarget->isAmdHsaOS()) 2217 return emitNonHSAIntrinsicError(DAG, DL, VT); 2218 2219 return lowerImplicitZextParam(DAG, Op, MVT::i16, 2220 SI::KernelInputOffsets::LOCAL_SIZE_X); 2221 case Intrinsic::r600_read_local_size_y: 2222 if (Subtarget->isAmdHsaOS()) 2223 return emitNonHSAIntrinsicError(DAG, DL, VT); 2224 2225 return lowerImplicitZextParam(DAG, Op, MVT::i16, 2226 SI::KernelInputOffsets::LOCAL_SIZE_Y); 2227 case Intrinsic::r600_read_local_size_z: 2228 if (Subtarget->isAmdHsaOS()) 2229 return emitNonHSAIntrinsicError(DAG, DL, VT); 2230 2231 return lowerImplicitZextParam(DAG, Op, MVT::i16, 2232 SI::KernelInputOffsets::LOCAL_SIZE_Z); 2233 case Intrinsic::amdgcn_workgroup_id_x: 2234 case Intrinsic::r600_read_tgid_x: 2235 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, 2236 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT); 2237 case Intrinsic::amdgcn_workgroup_id_y: 2238 case Intrinsic::r600_read_tgid_y: 2239 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, 2240 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT); 2241 case Intrinsic::amdgcn_workgroup_id_z: 2242 case Intrinsic::r600_read_tgid_z: 2243 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, 2244 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT); 2245 case Intrinsic::amdgcn_workitem_id_x: 2246 case Intrinsic::r600_read_tidig_x: 2247 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, 2248 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT); 2249 case Intrinsic::amdgcn_workitem_id_y: 2250 case Intrinsic::r600_read_tidig_y: 2251 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, 2252 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT); 2253 case Intrinsic::amdgcn_workitem_id_z: 2254 case Intrinsic::r600_read_tidig_z: 2255 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, 2256 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT); 2257 case AMDGPUIntrinsic::SI_load_const: { 2258 SDValue Ops[] = { 2259 Op.getOperand(1), 2260 Op.getOperand(2) 2261 }; 2262 2263 MachineMemOperand *MMO = MF.getMachineMemOperand( 2264 MachinePointerInfo(), 2265 MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable | 2266 MachineMemOperand::MOInvariant, 2267 VT.getStoreSize(), 4); 2268 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL, 2269 Op->getVTList(), Ops, VT, MMO); 2270 } 2271 case AMDGPUIntrinsic::amdgcn_fdiv_fast: { 2272 return lowerFDIV_FAST(Op, DAG); 2273 } 2274 case AMDGPUIntrinsic::SI_vs_load_input: 2275 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT, 2276 Op.getOperand(1), 2277 Op.getOperand(2), 2278 Op.getOperand(3)); 2279 2280 case AMDGPUIntrinsic::SI_fs_constant: { 2281 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3)); 2282 SDValue Glue = M0.getValue(1); 2283 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32, 2284 DAG.getConstant(2, DL, MVT::i32), // P0 2285 Op.getOperand(1), Op.getOperand(2), Glue); 2286 } 2287 case AMDGPUIntrinsic::SI_packf16: 2288 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef()) 2289 return DAG.getUNDEF(MVT::i32); 2290 return Op; 2291 case AMDGPUIntrinsic::SI_fs_interp: { 2292 SDValue IJ = Op.getOperand(4); 2293 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ, 2294 DAG.getConstant(0, DL, MVT::i32)); 2295 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ, 2296 DAG.getConstant(1, DL, MVT::i32)); 2297 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3)); 2298 SDValue Glue = M0.getValue(1); 2299 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL, 2300 DAG.getVTList(MVT::f32, MVT::Glue), 2301 I, Op.getOperand(1), Op.getOperand(2), Glue); 2302 Glue = SDValue(P1.getNode(), 1); 2303 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J, 2304 Op.getOperand(1), Op.getOperand(2), Glue); 2305 } 2306 case Intrinsic::amdgcn_interp_p1: { 2307 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4)); 2308 SDValue Glue = M0.getValue(1); 2309 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1), 2310 Op.getOperand(2), Op.getOperand(3), Glue); 2311 } 2312 case Intrinsic::amdgcn_interp_p2: { 2313 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5)); 2314 SDValue Glue = SDValue(M0.getNode(), 1); 2315 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1), 2316 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4), 2317 Glue); 2318 } 2319 case Intrinsic::amdgcn_sin: 2320 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1)); 2321 2322 case Intrinsic::amdgcn_cos: 2323 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1)); 2324 2325 case Intrinsic::amdgcn_log_clamp: { 2326 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS) 2327 return SDValue(); 2328 2329 DiagnosticInfoUnsupported BadIntrin( 2330 *MF.getFunction(), "intrinsic not supported on subtarget", 2331 DL.getDebugLoc()); 2332 DAG.getContext()->diagnose(BadIntrin); 2333 return DAG.getUNDEF(VT); 2334 } 2335 case Intrinsic::amdgcn_ldexp: 2336 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT, 2337 Op.getOperand(1), Op.getOperand(2)); 2338 2339 case Intrinsic::amdgcn_fract: 2340 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1)); 2341 2342 case Intrinsic::amdgcn_class: 2343 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT, 2344 Op.getOperand(1), Op.getOperand(2)); 2345 case Intrinsic::amdgcn_div_fmas: 2346 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT, 2347 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3), 2348 Op.getOperand(4)); 2349 2350 case Intrinsic::amdgcn_div_fixup: 2351 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT, 2352 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 2353 2354 case Intrinsic::amdgcn_trig_preop: 2355 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT, 2356 Op.getOperand(1), Op.getOperand(2)); 2357 case Intrinsic::amdgcn_div_scale: { 2358 // 3rd parameter required to be a constant. 2359 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3)); 2360 if (!Param) 2361 return DAG.getUNDEF(VT); 2362 2363 // Translate to the operands expected by the machine instruction. The 2364 // first parameter must be the same as the first instruction. 2365 SDValue Numerator = Op.getOperand(1); 2366 SDValue Denominator = Op.getOperand(2); 2367 2368 // Note this order is opposite of the machine instruction's operations, 2369 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The 2370 // intrinsic has the numerator as the first operand to match a normal 2371 // division operation. 2372 2373 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator; 2374 2375 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0, 2376 Denominator, Numerator); 2377 } 2378 case Intrinsic::amdgcn_icmp: { 2379 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3)); 2380 int CondCode = CD->getSExtValue(); 2381 2382 if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE || 2383 CondCode >= ICmpInst::Predicate::BAD_ICMP_PREDICATE) 2384 return DAG.getUNDEF(VT); 2385 2386 ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode); 2387 ISD::CondCode CCOpcode = getICmpCondCode(IcInput); 2388 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1), 2389 Op.getOperand(2), DAG.getCondCode(CCOpcode)); 2390 } 2391 case Intrinsic::amdgcn_fcmp: { 2392 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3)); 2393 int CondCode = CD->getSExtValue(); 2394 2395 if (CondCode <= FCmpInst::Predicate::FCMP_FALSE || 2396 CondCode >= FCmpInst::Predicate::FCMP_TRUE) 2397 return DAG.getUNDEF(VT); 2398 2399 FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode); 2400 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput); 2401 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1), 2402 Op.getOperand(2), DAG.getCondCode(CCOpcode)); 2403 } 2404 case Intrinsic::amdgcn_fmul_legacy: 2405 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT, 2406 Op.getOperand(1), Op.getOperand(2)); 2407 case Intrinsic::amdgcn_sffbh: 2408 case AMDGPUIntrinsic::AMDGPU_flbit_i32: // Legacy name. 2409 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1)); 2410 default: 2411 return AMDGPUTargetLowering::LowerOperation(Op, DAG); 2412 } 2413 } 2414 2415 SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, 2416 SelectionDAG &DAG) const { 2417 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 2418 switch (IntrID) { 2419 case Intrinsic::amdgcn_atomic_inc: 2420 case Intrinsic::amdgcn_atomic_dec: { 2421 MemSDNode *M = cast<MemSDNode>(Op); 2422 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ? 2423 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC; 2424 SDValue Ops[] = { 2425 M->getOperand(0), // Chain 2426 M->getOperand(2), // Ptr 2427 M->getOperand(3) // Value 2428 }; 2429 2430 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops, 2431 M->getMemoryVT(), M->getMemOperand()); 2432 } 2433 default: 2434 return SDValue(); 2435 } 2436 } 2437 2438 SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op, 2439 SelectionDAG &DAG) const { 2440 MachineFunction &MF = DAG.getMachineFunction(); 2441 SDLoc DL(Op); 2442 SDValue Chain = Op.getOperand(0); 2443 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 2444 2445 switch (IntrinsicID) { 2446 case AMDGPUIntrinsic::SI_sendmsg: { 2447 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3)); 2448 SDValue Glue = Chain.getValue(1); 2449 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain, 2450 Op.getOperand(2), Glue); 2451 } 2452 case AMDGPUIntrinsic::SI_tbuffer_store: { 2453 SDValue Ops[] = { 2454 Chain, 2455 Op.getOperand(2), 2456 Op.getOperand(3), 2457 Op.getOperand(4), 2458 Op.getOperand(5), 2459 Op.getOperand(6), 2460 Op.getOperand(7), 2461 Op.getOperand(8), 2462 Op.getOperand(9), 2463 Op.getOperand(10), 2464 Op.getOperand(11), 2465 Op.getOperand(12), 2466 Op.getOperand(13), 2467 Op.getOperand(14) 2468 }; 2469 2470 EVT VT = Op.getOperand(3).getValueType(); 2471 2472 MachineMemOperand *MMO = MF.getMachineMemOperand( 2473 MachinePointerInfo(), 2474 MachineMemOperand::MOStore, 2475 VT.getStoreSize(), 4); 2476 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL, 2477 Op->getVTList(), Ops, VT, MMO); 2478 } 2479 case AMDGPUIntrinsic::AMDGPU_kill: { 2480 SDValue Src = Op.getOperand(2); 2481 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) { 2482 if (!K->isNegative()) 2483 return Chain; 2484 2485 SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32); 2486 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne); 2487 } 2488 2489 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src); 2490 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast); 2491 } 2492 default: 2493 return SDValue(); 2494 } 2495 } 2496 2497 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { 2498 SDLoc DL(Op); 2499 LoadSDNode *Load = cast<LoadSDNode>(Op); 2500 ISD::LoadExtType ExtType = Load->getExtensionType(); 2501 EVT MemVT = Load->getMemoryVT(); 2502 2503 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) { 2504 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected"); 2505 // FIXME: Copied from PPC 2506 // First, load into 32 bits, then truncate to 1 bit. 2507 2508 SDValue Chain = Load->getChain(); 2509 SDValue BasePtr = Load->getBasePtr(); 2510 MachineMemOperand *MMO = Load->getMemOperand(); 2511 2512 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain, 2513 BasePtr, MVT::i8, MMO); 2514 2515 SDValue Ops[] = { 2516 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD), 2517 NewLD.getValue(1) 2518 }; 2519 2520 return DAG.getMergeValues(Ops, DL); 2521 } 2522 2523 if (!MemVT.isVector()) 2524 return SDValue(); 2525 2526 assert(Op.getValueType().getVectorElementType() == MVT::i32 && 2527 "Custom lowering for non-i32 vectors hasn't been implemented."); 2528 2529 unsigned AS = Load->getAddressSpace(); 2530 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT, 2531 AS, Load->getAlignment())) { 2532 SDValue Ops[2]; 2533 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG); 2534 return DAG.getMergeValues(Ops, DL); 2535 } 2536 2537 unsigned NumElements = MemVT.getVectorNumElements(); 2538 switch (AS) { 2539 case AMDGPUAS::CONSTANT_ADDRESS: 2540 if (isMemOpUniform(Load)) 2541 return SDValue(); 2542 // Non-uniform loads will be selected to MUBUF instructions, so they 2543 // have the same legalization requires ments as global and private 2544 // loads. 2545 // 2546 LLVM_FALLTHROUGH; 2547 case AMDGPUAS::GLOBAL_ADDRESS: 2548 case AMDGPUAS::FLAT_ADDRESS: 2549 if (NumElements > 4) 2550 return SplitVectorLoad(Op, DAG); 2551 // v4 loads are supported for private and global memory. 2552 return SDValue(); 2553 case AMDGPUAS::PRIVATE_ADDRESS: { 2554 // Depending on the setting of the private_element_size field in the 2555 // resource descriptor, we can only make private accesses up to a certain 2556 // size. 2557 switch (Subtarget->getMaxPrivateElementSize()) { 2558 case 4: 2559 return scalarizeVectorLoad(Load, DAG); 2560 case 8: 2561 if (NumElements > 2) 2562 return SplitVectorLoad(Op, DAG); 2563 return SDValue(); 2564 case 16: 2565 // Same as global/flat 2566 if (NumElements > 4) 2567 return SplitVectorLoad(Op, DAG); 2568 return SDValue(); 2569 default: 2570 llvm_unreachable("unsupported private_element_size"); 2571 } 2572 } 2573 case AMDGPUAS::LOCAL_ADDRESS: { 2574 if (NumElements > 2) 2575 return SplitVectorLoad(Op, DAG); 2576 2577 if (NumElements == 2) 2578 return SDValue(); 2579 2580 // If properly aligned, if we split we might be able to use ds_read_b64. 2581 return SplitVectorLoad(Op, DAG); 2582 } 2583 default: 2584 return SDValue(); 2585 } 2586 } 2587 2588 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 2589 if (Op.getValueType() != MVT::i64) 2590 return SDValue(); 2591 2592 SDLoc DL(Op); 2593 SDValue Cond = Op.getOperand(0); 2594 2595 SDValue Zero = DAG.getConstant(0, DL, MVT::i32); 2596 SDValue One = DAG.getConstant(1, DL, MVT::i32); 2597 2598 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1)); 2599 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2)); 2600 2601 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero); 2602 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero); 2603 2604 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1); 2605 2606 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One); 2607 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One); 2608 2609 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1); 2610 2611 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi}); 2612 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res); 2613 } 2614 2615 // Catch division cases where we can use shortcuts with rcp and rsq 2616 // instructions. 2617 SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op, 2618 SelectionDAG &DAG) const { 2619 SDLoc SL(Op); 2620 SDValue LHS = Op.getOperand(0); 2621 SDValue RHS = Op.getOperand(1); 2622 EVT VT = Op.getValueType(); 2623 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath; 2624 2625 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) { 2626 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals()))) { 2627 2628 if (CLHS->isExactlyValue(1.0)) { 2629 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to 2630 // the CI documentation has a worst case error of 1 ulp. 2631 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to 2632 // use it as long as we aren't trying to use denormals. 2633 2634 // 1.0 / sqrt(x) -> rsq(x) 2635 // 2636 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP 2637 // error seems really high at 2^29 ULP. 2638 if (RHS.getOpcode() == ISD::FSQRT) 2639 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0)); 2640 2641 // 1.0 / x -> rcp(x) 2642 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); 2643 } 2644 2645 // Same as for 1.0, but expand the sign out of the constant. 2646 if (CLHS->isExactlyValue(-1.0)) { 2647 // -1.0 / x -> rcp (fneg x) 2648 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 2649 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS); 2650 } 2651 } 2652 } 2653 2654 const SDNodeFlags *Flags = Op->getFlags(); 2655 2656 if (Unsafe || Flags->hasAllowReciprocal()) { 2657 // Turn into multiply by the reciprocal. 2658 // x / y -> x * (1.0 / y) 2659 SDNodeFlags Flags; 2660 Flags.setUnsafeAlgebra(true); 2661 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); 2662 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags); 2663 } 2664 2665 return SDValue(); 2666 } 2667 2668 // Faster 2.5 ULP division that does not support denormals. 2669 SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const { 2670 SDLoc SL(Op); 2671 SDValue LHS = Op.getOperand(1); 2672 SDValue RHS = Op.getOperand(2); 2673 2674 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS); 2675 2676 const APFloat K0Val(BitsToFloat(0x6f800000)); 2677 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32); 2678 2679 const APFloat K1Val(BitsToFloat(0x2f800000)); 2680 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32); 2681 2682 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); 2683 2684 EVT SetCCVT = 2685 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32); 2686 2687 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT); 2688 2689 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One); 2690 2691 // TODO: Should this propagate fast-math-flags? 2692 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3); 2693 2694 // rcp does not support denormals. 2695 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1); 2696 2697 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0); 2698 2699 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul); 2700 } 2701 2702 SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const { 2703 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG)) 2704 return FastLowered; 2705 2706 SDLoc SL(Op); 2707 SDValue LHS = Op.getOperand(0); 2708 SDValue RHS = Op.getOperand(1); 2709 2710 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); 2711 2712 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1); 2713 2714 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, RHS, RHS, LHS); 2715 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, LHS, RHS, LHS); 2716 2717 // Denominator is scaled to not be denormal, so using rcp is ok. 2718 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, DenominatorScaled); 2719 2720 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, DenominatorScaled); 2721 2722 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, ApproxRcp, One); 2723 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, ApproxRcp); 2724 2725 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, NumeratorScaled, Fma1); 2726 2727 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, NumeratorScaled); 2728 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul); 2729 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, NumeratorScaled); 2730 2731 SDValue Scale = NumeratorScaled.getValue(1); 2732 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, Fma4, Fma1, Fma3, Scale); 2733 2734 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS); 2735 } 2736 2737 SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const { 2738 if (DAG.getTarget().Options.UnsafeFPMath) 2739 return lowerFastUnsafeFDIV(Op, DAG); 2740 2741 SDLoc SL(Op); 2742 SDValue X = Op.getOperand(0); 2743 SDValue Y = Op.getOperand(1); 2744 2745 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64); 2746 2747 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1); 2748 2749 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X); 2750 2751 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0); 2752 2753 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0); 2754 2755 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One); 2756 2757 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp); 2758 2759 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One); 2760 2761 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X); 2762 2763 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1); 2764 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3); 2765 2766 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64, 2767 NegDivScale0, Mul, DivScale1); 2768 2769 SDValue Scale; 2770 2771 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) { 2772 // Workaround a hardware bug on SI where the condition output from div_scale 2773 // is not usable. 2774 2775 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32); 2776 2777 // Figure out if the scale to use for div_fmas. 2778 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X); 2779 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y); 2780 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0); 2781 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1); 2782 2783 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi); 2784 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi); 2785 2786 SDValue Scale0Hi 2787 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi); 2788 SDValue Scale1Hi 2789 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi); 2790 2791 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ); 2792 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ); 2793 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen); 2794 } else { 2795 Scale = DivScale1.getValue(1); 2796 } 2797 2798 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64, 2799 Fma4, Fma3, Mul, Scale); 2800 2801 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X); 2802 } 2803 2804 SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const { 2805 EVT VT = Op.getValueType(); 2806 2807 if (VT == MVT::f32) 2808 return LowerFDIV32(Op, DAG); 2809 2810 if (VT == MVT::f64) 2811 return LowerFDIV64(Op, DAG); 2812 2813 llvm_unreachable("Unexpected type for fdiv"); 2814 } 2815 2816 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const { 2817 SDLoc DL(Op); 2818 StoreSDNode *Store = cast<StoreSDNode>(Op); 2819 EVT VT = Store->getMemoryVT(); 2820 2821 if (VT == MVT::i1) { 2822 return DAG.getTruncStore(Store->getChain(), DL, 2823 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32), 2824 Store->getBasePtr(), MVT::i1, Store->getMemOperand()); 2825 } 2826 2827 assert(VT.isVector() && 2828 Store->getValue().getValueType().getScalarType() == MVT::i32); 2829 2830 unsigned AS = Store->getAddressSpace(); 2831 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT, 2832 AS, Store->getAlignment())) { 2833 return expandUnalignedStore(Store, DAG); 2834 } 2835 2836 unsigned NumElements = VT.getVectorNumElements(); 2837 switch (AS) { 2838 case AMDGPUAS::GLOBAL_ADDRESS: 2839 case AMDGPUAS::FLAT_ADDRESS: 2840 if (NumElements > 4) 2841 return SplitVectorStore(Op, DAG); 2842 return SDValue(); 2843 case AMDGPUAS::PRIVATE_ADDRESS: { 2844 switch (Subtarget->getMaxPrivateElementSize()) { 2845 case 4: 2846 return scalarizeVectorStore(Store, DAG); 2847 case 8: 2848 if (NumElements > 2) 2849 return SplitVectorStore(Op, DAG); 2850 return SDValue(); 2851 case 16: 2852 if (NumElements > 4) 2853 return SplitVectorStore(Op, DAG); 2854 return SDValue(); 2855 default: 2856 llvm_unreachable("unsupported private_element_size"); 2857 } 2858 } 2859 case AMDGPUAS::LOCAL_ADDRESS: { 2860 if (NumElements > 2) 2861 return SplitVectorStore(Op, DAG); 2862 2863 if (NumElements == 2) 2864 return Op; 2865 2866 // If properly aligned, if we split we might be able to use ds_write_b64. 2867 return SplitVectorStore(Op, DAG); 2868 } 2869 default: 2870 llvm_unreachable("unhandled address space"); 2871 } 2872 } 2873 2874 SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const { 2875 SDLoc DL(Op); 2876 EVT VT = Op.getValueType(); 2877 SDValue Arg = Op.getOperand(0); 2878 // TODO: Should this propagate fast-math-flags? 2879 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT, 2880 DAG.getNode(ISD::FMUL, DL, VT, Arg, 2881 DAG.getConstantFP(0.5/M_PI, DL, 2882 VT))); 2883 2884 switch (Op.getOpcode()) { 2885 case ISD::FCOS: 2886 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart); 2887 case ISD::FSIN: 2888 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart); 2889 default: 2890 llvm_unreachable("Wrong trig opcode"); 2891 } 2892 } 2893 2894 SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const { 2895 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op); 2896 assert(AtomicNode->isCompareAndSwap()); 2897 unsigned AS = AtomicNode->getAddressSpace(); 2898 2899 // No custom lowering required for local address space 2900 if (!isFlatGlobalAddrSpace(AS)) 2901 return Op; 2902 2903 // Non-local address space requires custom lowering for atomic compare 2904 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2 2905 SDLoc DL(Op); 2906 SDValue ChainIn = Op.getOperand(0); 2907 SDValue Addr = Op.getOperand(1); 2908 SDValue Old = Op.getOperand(2); 2909 SDValue New = Op.getOperand(3); 2910 EVT VT = Op.getValueType(); 2911 MVT SimpleVT = VT.getSimpleVT(); 2912 MVT VecType = MVT::getVectorVT(SimpleVT, 2); 2913 2914 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old}); 2915 SDValue Ops[] = { ChainIn, Addr, NewOld }; 2916 2917 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(), 2918 Ops, VT, AtomicNode->getMemOperand()); 2919 } 2920 2921 //===----------------------------------------------------------------------===// 2922 // Custom DAG optimizations 2923 //===----------------------------------------------------------------------===// 2924 2925 SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N, 2926 DAGCombinerInfo &DCI) const { 2927 EVT VT = N->getValueType(0); 2928 EVT ScalarVT = VT.getScalarType(); 2929 if (ScalarVT != MVT::f32) 2930 return SDValue(); 2931 2932 SelectionDAG &DAG = DCI.DAG; 2933 SDLoc DL(N); 2934 2935 SDValue Src = N->getOperand(0); 2936 EVT SrcVT = Src.getValueType(); 2937 2938 // TODO: We could try to match extracting the higher bytes, which would be 2939 // easier if i8 vectors weren't promoted to i32 vectors, particularly after 2940 // types are legalized. v4i8 -> v4f32 is probably the only case to worry 2941 // about in practice. 2942 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) { 2943 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) { 2944 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src); 2945 DCI.AddToWorklist(Cvt.getNode()); 2946 return Cvt; 2947 } 2948 } 2949 2950 return SDValue(); 2951 } 2952 2953 /// \brief Return true if the given offset Size in bytes can be folded into 2954 /// the immediate offsets of a memory instruction for the given address space. 2955 static bool canFoldOffset(unsigned OffsetSize, unsigned AS, 2956 const SISubtarget &STI) { 2957 switch (AS) { 2958 case AMDGPUAS::GLOBAL_ADDRESS: { 2959 // MUBUF instructions a 12-bit offset in bytes. 2960 return isUInt<12>(OffsetSize); 2961 } 2962 case AMDGPUAS::CONSTANT_ADDRESS: { 2963 // SMRD instructions have an 8-bit offset in dwords on SI and 2964 // a 20-bit offset in bytes on VI. 2965 if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) 2966 return isUInt<20>(OffsetSize); 2967 else 2968 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4); 2969 } 2970 case AMDGPUAS::LOCAL_ADDRESS: 2971 case AMDGPUAS::REGION_ADDRESS: { 2972 // The single offset versions have a 16-bit offset in bytes. 2973 return isUInt<16>(OffsetSize); 2974 } 2975 case AMDGPUAS::PRIVATE_ADDRESS: 2976 // Indirect register addressing does not use any offsets. 2977 default: 2978 return 0; 2979 } 2980 } 2981 2982 // (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2) 2983 2984 // This is a variant of 2985 // (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2), 2986 // 2987 // The normal DAG combiner will do this, but only if the add has one use since 2988 // that would increase the number of instructions. 2989 // 2990 // This prevents us from seeing a constant offset that can be folded into a 2991 // memory instruction's addressing mode. If we know the resulting add offset of 2992 // a pointer can be folded into an addressing offset, we can replace the pointer 2993 // operand with the add of new constant offset. This eliminates one of the uses, 2994 // and may allow the remaining use to also be simplified. 2995 // 2996 SDValue SITargetLowering::performSHLPtrCombine(SDNode *N, 2997 unsigned AddrSpace, 2998 DAGCombinerInfo &DCI) const { 2999 SDValue N0 = N->getOperand(0); 3000 SDValue N1 = N->getOperand(1); 3001 3002 if (N0.getOpcode() != ISD::ADD) 3003 return SDValue(); 3004 3005 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1); 3006 if (!CN1) 3007 return SDValue(); 3008 3009 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 3010 if (!CAdd) 3011 return SDValue(); 3012 3013 // If the resulting offset is too large, we can't fold it into the addressing 3014 // mode offset. 3015 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue(); 3016 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget())) 3017 return SDValue(); 3018 3019 SelectionDAG &DAG = DCI.DAG; 3020 SDLoc SL(N); 3021 EVT VT = N->getValueType(0); 3022 3023 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1); 3024 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32); 3025 3026 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset); 3027 } 3028 3029 static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) { 3030 return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) || 3031 (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) || 3032 (Opc == ISD::XOR && Val == 0); 3033 } 3034 3035 // Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This 3036 // will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit 3037 // integer combine opportunities since most 64-bit operations are decomposed 3038 // this way. TODO: We won't want this for SALU especially if it is an inline 3039 // immediate. 3040 SDValue SITargetLowering::splitBinaryBitConstantOp( 3041 DAGCombinerInfo &DCI, 3042 const SDLoc &SL, 3043 unsigned Opc, SDValue LHS, 3044 const ConstantSDNode *CRHS) const { 3045 uint64_t Val = CRHS->getZExtValue(); 3046 uint32_t ValLo = Lo_32(Val); 3047 uint32_t ValHi = Hi_32(Val); 3048 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3049 3050 if ((bitOpWithConstantIsReducible(Opc, ValLo) || 3051 bitOpWithConstantIsReducible(Opc, ValHi)) || 3052 (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) { 3053 // If we need to materialize a 64-bit immediate, it will be split up later 3054 // anyway. Avoid creating the harder to understand 64-bit immediate 3055 // materialization. 3056 return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi); 3057 } 3058 3059 return SDValue(); 3060 } 3061 3062 SDValue SITargetLowering::performAndCombine(SDNode *N, 3063 DAGCombinerInfo &DCI) const { 3064 if (DCI.isBeforeLegalize()) 3065 return SDValue(); 3066 3067 SelectionDAG &DAG = DCI.DAG; 3068 EVT VT = N->getValueType(0); 3069 SDValue LHS = N->getOperand(0); 3070 SDValue RHS = N->getOperand(1); 3071 3072 3073 if (VT == MVT::i64) { 3074 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS); 3075 if (CRHS) { 3076 if (SDValue Split 3077 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS)) 3078 return Split; 3079 } 3080 } 3081 3082 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) -> 3083 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity) 3084 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) { 3085 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get(); 3086 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get(); 3087 3088 SDValue X = LHS.getOperand(0); 3089 SDValue Y = RHS.getOperand(0); 3090 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X) 3091 return SDValue(); 3092 3093 if (LCC == ISD::SETO) { 3094 if (X != LHS.getOperand(1)) 3095 return SDValue(); 3096 3097 if (RCC == ISD::SETUNE) { 3098 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1)); 3099 if (!C1 || !C1->isInfinity() || C1->isNegative()) 3100 return SDValue(); 3101 3102 const uint32_t Mask = SIInstrFlags::N_NORMAL | 3103 SIInstrFlags::N_SUBNORMAL | 3104 SIInstrFlags::N_ZERO | 3105 SIInstrFlags::P_ZERO | 3106 SIInstrFlags::P_SUBNORMAL | 3107 SIInstrFlags::P_NORMAL; 3108 3109 static_assert(((~(SIInstrFlags::S_NAN | 3110 SIInstrFlags::Q_NAN | 3111 SIInstrFlags::N_INFINITY | 3112 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask, 3113 "mask not equal"); 3114 3115 SDLoc DL(N); 3116 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, 3117 X, DAG.getConstant(Mask, DL, MVT::i32)); 3118 } 3119 } 3120 } 3121 3122 return SDValue(); 3123 } 3124 3125 SDValue SITargetLowering::performOrCombine(SDNode *N, 3126 DAGCombinerInfo &DCI) const { 3127 SelectionDAG &DAG = DCI.DAG; 3128 SDValue LHS = N->getOperand(0); 3129 SDValue RHS = N->getOperand(1); 3130 3131 EVT VT = N->getValueType(0); 3132 if (VT == MVT::i1) { 3133 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2) 3134 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS && 3135 RHS.getOpcode() == AMDGPUISD::FP_CLASS) { 3136 SDValue Src = LHS.getOperand(0); 3137 if (Src != RHS.getOperand(0)) 3138 return SDValue(); 3139 3140 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1)); 3141 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1)); 3142 if (!CLHS || !CRHS) 3143 return SDValue(); 3144 3145 // Only 10 bits are used. 3146 static const uint32_t MaxMask = 0x3ff; 3147 3148 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask; 3149 SDLoc DL(N); 3150 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, 3151 Src, DAG.getConstant(NewMask, DL, MVT::i32)); 3152 } 3153 3154 return SDValue(); 3155 } 3156 3157 if (VT != MVT::i64) 3158 return SDValue(); 3159 3160 // TODO: This could be a generic combine with a predicate for extracting the 3161 // high half of an integer being free. 3162 3163 // (or i64:x, (zero_extend i32:y)) -> 3164 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x))) 3165 if (LHS.getOpcode() == ISD::ZERO_EXTEND && 3166 RHS.getOpcode() != ISD::ZERO_EXTEND) 3167 std::swap(LHS, RHS); 3168 3169 if (RHS.getOpcode() == ISD::ZERO_EXTEND) { 3170 SDValue ExtSrc = RHS.getOperand(0); 3171 EVT SrcVT = ExtSrc.getValueType(); 3172 if (SrcVT == MVT::i32) { 3173 SDLoc SL(N); 3174 SDValue LowLHS, HiBits; 3175 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG); 3176 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc); 3177 3178 DCI.AddToWorklist(LowOr.getNode()); 3179 DCI.AddToWorklist(HiBits.getNode()); 3180 3181 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, 3182 LowOr, HiBits); 3183 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 3184 } 3185 } 3186 3187 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 3188 if (CRHS) { 3189 if (SDValue Split 3190 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS)) 3191 return Split; 3192 } 3193 3194 return SDValue(); 3195 } 3196 3197 SDValue SITargetLowering::performXorCombine(SDNode *N, 3198 DAGCombinerInfo &DCI) const { 3199 EVT VT = N->getValueType(0); 3200 if (VT != MVT::i64) 3201 return SDValue(); 3202 3203 SDValue LHS = N->getOperand(0); 3204 SDValue RHS = N->getOperand(1); 3205 3206 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS); 3207 if (CRHS) { 3208 if (SDValue Split 3209 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS)) 3210 return Split; 3211 } 3212 3213 return SDValue(); 3214 } 3215 3216 SDValue SITargetLowering::performClassCombine(SDNode *N, 3217 DAGCombinerInfo &DCI) const { 3218 SelectionDAG &DAG = DCI.DAG; 3219 SDValue Mask = N->getOperand(1); 3220 3221 // fp_class x, 0 -> false 3222 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) { 3223 if (CMask->isNullValue()) 3224 return DAG.getConstant(0, SDLoc(N), MVT::i1); 3225 } 3226 3227 if (N->getOperand(0).isUndef()) 3228 return DAG.getUNDEF(MVT::i1); 3229 3230 return SDValue(); 3231 } 3232 3233 // Constant fold canonicalize. 3234 SDValue SITargetLowering::performFCanonicalizeCombine( 3235 SDNode *N, 3236 DAGCombinerInfo &DCI) const { 3237 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); 3238 if (!CFP) 3239 return SDValue(); 3240 3241 SelectionDAG &DAG = DCI.DAG; 3242 const APFloat &C = CFP->getValueAPF(); 3243 3244 // Flush denormals to 0 if not enabled. 3245 if (C.isDenormal()) { 3246 EVT VT = N->getValueType(0); 3247 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals()) 3248 return DAG.getConstantFP(0.0, SDLoc(N), VT); 3249 3250 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals()) 3251 return DAG.getConstantFP(0.0, SDLoc(N), VT); 3252 } 3253 3254 if (C.isNaN()) { 3255 EVT VT = N->getValueType(0); 3256 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics()); 3257 if (C.isSignaling()) { 3258 // Quiet a signaling NaN. 3259 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT); 3260 } 3261 3262 // Make sure it is the canonical NaN bitpattern. 3263 // 3264 // TODO: Can we use -1 as the canonical NaN value since it's an inline 3265 // immediate? 3266 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt()) 3267 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT); 3268 } 3269 3270 return SDValue(CFP, 0); 3271 } 3272 3273 static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) { 3274 switch (Opc) { 3275 case ISD::FMAXNUM: 3276 return AMDGPUISD::FMAX3; 3277 case ISD::SMAX: 3278 return AMDGPUISD::SMAX3; 3279 case ISD::UMAX: 3280 return AMDGPUISD::UMAX3; 3281 case ISD::FMINNUM: 3282 return AMDGPUISD::FMIN3; 3283 case ISD::SMIN: 3284 return AMDGPUISD::SMIN3; 3285 case ISD::UMIN: 3286 return AMDGPUISD::UMIN3; 3287 default: 3288 llvm_unreachable("Not a min/max opcode"); 3289 } 3290 } 3291 3292 static SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL, 3293 SDValue Op0, SDValue Op1, bool Signed) { 3294 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1); 3295 if (!K1) 3296 return SDValue(); 3297 3298 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1)); 3299 if (!K0) 3300 return SDValue(); 3301 3302 if (Signed) { 3303 if (K0->getAPIntValue().sge(K1->getAPIntValue())) 3304 return SDValue(); 3305 } else { 3306 if (K0->getAPIntValue().uge(K1->getAPIntValue())) 3307 return SDValue(); 3308 } 3309 3310 EVT VT = K0->getValueType(0); 3311 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT, 3312 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0)); 3313 } 3314 3315 static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) { 3316 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions()) 3317 return true; 3318 3319 return DAG.isKnownNeverNaN(Op); 3320 } 3321 3322 static SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL, 3323 SDValue Op0, SDValue Op1) { 3324 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1); 3325 if (!K1) 3326 return SDValue(); 3327 3328 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1)); 3329 if (!K0) 3330 return SDValue(); 3331 3332 // Ordered >= (although NaN inputs should have folded away by now). 3333 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF()); 3334 if (Cmp == APFloat::cmpGreaterThan) 3335 return SDValue(); 3336 3337 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a 3338 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then 3339 // give the other result, which is different from med3 with a NaN input. 3340 SDValue Var = Op0.getOperand(0); 3341 if (!isKnownNeverSNan(DAG, Var)) 3342 return SDValue(); 3343 3344 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0), 3345 Var, SDValue(K0, 0), SDValue(K1, 0)); 3346 } 3347 3348 SDValue SITargetLowering::performMinMaxCombine(SDNode *N, 3349 DAGCombinerInfo &DCI) const { 3350 SelectionDAG &DAG = DCI.DAG; 3351 3352 unsigned Opc = N->getOpcode(); 3353 SDValue Op0 = N->getOperand(0); 3354 SDValue Op1 = N->getOperand(1); 3355 3356 // Only do this if the inner op has one use since this will just increases 3357 // register pressure for no benefit. 3358 3359 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) { 3360 // max(max(a, b), c) -> max3(a, b, c) 3361 // min(min(a, b), c) -> min3(a, b, c) 3362 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) { 3363 SDLoc DL(N); 3364 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), 3365 DL, 3366 N->getValueType(0), 3367 Op0.getOperand(0), 3368 Op0.getOperand(1), 3369 Op1); 3370 } 3371 3372 // Try commuted. 3373 // max(a, max(b, c)) -> max3(a, b, c) 3374 // min(a, min(b, c)) -> min3(a, b, c) 3375 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) { 3376 SDLoc DL(N); 3377 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), 3378 DL, 3379 N->getValueType(0), 3380 Op0, 3381 Op1.getOperand(0), 3382 Op1.getOperand(1)); 3383 } 3384 } 3385 3386 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1) 3387 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) { 3388 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true)) 3389 return Med3; 3390 } 3391 3392 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) { 3393 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false)) 3394 return Med3; 3395 } 3396 3397 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1) 3398 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) || 3399 (Opc == AMDGPUISD::FMIN_LEGACY && 3400 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) && 3401 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) { 3402 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1)) 3403 return Res; 3404 } 3405 3406 return SDValue(); 3407 } 3408 3409 SDValue SITargetLowering::performSetCCCombine(SDNode *N, 3410 DAGCombinerInfo &DCI) const { 3411 SelectionDAG &DAG = DCI.DAG; 3412 SDLoc SL(N); 3413 3414 SDValue LHS = N->getOperand(0); 3415 SDValue RHS = N->getOperand(1); 3416 EVT VT = LHS.getValueType(); 3417 3418 if (VT != MVT::f32 && VT != MVT::f64) 3419 return SDValue(); 3420 3421 // Match isinf pattern 3422 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity)) 3423 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); 3424 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) { 3425 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS); 3426 if (!CRHS) 3427 return SDValue(); 3428 3429 const APFloat &APF = CRHS->getValueAPF(); 3430 if (APF.isInfinity() && !APF.isNegative()) { 3431 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY; 3432 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0), 3433 DAG.getConstant(Mask, SL, MVT::i32)); 3434 } 3435 } 3436 3437 return SDValue(); 3438 } 3439 3440 SDValue SITargetLowering::PerformDAGCombine(SDNode *N, 3441 DAGCombinerInfo &DCI) const { 3442 SelectionDAG &DAG = DCI.DAG; 3443 SDLoc DL(N); 3444 3445 switch (N->getOpcode()) { 3446 default: 3447 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); 3448 case ISD::SETCC: 3449 return performSetCCCombine(N, DCI); 3450 case ISD::FMAXNUM: 3451 case ISD::FMINNUM: 3452 case ISD::SMAX: 3453 case ISD::SMIN: 3454 case ISD::UMAX: 3455 case ISD::UMIN: 3456 case AMDGPUISD::FMIN_LEGACY: 3457 case AMDGPUISD::FMAX_LEGACY: { 3458 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG && 3459 N->getValueType(0) != MVT::f64 && 3460 getTargetMachine().getOptLevel() > CodeGenOpt::None) 3461 return performMinMaxCombine(N, DCI); 3462 break; 3463 } 3464 3465 case AMDGPUISD::CVT_F32_UBYTE0: 3466 case AMDGPUISD::CVT_F32_UBYTE1: 3467 case AMDGPUISD::CVT_F32_UBYTE2: 3468 case AMDGPUISD::CVT_F32_UBYTE3: { 3469 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0; 3470 SDValue Src = N->getOperand(0); 3471 3472 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero. 3473 if (Src.getOpcode() == ISD::SRL) { 3474 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x 3475 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x 3476 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x 3477 3478 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(1))) { 3479 unsigned SrcOffset = C->getZExtValue() + 8 * Offset; 3480 if (SrcOffset < 32 && SrcOffset % 8 == 0) { 3481 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, DL, 3482 MVT::f32, Src.getOperand(0)); 3483 } 3484 } 3485 } 3486 3487 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8); 3488 3489 APInt KnownZero, KnownOne; 3490 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), 3491 !DCI.isBeforeLegalizeOps()); 3492 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3493 if (TLO.ShrinkDemandedConstant(Src, Demanded) || 3494 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) { 3495 DCI.CommitTargetLoweringOpt(TLO); 3496 } 3497 3498 break; 3499 } 3500 3501 case ISD::UINT_TO_FP: { 3502 return performUCharToFloatCombine(N, DCI); 3503 } 3504 case ISD::FADD: { 3505 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 3506 break; 3507 3508 EVT VT = N->getValueType(0); 3509 if (VT != MVT::f32) 3510 break; 3511 3512 // Only do this if we are not trying to support denormals. v_mad_f32 does 3513 // not support denormals ever. 3514 if (Subtarget->hasFP32Denormals()) 3515 break; 3516 3517 SDValue LHS = N->getOperand(0); 3518 SDValue RHS = N->getOperand(1); 3519 3520 // These should really be instruction patterns, but writing patterns with 3521 // source modiifiers is a pain. 3522 3523 // fadd (fadd (a, a), b) -> mad 2.0, a, b 3524 if (LHS.getOpcode() == ISD::FADD) { 3525 SDValue A = LHS.getOperand(0); 3526 if (A == LHS.getOperand(1)) { 3527 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); 3528 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS); 3529 } 3530 } 3531 3532 // fadd (b, fadd (a, a)) -> mad 2.0, a, b 3533 if (RHS.getOpcode() == ISD::FADD) { 3534 SDValue A = RHS.getOperand(0); 3535 if (A == RHS.getOperand(1)) { 3536 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); 3537 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS); 3538 } 3539 } 3540 3541 return SDValue(); 3542 } 3543 case ISD::FSUB: { 3544 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 3545 break; 3546 3547 EVT VT = N->getValueType(0); 3548 3549 // Try to get the fneg to fold into the source modifier. This undoes generic 3550 // DAG combines and folds them into the mad. 3551 // 3552 // Only do this if we are not trying to support denormals. v_mad_f32 does 3553 // not support denormals ever. 3554 if (VT == MVT::f32 && 3555 !Subtarget->hasFP32Denormals()) { 3556 SDValue LHS = N->getOperand(0); 3557 SDValue RHS = N->getOperand(1); 3558 if (LHS.getOpcode() == ISD::FADD) { 3559 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c) 3560 3561 SDValue A = LHS.getOperand(0); 3562 if (A == LHS.getOperand(1)) { 3563 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); 3564 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS); 3565 3566 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS); 3567 } 3568 } 3569 3570 if (RHS.getOpcode() == ISD::FADD) { 3571 // (fsub c, (fadd a, a)) -> mad -2.0, a, c 3572 3573 SDValue A = RHS.getOperand(0); 3574 if (A == RHS.getOperand(1)) { 3575 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32); 3576 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS); 3577 } 3578 } 3579 3580 return SDValue(); 3581 } 3582 3583 break; 3584 } 3585 case ISD::LOAD: 3586 case ISD::STORE: 3587 case ISD::ATOMIC_LOAD: 3588 case ISD::ATOMIC_STORE: 3589 case ISD::ATOMIC_CMP_SWAP: 3590 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: 3591 case ISD::ATOMIC_SWAP: 3592 case ISD::ATOMIC_LOAD_ADD: 3593 case ISD::ATOMIC_LOAD_SUB: 3594 case ISD::ATOMIC_LOAD_AND: 3595 case ISD::ATOMIC_LOAD_OR: 3596 case ISD::ATOMIC_LOAD_XOR: 3597 case ISD::ATOMIC_LOAD_NAND: 3598 case ISD::ATOMIC_LOAD_MIN: 3599 case ISD::ATOMIC_LOAD_MAX: 3600 case ISD::ATOMIC_LOAD_UMIN: 3601 case ISD::ATOMIC_LOAD_UMAX: 3602 case AMDGPUISD::ATOMIC_INC: 3603 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics. 3604 if (DCI.isBeforeLegalize()) 3605 break; 3606 3607 MemSDNode *MemNode = cast<MemSDNode>(N); 3608 SDValue Ptr = MemNode->getBasePtr(); 3609 3610 // TODO: We could also do this for multiplies. 3611 unsigned AS = MemNode->getAddressSpace(); 3612 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) { 3613 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI); 3614 if (NewPtr) { 3615 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end()); 3616 3617 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr; 3618 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0); 3619 } 3620 } 3621 break; 3622 } 3623 case ISD::AND: 3624 return performAndCombine(N, DCI); 3625 case ISD::OR: 3626 return performOrCombine(N, DCI); 3627 case ISD::XOR: 3628 return performXorCombine(N, DCI); 3629 case AMDGPUISD::FP_CLASS: 3630 return performClassCombine(N, DCI); 3631 case ISD::FCANONICALIZE: 3632 return performFCanonicalizeCombine(N, DCI); 3633 case AMDGPUISD::FRACT: 3634 case AMDGPUISD::RCP: 3635 case AMDGPUISD::RSQ: 3636 case AMDGPUISD::RCP_LEGACY: 3637 case AMDGPUISD::RSQ_LEGACY: 3638 case AMDGPUISD::RSQ_CLAMP: 3639 case AMDGPUISD::LDEXP: { 3640 SDValue Src = N->getOperand(0); 3641 if (Src.isUndef()) 3642 return Src; 3643 break; 3644 } 3645 } 3646 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); 3647 } 3648 3649 /// \brief Helper function for adjustWritemask 3650 static unsigned SubIdx2Lane(unsigned Idx) { 3651 switch (Idx) { 3652 default: return 0; 3653 case AMDGPU::sub0: return 0; 3654 case AMDGPU::sub1: return 1; 3655 case AMDGPU::sub2: return 2; 3656 case AMDGPU::sub3: return 3; 3657 } 3658 } 3659 3660 /// \brief Adjust the writemask of MIMG instructions 3661 void SITargetLowering::adjustWritemask(MachineSDNode *&Node, 3662 SelectionDAG &DAG) const { 3663 SDNode *Users[4] = { }; 3664 unsigned Lane = 0; 3665 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3; 3666 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx); 3667 unsigned NewDmask = 0; 3668 3669 // Try to figure out the used register components 3670 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end(); 3671 I != E; ++I) { 3672 3673 // Abort if we can't understand the usage 3674 if (!I->isMachineOpcode() || 3675 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG) 3676 return; 3677 3678 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used. 3679 // Note that subregs are packed, i.e. Lane==0 is the first bit set 3680 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit 3681 // set, etc. 3682 Lane = SubIdx2Lane(I->getConstantOperandVal(1)); 3683 3684 // Set which texture component corresponds to the lane. 3685 unsigned Comp; 3686 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) { 3687 assert(Dmask); 3688 Comp = countTrailingZeros(Dmask); 3689 Dmask &= ~(1 << Comp); 3690 } 3691 3692 // Abort if we have more than one user per component 3693 if (Users[Lane]) 3694 return; 3695 3696 Users[Lane] = *I; 3697 NewDmask |= 1 << Comp; 3698 } 3699 3700 // Abort if there's no change 3701 if (NewDmask == OldDmask) 3702 return; 3703 3704 // Adjust the writemask in the node 3705 std::vector<SDValue> Ops; 3706 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx); 3707 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32)); 3708 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end()); 3709 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops); 3710 3711 // If we only got one lane, replace it with a copy 3712 // (if NewDmask has only one bit set...) 3713 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) { 3714 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(), 3715 MVT::i32); 3716 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, 3717 SDLoc(), Users[Lane]->getValueType(0), 3718 SDValue(Node, 0), RC); 3719 DAG.ReplaceAllUsesWith(Users[Lane], Copy); 3720 return; 3721 } 3722 3723 // Update the users of the node with the new indices 3724 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) { 3725 3726 SDNode *User = Users[i]; 3727 if (!User) 3728 continue; 3729 3730 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32); 3731 DAG.UpdateNodeOperands(User, User->getOperand(0), Op); 3732 3733 switch (Idx) { 3734 default: break; 3735 case AMDGPU::sub0: Idx = AMDGPU::sub1; break; 3736 case AMDGPU::sub1: Idx = AMDGPU::sub2; break; 3737 case AMDGPU::sub2: Idx = AMDGPU::sub3; break; 3738 } 3739 } 3740 } 3741 3742 static bool isFrameIndexOp(SDValue Op) { 3743 if (Op.getOpcode() == ISD::AssertZext) 3744 Op = Op.getOperand(0); 3745 3746 return isa<FrameIndexSDNode>(Op); 3747 } 3748 3749 /// \brief Legalize target independent instructions (e.g. INSERT_SUBREG) 3750 /// with frame index operands. 3751 /// LLVM assumes that inputs are to these instructions are registers. 3752 void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node, 3753 SelectionDAG &DAG) const { 3754 3755 SmallVector<SDValue, 8> Ops; 3756 for (unsigned i = 0; i < Node->getNumOperands(); ++i) { 3757 if (!isFrameIndexOp(Node->getOperand(i))) { 3758 Ops.push_back(Node->getOperand(i)); 3759 continue; 3760 } 3761 3762 SDLoc DL(Node); 3763 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, 3764 Node->getOperand(i).getValueType(), 3765 Node->getOperand(i)), 0)); 3766 } 3767 3768 DAG.UpdateNodeOperands(Node, Ops); 3769 } 3770 3771 /// \brief Fold the instructions after selecting them. 3772 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node, 3773 SelectionDAG &DAG) const { 3774 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3775 unsigned Opcode = Node->getMachineOpcode(); 3776 3777 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() && 3778 !TII->isGather4(Opcode)) 3779 adjustWritemask(Node, DAG); 3780 3781 if (Opcode == AMDGPU::INSERT_SUBREG || 3782 Opcode == AMDGPU::REG_SEQUENCE) { 3783 legalizeTargetIndependentNode(Node, DAG); 3784 return Node; 3785 } 3786 return Node; 3787 } 3788 3789 /// \brief Assign the register class depending on the number of 3790 /// bits set in the writemask 3791 void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI, 3792 SDNode *Node) const { 3793 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3794 3795 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 3796 3797 if (TII->isVOP3(MI.getOpcode())) { 3798 // Make sure constant bus requirements are respected. 3799 TII->legalizeOperandsVOP3(MRI, MI); 3800 return; 3801 } 3802 3803 if (TII->isMIMG(MI)) { 3804 unsigned VReg = MI.getOperand(0).getReg(); 3805 unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4; 3806 unsigned Writemask = MI.getOperand(DmaskIdx).getImm(); 3807 unsigned BitsSet = 0; 3808 for (unsigned i = 0; i < 4; ++i) 3809 BitsSet += Writemask & (1 << i) ? 1 : 0; 3810 3811 const TargetRegisterClass *RC; 3812 switch (BitsSet) { 3813 default: return; 3814 case 1: RC = &AMDGPU::VGPR_32RegClass; break; 3815 case 2: RC = &AMDGPU::VReg_64RegClass; break; 3816 case 3: RC = &AMDGPU::VReg_96RegClass; break; 3817 } 3818 3819 unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet); 3820 MI.setDesc(TII->get(NewOpcode)); 3821 MRI.setRegClass(VReg, RC); 3822 return; 3823 } 3824 3825 // Replace unused atomics with the no return version. 3826 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode()); 3827 if (NoRetAtomicOp != -1) { 3828 if (!Node->hasAnyUseOfValue(0)) { 3829 MI.setDesc(TII->get(NoRetAtomicOp)); 3830 MI.RemoveOperand(0); 3831 return; 3832 } 3833 3834 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg 3835 // instruction, because the return type of these instructions is a vec2 of 3836 // the memory type, so it can be tied to the input operand. 3837 // This means these instructions always have a use, so we need to add a 3838 // special case to check if the atomic has only one extract_subreg use, 3839 // which itself has no uses. 3840 if ((Node->hasNUsesOfValue(1, 0) && 3841 Node->use_begin()->isMachineOpcode() && 3842 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG && 3843 !Node->use_begin()->hasAnyUseOfValue(0))) { 3844 unsigned Def = MI.getOperand(0).getReg(); 3845 3846 // Change this into a noret atomic. 3847 MI.setDesc(TII->get(NoRetAtomicOp)); 3848 MI.RemoveOperand(0); 3849 3850 // If we only remove the def operand from the atomic instruction, the 3851 // extract_subreg will be left with a use of a vreg without a def. 3852 // So we need to insert an implicit_def to avoid machine verifier 3853 // errors. 3854 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), 3855 TII->get(AMDGPU::IMPLICIT_DEF), Def); 3856 } 3857 return; 3858 } 3859 } 3860 3861 static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL, 3862 uint64_t Val) { 3863 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32); 3864 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0); 3865 } 3866 3867 MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG, 3868 const SDLoc &DL, 3869 SDValue Ptr) const { 3870 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3871 3872 // Build the half of the subregister with the constants before building the 3873 // full 128-bit register. If we are building multiple resource descriptors, 3874 // this will allow CSEing of the 2-component register. 3875 const SDValue Ops0[] = { 3876 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32), 3877 buildSMovImm32(DAG, DL, 0), 3878 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 3879 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32), 3880 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32) 3881 }; 3882 3883 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, 3884 MVT::v2i32, Ops0), 0); 3885 3886 // Combine the constants and the pointer. 3887 const SDValue Ops1[] = { 3888 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), 3889 Ptr, 3890 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32), 3891 SubRegHi, 3892 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32) 3893 }; 3894 3895 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1); 3896 } 3897 3898 /// \brief Return a resource descriptor with the 'Add TID' bit enabled 3899 /// The TID (Thread ID) is multiplied by the stride value (bits [61:48] 3900 /// of the resource descriptor) to create an offset, which is added to 3901 /// the resource pointer. 3902 MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL, 3903 SDValue Ptr, uint32_t RsrcDword1, 3904 uint64_t RsrcDword2And3) const { 3905 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr); 3906 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr); 3907 if (RsrcDword1) { 3908 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi, 3909 DAG.getConstant(RsrcDword1, DL, MVT::i32)), 3910 0); 3911 } 3912 3913 SDValue DataLo = buildSMovImm32(DAG, DL, 3914 RsrcDword2And3 & UINT64_C(0xFFFFFFFF)); 3915 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32); 3916 3917 const SDValue Ops[] = { 3918 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), 3919 PtrLo, 3920 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 3921 PtrHi, 3922 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32), 3923 DataLo, 3924 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32), 3925 DataHi, 3926 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32) 3927 }; 3928 3929 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops); 3930 } 3931 3932 SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG, 3933 const TargetRegisterClass *RC, 3934 unsigned Reg, EVT VT) const { 3935 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT); 3936 3937 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()), 3938 cast<RegisterSDNode>(VReg)->getReg(), VT); 3939 } 3940 3941 //===----------------------------------------------------------------------===// 3942 // SI Inline Assembly Support 3943 //===----------------------------------------------------------------------===// 3944 3945 std::pair<unsigned, const TargetRegisterClass *> 3946 SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 3947 StringRef Constraint, 3948 MVT VT) const { 3949 3950 if (Constraint.size() == 1) { 3951 switch (Constraint[0]) { 3952 case 's': 3953 case 'r': 3954 switch (VT.getSizeInBits()) { 3955 default: 3956 return std::make_pair(0U, nullptr); 3957 case 32: 3958 return std::make_pair(0U, &AMDGPU::SReg_32RegClass); 3959 case 64: 3960 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass); 3961 case 128: 3962 return std::make_pair(0U, &AMDGPU::SReg_128RegClass); 3963 case 256: 3964 return std::make_pair(0U, &AMDGPU::SReg_256RegClass); 3965 } 3966 3967 case 'v': 3968 switch (VT.getSizeInBits()) { 3969 default: 3970 return std::make_pair(0U, nullptr); 3971 case 32: 3972 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass); 3973 case 64: 3974 return std::make_pair(0U, &AMDGPU::VReg_64RegClass); 3975 case 96: 3976 return std::make_pair(0U, &AMDGPU::VReg_96RegClass); 3977 case 128: 3978 return std::make_pair(0U, &AMDGPU::VReg_128RegClass); 3979 case 256: 3980 return std::make_pair(0U, &AMDGPU::VReg_256RegClass); 3981 case 512: 3982 return std::make_pair(0U, &AMDGPU::VReg_512RegClass); 3983 } 3984 } 3985 } 3986 3987 if (Constraint.size() > 1) { 3988 const TargetRegisterClass *RC = nullptr; 3989 if (Constraint[1] == 'v') { 3990 RC = &AMDGPU::VGPR_32RegClass; 3991 } else if (Constraint[1] == 's') { 3992 RC = &AMDGPU::SGPR_32RegClass; 3993 } 3994 3995 if (RC) { 3996 uint32_t Idx; 3997 bool Failed = Constraint.substr(2).getAsInteger(10, Idx); 3998 if (!Failed && Idx < RC->getNumRegs()) 3999 return std::make_pair(RC->getRegister(Idx), RC); 4000 } 4001 } 4002 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 4003 } 4004 4005 SITargetLowering::ConstraintType 4006 SITargetLowering::getConstraintType(StringRef Constraint) const { 4007 if (Constraint.size() == 1) { 4008 switch (Constraint[0]) { 4009 default: break; 4010 case 's': 4011 case 'v': 4012 return C_RegisterClass; 4013 } 4014 } 4015 return TargetLowering::getConstraintType(Constraint); 4016 } 4017