blob: 51241cf0a4324dd8e54b6a948c7249a534fcf1ab [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
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 Custom DAG lowering for SI
12//
13//===----------------------------------------------------------------------===//
14
NAKAMURA Takumi45e0a832014-07-20 11:15:07 +000015#ifdef _MSC_VER
16// Provide M_PI.
17#define _USE_MATH_DEFINES
18#include <cmath>
19#endif
20
Christian Konig99ee0f42013-03-07 09:04:14 +000021#include "AMDGPU.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000022#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000023#include "AMDGPUSubtarget.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000024#include "SIISelLowering.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025#include "SIInstrInfo.h"
26#include "SIMachineFunctionInfo.h"
27#include "SIRegisterInfo.h"
Alexey Samsonova253bf92014-08-27 19:36:53 +000028#include "llvm/ADT/BitVector.h"
Matt Arsenault9a10cea2016-01-26 04:29:24 +000029#include "llvm/ADT/StringSwitch.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000030#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
32#include "llvm/CodeGen/MachineRegisterInfo.h"
33#include "llvm/CodeGen/SelectionDAG.h"
Oliver Stannard7e7d9832016-02-02 13:52:43 +000034#include "llvm/IR/DiagnosticInfo.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000035#include "llvm/IR/Function.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000036
37using namespace llvm;
38
Wei Dinged0f97f2016-06-09 19:17:15 +000039// -amdgpu-fast-fdiv - Command line option to enable faster 2.5 ulp fdiv.
40static cl::opt<bool> EnableAMDGPUFastFDIV(
Matt Arsenault37fefd62016-06-10 02:18:02 +000041 "amdgpu-fast-fdiv",
42 cl::desc("Enable faster 2.5 ulp fdiv"),
Wei Dinged0f97f2016-06-09 19:17:15 +000043 cl::init(false));
44
Tom Stellardf110f8f2016-04-14 16:27:03 +000045static unsigned findFirstFreeSGPR(CCState &CCInfo) {
46 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs();
47 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) {
48 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) {
49 return AMDGPU::SGPR0 + Reg;
50 }
51 }
52 llvm_unreachable("Cannot allocate sgpr");
53}
54
Matt Arsenault43e92fe2016-06-24 06:30:11 +000055SITargetLowering::SITargetLowering(const TargetMachine &TM,
56 const SISubtarget &STI)
Eric Christopher7792e322015-01-30 23:24:40 +000057 : AMDGPUTargetLowering(TM, STI) {
Tom Stellard1bd80722014-04-30 15:31:33 +000058 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000059 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000060
Tom Stellard334b29c2014-04-17 21:00:09 +000061 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000062 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000063
Tom Stellard436780b2014-05-15 14:41:57 +000064 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
65 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
66 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000067
Matt Arsenault61001bb2015-11-25 19:58:34 +000068 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
69 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
70
Tom Stellard436780b2014-05-15 14:41:57 +000071 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
72 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000073
Tom Stellardf0a21072014-11-18 20:39:39 +000074 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000075 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
76
Tom Stellardf0a21072014-11-18 20:39:39 +000077 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000078 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000079
Eric Christopher23a3a7c2015-02-26 00:00:24 +000080 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000081
Tom Stellard35bb18c2013-08-26 15:06:04 +000082 // We need to custom lower vector stores from local memory
Matt Arsenault71e66762016-05-21 02:27:49 +000083 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000084 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000085 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
86 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000087 setOperationAction(ISD::LOAD, MVT::i1, Custom);
Matt Arsenault2b957b52016-05-02 20:07:26 +000088
Matt Arsenaultbcdfee72016-05-02 20:13:51 +000089 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000090 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
91 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
92 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
93 setOperationAction(ISD::STORE, MVT::i1, Custom);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +000094
Matt Arsenault71e66762016-05-21 02:27:49 +000095 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
96 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
97 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
98 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
99
100 setOperationAction(ISD::SELECT, MVT::i1, Promote);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000101 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +0000102 setOperationAction(ISD::SELECT, MVT::f64, Promote);
103 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000104
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000105 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
106 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
107 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
108 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Matt Arsenault71e66762016-05-21 02:27:49 +0000109 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000110
Tom Stellardd1efda82016-01-20 21:48:24 +0000111 setOperationAction(ISD::SETCC, MVT::i1, Promote);
Tom Stellard83747202013-07-18 21:43:53 +0000112 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
113 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
114
Matt Arsenault71e66762016-05-21 02:27:49 +0000115 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
116 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Matt Arsenaulte306a322014-10-21 16:25:08 +0000117
Matt Arsenault4e466652014-04-16 01:41:30 +0000118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
125
Tom Stellard9fa17912013-08-14 23:24:45 +0000126 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000127 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000128 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
129
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000130 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000131 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000132 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
133 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
134 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
135 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000136
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000137 // We only support LOAD/STORE and vector manipulation ops for vectors
138 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000139 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000140 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000141 switch (Op) {
Tom Stellard967bf582014-02-13 23:34:15 +0000142 case ISD::LOAD:
143 case ISD::STORE:
144 case ISD::BUILD_VECTOR:
145 case ISD::BITCAST:
146 case ISD::EXTRACT_VECTOR_ELT:
147 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000148 case ISD::INSERT_SUBVECTOR:
149 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000150 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000151 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000152 case ISD::CONCAT_VECTORS:
153 setOperationAction(Op, VT, Custom);
154 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000155 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000156 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000157 break;
158 }
159 }
160 }
161
Matt Arsenault61001bb2015-11-25 19:58:34 +0000162 // Most operations are naturally 32-bit vector operations. We only support
163 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
164 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
165 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
166 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
167
168 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
169 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
170
171 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
172 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
173
174 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
175 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
176 }
177
Matt Arsenault71e66762016-05-21 02:27:49 +0000178 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
179 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
180 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
181 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000182
Tom Stellard354a43c2016-04-01 18:27:37 +0000183 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling,
184 // and output demarshalling
185 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
186 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
187
188 // We can't return success/failure, only the old value,
189 // let LLVM add the comparison
190 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand);
191 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand);
192
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000193 if (getSubtarget()->hasFlatAddressSpace()) {
Matt Arsenault99c14522016-04-25 19:27:24 +0000194 setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom);
195 setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom);
196 }
197
Matt Arsenault71e66762016-05-21 02:27:49 +0000198 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
199 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
200
201 // On SI this is s_memtime and s_memrealtime on VI.
202 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
Matt Arsenault0bb294b2016-06-17 22:27:03 +0000203 setOperationAction(ISD::TRAP, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000204
205 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
206 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
207
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000208 if (Subtarget->getGeneration() >= SISubtarget::SEA_ISLANDS) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000209 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
210 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
211 setOperationAction(ISD::FRINT, MVT::f64, Legal);
212 }
213
214 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
215
216 setOperationAction(ISD::FSIN, MVT::f32, Custom);
217 setOperationAction(ISD::FCOS, MVT::f32, Custom);
218 setOperationAction(ISD::FDIV, MVT::f32, Custom);
219 setOperationAction(ISD::FDIV, MVT::f64, Custom);
220
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000221 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000222 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000223 setTargetDAGCombine(ISD::FMINNUM);
224 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000225 setTargetDAGCombine(ISD::SMIN);
226 setTargetDAGCombine(ISD::SMAX);
227 setTargetDAGCombine(ISD::UMIN);
228 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000229 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000230 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000231 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000232 setTargetDAGCombine(ISD::UINT_TO_FP);
Matt Arsenault9cd90712016-04-14 01:42:16 +0000233 setTargetDAGCombine(ISD::FCANONICALIZE);
Matt Arsenault364a6742014-06-11 17:50:44 +0000234
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000235 // All memory operations. Some folding on the pointer operand is done to help
236 // matching the constant offsets in the addressing modes.
237 setTargetDAGCombine(ISD::LOAD);
238 setTargetDAGCombine(ISD::STORE);
239 setTargetDAGCombine(ISD::ATOMIC_LOAD);
240 setTargetDAGCombine(ISD::ATOMIC_STORE);
241 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
242 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
243 setTargetDAGCombine(ISD::ATOMIC_SWAP);
244 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
245 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
246 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
247 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
248 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
249 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
250 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
251 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
252 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
253 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
254
Christian Konigeecebd02013-03-26 14:04:02 +0000255 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000256}
257
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000258const SISubtarget *SITargetLowering::getSubtarget() const {
259 return static_cast<const SISubtarget *>(Subtarget);
260}
261
Tom Stellard0125f2a2013-06-25 02:39:35 +0000262//===----------------------------------------------------------------------===//
263// TargetLowering queries
264//===----------------------------------------------------------------------===//
265
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000266bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
267 const CallInst &CI,
268 unsigned IntrID) const {
269 switch (IntrID) {
270 case Intrinsic::amdgcn_atomic_inc:
271 case Intrinsic::amdgcn_atomic_dec:
272 Info.opc = ISD::INTRINSIC_W_CHAIN;
273 Info.memVT = MVT::getVT(CI.getType());
274 Info.ptrVal = CI.getOperand(0);
275 Info.align = 0;
276 Info.vol = false;
277 Info.readMem = true;
278 Info.writeMem = true;
279 return true;
280 default:
281 return false;
282 }
283}
284
Matt Arsenaulte306a322014-10-21 16:25:08 +0000285bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
286 EVT) const {
287 // SI has some legal vector types, but no legal vector operations. Say no
288 // shuffles are legal in order to prefer scalarizing some vector operations.
289 return false;
290}
291
Tom Stellard70580f82015-07-20 14:28:41 +0000292bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
293 // Flat instructions do not have offsets, and only have the register
294 // address.
295 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
296}
297
Matt Arsenault711b3902015-08-07 20:18:34 +0000298bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
299 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
300 // additionally can do r + r + i with addr64. 32-bit has more addressing
301 // mode options. Depending on the resource constant, it can also do
302 // (i64 r0) + (i32 r1) * (i14 i).
303 //
304 // Private arrays end up using a scratch buffer most of the time, so also
305 // assume those use MUBUF instructions. Scratch loads / stores are currently
306 // implemented as mubuf instructions with offen bit set, so slightly
307 // different than the normal addr64.
308 if (!isUInt<12>(AM.BaseOffs))
309 return false;
310
311 // FIXME: Since we can split immediate into soffset and immediate offset,
312 // would it make sense to allow any immediate?
313
314 switch (AM.Scale) {
315 case 0: // r + i or just i, depending on HasBaseReg.
316 return true;
317 case 1:
318 return true; // We have r + r or r + i.
319 case 2:
320 if (AM.HasBaseReg) {
321 // Reject 2 * r + r.
322 return false;
323 }
324
325 // Allow 2 * r as r + r
326 // Or 2 * r + i is allowed as r + r + i.
327 return true;
328 default: // Don't allow n * r
329 return false;
330 }
331}
332
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000333bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
334 const AddrMode &AM, Type *Ty,
335 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000336 // No global is ever allowed as a base.
337 if (AM.BaseGV)
338 return false;
339
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000340 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000341 case AMDGPUAS::GLOBAL_ADDRESS: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000342 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) {
Tom Stellard70580f82015-07-20 14:28:41 +0000343 // Assume the we will use FLAT for all global memory accesses
344 // on VI.
345 // FIXME: This assumption is currently wrong. On VI we still use
346 // MUBUF instructions for the r + i addressing mode. As currently
347 // implemented, the MUBUF instructions only work on buffer < 4GB.
348 // It may be possible to support > 4GB buffers with MUBUF instructions,
349 // by setting the stride value in the resource descriptor which would
350 // increase the size limit to (stride * 4GB). However, this is risky,
351 // because it has never been validated.
352 return isLegalFlatAddressingMode(AM);
353 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000354
Matt Arsenault711b3902015-08-07 20:18:34 +0000355 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000356 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000357 case AMDGPUAS::CONSTANT_ADDRESS: {
358 // If the offset isn't a multiple of 4, it probably isn't going to be
359 // correctly aligned.
360 if (AM.BaseOffs % 4 != 0)
361 return isLegalMUBUFAddressingMode(AM);
362
363 // There are no SMRD extloads, so if we have to do a small type access we
364 // will use a MUBUF load.
365 // FIXME?: We also need to do this if unaligned, but we don't know the
366 // alignment here.
367 if (DL.getTypeStoreSize(Ty) < 4)
368 return isLegalMUBUFAddressingMode(AM);
369
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000370 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000371 // SMRD instructions have an 8-bit, dword offset on SI.
372 if (!isUInt<8>(AM.BaseOffs / 4))
373 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000374 } else if (Subtarget->getGeneration() == SISubtarget::SEA_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000375 // On CI+, this can also be a 32-bit literal constant offset. If it fits
376 // in 8-bits, it can use a smaller encoding.
377 if (!isUInt<32>(AM.BaseOffs / 4))
378 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000379 } else if (Subtarget->getGeneration() == SISubtarget::VOLCANIC_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000380 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
381 if (!isUInt<20>(AM.BaseOffs))
382 return false;
383 } else
384 llvm_unreachable("unhandled generation");
385
386 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
387 return true;
388
389 if (AM.Scale == 1 && AM.HasBaseReg)
390 return true;
391
392 return false;
393 }
394
395 case AMDGPUAS::PRIVATE_ADDRESS:
Matt Arsenault711b3902015-08-07 20:18:34 +0000396 return isLegalMUBUFAddressingMode(AM);
397
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000398 case AMDGPUAS::LOCAL_ADDRESS:
399 case AMDGPUAS::REGION_ADDRESS: {
400 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
401 // field.
402 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
403 // an 8-bit dword offset but we don't know the alignment here.
404 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000405 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000406
407 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
408 return true;
409
410 if (AM.Scale == 1 && AM.HasBaseReg)
411 return true;
412
Matt Arsenault5015a892014-08-15 17:17:07 +0000413 return false;
414 }
Tom Stellard70580f82015-07-20 14:28:41 +0000415 case AMDGPUAS::FLAT_ADDRESS:
Matt Arsenault7d1b6c82016-04-29 06:25:10 +0000416 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
417 // For an unknown address space, this usually means that this is for some
418 // reason being used for pure arithmetic, and not based on some addressing
419 // computation. We don't have instructions that compute pointers with any
420 // addressing modes, so treat them as having no offset like flat
421 // instructions.
Tom Stellard70580f82015-07-20 14:28:41 +0000422 return isLegalFlatAddressingMode(AM);
423
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000424 default:
425 llvm_unreachable("unhandled address space");
426 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000427}
428
Matt Arsenaulte6986632015-01-14 01:35:22 +0000429bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000430 unsigned AddrSpace,
431 unsigned Align,
432 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000433 if (IsFast)
434 *IsFast = false;
435
Matt Arsenault1018c892014-04-24 17:08:26 +0000436 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
437 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000438 if (!VT.isSimple() || VT == MVT::Other)
439 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000440
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000441 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
442 AddrSpace == AMDGPUAS::REGION_ADDRESS) {
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000443 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
444 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
445 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000446 bool AlignedBy4 = (Align % 4 == 0);
447 if (IsFast)
448 *IsFast = AlignedBy4;
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000449
Sanjay Patelce74db92015-09-03 15:03:19 +0000450 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000451 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000452
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000453 if (Subtarget->hasUnalignedBufferAccess()) {
454 // If we have an uniform constant load, it still requires using a slow
455 // buffer instruction if unaligned.
456 if (IsFast) {
457 *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS) ?
458 (Align % 4 == 0) : true;
459 }
460
461 return true;
462 }
463
Tom Stellard33e64c62015-02-04 20:49:52 +0000464 // Smaller than dword value must be aligned.
Tom Stellard33e64c62015-02-04 20:49:52 +0000465 if (VT.bitsLT(MVT::i32))
466 return false;
467
Matt Arsenault1018c892014-04-24 17:08:26 +0000468 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
469 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000470 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000471 if (IsFast)
472 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000473
474 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000475}
476
Matt Arsenault46645fa2014-07-28 17:49:26 +0000477EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
478 unsigned SrcAlign, bool IsMemset,
479 bool ZeroMemset,
480 bool MemcpyStrSrc,
481 MachineFunction &MF) const {
482 // FIXME: Should account for address space here.
483
484 // The default fallback uses the private pointer size as a guess for a type to
485 // use. Make sure we switch these to 64-bit accesses.
486
487 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
488 return MVT::v4i32;
489
490 if (Size >= 8 && DstAlign >= 4)
491 return MVT::v2i32;
492
493 // Use the default.
494 return MVT::Other;
495}
496
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000497static bool isFlatGlobalAddrSpace(unsigned AS) {
498 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
499 AS == AMDGPUAS::FLAT_ADDRESS ||
500 AS == AMDGPUAS::CONSTANT_ADDRESS;
501}
502
503bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
504 unsigned DestAS) const {
Matt Arsenault37fefd62016-06-10 02:18:02 +0000505 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000506}
507
Tom Stellarda6f24c62015-12-15 20:55:55 +0000508bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
509 const MemSDNode *MemNode = cast<MemSDNode>(N);
510 const Value *Ptr = MemNode->getMemOperand()->getValue();
511
512 // UndefValue means this is a load of a kernel input. These are uniform.
Tom Stellard418beb72016-07-13 14:23:33 +0000513 // Sometimes LDS instructions have constant pointers.
514 // If Ptr is null, then that means this mem operand contains a
515 // PseudoSourceValue like GOT.
516 if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) ||
517 isa<Constant>(Ptr) || isa<GlobalValue>(Ptr))
Tom Stellarda6f24c62015-12-15 20:55:55 +0000518 return true;
519
Tom Stellard418beb72016-07-13 14:23:33 +0000520 const Instruction *I = dyn_cast<Instruction>(Ptr);
Tom Stellarda6f24c62015-12-15 20:55:55 +0000521 return I && I->getMetadata("amdgpu.uniform");
522}
523
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000524TargetLoweringBase::LegalizeTypeAction
525SITargetLowering::getPreferredVectorAction(EVT VT) const {
526 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
527 return TypeSplitVector;
528
529 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000530}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000531
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000532bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
533 Type *Ty) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000534 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000535 return TII->isInlineConstant(Imm);
536}
537
Tom Stellard2e045bb2016-01-20 00:13:22 +0000538bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
539
540 // SimplifySetCC uses this function to determine whether or not it should
541 // create setcc with i1 operands. We don't have instructions for i1 setcc.
542 if (VT == MVT::i1 && Op == ISD::SETCC)
543 return false;
544
545 return TargetLowering::isTypeDesirableForOp(Op, VT);
546}
547
Jan Veselyfea814d2016-06-21 20:46:20 +0000548SDValue SITargetLowering::LowerParameterPtr(SelectionDAG &DAG,
549 const SDLoc &SL, SDValue Chain,
550 unsigned Offset) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000551 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000552 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000553 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaultac234b62015-11-30 21:15:57 +0000554 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000555
Matt Arsenault86033ca2014-07-28 17:31:39 +0000556 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000557 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000558 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
559 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
Jan Veselyfea814d2016-06-21 20:46:20 +0000560 return DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
561 DAG.getConstant(Offset, SL, PtrVT));
562}
563SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
564 const SDLoc &SL, SDValue Chain,
565 unsigned Offset, bool Signed) const {
566 const DataLayout &DL = DAG.getDataLayout();
567 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
568 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
569 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Mehdi Amini44ede332015-07-09 02:09:04 +0000570 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000571 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
572
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000573 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000574
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000575 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000576 if (MemVT.isFloatingPoint())
577 ExtTy = ISD::EXTLOAD;
578
Jan Veselyfea814d2016-06-21 20:46:20 +0000579 SDValue Ptr = LowerParameterPtr(DAG, SL, Chain, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +0000580 return DAG.getLoad(ISD::UNINDEXED, ExtTy, VT, SL, Chain, Ptr, PtrOffset,
581 PtrInfo, MemVT, Align, MachineMemOperand::MONonTemporal |
582 MachineMemOperand::MOInvariant);
Tom Stellard94593ee2013-06-03 17:40:18 +0000583}
584
Christian Konig2c8f6d52013-03-07 09:03:52 +0000585SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000586 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000587 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
588 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000589 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000590
591 MachineFunction &MF = DAG.getMachineFunction();
592 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000593 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000594 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000595
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000596 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) {
Matt Arsenaultd48da142015-11-02 23:23:02 +0000597 const Function *Fn = MF.getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000598 DiagnosticInfoUnsupported NoGraphicsHSA(
599 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
Matt Arsenaultd48da142015-11-02 23:23:02 +0000600 DAG.getContext()->diagnose(NoGraphicsHSA);
Diana Picus81bc3172016-05-26 15:24:55 +0000601 return DAG.getEntryNode();
Matt Arsenaultd48da142015-11-02 23:23:02 +0000602 }
603
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000604 // Create stack objects that are used for emitting debugger prologue if
605 // "amdgpu-debugger-emit-prologue" attribute was specified.
606 if (ST.debuggerEmitPrologue())
607 createDebuggerPrologueStackObjects(MF);
608
Christian Konig2c8f6d52013-03-07 09:03:52 +0000609 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000610 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000611
612 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000613 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000614
615 // First check if it's a PS input addr
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000616 if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000617 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000618
Marek Olsakfccabaf2016-01-13 11:45:36 +0000619 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000620 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000621 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000622 ++PSInputNum;
623 continue;
624 }
625
Marek Olsakfccabaf2016-01-13 11:45:36 +0000626 Info->markPSInputAllocated(PSInputNum);
627 if (Arg.Used)
628 Info->PSInputEna |= 1 << PSInputNum;
629
630 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000631 }
632
Matt Arsenault539ca882016-05-05 20:27:02 +0000633 if (AMDGPU::isShader(CallConv)) {
634 // Second split vertices into their elements
635 if (Arg.VT.isVector()) {
636 ISD::InputArg NewArg = Arg;
637 NewArg.Flags.setSplit();
638 NewArg.VT = Arg.VT.getVectorElementType();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000639
Matt Arsenault539ca882016-05-05 20:27:02 +0000640 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
641 // three or five element vertex only needs three or five registers,
642 // NOT four or eight.
643 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
644 unsigned NumElements = ParamType->getVectorNumElements();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000645
Matt Arsenault539ca882016-05-05 20:27:02 +0000646 for (unsigned j = 0; j != NumElements; ++j) {
647 Splits.push_back(NewArg);
648 NewArg.PartOffset += NewArg.VT.getStoreSize();
649 }
650 } else {
651 Splits.push_back(Arg);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000652 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000653 }
654 }
655
656 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000657 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
658 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000659
Christian Konig99ee0f42013-03-07 09:04:14 +0000660 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000661 //
662 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
663 // PSInputAddr, the user wants to enable some bits after the compilation
664 // based on run-time states. Since we can't know what the final PSInputEna
665 // will look like, so we shouldn't do anything here and the user should take
666 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000667 //
668 // Otherwise, the following restrictions apply:
669 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
670 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
671 // enabled too.
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000672 if (CallConv == CallingConv::AMDGPU_PS &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000673 ((Info->getPSInputAddr() & 0x7F) == 0 ||
NAKAMURA Takumife1202c2016-06-20 00:37:41 +0000674 ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000675 CCInfo.AllocateReg(AMDGPU::VGPR0);
676 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000677 Info->markPSInputAllocated(0);
678 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000679 }
680
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000681 if (!AMDGPU::isShader(CallConv)) {
Tom Stellardaf775432013-10-23 00:44:32 +0000682 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
683 Splits);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000684
685 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
686 } else {
687 assert(!Info->hasPrivateSegmentBuffer() && !Info->hasDispatchPtr() &&
688 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() &&
689 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
690 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() &&
691 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() &&
692 !Info->hasWorkItemIDZ());
Tom Stellardaf775432013-10-23 00:44:32 +0000693 }
694
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000695 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
696 if (Info->hasPrivateSegmentBuffer()) {
697 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
698 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
699 CCInfo.AllocateReg(PrivateSegmentBufferReg);
700 }
701
702 if (Info->hasDispatchPtr()) {
703 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
704 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
705 CCInfo.AllocateReg(DispatchPtrReg);
706 }
707
Matt Arsenault48ab5262016-04-25 19:27:18 +0000708 if (Info->hasQueuePtr()) {
709 unsigned QueuePtrReg = Info->addQueuePtr(*TRI);
710 MF.addLiveIn(QueuePtrReg, &AMDGPU::SReg_64RegClass);
711 CCInfo.AllocateReg(QueuePtrReg);
712 }
713
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000714 if (Info->hasKernargSegmentPtr()) {
715 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
716 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
717 CCInfo.AllocateReg(InputPtrReg);
718 }
719
Matt Arsenault296b8492016-02-12 06:31:30 +0000720 if (Info->hasFlatScratchInit()) {
721 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
722 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SReg_64RegClass);
723 CCInfo.AllocateReg(FlatScratchInitReg);
724 }
725
Christian Konig2c8f6d52013-03-07 09:03:52 +0000726 AnalyzeFormalArguments(CCInfo, Splits);
727
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000728 SmallVector<SDValue, 16> Chains;
729
Christian Konig2c8f6d52013-03-07 09:03:52 +0000730 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
731
Christian Konigb7be72d2013-05-17 09:46:48 +0000732 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000733 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000734 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000735 continue;
736 }
737
Christian Konig2c8f6d52013-03-07 09:03:52 +0000738 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000739 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000740
741 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000742 VT = Ins[i].VT;
743 EVT MemVT = Splits[i].VT;
Tom Stellardb5798b02015-06-26 21:15:03 +0000744 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
745 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000746 // The first 36 bytes of the input buffer contains information about
747 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000748 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000749 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000750 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000751
Craig Toppere3dcce92015-08-01 22:20:21 +0000752 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000753 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000754 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Tom Stellardca7ecf32014-08-22 18:49:31 +0000755 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
756 // On SI local pointers are just offsets into LDS, so they are always
757 // less than 16-bits. On CI and newer they could potentially be
758 // real pointers, so we can't guarantee their size.
759 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
760 DAG.getValueType(MVT::i16));
761 }
762
Tom Stellarded882c22013-06-03 17:40:11 +0000763 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000764 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000765 continue;
766 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000767 assert(VA.isRegLoc() && "Parameter must be in a register!");
768
769 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000770
771 if (VT == MVT::i64) {
772 // For now assume it is a pointer
773 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
774 &AMDGPU::SReg_64RegClass);
775 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000776 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
777 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000778 continue;
779 }
780
781 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
782
783 Reg = MF.addLiveIn(Reg, RC);
784 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
785
Christian Konig2c8f6d52013-03-07 09:03:52 +0000786 if (Arg.VT.isVector()) {
787
788 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000789 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000790 unsigned NumElements = ParamType->getVectorNumElements();
791
792 SmallVector<SDValue, 4> Regs;
793 Regs.push_back(Val);
794 for (unsigned j = 1; j != NumElements; ++j) {
795 Reg = ArgLocs[ArgIdx++].getLocReg();
796 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000797
798 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
799 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000800 }
801
802 // Fill up the missing vector elements
803 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000804 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000805
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000806 InVals.push_back(DAG.getBuildVector(Arg.VT, DL, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000807 continue;
808 }
809
810 InVals.push_back(Val);
811 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000812
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000813 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
814 // these from the dispatch pointer.
815
816 // Start adding system SGPRs.
817 if (Info->hasWorkGroupIDX()) {
818 unsigned Reg = Info->addWorkGroupIDX();
819 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
820 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000821 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000822
823 if (Info->hasWorkGroupIDY()) {
824 unsigned Reg = Info->addWorkGroupIDY();
825 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
826 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000827 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000828
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000829 if (Info->hasWorkGroupIDZ()) {
830 unsigned Reg = Info->addWorkGroupIDZ();
831 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
832 CCInfo.AllocateReg(Reg);
833 }
834
835 if (Info->hasWorkGroupInfo()) {
836 unsigned Reg = Info->addWorkGroupInfo();
837 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
838 CCInfo.AllocateReg(Reg);
839 }
840
841 if (Info->hasPrivateSegmentWaveByteOffset()) {
842 // Scratch wave offset passed in system SGPR.
Tom Stellardf110f8f2016-04-14 16:27:03 +0000843 unsigned PrivateSegmentWaveByteOffsetReg;
844
845 if (AMDGPU::isShader(CallConv)) {
846 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
847 Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
848 } else
849 PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset();
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000850
851 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
852 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
853 }
854
855 // Now that we've figured out where the scratch register inputs are, see if
856 // should reserve the arguments and use them directly.
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000857 bool HasStackObjects = MF.getFrameInfo()->hasStackObjects();
Matt Arsenault296b8492016-02-12 06:31:30 +0000858 // Record that we know we have non-spill stack objects so we don't need to
859 // check all stack objects later.
860 if (HasStackObjects)
861 Info->setHasNonSpillStackObjects(true);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000862
863 if (ST.isAmdHsaOS()) {
864 // TODO: Assume we will spill without optimizations.
865 if (HasStackObjects) {
866 // If we have stack objects, we unquestionably need the private buffer
867 // resource. For the HSA ABI, this will be the first 4 user SGPR
868 // inputs. We can reserve those and use them directly.
869
870 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
871 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
872 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
873
874 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
875 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
876 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
877 } else {
878 unsigned ReservedBufferReg
879 = TRI->reservedPrivateSegmentBufferReg(MF);
880 unsigned ReservedOffsetReg
881 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
882
883 // We tentatively reserve the last registers (skipping the last two
884 // which may contain VCC). After register allocation, we'll replace
885 // these with the ones immediately after those which were really
886 // allocated. In the prologue copies will be inserted from the argument
887 // to these reserved registers.
888 Info->setScratchRSrcReg(ReservedBufferReg);
889 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
890 }
891 } else {
892 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
893
894 // Without HSA, relocations are used for the scratch pointer and the
895 // buffer resource setup is always inserted in the prologue. Scratch wave
896 // offset is still in an input SGPR.
897 Info->setScratchRSrcReg(ReservedBufferReg);
898
899 if (HasStackObjects) {
900 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
901 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
902 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
903 } else {
904 unsigned ReservedOffsetReg
905 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
906 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
907 }
908 }
909
910 if (Info->hasWorkItemIDX()) {
911 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
912 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
913 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000914 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000915
916 if (Info->hasWorkItemIDY()) {
917 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
918 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
919 CCInfo.AllocateReg(Reg);
920 }
921
922 if (Info->hasWorkItemIDZ()) {
923 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
924 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
925 CCInfo.AllocateReg(Reg);
926 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000927
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000928 if (Chains.empty())
929 return Chain;
930
931 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000932}
933
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000934SDValue
935SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
936 bool isVarArg,
937 const SmallVectorImpl<ISD::OutputArg> &Outs,
938 const SmallVectorImpl<SDValue> &OutVals,
939 const SDLoc &DL, SelectionDAG &DAG) const {
Marek Olsak8a0f3352016-01-13 17:23:04 +0000940 MachineFunction &MF = DAG.getMachineFunction();
941 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
942
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000943 if (!AMDGPU::isShader(CallConv))
Marek Olsak8a0f3352016-01-13 17:23:04 +0000944 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
945 OutVals, DL, DAG);
946
Marek Olsak8e9cc632016-01-13 17:23:09 +0000947 Info->setIfReturnsVoid(Outs.size() == 0);
948
Marek Olsak8a0f3352016-01-13 17:23:04 +0000949 SmallVector<ISD::OutputArg, 48> Splits;
950 SmallVector<SDValue, 48> SplitVals;
951
952 // Split vectors into their elements.
953 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
954 const ISD::OutputArg &Out = Outs[i];
955
956 if (Out.VT.isVector()) {
957 MVT VT = Out.VT.getVectorElementType();
958 ISD::OutputArg NewOut = Out;
959 NewOut.Flags.setSplit();
960 NewOut.VT = VT;
961
962 // We want the original number of vector elements here, e.g.
963 // three or five, not four or eight.
964 unsigned NumElements = Out.ArgVT.getVectorNumElements();
965
966 for (unsigned j = 0; j != NumElements; ++j) {
967 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
968 DAG.getConstant(j, DL, MVT::i32));
969 SplitVals.push_back(Elem);
970 Splits.push_back(NewOut);
971 NewOut.PartOffset += NewOut.VT.getStoreSize();
972 }
973 } else {
974 SplitVals.push_back(OutVals[i]);
975 Splits.push_back(Out);
976 }
977 }
978
979 // CCValAssign - represent the assignment of the return value to a location.
980 SmallVector<CCValAssign, 48> RVLocs;
981
982 // CCState - Info about the registers and stack slots.
983 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
984 *DAG.getContext());
985
986 // Analyze outgoing return values.
987 AnalyzeReturn(CCInfo, Splits);
988
989 SDValue Flag;
990 SmallVector<SDValue, 48> RetOps;
991 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
992
993 // Copy the result values into the output registers.
994 for (unsigned i = 0, realRVLocIdx = 0;
995 i != RVLocs.size();
996 ++i, ++realRVLocIdx) {
997 CCValAssign &VA = RVLocs[i];
998 assert(VA.isRegLoc() && "Can only return in registers!");
999
1000 SDValue Arg = SplitVals[realRVLocIdx];
1001
1002 // Copied from other backends.
1003 switch (VA.getLocInfo()) {
1004 default: llvm_unreachable("Unknown loc info!");
1005 case CCValAssign::Full:
1006 break;
1007 case CCValAssign::BCvt:
1008 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1009 break;
1010 }
1011
1012 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
1013 Flag = Chain.getValue(1);
1014 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1015 }
1016
1017 // Update chain and glue.
1018 RetOps[0] = Chain;
1019 if (Flag.getNode())
1020 RetOps.push_back(Flag);
1021
Matt Arsenault9babdf42016-06-22 20:15:28 +00001022 unsigned Opc = Info->returnsVoid() ? AMDGPUISD::ENDPGM : AMDGPUISD::RETURN;
1023 return DAG.getNode(Opc, DL, MVT::Other, RetOps);
Marek Olsak8a0f3352016-01-13 17:23:04 +00001024}
1025
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001026unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1027 SelectionDAG &DAG) const {
1028 unsigned Reg = StringSwitch<unsigned>(RegName)
1029 .Case("m0", AMDGPU::M0)
1030 .Case("exec", AMDGPU::EXEC)
1031 .Case("exec_lo", AMDGPU::EXEC_LO)
1032 .Case("exec_hi", AMDGPU::EXEC_HI)
1033 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1034 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1035 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1036 .Default(AMDGPU::NoRegister);
1037
1038 if (Reg == AMDGPU::NoRegister) {
1039 report_fatal_error(Twine("invalid register name \""
1040 + StringRef(RegName) + "\"."));
1041
1042 }
1043
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001044 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001045 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1046 report_fatal_error(Twine("invalid register \""
1047 + StringRef(RegName) + "\" for subtarget."));
1048 }
1049
1050 switch (Reg) {
1051 case AMDGPU::M0:
1052 case AMDGPU::EXEC_LO:
1053 case AMDGPU::EXEC_HI:
1054 case AMDGPU::FLAT_SCR_LO:
1055 case AMDGPU::FLAT_SCR_HI:
1056 if (VT.getSizeInBits() == 32)
1057 return Reg;
1058 break;
1059 case AMDGPU::EXEC:
1060 case AMDGPU::FLAT_SCR:
1061 if (VT.getSizeInBits() == 64)
1062 return Reg;
1063 break;
1064 default:
1065 llvm_unreachable("missing register type checking");
1066 }
1067
1068 report_fatal_error(Twine("invalid type for register \""
1069 + StringRef(RegName) + "\"."));
1070}
1071
Matt Arsenault786724a2016-07-12 21:41:32 +00001072// If kill is not the last instruction, split the block so kill is always a
1073// proper terminator.
1074MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI,
1075 MachineBasicBlock *BB) const {
1076 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1077
1078 MachineBasicBlock::iterator SplitPoint(&MI);
1079 ++SplitPoint;
1080
1081 if (SplitPoint == BB->end()) {
1082 // Don't bother with a new block.
1083 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1084 return BB;
1085 }
1086
1087 MachineFunction *MF = BB->getParent();
1088 MachineBasicBlock *SplitBB
1089 = MF->CreateMachineBasicBlock(BB->getBasicBlock());
1090
Matt Arsenault786724a2016-07-12 21:41:32 +00001091 // Fix the block phi references to point to the new block for the defs in the
1092 // second piece of the block.
1093 for (MachineBasicBlock *Succ : BB->successors()) {
1094 for (MachineInstr &MI : *Succ) {
1095 if (!MI.isPHI())
1096 break;
1097
Matt Arsenault83ab0492016-07-15 00:58:09 +00001098 for (unsigned I = 2, E = MI.getNumOperands(); I != E; I += 2) {
1099 MachineOperand &FromBB = MI.getOperand(I);
Matt Arsenault786724a2016-07-12 21:41:32 +00001100 if (BB == FromBB.getMBB()) {
Matt Arsenault83ab0492016-07-15 00:58:09 +00001101 FromBB.setMBB(SplitBB);
Matt Arsenault786724a2016-07-12 21:41:32 +00001102 break;
1103 }
1104 }
1105 }
1106 }
1107
1108 MF->insert(++MachineFunction::iterator(BB), SplitBB);
1109 SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end());
1110
Matt Arsenault786724a2016-07-12 21:41:32 +00001111 SplitBB->transferSuccessors(BB);
1112 BB->addSuccessor(SplitBB);
1113
1114 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1115 return SplitBB;
1116}
1117
1118MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter(
1119 MachineInstr &MI, MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001120 switch (MI.getOpcode()) {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001121 case AMDGPU::SI_INIT_M0: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001122 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001123 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001124 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001125 .addOperand(MI.getOperand(0));
1126 MI.eraseFromParent();
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001127 break;
1128 }
Matt Arsenault20711b72015-02-20 22:10:45 +00001129 case AMDGPU::BRANCH:
1130 return BB;
Changpeng Fang01f60622016-03-15 17:28:44 +00001131 case AMDGPU::GET_GROUPSTATICSIZE: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001132 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1133
Changpeng Fang01f60622016-03-15 17:28:44 +00001134 MachineFunction *MF = BB->getParent();
1135 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001136 DebugLoc DL = MI.getDebugLoc();
1137 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOVK_I32))
1138 .addOperand(MI.getOperand(0))
1139 .addImm(MFI->LDSSize);
1140 MI.eraseFromParent();
Changpeng Fang01f60622016-03-15 17:28:44 +00001141 return BB;
1142 }
Matt Arsenault786724a2016-07-12 21:41:32 +00001143 case AMDGPU::SI_KILL:
1144 return splitKillBlock(MI, BB);
Changpeng Fang01f60622016-03-15 17:28:44 +00001145 default:
1146 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001147 }
1148 return BB;
1149}
1150
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001151bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1152 // This currently forces unfolding various combinations of fsub into fma with
1153 // free fneg'd operands. As long as we have fast FMA (controlled by
1154 // isFMAFasterThanFMulAndFAdd), we should perform these.
1155
1156 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1157 // most of these combines appear to be cycle neutral but save on instruction
1158 // count / code size.
1159 return true;
1160}
1161
Mehdi Amini44ede332015-07-09 02:09:04 +00001162EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1163 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001164 if (!VT.isVector()) {
1165 return MVT::i1;
1166 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001167 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001168}
1169
Mehdi Aminieaabc512015-07-09 15:12:23 +00001170MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001171 return MVT::i32;
1172}
1173
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001174// Answering this is somewhat tricky and depends on the specific device which
1175// have different rates for fma or all f64 operations.
1176//
1177// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1178// regardless of which device (although the number of cycles differs between
1179// devices), so it is always profitable for f64.
1180//
1181// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1182// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1183// which we can always do even without fused FP ops since it returns the same
1184// result as the separate operations and since it is always full
1185// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1186// however does not support denormals, so we do report fma as faster if we have
1187// a fast fma device and require denormals.
1188//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001189bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1190 VT = VT.getScalarType();
1191
1192 if (!VT.isSimple())
1193 return false;
1194
1195 switch (VT.getSimpleVT().SimpleTy) {
1196 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001197 // This is as fast on some subtargets. However, we always have full rate f32
1198 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001199 // which we should prefer over fma. We can't use this if we want to support
1200 // denormals, so only report this in these cases.
1201 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001202 case MVT::f64:
1203 return true;
1204 default:
1205 break;
1206 }
1207
1208 return false;
1209}
1210
Tom Stellard75aadc22012-12-11 21:25:42 +00001211//===----------------------------------------------------------------------===//
1212// Custom DAG Lowering Operations
1213//===----------------------------------------------------------------------===//
1214
1215SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1216 switch (Op.getOpcode()) {
1217 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001218 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001219 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001220 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001221 SDValue Result = LowerLOAD(Op, DAG);
1222 assert((!Result.getNode() ||
1223 Result.getNode()->getNumValues() == 2) &&
1224 "Load should return a value and a chain");
1225 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001226 }
Tom Stellardaf775432013-10-23 00:44:32 +00001227
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001228 case ISD::FSIN:
1229 case ISD::FCOS:
1230 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001231 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001232 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard354a43c2016-04-01 18:27:37 +00001233 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001234 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001235 case ISD::GlobalAddress: {
1236 MachineFunction &MF = DAG.getMachineFunction();
1237 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1238 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001239 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001240 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001241 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001242 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Matt Arsenault99c14522016-04-25 19:27:24 +00001243 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG);
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001244 case ISD::TRAP: return lowerTRAP(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001245 }
1246 return SDValue();
1247}
1248
Tom Stellardf8794352012-12-19 22:10:31 +00001249/// \brief Helper function for LowerBRCOND
1250static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001251
Tom Stellardf8794352012-12-19 22:10:31 +00001252 SDNode *Parent = Value.getNode();
1253 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1254 I != E; ++I) {
1255
1256 if (I.getUse().get() != Value)
1257 continue;
1258
1259 if (I->getOpcode() == Opcode)
1260 return *I;
1261 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001262 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001263}
1264
Tom Stellardb02094e2014-07-21 15:45:01 +00001265SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
1266
Tom Stellardc98ee202015-07-16 19:40:07 +00001267 SDLoc SL(Op);
Tom Stellardb02094e2014-07-21 15:45:01 +00001268 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
1269 unsigned FrameIndex = FINode->getIndex();
1270
Matt Arsenault3a619852016-02-27 20:26:57 +00001271 // A FrameIndex node represents a 32-bit offset into scratch memory. If the
1272 // high bit of a frame index offset were to be set, this would mean that it
1273 // represented an offset of ~2GB * 64 = ~128GB from the start of the scratch
1274 // buffer, with 64 being the number of threads per wave.
Tom Stellardc98ee202015-07-16 19:40:07 +00001275 //
Matt Arsenault3a619852016-02-27 20:26:57 +00001276 // The maximum private allocation for the entire GPU is 4G, and we are
1277 // concerned with the largest the index could ever be for an individual
1278 // workitem. This will occur with the minmum dispatch size. If a program
1279 // requires more, the dispatch size will be reduced.
1280 //
1281 // With this limit, we can mark the high bit of the FrameIndex node as known
1282 // zero, which is important, because it means in most situations we can prove
1283 // that values derived from FrameIndex nodes are non-negative. This enables us
1284 // to take advantage of more addressing modes when accessing scratch buffers,
1285 // since for scratch reads/writes, the register offset must always be
1286 // positive.
1287
1288 uint64_t MaxGPUAlloc = UINT64_C(4) * 1024 * 1024 * 1024;
1289
1290 // XXX - It is unclear if partial dispatch works. Assume it works at half wave
1291 // granularity. It is probably a full wave.
1292 uint64_t MinGranularity = 32;
1293
1294 unsigned KnownBits = Log2_64(MaxGPUAlloc / MinGranularity);
1295 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), KnownBits);
Tom Stellardc98ee202015-07-16 19:40:07 +00001296
1297 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
Tom Stellardc98ee202015-07-16 19:40:07 +00001298 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
Matt Arsenault3a619852016-02-27 20:26:57 +00001299 DAG.getValueType(ExtVT));
Tom Stellardb02094e2014-07-21 15:45:01 +00001300}
1301
Tom Stellardbc4497b2016-02-12 23:45:29 +00001302bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault16f48d72016-02-13 00:36:10 +00001303 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN)
Tom Stellardbc4497b2016-02-12 23:45:29 +00001304 return false;
1305
1306 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1307 default: return false;
1308 case AMDGPUIntrinsic::amdgcn_if:
1309 case AMDGPUIntrinsic::amdgcn_else:
Matt Arsenault48d70cb2016-07-09 17:18:39 +00001310 case AMDGPUIntrinsic::amdgcn_break:
Tom Stellardbc4497b2016-02-12 23:45:29 +00001311 case AMDGPUIntrinsic::amdgcn_if_break:
1312 case AMDGPUIntrinsic::amdgcn_else_break:
1313 case AMDGPUIntrinsic::amdgcn_loop:
1314 case AMDGPUIntrinsic::amdgcn_end_cf:
1315 return true;
1316 }
1317}
1318
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001319void SITargetLowering::createDebuggerPrologueStackObjects(
1320 MachineFunction &MF) const {
1321 // Create stack objects that are used for emitting debugger prologue.
1322 //
1323 // Debugger prologue writes work group IDs and work item IDs to scratch memory
1324 // at fixed location in the following format:
1325 // offset 0: work group ID x
1326 // offset 4: work group ID y
1327 // offset 8: work group ID z
1328 // offset 16: work item ID x
1329 // offset 20: work item ID y
1330 // offset 24: work item ID z
1331 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1332 int ObjectIdx = 0;
1333
1334 // For each dimension:
1335 for (unsigned i = 0; i < 3; ++i) {
1336 // Create fixed stack object for work group ID.
1337 ObjectIdx = MF.getFrameInfo()->CreateFixedObject(4, i * 4, true);
1338 Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx);
1339 // Create fixed stack object for work item ID.
1340 ObjectIdx = MF.getFrameInfo()->CreateFixedObject(4, i * 4 + 16, true);
1341 Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx);
1342 }
1343}
1344
Tom Stellardf8794352012-12-19 22:10:31 +00001345/// This transforms the control flow intrinsics to get the branch destination as
1346/// last parameter, also switches branch target with BR if the need arise
1347SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1348 SelectionDAG &DAG) const {
1349
Andrew Trickef9de2a2013-05-25 02:42:55 +00001350 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001351
1352 SDNode *Intr = BRCOND.getOperand(1).getNode();
1353 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001354 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001355 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001356
1357 if (Intr->getOpcode() == ISD::SETCC) {
1358 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001359 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001360 Intr = SetCC->getOperand(0).getNode();
1361
1362 } else {
1363 // Get the target from BR if we don't negate the condition
1364 BR = findUser(BRCOND, ISD::BR);
1365 Target = BR->getOperand(1);
1366 }
1367
Nicolai Haehnleffbd56a2016-05-05 17:36:36 +00001368 if (!isCFIntrinsic(Intr)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +00001369 // This is a uniform branch so we don't need to legalize.
1370 return BRCOND;
1371 }
1372
1373 assert(!SetCC ||
1374 (SetCC->getConstantOperandVal(1) == 1 &&
Tom Stellardbc4497b2016-02-12 23:45:29 +00001375 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1376 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00001377
1378 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001379 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001380
1381 // operands of the new intrinsic call
1382 SmallVector<SDValue, 4> Ops;
1383 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001384 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001385 Ops.push_back(Target);
1386
1387 // build the new intrinsic call
1388 SDNode *Result = DAG.getNode(
1389 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001390 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001391
1392 if (BR) {
1393 // Give the branch instruction our target
1394 SDValue Ops[] = {
1395 BR->getOperand(0),
1396 BRCOND.getOperand(2)
1397 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001398 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1399 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1400 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001401 }
1402
1403 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1404
1405 // Copy the intrinsic results to registers
1406 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1407 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1408 if (!CopyToReg)
1409 continue;
1410
1411 Chain = DAG.getCopyToReg(
1412 Chain, DL,
1413 CopyToReg->getOperand(1),
1414 SDValue(Result, i - 1),
1415 SDValue());
1416
1417 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1418 }
1419
1420 // Remove the old intrinsic from the chain
1421 DAG.ReplaceAllUsesOfValueWith(
1422 SDValue(Intr, Intr->getNumValues() - 1),
1423 Intr->getOperand(0));
1424
1425 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001426}
1427
Matt Arsenault99c14522016-04-25 19:27:24 +00001428SDValue SITargetLowering::getSegmentAperture(unsigned AS,
1429 SelectionDAG &DAG) const {
1430 SDLoc SL;
1431 MachineFunction &MF = DAG.getMachineFunction();
1432 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00001433 unsigned UserSGPR = Info->getQueuePtrUserSGPR();
1434 assert(UserSGPR != AMDGPU::NoRegister);
1435
Matt Arsenault99c14522016-04-25 19:27:24 +00001436 SDValue QueuePtr = CreateLiveInRegister(
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00001437 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
Matt Arsenault99c14522016-04-25 19:27:24 +00001438
1439 // Offset into amd_queue_t for group_segment_aperture_base_hi /
1440 // private_segment_aperture_base_hi.
1441 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44;
1442
1443 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr,
1444 DAG.getConstant(StructOffset, SL, MVT::i64));
1445
1446 // TODO: Use custom target PseudoSourceValue.
1447 // TODO: We should use the value from the IR intrinsic call, but it might not
1448 // be available and how do we get it?
1449 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()),
1450 AMDGPUAS::CONSTANT_ADDRESS));
1451
1452 MachinePointerInfo PtrInfo(V, StructOffset);
Justin Lebar9c375812016-07-15 18:27:10 +00001453 return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr, PtrInfo,
1454 MinAlign(64, StructOffset),
1455 MachineMemOperand::MOInvariant);
Matt Arsenault99c14522016-04-25 19:27:24 +00001456}
1457
1458SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
1459 SelectionDAG &DAG) const {
1460 SDLoc SL(Op);
1461 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op);
1462
1463 SDValue Src = ASC->getOperand(0);
1464
1465 // FIXME: Really support non-0 null pointers.
1466 SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32);
1467 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
1468
1469 // flat -> local/private
1470 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
1471 if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1472 ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1473 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
1474 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
1475
1476 return DAG.getNode(ISD::SELECT, SL, MVT::i32,
1477 NonNull, Ptr, SegmentNullPtr);
1478 }
1479 }
1480
1481 // local/private -> flat
1482 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
1483 if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1484 ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1485 SDValue NonNull
1486 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
1487
1488 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG);
1489 SDValue CvtPtr
1490 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
1491
1492 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull,
1493 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr),
1494 FlatNullPtr);
1495 }
1496 }
1497
1498 // global <-> flat are no-ops and never emitted.
1499
1500 const MachineFunction &MF = DAG.getMachineFunction();
1501 DiagnosticInfoUnsupported InvalidAddrSpaceCast(
1502 *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc());
1503 DAG.getContext()->diagnose(InvalidAddrSpaceCast);
1504
1505 return DAG.getUNDEF(ASC->getValueType(0));
1506}
1507
Tom Stellard418beb72016-07-13 14:23:33 +00001508static bool shouldEmitGOTReloc(const GlobalValue *GV,
1509 const TargetMachine &TM) {
1510 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
1511 !TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
Tom Stellardb164a982016-06-25 01:59:16 +00001512}
1513
Tom Stellard418beb72016-07-13 14:23:33 +00001514bool
1515SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1516 // We can fold offsets for anything that doesn't require a GOT relocation.
1517 return GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
1518 !shouldEmitGOTReloc(GA->getGlobal(), getTargetMachine());
1519}
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001520
Tom Stellard418beb72016-07-13 14:23:33 +00001521static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV,
1522 SDLoc DL, unsigned Offset, EVT PtrVT,
1523 unsigned GAFlags = SIInstrInfo::MO_NONE) {
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001524 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is
1525 // lowered to the following code sequence:
1526 // s_getpc_b64 s[0:1]
1527 // s_add_u32 s0, s0, $symbol
1528 // s_addc_u32 s1, s1, 0
1529 //
1530 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
1531 // a fixup or relocation is emitted to replace $symbol with a literal
1532 // constant, which is a pc-relative offset from the encoding of the $symbol
1533 // operand to the global variable.
1534 //
1535 // What we want here is an offset from the value returned by s_getpc
1536 // (which is the address of the s_add_u32 instruction) to the global
1537 // variable, but since the encoding of $symbol starts 4 bytes after the start
1538 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too
1539 // small. This requires us to add 4 to the global variable offset in order to
1540 // compute the correct address.
Tom Stellard418beb72016-07-13 14:23:33 +00001541 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
1542 GAFlags);
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001543 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, GA);
1544}
1545
Tom Stellard418beb72016-07-13 14:23:33 +00001546SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1547 SDValue Op,
1548 SelectionDAG &DAG) const {
1549 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1550
1551 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS &&
1552 GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS)
1553 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1554
1555 SDLoc DL(GSD);
1556 const GlobalValue *GV = GSD->getGlobal();
1557 EVT PtrVT = Op.getValueType();
1558
1559 if (!shouldEmitGOTReloc(GV, getTargetMachine()))
1560 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT);
1561
1562 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT,
1563 SIInstrInfo::MO_GOTPCREL);
1564
1565 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext());
1566 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
1567 const DataLayout &DataLayout = DAG.getDataLayout();
1568 unsigned Align = DataLayout.getABITypeAlignment(PtrTy);
1569 // FIXME: Use a PseudoSourceValue once those can be assigned an address space.
1570 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
1571
Justin Lebar9c375812016-07-15 18:27:10 +00001572 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align,
1573 MachineMemOperand::MOInvariant);
Tom Stellard418beb72016-07-13 14:23:33 +00001574}
1575
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001576SDValue SITargetLowering::lowerTRAP(SDValue Op,
1577 SelectionDAG &DAG) const {
1578 const MachineFunction &MF = DAG.getMachineFunction();
1579 DiagnosticInfoUnsupported NoTrap(*MF.getFunction(),
1580 "trap handler not supported",
1581 Op.getDebugLoc(),
1582 DS_Warning);
1583 DAG.getContext()->diagnose(NoTrap);
1584
1585 // Emit s_endpgm.
1586
1587 // FIXME: This should really be selected to s_trap, but that requires
1588 // setting up the trap handler for it o do anything.
Matt Arsenault9babdf42016-06-22 20:15:28 +00001589 return DAG.getNode(AMDGPUISD::ENDPGM, SDLoc(Op), MVT::Other,
1590 Op.getOperand(0));
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001591}
1592
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001593SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain,
1594 const SDLoc &DL, SDValue V) const {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001595 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
1596 // the destination register.
1597 //
Tom Stellardfc92e772015-05-12 14:18:14 +00001598 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1599 // so we will end up with redundant moves to m0.
1600 //
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001601 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
1602
1603 // A Null SDValue creates a glue result.
1604 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
1605 V, Chain);
1606 return SDValue(M0, 0);
Tom Stellardfc92e772015-05-12 14:18:14 +00001607}
1608
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001609SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1610 SDValue Op,
1611 MVT VT,
1612 unsigned Offset) const {
1613 SDLoc SL(Op);
1614 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1615 DAG.getEntryNode(), Offset, false);
1616 // The local size values will have the hi 16-bits as zero.
1617 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1618 DAG.getValueType(VT));
1619}
1620
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001621static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
Matt Arsenaulte0132462016-01-30 05:19:45 +00001622 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001623 "non-hsa intrinsic with hsa target",
1624 DL.getDebugLoc());
1625 DAG.getContext()->diagnose(BadIntrin);
1626 return DAG.getUNDEF(VT);
1627}
1628
1629static SDValue emitRemovedIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
1630 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
1631 "intrinsic not supported on subtarget",
1632 DL.getDebugLoc());
Matt Arsenaulte0132462016-01-30 05:19:45 +00001633 DAG.getContext()->diagnose(BadIntrin);
1634 return DAG.getUNDEF(VT);
1635}
1636
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001637SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1638 SelectionDAG &DAG) const {
1639 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001640 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001641 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001642
1643 EVT VT = Op.getValueType();
1644 SDLoc DL(Op);
1645 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1646
Sanjay Patela2607012015-09-16 16:31:21 +00001647 // TODO: Should this propagate fast-math-flags?
1648
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001649 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001650 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault48ab5262016-04-25 19:27:18 +00001651 case Intrinsic::amdgcn_queue_ptr: {
Matt Arsenault800fecf2016-01-11 21:18:33 +00001652 if (!Subtarget->isAmdHsaOS()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00001653 DiagnosticInfoUnsupported BadIntrin(
1654 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
1655 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00001656 DAG.getContext()->diagnose(BadIntrin);
1657 return DAG.getUNDEF(VT);
1658 }
1659
Matt Arsenault48ab5262016-04-25 19:27:18 +00001660 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ?
1661 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00001662 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
Matt Arsenault48ab5262016-04-25 19:27:18 +00001663 TRI->getPreloadedValue(MF, Reg), VT);
1664 }
Jan Veselyfea814d2016-06-21 20:46:20 +00001665 case Intrinsic::amdgcn_implicitarg_ptr: {
1666 unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
1667 return LowerParameterPtr(DAG, DL, DAG.getEntryNode(), offset);
1668 }
Matt Arsenaultdc4ebad2016-04-29 21:16:52 +00001669 case Intrinsic::amdgcn_kernarg_segment_ptr: {
1670 unsigned Reg
1671 = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
1672 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
1673 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001674 case Intrinsic::amdgcn_rcp:
1675 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
1676 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001677 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001678 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001679 case Intrinsic::amdgcn_rsq_legacy: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001680 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001681 return emitRemovedIntrinsicError(DAG, DL, VT);
1682
1683 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
1684 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +00001685 case Intrinsic::amdgcn_rsq_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001686 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00001687 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00001688
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001689 Type *Type = VT.getTypeForEVT(*DAG.getContext());
1690 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
1691 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
1692
1693 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
1694 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
1695 DAG.getConstantFP(Max, DL, VT));
1696 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
1697 DAG.getConstantFP(Min, DL, VT));
1698 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001699 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001700 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001701 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00001702
Tom Stellardec2e43c2014-09-22 15:35:29 +00001703 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1704 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001705 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001706 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001707 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00001708
Tom Stellardec2e43c2014-09-22 15:35:29 +00001709 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1710 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001711 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001712 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001713 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00001714
Tom Stellardec2e43c2014-09-22 15:35:29 +00001715 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1716 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001717 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001718 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001719 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00001720
Tom Stellardec2e43c2014-09-22 15:35:29 +00001721 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1722 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001723 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001724 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001725 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00001726
Tom Stellardec2e43c2014-09-22 15:35:29 +00001727 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1728 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001729 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001730 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001731 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00001732
Tom Stellardec2e43c2014-09-22 15:35:29 +00001733 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1734 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001735 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001736 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001737 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00001738
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001739 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1740 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001741 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001742 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001743 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00001744
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001745 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1746 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001747 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001748 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001749 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00001750
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001751 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1752 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenaultbef34e22016-01-22 21:30:34 +00001753 case Intrinsic::amdgcn_read_workdim:
1754 case AMDGPUIntrinsic::AMDGPU_read_workdim: // Legacy name.
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001755 // Really only 2 bits.
1756 return lowerImplicitZextParam(DAG, Op, MVT::i8,
1757 getImplicitParameterOffset(MFI, GRID_DIM));
Matt Arsenault43976df2016-01-30 04:25:19 +00001758 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001759 case Intrinsic::r600_read_tgid_x:
1760 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001761 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001762 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001763 case Intrinsic::r600_read_tgid_y:
1764 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001765 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001766 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001767 case Intrinsic::r600_read_tgid_z:
1768 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001769 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001770 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001771 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001772 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001773 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001774 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001775 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001776 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001777 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001778 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001779 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001780 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001781 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001782 case AMDGPUIntrinsic::SI_load_const: {
1783 SDValue Ops[] = {
1784 Op.getOperand(1),
1785 Op.getOperand(2)
1786 };
1787
1788 MachineMemOperand *MMO = MF.getMachineMemOperand(
1789 MachinePointerInfo(),
1790 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
1791 VT.getStoreSize(), 4);
1792 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
1793 Op->getVTList(), Ops, VT, MMO);
1794 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001795 case AMDGPUIntrinsic::SI_vs_load_input:
1796 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
1797 Op.getOperand(1),
1798 Op.getOperand(2),
1799 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00001800
Tom Stellard2a9d9472015-05-12 15:00:46 +00001801 case AMDGPUIntrinsic::SI_fs_constant: {
1802 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1803 SDValue Glue = M0.getValue(1);
1804 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
1805 DAG.getConstant(2, DL, MVT::i32), // P0
1806 Op.getOperand(1), Op.getOperand(2), Glue);
1807 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00001808 case AMDGPUIntrinsic::SI_packf16:
1809 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
1810 return DAG.getUNDEF(MVT::i32);
1811 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00001812 case AMDGPUIntrinsic::SI_fs_interp: {
1813 SDValue IJ = Op.getOperand(4);
1814 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1815 DAG.getConstant(0, DL, MVT::i32));
1816 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1817 DAG.getConstant(1, DL, MVT::i32));
1818 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1819 SDValue Glue = M0.getValue(1);
1820 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
1821 DAG.getVTList(MVT::f32, MVT::Glue),
1822 I, Op.getOperand(1), Op.getOperand(2), Glue);
1823 Glue = SDValue(P1.getNode(), 1);
1824 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
1825 Op.getOperand(1), Op.getOperand(2), Glue);
1826 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00001827 case Intrinsic::amdgcn_interp_p1: {
1828 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
1829 SDValue Glue = M0.getValue(1);
1830 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
1831 Op.getOperand(2), Op.getOperand(3), Glue);
1832 }
1833 case Intrinsic::amdgcn_interp_p2: {
1834 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
1835 SDValue Glue = SDValue(M0.getNode(), 1);
1836 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
1837 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
1838 Glue);
1839 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00001840 case Intrinsic::amdgcn_sin:
1841 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
1842
1843 case Intrinsic::amdgcn_cos:
1844 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
1845
1846 case Intrinsic::amdgcn_log_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001847 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00001848 return SDValue();
1849
1850 DiagnosticInfoUnsupported BadIntrin(
1851 *MF.getFunction(), "intrinsic not supported on subtarget",
1852 DL.getDebugLoc());
1853 DAG.getContext()->diagnose(BadIntrin);
1854 return DAG.getUNDEF(VT);
1855 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001856 case Intrinsic::amdgcn_ldexp:
1857 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
1858 Op.getOperand(1), Op.getOperand(2));
Matt Arsenault74015162016-05-28 00:19:52 +00001859
1860 case Intrinsic::amdgcn_fract:
1861 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
1862
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001863 case Intrinsic::amdgcn_class:
1864 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
1865 Op.getOperand(1), Op.getOperand(2));
1866 case Intrinsic::amdgcn_div_fmas:
1867 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
1868 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
1869 Op.getOperand(4));
1870
1871 case Intrinsic::amdgcn_div_fixup:
1872 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
1873 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
1874
1875 case Intrinsic::amdgcn_trig_preop:
1876 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
1877 Op.getOperand(1), Op.getOperand(2));
1878 case Intrinsic::amdgcn_div_scale: {
1879 // 3rd parameter required to be a constant.
1880 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1881 if (!Param)
1882 return DAG.getUNDEF(VT);
1883
1884 // Translate to the operands expected by the machine instruction. The
1885 // first parameter must be the same as the first instruction.
1886 SDValue Numerator = Op.getOperand(1);
1887 SDValue Denominator = Op.getOperand(2);
1888
1889 // Note this order is opposite of the machine instruction's operations,
1890 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
1891 // intrinsic has the numerator as the first operand to match a normal
1892 // division operation.
1893
1894 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
1895
1896 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
1897 Denominator, Numerator);
1898 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001899 default:
1900 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1901 }
1902}
1903
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001904SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
1905 SelectionDAG &DAG) const {
1906 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1907 switch (IntrID) {
1908 case Intrinsic::amdgcn_atomic_inc:
1909 case Intrinsic::amdgcn_atomic_dec: {
1910 MemSDNode *M = cast<MemSDNode>(Op);
1911 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
1912 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
1913 SDValue Ops[] = {
1914 M->getOperand(0), // Chain
1915 M->getOperand(2), // Ptr
1916 M->getOperand(3) // Value
1917 };
1918
1919 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
1920 M->getMemoryVT(), M->getMemOperand());
1921 }
1922 default:
1923 return SDValue();
1924 }
1925}
1926
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001927SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1928 SelectionDAG &DAG) const {
1929 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00001930 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001931 SDValue Chain = Op.getOperand(0);
1932 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1933
1934 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00001935 case AMDGPUIntrinsic::SI_sendmsg: {
1936 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
1937 SDValue Glue = Chain.getValue(1);
1938 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
1939 Op.getOperand(2), Glue);
1940 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001941 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001942 SDValue Ops[] = {
1943 Chain,
1944 Op.getOperand(2),
1945 Op.getOperand(3),
1946 Op.getOperand(4),
1947 Op.getOperand(5),
1948 Op.getOperand(6),
1949 Op.getOperand(7),
1950 Op.getOperand(8),
1951 Op.getOperand(9),
1952 Op.getOperand(10),
1953 Op.getOperand(11),
1954 Op.getOperand(12),
1955 Op.getOperand(13),
1956 Op.getOperand(14)
1957 };
1958
1959 EVT VT = Op.getOperand(3).getValueType();
1960
1961 MachineMemOperand *MMO = MF.getMachineMemOperand(
1962 MachinePointerInfo(),
1963 MachineMemOperand::MOStore,
1964 VT.getStoreSize(), 4);
1965 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1966 Op->getVTList(), Ops, VT, MMO);
1967 }
Matt Arsenault00568682016-07-13 06:04:22 +00001968 case AMDGPUIntrinsic::AMDGPU_kill: {
1969 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Op.getOperand(2))) {
1970 if (!K->isNegative())
1971 return Chain;
1972 }
1973
1974 return Op;
1975 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001976 default:
1977 return SDValue();
1978 }
1979}
1980
Tom Stellard81d871d2013-11-13 23:36:50 +00001981SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1982 SDLoc DL(Op);
1983 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001984 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00001985 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00001986
Matt Arsenaulta1436412016-02-10 18:21:45 +00001987 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
1988 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected");
Matt Arsenault6dfda962016-02-10 18:21:39 +00001989 // FIXME: Copied from PPC
1990 // First, load into 32 bits, then truncate to 1 bit.
1991
1992 SDValue Chain = Load->getChain();
1993 SDValue BasePtr = Load->getBasePtr();
1994 MachineMemOperand *MMO = Load->getMemOperand();
1995
1996 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
1997 BasePtr, MVT::i8, MMO);
1998
1999 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002000 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00002001 NewLD.getValue(1)
2002 };
2003
2004 return DAG.getMergeValues(Ops, DL);
2005 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002006
Matt Arsenaulta1436412016-02-10 18:21:45 +00002007 if (!MemVT.isVector())
2008 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002009
Matt Arsenaulta1436412016-02-10 18:21:45 +00002010 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
2011 "Custom lowering for non-i32 vectors hasn't been implemented.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002012
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002013 unsigned AS = Load->getAddressSpace();
2014 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT,
2015 AS, Load->getAlignment())) {
2016 SDValue Ops[2];
2017 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG);
2018 return DAG.getMergeValues(Ops, DL);
2019 }
2020
2021 unsigned NumElements = MemVT.getVectorNumElements();
2022 switch (AS) {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002023 case AMDGPUAS::CONSTANT_ADDRESS:
2024 if (isMemOpUniform(Load))
2025 return SDValue();
2026 // Non-uniform loads will be selected to MUBUF instructions, so they
2027 // have the same legalization requires ments as global and private
2028 // loads.
2029 //
2030 // Fall-through
2031 case AMDGPUAS::GLOBAL_ADDRESS:
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002032 case AMDGPUAS::FLAT_ADDRESS:
2033 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00002034 return SplitVectorLoad(Op, DAG);
2035 // v4 loads are supported for private and global memory.
2036 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002037 case AMDGPUAS::PRIVATE_ADDRESS: {
2038 // Depending on the setting of the private_element_size field in the
2039 // resource descriptor, we can only make private accesses up to a certain
2040 // size.
2041 switch (Subtarget->getMaxPrivateElementSize()) {
2042 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002043 return scalarizeVectorLoad(Load, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002044 case 8:
2045 if (NumElements > 2)
2046 return SplitVectorLoad(Op, DAG);
2047 return SDValue();
2048 case 16:
2049 // Same as global/flat
2050 if (NumElements > 4)
2051 return SplitVectorLoad(Op, DAG);
2052 return SDValue();
2053 default:
2054 llvm_unreachable("unsupported private_element_size");
2055 }
2056 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002057 case AMDGPUAS::LOCAL_ADDRESS: {
2058 if (NumElements > 2)
2059 return SplitVectorLoad(Op, DAG);
2060
2061 if (NumElements == 2)
2062 return SDValue();
2063
Matt Arsenaulta1436412016-02-10 18:21:45 +00002064 // If properly aligned, if we split we might be able to use ds_read_b64.
2065 return SplitVectorLoad(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002066 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00002067 default:
2068 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00002069 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002070}
2071
Tom Stellard0ec134f2014-02-04 17:18:40 +00002072SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2073 if (Op.getValueType() != MVT::i64)
2074 return SDValue();
2075
2076 SDLoc DL(Op);
2077 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002078
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002079 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2080 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002081
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002082 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
2083 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
2084
2085 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
2086 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002087
2088 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
2089
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002090 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
2091 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002092
2093 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
2094
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002095 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002096 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002097}
2098
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002099// Catch division cases where we can use shortcuts with rcp and rsq
2100// instructions.
2101SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002102 SDLoc SL(Op);
2103 SDValue LHS = Op.getOperand(0);
2104 SDValue RHS = Op.getOperand(1);
2105 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002106 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002107
2108 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002109 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
2110 CLHS->isExactlyValue(1.0)) {
2111 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
2112 // the CI documentation has a worst case error of 1 ulp.
2113 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
2114 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002115
2116 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002117 //
2118 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
2119 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002120 if (RHS.getOpcode() == ISD::FSQRT)
2121 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
2122
2123 // 1.0 / x -> rcp(x)
2124 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
2125 }
2126 }
2127
Wei Dinged0f97f2016-06-09 19:17:15 +00002128 const SDNodeFlags *Flags = Op->getFlags();
2129
2130 if (Unsafe || Flags->hasAllowReciprocal()) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002131 // Turn into multiply by the reciprocal.
2132 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00002133 SDNodeFlags Flags;
2134 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002135 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00002136 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002137 }
2138
2139 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002140}
2141
2142SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher538d09d02016-06-07 20:27:12 +00002143 if (SDValue FastLowered = LowerFastFDIV(Op, DAG))
2144 return FastLowered;
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002145
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002146 SDLoc SL(Op);
2147 SDValue LHS = Op.getOperand(0);
2148 SDValue RHS = Op.getOperand(1);
2149
Wei Dinged0f97f2016-06-09 19:17:15 +00002150 // faster 2.5 ulp fdiv when using -amdgpu-fast-fdiv flag
2151 if (EnableAMDGPUFastFDIV) {
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002152 // This does not support denormals.
Wei Dinged0f97f2016-06-09 19:17:15 +00002153 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002154
Wei Dinged0f97f2016-06-09 19:17:15 +00002155 const APFloat K0Val(BitsToFloat(0x6f800000));
2156 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002157
Wei Dinged0f97f2016-06-09 19:17:15 +00002158 const APFloat K1Val(BitsToFloat(0x2f800000));
2159 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002160
Wei Dinged0f97f2016-06-09 19:17:15 +00002161 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
2162
2163 EVT SetCCVT =
2164 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
2165
2166 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
2167
2168 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
2169
2170 // TODO: Should this propagate fast-math-flags?
2171
2172 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
2173
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002174 // rcp does not support denormals.
Wei Dinged0f97f2016-06-09 19:17:15 +00002175 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
2176
2177 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
2178
2179 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002180 }
2181
Wei Dinged0f97f2016-06-09 19:17:15 +00002182 // Generates more precise fpdiv32.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002183 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002184
Wei Dinged0f97f2016-06-09 19:17:15 +00002185 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002186
Wei Dinged0f97f2016-06-09 19:17:15 +00002187 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, RHS, RHS, LHS);
2188 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, LHS, RHS, LHS);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002189
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002190 // Denominator is scaled to not be denormal, so using rcp is ok.
Wei Dinged0f97f2016-06-09 19:17:15 +00002191 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002192
Wei Dinged0f97f2016-06-09 19:17:15 +00002193 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002194
Wei Dinged0f97f2016-06-09 19:17:15 +00002195 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, ApproxRcp, One);
2196 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, ApproxRcp);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002197
Wei Dinged0f97f2016-06-09 19:17:15 +00002198 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, NumeratorScaled, Fma1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002199
Wei Dinged0f97f2016-06-09 19:17:15 +00002200 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, NumeratorScaled);
2201 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul);
2202 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, NumeratorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002203
Wei Dinged0f97f2016-06-09 19:17:15 +00002204 SDValue Scale = NumeratorScaled.getValue(1);
2205 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, Fma4, Fma1, Fma3, Scale);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002206
Wei Dinged0f97f2016-06-09 19:17:15 +00002207 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002208}
2209
2210SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002211 if (DAG.getTarget().Options.UnsafeFPMath)
2212 return LowerFastFDIV(Op, DAG);
2213
2214 SDLoc SL(Op);
2215 SDValue X = Op.getOperand(0);
2216 SDValue Y = Op.getOperand(1);
2217
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002218 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002219
2220 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
2221
2222 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
2223
2224 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
2225
2226 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
2227
2228 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
2229
2230 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
2231
2232 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
2233
2234 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
2235
2236 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
2237 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
2238
2239 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
2240 NegDivScale0, Mul, DivScale1);
2241
2242 SDValue Scale;
2243
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002244 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002245 // Workaround a hardware bug on SI where the condition output from div_scale
2246 // is not usable.
2247
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002248 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002249
2250 // Figure out if the scale to use for div_fmas.
2251 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
2252 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
2253 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
2254 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
2255
2256 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
2257 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
2258
2259 SDValue Scale0Hi
2260 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
2261 SDValue Scale1Hi
2262 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
2263
2264 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
2265 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
2266 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
2267 } else {
2268 Scale = DivScale1.getValue(1);
2269 }
2270
2271 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
2272 Fma4, Fma3, Mul, Scale);
2273
2274 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002275}
2276
2277SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
2278 EVT VT = Op.getValueType();
2279
2280 if (VT == MVT::f32)
2281 return LowerFDIV32(Op, DAG);
2282
2283 if (VT == MVT::f64)
2284 return LowerFDIV64(Op, DAG);
2285
2286 llvm_unreachable("Unexpected type for fdiv");
2287}
2288
Tom Stellard81d871d2013-11-13 23:36:50 +00002289SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2290 SDLoc DL(Op);
2291 StoreSDNode *Store = cast<StoreSDNode>(Op);
2292 EVT VT = Store->getMemoryVT();
2293
Matt Arsenault95245662016-02-11 05:32:46 +00002294 if (VT == MVT::i1) {
2295 return DAG.getTruncStore(Store->getChain(), DL,
2296 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
2297 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00002298 }
2299
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002300 assert(VT.isVector() &&
2301 Store->getValue().getValueType().getScalarType() == MVT::i32);
2302
2303 unsigned AS = Store->getAddressSpace();
2304 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
2305 AS, Store->getAlignment())) {
2306 return expandUnalignedStore(Store, DAG);
2307 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002308
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002309 unsigned NumElements = VT.getVectorNumElements();
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002310 switch (AS) {
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002311 case AMDGPUAS::GLOBAL_ADDRESS:
2312 case AMDGPUAS::FLAT_ADDRESS:
2313 if (NumElements > 4)
2314 return SplitVectorStore(Op, DAG);
2315 return SDValue();
2316 case AMDGPUAS::PRIVATE_ADDRESS: {
2317 switch (Subtarget->getMaxPrivateElementSize()) {
2318 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002319 return scalarizeVectorStore(Store, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002320 case 8:
2321 if (NumElements > 2)
2322 return SplitVectorStore(Op, DAG);
2323 return SDValue();
2324 case 16:
2325 if (NumElements > 4)
2326 return SplitVectorStore(Op, DAG);
2327 return SDValue();
2328 default:
2329 llvm_unreachable("unsupported private_element_size");
2330 }
2331 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002332 case AMDGPUAS::LOCAL_ADDRESS: {
2333 if (NumElements > 2)
2334 return SplitVectorStore(Op, DAG);
2335
2336 if (NumElements == 2)
2337 return Op;
2338
Matt Arsenault95245662016-02-11 05:32:46 +00002339 // If properly aligned, if we split we might be able to use ds_write_b64.
2340 return SplitVectorStore(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002341 }
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002342 default:
2343 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00002344 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002345}
2346
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002347SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002348 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002349 EVT VT = Op.getValueType();
2350 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00002351 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002352 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
2353 DAG.getNode(ISD::FMUL, DL, VT, Arg,
2354 DAG.getConstantFP(0.5/M_PI, DL,
2355 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002356
2357 switch (Op.getOpcode()) {
2358 case ISD::FCOS:
2359 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
2360 case ISD::FSIN:
2361 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
2362 default:
2363 llvm_unreachable("Wrong trig opcode");
2364 }
2365}
2366
Tom Stellard354a43c2016-04-01 18:27:37 +00002367SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
2368 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
2369 assert(AtomicNode->isCompareAndSwap());
2370 unsigned AS = AtomicNode->getAddressSpace();
2371
2372 // No custom lowering required for local address space
2373 if (!isFlatGlobalAddrSpace(AS))
2374 return Op;
2375
2376 // Non-local address space requires custom lowering for atomic compare
2377 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
2378 SDLoc DL(Op);
2379 SDValue ChainIn = Op.getOperand(0);
2380 SDValue Addr = Op.getOperand(1);
2381 SDValue Old = Op.getOperand(2);
2382 SDValue New = Op.getOperand(3);
2383 EVT VT = Op.getValueType();
2384 MVT SimpleVT = VT.getSimpleVT();
2385 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
2386
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002387 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
Tom Stellard354a43c2016-04-01 18:27:37 +00002388 SDValue Ops[] = { ChainIn, Addr, NewOld };
Matt Arsenault88701812016-06-09 23:42:48 +00002389
2390 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(),
2391 Ops, VT, AtomicNode->getMemOperand());
Tom Stellard354a43c2016-04-01 18:27:37 +00002392}
2393
Tom Stellard75aadc22012-12-11 21:25:42 +00002394//===----------------------------------------------------------------------===//
2395// Custom DAG optimizations
2396//===----------------------------------------------------------------------===//
2397
Matt Arsenault364a6742014-06-11 17:50:44 +00002398SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00002399 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00002400 EVT VT = N->getValueType(0);
2401 EVT ScalarVT = VT.getScalarType();
2402 if (ScalarVT != MVT::f32)
2403 return SDValue();
2404
2405 SelectionDAG &DAG = DCI.DAG;
2406 SDLoc DL(N);
2407
2408 SDValue Src = N->getOperand(0);
2409 EVT SrcVT = Src.getValueType();
2410
2411 // TODO: We could try to match extracting the higher bytes, which would be
2412 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
2413 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
2414 // about in practice.
2415 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
2416 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
2417 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
2418 DCI.AddToWorklist(Cvt.getNode());
2419 return Cvt;
2420 }
2421 }
2422
Matt Arsenault364a6742014-06-11 17:50:44 +00002423 return SDValue();
2424}
2425
Eric Christopher6c5b5112015-03-11 18:43:21 +00002426/// \brief Return true if the given offset Size in bytes can be folded into
2427/// the immediate offsets of a memory instruction for the given address space.
2428static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002429 const SISubtarget &STI) {
Eric Christopher6c5b5112015-03-11 18:43:21 +00002430 switch (AS) {
2431 case AMDGPUAS::GLOBAL_ADDRESS: {
2432 // MUBUF instructions a 12-bit offset in bytes.
2433 return isUInt<12>(OffsetSize);
2434 }
2435 case AMDGPUAS::CONSTANT_ADDRESS: {
2436 // SMRD instructions have an 8-bit offset in dwords on SI and
2437 // a 20-bit offset in bytes on VI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002438 if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Eric Christopher6c5b5112015-03-11 18:43:21 +00002439 return isUInt<20>(OffsetSize);
2440 else
2441 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
2442 }
2443 case AMDGPUAS::LOCAL_ADDRESS:
2444 case AMDGPUAS::REGION_ADDRESS: {
2445 // The single offset versions have a 16-bit offset in bytes.
2446 return isUInt<16>(OffsetSize);
2447 }
2448 case AMDGPUAS::PRIVATE_ADDRESS:
2449 // Indirect register addressing does not use any offsets.
2450 default:
2451 return 0;
2452 }
2453}
2454
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002455// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
2456
2457// This is a variant of
2458// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
2459//
2460// The normal DAG combiner will do this, but only if the add has one use since
2461// that would increase the number of instructions.
2462//
2463// This prevents us from seeing a constant offset that can be folded into a
2464// memory instruction's addressing mode. If we know the resulting add offset of
2465// a pointer can be folded into an addressing offset, we can replace the pointer
2466// operand with the add of new constant offset. This eliminates one of the uses,
2467// and may allow the remaining use to also be simplified.
2468//
2469SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
2470 unsigned AddrSpace,
2471 DAGCombinerInfo &DCI) const {
2472 SDValue N0 = N->getOperand(0);
2473 SDValue N1 = N->getOperand(1);
2474
2475 if (N0.getOpcode() != ISD::ADD)
2476 return SDValue();
2477
2478 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
2479 if (!CN1)
2480 return SDValue();
2481
2482 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2483 if (!CAdd)
2484 return SDValue();
2485
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002486 // If the resulting offset is too large, we can't fold it into the addressing
2487 // mode offset.
2488 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002489 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget()))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002490 return SDValue();
2491
2492 SelectionDAG &DAG = DCI.DAG;
2493 SDLoc SL(N);
2494 EVT VT = N->getValueType(0);
2495
2496 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002497 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002498
2499 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
2500}
2501
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002502SDValue SITargetLowering::performAndCombine(SDNode *N,
2503 DAGCombinerInfo &DCI) const {
2504 if (DCI.isBeforeLegalize())
2505 return SDValue();
2506
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002507 if (SDValue Base = AMDGPUTargetLowering::performAndCombine(N, DCI))
2508 return Base;
2509
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002510 SelectionDAG &DAG = DCI.DAG;
2511
2512 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
2513 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
2514 SDValue LHS = N->getOperand(0);
2515 SDValue RHS = N->getOperand(1);
2516
2517 if (LHS.getOpcode() == ISD::SETCC &&
2518 RHS.getOpcode() == ISD::SETCC) {
2519 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
2520 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
2521
2522 SDValue X = LHS.getOperand(0);
2523 SDValue Y = RHS.getOperand(0);
2524 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
2525 return SDValue();
2526
2527 if (LCC == ISD::SETO) {
2528 if (X != LHS.getOperand(1))
2529 return SDValue();
2530
2531 if (RCC == ISD::SETUNE) {
2532 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
2533 if (!C1 || !C1->isInfinity() || C1->isNegative())
2534 return SDValue();
2535
2536 const uint32_t Mask = SIInstrFlags::N_NORMAL |
2537 SIInstrFlags::N_SUBNORMAL |
2538 SIInstrFlags::N_ZERO |
2539 SIInstrFlags::P_ZERO |
2540 SIInstrFlags::P_SUBNORMAL |
2541 SIInstrFlags::P_NORMAL;
2542
2543 static_assert(((~(SIInstrFlags::S_NAN |
2544 SIInstrFlags::Q_NAN |
2545 SIInstrFlags::N_INFINITY |
2546 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
2547 "mask not equal");
2548
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002549 SDLoc DL(N);
2550 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2551 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002552 }
2553 }
2554 }
2555
2556 return SDValue();
2557}
2558
Matt Arsenaultf2290332015-01-06 23:00:39 +00002559SDValue SITargetLowering::performOrCombine(SDNode *N,
2560 DAGCombinerInfo &DCI) const {
2561 SelectionDAG &DAG = DCI.DAG;
2562 SDValue LHS = N->getOperand(0);
2563 SDValue RHS = N->getOperand(1);
2564
Matt Arsenault3b082382016-04-12 18:24:38 +00002565 EVT VT = N->getValueType(0);
2566 if (VT == MVT::i64) {
2567 // TODO: This could be a generic combine with a predicate for extracting the
2568 // high half of an integer being free.
2569
2570 // (or i64:x, (zero_extend i32:y)) ->
2571 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
2572 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
2573 RHS.getOpcode() != ISD::ZERO_EXTEND)
2574 std::swap(LHS, RHS);
2575
2576 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
2577 SDValue ExtSrc = RHS.getOperand(0);
2578 EVT SrcVT = ExtSrc.getValueType();
2579 if (SrcVT == MVT::i32) {
2580 SDLoc SL(N);
2581 SDValue LowLHS, HiBits;
2582 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
2583 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
2584
2585 DCI.AddToWorklist(LowOr.getNode());
2586 DCI.AddToWorklist(HiBits.getNode());
2587
2588 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
2589 LowOr, HiBits);
2590 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2591 }
2592 }
2593 }
2594
Matt Arsenaultf2290332015-01-06 23:00:39 +00002595 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
2596 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
2597 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
2598 SDValue Src = LHS.getOperand(0);
2599 if (Src != RHS.getOperand(0))
2600 return SDValue();
2601
2602 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
2603 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
2604 if (!CLHS || !CRHS)
2605 return SDValue();
2606
2607 // Only 10 bits are used.
2608 static const uint32_t MaxMask = 0x3ff;
2609
2610 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002611 SDLoc DL(N);
2612 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2613 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00002614 }
2615
2616 return SDValue();
2617}
2618
2619SDValue SITargetLowering::performClassCombine(SDNode *N,
2620 DAGCombinerInfo &DCI) const {
2621 SelectionDAG &DAG = DCI.DAG;
2622 SDValue Mask = N->getOperand(1);
2623
2624 // fp_class x, 0 -> false
2625 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
2626 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002627 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002628 }
2629
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002630 if (N->getOperand(0).isUndef())
2631 return DAG.getUNDEF(MVT::i1);
2632
Matt Arsenaultf2290332015-01-06 23:00:39 +00002633 return SDValue();
2634}
2635
Matt Arsenault9cd90712016-04-14 01:42:16 +00002636// Constant fold canonicalize.
2637SDValue SITargetLowering::performFCanonicalizeCombine(
2638 SDNode *N,
2639 DAGCombinerInfo &DCI) const {
2640 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
2641 if (!CFP)
2642 return SDValue();
2643
2644 SelectionDAG &DAG = DCI.DAG;
2645 const APFloat &C = CFP->getValueAPF();
2646
2647 // Flush denormals to 0 if not enabled.
2648 if (C.isDenormal()) {
2649 EVT VT = N->getValueType(0);
2650 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
2651 return DAG.getConstantFP(0.0, SDLoc(N), VT);
2652
2653 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
2654 return DAG.getConstantFP(0.0, SDLoc(N), VT);
2655 }
2656
2657 if (C.isNaN()) {
2658 EVT VT = N->getValueType(0);
2659 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
2660 if (C.isSignaling()) {
2661 // Quiet a signaling NaN.
2662 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
2663 }
2664
2665 // Make sure it is the canonical NaN bitpattern.
2666 //
2667 // TODO: Can we use -1 as the canonical NaN value since it's an inline
2668 // immediate?
2669 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
2670 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
2671 }
2672
2673 return SDValue(CFP, 0);
2674}
2675
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002676static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
2677 switch (Opc) {
2678 case ISD::FMAXNUM:
2679 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002680 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002681 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002682 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002683 return AMDGPUISD::UMAX3;
2684 case ISD::FMINNUM:
2685 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002686 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002687 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002688 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002689 return AMDGPUISD::UMIN3;
2690 default:
2691 llvm_unreachable("Not a min/max opcode");
2692 }
2693}
2694
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002695static SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
2696 SDValue Op0, SDValue Op1, bool Signed) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00002697 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
2698 if (!K1)
2699 return SDValue();
2700
2701 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
2702 if (!K0)
2703 return SDValue();
2704
Matt Arsenaultf639c322016-01-28 20:53:42 +00002705 if (Signed) {
2706 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
2707 return SDValue();
2708 } else {
2709 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
2710 return SDValue();
2711 }
2712
2713 EVT VT = K0->getValueType(0);
2714 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
2715 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
2716}
2717
2718static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
2719 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
2720 return true;
2721
2722 return DAG.isKnownNeverNaN(Op);
2723}
2724
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002725static SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
2726 SDValue Op0, SDValue Op1) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00002727 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
2728 if (!K1)
2729 return SDValue();
2730
2731 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
2732 if (!K0)
2733 return SDValue();
2734
2735 // Ordered >= (although NaN inputs should have folded away by now).
2736 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
2737 if (Cmp == APFloat::cmpGreaterThan)
2738 return SDValue();
2739
2740 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
2741 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
2742 // give the other result, which is different from med3 with a NaN input.
2743 SDValue Var = Op0.getOperand(0);
2744 if (!isKnownNeverSNan(DAG, Var))
2745 return SDValue();
2746
2747 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
2748 Var, SDValue(K0, 0), SDValue(K1, 0));
2749}
2750
2751SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
2752 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002753 SelectionDAG &DAG = DCI.DAG;
2754
2755 unsigned Opc = N->getOpcode();
2756 SDValue Op0 = N->getOperand(0);
2757 SDValue Op1 = N->getOperand(1);
2758
2759 // Only do this if the inner op has one use since this will just increases
2760 // register pressure for no benefit.
2761
Matt Arsenault5b39b342016-01-28 20:53:48 +00002762 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
2763 // max(max(a, b), c) -> max3(a, b, c)
2764 // min(min(a, b), c) -> min3(a, b, c)
2765 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
2766 SDLoc DL(N);
2767 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2768 DL,
2769 N->getValueType(0),
2770 Op0.getOperand(0),
2771 Op0.getOperand(1),
2772 Op1);
2773 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002774
Matt Arsenault5b39b342016-01-28 20:53:48 +00002775 // Try commuted.
2776 // max(a, max(b, c)) -> max3(a, b, c)
2777 // min(a, min(b, c)) -> min3(a, b, c)
2778 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
2779 SDLoc DL(N);
2780 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2781 DL,
2782 N->getValueType(0),
2783 Op0,
2784 Op1.getOperand(0),
2785 Op1.getOperand(1));
2786 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002787 }
2788
Matt Arsenaultf639c322016-01-28 20:53:42 +00002789 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
2790 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
2791 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
2792 return Med3;
2793 }
2794
2795 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
2796 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
2797 return Med3;
2798 }
2799
2800 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00002801 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
2802 (Opc == AMDGPUISD::FMIN_LEGACY &&
2803 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00002804 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
2805 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
2806 return Res;
2807 }
2808
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002809 return SDValue();
2810}
2811
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002812SDValue SITargetLowering::performSetCCCombine(SDNode *N,
2813 DAGCombinerInfo &DCI) const {
2814 SelectionDAG &DAG = DCI.DAG;
2815 SDLoc SL(N);
2816
2817 SDValue LHS = N->getOperand(0);
2818 SDValue RHS = N->getOperand(1);
2819 EVT VT = LHS.getValueType();
2820
2821 if (VT != MVT::f32 && VT != MVT::f64)
2822 return SDValue();
2823
2824 // Match isinf pattern
2825 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
2826 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
2827 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
2828 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2829 if (!CRHS)
2830 return SDValue();
2831
2832 const APFloat &APF = CRHS->getValueAPF();
2833 if (APF.isInfinity() && !APF.isNegative()) {
2834 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002835 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
2836 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002837 }
2838 }
2839
2840 return SDValue();
2841}
2842
Tom Stellard75aadc22012-12-11 21:25:42 +00002843SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
2844 DAGCombinerInfo &DCI) const {
2845 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002846 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00002847
2848 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00002849 default:
2850 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002851 case ISD::SETCC:
2852 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00002853 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002854 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002855 case ISD::SMAX:
2856 case ISD::SMIN:
2857 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00002858 case ISD::UMIN:
2859 case AMDGPUISD::FMIN_LEGACY:
2860 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002861 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00002862 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002863 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00002864 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002865 break;
2866 }
Matt Arsenault364a6742014-06-11 17:50:44 +00002867
2868 case AMDGPUISD::CVT_F32_UBYTE0:
2869 case AMDGPUISD::CVT_F32_UBYTE1:
2870 case AMDGPUISD::CVT_F32_UBYTE2:
2871 case AMDGPUISD::CVT_F32_UBYTE3: {
2872 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
Matt Arsenault364a6742014-06-11 17:50:44 +00002873 SDValue Src = N->getOperand(0);
Matt Arsenaulta949dc62016-05-09 16:29:50 +00002874
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002875 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero.
Matt Arsenaulta949dc62016-05-09 16:29:50 +00002876 if (Src.getOpcode() == ISD::SRL) {
2877 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
2878 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
2879 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x
2880
2881 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(1))) {
2882 unsigned SrcOffset = C->getZExtValue() + 8 * Offset;
2883 if (SrcOffset < 32 && SrcOffset % 8 == 0) {
2884 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, DL,
2885 MVT::f32, Src.getOperand(0));
2886 }
2887 }
2888 }
2889
Matt Arsenault364a6742014-06-11 17:50:44 +00002890 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
2891
2892 APInt KnownZero, KnownOne;
2893 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
2894 !DCI.isBeforeLegalizeOps());
2895 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2896 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
2897 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
2898 DCI.CommitTargetLoweringOpt(TLO);
2899 }
2900
2901 break;
2902 }
2903
2904 case ISD::UINT_TO_FP: {
2905 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002906 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002907 case ISD::FADD: {
2908 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2909 break;
2910
2911 EVT VT = N->getValueType(0);
2912 if (VT != MVT::f32)
2913 break;
2914
Matt Arsenault8d630032015-02-20 22:10:41 +00002915 // Only do this if we are not trying to support denormals. v_mad_f32 does
2916 // not support denormals ever.
2917 if (Subtarget->hasFP32Denormals())
2918 break;
2919
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002920 SDValue LHS = N->getOperand(0);
2921 SDValue RHS = N->getOperand(1);
2922
2923 // These should really be instruction patterns, but writing patterns with
2924 // source modiifiers is a pain.
2925
2926 // fadd (fadd (a, a), b) -> mad 2.0, a, b
2927 if (LHS.getOpcode() == ISD::FADD) {
2928 SDValue A = LHS.getOperand(0);
2929 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002930 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002931 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002932 }
2933 }
2934
2935 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
2936 if (RHS.getOpcode() == ISD::FADD) {
2937 SDValue A = RHS.getOperand(0);
2938 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002939 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002940 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002941 }
2942 }
2943
Matt Arsenault8d630032015-02-20 22:10:41 +00002944 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002945 }
Matt Arsenault8675db12014-08-29 16:01:14 +00002946 case ISD::FSUB: {
2947 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2948 break;
2949
2950 EVT VT = N->getValueType(0);
2951
2952 // Try to get the fneg to fold into the source modifier. This undoes generic
2953 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00002954 //
2955 // Only do this if we are not trying to support denormals. v_mad_f32 does
2956 // not support denormals ever.
2957 if (VT == MVT::f32 &&
2958 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00002959 SDValue LHS = N->getOperand(0);
2960 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002961 if (LHS.getOpcode() == ISD::FADD) {
2962 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
2963
2964 SDValue A = LHS.getOperand(0);
2965 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002966 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002967 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
2968
Matt Arsenault8d630032015-02-20 22:10:41 +00002969 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002970 }
2971 }
2972
2973 if (RHS.getOpcode() == ISD::FADD) {
2974 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
2975
2976 SDValue A = RHS.getOperand(0);
2977 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002978 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002979 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002980 }
2981 }
Matt Arsenault8d630032015-02-20 22:10:41 +00002982
2983 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00002984 }
2985
2986 break;
2987 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002988 case ISD::LOAD:
2989 case ISD::STORE:
2990 case ISD::ATOMIC_LOAD:
2991 case ISD::ATOMIC_STORE:
2992 case ISD::ATOMIC_CMP_SWAP:
2993 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
2994 case ISD::ATOMIC_SWAP:
2995 case ISD::ATOMIC_LOAD_ADD:
2996 case ISD::ATOMIC_LOAD_SUB:
2997 case ISD::ATOMIC_LOAD_AND:
2998 case ISD::ATOMIC_LOAD_OR:
2999 case ISD::ATOMIC_LOAD_XOR:
3000 case ISD::ATOMIC_LOAD_NAND:
3001 case ISD::ATOMIC_LOAD_MIN:
3002 case ISD::ATOMIC_LOAD_MAX:
3003 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00003004 case ISD::ATOMIC_LOAD_UMAX:
3005 case AMDGPUISD::ATOMIC_INC:
3006 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003007 if (DCI.isBeforeLegalize())
3008 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003009
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003010 MemSDNode *MemNode = cast<MemSDNode>(N);
3011 SDValue Ptr = MemNode->getBasePtr();
3012
3013 // TODO: We could also do this for multiplies.
3014 unsigned AS = MemNode->getAddressSpace();
3015 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
3016 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
3017 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00003018 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003019
3020 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
3021 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
3022 }
3023 }
3024 break;
3025 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003026 case ISD::AND:
3027 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003028 case ISD::OR:
3029 return performOrCombine(N, DCI);
3030 case AMDGPUISD::FP_CLASS:
3031 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00003032 case ISD::FCANONICALIZE:
3033 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003034 case AMDGPUISD::FRACT:
3035 case AMDGPUISD::RCP:
3036 case AMDGPUISD::RSQ:
3037 case AMDGPUISD::RSQ_LEGACY:
3038 case AMDGPUISD::RSQ_CLAMP:
3039 case AMDGPUISD::LDEXP: {
3040 SDValue Src = N->getOperand(0);
3041 if (Src.isUndef())
3042 return Src;
3043 break;
3044 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003045 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003046 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00003047}
Christian Konigd910b7d2013-02-26 17:52:16 +00003048
Christian Konigf82901a2013-02-26 17:52:23 +00003049/// \brief Analyze the possible immediate value Op
3050///
3051/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
3052/// and the immediate value if it's a literal immediate
3053int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003054 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Christian Konigf82901a2013-02-26 17:52:23 +00003055
Tom Stellardedbf1eb2013-04-05 23:31:20 +00003056 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00003057 if (TII->isInlineConstant(Node->getAPIntValue()))
3058 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00003059
Matt Arsenault11a4d672015-02-13 19:05:03 +00003060 uint64_t Val = Node->getZExtValue();
3061 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00003062 }
3063
3064 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
3065 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
3066 return 0;
3067
3068 if (Node->getValueType(0) == MVT::f32)
3069 return FloatToBits(Node->getValueAPF().convertToFloat());
3070
3071 return -1;
3072 }
3073
3074 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00003075}
3076
Christian Konig8e06e2a2013-04-10 08:39:08 +00003077/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00003078static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00003079 switch (Idx) {
3080 default: return 0;
3081 case AMDGPU::sub0: return 0;
3082 case AMDGPU::sub1: return 1;
3083 case AMDGPU::sub2: return 2;
3084 case AMDGPU::sub3: return 3;
3085 }
3086}
3087
3088/// \brief Adjust the writemask of MIMG instructions
3089void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
3090 SelectionDAG &DAG) const {
3091 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00003092 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003093 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
3094 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00003095 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003096
3097 // Try to figure out the used register components
3098 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
3099 I != E; ++I) {
3100
3101 // Abort if we can't understand the usage
3102 if (!I->isMachineOpcode() ||
3103 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
3104 return;
3105
Tom Stellard54774e52013-10-23 02:53:47 +00003106 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
3107 // Note that subregs are packed, i.e. Lane==0 is the first bit set
3108 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
3109 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00003110 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00003111
Tom Stellard54774e52013-10-23 02:53:47 +00003112 // Set which texture component corresponds to the lane.
3113 unsigned Comp;
3114 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
3115 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00003116 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00003117 Dmask &= ~(1 << Comp);
3118 }
3119
Christian Konig8e06e2a2013-04-10 08:39:08 +00003120 // Abort if we have more than one user per component
3121 if (Users[Lane])
3122 return;
3123
3124 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00003125 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003126 }
3127
Tom Stellard54774e52013-10-23 02:53:47 +00003128 // Abort if there's no change
3129 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00003130 return;
3131
3132 // Adjust the writemask in the node
3133 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003134 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003135 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003136 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00003137 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003138
Christian Konig8b1ed282013-04-10 08:39:16 +00003139 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00003140 // (if NewDmask has only one bit set...)
3141 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003142 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
3143 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00003144 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003145 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00003146 SDValue(Node, 0), RC);
3147 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
3148 return;
3149 }
3150
Christian Konig8e06e2a2013-04-10 08:39:08 +00003151 // Update the users of the node with the new indices
3152 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
3153
3154 SDNode *User = Users[i];
3155 if (!User)
3156 continue;
3157
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003158 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003159 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
3160
3161 switch (Idx) {
3162 default: break;
3163 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
3164 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
3165 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
3166 }
3167 }
3168}
3169
Tom Stellardc98ee202015-07-16 19:40:07 +00003170static bool isFrameIndexOp(SDValue Op) {
3171 if (Op.getOpcode() == ISD::AssertZext)
3172 Op = Op.getOperand(0);
3173
3174 return isa<FrameIndexSDNode>(Op);
3175}
3176
Tom Stellard3457a842014-10-09 19:06:00 +00003177/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
3178/// with frame index operands.
3179/// LLVM assumes that inputs are to these instructions are registers.
3180void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
3181 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003182
3183 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00003184 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00003185 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00003186 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003187 continue;
3188 }
3189
Tom Stellard3457a842014-10-09 19:06:00 +00003190 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003191 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00003192 Node->getOperand(i).getValueType(),
3193 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003194 }
3195
Tom Stellard3457a842014-10-09 19:06:00 +00003196 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003197}
3198
Matt Arsenault08d84942014-06-03 23:06:13 +00003199/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00003200SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
3201 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003202 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003203 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00003204
Nicolai Haehnlec06bfa12016-07-11 21:59:43 +00003205 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() &&
3206 !TII->isGather4(Opcode))
Christian Konig8e06e2a2013-04-10 08:39:08 +00003207 adjustWritemask(Node, DAG);
3208
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003209 if (Opcode == AMDGPU::INSERT_SUBREG ||
3210 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003211 legalizeTargetIndependentNode(Node, DAG);
3212 return Node;
3213 }
Tom Stellard654d6692015-01-08 15:08:17 +00003214 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003215}
Christian Konig8b1ed282013-04-10 08:39:16 +00003216
3217/// \brief Assign the register class depending on the number of
3218/// bits set in the writemask
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003219void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
Christian Konig8b1ed282013-04-10 08:39:16 +00003220 SDNode *Node) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003221 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003222
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003223 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003224
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003225 if (TII->isVOP3(MI.getOpcode())) {
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003226 // Make sure constant bus requirements are respected.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003227 TII->legalizeOperandsVOP3(MRI, MI);
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003228 return;
3229 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00003230
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003231 if (TII->isMIMG(MI)) {
3232 unsigned VReg = MI.getOperand(0).getReg();
3233 unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4;
3234 unsigned Writemask = MI.getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003235 unsigned BitsSet = 0;
3236 for (unsigned i = 0; i < 4; ++i)
3237 BitsSet += Writemask & (1 << i) ? 1 : 0;
3238
3239 const TargetRegisterClass *RC;
3240 switch (BitsSet) {
3241 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00003242 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003243 case 2: RC = &AMDGPU::VReg_64RegClass; break;
3244 case 3: RC = &AMDGPU::VReg_96RegClass; break;
3245 }
3246
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003247 unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet);
3248 MI.setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003249 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00003250 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00003251 }
3252
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003253 // Replace unused atomics with the no return version.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003254 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003255 if (NoRetAtomicOp != -1) {
3256 if (!Node->hasAnyUseOfValue(0)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003257 MI.setDesc(TII->get(NoRetAtomicOp));
3258 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003259 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003260 }
3261
Tom Stellard354a43c2016-04-01 18:27:37 +00003262 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
3263 // instruction, because the return type of these instructions is a vec2 of
3264 // the memory type, so it can be tied to the input operand.
3265 // This means these instructions always have a use, so we need to add a
3266 // special case to check if the atomic has only one extract_subreg use,
3267 // which itself has no uses.
3268 if ((Node->hasNUsesOfValue(1, 0) &&
Nicolai Haehnle750082d2016-04-15 14:42:36 +00003269 Node->use_begin()->isMachineOpcode() &&
Tom Stellard354a43c2016-04-01 18:27:37 +00003270 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
3271 !Node->use_begin()->hasAnyUseOfValue(0))) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003272 unsigned Def = MI.getOperand(0).getReg();
Tom Stellard354a43c2016-04-01 18:27:37 +00003273
3274 // Change this into a noret atomic.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003275 MI.setDesc(TII->get(NoRetAtomicOp));
3276 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003277
3278 // If we only remove the def operand from the atomic instruction, the
3279 // extract_subreg will be left with a use of a vreg without a def.
3280 // So we need to insert an implicit_def to avoid machine verifier
3281 // errors.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003282 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
Tom Stellard354a43c2016-04-01 18:27:37 +00003283 TII->get(AMDGPU::IMPLICIT_DEF), Def);
3284 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003285 return;
3286 }
Christian Konig8b1ed282013-04-10 08:39:16 +00003287}
Tom Stellard0518ff82013-06-03 17:39:58 +00003288
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003289static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL,
3290 uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003291 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00003292 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
3293}
3294
3295MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003296 const SDLoc &DL,
Matt Arsenault485defe2014-11-05 19:01:17 +00003297 SDValue Ptr) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003298 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault485defe2014-11-05 19:01:17 +00003299
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003300 // Build the half of the subregister with the constants before building the
3301 // full 128-bit register. If we are building multiple resource descriptors,
3302 // this will allow CSEing of the 2-component register.
3303 const SDValue Ops0[] = {
3304 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
3305 buildSMovImm32(DAG, DL, 0),
3306 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
3307 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
3308 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
3309 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003310
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003311 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
3312 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00003313
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003314 // Combine the constants and the pointer.
3315 const SDValue Ops1[] = {
3316 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
3317 Ptr,
3318 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
3319 SubRegHi,
3320 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
3321 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003322
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003323 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00003324}
3325
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003326/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003327/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
3328/// of the resource descriptor) to create an offset, which is added to
3329/// the resource pointer.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003330MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL,
3331 SDValue Ptr, uint32_t RsrcDword1,
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003332 uint64_t RsrcDword2And3) const {
3333 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
3334 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
3335 if (RsrcDword1) {
3336 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003337 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
3338 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003339 }
3340
3341 SDValue DataLo = buildSMovImm32(DAG, DL,
3342 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
3343 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
3344
3345 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003346 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003347 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003348 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003349 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003350 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003351 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003352 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003353 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003354 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003355 };
3356
3357 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
3358}
3359
Tom Stellard94593ee2013-06-03 17:40:18 +00003360SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3361 const TargetRegisterClass *RC,
3362 unsigned Reg, EVT VT) const {
3363 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
3364
3365 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
3366 cast<RegisterSDNode>(VReg)->getReg(), VT);
3367}
Tom Stellardd7e6f132015-04-08 01:09:26 +00003368
3369//===----------------------------------------------------------------------===//
3370// SI Inline Assembly Support
3371//===----------------------------------------------------------------------===//
3372
3373std::pair<unsigned, const TargetRegisterClass *>
3374SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003375 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00003376 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003377
3378 if (Constraint.size() == 1) {
3379 switch (Constraint[0]) {
3380 case 's':
3381 case 'r':
3382 switch (VT.getSizeInBits()) {
3383 default:
3384 return std::make_pair(0U, nullptr);
3385 case 32:
Tom Stellardd7e6f132015-04-08 01:09:26 +00003386 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003387 case 64:
3388 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
3389 case 128:
3390 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
3391 case 256:
3392 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
3393 }
3394
3395 case 'v':
3396 switch (VT.getSizeInBits()) {
3397 default:
3398 return std::make_pair(0U, nullptr);
3399 case 32:
3400 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
3401 case 64:
3402 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
3403 case 96:
3404 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
3405 case 128:
3406 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
3407 case 256:
3408 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
3409 case 512:
3410 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
3411 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00003412 }
3413 }
3414
3415 if (Constraint.size() > 1) {
3416 const TargetRegisterClass *RC = nullptr;
3417 if (Constraint[1] == 'v') {
3418 RC = &AMDGPU::VGPR_32RegClass;
3419 } else if (Constraint[1] == 's') {
3420 RC = &AMDGPU::SGPR_32RegClass;
3421 }
3422
3423 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00003424 uint32_t Idx;
3425 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
3426 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00003427 return std::make_pair(RC->getRegister(Idx), RC);
3428 }
3429 }
3430 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
3431}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003432
3433SITargetLowering::ConstraintType
3434SITargetLowering::getConstraintType(StringRef Constraint) const {
3435 if (Constraint.size() == 1) {
3436 switch (Constraint[0]) {
3437 default: break;
3438 case 's':
3439 case 'v':
3440 return C_RegisterClass;
3441 }
3442 }
3443 return TargetLowering::getConstraintType(Constraint);
3444}