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