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