1d5b43f59545b1dbdf948e77af4e8a201f23b367
[oota-llvm.git] / lib / Target / R600 / SIISelLowering.cpp
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 "AMDGPUIntrinsicInfo.h"
24 #include "AMDGPUSubtarget.h"
25 #include "SIInstrInfo.h"
26 #include "SIMachineFunctionInfo.h"
27 #include "SIRegisterInfo.h"
28 #include "llvm/CodeGen/CallingConvLower.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/SelectionDAG.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/ADT/SmallString.h"
34
35 using namespace llvm;
36
37 SITargetLowering::SITargetLowering(TargetMachine &TM) :
38     AMDGPUTargetLowering(TM) {
39   addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
40   addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
41
42   addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
43   addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
44
45   addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
46   addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass);
47
48   addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
49   addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
50   addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
51
52   addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
53   addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
54
55   addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass);
56   addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
57
58   addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass);
59   addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
60
61   computeRegisterProperties();
62
63   // Condition Codes
64   setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
65   setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
66   setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
67   setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
68   setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
69   setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
70
71   setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
72   setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
73   setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
74   setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
75   setCondCodeAction(ISD::SETULE, MVT::f64, Expand);
76   setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
77
78   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
79   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
80   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
81   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
82
83   setOperationAction(ISD::ADD, MVT::i32, Legal);
84   setOperationAction(ISD::ADDC, MVT::i32, Legal);
85   setOperationAction(ISD::ADDE, MVT::i32, Legal);
86   setOperationAction(ISD::SUBC, MVT::i32, Legal);
87   setOperationAction(ISD::SUBE, MVT::i32, Legal);
88
89   setOperationAction(ISD::FSIN, MVT::f32, Custom);
90   setOperationAction(ISD::FCOS, MVT::f32, Custom);
91
92   // We need to custom lower vector stores from local memory
93   setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
94   setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
95   setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
96
97   setOperationAction(ISD::STORE, MVT::v8i32, Custom);
98   setOperationAction(ISD::STORE, MVT::v16i32, Custom);
99
100   setOperationAction(ISD::STORE, MVT::i1, Custom);
101   setOperationAction(ISD::STORE, MVT::i32, Custom);
102   setOperationAction(ISD::STORE, MVT::v2i32, Custom);
103   setOperationAction(ISD::STORE, MVT::v4i32, Custom);
104
105   setOperationAction(ISD::SELECT, MVT::f32, Promote);
106   AddPromotedToType(ISD::SELECT, MVT::f32, MVT::i32);
107   setOperationAction(ISD::SELECT, MVT::i64, Custom);
108   setOperationAction(ISD::SELECT, MVT::f64, Promote);
109   AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
110
111   setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
112   setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
113   setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
114   setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
115
116   setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
117   setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
118
119   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
120   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
121   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
122
123   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
124   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
125   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
126
127   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
128   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
129   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
130
131   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Custom);
132
133   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
134
135   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
136   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
137   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
138   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
139
140   setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
141   setOperationAction(ISD::BRCOND, MVT::Other, Custom);
142
143   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
144   setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
145   setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
146   setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Expand);
147   setLoadExtAction(ISD::SEXTLOAD, MVT::v8i16, Expand);
148   setLoadExtAction(ISD::SEXTLOAD, MVT::v16i16, Expand);
149
150   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
151   setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
152   setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
153   setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Expand);
154
155   setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
156   setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
157   setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
158   setLoadExtAction(ISD::EXTLOAD, MVT::i32, Expand);
159   setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
160
161   setTruncStoreAction(MVT::i32, MVT::i8, Custom);
162   setTruncStoreAction(MVT::i32, MVT::i16, Custom);
163   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
164   setTruncStoreAction(MVT::i64, MVT::i32, Expand);
165   setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
166   setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
167
168   setOperationAction(ISD::LOAD, MVT::i1, Custom);
169
170   setOperationAction(ISD::FP_TO_SINT, MVT::i64, Expand);
171   setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
172
173   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
174   setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
175   setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
176
177   // These should use UDIVREM, so set them to expand
178   setOperationAction(ISD::UDIV, MVT::i64, Expand);
179   setOperationAction(ISD::UREM, MVT::i64, Expand);
180
181   // We only support LOAD/STORE and vector manipulation ops for vectors
182   // with > 4 elements.
183   MVT VecTypes[] = {
184     MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32
185   };
186
187   setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
188   setOperationAction(ISD::SELECT, MVT::i1, Promote);
189
190   for (MVT VT : VecTypes) {
191     for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
192       switch(Op) {
193       case ISD::LOAD:
194       case ISD::STORE:
195       case ISD::BUILD_VECTOR:
196       case ISD::BITCAST:
197       case ISD::EXTRACT_VECTOR_ELT:
198       case ISD::INSERT_VECTOR_ELT:
199       case ISD::INSERT_SUBVECTOR:
200       case ISD::EXTRACT_SUBVECTOR:
201         break;
202       case ISD::CONCAT_VECTORS:
203         setOperationAction(Op, VT, Custom);
204         break;
205       default:
206         setOperationAction(Op, VT, Expand);
207         break;
208       }
209     }
210   }
211
212   for (int I = MVT::v1f64; I <= MVT::v8f64; ++I) {
213     MVT::SimpleValueType VT = static_cast<MVT::SimpleValueType>(I);
214     setOperationAction(ISD::FTRUNC, VT, Expand);
215     setOperationAction(ISD::FCEIL, VT, Expand);
216     setOperationAction(ISD::FFLOOR, VT, Expand);
217   }
218
219   if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
220     setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
221     setOperationAction(ISD::FCEIL, MVT::f64, Legal);
222     setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
223     setOperationAction(ISD::FRINT, MVT::f64, Legal);
224   }
225
226   setOperationAction(ISD::FDIV, MVT::f32, Custom);
227
228   setTargetDAGCombine(ISD::SELECT_CC);
229   setTargetDAGCombine(ISD::SETCC);
230
231   setTargetDAGCombine(ISD::UINT_TO_FP);
232
233   // All memory operations. Some folding on the pointer operand is done to help
234   // matching the constant offsets in the addressing modes.
235   setTargetDAGCombine(ISD::LOAD);
236   setTargetDAGCombine(ISD::STORE);
237   setTargetDAGCombine(ISD::ATOMIC_LOAD);
238   setTargetDAGCombine(ISD::ATOMIC_STORE);
239   setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
240   setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
241   setTargetDAGCombine(ISD::ATOMIC_SWAP);
242   setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
243   setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
244   setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
245   setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
246   setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
247   setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
248   setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
249   setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
250   setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
251   setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
252
253   setSchedulingPreference(Sched::RegPressure);
254 }
255
256 //===----------------------------------------------------------------------===//
257 // TargetLowering queries
258 //===----------------------------------------------------------------------===//
259
260 // FIXME: This really needs an address space argument. The immediate offset
261 // size is different for different sets of memory instruction sets.
262
263 // The single offset DS instructions have a 16-bit unsigned byte offset.
264 //
265 // MUBUF / MTBUF have a 12-bit unsigned byte offset, and additionally can do r +
266 // r + i with addr64. 32-bit has more addressing mode options. Depending on the
267 // resource constant, it can also do (i64 r0) + (i32 r1) * (i14 i).
268 //
269 // SMRD instructions have an 8-bit, dword offset.
270 //
271 bool SITargetLowering::isLegalAddressingMode(const AddrMode &AM,
272                                              Type *Ty) const {
273   // No global is ever allowed as a base.
274   if (AM.BaseGV)
275     return false;
276
277   // Allow a 16-bit unsigned immediate field, since this is what DS instructions
278   // use.
279   if (!isUInt<16>(AM.BaseOffs))
280     return false;
281
282   // Only support r+r,
283   switch (AM.Scale) {
284   case 0:  // "r+i" or just "i", depending on HasBaseReg.
285     break;
286   case 1:
287     if (AM.HasBaseReg && AM.BaseOffs)  // "r+r+i" is not allowed.
288       return false;
289     // Otherwise we have r+r or r+i.
290     break;
291   case 2:
292     if (AM.HasBaseReg || AM.BaseOffs)  // 2*r+r  or  2*r+i is not allowed.
293       return false;
294     // Allow 2*r as r+r.
295     break;
296   default: // Don't allow n * r
297     return false;
298   }
299
300   return true;
301 }
302
303 bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT  VT,
304                                                       unsigned AddrSpace,
305                                                       unsigned Align,
306                                                       bool *IsFast) const {
307   if (IsFast)
308     *IsFast = false;
309
310   // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
311   // which isn't a simple VT.
312   if (!VT.isSimple() || VT == MVT::Other)
313     return false;
314
315   // XXX - CI changes say "Support for unaligned memory accesses" but I don't
316   // see what for specifically. The wording everywhere else seems to be the
317   // same.
318
319   // XXX - The only mention I see of this in the ISA manual is for LDS direct
320   // reads the "byte address and must be dword aligned". Is it also true for the
321   // normal loads and stores?
322   if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
323     // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
324     // aligned, 8 byte access in a single operation using ds_read2/write2_b32
325     // with adjacent offsets.
326     return Align % 4 == 0;
327   }
328
329   // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
330   // byte-address are ignored, thus forcing Dword alignment.
331   // This applies to private, global, and constant memory.
332   if (IsFast)
333     *IsFast = true;
334   return VT.bitsGT(MVT::i32);
335 }
336
337 EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
338                                           unsigned SrcAlign, bool IsMemset,
339                                           bool ZeroMemset,
340                                           bool MemcpyStrSrc,
341                                           MachineFunction &MF) const {
342   // FIXME: Should account for address space here.
343
344   // The default fallback uses the private pointer size as a guess for a type to
345   // use. Make sure we switch these to 64-bit accesses.
346
347   if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
348     return MVT::v4i32;
349
350   if (Size >= 8 && DstAlign >= 4)
351     return MVT::v2i32;
352
353   // Use the default.
354   return MVT::Other;
355 }
356
357 TargetLoweringBase::LegalizeTypeAction
358 SITargetLowering::getPreferredVectorAction(EVT VT) const {
359   if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
360     return TypeSplitVector;
361
362   return TargetLoweringBase::getPreferredVectorAction(VT);
363 }
364
365 bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
366                                                          Type *Ty) const {
367   const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
368       getTargetMachine().getSubtargetImpl()->getInstrInfo());
369   return TII->isInlineConstant(Imm);
370 }
371
372 SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
373                                          SDLoc SL, SDValue Chain,
374                                          unsigned Offset, bool Signed) const {
375   const DataLayout *DL = getDataLayout();
376
377   Type *Ty = VT.getTypeForEVT(*DAG.getContext());
378
379   MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
380   PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
381   SDValue BasePtr =  DAG.getCopyFromReg(Chain, SL,
382                            MRI.getLiveInVirtReg(AMDGPU::SGPR0_SGPR1), MVT::i64);
383   SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, BasePtr,
384                                              DAG.getConstant(Offset, MVT::i64));
385   SDValue PtrOffset = DAG.getUNDEF(getPointerTy(AMDGPUAS::CONSTANT_ADDRESS));
386   MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
387
388   return DAG.getLoad(ISD::UNINDEXED, Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD,
389                      VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
390                      false, // isVolatile
391                      true, // isNonTemporal
392                      true, // isInvariant
393                      DL->getABITypeAlignment(Ty)); // Alignment
394 }
395
396 SDValue SITargetLowering::LowerFormalArguments(
397                                       SDValue Chain,
398                                       CallingConv::ID CallConv,
399                                       bool isVarArg,
400                                       const SmallVectorImpl<ISD::InputArg> &Ins,
401                                       SDLoc DL, SelectionDAG &DAG,
402                                       SmallVectorImpl<SDValue> &InVals) const {
403
404   const TargetRegisterInfo *TRI =
405       getTargetMachine().getSubtargetImpl()->getRegisterInfo();
406
407   MachineFunction &MF = DAG.getMachineFunction();
408   FunctionType *FType = MF.getFunction()->getFunctionType();
409   SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
410
411   assert(CallConv == CallingConv::C);
412
413   SmallVector<ISD::InputArg, 16> Splits;
414   uint32_t Skipped = 0;
415
416   for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
417     const ISD::InputArg &Arg = Ins[i];
418
419     // First check if it's a PS input addr
420     if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
421         !Arg.Flags.isByVal()) {
422
423       assert((PSInputNum <= 15) && "Too many PS inputs!");
424
425       if (!Arg.Used) {
426         // We can savely skip PS inputs
427         Skipped |= 1 << i;
428         ++PSInputNum;
429         continue;
430       }
431
432       Info->PSInputAddr |= 1 << PSInputNum++;
433     }
434
435     // Second split vertices into their elements
436     if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
437       ISD::InputArg NewArg = Arg;
438       NewArg.Flags.setSplit();
439       NewArg.VT = Arg.VT.getVectorElementType();
440
441       // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
442       // three or five element vertex only needs three or five registers,
443       // NOT four or eigth.
444       Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
445       unsigned NumElements = ParamType->getVectorNumElements();
446
447       for (unsigned j = 0; j != NumElements; ++j) {
448         Splits.push_back(NewArg);
449         NewArg.PartOffset += NewArg.VT.getStoreSize();
450       }
451
452     } else if (Info->getShaderType() != ShaderType::COMPUTE) {
453       Splits.push_back(Arg);
454     }
455   }
456
457   SmallVector<CCValAssign, 16> ArgLocs;
458   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
459                  *DAG.getContext());
460
461   // At least one interpolation mode must be enabled or else the GPU will hang.
462   if (Info->getShaderType() == ShaderType::PIXEL &&
463       (Info->PSInputAddr & 0x7F) == 0) {
464     Info->PSInputAddr |= 1;
465     CCInfo.AllocateReg(AMDGPU::VGPR0);
466     CCInfo.AllocateReg(AMDGPU::VGPR1);
467   }
468
469   // The pointer to the list of arguments is stored in SGPR0, SGPR1
470         // The pointer to the scratch buffer is stored in SGPR2, SGPR3
471   if (Info->getShaderType() == ShaderType::COMPUTE) {
472     Info->NumUserSGPRs = 4;
473     CCInfo.AllocateReg(AMDGPU::SGPR0);
474     CCInfo.AllocateReg(AMDGPU::SGPR1);
475     CCInfo.AllocateReg(AMDGPU::SGPR2);
476     CCInfo.AllocateReg(AMDGPU::SGPR3);
477     MF.addLiveIn(AMDGPU::SGPR0_SGPR1, &AMDGPU::SReg_64RegClass);
478     MF.addLiveIn(AMDGPU::SGPR2_SGPR3, &AMDGPU::SReg_64RegClass);
479   }
480
481   if (Info->getShaderType() == ShaderType::COMPUTE) {
482     getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
483                             Splits);
484   }
485
486   AnalyzeFormalArguments(CCInfo, Splits);
487
488   for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
489
490     const ISD::InputArg &Arg = Ins[i];
491     if (Skipped & (1 << i)) {
492       InVals.push_back(DAG.getUNDEF(Arg.VT));
493       continue;
494     }
495
496     CCValAssign &VA = ArgLocs[ArgIdx++];
497     EVT VT = VA.getLocVT();
498
499     if (VA.isMemLoc()) {
500       VT = Ins[i].VT;
501       EVT MemVT = Splits[i].VT;
502       // The first 36 bytes of the input buffer contains information about
503       // thread group and global sizes.
504       SDValue Arg = LowerParameter(DAG, VT, MemVT,  DL, DAG.getRoot(),
505                                    36 + VA.getLocMemOffset(),
506                                    Ins[i].Flags.isSExt());
507       InVals.push_back(Arg);
508       continue;
509     }
510     assert(VA.isRegLoc() && "Parameter must be in a register!");
511
512     unsigned Reg = VA.getLocReg();
513
514     if (VT == MVT::i64) {
515       // For now assume it is a pointer
516       Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
517                                      &AMDGPU::SReg_64RegClass);
518       Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
519       InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
520       continue;
521     }
522
523     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
524
525     Reg = MF.addLiveIn(Reg, RC);
526     SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
527
528     if (Arg.VT.isVector()) {
529
530       // Build a vector from the registers
531       Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
532       unsigned NumElements = ParamType->getVectorNumElements();
533
534       SmallVector<SDValue, 4> Regs;
535       Regs.push_back(Val);
536       for (unsigned j = 1; j != NumElements; ++j) {
537         Reg = ArgLocs[ArgIdx++].getLocReg();
538         Reg = MF.addLiveIn(Reg, RC);
539         Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
540       }
541
542       // Fill up the missing vector elements
543       NumElements = Arg.VT.getVectorNumElements() - NumElements;
544       for (unsigned j = 0; j != NumElements; ++j)
545         Regs.push_back(DAG.getUNDEF(VT));
546
547       InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
548       continue;
549     }
550
551     InVals.push_back(Val);
552   }
553   return Chain;
554 }
555
556 MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
557     MachineInstr * MI, MachineBasicBlock * BB) const {
558
559   MachineBasicBlock::iterator I = *MI;
560   const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
561       getTargetMachine().getSubtargetImpl()->getInstrInfo());
562   MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
563
564   switch (MI->getOpcode()) {
565   default:
566     return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
567   case AMDGPU::BRANCH: return BB;
568   case AMDGPU::SI_ADDR64_RSRC: {
569     unsigned SuperReg = MI->getOperand(0).getReg();
570     unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
571     unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
572     unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
573     unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
574     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo)
575             .addOperand(MI->getOperand(1));
576     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo)
577             .addImm(0);
578     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi)
579             .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32);
580     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi)
581             .addReg(SubRegHiLo)
582             .addImm(AMDGPU::sub0)
583             .addReg(SubRegHiHi)
584             .addImm(AMDGPU::sub1);
585     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg)
586             .addReg(SubRegLo)
587             .addImm(AMDGPU::sub0_sub1)
588             .addReg(SubRegHi)
589             .addImm(AMDGPU::sub2_sub3);
590     MI->eraseFromParent();
591     break;
592   }
593   case AMDGPU::SI_BUFFER_RSRC: {
594     unsigned SuperReg = MI->getOperand(0).getReg();
595     unsigned Args[4];
596     for (unsigned i = 0, e = 4; i < e; ++i) {
597       MachineOperand &Arg = MI->getOperand(i + 1);
598
599       if (Arg.isReg()) {
600         Args[i] = Arg.getReg();
601         continue;
602       }
603
604       assert(Arg.isImm());
605       unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
606       BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), Reg)
607               .addImm(Arg.getImm());
608       Args[i] = Reg;
609     }
610     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE),
611             SuperReg)
612             .addReg(Args[0])
613             .addImm(AMDGPU::sub0)
614             .addReg(Args[1])
615             .addImm(AMDGPU::sub1)
616             .addReg(Args[2])
617             .addImm(AMDGPU::sub2)
618             .addReg(Args[3])
619             .addImm(AMDGPU::sub3);
620     MI->eraseFromParent();
621     break;
622   }
623   case AMDGPU::V_SUB_F64: {
624     unsigned DestReg = MI->getOperand(0).getReg();
625     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64), DestReg)
626       .addImm(0)  // SRC0 modifiers
627       .addReg(MI->getOperand(1).getReg())
628       .addImm(1)  // SRC1 modifiers
629       .addReg(MI->getOperand(2).getReg())
630       .addImm(0)  // CLAMP
631       .addImm(0); // OMOD
632     MI->eraseFromParent();
633     break;
634   }
635   case AMDGPU::SI_RegisterStorePseudo: {
636     MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
637     unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
638     MachineInstrBuilder MIB =
639         BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
640                 Reg);
641     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
642       MIB.addOperand(MI->getOperand(i));
643
644     MI->eraseFromParent();
645     break;
646   }
647   case AMDGPU::FABS_SI: {
648     MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
649     const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
650         getTargetMachine().getSubtargetImpl()->getInstrInfo());
651     DebugLoc DL = MI->getDebugLoc();
652     unsigned DestReg = MI->getOperand(0).getReg();
653     unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
654
655     BuildMI(*BB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), Reg)
656       .addImm(0x7fffffff);
657     BuildMI(*BB, I, DL, TII->get(AMDGPU::V_AND_B32_e32), DestReg)
658       .addReg(MI->getOperand(1).getReg())
659       .addReg(Reg);
660     MI->eraseFromParent();
661     break;
662   }
663   case AMDGPU::FABS64_SI: {
664     MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
665     const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
666       getTargetMachine().getSubtargetImpl()->getInstrInfo());
667
668     DebugLoc DL = MI->getDebugLoc();
669     unsigned SuperReg = MI->getOperand(0).getReg();
670     unsigned SrcReg = MI->getOperand(1).getReg();
671
672     unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
673
674     // Copy the subregister to make sure it is the right register class.
675     unsigned VReg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
676     BuildMI(*BB, I, DL, TII->get(AMDGPU::COPY), VReg)
677       .addReg(SrcReg, 0, AMDGPU::sub1);
678
679     // We only need to mask the upper half of the register pair.
680     BuildMI(*BB, I, DL, TII->get(AMDGPU::V_AND_B32_e32), TmpReg)
681       .addImm(0x7fffffff)
682       .addReg(VReg);
683
684     BuildMI(*BB, I, DL, TII->get(AMDGPU::REG_SEQUENCE), SuperReg)
685       .addReg(SrcReg, 0, AMDGPU::sub0)
686       .addImm(AMDGPU::sub0)
687       .addReg(TmpReg)
688       .addImm(AMDGPU::sub1);
689     MI->eraseFromParent();
690     break;
691   }
692   case AMDGPU::FNEG_SI: {
693     MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
694     const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
695         getTargetMachine().getSubtargetImpl()->getInstrInfo());
696     DebugLoc DL = MI->getDebugLoc();
697     unsigned DestReg = MI->getOperand(0).getReg();
698     unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
699
700     // FIXME: Should use SALU instructions
701     BuildMI(*BB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), Reg)
702       .addImm(0x80000000);
703     BuildMI(*BB, I, DL, TII->get(AMDGPU::V_XOR_B32_e32), DestReg)
704       .addReg(MI->getOperand(1).getReg())
705       .addReg(Reg);
706     MI->eraseFromParent();
707     break;
708   }
709   case AMDGPU::FNEG64_SI: {
710     MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
711     const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
712         getTargetMachine().getSubtargetImpl()->getInstrInfo());
713
714     DebugLoc DL = MI->getDebugLoc();
715     unsigned SrcReg = MI->getOperand(1).getReg();
716     unsigned DestReg = MI->getOperand(0).getReg();
717
718     unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
719     unsigned ImmReg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
720
721     // FIXME: Should use SALU instructions
722     BuildMI(*BB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), ImmReg)
723       .addImm(0x80000000);
724     BuildMI(*BB, I, DL, TII->get(AMDGPU::V_XOR_B32_e32), TmpReg)
725       .addReg(SrcReg, 0, AMDGPU::sub1)
726       .addReg(ImmReg);
727
728     BuildMI(*BB, I, DL, TII->get(AMDGPU::REG_SEQUENCE), DestReg)
729       .addReg(SrcReg, 0, AMDGPU::sub0)
730       .addImm(AMDGPU::sub0)
731       .addReg(TmpReg)
732       .addImm(AMDGPU::sub1);
733     MI->eraseFromParent();
734     break;
735   }
736   case AMDGPU::FCLAMP_SI: {
737     const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
738         getTargetMachine().getSubtargetImpl()->getInstrInfo());
739     DebugLoc DL = MI->getDebugLoc();
740     unsigned DestReg = MI->getOperand(0).getReg();
741     BuildMI(*BB, I, DL, TII->get(AMDGPU::V_ADD_F32_e64), DestReg)
742       .addImm(0) // SRC0 modifiers
743       .addOperand(MI->getOperand(1))
744       .addImm(0) // SRC1 modifiers
745       .addImm(0) // SRC1
746       .addImm(1) // CLAMP
747       .addImm(0); // OMOD
748     MI->eraseFromParent();
749   }
750   }
751   return BB;
752 }
753
754 EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
755   if (!VT.isVector()) {
756     return MVT::i1;
757   }
758   return MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
759 }
760
761 MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
762   return MVT::i32;
763 }
764
765 bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
766   VT = VT.getScalarType();
767
768   if (!VT.isSimple())
769     return false;
770
771   switch (VT.getSimpleVT().SimpleTy) {
772   case MVT::f32:
773     return false; /* There is V_MAD_F32 for f32 */
774   case MVT::f64:
775     return true;
776   default:
777     break;
778   }
779
780   return false;
781 }
782
783 //===----------------------------------------------------------------------===//
784 // Custom DAG Lowering Operations
785 //===----------------------------------------------------------------------===//
786
787 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
788   switch (Op.getOpcode()) {
789   default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
790   case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
791   case ISD::BRCOND: return LowerBRCOND(Op, DAG);
792   case ISD::LOAD: {
793     SDValue Result = LowerLOAD(Op, DAG);
794     assert((!Result.getNode() ||
795             Result.getNode()->getNumValues() == 2) &&
796            "Load should return a value and a chain");
797     return Result;
798   }
799
800   case ISD::FSIN:
801   case ISD::FCOS:
802     return LowerTrig(Op, DAG);
803   case ISD::SELECT: return LowerSELECT(Op, DAG);
804   case ISD::FDIV: return LowerFDIV(Op, DAG);
805   case ISD::STORE: return LowerSTORE(Op, DAG);
806   case ISD::GlobalAddress: {
807     MachineFunction &MF = DAG.getMachineFunction();
808     SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
809     return LowerGlobalAddress(MFI, Op, DAG);
810   }
811   case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
812   case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
813   }
814   return SDValue();
815 }
816
817 /// \brief Helper function for LowerBRCOND
818 static SDNode *findUser(SDValue Value, unsigned Opcode) {
819
820   SDNode *Parent = Value.getNode();
821   for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
822        I != E; ++I) {
823
824     if (I.getUse().get() != Value)
825       continue;
826
827     if (I->getOpcode() == Opcode)
828       return *I;
829   }
830   return nullptr;
831 }
832
833 SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
834
835   MachineFunction &MF = DAG.getMachineFunction();
836   const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
837       getTargetMachine().getSubtargetImpl()->getInstrInfo());
838   const SIRegisterInfo &TRI = TII->getRegisterInfo();
839   FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
840   unsigned FrameIndex = FINode->getIndex();
841
842   CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
843     TRI.getPreloadedValue(MF, SIRegisterInfo::SCRATCH_WAVE_OFFSET), MVT::i32);
844
845   return DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
846 }
847
848 /// This transforms the control flow intrinsics to get the branch destination as
849 /// last parameter, also switches branch target with BR if the need arise
850 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
851                                       SelectionDAG &DAG) const {
852
853   SDLoc DL(BRCOND);
854
855   SDNode *Intr = BRCOND.getOperand(1).getNode();
856   SDValue Target = BRCOND.getOperand(2);
857   SDNode *BR = nullptr;
858
859   if (Intr->getOpcode() == ISD::SETCC) {
860     // As long as we negate the condition everything is fine
861     SDNode *SetCC = Intr;
862     assert(SetCC->getConstantOperandVal(1) == 1);
863     assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
864            ISD::SETNE);
865     Intr = SetCC->getOperand(0).getNode();
866
867   } else {
868     // Get the target from BR if we don't negate the condition
869     BR = findUser(BRCOND, ISD::BR);
870     Target = BR->getOperand(1);
871   }
872
873   assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
874
875   // Build the result and
876   SmallVector<EVT, 4> Res;
877   for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
878     Res.push_back(Intr->getValueType(i));
879
880   // operands of the new intrinsic call
881   SmallVector<SDValue, 4> Ops;
882   Ops.push_back(BRCOND.getOperand(0));
883   for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
884     Ops.push_back(Intr->getOperand(i));
885   Ops.push_back(Target);
886
887   // build the new intrinsic call
888   SDNode *Result = DAG.getNode(
889     Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
890     DAG.getVTList(Res), Ops).getNode();
891
892   if (BR) {
893     // Give the branch instruction our target
894     SDValue Ops[] = {
895       BR->getOperand(0),
896       BRCOND.getOperand(2)
897     };
898     SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
899     DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
900     BR = NewBR.getNode();
901   }
902
903   SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
904
905   // Copy the intrinsic results to registers
906   for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
907     SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
908     if (!CopyToReg)
909       continue;
910
911     Chain = DAG.getCopyToReg(
912       Chain, DL,
913       CopyToReg->getOperand(1),
914       SDValue(Result, i - 1),
915       SDValue());
916
917     DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
918   }
919
920   // Remove the old intrinsic from the chain
921   DAG.ReplaceAllUsesOfValueWith(
922     SDValue(Intr, Intr->getNumValues() - 1),
923     Intr->getOperand(0));
924
925   return Chain;
926 }
927
928 SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
929                                              SDValue Op,
930                                              SelectionDAG &DAG) const {
931   GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
932
933   if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
934     return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
935
936   SDLoc DL(GSD);
937   const GlobalValue *GV = GSD->getGlobal();
938   MVT PtrVT = getPointerTy(GSD->getAddressSpace());
939
940   SDValue Ptr = DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT);
941   SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
942
943   SDValue PtrLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
944                               DAG.getConstant(0, MVT::i32));
945   SDValue PtrHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
946                               DAG.getConstant(1, MVT::i32));
947
948   SDValue Lo = DAG.getNode(ISD::ADDC, DL, DAG.getVTList(MVT::i32, MVT::Glue),
949                            PtrLo, GA);
950   SDValue Hi = DAG.getNode(ISD::ADDE, DL, DAG.getVTList(MVT::i32, MVT::Glue),
951                            PtrHi, DAG.getConstant(0, MVT::i32),
952                            SDValue(Lo.getNode(), 1));
953   return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
954 }
955
956 SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
957                                                   SelectionDAG &DAG) const {
958   MachineFunction &MF = DAG.getMachineFunction();
959   SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
960
961   EVT VT = Op.getValueType();
962   SDLoc DL(Op);
963   unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
964
965   switch (IntrinsicID) {
966   case Intrinsic::r600_read_ngroups_x:
967     return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 0, false);
968   case Intrinsic::r600_read_ngroups_y:
969     return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4, false);
970   case Intrinsic::r600_read_ngroups_z:
971     return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 8, false);
972   case Intrinsic::r600_read_global_size_x:
973     return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 12, false);
974   case Intrinsic::r600_read_global_size_y:
975     return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 16, false);
976   case Intrinsic::r600_read_global_size_z:
977     return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 20, false);
978   case Intrinsic::r600_read_local_size_x:
979     return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 24, false);
980   case Intrinsic::r600_read_local_size_y:
981     return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 28, false);
982   case Intrinsic::r600_read_local_size_z:
983     return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 32, false);
984   case Intrinsic::r600_read_tgid_x:
985     return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
986       AMDGPU::SReg_32RegClass.getRegister(MFI->NumUserSGPRs + 0), VT);
987   case Intrinsic::r600_read_tgid_y:
988     return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
989       AMDGPU::SReg_32RegClass.getRegister(MFI->NumUserSGPRs + 1), VT);
990   case Intrinsic::r600_read_tgid_z:
991     return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
992       AMDGPU::SReg_32RegClass.getRegister(MFI->NumUserSGPRs + 2), VT);
993   case Intrinsic::r600_read_tidig_x:
994     return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
995                                 AMDGPU::VGPR0, VT);
996   case Intrinsic::r600_read_tidig_y:
997     return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
998                                 AMDGPU::VGPR1, VT);
999   case Intrinsic::r600_read_tidig_z:
1000     return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
1001                                 AMDGPU::VGPR2, VT);
1002   case AMDGPUIntrinsic::SI_load_const: {
1003     SDValue Ops[] = {
1004       Op.getOperand(1),
1005       Op.getOperand(2)
1006     };
1007
1008     MachineMemOperand *MMO = MF.getMachineMemOperand(
1009       MachinePointerInfo(),
1010       MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
1011       VT.getStoreSize(), 4);
1012     return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
1013                                    Op->getVTList(), Ops, VT, MMO);
1014   }
1015   case AMDGPUIntrinsic::SI_sample:
1016     return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
1017   case AMDGPUIntrinsic::SI_sampleb:
1018     return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
1019   case AMDGPUIntrinsic::SI_sampled:
1020     return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
1021   case AMDGPUIntrinsic::SI_samplel:
1022     return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
1023   case AMDGPUIntrinsic::SI_vs_load_input:
1024     return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
1025                        Op.getOperand(1),
1026                        Op.getOperand(2),
1027                        Op.getOperand(3));
1028   default:
1029     return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1030   }
1031 }
1032
1033 SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1034                                               SelectionDAG &DAG) const {
1035   MachineFunction &MF = DAG.getMachineFunction();
1036   SDValue Chain = Op.getOperand(0);
1037   unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1038
1039   switch (IntrinsicID) {
1040   case AMDGPUIntrinsic::SI_tbuffer_store: {
1041     SDLoc DL(Op);
1042     SDValue Ops[] = {
1043       Chain,
1044       Op.getOperand(2),
1045       Op.getOperand(3),
1046       Op.getOperand(4),
1047       Op.getOperand(5),
1048       Op.getOperand(6),
1049       Op.getOperand(7),
1050       Op.getOperand(8),
1051       Op.getOperand(9),
1052       Op.getOperand(10),
1053       Op.getOperand(11),
1054       Op.getOperand(12),
1055       Op.getOperand(13),
1056       Op.getOperand(14)
1057     };
1058
1059     EVT VT = Op.getOperand(3).getValueType();
1060
1061     MachineMemOperand *MMO = MF.getMachineMemOperand(
1062       MachinePointerInfo(),
1063       MachineMemOperand::MOStore,
1064       VT.getStoreSize(), 4);
1065     return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1066                                    Op->getVTList(), Ops, VT, MMO);
1067   }
1068   default:
1069     return SDValue();
1070   }
1071 }
1072
1073 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1074   SDLoc DL(Op);
1075   LoadSDNode *Load = cast<LoadSDNode>(Op);
1076
1077   if (Op.getValueType().isVector()) {
1078     assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1079            "Custom lowering for non-i32 vectors hasn't been implemented.");
1080     unsigned NumElements = Op.getValueType().getVectorNumElements();
1081     assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
1082     switch (Load->getAddressSpace()) {
1083       default: break;
1084       case AMDGPUAS::GLOBAL_ADDRESS:
1085       case AMDGPUAS::PRIVATE_ADDRESS:
1086         // v4 loads are supported for private and global memory.
1087         if (NumElements <= 4)
1088           break;
1089         // fall-through
1090       case AMDGPUAS::LOCAL_ADDRESS:
1091         return ScalarizeVectorLoad(Op, DAG);
1092     }
1093   }
1094
1095   return AMDGPUTargetLowering::LowerLOAD(Op, DAG);
1096 }
1097
1098 SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
1099                                                const SDValue &Op,
1100                                                SelectionDAG &DAG) const {
1101   return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
1102                      Op.getOperand(2),
1103                      Op.getOperand(3),
1104                      Op.getOperand(4));
1105 }
1106
1107 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1108   if (Op.getValueType() != MVT::i64)
1109     return SDValue();
1110
1111   SDLoc DL(Op);
1112   SDValue Cond = Op.getOperand(0);
1113
1114   SDValue Zero = DAG.getConstant(0, MVT::i32);
1115   SDValue One = DAG.getConstant(1, MVT::i32);
1116
1117   SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1118   SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1119
1120   SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1121   SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
1122
1123   SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1124
1125   SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1126   SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
1127
1128   SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1129
1130   SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1131   return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
1132 }
1133
1134 // Catch division cases where we can use shortcuts with rcp and rsq
1135 // instructions.
1136 SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
1137   SDLoc SL(Op);
1138   SDValue LHS = Op.getOperand(0);
1139   SDValue RHS = Op.getOperand(1);
1140   EVT VT = Op.getValueType();
1141   bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
1142
1143   if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
1144     if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1145         CLHS->isExactlyValue(1.0)) {
1146       // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1147       // the CI documentation has a worst case error of 1 ulp.
1148       // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1149       // use it as long as we aren't trying to use denormals.
1150
1151       // 1.0 / sqrt(x) -> rsq(x)
1152       //
1153       // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1154       // error seems really high at 2^29 ULP.
1155       if (RHS.getOpcode() == ISD::FSQRT)
1156         return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1157
1158       // 1.0 / x -> rcp(x)
1159       return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1160     }
1161   }
1162
1163   if (Unsafe) {
1164     // Turn into multiply by the reciprocal.
1165     // x / y -> x * (1.0 / y)
1166     SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1167     return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip);
1168   }
1169
1170   return SDValue();
1171 }
1172
1173 SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
1174   SDValue FastLowered = LowerFastFDIV(Op, DAG);
1175   if (FastLowered.getNode())
1176     return FastLowered;
1177
1178   // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1179   // selection error for now rather than do something incorrect.
1180   if (Subtarget->hasFP32Denormals())
1181     return SDValue();
1182
1183   SDLoc SL(Op);
1184   SDValue LHS = Op.getOperand(0);
1185   SDValue RHS = Op.getOperand(1);
1186
1187   SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1188
1189   const APFloat K0Val(BitsToFloat(0x6f800000));
1190   const SDValue K0 = DAG.getConstantFP(K0Val, MVT::f32);
1191
1192   const APFloat K1Val(BitsToFloat(0x2f800000));
1193   const SDValue K1 = DAG.getConstantFP(K1Val, MVT::f32);
1194
1195   const SDValue One = DAG.getTargetConstantFP(1.0, MVT::f32);
1196
1197   EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f32);
1198
1199   SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1200
1201   SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1202
1203   r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1204
1205   SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1206
1207   SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1208
1209   return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1210 }
1211
1212 SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
1213   return SDValue();
1214 }
1215
1216 SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1217   EVT VT = Op.getValueType();
1218
1219   if (VT == MVT::f32)
1220     return LowerFDIV32(Op, DAG);
1221
1222   if (VT == MVT::f64)
1223     return LowerFDIV64(Op, DAG);
1224
1225   llvm_unreachable("Unexpected type for fdiv");
1226 }
1227
1228 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1229   SDLoc DL(Op);
1230   StoreSDNode *Store = cast<StoreSDNode>(Op);
1231   EVT VT = Store->getMemoryVT();
1232
1233   // These stores are legal.
1234   if (Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS &&
1235       VT.isVector() && VT.getVectorNumElements() == 2 &&
1236       VT.getVectorElementType() == MVT::i32)
1237     return SDValue();
1238
1239   if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1240     if (VT.isVector() && VT.getVectorNumElements() > 4)
1241       return ScalarizeVectorStore(Op, DAG);
1242     return SDValue();
1243   }
1244
1245   SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1246   if (Ret.getNode())
1247     return Ret;
1248
1249   if (VT.isVector() && VT.getVectorNumElements() >= 8)
1250       return ScalarizeVectorStore(Op, DAG);
1251
1252   if (VT == MVT::i1)
1253     return DAG.getTruncStore(Store->getChain(), DL,
1254                         DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1255                         Store->getBasePtr(), MVT::i1, Store->getMemOperand());
1256
1257   return SDValue();
1258 }
1259
1260 SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
1261   EVT VT = Op.getValueType();
1262   SDValue Arg = Op.getOperand(0);
1263   SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, SDLoc(Op), VT,
1264         DAG.getNode(ISD::FMUL, SDLoc(Op), VT, Arg,
1265           DAG.getConstantFP(0.5 / M_PI, VT)));
1266
1267   switch (Op.getOpcode()) {
1268   case ISD::FCOS:
1269     return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
1270   case ISD::FSIN:
1271     return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
1272   default:
1273     llvm_unreachable("Wrong trig opcode");
1274   }
1275 }
1276
1277 //===----------------------------------------------------------------------===//
1278 // Custom DAG optimizations
1279 //===----------------------------------------------------------------------===//
1280
1281 SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
1282                                                      DAGCombinerInfo &DCI) {
1283   EVT VT = N->getValueType(0);
1284   EVT ScalarVT = VT.getScalarType();
1285   if (ScalarVT != MVT::f32)
1286     return SDValue();
1287
1288   SelectionDAG &DAG = DCI.DAG;
1289   SDLoc DL(N);
1290
1291   SDValue Src = N->getOperand(0);
1292   EVT SrcVT = Src.getValueType();
1293
1294   // TODO: We could try to match extracting the higher bytes, which would be
1295   // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1296   // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1297   // about in practice.
1298   if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1299     if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1300       SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1301       DCI.AddToWorklist(Cvt.getNode());
1302       return Cvt;
1303     }
1304   }
1305
1306   // We are primarily trying to catch operations on illegal vector types
1307   // before they are expanded.
1308   // For scalars, we can use the more flexible method of checking masked bits
1309   // after legalization.
1310   if (!DCI.isBeforeLegalize() ||
1311       !SrcVT.isVector() ||
1312       SrcVT.getVectorElementType() != MVT::i8) {
1313     return SDValue();
1314   }
1315
1316   assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1317
1318   // Weird sized vectors are a pain to handle, but we know 3 is really the same
1319   // size as 4.
1320   unsigned NElts = SrcVT.getVectorNumElements();
1321   if (!SrcVT.isSimple() && NElts != 3)
1322     return SDValue();
1323
1324   // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1325   // prevent a mess from expanding to v4i32 and repacking.
1326   if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1327     EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1328     EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1329     EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
1330
1331     LoadSDNode *Load = cast<LoadSDNode>(Src);
1332     SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1333                                      Load->getChain(),
1334                                      Load->getBasePtr(),
1335                                      LoadVT,
1336                                      Load->getMemOperand());
1337
1338     // Make sure successors of the original load stay after it by updating
1339     // them to use the new Chain.
1340     DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1341
1342     SmallVector<SDValue, 4> Elts;
1343     if (RegVT.isVector())
1344       DAG.ExtractVectorElements(NewLoad, Elts);
1345     else
1346       Elts.push_back(NewLoad);
1347
1348     SmallVector<SDValue, 4> Ops;
1349
1350     unsigned EltIdx = 0;
1351     for (SDValue Elt : Elts) {
1352       unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1353       for (unsigned I = 0; I < ComponentsInElt; ++I) {
1354         unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1355         SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1356         DCI.AddToWorklist(Cvt.getNode());
1357         Ops.push_back(Cvt);
1358       }
1359
1360       ++EltIdx;
1361     }
1362
1363     assert(Ops.size() == NElts);
1364
1365     return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1366   }
1367
1368   return SDValue();
1369 }
1370
1371 // (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
1372
1373 // This is a variant of
1374 // (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
1375 //
1376 // The normal DAG combiner will do this, but only if the add has one use since
1377 // that would increase the number of instructions.
1378 //
1379 // This prevents us from seeing a constant offset that can be folded into a
1380 // memory instruction's addressing mode. If we know the resulting add offset of
1381 // a pointer can be folded into an addressing offset, we can replace the pointer
1382 // operand with the add of new constant offset. This eliminates one of the uses,
1383 // and may allow the remaining use to also be simplified.
1384 //
1385 SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
1386                                                unsigned AddrSpace,
1387                                                DAGCombinerInfo &DCI) const {
1388   SDValue N0 = N->getOperand(0);
1389   SDValue N1 = N->getOperand(1);
1390
1391   if (N0.getOpcode() != ISD::ADD)
1392     return SDValue();
1393
1394   const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
1395   if (!CN1)
1396     return SDValue();
1397
1398   const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1399   if (!CAdd)
1400     return SDValue();
1401
1402   const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1403       getTargetMachine().getSubtargetImpl()->getInstrInfo());
1404
1405   // If the resulting offset is too large, we can't fold it into the addressing
1406   // mode offset.
1407   APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
1408   if (!TII->canFoldOffset(Offset.getZExtValue(), AddrSpace))
1409     return SDValue();
1410
1411   SelectionDAG &DAG = DCI.DAG;
1412   SDLoc SL(N);
1413   EVT VT = N->getValueType(0);
1414
1415   SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
1416   SDValue COffset = DAG.getConstant(Offset, MVT::i32);
1417
1418   return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
1419 }
1420
1421 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1422                                             DAGCombinerInfo &DCI) const {
1423   SelectionDAG &DAG = DCI.DAG;
1424   SDLoc DL(N);
1425   EVT VT = N->getValueType(0);
1426
1427   switch (N->getOpcode()) {
1428     default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1429     case ISD::SETCC: {
1430       SDValue Arg0 = N->getOperand(0);
1431       SDValue Arg1 = N->getOperand(1);
1432       SDValue CC = N->getOperand(2);
1433       ConstantSDNode * C = nullptr;
1434       ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
1435
1436       // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
1437       if (VT == MVT::i1
1438           && Arg0.getOpcode() == ISD::SIGN_EXTEND
1439           && Arg0.getOperand(0).getValueType() == MVT::i1
1440           && (C = dyn_cast<ConstantSDNode>(Arg1))
1441           && C->isNullValue()
1442           && CCOp == ISD::SETNE) {
1443         return SimplifySetCC(VT, Arg0.getOperand(0),
1444                              DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
1445       }
1446       break;
1447     }
1448
1449   case AMDGPUISD::CVT_F32_UBYTE0:
1450   case AMDGPUISD::CVT_F32_UBYTE1:
1451   case AMDGPUISD::CVT_F32_UBYTE2:
1452   case AMDGPUISD::CVT_F32_UBYTE3: {
1453     unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
1454
1455     SDValue Src = N->getOperand(0);
1456     APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
1457
1458     APInt KnownZero, KnownOne;
1459     TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1460                                           !DCI.isBeforeLegalizeOps());
1461     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1462     if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
1463         TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
1464       DCI.CommitTargetLoweringOpt(TLO);
1465     }
1466
1467     break;
1468   }
1469
1470   case ISD::UINT_TO_FP: {
1471     return performUCharToFloatCombine(N, DCI);
1472   }
1473   case ISD::LOAD:
1474   case ISD::STORE:
1475   case ISD::ATOMIC_LOAD:
1476   case ISD::ATOMIC_STORE:
1477   case ISD::ATOMIC_CMP_SWAP:
1478   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
1479   case ISD::ATOMIC_SWAP:
1480   case ISD::ATOMIC_LOAD_ADD:
1481   case ISD::ATOMIC_LOAD_SUB:
1482   case ISD::ATOMIC_LOAD_AND:
1483   case ISD::ATOMIC_LOAD_OR:
1484   case ISD::ATOMIC_LOAD_XOR:
1485   case ISD::ATOMIC_LOAD_NAND:
1486   case ISD::ATOMIC_LOAD_MIN:
1487   case ISD::ATOMIC_LOAD_MAX:
1488   case ISD::ATOMIC_LOAD_UMIN:
1489   case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics.
1490     if (DCI.isBeforeLegalize())
1491       break;
1492
1493     MemSDNode *MemNode = cast<MemSDNode>(N);
1494     SDValue Ptr = MemNode->getBasePtr();
1495
1496     // TODO: We could also do this for multiplies.
1497     unsigned AS = MemNode->getAddressSpace();
1498     if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
1499       SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
1500       if (NewPtr) {
1501         SmallVector<SDValue, 8> NewOps;
1502         for (unsigned I = 0, N = MemNode->getNumOperands(); I != N; ++I)
1503           NewOps.push_back(MemNode->getOperand(I));
1504
1505         NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
1506         return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
1507       }
1508     }
1509     break;
1510   }
1511   }
1512   return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1513 }
1514
1515 /// \brief Test if RegClass is one of the VSrc classes
1516 static bool isVSrc(unsigned RegClass) {
1517   return AMDGPU::VSrc_32RegClassID == RegClass ||
1518          AMDGPU::VSrc_64RegClassID == RegClass;
1519 }
1520
1521 /// \brief Test if RegClass is one of the SSrc classes
1522 static bool isSSrc(unsigned RegClass) {
1523   return AMDGPU::SSrc_32RegClassID == RegClass ||
1524          AMDGPU::SSrc_64RegClassID == RegClass;
1525 }
1526
1527 /// \brief Analyze the possible immediate value Op
1528 ///
1529 /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1530 /// and the immediate value if it's a literal immediate
1531 int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1532
1533   union {
1534     int32_t I;
1535     float F;
1536   } Imm;
1537
1538   if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
1539     if (Node->getZExtValue() >> 32) {
1540         return -1;
1541     }
1542     Imm.I = Node->getSExtValue();
1543   } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1544     if (N->getValueType(0) != MVT::f32)
1545       return -1;
1546     Imm.F = Node->getValueAPF().convertToFloat();
1547   } else
1548     return -1; // It isn't an immediate
1549
1550   if ((Imm.I >= -16 && Imm.I <= 64) ||
1551       Imm.F == 0.5f || Imm.F == -0.5f ||
1552       Imm.F == 1.0f || Imm.F == -1.0f ||
1553       Imm.F == 2.0f || Imm.F == -2.0f ||
1554       Imm.F == 4.0f || Imm.F == -4.0f)
1555     return 0; // It's an inline immediate
1556
1557   return Imm.I; // It's a literal immediate
1558 }
1559
1560 /// \brief Try to fold an immediate directly into an instruction
1561 bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
1562                                bool &ScalarSlotUsed) const {
1563
1564   MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
1565   const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1566       getTargetMachine().getSubtargetImpl()->getInstrInfo());
1567   if (!Mov || !TII->isMov(Mov->getMachineOpcode()))
1568     return false;
1569
1570   const SDValue &Op = Mov->getOperand(0);
1571   int32_t Value = analyzeImmediate(Op.getNode());
1572   if (Value == -1) {
1573     // Not an immediate at all
1574     return false;
1575
1576   } else if (Value == 0) {
1577     // Inline immediates can always be fold
1578     Operand = Op;
1579     return true;
1580
1581   } else if (Value == Immediate) {
1582     // Already fold literal immediate
1583     Operand = Op;
1584     return true;
1585
1586   } else if (!ScalarSlotUsed && !Immediate) {
1587     // Fold this literal immediate
1588     ScalarSlotUsed = true;
1589     Immediate = Value;
1590     Operand = Op;
1591     return true;
1592
1593   }
1594
1595   return false;
1596 }
1597
1598 const TargetRegisterClass *SITargetLowering::getRegClassForNode(
1599                                    SelectionDAG &DAG, const SDValue &Op) const {
1600   const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1601       getTargetMachine().getSubtargetImpl()->getInstrInfo());
1602   const SIRegisterInfo &TRI = TII->getRegisterInfo();
1603
1604   if (!Op->isMachineOpcode()) {
1605     switch(Op->getOpcode()) {
1606     case ISD::CopyFromReg: {
1607       MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1608       unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1609       if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1610         return MRI.getRegClass(Reg);
1611       }
1612       return TRI.getPhysRegClass(Reg);
1613     }
1614     default:  return nullptr;
1615     }
1616   }
1617   const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1618   int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1619   if (OpClassID != -1) {
1620     return TRI.getRegClass(OpClassID);
1621   }
1622   switch(Op.getMachineOpcode()) {
1623   case AMDGPU::COPY_TO_REGCLASS:
1624     // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1625     OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1626
1627     // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1628     // class, then the register class for the value could be either a
1629     // VReg or and SReg.  In order to get a more accurate
1630     if (OpClassID == AMDGPU::VSrc_32RegClassID ||
1631         OpClassID == AMDGPU::VSrc_64RegClassID) {
1632       return getRegClassForNode(DAG, Op.getOperand(0));
1633     }
1634     return TRI.getRegClass(OpClassID);
1635   case AMDGPU::EXTRACT_SUBREG: {
1636     int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1637     const TargetRegisterClass *SuperClass =
1638       getRegClassForNode(DAG, Op.getOperand(0));
1639     return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1640   }
1641   case AMDGPU::REG_SEQUENCE:
1642     // Operand 0 is the register class id for REG_SEQUENCE instructions.
1643     return TRI.getRegClass(
1644       cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1645   default:
1646     return getRegClassFor(Op.getSimpleValueType());
1647   }
1648 }
1649
1650 /// \brief Does "Op" fit into register class "RegClass" ?
1651 bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
1652                                     unsigned RegClass) const {
1653   const TargetRegisterInfo *TRI =
1654       getTargetMachine().getSubtargetImpl()->getRegisterInfo();
1655   const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1656   if (!RC) {
1657     return false;
1658   }
1659   return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
1660 }
1661
1662 /// \brief Make sure that we don't exeed the number of allowed scalars
1663 void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
1664                                        unsigned RegClass,
1665                                        bool &ScalarSlotUsed) const {
1666
1667   // First map the operands register class to a destination class
1668   if (RegClass == AMDGPU::VSrc_32RegClassID)
1669     RegClass = AMDGPU::VReg_32RegClassID;
1670   else if (RegClass == AMDGPU::VSrc_64RegClassID)
1671     RegClass = AMDGPU::VReg_64RegClassID;
1672   else
1673     return;
1674
1675   // Nothing to do if they fit naturally
1676   if (fitsRegClass(DAG, Operand, RegClass))
1677     return;
1678
1679   // If the scalar slot isn't used yet use it now
1680   if (!ScalarSlotUsed) {
1681     ScalarSlotUsed = true;
1682     return;
1683   }
1684
1685   // This is a conservative aproach. It is possible that we can't determine the
1686   // correct register class and copy too often, but better safe than sorry.
1687
1688   SDNode *Node;
1689   // We can't use COPY_TO_REGCLASS with FrameIndex arguments.
1690   if (isa<FrameIndexSDNode>(Operand)) {
1691     unsigned Opcode = Operand.getValueType() == MVT::i32 ?
1692                       AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
1693     Node = DAG.getMachineNode(Opcode, SDLoc(), Operand.getValueType(),
1694                               Operand);
1695   } else {
1696     SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
1697     Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(),
1698                               Operand.getValueType(), Operand, RC);
1699   }
1700   Operand = SDValue(Node, 0);
1701 }
1702
1703 /// \returns true if \p Node's operands are different from the SDValue list
1704 /// \p Ops
1705 static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) {
1706   for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) {
1707     if (Ops[i].getNode() != Node->getOperand(i).getNode()) {
1708       return true;
1709     }
1710   }
1711   return false;
1712 }
1713
1714 /// \brief Try to fold the Nodes operands into the Node
1715 SDNode *SITargetLowering::foldOperands(MachineSDNode *Node,
1716                                        SelectionDAG &DAG) const {
1717
1718   // Original encoding (either e32 or e64)
1719   int Opcode = Node->getMachineOpcode();
1720   const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1721       getTargetMachine().getSubtargetImpl()->getInstrInfo());
1722   const MCInstrDesc *Desc = &TII->get(Opcode);
1723
1724   unsigned NumDefs = Desc->getNumDefs();
1725   unsigned NumOps = Desc->getNumOperands();
1726
1727   // Commuted opcode if available
1728   int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1;
1729   const MCInstrDesc *DescRev = OpcodeRev == -1 ? nullptr : &TII->get(OpcodeRev);
1730
1731   assert(!DescRev || DescRev->getNumDefs() == NumDefs);
1732   assert(!DescRev || DescRev->getNumOperands() == NumOps);
1733
1734   // e64 version if available, -1 otherwise
1735   int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
1736   const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? nullptr : &TII->get(OpcodeE64);
1737   int InputModifiers[3] = {0};
1738
1739   assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
1740
1741   int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
1742   bool HaveVSrc = false, HaveSSrc = false;
1743
1744   // First figure out what we already have in this instruction.
1745   for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1746        i != e && Op < NumOps; ++i, ++Op) {
1747
1748     unsigned RegClass = Desc->OpInfo[Op].RegClass;
1749     if (isVSrc(RegClass))
1750       HaveVSrc = true;
1751     else if (isSSrc(RegClass))
1752       HaveSSrc = true;
1753     else
1754       continue;
1755
1756     int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
1757     if (Imm != -1 && Imm != 0) {
1758       // Literal immediate
1759       Immediate = Imm;
1760     }
1761   }
1762
1763   // If we neither have VSrc nor SSrc, it makes no sense to continue.
1764   if (!HaveVSrc && !HaveSSrc)
1765     return Node;
1766
1767   // No scalar allowed when we have both VSrc and SSrc
1768   bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
1769
1770   // Second go over the operands and try to fold them
1771   std::vector<SDValue> Ops;
1772   bool Promote2e64 = false;
1773   for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1774        i != e && Op < NumOps; ++i, ++Op) {
1775
1776     const SDValue &Operand = Node->getOperand(i);
1777     Ops.push_back(Operand);
1778
1779     // Already folded immediate?
1780     if (isa<ConstantSDNode>(Operand.getNode()) ||
1781         isa<ConstantFPSDNode>(Operand.getNode()))
1782       continue;
1783
1784     // Is this a VSrc or SSrc operand?
1785     unsigned RegClass = Desc->OpInfo[Op].RegClass;
1786     if (isVSrc(RegClass) || isSSrc(RegClass)) {
1787       // Try to fold the immediates
1788       if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
1789         // Folding didn't work, make sure we don't hit the SReg limit.
1790         ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
1791       }
1792       continue;
1793     } else {
1794       // If it's not a VSrc or SSrc operand check if we have a GlobalAddress.
1795       // These will be lowered to immediates, so we will need to insert a MOV.
1796       if (isa<GlobalAddressSDNode>(Ops[i])) {
1797         SDNode *Node = DAG.getMachineNode(AMDGPU::V_MOV_B32_e32, SDLoc(),
1798                                     Operand.getValueType(), Operand);
1799         Ops[i] = SDValue(Node, 0);
1800       }
1801     }
1802
1803     if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) {
1804
1805       unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass;
1806       assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass));
1807
1808       // Test if it makes sense to swap operands
1809       if (foldImm(Ops[1], Immediate, ScalarSlotUsed) ||
1810           (!fitsRegClass(DAG, Ops[1], RegClass) &&
1811            fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1812
1813         // Swap commutable operands
1814         std::swap(Ops[0], Ops[1]);
1815
1816         Desc = DescRev;
1817         DescRev = nullptr;
1818         continue;
1819       }
1820     }
1821
1822     if (Immediate)
1823       continue;
1824
1825     if (DescE64) {
1826       // Test if it makes sense to switch to e64 encoding
1827       unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass;
1828       if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass))
1829         continue;
1830
1831       int32_t TmpImm = -1;
1832       if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) ||
1833           (!fitsRegClass(DAG, Ops[i], RegClass) &&
1834            fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1835
1836         // Switch to e64 encoding
1837         Immediate = -1;
1838         Promote2e64 = true;
1839         Desc = DescE64;
1840         DescE64 = nullptr;
1841       }
1842     }
1843
1844     if (!DescE64 && !Promote2e64)
1845       continue;
1846     if (!Operand.isMachineOpcode())
1847       continue;
1848   }
1849
1850   if (Promote2e64) {
1851     std::vector<SDValue> OldOps(Ops);
1852     Ops.clear();
1853     bool HasModifiers = TII->hasModifiers(Desc->Opcode);
1854     for (unsigned i = 0; i < OldOps.size(); ++i) {
1855       // src_modifier
1856       if (HasModifiers)
1857         Ops.push_back(DAG.getTargetConstant(InputModifiers[i], MVT::i32));
1858       Ops.push_back(OldOps[i]);
1859     }
1860     // Add the modifier flags while promoting
1861     if (HasModifiers) {
1862       for (unsigned i = 0; i < 2; ++i)
1863         Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
1864     }
1865   }
1866
1867   // Add optional chain and glue
1868   for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
1869     Ops.push_back(Node->getOperand(i));
1870
1871   // Nodes that have a glue result are not CSE'd by getMachineNode(), so in
1872   // this case a brand new node is always be created, even if the operands
1873   // are the same as before.  So, manually check if anything has been changed.
1874   if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) {
1875     return Node;
1876   }
1877
1878   // Create a complete new instruction
1879   return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops);
1880 }
1881
1882 /// \brief Helper function for adjustWritemask
1883 static unsigned SubIdx2Lane(unsigned Idx) {
1884   switch (Idx) {
1885   default: return 0;
1886   case AMDGPU::sub0: return 0;
1887   case AMDGPU::sub1: return 1;
1888   case AMDGPU::sub2: return 2;
1889   case AMDGPU::sub3: return 3;
1890   }
1891 }
1892
1893 /// \brief Adjust the writemask of MIMG instructions
1894 void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1895                                        SelectionDAG &DAG) const {
1896   SDNode *Users[4] = { };
1897   unsigned Lane = 0;
1898   unsigned OldDmask = Node->getConstantOperandVal(0);
1899   unsigned NewDmask = 0;
1900
1901   // Try to figure out the used register components
1902   for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1903        I != E; ++I) {
1904
1905     // Abort if we can't understand the usage
1906     if (!I->isMachineOpcode() ||
1907         I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1908       return;
1909
1910     // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1911     // Note that subregs are packed, i.e. Lane==0 is the first bit set
1912     // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1913     // set, etc.
1914     Lane = SubIdx2Lane(I->getConstantOperandVal(1));
1915
1916     // Set which texture component corresponds to the lane.
1917     unsigned Comp;
1918     for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1919       assert(Dmask);
1920       Comp = countTrailingZeros(Dmask);
1921       Dmask &= ~(1 << Comp);
1922     }
1923
1924     // Abort if we have more than one user per component
1925     if (Users[Lane])
1926       return;
1927
1928     Users[Lane] = *I;
1929     NewDmask |= 1 << Comp;
1930   }
1931
1932   // Abort if there's no change
1933   if (NewDmask == OldDmask)
1934     return;
1935
1936   // Adjust the writemask in the node
1937   std::vector<SDValue> Ops;
1938   Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
1939   for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1940     Ops.push_back(Node->getOperand(i));
1941   Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
1942
1943   // If we only got one lane, replace it with a copy
1944   // (if NewDmask has only one bit set...)
1945   if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
1946     SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32);
1947     SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1948                                       SDLoc(), Users[Lane]->getValueType(0),
1949                                       SDValue(Node, 0), RC);
1950     DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1951     return;
1952   }
1953
1954   // Update the users of the node with the new indices
1955   for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1956
1957     SDNode *User = Users[i];
1958     if (!User)
1959       continue;
1960
1961     SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1962     DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1963
1964     switch (Idx) {
1965     default: break;
1966     case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1967     case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1968     case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1969     }
1970   }
1971 }
1972
1973 /// \brief Fold the instructions after selecting them.
1974 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1975                                           SelectionDAG &DAG) const {
1976   const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1977       getTargetMachine().getSubtargetImpl()->getInstrInfo());
1978   Node = AdjustRegClass(Node, DAG);
1979
1980   if (TII->isMIMG(Node->getMachineOpcode()))
1981     adjustWritemask(Node, DAG);
1982
1983   return foldOperands(Node, DAG);
1984 }
1985
1986 /// \brief Assign the register class depending on the number of
1987 /// bits set in the writemask
1988 void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1989                                                      SDNode *Node) const {
1990   const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1991       getTargetMachine().getSubtargetImpl()->getInstrInfo());
1992   if (!TII->isMIMG(MI->getOpcode()))
1993     return;
1994
1995   unsigned VReg = MI->getOperand(0).getReg();
1996   unsigned Writemask = MI->getOperand(1).getImm();
1997   unsigned BitsSet = 0;
1998   for (unsigned i = 0; i < 4; ++i)
1999     BitsSet += Writemask & (1 << i) ? 1 : 0;
2000
2001   const TargetRegisterClass *RC;
2002   switch (BitsSet) {
2003   default: return;
2004   case 1:  RC = &AMDGPU::VReg_32RegClass; break;
2005   case 2:  RC = &AMDGPU::VReg_64RegClass; break;
2006   case 3:  RC = &AMDGPU::VReg_96RegClass; break;
2007   }
2008
2009   unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
2010   MI->setDesc(TII->get(NewOpcode));
2011   MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
2012   MRI.setRegClass(VReg, RC);
2013 }
2014
2015 MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
2016                                                 SelectionDAG &DAG) const {
2017
2018   SDLoc DL(N);
2019   unsigned NewOpcode = N->getMachineOpcode();
2020
2021   switch (N->getMachineOpcode()) {
2022   default: return N;
2023   case AMDGPU::S_LOAD_DWORD_IMM:
2024     NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
2025     // Fall-through
2026   case AMDGPU::S_LOAD_DWORDX2_SGPR:
2027     if (NewOpcode == N->getMachineOpcode()) {
2028       NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
2029     }
2030     // Fall-through
2031   case AMDGPU::S_LOAD_DWORDX4_IMM:
2032   case AMDGPU::S_LOAD_DWORDX4_SGPR: {
2033     if (NewOpcode == N->getMachineOpcode()) {
2034       NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
2035     }
2036     if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
2037       return N;
2038     }
2039     ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
2040     SDValue Ops[] = {
2041       SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128,
2042                                  DAG.getConstant(0, MVT::i64)), 0),
2043       N->getOperand(0),
2044       DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32)
2045     };
2046     return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
2047   }
2048   }
2049 }
2050
2051 SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2052                                                const TargetRegisterClass *RC,
2053                                                unsigned Reg, EVT VT) const {
2054   SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
2055
2056   return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
2057                             cast<RegisterSDNode>(VReg)->getReg(), VT);
2058 }