blob: 0f3119d93eb76a160efaf947f9b1dfd66ee03984 [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;
Tom Stellardf95b1622013-10-23 00:44:32 +000031static bool allocateStack(unsigned ValNo, MVT ValVT, MVT LocVT,
32 CCValAssign::LocInfo LocInfo,
33 ISD::ArgFlagsTy ArgFlags, CCState &State) {
34 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() / 8, ArgFlags.getOrigAlign());
35 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
36
37 return true;
38}
Tom Stellardf98f2ce2012-12-11 21:25:42 +000039
Christian Konig90c64cb2013-03-07 09:03:52 +000040#include "AMDGPUGenCallingConv.inc"
41
Tom Stellardf98f2ce2012-12-11 21:25:42 +000042AMDGPUTargetLowering::AMDGPUTargetLowering(TargetMachine &TM) :
43 TargetLowering(TM, new TargetLoweringObjectFileELF()) {
44
45 // Initialize target lowering borrowed from AMDIL
46 InitAMDILLowering();
47
48 // We need to custom lower some of the intrinsics
49 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
50
51 // Library functions. These default to Expand, but we have instructions
52 // for them.
53 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
54 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
55 setOperationAction(ISD::FPOW, MVT::f32, Legal);
56 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
57 setOperationAction(ISD::FABS, MVT::f32, Legal);
58 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
59 setOperationAction(ISD::FRINT, MVT::f32, Legal);
60
Tom Stellardba534c22013-05-20 15:02:19 +000061 // The hardware supports ROTR, but not ROTL
62 setOperationAction(ISD::ROTL, MVT::i32, Expand);
63
Tom Stellardf98f2ce2012-12-11 21:25:42 +000064 // Lower floating point store/load to integer store/load to reduce the number
65 // of patterns in tablegen.
66 setOperationAction(ISD::STORE, MVT::f32, Promote);
67 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
68
Tom Stellardfc047272013-07-18 21:43:42 +000069 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
70 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
71
Tom Stellardf98f2ce2012-12-11 21:25:42 +000072 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
73 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
74
Tom Stellardf95b1622013-10-23 00:44:32 +000075 setOperationAction(ISD::STORE, MVT::v8f32, Promote);
76 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32);
77
78 setOperationAction(ISD::STORE, MVT::v16f32, Promote);
79 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32);
80
Tom Stellard68e13282013-07-12 18:14:56 +000081 setOperationAction(ISD::STORE, MVT::f64, Promote);
82 AddPromotedToType(ISD::STORE, MVT::f64, MVT::i64);
83
Tom Stellard7a0282d2013-08-26 15:05:44 +000084 // Custom lowering of vector stores is required for local address space
85 // stores.
86 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
87 // XXX: Native v2i32 local address space stores are possible, but not
88 // currently implemented.
89 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
90
Tom Stellard4c52d452013-08-16 01:12:11 +000091 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom);
92 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom);
93 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom);
94 // XXX: This can be change to Custom, once ExpandVectorStores can
95 // handle 64-bit stores.
96 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
97
Tom Stellardf98f2ce2012-12-11 21:25:42 +000098 setOperationAction(ISD::LOAD, MVT::f32, Promote);
99 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
100
Tom Stellardac85f3f2013-07-18 21:43:48 +0000101 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
102 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
103
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000104 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
105 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
106
Tom Stellardf95b1622013-10-23 00:44:32 +0000107 setOperationAction(ISD::LOAD, MVT::v8f32, Promote);
108 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32);
109
110 setOperationAction(ISD::LOAD, MVT::v16f32, Promote);
111 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32);
112
Tom Stellard68e13282013-07-12 18:14:56 +0000113 setOperationAction(ISD::LOAD, MVT::f64, Promote);
114 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::i64);
115
Tom Stellarda41520c2013-08-14 23:25:00 +0000116 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
117 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
118 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
119 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
Tom Stellard692ee102013-08-01 15:23:42 +0000120
Tom Stellard30d84d82013-08-16 01:12:16 +0000121 setLoadExtAction(ISD::EXTLOAD, MVT::v2i8, Expand);
122 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i8, Expand);
123 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i8, Expand);
124 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
125 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i8, Expand);
126 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i8, Expand);
127 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, Expand);
128 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, Expand);
129 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, Expand);
130 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, Expand);
131 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, Expand);
132 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, Expand);
133
Tom Stellardd7a472c2013-07-23 01:47:46 +0000134 setOperationAction(ISD::FNEG, MVT::v2f32, Expand);
135 setOperationAction(ISD::FNEG, MVT::v4f32, Expand);
136
Tom Stellardaa1d0782013-10-30 17:22:05 +0000137 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
138
Christian Konig45b14e32013-03-27 09:12:51 +0000139 setOperationAction(ISD::MUL, MVT::i64, Expand);
140
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000141 setOperationAction(ISD::UDIV, MVT::i32, Expand);
142 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
143 setOperationAction(ISD::UREM, MVT::i32, Expand);
Tom Stellardf5660aa2013-07-18 21:43:35 +0000144 setOperationAction(ISD::VSELECT, MVT::v2f32, Expand);
145 setOperationAction(ISD::VSELECT, MVT::v4f32, Expand);
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000146
Tom Stellard5464a922013-08-21 22:14:17 +0000147 static const MVT::SimpleValueType IntTypes[] = {
148 MVT::v2i32, MVT::v4i32
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000149 };
Tom Stellard0991c312013-08-16 23:51:24 +0000150 const size_t NumIntTypes = array_lengthof(IntTypes);
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000151
Tom Stellard0991c312013-08-16 23:51:24 +0000152 for (unsigned int x = 0; x < NumIntTypes; ++x) {
Tom Stellard5464a922013-08-21 22:14:17 +0000153 MVT::SimpleValueType VT = IntTypes[x];
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000154 //Expand the following operations for the current type by default
155 setOperationAction(ISD::ADD, VT, Expand);
156 setOperationAction(ISD::AND, VT, Expand);
Tom Stellarde3d60ac2013-07-30 14:31:03 +0000157 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
158 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000159 setOperationAction(ISD::MUL, VT, Expand);
160 setOperationAction(ISD::OR, VT, Expand);
161 setOperationAction(ISD::SHL, VT, Expand);
Tom Stellarde3d60ac2013-07-30 14:31:03 +0000162 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000163 setOperationAction(ISD::SRL, VT, Expand);
164 setOperationAction(ISD::SRA, VT, Expand);
165 setOperationAction(ISD::SUB, VT, Expand);
166 setOperationAction(ISD::UDIV, VT, Expand);
Tom Stellarde3d60ac2013-07-30 14:31:03 +0000167 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000168 setOperationAction(ISD::UREM, VT, Expand);
Tom Stellardf5660aa2013-07-18 21:43:35 +0000169 setOperationAction(ISD::VSELECT, VT, Expand);
Aaron Watryf97c7fe2013-06-25 13:55:57 +0000170 setOperationAction(ISD::XOR, VT, Expand);
171 }
Tom Stellard0991c312013-08-16 23:51:24 +0000172
Tom Stellard5464a922013-08-21 22:14:17 +0000173 static const MVT::SimpleValueType FloatTypes[] = {
174 MVT::v2f32, MVT::v4f32
Tom Stellard0991c312013-08-16 23:51:24 +0000175 };
176 const size_t NumFloatTypes = array_lengthof(FloatTypes);
177
178 for (unsigned int x = 0; x < NumFloatTypes; ++x) {
Tom Stellard5464a922013-08-21 22:14:17 +0000179 MVT::SimpleValueType VT = FloatTypes[x];
Tom Stellard0991c312013-08-16 23:51:24 +0000180 setOperationAction(ISD::FADD, VT, Expand);
181 setOperationAction(ISD::FDIV, VT, Expand);
Tom Stellard84c0bd92013-08-16 23:51:29 +0000182 setOperationAction(ISD::FFLOOR, VT, Expand);
Tom Stellard0991c312013-08-16 23:51:24 +0000183 setOperationAction(ISD::FMUL, VT, Expand);
Tom Stellard3cae8232013-08-16 23:51:33 +0000184 setOperationAction(ISD::FRINT, VT, Expand);
Tom Stellardf54a8402013-10-29 16:37:20 +0000185 setOperationAction(ISD::FSQRT, VT, Expand);
Tom Stellard0991c312013-08-16 23:51:24 +0000186 setOperationAction(ISD::FSUB, VT, Expand);
187 }
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000188}
189
Tom Stellard2b272a12013-08-05 22:22:07 +0000190//===----------------------------------------------------------------------===//
191// Target Information
192//===----------------------------------------------------------------------===//
193
194MVT AMDGPUTargetLowering::getVectorIdxTy() const {
195 return MVT::i32;
196}
197
198
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000199//===---------------------------------------------------------------------===//
Tom Stellard1f67c632013-07-23 23:55:03 +0000200// Target Properties
201//===---------------------------------------------------------------------===//
202
203bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
204 assert(VT.isFloatingPoint());
205 return VT == MVT::f32;
206}
207
208bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
209 assert(VT.isFloatingPoint());
210 return VT == MVT::f32;
211}
212
213//===---------------------------------------------------------------------===//
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000214// TargetLowering Callbacks
215//===---------------------------------------------------------------------===//
216
Christian Konig90c64cb2013-03-07 09:03:52 +0000217void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
218 const SmallVectorImpl<ISD::InputArg> &Ins) const {
219
220 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000221}
222
223SDValue AMDGPUTargetLowering::LowerReturn(
224 SDValue Chain,
225 CallingConv::ID CallConv,
226 bool isVarArg,
227 const SmallVectorImpl<ISD::OutputArg> &Outs,
228 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000229 SDLoc DL, SelectionDAG &DAG) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000230 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain);
231}
232
233//===---------------------------------------------------------------------===//
234// Target specific lowering
235//===---------------------------------------------------------------------===//
236
237SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
238 const {
239 switch (Op.getOpcode()) {
240 default:
241 Op.getNode()->dump();
242 assert(0 && "Custom lowering code for this"
243 "instruction is not implemented yet!");
244 break;
245 // AMDIL DAG lowering
246 case ISD::SDIV: return LowerSDIV(Op, DAG);
247 case ISD::SREM: return LowerSREM(Op, DAG);
248 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
249 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
250 // AMDGPU DAG lowering
Tom Stellarda41520c2013-08-14 23:25:00 +0000251 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
252 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000253 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Tom Stellard7a0282d2013-08-26 15:05:44 +0000254 case ISD::STORE: return LowerSTORE(Op, DAG);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000255 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
Tom Stellardaa1d0782013-10-30 17:22:05 +0000256 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000257 }
258 return Op;
259}
260
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000261SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
262 SDValue Op,
263 SelectionDAG &DAG) const {
264
265 const DataLayout *TD = getTargetMachine().getDataLayout();
266 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
Tom Stellardda25cd32013-08-26 15:05:36 +0000267
268 assert(G->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS);
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000269 // XXX: What does the value of G->getOffset() mean?
270 assert(G->getOffset() == 0 &&
271 "Do not know what to do with an non-zero offset");
272
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000273 const GlobalValue *GV = G->getGlobal();
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000274
Tom Stellard470c4512013-09-05 18:37:57 +0000275 unsigned Offset;
276 if (MFI->LocalMemoryObjects.count(GV) == 0) {
277 uint64_t Size = TD->getTypeAllocSize(GV->getType()->getElementType());
278 Offset = MFI->LDSSize;
279 MFI->LocalMemoryObjects[GV] = Offset;
280 // XXX: Account for alignment?
281 MFI->LDSSize += Size;
282 } else {
283 Offset = MFI->LocalMemoryObjects[GV];
284 }
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000285
Tom Stellardda25cd32013-08-26 15:05:36 +0000286 return DAG.getConstant(Offset, getPointerTy(G->getAddressSpace()));
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000287}
288
Tom Stellarda41520c2013-08-14 23:25:00 +0000289void AMDGPUTargetLowering::ExtractVectorElements(SDValue Op, SelectionDAG &DAG,
290 SmallVectorImpl<SDValue> &Args,
291 unsigned Start,
292 unsigned Count) const {
293 EVT VT = Op.getValueType();
294 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
295 Args.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
296 VT.getVectorElementType(),
297 Op, DAG.getConstant(i, MVT::i32)));
298 }
299}
300
301SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
302 SelectionDAG &DAG) const {
303 SmallVector<SDValue, 8> Args;
304 SDValue A = Op.getOperand(0);
305 SDValue B = Op.getOperand(1);
306
307 ExtractVectorElements(A, DAG, Args, 0,
308 A.getValueType().getVectorNumElements());
309 ExtractVectorElements(B, DAG, Args, 0,
310 B.getValueType().getVectorNumElements());
311
312 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(),
313 &Args[0], Args.size());
314}
315
316SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
317 SelectionDAG &DAG) const {
318
319 SmallVector<SDValue, 8> Args;
320 EVT VT = Op.getValueType();
321 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
322 ExtractVectorElements(Op.getOperand(0), DAG, Args, Start,
323 VT.getVectorNumElements());
324
325 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(),
326 &Args[0], Args.size());
327}
328
329
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000330SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
331 SelectionDAG &DAG) const {
332 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000333 SDLoc DL(Op);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000334 EVT VT = Op.getValueType();
335
336 switch (IntrinsicID) {
337 default: return Op;
338 case AMDGPUIntrinsic::AMDIL_abs:
339 return LowerIntrinsicIABS(Op, DAG);
340 case AMDGPUIntrinsic::AMDIL_exp:
341 return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1));
342 case AMDGPUIntrinsic::AMDGPU_lrp:
343 return LowerIntrinsicLRP(Op, DAG);
344 case AMDGPUIntrinsic::AMDIL_fraction:
345 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000346 case AMDGPUIntrinsic::AMDIL_max:
347 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, Op.getOperand(1),
348 Op.getOperand(2));
349 case AMDGPUIntrinsic::AMDGPU_imax:
350 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Op.getOperand(1),
351 Op.getOperand(2));
352 case AMDGPUIntrinsic::AMDGPU_umax:
353 return DAG.getNode(AMDGPUISD::UMAX, DL, VT, Op.getOperand(1),
354 Op.getOperand(2));
355 case AMDGPUIntrinsic::AMDIL_min:
356 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, Op.getOperand(1),
357 Op.getOperand(2));
358 case AMDGPUIntrinsic::AMDGPU_imin:
359 return DAG.getNode(AMDGPUISD::SMIN, DL, VT, Op.getOperand(1),
360 Op.getOperand(2));
361 case AMDGPUIntrinsic::AMDGPU_umin:
362 return DAG.getNode(AMDGPUISD::UMIN, DL, VT, Op.getOperand(1),
363 Op.getOperand(2));
364 case AMDGPUIntrinsic::AMDIL_round_nearest:
365 return DAG.getNode(ISD::FRINT, DL, VT, Op.getOperand(1));
366 }
367}
368
369///IABS(a) = SMAX(sub(0, a), a)
370SDValue AMDGPUTargetLowering::LowerIntrinsicIABS(SDValue Op,
371 SelectionDAG &DAG) const {
372
Andrew Trickac6d9be2013-05-25 02:42:55 +0000373 SDLoc DL(Op);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000374 EVT VT = Op.getValueType();
375 SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
376 Op.getOperand(1));
377
378 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Neg, Op.getOperand(1));
379}
380
381/// Linear Interpolation
382/// LRP(a, b, c) = muladd(a, b, (1 - a) * c)
383SDValue AMDGPUTargetLowering::LowerIntrinsicLRP(SDValue Op,
384 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000385 SDLoc DL(Op);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000386 EVT VT = Op.getValueType();
387 SDValue OneSubA = DAG.getNode(ISD::FSUB, DL, VT,
388 DAG.getConstantFP(1.0f, MVT::f32),
389 Op.getOperand(1));
390 SDValue OneSubAC = DAG.getNode(ISD::FMUL, DL, VT, OneSubA,
391 Op.getOperand(3));
Vincent Lejeunee3111962013-02-18 14:11:28 +0000392 return DAG.getNode(ISD::FADD, DL, VT,
393 DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), Op.getOperand(2)),
394 OneSubAC);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000395}
396
397/// \brief Generate Min/Max node
398SDValue AMDGPUTargetLowering::LowerMinMax(SDValue Op,
399 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000400 SDLoc DL(Op);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000401 EVT VT = Op.getValueType();
402
403 SDValue LHS = Op.getOperand(0);
404 SDValue RHS = Op.getOperand(1);
405 SDValue True = Op.getOperand(2);
406 SDValue False = Op.getOperand(3);
407 SDValue CC = Op.getOperand(4);
408
409 if (VT != MVT::f32 ||
410 !((LHS == True && RHS == False) || (LHS == False && RHS == True))) {
411 return SDValue();
412 }
413
414 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
415 switch (CCOpcode) {
416 case ISD::SETOEQ:
417 case ISD::SETONE:
418 case ISD::SETUNE:
419 case ISD::SETNE:
420 case ISD::SETUEQ:
421 case ISD::SETEQ:
422 case ISD::SETFALSE:
423 case ISD::SETFALSE2:
424 case ISD::SETTRUE:
425 case ISD::SETTRUE2:
426 case ISD::SETUO:
427 case ISD::SETO:
428 assert(0 && "Operation should already be optimised !");
429 case ISD::SETULE:
430 case ISD::SETULT:
431 case ISD::SETOLE:
432 case ISD::SETOLT:
433 case ISD::SETLE:
434 case ISD::SETLT: {
435 if (LHS == True)
436 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
437 else
438 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
439 }
440 case ISD::SETGT:
441 case ISD::SETGE:
442 case ISD::SETUGE:
443 case ISD::SETOGE:
444 case ISD::SETUGT:
445 case ISD::SETOGT: {
446 if (LHS == True)
447 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
448 else
449 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
450 }
451 case ISD::SETCC_INVALID:
452 assert(0 && "Invalid setcc condcode !");
453 }
454 return Op;
455}
456
Tom Stellardd08a9302013-08-26 15:06:04 +0000457SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue &Op,
458 SelectionDAG &DAG) const {
459 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
460 EVT MemEltVT = Load->getMemoryVT().getVectorElementType();
461 EVT EltVT = Op.getValueType().getVectorElementType();
462 EVT PtrVT = Load->getBasePtr().getValueType();
463 unsigned NumElts = Load->getMemoryVT().getVectorNumElements();
464 SmallVector<SDValue, 8> Loads;
465 SDLoc SL(Op);
466
467 for (unsigned i = 0, e = NumElts; i != e; ++i) {
468 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, Load->getBasePtr(),
469 DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8), PtrVT));
470 Loads.push_back(DAG.getExtLoad(Load->getExtensionType(), SL, EltVT,
471 Load->getChain(), Ptr,
472 MachinePointerInfo(Load->getMemOperand()->getValue()),
473 MemEltVT, Load->isVolatile(), Load->isNonTemporal(),
474 Load->getAlignment()));
475 }
476 return DAG.getNode(ISD::BUILD_VECTOR, SL, Op.getValueType(), &Loads[0],
477 Loads.size());
478}
479
Tom Stellard7a0282d2013-08-26 15:05:44 +0000480SDValue AMDGPUTargetLowering::MergeVectorStore(const SDValue &Op,
481 SelectionDAG &DAG) const {
482 StoreSDNode *Store = dyn_cast<StoreSDNode>(Op);
483 EVT MemVT = Store->getMemoryVT();
484 unsigned MemBits = MemVT.getSizeInBits();
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000485
Tom Stellard7a0282d2013-08-26 15:05:44 +0000486 // Byte stores are really expensive, so if possible, try to pack
487 // 32-bit vector truncatating store into an i32 store.
488 // XXX: We could also handle optimize other vector bitwidths
489 if (!MemVT.isVector() || MemBits > 32) {
490 return SDValue();
491 }
492
493 SDLoc DL(Op);
494 const SDValue &Value = Store->getValue();
495 EVT VT = Value.getValueType();
496 const SDValue &Ptr = Store->getBasePtr();
497 EVT MemEltVT = MemVT.getVectorElementType();
498 unsigned MemEltBits = MemEltVT.getSizeInBits();
499 unsigned MemNumElements = MemVT.getVectorNumElements();
500 EVT PackedVT = EVT::getIntegerVT(*DAG.getContext(), MemVT.getSizeInBits());
501 SDValue Mask;
502 switch(MemEltBits) {
503 case 8:
504 Mask = DAG.getConstant(0xFF, PackedVT);
505 break;
506 case 16:
507 Mask = DAG.getConstant(0xFFFF, PackedVT);
508 break;
509 default:
510 llvm_unreachable("Cannot lower this vector store");
511 }
512 SDValue PackedValue;
513 for (unsigned i = 0; i < MemNumElements; ++i) {
514 EVT ElemVT = VT.getVectorElementType();
515 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT, Value,
516 DAG.getConstant(i, MVT::i32));
517 Elt = DAG.getZExtOrTrunc(Elt, DL, PackedVT);
518 Elt = DAG.getNode(ISD::AND, DL, PackedVT, Elt, Mask);
519 SDValue Shift = DAG.getConstant(MemEltBits * i, PackedVT);
520 Elt = DAG.getNode(ISD::SHL, DL, PackedVT, Elt, Shift);
521 if (i == 0) {
522 PackedValue = Elt;
523 } else {
524 PackedValue = DAG.getNode(ISD::OR, DL, PackedVT, PackedValue, Elt);
525 }
526 }
527 return DAG.getStore(Store->getChain(), DL, PackedValue, Ptr,
528 MachinePointerInfo(Store->getMemOperand()->getValue()),
529 Store->isVolatile(), Store->isNonTemporal(),
530 Store->getAlignment());
531}
532
533SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
534 SelectionDAG &DAG) const {
535 StoreSDNode *Store = cast<StoreSDNode>(Op);
536 EVT MemEltVT = Store->getMemoryVT().getVectorElementType();
537 EVT EltVT = Store->getValue().getValueType().getVectorElementType();
538 EVT PtrVT = Store->getBasePtr().getValueType();
539 unsigned NumElts = Store->getMemoryVT().getVectorNumElements();
540 SDLoc SL(Op);
541
542 SmallVector<SDValue, 8> Chains;
543
544 for (unsigned i = 0, e = NumElts; i != e; ++i) {
545 SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
546 Store->getValue(), DAG.getConstant(i, MVT::i32));
547 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT,
548 Store->getBasePtr(),
549 DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8),
550 PtrVT));
Tom Stellard8e780122013-08-26 15:05:49 +0000551 Chains.push_back(DAG.getTruncStore(Store->getChain(), SL, Val, Ptr,
Tom Stellard7a0282d2013-08-26 15:05:44 +0000552 MachinePointerInfo(Store->getMemOperand()->getValue()),
Tom Stellard8e780122013-08-26 15:05:49 +0000553 MemEltVT, Store->isVolatile(), Store->isNonTemporal(),
Tom Stellard7a0282d2013-08-26 15:05:44 +0000554 Store->getAlignment()));
555 }
556 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, &Chains[0], NumElts);
557}
558
559SDValue AMDGPUTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
560 SDValue Result = AMDGPUTargetLowering::MergeVectorStore(Op, DAG);
561 if (Result.getNode()) {
562 return Result;
563 }
564
565 StoreSDNode *Store = cast<StoreSDNode>(Op);
566 if (Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS &&
567 Store->getValue().getValueType().isVector()) {
568 return SplitVectorStore(Op, DAG);
569 }
570 return SDValue();
571}
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000572
573SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
574 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000575 SDLoc DL(Op);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000576 EVT VT = Op.getValueType();
577
578 SDValue Num = Op.getOperand(0);
579 SDValue Den = Op.getOperand(1);
580
581 SmallVector<SDValue, 8> Results;
582
583 // RCP = URECIP(Den) = 2^32 / Den + e
584 // e is rounding error.
585 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
586
587 // RCP_LO = umulo(RCP, Den) */
588 SDValue RCP_LO = DAG.getNode(ISD::UMULO, DL, VT, RCP, Den);
589
590 // RCP_HI = mulhu (RCP, Den) */
591 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
592
593 // NEG_RCP_LO = -RCP_LO
594 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
595 RCP_LO);
596
597 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
598 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
599 NEG_RCP_LO, RCP_LO,
600 ISD::SETEQ);
601 // Calculate the rounding error from the URECIP instruction
602 // E = mulhu(ABS_RCP_LO, RCP)
603 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
604
605 // RCP_A_E = RCP + E
606 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
607
608 // RCP_S_E = RCP - E
609 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
610
611 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
612 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
613 RCP_A_E, RCP_S_E,
614 ISD::SETEQ);
615 // Quotient = mulhu(Tmp0, Num)
616 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
617
618 // Num_S_Remainder = Quotient * Den
619 SDValue Num_S_Remainder = DAG.getNode(ISD::UMULO, DL, VT, Quotient, Den);
620
621 // Remainder = Num - Num_S_Remainder
622 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
623
624 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
625 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
626 DAG.getConstant(-1, VT),
627 DAG.getConstant(0, VT),
628 ISD::SETGE);
629 // Remainder_GE_Zero = (Remainder >= 0 ? -1 : 0)
630 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Remainder,
631 DAG.getConstant(0, VT),
632 DAG.getConstant(-1, VT),
633 DAG.getConstant(0, VT),
634 ISD::SETGE);
635 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
636 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
637 Remainder_GE_Zero);
638
639 // Calculate Division result:
640
641 // Quotient_A_One = Quotient + 1
642 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
643 DAG.getConstant(1, VT));
644
645 // Quotient_S_One = Quotient - 1
646 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
647 DAG.getConstant(1, VT));
648
649 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
650 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
651 Quotient, Quotient_A_One, ISD::SETEQ);
652
653 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
654 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
655 Quotient_S_One, Div, ISD::SETEQ);
656
657 // Calculate Rem result:
658
659 // Remainder_S_Den = Remainder - Den
660 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
661
662 // Remainder_A_Den = Remainder + Den
663 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
664
665 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
666 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
667 Remainder, Remainder_S_Den, ISD::SETEQ);
668
669 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
670 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
671 Remainder_A_Den, Rem, ISD::SETEQ);
672 SDValue Ops[2];
673 Ops[0] = Div;
674 Ops[1] = Rem;
675 return DAG.getMergeValues(Ops, 2, DL);
676}
677
Tom Stellardaa1d0782013-10-30 17:22:05 +0000678SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
679 SelectionDAG &DAG) const {
680 SDValue S0 = Op.getOperand(0);
681 SDLoc DL(Op);
682 if (Op.getValueType() != MVT::f32 || S0.getValueType() != MVT::i64)
683 return SDValue();
684
685 // f32 uint_to_fp i64
686 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
687 DAG.getConstant(0, MVT::i32));
688 SDValue FloatLo = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Lo);
689 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
690 DAG.getConstant(1, MVT::i32));
691 SDValue FloatHi = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Hi);
692 FloatHi = DAG.getNode(ISD::FMUL, DL, MVT::f32, FloatHi,
693 DAG.getConstantFP(4294967296.0f, MVT::f32)); // 2^32
694 return DAG.getNode(ISD::FADD, DL, MVT::f32, FloatLo, FloatHi);
695
696}
Tom Stellard4c52d452013-08-16 01:12:11 +0000697
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000698//===----------------------------------------------------------------------===//
699// Helper functions
700//===----------------------------------------------------------------------===//
701
Tom Stellardf95b1622013-10-23 00:44:32 +0000702void AMDGPUTargetLowering::getOriginalFunctionArgs(
703 SelectionDAG &DAG,
704 const Function *F,
705 const SmallVectorImpl<ISD::InputArg> &Ins,
706 SmallVectorImpl<ISD::InputArg> &OrigIns) const {
707
708 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
709 if (Ins[i].ArgVT == Ins[i].VT) {
710 OrigIns.push_back(Ins[i]);
711 continue;
712 }
713
714 EVT VT;
715 if (Ins[i].ArgVT.isVector() && !Ins[i].VT.isVector()) {
716 // Vector has been split into scalars.
717 VT = Ins[i].ArgVT.getVectorElementType();
718 } else if (Ins[i].VT.isVector() && Ins[i].ArgVT.isVector() &&
719 Ins[i].ArgVT.getVectorElementType() !=
720 Ins[i].VT.getVectorElementType()) {
721 // Vector elements have been promoted
722 VT = Ins[i].ArgVT;
723 } else {
724 // Vector has been spilt into smaller vectors.
725 VT = Ins[i].VT;
726 }
727
728 ISD::InputArg Arg(Ins[i].Flags, VT, VT, Ins[i].Used,
729 Ins[i].OrigArgIndex, Ins[i].PartOffset);
730 OrigIns.push_back(Arg);
731 }
732}
733
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000734bool AMDGPUTargetLowering::isHWTrueValue(SDValue Op) const {
735 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
736 return CFP->isExactlyValue(1.0);
737 }
738 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
739 return C->isAllOnesValue();
740 }
741 return false;
742}
743
744bool AMDGPUTargetLowering::isHWFalseValue(SDValue Op) const {
745 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
746 return CFP->getValueAPF().isZero();
747 }
748 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
749 return C->isNullValue();
750 }
751 return false;
752}
753
754SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
755 const TargetRegisterClass *RC,
756 unsigned Reg, EVT VT) const {
757 MachineFunction &MF = DAG.getMachineFunction();
758 MachineRegisterInfo &MRI = MF.getRegInfo();
759 unsigned VirtualRegister;
760 if (!MRI.isLiveIn(Reg)) {
761 VirtualRegister = MRI.createVirtualRegister(RC);
762 MRI.addLiveIn(Reg, VirtualRegister);
763 } else {
764 VirtualRegister = MRI.getLiveInVirtReg(Reg);
765 }
766 return DAG.getRegister(VirtualRegister, VT);
767}
768
769#define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
770
771const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
772 switch (Opcode) {
773 default: return 0;
774 // AMDIL DAG nodes
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000775 NODE_NAME_CASE(CALL);
776 NODE_NAME_CASE(UMUL);
777 NODE_NAME_CASE(DIV_INF);
778 NODE_NAME_CASE(RET_FLAG);
779 NODE_NAME_CASE(BRANCH_COND);
780
781 // AMDGPU DAG nodes
782 NODE_NAME_CASE(DWORDADDR)
783 NODE_NAME_CASE(FRACT)
784 NODE_NAME_CASE(FMAX)
785 NODE_NAME_CASE(SMAX)
786 NODE_NAME_CASE(UMAX)
787 NODE_NAME_CASE(FMIN)
788 NODE_NAME_CASE(SMIN)
789 NODE_NAME_CASE(UMIN)
790 NODE_NAME_CASE(URECIP)
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000791 NODE_NAME_CASE(EXPORT)
Tom Stellardc7e18882013-01-23 02:09:03 +0000792 NODE_NAME_CASE(CONST_ADDRESS)
Tom Stellardc0b0c672013-02-06 17:32:29 +0000793 NODE_NAME_CASE(REGISTER_LOAD)
794 NODE_NAME_CASE(REGISTER_STORE)
Tom Stellard68db37b2013-08-14 23:24:45 +0000795 NODE_NAME_CASE(LOAD_CONSTANT)
796 NODE_NAME_CASE(LOAD_INPUT)
797 NODE_NAME_CASE(SAMPLE)
798 NODE_NAME_CASE(SAMPLEB)
799 NODE_NAME_CASE(SAMPLED)
800 NODE_NAME_CASE(SAMPLEL)
Tom Stellardec484272013-08-16 01:12:06 +0000801 NODE_NAME_CASE(STORE_MSKOR)
Tom Stellarda3c2bcf2013-09-12 02:55:14 +0000802 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000803 }
804}