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 "SIISelLowering.h" 22 #include "AMDGPU.h" 23 #include "AMDGPUDiagnosticInfoUnsupported.h" 24 #include "AMDGPUIntrinsicInfo.h" 25 #include "AMDGPUSubtarget.h" 26 #include "SIInstrInfo.h" 27 #include "SIMachineFunctionInfo.h" 28 #include "SIRegisterInfo.h" 29 #include "llvm/ADT/BitVector.h" 30 #include "llvm/ADT/StringSwitch.h" 31 #include "llvm/CodeGen/CallingConvLower.h" 32 #include "llvm/CodeGen/MachineInstrBuilder.h" 33 #include "llvm/CodeGen/MachineRegisterInfo.h" 34 #include "llvm/CodeGen/SelectionDAG.h" 35 #include "llvm/IR/Function.h" 36 #include "llvm/ADT/SmallString.h" 37 38 using namespace llvm; 39 40 SITargetLowering::SITargetLowering(TargetMachine &TM, 41 const AMDGPUSubtarget &STI) 42 : AMDGPUTargetLowering(TM, STI) { 43 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass); 44 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass); 45 46 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass); 47 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass); 48 49 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass); 50 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass); 51 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass); 52 53 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass); 54 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass); 55 56 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass); 57 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass); 58 59 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass); 60 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass); 61 62 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass); 63 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass); 64 65 computeRegisterProperties(STI.getRegisterInfo()); 66 67 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand); 68 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand); 69 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand); 70 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand); 71 72 setOperationAction(ISD::ADD, MVT::i32, Legal); 73 setOperationAction(ISD::ADDC, MVT::i32, Legal); 74 setOperationAction(ISD::ADDE, MVT::i32, Legal); 75 setOperationAction(ISD::SUBC, MVT::i32, Legal); 76 setOperationAction(ISD::SUBE, MVT::i32, Legal); 77 78 setOperationAction(ISD::FSIN, MVT::f32, Custom); 79 setOperationAction(ISD::FCOS, MVT::f32, Custom); 80 81 setOperationAction(ISD::FMINNUM, MVT::f64, Legal); 82 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal); 83 84 // We need to custom lower vector stores from local memory 85 setOperationAction(ISD::LOAD, MVT::v4i32, Custom); 86 setOperationAction(ISD::LOAD, MVT::v8i32, Custom); 87 setOperationAction(ISD::LOAD, MVT::v16i32, Custom); 88 89 setOperationAction(ISD::STORE, MVT::v8i32, Custom); 90 setOperationAction(ISD::STORE, MVT::v16i32, Custom); 91 92 setOperationAction(ISD::STORE, MVT::i1, Custom); 93 setOperationAction(ISD::STORE, MVT::v4i32, Custom); 94 95 setOperationAction(ISD::SELECT, MVT::i64, Custom); 96 setOperationAction(ISD::SELECT, MVT::f64, Promote); 97 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64); 98 99 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); 100 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand); 101 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); 102 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); 103 104 setOperationAction(ISD::SETCC, MVT::i1, Promote); 105 setOperationAction(ISD::SETCC, MVT::v2i1, Expand); 106 setOperationAction(ISD::SETCC, MVT::v4i1, Expand); 107 108 setOperationAction(ISD::BSWAP, MVT::i32, Legal); 109 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal); 110 111 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal); 112 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom); 113 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom); 114 115 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal); 116 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom); 117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom); 118 119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal); 120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom); 121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom); 122 123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal); 124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom); 125 126 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 127 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom); 128 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom); 129 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom); 130 131 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom); 132 setOperationAction(ISD::BRCOND, MVT::Other, Custom); 133 134 for (MVT VT : MVT::integer_valuetypes()) { 135 if (VT == MVT::i64) 136 continue; 137 138 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 139 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal); 140 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal); 141 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand); 142 143 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); 144 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal); 145 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal); 146 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand); 147 148 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote); 149 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal); 150 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal); 151 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand); 152 } 153 154 for (MVT VT : MVT::integer_vector_valuetypes()) { 155 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand); 156 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand); 157 } 158 159 for (MVT VT : MVT::fp_valuetypes()) 160 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand); 161 162 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand); 163 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand); 164 165 setTruncStoreAction(MVT::i64, MVT::i32, Expand); 166 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand); 167 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand); 168 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand); 169 170 171 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand); 172 173 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand); 174 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand); 175 176 setOperationAction(ISD::LOAD, MVT::i1, Custom); 177 178 setOperationAction(ISD::LOAD, MVT::v2i64, Promote); 179 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32); 180 181 setOperationAction(ISD::STORE, MVT::v2i64, Promote); 182 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32); 183 184 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand); 185 186 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 187 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); 188 setOperationAction(ISD::FrameIndex, MVT::i32, Custom); 189 190 // These should use UDIVREM, so set them to expand 191 setOperationAction(ISD::UDIV, MVT::i64, Expand); 192 setOperationAction(ISD::UREM, MVT::i64, Expand); 193 194 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand); 195 setOperationAction(ISD::SELECT, MVT::i1, Promote); 196 197 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand); 198 199 200 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand); 201 202 // We only support LOAD/STORE and vector manipulation ops for vectors 203 // with > 4 elements. 204 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) { 205 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) { 206 switch(Op) { 207 case ISD::LOAD: 208 case ISD::STORE: 209 case ISD::BUILD_VECTOR: 210 case ISD::BITCAST: 211 case ISD::EXTRACT_VECTOR_ELT: 212 case ISD::INSERT_VECTOR_ELT: 213 case ISD::INSERT_SUBVECTOR: 214 case ISD::EXTRACT_SUBVECTOR: 215 case ISD::SCALAR_TO_VECTOR: 216 break; 217 case ISD::CONCAT_VECTORS: 218 setOperationAction(Op, VT, Custom); 219 break; 220 default: 221 setOperationAction(Op, VT, Expand); 222 break; 223 } 224 } 225 } 226 227 // Most operations are naturally 32-bit vector operations. We only support 228 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32. 229 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) { 230 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote); 231 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32); 232 233 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote); 234 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32); 235 236 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote); 237 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32); 238 239 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote); 240 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32); 241 } 242 243 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) { 244 setOperationAction(ISD::FTRUNC, MVT::f64, Legal); 245 setOperationAction(ISD::FCEIL, MVT::f64, Legal); 246 setOperationAction(ISD::FRINT, MVT::f64, Legal); 247 } 248 249 setOperationAction(ISD::FFLOOR, MVT::f64, Legal); 250 setOperationAction(ISD::FDIV, MVT::f32, Custom); 251 setOperationAction(ISD::FDIV, MVT::f64, Custom); 252 253 setTargetDAGCombine(ISD::FADD); 254 setTargetDAGCombine(ISD::FSUB); 255 setTargetDAGCombine(ISD::FMINNUM); 256 setTargetDAGCombine(ISD::FMAXNUM); 257 setTargetDAGCombine(ISD::SMIN); 258 setTargetDAGCombine(ISD::SMAX); 259 setTargetDAGCombine(ISD::UMIN); 260 setTargetDAGCombine(ISD::UMAX); 261 setTargetDAGCombine(ISD::SETCC); 262 setTargetDAGCombine(ISD::AND); 263 setTargetDAGCombine(ISD::OR); 264 setTargetDAGCombine(ISD::UINT_TO_FP); 265 266 // All memory operations. Some folding on the pointer operand is done to help 267 // matching the constant offsets in the addressing modes. 268 setTargetDAGCombine(ISD::LOAD); 269 setTargetDAGCombine(ISD::STORE); 270 setTargetDAGCombine(ISD::ATOMIC_LOAD); 271 setTargetDAGCombine(ISD::ATOMIC_STORE); 272 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP); 273 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS); 274 setTargetDAGCombine(ISD::ATOMIC_SWAP); 275 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD); 276 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB); 277 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND); 278 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR); 279 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR); 280 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND); 281 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN); 282 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX); 283 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN); 284 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX); 285 286 setSchedulingPreference(Sched::RegPressure); 287 } 288 289 //===----------------------------------------------------------------------===// 290 // TargetLowering queries 291 //===----------------------------------------------------------------------===// 292 293 bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &, 294 EVT) const { 295 // SI has some legal vector types, but no legal vector operations. Say no 296 // shuffles are legal in order to prefer scalarizing some vector operations. 297 return false; 298 } 299 300 bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const { 301 // Flat instructions do not have offsets, and only have the register 302 // address. 303 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1); 304 } 305 306 bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const { 307 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and 308 // additionally can do r + r + i with addr64. 32-bit has more addressing 309 // mode options. Depending on the resource constant, it can also do 310 // (i64 r0) + (i32 r1) * (i14 i). 311 // 312 // Private arrays end up using a scratch buffer most of the time, so also 313 // assume those use MUBUF instructions. Scratch loads / stores are currently 314 // implemented as mubuf instructions with offen bit set, so slightly 315 // different than the normal addr64. 316 if (!isUInt<12>(AM.BaseOffs)) 317 return false; 318 319 // FIXME: Since we can split immediate into soffset and immediate offset, 320 // would it make sense to allow any immediate? 321 322 switch (AM.Scale) { 323 case 0: // r + i or just i, depending on HasBaseReg. 324 return true; 325 case 1: 326 return true; // We have r + r or r + i. 327 case 2: 328 if (AM.HasBaseReg) { 329 // Reject 2 * r + r. 330 return false; 331 } 332 333 // Allow 2 * r as r + r 334 // Or 2 * r + i is allowed as r + r + i. 335 return true; 336 default: // Don't allow n * r 337 return false; 338 } 339 } 340 341 bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL, 342 const AddrMode &AM, Type *Ty, 343 unsigned AS) const { 344 // No global is ever allowed as a base. 345 if (AM.BaseGV) 346 return false; 347 348 switch (AS) { 349 case AMDGPUAS::GLOBAL_ADDRESS: { 350 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 351 // Assume the we will use FLAT for all global memory accesses 352 // on VI. 353 // FIXME: This assumption is currently wrong. On VI we still use 354 // MUBUF instructions for the r + i addressing mode. As currently 355 // implemented, the MUBUF instructions only work on buffer < 4GB. 356 // It may be possible to support > 4GB buffers with MUBUF instructions, 357 // by setting the stride value in the resource descriptor which would 358 // increase the size limit to (stride * 4GB). However, this is risky, 359 // because it has never been validated. 360 return isLegalFlatAddressingMode(AM); 361 } 362 363 return isLegalMUBUFAddressingMode(AM); 364 } 365 case AMDGPUAS::CONSTANT_ADDRESS: { 366 // If the offset isn't a multiple of 4, it probably isn't going to be 367 // correctly aligned. 368 if (AM.BaseOffs % 4 != 0) 369 return isLegalMUBUFAddressingMode(AM); 370 371 // There are no SMRD extloads, so if we have to do a small type access we 372 // will use a MUBUF load. 373 // FIXME?: We also need to do this if unaligned, but we don't know the 374 // alignment here. 375 if (DL.getTypeStoreSize(Ty) < 4) 376 return isLegalMUBUFAddressingMode(AM); 377 378 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) { 379 // SMRD instructions have an 8-bit, dword offset on SI. 380 if (!isUInt<8>(AM.BaseOffs / 4)) 381 return false; 382 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) { 383 // On CI+, this can also be a 32-bit literal constant offset. If it fits 384 // in 8-bits, it can use a smaller encoding. 385 if (!isUInt<32>(AM.BaseOffs / 4)) 386 return false; 387 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) { 388 // On VI, these use the SMEM format and the offset is 20-bit in bytes. 389 if (!isUInt<20>(AM.BaseOffs)) 390 return false; 391 } else 392 llvm_unreachable("unhandled generation"); 393 394 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg. 395 return true; 396 397 if (AM.Scale == 1 && AM.HasBaseReg) 398 return true; 399 400 return false; 401 } 402 403 case AMDGPUAS::PRIVATE_ADDRESS: 404 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE: 405 return isLegalMUBUFAddressingMode(AM); 406 407 case AMDGPUAS::LOCAL_ADDRESS: 408 case AMDGPUAS::REGION_ADDRESS: { 409 // Basic, single offset DS instructions allow a 16-bit unsigned immediate 410 // field. 411 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have 412 // an 8-bit dword offset but we don't know the alignment here. 413 if (!isUInt<16>(AM.BaseOffs)) 414 return false; 415 416 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg. 417 return true; 418 419 if (AM.Scale == 1 && AM.HasBaseReg) 420 return true; 421 422 return false; 423 } 424 case AMDGPUAS::FLAT_ADDRESS: 425 return isLegalFlatAddressingMode(AM); 426 427 default: 428 llvm_unreachable("unhandled address space"); 429 } 430 } 431 432 bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT, 433 unsigned AddrSpace, 434 unsigned Align, 435 bool *IsFast) const { 436 if (IsFast) 437 *IsFast = false; 438 439 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96, 440 // which isn't a simple VT. 441 if (!VT.isSimple() || VT == MVT::Other) 442 return false; 443 444 // TODO - CI+ supports unaligned memory accesses, but this requires driver 445 // support. 446 447 // XXX - The only mention I see of this in the ISA manual is for LDS direct 448 // reads the "byte address and must be dword aligned". Is it also true for the 449 // normal loads and stores? 450 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) { 451 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte 452 // aligned, 8 byte access in a single operation using ds_read2/write2_b32 453 // with adjacent offsets. 454 bool AlignedBy4 = (Align % 4 == 0); 455 if (IsFast) 456 *IsFast = AlignedBy4; 457 return AlignedBy4; 458 } 459 460 // Smaller than dword value must be aligned. 461 // FIXME: This should be allowed on CI+ 462 if (VT.bitsLT(MVT::i32)) 463 return false; 464 465 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the 466 // byte-address are ignored, thus forcing Dword alignment. 467 // This applies to private, global, and constant memory. 468 if (IsFast) 469 *IsFast = true; 470 471 return VT.bitsGT(MVT::i32) && Align % 4 == 0; 472 } 473 474 EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign, 475 unsigned SrcAlign, bool IsMemset, 476 bool ZeroMemset, 477 bool MemcpyStrSrc, 478 MachineFunction &MF) const { 479 // FIXME: Should account for address space here. 480 481 // The default fallback uses the private pointer size as a guess for a type to 482 // use. Make sure we switch these to 64-bit accesses. 483 484 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global 485 return MVT::v4i32; 486 487 if (Size >= 8 && DstAlign >= 4) 488 return MVT::v2i32; 489 490 // Use the default. 491 return MVT::Other; 492 } 493 494 static bool isFlatGlobalAddrSpace(unsigned AS) { 495 return AS == AMDGPUAS::GLOBAL_ADDRESS || 496 AS == AMDGPUAS::FLAT_ADDRESS || 497 AS == AMDGPUAS::CONSTANT_ADDRESS; 498 } 499 500 bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS, 501 unsigned DestAS) const { 502 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS); 503 } 504 505 506 bool SITargetLowering::isMemOpUniform(const SDNode *N) const { 507 const MemSDNode *MemNode = cast<MemSDNode>(N); 508 const Value *Ptr = MemNode->getMemOperand()->getValue(); 509 510 // UndefValue means this is a load of a kernel input. These are uniform. 511 // Sometimes LDS instructions have constant pointers 512 if (isa<UndefValue>(Ptr) || isa<Argument>(Ptr) || isa<Constant>(Ptr) || 513 isa<GlobalValue>(Ptr)) 514 return true; 515 516 const Instruction *I = dyn_cast_or_null<Instruction>(Ptr); 517 return I && I->getMetadata("amdgpu.uniform"); 518 } 519 520 TargetLoweringBase::LegalizeTypeAction 521 SITargetLowering::getPreferredVectorAction(EVT VT) const { 522 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16)) 523 return TypeSplitVector; 524 525 return TargetLoweringBase::getPreferredVectorAction(VT); 526 } 527 528 bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, 529 Type *Ty) const { 530 const SIInstrInfo *TII = 531 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); 532 return TII->isInlineConstant(Imm); 533 } 534 535 bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const { 536 537 // SimplifySetCC uses this function to determine whether or not it should 538 // create setcc with i1 operands. We don't have instructions for i1 setcc. 539 if (VT == MVT::i1 && Op == ISD::SETCC) 540 return false; 541 542 return TargetLowering::isTypeDesirableForOp(Op, VT); 543 } 544 545 SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT, 546 SDLoc SL, SDValue Chain, 547 unsigned Offset, bool Signed) const { 548 const DataLayout &DL = DAG.getDataLayout(); 549 MachineFunction &MF = DAG.getMachineFunction(); 550 const SIRegisterInfo *TRI = 551 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo()); 552 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR); 553 554 Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 555 556 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); 557 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS); 558 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS); 559 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL, 560 MRI.getLiveInVirtReg(InputPtrReg), PtrVT); 561 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr, 562 DAG.getConstant(Offset, SL, PtrVT)); 563 SDValue PtrOffset = DAG.getUNDEF(PtrVT); 564 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); 565 566 unsigned Align = DL.getABITypeAlignment(Ty); 567 568 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD; 569 if (MemVT.isFloatingPoint()) 570 ExtTy = ISD::EXTLOAD; 571 572 return DAG.getLoad(ISD::UNINDEXED, ExtTy, 573 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT, 574 false, // isVolatile 575 true, // isNonTemporal 576 true, // isInvariant 577 Align); // Alignment 578 } 579 580 SDValue SITargetLowering::LowerFormalArguments( 581 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 582 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG, 583 SmallVectorImpl<SDValue> &InVals) const { 584 const SIRegisterInfo *TRI = 585 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo()); 586 587 MachineFunction &MF = DAG.getMachineFunction(); 588 FunctionType *FType = MF.getFunction()->getFunctionType(); 589 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 590 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>(); 591 592 if (Subtarget->isAmdHsaOS() && Info->getShaderType() != ShaderType::COMPUTE) { 593 const Function *Fn = MF.getFunction(); 594 DiagnosticInfoUnsupported NoGraphicsHSA(*Fn, "non-compute shaders with HSA"); 595 DAG.getContext()->diagnose(NoGraphicsHSA); 596 return SDValue(); 597 } 598 599 // FIXME: We currently assume all calling conventions are kernels. 600 601 SmallVector<ISD::InputArg, 16> Splits; 602 BitVector Skipped(Ins.size()); 603 604 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) { 605 const ISD::InputArg &Arg = Ins[i]; 606 607 // First check if it's a PS input addr 608 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() && 609 !Arg.Flags.isByVal() && PSInputNum <= 15) { 610 611 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) { 612 // We can safely skip PS inputs 613 Skipped.set(i); 614 ++PSInputNum; 615 continue; 616 } 617 618 Info->markPSInputAllocated(PSInputNum); 619 if (Arg.Used) 620 Info->PSInputEna |= 1 << PSInputNum; 621 622 ++PSInputNum; 623 } 624 625 // Second split vertices into their elements 626 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) { 627 ISD::InputArg NewArg = Arg; 628 NewArg.Flags.setSplit(); 629 NewArg.VT = Arg.VT.getVectorElementType(); 630 631 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a 632 // three or five element vertex only needs three or five registers, 633 // NOT four or eight. 634 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex()); 635 unsigned NumElements = ParamType->getVectorNumElements(); 636 637 for (unsigned j = 0; j != NumElements; ++j) { 638 Splits.push_back(NewArg); 639 NewArg.PartOffset += NewArg.VT.getStoreSize(); 640 } 641 642 } else if (Info->getShaderType() != ShaderType::COMPUTE) { 643 Splits.push_back(Arg); 644 } 645 } 646 647 SmallVector<CCValAssign, 16> ArgLocs; 648 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 649 *DAG.getContext()); 650 651 // At least one interpolation mode must be enabled or else the GPU will hang. 652 // 653 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set 654 // PSInputAddr, the user wants to enable some bits after the compilation 655 // based on run-time states. Since we can't know what the final PSInputEna 656 // will look like, so we shouldn't do anything here and the user should take 657 // responsibility for the correct programming. 658 // 659 // Otherwise, the following restrictions apply: 660 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled. 661 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be 662 // enabled too. 663 if (Info->getShaderType() == ShaderType::PIXEL && 664 ((Info->getPSInputAddr() & 0x7F) == 0 || 665 ((Info->getPSInputAddr() & 0xF) == 0 && 666 Info->isPSInputAllocated(11)))) { 667 CCInfo.AllocateReg(AMDGPU::VGPR0); 668 CCInfo.AllocateReg(AMDGPU::VGPR1); 669 Info->markPSInputAllocated(0); 670 Info->PSInputEna |= 1; 671 } 672 673 if (Info->getShaderType() == ShaderType::COMPUTE) { 674 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins, 675 Splits); 676 } 677 678 // FIXME: How should these inputs interact with inreg / custom SGPR inputs? 679 if (Info->hasPrivateSegmentBuffer()) { 680 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI); 681 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass); 682 CCInfo.AllocateReg(PrivateSegmentBufferReg); 683 } 684 685 if (Info->hasDispatchPtr()) { 686 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI); 687 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass); 688 CCInfo.AllocateReg(DispatchPtrReg); 689 } 690 691 if (Info->hasKernargSegmentPtr()) { 692 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI); 693 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass); 694 CCInfo.AllocateReg(InputPtrReg); 695 } 696 697 AnalyzeFormalArguments(CCInfo, Splits); 698 699 SmallVector<SDValue, 16> Chains; 700 701 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) { 702 703 const ISD::InputArg &Arg = Ins[i]; 704 if (Skipped[i]) { 705 InVals.push_back(DAG.getUNDEF(Arg.VT)); 706 continue; 707 } 708 709 CCValAssign &VA = ArgLocs[ArgIdx++]; 710 MVT VT = VA.getLocVT(); 711 712 if (VA.isMemLoc()) { 713 VT = Ins[i].VT; 714 EVT MemVT = Splits[i].VT; 715 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() + 716 VA.getLocMemOffset(); 717 // The first 36 bytes of the input buffer contains information about 718 // thread group and global sizes. 719 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain, 720 Offset, Ins[i].Flags.isSExt()); 721 Chains.push_back(Arg.getValue(1)); 722 723 auto *ParamTy = 724 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex())); 725 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS && 726 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) { 727 // On SI local pointers are just offsets into LDS, so they are always 728 // less than 16-bits. On CI and newer they could potentially be 729 // real pointers, so we can't guarantee their size. 730 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg, 731 DAG.getValueType(MVT::i16)); 732 } 733 734 InVals.push_back(Arg); 735 Info->ABIArgOffset = Offset + MemVT.getStoreSize(); 736 continue; 737 } 738 assert(VA.isRegLoc() && "Parameter must be in a register!"); 739 740 unsigned Reg = VA.getLocReg(); 741 742 if (VT == MVT::i64) { 743 // For now assume it is a pointer 744 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0, 745 &AMDGPU::SReg_64RegClass); 746 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass); 747 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT); 748 InVals.push_back(Copy); 749 continue; 750 } 751 752 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT); 753 754 Reg = MF.addLiveIn(Reg, RC); 755 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT); 756 757 if (Arg.VT.isVector()) { 758 759 // Build a vector from the registers 760 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex()); 761 unsigned NumElements = ParamType->getVectorNumElements(); 762 763 SmallVector<SDValue, 4> Regs; 764 Regs.push_back(Val); 765 for (unsigned j = 1; j != NumElements; ++j) { 766 Reg = ArgLocs[ArgIdx++].getLocReg(); 767 Reg = MF.addLiveIn(Reg, RC); 768 769 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT); 770 Regs.push_back(Copy); 771 } 772 773 // Fill up the missing vector elements 774 NumElements = Arg.VT.getVectorNumElements() - NumElements; 775 Regs.append(NumElements, DAG.getUNDEF(VT)); 776 777 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs)); 778 continue; 779 } 780 781 InVals.push_back(Val); 782 } 783 784 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read 785 // these from the dispatch pointer. 786 787 // Start adding system SGPRs. 788 if (Info->hasWorkGroupIDX()) { 789 unsigned Reg = Info->addWorkGroupIDX(); 790 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); 791 CCInfo.AllocateReg(Reg); 792 } else 793 llvm_unreachable("work group id x is always enabled"); 794 795 if (Info->hasWorkGroupIDY()) { 796 unsigned Reg = Info->addWorkGroupIDY(); 797 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); 798 CCInfo.AllocateReg(Reg); 799 } 800 801 if (Info->hasWorkGroupIDZ()) { 802 unsigned Reg = Info->addWorkGroupIDZ(); 803 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); 804 CCInfo.AllocateReg(Reg); 805 } 806 807 if (Info->hasWorkGroupInfo()) { 808 unsigned Reg = Info->addWorkGroupInfo(); 809 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); 810 CCInfo.AllocateReg(Reg); 811 } 812 813 if (Info->hasPrivateSegmentWaveByteOffset()) { 814 // Scratch wave offset passed in system SGPR. 815 unsigned PrivateSegmentWaveByteOffsetReg 816 = Info->addPrivateSegmentWaveByteOffset(); 817 818 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass); 819 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg); 820 } 821 822 // Now that we've figured out where the scratch register inputs are, see if 823 // should reserve the arguments and use them directly. 824 825 bool HasStackObjects = MF.getFrameInfo()->hasStackObjects(); 826 827 if (ST.isAmdHsaOS()) { 828 // TODO: Assume we will spill without optimizations. 829 if (HasStackObjects) { 830 // If we have stack objects, we unquestionably need the private buffer 831 // resource. For the HSA ABI, this will be the first 4 user SGPR 832 // inputs. We can reserve those and use them directly. 833 834 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue( 835 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER); 836 Info->setScratchRSrcReg(PrivateSegmentBufferReg); 837 838 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue( 839 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); 840 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg); 841 } else { 842 unsigned ReservedBufferReg 843 = TRI->reservedPrivateSegmentBufferReg(MF); 844 unsigned ReservedOffsetReg 845 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); 846 847 // We tentatively reserve the last registers (skipping the last two 848 // which may contain VCC). After register allocation, we'll replace 849 // these with the ones immediately after those which were really 850 // allocated. In the prologue copies will be inserted from the argument 851 // to these reserved registers. 852 Info->setScratchRSrcReg(ReservedBufferReg); 853 Info->setScratchWaveOffsetReg(ReservedOffsetReg); 854 } 855 } else { 856 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF); 857 858 // Without HSA, relocations are used for the scratch pointer and the 859 // buffer resource setup is always inserted in the prologue. Scratch wave 860 // offset is still in an input SGPR. 861 Info->setScratchRSrcReg(ReservedBufferReg); 862 863 if (HasStackObjects) { 864 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue( 865 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); 866 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg); 867 } else { 868 unsigned ReservedOffsetReg 869 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); 870 Info->setScratchWaveOffsetReg(ReservedOffsetReg); 871 } 872 } 873 874 if (Info->hasWorkItemIDX()) { 875 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X); 876 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 877 CCInfo.AllocateReg(Reg); 878 } else 879 llvm_unreachable("workitem id x should always be enabled"); 880 881 if (Info->hasWorkItemIDY()) { 882 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y); 883 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 884 CCInfo.AllocateReg(Reg); 885 } 886 887 if (Info->hasWorkItemIDZ()) { 888 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z); 889 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 890 CCInfo.AllocateReg(Reg); 891 } 892 893 if (Chains.empty()) 894 return Chain; 895 896 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 897 } 898 899 SDValue SITargetLowering::LowerReturn(SDValue Chain, 900 CallingConv::ID CallConv, 901 bool isVarArg, 902 const SmallVectorImpl<ISD::OutputArg> &Outs, 903 const SmallVectorImpl<SDValue> &OutVals, 904 SDLoc DL, SelectionDAG &DAG) const { 905 MachineFunction &MF = DAG.getMachineFunction(); 906 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 907 908 if (Info->getShaderType() == ShaderType::COMPUTE) 909 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs, 910 OutVals, DL, DAG); 911 912 Info->setIfReturnsVoid(Outs.size() == 0); 913 914 SmallVector<ISD::OutputArg, 48> Splits; 915 SmallVector<SDValue, 48> SplitVals; 916 917 // Split vectors into their elements. 918 for (unsigned i = 0, e = Outs.size(); i != e; ++i) { 919 const ISD::OutputArg &Out = Outs[i]; 920 921 if (Out.VT.isVector()) { 922 MVT VT = Out.VT.getVectorElementType(); 923 ISD::OutputArg NewOut = Out; 924 NewOut.Flags.setSplit(); 925 NewOut.VT = VT; 926 927 // We want the original number of vector elements here, e.g. 928 // three or five, not four or eight. 929 unsigned NumElements = Out.ArgVT.getVectorNumElements(); 930 931 for (unsigned j = 0; j != NumElements; ++j) { 932 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i], 933 DAG.getConstant(j, DL, MVT::i32)); 934 SplitVals.push_back(Elem); 935 Splits.push_back(NewOut); 936 NewOut.PartOffset += NewOut.VT.getStoreSize(); 937 } 938 } else { 939 SplitVals.push_back(OutVals[i]); 940 Splits.push_back(Out); 941 } 942 } 943 944 // CCValAssign - represent the assignment of the return value to a location. 945 SmallVector<CCValAssign, 48> RVLocs; 946 947 // CCState - Info about the registers and stack slots. 948 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 949 *DAG.getContext()); 950 951 // Analyze outgoing return values. 952 AnalyzeReturn(CCInfo, Splits); 953 954 SDValue Flag; 955 SmallVector<SDValue, 48> RetOps; 956 RetOps.push_back(Chain); // Operand #0 = Chain (updated below) 957 958 // Copy the result values into the output registers. 959 for (unsigned i = 0, realRVLocIdx = 0; 960 i != RVLocs.size(); 961 ++i, ++realRVLocIdx) { 962 CCValAssign &VA = RVLocs[i]; 963 assert(VA.isRegLoc() && "Can only return in registers!"); 964 965 SDValue Arg = SplitVals[realRVLocIdx]; 966 967 // Copied from other backends. 968 switch (VA.getLocInfo()) { 969 default: llvm_unreachable("Unknown loc info!"); 970 case CCValAssign::Full: 971 break; 972 case CCValAssign::BCvt: 973 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg); 974 break; 975 } 976 977 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag); 978 Flag = Chain.getValue(1); 979 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 980 } 981 982 // Update chain and glue. 983 RetOps[0] = Chain; 984 if (Flag.getNode()) 985 RetOps.push_back(Flag); 986 987 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, RetOps); 988 } 989 990 unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT, 991 SelectionDAG &DAG) const { 992 unsigned Reg = StringSwitch<unsigned>(RegName) 993 .Case("m0", AMDGPU::M0) 994 .Case("exec", AMDGPU::EXEC) 995 .Case("exec_lo", AMDGPU::EXEC_LO) 996 .Case("exec_hi", AMDGPU::EXEC_HI) 997 .Case("flat_scratch", AMDGPU::FLAT_SCR) 998 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO) 999 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI) 1000 .Default(AMDGPU::NoRegister); 1001 1002 if (Reg == AMDGPU::NoRegister) { 1003 report_fatal_error(Twine("invalid register name \"" 1004 + StringRef(RegName) + "\".")); 1005 1006 } 1007 1008 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS && 1009 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) { 1010 report_fatal_error(Twine("invalid register \"" 1011 + StringRef(RegName) + "\" for subtarget.")); 1012 } 1013 1014 switch (Reg) { 1015 case AMDGPU::M0: 1016 case AMDGPU::EXEC_LO: 1017 case AMDGPU::EXEC_HI: 1018 case AMDGPU::FLAT_SCR_LO: 1019 case AMDGPU::FLAT_SCR_HI: 1020 if (VT.getSizeInBits() == 32) 1021 return Reg; 1022 break; 1023 case AMDGPU::EXEC: 1024 case AMDGPU::FLAT_SCR: 1025 if (VT.getSizeInBits() == 64) 1026 return Reg; 1027 break; 1028 default: 1029 llvm_unreachable("missing register type checking"); 1030 } 1031 1032 report_fatal_error(Twine("invalid type for register \"" 1033 + StringRef(RegName) + "\".")); 1034 } 1035 1036 MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter( 1037 MachineInstr * MI, MachineBasicBlock * BB) const { 1038 1039 switch (MI->getOpcode()) { 1040 default: 1041 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB); 1042 case AMDGPU::BRANCH: 1043 return BB; 1044 } 1045 return BB; 1046 } 1047 1048 bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const { 1049 // This currently forces unfolding various combinations of fsub into fma with 1050 // free fneg'd operands. As long as we have fast FMA (controlled by 1051 // isFMAFasterThanFMulAndFAdd), we should perform these. 1052 1053 // When fma is quarter rate, for f64 where add / sub are at best half rate, 1054 // most of these combines appear to be cycle neutral but save on instruction 1055 // count / code size. 1056 return true; 1057 } 1058 1059 EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx, 1060 EVT VT) const { 1061 if (!VT.isVector()) { 1062 return MVT::i1; 1063 } 1064 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements()); 1065 } 1066 1067 MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const { 1068 return MVT::i32; 1069 } 1070 1071 // Answering this is somewhat tricky and depends on the specific device which 1072 // have different rates for fma or all f64 operations. 1073 // 1074 // v_fma_f64 and v_mul_f64 always take the same number of cycles as each other 1075 // regardless of which device (although the number of cycles differs between 1076 // devices), so it is always profitable for f64. 1077 // 1078 // v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable 1079 // only on full rate devices. Normally, we should prefer selecting v_mad_f32 1080 // which we can always do even without fused FP ops since it returns the same 1081 // result as the separate operations and since it is always full 1082 // rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32 1083 // however does not support denormals, so we do report fma as faster if we have 1084 // a fast fma device and require denormals. 1085 // 1086 bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { 1087 VT = VT.getScalarType(); 1088 1089 if (!VT.isSimple()) 1090 return false; 1091 1092 switch (VT.getSimpleVT().SimpleTy) { 1093 case MVT::f32: 1094 // This is as fast on some subtargets. However, we always have full rate f32 1095 // mad available which returns the same result as the separate operations 1096 // which we should prefer over fma. We can't use this if we want to support 1097 // denormals, so only report this in these cases. 1098 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32(); 1099 case MVT::f64: 1100 return true; 1101 default: 1102 break; 1103 } 1104 1105 return false; 1106 } 1107 1108 //===----------------------------------------------------------------------===// 1109 // Custom DAG Lowering Operations 1110 //===----------------------------------------------------------------------===// 1111 1112 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 1113 switch (Op.getOpcode()) { 1114 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); 1115 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG); 1116 case ISD::BRCOND: return LowerBRCOND(Op, DAG); 1117 case ISD::LOAD: { 1118 SDValue Result = LowerLOAD(Op, DAG); 1119 assert((!Result.getNode() || 1120 Result.getNode()->getNumValues() == 2) && 1121 "Load should return a value and a chain"); 1122 return Result; 1123 } 1124 1125 case ISD::FSIN: 1126 case ISD::FCOS: 1127 return LowerTrig(Op, DAG); 1128 case ISD::SELECT: return LowerSELECT(Op, DAG); 1129 case ISD::FDIV: return LowerFDIV(Op, DAG); 1130 case ISD::STORE: return LowerSTORE(Op, DAG); 1131 case ISD::GlobalAddress: { 1132 MachineFunction &MF = DAG.getMachineFunction(); 1133 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 1134 return LowerGlobalAddress(MFI, Op, DAG); 1135 } 1136 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); 1137 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG); 1138 } 1139 return SDValue(); 1140 } 1141 1142 /// \brief Helper function for LowerBRCOND 1143 static SDNode *findUser(SDValue Value, unsigned Opcode) { 1144 1145 SDNode *Parent = Value.getNode(); 1146 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end(); 1147 I != E; ++I) { 1148 1149 if (I.getUse().get() != Value) 1150 continue; 1151 1152 if (I->getOpcode() == Opcode) 1153 return *I; 1154 } 1155 return nullptr; 1156 } 1157 1158 SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const { 1159 1160 SDLoc SL(Op); 1161 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op); 1162 unsigned FrameIndex = FINode->getIndex(); 1163 1164 // A FrameIndex node represents a 32-bit offset into scratch memory. If 1165 // the high bit of a frame index offset were to be set, this would mean 1166 // that it represented an offset of ~2GB * 64 = ~128GB from the start of the 1167 // scratch buffer, with 64 being the number of threads per wave. 1168 // 1169 // If we know the machine uses less than 128GB of scratch, then we can 1170 // amrk the high bit of the FrameIndex node as known zero, 1171 // which is important, because it means in most situations we can 1172 // prove that values derived from FrameIndex nodes are non-negative. 1173 // This enables us to take advantage of more addressing modes when 1174 // accessing scratch buffers, since for scratch reads/writes, the register 1175 // offset must always be positive. 1176 1177 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32); 1178 if (Subtarget->enableHugeScratchBuffer()) 1179 return TFI; 1180 1181 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI, 1182 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), 31))); 1183 } 1184 1185 /// This transforms the control flow intrinsics to get the branch destination as 1186 /// last parameter, also switches branch target with BR if the need arise 1187 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, 1188 SelectionDAG &DAG) const { 1189 1190 SDLoc DL(BRCOND); 1191 1192 SDNode *Intr = BRCOND.getOperand(1).getNode(); 1193 SDValue Target = BRCOND.getOperand(2); 1194 SDNode *BR = nullptr; 1195 1196 if (Intr->getOpcode() == ISD::SETCC) { 1197 // As long as we negate the condition everything is fine 1198 SDNode *SetCC = Intr; 1199 assert(SetCC->getConstantOperandVal(1) == 1); 1200 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() == 1201 ISD::SETNE); 1202 Intr = SetCC->getOperand(0).getNode(); 1203 1204 } else { 1205 // Get the target from BR if we don't negate the condition 1206 BR = findUser(BRCOND, ISD::BR); 1207 Target = BR->getOperand(1); 1208 } 1209 1210 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN); 1211 1212 // Build the result and 1213 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end()); 1214 1215 // operands of the new intrinsic call 1216 SmallVector<SDValue, 4> Ops; 1217 Ops.push_back(BRCOND.getOperand(0)); 1218 Ops.append(Intr->op_begin() + 1, Intr->op_end()); 1219 Ops.push_back(Target); 1220 1221 // build the new intrinsic call 1222 SDNode *Result = DAG.getNode( 1223 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL, 1224 DAG.getVTList(Res), Ops).getNode(); 1225 1226 if (BR) { 1227 // Give the branch instruction our target 1228 SDValue Ops[] = { 1229 BR->getOperand(0), 1230 BRCOND.getOperand(2) 1231 }; 1232 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops); 1233 DAG.ReplaceAllUsesWith(BR, NewBR.getNode()); 1234 BR = NewBR.getNode(); 1235 } 1236 1237 SDValue Chain = SDValue(Result, Result->getNumValues() - 1); 1238 1239 // Copy the intrinsic results to registers 1240 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) { 1241 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg); 1242 if (!CopyToReg) 1243 continue; 1244 1245 Chain = DAG.getCopyToReg( 1246 Chain, DL, 1247 CopyToReg->getOperand(1), 1248 SDValue(Result, i - 1), 1249 SDValue()); 1250 1251 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0)); 1252 } 1253 1254 // Remove the old intrinsic from the chain 1255 DAG.ReplaceAllUsesOfValueWith( 1256 SDValue(Intr, Intr->getNumValues() - 1), 1257 Intr->getOperand(0)); 1258 1259 return Chain; 1260 } 1261 1262 SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI, 1263 SDValue Op, 1264 SelectionDAG &DAG) const { 1265 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op); 1266 1267 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS) 1268 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG); 1269 1270 SDLoc DL(GSD); 1271 const GlobalValue *GV = GSD->getGlobal(); 1272 MVT PtrVT = getPointerTy(DAG.getDataLayout(), GSD->getAddressSpace()); 1273 1274 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32); 1275 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT, GA); 1276 } 1277 1278 SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL, 1279 SDValue V) const { 1280 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions, 1281 // so we will end up with redundant moves to m0. 1282 // 1283 // We can't use S_MOV_B32, because there is no way to specify m0 as the 1284 // destination register. 1285 // 1286 // We have to use them both. Machine cse will combine all the S_MOV_B32 1287 // instructions and the register coalescer eliminate the extra copies. 1288 SDNode *M0 = DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, V.getValueType(), V); 1289 return DAG.getCopyToReg(Chain, DL, DAG.getRegister(AMDGPU::M0, MVT::i32), 1290 SDValue(M0, 0), SDValue()); // Glue 1291 // A Null SDValue creates 1292 // a glue result. 1293 } 1294 1295 SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG, 1296 SDValue Op, 1297 MVT VT, 1298 unsigned Offset) const { 1299 SDLoc SL(Op); 1300 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL, 1301 DAG.getEntryNode(), Offset, false); 1302 // The local size values will have the hi 16-bits as zero. 1303 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param, 1304 DAG.getValueType(VT)); 1305 } 1306 1307 SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, 1308 SelectionDAG &DAG) const { 1309 MachineFunction &MF = DAG.getMachineFunction(); 1310 auto MFI = MF.getInfo<SIMachineFunctionInfo>(); 1311 const SIRegisterInfo *TRI = 1312 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo()); 1313 1314 EVT VT = Op.getValueType(); 1315 SDLoc DL(Op); 1316 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 1317 1318 // TODO: Should this propagate fast-math-flags? 1319 1320 switch (IntrinsicID) { 1321 case Intrinsic::amdgcn_dispatch_ptr: 1322 if (!Subtarget->isAmdHsaOS()) { 1323 DiagnosticInfoUnsupported BadIntrin(*MF.getFunction(), 1324 "hsa intrinsic without hsa target"); 1325 DAG.getContext()->diagnose(BadIntrin); 1326 return DAG.getUNDEF(VT); 1327 } 1328 1329 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, 1330 TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_PTR), VT); 1331 case Intrinsic::amdgcn_rcp: 1332 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1)); 1333 case Intrinsic::amdgcn_rsq: 1334 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name 1335 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); 1336 case Intrinsic::amdgcn_rsq_clamped: 1337 case AMDGPUIntrinsic::AMDGPU_rsq_clamped: { // Legacy name 1338 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) 1339 return DAG.getNode(AMDGPUISD::RSQ_CLAMPED, DL, VT, Op.getOperand(1)); 1340 1341 Type *Type = VT.getTypeForEVT(*DAG.getContext()); 1342 APFloat Max = APFloat::getLargest(Type->getFltSemantics()); 1343 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true); 1344 1345 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); 1346 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq, 1347 DAG.getConstantFP(Max, DL, VT)); 1348 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp, 1349 DAG.getConstantFP(Min, DL, VT)); 1350 } 1351 case Intrinsic::r600_read_ngroups_x: 1352 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 1353 SI::KernelInputOffsets::NGROUPS_X, false); 1354 case Intrinsic::r600_read_ngroups_y: 1355 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 1356 SI::KernelInputOffsets::NGROUPS_Y, false); 1357 case Intrinsic::r600_read_ngroups_z: 1358 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 1359 SI::KernelInputOffsets::NGROUPS_Z, false); 1360 case Intrinsic::r600_read_global_size_x: 1361 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 1362 SI::KernelInputOffsets::GLOBAL_SIZE_X, false); 1363 case Intrinsic::r600_read_global_size_y: 1364 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 1365 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false); 1366 case Intrinsic::r600_read_global_size_z: 1367 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 1368 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false); 1369 case Intrinsic::r600_read_local_size_x: 1370 return lowerImplicitZextParam(DAG, Op, MVT::i16, 1371 SI::KernelInputOffsets::LOCAL_SIZE_X); 1372 case Intrinsic::r600_read_local_size_y: 1373 return lowerImplicitZextParam(DAG, Op, MVT::i16, 1374 SI::KernelInputOffsets::LOCAL_SIZE_Y); 1375 case Intrinsic::r600_read_local_size_z: 1376 return lowerImplicitZextParam(DAG, Op, MVT::i16, 1377 SI::KernelInputOffsets::LOCAL_SIZE_Z); 1378 case Intrinsic::amdgcn_read_workdim: 1379 case AMDGPUIntrinsic::AMDGPU_read_workdim: // Legacy name. 1380 // Really only 2 bits. 1381 return lowerImplicitZextParam(DAG, Op, MVT::i8, 1382 getImplicitParameterOffset(MFI, GRID_DIM)); 1383 case Intrinsic::r600_read_tgid_x: 1384 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, 1385 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT); 1386 case Intrinsic::r600_read_tgid_y: 1387 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, 1388 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT); 1389 case Intrinsic::r600_read_tgid_z: 1390 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, 1391 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT); 1392 case Intrinsic::r600_read_tidig_x: 1393 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, 1394 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT); 1395 case Intrinsic::r600_read_tidig_y: 1396 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, 1397 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT); 1398 case Intrinsic::r600_read_tidig_z: 1399 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, 1400 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT); 1401 case AMDGPUIntrinsic::SI_load_const: { 1402 SDValue Ops[] = { 1403 Op.getOperand(1), 1404 Op.getOperand(2) 1405 }; 1406 1407 MachineMemOperand *MMO = MF.getMachineMemOperand( 1408 MachinePointerInfo(), 1409 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant, 1410 VT.getStoreSize(), 4); 1411 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL, 1412 Op->getVTList(), Ops, VT, MMO); 1413 } 1414 case AMDGPUIntrinsic::SI_vs_load_input: 1415 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT, 1416 Op.getOperand(1), 1417 Op.getOperand(2), 1418 Op.getOperand(3)); 1419 1420 case AMDGPUIntrinsic::SI_fs_constant: { 1421 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3)); 1422 SDValue Glue = M0.getValue(1); 1423 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32, 1424 DAG.getConstant(2, DL, MVT::i32), // P0 1425 Op.getOperand(1), Op.getOperand(2), Glue); 1426 } 1427 case AMDGPUIntrinsic::SI_packf16: 1428 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef()) 1429 return DAG.getUNDEF(MVT::i32); 1430 return Op; 1431 case AMDGPUIntrinsic::SI_fs_interp: { 1432 SDValue IJ = Op.getOperand(4); 1433 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ, 1434 DAG.getConstant(0, DL, MVT::i32)); 1435 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ, 1436 DAG.getConstant(1, DL, MVT::i32)); 1437 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3)); 1438 SDValue Glue = M0.getValue(1); 1439 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL, 1440 DAG.getVTList(MVT::f32, MVT::Glue), 1441 I, Op.getOperand(1), Op.getOperand(2), Glue); 1442 Glue = SDValue(P1.getNode(), 1); 1443 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J, 1444 Op.getOperand(1), Op.getOperand(2), Glue); 1445 } 1446 case Intrinsic::amdgcn_interp_p1: { 1447 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4)); 1448 SDValue Glue = M0.getValue(1); 1449 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1), 1450 Op.getOperand(2), Op.getOperand(3), Glue); 1451 } 1452 case Intrinsic::amdgcn_interp_p2: { 1453 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5)); 1454 SDValue Glue = SDValue(M0.getNode(), 1); 1455 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1), 1456 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4), 1457 Glue); 1458 } 1459 case Intrinsic::amdgcn_ldexp: 1460 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT, 1461 Op.getOperand(1), Op.getOperand(2)); 1462 case Intrinsic::amdgcn_class: 1463 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT, 1464 Op.getOperand(1), Op.getOperand(2)); 1465 case Intrinsic::amdgcn_div_fmas: 1466 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT, 1467 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3), 1468 Op.getOperand(4)); 1469 1470 case Intrinsic::amdgcn_div_fixup: 1471 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT, 1472 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 1473 1474 case Intrinsic::amdgcn_trig_preop: 1475 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT, 1476 Op.getOperand(1), Op.getOperand(2)); 1477 case Intrinsic::amdgcn_div_scale: { 1478 // 3rd parameter required to be a constant. 1479 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3)); 1480 if (!Param) 1481 return DAG.getUNDEF(VT); 1482 1483 // Translate to the operands expected by the machine instruction. The 1484 // first parameter must be the same as the first instruction. 1485 SDValue Numerator = Op.getOperand(1); 1486 SDValue Denominator = Op.getOperand(2); 1487 1488 // Note this order is opposite of the machine instruction's operations, 1489 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The 1490 // intrinsic has the numerator as the first operand to match a normal 1491 // division operation. 1492 1493 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator; 1494 1495 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0, 1496 Denominator, Numerator); 1497 } 1498 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte0: 1499 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Op.getOperand(1)); 1500 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte1: 1501 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE1, DL, VT, Op.getOperand(1)); 1502 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte2: 1503 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE2, DL, VT, Op.getOperand(1)); 1504 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte3: 1505 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE3, DL, VT, Op.getOperand(1)); 1506 default: 1507 return AMDGPUTargetLowering::LowerOperation(Op, DAG); 1508 } 1509 } 1510 1511 SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op, 1512 SelectionDAG &DAG) const { 1513 MachineFunction &MF = DAG.getMachineFunction(); 1514 SDLoc DL(Op); 1515 SDValue Chain = Op.getOperand(0); 1516 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 1517 1518 switch (IntrinsicID) { 1519 case AMDGPUIntrinsic::SI_sendmsg: { 1520 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3)); 1521 SDValue Glue = Chain.getValue(1); 1522 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain, 1523 Op.getOperand(2), Glue); 1524 } 1525 case AMDGPUIntrinsic::SI_tbuffer_store: { 1526 SDValue Ops[] = { 1527 Chain, 1528 Op.getOperand(2), 1529 Op.getOperand(3), 1530 Op.getOperand(4), 1531 Op.getOperand(5), 1532 Op.getOperand(6), 1533 Op.getOperand(7), 1534 Op.getOperand(8), 1535 Op.getOperand(9), 1536 Op.getOperand(10), 1537 Op.getOperand(11), 1538 Op.getOperand(12), 1539 Op.getOperand(13), 1540 Op.getOperand(14) 1541 }; 1542 1543 EVT VT = Op.getOperand(3).getValueType(); 1544 1545 MachineMemOperand *MMO = MF.getMachineMemOperand( 1546 MachinePointerInfo(), 1547 MachineMemOperand::MOStore, 1548 VT.getStoreSize(), 4); 1549 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL, 1550 Op->getVTList(), Ops, VT, MMO); 1551 } 1552 default: 1553 return SDValue(); 1554 } 1555 } 1556 1557 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { 1558 SDLoc DL(Op); 1559 LoadSDNode *Load = cast<LoadSDNode>(Op); 1560 1561 if (Op.getValueType().isVector()) { 1562 assert(Op.getValueType().getVectorElementType() == MVT::i32 && 1563 "Custom lowering for non-i32 vectors hasn't been implemented."); 1564 unsigned NumElements = Op.getValueType().getVectorNumElements(); 1565 assert(NumElements != 2 && "v2 loads are supported for all address spaces."); 1566 1567 switch (Load->getAddressSpace()) { 1568 default: break; 1569 case AMDGPUAS::CONSTANT_ADDRESS: 1570 if (isMemOpUniform(Load)) 1571 break; 1572 // Non-uniform loads will be selected to MUBUF instructions, so they 1573 // have the same legalization requires ments as global and private 1574 // loads. 1575 // 1576 // Fall-through 1577 case AMDGPUAS::GLOBAL_ADDRESS: 1578 case AMDGPUAS::PRIVATE_ADDRESS: 1579 if (NumElements >= 8) 1580 return SplitVectorLoad(Op, DAG); 1581 1582 // v4 loads are supported for private and global memory. 1583 if (NumElements <= 4) 1584 break; 1585 // fall-through 1586 case AMDGPUAS::LOCAL_ADDRESS: 1587 // If properly aligned, if we split we might be able to use ds_read_b64. 1588 return SplitVectorLoad(Op, DAG); 1589 } 1590 } 1591 1592 return AMDGPUTargetLowering::LowerLOAD(Op, DAG); 1593 } 1594 1595 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 1596 if (Op.getValueType() != MVT::i64) 1597 return SDValue(); 1598 1599 SDLoc DL(Op); 1600 SDValue Cond = Op.getOperand(0); 1601 1602 SDValue Zero = DAG.getConstant(0, DL, MVT::i32); 1603 SDValue One = DAG.getConstant(1, DL, MVT::i32); 1604 1605 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1)); 1606 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2)); 1607 1608 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero); 1609 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero); 1610 1611 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1); 1612 1613 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One); 1614 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One); 1615 1616 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1); 1617 1618 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi); 1619 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res); 1620 } 1621 1622 // Catch division cases where we can use shortcuts with rcp and rsq 1623 // instructions. 1624 SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const { 1625 SDLoc SL(Op); 1626 SDValue LHS = Op.getOperand(0); 1627 SDValue RHS = Op.getOperand(1); 1628 EVT VT = Op.getValueType(); 1629 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath; 1630 1631 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) { 1632 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) && 1633 CLHS->isExactlyValue(1.0)) { 1634 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to 1635 // the CI documentation has a worst case error of 1 ulp. 1636 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to 1637 // use it as long as we aren't trying to use denormals. 1638 1639 // 1.0 / sqrt(x) -> rsq(x) 1640 // 1641 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP 1642 // error seems really high at 2^29 ULP. 1643 if (RHS.getOpcode() == ISD::FSQRT) 1644 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0)); 1645 1646 // 1.0 / x -> rcp(x) 1647 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); 1648 } 1649 } 1650 1651 if (Unsafe) { 1652 // Turn into multiply by the reciprocal. 1653 // x / y -> x * (1.0 / y) 1654 SDNodeFlags Flags; 1655 Flags.setUnsafeAlgebra(true); 1656 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); 1657 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags); 1658 } 1659 1660 return SDValue(); 1661 } 1662 1663 SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const { 1664 SDValue FastLowered = LowerFastFDIV(Op, DAG); 1665 if (FastLowered.getNode()) 1666 return FastLowered; 1667 1668 // This uses v_rcp_f32 which does not handle denormals. Let this hit a 1669 // selection error for now rather than do something incorrect. 1670 if (Subtarget->hasFP32Denormals()) 1671 return SDValue(); 1672 1673 SDLoc SL(Op); 1674 SDValue LHS = Op.getOperand(0); 1675 SDValue RHS = Op.getOperand(1); 1676 1677 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS); 1678 1679 const APFloat K0Val(BitsToFloat(0x6f800000)); 1680 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32); 1681 1682 const APFloat K1Val(BitsToFloat(0x2f800000)); 1683 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32); 1684 1685 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); 1686 1687 EVT SetCCVT = 1688 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32); 1689 1690 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT); 1691 1692 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One); 1693 1694 // TODO: Should this propagate fast-math-flags? 1695 1696 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3); 1697 1698 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1); 1699 1700 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0); 1701 1702 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul); 1703 } 1704 1705 SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const { 1706 if (DAG.getTarget().Options.UnsafeFPMath) 1707 return LowerFastFDIV(Op, DAG); 1708 1709 SDLoc SL(Op); 1710 SDValue X = Op.getOperand(0); 1711 SDValue Y = Op.getOperand(1); 1712 1713 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64); 1714 1715 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1); 1716 1717 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X); 1718 1719 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0); 1720 1721 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0); 1722 1723 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One); 1724 1725 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp); 1726 1727 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One); 1728 1729 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X); 1730 1731 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1); 1732 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3); 1733 1734 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64, 1735 NegDivScale0, Mul, DivScale1); 1736 1737 SDValue Scale; 1738 1739 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) { 1740 // Workaround a hardware bug on SI where the condition output from div_scale 1741 // is not usable. 1742 1743 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32); 1744 1745 // Figure out if the scale to use for div_fmas. 1746 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X); 1747 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y); 1748 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0); 1749 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1); 1750 1751 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi); 1752 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi); 1753 1754 SDValue Scale0Hi 1755 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi); 1756 SDValue Scale1Hi 1757 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi); 1758 1759 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ); 1760 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ); 1761 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen); 1762 } else { 1763 Scale = DivScale1.getValue(1); 1764 } 1765 1766 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64, 1767 Fma4, Fma3, Mul, Scale); 1768 1769 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X); 1770 } 1771 1772 SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const { 1773 EVT VT = Op.getValueType(); 1774 1775 if (VT == MVT::f32) 1776 return LowerFDIV32(Op, DAG); 1777 1778 if (VT == MVT::f64) 1779 return LowerFDIV64(Op, DAG); 1780 1781 llvm_unreachable("Unexpected type for fdiv"); 1782 } 1783 1784 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const { 1785 SDLoc DL(Op); 1786 StoreSDNode *Store = cast<StoreSDNode>(Op); 1787 EVT VT = Store->getMemoryVT(); 1788 1789 // These stores are legal. 1790 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) { 1791 if (VT.isVector() && VT.getVectorNumElements() > 4) 1792 return ScalarizeVectorStore(Op, DAG); 1793 return SDValue(); 1794 } 1795 1796 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG); 1797 if (Ret.getNode()) 1798 return Ret; 1799 1800 if (VT.isVector() && VT.getVectorNumElements() >= 8) 1801 return SplitVectorStore(Op, DAG); 1802 1803 if (VT == MVT::i1) 1804 return DAG.getTruncStore(Store->getChain(), DL, 1805 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32), 1806 Store->getBasePtr(), MVT::i1, Store->getMemOperand()); 1807 1808 return SDValue(); 1809 } 1810 1811 SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const { 1812 SDLoc DL(Op); 1813 EVT VT = Op.getValueType(); 1814 SDValue Arg = Op.getOperand(0); 1815 // TODO: Should this propagate fast-math-flags? 1816 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT, 1817 DAG.getNode(ISD::FMUL, DL, VT, Arg, 1818 DAG.getConstantFP(0.5/M_PI, DL, 1819 VT))); 1820 1821 switch (Op.getOpcode()) { 1822 case ISD::FCOS: 1823 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart); 1824 case ISD::FSIN: 1825 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart); 1826 default: 1827 llvm_unreachable("Wrong trig opcode"); 1828 } 1829 } 1830 1831 //===----------------------------------------------------------------------===// 1832 // Custom DAG optimizations 1833 //===----------------------------------------------------------------------===// 1834 1835 SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N, 1836 DAGCombinerInfo &DCI) const { 1837 EVT VT = N->getValueType(0); 1838 EVT ScalarVT = VT.getScalarType(); 1839 if (ScalarVT != MVT::f32) 1840 return SDValue(); 1841 1842 SelectionDAG &DAG = DCI.DAG; 1843 SDLoc DL(N); 1844 1845 SDValue Src = N->getOperand(0); 1846 EVT SrcVT = Src.getValueType(); 1847 1848 // TODO: We could try to match extracting the higher bytes, which would be 1849 // easier if i8 vectors weren't promoted to i32 vectors, particularly after 1850 // types are legalized. v4i8 -> v4f32 is probably the only case to worry 1851 // about in practice. 1852 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) { 1853 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) { 1854 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src); 1855 DCI.AddToWorklist(Cvt.getNode()); 1856 return Cvt; 1857 } 1858 } 1859 1860 // We are primarily trying to catch operations on illegal vector types 1861 // before they are expanded. 1862 // For scalars, we can use the more flexible method of checking masked bits 1863 // after legalization. 1864 if (!DCI.isBeforeLegalize() || 1865 !SrcVT.isVector() || 1866 SrcVT.getVectorElementType() != MVT::i8) { 1867 return SDValue(); 1868 } 1869 1870 assert(DCI.isBeforeLegalize() && "Unexpected legal type"); 1871 1872 // Weird sized vectors are a pain to handle, but we know 3 is really the same 1873 // size as 4. 1874 unsigned NElts = SrcVT.getVectorNumElements(); 1875 if (!SrcVT.isSimple() && NElts != 3) 1876 return SDValue(); 1877 1878 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to 1879 // prevent a mess from expanding to v4i32 and repacking. 1880 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) { 1881 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT); 1882 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT); 1883 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts); 1884 LoadSDNode *Load = cast<LoadSDNode>(Src); 1885 1886 unsigned AS = Load->getAddressSpace(); 1887 unsigned Align = Load->getAlignment(); 1888 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext()); 1889 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty); 1890 1891 // Don't try to replace the load if we have to expand it due to alignment 1892 // problems. Otherwise we will end up scalarizing the load, and trying to 1893 // repack into the vector for no real reason. 1894 if (Align < ABIAlignment && 1895 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) { 1896 return SDValue(); 1897 } 1898 1899 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT, 1900 Load->getChain(), 1901 Load->getBasePtr(), 1902 LoadVT, 1903 Load->getMemOperand()); 1904 1905 // Make sure successors of the original load stay after it by updating 1906 // them to use the new Chain. 1907 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1)); 1908 1909 SmallVector<SDValue, 4> Elts; 1910 if (RegVT.isVector()) 1911 DAG.ExtractVectorElements(NewLoad, Elts); 1912 else 1913 Elts.push_back(NewLoad); 1914 1915 SmallVector<SDValue, 4> Ops; 1916 1917 unsigned EltIdx = 0; 1918 for (SDValue Elt : Elts) { 1919 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx); 1920 for (unsigned I = 0; I < ComponentsInElt; ++I) { 1921 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I; 1922 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt); 1923 DCI.AddToWorklist(Cvt.getNode()); 1924 Ops.push_back(Cvt); 1925 } 1926 1927 ++EltIdx; 1928 } 1929 1930 assert(Ops.size() == NElts); 1931 1932 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops); 1933 } 1934 1935 return SDValue(); 1936 } 1937 1938 /// \brief Return true if the given offset Size in bytes can be folded into 1939 /// the immediate offsets of a memory instruction for the given address space. 1940 static bool canFoldOffset(unsigned OffsetSize, unsigned AS, 1941 const AMDGPUSubtarget &STI) { 1942 switch (AS) { 1943 case AMDGPUAS::GLOBAL_ADDRESS: { 1944 // MUBUF instructions a 12-bit offset in bytes. 1945 return isUInt<12>(OffsetSize); 1946 } 1947 case AMDGPUAS::CONSTANT_ADDRESS: { 1948 // SMRD instructions have an 8-bit offset in dwords on SI and 1949 // a 20-bit offset in bytes on VI. 1950 if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) 1951 return isUInt<20>(OffsetSize); 1952 else 1953 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4); 1954 } 1955 case AMDGPUAS::LOCAL_ADDRESS: 1956 case AMDGPUAS::REGION_ADDRESS: { 1957 // The single offset versions have a 16-bit offset in bytes. 1958 return isUInt<16>(OffsetSize); 1959 } 1960 case AMDGPUAS::PRIVATE_ADDRESS: 1961 // Indirect register addressing does not use any offsets. 1962 default: 1963 return 0; 1964 } 1965 } 1966 1967 // (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2) 1968 1969 // This is a variant of 1970 // (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2), 1971 // 1972 // The normal DAG combiner will do this, but only if the add has one use since 1973 // that would increase the number of instructions. 1974 // 1975 // This prevents us from seeing a constant offset that can be folded into a 1976 // memory instruction's addressing mode. If we know the resulting add offset of 1977 // a pointer can be folded into an addressing offset, we can replace the pointer 1978 // operand with the add of new constant offset. This eliminates one of the uses, 1979 // and may allow the remaining use to also be simplified. 1980 // 1981 SDValue SITargetLowering::performSHLPtrCombine(SDNode *N, 1982 unsigned AddrSpace, 1983 DAGCombinerInfo &DCI) const { 1984 SDValue N0 = N->getOperand(0); 1985 SDValue N1 = N->getOperand(1); 1986 1987 if (N0.getOpcode() != ISD::ADD) 1988 return SDValue(); 1989 1990 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1); 1991 if (!CN1) 1992 return SDValue(); 1993 1994 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 1995 if (!CAdd) 1996 return SDValue(); 1997 1998 // If the resulting offset is too large, we can't fold it into the addressing 1999 // mode offset. 2000 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue(); 2001 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget)) 2002 return SDValue(); 2003 2004 SelectionDAG &DAG = DCI.DAG; 2005 SDLoc SL(N); 2006 EVT VT = N->getValueType(0); 2007 2008 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1); 2009 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32); 2010 2011 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset); 2012 } 2013 2014 SDValue SITargetLowering::performAndCombine(SDNode *N, 2015 DAGCombinerInfo &DCI) const { 2016 if (DCI.isBeforeLegalize()) 2017 return SDValue(); 2018 2019 if (SDValue Base = AMDGPUTargetLowering::performAndCombine(N, DCI)) 2020 return Base; 2021 2022 SelectionDAG &DAG = DCI.DAG; 2023 2024 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) -> 2025 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity) 2026 SDValue LHS = N->getOperand(0); 2027 SDValue RHS = N->getOperand(1); 2028 2029 if (LHS.getOpcode() == ISD::SETCC && 2030 RHS.getOpcode() == ISD::SETCC) { 2031 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get(); 2032 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get(); 2033 2034 SDValue X = LHS.getOperand(0); 2035 SDValue Y = RHS.getOperand(0); 2036 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X) 2037 return SDValue(); 2038 2039 if (LCC == ISD::SETO) { 2040 if (X != LHS.getOperand(1)) 2041 return SDValue(); 2042 2043 if (RCC == ISD::SETUNE) { 2044 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1)); 2045 if (!C1 || !C1->isInfinity() || C1->isNegative()) 2046 return SDValue(); 2047 2048 const uint32_t Mask = SIInstrFlags::N_NORMAL | 2049 SIInstrFlags::N_SUBNORMAL | 2050 SIInstrFlags::N_ZERO | 2051 SIInstrFlags::P_ZERO | 2052 SIInstrFlags::P_SUBNORMAL | 2053 SIInstrFlags::P_NORMAL; 2054 2055 static_assert(((~(SIInstrFlags::S_NAN | 2056 SIInstrFlags::Q_NAN | 2057 SIInstrFlags::N_INFINITY | 2058 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask, 2059 "mask not equal"); 2060 2061 SDLoc DL(N); 2062 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, 2063 X, DAG.getConstant(Mask, DL, MVT::i32)); 2064 } 2065 } 2066 } 2067 2068 return SDValue(); 2069 } 2070 2071 SDValue SITargetLowering::performOrCombine(SDNode *N, 2072 DAGCombinerInfo &DCI) const { 2073 SelectionDAG &DAG = DCI.DAG; 2074 SDValue LHS = N->getOperand(0); 2075 SDValue RHS = N->getOperand(1); 2076 2077 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2) 2078 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS && 2079 RHS.getOpcode() == AMDGPUISD::FP_CLASS) { 2080 SDValue Src = LHS.getOperand(0); 2081 if (Src != RHS.getOperand(0)) 2082 return SDValue(); 2083 2084 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1)); 2085 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1)); 2086 if (!CLHS || !CRHS) 2087 return SDValue(); 2088 2089 // Only 10 bits are used. 2090 static const uint32_t MaxMask = 0x3ff; 2091 2092 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask; 2093 SDLoc DL(N); 2094 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, 2095 Src, DAG.getConstant(NewMask, DL, MVT::i32)); 2096 } 2097 2098 return SDValue(); 2099 } 2100 2101 SDValue SITargetLowering::performClassCombine(SDNode *N, 2102 DAGCombinerInfo &DCI) const { 2103 SelectionDAG &DAG = DCI.DAG; 2104 SDValue Mask = N->getOperand(1); 2105 2106 // fp_class x, 0 -> false 2107 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) { 2108 if (CMask->isNullValue()) 2109 return DAG.getConstant(0, SDLoc(N), MVT::i1); 2110 } 2111 2112 return SDValue(); 2113 } 2114 2115 static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) { 2116 switch (Opc) { 2117 case ISD::FMAXNUM: 2118 return AMDGPUISD::FMAX3; 2119 case ISD::SMAX: 2120 return AMDGPUISD::SMAX3; 2121 case ISD::UMAX: 2122 return AMDGPUISD::UMAX3; 2123 case ISD::FMINNUM: 2124 return AMDGPUISD::FMIN3; 2125 case ISD::SMIN: 2126 return AMDGPUISD::SMIN3; 2127 case ISD::UMIN: 2128 return AMDGPUISD::UMIN3; 2129 default: 2130 llvm_unreachable("Not a min/max opcode"); 2131 } 2132 } 2133 2134 SDValue SITargetLowering::performMin3Max3Combine(SDNode *N, 2135 DAGCombinerInfo &DCI) const { 2136 SelectionDAG &DAG = DCI.DAG; 2137 2138 unsigned Opc = N->getOpcode(); 2139 SDValue Op0 = N->getOperand(0); 2140 SDValue Op1 = N->getOperand(1); 2141 2142 // Only do this if the inner op has one use since this will just increases 2143 // register pressure for no benefit. 2144 2145 // max(max(a, b), c) 2146 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) { 2147 SDLoc DL(N); 2148 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), 2149 DL, 2150 N->getValueType(0), 2151 Op0.getOperand(0), 2152 Op0.getOperand(1), 2153 Op1); 2154 } 2155 2156 // max(a, max(b, c)) 2157 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) { 2158 SDLoc DL(N); 2159 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), 2160 DL, 2161 N->getValueType(0), 2162 Op0, 2163 Op1.getOperand(0), 2164 Op1.getOperand(1)); 2165 } 2166 2167 return SDValue(); 2168 } 2169 2170 SDValue SITargetLowering::performSetCCCombine(SDNode *N, 2171 DAGCombinerInfo &DCI) const { 2172 SelectionDAG &DAG = DCI.DAG; 2173 SDLoc SL(N); 2174 2175 SDValue LHS = N->getOperand(0); 2176 SDValue RHS = N->getOperand(1); 2177 EVT VT = LHS.getValueType(); 2178 2179 if (VT != MVT::f32 && VT != MVT::f64) 2180 return SDValue(); 2181 2182 // Match isinf pattern 2183 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity)) 2184 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); 2185 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) { 2186 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS); 2187 if (!CRHS) 2188 return SDValue(); 2189 2190 const APFloat &APF = CRHS->getValueAPF(); 2191 if (APF.isInfinity() && !APF.isNegative()) { 2192 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY; 2193 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0), 2194 DAG.getConstant(Mask, SL, MVT::i32)); 2195 } 2196 } 2197 2198 return SDValue(); 2199 } 2200 2201 SDValue SITargetLowering::PerformDAGCombine(SDNode *N, 2202 DAGCombinerInfo &DCI) const { 2203 SelectionDAG &DAG = DCI.DAG; 2204 SDLoc DL(N); 2205 2206 switch (N->getOpcode()) { 2207 default: 2208 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); 2209 case ISD::SETCC: 2210 return performSetCCCombine(N, DCI); 2211 case ISD::FMAXNUM: // TODO: What about fmax_legacy? 2212 case ISD::FMINNUM: 2213 case ISD::SMAX: 2214 case ISD::SMIN: 2215 case ISD::UMAX: 2216 case ISD::UMIN: { 2217 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG && 2218 N->getValueType(0) != MVT::f64 && 2219 getTargetMachine().getOptLevel() > CodeGenOpt::None) 2220 return performMin3Max3Combine(N, DCI); 2221 break; 2222 } 2223 2224 case AMDGPUISD::CVT_F32_UBYTE0: 2225 case AMDGPUISD::CVT_F32_UBYTE1: 2226 case AMDGPUISD::CVT_F32_UBYTE2: 2227 case AMDGPUISD::CVT_F32_UBYTE3: { 2228 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0; 2229 2230 SDValue Src = N->getOperand(0); 2231 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8); 2232 2233 APInt KnownZero, KnownOne; 2234 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), 2235 !DCI.isBeforeLegalizeOps()); 2236 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2237 if (TLO.ShrinkDemandedConstant(Src, Demanded) || 2238 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) { 2239 DCI.CommitTargetLoweringOpt(TLO); 2240 } 2241 2242 break; 2243 } 2244 2245 case ISD::UINT_TO_FP: { 2246 return performUCharToFloatCombine(N, DCI); 2247 } 2248 case ISD::FADD: { 2249 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 2250 break; 2251 2252 EVT VT = N->getValueType(0); 2253 if (VT != MVT::f32) 2254 break; 2255 2256 // Only do this if we are not trying to support denormals. v_mad_f32 does 2257 // not support denormals ever. 2258 if (Subtarget->hasFP32Denormals()) 2259 break; 2260 2261 SDValue LHS = N->getOperand(0); 2262 SDValue RHS = N->getOperand(1); 2263 2264 // These should really be instruction patterns, but writing patterns with 2265 // source modiifiers is a pain. 2266 2267 // fadd (fadd (a, a), b) -> mad 2.0, a, b 2268 if (LHS.getOpcode() == ISD::FADD) { 2269 SDValue A = LHS.getOperand(0); 2270 if (A == LHS.getOperand(1)) { 2271 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); 2272 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS); 2273 } 2274 } 2275 2276 // fadd (b, fadd (a, a)) -> mad 2.0, a, b 2277 if (RHS.getOpcode() == ISD::FADD) { 2278 SDValue A = RHS.getOperand(0); 2279 if (A == RHS.getOperand(1)) { 2280 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); 2281 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS); 2282 } 2283 } 2284 2285 return SDValue(); 2286 } 2287 case ISD::FSUB: { 2288 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 2289 break; 2290 2291 EVT VT = N->getValueType(0); 2292 2293 // Try to get the fneg to fold into the source modifier. This undoes generic 2294 // DAG combines and folds them into the mad. 2295 // 2296 // Only do this if we are not trying to support denormals. v_mad_f32 does 2297 // not support denormals ever. 2298 if (VT == MVT::f32 && 2299 !Subtarget->hasFP32Denormals()) { 2300 SDValue LHS = N->getOperand(0); 2301 SDValue RHS = N->getOperand(1); 2302 if (LHS.getOpcode() == ISD::FADD) { 2303 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c) 2304 2305 SDValue A = LHS.getOperand(0); 2306 if (A == LHS.getOperand(1)) { 2307 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); 2308 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS); 2309 2310 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS); 2311 } 2312 } 2313 2314 if (RHS.getOpcode() == ISD::FADD) { 2315 // (fsub c, (fadd a, a)) -> mad -2.0, a, c 2316 2317 SDValue A = RHS.getOperand(0); 2318 if (A == RHS.getOperand(1)) { 2319 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32); 2320 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS); 2321 } 2322 } 2323 2324 return SDValue(); 2325 } 2326 2327 break; 2328 } 2329 case ISD::LOAD: 2330 case ISD::STORE: 2331 case ISD::ATOMIC_LOAD: 2332 case ISD::ATOMIC_STORE: 2333 case ISD::ATOMIC_CMP_SWAP: 2334 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: 2335 case ISD::ATOMIC_SWAP: 2336 case ISD::ATOMIC_LOAD_ADD: 2337 case ISD::ATOMIC_LOAD_SUB: 2338 case ISD::ATOMIC_LOAD_AND: 2339 case ISD::ATOMIC_LOAD_OR: 2340 case ISD::ATOMIC_LOAD_XOR: 2341 case ISD::ATOMIC_LOAD_NAND: 2342 case ISD::ATOMIC_LOAD_MIN: 2343 case ISD::ATOMIC_LOAD_MAX: 2344 case ISD::ATOMIC_LOAD_UMIN: 2345 case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics. 2346 if (DCI.isBeforeLegalize()) 2347 break; 2348 2349 MemSDNode *MemNode = cast<MemSDNode>(N); 2350 SDValue Ptr = MemNode->getBasePtr(); 2351 2352 // TODO: We could also do this for multiplies. 2353 unsigned AS = MemNode->getAddressSpace(); 2354 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) { 2355 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI); 2356 if (NewPtr) { 2357 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end()); 2358 2359 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr; 2360 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0); 2361 } 2362 } 2363 break; 2364 } 2365 case ISD::AND: 2366 return performAndCombine(N, DCI); 2367 case ISD::OR: 2368 return performOrCombine(N, DCI); 2369 case AMDGPUISD::FP_CLASS: 2370 return performClassCombine(N, DCI); 2371 } 2372 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); 2373 } 2374 2375 /// \brief Analyze the possible immediate value Op 2376 /// 2377 /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate 2378 /// and the immediate value if it's a literal immediate 2379 int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const { 2380 2381 const SIInstrInfo *TII = 2382 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); 2383 2384 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) { 2385 if (TII->isInlineConstant(Node->getAPIntValue())) 2386 return 0; 2387 2388 uint64_t Val = Node->getZExtValue(); 2389 return isUInt<32>(Val) ? Val : -1; 2390 } 2391 2392 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) { 2393 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt())) 2394 return 0; 2395 2396 if (Node->getValueType(0) == MVT::f32) 2397 return FloatToBits(Node->getValueAPF().convertToFloat()); 2398 2399 return -1; 2400 } 2401 2402 return -1; 2403 } 2404 2405 /// \brief Helper function for adjustWritemask 2406 static unsigned SubIdx2Lane(unsigned Idx) { 2407 switch (Idx) { 2408 default: return 0; 2409 case AMDGPU::sub0: return 0; 2410 case AMDGPU::sub1: return 1; 2411 case AMDGPU::sub2: return 2; 2412 case AMDGPU::sub3: return 3; 2413 } 2414 } 2415 2416 /// \brief Adjust the writemask of MIMG instructions 2417 void SITargetLowering::adjustWritemask(MachineSDNode *&Node, 2418 SelectionDAG &DAG) const { 2419 SDNode *Users[4] = { }; 2420 unsigned Lane = 0; 2421 unsigned OldDmask = Node->getConstantOperandVal(0); 2422 unsigned NewDmask = 0; 2423 2424 // Try to figure out the used register components 2425 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end(); 2426 I != E; ++I) { 2427 2428 // Abort if we can't understand the usage 2429 if (!I->isMachineOpcode() || 2430 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG) 2431 return; 2432 2433 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used. 2434 // Note that subregs are packed, i.e. Lane==0 is the first bit set 2435 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit 2436 // set, etc. 2437 Lane = SubIdx2Lane(I->getConstantOperandVal(1)); 2438 2439 // Set which texture component corresponds to the lane. 2440 unsigned Comp; 2441 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) { 2442 assert(Dmask); 2443 Comp = countTrailingZeros(Dmask); 2444 Dmask &= ~(1 << Comp); 2445 } 2446 2447 // Abort if we have more than one user per component 2448 if (Users[Lane]) 2449 return; 2450 2451 Users[Lane] = *I; 2452 NewDmask |= 1 << Comp; 2453 } 2454 2455 // Abort if there's no change 2456 if (NewDmask == OldDmask) 2457 return; 2458 2459 // Adjust the writemask in the node 2460 std::vector<SDValue> Ops; 2461 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32)); 2462 Ops.insert(Ops.end(), Node->op_begin() + 1, Node->op_end()); 2463 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops); 2464 2465 // If we only got one lane, replace it with a copy 2466 // (if NewDmask has only one bit set...) 2467 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) { 2468 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(), 2469 MVT::i32); 2470 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, 2471 SDLoc(), Users[Lane]->getValueType(0), 2472 SDValue(Node, 0), RC); 2473 DAG.ReplaceAllUsesWith(Users[Lane], Copy); 2474 return; 2475 } 2476 2477 // Update the users of the node with the new indices 2478 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) { 2479 2480 SDNode *User = Users[i]; 2481 if (!User) 2482 continue; 2483 2484 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32); 2485 DAG.UpdateNodeOperands(User, User->getOperand(0), Op); 2486 2487 switch (Idx) { 2488 default: break; 2489 case AMDGPU::sub0: Idx = AMDGPU::sub1; break; 2490 case AMDGPU::sub1: Idx = AMDGPU::sub2; break; 2491 case AMDGPU::sub2: Idx = AMDGPU::sub3; break; 2492 } 2493 } 2494 } 2495 2496 static bool isFrameIndexOp(SDValue Op) { 2497 if (Op.getOpcode() == ISD::AssertZext) 2498 Op = Op.getOperand(0); 2499 2500 return isa<FrameIndexSDNode>(Op); 2501 } 2502 2503 /// \brief Legalize target independent instructions (e.g. INSERT_SUBREG) 2504 /// with frame index operands. 2505 /// LLVM assumes that inputs are to these instructions are registers. 2506 void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node, 2507 SelectionDAG &DAG) const { 2508 2509 SmallVector<SDValue, 8> Ops; 2510 for (unsigned i = 0; i < Node->getNumOperands(); ++i) { 2511 if (!isFrameIndexOp(Node->getOperand(i))) { 2512 Ops.push_back(Node->getOperand(i)); 2513 continue; 2514 } 2515 2516 SDLoc DL(Node); 2517 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, 2518 Node->getOperand(i).getValueType(), 2519 Node->getOperand(i)), 0)); 2520 } 2521 2522 DAG.UpdateNodeOperands(Node, Ops); 2523 } 2524 2525 /// \brief Fold the instructions after selecting them. 2526 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node, 2527 SelectionDAG &DAG) const { 2528 const SIInstrInfo *TII = 2529 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); 2530 2531 if (TII->isMIMG(Node->getMachineOpcode())) 2532 adjustWritemask(Node, DAG); 2533 2534 if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG || 2535 Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) { 2536 legalizeTargetIndependentNode(Node, DAG); 2537 return Node; 2538 } 2539 return Node; 2540 } 2541 2542 /// \brief Assign the register class depending on the number of 2543 /// bits set in the writemask 2544 void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI, 2545 SDNode *Node) const { 2546 const SIInstrInfo *TII = 2547 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); 2548 2549 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); 2550 2551 if (TII->isVOP3(MI->getOpcode())) { 2552 // Make sure constant bus requirements are respected. 2553 TII->legalizeOperandsVOP3(MRI, MI); 2554 return; 2555 } 2556 2557 if (TII->isMIMG(*MI)) { 2558 unsigned VReg = MI->getOperand(0).getReg(); 2559 unsigned Writemask = MI->getOperand(1).getImm(); 2560 unsigned BitsSet = 0; 2561 for (unsigned i = 0; i < 4; ++i) 2562 BitsSet += Writemask & (1 << i) ? 1 : 0; 2563 2564 const TargetRegisterClass *RC; 2565 switch (BitsSet) { 2566 default: return; 2567 case 1: RC = &AMDGPU::VGPR_32RegClass; break; 2568 case 2: RC = &AMDGPU::VReg_64RegClass; break; 2569 case 3: RC = &AMDGPU::VReg_96RegClass; break; 2570 } 2571 2572 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet); 2573 MI->setDesc(TII->get(NewOpcode)); 2574 MRI.setRegClass(VReg, RC); 2575 return; 2576 } 2577 2578 // Replace unused atomics with the no return version. 2579 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode()); 2580 if (NoRetAtomicOp != -1) { 2581 if (!Node->hasAnyUseOfValue(0)) { 2582 MI->setDesc(TII->get(NoRetAtomicOp)); 2583 MI->RemoveOperand(0); 2584 } 2585 2586 return; 2587 } 2588 } 2589 2590 static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) { 2591 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32); 2592 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0); 2593 } 2594 2595 MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG, 2596 SDLoc DL, 2597 SDValue Ptr) const { 2598 const SIInstrInfo *TII = 2599 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); 2600 2601 // Build the half of the subregister with the constants before building the 2602 // full 128-bit register. If we are building multiple resource descriptors, 2603 // this will allow CSEing of the 2-component register. 2604 const SDValue Ops0[] = { 2605 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32), 2606 buildSMovImm32(DAG, DL, 0), 2607 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 2608 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32), 2609 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32) 2610 }; 2611 2612 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, 2613 MVT::v2i32, Ops0), 0); 2614 2615 // Combine the constants and the pointer. 2616 const SDValue Ops1[] = { 2617 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), 2618 Ptr, 2619 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32), 2620 SubRegHi, 2621 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32) 2622 }; 2623 2624 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1); 2625 } 2626 2627 /// \brief Return a resource descriptor with the 'Add TID' bit enabled 2628 /// The TID (Thread ID) is multiplied by the stride value (bits [61:48] 2629 /// of the resource descriptor) to create an offset, which is added to 2630 /// the resource pointer. 2631 MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, 2632 SDLoc DL, 2633 SDValue Ptr, 2634 uint32_t RsrcDword1, 2635 uint64_t RsrcDword2And3) const { 2636 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr); 2637 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr); 2638 if (RsrcDword1) { 2639 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi, 2640 DAG.getConstant(RsrcDword1, DL, MVT::i32)), 2641 0); 2642 } 2643 2644 SDValue DataLo = buildSMovImm32(DAG, DL, 2645 RsrcDword2And3 & UINT64_C(0xFFFFFFFF)); 2646 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32); 2647 2648 const SDValue Ops[] = { 2649 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), 2650 PtrLo, 2651 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 2652 PtrHi, 2653 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32), 2654 DataLo, 2655 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32), 2656 DataHi, 2657 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32) 2658 }; 2659 2660 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops); 2661 } 2662 2663 SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG, 2664 const TargetRegisterClass *RC, 2665 unsigned Reg, EVT VT) const { 2666 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT); 2667 2668 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()), 2669 cast<RegisterSDNode>(VReg)->getReg(), VT); 2670 } 2671 2672 //===----------------------------------------------------------------------===// 2673 // SI Inline Assembly Support 2674 //===----------------------------------------------------------------------===// 2675 2676 std::pair<unsigned, const TargetRegisterClass *> 2677 SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 2678 StringRef Constraint, 2679 MVT VT) const { 2680 2681 if (Constraint.size() == 1) { 2682 switch (Constraint[0]) { 2683 case 's': 2684 case 'r': 2685 switch (VT.getSizeInBits()) { 2686 default: 2687 return std::make_pair(0U, nullptr); 2688 case 32: 2689 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass); 2690 case 64: 2691 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass); 2692 case 128: 2693 return std::make_pair(0U, &AMDGPU::SReg_128RegClass); 2694 case 256: 2695 return std::make_pair(0U, &AMDGPU::SReg_256RegClass); 2696 } 2697 2698 case 'v': 2699 switch (VT.getSizeInBits()) { 2700 default: 2701 return std::make_pair(0U, nullptr); 2702 case 32: 2703 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass); 2704 case 64: 2705 return std::make_pair(0U, &AMDGPU::VReg_64RegClass); 2706 case 96: 2707 return std::make_pair(0U, &AMDGPU::VReg_96RegClass); 2708 case 128: 2709 return std::make_pair(0U, &AMDGPU::VReg_128RegClass); 2710 case 256: 2711 return std::make_pair(0U, &AMDGPU::VReg_256RegClass); 2712 case 512: 2713 return std::make_pair(0U, &AMDGPU::VReg_512RegClass); 2714 } 2715 } 2716 } 2717 2718 if (Constraint.size() > 1) { 2719 const TargetRegisterClass *RC = nullptr; 2720 if (Constraint[1] == 'v') { 2721 RC = &AMDGPU::VGPR_32RegClass; 2722 } else if (Constraint[1] == 's') { 2723 RC = &AMDGPU::SGPR_32RegClass; 2724 } 2725 2726 if (RC) { 2727 uint32_t Idx; 2728 bool Failed = Constraint.substr(2).getAsInteger(10, Idx); 2729 if (!Failed && Idx < RC->getNumRegs()) 2730 return std::make_pair(RC->getRegister(Idx), RC); 2731 } 2732 } 2733 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 2734 } 2735 2736 SITargetLowering::ConstraintType 2737 SITargetLowering::getConstraintType(StringRef Constraint) const { 2738 if (Constraint.size() == 1) { 2739 switch (Constraint[0]) { 2740 default: break; 2741 case 's': 2742 case 'v': 2743 return C_RegisterClass; 2744 } 2745 } 2746 return TargetLowering::getConstraintType(Constraint); 2747 } 2748