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