1 //===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Custom DAG lowering for SI
13 //===----------------------------------------------------------------------===//
17 #define _USE_MATH_DEFINES
21 #include "SIISelLowering.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/CodeGen/CallingConvLower.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineRegisterInfo.h"
33 #include "llvm/CodeGen/SelectionDAG.h"
34 #include "llvm/IR/Function.h"
35 #include "llvm/ADT/SmallString.h"
39 SITargetLowering::SITargetLowering(TargetMachine &TM,
40 const AMDGPUSubtarget &STI)
41 : AMDGPUTargetLowering(TM, STI) {
42 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
43 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
45 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
46 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
48 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
49 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
51 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
52 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
53 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
55 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
56 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
58 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
59 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
61 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
62 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
64 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
65 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
67 computeRegisterProperties(STI.getRegisterInfo());
69 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
70 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
71 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
72 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
74 setOperationAction(ISD::ADD, MVT::i32, Legal);
75 setOperationAction(ISD::ADDC, MVT::i32, Legal);
76 setOperationAction(ISD::ADDE, MVT::i32, Legal);
77 setOperationAction(ISD::SUBC, MVT::i32, Legal);
78 setOperationAction(ISD::SUBE, MVT::i32, Legal);
80 setOperationAction(ISD::FSIN, MVT::f32, Custom);
81 setOperationAction(ISD::FCOS, MVT::f32, Custom);
83 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
84 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
86 // We need to custom lower vector stores from local memory
87 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
88 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
89 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
91 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
92 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
94 setOperationAction(ISD::STORE, MVT::i1, Custom);
95 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
97 setOperationAction(ISD::SELECT, MVT::i64, Custom);
98 setOperationAction(ISD::SELECT, MVT::f64, Promote);
99 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
101 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
102 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
103 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
104 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
106 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
107 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
109 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
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);
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);
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);
123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
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);
131 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
132 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
134 for (MVT VT : MVT::integer_valuetypes()) {
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);
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);
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);
154 for (MVT VT : MVT::integer_vector_valuetypes()) {
155 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand);
156 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand);
159 for (MVT VT : MVT::fp_valuetypes())
160 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
162 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
163 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
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);
171 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand);
173 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
174 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand);
176 setOperationAction(ISD::LOAD, MVT::i1, Custom);
178 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
179 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32);
181 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
182 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32);
184 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
186 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
187 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
188 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
190 // These should use UDIVREM, so set them to expand
191 setOperationAction(ISD::UDIV, MVT::i64, Expand);
192 setOperationAction(ISD::UREM, MVT::i64, Expand);
194 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
195 setOperationAction(ISD::SELECT, MVT::i1, Promote);
197 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
200 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
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) {
209 case ISD::BUILD_VECTOR:
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:
217 case ISD::CONCAT_VECTORS:
218 setOperationAction(Op, VT, Custom);
221 setOperationAction(Op, VT, Expand);
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);
233 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
234 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
236 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
237 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
239 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
240 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
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);
249 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
250 setOperationAction(ISD::FDIV, MVT::f32, Custom);
251 setOperationAction(ISD::FDIV, MVT::f64, Custom);
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::SELECT_CC);
262 setTargetDAGCombine(ISD::SETCC);
263 setTargetDAGCombine(ISD::AND);
264 setTargetDAGCombine(ISD::OR);
265 setTargetDAGCombine(ISD::UINT_TO_FP);
267 // All memory operations. Some folding on the pointer operand is done to help
268 // matching the constant offsets in the addressing modes.
269 setTargetDAGCombine(ISD::LOAD);
270 setTargetDAGCombine(ISD::STORE);
271 setTargetDAGCombine(ISD::ATOMIC_LOAD);
272 setTargetDAGCombine(ISD::ATOMIC_STORE);
273 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
274 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
275 setTargetDAGCombine(ISD::ATOMIC_SWAP);
276 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
277 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
278 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
279 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
280 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
281 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
282 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
283 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
284 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
285 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
287 setSchedulingPreference(Sched::RegPressure);
290 //===----------------------------------------------------------------------===//
291 // TargetLowering queries
292 //===----------------------------------------------------------------------===//
294 bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
296 // SI has some legal vector types, but no legal vector operations. Say no
297 // shuffles are legal in order to prefer scalarizing some vector operations.
301 bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
302 // Flat instructions do not have offsets, and only have the register
304 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
307 bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
308 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
309 // additionally can do r + r + i with addr64. 32-bit has more addressing
310 // mode options. Depending on the resource constant, it can also do
311 // (i64 r0) + (i32 r1) * (i14 i).
313 // Private arrays end up using a scratch buffer most of the time, so also
314 // assume those use MUBUF instructions. Scratch loads / stores are currently
315 // implemented as mubuf instructions with offen bit set, so slightly
316 // different than the normal addr64.
317 if (!isUInt<12>(AM.BaseOffs))
320 // FIXME: Since we can split immediate into soffset and immediate offset,
321 // would it make sense to allow any immediate?
324 case 0: // r + i or just i, depending on HasBaseReg.
327 return true; // We have r + r or r + i.
334 // Allow 2 * r as r + r
335 // Or 2 * r + i is allowed as r + r + i.
337 default: // Don't allow n * r
342 bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
343 const AddrMode &AM, Type *Ty,
345 // No global is ever allowed as a base.
350 case AMDGPUAS::GLOBAL_ADDRESS: {
351 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
352 // Assume the we will use FLAT for all global memory accesses
354 // FIXME: This assumption is currently wrong. On VI we still use
355 // MUBUF instructions for the r + i addressing mode. As currently
356 // implemented, the MUBUF instructions only work on buffer < 4GB.
357 // It may be possible to support > 4GB buffers with MUBUF instructions,
358 // by setting the stride value in the resource descriptor which would
359 // increase the size limit to (stride * 4GB). However, this is risky,
360 // because it has never been validated.
361 return isLegalFlatAddressingMode(AM);
364 return isLegalMUBUFAddressingMode(AM);
366 case AMDGPUAS::CONSTANT_ADDRESS: {
367 // If the offset isn't a multiple of 4, it probably isn't going to be
368 // correctly aligned.
369 if (AM.BaseOffs % 4 != 0)
370 return isLegalMUBUFAddressingMode(AM);
372 // There are no SMRD extloads, so if we have to do a small type access we
373 // will use a MUBUF load.
374 // FIXME?: We also need to do this if unaligned, but we don't know the
376 if (DL.getTypeStoreSize(Ty) < 4)
377 return isLegalMUBUFAddressingMode(AM);
379 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
380 // SMRD instructions have an 8-bit, dword offset on SI.
381 if (!isUInt<8>(AM.BaseOffs / 4))
383 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) {
384 // On CI+, this can also be a 32-bit literal constant offset. If it fits
385 // in 8-bits, it can use a smaller encoding.
386 if (!isUInt<32>(AM.BaseOffs / 4))
388 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) {
389 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
390 if (!isUInt<20>(AM.BaseOffs))
393 llvm_unreachable("unhandled generation");
395 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
398 if (AM.Scale == 1 && AM.HasBaseReg)
404 case AMDGPUAS::PRIVATE_ADDRESS:
405 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
406 return isLegalMUBUFAddressingMode(AM);
408 case AMDGPUAS::LOCAL_ADDRESS:
409 case AMDGPUAS::REGION_ADDRESS: {
410 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
412 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
413 // an 8-bit dword offset but we don't know the alignment here.
414 if (!isUInt<16>(AM.BaseOffs))
417 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
420 if (AM.Scale == 1 && AM.HasBaseReg)
425 case AMDGPUAS::FLAT_ADDRESS:
426 return isLegalFlatAddressingMode(AM);
429 llvm_unreachable("unhandled address space");
433 bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
436 bool *IsFast) const {
440 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
441 // which isn't a simple VT.
442 if (!VT.isSimple() || VT == MVT::Other)
445 // TODO - CI+ supports unaligned memory accesses, but this requires driver
448 // XXX - The only mention I see of this in the ISA manual is for LDS direct
449 // reads the "byte address and must be dword aligned". Is it also true for the
450 // normal loads and stores?
451 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
452 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
453 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
454 // with adjacent offsets.
455 bool AlignedBy4 = (Align % 4 == 0);
457 *IsFast = AlignedBy4;
461 // Smaller than dword value must be aligned.
462 // FIXME: This should be allowed on CI+
463 if (VT.bitsLT(MVT::i32))
466 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
467 // byte-address are ignored, thus forcing Dword alignment.
468 // This applies to private, global, and constant memory.
472 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
475 EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
476 unsigned SrcAlign, bool IsMemset,
479 MachineFunction &MF) const {
480 // FIXME: Should account for address space here.
482 // The default fallback uses the private pointer size as a guess for a type to
483 // use. Make sure we switch these to 64-bit accesses.
485 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
488 if (Size >= 8 && DstAlign >= 4)
495 TargetLoweringBase::LegalizeTypeAction
496 SITargetLowering::getPreferredVectorAction(EVT VT) const {
497 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
498 return TypeSplitVector;
500 return TargetLoweringBase::getPreferredVectorAction(VT);
503 bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
505 const SIInstrInfo *TII =
506 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
507 return TII->isInlineConstant(Imm);
510 SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
511 SDLoc SL, SDValue Chain,
512 unsigned Offset, bool Signed) const {
513 const DataLayout &DL = DAG.getDataLayout();
514 MachineFunction &MF = DAG.getMachineFunction();
515 const SIRegisterInfo *TRI =
516 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
517 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
519 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
521 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
522 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
523 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
524 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
525 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
526 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
527 DAG.getConstant(Offset, SL, PtrVT));
528 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
529 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
531 unsigned Align = DL.getABITypeAlignment(Ty);
533 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
534 if (MemVT.isFloatingPoint())
535 ExtTy = ISD::EXTLOAD;
537 return DAG.getLoad(ISD::UNINDEXED, ExtTy,
538 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
540 true, // isNonTemporal
545 SDValue SITargetLowering::LowerFormalArguments(
546 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
547 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
548 SmallVectorImpl<SDValue> &InVals) const {
549 const SIRegisterInfo *TRI =
550 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
552 MachineFunction &MF = DAG.getMachineFunction();
553 FunctionType *FType = MF.getFunction()->getFunctionType();
554 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
556 if (Subtarget->isAmdHsaOS() && Info->getShaderType() != ShaderType::COMPUTE) {
557 const Function *Fn = MF.getFunction();
558 DiagnosticInfoUnsupported NoGraphicsHSA(*Fn, "non-compute shaders with HSA");
559 DAG.getContext()->diagnose(NoGraphicsHSA);
563 // FIXME: We currently assume all calling conventions are kernels.
565 SmallVector<ISD::InputArg, 16> Splits;
566 BitVector Skipped(Ins.size());
568 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
569 const ISD::InputArg &Arg = Ins[i];
571 // First check if it's a PS input addr
572 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
573 !Arg.Flags.isByVal()) {
575 assert((PSInputNum <= 15) && "Too many PS inputs!");
578 // We can safely skip PS inputs
584 Info->PSInputAddr |= 1 << PSInputNum++;
587 // Second split vertices into their elements
588 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
589 ISD::InputArg NewArg = Arg;
590 NewArg.Flags.setSplit();
591 NewArg.VT = Arg.VT.getVectorElementType();
593 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
594 // three or five element vertex only needs three or five registers,
595 // NOT four or eight.
596 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
597 unsigned NumElements = ParamType->getVectorNumElements();
599 for (unsigned j = 0; j != NumElements; ++j) {
600 Splits.push_back(NewArg);
601 NewArg.PartOffset += NewArg.VT.getStoreSize();
604 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
605 Splits.push_back(Arg);
609 SmallVector<CCValAssign, 16> ArgLocs;
610 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
613 // At least one interpolation mode must be enabled or else the GPU will hang.
614 if (Info->getShaderType() == ShaderType::PIXEL &&
615 (Info->PSInputAddr & 0x7F) == 0) {
616 Info->PSInputAddr |= 1;
617 CCInfo.AllocateReg(AMDGPU::VGPR0);
618 CCInfo.AllocateReg(AMDGPU::VGPR1);
621 // The pointer to the list of arguments is stored in SGPR0, SGPR1
622 // The pointer to the scratch buffer is stored in SGPR2, SGPR3
623 if (Info->getShaderType() == ShaderType::COMPUTE) {
624 if (Subtarget->isAmdHsaOS())
625 Info->NumUserSGPRs = 2; // FIXME: Need to support scratch buffers.
627 Info->NumUserSGPRs = 4;
629 unsigned InputPtrReg =
630 TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
631 unsigned InputPtrRegLo =
632 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 0);
633 unsigned InputPtrRegHi =
634 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 1);
636 unsigned ScratchPtrReg =
637 TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_PTR);
638 unsigned ScratchPtrRegLo =
639 TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 0);
640 unsigned ScratchPtrRegHi =
641 TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 1);
643 CCInfo.AllocateReg(InputPtrRegLo);
644 CCInfo.AllocateReg(InputPtrRegHi);
645 CCInfo.AllocateReg(ScratchPtrRegLo);
646 CCInfo.AllocateReg(ScratchPtrRegHi);
647 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
648 MF.addLiveIn(ScratchPtrReg, &AMDGPU::SReg_64RegClass);
649 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
650 if (Subtarget->isAmdHsaOS() && MFI->hasDispatchPtr()) {
651 unsigned DispatchPtrReg =
652 TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_PTR);
653 unsigned DispatchPtrRegLo =
654 TRI->getPhysRegSubReg(DispatchPtrReg, &AMDGPU::SReg_32RegClass, 0);
655 unsigned DispatchPtrRegHi =
656 TRI->getPhysRegSubReg(DispatchPtrReg, &AMDGPU::SReg_32RegClass, 1);
657 CCInfo.AllocateReg(DispatchPtrRegLo);
658 CCInfo.AllocateReg(DispatchPtrRegHi);
659 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
663 if (Info->getShaderType() == ShaderType::COMPUTE) {
664 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
668 AnalyzeFormalArguments(CCInfo, Splits);
670 SmallVector<SDValue, 16> Chains;
672 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
674 const ISD::InputArg &Arg = Ins[i];
676 InVals.push_back(DAG.getUNDEF(Arg.VT));
680 CCValAssign &VA = ArgLocs[ArgIdx++];
681 MVT VT = VA.getLocVT();
685 EVT MemVT = Splits[i].VT;
686 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
687 VA.getLocMemOffset();
688 // The first 36 bytes of the input buffer contains information about
689 // thread group and global sizes.
690 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
691 Offset, Ins[i].Flags.isSExt());
692 Chains.push_back(Arg.getValue(1));
695 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
696 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
697 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
698 // On SI local pointers are just offsets into LDS, so they are always
699 // less than 16-bits. On CI and newer they could potentially be
700 // real pointers, so we can't guarantee their size.
701 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
702 DAG.getValueType(MVT::i16));
705 InVals.push_back(Arg);
706 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
709 assert(VA.isRegLoc() && "Parameter must be in a register!");
711 unsigned Reg = VA.getLocReg();
713 if (VT == MVT::i64) {
714 // For now assume it is a pointer
715 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
716 &AMDGPU::SReg_64RegClass);
717 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
718 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
719 InVals.push_back(Copy);
723 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
725 Reg = MF.addLiveIn(Reg, RC);
726 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
728 if (Arg.VT.isVector()) {
730 // Build a vector from the registers
731 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
732 unsigned NumElements = ParamType->getVectorNumElements();
734 SmallVector<SDValue, 4> Regs;
736 for (unsigned j = 1; j != NumElements; ++j) {
737 Reg = ArgLocs[ArgIdx++].getLocReg();
738 Reg = MF.addLiveIn(Reg, RC);
740 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
741 Regs.push_back(Copy);
744 // Fill up the missing vector elements
745 NumElements = Arg.VT.getVectorNumElements() - NumElements;
746 Regs.append(NumElements, DAG.getUNDEF(VT));
748 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
752 InVals.push_back(Val);
755 if (Info->getShaderType() != ShaderType::COMPUTE) {
756 unsigned ScratchIdx = CCInfo.getFirstUnallocated(makeArrayRef(
757 AMDGPU::SGPR_32RegClass.begin(), AMDGPU::SGPR_32RegClass.getNumRegs()));
758 Info->ScratchOffsetReg = AMDGPU::SGPR_32RegClass.getRegister(ScratchIdx);
764 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
767 MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
768 MachineInstr * MI, MachineBasicBlock * BB) const {
770 MachineBasicBlock::iterator I = *MI;
771 const SIInstrInfo *TII =
772 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
774 switch (MI->getOpcode()) {
776 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
779 case AMDGPU::SI_RegisterStorePseudo: {
780 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
781 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
782 MachineInstrBuilder MIB =
783 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
785 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
786 MIB.addOperand(MI->getOperand(i));
788 MI->eraseFromParent();
795 bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
796 // This currently forces unfolding various combinations of fsub into fma with
797 // free fneg'd operands. As long as we have fast FMA (controlled by
798 // isFMAFasterThanFMulAndFAdd), we should perform these.
800 // When fma is quarter rate, for f64 where add / sub are at best half rate,
801 // most of these combines appear to be cycle neutral but save on instruction
802 // count / code size.
806 EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
808 if (!VT.isVector()) {
811 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
814 MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
818 // Answering this is somewhat tricky and depends on the specific device which
819 // have different rates for fma or all f64 operations.
821 // v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
822 // regardless of which device (although the number of cycles differs between
823 // devices), so it is always profitable for f64.
825 // v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
826 // only on full rate devices. Normally, we should prefer selecting v_mad_f32
827 // which we can always do even without fused FP ops since it returns the same
828 // result as the separate operations and since it is always full
829 // rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
830 // however does not support denormals, so we do report fma as faster if we have
831 // a fast fma device and require denormals.
833 bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
834 VT = VT.getScalarType();
839 switch (VT.getSimpleVT().SimpleTy) {
841 // This is as fast on some subtargets. However, we always have full rate f32
842 // mad available which returns the same result as the separate operations
843 // which we should prefer over fma. We can't use this if we want to support
844 // denormals, so only report this in these cases.
845 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
855 //===----------------------------------------------------------------------===//
856 // Custom DAG Lowering Operations
857 //===----------------------------------------------------------------------===//
859 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
860 switch (Op.getOpcode()) {
861 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
862 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
863 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
865 SDValue Result = LowerLOAD(Op, DAG);
866 assert((!Result.getNode() ||
867 Result.getNode()->getNumValues() == 2) &&
868 "Load should return a value and a chain");
874 return LowerTrig(Op, DAG);
875 case ISD::SELECT: return LowerSELECT(Op, DAG);
876 case ISD::FDIV: return LowerFDIV(Op, DAG);
877 case ISD::STORE: return LowerSTORE(Op, DAG);
878 case ISD::GlobalAddress: {
879 MachineFunction &MF = DAG.getMachineFunction();
880 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
881 return LowerGlobalAddress(MFI, Op, DAG);
883 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
884 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
889 /// \brief Helper function for LowerBRCOND
890 static SDNode *findUser(SDValue Value, unsigned Opcode) {
892 SDNode *Parent = Value.getNode();
893 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
896 if (I.getUse().get() != Value)
899 if (I->getOpcode() == Opcode)
905 SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
908 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
909 unsigned FrameIndex = FINode->getIndex();
911 // A FrameIndex node represents a 32-bit offset into scratch memory. If
912 // the high bit of a frame index offset were to be set, this would mean
913 // that it represented an offset of ~2GB * 64 = ~128GB from the start of the
914 // scratch buffer, with 64 being the number of threads per wave.
916 // If we know the machine uses less than 128GB of scratch, then we can
917 // amrk the high bit of the FrameIndex node as known zero,
918 // which is important, because it means in most situations we can
919 // prove that values derived from FrameIndex nodes are non-negative.
920 // This enables us to take advantage of more addressing modes when
921 // accessing scratch buffers, since for scratch reads/writes, the register
922 // offset must always be positive.
924 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
925 if (Subtarget->enableHugeScratchBuffer())
928 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
929 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), 31)));
932 /// This transforms the control flow intrinsics to get the branch destination as
933 /// last parameter, also switches branch target with BR if the need arise
934 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
935 SelectionDAG &DAG) const {
939 SDNode *Intr = BRCOND.getOperand(1).getNode();
940 SDValue Target = BRCOND.getOperand(2);
941 SDNode *BR = nullptr;
943 if (Intr->getOpcode() == ISD::SETCC) {
944 // As long as we negate the condition everything is fine
945 SDNode *SetCC = Intr;
946 assert(SetCC->getConstantOperandVal(1) == 1);
947 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
949 Intr = SetCC->getOperand(0).getNode();
952 // Get the target from BR if we don't negate the condition
953 BR = findUser(BRCOND, ISD::BR);
954 Target = BR->getOperand(1);
957 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
959 // Build the result and
960 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
962 // operands of the new intrinsic call
963 SmallVector<SDValue, 4> Ops;
964 Ops.push_back(BRCOND.getOperand(0));
965 Ops.append(Intr->op_begin() + 1, Intr->op_end());
966 Ops.push_back(Target);
968 // build the new intrinsic call
969 SDNode *Result = DAG.getNode(
970 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
971 DAG.getVTList(Res), Ops).getNode();
974 // Give the branch instruction our target
979 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
980 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
981 BR = NewBR.getNode();
984 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
986 // Copy the intrinsic results to registers
987 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
988 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
992 Chain = DAG.getCopyToReg(
994 CopyToReg->getOperand(1),
995 SDValue(Result, i - 1),
998 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1001 // Remove the old intrinsic from the chain
1002 DAG.ReplaceAllUsesOfValueWith(
1003 SDValue(Intr, Intr->getNumValues() - 1),
1004 Intr->getOperand(0));
1009 SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1011 SelectionDAG &DAG) const {
1012 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1014 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
1015 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1018 const GlobalValue *GV = GSD->getGlobal();
1019 MVT PtrVT = getPointerTy(DAG.getDataLayout(), GSD->getAddressSpace());
1021 SDValue Ptr = DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT);
1022 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
1024 SDValue PtrLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
1025 DAG.getConstant(0, DL, MVT::i32));
1026 SDValue PtrHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
1027 DAG.getConstant(1, DL, MVT::i32));
1029 SDValue Lo = DAG.getNode(ISD::ADDC, DL, DAG.getVTList(MVT::i32, MVT::Glue),
1031 SDValue Hi = DAG.getNode(ISD::ADDE, DL, DAG.getVTList(MVT::i32, MVT::Glue),
1032 PtrHi, DAG.getConstant(0, DL, MVT::i32),
1033 SDValue(Lo.getNode(), 1));
1034 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1037 SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL,
1039 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1040 // so we will end up with redundant moves to m0.
1042 // We can't use S_MOV_B32, because there is no way to specify m0 as the
1043 // destination register.
1045 // We have to use them both. Machine cse will combine all the S_MOV_B32
1046 // instructions and the register coalescer eliminate the extra copies.
1047 SDNode *M0 = DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, V.getValueType(), V);
1048 return DAG.getCopyToReg(Chain, DL, DAG.getRegister(AMDGPU::M0, MVT::i32),
1049 SDValue(M0, 0), SDValue()); // Glue
1050 // A Null SDValue creates
1054 SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1055 SelectionDAG &DAG) const {
1056 MachineFunction &MF = DAG.getMachineFunction();
1057 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
1058 const SIRegisterInfo *TRI =
1059 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
1061 EVT VT = Op.getValueType();
1063 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1065 // TODO: Should this propagate fast-math-flags?
1067 switch (IntrinsicID) {
1068 case Intrinsic::amdgcn_dispatch_ptr:
1069 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
1070 TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_PTR), VT);
1072 case Intrinsic::r600_read_ngroups_x:
1073 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1074 SI::KernelInputOffsets::NGROUPS_X, false);
1075 case Intrinsic::r600_read_ngroups_y:
1076 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1077 SI::KernelInputOffsets::NGROUPS_Y, false);
1078 case Intrinsic::r600_read_ngroups_z:
1079 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1080 SI::KernelInputOffsets::NGROUPS_Z, false);
1081 case Intrinsic::r600_read_global_size_x:
1082 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1083 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
1084 case Intrinsic::r600_read_global_size_y:
1085 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1086 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
1087 case Intrinsic::r600_read_global_size_z:
1088 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1089 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
1090 case Intrinsic::r600_read_local_size_x:
1091 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1092 SI::KernelInputOffsets::LOCAL_SIZE_X, false);
1093 case Intrinsic::r600_read_local_size_y:
1094 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1095 SI::KernelInputOffsets::LOCAL_SIZE_Y, false);
1096 case Intrinsic::r600_read_local_size_z:
1097 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1098 SI::KernelInputOffsets::LOCAL_SIZE_Z, false);
1100 case Intrinsic::AMDGPU_read_workdim:
1101 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1102 getImplicitParameterOffset(MFI, GRID_DIM), false);
1104 case Intrinsic::r600_read_tgid_x:
1105 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
1106 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_X), VT);
1107 case Intrinsic::r600_read_tgid_y:
1108 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
1109 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Y), VT);
1110 case Intrinsic::r600_read_tgid_z:
1111 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
1112 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Z), VT);
1113 case Intrinsic::r600_read_tidig_x:
1114 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
1115 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_X), VT);
1116 case Intrinsic::r600_read_tidig_y:
1117 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
1118 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Y), VT);
1119 case Intrinsic::r600_read_tidig_z:
1120 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
1121 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Z), VT);
1122 case AMDGPUIntrinsic::SI_load_const: {
1128 MachineMemOperand *MMO = MF.getMachineMemOperand(
1129 MachinePointerInfo(),
1130 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
1131 VT.getStoreSize(), 4);
1132 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
1133 Op->getVTList(), Ops, VT, MMO);
1135 case AMDGPUIntrinsic::SI_sample:
1136 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
1137 case AMDGPUIntrinsic::SI_sampleb:
1138 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
1139 case AMDGPUIntrinsic::SI_sampled:
1140 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
1141 case AMDGPUIntrinsic::SI_samplel:
1142 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
1143 case AMDGPUIntrinsic::SI_vs_load_input:
1144 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
1149 case AMDGPUIntrinsic::AMDGPU_fract:
1150 case AMDGPUIntrinsic::AMDIL_fraction: // Legacy name.
1151 return DAG.getNode(ISD::FSUB, DL, VT, Op.getOperand(1),
1152 DAG.getNode(ISD::FFLOOR, DL, VT, Op.getOperand(1)));
1153 case AMDGPUIntrinsic::SI_fs_constant: {
1154 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1155 SDValue Glue = M0.getValue(1);
1156 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
1157 DAG.getConstant(2, DL, MVT::i32), // P0
1158 Op.getOperand(1), Op.getOperand(2), Glue);
1160 case AMDGPUIntrinsic::SI_packf16:
1161 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
1162 return DAG.getUNDEF(MVT::i32);
1164 case AMDGPUIntrinsic::SI_fs_interp: {
1165 SDValue IJ = Op.getOperand(4);
1166 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1167 DAG.getConstant(0, DL, MVT::i32));
1168 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1169 DAG.getConstant(1, DL, MVT::i32));
1170 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1171 SDValue Glue = M0.getValue(1);
1172 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
1173 DAG.getVTList(MVT::f32, MVT::Glue),
1174 I, Op.getOperand(1), Op.getOperand(2), Glue);
1175 Glue = SDValue(P1.getNode(), 1);
1176 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
1177 Op.getOperand(1), Op.getOperand(2), Glue);
1180 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1184 SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1185 SelectionDAG &DAG) const {
1186 MachineFunction &MF = DAG.getMachineFunction();
1188 SDValue Chain = Op.getOperand(0);
1189 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1191 switch (IntrinsicID) {
1192 case AMDGPUIntrinsic::SI_sendmsg: {
1193 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
1194 SDValue Glue = Chain.getValue(1);
1195 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
1196 Op.getOperand(2), Glue);
1198 case AMDGPUIntrinsic::SI_tbuffer_store: {
1216 EVT VT = Op.getOperand(3).getValueType();
1218 MachineMemOperand *MMO = MF.getMachineMemOperand(
1219 MachinePointerInfo(),
1220 MachineMemOperand::MOStore,
1221 VT.getStoreSize(), 4);
1222 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1223 Op->getVTList(), Ops, VT, MMO);
1230 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1232 LoadSDNode *Load = cast<LoadSDNode>(Op);
1234 if (Op.getValueType().isVector()) {
1235 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1236 "Custom lowering for non-i32 vectors hasn't been implemented.");
1237 unsigned NumElements = Op.getValueType().getVectorNumElements();
1238 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
1240 switch (Load->getAddressSpace()) {
1242 case AMDGPUAS::GLOBAL_ADDRESS:
1243 case AMDGPUAS::PRIVATE_ADDRESS:
1244 if (NumElements >= 8)
1245 return SplitVectorLoad(Op, DAG);
1247 // v4 loads are supported for private and global memory.
1248 if (NumElements <= 4)
1251 case AMDGPUAS::LOCAL_ADDRESS:
1252 // If properly aligned, if we split we might be able to use ds_read_b64.
1253 return SplitVectorLoad(Op, DAG);
1257 return AMDGPUTargetLowering::LowerLOAD(Op, DAG);
1260 SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
1262 SelectionDAG &DAG) const {
1263 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
1269 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1270 if (Op.getValueType() != MVT::i64)
1274 SDValue Cond = Op.getOperand(0);
1276 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1277 SDValue One = DAG.getConstant(1, DL, MVT::i32);
1279 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1280 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1282 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1283 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
1285 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1287 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1288 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
1290 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1292 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1293 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
1296 // Catch division cases where we can use shortcuts with rcp and rsq
1298 SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
1300 SDValue LHS = Op.getOperand(0);
1301 SDValue RHS = Op.getOperand(1);
1302 EVT VT = Op.getValueType();
1303 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
1305 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
1306 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1307 CLHS->isExactlyValue(1.0)) {
1308 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1309 // the CI documentation has a worst case error of 1 ulp.
1310 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1311 // use it as long as we aren't trying to use denormals.
1313 // 1.0 / sqrt(x) -> rsq(x)
1315 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1316 // error seems really high at 2^29 ULP.
1317 if (RHS.getOpcode() == ISD::FSQRT)
1318 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1320 // 1.0 / x -> rcp(x)
1321 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1326 // Turn into multiply by the reciprocal.
1327 // x / y -> x * (1.0 / y)
1329 Flags.setUnsafeAlgebra(true);
1330 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1331 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
1337 SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
1338 SDValue FastLowered = LowerFastFDIV(Op, DAG);
1339 if (FastLowered.getNode())
1342 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1343 // selection error for now rather than do something incorrect.
1344 if (Subtarget->hasFP32Denormals())
1348 SDValue LHS = Op.getOperand(0);
1349 SDValue RHS = Op.getOperand(1);
1351 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1353 const APFloat K0Val(BitsToFloat(0x6f800000));
1354 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
1356 const APFloat K1Val(BitsToFloat(0x2f800000));
1357 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
1359 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
1362 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
1364 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1366 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1368 // TODO: Should this propagate fast-math-flags?
1370 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1372 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1374 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1376 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1379 SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
1380 if (DAG.getTarget().Options.UnsafeFPMath)
1381 return LowerFastFDIV(Op, DAG);
1384 SDValue X = Op.getOperand(0);
1385 SDValue Y = Op.getOperand(1);
1387 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
1389 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
1391 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
1393 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
1395 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
1397 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
1399 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
1401 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
1403 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
1405 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
1406 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
1408 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
1409 NegDivScale0, Mul, DivScale1);
1413 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1414 // Workaround a hardware bug on SI where the condition output from div_scale
1417 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
1419 // Figure out if the scale to use for div_fmas.
1420 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1421 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
1422 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
1423 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
1425 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
1426 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
1429 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
1431 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
1433 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
1434 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
1435 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
1437 Scale = DivScale1.getValue(1);
1440 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
1441 Fma4, Fma3, Mul, Scale);
1443 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
1446 SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1447 EVT VT = Op.getValueType();
1450 return LowerFDIV32(Op, DAG);
1453 return LowerFDIV64(Op, DAG);
1455 llvm_unreachable("Unexpected type for fdiv");
1458 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1460 StoreSDNode *Store = cast<StoreSDNode>(Op);
1461 EVT VT = Store->getMemoryVT();
1463 // These stores are legal.
1464 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1465 if (VT.isVector() && VT.getVectorNumElements() > 4)
1466 return ScalarizeVectorStore(Op, DAG);
1470 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1474 if (VT.isVector() && VT.getVectorNumElements() >= 8)
1475 return SplitVectorStore(Op, DAG);
1478 return DAG.getTruncStore(Store->getChain(), DL,
1479 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1480 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
1485 SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
1487 EVT VT = Op.getValueType();
1488 SDValue Arg = Op.getOperand(0);
1489 // TODO: Should this propagate fast-math-flags?
1490 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
1491 DAG.getNode(ISD::FMUL, DL, VT, Arg,
1492 DAG.getConstantFP(0.5/M_PI, DL,
1495 switch (Op.getOpcode()) {
1497 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
1499 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
1501 llvm_unreachable("Wrong trig opcode");
1505 //===----------------------------------------------------------------------===//
1506 // Custom DAG optimizations
1507 //===----------------------------------------------------------------------===//
1509 SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
1510 DAGCombinerInfo &DCI) const {
1511 EVT VT = N->getValueType(0);
1512 EVT ScalarVT = VT.getScalarType();
1513 if (ScalarVT != MVT::f32)
1516 SelectionDAG &DAG = DCI.DAG;
1519 SDValue Src = N->getOperand(0);
1520 EVT SrcVT = Src.getValueType();
1522 // TODO: We could try to match extracting the higher bytes, which would be
1523 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1524 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1525 // about in practice.
1526 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1527 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1528 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1529 DCI.AddToWorklist(Cvt.getNode());
1534 // We are primarily trying to catch operations on illegal vector types
1535 // before they are expanded.
1536 // For scalars, we can use the more flexible method of checking masked bits
1537 // after legalization.
1538 if (!DCI.isBeforeLegalize() ||
1539 !SrcVT.isVector() ||
1540 SrcVT.getVectorElementType() != MVT::i8) {
1544 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1546 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1548 unsigned NElts = SrcVT.getVectorNumElements();
1549 if (!SrcVT.isSimple() && NElts != 3)
1552 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1553 // prevent a mess from expanding to v4i32 and repacking.
1554 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1555 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1556 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1557 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
1558 LoadSDNode *Load = cast<LoadSDNode>(Src);
1560 unsigned AS = Load->getAddressSpace();
1561 unsigned Align = Load->getAlignment();
1562 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
1563 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty);
1565 // Don't try to replace the load if we have to expand it due to alignment
1566 // problems. Otherwise we will end up scalarizing the load, and trying to
1567 // repack into the vector for no real reason.
1568 if (Align < ABIAlignment &&
1569 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
1573 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1577 Load->getMemOperand());
1579 // Make sure successors of the original load stay after it by updating
1580 // them to use the new Chain.
1581 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1583 SmallVector<SDValue, 4> Elts;
1584 if (RegVT.isVector())
1585 DAG.ExtractVectorElements(NewLoad, Elts);
1587 Elts.push_back(NewLoad);
1589 SmallVector<SDValue, 4> Ops;
1591 unsigned EltIdx = 0;
1592 for (SDValue Elt : Elts) {
1593 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1594 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1595 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1596 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1597 DCI.AddToWorklist(Cvt.getNode());
1604 assert(Ops.size() == NElts);
1606 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1612 /// \brief Return true if the given offset Size in bytes can be folded into
1613 /// the immediate offsets of a memory instruction for the given address space.
1614 static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
1615 const AMDGPUSubtarget &STI) {
1617 case AMDGPUAS::GLOBAL_ADDRESS: {
1618 // MUBUF instructions a 12-bit offset in bytes.
1619 return isUInt<12>(OffsetSize);
1621 case AMDGPUAS::CONSTANT_ADDRESS: {
1622 // SMRD instructions have an 8-bit offset in dwords on SI and
1623 // a 20-bit offset in bytes on VI.
1624 if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
1625 return isUInt<20>(OffsetSize);
1627 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
1629 case AMDGPUAS::LOCAL_ADDRESS:
1630 case AMDGPUAS::REGION_ADDRESS: {
1631 // The single offset versions have a 16-bit offset in bytes.
1632 return isUInt<16>(OffsetSize);
1634 case AMDGPUAS::PRIVATE_ADDRESS:
1635 // Indirect register addressing does not use any offsets.
1641 // (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
1643 // This is a variant of
1644 // (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
1646 // The normal DAG combiner will do this, but only if the add has one use since
1647 // that would increase the number of instructions.
1649 // This prevents us from seeing a constant offset that can be folded into a
1650 // memory instruction's addressing mode. If we know the resulting add offset of
1651 // a pointer can be folded into an addressing offset, we can replace the pointer
1652 // operand with the add of new constant offset. This eliminates one of the uses,
1653 // and may allow the remaining use to also be simplified.
1655 SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
1657 DAGCombinerInfo &DCI) const {
1658 SDValue N0 = N->getOperand(0);
1659 SDValue N1 = N->getOperand(1);
1661 if (N0.getOpcode() != ISD::ADD)
1664 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
1668 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1672 // If the resulting offset is too large, we can't fold it into the addressing
1674 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
1675 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget))
1678 SelectionDAG &DAG = DCI.DAG;
1680 EVT VT = N->getValueType(0);
1682 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
1683 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
1685 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
1688 SDValue SITargetLowering::performAndCombine(SDNode *N,
1689 DAGCombinerInfo &DCI) const {
1690 if (DCI.isBeforeLegalize())
1693 SelectionDAG &DAG = DCI.DAG;
1695 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
1696 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
1697 SDValue LHS = N->getOperand(0);
1698 SDValue RHS = N->getOperand(1);
1700 if (LHS.getOpcode() == ISD::SETCC &&
1701 RHS.getOpcode() == ISD::SETCC) {
1702 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
1703 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
1705 SDValue X = LHS.getOperand(0);
1706 SDValue Y = RHS.getOperand(0);
1707 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
1710 if (LCC == ISD::SETO) {
1711 if (X != LHS.getOperand(1))
1714 if (RCC == ISD::SETUNE) {
1715 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
1716 if (!C1 || !C1->isInfinity() || C1->isNegative())
1719 const uint32_t Mask = SIInstrFlags::N_NORMAL |
1720 SIInstrFlags::N_SUBNORMAL |
1721 SIInstrFlags::N_ZERO |
1722 SIInstrFlags::P_ZERO |
1723 SIInstrFlags::P_SUBNORMAL |
1724 SIInstrFlags::P_NORMAL;
1726 static_assert(((~(SIInstrFlags::S_NAN |
1727 SIInstrFlags::Q_NAN |
1728 SIInstrFlags::N_INFINITY |
1729 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
1733 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
1734 X, DAG.getConstant(Mask, DL, MVT::i32));
1742 SDValue SITargetLowering::performOrCombine(SDNode *N,
1743 DAGCombinerInfo &DCI) const {
1744 SelectionDAG &DAG = DCI.DAG;
1745 SDValue LHS = N->getOperand(0);
1746 SDValue RHS = N->getOperand(1);
1748 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
1749 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
1750 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
1751 SDValue Src = LHS.getOperand(0);
1752 if (Src != RHS.getOperand(0))
1755 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
1756 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
1760 // Only 10 bits are used.
1761 static const uint32_t MaxMask = 0x3ff;
1763 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
1765 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
1766 Src, DAG.getConstant(NewMask, DL, MVT::i32));
1772 SDValue SITargetLowering::performClassCombine(SDNode *N,
1773 DAGCombinerInfo &DCI) const {
1774 SelectionDAG &DAG = DCI.DAG;
1775 SDValue Mask = N->getOperand(1);
1777 // fp_class x, 0 -> false
1778 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
1779 if (CMask->isNullValue())
1780 return DAG.getConstant(0, SDLoc(N), MVT::i1);
1786 static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
1789 return AMDGPUISD::FMAX3;
1791 return AMDGPUISD::SMAX3;
1793 return AMDGPUISD::UMAX3;
1795 return AMDGPUISD::FMIN3;
1797 return AMDGPUISD::SMIN3;
1799 return AMDGPUISD::UMIN3;
1801 llvm_unreachable("Not a min/max opcode");
1805 SDValue SITargetLowering::performMin3Max3Combine(SDNode *N,
1806 DAGCombinerInfo &DCI) const {
1807 SelectionDAG &DAG = DCI.DAG;
1809 unsigned Opc = N->getOpcode();
1810 SDValue Op0 = N->getOperand(0);
1811 SDValue Op1 = N->getOperand(1);
1813 // Only do this if the inner op has one use since this will just increases
1814 // register pressure for no benefit.
1816 // max(max(a, b), c)
1817 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
1819 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1827 // max(a, max(b, c))
1828 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
1830 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1841 SDValue SITargetLowering::performSetCCCombine(SDNode *N,
1842 DAGCombinerInfo &DCI) const {
1843 SelectionDAG &DAG = DCI.DAG;
1846 SDValue LHS = N->getOperand(0);
1847 SDValue RHS = N->getOperand(1);
1848 EVT VT = LHS.getValueType();
1850 if (VT != MVT::f32 && VT != MVT::f64)
1853 // Match isinf pattern
1854 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
1855 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
1856 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
1857 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
1861 const APFloat &APF = CRHS->getValueAPF();
1862 if (APF.isInfinity() && !APF.isNegative()) {
1863 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
1864 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
1865 DAG.getConstant(Mask, SL, MVT::i32));
1872 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1873 DAGCombinerInfo &DCI) const {
1874 SelectionDAG &DAG = DCI.DAG;
1877 switch (N->getOpcode()) {
1879 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1881 return performSetCCCombine(N, DCI);
1882 case ISD::FMAXNUM: // TODO: What about fmax_legacy?
1888 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
1889 N->getValueType(0) != MVT::f64 &&
1890 getTargetMachine().getOptLevel() > CodeGenOpt::None)
1891 return performMin3Max3Combine(N, DCI);
1895 case AMDGPUISD::CVT_F32_UBYTE0:
1896 case AMDGPUISD::CVT_F32_UBYTE1:
1897 case AMDGPUISD::CVT_F32_UBYTE2:
1898 case AMDGPUISD::CVT_F32_UBYTE3: {
1899 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
1901 SDValue Src = N->getOperand(0);
1902 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
1904 APInt KnownZero, KnownOne;
1905 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1906 !DCI.isBeforeLegalizeOps());
1907 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1908 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
1909 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
1910 DCI.CommitTargetLoweringOpt(TLO);
1916 case ISD::UINT_TO_FP: {
1917 return performUCharToFloatCombine(N, DCI);
1920 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1923 EVT VT = N->getValueType(0);
1927 // Only do this if we are not trying to support denormals. v_mad_f32 does
1928 // not support denormals ever.
1929 if (Subtarget->hasFP32Denormals())
1932 SDValue LHS = N->getOperand(0);
1933 SDValue RHS = N->getOperand(1);
1935 // These should really be instruction patterns, but writing patterns with
1936 // source modiifiers is a pain.
1938 // fadd (fadd (a, a), b) -> mad 2.0, a, b
1939 if (LHS.getOpcode() == ISD::FADD) {
1940 SDValue A = LHS.getOperand(0);
1941 if (A == LHS.getOperand(1)) {
1942 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
1943 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
1947 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
1948 if (RHS.getOpcode() == ISD::FADD) {
1949 SDValue A = RHS.getOperand(0);
1950 if (A == RHS.getOperand(1)) {
1951 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
1952 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
1959 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1962 EVT VT = N->getValueType(0);
1964 // Try to get the fneg to fold into the source modifier. This undoes generic
1965 // DAG combines and folds them into the mad.
1967 // Only do this if we are not trying to support denormals. v_mad_f32 does
1968 // not support denormals ever.
1969 if (VT == MVT::f32 &&
1970 !Subtarget->hasFP32Denormals()) {
1971 SDValue LHS = N->getOperand(0);
1972 SDValue RHS = N->getOperand(1);
1973 if (LHS.getOpcode() == ISD::FADD) {
1974 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
1976 SDValue A = LHS.getOperand(0);
1977 if (A == LHS.getOperand(1)) {
1978 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
1979 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
1981 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
1985 if (RHS.getOpcode() == ISD::FADD) {
1986 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
1988 SDValue A = RHS.getOperand(0);
1989 if (A == RHS.getOperand(1)) {
1990 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
1991 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
2003 case ISD::ATOMIC_LOAD:
2004 case ISD::ATOMIC_STORE:
2005 case ISD::ATOMIC_CMP_SWAP:
2006 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
2007 case ISD::ATOMIC_SWAP:
2008 case ISD::ATOMIC_LOAD_ADD:
2009 case ISD::ATOMIC_LOAD_SUB:
2010 case ISD::ATOMIC_LOAD_AND:
2011 case ISD::ATOMIC_LOAD_OR:
2012 case ISD::ATOMIC_LOAD_XOR:
2013 case ISD::ATOMIC_LOAD_NAND:
2014 case ISD::ATOMIC_LOAD_MIN:
2015 case ISD::ATOMIC_LOAD_MAX:
2016 case ISD::ATOMIC_LOAD_UMIN:
2017 case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics.
2018 if (DCI.isBeforeLegalize())
2021 MemSDNode *MemNode = cast<MemSDNode>(N);
2022 SDValue Ptr = MemNode->getBasePtr();
2024 // TODO: We could also do this for multiplies.
2025 unsigned AS = MemNode->getAddressSpace();
2026 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
2027 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
2029 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
2031 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
2032 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
2038 return performAndCombine(N, DCI);
2040 return performOrCombine(N, DCI);
2041 case AMDGPUISD::FP_CLASS:
2042 return performClassCombine(N, DCI);
2044 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
2047 /// \brief Analyze the possible immediate value Op
2049 /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
2050 /// and the immediate value if it's a literal immediate
2051 int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
2053 const SIInstrInfo *TII =
2054 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
2056 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
2057 if (TII->isInlineConstant(Node->getAPIntValue()))
2060 uint64_t Val = Node->getZExtValue();
2061 return isUInt<32>(Val) ? Val : -1;
2064 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
2065 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
2068 if (Node->getValueType(0) == MVT::f32)
2069 return FloatToBits(Node->getValueAPF().convertToFloat());
2077 /// \brief Helper function for adjustWritemask
2078 static unsigned SubIdx2Lane(unsigned Idx) {
2081 case AMDGPU::sub0: return 0;
2082 case AMDGPU::sub1: return 1;
2083 case AMDGPU::sub2: return 2;
2084 case AMDGPU::sub3: return 3;
2088 /// \brief Adjust the writemask of MIMG instructions
2089 void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
2090 SelectionDAG &DAG) const {
2091 SDNode *Users[4] = { };
2093 unsigned OldDmask = Node->getConstantOperandVal(0);
2094 unsigned NewDmask = 0;
2096 // Try to figure out the used register components
2097 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
2100 // Abort if we can't understand the usage
2101 if (!I->isMachineOpcode() ||
2102 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
2105 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
2106 // Note that subregs are packed, i.e. Lane==0 is the first bit set
2107 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
2109 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
2111 // Set which texture component corresponds to the lane.
2113 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
2115 Comp = countTrailingZeros(Dmask);
2116 Dmask &= ~(1 << Comp);
2119 // Abort if we have more than one user per component
2124 NewDmask |= 1 << Comp;
2127 // Abort if there's no change
2128 if (NewDmask == OldDmask)
2131 // Adjust the writemask in the node
2132 std::vector<SDValue> Ops;
2133 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
2134 Ops.insert(Ops.end(), Node->op_begin() + 1, Node->op_end());
2135 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
2137 // If we only got one lane, replace it with a copy
2138 // (if NewDmask has only one bit set...)
2139 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
2140 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
2142 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
2143 SDLoc(), Users[Lane]->getValueType(0),
2144 SDValue(Node, 0), RC);
2145 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
2149 // Update the users of the node with the new indices
2150 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
2152 SDNode *User = Users[i];
2156 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
2157 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
2161 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
2162 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
2163 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
2168 static bool isFrameIndexOp(SDValue Op) {
2169 if (Op.getOpcode() == ISD::AssertZext)
2170 Op = Op.getOperand(0);
2172 return isa<FrameIndexSDNode>(Op);
2175 /// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
2176 /// with frame index operands.
2177 /// LLVM assumes that inputs are to these instructions are registers.
2178 void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
2179 SelectionDAG &DAG) const {
2181 SmallVector<SDValue, 8> Ops;
2182 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
2183 if (!isFrameIndexOp(Node->getOperand(i))) {
2184 Ops.push_back(Node->getOperand(i));
2189 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
2190 Node->getOperand(i).getValueType(),
2191 Node->getOperand(i)), 0));
2194 DAG.UpdateNodeOperands(Node, Ops);
2197 /// \brief Fold the instructions after selecting them.
2198 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
2199 SelectionDAG &DAG) const {
2200 const SIInstrInfo *TII =
2201 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
2203 if (TII->isMIMG(Node->getMachineOpcode()))
2204 adjustWritemask(Node, DAG);
2206 if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG ||
2207 Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) {
2208 legalizeTargetIndependentNode(Node, DAG);
2214 /// \brief Assign the register class depending on the number of
2215 /// bits set in the writemask
2216 void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
2217 SDNode *Node) const {
2218 const SIInstrInfo *TII =
2219 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
2221 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
2223 if (TII->isVOP3(MI->getOpcode())) {
2224 // Make sure constant bus requirements are respected.
2225 TII->legalizeOperandsVOP3(MRI, MI);
2229 if (TII->isMIMG(*MI)) {
2230 unsigned VReg = MI->getOperand(0).getReg();
2231 unsigned Writemask = MI->getOperand(1).getImm();
2232 unsigned BitsSet = 0;
2233 for (unsigned i = 0; i < 4; ++i)
2234 BitsSet += Writemask & (1 << i) ? 1 : 0;
2236 const TargetRegisterClass *RC;
2239 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
2240 case 2: RC = &AMDGPU::VReg_64RegClass; break;
2241 case 3: RC = &AMDGPU::VReg_96RegClass; break;
2244 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
2245 MI->setDesc(TII->get(NewOpcode));
2246 MRI.setRegClass(VReg, RC);
2250 // Replace unused atomics with the no return version.
2251 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
2252 if (NoRetAtomicOp != -1) {
2253 if (!Node->hasAnyUseOfValue(0)) {
2254 MI->setDesc(TII->get(NoRetAtomicOp));
2255 MI->RemoveOperand(0);
2262 static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
2263 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
2264 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
2267 MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
2269 SDValue Ptr) const {
2270 const SIInstrInfo *TII =
2271 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
2273 // Build the half of the subregister with the constants before building the
2274 // full 128-bit register. If we are building multiple resource descriptors,
2275 // this will allow CSEing of the 2-component register.
2276 const SDValue Ops0[] = {
2277 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
2278 buildSMovImm32(DAG, DL, 0),
2279 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
2280 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
2281 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
2284 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
2285 MVT::v2i32, Ops0), 0);
2287 // Combine the constants and the pointer.
2288 const SDValue Ops1[] = {
2289 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
2291 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
2293 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
2296 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
2299 /// \brief Return a resource descriptor with the 'Add TID' bit enabled
2300 /// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
2301 /// of the resource descriptor) to create an offset, which is added to
2302 /// the resource pointer.
2303 MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
2306 uint32_t RsrcDword1,
2307 uint64_t RsrcDword2And3) const {
2308 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
2309 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
2311 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
2312 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
2316 SDValue DataLo = buildSMovImm32(DAG, DL,
2317 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
2318 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
2320 const SDValue Ops[] = {
2321 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
2323 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
2325 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
2327 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
2329 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
2332 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2335 MachineSDNode *SITargetLowering::buildScratchRSRC(SelectionDAG &DAG,
2337 SDValue Ptr) const {
2338 const SIInstrInfo *TII =
2339 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
2341 return buildRSRC(DAG, DL, Ptr, 0, TII->getScratchRsrcWords23());
2344 SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2345 const TargetRegisterClass *RC,
2346 unsigned Reg, EVT VT) const {
2347 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
2349 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
2350 cast<RegisterSDNode>(VReg)->getReg(), VT);
2353 //===----------------------------------------------------------------------===//
2354 // SI Inline Assembly Support
2355 //===----------------------------------------------------------------------===//
2357 std::pair<unsigned, const TargetRegisterClass *>
2358 SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
2359 StringRef Constraint,
2361 if (Constraint == "r") {
2362 switch(VT.SimpleTy) {
2363 default: llvm_unreachable("Unhandled type for 'r' inline asm constraint");
2365 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
2367 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
2371 if (Constraint.size() > 1) {
2372 const TargetRegisterClass *RC = nullptr;
2373 if (Constraint[1] == 'v') {
2374 RC = &AMDGPU::VGPR_32RegClass;
2375 } else if (Constraint[1] == 's') {
2376 RC = &AMDGPU::SGPR_32RegClass;
2381 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
2382 if (!Failed && Idx < RC->getNumRegs())
2383 return std::make_pair(RC->getRegister(Idx), RC);
2386 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);