blob: 92c832f7a4624a24545d65981143dcf48a5c207d [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"
Wei Ding07e03712016-07-28 16:42:13 +000034#include "llvm/CodeGen/Analysis.h"
Oliver Stannard7e7d9832016-02-02 13:52:43 +000035#include "llvm/IR/DiagnosticInfo.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000036#include "llvm/IR/Function.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000037
38using namespace llvm;
39
Tom Stellardf110f8f2016-04-14 16:27:03 +000040static unsigned findFirstFreeSGPR(CCState &CCInfo) {
41 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs();
42 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) {
43 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) {
44 return AMDGPU::SGPR0 + Reg;
45 }
46 }
47 llvm_unreachable("Cannot allocate sgpr");
48}
49
Matt Arsenault43e92fe2016-06-24 06:30:11 +000050SITargetLowering::SITargetLowering(const TargetMachine &TM,
51 const SISubtarget &STI)
Eric Christopher7792e322015-01-30 23:24:40 +000052 : AMDGPUTargetLowering(TM, STI) {
Tom Stellard1bd80722014-04-30 15:31:33 +000053 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000054 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000055
Tom Stellard334b29c2014-04-17 21:00:09 +000056 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000057 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000058
Tom Stellard436780b2014-05-15 14:41:57 +000059 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
60 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
61 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000062
Matt Arsenault61001bb2015-11-25 19:58:34 +000063 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
64 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
65
Tom Stellard436780b2014-05-15 14:41:57 +000066 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
67 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000068
Tom Stellardf0a21072014-11-18 20:39:39 +000069 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000070 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
71
Tom Stellardf0a21072014-11-18 20:39:39 +000072 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000073 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000074
Eric Christopher23a3a7c2015-02-26 00:00:24 +000075 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000076
Tom Stellard35bb18c2013-08-26 15:06:04 +000077 // We need to custom lower vector stores from local memory
Matt Arsenault71e66762016-05-21 02:27:49 +000078 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000079 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000080 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
81 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000082 setOperationAction(ISD::LOAD, MVT::i1, Custom);
Matt Arsenault2b957b52016-05-02 20:07:26 +000083
Matt Arsenaultbcdfee72016-05-02 20:13:51 +000084 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000085 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
86 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
87 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
88 setOperationAction(ISD::STORE, MVT::i1, Custom);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +000089
Matt Arsenault71e66762016-05-21 02:27:49 +000090 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
91 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
92 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
93 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
94
95 setOperationAction(ISD::SELECT, MVT::i1, Promote);
Tom Stellard0ec134f2014-02-04 17:18:40 +000096 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +000097 setOperationAction(ISD::SELECT, MVT::f64, Promote);
98 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +000099
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000100 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
101 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
102 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
103 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Matt Arsenault71e66762016-05-21 02:27:49 +0000104 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000105
Tom Stellardd1efda82016-01-20 21:48:24 +0000106 setOperationAction(ISD::SETCC, MVT::i1, Promote);
Tom Stellard83747202013-07-18 21:43:53 +0000107 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
108 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
109
Matt Arsenault71e66762016-05-21 02:27:49 +0000110 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
111 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Matt Arsenaulte306a322014-10-21 16:25:08 +0000112
Matt Arsenault4e466652014-04-16 01:41:30 +0000113 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
114 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000115 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
116 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
120
Tom Stellard9fa17912013-08-14 23:24:45 +0000121 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000122 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000123 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
124
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000125 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000126 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000127 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
128 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
129 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
130 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000131
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000132 // We only support LOAD/STORE and vector manipulation ops for vectors
133 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000134 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000135 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000136 switch (Op) {
Tom Stellard967bf582014-02-13 23:34:15 +0000137 case ISD::LOAD:
138 case ISD::STORE:
139 case ISD::BUILD_VECTOR:
140 case ISD::BITCAST:
141 case ISD::EXTRACT_VECTOR_ELT:
142 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000143 case ISD::INSERT_SUBVECTOR:
144 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000145 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000146 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000147 case ISD::CONCAT_VECTORS:
148 setOperationAction(Op, VT, Custom);
149 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000150 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000151 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000152 break;
153 }
154 }
155 }
156
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000157 // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that
158 // is expanded to avoid having two separate loops in case the index is a VGPR.
159
Matt Arsenault61001bb2015-11-25 19:58:34 +0000160 // Most operations are naturally 32-bit vector operations. We only support
161 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
162 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
163 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
164 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
165
166 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
167 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
168
169 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
170 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
171
172 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
173 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
174 }
175
Matt Arsenault71e66762016-05-21 02:27:49 +0000176 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
177 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
178 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
179 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000180
Tom Stellard354a43c2016-04-01 18:27:37 +0000181 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling,
182 // and output demarshalling
183 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
184 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
185
186 // We can't return success/failure, only the old value,
187 // let LLVM add the comparison
188 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand);
189 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand);
190
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000191 if (getSubtarget()->hasFlatAddressSpace()) {
Matt Arsenault99c14522016-04-25 19:27:24 +0000192 setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom);
193 setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom);
194 }
195
Matt Arsenault71e66762016-05-21 02:27:49 +0000196 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
197 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
198
199 // On SI this is s_memtime and s_memrealtime on VI.
200 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
Matt Arsenault0bb294b2016-06-17 22:27:03 +0000201 setOperationAction(ISD::TRAP, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000202
203 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
204 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
205
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000206 if (Subtarget->getGeneration() >= SISubtarget::SEA_ISLANDS) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000207 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
208 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
209 setOperationAction(ISD::FRINT, MVT::f64, Legal);
210 }
211
212 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
213
214 setOperationAction(ISD::FSIN, MVT::f32, Custom);
215 setOperationAction(ISD::FCOS, MVT::f32, Custom);
216 setOperationAction(ISD::FDIV, MVT::f32, Custom);
217 setOperationAction(ISD::FDIV, MVT::f64, Custom);
218
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000219 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000220 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000221 setTargetDAGCombine(ISD::FMINNUM);
222 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000223 setTargetDAGCombine(ISD::SMIN);
224 setTargetDAGCombine(ISD::SMAX);
225 setTargetDAGCombine(ISD::UMIN);
226 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000227 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000228 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000229 setTargetDAGCombine(ISD::OR);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000230 setTargetDAGCombine(ISD::XOR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000231 setTargetDAGCombine(ISD::UINT_TO_FP);
Matt Arsenault9cd90712016-04-14 01:42:16 +0000232 setTargetDAGCombine(ISD::FCANONICALIZE);
Matt Arsenault364a6742014-06-11 17:50:44 +0000233
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000234 // All memory operations. Some folding on the pointer operand is done to help
235 // matching the constant offsets in the addressing modes.
236 setTargetDAGCombine(ISD::LOAD);
237 setTargetDAGCombine(ISD::STORE);
238 setTargetDAGCombine(ISD::ATOMIC_LOAD);
239 setTargetDAGCombine(ISD::ATOMIC_STORE);
240 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
241 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
242 setTargetDAGCombine(ISD::ATOMIC_SWAP);
243 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
244 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
245 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
246 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
247 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
248 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
249 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
250 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
251 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
252 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
253
Christian Konigeecebd02013-03-26 14:04:02 +0000254 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000255}
256
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000257const SISubtarget *SITargetLowering::getSubtarget() const {
258 return static_cast<const SISubtarget *>(Subtarget);
259}
260
Tom Stellard0125f2a2013-06-25 02:39:35 +0000261//===----------------------------------------------------------------------===//
262// TargetLowering queries
263//===----------------------------------------------------------------------===//
264
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000265bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
266 const CallInst &CI,
267 unsigned IntrID) const {
268 switch (IntrID) {
269 case Intrinsic::amdgcn_atomic_inc:
270 case Intrinsic::amdgcn_atomic_dec:
271 Info.opc = ISD::INTRINSIC_W_CHAIN;
272 Info.memVT = MVT::getVT(CI.getType());
273 Info.ptrVal = CI.getOperand(0);
274 Info.align = 0;
275 Info.vol = false;
276 Info.readMem = true;
277 Info.writeMem = true;
278 return true;
279 default:
280 return false;
281 }
282}
283
Matt Arsenaulte306a322014-10-21 16:25:08 +0000284bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
285 EVT) const {
286 // SI has some legal vector types, but no legal vector operations. Say no
287 // shuffles are legal in order to prefer scalarizing some vector operations.
288 return false;
289}
290
Tom Stellard70580f82015-07-20 14:28:41 +0000291bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
292 // Flat instructions do not have offsets, and only have the register
293 // address.
294 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
295}
296
Matt Arsenault711b3902015-08-07 20:18:34 +0000297bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
298 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
299 // additionally can do r + r + i with addr64. 32-bit has more addressing
300 // mode options. Depending on the resource constant, it can also do
301 // (i64 r0) + (i32 r1) * (i14 i).
302 //
303 // Private arrays end up using a scratch buffer most of the time, so also
304 // assume those use MUBUF instructions. Scratch loads / stores are currently
305 // implemented as mubuf instructions with offen bit set, so slightly
306 // different than the normal addr64.
307 if (!isUInt<12>(AM.BaseOffs))
308 return false;
309
310 // FIXME: Since we can split immediate into soffset and immediate offset,
311 // would it make sense to allow any immediate?
312
313 switch (AM.Scale) {
314 case 0: // r + i or just i, depending on HasBaseReg.
315 return true;
316 case 1:
317 return true; // We have r + r or r + i.
318 case 2:
319 if (AM.HasBaseReg) {
320 // Reject 2 * r + r.
321 return false;
322 }
323
324 // Allow 2 * r as r + r
325 // Or 2 * r + i is allowed as r + r + i.
326 return true;
327 default: // Don't allow n * r
328 return false;
329 }
330}
331
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000332bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
333 const AddrMode &AM, Type *Ty,
334 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000335 // No global is ever allowed as a base.
336 if (AM.BaseGV)
337 return false;
338
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000339 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000340 case AMDGPUAS::GLOBAL_ADDRESS: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000341 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) {
Tom Stellard70580f82015-07-20 14:28:41 +0000342 // Assume the we will use FLAT for all global memory accesses
343 // on VI.
344 // FIXME: This assumption is currently wrong. On VI we still use
345 // MUBUF instructions for the r + i addressing mode. As currently
346 // implemented, the MUBUF instructions only work on buffer < 4GB.
347 // It may be possible to support > 4GB buffers with MUBUF instructions,
348 // by setting the stride value in the resource descriptor which would
349 // increase the size limit to (stride * 4GB). However, this is risky,
350 // because it has never been validated.
351 return isLegalFlatAddressingMode(AM);
352 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000353
Matt Arsenault711b3902015-08-07 20:18:34 +0000354 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000355 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000356 case AMDGPUAS::CONSTANT_ADDRESS: {
357 // If the offset isn't a multiple of 4, it probably isn't going to be
358 // correctly aligned.
Matt Arsenault3cc1e002016-08-13 01:43:51 +0000359 // FIXME: Can we get the real alignment here?
Matt Arsenault711b3902015-08-07 20:18:34 +0000360 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.
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000438 // Until MVT is extended to handle this, simply check for the size and
439 // rely on the condition below: allow accesses if the size is a multiple of 4.
440 if (VT == MVT::Other || (VT != MVT::Other && VT.getSizeInBits() > 1024 &&
441 VT.getStoreSize() > 16)) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000442 return false;
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000443 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000444
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000445 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
446 AddrSpace == AMDGPUAS::REGION_ADDRESS) {
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000447 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
448 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
449 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000450 bool AlignedBy4 = (Align % 4 == 0);
451 if (IsFast)
452 *IsFast = AlignedBy4;
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000453
Sanjay Patelce74db92015-09-03 15:03:19 +0000454 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000455 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000456
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000457 if (Subtarget->hasUnalignedBufferAccess()) {
458 // If we have an uniform constant load, it still requires using a slow
459 // buffer instruction if unaligned.
460 if (IsFast) {
461 *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS) ?
462 (Align % 4 == 0) : true;
463 }
464
465 return true;
466 }
467
Tom Stellard33e64c62015-02-04 20:49:52 +0000468 // Smaller than dword value must be aligned.
Tom Stellard33e64c62015-02-04 20:49:52 +0000469 if (VT.bitsLT(MVT::i32))
470 return false;
471
Matt Arsenault1018c892014-04-24 17:08:26 +0000472 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
473 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000474 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000475 if (IsFast)
476 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000477
478 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000479}
480
Matt Arsenault46645fa2014-07-28 17:49:26 +0000481EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
482 unsigned SrcAlign, bool IsMemset,
483 bool ZeroMemset,
484 bool MemcpyStrSrc,
485 MachineFunction &MF) const {
486 // FIXME: Should account for address space here.
487
488 // The default fallback uses the private pointer size as a guess for a type to
489 // use. Make sure we switch these to 64-bit accesses.
490
491 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
492 return MVT::v4i32;
493
494 if (Size >= 8 && DstAlign >= 4)
495 return MVT::v2i32;
496
497 // Use the default.
498 return MVT::Other;
499}
500
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000501static bool isFlatGlobalAddrSpace(unsigned AS) {
502 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
503 AS == AMDGPUAS::FLAT_ADDRESS ||
504 AS == AMDGPUAS::CONSTANT_ADDRESS;
505}
506
507bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
508 unsigned DestAS) const {
Matt Arsenault37fefd62016-06-10 02:18:02 +0000509 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000510}
511
Tom Stellarda6f24c62015-12-15 20:55:55 +0000512bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
513 const MemSDNode *MemNode = cast<MemSDNode>(N);
514 const Value *Ptr = MemNode->getMemOperand()->getValue();
515
516 // UndefValue means this is a load of a kernel input. These are uniform.
Tom Stellard418beb72016-07-13 14:23:33 +0000517 // Sometimes LDS instructions have constant pointers.
518 // If Ptr is null, then that means this mem operand contains a
519 // PseudoSourceValue like GOT.
520 if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) ||
521 isa<Constant>(Ptr) || isa<GlobalValue>(Ptr))
Tom Stellarda6f24c62015-12-15 20:55:55 +0000522 return true;
523
Tom Stellard418beb72016-07-13 14:23:33 +0000524 const Instruction *I = dyn_cast<Instruction>(Ptr);
Tom Stellarda6f24c62015-12-15 20:55:55 +0000525 return I && I->getMetadata("amdgpu.uniform");
526}
527
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000528TargetLoweringBase::LegalizeTypeAction
529SITargetLowering::getPreferredVectorAction(EVT VT) const {
530 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
531 return TypeSplitVector;
532
533 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000534}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000535
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000536bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
537 Type *Ty) const {
Matt Arsenault749035b2016-07-30 01:40:36 +0000538 // FIXME: Could be smarter if called for vector constants.
539 return true;
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000540}
541
Tom Stellard2e045bb2016-01-20 00:13:22 +0000542bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
543
544 // SimplifySetCC uses this function to determine whether or not it should
545 // create setcc with i1 operands. We don't have instructions for i1 setcc.
546 if (VT == MVT::i1 && Op == ISD::SETCC)
547 return false;
548
549 return TargetLowering::isTypeDesirableForOp(Op, VT);
550}
551
Jan Veselyfea814d2016-06-21 20:46:20 +0000552SDValue SITargetLowering::LowerParameterPtr(SelectionDAG &DAG,
553 const SDLoc &SL, SDValue Chain,
554 unsigned Offset) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000555 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000556 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000557 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaultac234b62015-11-30 21:15:57 +0000558 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000559
Matt Arsenault86033ca2014-07-28 17:31:39 +0000560 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000561 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000562 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
563 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
Jan Veselyfea814d2016-06-21 20:46:20 +0000564 return DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
565 DAG.getConstant(Offset, SL, PtrVT));
566}
567SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
568 const SDLoc &SL, SDValue Chain,
569 unsigned Offset, bool Signed) const {
570 const DataLayout &DL = DAG.getDataLayout();
571 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
572 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
573 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Mehdi Amini44ede332015-07-09 02:09:04 +0000574 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000575 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
576
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000577 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000578
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000579 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000580 if (MemVT.isFloatingPoint())
581 ExtTy = ISD::EXTLOAD;
582
Jan Veselyfea814d2016-06-21 20:46:20 +0000583 SDValue Ptr = LowerParameterPtr(DAG, SL, Chain, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +0000584 return DAG.getLoad(ISD::UNINDEXED, ExtTy, VT, SL, Chain, Ptr, PtrOffset,
Justin Lebaradbf09e2016-09-11 01:38:58 +0000585 PtrInfo, MemVT, Align,
586 MachineMemOperand::MONonTemporal |
587 MachineMemOperand::MODereferenceable |
588 MachineMemOperand::MOInvariant);
Tom Stellard94593ee2013-06-03 17:40:18 +0000589}
590
Christian Konig2c8f6d52013-03-07 09:03:52 +0000591SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000592 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000593 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
594 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000595 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000596
597 MachineFunction &MF = DAG.getMachineFunction();
598 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000599 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000600 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000601
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000602 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) {
Matt Arsenaultd48da142015-11-02 23:23:02 +0000603 const Function *Fn = MF.getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000604 DiagnosticInfoUnsupported NoGraphicsHSA(
605 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
Matt Arsenaultd48da142015-11-02 23:23:02 +0000606 DAG.getContext()->diagnose(NoGraphicsHSA);
Diana Picus81bc3172016-05-26 15:24:55 +0000607 return DAG.getEntryNode();
Matt Arsenaultd48da142015-11-02 23:23:02 +0000608 }
609
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000610 // Create stack objects that are used for emitting debugger prologue if
611 // "amdgpu-debugger-emit-prologue" attribute was specified.
612 if (ST.debuggerEmitPrologue())
613 createDebuggerPrologueStackObjects(MF);
614
Christian Konig2c8f6d52013-03-07 09:03:52 +0000615 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000616 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000617
618 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000619 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000620
621 // First check if it's a PS input addr
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000622 if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000623 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000624
Marek Olsakfccabaf2016-01-13 11:45:36 +0000625 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000626 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000627 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000628 ++PSInputNum;
629 continue;
630 }
631
Marek Olsakfccabaf2016-01-13 11:45:36 +0000632 Info->markPSInputAllocated(PSInputNum);
633 if (Arg.Used)
634 Info->PSInputEna |= 1 << PSInputNum;
635
636 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000637 }
638
Matt Arsenault539ca882016-05-05 20:27:02 +0000639 if (AMDGPU::isShader(CallConv)) {
640 // Second split vertices into their elements
641 if (Arg.VT.isVector()) {
642 ISD::InputArg NewArg = Arg;
643 NewArg.Flags.setSplit();
644 NewArg.VT = Arg.VT.getVectorElementType();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000645
Matt Arsenault539ca882016-05-05 20:27:02 +0000646 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
647 // three or five element vertex only needs three or five registers,
648 // NOT four or eight.
649 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
650 unsigned NumElements = ParamType->getVectorNumElements();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000651
Matt Arsenault539ca882016-05-05 20:27:02 +0000652 for (unsigned j = 0; j != NumElements; ++j) {
653 Splits.push_back(NewArg);
654 NewArg.PartOffset += NewArg.VT.getStoreSize();
655 }
656 } else {
657 Splits.push_back(Arg);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000658 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000659 }
660 }
661
662 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000663 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
664 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000665
Christian Konig99ee0f42013-03-07 09:04:14 +0000666 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000667 //
668 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
669 // PSInputAddr, the user wants to enable some bits after the compilation
670 // based on run-time states. Since we can't know what the final PSInputEna
671 // will look like, so we shouldn't do anything here and the user should take
672 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000673 //
674 // Otherwise, the following restrictions apply:
675 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
676 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
677 // enabled too.
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000678 if (CallConv == CallingConv::AMDGPU_PS &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000679 ((Info->getPSInputAddr() & 0x7F) == 0 ||
NAKAMURA Takumife1202c2016-06-20 00:37:41 +0000680 ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000681 CCInfo.AllocateReg(AMDGPU::VGPR0);
682 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000683 Info->markPSInputAllocated(0);
684 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000685 }
686
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000687 if (!AMDGPU::isShader(CallConv)) {
Tom Stellardf110f8f2016-04-14 16:27:03 +0000688 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
689 } else {
690 assert(!Info->hasPrivateSegmentBuffer() && !Info->hasDispatchPtr() &&
691 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() &&
692 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
693 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() &&
694 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() &&
695 !Info->hasWorkItemIDZ());
Tom Stellardaf775432013-10-23 00:44:32 +0000696 }
697
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000698 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
699 if (Info->hasPrivateSegmentBuffer()) {
700 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
701 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
702 CCInfo.AllocateReg(PrivateSegmentBufferReg);
703 }
704
705 if (Info->hasDispatchPtr()) {
706 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
707 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
708 CCInfo.AllocateReg(DispatchPtrReg);
709 }
710
Matt Arsenault48ab5262016-04-25 19:27:18 +0000711 if (Info->hasQueuePtr()) {
712 unsigned QueuePtrReg = Info->addQueuePtr(*TRI);
713 MF.addLiveIn(QueuePtrReg, &AMDGPU::SReg_64RegClass);
714 CCInfo.AllocateReg(QueuePtrReg);
715 }
716
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000717 if (Info->hasKernargSegmentPtr()) {
718 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
719 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
720 CCInfo.AllocateReg(InputPtrReg);
721 }
722
Matt Arsenault8d718dc2016-07-22 17:01:30 +0000723 if (Info->hasDispatchID()) {
724 unsigned DispatchIDReg = Info->addDispatchID(*TRI);
725 MF.addLiveIn(DispatchIDReg, &AMDGPU::SReg_64RegClass);
726 CCInfo.AllocateReg(DispatchIDReg);
727 }
728
Matt Arsenault296b8492016-02-12 06:31:30 +0000729 if (Info->hasFlatScratchInit()) {
730 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
731 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SReg_64RegClass);
732 CCInfo.AllocateReg(FlatScratchInitReg);
733 }
734
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000735 if (!AMDGPU::isShader(CallConv))
736 analyzeFormalArgumentsCompute(CCInfo, Ins);
737 else
738 AnalyzeFormalArguments(CCInfo, Splits);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000739
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000740 SmallVector<SDValue, 16> Chains;
741
Christian Konig2c8f6d52013-03-07 09:03:52 +0000742 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
743
Christian Konigb7be72d2013-05-17 09:46:48 +0000744 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000745 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000746 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000747 continue;
748 }
749
Christian Konig2c8f6d52013-03-07 09:03:52 +0000750 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000751 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000752
753 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000754 VT = Ins[i].VT;
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000755 EVT MemVT = VA.getLocVT();
Tom Stellardb5798b02015-06-26 21:15:03 +0000756 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
757 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000758 // The first 36 bytes of the input buffer contains information about
759 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000760 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000761 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000762 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000763
Craig Toppere3dcce92015-08-01 22:20:21 +0000764 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000765 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000766 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Tom Stellardca7ecf32014-08-22 18:49:31 +0000767 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
768 // On SI local pointers are just offsets into LDS, so they are always
769 // less than 16-bits. On CI and newer they could potentially be
770 // real pointers, so we can't guarantee their size.
771 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
772 DAG.getValueType(MVT::i16));
773 }
774
Tom Stellarded882c22013-06-03 17:40:11 +0000775 InVals.push_back(Arg);
Matt Arsenault52ef4012016-07-26 16:45:58 +0000776 Info->setABIArgOffset(Offset + MemVT.getStoreSize());
Tom Stellarded882c22013-06-03 17:40:11 +0000777 continue;
778 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000779 assert(VA.isRegLoc() && "Parameter must be in a register!");
780
781 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000782
783 if (VT == MVT::i64) {
784 // For now assume it is a pointer
785 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
786 &AMDGPU::SReg_64RegClass);
787 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000788 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
789 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000790 continue;
791 }
792
793 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
794
795 Reg = MF.addLiveIn(Reg, RC);
796 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
797
Christian Konig2c8f6d52013-03-07 09:03:52 +0000798 if (Arg.VT.isVector()) {
799
800 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000801 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000802 unsigned NumElements = ParamType->getVectorNumElements();
803
804 SmallVector<SDValue, 4> Regs;
805 Regs.push_back(Val);
806 for (unsigned j = 1; j != NumElements; ++j) {
807 Reg = ArgLocs[ArgIdx++].getLocReg();
808 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000809
810 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
811 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000812 }
813
814 // Fill up the missing vector elements
815 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000816 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000817
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000818 InVals.push_back(DAG.getBuildVector(Arg.VT, DL, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000819 continue;
820 }
821
822 InVals.push_back(Val);
823 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000824
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000825 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
826 // these from the dispatch pointer.
827
828 // Start adding system SGPRs.
829 if (Info->hasWorkGroupIDX()) {
830 unsigned Reg = Info->addWorkGroupIDX();
831 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
832 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000833 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000834
835 if (Info->hasWorkGroupIDY()) {
836 unsigned Reg = Info->addWorkGroupIDY();
837 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
838 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000839 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000840
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000841 if (Info->hasWorkGroupIDZ()) {
842 unsigned Reg = Info->addWorkGroupIDZ();
843 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
844 CCInfo.AllocateReg(Reg);
845 }
846
847 if (Info->hasWorkGroupInfo()) {
848 unsigned Reg = Info->addWorkGroupInfo();
849 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
850 CCInfo.AllocateReg(Reg);
851 }
852
853 if (Info->hasPrivateSegmentWaveByteOffset()) {
854 // Scratch wave offset passed in system SGPR.
Tom Stellardf110f8f2016-04-14 16:27:03 +0000855 unsigned PrivateSegmentWaveByteOffsetReg;
856
857 if (AMDGPU::isShader(CallConv)) {
858 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
859 Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
860 } else
861 PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset();
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000862
863 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
864 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
865 }
866
867 // Now that we've figured out where the scratch register inputs are, see if
868 // should reserve the arguments and use them directly.
Matthias Braun941a7052016-07-28 18:40:00 +0000869 bool HasStackObjects = MF.getFrameInfo().hasStackObjects();
Matt Arsenault296b8492016-02-12 06:31:30 +0000870 // Record that we know we have non-spill stack objects so we don't need to
871 // check all stack objects later.
872 if (HasStackObjects)
873 Info->setHasNonSpillStackObjects(true);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000874
Tom Stellard0b76fc4c2016-09-16 21:34:26 +0000875 if (ST.isAmdCodeObjectV2()) {
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000876 // TODO: Assume we will spill without optimizations.
877 if (HasStackObjects) {
878 // If we have stack objects, we unquestionably need the private buffer
Tom Stellard0b76fc4c2016-09-16 21:34:26 +0000879 // resource. For the Code Object V2 ABI, this will be the first 4 user
880 // SGPR inputs. We can reserve those and use them directly.
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000881
882 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
883 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
884 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
885
886 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
887 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
888 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
889 } else {
890 unsigned ReservedBufferReg
891 = TRI->reservedPrivateSegmentBufferReg(MF);
892 unsigned ReservedOffsetReg
893 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
894
895 // We tentatively reserve the last registers (skipping the last two
896 // which may contain VCC). After register allocation, we'll replace
897 // these with the ones immediately after those which were really
898 // allocated. In the prologue copies will be inserted from the argument
899 // to these reserved registers.
900 Info->setScratchRSrcReg(ReservedBufferReg);
901 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
902 }
903 } else {
904 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
905
906 // Without HSA, relocations are used for the scratch pointer and the
907 // buffer resource setup is always inserted in the prologue. Scratch wave
908 // offset is still in an input SGPR.
909 Info->setScratchRSrcReg(ReservedBufferReg);
910
911 if (HasStackObjects) {
912 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
913 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
914 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
915 } else {
916 unsigned ReservedOffsetReg
917 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
918 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
919 }
920 }
921
922 if (Info->hasWorkItemIDX()) {
923 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
924 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
925 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000926 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000927
928 if (Info->hasWorkItemIDY()) {
929 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
930 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
931 CCInfo.AllocateReg(Reg);
932 }
933
934 if (Info->hasWorkItemIDZ()) {
935 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
936 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
937 CCInfo.AllocateReg(Reg);
938 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000939
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000940 if (Chains.empty())
941 return Chain;
942
943 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000944}
945
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000946SDValue
947SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
948 bool isVarArg,
949 const SmallVectorImpl<ISD::OutputArg> &Outs,
950 const SmallVectorImpl<SDValue> &OutVals,
951 const SDLoc &DL, SelectionDAG &DAG) const {
Marek Olsak8a0f3352016-01-13 17:23:04 +0000952 MachineFunction &MF = DAG.getMachineFunction();
953 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
954
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000955 if (!AMDGPU::isShader(CallConv))
Marek Olsak8a0f3352016-01-13 17:23:04 +0000956 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
957 OutVals, DL, DAG);
958
Marek Olsak8e9cc632016-01-13 17:23:09 +0000959 Info->setIfReturnsVoid(Outs.size() == 0);
960
Marek Olsak8a0f3352016-01-13 17:23:04 +0000961 SmallVector<ISD::OutputArg, 48> Splits;
962 SmallVector<SDValue, 48> SplitVals;
963
964 // Split vectors into their elements.
965 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
966 const ISD::OutputArg &Out = Outs[i];
967
968 if (Out.VT.isVector()) {
969 MVT VT = Out.VT.getVectorElementType();
970 ISD::OutputArg NewOut = Out;
971 NewOut.Flags.setSplit();
972 NewOut.VT = VT;
973
974 // We want the original number of vector elements here, e.g.
975 // three or five, not four or eight.
976 unsigned NumElements = Out.ArgVT.getVectorNumElements();
977
978 for (unsigned j = 0; j != NumElements; ++j) {
979 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
980 DAG.getConstant(j, DL, MVT::i32));
981 SplitVals.push_back(Elem);
982 Splits.push_back(NewOut);
983 NewOut.PartOffset += NewOut.VT.getStoreSize();
984 }
985 } else {
986 SplitVals.push_back(OutVals[i]);
987 Splits.push_back(Out);
988 }
989 }
990
991 // CCValAssign - represent the assignment of the return value to a location.
992 SmallVector<CCValAssign, 48> RVLocs;
993
994 // CCState - Info about the registers and stack slots.
995 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
996 *DAG.getContext());
997
998 // Analyze outgoing return values.
999 AnalyzeReturn(CCInfo, Splits);
1000
1001 SDValue Flag;
1002 SmallVector<SDValue, 48> RetOps;
1003 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1004
1005 // Copy the result values into the output registers.
1006 for (unsigned i = 0, realRVLocIdx = 0;
1007 i != RVLocs.size();
1008 ++i, ++realRVLocIdx) {
1009 CCValAssign &VA = RVLocs[i];
1010 assert(VA.isRegLoc() && "Can only return in registers!");
1011
1012 SDValue Arg = SplitVals[realRVLocIdx];
1013
1014 // Copied from other backends.
1015 switch (VA.getLocInfo()) {
1016 default: llvm_unreachable("Unknown loc info!");
1017 case CCValAssign::Full:
1018 break;
1019 case CCValAssign::BCvt:
1020 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1021 break;
1022 }
1023
1024 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
1025 Flag = Chain.getValue(1);
1026 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1027 }
1028
1029 // Update chain and glue.
1030 RetOps[0] = Chain;
1031 if (Flag.getNode())
1032 RetOps.push_back(Flag);
1033
Matt Arsenault9babdf42016-06-22 20:15:28 +00001034 unsigned Opc = Info->returnsVoid() ? AMDGPUISD::ENDPGM : AMDGPUISD::RETURN;
1035 return DAG.getNode(Opc, DL, MVT::Other, RetOps);
Marek Olsak8a0f3352016-01-13 17:23:04 +00001036}
1037
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001038unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1039 SelectionDAG &DAG) const {
1040 unsigned Reg = StringSwitch<unsigned>(RegName)
1041 .Case("m0", AMDGPU::M0)
1042 .Case("exec", AMDGPU::EXEC)
1043 .Case("exec_lo", AMDGPU::EXEC_LO)
1044 .Case("exec_hi", AMDGPU::EXEC_HI)
1045 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1046 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1047 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1048 .Default(AMDGPU::NoRegister);
1049
1050 if (Reg == AMDGPU::NoRegister) {
1051 report_fatal_error(Twine("invalid register name \""
1052 + StringRef(RegName) + "\"."));
1053
1054 }
1055
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001056 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001057 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1058 report_fatal_error(Twine("invalid register \""
1059 + StringRef(RegName) + "\" for subtarget."));
1060 }
1061
1062 switch (Reg) {
1063 case AMDGPU::M0:
1064 case AMDGPU::EXEC_LO:
1065 case AMDGPU::EXEC_HI:
1066 case AMDGPU::FLAT_SCR_LO:
1067 case AMDGPU::FLAT_SCR_HI:
1068 if (VT.getSizeInBits() == 32)
1069 return Reg;
1070 break;
1071 case AMDGPU::EXEC:
1072 case AMDGPU::FLAT_SCR:
1073 if (VT.getSizeInBits() == 64)
1074 return Reg;
1075 break;
1076 default:
1077 llvm_unreachable("missing register type checking");
1078 }
1079
1080 report_fatal_error(Twine("invalid type for register \""
1081 + StringRef(RegName) + "\"."));
1082}
1083
Matt Arsenault786724a2016-07-12 21:41:32 +00001084// If kill is not the last instruction, split the block so kill is always a
1085// proper terminator.
1086MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI,
1087 MachineBasicBlock *BB) const {
1088 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1089
1090 MachineBasicBlock::iterator SplitPoint(&MI);
1091 ++SplitPoint;
1092
1093 if (SplitPoint == BB->end()) {
1094 // Don't bother with a new block.
1095 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1096 return BB;
1097 }
1098
1099 MachineFunction *MF = BB->getParent();
1100 MachineBasicBlock *SplitBB
1101 = MF->CreateMachineBasicBlock(BB->getBasicBlock());
1102
Matt Arsenault786724a2016-07-12 21:41:32 +00001103 MF->insert(++MachineFunction::iterator(BB), SplitBB);
1104 SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end());
1105
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001106 SplitBB->transferSuccessorsAndUpdatePHIs(BB);
Matt Arsenault786724a2016-07-12 21:41:32 +00001107 BB->addSuccessor(SplitBB);
1108
1109 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1110 return SplitBB;
1111}
1112
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001113// Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the
1114// wavefront. If the value is uniform and just happens to be in a VGPR, this
1115// will only do one iteration. In the worst case, this will loop 64 times.
1116//
1117// TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value.
1118static void emitLoadM0FromVGPRLoop(const SIInstrInfo *TII,
1119 MachineRegisterInfo &MRI,
1120 MachineBasicBlock &OrigBB,
1121 MachineBasicBlock &LoopBB,
1122 const DebugLoc &DL,
1123 MachineInstr *MovRel,
1124 const MachineOperand &IdxReg,
1125 unsigned InitReg,
1126 unsigned ResultReg,
1127 unsigned PhiReg,
1128 unsigned InitSaveExecReg,
1129 int Offset) {
1130 MachineBasicBlock::iterator I = LoopBB.begin();
1131
1132 unsigned PhiExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1133 unsigned NewExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1134 unsigned CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1135 unsigned CondReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1136
1137 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg)
1138 .addReg(InitReg)
1139 .addMBB(&OrigBB)
1140 .addReg(ResultReg)
1141 .addMBB(&LoopBB);
1142
1143 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec)
1144 .addReg(InitSaveExecReg)
1145 .addMBB(&OrigBB)
1146 .addReg(NewExec)
1147 .addMBB(&LoopBB);
1148
1149 // Read the next variant <- also loop target.
1150 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg)
1151 .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef()));
1152
1153 // Compare the just read M0 value to all possible Idx values.
1154 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg)
1155 .addReg(CurrentIdxReg)
Matt Arsenaultf0ba86a2016-07-21 09:40:57 +00001156 .addReg(IdxReg.getReg(), 0, IdxReg.getSubReg());
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001157
1158 // Move index from VCC into M0
1159 if (Offset == 0) {
1160 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1161 .addReg(CurrentIdxReg, RegState::Kill);
1162 } else {
1163 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1164 .addReg(CurrentIdxReg, RegState::Kill)
1165 .addImm(Offset);
1166 }
1167
1168 // Update EXEC, save the original EXEC value to VCC.
1169 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), NewExec)
1170 .addReg(CondReg, RegState::Kill);
1171
1172 MRI.setSimpleHint(NewExec, CondReg);
1173
1174 // Do the actual move.
1175 LoopBB.insert(I, MovRel);
1176
1177 // Update EXEC, switch all done bits to 0 and all todo bits to 1.
1178 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
1179 .addReg(AMDGPU::EXEC)
1180 .addReg(NewExec);
1181
1182 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use
1183 // s_cbranch_scc0?
1184
1185 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover.
1186 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
1187 .addMBB(&LoopBB);
1188}
1189
1190// This has slightly sub-optimal regalloc when the source vector is killed by
1191// the read. The register allocator does not understand that the kill is
1192// per-workitem, so is kept alive for the whole loop so we end up not re-using a
1193// subregister from it, using 1 more VGPR than necessary. This was saved when
1194// this was expanded after register allocation.
1195static MachineBasicBlock *loadM0FromVGPR(const SIInstrInfo *TII,
1196 MachineBasicBlock &MBB,
1197 MachineInstr &MI,
1198 MachineInstr *MovRel,
1199 unsigned InitResultReg,
1200 unsigned PhiReg,
1201 int Offset) {
1202 MachineFunction *MF = MBB.getParent();
1203 MachineRegisterInfo &MRI = MF->getRegInfo();
1204 const DebugLoc &DL = MI.getDebugLoc();
1205 MachineBasicBlock::iterator I(&MI);
1206
1207 unsigned DstReg = MI.getOperand(0).getReg();
1208 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1209 unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1210
1211 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec);
1212
1213 // Save the EXEC mask
1214 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec)
1215 .addReg(AMDGPU::EXEC);
1216
1217 // To insert the loop we need to split the block. Move everything after this
1218 // point to a new block, and insert a new empty block between the two.
1219 MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock();
1220 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock();
1221 MachineFunction::iterator MBBI(MBB);
1222 ++MBBI;
1223
1224 MF->insert(MBBI, LoopBB);
1225 MF->insert(MBBI, RemainderBB);
1226
1227 LoopBB->addSuccessor(LoopBB);
1228 LoopBB->addSuccessor(RemainderBB);
1229
1230 // Move the rest of the block into a new block.
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001231 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001232 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end());
1233
1234 MBB.addSuccessor(LoopBB);
1235
1236 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1237
1238 emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, MovRel, *Idx,
1239 InitResultReg, DstReg, PhiReg, TmpExec, Offset);
1240
1241 MachineBasicBlock::iterator First = RemainderBB->begin();
1242 BuildMI(*RemainderBB, First, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
1243 .addReg(SaveExec);
1244
1245 MI.eraseFromParent();
1246
1247 return RemainderBB;
1248}
1249
1250// Returns subreg index, offset
1251static std::pair<unsigned, int>
1252computeIndirectRegAndOffset(const SIRegisterInfo &TRI,
1253 const TargetRegisterClass *SuperRC,
1254 unsigned VecReg,
1255 int Offset) {
1256 int NumElts = SuperRC->getSize() / 4;
1257
1258 // Skip out of bounds offsets, or else we would end up using an undefined
1259 // register.
1260 if (Offset >= NumElts || Offset < 0)
1261 return std::make_pair(AMDGPU::sub0, Offset);
1262
1263 return std::make_pair(AMDGPU::sub0 + Offset, 0);
1264}
1265
1266// Return true if the index is an SGPR and was set.
1267static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII,
1268 MachineRegisterInfo &MRI,
1269 MachineInstr &MI,
1270 int Offset) {
1271 MachineBasicBlock *MBB = MI.getParent();
1272 const DebugLoc &DL = MI.getDebugLoc();
1273 MachineBasicBlock::iterator I(&MI);
1274
1275 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1276 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
1277
1278 assert(Idx->getReg() != AMDGPU::NoRegister);
1279
1280 if (!TII->getRegisterInfo().isSGPRClass(IdxRC))
1281 return false;
1282
1283 if (Offset == 0) {
1284 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1285 .addOperand(*Idx);
1286 } else {
1287 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1288 .addOperand(*Idx)
1289 .addImm(Offset);
1290 }
1291
1292 return true;
1293}
1294
1295// Control flow needs to be inserted if indexing with a VGPR.
1296static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI,
1297 MachineBasicBlock &MBB,
1298 const SIInstrInfo *TII) {
1299 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1300 MachineFunction *MF = MBB.getParent();
1301 MachineRegisterInfo &MRI = MF->getRegInfo();
1302
1303 unsigned Dst = MI.getOperand(0).getReg();
1304 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
1305 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1306
1307 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
1308
1309 unsigned SubReg;
1310 std::tie(SubReg, Offset)
1311 = computeIndirectRegAndOffset(TRI, VecRC, SrcVec->getReg(), Offset);
1312
1313 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset)) {
1314 MachineBasicBlock::iterator I(&MI);
1315 const DebugLoc &DL = MI.getDebugLoc();
1316
1317 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
1318 .addReg(SrcVec->getReg(), RegState::Undef, SubReg)
1319 .addReg(SrcVec->getReg(), RegState::Implicit);
1320 MI.eraseFromParent();
1321
1322 return &MBB;
1323 }
1324
1325 const DebugLoc &DL = MI.getDebugLoc();
1326 MachineBasicBlock::iterator I(&MI);
1327
1328 unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1329 unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1330
1331 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg);
1332
1333 MachineInstr *MovRel =
1334 BuildMI(*MF, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
1335 .addReg(SrcVec->getReg(), RegState::Undef, SubReg)
1336 .addReg(SrcVec->getReg(), RegState::Implicit);
1337
1338 return loadM0FromVGPR(TII, MBB, MI, MovRel, InitReg, PhiReg, Offset);
1339}
1340
1341static MachineBasicBlock *emitIndirectDst(MachineInstr &MI,
1342 MachineBasicBlock &MBB,
1343 const SIInstrInfo *TII) {
1344 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1345 MachineFunction *MF = MBB.getParent();
1346 MachineRegisterInfo &MRI = MF->getRegInfo();
1347
1348 unsigned Dst = MI.getOperand(0).getReg();
1349 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
1350 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1351 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val);
1352 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1353 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
1354
1355 // This can be an immediate, but will be folded later.
1356 assert(Val->getReg());
1357
1358 unsigned SubReg;
1359 std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC,
1360 SrcVec->getReg(),
1361 Offset);
1362 if (Idx->getReg() == AMDGPU::NoRegister) {
1363 MachineBasicBlock::iterator I(&MI);
1364 const DebugLoc &DL = MI.getDebugLoc();
1365
1366 assert(Offset == 0);
1367
1368 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst)
1369 .addOperand(*SrcVec)
1370 .addOperand(*Val)
1371 .addImm(SubReg);
1372
1373 MI.eraseFromParent();
1374 return &MBB;
1375 }
1376
1377 const MCInstrDesc &MovRelDesc = TII->get(AMDGPU::V_MOVRELD_B32_e32);
1378 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset)) {
1379 MachineBasicBlock::iterator I(&MI);
1380 const DebugLoc &DL = MI.getDebugLoc();
1381
1382 MachineInstr *MovRel =
1383 BuildMI(MBB, I, DL, MovRelDesc)
1384 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst
1385 .addOperand(*Val)
1386 .addReg(Dst, RegState::ImplicitDefine)
1387 .addReg(SrcVec->getReg(), RegState::Implicit);
1388
1389 const int ImpDefIdx = MovRelDesc.getNumOperands() +
1390 MovRelDesc.getNumImplicitUses();
1391 const int ImpUseIdx = ImpDefIdx + 1;
1392
1393 MovRel->tieOperands(ImpDefIdx, ImpUseIdx);
1394 MI.eraseFromParent();
1395 return &MBB;
1396 }
1397
1398 if (Val->isReg())
1399 MRI.clearKillFlags(Val->getReg());
1400
1401 const DebugLoc &DL = MI.getDebugLoc();
1402 unsigned PhiReg = MRI.createVirtualRegister(VecRC);
1403
1404 // vdst is not actually read and just provides the base register index.
1405 MachineInstr *MovRel =
1406 BuildMI(*MF, DL, MovRelDesc)
1407 .addReg(PhiReg, RegState::Undef, SubReg) // vdst
1408 .addOperand(*Val)
1409 .addReg(Dst, RegState::ImplicitDefine)
1410 .addReg(PhiReg, RegState::Implicit);
1411
1412 const int ImpDefIdx = MovRelDesc.getNumOperands() +
1413 MovRelDesc.getNumImplicitUses();
1414 const int ImpUseIdx = ImpDefIdx + 1;
1415
1416 MovRel->tieOperands(ImpDefIdx, ImpUseIdx);
1417
1418 return loadM0FromVGPR(TII, MBB, MI, MovRel,
1419 SrcVec->getReg(), PhiReg, Offset);
1420}
1421
Matt Arsenault786724a2016-07-12 21:41:32 +00001422MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter(
1423 MachineInstr &MI, MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001424 switch (MI.getOpcode()) {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001425 case AMDGPU::SI_INIT_M0: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001426 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001427 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001428 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001429 .addOperand(MI.getOperand(0));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001430 MI.eraseFromParent();
Matt Arsenault20711b72015-02-20 22:10:45 +00001431 return BB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001432 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001433 case AMDGPU::GET_GROUPSTATICSIZE: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001434 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1435
Changpeng Fang01f60622016-03-15 17:28:44 +00001436 MachineFunction *MF = BB->getParent();
1437 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001438 DebugLoc DL = MI.getDebugLoc();
Matt Arsenault3c07c812016-07-22 17:01:33 +00001439 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32))
1440 .addOperand(MI.getOperand(0))
Matt Arsenault52ef4012016-07-26 16:45:58 +00001441 .addImm(MFI->getLDSSize());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001442 MI.eraseFromParent();
Changpeng Fang01f60622016-03-15 17:28:44 +00001443 return BB;
1444 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001445 case AMDGPU::SI_INDIRECT_SRC_V1:
1446 case AMDGPU::SI_INDIRECT_SRC_V2:
1447 case AMDGPU::SI_INDIRECT_SRC_V4:
1448 case AMDGPU::SI_INDIRECT_SRC_V8:
1449 case AMDGPU::SI_INDIRECT_SRC_V16:
1450 return emitIndirectSrc(MI, *BB, getSubtarget()->getInstrInfo());
1451 case AMDGPU::SI_INDIRECT_DST_V1:
1452 case AMDGPU::SI_INDIRECT_DST_V2:
1453 case AMDGPU::SI_INDIRECT_DST_V4:
1454 case AMDGPU::SI_INDIRECT_DST_V8:
1455 case AMDGPU::SI_INDIRECT_DST_V16:
1456 return emitIndirectDst(MI, *BB, getSubtarget()->getInstrInfo());
Matt Arsenault786724a2016-07-12 21:41:32 +00001457 case AMDGPU::SI_KILL:
1458 return splitKillBlock(MI, BB);
Matt Arsenault22e41792016-08-27 01:00:37 +00001459 case AMDGPU::V_CNDMASK_B64_PSEUDO: {
1460 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
1461 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1462
1463 unsigned Dst = MI.getOperand(0).getReg();
1464 unsigned Src0 = MI.getOperand(1).getReg();
1465 unsigned Src1 = MI.getOperand(2).getReg();
1466 const DebugLoc &DL = MI.getDebugLoc();
1467 unsigned SrcCond = MI.getOperand(3).getReg();
1468
1469 unsigned DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1470 unsigned DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1471
1472 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo)
1473 .addReg(Src0, 0, AMDGPU::sub0)
1474 .addReg(Src1, 0, AMDGPU::sub0)
1475 .addReg(SrcCond);
1476 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi)
1477 .addReg(Src0, 0, AMDGPU::sub1)
1478 .addReg(Src1, 0, AMDGPU::sub1)
1479 .addReg(SrcCond);
1480
1481 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst)
1482 .addReg(DstLo)
1483 .addImm(AMDGPU::sub0)
1484 .addReg(DstHi)
1485 .addImm(AMDGPU::sub1);
1486 MI.eraseFromParent();
1487 return BB;
1488 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001489 default:
1490 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001491 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001492}
1493
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001494bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1495 // This currently forces unfolding various combinations of fsub into fma with
1496 // free fneg'd operands. As long as we have fast FMA (controlled by
1497 // isFMAFasterThanFMulAndFAdd), we should perform these.
1498
1499 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1500 // most of these combines appear to be cycle neutral but save on instruction
1501 // count / code size.
1502 return true;
1503}
1504
Mehdi Amini44ede332015-07-09 02:09:04 +00001505EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1506 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001507 if (!VT.isVector()) {
1508 return MVT::i1;
1509 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001510 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001511}
1512
Mehdi Aminieaabc512015-07-09 15:12:23 +00001513MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001514 return MVT::i32;
1515}
1516
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001517// Answering this is somewhat tricky and depends on the specific device which
1518// have different rates for fma or all f64 operations.
1519//
1520// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1521// regardless of which device (although the number of cycles differs between
1522// devices), so it is always profitable for f64.
1523//
1524// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1525// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1526// which we can always do even without fused FP ops since it returns the same
1527// result as the separate operations and since it is always full
1528// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1529// however does not support denormals, so we do report fma as faster if we have
1530// a fast fma device and require denormals.
1531//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001532bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1533 VT = VT.getScalarType();
1534
1535 if (!VT.isSimple())
1536 return false;
1537
1538 switch (VT.getSimpleVT().SimpleTy) {
1539 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001540 // This is as fast on some subtargets. However, we always have full rate f32
1541 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001542 // which we should prefer over fma. We can't use this if we want to support
1543 // denormals, so only report this in these cases.
1544 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001545 case MVT::f64:
1546 return true;
1547 default:
1548 break;
1549 }
1550
1551 return false;
1552}
1553
Tom Stellard75aadc22012-12-11 21:25:42 +00001554//===----------------------------------------------------------------------===//
1555// Custom DAG Lowering Operations
1556//===----------------------------------------------------------------------===//
1557
1558SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1559 switch (Op.getOpcode()) {
1560 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001561 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001562 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001563 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001564 SDValue Result = LowerLOAD(Op, DAG);
1565 assert((!Result.getNode() ||
1566 Result.getNode()->getNumValues() == 2) &&
1567 "Load should return a value and a chain");
1568 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001569 }
Tom Stellardaf775432013-10-23 00:44:32 +00001570
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001571 case ISD::FSIN:
1572 case ISD::FCOS:
1573 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001574 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001575 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard354a43c2016-04-01 18:27:37 +00001576 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001577 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001578 case ISD::GlobalAddress: {
1579 MachineFunction &MF = DAG.getMachineFunction();
1580 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1581 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001582 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001583 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001584 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001585 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Matt Arsenault99c14522016-04-25 19:27:24 +00001586 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG);
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001587 case ISD::TRAP: return lowerTRAP(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001588 }
1589 return SDValue();
1590}
1591
Tom Stellardf8794352012-12-19 22:10:31 +00001592/// \brief Helper function for LowerBRCOND
1593static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001594
Tom Stellardf8794352012-12-19 22:10:31 +00001595 SDNode *Parent = Value.getNode();
1596 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1597 I != E; ++I) {
1598
1599 if (I.getUse().get() != Value)
1600 continue;
1601
1602 if (I->getOpcode() == Opcode)
1603 return *I;
1604 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001605 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001606}
1607
Tom Stellardb02094e2014-07-21 15:45:01 +00001608SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
1609
Tom Stellardc98ee202015-07-16 19:40:07 +00001610 SDLoc SL(Op);
Tom Stellardb02094e2014-07-21 15:45:01 +00001611 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
1612 unsigned FrameIndex = FINode->getIndex();
1613
Matt Arsenault3a619852016-02-27 20:26:57 +00001614 // A FrameIndex node represents a 32-bit offset into scratch memory. If the
1615 // high bit of a frame index offset were to be set, this would mean that it
1616 // represented an offset of ~2GB * 64 = ~128GB from the start of the scratch
1617 // buffer, with 64 being the number of threads per wave.
Tom Stellardc98ee202015-07-16 19:40:07 +00001618 //
Matt Arsenault3a619852016-02-27 20:26:57 +00001619 // The maximum private allocation for the entire GPU is 4G, and we are
1620 // concerned with the largest the index could ever be for an individual
1621 // workitem. This will occur with the minmum dispatch size. If a program
1622 // requires more, the dispatch size will be reduced.
1623 //
1624 // With this limit, we can mark the high bit of the FrameIndex node as known
1625 // zero, which is important, because it means in most situations we can prove
1626 // that values derived from FrameIndex nodes are non-negative. This enables us
1627 // to take advantage of more addressing modes when accessing scratch buffers,
1628 // since for scratch reads/writes, the register offset must always be
1629 // positive.
1630
1631 uint64_t MaxGPUAlloc = UINT64_C(4) * 1024 * 1024 * 1024;
1632
1633 // XXX - It is unclear if partial dispatch works. Assume it works at half wave
1634 // granularity. It is probably a full wave.
1635 uint64_t MinGranularity = 32;
1636
1637 unsigned KnownBits = Log2_64(MaxGPUAlloc / MinGranularity);
1638 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), KnownBits);
Tom Stellardc98ee202015-07-16 19:40:07 +00001639
1640 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
Tom Stellardc98ee202015-07-16 19:40:07 +00001641 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
Matt Arsenault3a619852016-02-27 20:26:57 +00001642 DAG.getValueType(ExtVT));
Tom Stellardb02094e2014-07-21 15:45:01 +00001643}
1644
Tom Stellardbc4497b2016-02-12 23:45:29 +00001645bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault16f48d72016-02-13 00:36:10 +00001646 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN)
Tom Stellardbc4497b2016-02-12 23:45:29 +00001647 return false;
1648
1649 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1650 default: return false;
1651 case AMDGPUIntrinsic::amdgcn_if:
1652 case AMDGPUIntrinsic::amdgcn_else:
Matt Arsenault48d70cb2016-07-09 17:18:39 +00001653 case AMDGPUIntrinsic::amdgcn_break:
Tom Stellardbc4497b2016-02-12 23:45:29 +00001654 case AMDGPUIntrinsic::amdgcn_if_break:
1655 case AMDGPUIntrinsic::amdgcn_else_break:
1656 case AMDGPUIntrinsic::amdgcn_loop:
1657 case AMDGPUIntrinsic::amdgcn_end_cf:
1658 return true;
1659 }
1660}
1661
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001662void SITargetLowering::createDebuggerPrologueStackObjects(
1663 MachineFunction &MF) const {
1664 // Create stack objects that are used for emitting debugger prologue.
1665 //
1666 // Debugger prologue writes work group IDs and work item IDs to scratch memory
1667 // at fixed location in the following format:
1668 // offset 0: work group ID x
1669 // offset 4: work group ID y
1670 // offset 8: work group ID z
1671 // offset 16: work item ID x
1672 // offset 20: work item ID y
1673 // offset 24: work item ID z
1674 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1675 int ObjectIdx = 0;
1676
1677 // For each dimension:
1678 for (unsigned i = 0; i < 3; ++i) {
1679 // Create fixed stack object for work group ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001680 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001681 Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx);
1682 // Create fixed stack object for work item ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001683 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4 + 16, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001684 Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx);
1685 }
1686}
1687
Tom Stellardf8794352012-12-19 22:10:31 +00001688/// This transforms the control flow intrinsics to get the branch destination as
1689/// last parameter, also switches branch target with BR if the need arise
1690SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1691 SelectionDAG &DAG) const {
1692
Andrew Trickef9de2a2013-05-25 02:42:55 +00001693 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001694
1695 SDNode *Intr = BRCOND.getOperand(1).getNode();
1696 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001697 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001698 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001699
1700 if (Intr->getOpcode() == ISD::SETCC) {
1701 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001702 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001703 Intr = SetCC->getOperand(0).getNode();
1704
1705 } else {
1706 // Get the target from BR if we don't negate the condition
1707 BR = findUser(BRCOND, ISD::BR);
1708 Target = BR->getOperand(1);
1709 }
1710
Nicolai Haehnleffbd56a2016-05-05 17:36:36 +00001711 if (!isCFIntrinsic(Intr)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +00001712 // This is a uniform branch so we don't need to legalize.
1713 return BRCOND;
1714 }
1715
1716 assert(!SetCC ||
1717 (SetCC->getConstantOperandVal(1) == 1 &&
Tom Stellardbc4497b2016-02-12 23:45:29 +00001718 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1719 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00001720
1721 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001722 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001723
1724 // operands of the new intrinsic call
1725 SmallVector<SDValue, 4> Ops;
1726 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001727 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001728 Ops.push_back(Target);
1729
1730 // build the new intrinsic call
1731 SDNode *Result = DAG.getNode(
1732 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001733 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001734
1735 if (BR) {
1736 // Give the branch instruction our target
1737 SDValue Ops[] = {
1738 BR->getOperand(0),
1739 BRCOND.getOperand(2)
1740 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001741 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1742 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1743 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001744 }
1745
1746 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1747
1748 // Copy the intrinsic results to registers
1749 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1750 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1751 if (!CopyToReg)
1752 continue;
1753
1754 Chain = DAG.getCopyToReg(
1755 Chain, DL,
1756 CopyToReg->getOperand(1),
1757 SDValue(Result, i - 1),
1758 SDValue());
1759
1760 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1761 }
1762
1763 // Remove the old intrinsic from the chain
1764 DAG.ReplaceAllUsesOfValueWith(
1765 SDValue(Intr, Intr->getNumValues() - 1),
1766 Intr->getOperand(0));
1767
1768 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001769}
1770
Matt Arsenault99c14522016-04-25 19:27:24 +00001771SDValue SITargetLowering::getSegmentAperture(unsigned AS,
1772 SelectionDAG &DAG) const {
1773 SDLoc SL;
1774 MachineFunction &MF = DAG.getMachineFunction();
1775 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00001776 unsigned UserSGPR = Info->getQueuePtrUserSGPR();
1777 assert(UserSGPR != AMDGPU::NoRegister);
1778
Matt Arsenault99c14522016-04-25 19:27:24 +00001779 SDValue QueuePtr = CreateLiveInRegister(
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00001780 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
Matt Arsenault99c14522016-04-25 19:27:24 +00001781
1782 // Offset into amd_queue_t for group_segment_aperture_base_hi /
1783 // private_segment_aperture_base_hi.
1784 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44;
1785
1786 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr,
1787 DAG.getConstant(StructOffset, SL, MVT::i64));
1788
1789 // TODO: Use custom target PseudoSourceValue.
1790 // TODO: We should use the value from the IR intrinsic call, but it might not
1791 // be available and how do we get it?
1792 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()),
1793 AMDGPUAS::CONSTANT_ADDRESS));
1794
1795 MachinePointerInfo PtrInfo(V, StructOffset);
Justin Lebar9c375812016-07-15 18:27:10 +00001796 return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr, PtrInfo,
1797 MinAlign(64, StructOffset),
Justin Lebaradbf09e2016-09-11 01:38:58 +00001798 MachineMemOperand::MODereferenceable |
1799 MachineMemOperand::MOInvariant);
Matt Arsenault99c14522016-04-25 19:27:24 +00001800}
1801
1802SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
1803 SelectionDAG &DAG) const {
1804 SDLoc SL(Op);
1805 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op);
1806
1807 SDValue Src = ASC->getOperand(0);
1808
1809 // FIXME: Really support non-0 null pointers.
1810 SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32);
1811 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
1812
1813 // flat -> local/private
1814 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
1815 if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1816 ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1817 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
1818 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
1819
1820 return DAG.getNode(ISD::SELECT, SL, MVT::i32,
1821 NonNull, Ptr, SegmentNullPtr);
1822 }
1823 }
1824
1825 // local/private -> flat
1826 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
1827 if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1828 ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1829 SDValue NonNull
1830 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
1831
1832 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG);
1833 SDValue CvtPtr
1834 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
1835
1836 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull,
1837 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr),
1838 FlatNullPtr);
1839 }
1840 }
1841
1842 // global <-> flat are no-ops and never emitted.
1843
1844 const MachineFunction &MF = DAG.getMachineFunction();
1845 DiagnosticInfoUnsupported InvalidAddrSpaceCast(
1846 *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc());
1847 DAG.getContext()->diagnose(InvalidAddrSpaceCast);
1848
1849 return DAG.getUNDEF(ASC->getValueType(0));
1850}
1851
Tom Stellard418beb72016-07-13 14:23:33 +00001852static bool shouldEmitGOTReloc(const GlobalValue *GV,
1853 const TargetMachine &TM) {
1854 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
1855 !TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
Tom Stellardb164a982016-06-25 01:59:16 +00001856}
1857
Tom Stellard418beb72016-07-13 14:23:33 +00001858bool
1859SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1860 // We can fold offsets for anything that doesn't require a GOT relocation.
1861 return GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
1862 !shouldEmitGOTReloc(GA->getGlobal(), getTargetMachine());
1863}
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001864
Tom Stellard418beb72016-07-13 14:23:33 +00001865static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV,
1866 SDLoc DL, unsigned Offset, EVT PtrVT,
1867 unsigned GAFlags = SIInstrInfo::MO_NONE) {
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001868 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is
1869 // lowered to the following code sequence:
1870 // s_getpc_b64 s[0:1]
1871 // s_add_u32 s0, s0, $symbol
1872 // s_addc_u32 s1, s1, 0
1873 //
1874 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
1875 // a fixup or relocation is emitted to replace $symbol with a literal
1876 // constant, which is a pc-relative offset from the encoding of the $symbol
1877 // operand to the global variable.
1878 //
1879 // What we want here is an offset from the value returned by s_getpc
1880 // (which is the address of the s_add_u32 instruction) to the global
1881 // variable, but since the encoding of $symbol starts 4 bytes after the start
1882 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too
1883 // small. This requires us to add 4 to the global variable offset in order to
1884 // compute the correct address.
Tom Stellard418beb72016-07-13 14:23:33 +00001885 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
1886 GAFlags);
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001887 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, GA);
1888}
1889
Tom Stellard418beb72016-07-13 14:23:33 +00001890SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1891 SDValue Op,
1892 SelectionDAG &DAG) const {
1893 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1894
1895 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS &&
1896 GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS)
1897 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1898
1899 SDLoc DL(GSD);
1900 const GlobalValue *GV = GSD->getGlobal();
1901 EVT PtrVT = Op.getValueType();
1902
1903 if (!shouldEmitGOTReloc(GV, getTargetMachine()))
1904 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT);
1905
1906 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT,
1907 SIInstrInfo::MO_GOTPCREL);
1908
1909 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext());
1910 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
1911 const DataLayout &DataLayout = DAG.getDataLayout();
1912 unsigned Align = DataLayout.getABITypeAlignment(PtrTy);
1913 // FIXME: Use a PseudoSourceValue once those can be assigned an address space.
1914 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
1915
Justin Lebar9c375812016-07-15 18:27:10 +00001916 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align,
Justin Lebaradbf09e2016-09-11 01:38:58 +00001917 MachineMemOperand::MODereferenceable |
1918 MachineMemOperand::MOInvariant);
Tom Stellard418beb72016-07-13 14:23:33 +00001919}
1920
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001921SDValue SITargetLowering::lowerTRAP(SDValue Op,
1922 SelectionDAG &DAG) const {
1923 const MachineFunction &MF = DAG.getMachineFunction();
1924 DiagnosticInfoUnsupported NoTrap(*MF.getFunction(),
1925 "trap handler not supported",
1926 Op.getDebugLoc(),
1927 DS_Warning);
1928 DAG.getContext()->diagnose(NoTrap);
1929
1930 // Emit s_endpgm.
1931
1932 // FIXME: This should really be selected to s_trap, but that requires
1933 // setting up the trap handler for it o do anything.
Matt Arsenault9babdf42016-06-22 20:15:28 +00001934 return DAG.getNode(AMDGPUISD::ENDPGM, SDLoc(Op), MVT::Other,
1935 Op.getOperand(0));
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001936}
1937
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001938SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain,
1939 const SDLoc &DL, SDValue V) const {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001940 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
1941 // the destination register.
1942 //
Tom Stellardfc92e772015-05-12 14:18:14 +00001943 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1944 // so we will end up with redundant moves to m0.
1945 //
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001946 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
1947
1948 // A Null SDValue creates a glue result.
1949 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
1950 V, Chain);
1951 return SDValue(M0, 0);
Tom Stellardfc92e772015-05-12 14:18:14 +00001952}
1953
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001954SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1955 SDValue Op,
1956 MVT VT,
1957 unsigned Offset) const {
1958 SDLoc SL(Op);
1959 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1960 DAG.getEntryNode(), Offset, false);
1961 // The local size values will have the hi 16-bits as zero.
1962 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1963 DAG.getValueType(VT));
1964}
1965
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001966static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
Matt Arsenaulte0132462016-01-30 05:19:45 +00001967 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001968 "non-hsa intrinsic with hsa target",
1969 DL.getDebugLoc());
1970 DAG.getContext()->diagnose(BadIntrin);
1971 return DAG.getUNDEF(VT);
1972}
1973
1974static SDValue emitRemovedIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
1975 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
1976 "intrinsic not supported on subtarget",
1977 DL.getDebugLoc());
Matt Arsenaulte0132462016-01-30 05:19:45 +00001978 DAG.getContext()->diagnose(BadIntrin);
1979 return DAG.getUNDEF(VT);
1980}
1981
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001982SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1983 SelectionDAG &DAG) const {
1984 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001985 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001986 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001987
1988 EVT VT = Op.getValueType();
1989 SDLoc DL(Op);
1990 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1991
Sanjay Patela2607012015-09-16 16:31:21 +00001992 // TODO: Should this propagate fast-math-flags?
1993
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001994 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001995 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault48ab5262016-04-25 19:27:18 +00001996 case Intrinsic::amdgcn_queue_ptr: {
Tom Stellard0b76fc4c2016-09-16 21:34:26 +00001997 if (!Subtarget->isAmdCodeObjectV2()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00001998 DiagnosticInfoUnsupported BadIntrin(
1999 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
2000 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00002001 DAG.getContext()->diagnose(BadIntrin);
2002 return DAG.getUNDEF(VT);
2003 }
2004
Matt Arsenault48ab5262016-04-25 19:27:18 +00002005 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ?
2006 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00002007 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
Matt Arsenault48ab5262016-04-25 19:27:18 +00002008 TRI->getPreloadedValue(MF, Reg), VT);
2009 }
Jan Veselyfea814d2016-06-21 20:46:20 +00002010 case Intrinsic::amdgcn_implicitarg_ptr: {
2011 unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
2012 return LowerParameterPtr(DAG, DL, DAG.getEntryNode(), offset);
2013 }
Matt Arsenaultdc4ebad2016-04-29 21:16:52 +00002014 case Intrinsic::amdgcn_kernarg_segment_ptr: {
2015 unsigned Reg
2016 = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
2017 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
2018 }
Matt Arsenault8d718dc2016-07-22 17:01:30 +00002019 case Intrinsic::amdgcn_dispatch_id: {
2020 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_ID);
2021 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
2022 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002023 case Intrinsic::amdgcn_rcp:
2024 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
2025 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00002026 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002027 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002028 case Intrinsic::amdgcn_rsq_legacy: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002029 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002030 return emitRemovedIntrinsicError(DAG, DL, VT);
2031
2032 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
2033 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002034 case Intrinsic::amdgcn_rcp_legacy: {
2035 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
2036 return emitRemovedIntrinsicError(DAG, DL, VT);
2037 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1));
2038 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +00002039 case Intrinsic::amdgcn_rsq_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002040 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00002041 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00002042
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002043 Type *Type = VT.getTypeForEVT(*DAG.getContext());
2044 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
2045 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
2046
2047 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
2048 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
2049 DAG.getConstantFP(Max, DL, VT));
2050 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
2051 DAG.getConstantFP(Min, DL, VT));
2052 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002053 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002054 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002055 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002056
Tom Stellardec2e43c2014-09-22 15:35:29 +00002057 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2058 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002059 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002060 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002061 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002062
Tom Stellardec2e43c2014-09-22 15:35:29 +00002063 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2064 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002065 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002066 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002067 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002068
Tom Stellardec2e43c2014-09-22 15:35:29 +00002069 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2070 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002071 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002072 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002073 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002074
Tom Stellardec2e43c2014-09-22 15:35:29 +00002075 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2076 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002077 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002078 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002079 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002080
Tom Stellardec2e43c2014-09-22 15:35:29 +00002081 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2082 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002083 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002084 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002085 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002086
Tom Stellardec2e43c2014-09-22 15:35:29 +00002087 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2088 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002089 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002090 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002091 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002092
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002093 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2094 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002095 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002096 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002097 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002098
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002099 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2100 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002101 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002102 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002103 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002104
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002105 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2106 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenault43976df2016-01-30 04:25:19 +00002107 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002108 case Intrinsic::r600_read_tgid_x:
2109 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002110 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002111 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002112 case Intrinsic::r600_read_tgid_y:
2113 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002114 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002115 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002116 case Intrinsic::r600_read_tgid_z:
2117 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002118 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002119 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002120 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002121 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002122 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002123 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002124 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002125 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002126 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002127 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002128 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002129 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002130 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002131 case AMDGPUIntrinsic::SI_load_const: {
2132 SDValue Ops[] = {
2133 Op.getOperand(1),
2134 Op.getOperand(2)
2135 };
2136
2137 MachineMemOperand *MMO = MF.getMachineMemOperand(
Justin Lebaradbf09e2016-09-11 01:38:58 +00002138 MachinePointerInfo(),
2139 MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable |
2140 MachineMemOperand::MOInvariant,
2141 VT.getStoreSize(), 4);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002142 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
2143 Op->getVTList(), Ops, VT, MMO);
2144 }
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002145 case AMDGPUIntrinsic::amdgcn_fdiv_fast: {
2146 return lowerFDIV_FAST(Op, DAG);
2147 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002148 case AMDGPUIntrinsic::SI_vs_load_input:
2149 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
2150 Op.getOperand(1),
2151 Op.getOperand(2),
2152 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00002153
Tom Stellard2a9d9472015-05-12 15:00:46 +00002154 case AMDGPUIntrinsic::SI_fs_constant: {
2155 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2156 SDValue Glue = M0.getValue(1);
2157 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
2158 DAG.getConstant(2, DL, MVT::i32), // P0
2159 Op.getOperand(1), Op.getOperand(2), Glue);
2160 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00002161 case AMDGPUIntrinsic::SI_packf16:
2162 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
2163 return DAG.getUNDEF(MVT::i32);
2164 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00002165 case AMDGPUIntrinsic::SI_fs_interp: {
2166 SDValue IJ = Op.getOperand(4);
2167 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2168 DAG.getConstant(0, DL, MVT::i32));
2169 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2170 DAG.getConstant(1, DL, MVT::i32));
2171 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2172 SDValue Glue = M0.getValue(1);
2173 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
2174 DAG.getVTList(MVT::f32, MVT::Glue),
2175 I, Op.getOperand(1), Op.getOperand(2), Glue);
2176 Glue = SDValue(P1.getNode(), 1);
2177 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
2178 Op.getOperand(1), Op.getOperand(2), Glue);
2179 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00002180 case Intrinsic::amdgcn_interp_p1: {
2181 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
2182 SDValue Glue = M0.getValue(1);
2183 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
2184 Op.getOperand(2), Op.getOperand(3), Glue);
2185 }
2186 case Intrinsic::amdgcn_interp_p2: {
2187 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
2188 SDValue Glue = SDValue(M0.getNode(), 1);
2189 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
2190 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
2191 Glue);
2192 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002193 case Intrinsic::amdgcn_sin:
2194 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
2195
2196 case Intrinsic::amdgcn_cos:
2197 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
2198
2199 case Intrinsic::amdgcn_log_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002200 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002201 return SDValue();
2202
2203 DiagnosticInfoUnsupported BadIntrin(
2204 *MF.getFunction(), "intrinsic not supported on subtarget",
2205 DL.getDebugLoc());
2206 DAG.getContext()->diagnose(BadIntrin);
2207 return DAG.getUNDEF(VT);
2208 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002209 case Intrinsic::amdgcn_ldexp:
2210 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
2211 Op.getOperand(1), Op.getOperand(2));
Matt Arsenault74015162016-05-28 00:19:52 +00002212
2213 case Intrinsic::amdgcn_fract:
2214 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
2215
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002216 case Intrinsic::amdgcn_class:
2217 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
2218 Op.getOperand(1), Op.getOperand(2));
2219 case Intrinsic::amdgcn_div_fmas:
2220 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
2221 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
2222 Op.getOperand(4));
2223
2224 case Intrinsic::amdgcn_div_fixup:
2225 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
2226 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
2227
2228 case Intrinsic::amdgcn_trig_preop:
2229 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
2230 Op.getOperand(1), Op.getOperand(2));
2231 case Intrinsic::amdgcn_div_scale: {
2232 // 3rd parameter required to be a constant.
2233 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2234 if (!Param)
2235 return DAG.getUNDEF(VT);
2236
2237 // Translate to the operands expected by the machine instruction. The
2238 // first parameter must be the same as the first instruction.
2239 SDValue Numerator = Op.getOperand(1);
2240 SDValue Denominator = Op.getOperand(2);
2241
2242 // Note this order is opposite of the machine instruction's operations,
2243 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
2244 // intrinsic has the numerator as the first operand to match a normal
2245 // division operation.
2246
2247 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
2248
2249 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
2250 Denominator, Numerator);
2251 }
Wei Ding07e03712016-07-28 16:42:13 +00002252 case Intrinsic::amdgcn_icmp: {
2253 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2254 int CondCode = CD->getSExtValue();
2255
2256 if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE ||
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002257 CondCode >= ICmpInst::Predicate::BAD_ICMP_PREDICATE)
Wei Ding07e03712016-07-28 16:42:13 +00002258 return DAG.getUNDEF(VT);
2259
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002260 ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode);
Wei Ding07e03712016-07-28 16:42:13 +00002261 ISD::CondCode CCOpcode = getICmpCondCode(IcInput);
2262 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2263 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2264 }
2265 case Intrinsic::amdgcn_fcmp: {
2266 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2267 int CondCode = CD->getSExtValue();
2268
2269 if (CondCode <= FCmpInst::Predicate::FCMP_FALSE ||
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002270 CondCode >= FCmpInst::Predicate::FCMP_TRUE)
Wei Ding07e03712016-07-28 16:42:13 +00002271 return DAG.getUNDEF(VT);
2272
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002273 FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode);
Wei Ding07e03712016-07-28 16:42:13 +00002274 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput);
2275 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2276 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2277 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002278 case Intrinsic::amdgcn_fmul_legacy:
2279 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT,
2280 Op.getOperand(1), Op.getOperand(2));
Matt Arsenaultc96e1de2016-07-18 18:35:05 +00002281 case Intrinsic::amdgcn_sffbh:
2282 case AMDGPUIntrinsic::AMDGPU_flbit_i32: // Legacy name.
2283 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002284 default:
2285 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
2286 }
2287}
2288
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002289SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
2290 SelectionDAG &DAG) const {
2291 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2292 switch (IntrID) {
2293 case Intrinsic::amdgcn_atomic_inc:
2294 case Intrinsic::amdgcn_atomic_dec: {
2295 MemSDNode *M = cast<MemSDNode>(Op);
2296 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
2297 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
2298 SDValue Ops[] = {
2299 M->getOperand(0), // Chain
2300 M->getOperand(2), // Ptr
2301 M->getOperand(3) // Value
2302 };
2303
2304 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
2305 M->getMemoryVT(), M->getMemOperand());
2306 }
2307 default:
2308 return SDValue();
2309 }
2310}
2311
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002312SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
2313 SelectionDAG &DAG) const {
2314 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00002315 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002316 SDValue Chain = Op.getOperand(0);
2317 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2318
2319 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00002320 case AMDGPUIntrinsic::SI_sendmsg: {
2321 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
2322 SDValue Glue = Chain.getValue(1);
2323 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
2324 Op.getOperand(2), Glue);
2325 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002326 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002327 SDValue Ops[] = {
2328 Chain,
2329 Op.getOperand(2),
2330 Op.getOperand(3),
2331 Op.getOperand(4),
2332 Op.getOperand(5),
2333 Op.getOperand(6),
2334 Op.getOperand(7),
2335 Op.getOperand(8),
2336 Op.getOperand(9),
2337 Op.getOperand(10),
2338 Op.getOperand(11),
2339 Op.getOperand(12),
2340 Op.getOperand(13),
2341 Op.getOperand(14)
2342 };
2343
2344 EVT VT = Op.getOperand(3).getValueType();
2345
2346 MachineMemOperand *MMO = MF.getMachineMemOperand(
2347 MachinePointerInfo(),
2348 MachineMemOperand::MOStore,
2349 VT.getStoreSize(), 4);
2350 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
2351 Op->getVTList(), Ops, VT, MMO);
2352 }
Matt Arsenault00568682016-07-13 06:04:22 +00002353 case AMDGPUIntrinsic::AMDGPU_kill: {
Matt Arsenault03006fd2016-07-19 16:27:56 +00002354 SDValue Src = Op.getOperand(2);
2355 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) {
Matt Arsenault00568682016-07-13 06:04:22 +00002356 if (!K->isNegative())
2357 return Chain;
Matt Arsenault03006fd2016-07-19 16:27:56 +00002358
2359 SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32);
2360 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne);
Matt Arsenault00568682016-07-13 06:04:22 +00002361 }
2362
Matt Arsenault03006fd2016-07-19 16:27:56 +00002363 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src);
2364 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast);
Matt Arsenault00568682016-07-13 06:04:22 +00002365 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002366 default:
2367 return SDValue();
2368 }
2369}
2370
Tom Stellard81d871d2013-11-13 23:36:50 +00002371SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2372 SDLoc DL(Op);
2373 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00002374 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00002375 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00002376
Matt Arsenaulta1436412016-02-10 18:21:45 +00002377 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
2378 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected");
Matt Arsenault6dfda962016-02-10 18:21:39 +00002379 // FIXME: Copied from PPC
2380 // First, load into 32 bits, then truncate to 1 bit.
2381
2382 SDValue Chain = Load->getChain();
2383 SDValue BasePtr = Load->getBasePtr();
2384 MachineMemOperand *MMO = Load->getMemOperand();
2385
2386 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
2387 BasePtr, MVT::i8, MMO);
2388
2389 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002390 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00002391 NewLD.getValue(1)
2392 };
2393
2394 return DAG.getMergeValues(Ops, DL);
2395 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002396
Matt Arsenaulta1436412016-02-10 18:21:45 +00002397 if (!MemVT.isVector())
2398 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002399
Matt Arsenaulta1436412016-02-10 18:21:45 +00002400 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
2401 "Custom lowering for non-i32 vectors hasn't been implemented.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002402
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002403 unsigned AS = Load->getAddressSpace();
2404 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT,
2405 AS, Load->getAlignment())) {
2406 SDValue Ops[2];
2407 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG);
2408 return DAG.getMergeValues(Ops, DL);
2409 }
2410
2411 unsigned NumElements = MemVT.getVectorNumElements();
2412 switch (AS) {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002413 case AMDGPUAS::CONSTANT_ADDRESS:
2414 if (isMemOpUniform(Load))
2415 return SDValue();
2416 // Non-uniform loads will be selected to MUBUF instructions, so they
2417 // have the same legalization requires ments as global and private
2418 // loads.
2419 //
Justin Bognerb03fd122016-08-17 05:10:15 +00002420 LLVM_FALLTHROUGH;
Matt Arsenaulta1436412016-02-10 18:21:45 +00002421 case AMDGPUAS::GLOBAL_ADDRESS:
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002422 case AMDGPUAS::FLAT_ADDRESS:
2423 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00002424 return SplitVectorLoad(Op, DAG);
2425 // v4 loads are supported for private and global memory.
2426 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002427 case AMDGPUAS::PRIVATE_ADDRESS: {
2428 // Depending on the setting of the private_element_size field in the
2429 // resource descriptor, we can only make private accesses up to a certain
2430 // size.
2431 switch (Subtarget->getMaxPrivateElementSize()) {
2432 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002433 return scalarizeVectorLoad(Load, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002434 case 8:
2435 if (NumElements > 2)
2436 return SplitVectorLoad(Op, DAG);
2437 return SDValue();
2438 case 16:
2439 // Same as global/flat
2440 if (NumElements > 4)
2441 return SplitVectorLoad(Op, DAG);
2442 return SDValue();
2443 default:
2444 llvm_unreachable("unsupported private_element_size");
2445 }
2446 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002447 case AMDGPUAS::LOCAL_ADDRESS: {
2448 if (NumElements > 2)
2449 return SplitVectorLoad(Op, DAG);
2450
2451 if (NumElements == 2)
2452 return SDValue();
2453
Matt Arsenaulta1436412016-02-10 18:21:45 +00002454 // If properly aligned, if we split we might be able to use ds_read_b64.
2455 return SplitVectorLoad(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002456 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00002457 default:
2458 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00002459 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002460}
2461
Tom Stellard0ec134f2014-02-04 17:18:40 +00002462SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2463 if (Op.getValueType() != MVT::i64)
2464 return SDValue();
2465
2466 SDLoc DL(Op);
2467 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002468
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002469 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2470 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002471
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002472 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
2473 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
2474
2475 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
2476 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002477
2478 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
2479
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002480 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
2481 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002482
2483 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
2484
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002485 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002486 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002487}
2488
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002489// Catch division cases where we can use shortcuts with rcp and rsq
2490// instructions.
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002491SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op,
2492 SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002493 SDLoc SL(Op);
2494 SDValue LHS = Op.getOperand(0);
2495 SDValue RHS = Op.getOperand(1);
2496 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002497 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002498
2499 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault979902b2016-08-02 22:25:04 +00002500 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals()))) {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002501
Matt Arsenault979902b2016-08-02 22:25:04 +00002502 if (CLHS->isExactlyValue(1.0)) {
2503 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
2504 // the CI documentation has a worst case error of 1 ulp.
2505 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
2506 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002507
Matt Arsenault979902b2016-08-02 22:25:04 +00002508 // 1.0 / sqrt(x) -> rsq(x)
2509 //
2510 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
2511 // error seems really high at 2^29 ULP.
2512 if (RHS.getOpcode() == ISD::FSQRT)
2513 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
2514
2515 // 1.0 / x -> rcp(x)
2516 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
2517 }
2518
2519 // Same as for 1.0, but expand the sign out of the constant.
2520 if (CLHS->isExactlyValue(-1.0)) {
2521 // -1.0 / x -> rcp (fneg x)
2522 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2523 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS);
2524 }
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002525 }
2526 }
2527
Wei Dinged0f97f2016-06-09 19:17:15 +00002528 const SDNodeFlags *Flags = Op->getFlags();
2529
2530 if (Unsafe || Flags->hasAllowReciprocal()) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002531 // Turn into multiply by the reciprocal.
2532 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00002533 SDNodeFlags Flags;
2534 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002535 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00002536 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002537 }
2538
2539 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002540}
2541
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002542// Faster 2.5 ULP division that does not support denormals.
2543SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const {
2544 SDLoc SL(Op);
2545 SDValue LHS = Op.getOperand(1);
2546 SDValue RHS = Op.getOperand(2);
2547
2548 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
2549
2550 const APFloat K0Val(BitsToFloat(0x6f800000));
2551 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
2552
2553 const APFloat K1Val(BitsToFloat(0x2f800000));
2554 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
2555
2556 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
2557
2558 EVT SetCCVT =
2559 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
2560
2561 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
2562
2563 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
2564
2565 // TODO: Should this propagate fast-math-flags?
2566 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
2567
2568 // rcp does not support denormals.
2569 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
2570
2571 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
2572
2573 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
2574}
2575
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002576SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002577 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
Eric Christopher538d09d02016-06-07 20:27:12 +00002578 return FastLowered;
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002579
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002580 SDLoc SL(Op);
2581 SDValue LHS = Op.getOperand(0);
2582 SDValue RHS = Op.getOperand(1);
2583
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002584 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002585
Wei Dinged0f97f2016-06-09 19:17:15 +00002586 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002587
Wei Dinged0f97f2016-06-09 19:17:15 +00002588 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, RHS, RHS, LHS);
2589 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, LHS, RHS, LHS);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002590
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002591 // Denominator is scaled to not be denormal, so using rcp is ok.
Wei Dinged0f97f2016-06-09 19:17:15 +00002592 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002593
Wei Dinged0f97f2016-06-09 19:17:15 +00002594 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002595
Wei Dinged0f97f2016-06-09 19:17:15 +00002596 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, ApproxRcp, One);
2597 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, ApproxRcp);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002598
Wei Dinged0f97f2016-06-09 19:17:15 +00002599 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, NumeratorScaled, Fma1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002600
Wei Dinged0f97f2016-06-09 19:17:15 +00002601 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, NumeratorScaled);
2602 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul);
2603 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, NumeratorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002604
Wei Dinged0f97f2016-06-09 19:17:15 +00002605 SDValue Scale = NumeratorScaled.getValue(1);
2606 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, Fma4, Fma1, Fma3, Scale);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002607
Wei Dinged0f97f2016-06-09 19:17:15 +00002608 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002609}
2610
2611SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002612 if (DAG.getTarget().Options.UnsafeFPMath)
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002613 return lowerFastUnsafeFDIV(Op, DAG);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002614
2615 SDLoc SL(Op);
2616 SDValue X = Op.getOperand(0);
2617 SDValue Y = Op.getOperand(1);
2618
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002619 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002620
2621 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
2622
2623 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
2624
2625 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
2626
2627 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
2628
2629 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
2630
2631 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
2632
2633 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
2634
2635 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
2636
2637 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
2638 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
2639
2640 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
2641 NegDivScale0, Mul, DivScale1);
2642
2643 SDValue Scale;
2644
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002645 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002646 // Workaround a hardware bug on SI where the condition output from div_scale
2647 // is not usable.
2648
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002649 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002650
2651 // Figure out if the scale to use for div_fmas.
2652 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
2653 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
2654 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
2655 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
2656
2657 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
2658 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
2659
2660 SDValue Scale0Hi
2661 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
2662 SDValue Scale1Hi
2663 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
2664
2665 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
2666 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
2667 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
2668 } else {
2669 Scale = DivScale1.getValue(1);
2670 }
2671
2672 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
2673 Fma4, Fma3, Mul, Scale);
2674
2675 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002676}
2677
2678SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
2679 EVT VT = Op.getValueType();
2680
2681 if (VT == MVT::f32)
2682 return LowerFDIV32(Op, DAG);
2683
2684 if (VT == MVT::f64)
2685 return LowerFDIV64(Op, DAG);
2686
2687 llvm_unreachable("Unexpected type for fdiv");
2688}
2689
Tom Stellard81d871d2013-11-13 23:36:50 +00002690SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2691 SDLoc DL(Op);
2692 StoreSDNode *Store = cast<StoreSDNode>(Op);
2693 EVT VT = Store->getMemoryVT();
2694
Matt Arsenault95245662016-02-11 05:32:46 +00002695 if (VT == MVT::i1) {
2696 return DAG.getTruncStore(Store->getChain(), DL,
2697 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
2698 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00002699 }
2700
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002701 assert(VT.isVector() &&
2702 Store->getValue().getValueType().getScalarType() == MVT::i32);
2703
2704 unsigned AS = Store->getAddressSpace();
2705 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
2706 AS, Store->getAlignment())) {
2707 return expandUnalignedStore(Store, DAG);
2708 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002709
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002710 unsigned NumElements = VT.getVectorNumElements();
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002711 switch (AS) {
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002712 case AMDGPUAS::GLOBAL_ADDRESS:
2713 case AMDGPUAS::FLAT_ADDRESS:
2714 if (NumElements > 4)
2715 return SplitVectorStore(Op, DAG);
2716 return SDValue();
2717 case AMDGPUAS::PRIVATE_ADDRESS: {
2718 switch (Subtarget->getMaxPrivateElementSize()) {
2719 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002720 return scalarizeVectorStore(Store, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002721 case 8:
2722 if (NumElements > 2)
2723 return SplitVectorStore(Op, DAG);
2724 return SDValue();
2725 case 16:
2726 if (NumElements > 4)
2727 return SplitVectorStore(Op, DAG);
2728 return SDValue();
2729 default:
2730 llvm_unreachable("unsupported private_element_size");
2731 }
2732 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002733 case AMDGPUAS::LOCAL_ADDRESS: {
2734 if (NumElements > 2)
2735 return SplitVectorStore(Op, DAG);
2736
2737 if (NumElements == 2)
2738 return Op;
2739
Matt Arsenault95245662016-02-11 05:32:46 +00002740 // If properly aligned, if we split we might be able to use ds_write_b64.
2741 return SplitVectorStore(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002742 }
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002743 default:
2744 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00002745 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002746}
2747
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002748SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002749 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002750 EVT VT = Op.getValueType();
2751 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00002752 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002753 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
2754 DAG.getNode(ISD::FMUL, DL, VT, Arg,
2755 DAG.getConstantFP(0.5/M_PI, DL,
2756 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002757
2758 switch (Op.getOpcode()) {
2759 case ISD::FCOS:
2760 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
2761 case ISD::FSIN:
2762 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
2763 default:
2764 llvm_unreachable("Wrong trig opcode");
2765 }
2766}
2767
Tom Stellard354a43c2016-04-01 18:27:37 +00002768SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
2769 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
2770 assert(AtomicNode->isCompareAndSwap());
2771 unsigned AS = AtomicNode->getAddressSpace();
2772
2773 // No custom lowering required for local address space
2774 if (!isFlatGlobalAddrSpace(AS))
2775 return Op;
2776
2777 // Non-local address space requires custom lowering for atomic compare
2778 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
2779 SDLoc DL(Op);
2780 SDValue ChainIn = Op.getOperand(0);
2781 SDValue Addr = Op.getOperand(1);
2782 SDValue Old = Op.getOperand(2);
2783 SDValue New = Op.getOperand(3);
2784 EVT VT = Op.getValueType();
2785 MVT SimpleVT = VT.getSimpleVT();
2786 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
2787
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002788 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
Tom Stellard354a43c2016-04-01 18:27:37 +00002789 SDValue Ops[] = { ChainIn, Addr, NewOld };
Matt Arsenault88701812016-06-09 23:42:48 +00002790
2791 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(),
2792 Ops, VT, AtomicNode->getMemOperand());
Tom Stellard354a43c2016-04-01 18:27:37 +00002793}
2794
Tom Stellard75aadc22012-12-11 21:25:42 +00002795//===----------------------------------------------------------------------===//
2796// Custom DAG optimizations
2797//===----------------------------------------------------------------------===//
2798
Matt Arsenault364a6742014-06-11 17:50:44 +00002799SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00002800 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00002801 EVT VT = N->getValueType(0);
2802 EVT ScalarVT = VT.getScalarType();
2803 if (ScalarVT != MVT::f32)
2804 return SDValue();
2805
2806 SelectionDAG &DAG = DCI.DAG;
2807 SDLoc DL(N);
2808
2809 SDValue Src = N->getOperand(0);
2810 EVT SrcVT = Src.getValueType();
2811
2812 // TODO: We could try to match extracting the higher bytes, which would be
2813 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
2814 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
2815 // about in practice.
2816 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
2817 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
2818 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
2819 DCI.AddToWorklist(Cvt.getNode());
2820 return Cvt;
2821 }
2822 }
2823
Matt Arsenault364a6742014-06-11 17:50:44 +00002824 return SDValue();
2825}
2826
Eric Christopher6c5b5112015-03-11 18:43:21 +00002827/// \brief Return true if the given offset Size in bytes can be folded into
2828/// the immediate offsets of a memory instruction for the given address space.
2829static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002830 const SISubtarget &STI) {
Eric Christopher6c5b5112015-03-11 18:43:21 +00002831 switch (AS) {
2832 case AMDGPUAS::GLOBAL_ADDRESS: {
2833 // MUBUF instructions a 12-bit offset in bytes.
2834 return isUInt<12>(OffsetSize);
2835 }
2836 case AMDGPUAS::CONSTANT_ADDRESS: {
2837 // SMRD instructions have an 8-bit offset in dwords on SI and
2838 // a 20-bit offset in bytes on VI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002839 if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Eric Christopher6c5b5112015-03-11 18:43:21 +00002840 return isUInt<20>(OffsetSize);
2841 else
2842 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
2843 }
2844 case AMDGPUAS::LOCAL_ADDRESS:
2845 case AMDGPUAS::REGION_ADDRESS: {
2846 // The single offset versions have a 16-bit offset in bytes.
2847 return isUInt<16>(OffsetSize);
2848 }
2849 case AMDGPUAS::PRIVATE_ADDRESS:
2850 // Indirect register addressing does not use any offsets.
2851 default:
2852 return 0;
2853 }
2854}
2855
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002856// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
2857
2858// This is a variant of
2859// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
2860//
2861// The normal DAG combiner will do this, but only if the add has one use since
2862// that would increase the number of instructions.
2863//
2864// This prevents us from seeing a constant offset that can be folded into a
2865// memory instruction's addressing mode. If we know the resulting add offset of
2866// a pointer can be folded into an addressing offset, we can replace the pointer
2867// operand with the add of new constant offset. This eliminates one of the uses,
2868// and may allow the remaining use to also be simplified.
2869//
2870SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
2871 unsigned AddrSpace,
2872 DAGCombinerInfo &DCI) const {
2873 SDValue N0 = N->getOperand(0);
2874 SDValue N1 = N->getOperand(1);
2875
2876 if (N0.getOpcode() != ISD::ADD)
2877 return SDValue();
2878
2879 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
2880 if (!CN1)
2881 return SDValue();
2882
2883 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2884 if (!CAdd)
2885 return SDValue();
2886
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002887 // If the resulting offset is too large, we can't fold it into the addressing
2888 // mode offset.
2889 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002890 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget()))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002891 return SDValue();
2892
2893 SelectionDAG &DAG = DCI.DAG;
2894 SDLoc SL(N);
2895 EVT VT = N->getValueType(0);
2896
2897 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002898 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002899
2900 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
2901}
2902
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002903static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) {
2904 return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) ||
2905 (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) ||
2906 (Opc == ISD::XOR && Val == 0);
2907}
2908
2909// Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This
2910// will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit
2911// integer combine opportunities since most 64-bit operations are decomposed
2912// this way. TODO: We won't want this for SALU especially if it is an inline
2913// immediate.
2914SDValue SITargetLowering::splitBinaryBitConstantOp(
2915 DAGCombinerInfo &DCI,
2916 const SDLoc &SL,
2917 unsigned Opc, SDValue LHS,
2918 const ConstantSDNode *CRHS) const {
2919 uint64_t Val = CRHS->getZExtValue();
2920 uint32_t ValLo = Lo_32(Val);
2921 uint32_t ValHi = Hi_32(Val);
2922 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
2923
2924 if ((bitOpWithConstantIsReducible(Opc, ValLo) ||
2925 bitOpWithConstantIsReducible(Opc, ValHi)) ||
2926 (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) {
2927 // If we need to materialize a 64-bit immediate, it will be split up later
2928 // anyway. Avoid creating the harder to understand 64-bit immediate
2929 // materialization.
2930 return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi);
2931 }
2932
2933 return SDValue();
2934}
2935
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002936SDValue SITargetLowering::performAndCombine(SDNode *N,
2937 DAGCombinerInfo &DCI) const {
2938 if (DCI.isBeforeLegalize())
2939 return SDValue();
2940
2941 SelectionDAG &DAG = DCI.DAG;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002942 EVT VT = N->getValueType(0);
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002943 SDValue LHS = N->getOperand(0);
2944 SDValue RHS = N->getOperand(1);
2945
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002946
2947 if (VT == MVT::i64) {
2948 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
2949 if (CRHS) {
2950 if (SDValue Split
2951 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS))
2952 return Split;
2953 }
2954 }
2955
2956 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
2957 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
2958 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) {
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002959 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
2960 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
2961
2962 SDValue X = LHS.getOperand(0);
2963 SDValue Y = RHS.getOperand(0);
2964 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
2965 return SDValue();
2966
2967 if (LCC == ISD::SETO) {
2968 if (X != LHS.getOperand(1))
2969 return SDValue();
2970
2971 if (RCC == ISD::SETUNE) {
2972 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
2973 if (!C1 || !C1->isInfinity() || C1->isNegative())
2974 return SDValue();
2975
2976 const uint32_t Mask = SIInstrFlags::N_NORMAL |
2977 SIInstrFlags::N_SUBNORMAL |
2978 SIInstrFlags::N_ZERO |
2979 SIInstrFlags::P_ZERO |
2980 SIInstrFlags::P_SUBNORMAL |
2981 SIInstrFlags::P_NORMAL;
2982
2983 static_assert(((~(SIInstrFlags::S_NAN |
2984 SIInstrFlags::Q_NAN |
2985 SIInstrFlags::N_INFINITY |
2986 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
2987 "mask not equal");
2988
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002989 SDLoc DL(N);
2990 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2991 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002992 }
2993 }
2994 }
2995
2996 return SDValue();
2997}
2998
Matt Arsenaultf2290332015-01-06 23:00:39 +00002999SDValue SITargetLowering::performOrCombine(SDNode *N,
3000 DAGCombinerInfo &DCI) const {
3001 SelectionDAG &DAG = DCI.DAG;
3002 SDValue LHS = N->getOperand(0);
3003 SDValue RHS = N->getOperand(1);
3004
Matt Arsenault3b082382016-04-12 18:24:38 +00003005 EVT VT = N->getValueType(0);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003006 if (VT == MVT::i1) {
3007 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
3008 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
3009 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
3010 SDValue Src = LHS.getOperand(0);
3011 if (Src != RHS.getOperand(0))
3012 return SDValue();
Matt Arsenault3b082382016-04-12 18:24:38 +00003013
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003014 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
3015 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
3016 if (!CLHS || !CRHS)
3017 return SDValue();
Matt Arsenault3b082382016-04-12 18:24:38 +00003018
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003019 // Only 10 bits are used.
3020 static const uint32_t MaxMask = 0x3ff;
Matt Arsenault3b082382016-04-12 18:24:38 +00003021
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003022 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
3023 SDLoc DL(N);
3024 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
3025 Src, DAG.getConstant(NewMask, DL, MVT::i32));
3026 }
Matt Arsenault3b082382016-04-12 18:24:38 +00003027
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003028 return SDValue();
3029 }
3030
3031 if (VT != MVT::i64)
3032 return SDValue();
3033
3034 // TODO: This could be a generic combine with a predicate for extracting the
3035 // high half of an integer being free.
3036
3037 // (or i64:x, (zero_extend i32:y)) ->
3038 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
3039 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
3040 RHS.getOpcode() != ISD::ZERO_EXTEND)
3041 std::swap(LHS, RHS);
3042
3043 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
3044 SDValue ExtSrc = RHS.getOperand(0);
3045 EVT SrcVT = ExtSrc.getValueType();
3046 if (SrcVT == MVT::i32) {
3047 SDLoc SL(N);
3048 SDValue LowLHS, HiBits;
3049 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
3050 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
3051
3052 DCI.AddToWorklist(LowOr.getNode());
3053 DCI.AddToWorklist(HiBits.getNode());
3054
3055 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
3056 LowOr, HiBits);
3057 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
Matt Arsenault3b082382016-04-12 18:24:38 +00003058 }
3059 }
3060
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003061 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
3062 if (CRHS) {
3063 if (SDValue Split
3064 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS))
3065 return Split;
3066 }
Matt Arsenaultf2290332015-01-06 23:00:39 +00003067
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003068 return SDValue();
3069}
Matt Arsenaultf2290332015-01-06 23:00:39 +00003070
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003071SDValue SITargetLowering::performXorCombine(SDNode *N,
3072 DAGCombinerInfo &DCI) const {
3073 EVT VT = N->getValueType(0);
3074 if (VT != MVT::i64)
3075 return SDValue();
Matt Arsenaultf2290332015-01-06 23:00:39 +00003076
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003077 SDValue LHS = N->getOperand(0);
3078 SDValue RHS = N->getOperand(1);
3079
3080 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
3081 if (CRHS) {
3082 if (SDValue Split
3083 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS))
3084 return Split;
Matt Arsenaultf2290332015-01-06 23:00:39 +00003085 }
3086
3087 return SDValue();
3088}
3089
3090SDValue SITargetLowering::performClassCombine(SDNode *N,
3091 DAGCombinerInfo &DCI) const {
3092 SelectionDAG &DAG = DCI.DAG;
3093 SDValue Mask = N->getOperand(1);
3094
3095 // fp_class x, 0 -> false
3096 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
3097 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003098 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003099 }
3100
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003101 if (N->getOperand(0).isUndef())
3102 return DAG.getUNDEF(MVT::i1);
3103
Matt Arsenaultf2290332015-01-06 23:00:39 +00003104 return SDValue();
3105}
3106
Matt Arsenault9cd90712016-04-14 01:42:16 +00003107// Constant fold canonicalize.
3108SDValue SITargetLowering::performFCanonicalizeCombine(
3109 SDNode *N,
3110 DAGCombinerInfo &DCI) const {
3111 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
3112 if (!CFP)
3113 return SDValue();
3114
3115 SelectionDAG &DAG = DCI.DAG;
3116 const APFloat &C = CFP->getValueAPF();
3117
3118 // Flush denormals to 0 if not enabled.
3119 if (C.isDenormal()) {
3120 EVT VT = N->getValueType(0);
3121 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
3122 return DAG.getConstantFP(0.0, SDLoc(N), VT);
3123
3124 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
3125 return DAG.getConstantFP(0.0, SDLoc(N), VT);
3126 }
3127
3128 if (C.isNaN()) {
3129 EVT VT = N->getValueType(0);
3130 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
3131 if (C.isSignaling()) {
3132 // Quiet a signaling NaN.
3133 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3134 }
3135
3136 // Make sure it is the canonical NaN bitpattern.
3137 //
3138 // TODO: Can we use -1 as the canonical NaN value since it's an inline
3139 // immediate?
3140 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
3141 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3142 }
3143
3144 return SDValue(CFP, 0);
3145}
3146
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003147static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
3148 switch (Opc) {
3149 case ISD::FMAXNUM:
3150 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003151 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003152 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003153 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003154 return AMDGPUISD::UMAX3;
3155 case ISD::FMINNUM:
3156 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003157 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003158 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003159 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003160 return AMDGPUISD::UMIN3;
3161 default:
3162 llvm_unreachable("Not a min/max opcode");
3163 }
3164}
3165
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003166static SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3167 SDValue Op0, SDValue Op1, bool Signed) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003168 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
3169 if (!K1)
3170 return SDValue();
3171
3172 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
3173 if (!K0)
3174 return SDValue();
3175
Matt Arsenaultf639c322016-01-28 20:53:42 +00003176 if (Signed) {
3177 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
3178 return SDValue();
3179 } else {
3180 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
3181 return SDValue();
3182 }
3183
3184 EVT VT = K0->getValueType(0);
3185 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
3186 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
3187}
3188
3189static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
3190 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
3191 return true;
3192
3193 return DAG.isKnownNeverNaN(Op);
3194}
3195
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003196static SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3197 SDValue Op0, SDValue Op1) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003198 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
3199 if (!K1)
3200 return SDValue();
3201
3202 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
3203 if (!K0)
3204 return SDValue();
3205
3206 // Ordered >= (although NaN inputs should have folded away by now).
3207 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
3208 if (Cmp == APFloat::cmpGreaterThan)
3209 return SDValue();
3210
3211 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
3212 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
3213 // give the other result, which is different from med3 with a NaN input.
3214 SDValue Var = Op0.getOperand(0);
3215 if (!isKnownNeverSNan(DAG, Var))
3216 return SDValue();
3217
3218 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
3219 Var, SDValue(K0, 0), SDValue(K1, 0));
3220}
3221
3222SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
3223 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003224 SelectionDAG &DAG = DCI.DAG;
3225
3226 unsigned Opc = N->getOpcode();
3227 SDValue Op0 = N->getOperand(0);
3228 SDValue Op1 = N->getOperand(1);
3229
3230 // Only do this if the inner op has one use since this will just increases
3231 // register pressure for no benefit.
3232
Matt Arsenault5b39b342016-01-28 20:53:48 +00003233 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
3234 // max(max(a, b), c) -> max3(a, b, c)
3235 // min(min(a, b), c) -> min3(a, b, c)
3236 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
3237 SDLoc DL(N);
3238 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3239 DL,
3240 N->getValueType(0),
3241 Op0.getOperand(0),
3242 Op0.getOperand(1),
3243 Op1);
3244 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003245
Matt Arsenault5b39b342016-01-28 20:53:48 +00003246 // Try commuted.
3247 // max(a, max(b, c)) -> max3(a, b, c)
3248 // min(a, min(b, c)) -> min3(a, b, c)
3249 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
3250 SDLoc DL(N);
3251 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3252 DL,
3253 N->getValueType(0),
3254 Op0,
3255 Op1.getOperand(0),
3256 Op1.getOperand(1));
3257 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003258 }
3259
Matt Arsenaultf639c322016-01-28 20:53:42 +00003260 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
3261 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
3262 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
3263 return Med3;
3264 }
3265
3266 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
3267 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
3268 return Med3;
3269 }
3270
3271 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00003272 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
3273 (Opc == AMDGPUISD::FMIN_LEGACY &&
3274 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00003275 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
3276 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
3277 return Res;
3278 }
3279
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003280 return SDValue();
3281}
3282
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003283SDValue SITargetLowering::performSetCCCombine(SDNode *N,
3284 DAGCombinerInfo &DCI) const {
3285 SelectionDAG &DAG = DCI.DAG;
3286 SDLoc SL(N);
3287
3288 SDValue LHS = N->getOperand(0);
3289 SDValue RHS = N->getOperand(1);
3290 EVT VT = LHS.getValueType();
3291
3292 if (VT != MVT::f32 && VT != MVT::f64)
3293 return SDValue();
3294
3295 // Match isinf pattern
3296 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
3297 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
3298 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
3299 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
3300 if (!CRHS)
3301 return SDValue();
3302
3303 const APFloat &APF = CRHS->getValueAPF();
3304 if (APF.isInfinity() && !APF.isNegative()) {
3305 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003306 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
3307 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003308 }
3309 }
3310
3311 return SDValue();
3312}
3313
Tom Stellard75aadc22012-12-11 21:25:42 +00003314SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
3315 DAGCombinerInfo &DCI) const {
3316 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003317 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00003318
3319 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00003320 default:
3321 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003322 case ISD::SETCC:
3323 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00003324 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003325 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003326 case ISD::SMAX:
3327 case ISD::SMIN:
3328 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00003329 case ISD::UMIN:
3330 case AMDGPUISD::FMIN_LEGACY:
3331 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003332 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00003333 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003334 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00003335 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003336 break;
3337 }
Matt Arsenault364a6742014-06-11 17:50:44 +00003338
3339 case AMDGPUISD::CVT_F32_UBYTE0:
3340 case AMDGPUISD::CVT_F32_UBYTE1:
3341 case AMDGPUISD::CVT_F32_UBYTE2:
3342 case AMDGPUISD::CVT_F32_UBYTE3: {
3343 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
Matt Arsenault364a6742014-06-11 17:50:44 +00003344 SDValue Src = N->getOperand(0);
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003345
Matt Arsenault327bb5a2016-07-01 22:47:50 +00003346 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero.
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003347 if (Src.getOpcode() == ISD::SRL) {
3348 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
3349 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
3350 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x
3351
3352 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(1))) {
3353 unsigned SrcOffset = C->getZExtValue() + 8 * Offset;
3354 if (SrcOffset < 32 && SrcOffset % 8 == 0) {
3355 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, DL,
3356 MVT::f32, Src.getOperand(0));
3357 }
3358 }
3359 }
3360
Matt Arsenault364a6742014-06-11 17:50:44 +00003361 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
3362
3363 APInt KnownZero, KnownOne;
3364 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
3365 !DCI.isBeforeLegalizeOps());
3366 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3367 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
3368 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
3369 DCI.CommitTargetLoweringOpt(TLO);
3370 }
3371
3372 break;
3373 }
3374
3375 case ISD::UINT_TO_FP: {
3376 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003377 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003378 case ISD::FADD: {
3379 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3380 break;
3381
3382 EVT VT = N->getValueType(0);
3383 if (VT != MVT::f32)
3384 break;
3385
Matt Arsenault8d630032015-02-20 22:10:41 +00003386 // Only do this if we are not trying to support denormals. v_mad_f32 does
3387 // not support denormals ever.
3388 if (Subtarget->hasFP32Denormals())
3389 break;
3390
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003391 SDValue LHS = N->getOperand(0);
3392 SDValue RHS = N->getOperand(1);
3393
3394 // These should really be instruction patterns, but writing patterns with
3395 // source modiifiers is a pain.
3396
3397 // fadd (fadd (a, a), b) -> mad 2.0, a, b
3398 if (LHS.getOpcode() == ISD::FADD) {
3399 SDValue A = LHS.getOperand(0);
3400 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003401 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003402 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003403 }
3404 }
3405
3406 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
3407 if (RHS.getOpcode() == ISD::FADD) {
3408 SDValue A = RHS.getOperand(0);
3409 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003410 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003411 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003412 }
3413 }
3414
Matt Arsenault8d630032015-02-20 22:10:41 +00003415 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003416 }
Matt Arsenault8675db12014-08-29 16:01:14 +00003417 case ISD::FSUB: {
3418 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3419 break;
3420
3421 EVT VT = N->getValueType(0);
3422
3423 // Try to get the fneg to fold into the source modifier. This undoes generic
3424 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00003425 //
3426 // Only do this if we are not trying to support denormals. v_mad_f32 does
3427 // not support denormals ever.
3428 if (VT == MVT::f32 &&
3429 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00003430 SDValue LHS = N->getOperand(0);
3431 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003432 if (LHS.getOpcode() == ISD::FADD) {
3433 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
3434
3435 SDValue A = LHS.getOperand(0);
3436 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003437 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003438 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
3439
Matt Arsenault8d630032015-02-20 22:10:41 +00003440 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003441 }
3442 }
3443
3444 if (RHS.getOpcode() == ISD::FADD) {
3445 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
3446
3447 SDValue A = RHS.getOperand(0);
3448 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003449 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003450 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003451 }
3452 }
Matt Arsenault8d630032015-02-20 22:10:41 +00003453
3454 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00003455 }
3456
3457 break;
3458 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003459 case ISD::LOAD:
3460 case ISD::STORE:
3461 case ISD::ATOMIC_LOAD:
3462 case ISD::ATOMIC_STORE:
3463 case ISD::ATOMIC_CMP_SWAP:
3464 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
3465 case ISD::ATOMIC_SWAP:
3466 case ISD::ATOMIC_LOAD_ADD:
3467 case ISD::ATOMIC_LOAD_SUB:
3468 case ISD::ATOMIC_LOAD_AND:
3469 case ISD::ATOMIC_LOAD_OR:
3470 case ISD::ATOMIC_LOAD_XOR:
3471 case ISD::ATOMIC_LOAD_NAND:
3472 case ISD::ATOMIC_LOAD_MIN:
3473 case ISD::ATOMIC_LOAD_MAX:
3474 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00003475 case ISD::ATOMIC_LOAD_UMAX:
3476 case AMDGPUISD::ATOMIC_INC:
3477 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003478 if (DCI.isBeforeLegalize())
3479 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003480
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003481 MemSDNode *MemNode = cast<MemSDNode>(N);
3482 SDValue Ptr = MemNode->getBasePtr();
3483
3484 // TODO: We could also do this for multiplies.
3485 unsigned AS = MemNode->getAddressSpace();
3486 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
3487 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
3488 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00003489 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003490
3491 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
3492 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
3493 }
3494 }
3495 break;
3496 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003497 case ISD::AND:
3498 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003499 case ISD::OR:
3500 return performOrCombine(N, DCI);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003501 case ISD::XOR:
3502 return performXorCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003503 case AMDGPUISD::FP_CLASS:
3504 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00003505 case ISD::FCANONICALIZE:
3506 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003507 case AMDGPUISD::FRACT:
3508 case AMDGPUISD::RCP:
3509 case AMDGPUISD::RSQ:
Matt Arsenault32fc5272016-07-26 16:45:45 +00003510 case AMDGPUISD::RCP_LEGACY:
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003511 case AMDGPUISD::RSQ_LEGACY:
3512 case AMDGPUISD::RSQ_CLAMP:
3513 case AMDGPUISD::LDEXP: {
3514 SDValue Src = N->getOperand(0);
3515 if (Src.isUndef())
3516 return Src;
3517 break;
3518 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003519 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003520 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00003521}
Christian Konigd910b7d2013-02-26 17:52:16 +00003522
Christian Konig8e06e2a2013-04-10 08:39:08 +00003523/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00003524static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00003525 switch (Idx) {
3526 default: return 0;
3527 case AMDGPU::sub0: return 0;
3528 case AMDGPU::sub1: return 1;
3529 case AMDGPU::sub2: return 2;
3530 case AMDGPU::sub3: return 3;
3531 }
3532}
3533
3534/// \brief Adjust the writemask of MIMG instructions
3535void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
3536 SelectionDAG &DAG) const {
3537 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00003538 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003539 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
3540 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00003541 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003542
3543 // Try to figure out the used register components
3544 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
3545 I != E; ++I) {
3546
3547 // Abort if we can't understand the usage
3548 if (!I->isMachineOpcode() ||
3549 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
3550 return;
3551
Tom Stellard54774e52013-10-23 02:53:47 +00003552 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
3553 // Note that subregs are packed, i.e. Lane==0 is the first bit set
3554 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
3555 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00003556 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00003557
Tom Stellard54774e52013-10-23 02:53:47 +00003558 // Set which texture component corresponds to the lane.
3559 unsigned Comp;
3560 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
3561 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00003562 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00003563 Dmask &= ~(1 << Comp);
3564 }
3565
Christian Konig8e06e2a2013-04-10 08:39:08 +00003566 // Abort if we have more than one user per component
3567 if (Users[Lane])
3568 return;
3569
3570 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00003571 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003572 }
3573
Tom Stellard54774e52013-10-23 02:53:47 +00003574 // Abort if there's no change
3575 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00003576 return;
3577
3578 // Adjust the writemask in the node
3579 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003580 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003581 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003582 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00003583 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003584
Christian Konig8b1ed282013-04-10 08:39:16 +00003585 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00003586 // (if NewDmask has only one bit set...)
3587 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003588 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
3589 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00003590 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003591 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00003592 SDValue(Node, 0), RC);
3593 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
3594 return;
3595 }
3596
Christian Konig8e06e2a2013-04-10 08:39:08 +00003597 // Update the users of the node with the new indices
3598 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
3599
3600 SDNode *User = Users[i];
3601 if (!User)
3602 continue;
3603
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003604 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003605 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
3606
3607 switch (Idx) {
3608 default: break;
3609 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
3610 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
3611 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
3612 }
3613 }
3614}
3615
Tom Stellardc98ee202015-07-16 19:40:07 +00003616static bool isFrameIndexOp(SDValue Op) {
3617 if (Op.getOpcode() == ISD::AssertZext)
3618 Op = Op.getOperand(0);
3619
3620 return isa<FrameIndexSDNode>(Op);
3621}
3622
Tom Stellard3457a842014-10-09 19:06:00 +00003623/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
3624/// with frame index operands.
3625/// LLVM assumes that inputs are to these instructions are registers.
3626void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
3627 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003628
3629 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00003630 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00003631 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00003632 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003633 continue;
3634 }
3635
Tom Stellard3457a842014-10-09 19:06:00 +00003636 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003637 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00003638 Node->getOperand(i).getValueType(),
3639 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003640 }
3641
Tom Stellard3457a842014-10-09 19:06:00 +00003642 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003643}
3644
Matt Arsenault08d84942014-06-03 23:06:13 +00003645/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00003646SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
3647 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003648 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003649 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00003650
Nicolai Haehnlec06bfa12016-07-11 21:59:43 +00003651 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() &&
3652 !TII->isGather4(Opcode))
Christian Konig8e06e2a2013-04-10 08:39:08 +00003653 adjustWritemask(Node, DAG);
3654
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003655 if (Opcode == AMDGPU::INSERT_SUBREG ||
3656 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003657 legalizeTargetIndependentNode(Node, DAG);
3658 return Node;
3659 }
Tom Stellard654d6692015-01-08 15:08:17 +00003660 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003661}
Christian Konig8b1ed282013-04-10 08:39:16 +00003662
3663/// \brief Assign the register class depending on the number of
3664/// bits set in the writemask
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003665void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
Christian Konig8b1ed282013-04-10 08:39:16 +00003666 SDNode *Node) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003667 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003668
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003669 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003670
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003671 if (TII->isVOP3(MI.getOpcode())) {
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003672 // Make sure constant bus requirements are respected.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003673 TII->legalizeOperandsVOP3(MRI, MI);
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003674 return;
3675 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00003676
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003677 if (TII->isMIMG(MI)) {
3678 unsigned VReg = MI.getOperand(0).getReg();
3679 unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4;
3680 unsigned Writemask = MI.getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003681 unsigned BitsSet = 0;
3682 for (unsigned i = 0; i < 4; ++i)
3683 BitsSet += Writemask & (1 << i) ? 1 : 0;
3684
3685 const TargetRegisterClass *RC;
3686 switch (BitsSet) {
3687 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00003688 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003689 case 2: RC = &AMDGPU::VReg_64RegClass; break;
3690 case 3: RC = &AMDGPU::VReg_96RegClass; break;
3691 }
3692
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003693 unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet);
3694 MI.setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003695 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00003696 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00003697 }
3698
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003699 // Replace unused atomics with the no return version.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003700 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003701 if (NoRetAtomicOp != -1) {
3702 if (!Node->hasAnyUseOfValue(0)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003703 MI.setDesc(TII->get(NoRetAtomicOp));
3704 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003705 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003706 }
3707
Tom Stellard354a43c2016-04-01 18:27:37 +00003708 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
3709 // instruction, because the return type of these instructions is a vec2 of
3710 // the memory type, so it can be tied to the input operand.
3711 // This means these instructions always have a use, so we need to add a
3712 // special case to check if the atomic has only one extract_subreg use,
3713 // which itself has no uses.
3714 if ((Node->hasNUsesOfValue(1, 0) &&
Nicolai Haehnle750082d2016-04-15 14:42:36 +00003715 Node->use_begin()->isMachineOpcode() &&
Tom Stellard354a43c2016-04-01 18:27:37 +00003716 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
3717 !Node->use_begin()->hasAnyUseOfValue(0))) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003718 unsigned Def = MI.getOperand(0).getReg();
Tom Stellard354a43c2016-04-01 18:27:37 +00003719
3720 // Change this into a noret atomic.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003721 MI.setDesc(TII->get(NoRetAtomicOp));
3722 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003723
3724 // If we only remove the def operand from the atomic instruction, the
3725 // extract_subreg will be left with a use of a vreg without a def.
3726 // So we need to insert an implicit_def to avoid machine verifier
3727 // errors.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003728 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
Tom Stellard354a43c2016-04-01 18:27:37 +00003729 TII->get(AMDGPU::IMPLICIT_DEF), Def);
3730 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003731 return;
3732 }
Christian Konig8b1ed282013-04-10 08:39:16 +00003733}
Tom Stellard0518ff82013-06-03 17:39:58 +00003734
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003735static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL,
3736 uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003737 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00003738 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
3739}
3740
3741MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003742 const SDLoc &DL,
Matt Arsenault485defe2014-11-05 19:01:17 +00003743 SDValue Ptr) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003744 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault485defe2014-11-05 19:01:17 +00003745
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003746 // Build the half of the subregister with the constants before building the
3747 // full 128-bit register. If we are building multiple resource descriptors,
3748 // this will allow CSEing of the 2-component register.
3749 const SDValue Ops0[] = {
3750 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
3751 buildSMovImm32(DAG, DL, 0),
3752 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
3753 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
3754 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
3755 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003756
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003757 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
3758 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00003759
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003760 // Combine the constants and the pointer.
3761 const SDValue Ops1[] = {
3762 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
3763 Ptr,
3764 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
3765 SubRegHi,
3766 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
3767 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003768
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003769 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00003770}
3771
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003772/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003773/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
3774/// of the resource descriptor) to create an offset, which is added to
3775/// the resource pointer.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003776MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL,
3777 SDValue Ptr, uint32_t RsrcDword1,
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003778 uint64_t RsrcDword2And3) const {
3779 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
3780 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
3781 if (RsrcDword1) {
3782 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003783 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
3784 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003785 }
3786
3787 SDValue DataLo = buildSMovImm32(DAG, DL,
3788 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
3789 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
3790
3791 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003792 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003793 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003794 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003795 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003796 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003797 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003798 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003799 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003800 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003801 };
3802
3803 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
3804}
3805
Tom Stellard94593ee2013-06-03 17:40:18 +00003806SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3807 const TargetRegisterClass *RC,
3808 unsigned Reg, EVT VT) const {
3809 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
3810
3811 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
3812 cast<RegisterSDNode>(VReg)->getReg(), VT);
3813}
Tom Stellardd7e6f132015-04-08 01:09:26 +00003814
3815//===----------------------------------------------------------------------===//
3816// SI Inline Assembly Support
3817//===----------------------------------------------------------------------===//
3818
3819std::pair<unsigned, const TargetRegisterClass *>
3820SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003821 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00003822 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003823
3824 if (Constraint.size() == 1) {
3825 switch (Constraint[0]) {
3826 case 's':
3827 case 'r':
3828 switch (VT.getSizeInBits()) {
3829 default:
3830 return std::make_pair(0U, nullptr);
3831 case 32:
Matt Arsenaulta609e2d2016-08-30 20:50:08 +00003832 return std::make_pair(0U, &AMDGPU::SReg_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003833 case 64:
3834 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
3835 case 128:
3836 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
3837 case 256:
3838 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
3839 }
3840
3841 case 'v':
3842 switch (VT.getSizeInBits()) {
3843 default:
3844 return std::make_pair(0U, nullptr);
3845 case 32:
3846 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
3847 case 64:
3848 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
3849 case 96:
3850 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
3851 case 128:
3852 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
3853 case 256:
3854 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
3855 case 512:
3856 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
3857 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00003858 }
3859 }
3860
3861 if (Constraint.size() > 1) {
3862 const TargetRegisterClass *RC = nullptr;
3863 if (Constraint[1] == 'v') {
3864 RC = &AMDGPU::VGPR_32RegClass;
3865 } else if (Constraint[1] == 's') {
3866 RC = &AMDGPU::SGPR_32RegClass;
3867 }
3868
3869 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00003870 uint32_t Idx;
3871 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
3872 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00003873 return std::make_pair(RC->getRegister(Idx), RC);
3874 }
3875 }
3876 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
3877}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003878
3879SITargetLowering::ConstraintType
3880SITargetLowering::getConstraintType(StringRef Constraint) const {
3881 if (Constraint.size() == 1) {
3882 switch (Constraint[0]) {
3883 default: break;
3884 case 's':
3885 case 'v':
3886 return C_RegisterClass;
3887 }
3888 }
3889 return TargetLowering::getConstraintType(Constraint);
3890}