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