blob: 033b9408d2c345b138087a876dbe05844dc3d2fc [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 Arsenaultc96e1de2016-07-18 18:35:05 +00001899 case Intrinsic::amdgcn_sffbh:
1900 case AMDGPUIntrinsic::AMDGPU_flbit_i32: // Legacy name.
1901 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001902 default:
1903 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1904 }
1905}
1906
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001907SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
1908 SelectionDAG &DAG) const {
1909 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1910 switch (IntrID) {
1911 case Intrinsic::amdgcn_atomic_inc:
1912 case Intrinsic::amdgcn_atomic_dec: {
1913 MemSDNode *M = cast<MemSDNode>(Op);
1914 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
1915 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
1916 SDValue Ops[] = {
1917 M->getOperand(0), // Chain
1918 M->getOperand(2), // Ptr
1919 M->getOperand(3) // Value
1920 };
1921
1922 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
1923 M->getMemoryVT(), M->getMemOperand());
1924 }
1925 default:
1926 return SDValue();
1927 }
1928}
1929
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001930SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1931 SelectionDAG &DAG) const {
1932 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00001933 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001934 SDValue Chain = Op.getOperand(0);
1935 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1936
1937 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00001938 case AMDGPUIntrinsic::SI_sendmsg: {
1939 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
1940 SDValue Glue = Chain.getValue(1);
1941 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
1942 Op.getOperand(2), Glue);
1943 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001944 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001945 SDValue Ops[] = {
1946 Chain,
1947 Op.getOperand(2),
1948 Op.getOperand(3),
1949 Op.getOperand(4),
1950 Op.getOperand(5),
1951 Op.getOperand(6),
1952 Op.getOperand(7),
1953 Op.getOperand(8),
1954 Op.getOperand(9),
1955 Op.getOperand(10),
1956 Op.getOperand(11),
1957 Op.getOperand(12),
1958 Op.getOperand(13),
1959 Op.getOperand(14)
1960 };
1961
1962 EVT VT = Op.getOperand(3).getValueType();
1963
1964 MachineMemOperand *MMO = MF.getMachineMemOperand(
1965 MachinePointerInfo(),
1966 MachineMemOperand::MOStore,
1967 VT.getStoreSize(), 4);
1968 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1969 Op->getVTList(), Ops, VT, MMO);
1970 }
Matt Arsenault00568682016-07-13 06:04:22 +00001971 case AMDGPUIntrinsic::AMDGPU_kill: {
1972 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Op.getOperand(2))) {
1973 if (!K->isNegative())
1974 return Chain;
1975 }
1976
1977 return Op;
1978 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001979 default:
1980 return SDValue();
1981 }
1982}
1983
Tom Stellard81d871d2013-11-13 23:36:50 +00001984SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1985 SDLoc DL(Op);
1986 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001987 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00001988 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00001989
Matt Arsenaulta1436412016-02-10 18:21:45 +00001990 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
1991 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected");
Matt Arsenault6dfda962016-02-10 18:21:39 +00001992 // FIXME: Copied from PPC
1993 // First, load into 32 bits, then truncate to 1 bit.
1994
1995 SDValue Chain = Load->getChain();
1996 SDValue BasePtr = Load->getBasePtr();
1997 MachineMemOperand *MMO = Load->getMemOperand();
1998
1999 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
2000 BasePtr, MVT::i8, MMO);
2001
2002 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002003 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00002004 NewLD.getValue(1)
2005 };
2006
2007 return DAG.getMergeValues(Ops, DL);
2008 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002009
Matt Arsenaulta1436412016-02-10 18:21:45 +00002010 if (!MemVT.isVector())
2011 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002012
Matt Arsenaulta1436412016-02-10 18:21:45 +00002013 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
2014 "Custom lowering for non-i32 vectors hasn't been implemented.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002015
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002016 unsigned AS = Load->getAddressSpace();
2017 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT,
2018 AS, Load->getAlignment())) {
2019 SDValue Ops[2];
2020 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG);
2021 return DAG.getMergeValues(Ops, DL);
2022 }
2023
2024 unsigned NumElements = MemVT.getVectorNumElements();
2025 switch (AS) {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002026 case AMDGPUAS::CONSTANT_ADDRESS:
2027 if (isMemOpUniform(Load))
2028 return SDValue();
2029 // Non-uniform loads will be selected to MUBUF instructions, so they
2030 // have the same legalization requires ments as global and private
2031 // loads.
2032 //
2033 // Fall-through
2034 case AMDGPUAS::GLOBAL_ADDRESS:
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002035 case AMDGPUAS::FLAT_ADDRESS:
2036 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00002037 return SplitVectorLoad(Op, DAG);
2038 // v4 loads are supported for private and global memory.
2039 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002040 case AMDGPUAS::PRIVATE_ADDRESS: {
2041 // Depending on the setting of the private_element_size field in the
2042 // resource descriptor, we can only make private accesses up to a certain
2043 // size.
2044 switch (Subtarget->getMaxPrivateElementSize()) {
2045 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002046 return scalarizeVectorLoad(Load, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002047 case 8:
2048 if (NumElements > 2)
2049 return SplitVectorLoad(Op, DAG);
2050 return SDValue();
2051 case 16:
2052 // Same as global/flat
2053 if (NumElements > 4)
2054 return SplitVectorLoad(Op, DAG);
2055 return SDValue();
2056 default:
2057 llvm_unreachable("unsupported private_element_size");
2058 }
2059 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002060 case AMDGPUAS::LOCAL_ADDRESS: {
2061 if (NumElements > 2)
2062 return SplitVectorLoad(Op, DAG);
2063
2064 if (NumElements == 2)
2065 return SDValue();
2066
Matt Arsenaulta1436412016-02-10 18:21:45 +00002067 // If properly aligned, if we split we might be able to use ds_read_b64.
2068 return SplitVectorLoad(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002069 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00002070 default:
2071 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00002072 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002073}
2074
Tom Stellard0ec134f2014-02-04 17:18:40 +00002075SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2076 if (Op.getValueType() != MVT::i64)
2077 return SDValue();
2078
2079 SDLoc DL(Op);
2080 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002081
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002082 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2083 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002084
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002085 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
2086 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
2087
2088 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
2089 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002090
2091 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
2092
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002093 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
2094 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002095
2096 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
2097
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002098 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002099 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002100}
2101
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002102// Catch division cases where we can use shortcuts with rcp and rsq
2103// instructions.
2104SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002105 SDLoc SL(Op);
2106 SDValue LHS = Op.getOperand(0);
2107 SDValue RHS = Op.getOperand(1);
2108 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002109 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002110
2111 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002112 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
2113 CLHS->isExactlyValue(1.0)) {
2114 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
2115 // the CI documentation has a worst case error of 1 ulp.
2116 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
2117 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002118
2119 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002120 //
2121 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
2122 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002123 if (RHS.getOpcode() == ISD::FSQRT)
2124 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
2125
2126 // 1.0 / x -> rcp(x)
2127 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
2128 }
2129 }
2130
Wei Dinged0f97f2016-06-09 19:17:15 +00002131 const SDNodeFlags *Flags = Op->getFlags();
2132
2133 if (Unsafe || Flags->hasAllowReciprocal()) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002134 // Turn into multiply by the reciprocal.
2135 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00002136 SDNodeFlags Flags;
2137 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002138 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00002139 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002140 }
2141
2142 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002143}
2144
2145SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher538d09d02016-06-07 20:27:12 +00002146 if (SDValue FastLowered = LowerFastFDIV(Op, DAG))
2147 return FastLowered;
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002148
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002149 SDLoc SL(Op);
2150 SDValue LHS = Op.getOperand(0);
2151 SDValue RHS = Op.getOperand(1);
2152
Wei Dinged0f97f2016-06-09 19:17:15 +00002153 // faster 2.5 ulp fdiv when using -amdgpu-fast-fdiv flag
2154 if (EnableAMDGPUFastFDIV) {
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002155 // This does not support denormals.
Wei Dinged0f97f2016-06-09 19:17:15 +00002156 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002157
Wei Dinged0f97f2016-06-09 19:17:15 +00002158 const APFloat K0Val(BitsToFloat(0x6f800000));
2159 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002160
Wei Dinged0f97f2016-06-09 19:17:15 +00002161 const APFloat K1Val(BitsToFloat(0x2f800000));
2162 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002163
Wei Dinged0f97f2016-06-09 19:17:15 +00002164 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
2165
2166 EVT SetCCVT =
2167 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
2168
2169 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
2170
2171 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
2172
2173 // TODO: Should this propagate fast-math-flags?
2174
2175 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
2176
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002177 // rcp does not support denormals.
Wei Dinged0f97f2016-06-09 19:17:15 +00002178 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
2179
2180 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
2181
2182 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002183 }
2184
Wei Dinged0f97f2016-06-09 19:17:15 +00002185 // Generates more precise fpdiv32.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002186 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002187
Wei Dinged0f97f2016-06-09 19:17:15 +00002188 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002189
Wei Dinged0f97f2016-06-09 19:17:15 +00002190 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, RHS, RHS, LHS);
2191 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, LHS, RHS, LHS);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002192
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002193 // Denominator is scaled to not be denormal, so using rcp is ok.
Wei Dinged0f97f2016-06-09 19:17:15 +00002194 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002195
Wei Dinged0f97f2016-06-09 19:17:15 +00002196 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002197
Wei Dinged0f97f2016-06-09 19:17:15 +00002198 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, ApproxRcp, One);
2199 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, ApproxRcp);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002200
Wei Dinged0f97f2016-06-09 19:17:15 +00002201 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, NumeratorScaled, Fma1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002202
Wei Dinged0f97f2016-06-09 19:17:15 +00002203 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, NumeratorScaled);
2204 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul);
2205 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, NumeratorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002206
Wei Dinged0f97f2016-06-09 19:17:15 +00002207 SDValue Scale = NumeratorScaled.getValue(1);
2208 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, Fma4, Fma1, Fma3, Scale);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002209
Wei Dinged0f97f2016-06-09 19:17:15 +00002210 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002211}
2212
2213SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002214 if (DAG.getTarget().Options.UnsafeFPMath)
2215 return LowerFastFDIV(Op, DAG);
2216
2217 SDLoc SL(Op);
2218 SDValue X = Op.getOperand(0);
2219 SDValue Y = Op.getOperand(1);
2220
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002221 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002222
2223 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
2224
2225 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
2226
2227 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
2228
2229 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
2230
2231 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
2232
2233 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
2234
2235 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
2236
2237 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
2238
2239 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
2240 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
2241
2242 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
2243 NegDivScale0, Mul, DivScale1);
2244
2245 SDValue Scale;
2246
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002247 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002248 // Workaround a hardware bug on SI where the condition output from div_scale
2249 // is not usable.
2250
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002251 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002252
2253 // Figure out if the scale to use for div_fmas.
2254 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
2255 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
2256 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
2257 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
2258
2259 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
2260 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
2261
2262 SDValue Scale0Hi
2263 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
2264 SDValue Scale1Hi
2265 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
2266
2267 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
2268 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
2269 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
2270 } else {
2271 Scale = DivScale1.getValue(1);
2272 }
2273
2274 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
2275 Fma4, Fma3, Mul, Scale);
2276
2277 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002278}
2279
2280SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
2281 EVT VT = Op.getValueType();
2282
2283 if (VT == MVT::f32)
2284 return LowerFDIV32(Op, DAG);
2285
2286 if (VT == MVT::f64)
2287 return LowerFDIV64(Op, DAG);
2288
2289 llvm_unreachable("Unexpected type for fdiv");
2290}
2291
Tom Stellard81d871d2013-11-13 23:36:50 +00002292SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2293 SDLoc DL(Op);
2294 StoreSDNode *Store = cast<StoreSDNode>(Op);
2295 EVT VT = Store->getMemoryVT();
2296
Matt Arsenault95245662016-02-11 05:32:46 +00002297 if (VT == MVT::i1) {
2298 return DAG.getTruncStore(Store->getChain(), DL,
2299 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
2300 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00002301 }
2302
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002303 assert(VT.isVector() &&
2304 Store->getValue().getValueType().getScalarType() == MVT::i32);
2305
2306 unsigned AS = Store->getAddressSpace();
2307 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
2308 AS, Store->getAlignment())) {
2309 return expandUnalignedStore(Store, DAG);
2310 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002311
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002312 unsigned NumElements = VT.getVectorNumElements();
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002313 switch (AS) {
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002314 case AMDGPUAS::GLOBAL_ADDRESS:
2315 case AMDGPUAS::FLAT_ADDRESS:
2316 if (NumElements > 4)
2317 return SplitVectorStore(Op, DAG);
2318 return SDValue();
2319 case AMDGPUAS::PRIVATE_ADDRESS: {
2320 switch (Subtarget->getMaxPrivateElementSize()) {
2321 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002322 return scalarizeVectorStore(Store, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002323 case 8:
2324 if (NumElements > 2)
2325 return SplitVectorStore(Op, DAG);
2326 return SDValue();
2327 case 16:
2328 if (NumElements > 4)
2329 return SplitVectorStore(Op, DAG);
2330 return SDValue();
2331 default:
2332 llvm_unreachable("unsupported private_element_size");
2333 }
2334 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002335 case AMDGPUAS::LOCAL_ADDRESS: {
2336 if (NumElements > 2)
2337 return SplitVectorStore(Op, DAG);
2338
2339 if (NumElements == 2)
2340 return Op;
2341
Matt Arsenault95245662016-02-11 05:32:46 +00002342 // If properly aligned, if we split we might be able to use ds_write_b64.
2343 return SplitVectorStore(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002344 }
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002345 default:
2346 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00002347 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002348}
2349
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002350SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002351 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002352 EVT VT = Op.getValueType();
2353 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00002354 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002355 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
2356 DAG.getNode(ISD::FMUL, DL, VT, Arg,
2357 DAG.getConstantFP(0.5/M_PI, DL,
2358 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002359
2360 switch (Op.getOpcode()) {
2361 case ISD::FCOS:
2362 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
2363 case ISD::FSIN:
2364 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
2365 default:
2366 llvm_unreachable("Wrong trig opcode");
2367 }
2368}
2369
Tom Stellard354a43c2016-04-01 18:27:37 +00002370SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
2371 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
2372 assert(AtomicNode->isCompareAndSwap());
2373 unsigned AS = AtomicNode->getAddressSpace();
2374
2375 // No custom lowering required for local address space
2376 if (!isFlatGlobalAddrSpace(AS))
2377 return Op;
2378
2379 // Non-local address space requires custom lowering for atomic compare
2380 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
2381 SDLoc DL(Op);
2382 SDValue ChainIn = Op.getOperand(0);
2383 SDValue Addr = Op.getOperand(1);
2384 SDValue Old = Op.getOperand(2);
2385 SDValue New = Op.getOperand(3);
2386 EVT VT = Op.getValueType();
2387 MVT SimpleVT = VT.getSimpleVT();
2388 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
2389
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002390 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
Tom Stellard354a43c2016-04-01 18:27:37 +00002391 SDValue Ops[] = { ChainIn, Addr, NewOld };
Matt Arsenault88701812016-06-09 23:42:48 +00002392
2393 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(),
2394 Ops, VT, AtomicNode->getMemOperand());
Tom Stellard354a43c2016-04-01 18:27:37 +00002395}
2396
Tom Stellard75aadc22012-12-11 21:25:42 +00002397//===----------------------------------------------------------------------===//
2398// Custom DAG optimizations
2399//===----------------------------------------------------------------------===//
2400
Matt Arsenault364a6742014-06-11 17:50:44 +00002401SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00002402 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00002403 EVT VT = N->getValueType(0);
2404 EVT ScalarVT = VT.getScalarType();
2405 if (ScalarVT != MVT::f32)
2406 return SDValue();
2407
2408 SelectionDAG &DAG = DCI.DAG;
2409 SDLoc DL(N);
2410
2411 SDValue Src = N->getOperand(0);
2412 EVT SrcVT = Src.getValueType();
2413
2414 // TODO: We could try to match extracting the higher bytes, which would be
2415 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
2416 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
2417 // about in practice.
2418 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
2419 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
2420 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
2421 DCI.AddToWorklist(Cvt.getNode());
2422 return Cvt;
2423 }
2424 }
2425
Matt Arsenault364a6742014-06-11 17:50:44 +00002426 return SDValue();
2427}
2428
Eric Christopher6c5b5112015-03-11 18:43:21 +00002429/// \brief Return true if the given offset Size in bytes can be folded into
2430/// the immediate offsets of a memory instruction for the given address space.
2431static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002432 const SISubtarget &STI) {
Eric Christopher6c5b5112015-03-11 18:43:21 +00002433 switch (AS) {
2434 case AMDGPUAS::GLOBAL_ADDRESS: {
2435 // MUBUF instructions a 12-bit offset in bytes.
2436 return isUInt<12>(OffsetSize);
2437 }
2438 case AMDGPUAS::CONSTANT_ADDRESS: {
2439 // SMRD instructions have an 8-bit offset in dwords on SI and
2440 // a 20-bit offset in bytes on VI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002441 if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Eric Christopher6c5b5112015-03-11 18:43:21 +00002442 return isUInt<20>(OffsetSize);
2443 else
2444 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
2445 }
2446 case AMDGPUAS::LOCAL_ADDRESS:
2447 case AMDGPUAS::REGION_ADDRESS: {
2448 // The single offset versions have a 16-bit offset in bytes.
2449 return isUInt<16>(OffsetSize);
2450 }
2451 case AMDGPUAS::PRIVATE_ADDRESS:
2452 // Indirect register addressing does not use any offsets.
2453 default:
2454 return 0;
2455 }
2456}
2457
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002458// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
2459
2460// This is a variant of
2461// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
2462//
2463// The normal DAG combiner will do this, but only if the add has one use since
2464// that would increase the number of instructions.
2465//
2466// This prevents us from seeing a constant offset that can be folded into a
2467// memory instruction's addressing mode. If we know the resulting add offset of
2468// a pointer can be folded into an addressing offset, we can replace the pointer
2469// operand with the add of new constant offset. This eliminates one of the uses,
2470// and may allow the remaining use to also be simplified.
2471//
2472SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
2473 unsigned AddrSpace,
2474 DAGCombinerInfo &DCI) const {
2475 SDValue N0 = N->getOperand(0);
2476 SDValue N1 = N->getOperand(1);
2477
2478 if (N0.getOpcode() != ISD::ADD)
2479 return SDValue();
2480
2481 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
2482 if (!CN1)
2483 return SDValue();
2484
2485 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2486 if (!CAdd)
2487 return SDValue();
2488
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002489 // If the resulting offset is too large, we can't fold it into the addressing
2490 // mode offset.
2491 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002492 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget()))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002493 return SDValue();
2494
2495 SelectionDAG &DAG = DCI.DAG;
2496 SDLoc SL(N);
2497 EVT VT = N->getValueType(0);
2498
2499 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002500 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002501
2502 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
2503}
2504
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002505SDValue SITargetLowering::performAndCombine(SDNode *N,
2506 DAGCombinerInfo &DCI) const {
2507 if (DCI.isBeforeLegalize())
2508 return SDValue();
2509
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002510 if (SDValue Base = AMDGPUTargetLowering::performAndCombine(N, DCI))
2511 return Base;
2512
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002513 SelectionDAG &DAG = DCI.DAG;
2514
2515 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
2516 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
2517 SDValue LHS = N->getOperand(0);
2518 SDValue RHS = N->getOperand(1);
2519
2520 if (LHS.getOpcode() == ISD::SETCC &&
2521 RHS.getOpcode() == ISD::SETCC) {
2522 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
2523 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
2524
2525 SDValue X = LHS.getOperand(0);
2526 SDValue Y = RHS.getOperand(0);
2527 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
2528 return SDValue();
2529
2530 if (LCC == ISD::SETO) {
2531 if (X != LHS.getOperand(1))
2532 return SDValue();
2533
2534 if (RCC == ISD::SETUNE) {
2535 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
2536 if (!C1 || !C1->isInfinity() || C1->isNegative())
2537 return SDValue();
2538
2539 const uint32_t Mask = SIInstrFlags::N_NORMAL |
2540 SIInstrFlags::N_SUBNORMAL |
2541 SIInstrFlags::N_ZERO |
2542 SIInstrFlags::P_ZERO |
2543 SIInstrFlags::P_SUBNORMAL |
2544 SIInstrFlags::P_NORMAL;
2545
2546 static_assert(((~(SIInstrFlags::S_NAN |
2547 SIInstrFlags::Q_NAN |
2548 SIInstrFlags::N_INFINITY |
2549 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
2550 "mask not equal");
2551
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002552 SDLoc DL(N);
2553 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2554 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002555 }
2556 }
2557 }
2558
2559 return SDValue();
2560}
2561
Matt Arsenaultf2290332015-01-06 23:00:39 +00002562SDValue SITargetLowering::performOrCombine(SDNode *N,
2563 DAGCombinerInfo &DCI) const {
2564 SelectionDAG &DAG = DCI.DAG;
2565 SDValue LHS = N->getOperand(0);
2566 SDValue RHS = N->getOperand(1);
2567
Matt Arsenault3b082382016-04-12 18:24:38 +00002568 EVT VT = N->getValueType(0);
2569 if (VT == MVT::i64) {
2570 // TODO: This could be a generic combine with a predicate for extracting the
2571 // high half of an integer being free.
2572
2573 // (or i64:x, (zero_extend i32:y)) ->
2574 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
2575 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
2576 RHS.getOpcode() != ISD::ZERO_EXTEND)
2577 std::swap(LHS, RHS);
2578
2579 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
2580 SDValue ExtSrc = RHS.getOperand(0);
2581 EVT SrcVT = ExtSrc.getValueType();
2582 if (SrcVT == MVT::i32) {
2583 SDLoc SL(N);
2584 SDValue LowLHS, HiBits;
2585 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
2586 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
2587
2588 DCI.AddToWorklist(LowOr.getNode());
2589 DCI.AddToWorklist(HiBits.getNode());
2590
2591 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
2592 LowOr, HiBits);
2593 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2594 }
2595 }
2596 }
2597
Matt Arsenaultf2290332015-01-06 23:00:39 +00002598 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
2599 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
2600 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
2601 SDValue Src = LHS.getOperand(0);
2602 if (Src != RHS.getOperand(0))
2603 return SDValue();
2604
2605 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
2606 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
2607 if (!CLHS || !CRHS)
2608 return SDValue();
2609
2610 // Only 10 bits are used.
2611 static const uint32_t MaxMask = 0x3ff;
2612
2613 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002614 SDLoc DL(N);
2615 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2616 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00002617 }
2618
2619 return SDValue();
2620}
2621
2622SDValue SITargetLowering::performClassCombine(SDNode *N,
2623 DAGCombinerInfo &DCI) const {
2624 SelectionDAG &DAG = DCI.DAG;
2625 SDValue Mask = N->getOperand(1);
2626
2627 // fp_class x, 0 -> false
2628 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
2629 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002630 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002631 }
2632
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002633 if (N->getOperand(0).isUndef())
2634 return DAG.getUNDEF(MVT::i1);
2635
Matt Arsenaultf2290332015-01-06 23:00:39 +00002636 return SDValue();
2637}
2638
Matt Arsenault9cd90712016-04-14 01:42:16 +00002639// Constant fold canonicalize.
2640SDValue SITargetLowering::performFCanonicalizeCombine(
2641 SDNode *N,
2642 DAGCombinerInfo &DCI) const {
2643 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
2644 if (!CFP)
2645 return SDValue();
2646
2647 SelectionDAG &DAG = DCI.DAG;
2648 const APFloat &C = CFP->getValueAPF();
2649
2650 // Flush denormals to 0 if not enabled.
2651 if (C.isDenormal()) {
2652 EVT VT = N->getValueType(0);
2653 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
2654 return DAG.getConstantFP(0.0, SDLoc(N), VT);
2655
2656 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
2657 return DAG.getConstantFP(0.0, SDLoc(N), VT);
2658 }
2659
2660 if (C.isNaN()) {
2661 EVT VT = N->getValueType(0);
2662 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
2663 if (C.isSignaling()) {
2664 // Quiet a signaling NaN.
2665 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
2666 }
2667
2668 // Make sure it is the canonical NaN bitpattern.
2669 //
2670 // TODO: Can we use -1 as the canonical NaN value since it's an inline
2671 // immediate?
2672 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
2673 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
2674 }
2675
2676 return SDValue(CFP, 0);
2677}
2678
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002679static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
2680 switch (Opc) {
2681 case ISD::FMAXNUM:
2682 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002683 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002684 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002685 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002686 return AMDGPUISD::UMAX3;
2687 case ISD::FMINNUM:
2688 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002689 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002690 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002691 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002692 return AMDGPUISD::UMIN3;
2693 default:
2694 llvm_unreachable("Not a min/max opcode");
2695 }
2696}
2697
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002698static SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
2699 SDValue Op0, SDValue Op1, bool Signed) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00002700 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
2701 if (!K1)
2702 return SDValue();
2703
2704 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
2705 if (!K0)
2706 return SDValue();
2707
Matt Arsenaultf639c322016-01-28 20:53:42 +00002708 if (Signed) {
2709 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
2710 return SDValue();
2711 } else {
2712 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
2713 return SDValue();
2714 }
2715
2716 EVT VT = K0->getValueType(0);
2717 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
2718 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
2719}
2720
2721static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
2722 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
2723 return true;
2724
2725 return DAG.isKnownNeverNaN(Op);
2726}
2727
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002728static SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
2729 SDValue Op0, SDValue Op1) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00002730 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
2731 if (!K1)
2732 return SDValue();
2733
2734 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
2735 if (!K0)
2736 return SDValue();
2737
2738 // Ordered >= (although NaN inputs should have folded away by now).
2739 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
2740 if (Cmp == APFloat::cmpGreaterThan)
2741 return SDValue();
2742
2743 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
2744 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
2745 // give the other result, which is different from med3 with a NaN input.
2746 SDValue Var = Op0.getOperand(0);
2747 if (!isKnownNeverSNan(DAG, Var))
2748 return SDValue();
2749
2750 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
2751 Var, SDValue(K0, 0), SDValue(K1, 0));
2752}
2753
2754SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
2755 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002756 SelectionDAG &DAG = DCI.DAG;
2757
2758 unsigned Opc = N->getOpcode();
2759 SDValue Op0 = N->getOperand(0);
2760 SDValue Op1 = N->getOperand(1);
2761
2762 // Only do this if the inner op has one use since this will just increases
2763 // register pressure for no benefit.
2764
Matt Arsenault5b39b342016-01-28 20:53:48 +00002765 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
2766 // max(max(a, b), c) -> max3(a, b, c)
2767 // min(min(a, b), c) -> min3(a, b, c)
2768 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
2769 SDLoc DL(N);
2770 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2771 DL,
2772 N->getValueType(0),
2773 Op0.getOperand(0),
2774 Op0.getOperand(1),
2775 Op1);
2776 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002777
Matt Arsenault5b39b342016-01-28 20:53:48 +00002778 // Try commuted.
2779 // max(a, max(b, c)) -> max3(a, b, c)
2780 // min(a, min(b, c)) -> min3(a, b, c)
2781 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
2782 SDLoc DL(N);
2783 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2784 DL,
2785 N->getValueType(0),
2786 Op0,
2787 Op1.getOperand(0),
2788 Op1.getOperand(1));
2789 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002790 }
2791
Matt Arsenaultf639c322016-01-28 20:53:42 +00002792 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
2793 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
2794 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
2795 return Med3;
2796 }
2797
2798 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
2799 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
2800 return Med3;
2801 }
2802
2803 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00002804 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
2805 (Opc == AMDGPUISD::FMIN_LEGACY &&
2806 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00002807 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
2808 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
2809 return Res;
2810 }
2811
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002812 return SDValue();
2813}
2814
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002815SDValue SITargetLowering::performSetCCCombine(SDNode *N,
2816 DAGCombinerInfo &DCI) const {
2817 SelectionDAG &DAG = DCI.DAG;
2818 SDLoc SL(N);
2819
2820 SDValue LHS = N->getOperand(0);
2821 SDValue RHS = N->getOperand(1);
2822 EVT VT = LHS.getValueType();
2823
2824 if (VT != MVT::f32 && VT != MVT::f64)
2825 return SDValue();
2826
2827 // Match isinf pattern
2828 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
2829 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
2830 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
2831 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2832 if (!CRHS)
2833 return SDValue();
2834
2835 const APFloat &APF = CRHS->getValueAPF();
2836 if (APF.isInfinity() && !APF.isNegative()) {
2837 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002838 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
2839 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002840 }
2841 }
2842
2843 return SDValue();
2844}
2845
Tom Stellard75aadc22012-12-11 21:25:42 +00002846SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
2847 DAGCombinerInfo &DCI) const {
2848 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002849 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00002850
2851 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00002852 default:
2853 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002854 case ISD::SETCC:
2855 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00002856 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002857 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002858 case ISD::SMAX:
2859 case ISD::SMIN:
2860 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00002861 case ISD::UMIN:
2862 case AMDGPUISD::FMIN_LEGACY:
2863 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002864 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00002865 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002866 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00002867 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002868 break;
2869 }
Matt Arsenault364a6742014-06-11 17:50:44 +00002870
2871 case AMDGPUISD::CVT_F32_UBYTE0:
2872 case AMDGPUISD::CVT_F32_UBYTE1:
2873 case AMDGPUISD::CVT_F32_UBYTE2:
2874 case AMDGPUISD::CVT_F32_UBYTE3: {
2875 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
Matt Arsenault364a6742014-06-11 17:50:44 +00002876 SDValue Src = N->getOperand(0);
Matt Arsenaulta949dc62016-05-09 16:29:50 +00002877
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002878 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero.
Matt Arsenaulta949dc62016-05-09 16:29:50 +00002879 if (Src.getOpcode() == ISD::SRL) {
2880 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
2881 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
2882 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x
2883
2884 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(1))) {
2885 unsigned SrcOffset = C->getZExtValue() + 8 * Offset;
2886 if (SrcOffset < 32 && SrcOffset % 8 == 0) {
2887 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, DL,
2888 MVT::f32, Src.getOperand(0));
2889 }
2890 }
2891 }
2892
Matt Arsenault364a6742014-06-11 17:50:44 +00002893 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
2894
2895 APInt KnownZero, KnownOne;
2896 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
2897 !DCI.isBeforeLegalizeOps());
2898 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2899 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
2900 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
2901 DCI.CommitTargetLoweringOpt(TLO);
2902 }
2903
2904 break;
2905 }
2906
2907 case ISD::UINT_TO_FP: {
2908 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002909 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002910 case ISD::FADD: {
2911 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2912 break;
2913
2914 EVT VT = N->getValueType(0);
2915 if (VT != MVT::f32)
2916 break;
2917
Matt Arsenault8d630032015-02-20 22:10:41 +00002918 // Only do this if we are not trying to support denormals. v_mad_f32 does
2919 // not support denormals ever.
2920 if (Subtarget->hasFP32Denormals())
2921 break;
2922
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002923 SDValue LHS = N->getOperand(0);
2924 SDValue RHS = N->getOperand(1);
2925
2926 // These should really be instruction patterns, but writing patterns with
2927 // source modiifiers is a pain.
2928
2929 // fadd (fadd (a, a), b) -> mad 2.0, a, b
2930 if (LHS.getOpcode() == ISD::FADD) {
2931 SDValue A = LHS.getOperand(0);
2932 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002933 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002934 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002935 }
2936 }
2937
2938 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
2939 if (RHS.getOpcode() == ISD::FADD) {
2940 SDValue A = RHS.getOperand(0);
2941 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002942 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002943 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002944 }
2945 }
2946
Matt Arsenault8d630032015-02-20 22:10:41 +00002947 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002948 }
Matt Arsenault8675db12014-08-29 16:01:14 +00002949 case ISD::FSUB: {
2950 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2951 break;
2952
2953 EVT VT = N->getValueType(0);
2954
2955 // Try to get the fneg to fold into the source modifier. This undoes generic
2956 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00002957 //
2958 // Only do this if we are not trying to support denormals. v_mad_f32 does
2959 // not support denormals ever.
2960 if (VT == MVT::f32 &&
2961 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00002962 SDValue LHS = N->getOperand(0);
2963 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002964 if (LHS.getOpcode() == ISD::FADD) {
2965 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
2966
2967 SDValue A = LHS.getOperand(0);
2968 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002969 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002970 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
2971
Matt Arsenault8d630032015-02-20 22:10:41 +00002972 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002973 }
2974 }
2975
2976 if (RHS.getOpcode() == ISD::FADD) {
2977 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
2978
2979 SDValue A = RHS.getOperand(0);
2980 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002981 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002982 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002983 }
2984 }
Matt Arsenault8d630032015-02-20 22:10:41 +00002985
2986 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00002987 }
2988
2989 break;
2990 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002991 case ISD::LOAD:
2992 case ISD::STORE:
2993 case ISD::ATOMIC_LOAD:
2994 case ISD::ATOMIC_STORE:
2995 case ISD::ATOMIC_CMP_SWAP:
2996 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
2997 case ISD::ATOMIC_SWAP:
2998 case ISD::ATOMIC_LOAD_ADD:
2999 case ISD::ATOMIC_LOAD_SUB:
3000 case ISD::ATOMIC_LOAD_AND:
3001 case ISD::ATOMIC_LOAD_OR:
3002 case ISD::ATOMIC_LOAD_XOR:
3003 case ISD::ATOMIC_LOAD_NAND:
3004 case ISD::ATOMIC_LOAD_MIN:
3005 case ISD::ATOMIC_LOAD_MAX:
3006 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00003007 case ISD::ATOMIC_LOAD_UMAX:
3008 case AMDGPUISD::ATOMIC_INC:
3009 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003010 if (DCI.isBeforeLegalize())
3011 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003012
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003013 MemSDNode *MemNode = cast<MemSDNode>(N);
3014 SDValue Ptr = MemNode->getBasePtr();
3015
3016 // TODO: We could also do this for multiplies.
3017 unsigned AS = MemNode->getAddressSpace();
3018 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
3019 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
3020 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00003021 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003022
3023 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
3024 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
3025 }
3026 }
3027 break;
3028 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003029 case ISD::AND:
3030 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003031 case ISD::OR:
3032 return performOrCombine(N, DCI);
3033 case AMDGPUISD::FP_CLASS:
3034 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00003035 case ISD::FCANONICALIZE:
3036 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003037 case AMDGPUISD::FRACT:
3038 case AMDGPUISD::RCP:
3039 case AMDGPUISD::RSQ:
3040 case AMDGPUISD::RSQ_LEGACY:
3041 case AMDGPUISD::RSQ_CLAMP:
3042 case AMDGPUISD::LDEXP: {
3043 SDValue Src = N->getOperand(0);
3044 if (Src.isUndef())
3045 return Src;
3046 break;
3047 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003048 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003049 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00003050}
Christian Konigd910b7d2013-02-26 17:52:16 +00003051
Christian Konigf82901a2013-02-26 17:52:23 +00003052/// \brief Analyze the possible immediate value Op
3053///
3054/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
3055/// and the immediate value if it's a literal immediate
3056int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003057 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Christian Konigf82901a2013-02-26 17:52:23 +00003058
Tom Stellardedbf1eb2013-04-05 23:31:20 +00003059 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00003060 if (TII->isInlineConstant(Node->getAPIntValue()))
3061 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00003062
Matt Arsenault11a4d672015-02-13 19:05:03 +00003063 uint64_t Val = Node->getZExtValue();
3064 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00003065 }
3066
3067 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
3068 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
3069 return 0;
3070
3071 if (Node->getValueType(0) == MVT::f32)
3072 return FloatToBits(Node->getValueAPF().convertToFloat());
3073
3074 return -1;
3075 }
3076
3077 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00003078}
3079
Christian Konig8e06e2a2013-04-10 08:39:08 +00003080/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00003081static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00003082 switch (Idx) {
3083 default: return 0;
3084 case AMDGPU::sub0: return 0;
3085 case AMDGPU::sub1: return 1;
3086 case AMDGPU::sub2: return 2;
3087 case AMDGPU::sub3: return 3;
3088 }
3089}
3090
3091/// \brief Adjust the writemask of MIMG instructions
3092void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
3093 SelectionDAG &DAG) const {
3094 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00003095 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003096 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
3097 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00003098 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003099
3100 // Try to figure out the used register components
3101 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
3102 I != E; ++I) {
3103
3104 // Abort if we can't understand the usage
3105 if (!I->isMachineOpcode() ||
3106 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
3107 return;
3108
Tom Stellard54774e52013-10-23 02:53:47 +00003109 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
3110 // Note that subregs are packed, i.e. Lane==0 is the first bit set
3111 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
3112 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00003113 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00003114
Tom Stellard54774e52013-10-23 02:53:47 +00003115 // Set which texture component corresponds to the lane.
3116 unsigned Comp;
3117 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
3118 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00003119 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00003120 Dmask &= ~(1 << Comp);
3121 }
3122
Christian Konig8e06e2a2013-04-10 08:39:08 +00003123 // Abort if we have more than one user per component
3124 if (Users[Lane])
3125 return;
3126
3127 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00003128 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003129 }
3130
Tom Stellard54774e52013-10-23 02:53:47 +00003131 // Abort if there's no change
3132 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00003133 return;
3134
3135 // Adjust the writemask in the node
3136 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003137 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003138 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003139 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00003140 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003141
Christian Konig8b1ed282013-04-10 08:39:16 +00003142 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00003143 // (if NewDmask has only one bit set...)
3144 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003145 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
3146 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00003147 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003148 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00003149 SDValue(Node, 0), RC);
3150 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
3151 return;
3152 }
3153
Christian Konig8e06e2a2013-04-10 08:39:08 +00003154 // Update the users of the node with the new indices
3155 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
3156
3157 SDNode *User = Users[i];
3158 if (!User)
3159 continue;
3160
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003161 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003162 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
3163
3164 switch (Idx) {
3165 default: break;
3166 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
3167 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
3168 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
3169 }
3170 }
3171}
3172
Tom Stellardc98ee202015-07-16 19:40:07 +00003173static bool isFrameIndexOp(SDValue Op) {
3174 if (Op.getOpcode() == ISD::AssertZext)
3175 Op = Op.getOperand(0);
3176
3177 return isa<FrameIndexSDNode>(Op);
3178}
3179
Tom Stellard3457a842014-10-09 19:06:00 +00003180/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
3181/// with frame index operands.
3182/// LLVM assumes that inputs are to these instructions are registers.
3183void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
3184 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003185
3186 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00003187 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00003188 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00003189 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003190 continue;
3191 }
3192
Tom Stellard3457a842014-10-09 19:06:00 +00003193 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003194 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00003195 Node->getOperand(i).getValueType(),
3196 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003197 }
3198
Tom Stellard3457a842014-10-09 19:06:00 +00003199 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003200}
3201
Matt Arsenault08d84942014-06-03 23:06:13 +00003202/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00003203SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
3204 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003205 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003206 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00003207
Nicolai Haehnlec06bfa12016-07-11 21:59:43 +00003208 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() &&
3209 !TII->isGather4(Opcode))
Christian Konig8e06e2a2013-04-10 08:39:08 +00003210 adjustWritemask(Node, DAG);
3211
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003212 if (Opcode == AMDGPU::INSERT_SUBREG ||
3213 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003214 legalizeTargetIndependentNode(Node, DAG);
3215 return Node;
3216 }
Tom Stellard654d6692015-01-08 15:08:17 +00003217 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003218}
Christian Konig8b1ed282013-04-10 08:39:16 +00003219
3220/// \brief Assign the register class depending on the number of
3221/// bits set in the writemask
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003222void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
Christian Konig8b1ed282013-04-10 08:39:16 +00003223 SDNode *Node) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003224 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003225
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003226 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003227
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003228 if (TII->isVOP3(MI.getOpcode())) {
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003229 // Make sure constant bus requirements are respected.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003230 TII->legalizeOperandsVOP3(MRI, MI);
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003231 return;
3232 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00003233
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003234 if (TII->isMIMG(MI)) {
3235 unsigned VReg = MI.getOperand(0).getReg();
3236 unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4;
3237 unsigned Writemask = MI.getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003238 unsigned BitsSet = 0;
3239 for (unsigned i = 0; i < 4; ++i)
3240 BitsSet += Writemask & (1 << i) ? 1 : 0;
3241
3242 const TargetRegisterClass *RC;
3243 switch (BitsSet) {
3244 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00003245 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003246 case 2: RC = &AMDGPU::VReg_64RegClass; break;
3247 case 3: RC = &AMDGPU::VReg_96RegClass; break;
3248 }
3249
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003250 unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet);
3251 MI.setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003252 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00003253 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00003254 }
3255
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003256 // Replace unused atomics with the no return version.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003257 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003258 if (NoRetAtomicOp != -1) {
3259 if (!Node->hasAnyUseOfValue(0)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003260 MI.setDesc(TII->get(NoRetAtomicOp));
3261 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003262 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003263 }
3264
Tom Stellard354a43c2016-04-01 18:27:37 +00003265 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
3266 // instruction, because the return type of these instructions is a vec2 of
3267 // the memory type, so it can be tied to the input operand.
3268 // This means these instructions always have a use, so we need to add a
3269 // special case to check if the atomic has only one extract_subreg use,
3270 // which itself has no uses.
3271 if ((Node->hasNUsesOfValue(1, 0) &&
Nicolai Haehnle750082d2016-04-15 14:42:36 +00003272 Node->use_begin()->isMachineOpcode() &&
Tom Stellard354a43c2016-04-01 18:27:37 +00003273 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
3274 !Node->use_begin()->hasAnyUseOfValue(0))) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003275 unsigned Def = MI.getOperand(0).getReg();
Tom Stellard354a43c2016-04-01 18:27:37 +00003276
3277 // Change this into a noret atomic.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003278 MI.setDesc(TII->get(NoRetAtomicOp));
3279 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003280
3281 // If we only remove the def operand from the atomic instruction, the
3282 // extract_subreg will be left with a use of a vreg without a def.
3283 // So we need to insert an implicit_def to avoid machine verifier
3284 // errors.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003285 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
Tom Stellard354a43c2016-04-01 18:27:37 +00003286 TII->get(AMDGPU::IMPLICIT_DEF), Def);
3287 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003288 return;
3289 }
Christian Konig8b1ed282013-04-10 08:39:16 +00003290}
Tom Stellard0518ff82013-06-03 17:39:58 +00003291
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003292static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL,
3293 uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003294 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00003295 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
3296}
3297
3298MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003299 const SDLoc &DL,
Matt Arsenault485defe2014-11-05 19:01:17 +00003300 SDValue Ptr) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003301 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault485defe2014-11-05 19:01:17 +00003302
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003303 // Build the half of the subregister with the constants before building the
3304 // full 128-bit register. If we are building multiple resource descriptors,
3305 // this will allow CSEing of the 2-component register.
3306 const SDValue Ops0[] = {
3307 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
3308 buildSMovImm32(DAG, DL, 0),
3309 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
3310 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
3311 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
3312 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003313
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003314 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
3315 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00003316
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003317 // Combine the constants and the pointer.
3318 const SDValue Ops1[] = {
3319 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
3320 Ptr,
3321 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
3322 SubRegHi,
3323 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
3324 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003325
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003326 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00003327}
3328
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003329/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003330/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
3331/// of the resource descriptor) to create an offset, which is added to
3332/// the resource pointer.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003333MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL,
3334 SDValue Ptr, uint32_t RsrcDword1,
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003335 uint64_t RsrcDword2And3) const {
3336 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
3337 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
3338 if (RsrcDword1) {
3339 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003340 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
3341 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003342 }
3343
3344 SDValue DataLo = buildSMovImm32(DAG, DL,
3345 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
3346 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
3347
3348 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003349 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003350 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003351 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003352 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003353 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003354 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003355 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003356 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003357 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003358 };
3359
3360 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
3361}
3362
Tom Stellard94593ee2013-06-03 17:40:18 +00003363SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3364 const TargetRegisterClass *RC,
3365 unsigned Reg, EVT VT) const {
3366 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
3367
3368 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
3369 cast<RegisterSDNode>(VReg)->getReg(), VT);
3370}
Tom Stellardd7e6f132015-04-08 01:09:26 +00003371
3372//===----------------------------------------------------------------------===//
3373// SI Inline Assembly Support
3374//===----------------------------------------------------------------------===//
3375
3376std::pair<unsigned, const TargetRegisterClass *>
3377SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003378 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00003379 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003380
3381 if (Constraint.size() == 1) {
3382 switch (Constraint[0]) {
3383 case 's':
3384 case 'r':
3385 switch (VT.getSizeInBits()) {
3386 default:
3387 return std::make_pair(0U, nullptr);
3388 case 32:
Tom Stellardd7e6f132015-04-08 01:09:26 +00003389 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003390 case 64:
3391 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
3392 case 128:
3393 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
3394 case 256:
3395 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
3396 }
3397
3398 case 'v':
3399 switch (VT.getSizeInBits()) {
3400 default:
3401 return std::make_pair(0U, nullptr);
3402 case 32:
3403 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
3404 case 64:
3405 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
3406 case 96:
3407 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
3408 case 128:
3409 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
3410 case 256:
3411 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
3412 case 512:
3413 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
3414 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00003415 }
3416 }
3417
3418 if (Constraint.size() > 1) {
3419 const TargetRegisterClass *RC = nullptr;
3420 if (Constraint[1] == 'v') {
3421 RC = &AMDGPU::VGPR_32RegClass;
3422 } else if (Constraint[1] == 's') {
3423 RC = &AMDGPU::SGPR_32RegClass;
3424 }
3425
3426 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00003427 uint32_t Idx;
3428 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
3429 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00003430 return std::make_pair(RC->getRegister(Idx), RC);
3431 }
3432 }
3433 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
3434}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003435
3436SITargetLowering::ConstraintType
3437SITargetLowering::getConstraintType(StringRef Constraint) const {
3438 if (Constraint.size() == 1) {
3439 switch (Constraint[0]) {
3440 default: break;
3441 case 's':
3442 case 'v':
3443 return C_RegisterClass;
3444 }
3445 }
3446 return TargetLowering::getConstraintType(Constraint);
3447}