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