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