R600/SI: Handle sign_extend and zero_extend to i64 with patterns.
[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 #include "SIISelLowering.h"
16 #include "AMDGPU.h"
17 #include "AMDGPUSubtarget.h"
18 #include "AMDILIntrinsicInfo.h"
19 #include "SIInstrInfo.h"
20 #include "SIMachineFunctionInfo.h"
21 #include "SIRegisterInfo.h"
22 #include "llvm/CodeGen/CallingConvLower.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/SelectionDAG.h"
26 #include "llvm/IR/Function.h"
27
28 using namespace llvm;
29
30 SITargetLowering::SITargetLowering(TargetMachine &TM) :
31     AMDGPUTargetLowering(TM) {
32   addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
33   addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
34
35   addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
36   addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
37
38   addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
39   addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass);
40
41   addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
42   addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
43   addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
44
45   addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
46   addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
47
48   addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass);
49   addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
50
51   addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass);
52   addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
53
54   computeRegisterProperties();
55
56   // Condition Codes
57   setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
58   setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
59   setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
60   setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
61   setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
62   setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
63
64   setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
65   setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
66   setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
67   setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
68   setCondCodeAction(ISD::SETULE, MVT::f64, Expand);
69   setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
70
71   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
72   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
73   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
74   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
75
76   setOperationAction(ISD::ADD, MVT::i32, Legal);
77   setOperationAction(ISD::ADDC, MVT::i32, Legal);
78   setOperationAction(ISD::ADDE, MVT::i32, Legal);
79
80   // We need to custom lower vector stores from local memory
81   setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
82   setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
83   setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
84   setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
85
86   setOperationAction(ISD::STORE, MVT::v8i32, Custom);
87   setOperationAction(ISD::STORE, MVT::v16i32, Custom);
88
89   // We need to custom lower loads/stores from private memory
90   setOperationAction(ISD::LOAD, MVT::i32, Custom);
91   setOperationAction(ISD::LOAD, MVT::i64, Custom);
92   setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
93   setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
94   setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
95
96   setOperationAction(ISD::STORE, MVT::i1, Custom);
97   setOperationAction(ISD::STORE, MVT::i32, Custom);
98   setOperationAction(ISD::STORE, MVT::i64, Custom);
99   setOperationAction(ISD::STORE, MVT::v2i32, Custom);
100   setOperationAction(ISD::STORE, MVT::v4i32, Custom);
101
102   setOperationAction(ISD::SELECT, MVT::f32, Promote);
103   AddPromotedToType(ISD::SELECT, MVT::f32, MVT::i32);
104   setOperationAction(ISD::SELECT, MVT::i64, Custom);
105   setOperationAction(ISD::SELECT, MVT::f64, Promote);
106   AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
107
108   setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
109   setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
110   setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
111   setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
112
113   setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
114   setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
115
116   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
117   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
118   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
119
120   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
121   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
122   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
123
124   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
125   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
126   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
127
128   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Custom);
129
130   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
131
132   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
133   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
134   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
135   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
136
137   setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
138
139   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
140   setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
141   setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
142   setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Expand);
143   setLoadExtAction(ISD::SEXTLOAD, MVT::v8i16, Expand);
144   setLoadExtAction(ISD::SEXTLOAD, MVT::v16i16, Expand);
145
146   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
147   setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
148   setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
149   setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Expand);
150
151   setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
152   setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
153   setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
154   setLoadExtAction(ISD::EXTLOAD, MVT::i32, Expand);
155   setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
156
157   setTruncStoreAction(MVT::i32, MVT::i8, Custom);
158   setTruncStoreAction(MVT::i32, MVT::i16, Custom);
159   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
160   setTruncStoreAction(MVT::i64, MVT::i32, Expand);
161   setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
162   setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
163
164   setOperationAction(ISD::LOAD, MVT::i1, Custom);
165
166   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
167   setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
168   setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
169
170   // These should use UDIVREM, so set them to expand
171   setOperationAction(ISD::UDIV, MVT::i64, Expand);
172   setOperationAction(ISD::UREM, MVT::i64, Expand);
173
174   // We only support LOAD/STORE and vector manipulation ops for vectors
175   // with > 4 elements.
176   MVT VecTypes[] = {
177     MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32
178   };
179
180   for (MVT VT : VecTypes) {
181     for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
182       switch(Op) {
183       case ISD::LOAD:
184       case ISD::STORE:
185       case ISD::BUILD_VECTOR:
186       case ISD::BITCAST:
187       case ISD::EXTRACT_VECTOR_ELT:
188       case ISD::INSERT_VECTOR_ELT:
189       case ISD::CONCAT_VECTORS:
190       case ISD::INSERT_SUBVECTOR:
191       case ISD::EXTRACT_SUBVECTOR:
192         break;
193       default:
194         setOperationAction(Op, VT, Expand);
195         break;
196       }
197     }
198   }
199
200   for (int I = MVT::v1f64; I <= MVT::v8f64; ++I) {
201     MVT::SimpleValueType VT = static_cast<MVT::SimpleValueType>(I);
202     setOperationAction(ISD::FTRUNC, VT, Expand);
203     setOperationAction(ISD::FCEIL, VT, Expand);
204     setOperationAction(ISD::FFLOOR, VT, Expand);
205   }
206
207   if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
208     setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
209     setOperationAction(ISD::FCEIL, MVT::f64, Legal);
210     setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
211     setOperationAction(ISD::FRINT, MVT::f64, Legal);
212   }
213
214   setTargetDAGCombine(ISD::SELECT_CC);
215   setTargetDAGCombine(ISD::SETCC);
216
217   setSchedulingPreference(Sched::RegPressure);
218 }
219
220 //===----------------------------------------------------------------------===//
221 // TargetLowering queries
222 //===----------------------------------------------------------------------===//
223
224 bool SITargetLowering::allowsUnalignedMemoryAccesses(EVT  VT,
225                                                      unsigned AddrSpace,
226                                                      bool *IsFast) const {
227   if (IsFast)
228     *IsFast = false;
229
230   // XXX: This depends on the address space and also we may want to revist
231   // the alignment values we specify in the DataLayout.
232
233   // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
234   // which isn't a simple VT.
235   if (!VT.isSimple() || VT == MVT::Other)
236     return false;
237
238   // XXX - CI changes say "Support for unaligned memory accesses" but I don't
239   // see what for specifically. The wording everywhere else seems to be the
240   // same.
241
242   // 3.6.4 - Operations using pairs of VGPRs (for example: double-floats) have
243   // no alignment restrictions.
244   if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) {
245     // Using any pair of GPRs should be the same as any other pair.
246     if (IsFast)
247       *IsFast = true;
248     return VT.bitsGE(MVT::i64);
249   }
250
251   // XXX - The only mention I see of this in the ISA manual is for LDS direct
252   // reads the "byte address and must be dword aligned". Is it also true for the
253   // normal loads and stores?
254   if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS)
255     return false;
256
257   // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
258   // byte-address are ignored, thus forcing Dword alignment.
259   if (IsFast)
260     *IsFast = true;
261   return VT.bitsGT(MVT::i32);
262 }
263
264 bool SITargetLowering::shouldSplitVectorType(EVT VT) const {
265   return VT.getScalarType().bitsLE(MVT::i16);
266 }
267
268 bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
269                                                          Type *Ty) const {
270   const SIInstrInfo *TII =
271     static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
272   return TII->isInlineConstant(Imm);
273 }
274
275 SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
276                                          SDLoc DL, SDValue Chain,
277                                          unsigned Offset, bool Signed) const {
278   MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
279   PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
280                                             AMDGPUAS::CONSTANT_ADDRESS);
281   SDValue BasePtr =  DAG.getCopyFromReg(Chain, DL,
282                            MRI.getLiveInVirtReg(AMDGPU::SGPR0_SGPR1), MVT::i64);
283   SDValue Ptr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr,
284                                              DAG.getConstant(Offset, MVT::i64));
285   return DAG.getExtLoad(Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD, DL, VT, Chain, Ptr,
286                             MachinePointerInfo(UndefValue::get(PtrTy)), MemVT,
287                             false, false, MemVT.getSizeInBits() >> 3);
288
289 }
290
291 SDValue SITargetLowering::LowerFormalArguments(
292                                       SDValue Chain,
293                                       CallingConv::ID CallConv,
294                                       bool isVarArg,
295                                       const SmallVectorImpl<ISD::InputArg> &Ins,
296                                       SDLoc DL, SelectionDAG &DAG,
297                                       SmallVectorImpl<SDValue> &InVals) const {
298
299   const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
300
301   MachineFunction &MF = DAG.getMachineFunction();
302   FunctionType *FType = MF.getFunction()->getFunctionType();
303   SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
304
305   assert(CallConv == CallingConv::C);
306
307   SmallVector<ISD::InputArg, 16> Splits;
308   uint32_t Skipped = 0;
309
310   for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
311     const ISD::InputArg &Arg = Ins[i];
312
313     // First check if it's a PS input addr
314     if (Info->ShaderType == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
315         !Arg.Flags.isByVal()) {
316
317       assert((PSInputNum <= 15) && "Too many PS inputs!");
318
319       if (!Arg.Used) {
320         // We can savely skip PS inputs
321         Skipped |= 1 << i;
322         ++PSInputNum;
323         continue;
324       }
325
326       Info->PSInputAddr |= 1 << PSInputNum++;
327     }
328
329     // Second split vertices into their elements
330     if (Info->ShaderType != ShaderType::COMPUTE && Arg.VT.isVector()) {
331       ISD::InputArg NewArg = Arg;
332       NewArg.Flags.setSplit();
333       NewArg.VT = Arg.VT.getVectorElementType();
334
335       // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
336       // three or five element vertex only needs three or five registers,
337       // NOT four or eigth.
338       Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
339       unsigned NumElements = ParamType->getVectorNumElements();
340
341       for (unsigned j = 0; j != NumElements; ++j) {
342         Splits.push_back(NewArg);
343         NewArg.PartOffset += NewArg.VT.getStoreSize();
344       }
345
346     } else if (Info->ShaderType != ShaderType::COMPUTE) {
347       Splits.push_back(Arg);
348     }
349   }
350
351   SmallVector<CCValAssign, 16> ArgLocs;
352   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
353                  getTargetMachine(), ArgLocs, *DAG.getContext());
354
355   // At least one interpolation mode must be enabled or else the GPU will hang.
356   if (Info->ShaderType == ShaderType::PIXEL && (Info->PSInputAddr & 0x7F) == 0) {
357     Info->PSInputAddr |= 1;
358     CCInfo.AllocateReg(AMDGPU::VGPR0);
359     CCInfo.AllocateReg(AMDGPU::VGPR1);
360   }
361
362   // The pointer to the list of arguments is stored in SGPR0, SGPR1
363   if (Info->ShaderType == ShaderType::COMPUTE) {
364     CCInfo.AllocateReg(AMDGPU::SGPR0);
365     CCInfo.AllocateReg(AMDGPU::SGPR1);
366     MF.addLiveIn(AMDGPU::SGPR0_SGPR1, &AMDGPU::SReg_64RegClass);
367   }
368
369   if (Info->ShaderType == ShaderType::COMPUTE) {
370     getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
371                             Splits);
372   }
373
374   AnalyzeFormalArguments(CCInfo, Splits);
375
376   for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
377
378     const ISD::InputArg &Arg = Ins[i];
379     if (Skipped & (1 << i)) {
380       InVals.push_back(DAG.getUNDEF(Arg.VT));
381       continue;
382     }
383
384     CCValAssign &VA = ArgLocs[ArgIdx++];
385     EVT VT = VA.getLocVT();
386
387     if (VA.isMemLoc()) {
388       VT = Ins[i].VT;
389       EVT MemVT = Splits[i].VT;
390       // The first 36 bytes of the input buffer contains information about
391       // thread group and global sizes.
392       SDValue Arg = LowerParameter(DAG, VT, MemVT,  DL, DAG.getRoot(),
393                                    36 + VA.getLocMemOffset(),
394                                    Ins[i].Flags.isSExt());
395       InVals.push_back(Arg);
396       continue;
397     }
398     assert(VA.isRegLoc() && "Parameter must be in a register!");
399
400     unsigned Reg = VA.getLocReg();
401
402     if (VT == MVT::i64) {
403       // For now assume it is a pointer
404       Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
405                                      &AMDGPU::SReg_64RegClass);
406       Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
407       InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
408       continue;
409     }
410
411     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
412
413     Reg = MF.addLiveIn(Reg, RC);
414     SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
415
416     if (Arg.VT.isVector()) {
417
418       // Build a vector from the registers
419       Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
420       unsigned NumElements = ParamType->getVectorNumElements();
421
422       SmallVector<SDValue, 4> Regs;
423       Regs.push_back(Val);
424       for (unsigned j = 1; j != NumElements; ++j) {
425         Reg = ArgLocs[ArgIdx++].getLocReg();
426         Reg = MF.addLiveIn(Reg, RC);
427         Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
428       }
429
430       // Fill up the missing vector elements
431       NumElements = Arg.VT.getVectorNumElements() - NumElements;
432       for (unsigned j = 0; j != NumElements; ++j)
433         Regs.push_back(DAG.getUNDEF(VT));
434
435       InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
436       continue;
437     }
438
439     InVals.push_back(Val);
440   }
441   return Chain;
442 }
443
444 MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
445     MachineInstr * MI, MachineBasicBlock * BB) const {
446
447   MachineBasicBlock::iterator I = *MI;
448   const SIInstrInfo *TII =
449     static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
450   MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
451
452   switch (MI->getOpcode()) {
453   default:
454     return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
455   case AMDGPU::BRANCH: return BB;
456   case AMDGPU::SI_ADDR64_RSRC: {
457     unsigned SuperReg = MI->getOperand(0).getReg();
458     unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
459     unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
460     unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
461     unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
462     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo)
463             .addOperand(MI->getOperand(1));
464     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo)
465             .addImm(0);
466     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi)
467             .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32);
468     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi)
469             .addReg(SubRegHiLo)
470             .addImm(AMDGPU::sub0)
471             .addReg(SubRegHiHi)
472             .addImm(AMDGPU::sub1);
473     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg)
474             .addReg(SubRegLo)
475             .addImm(AMDGPU::sub0_sub1)
476             .addReg(SubRegHi)
477             .addImm(AMDGPU::sub2_sub3);
478     MI->eraseFromParent();
479     break;
480   }
481   case AMDGPU::V_SUB_F64:
482     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64),
483             MI->getOperand(0).getReg())
484             .addReg(MI->getOperand(1).getReg())
485             .addReg(MI->getOperand(2).getReg())
486             .addImm(0)  /* src2 */
487             .addImm(0)  /* ABS */
488             .addImm(0)  /* CLAMP */
489             .addImm(0)  /* OMOD */
490             .addImm(2); /* NEG */
491     MI->eraseFromParent();
492     break;
493
494   case AMDGPU::SI_RegisterStorePseudo: {
495     MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
496     unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
497     MachineInstrBuilder MIB =
498         BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
499                 Reg);
500     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
501       MIB.addOperand(MI->getOperand(i));
502
503     MI->eraseFromParent();
504     break;
505   }
506   case AMDGPU::FABS_SI: {
507     MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
508     const SIInstrInfo *TII =
509       static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
510     unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
511     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32),
512             Reg)
513             .addImm(0x7fffffff);
514     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_AND_B32_e32),
515             MI->getOperand(0).getReg())
516             .addReg(MI->getOperand(1).getReg())
517             .addReg(Reg);
518     MI->eraseFromParent();
519     break;
520   }
521   case AMDGPU::FNEG_SI: {
522     MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
523     const SIInstrInfo *TII =
524       static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
525     unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
526     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32),
527             Reg)
528             .addImm(0x80000000);
529     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_XOR_B32_e32),
530             MI->getOperand(0).getReg())
531             .addReg(MI->getOperand(1).getReg())
532             .addReg(Reg);
533     MI->eraseFromParent();
534     break;
535   }
536   case AMDGPU::FCLAMP_SI: {
537     const SIInstrInfo *TII =
538       static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
539     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F32_e64),
540             MI->getOperand(0).getReg())
541             .addImm(0) // SRC0 modifiers
542             .addOperand(MI->getOperand(1))
543             .addImm(0) // SRC1 modifiers
544             .addImm(0) // SRC1
545             .addImm(1) // CLAMP
546             .addImm(0); // OMOD
547     MI->eraseFromParent();
548   }
549   }
550   return BB;
551 }
552
553 EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
554   if (!VT.isVector()) {
555     return MVT::i1;
556   }
557   return MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
558 }
559
560 MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
561   return MVT::i32;
562 }
563
564 bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
565   VT = VT.getScalarType();
566
567   if (!VT.isSimple())
568     return false;
569
570   switch (VT.getSimpleVT().SimpleTy) {
571   case MVT::f32:
572     return false; /* There is V_MAD_F32 for f32 */
573   case MVT::f64:
574     return true;
575   default:
576     break;
577   }
578
579   return false;
580 }
581
582 //===----------------------------------------------------------------------===//
583 // Custom DAG Lowering Operations
584 //===----------------------------------------------------------------------===//
585
586 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
587   MachineFunction &MF = DAG.getMachineFunction();
588   SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
589   switch (Op.getOpcode()) {
590   default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
591   case ISD::BRCOND: return LowerBRCOND(Op, DAG);
592   case ISD::LOAD: {
593     LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
594     if (Op.getValueType().isVector() &&
595         (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
596          Load->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
597          (Load->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
598           Op.getValueType().getVectorNumElements() > 4))) {
599       SDValue MergedValues[2] = {
600         SplitVectorLoad(Op, DAG),
601         Load->getChain()
602       };
603       return DAG.getMergeValues(MergedValues, SDLoc(Op));
604     } else {
605       return LowerLOAD(Op, DAG);
606     }
607   }
608
609   case ISD::SELECT: return LowerSELECT(Op, DAG);
610   case ISD::STORE: return LowerSTORE(Op, DAG);
611   case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
612   case ISD::INTRINSIC_WO_CHAIN: {
613     unsigned IntrinsicID =
614                          cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
615     EVT VT = Op.getValueType();
616     SDLoc DL(Op);
617     //XXX: Hardcoded we only use two to store the pointer to the parameters.
618     unsigned NumUserSGPRs = 2;
619     switch (IntrinsicID) {
620     default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
621     case Intrinsic::r600_read_ngroups_x:
622       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 0, false);
623     case Intrinsic::r600_read_ngroups_y:
624       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4, false);
625     case Intrinsic::r600_read_ngroups_z:
626       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 8, false);
627     case Intrinsic::r600_read_global_size_x:
628       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 12, false);
629     case Intrinsic::r600_read_global_size_y:
630       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 16, false);
631     case Intrinsic::r600_read_global_size_z:
632       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 20, false);
633     case Intrinsic::r600_read_local_size_x:
634       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 24, false);
635     case Intrinsic::r600_read_local_size_y:
636       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 28, false);
637     case Intrinsic::r600_read_local_size_z:
638       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 32, false);
639     case Intrinsic::r600_read_tgid_x:
640       return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
641                      AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 0), VT);
642     case Intrinsic::r600_read_tgid_y:
643       return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
644                      AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 1), VT);
645     case Intrinsic::r600_read_tgid_z:
646       return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
647                      AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 2), VT);
648     case Intrinsic::r600_read_tidig_x:
649       return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
650                                   AMDGPU::VGPR0, VT);
651     case Intrinsic::r600_read_tidig_y:
652       return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
653                                   AMDGPU::VGPR1, VT);
654     case Intrinsic::r600_read_tidig_z:
655       return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
656                                   AMDGPU::VGPR2, VT);
657     case AMDGPUIntrinsic::SI_load_const: {
658       SDValue Ops [] = {
659         Op.getOperand(1),
660         Op.getOperand(2)
661       };
662
663       MachineMemOperand *MMO = MF.getMachineMemOperand(
664           MachinePointerInfo(),
665           MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
666           VT.getSizeInBits() / 8, 4);
667       return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
668                                      Op->getVTList(), Ops, VT, MMO);
669     }
670     case AMDGPUIntrinsic::SI_sample:
671       return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
672     case AMDGPUIntrinsic::SI_sampleb:
673       return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
674     case AMDGPUIntrinsic::SI_sampled:
675       return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
676     case AMDGPUIntrinsic::SI_samplel:
677       return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
678     case AMDGPUIntrinsic::SI_vs_load_input:
679       return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
680                          Op.getOperand(1),
681                          Op.getOperand(2),
682                          Op.getOperand(3));
683     }
684   }
685
686   case ISD::INTRINSIC_VOID:
687     SDValue Chain = Op.getOperand(0);
688     unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
689
690     switch (IntrinsicID) {
691       case AMDGPUIntrinsic::SI_tbuffer_store: {
692         SDLoc DL(Op);
693         SDValue Ops [] = {
694           Chain,
695           Op.getOperand(2),
696           Op.getOperand(3),
697           Op.getOperand(4),
698           Op.getOperand(5),
699           Op.getOperand(6),
700           Op.getOperand(7),
701           Op.getOperand(8),
702           Op.getOperand(9),
703           Op.getOperand(10),
704           Op.getOperand(11),
705           Op.getOperand(12),
706           Op.getOperand(13),
707           Op.getOperand(14)
708         };
709         EVT VT = Op.getOperand(3).getValueType();
710
711         MachineMemOperand *MMO = MF.getMachineMemOperand(
712             MachinePointerInfo(),
713             MachineMemOperand::MOStore,
714             VT.getSizeInBits() / 8, 4);
715         return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
716                                        Op->getVTList(), Ops, VT, MMO);
717       }
718       default:
719         break;
720     }
721   }
722   return SDValue();
723 }
724
725 /// \brief Helper function for LowerBRCOND
726 static SDNode *findUser(SDValue Value, unsigned Opcode) {
727
728   SDNode *Parent = Value.getNode();
729   for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
730        I != E; ++I) {
731
732     if (I.getUse().get() != Value)
733       continue;
734
735     if (I->getOpcode() == Opcode)
736       return *I;
737   }
738   return nullptr;
739 }
740
741 /// This transforms the control flow intrinsics to get the branch destination as
742 /// last parameter, also switches branch target with BR if the need arise
743 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
744                                       SelectionDAG &DAG) const {
745
746   SDLoc DL(BRCOND);
747
748   SDNode *Intr = BRCOND.getOperand(1).getNode();
749   SDValue Target = BRCOND.getOperand(2);
750   SDNode *BR = nullptr;
751
752   if (Intr->getOpcode() == ISD::SETCC) {
753     // As long as we negate the condition everything is fine
754     SDNode *SetCC = Intr;
755     assert(SetCC->getConstantOperandVal(1) == 1);
756     assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
757            ISD::SETNE);
758     Intr = SetCC->getOperand(0).getNode();
759
760   } else {
761     // Get the target from BR if we don't negate the condition
762     BR = findUser(BRCOND, ISD::BR);
763     Target = BR->getOperand(1);
764   }
765
766   assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
767
768   // Build the result and
769   SmallVector<EVT, 4> Res;
770   for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
771     Res.push_back(Intr->getValueType(i));
772
773   // operands of the new intrinsic call
774   SmallVector<SDValue, 4> Ops;
775   Ops.push_back(BRCOND.getOperand(0));
776   for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
777     Ops.push_back(Intr->getOperand(i));
778   Ops.push_back(Target);
779
780   // build the new intrinsic call
781   SDNode *Result = DAG.getNode(
782     Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
783     DAG.getVTList(Res), Ops).getNode();
784
785   if (BR) {
786     // Give the branch instruction our target
787     SDValue Ops[] = {
788       BR->getOperand(0),
789       BRCOND.getOperand(2)
790     };
791     DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops);
792   }
793
794   SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
795
796   // Copy the intrinsic results to registers
797   for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
798     SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
799     if (!CopyToReg)
800       continue;
801
802     Chain = DAG.getCopyToReg(
803       Chain, DL,
804       CopyToReg->getOperand(1),
805       SDValue(Result, i - 1),
806       SDValue());
807
808     DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
809   }
810
811   // Remove the old intrinsic from the chain
812   DAG.ReplaceAllUsesOfValueWith(
813     SDValue(Intr, Intr->getNumValues() - 1),
814     Intr->getOperand(0));
815
816   return Chain;
817 }
818
819 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
820   SDLoc DL(Op);
821   LoadSDNode *Load = cast<LoadSDNode>(Op);
822   SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
823   SDValue MergedValues[2];
824   MergedValues[1] = Load->getChain();
825   if (Ret.getNode()) {
826     MergedValues[0] = Ret;
827     return DAG.getMergeValues(MergedValues, DL);
828   }
829
830   if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
831     return SDValue();
832   }
833
834   EVT MemVT = Load->getMemoryVT();
835
836   assert(!MemVT.isVector() && "Private loads should be scalarized");
837   assert(!MemVT.isFloatingPoint() && "FP loads should be promoted to int");
838
839   SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
840                             DAG.getConstant(2, MVT::i32));
841   Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
842                     Load->getChain(), Ptr,
843                     DAG.getTargetConstant(0, MVT::i32),
844                     Op.getOperand(2));
845   if (MemVT.getSizeInBits() == 64) {
846     SDValue IncPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
847                                  DAG.getConstant(1, MVT::i32));
848
849     SDValue LoadUpper = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
850                                     Load->getChain(), IncPtr,
851                                     DAG.getTargetConstant(0, MVT::i32),
852                                     Op.getOperand(2));
853
854     Ret = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ret, LoadUpper);
855   }
856
857   MergedValues[0] = Ret;
858   return DAG.getMergeValues(MergedValues, DL);
859
860 }
861
862 SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
863                                                const SDValue &Op,
864                                                SelectionDAG &DAG) const {
865   return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
866                      Op.getOperand(2),
867                      Op.getOperand(3),
868                      Op.getOperand(4));
869 }
870
871 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
872   if (Op.getValueType() != MVT::i64)
873     return SDValue();
874
875   SDLoc DL(Op);
876   SDValue Cond = Op.getOperand(0);
877
878   SDValue Zero = DAG.getConstant(0, MVT::i32);
879   SDValue One = DAG.getConstant(1, MVT::i32);
880
881   SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
882   SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
883
884   SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
885   SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
886
887   SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
888
889   SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
890   SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
891
892   SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
893
894   SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
895   return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
896 }
897
898 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
899   SDLoc DL(Op);
900   StoreSDNode *Store = cast<StoreSDNode>(Op);
901   EVT VT = Store->getMemoryVT();
902
903   SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
904   if (Ret.getNode())
905     return Ret;
906
907   if (VT.isVector() && VT.getVectorNumElements() >= 8)
908       return SplitVectorStore(Op, DAG);
909
910   if (VT == MVT::i1)
911     return DAG.getTruncStore(Store->getChain(), DL,
912                         DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
913                         Store->getBasePtr(), MVT::i1, Store->getMemOperand());
914
915   if (Store->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
916     return SDValue();
917
918   SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Store->getBasePtr(),
919                             DAG.getConstant(2, MVT::i32));
920   SDValue Chain = Store->getChain();
921   SmallVector<SDValue, 8> Values;
922
923   if (Store->isTruncatingStore()) {
924     unsigned Mask = 0;
925     if (Store->getMemoryVT() == MVT::i8) {
926       Mask = 0xff;
927     } else if (Store->getMemoryVT() == MVT::i16) {
928       Mask = 0xffff;
929     }
930     SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
931                               Chain, Store->getBasePtr(),
932                               DAG.getConstant(0, MVT::i32));
933     SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getBasePtr(),
934                                   DAG.getConstant(0x3, MVT::i32));
935     SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
936                                    DAG.getConstant(3, MVT::i32));
937     SDValue MaskedValue = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getValue(),
938                                       DAG.getConstant(Mask, MVT::i32));
939     SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
940                                        MaskedValue, ShiftAmt);
941     SDValue RotrAmt = DAG.getNode(ISD::SUB, DL, MVT::i32,
942                                   DAG.getConstant(32, MVT::i32), ShiftAmt);
943     SDValue DstMask = DAG.getNode(ISD::ROTR, DL, MVT::i32,
944                                   DAG.getConstant(Mask, MVT::i32),
945                                   RotrAmt);
946     Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
947     Dst = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
948
949     Values.push_back(Dst);
950   } else if (VT == MVT::i64) {
951     for (unsigned i = 0; i < 2; ++i) {
952       Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
953                        Store->getValue(), DAG.getConstant(i, MVT::i32)));
954     }
955   } else if (VT == MVT::i128) {
956     for (unsigned i = 0; i < 2; ++i) {
957       for (unsigned j = 0; j < 2; ++j) {
958         Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
959                            DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64,
960                            Store->getValue(), DAG.getConstant(i, MVT::i32)),
961                          DAG.getConstant(j, MVT::i32)));
962       }
963     }
964   } else {
965     Values.push_back(Store->getValue());
966   }
967
968   for (unsigned i = 0; i < Values.size(); ++i) {
969     SDValue PartPtr = DAG.getNode(ISD::ADD, DL, MVT::i32,
970                                   Ptr, DAG.getConstant(i, MVT::i32));
971     Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
972                         Chain, Values[i], PartPtr,
973                         DAG.getTargetConstant(0, MVT::i32));
974   }
975   return Chain;
976 }
977
978 //===----------------------------------------------------------------------===//
979 // Custom DAG optimizations
980 //===----------------------------------------------------------------------===//
981
982 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
983                                             DAGCombinerInfo &DCI) const {
984   SelectionDAG &DAG = DCI.DAG;
985   SDLoc DL(N);
986   EVT VT = N->getValueType(0);
987
988   switch (N->getOpcode()) {
989     default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
990     case ISD::SELECT_CC: {
991       ConstantSDNode *True, *False;
992       // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
993       if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
994           && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
995           && True->isAllOnesValue()
996           && False->isNullValue()
997           && VT == MVT::i1) {
998         return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
999                            N->getOperand(1), N->getOperand(4));
1000
1001       }
1002       break;
1003     }
1004     case ISD::SETCC: {
1005       SDValue Arg0 = N->getOperand(0);
1006       SDValue Arg1 = N->getOperand(1);
1007       SDValue CC = N->getOperand(2);
1008       ConstantSDNode * C = nullptr;
1009       ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
1010
1011       // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
1012       if (VT == MVT::i1
1013           && Arg0.getOpcode() == ISD::SIGN_EXTEND
1014           && Arg0.getOperand(0).getValueType() == MVT::i1
1015           && (C = dyn_cast<ConstantSDNode>(Arg1))
1016           && C->isNullValue()
1017           && CCOp == ISD::SETNE) {
1018         return SimplifySetCC(VT, Arg0.getOperand(0),
1019                              DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
1020       }
1021       break;
1022     }
1023   }
1024
1025   return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1026 }
1027
1028 /// \brief Test if RegClass is one of the VSrc classes
1029 static bool isVSrc(unsigned RegClass) {
1030   return AMDGPU::VSrc_32RegClassID == RegClass ||
1031          AMDGPU::VSrc_64RegClassID == RegClass;
1032 }
1033
1034 /// \brief Test if RegClass is one of the SSrc classes
1035 static bool isSSrc(unsigned RegClass) {
1036   return AMDGPU::SSrc_32RegClassID == RegClass ||
1037          AMDGPU::SSrc_64RegClassID == RegClass;
1038 }
1039
1040 /// \brief Analyze the possible immediate value Op
1041 ///
1042 /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1043 /// and the immediate value if it's a literal immediate
1044 int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1045
1046   union {
1047     int32_t I;
1048     float F;
1049   } Imm;
1050
1051   if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
1052     if (Node->getZExtValue() >> 32) {
1053         return -1;
1054     }
1055     Imm.I = Node->getSExtValue();
1056   } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1057     if (N->getValueType(0) != MVT::f32)
1058       return -1;
1059     Imm.F = Node->getValueAPF().convertToFloat();
1060   } else
1061     return -1; // It isn't an immediate
1062
1063   if ((Imm.I >= -16 && Imm.I <= 64) ||
1064       Imm.F == 0.5f || Imm.F == -0.5f ||
1065       Imm.F == 1.0f || Imm.F == -1.0f ||
1066       Imm.F == 2.0f || Imm.F == -2.0f ||
1067       Imm.F == 4.0f || Imm.F == -4.0f)
1068     return 0; // It's an inline immediate
1069
1070   return Imm.I; // It's a literal immediate
1071 }
1072
1073 /// \brief Try to fold an immediate directly into an instruction
1074 bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
1075                                bool &ScalarSlotUsed) const {
1076
1077   MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
1078   const SIInstrInfo *TII =
1079     static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1080   if (!Mov || !TII->isMov(Mov->getMachineOpcode()))
1081     return false;
1082
1083   const SDValue &Op = Mov->getOperand(0);
1084   int32_t Value = analyzeImmediate(Op.getNode());
1085   if (Value == -1) {
1086     // Not an immediate at all
1087     return false;
1088
1089   } else if (Value == 0) {
1090     // Inline immediates can always be fold
1091     Operand = Op;
1092     return true;
1093
1094   } else if (Value == Immediate) {
1095     // Already fold literal immediate
1096     Operand = Op;
1097     return true;
1098
1099   } else if (!ScalarSlotUsed && !Immediate) {
1100     // Fold this literal immediate
1101     ScalarSlotUsed = true;
1102     Immediate = Value;
1103     Operand = Op;
1104     return true;
1105
1106   }
1107
1108   return false;
1109 }
1110
1111 const TargetRegisterClass *SITargetLowering::getRegClassForNode(
1112                                    SelectionDAG &DAG, const SDValue &Op) const {
1113   const SIInstrInfo *TII =
1114     static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1115   const SIRegisterInfo &TRI = TII->getRegisterInfo();
1116
1117   if (!Op->isMachineOpcode()) {
1118     switch(Op->getOpcode()) {
1119     case ISD::CopyFromReg: {
1120       MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1121       unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1122       if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1123         return MRI.getRegClass(Reg);
1124       }
1125       return TRI.getPhysRegClass(Reg);
1126     }
1127     default:  return nullptr;
1128     }
1129   }
1130   const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1131   int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1132   if (OpClassID != -1) {
1133     return TRI.getRegClass(OpClassID);
1134   }
1135   switch(Op.getMachineOpcode()) {
1136   case AMDGPU::COPY_TO_REGCLASS:
1137     // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1138     OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1139
1140     // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1141     // class, then the register class for the value could be either a
1142     // VReg or and SReg.  In order to get a more accurate
1143     if (OpClassID == AMDGPU::VSrc_32RegClassID ||
1144         OpClassID == AMDGPU::VSrc_64RegClassID) {
1145       return getRegClassForNode(DAG, Op.getOperand(0));
1146     }
1147     return TRI.getRegClass(OpClassID);
1148   case AMDGPU::EXTRACT_SUBREG: {
1149     int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1150     const TargetRegisterClass *SuperClass =
1151       getRegClassForNode(DAG, Op.getOperand(0));
1152     return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1153   }
1154   case AMDGPU::REG_SEQUENCE:
1155     // Operand 0 is the register class id for REG_SEQUENCE instructions.
1156     return TRI.getRegClass(
1157       cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1158   default:
1159     return getRegClassFor(Op.getSimpleValueType());
1160   }
1161 }
1162
1163 /// \brief Does "Op" fit into register class "RegClass" ?
1164 bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
1165                                     unsigned RegClass) const {
1166   const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1167   const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1168   if (!RC) {
1169     return false;
1170   }
1171   return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
1172 }
1173
1174 /// \brief Make sure that we don't exeed the number of allowed scalars
1175 void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
1176                                        unsigned RegClass,
1177                                        bool &ScalarSlotUsed) const {
1178
1179   // First map the operands register class to a destination class
1180   if (RegClass == AMDGPU::VSrc_32RegClassID)
1181     RegClass = AMDGPU::VReg_32RegClassID;
1182   else if (RegClass == AMDGPU::VSrc_64RegClassID)
1183     RegClass = AMDGPU::VReg_64RegClassID;
1184   else
1185     return;
1186
1187   // Nothing to do if they fit naturally
1188   if (fitsRegClass(DAG, Operand, RegClass))
1189     return;
1190
1191   // If the scalar slot isn't used yet use it now
1192   if (!ScalarSlotUsed) {
1193     ScalarSlotUsed = true;
1194     return;
1195   }
1196
1197   // This is a conservative aproach. It is possible that we can't determine the
1198   // correct register class and copy too often, but better safe than sorry.
1199   SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
1200   SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(),
1201                                     Operand.getValueType(), Operand, RC);
1202   Operand = SDValue(Node, 0);
1203 }
1204
1205 /// \returns true if \p Node's operands are different from the SDValue list
1206 /// \p Ops
1207 static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) {
1208   for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) {
1209     if (Ops[i].getNode() != Node->getOperand(i).getNode()) {
1210       return true;
1211     }
1212   }
1213   return false;
1214 }
1215
1216 /// \brief Try to fold the Nodes operands into the Node
1217 SDNode *SITargetLowering::foldOperands(MachineSDNode *Node,
1218                                        SelectionDAG &DAG) const {
1219
1220   // Original encoding (either e32 or e64)
1221   int Opcode = Node->getMachineOpcode();
1222   const SIInstrInfo *TII =
1223     static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1224   const MCInstrDesc *Desc = &TII->get(Opcode);
1225
1226   unsigned NumDefs = Desc->getNumDefs();
1227   unsigned NumOps = Desc->getNumOperands();
1228
1229   // Commuted opcode if available
1230   int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1;
1231   const MCInstrDesc *DescRev = OpcodeRev == -1 ? nullptr : &TII->get(OpcodeRev);
1232
1233   assert(!DescRev || DescRev->getNumDefs() == NumDefs);
1234   assert(!DescRev || DescRev->getNumOperands() == NumOps);
1235
1236   // e64 version if available, -1 otherwise
1237   int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
1238   const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? nullptr : &TII->get(OpcodeE64);
1239   int InputModifiers[3] = {0};
1240
1241   assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
1242
1243   int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
1244   bool HaveVSrc = false, HaveSSrc = false;
1245
1246   // First figure out what we already have in this instruction.
1247   for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1248        i != e && Op < NumOps; ++i, ++Op) {
1249
1250     unsigned RegClass = Desc->OpInfo[Op].RegClass;
1251     if (isVSrc(RegClass))
1252       HaveVSrc = true;
1253     else if (isSSrc(RegClass))
1254       HaveSSrc = true;
1255     else
1256       continue;
1257
1258     int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
1259     if (Imm != -1 && Imm != 0) {
1260       // Literal immediate
1261       Immediate = Imm;
1262     }
1263   }
1264
1265   // If we neither have VSrc nor SSrc, it makes no sense to continue.
1266   if (!HaveVSrc && !HaveSSrc)
1267     return Node;
1268
1269   // No scalar allowed when we have both VSrc and SSrc
1270   bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
1271
1272   // Second go over the operands and try to fold them
1273   std::vector<SDValue> Ops;
1274   bool Promote2e64 = false;
1275   for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1276        i != e && Op < NumOps; ++i, ++Op) {
1277
1278     const SDValue &Operand = Node->getOperand(i);
1279     Ops.push_back(Operand);
1280
1281     // Already folded immediate?
1282     if (isa<ConstantSDNode>(Operand.getNode()) ||
1283         isa<ConstantFPSDNode>(Operand.getNode()))
1284       continue;
1285
1286     // Is this a VSrc or SSrc operand?
1287     unsigned RegClass = Desc->OpInfo[Op].RegClass;
1288     if (isVSrc(RegClass) || isSSrc(RegClass)) {
1289       // Try to fold the immediates
1290       if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
1291         // Folding didn't work, make sure we don't hit the SReg limit.
1292         ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
1293       }
1294       continue;
1295     }
1296
1297     if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) {
1298
1299       unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass;
1300       assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass));
1301
1302       // Test if it makes sense to swap operands
1303       if (foldImm(Ops[1], Immediate, ScalarSlotUsed) ||
1304           (!fitsRegClass(DAG, Ops[1], RegClass) &&
1305            fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1306
1307         // Swap commutable operands
1308         std::swap(Ops[0], Ops[1]);
1309
1310         Desc = DescRev;
1311         DescRev = nullptr;
1312         continue;
1313       }
1314     }
1315
1316     if (Immediate)
1317       continue;
1318
1319     if (DescE64) {
1320       // Test if it makes sense to switch to e64 encoding
1321       unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass;
1322       if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass))
1323         continue;
1324
1325       int32_t TmpImm = -1;
1326       if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) ||
1327           (!fitsRegClass(DAG, Ops[i], RegClass) &&
1328            fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1329
1330         // Switch to e64 encoding
1331         Immediate = -1;
1332         Promote2e64 = true;
1333         Desc = DescE64;
1334         DescE64 = nullptr;
1335       }
1336     }
1337
1338     if (!DescE64 && !Promote2e64)
1339       continue;
1340     if (!Operand.isMachineOpcode())
1341       continue;
1342     if (Operand.getMachineOpcode() == AMDGPU::FNEG_SI) {
1343       Ops.pop_back();
1344       Ops.push_back(Operand.getOperand(0));
1345       InputModifiers[i] = 1;
1346       Promote2e64 = true;
1347       if (!DescE64)
1348         continue;
1349       Desc = DescE64;
1350       DescE64 = nullptr;
1351     }
1352     else if (Operand.getMachineOpcode() == AMDGPU::FABS_SI) {
1353       Ops.pop_back();
1354       Ops.push_back(Operand.getOperand(0));
1355       InputModifiers[i] = 2;
1356       Promote2e64 = true;
1357       if (!DescE64)
1358         continue;
1359       Desc = DescE64;
1360       DescE64 = nullptr;
1361     }
1362   }
1363
1364   if (Promote2e64) {
1365     std::vector<SDValue> OldOps(Ops);
1366     Ops.clear();
1367     for (unsigned i = 0; i < OldOps.size(); ++i) {
1368       // src_modifier
1369       Ops.push_back(DAG.getTargetConstant(InputModifiers[i], MVT::i32));
1370       Ops.push_back(OldOps[i]);
1371     }
1372     // Add the modifier flags while promoting
1373     for (unsigned i = 0; i < 2; ++i)
1374       Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
1375   }
1376
1377   // Add optional chain and glue
1378   for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
1379     Ops.push_back(Node->getOperand(i));
1380
1381   // Nodes that have a glue result are not CSE'd by getMachineNode(), so in
1382   // this case a brand new node is always be created, even if the operands
1383   // are the same as before.  So, manually check if anything has been changed.
1384   if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) {
1385     return Node;
1386   }
1387
1388   // Create a complete new instruction
1389   return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops);
1390 }
1391
1392 /// \brief Helper function for adjustWritemask
1393 static unsigned SubIdx2Lane(unsigned Idx) {
1394   switch (Idx) {
1395   default: return 0;
1396   case AMDGPU::sub0: return 0;
1397   case AMDGPU::sub1: return 1;
1398   case AMDGPU::sub2: return 2;
1399   case AMDGPU::sub3: return 3;
1400   }
1401 }
1402
1403 /// \brief Adjust the writemask of MIMG instructions
1404 void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1405                                        SelectionDAG &DAG) const {
1406   SDNode *Users[4] = { };
1407   unsigned Lane = 0;
1408   unsigned OldDmask = Node->getConstantOperandVal(0);
1409   unsigned NewDmask = 0;
1410
1411   // Try to figure out the used register components
1412   for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1413        I != E; ++I) {
1414
1415     // Abort if we can't understand the usage
1416     if (!I->isMachineOpcode() ||
1417         I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1418       return;
1419
1420     // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1421     // Note that subregs are packed, i.e. Lane==0 is the first bit set
1422     // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1423     // set, etc.
1424     Lane = SubIdx2Lane(I->getConstantOperandVal(1));
1425
1426     // Set which texture component corresponds to the lane.
1427     unsigned Comp;
1428     for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1429       assert(Dmask);
1430       Comp = countTrailingZeros(Dmask);
1431       Dmask &= ~(1 << Comp);
1432     }
1433
1434     // Abort if we have more than one user per component
1435     if (Users[Lane])
1436       return;
1437
1438     Users[Lane] = *I;
1439     NewDmask |= 1 << Comp;
1440   }
1441
1442   // Abort if there's no change
1443   if (NewDmask == OldDmask)
1444     return;
1445
1446   // Adjust the writemask in the node
1447   std::vector<SDValue> Ops;
1448   Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
1449   for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1450     Ops.push_back(Node->getOperand(i));
1451   Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
1452
1453   // If we only got one lane, replace it with a copy
1454   // (if NewDmask has only one bit set...)
1455   if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
1456     SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32);
1457     SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1458                                       SDLoc(), Users[Lane]->getValueType(0),
1459                                       SDValue(Node, 0), RC);
1460     DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1461     return;
1462   }
1463
1464   // Update the users of the node with the new indices
1465   for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1466
1467     SDNode *User = Users[i];
1468     if (!User)
1469       continue;
1470
1471     SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1472     DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1473
1474     switch (Idx) {
1475     default: break;
1476     case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1477     case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1478     case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1479     }
1480   }
1481 }
1482
1483 /// \brief Fold the instructions after selecting them.
1484 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1485                                           SelectionDAG &DAG) const {
1486   const SIInstrInfo *TII =
1487       static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1488   Node = AdjustRegClass(Node, DAG);
1489
1490   if (TII->isMIMG(Node->getMachineOpcode()))
1491     adjustWritemask(Node, DAG);
1492
1493   return foldOperands(Node, DAG);
1494 }
1495
1496 /// \brief Assign the register class depending on the number of
1497 /// bits set in the writemask
1498 void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1499                                                      SDNode *Node) const {
1500   const SIInstrInfo *TII =
1501       static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1502   if (!TII->isMIMG(MI->getOpcode()))
1503     return;
1504
1505   unsigned VReg = MI->getOperand(0).getReg();
1506   unsigned Writemask = MI->getOperand(1).getImm();
1507   unsigned BitsSet = 0;
1508   for (unsigned i = 0; i < 4; ++i)
1509     BitsSet += Writemask & (1 << i) ? 1 : 0;
1510
1511   const TargetRegisterClass *RC;
1512   switch (BitsSet) {
1513   default: return;
1514   case 1:  RC = &AMDGPU::VReg_32RegClass; break;
1515   case 2:  RC = &AMDGPU::VReg_64RegClass; break;
1516   case 3:  RC = &AMDGPU::VReg_96RegClass; break;
1517   }
1518
1519   unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
1520   MI->setDesc(TII->get(NewOpcode));
1521   MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1522   MRI.setRegClass(VReg, RC);
1523 }
1524
1525 MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
1526                                                 SelectionDAG &DAG) const {
1527
1528   SDLoc DL(N);
1529   unsigned NewOpcode = N->getMachineOpcode();
1530
1531   switch (N->getMachineOpcode()) {
1532   default: return N;
1533   case AMDGPU::S_LOAD_DWORD_IMM:
1534     NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
1535     // Fall-through
1536   case AMDGPU::S_LOAD_DWORDX2_SGPR:
1537     if (NewOpcode == N->getMachineOpcode()) {
1538       NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
1539     }
1540     // Fall-through
1541   case AMDGPU::S_LOAD_DWORDX4_IMM:
1542   case AMDGPU::S_LOAD_DWORDX4_SGPR: {
1543     if (NewOpcode == N->getMachineOpcode()) {
1544       NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
1545     }
1546     if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
1547       return N;
1548     }
1549     ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
1550     SDValue Ops[] = {
1551       SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128,
1552                                  DAG.getConstant(0, MVT::i64)), 0),
1553       N->getOperand(0),
1554       DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32)
1555     };
1556     return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
1557   }
1558   }
1559 }
1560
1561 SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
1562                                                const TargetRegisterClass *RC,
1563                                                unsigned Reg, EVT VT) const {
1564   SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
1565
1566   return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
1567                             cast<RegisterSDNode>(VReg)->getReg(), VT);
1568 }