R600: Always implement both versions of isTruncateFree and add a sanity check.
[oota-llvm.git] / lib / Target / R600 / AMDGPUISelLowering.cpp
1 //===-- AMDGPUISelLowering.cpp - AMDGPU Common DAG lowering functions -----===//
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 This is the parent TargetLowering class for hardware code gen
12 /// targets.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "AMDGPUISelLowering.h"
17 #include "AMDGPU.h"
18 #include "AMDGPUFrameLowering.h"
19 #include "AMDGPURegisterInfo.h"
20 #include "AMDGPUSubtarget.h"
21 #include "AMDILIntrinsicInfo.h"
22 #include "R600MachineFunctionInfo.h"
23 #include "SIMachineFunctionInfo.h"
24 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/CodeGen/CallingConvLower.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/SelectionDAG.h"
29 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
30 #include "llvm/IR/DataLayout.h"
31
32 using namespace llvm;
33 static bool allocateStack(unsigned ValNo, MVT ValVT, MVT LocVT,
34                       CCValAssign::LocInfo LocInfo,
35                       ISD::ArgFlagsTy ArgFlags, CCState &State) {
36   unsigned Offset = State.AllocateStack(ValVT.getStoreSize(),
37                                         ArgFlags.getOrigAlign());
38   State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
39
40   return true;
41 }
42
43 #include "AMDGPUGenCallingConv.inc"
44
45 AMDGPUTargetLowering::AMDGPUTargetLowering(TargetMachine &TM) :
46   TargetLowering(TM, new TargetLoweringObjectFileELF()) {
47
48   // Initialize target lowering borrowed from AMDIL
49   InitAMDILLowering();
50
51   // We need to custom lower some of the intrinsics
52   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
53
54   // Library functions.  These default to Expand, but we have instructions
55   // for them.
56   setOperationAction(ISD::FCEIL,  MVT::f32, Legal);
57   setOperationAction(ISD::FEXP2,  MVT::f32, Legal);
58   setOperationAction(ISD::FPOW,   MVT::f32, Legal);
59   setOperationAction(ISD::FLOG2,  MVT::f32, Legal);
60   setOperationAction(ISD::FABS,   MVT::f32, Legal);
61   setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
62   setOperationAction(ISD::FRINT,  MVT::f32, Legal);
63   setOperationAction(ISD::FROUND, MVT::f32, Legal);
64   setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
65
66   // The hardware supports ROTR, but not ROTL
67   setOperationAction(ISD::ROTL, MVT::i32, Expand);
68
69   // Lower floating point store/load to integer store/load to reduce the number
70   // of patterns in tablegen.
71   setOperationAction(ISD::STORE, MVT::f32, Promote);
72   AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
73
74   setOperationAction(ISD::STORE, MVT::v2f32, Promote);
75   AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
76
77   setOperationAction(ISD::STORE, MVT::v4f32, Promote);
78   AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
79
80   setOperationAction(ISD::STORE, MVT::v8f32, Promote);
81   AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32);
82
83   setOperationAction(ISD::STORE, MVT::v16f32, Promote);
84   AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32);
85
86   setOperationAction(ISD::STORE, MVT::f64, Promote);
87   AddPromotedToType(ISD::STORE, MVT::f64, MVT::i64);
88
89   // Custom lowering of vector stores is required for local address space
90   // stores.
91   setOperationAction(ISD::STORE, MVT::v4i32, Custom);
92   // XXX: Native v2i32 local address space stores are possible, but not
93   // currently implemented.
94   setOperationAction(ISD::STORE, MVT::v2i32, Custom);
95
96   setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom);
97   setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom);
98   setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom);
99   // XXX: This can be change to Custom, once ExpandVectorStores can
100   // handle 64-bit stores.
101   setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
102
103   setOperationAction(ISD::LOAD, MVT::f32, Promote);
104   AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
105
106   setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
107   AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
108
109   setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
110   AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
111
112   setOperationAction(ISD::LOAD, MVT::v8f32, Promote);
113   AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32);
114
115   setOperationAction(ISD::LOAD, MVT::v16f32, Promote);
116   AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32);
117
118   setOperationAction(ISD::LOAD, MVT::f64, Promote);
119   AddPromotedToType(ISD::LOAD, MVT::f64, MVT::i64);
120
121   setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
122   setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
123   setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
124   setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
125
126   setLoadExtAction(ISD::EXTLOAD, MVT::v2i8, Expand);
127   setLoadExtAction(ISD::SEXTLOAD, MVT::v2i8, Expand);
128   setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i8, Expand);
129   setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
130   setLoadExtAction(ISD::SEXTLOAD, MVT::v4i8, Expand);
131   setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i8, Expand);
132   setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, Expand);
133   setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, Expand);
134   setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, Expand);
135   setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, Expand);
136   setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, Expand);
137   setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, Expand);
138
139   setOperationAction(ISD::BR_CC, MVT::i1, Expand);
140
141   setOperationAction(ISD::FNEG, MVT::v2f32, Expand);
142   setOperationAction(ISD::FNEG, MVT::v4f32, Expand);
143
144   setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
145
146   setOperationAction(ISD::MUL, MVT::i64, Expand);
147
148   setOperationAction(ISD::UDIV, MVT::i32, Expand);
149   setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
150   setOperationAction(ISD::UREM, MVT::i32, Expand);
151   setOperationAction(ISD::VSELECT, MVT::v2f32, Expand);
152   setOperationAction(ISD::VSELECT, MVT::v4f32, Expand);
153
154   static const MVT::SimpleValueType IntTypes[] = {
155     MVT::v2i32, MVT::v4i32
156   };
157   const size_t NumIntTypes = array_lengthof(IntTypes);
158
159   for (unsigned int x  = 0; x < NumIntTypes; ++x) {
160     MVT::SimpleValueType VT = IntTypes[x];
161     //Expand the following operations for the current type by default
162     setOperationAction(ISD::ADD,  VT, Expand);
163     setOperationAction(ISD::AND,  VT, Expand);
164     setOperationAction(ISD::FP_TO_SINT, VT, Expand);
165     setOperationAction(ISD::FP_TO_UINT, VT, Expand);
166     setOperationAction(ISD::MUL,  VT, Expand);
167     setOperationAction(ISD::OR,   VT, Expand);
168     setOperationAction(ISD::SHL,  VT, Expand);
169     setOperationAction(ISD::SINT_TO_FP, VT, Expand);
170     setOperationAction(ISD::SRL,  VT, Expand);
171     setOperationAction(ISD::SRA,  VT, Expand);
172     setOperationAction(ISD::SUB,  VT, Expand);
173     setOperationAction(ISD::UDIV, VT, Expand);
174     setOperationAction(ISD::UINT_TO_FP, VT, Expand);
175     setOperationAction(ISD::UREM, VT, Expand);
176     setOperationAction(ISD::VSELECT, VT, Expand);
177     setOperationAction(ISD::XOR,  VT, Expand);
178   }
179
180   static const MVT::SimpleValueType FloatTypes[] = {
181     MVT::v2f32, MVT::v4f32
182   };
183   const size_t NumFloatTypes = array_lengthof(FloatTypes);
184
185   for (unsigned int x = 0; x < NumFloatTypes; ++x) {
186     MVT::SimpleValueType VT = FloatTypes[x];
187     setOperationAction(ISD::FABS, VT, Expand);
188     setOperationAction(ISD::FADD, VT, Expand);
189     setOperationAction(ISD::FDIV, VT, Expand);
190     setOperationAction(ISD::FPOW, VT, Expand);
191     setOperationAction(ISD::FFLOOR, VT, Expand);
192     setOperationAction(ISD::FTRUNC, VT, Expand);
193     setOperationAction(ISD::FMUL, VT, Expand);
194     setOperationAction(ISD::FRINT, VT, Expand);
195     setOperationAction(ISD::FSQRT, VT, Expand);
196     setOperationAction(ISD::FSUB, VT, Expand);
197   }
198 }
199
200 //===----------------------------------------------------------------------===//
201 // Target Information
202 //===----------------------------------------------------------------------===//
203
204 MVT AMDGPUTargetLowering::getVectorIdxTy() const {
205   return MVT::i32;
206 }
207
208 bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy,
209                                                    EVT CastTy) const {
210   if (LoadTy.getSizeInBits() != CastTy.getSizeInBits())
211     return true;
212
213   unsigned LScalarSize = LoadTy.getScalarType().getSizeInBits();
214   unsigned CastScalarSize = CastTy.getScalarType().getSizeInBits();
215
216   return ((LScalarSize <= CastScalarSize) ||
217           (CastScalarSize >= 32) ||
218           (LScalarSize < 32));
219 }
220
221 //===---------------------------------------------------------------------===//
222 // Target Properties
223 //===---------------------------------------------------------------------===//
224
225 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
226   assert(VT.isFloatingPoint());
227   return VT == MVT::f32;
228 }
229
230 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
231   assert(VT.isFloatingPoint());
232   return VT == MVT::f32;
233 }
234
235 bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const {
236   // Truncate is just accessing a subregister.
237   return Dest.bitsLT(Source) && (Dest.getSizeInBits() % 32 == 0);
238 }
239
240 bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const {
241   // Truncate is just accessing a subregister.
242   return Dest->getPrimitiveSizeInBits() < Source->getPrimitiveSizeInBits() &&
243          (Dest->getPrimitiveSizeInBits() % 32 == 0);
244 }
245
246 //===---------------------------------------------------------------------===//
247 // TargetLowering Callbacks
248 //===---------------------------------------------------------------------===//
249
250 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
251                              const SmallVectorImpl<ISD::InputArg> &Ins) const {
252
253   State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
254 }
255
256 SDValue AMDGPUTargetLowering::LowerReturn(
257                                      SDValue Chain,
258                                      CallingConv::ID CallConv,
259                                      bool isVarArg,
260                                      const SmallVectorImpl<ISD::OutputArg> &Outs,
261                                      const SmallVectorImpl<SDValue> &OutVals,
262                                      SDLoc DL, SelectionDAG &DAG) const {
263   return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain);
264 }
265
266 //===---------------------------------------------------------------------===//
267 // Target specific lowering
268 //===---------------------------------------------------------------------===//
269
270 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
271     const {
272   switch (Op.getOpcode()) {
273   default:
274     Op.getNode()->dump();
275     llvm_unreachable("Custom lowering code for this"
276                      "instruction is not implemented yet!");
277     break;
278   // AMDIL DAG lowering
279   case ISD::SDIV: return LowerSDIV(Op, DAG);
280   case ISD::SREM: return LowerSREM(Op, DAG);
281   case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
282   case ISD::BRCOND: return LowerBRCOND(Op, DAG);
283   // AMDGPU DAG lowering
284   case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
285   case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
286   case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
287   case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
288   case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
289   case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
290   }
291   return Op;
292 }
293
294 SDValue AMDGPUTargetLowering::LowerConstantInitializer(const Constant* Init,
295                                                        const GlobalValue *GV,
296                                                        const SDValue &InitPtr,
297                                                        SDValue Chain,
298                                                        SelectionDAG &DAG) const {
299   const DataLayout *TD = getTargetMachine().getDataLayout();
300   SDLoc DL(InitPtr);
301   if (const ConstantInt *CI = dyn_cast<ConstantInt>(Init)) {
302     EVT VT = EVT::getEVT(CI->getType());
303     PointerType *PtrTy = PointerType::get(CI->getType(), 0);
304     return DAG.getStore(Chain, DL,  DAG.getConstant(*CI, VT), InitPtr,
305                  MachinePointerInfo(UndefValue::get(PtrTy)), false, false,
306                  TD->getPrefTypeAlignment(CI->getType()));
307   } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Init)) {
308     EVT VT = EVT::getEVT(CFP->getType());
309     PointerType *PtrTy = PointerType::get(CFP->getType(), 0);
310     return DAG.getStore(Chain, DL, DAG.getConstantFP(*CFP, VT), InitPtr,
311                  MachinePointerInfo(UndefValue::get(PtrTy)), false, false,
312                  TD->getPrefTypeAlignment(CFP->getType()));
313   } else if (Init->getType()->isAggregateType()) {
314     EVT PtrVT = InitPtr.getValueType();
315     unsigned NumElements = Init->getType()->getArrayNumElements();
316     SmallVector<SDValue, 8> Chains;
317     for (unsigned i = 0; i < NumElements; ++i) {
318       SDValue Offset = DAG.getConstant(i * TD->getTypeAllocSize(
319           Init->getType()->getArrayElementType()), PtrVT);
320       SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, InitPtr, Offset);
321       Chains.push_back(LowerConstantInitializer(Init->getAggregateElement(i),
322                        GV, Ptr, Chain, DAG));
323     }
324     return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
325                        Chains.size());
326   } else {
327     Init->dump();
328     llvm_unreachable("Unhandled constant initializer");
329   }
330 }
331
332 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
333                                                  SDValue Op,
334                                                  SelectionDAG &DAG) const {
335
336   const DataLayout *TD = getTargetMachine().getDataLayout();
337   GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
338   const GlobalValue *GV = G->getGlobal();
339
340   switch (G->getAddressSpace()) {
341   default: llvm_unreachable("Global Address lowering not implemented for this "
342                             "address space");
343   case AMDGPUAS::LOCAL_ADDRESS: {
344     // XXX: What does the value of G->getOffset() mean?
345     assert(G->getOffset() == 0 &&
346          "Do not know what to do with an non-zero offset");
347
348     unsigned Offset;
349     if (MFI->LocalMemoryObjects.count(GV) == 0) {
350       uint64_t Size = TD->getTypeAllocSize(GV->getType()->getElementType());
351       Offset = MFI->LDSSize;
352       MFI->LocalMemoryObjects[GV] = Offset;
353       // XXX: Account for alignment?
354       MFI->LDSSize += Size;
355     } else {
356       Offset = MFI->LocalMemoryObjects[GV];
357     }
358
359     return DAG.getConstant(Offset, getPointerTy(G->getAddressSpace()));
360   }
361   case AMDGPUAS::CONSTANT_ADDRESS: {
362     MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
363     Type *EltType = GV->getType()->getElementType();
364     unsigned Size = TD->getTypeAllocSize(EltType);
365     unsigned Alignment = TD->getPrefTypeAlignment(EltType);
366
367     const GlobalVariable *Var = dyn_cast<GlobalVariable>(GV);
368     const Constant *Init = Var->getInitializer();
369     int FI = FrameInfo->CreateStackObject(Size, Alignment, false);
370     SDValue InitPtr = DAG.getFrameIndex(FI,
371         getPointerTy(AMDGPUAS::PRIVATE_ADDRESS));
372     SmallVector<SDNode*, 8> WorkList;
373
374     for (SDNode::use_iterator I = DAG.getEntryNode()->use_begin(),
375                               E = DAG.getEntryNode()->use_end(); I != E; ++I) {
376       if (I->getOpcode() != AMDGPUISD::REGISTER_LOAD && I->getOpcode() != ISD::LOAD)
377         continue;
378       WorkList.push_back(*I);
379     }
380     SDValue Chain = LowerConstantInitializer(Init, GV, InitPtr, DAG.getEntryNode(), DAG);
381     for (SmallVector<SDNode*, 8>::iterator I = WorkList.begin(),
382                                            E = WorkList.end(); I != E; ++I) {
383       SmallVector<SDValue, 8> Ops;
384       Ops.push_back(Chain);
385       for (unsigned i = 1; i < (*I)->getNumOperands(); ++i) {
386         Ops.push_back((*I)->getOperand(i));
387       }
388       DAG.UpdateNodeOperands(*I, &Ops[0], Ops.size());
389     }
390     return DAG.getZExtOrTrunc(InitPtr, SDLoc(Op),
391         getPointerTy(AMDGPUAS::CONSTANT_ADDRESS));
392   }
393   }
394 }
395
396 void AMDGPUTargetLowering::ExtractVectorElements(SDValue Op, SelectionDAG &DAG,
397                                          SmallVectorImpl<SDValue> &Args,
398                                          unsigned Start,
399                                          unsigned Count) const {
400   EVT VT = Op.getValueType();
401   for (unsigned i = Start, e = Start + Count; i != e; ++i) {
402     Args.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
403                                VT.getVectorElementType(),
404                                Op, DAG.getConstant(i, MVT::i32)));
405   }
406 }
407
408 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
409                                                   SelectionDAG &DAG) const {
410   SmallVector<SDValue, 8> Args;
411   SDValue A = Op.getOperand(0);
412   SDValue B = Op.getOperand(1);
413
414   ExtractVectorElements(A, DAG, Args, 0,
415                         A.getValueType().getVectorNumElements());
416   ExtractVectorElements(B, DAG, Args, 0,
417                         B.getValueType().getVectorNumElements());
418
419   return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(),
420                      &Args[0], Args.size());
421 }
422
423 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
424                                                      SelectionDAG &DAG) const {
425
426   SmallVector<SDValue, 8> Args;
427   EVT VT = Op.getValueType();
428   unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
429   ExtractVectorElements(Op.getOperand(0), DAG, Args, Start,
430                         VT.getVectorNumElements());
431
432   return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(),
433                      &Args[0], Args.size());
434 }
435
436 SDValue AMDGPUTargetLowering::LowerFrameIndex(SDValue Op,
437                                               SelectionDAG &DAG) const {
438
439   MachineFunction &MF = DAG.getMachineFunction();
440   const AMDGPUFrameLowering *TFL =
441    static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
442
443   FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op);
444   assert(FIN);
445
446   unsigned FrameIndex = FIN->getIndex();
447   unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
448   return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF),
449                          Op.getValueType());
450 }
451
452 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
453     SelectionDAG &DAG) const {
454   unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
455   SDLoc DL(Op);
456   EVT VT = Op.getValueType();
457
458   switch (IntrinsicID) {
459     default: return Op;
460     case AMDGPUIntrinsic::AMDIL_abs:
461       return LowerIntrinsicIABS(Op, DAG);
462     case AMDGPUIntrinsic::AMDIL_exp:
463       return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1));
464     case AMDGPUIntrinsic::AMDGPU_lrp:
465       return LowerIntrinsicLRP(Op, DAG);
466     case AMDGPUIntrinsic::AMDIL_fraction:
467       return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
468     case AMDGPUIntrinsic::AMDIL_max:
469       return DAG.getNode(AMDGPUISD::FMAX, DL, VT, Op.getOperand(1),
470                                                   Op.getOperand(2));
471     case AMDGPUIntrinsic::AMDGPU_imax:
472       return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Op.getOperand(1),
473                                                   Op.getOperand(2));
474     case AMDGPUIntrinsic::AMDGPU_umax:
475       return DAG.getNode(AMDGPUISD::UMAX, DL, VT, Op.getOperand(1),
476                                                   Op.getOperand(2));
477     case AMDGPUIntrinsic::AMDIL_min:
478       return DAG.getNode(AMDGPUISD::FMIN, DL, VT, Op.getOperand(1),
479                                                   Op.getOperand(2));
480     case AMDGPUIntrinsic::AMDGPU_imin:
481       return DAG.getNode(AMDGPUISD::SMIN, DL, VT, Op.getOperand(1),
482                                                   Op.getOperand(2));
483     case AMDGPUIntrinsic::AMDGPU_umin:
484       return DAG.getNode(AMDGPUISD::UMIN, DL, VT, Op.getOperand(1),
485                                                   Op.getOperand(2));
486     case AMDGPUIntrinsic::AMDIL_round_nearest:
487       return DAG.getNode(ISD::FRINT, DL, VT, Op.getOperand(1));
488   }
489 }
490
491 ///IABS(a) = SMAX(sub(0, a), a)
492 SDValue AMDGPUTargetLowering::LowerIntrinsicIABS(SDValue Op,
493     SelectionDAG &DAG) const {
494
495   SDLoc DL(Op);
496   EVT VT = Op.getValueType();
497   SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
498                                               Op.getOperand(1));
499
500   return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Neg, Op.getOperand(1));
501 }
502
503 /// Linear Interpolation
504 /// LRP(a, b, c) = muladd(a,  b, (1 - a) * c)
505 SDValue AMDGPUTargetLowering::LowerIntrinsicLRP(SDValue Op,
506     SelectionDAG &DAG) const {
507   SDLoc DL(Op);
508   EVT VT = Op.getValueType();
509   SDValue OneSubA = DAG.getNode(ISD::FSUB, DL, VT,
510                                 DAG.getConstantFP(1.0f, MVT::f32),
511                                 Op.getOperand(1));
512   SDValue OneSubAC = DAG.getNode(ISD::FMUL, DL, VT, OneSubA,
513                                                     Op.getOperand(3));
514   return DAG.getNode(ISD::FADD, DL, VT,
515       DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), Op.getOperand(2)),
516       OneSubAC);
517 }
518
519 /// \brief Generate Min/Max node
520 SDValue AMDGPUTargetLowering::LowerMinMax(SDValue Op,
521     SelectionDAG &DAG) const {
522   SDLoc DL(Op);
523   EVT VT = Op.getValueType();
524
525   SDValue LHS = Op.getOperand(0);
526   SDValue RHS = Op.getOperand(1);
527   SDValue True = Op.getOperand(2);
528   SDValue False = Op.getOperand(3);
529   SDValue CC = Op.getOperand(4);
530
531   if (VT != MVT::f32 ||
532       !((LHS == True && RHS == False) || (LHS == False && RHS == True))) {
533     return SDValue();
534   }
535
536   ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
537   switch (CCOpcode) {
538   case ISD::SETOEQ:
539   case ISD::SETONE:
540   case ISD::SETUNE:
541   case ISD::SETNE:
542   case ISD::SETUEQ:
543   case ISD::SETEQ:
544   case ISD::SETFALSE:
545   case ISD::SETFALSE2:
546   case ISD::SETTRUE:
547   case ISD::SETTRUE2:
548   case ISD::SETUO:
549   case ISD::SETO:
550     llvm_unreachable("Operation should already be optimised!");
551   case ISD::SETULE:
552   case ISD::SETULT:
553   case ISD::SETOLE:
554   case ISD::SETOLT:
555   case ISD::SETLE:
556   case ISD::SETLT: {
557     if (LHS == True)
558       return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
559     else
560       return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
561   }
562   case ISD::SETGT:
563   case ISD::SETGE:
564   case ISD::SETUGE:
565   case ISD::SETOGE:
566   case ISD::SETUGT:
567   case ISD::SETOGT: {
568     if (LHS == True)
569       return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
570     else
571       return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
572   }
573   case ISD::SETCC_INVALID:
574     llvm_unreachable("Invalid setcc condcode!");
575   }
576   return Op;
577 }
578
579 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue &Op,
580                                               SelectionDAG &DAG) const {
581   LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
582   EVT MemEltVT = Load->getMemoryVT().getVectorElementType();
583   EVT EltVT = Op.getValueType().getVectorElementType();
584   EVT PtrVT = Load->getBasePtr().getValueType();
585   unsigned NumElts = Load->getMemoryVT().getVectorNumElements();
586   SmallVector<SDValue, 8> Loads;
587   SDLoc SL(Op);
588
589   for (unsigned i = 0, e = NumElts; i != e; ++i) {
590     SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, Load->getBasePtr(),
591                     DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8), PtrVT));
592     Loads.push_back(DAG.getExtLoad(Load->getExtensionType(), SL, EltVT,
593                         Load->getChain(), Ptr,
594                         MachinePointerInfo(Load->getMemOperand()->getValue()),
595                         MemEltVT, Load->isVolatile(), Load->isNonTemporal(),
596                         Load->getAlignment()));
597   }
598   return DAG.getNode(ISD::BUILD_VECTOR, SL, Op.getValueType(), &Loads[0],
599                      Loads.size());
600 }
601
602 SDValue AMDGPUTargetLowering::MergeVectorStore(const SDValue &Op,
603                                                SelectionDAG &DAG) const {
604   StoreSDNode *Store = dyn_cast<StoreSDNode>(Op);
605   EVT MemVT = Store->getMemoryVT();
606   unsigned MemBits = MemVT.getSizeInBits();
607
608   // Byte stores are really expensive, so if possible, try to pack
609   // 32-bit vector truncatating store into an i32 store.
610   // XXX: We could also handle optimize other vector bitwidths
611   if (!MemVT.isVector() || MemBits > 32) {
612     return SDValue();
613   }
614
615   SDLoc DL(Op);
616   const SDValue &Value = Store->getValue();
617   EVT VT = Value.getValueType();
618   const SDValue &Ptr = Store->getBasePtr();
619   EVT MemEltVT = MemVT.getVectorElementType();
620   unsigned MemEltBits = MemEltVT.getSizeInBits();
621   unsigned MemNumElements = MemVT.getVectorNumElements();
622   EVT PackedVT = EVT::getIntegerVT(*DAG.getContext(), MemVT.getSizeInBits());
623   SDValue Mask;
624   switch(MemEltBits) {
625   case 8:
626     Mask = DAG.getConstant(0xFF, PackedVT);
627     break;
628   case 16:
629     Mask = DAG.getConstant(0xFFFF, PackedVT);
630     break;
631   default:
632     llvm_unreachable("Cannot lower this vector store");
633   }
634   SDValue PackedValue;
635   for (unsigned i = 0; i < MemNumElements; ++i) {
636     EVT ElemVT = VT.getVectorElementType();
637     SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT, Value,
638                               DAG.getConstant(i, MVT::i32));
639     Elt = DAG.getZExtOrTrunc(Elt, DL, PackedVT);
640     Elt = DAG.getNode(ISD::AND, DL, PackedVT, Elt, Mask);
641     SDValue Shift = DAG.getConstant(MemEltBits * i, PackedVT);
642     Elt = DAG.getNode(ISD::SHL, DL, PackedVT, Elt, Shift);
643     if (i == 0) {
644       PackedValue = Elt;
645     } else {
646       PackedValue = DAG.getNode(ISD::OR, DL, PackedVT, PackedValue, Elt);
647     }
648   }
649   return DAG.getStore(Store->getChain(), DL, PackedValue, Ptr,
650                       MachinePointerInfo(Store->getMemOperand()->getValue()),
651                       Store->isVolatile(),  Store->isNonTemporal(),
652                       Store->getAlignment());
653 }
654
655 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
656                                             SelectionDAG &DAG) const {
657   StoreSDNode *Store = cast<StoreSDNode>(Op);
658   EVT MemEltVT = Store->getMemoryVT().getVectorElementType();
659   EVT EltVT = Store->getValue().getValueType().getVectorElementType();
660   EVT PtrVT = Store->getBasePtr().getValueType();
661   unsigned NumElts = Store->getMemoryVT().getVectorNumElements();
662   SDLoc SL(Op);
663
664   SmallVector<SDValue, 8> Chains;
665
666   for (unsigned i = 0, e = NumElts; i != e; ++i) {
667     SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
668                               Store->getValue(), DAG.getConstant(i, MVT::i32));
669     SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT,
670                               Store->getBasePtr(),
671                             DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8),
672                                             PtrVT));
673     Chains.push_back(DAG.getTruncStore(Store->getChain(), SL, Val, Ptr,
674                          MachinePointerInfo(Store->getMemOperand()->getValue()),
675                          MemEltVT, Store->isVolatile(), Store->isNonTemporal(),
676                          Store->getAlignment()));
677   }
678   return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, &Chains[0], NumElts);
679 }
680
681 SDValue AMDGPUTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
682   SDLoc DL(Op);
683   LoadSDNode *Load = cast<LoadSDNode>(Op);
684   ISD::LoadExtType ExtType = Load->getExtensionType();
685
686   // Lower loads constant address space global variable loads
687   if (Load->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS &&
688       isa<GlobalVariable>(GetUnderlyingObject(Load->getPointerInfo().V))) {
689
690     SDValue Ptr = DAG.getZExtOrTrunc(Load->getBasePtr(), DL,
691         getPointerTy(AMDGPUAS::PRIVATE_ADDRESS));
692     Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
693         DAG.getConstant(2, MVT::i32));
694     return DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
695                        Load->getChain(), Ptr,
696                        DAG.getTargetConstant(0, MVT::i32), Op.getOperand(2));
697   }
698
699   if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS ||
700       ExtType == ISD::NON_EXTLOAD || Load->getMemoryVT().bitsGE(MVT::i32))
701     return SDValue();
702
703
704   EVT VT = Op.getValueType();
705   EVT MemVT = Load->getMemoryVT();
706   unsigned Mask = 0;
707   if (Load->getMemoryVT() == MVT::i8) {
708     Mask = 0xff;
709   } else if (Load->getMemoryVT() == MVT::i16) {
710     Mask = 0xffff;
711   }
712   SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
713                             DAG.getConstant(2, MVT::i32));
714   SDValue Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
715                             Load->getChain(), Ptr,
716                             DAG.getTargetConstant(0, MVT::i32),
717                             Op.getOperand(2));
718   SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32,
719                                 Load->getBasePtr(),
720                                 DAG.getConstant(0x3, MVT::i32));
721   SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
722                                  DAG.getConstant(3, MVT::i32));
723   Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Ret, ShiftAmt);
724   Ret = DAG.getNode(ISD::AND, DL, MVT::i32, Ret,
725                     DAG.getConstant(Mask, MVT::i32));
726   if (ExtType == ISD::SEXTLOAD) {
727     SDValue SExtShift = DAG.getConstant(
728         VT.getSizeInBits() - MemVT.getSizeInBits(), MVT::i32);
729     Ret = DAG.getNode(ISD::SHL, DL, MVT::i32, Ret, SExtShift);
730     Ret = DAG.getNode(ISD::SRA, DL, MVT::i32, Ret, SExtShift);
731   }
732
733   return Ret;
734 }
735
736 SDValue AMDGPUTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
737   SDLoc DL(Op);
738   SDValue Result = AMDGPUTargetLowering::MergeVectorStore(Op, DAG);
739   if (Result.getNode()) {
740     return Result;
741   }
742
743   StoreSDNode *Store = cast<StoreSDNode>(Op);
744   SDValue Chain = Store->getChain();
745   if ((Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
746        Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) &&
747       Store->getValue().getValueType().isVector()) {
748     return SplitVectorStore(Op, DAG);
749   }
750
751   if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS &&
752       Store->getMemoryVT().bitsLT(MVT::i32)) {
753     unsigned Mask = 0;
754     if (Store->getMemoryVT() == MVT::i8) {
755       Mask = 0xff;
756     } else if (Store->getMemoryVT() == MVT::i16) {
757       Mask = 0xffff;
758     }
759     SDValue TruncPtr = DAG.getZExtOrTrunc(Store->getBasePtr(), DL, MVT::i32);
760     SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, TruncPtr,
761                               DAG.getConstant(2, MVT::i32));
762     SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
763                               Chain, Ptr, DAG.getTargetConstant(0, MVT::i32));
764     SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, TruncPtr,
765                                   DAG.getConstant(0x3, MVT::i32));
766     SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
767                                    DAG.getConstant(3, MVT::i32));
768     SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32,
769                                     Store->getValue());
770     SDValue MaskedValue = DAG.getNode(ISD::AND, DL, MVT::i32, SExtValue,
771                                       DAG.getConstant(Mask, MVT::i32));
772     SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
773                                        MaskedValue, ShiftAmt);
774     SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32, DAG.getConstant(Mask, MVT::i32),
775                                   ShiftAmt);
776     DstMask = DAG.getNode(ISD::XOR, DL, MVT::i32, DstMask,
777                           DAG.getConstant(0xffffffff, MVT::i32));
778     Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
779
780     SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
781     return DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
782                        Chain, Value, Ptr, DAG.getTargetConstant(0, MVT::i32));
783   }
784   return SDValue();
785 }
786
787 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
788     SelectionDAG &DAG) const {
789   SDLoc DL(Op);
790   EVT VT = Op.getValueType();
791
792   SDValue Num = Op.getOperand(0);
793   SDValue Den = Op.getOperand(1);
794
795   SmallVector<SDValue, 8> Results;
796
797   // RCP =  URECIP(Den) = 2^32 / Den + e
798   // e is rounding error.
799   SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
800
801   // RCP_LO = umulo(RCP, Den) */
802   SDValue RCP_LO = DAG.getNode(ISD::UMULO, DL, VT, RCP, Den);
803
804   // RCP_HI = mulhu (RCP, Den) */
805   SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
806
807   // NEG_RCP_LO = -RCP_LO
808   SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
809                                                      RCP_LO);
810
811   // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
812   SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
813                                            NEG_RCP_LO, RCP_LO,
814                                            ISD::SETEQ);
815   // Calculate the rounding error from the URECIP instruction
816   // E = mulhu(ABS_RCP_LO, RCP)
817   SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
818
819   // RCP_A_E = RCP + E
820   SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
821
822   // RCP_S_E = RCP - E
823   SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
824
825   // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
826   SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
827                                      RCP_A_E, RCP_S_E,
828                                      ISD::SETEQ);
829   // Quotient = mulhu(Tmp0, Num)
830   SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
831
832   // Num_S_Remainder = Quotient * Den
833   SDValue Num_S_Remainder = DAG.getNode(ISD::UMULO, DL, VT, Quotient, Den);
834
835   // Remainder = Num - Num_S_Remainder
836   SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
837
838   // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
839   SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
840                                                  DAG.getConstant(-1, VT),
841                                                  DAG.getConstant(0, VT),
842                                                  ISD::SETUGE);
843   // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
844   SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
845                                                   Num_S_Remainder,
846                                                   DAG.getConstant(-1, VT),
847                                                   DAG.getConstant(0, VT),
848                                                   ISD::SETUGE);
849   // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
850   SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
851                                                Remainder_GE_Zero);
852
853   // Calculate Division result:
854
855   // Quotient_A_One = Quotient + 1
856   SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
857                                                          DAG.getConstant(1, VT));
858
859   // Quotient_S_One = Quotient - 1
860   SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
861                                                          DAG.getConstant(1, VT));
862
863   // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
864   SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
865                                      Quotient, Quotient_A_One, ISD::SETEQ);
866
867   // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
868   Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
869                             Quotient_S_One, Div, ISD::SETEQ);
870
871   // Calculate Rem result:
872
873   // Remainder_S_Den = Remainder - Den
874   SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
875
876   // Remainder_A_Den = Remainder + Den
877   SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
878
879   // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
880   SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
881                                     Remainder, Remainder_S_Den, ISD::SETEQ);
882
883   // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
884   Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
885                             Remainder_A_Den, Rem, ISD::SETEQ);
886   SDValue Ops[2];
887   Ops[0] = Div;
888   Ops[1] = Rem;
889   return DAG.getMergeValues(Ops, 2, DL);
890 }
891
892 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
893                                                SelectionDAG &DAG) const {
894   SDValue S0 = Op.getOperand(0);
895   SDLoc DL(Op);
896   if (Op.getValueType() != MVT::f32 || S0.getValueType() != MVT::i64)
897     return SDValue();
898
899   // f32 uint_to_fp i64
900   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
901                            DAG.getConstant(0, MVT::i32));
902   SDValue FloatLo = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Lo);
903   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
904                            DAG.getConstant(1, MVT::i32));
905   SDValue FloatHi = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Hi);
906   FloatHi = DAG.getNode(ISD::FMUL, DL, MVT::f32, FloatHi,
907                         DAG.getConstantFP(4294967296.0f, MVT::f32)); // 2^32
908   return DAG.getNode(ISD::FADD, DL, MVT::f32, FloatLo, FloatHi);
909
910 }
911
912 //===----------------------------------------------------------------------===//
913 // Helper functions
914 //===----------------------------------------------------------------------===//
915
916 void AMDGPUTargetLowering::getOriginalFunctionArgs(
917                                SelectionDAG &DAG,
918                                const Function *F,
919                                const SmallVectorImpl<ISD::InputArg> &Ins,
920                                SmallVectorImpl<ISD::InputArg> &OrigIns) const {
921
922   for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
923     if (Ins[i].ArgVT == Ins[i].VT) {
924       OrigIns.push_back(Ins[i]);
925       continue;
926     }
927
928     EVT VT;
929     if (Ins[i].ArgVT.isVector() && !Ins[i].VT.isVector()) {
930       // Vector has been split into scalars.
931       VT = Ins[i].ArgVT.getVectorElementType();
932     } else if (Ins[i].VT.isVector() && Ins[i].ArgVT.isVector() &&
933                Ins[i].ArgVT.getVectorElementType() !=
934                Ins[i].VT.getVectorElementType()) {
935       // Vector elements have been promoted
936       VT = Ins[i].ArgVT;
937     } else {
938       // Vector has been spilt into smaller vectors.
939       VT = Ins[i].VT;
940     }
941
942     ISD::InputArg Arg(Ins[i].Flags, VT, VT, Ins[i].Used,
943                       Ins[i].OrigArgIndex, Ins[i].PartOffset);
944     OrigIns.push_back(Arg);
945   }
946 }
947
948 bool AMDGPUTargetLowering::isHWTrueValue(SDValue Op) const {
949   if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
950     return CFP->isExactlyValue(1.0);
951   }
952   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
953     return C->isAllOnesValue();
954   }
955   return false;
956 }
957
958 bool AMDGPUTargetLowering::isHWFalseValue(SDValue Op) const {
959   if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
960     return CFP->getValueAPF().isZero();
961   }
962   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
963     return C->isNullValue();
964   }
965   return false;
966 }
967
968 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
969                                                   const TargetRegisterClass *RC,
970                                                    unsigned Reg, EVT VT) const {
971   MachineFunction &MF = DAG.getMachineFunction();
972   MachineRegisterInfo &MRI = MF.getRegInfo();
973   unsigned VirtualRegister;
974   if (!MRI.isLiveIn(Reg)) {
975     VirtualRegister = MRI.createVirtualRegister(RC);
976     MRI.addLiveIn(Reg, VirtualRegister);
977   } else {
978     VirtualRegister = MRI.getLiveInVirtReg(Reg);
979   }
980   return DAG.getRegister(VirtualRegister, VT);
981 }
982
983 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
984
985 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
986   switch (Opcode) {
987   default: return 0;
988   // AMDIL DAG nodes
989   NODE_NAME_CASE(CALL);
990   NODE_NAME_CASE(UMUL);
991   NODE_NAME_CASE(DIV_INF);
992   NODE_NAME_CASE(RET_FLAG);
993   NODE_NAME_CASE(BRANCH_COND);
994
995   // AMDGPU DAG nodes
996   NODE_NAME_CASE(DWORDADDR)
997   NODE_NAME_CASE(FRACT)
998   NODE_NAME_CASE(FMAX)
999   NODE_NAME_CASE(SMAX)
1000   NODE_NAME_CASE(UMAX)
1001   NODE_NAME_CASE(FMIN)
1002   NODE_NAME_CASE(SMIN)
1003   NODE_NAME_CASE(UMIN)
1004   NODE_NAME_CASE(URECIP)
1005   NODE_NAME_CASE(EXPORT)
1006   NODE_NAME_CASE(CONST_ADDRESS)
1007   NODE_NAME_CASE(REGISTER_LOAD)
1008   NODE_NAME_CASE(REGISTER_STORE)
1009   NODE_NAME_CASE(LOAD_CONSTANT)
1010   NODE_NAME_CASE(LOAD_INPUT)
1011   NODE_NAME_CASE(SAMPLE)
1012   NODE_NAME_CASE(SAMPLEB)
1013   NODE_NAME_CASE(SAMPLED)
1014   NODE_NAME_CASE(SAMPLEL)
1015   NODE_NAME_CASE(STORE_MSKOR)
1016   NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
1017   }
1018 }