blob: dd8b73f00d53a99040a3775694b15fd095dabf3b [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"
Tom Stellarde7397ee2013-06-03 17:40:11 +000017#include "AMDGPU.h"
Christian Konig90c64cb2013-03-07 09:03:52 +000018#include "AMDGPURegisterInfo.h"
Christian Konig90c64cb2013-03-07 09:03:52 +000019#include "AMDGPUSubtarget.h"
Benjamin Kramer5c352902013-05-23 17:10:37 +000020#include "AMDILIntrinsicInfo.h"
Tom Stellardf502c292013-07-23 01:48:05 +000021#include "R600MachineFunctionInfo.h"
Tom Stellarde7397ee2013-06-03 17:40:11 +000022#include "SIMachineFunctionInfo.h"
Christian Konig90c64cb2013-03-07 09:03:52 +000023#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000024#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/SelectionDAG.h"
27#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Tom Stellarde3d4cbc2013-06-28 15:47:08 +000028#include "llvm/IR/DataLayout.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000029
30using namespace llvm;
31
Christian Konig90c64cb2013-03-07 09:03:52 +000032#include "AMDGPUGenCallingConv.inc"
33
Tom Stellardf98f2ce2012-12-11 21:25:42 +000034AMDGPUTargetLowering::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
Tom Stellardba534c22013-05-20 15:02:19 +000053 // The hardware supports ROTR, but not ROTL
54 setOperationAction(ISD::ROTL, MVT::i32, Expand);
55
Tom Stellardf98f2ce2012-12-11 21:25:42 +000056 // 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
Tom Stellardfc047272013-07-18 21:43:42 +000061 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
62 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
63
Tom Stellardf98f2ce2012-12-11 21:25:42 +000064 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
65 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
66
Tom Stellard68e13282013-07-12 18:14:56 +000067 setOperationAction(ISD::STORE, MVT::f64, Promote);
68 AddPromotedToType(ISD::STORE, MVT::f64, MVT::i64);
69
Tom Stellard4c52d452013-08-16 01:12:11 +000070 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom);
71 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom);
72 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom);
73 // XXX: This can be change to Custom, once ExpandVectorStores can
74 // handle 64-bit stores.
75 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
76
Tom Stellardf98f2ce2012-12-11 21:25:42 +000077 setOperationAction(ISD::LOAD, MVT::f32, Promote);
78 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
79
Tom Stellardac85f3f2013-07-18 21:43:48 +000080 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
81 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
82
Tom Stellardf98f2ce2012-12-11 21:25:42 +000083 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
84 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
85
Tom Stellard68e13282013-07-12 18:14:56 +000086 setOperationAction(ISD::LOAD, MVT::f64, Promote);
87 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::i64);
88
Tom Stellarda41520c2013-08-14 23:25:00 +000089 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
90 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
91 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
92 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
Tom Stellard692ee102013-08-01 15:23:42 +000093
Tom Stellard30d84d82013-08-16 01:12:16 +000094 setLoadExtAction(ISD::EXTLOAD, MVT::v2i8, Expand);
95 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i8, Expand);
96 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i8, Expand);
97 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
98 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i8, Expand);
99 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i8, Expand);
100 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, Expand);
101 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, Expand);
102 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, Expand);
103 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, Expand);
104 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, Expand);
105 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, Expand);
106
Tom Stellardd7a472c2013-07-23 01:47:46 +0000107 setOperationAction(ISD::FNEG, MVT::v2f32, Expand);
108 setOperationAction(ISD::FNEG, MVT::v4f32, Expand);
109
Christian Konig45b14e32013-03-27 09:12:51 +0000110 setOperationAction(ISD::MUL, MVT::i64, Expand);
111
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000112 setOperationAction(ISD::UDIV, MVT::i32, Expand);
113 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
114 setOperationAction(ISD::UREM, MVT::i32, Expand);
Tom Stellardf5660aa2013-07-18 21:43:35 +0000115 setOperationAction(ISD::VSELECT, MVT::v2f32, Expand);
116 setOperationAction(ISD::VSELECT, MVT::v4f32, Expand);
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000117
Tom Stellard0991c312013-08-16 23:51:24 +0000118 static const int IntTypes[] = {
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000119 (int)MVT::v2i32,
120 (int)MVT::v4i32
121 };
Tom Stellard0991c312013-08-16 23:51:24 +0000122 const size_t NumIntTypes = array_lengthof(IntTypes);
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000123
Tom Stellard0991c312013-08-16 23:51:24 +0000124 for (unsigned int x = 0; x < NumIntTypes; ++x) {
125 MVT::SimpleValueType VT = (MVT::SimpleValueType)IntTypes[x];
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000126 //Expand the following operations for the current type by default
127 setOperationAction(ISD::ADD, VT, Expand);
128 setOperationAction(ISD::AND, VT, Expand);
Tom Stellarde3d60ac2013-07-30 14:31:03 +0000129 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
130 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000131 setOperationAction(ISD::MUL, VT, Expand);
132 setOperationAction(ISD::OR, VT, Expand);
133 setOperationAction(ISD::SHL, VT, Expand);
Tom Stellarde3d60ac2013-07-30 14:31:03 +0000134 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000135 setOperationAction(ISD::SRL, VT, Expand);
136 setOperationAction(ISD::SRA, VT, Expand);
137 setOperationAction(ISD::SUB, VT, Expand);
138 setOperationAction(ISD::UDIV, VT, Expand);
Tom Stellarde3d60ac2013-07-30 14:31:03 +0000139 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000140 setOperationAction(ISD::UREM, VT, Expand);
Tom Stellardf5660aa2013-07-18 21:43:35 +0000141 setOperationAction(ISD::VSELECT, VT, Expand);
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000142 setOperationAction(ISD::XOR, VT, Expand);
143 }
Tom Stellard0991c312013-08-16 23:51:24 +0000144
145 static const int FloatTypes[] = {
146 (int)MVT::v2f32,
147 (int)MVT::v4f32
148 };
149 const size_t NumFloatTypes = array_lengthof(FloatTypes);
150
151 for (unsigned int x = 0; x < NumFloatTypes; ++x) {
152 MVT::SimpleValueType VT = (MVT::SimpleValueType)FloatTypes[x];
153 setOperationAction(ISD::FADD, VT, Expand);
154 setOperationAction(ISD::FDIV, VT, Expand);
Tom Stellard84c0bd92013-08-16 23:51:29 +0000155 setOperationAction(ISD::FFLOOR, VT, Expand);
Tom Stellard0991c312013-08-16 23:51:24 +0000156 setOperationAction(ISD::FMUL, VT, Expand);
Tom Stellard3cae8232013-08-16 23:51:33 +0000157 setOperationAction(ISD::FRINT, VT, Expand);
Tom Stellard0991c312013-08-16 23:51:24 +0000158 setOperationAction(ISD::FSUB, VT, Expand);
159 }
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000160}
161
Tom Stellard2b272a12013-08-05 22:22:07 +0000162//===----------------------------------------------------------------------===//
163// Target Information
164//===----------------------------------------------------------------------===//
165
166MVT AMDGPUTargetLowering::getVectorIdxTy() const {
167 return MVT::i32;
168}
169
170
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000171//===---------------------------------------------------------------------===//
Tom Stellard1f67c632013-07-23 23:55:03 +0000172// Target Properties
173//===---------------------------------------------------------------------===//
174
175bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
176 assert(VT.isFloatingPoint());
177 return VT == MVT::f32;
178}
179
180bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
181 assert(VT.isFloatingPoint());
182 return VT == MVT::f32;
183}
184
185//===---------------------------------------------------------------------===//
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000186// TargetLowering Callbacks
187//===---------------------------------------------------------------------===//
188
Christian Konig90c64cb2013-03-07 09:03:52 +0000189void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
190 const SmallVectorImpl<ISD::InputArg> &Ins) const {
191
192 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000193}
194
195SDValue AMDGPUTargetLowering::LowerReturn(
196 SDValue Chain,
197 CallingConv::ID CallConv,
198 bool isVarArg,
199 const SmallVectorImpl<ISD::OutputArg> &Outs,
200 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000201 SDLoc DL, SelectionDAG &DAG) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000202 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain);
203}
204
205//===---------------------------------------------------------------------===//
206// Target specific lowering
207//===---------------------------------------------------------------------===//
208
209SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
210 const {
211 switch (Op.getOpcode()) {
212 default:
213 Op.getNode()->dump();
214 assert(0 && "Custom lowering code for this"
215 "instruction is not implemented yet!");
216 break;
217 // AMDIL DAG lowering
218 case ISD::SDIV: return LowerSDIV(Op, DAG);
219 case ISD::SREM: return LowerSREM(Op, DAG);
220 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
221 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
222 // AMDGPU DAG lowering
Tom Stellarda41520c2013-08-14 23:25:00 +0000223 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
224 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000225 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Tom Stellard4c52d452013-08-16 01:12:11 +0000226 case ISD::STORE: return LowerVectorStore(Op, DAG);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000227 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
228 }
229 return Op;
230}
231
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000232SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
233 SDValue Op,
234 SelectionDAG &DAG) const {
235
236 const DataLayout *TD = getTargetMachine().getDataLayout();
237 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
238 // XXX: What does the value of G->getOffset() mean?
239 assert(G->getOffset() == 0 &&
240 "Do not know what to do with an non-zero offset");
241
242 unsigned Offset = MFI->LDSSize;
243 const GlobalValue *GV = G->getGlobal();
244 uint64_t Size = TD->getTypeAllocSize(GV->getType()->getElementType());
245
246 // XXX: Account for alignment?
247 MFI->LDSSize += Size;
248
Michel Danzera3e39dc2013-07-10 16:37:07 +0000249 return DAG.getConstant(Offset, TD->getPointerSize() == 8 ? MVT::i64 : MVT::i32);
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000250}
251
Tom Stellarda41520c2013-08-14 23:25:00 +0000252void AMDGPUTargetLowering::ExtractVectorElements(SDValue Op, SelectionDAG &DAG,
253 SmallVectorImpl<SDValue> &Args,
254 unsigned Start,
255 unsigned Count) const {
256 EVT VT = Op.getValueType();
257 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
258 Args.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
259 VT.getVectorElementType(),
260 Op, DAG.getConstant(i, MVT::i32)));
261 }
262}
263
264SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
265 SelectionDAG &DAG) const {
266 SmallVector<SDValue, 8> Args;
267 SDValue A = Op.getOperand(0);
268 SDValue B = Op.getOperand(1);
269
270 ExtractVectorElements(A, DAG, Args, 0,
271 A.getValueType().getVectorNumElements());
272 ExtractVectorElements(B, DAG, Args, 0,
273 B.getValueType().getVectorNumElements());
274
275 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(),
276 &Args[0], Args.size());
277}
278
279SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
280 SelectionDAG &DAG) const {
281
282 SmallVector<SDValue, 8> Args;
283 EVT VT = Op.getValueType();
284 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
285 ExtractVectorElements(Op.getOperand(0), DAG, Args, Start,
286 VT.getVectorNumElements());
287
288 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(),
289 &Args[0], Args.size());
290}
291
292
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000293SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
294 SelectionDAG &DAG) const {
295 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000296 SDLoc DL(Op);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000297 EVT VT = Op.getValueType();
298
299 switch (IntrinsicID) {
300 default: return Op;
301 case AMDGPUIntrinsic::AMDIL_abs:
302 return LowerIntrinsicIABS(Op, DAG);
303 case AMDGPUIntrinsic::AMDIL_exp:
304 return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1));
305 case AMDGPUIntrinsic::AMDGPU_lrp:
306 return LowerIntrinsicLRP(Op, DAG);
307 case AMDGPUIntrinsic::AMDIL_fraction:
308 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000309 case AMDGPUIntrinsic::AMDIL_max:
310 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, Op.getOperand(1),
311 Op.getOperand(2));
312 case AMDGPUIntrinsic::AMDGPU_imax:
313 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Op.getOperand(1),
314 Op.getOperand(2));
315 case AMDGPUIntrinsic::AMDGPU_umax:
316 return DAG.getNode(AMDGPUISD::UMAX, DL, VT, Op.getOperand(1),
317 Op.getOperand(2));
318 case AMDGPUIntrinsic::AMDIL_min:
319 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, Op.getOperand(1),
320 Op.getOperand(2));
321 case AMDGPUIntrinsic::AMDGPU_imin:
322 return DAG.getNode(AMDGPUISD::SMIN, DL, VT, Op.getOperand(1),
323 Op.getOperand(2));
324 case AMDGPUIntrinsic::AMDGPU_umin:
325 return DAG.getNode(AMDGPUISD::UMIN, DL, VT, Op.getOperand(1),
326 Op.getOperand(2));
327 case AMDGPUIntrinsic::AMDIL_round_nearest:
328 return DAG.getNode(ISD::FRINT, DL, VT, Op.getOperand(1));
329 }
330}
331
332///IABS(a) = SMAX(sub(0, a), a)
333SDValue AMDGPUTargetLowering::LowerIntrinsicIABS(SDValue Op,
334 SelectionDAG &DAG) const {
335
Andrew Trickac6d9be2013-05-25 02:42:55 +0000336 SDLoc DL(Op);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000337 EVT VT = Op.getValueType();
338 SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
339 Op.getOperand(1));
340
341 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Neg, Op.getOperand(1));
342}
343
344/// Linear Interpolation
345/// LRP(a, b, c) = muladd(a, b, (1 - a) * c)
346SDValue AMDGPUTargetLowering::LowerIntrinsicLRP(SDValue Op,
347 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000348 SDLoc DL(Op);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000349 EVT VT = Op.getValueType();
350 SDValue OneSubA = DAG.getNode(ISD::FSUB, DL, VT,
351 DAG.getConstantFP(1.0f, MVT::f32),
352 Op.getOperand(1));
353 SDValue OneSubAC = DAG.getNode(ISD::FMUL, DL, VT, OneSubA,
354 Op.getOperand(3));
Vincent Lejeunee3111962013-02-18 14:11:28 +0000355 return DAG.getNode(ISD::FADD, DL, VT,
356 DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), Op.getOperand(2)),
357 OneSubAC);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000358}
359
360/// \brief Generate Min/Max node
361SDValue AMDGPUTargetLowering::LowerMinMax(SDValue Op,
362 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000363 SDLoc DL(Op);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000364 EVT VT = Op.getValueType();
365
366 SDValue LHS = Op.getOperand(0);
367 SDValue RHS = Op.getOperand(1);
368 SDValue True = Op.getOperand(2);
369 SDValue False = Op.getOperand(3);
370 SDValue CC = Op.getOperand(4);
371
372 if (VT != MVT::f32 ||
373 !((LHS == True && RHS == False) || (LHS == False && RHS == True))) {
374 return SDValue();
375 }
376
377 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
378 switch (CCOpcode) {
379 case ISD::SETOEQ:
380 case ISD::SETONE:
381 case ISD::SETUNE:
382 case ISD::SETNE:
383 case ISD::SETUEQ:
384 case ISD::SETEQ:
385 case ISD::SETFALSE:
386 case ISD::SETFALSE2:
387 case ISD::SETTRUE:
388 case ISD::SETTRUE2:
389 case ISD::SETUO:
390 case ISD::SETO:
391 assert(0 && "Operation should already be optimised !");
392 case ISD::SETULE:
393 case ISD::SETULT:
394 case ISD::SETOLE:
395 case ISD::SETOLT:
396 case ISD::SETLE:
397 case ISD::SETLT: {
398 if (LHS == True)
399 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
400 else
401 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
402 }
403 case ISD::SETGT:
404 case ISD::SETGE:
405 case ISD::SETUGE:
406 case ISD::SETOGE:
407 case ISD::SETUGT:
408 case ISD::SETOGT: {
409 if (LHS == True)
410 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
411 else
412 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
413 }
414 case ISD::SETCC_INVALID:
415 assert(0 && "Invalid setcc condcode !");
416 }
417 return Op;
418}
419
420
421
422SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
423 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000424 SDLoc DL(Op);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000425 EVT VT = Op.getValueType();
426
427 SDValue Num = Op.getOperand(0);
428 SDValue Den = Op.getOperand(1);
429
430 SmallVector<SDValue, 8> Results;
431
432 // RCP = URECIP(Den) = 2^32 / Den + e
433 // e is rounding error.
434 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
435
436 // RCP_LO = umulo(RCP, Den) */
437 SDValue RCP_LO = DAG.getNode(ISD::UMULO, DL, VT, RCP, Den);
438
439 // RCP_HI = mulhu (RCP, Den) */
440 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
441
442 // NEG_RCP_LO = -RCP_LO
443 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
444 RCP_LO);
445
446 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
447 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
448 NEG_RCP_LO, RCP_LO,
449 ISD::SETEQ);
450 // Calculate the rounding error from the URECIP instruction
451 // E = mulhu(ABS_RCP_LO, RCP)
452 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
453
454 // RCP_A_E = RCP + E
455 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
456
457 // RCP_S_E = RCP - E
458 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
459
460 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
461 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
462 RCP_A_E, RCP_S_E,
463 ISD::SETEQ);
464 // Quotient = mulhu(Tmp0, Num)
465 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
466
467 // Num_S_Remainder = Quotient * Den
468 SDValue Num_S_Remainder = DAG.getNode(ISD::UMULO, DL, VT, Quotient, Den);
469
470 // Remainder = Num - Num_S_Remainder
471 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
472
473 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
474 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
475 DAG.getConstant(-1, VT),
476 DAG.getConstant(0, VT),
477 ISD::SETGE);
478 // Remainder_GE_Zero = (Remainder >= 0 ? -1 : 0)
479 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Remainder,
480 DAG.getConstant(0, VT),
481 DAG.getConstant(-1, VT),
482 DAG.getConstant(0, VT),
483 ISD::SETGE);
484 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
485 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
486 Remainder_GE_Zero);
487
488 // Calculate Division result:
489
490 // Quotient_A_One = Quotient + 1
491 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
492 DAG.getConstant(1, VT));
493
494 // Quotient_S_One = Quotient - 1
495 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
496 DAG.getConstant(1, VT));
497
498 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
499 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
500 Quotient, Quotient_A_One, ISD::SETEQ);
501
502 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
503 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
504 Quotient_S_One, Div, ISD::SETEQ);
505
506 // Calculate Rem result:
507
508 // Remainder_S_Den = Remainder - Den
509 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
510
511 // Remainder_A_Den = Remainder + Den
512 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
513
514 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
515 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
516 Remainder, Remainder_S_Den, ISD::SETEQ);
517
518 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
519 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
520 Remainder_A_Den, Rem, ISD::SETEQ);
521 SDValue Ops[2];
522 Ops[0] = Div;
523 Ops[1] = Rem;
524 return DAG.getMergeValues(Ops, 2, DL);
525}
526
Tom Stellard4c52d452013-08-16 01:12:11 +0000527SDValue AMDGPUTargetLowering::LowerVectorStore(const SDValue &Op,
528 SelectionDAG &DAG) const {
529 StoreSDNode *Store = dyn_cast<StoreSDNode>(Op);
530 EVT MemVT = Store->getMemoryVT();
531 unsigned MemBits = MemVT.getSizeInBits();
532
533 // Byte stores are really expensive, so if possible, try to pack
534 // 32-bit vector truncatating store into an i32 store.
535 // XXX: We could also handle optimize other vector bitwidths
536 if (!MemVT.isVector() || MemBits > 32) {
537 return SDValue();
538 }
539
540 SDLoc DL(Op);
541 const SDValue &Value = Store->getValue();
542 EVT VT = Value.getValueType();
543 const SDValue &Ptr = Store->getBasePtr();
544 EVT MemEltVT = MemVT.getVectorElementType();
545 unsigned MemEltBits = MemEltVT.getSizeInBits();
546 unsigned MemNumElements = MemVT.getVectorNumElements();
547 EVT PackedVT = EVT::getIntegerVT(*DAG.getContext(), MemVT.getSizeInBits());
548 SDValue Mask;
549 switch(MemEltBits) {
550 case 8:
551 Mask = DAG.getConstant(0xFF, PackedVT);
552 break;
553 case 16:
554 Mask = DAG.getConstant(0xFFFF, PackedVT);
555 break;
556 default:
557 llvm_unreachable("Cannot lower this vector store");
558 }
559 SDValue PackedValue;
560 for (unsigned i = 0; i < MemNumElements; ++i) {
561 EVT ElemVT = VT.getVectorElementType();
562 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT, Value,
563 DAG.getConstant(i, MVT::i32));
564 Elt = DAG.getZExtOrTrunc(Elt, DL, PackedVT);
565 Elt = DAG.getNode(ISD::AND, DL, PackedVT, Elt, Mask);
566 SDValue Shift = DAG.getConstant(MemEltBits * i, PackedVT);
567 Elt = DAG.getNode(ISD::SHL, DL, PackedVT, Elt, Shift);
568 if (i == 0) {
569 PackedValue = Elt;
570 } else {
571 PackedValue = DAG.getNode(ISD::OR, DL, PackedVT, PackedValue, Elt);
572 }
573 }
574 return DAG.getStore(Store->getChain(), DL, PackedValue, Ptr,
575 MachinePointerInfo(Store->getMemOperand()->getValue()),
576 Store->isVolatile(), Store->isNonTemporal(),
577 Store->getAlignment());
578}
579
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000580//===----------------------------------------------------------------------===//
581// Helper functions
582//===----------------------------------------------------------------------===//
583
584bool AMDGPUTargetLowering::isHWTrueValue(SDValue Op) const {
585 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
586 return CFP->isExactlyValue(1.0);
587 }
588 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
589 return C->isAllOnesValue();
590 }
591 return false;
592}
593
594bool AMDGPUTargetLowering::isHWFalseValue(SDValue Op) const {
595 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
596 return CFP->getValueAPF().isZero();
597 }
598 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
599 return C->isNullValue();
600 }
601 return false;
602}
603
604SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
605 const TargetRegisterClass *RC,
606 unsigned Reg, EVT VT) const {
607 MachineFunction &MF = DAG.getMachineFunction();
608 MachineRegisterInfo &MRI = MF.getRegInfo();
609 unsigned VirtualRegister;
610 if (!MRI.isLiveIn(Reg)) {
611 VirtualRegister = MRI.createVirtualRegister(RC);
612 MRI.addLiveIn(Reg, VirtualRegister);
613 } else {
614 VirtualRegister = MRI.getLiveInVirtReg(Reg);
615 }
616 return DAG.getRegister(VirtualRegister, VT);
617}
618
619#define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
620
621const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
622 switch (Opcode) {
623 default: return 0;
624 // AMDIL DAG nodes
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000625 NODE_NAME_CASE(CALL);
626 NODE_NAME_CASE(UMUL);
627 NODE_NAME_CASE(DIV_INF);
628 NODE_NAME_CASE(RET_FLAG);
629 NODE_NAME_CASE(BRANCH_COND);
630
631 // AMDGPU DAG nodes
632 NODE_NAME_CASE(DWORDADDR)
633 NODE_NAME_CASE(FRACT)
634 NODE_NAME_CASE(FMAX)
635 NODE_NAME_CASE(SMAX)
636 NODE_NAME_CASE(UMAX)
637 NODE_NAME_CASE(FMIN)
638 NODE_NAME_CASE(SMIN)
639 NODE_NAME_CASE(UMIN)
640 NODE_NAME_CASE(URECIP)
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000641 NODE_NAME_CASE(EXPORT)
Tom Stellardc7e18882013-01-23 02:09:03 +0000642 NODE_NAME_CASE(CONST_ADDRESS)
Tom Stellardc0b0c672013-02-06 17:32:29 +0000643 NODE_NAME_CASE(REGISTER_LOAD)
644 NODE_NAME_CASE(REGISTER_STORE)
Tom Stellard68db37b2013-08-14 23:24:45 +0000645 NODE_NAME_CASE(LOAD_CONSTANT)
646 NODE_NAME_CASE(LOAD_INPUT)
647 NODE_NAME_CASE(SAMPLE)
648 NODE_NAME_CASE(SAMPLEB)
649 NODE_NAME_CASE(SAMPLED)
650 NODE_NAME_CASE(SAMPLEL)
Tom Stellardec484272013-08-16 01:12:06 +0000651 NODE_NAME_CASE(STORE_MSKOR)
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000652 }
653}