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