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