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