R600/SI: Expand vector fp <-> int conversions
[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
32 #include "AMDGPUGenCallingConv.inc"
33
34 AMDGPUTargetLowering::AMDGPUTargetLowering(TargetMachine &TM) :
35   TargetLowering(TM, new TargetLoweringObjectFileELF()) {
36
37   // Initialize target lowering borrowed from AMDIL
38   InitAMDILLowering();
39
40   // We need to custom lower some of the intrinsics
41   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
42
43   // Library functions.  These default to Expand, but we have instructions
44   // for them.
45   setOperationAction(ISD::FCEIL,  MVT::f32, Legal);
46   setOperationAction(ISD::FEXP2,  MVT::f32, Legal);
47   setOperationAction(ISD::FPOW,   MVT::f32, Legal);
48   setOperationAction(ISD::FLOG2,  MVT::f32, Legal);
49   setOperationAction(ISD::FABS,   MVT::f32, Legal);
50   setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
51   setOperationAction(ISD::FRINT,  MVT::f32, Legal);
52
53   // The hardware supports ROTR, but not ROTL
54   setOperationAction(ISD::ROTL, MVT::i32, Expand);
55
56   // Lower floating point store/load to integer store/load to reduce the number
57   // of patterns in tablegen.
58   setOperationAction(ISD::STORE, MVT::f32, Promote);
59   AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
60
61   setOperationAction(ISD::STORE, MVT::v2f32, Promote);
62   AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
63
64   setOperationAction(ISD::STORE, MVT::v4f32, Promote);
65   AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
66
67   setOperationAction(ISD::STORE, MVT::f64, Promote);
68   AddPromotedToType(ISD::STORE, MVT::f64, MVT::i64);
69
70   setOperationAction(ISD::LOAD, MVT::f32, Promote);
71   AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
72
73   setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
74   AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
75
76   setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
77   AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
78
79   setOperationAction(ISD::LOAD, MVT::f64, Promote);
80   AddPromotedToType(ISD::LOAD, MVT::f64, MVT::i64);
81
82   setOperationAction(ISD::FNEG, MVT::v2f32, Expand);
83   setOperationAction(ISD::FNEG, MVT::v4f32, Expand);
84
85   setOperationAction(ISD::MUL, MVT::i64, Expand);
86
87   setOperationAction(ISD::UDIV, MVT::i32, Expand);
88   setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
89   setOperationAction(ISD::UREM, MVT::i32, Expand);
90   setOperationAction(ISD::VSELECT, MVT::v2f32, Expand);
91   setOperationAction(ISD::VSELECT, MVT::v4f32, Expand);
92
93   static const int types[] = {
94     (int)MVT::v2i32,
95     (int)MVT::v4i32
96   };
97   const size_t NumTypes = array_lengthof(types);
98
99   for (unsigned int x  = 0; x < NumTypes; ++x) {
100     MVT::SimpleValueType VT = (MVT::SimpleValueType)types[x];
101     //Expand the following operations for the current type by default
102     setOperationAction(ISD::ADD,  VT, Expand);
103     setOperationAction(ISD::AND,  VT, Expand);
104     setOperationAction(ISD::FP_TO_SINT, VT, Expand);
105     setOperationAction(ISD::FP_TO_UINT, VT, Expand);
106     setOperationAction(ISD::MUL,  VT, Expand);
107     setOperationAction(ISD::OR,   VT, Expand);
108     setOperationAction(ISD::SHL,  VT, Expand);
109     setOperationAction(ISD::SINT_TO_FP, VT, Expand);
110     setOperationAction(ISD::SRL,  VT, Expand);
111     setOperationAction(ISD::SRA,  VT, Expand);
112     setOperationAction(ISD::SUB,  VT, Expand);
113     setOperationAction(ISD::UDIV, VT, Expand);
114     setOperationAction(ISD::UINT_TO_FP, VT, Expand);
115     setOperationAction(ISD::UREM, VT, Expand);
116     setOperationAction(ISD::VSELECT, VT, Expand);
117     setOperationAction(ISD::XOR,  VT, Expand);
118   }
119 }
120
121 //===---------------------------------------------------------------------===//
122 // Target Properties
123 //===---------------------------------------------------------------------===//
124
125 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
126   assert(VT.isFloatingPoint());
127   return VT == MVT::f32;
128 }
129
130 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
131   assert(VT.isFloatingPoint());
132   return VT == MVT::f32;
133 }
134
135 //===---------------------------------------------------------------------===//
136 // TargetLowering Callbacks
137 //===---------------------------------------------------------------------===//
138
139 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
140                              const SmallVectorImpl<ISD::InputArg> &Ins) const {
141
142   State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
143 }
144
145 SDValue AMDGPUTargetLowering::LowerReturn(
146                                      SDValue Chain,
147                                      CallingConv::ID CallConv,
148                                      bool isVarArg,
149                                      const SmallVectorImpl<ISD::OutputArg> &Outs,
150                                      const SmallVectorImpl<SDValue> &OutVals,
151                                      SDLoc DL, SelectionDAG &DAG) const {
152   return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain);
153 }
154
155 //===---------------------------------------------------------------------===//
156 // Target specific lowering
157 //===---------------------------------------------------------------------===//
158
159 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
160     const {
161   switch (Op.getOpcode()) {
162   default:
163     Op.getNode()->dump();
164     assert(0 && "Custom lowering code for this"
165         "instruction is not implemented yet!");
166     break;
167   // AMDIL DAG lowering
168   case ISD::SDIV: return LowerSDIV(Op, DAG);
169   case ISD::SREM: return LowerSREM(Op, DAG);
170   case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
171   case ISD::BRCOND: return LowerBRCOND(Op, DAG);
172   // AMDGPU DAG lowering
173   case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
174   case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
175   }
176   return Op;
177 }
178
179 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
180                                                  SDValue Op,
181                                                  SelectionDAG &DAG) const {
182
183   const DataLayout *TD = getTargetMachine().getDataLayout();
184   GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
185   // XXX: What does the value of G->getOffset() mean?
186   assert(G->getOffset() == 0 &&
187          "Do not know what to do with an non-zero offset");
188
189   unsigned Offset = MFI->LDSSize;
190   const GlobalValue *GV = G->getGlobal();
191   uint64_t Size = TD->getTypeAllocSize(GV->getType()->getElementType());
192
193   // XXX: Account for alignment?
194   MFI->LDSSize += Size;
195
196   return DAG.getConstant(Offset, TD->getPointerSize() == 8 ? MVT::i64 : MVT::i32);
197 }
198
199 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
200     SelectionDAG &DAG) const {
201   unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
202   SDLoc DL(Op);
203   EVT VT = Op.getValueType();
204
205   switch (IntrinsicID) {
206     default: return Op;
207     case AMDGPUIntrinsic::AMDIL_abs:
208       return LowerIntrinsicIABS(Op, DAG);
209     case AMDGPUIntrinsic::AMDIL_exp:
210       return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1));
211     case AMDGPUIntrinsic::AMDGPU_lrp:
212       return LowerIntrinsicLRP(Op, DAG);
213     case AMDGPUIntrinsic::AMDIL_fraction:
214       return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
215     case AMDGPUIntrinsic::AMDIL_max:
216       return DAG.getNode(AMDGPUISD::FMAX, DL, VT, Op.getOperand(1),
217                                                   Op.getOperand(2));
218     case AMDGPUIntrinsic::AMDGPU_imax:
219       return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Op.getOperand(1),
220                                                   Op.getOperand(2));
221     case AMDGPUIntrinsic::AMDGPU_umax:
222       return DAG.getNode(AMDGPUISD::UMAX, DL, VT, Op.getOperand(1),
223                                                   Op.getOperand(2));
224     case AMDGPUIntrinsic::AMDIL_min:
225       return DAG.getNode(AMDGPUISD::FMIN, DL, VT, Op.getOperand(1),
226                                                   Op.getOperand(2));
227     case AMDGPUIntrinsic::AMDGPU_imin:
228       return DAG.getNode(AMDGPUISD::SMIN, DL, VT, Op.getOperand(1),
229                                                   Op.getOperand(2));
230     case AMDGPUIntrinsic::AMDGPU_umin:
231       return DAG.getNode(AMDGPUISD::UMIN, DL, VT, Op.getOperand(1),
232                                                   Op.getOperand(2));
233     case AMDGPUIntrinsic::AMDIL_round_nearest:
234       return DAG.getNode(ISD::FRINT, DL, VT, Op.getOperand(1));
235   }
236 }
237
238 ///IABS(a) = SMAX(sub(0, a), a)
239 SDValue AMDGPUTargetLowering::LowerIntrinsicIABS(SDValue Op,
240     SelectionDAG &DAG) const {
241
242   SDLoc DL(Op);
243   EVT VT = Op.getValueType();
244   SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
245                                               Op.getOperand(1));
246
247   return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Neg, Op.getOperand(1));
248 }
249
250 /// Linear Interpolation
251 /// LRP(a, b, c) = muladd(a,  b, (1 - a) * c)
252 SDValue AMDGPUTargetLowering::LowerIntrinsicLRP(SDValue Op,
253     SelectionDAG &DAG) const {
254   SDLoc DL(Op);
255   EVT VT = Op.getValueType();
256   SDValue OneSubA = DAG.getNode(ISD::FSUB, DL, VT,
257                                 DAG.getConstantFP(1.0f, MVT::f32),
258                                 Op.getOperand(1));
259   SDValue OneSubAC = DAG.getNode(ISD::FMUL, DL, VT, OneSubA,
260                                                     Op.getOperand(3));
261   return DAG.getNode(ISD::FADD, DL, VT,
262       DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), Op.getOperand(2)),
263       OneSubAC);
264 }
265
266 /// \brief Generate Min/Max node
267 SDValue AMDGPUTargetLowering::LowerMinMax(SDValue Op,
268     SelectionDAG &DAG) const {
269   SDLoc DL(Op);
270   EVT VT = Op.getValueType();
271
272   SDValue LHS = Op.getOperand(0);
273   SDValue RHS = Op.getOperand(1);
274   SDValue True = Op.getOperand(2);
275   SDValue False = Op.getOperand(3);
276   SDValue CC = Op.getOperand(4);
277
278   if (VT != MVT::f32 ||
279       !((LHS == True && RHS == False) || (LHS == False && RHS == True))) {
280     return SDValue();
281   }
282
283   ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
284   switch (CCOpcode) {
285   case ISD::SETOEQ:
286   case ISD::SETONE:
287   case ISD::SETUNE:
288   case ISD::SETNE:
289   case ISD::SETUEQ:
290   case ISD::SETEQ:
291   case ISD::SETFALSE:
292   case ISD::SETFALSE2:
293   case ISD::SETTRUE:
294   case ISD::SETTRUE2:
295   case ISD::SETUO:
296   case ISD::SETO:
297     assert(0 && "Operation should already be optimised !");
298   case ISD::SETULE:
299   case ISD::SETULT:
300   case ISD::SETOLE:
301   case ISD::SETOLT:
302   case ISD::SETLE:
303   case ISD::SETLT: {
304     if (LHS == True)
305       return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
306     else
307       return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
308   }
309   case ISD::SETGT:
310   case ISD::SETGE:
311   case ISD::SETUGE:
312   case ISD::SETOGE:
313   case ISD::SETUGT:
314   case ISD::SETOGT: {
315     if (LHS == True)
316       return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
317     else
318       return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
319   }
320   case ISD::SETCC_INVALID:
321     assert(0 && "Invalid setcc condcode !");
322   }
323   return Op;
324 }
325
326
327
328 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
329     SelectionDAG &DAG) const {
330   SDLoc DL(Op);
331   EVT VT = Op.getValueType();
332
333   SDValue Num = Op.getOperand(0);
334   SDValue Den = Op.getOperand(1);
335
336   SmallVector<SDValue, 8> Results;
337
338   // RCP =  URECIP(Den) = 2^32 / Den + e
339   // e is rounding error.
340   SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
341
342   // RCP_LO = umulo(RCP, Den) */
343   SDValue RCP_LO = DAG.getNode(ISD::UMULO, DL, VT, RCP, Den);
344
345   // RCP_HI = mulhu (RCP, Den) */
346   SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
347
348   // NEG_RCP_LO = -RCP_LO
349   SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
350                                                      RCP_LO);
351
352   // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
353   SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
354                                            NEG_RCP_LO, RCP_LO,
355                                            ISD::SETEQ);
356   // Calculate the rounding error from the URECIP instruction
357   // E = mulhu(ABS_RCP_LO, RCP)
358   SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
359
360   // RCP_A_E = RCP + E
361   SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
362
363   // RCP_S_E = RCP - E
364   SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
365
366   // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
367   SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
368                                      RCP_A_E, RCP_S_E,
369                                      ISD::SETEQ);
370   // Quotient = mulhu(Tmp0, Num)
371   SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
372
373   // Num_S_Remainder = Quotient * Den
374   SDValue Num_S_Remainder = DAG.getNode(ISD::UMULO, DL, VT, Quotient, Den);
375
376   // Remainder = Num - Num_S_Remainder
377   SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
378
379   // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
380   SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
381                                                  DAG.getConstant(-1, VT),
382                                                  DAG.getConstant(0, VT),
383                                                  ISD::SETGE);
384   // Remainder_GE_Zero = (Remainder >= 0 ? -1 : 0)
385   SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Remainder,
386                                                   DAG.getConstant(0, VT),
387                                                   DAG.getConstant(-1, VT),
388                                                   DAG.getConstant(0, VT),
389                                                   ISD::SETGE);
390   // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
391   SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
392                                                Remainder_GE_Zero);
393
394   // Calculate Division result:
395
396   // Quotient_A_One = Quotient + 1
397   SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
398                                                          DAG.getConstant(1, VT));
399
400   // Quotient_S_One = Quotient - 1
401   SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
402                                                          DAG.getConstant(1, VT));
403
404   // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
405   SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
406                                      Quotient, Quotient_A_One, ISD::SETEQ);
407
408   // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
409   Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
410                             Quotient_S_One, Div, ISD::SETEQ);
411
412   // Calculate Rem result:
413
414   // Remainder_S_Den = Remainder - Den
415   SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
416
417   // Remainder_A_Den = Remainder + Den
418   SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
419
420   // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
421   SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
422                                     Remainder, Remainder_S_Den, ISD::SETEQ);
423
424   // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
425   Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
426                             Remainder_A_Den, Rem, ISD::SETEQ);
427   SDValue Ops[2];
428   Ops[0] = Div;
429   Ops[1] = Rem;
430   return DAG.getMergeValues(Ops, 2, DL);
431 }
432
433 //===----------------------------------------------------------------------===//
434 // Helper functions
435 //===----------------------------------------------------------------------===//
436
437 bool AMDGPUTargetLowering::isHWTrueValue(SDValue Op) const {
438   if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
439     return CFP->isExactlyValue(1.0);
440   }
441   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
442     return C->isAllOnesValue();
443   }
444   return false;
445 }
446
447 bool AMDGPUTargetLowering::isHWFalseValue(SDValue Op) const {
448   if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
449     return CFP->getValueAPF().isZero();
450   }
451   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
452     return C->isNullValue();
453   }
454   return false;
455 }
456
457 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
458                                                   const TargetRegisterClass *RC,
459                                                    unsigned Reg, EVT VT) const {
460   MachineFunction &MF = DAG.getMachineFunction();
461   MachineRegisterInfo &MRI = MF.getRegInfo();
462   unsigned VirtualRegister;
463   if (!MRI.isLiveIn(Reg)) {
464     VirtualRegister = MRI.createVirtualRegister(RC);
465     MRI.addLiveIn(Reg, VirtualRegister);
466   } else {
467     VirtualRegister = MRI.getLiveInVirtReg(Reg);
468   }
469   return DAG.getRegister(VirtualRegister, VT);
470 }
471
472 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
473
474 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
475   switch (Opcode) {
476   default: return 0;
477   // AMDIL DAG nodes
478   NODE_NAME_CASE(CALL);
479   NODE_NAME_CASE(UMUL);
480   NODE_NAME_CASE(DIV_INF);
481   NODE_NAME_CASE(RET_FLAG);
482   NODE_NAME_CASE(BRANCH_COND);
483
484   // AMDGPU DAG nodes
485   NODE_NAME_CASE(DWORDADDR)
486   NODE_NAME_CASE(FRACT)
487   NODE_NAME_CASE(FMAX)
488   NODE_NAME_CASE(SMAX)
489   NODE_NAME_CASE(UMAX)
490   NODE_NAME_CASE(FMIN)
491   NODE_NAME_CASE(SMIN)
492   NODE_NAME_CASE(UMIN)
493   NODE_NAME_CASE(URECIP)
494   NODE_NAME_CASE(EXPORT)
495   NODE_NAME_CASE(CONST_ADDRESS)
496   NODE_NAME_CASE(REGISTER_LOAD)
497   NODE_NAME_CASE(REGISTER_STORE)
498   }
499 }