blob: a2c4e6a405697eda1b7b920dfc81baf77d8a9e9f [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief Custom DAG lowering for SI
12//
13//===----------------------------------------------------------------------===//
14
NAKAMURA Takumi45e0a832014-07-20 11:15:07 +000015#ifdef _MSC_VER
16// Provide M_PI.
17#define _USE_MATH_DEFINES
18#include <cmath>
19#endif
20
Christian Konig99ee0f42013-03-07 09:04:14 +000021#include "AMDGPU.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000022#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000023#include "AMDGPUSubtarget.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000024#include "SIISelLowering.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025#include "SIInstrInfo.h"
26#include "SIMachineFunctionInfo.h"
27#include "SIRegisterInfo.h"
Alexey Samsonova253bf92014-08-27 19:36:53 +000028#include "llvm/ADT/BitVector.h"
Matt Arsenault9a10cea2016-01-26 04:29:24 +000029#include "llvm/ADT/StringSwitch.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000030#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
32#include "llvm/CodeGen/MachineRegisterInfo.h"
33#include "llvm/CodeGen/SelectionDAG.h"
Oliver Stannard7e7d9832016-02-02 13:52:43 +000034#include "llvm/IR/DiagnosticInfo.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000035#include "llvm/IR/Function.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000036
37using namespace llvm;
38
Wei Dinged0f97f2016-06-09 19:17:15 +000039// -amdgpu-fast-fdiv - Command line option to enable faster 2.5 ulp fdiv.
40static cl::opt<bool> EnableAMDGPUFastFDIV(
Matt Arsenault37fefd62016-06-10 02:18:02 +000041 "amdgpu-fast-fdiv",
42 cl::desc("Enable faster 2.5 ulp fdiv"),
Wei Dinged0f97f2016-06-09 19:17:15 +000043 cl::init(false));
44
Tom Stellardf110f8f2016-04-14 16:27:03 +000045static unsigned findFirstFreeSGPR(CCState &CCInfo) {
46 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs();
47 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) {
48 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) {
49 return AMDGPU::SGPR0 + Reg;
50 }
51 }
52 llvm_unreachable("Cannot allocate sgpr");
53}
54
Matt Arsenault43e92fe2016-06-24 06:30:11 +000055SITargetLowering::SITargetLowering(const TargetMachine &TM,
56 const SISubtarget &STI)
Eric Christopher7792e322015-01-30 23:24:40 +000057 : AMDGPUTargetLowering(TM, STI) {
Tom Stellard1bd80722014-04-30 15:31:33 +000058 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000059 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000060
Tom Stellard334b29c2014-04-17 21:00:09 +000061 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000062 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000063
Tom Stellard436780b2014-05-15 14:41:57 +000064 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
65 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
66 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000067
Matt Arsenault61001bb2015-11-25 19:58:34 +000068 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
69 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
70
Tom Stellard436780b2014-05-15 14:41:57 +000071 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
72 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000073
Tom Stellardf0a21072014-11-18 20:39:39 +000074 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000075 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
76
Tom Stellardf0a21072014-11-18 20:39:39 +000077 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000078 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000079
Eric Christopher23a3a7c2015-02-26 00:00:24 +000080 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000081
Tom Stellard35bb18c2013-08-26 15:06:04 +000082 // We need to custom lower vector stores from local memory
Matt Arsenault71e66762016-05-21 02:27:49 +000083 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000084 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000085 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
86 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000087 setOperationAction(ISD::LOAD, MVT::i1, Custom);
Matt Arsenault2b957b52016-05-02 20:07:26 +000088
Matt Arsenaultbcdfee72016-05-02 20:13:51 +000089 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000090 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
91 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
92 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
93 setOperationAction(ISD::STORE, MVT::i1, Custom);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +000094
Matt Arsenault71e66762016-05-21 02:27:49 +000095 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
96 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
97 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
98 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
99
100 setOperationAction(ISD::SELECT, MVT::i1, Promote);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000101 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +0000102 setOperationAction(ISD::SELECT, MVT::f64, Promote);
103 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000104
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000105 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
106 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
107 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
108 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Matt Arsenault71e66762016-05-21 02:27:49 +0000109 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000110
Tom Stellardd1efda82016-01-20 21:48:24 +0000111 setOperationAction(ISD::SETCC, MVT::i1, Promote);
Tom Stellard83747202013-07-18 21:43:53 +0000112 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
113 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
114
Matt Arsenault71e66762016-05-21 02:27:49 +0000115 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
116 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Matt Arsenaulte306a322014-10-21 16:25:08 +0000117
Matt Arsenault4e466652014-04-16 01:41:30 +0000118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
125
Tom Stellard9fa17912013-08-14 23:24:45 +0000126 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000127 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000128 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
129
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000130 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000131 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000132 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
133 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
134 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
135 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000136
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000137 // We only support LOAD/STORE and vector manipulation ops for vectors
138 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000139 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000140 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000141 switch (Op) {
Tom Stellard967bf582014-02-13 23:34:15 +0000142 case ISD::LOAD:
143 case ISD::STORE:
144 case ISD::BUILD_VECTOR:
145 case ISD::BITCAST:
146 case ISD::EXTRACT_VECTOR_ELT:
147 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000148 case ISD::INSERT_SUBVECTOR:
149 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000150 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000151 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000152 case ISD::CONCAT_VECTORS:
153 setOperationAction(Op, VT, Custom);
154 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000155 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000156 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000157 break;
158 }
159 }
160 }
161
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000162 // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that
163 // is expanded to avoid having two separate loops in case the index is a VGPR.
164
Matt Arsenault61001bb2015-11-25 19:58:34 +0000165 // Most operations are naturally 32-bit vector operations. We only support
166 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
167 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
168 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
169 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
170
171 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
172 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
173
174 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
175 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
176
177 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
178 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
179 }
180
Matt Arsenault71e66762016-05-21 02:27:49 +0000181 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
182 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
183 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
184 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000185
Tom Stellard354a43c2016-04-01 18:27:37 +0000186 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling,
187 // and output demarshalling
188 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
189 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
190
191 // We can't return success/failure, only the old value,
192 // let LLVM add the comparison
193 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand);
194 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand);
195
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000196 if (getSubtarget()->hasFlatAddressSpace()) {
Matt Arsenault99c14522016-04-25 19:27:24 +0000197 setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom);
198 setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom);
199 }
200
Matt Arsenault71e66762016-05-21 02:27:49 +0000201 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
202 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
203
204 // On SI this is s_memtime and s_memrealtime on VI.
205 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
Matt Arsenault0bb294b2016-06-17 22:27:03 +0000206 setOperationAction(ISD::TRAP, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000207
208 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
209 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
210
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000211 if (Subtarget->getGeneration() >= SISubtarget::SEA_ISLANDS) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000212 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
213 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
214 setOperationAction(ISD::FRINT, MVT::f64, Legal);
215 }
216
217 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
218
219 setOperationAction(ISD::FSIN, MVT::f32, Custom);
220 setOperationAction(ISD::FCOS, MVT::f32, Custom);
221 setOperationAction(ISD::FDIV, MVT::f32, Custom);
222 setOperationAction(ISD::FDIV, MVT::f64, Custom);
223
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000224 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000225 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000226 setTargetDAGCombine(ISD::FMINNUM);
227 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000228 setTargetDAGCombine(ISD::SMIN);
229 setTargetDAGCombine(ISD::SMAX);
230 setTargetDAGCombine(ISD::UMIN);
231 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000232 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000233 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000234 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000235 setTargetDAGCombine(ISD::UINT_TO_FP);
Matt Arsenault9cd90712016-04-14 01:42:16 +0000236 setTargetDAGCombine(ISD::FCANONICALIZE);
Matt Arsenault364a6742014-06-11 17:50:44 +0000237
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000238 // All memory operations. Some folding on the pointer operand is done to help
239 // matching the constant offsets in the addressing modes.
240 setTargetDAGCombine(ISD::LOAD);
241 setTargetDAGCombine(ISD::STORE);
242 setTargetDAGCombine(ISD::ATOMIC_LOAD);
243 setTargetDAGCombine(ISD::ATOMIC_STORE);
244 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
245 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
246 setTargetDAGCombine(ISD::ATOMIC_SWAP);
247 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
248 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
249 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
250 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
251 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
252 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
253 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
254 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
255 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
256 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
257
Christian Konigeecebd02013-03-26 14:04:02 +0000258 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000259}
260
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000261const SISubtarget *SITargetLowering::getSubtarget() const {
262 return static_cast<const SISubtarget *>(Subtarget);
263}
264
Tom Stellard0125f2a2013-06-25 02:39:35 +0000265//===----------------------------------------------------------------------===//
266// TargetLowering queries
267//===----------------------------------------------------------------------===//
268
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000269bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
270 const CallInst &CI,
271 unsigned IntrID) const {
272 switch (IntrID) {
273 case Intrinsic::amdgcn_atomic_inc:
274 case Intrinsic::amdgcn_atomic_dec:
275 Info.opc = ISD::INTRINSIC_W_CHAIN;
276 Info.memVT = MVT::getVT(CI.getType());
277 Info.ptrVal = CI.getOperand(0);
278 Info.align = 0;
279 Info.vol = false;
280 Info.readMem = true;
281 Info.writeMem = true;
282 return true;
283 default:
284 return false;
285 }
286}
287
Matt Arsenaulte306a322014-10-21 16:25:08 +0000288bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
289 EVT) const {
290 // SI has some legal vector types, but no legal vector operations. Say no
291 // shuffles are legal in order to prefer scalarizing some vector operations.
292 return false;
293}
294
Tom Stellard70580f82015-07-20 14:28:41 +0000295bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
296 // Flat instructions do not have offsets, and only have the register
297 // address.
298 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
299}
300
Matt Arsenault711b3902015-08-07 20:18:34 +0000301bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
302 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
303 // additionally can do r + r + i with addr64. 32-bit has more addressing
304 // mode options. Depending on the resource constant, it can also do
305 // (i64 r0) + (i32 r1) * (i14 i).
306 //
307 // Private arrays end up using a scratch buffer most of the time, so also
308 // assume those use MUBUF instructions. Scratch loads / stores are currently
309 // implemented as mubuf instructions with offen bit set, so slightly
310 // different than the normal addr64.
311 if (!isUInt<12>(AM.BaseOffs))
312 return false;
313
314 // FIXME: Since we can split immediate into soffset and immediate offset,
315 // would it make sense to allow any immediate?
316
317 switch (AM.Scale) {
318 case 0: // r + i or just i, depending on HasBaseReg.
319 return true;
320 case 1:
321 return true; // We have r + r or r + i.
322 case 2:
323 if (AM.HasBaseReg) {
324 // Reject 2 * r + r.
325 return false;
326 }
327
328 // Allow 2 * r as r + r
329 // Or 2 * r + i is allowed as r + r + i.
330 return true;
331 default: // Don't allow n * r
332 return false;
333 }
334}
335
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000336bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
337 const AddrMode &AM, Type *Ty,
338 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000339 // No global is ever allowed as a base.
340 if (AM.BaseGV)
341 return false;
342
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000343 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000344 case AMDGPUAS::GLOBAL_ADDRESS: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000345 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) {
Tom Stellard70580f82015-07-20 14:28:41 +0000346 // Assume the we will use FLAT for all global memory accesses
347 // on VI.
348 // FIXME: This assumption is currently wrong. On VI we still use
349 // MUBUF instructions for the r + i addressing mode. As currently
350 // implemented, the MUBUF instructions only work on buffer < 4GB.
351 // It may be possible to support > 4GB buffers with MUBUF instructions,
352 // by setting the stride value in the resource descriptor which would
353 // increase the size limit to (stride * 4GB). However, this is risky,
354 // because it has never been validated.
355 return isLegalFlatAddressingMode(AM);
356 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000357
Matt Arsenault711b3902015-08-07 20:18:34 +0000358 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000359 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000360 case AMDGPUAS::CONSTANT_ADDRESS: {
361 // If the offset isn't a multiple of 4, it probably isn't going to be
362 // correctly aligned.
363 if (AM.BaseOffs % 4 != 0)
364 return isLegalMUBUFAddressingMode(AM);
365
366 // There are no SMRD extloads, so if we have to do a small type access we
367 // will use a MUBUF load.
368 // FIXME?: We also need to do this if unaligned, but we don't know the
369 // alignment here.
370 if (DL.getTypeStoreSize(Ty) < 4)
371 return isLegalMUBUFAddressingMode(AM);
372
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000373 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000374 // SMRD instructions have an 8-bit, dword offset on SI.
375 if (!isUInt<8>(AM.BaseOffs / 4))
376 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000377 } else if (Subtarget->getGeneration() == SISubtarget::SEA_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000378 // On CI+, this can also be a 32-bit literal constant offset. If it fits
379 // in 8-bits, it can use a smaller encoding.
380 if (!isUInt<32>(AM.BaseOffs / 4))
381 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000382 } else if (Subtarget->getGeneration() == SISubtarget::VOLCANIC_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000383 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
384 if (!isUInt<20>(AM.BaseOffs))
385 return false;
386 } else
387 llvm_unreachable("unhandled generation");
388
389 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
390 return true;
391
392 if (AM.Scale == 1 && AM.HasBaseReg)
393 return true;
394
395 return false;
396 }
397
398 case AMDGPUAS::PRIVATE_ADDRESS:
Matt Arsenault711b3902015-08-07 20:18:34 +0000399 return isLegalMUBUFAddressingMode(AM);
400
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000401 case AMDGPUAS::LOCAL_ADDRESS:
402 case AMDGPUAS::REGION_ADDRESS: {
403 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
404 // field.
405 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
406 // an 8-bit dword offset but we don't know the alignment here.
407 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000408 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000409
410 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
411 return true;
412
413 if (AM.Scale == 1 && AM.HasBaseReg)
414 return true;
415
Matt Arsenault5015a892014-08-15 17:17:07 +0000416 return false;
417 }
Tom Stellard70580f82015-07-20 14:28:41 +0000418 case AMDGPUAS::FLAT_ADDRESS:
Matt Arsenault7d1b6c82016-04-29 06:25:10 +0000419 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
420 // For an unknown address space, this usually means that this is for some
421 // reason being used for pure arithmetic, and not based on some addressing
422 // computation. We don't have instructions that compute pointers with any
423 // addressing modes, so treat them as having no offset like flat
424 // instructions.
Tom Stellard70580f82015-07-20 14:28:41 +0000425 return isLegalFlatAddressingMode(AM);
426
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000427 default:
428 llvm_unreachable("unhandled address space");
429 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000430}
431
Matt Arsenaulte6986632015-01-14 01:35:22 +0000432bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000433 unsigned AddrSpace,
434 unsigned Align,
435 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000436 if (IsFast)
437 *IsFast = false;
438
Matt Arsenault1018c892014-04-24 17:08:26 +0000439 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
440 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000441 if (!VT.isSimple() || VT == MVT::Other)
442 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000443
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000444 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
445 AddrSpace == AMDGPUAS::REGION_ADDRESS) {
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000446 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
447 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
448 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000449 bool AlignedBy4 = (Align % 4 == 0);
450 if (IsFast)
451 *IsFast = AlignedBy4;
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000452
Sanjay Patelce74db92015-09-03 15:03:19 +0000453 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000454 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000455
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000456 if (Subtarget->hasUnalignedBufferAccess()) {
457 // If we have an uniform constant load, it still requires using a slow
458 // buffer instruction if unaligned.
459 if (IsFast) {
460 *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS) ?
461 (Align % 4 == 0) : true;
462 }
463
464 return true;
465 }
466
Tom Stellard33e64c62015-02-04 20:49:52 +0000467 // Smaller than dword value must be aligned.
Tom Stellard33e64c62015-02-04 20:49:52 +0000468 if (VT.bitsLT(MVT::i32))
469 return false;
470
Matt Arsenault1018c892014-04-24 17:08:26 +0000471 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
472 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000473 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000474 if (IsFast)
475 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000476
477 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000478}
479
Matt Arsenault46645fa2014-07-28 17:49:26 +0000480EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
481 unsigned SrcAlign, bool IsMemset,
482 bool ZeroMemset,
483 bool MemcpyStrSrc,
484 MachineFunction &MF) const {
485 // FIXME: Should account for address space here.
486
487 // The default fallback uses the private pointer size as a guess for a type to
488 // use. Make sure we switch these to 64-bit accesses.
489
490 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
491 return MVT::v4i32;
492
493 if (Size >= 8 && DstAlign >= 4)
494 return MVT::v2i32;
495
496 // Use the default.
497 return MVT::Other;
498}
499
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000500static bool isFlatGlobalAddrSpace(unsigned AS) {
501 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
502 AS == AMDGPUAS::FLAT_ADDRESS ||
503 AS == AMDGPUAS::CONSTANT_ADDRESS;
504}
505
506bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
507 unsigned DestAS) const {
Matt Arsenault37fefd62016-06-10 02:18:02 +0000508 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000509}
510
Tom Stellarda6f24c62015-12-15 20:55:55 +0000511bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
512 const MemSDNode *MemNode = cast<MemSDNode>(N);
513 const Value *Ptr = MemNode->getMemOperand()->getValue();
514
515 // UndefValue means this is a load of a kernel input. These are uniform.
Tom Stellard418beb72016-07-13 14:23:33 +0000516 // Sometimes LDS instructions have constant pointers.
517 // If Ptr is null, then that means this mem operand contains a
518 // PseudoSourceValue like GOT.
519 if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) ||
520 isa<Constant>(Ptr) || isa<GlobalValue>(Ptr))
Tom Stellarda6f24c62015-12-15 20:55:55 +0000521 return true;
522
Tom Stellard418beb72016-07-13 14:23:33 +0000523 const Instruction *I = dyn_cast<Instruction>(Ptr);
Tom Stellarda6f24c62015-12-15 20:55:55 +0000524 return I && I->getMetadata("amdgpu.uniform");
525}
526
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000527TargetLoweringBase::LegalizeTypeAction
528SITargetLowering::getPreferredVectorAction(EVT VT) const {
529 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
530 return TypeSplitVector;
531
532 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000533}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000534
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000535bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
536 Type *Ty) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000537 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000538 return TII->isInlineConstant(Imm);
539}
540
Tom Stellard2e045bb2016-01-20 00:13:22 +0000541bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
542
543 // SimplifySetCC uses this function to determine whether or not it should
544 // create setcc with i1 operands. We don't have instructions for i1 setcc.
545 if (VT == MVT::i1 && Op == ISD::SETCC)
546 return false;
547
548 return TargetLowering::isTypeDesirableForOp(Op, VT);
549}
550
Jan Veselyfea814d2016-06-21 20:46:20 +0000551SDValue SITargetLowering::LowerParameterPtr(SelectionDAG &DAG,
552 const SDLoc &SL, SDValue Chain,
553 unsigned Offset) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000554 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000555 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000556 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaultac234b62015-11-30 21:15:57 +0000557 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000558
Matt Arsenault86033ca2014-07-28 17:31:39 +0000559 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000560 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000561 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
562 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
Jan Veselyfea814d2016-06-21 20:46:20 +0000563 return DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
564 DAG.getConstant(Offset, SL, PtrVT));
565}
566SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
567 const SDLoc &SL, SDValue Chain,
568 unsigned Offset, bool Signed) const {
569 const DataLayout &DL = DAG.getDataLayout();
570 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
571 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
572 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Mehdi Amini44ede332015-07-09 02:09:04 +0000573 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000574 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
575
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000576 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000577
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000578 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000579 if (MemVT.isFloatingPoint())
580 ExtTy = ISD::EXTLOAD;
581
Jan Veselyfea814d2016-06-21 20:46:20 +0000582 SDValue Ptr = LowerParameterPtr(DAG, SL, Chain, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +0000583 return DAG.getLoad(ISD::UNINDEXED, ExtTy, VT, SL, Chain, Ptr, PtrOffset,
584 PtrInfo, MemVT, Align, MachineMemOperand::MONonTemporal |
585 MachineMemOperand::MOInvariant);
Tom Stellard94593ee2013-06-03 17:40:18 +0000586}
587
Christian Konig2c8f6d52013-03-07 09:03:52 +0000588SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000589 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000590 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
591 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000592 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000593
594 MachineFunction &MF = DAG.getMachineFunction();
595 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000596 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000597 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000598
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000599 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) {
Matt Arsenaultd48da142015-11-02 23:23:02 +0000600 const Function *Fn = MF.getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000601 DiagnosticInfoUnsupported NoGraphicsHSA(
602 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
Matt Arsenaultd48da142015-11-02 23:23:02 +0000603 DAG.getContext()->diagnose(NoGraphicsHSA);
Diana Picus81bc3172016-05-26 15:24:55 +0000604 return DAG.getEntryNode();
Matt Arsenaultd48da142015-11-02 23:23:02 +0000605 }
606
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000607 // Create stack objects that are used for emitting debugger prologue if
608 // "amdgpu-debugger-emit-prologue" attribute was specified.
609 if (ST.debuggerEmitPrologue())
610 createDebuggerPrologueStackObjects(MF);
611
Christian Konig2c8f6d52013-03-07 09:03:52 +0000612 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000613 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000614
615 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000616 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000617
618 // First check if it's a PS input addr
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000619 if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000620 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000621
Marek Olsakfccabaf2016-01-13 11:45:36 +0000622 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000623 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000624 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000625 ++PSInputNum;
626 continue;
627 }
628
Marek Olsakfccabaf2016-01-13 11:45:36 +0000629 Info->markPSInputAllocated(PSInputNum);
630 if (Arg.Used)
631 Info->PSInputEna |= 1 << PSInputNum;
632
633 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000634 }
635
Matt Arsenault539ca882016-05-05 20:27:02 +0000636 if (AMDGPU::isShader(CallConv)) {
637 // Second split vertices into their elements
638 if (Arg.VT.isVector()) {
639 ISD::InputArg NewArg = Arg;
640 NewArg.Flags.setSplit();
641 NewArg.VT = Arg.VT.getVectorElementType();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000642
Matt Arsenault539ca882016-05-05 20:27:02 +0000643 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
644 // three or five element vertex only needs three or five registers,
645 // NOT four or eight.
646 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
647 unsigned NumElements = ParamType->getVectorNumElements();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000648
Matt Arsenault539ca882016-05-05 20:27:02 +0000649 for (unsigned j = 0; j != NumElements; ++j) {
650 Splits.push_back(NewArg);
651 NewArg.PartOffset += NewArg.VT.getStoreSize();
652 }
653 } else {
654 Splits.push_back(Arg);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000655 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000656 }
657 }
658
659 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000660 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
661 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000662
Christian Konig99ee0f42013-03-07 09:04:14 +0000663 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000664 //
665 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
666 // PSInputAddr, the user wants to enable some bits after the compilation
667 // based on run-time states. Since we can't know what the final PSInputEna
668 // will look like, so we shouldn't do anything here and the user should take
669 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000670 //
671 // Otherwise, the following restrictions apply:
672 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
673 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
674 // enabled too.
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000675 if (CallConv == CallingConv::AMDGPU_PS &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000676 ((Info->getPSInputAddr() & 0x7F) == 0 ||
NAKAMURA Takumife1202c2016-06-20 00:37:41 +0000677 ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000678 CCInfo.AllocateReg(AMDGPU::VGPR0);
679 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000680 Info->markPSInputAllocated(0);
681 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000682 }
683
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000684 if (!AMDGPU::isShader(CallConv)) {
Tom Stellardaf775432013-10-23 00:44:32 +0000685 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
686 Splits);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000687
688 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 Arsenault296b8492016-02-12 06:31:30 +0000723 if (Info->hasFlatScratchInit()) {
724 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
725 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SReg_64RegClass);
726 CCInfo.AllocateReg(FlatScratchInitReg);
727 }
728
Christian Konig2c8f6d52013-03-07 09:03:52 +0000729 AnalyzeFormalArguments(CCInfo, Splits);
730
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000731 SmallVector<SDValue, 16> Chains;
732
Christian Konig2c8f6d52013-03-07 09:03:52 +0000733 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
734
Christian Konigb7be72d2013-05-17 09:46:48 +0000735 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000736 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000737 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000738 continue;
739 }
740
Christian Konig2c8f6d52013-03-07 09:03:52 +0000741 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000742 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000743
744 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000745 VT = Ins[i].VT;
746 EVT MemVT = Splits[i].VT;
Tom Stellardb5798b02015-06-26 21:15:03 +0000747 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
748 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000749 // The first 36 bytes of the input buffer contains information about
750 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000751 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000752 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000753 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000754
Craig Toppere3dcce92015-08-01 22:20:21 +0000755 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000756 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000757 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Tom Stellardca7ecf32014-08-22 18:49:31 +0000758 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
759 // On SI local pointers are just offsets into LDS, so they are always
760 // less than 16-bits. On CI and newer they could potentially be
761 // real pointers, so we can't guarantee their size.
762 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
763 DAG.getValueType(MVT::i16));
764 }
765
Tom Stellarded882c22013-06-03 17:40:11 +0000766 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000767 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000768 continue;
769 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000770 assert(VA.isRegLoc() && "Parameter must be in a register!");
771
772 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000773
774 if (VT == MVT::i64) {
775 // For now assume it is a pointer
776 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
777 &AMDGPU::SReg_64RegClass);
778 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000779 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
780 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000781 continue;
782 }
783
784 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
785
786 Reg = MF.addLiveIn(Reg, RC);
787 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
788
Christian Konig2c8f6d52013-03-07 09:03:52 +0000789 if (Arg.VT.isVector()) {
790
791 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000792 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000793 unsigned NumElements = ParamType->getVectorNumElements();
794
795 SmallVector<SDValue, 4> Regs;
796 Regs.push_back(Val);
797 for (unsigned j = 1; j != NumElements; ++j) {
798 Reg = ArgLocs[ArgIdx++].getLocReg();
799 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000800
801 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
802 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000803 }
804
805 // Fill up the missing vector elements
806 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000807 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000808
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000809 InVals.push_back(DAG.getBuildVector(Arg.VT, DL, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000810 continue;
811 }
812
813 InVals.push_back(Val);
814 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000815
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000816 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
817 // these from the dispatch pointer.
818
819 // Start adding system SGPRs.
820 if (Info->hasWorkGroupIDX()) {
821 unsigned Reg = Info->addWorkGroupIDX();
822 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
823 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000824 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000825
826 if (Info->hasWorkGroupIDY()) {
827 unsigned Reg = Info->addWorkGroupIDY();
828 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
829 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000830 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000831
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000832 if (Info->hasWorkGroupIDZ()) {
833 unsigned Reg = Info->addWorkGroupIDZ();
834 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
835 CCInfo.AllocateReg(Reg);
836 }
837
838 if (Info->hasWorkGroupInfo()) {
839 unsigned Reg = Info->addWorkGroupInfo();
840 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
841 CCInfo.AllocateReg(Reg);
842 }
843
844 if (Info->hasPrivateSegmentWaveByteOffset()) {
845 // Scratch wave offset passed in system SGPR.
Tom Stellardf110f8f2016-04-14 16:27:03 +0000846 unsigned PrivateSegmentWaveByteOffsetReg;
847
848 if (AMDGPU::isShader(CallConv)) {
849 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
850 Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
851 } else
852 PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset();
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000853
854 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
855 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
856 }
857
858 // Now that we've figured out where the scratch register inputs are, see if
859 // should reserve the arguments and use them directly.
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000860 bool HasStackObjects = MF.getFrameInfo()->hasStackObjects();
Matt Arsenault296b8492016-02-12 06:31:30 +0000861 // Record that we know we have non-spill stack objects so we don't need to
862 // check all stack objects later.
863 if (HasStackObjects)
864 Info->setHasNonSpillStackObjects(true);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000865
866 if (ST.isAmdHsaOS()) {
867 // TODO: Assume we will spill without optimizations.
868 if (HasStackObjects) {
869 // If we have stack objects, we unquestionably need the private buffer
870 // resource. For the HSA ABI, this will be the first 4 user SGPR
871 // inputs. We can reserve those and use them directly.
872
873 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
874 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
875 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
876
877 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
878 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
879 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
880 } else {
881 unsigned ReservedBufferReg
882 = TRI->reservedPrivateSegmentBufferReg(MF);
883 unsigned ReservedOffsetReg
884 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
885
886 // We tentatively reserve the last registers (skipping the last two
887 // which may contain VCC). After register allocation, we'll replace
888 // these with the ones immediately after those which were really
889 // allocated. In the prologue copies will be inserted from the argument
890 // to these reserved registers.
891 Info->setScratchRSrcReg(ReservedBufferReg);
892 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
893 }
894 } else {
895 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
896
897 // Without HSA, relocations are used for the scratch pointer and the
898 // buffer resource setup is always inserted in the prologue. Scratch wave
899 // offset is still in an input SGPR.
900 Info->setScratchRSrcReg(ReservedBufferReg);
901
902 if (HasStackObjects) {
903 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
904 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
905 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
906 } else {
907 unsigned ReservedOffsetReg
908 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
909 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
910 }
911 }
912
913 if (Info->hasWorkItemIDX()) {
914 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
915 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
916 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000917 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000918
919 if (Info->hasWorkItemIDY()) {
920 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
921 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
922 CCInfo.AllocateReg(Reg);
923 }
924
925 if (Info->hasWorkItemIDZ()) {
926 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
927 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
928 CCInfo.AllocateReg(Reg);
929 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000930
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000931 if (Chains.empty())
932 return Chain;
933
934 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000935}
936
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000937SDValue
938SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
939 bool isVarArg,
940 const SmallVectorImpl<ISD::OutputArg> &Outs,
941 const SmallVectorImpl<SDValue> &OutVals,
942 const SDLoc &DL, SelectionDAG &DAG) const {
Marek Olsak8a0f3352016-01-13 17:23:04 +0000943 MachineFunction &MF = DAG.getMachineFunction();
944 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
945
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000946 if (!AMDGPU::isShader(CallConv))
Marek Olsak8a0f3352016-01-13 17:23:04 +0000947 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
948 OutVals, DL, DAG);
949
Marek Olsak8e9cc632016-01-13 17:23:09 +0000950 Info->setIfReturnsVoid(Outs.size() == 0);
951
Marek Olsak8a0f3352016-01-13 17:23:04 +0000952 SmallVector<ISD::OutputArg, 48> Splits;
953 SmallVector<SDValue, 48> SplitVals;
954
955 // Split vectors into their elements.
956 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
957 const ISD::OutputArg &Out = Outs[i];
958
959 if (Out.VT.isVector()) {
960 MVT VT = Out.VT.getVectorElementType();
961 ISD::OutputArg NewOut = Out;
962 NewOut.Flags.setSplit();
963 NewOut.VT = VT;
964
965 // We want the original number of vector elements here, e.g.
966 // three or five, not four or eight.
967 unsigned NumElements = Out.ArgVT.getVectorNumElements();
968
969 for (unsigned j = 0; j != NumElements; ++j) {
970 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
971 DAG.getConstant(j, DL, MVT::i32));
972 SplitVals.push_back(Elem);
973 Splits.push_back(NewOut);
974 NewOut.PartOffset += NewOut.VT.getStoreSize();
975 }
976 } else {
977 SplitVals.push_back(OutVals[i]);
978 Splits.push_back(Out);
979 }
980 }
981
982 // CCValAssign - represent the assignment of the return value to a location.
983 SmallVector<CCValAssign, 48> RVLocs;
984
985 // CCState - Info about the registers and stack slots.
986 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
987 *DAG.getContext());
988
989 // Analyze outgoing return values.
990 AnalyzeReturn(CCInfo, Splits);
991
992 SDValue Flag;
993 SmallVector<SDValue, 48> RetOps;
994 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
995
996 // Copy the result values into the output registers.
997 for (unsigned i = 0, realRVLocIdx = 0;
998 i != RVLocs.size();
999 ++i, ++realRVLocIdx) {
1000 CCValAssign &VA = RVLocs[i];
1001 assert(VA.isRegLoc() && "Can only return in registers!");
1002
1003 SDValue Arg = SplitVals[realRVLocIdx];
1004
1005 // Copied from other backends.
1006 switch (VA.getLocInfo()) {
1007 default: llvm_unreachable("Unknown loc info!");
1008 case CCValAssign::Full:
1009 break;
1010 case CCValAssign::BCvt:
1011 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1012 break;
1013 }
1014
1015 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
1016 Flag = Chain.getValue(1);
1017 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1018 }
1019
1020 // Update chain and glue.
1021 RetOps[0] = Chain;
1022 if (Flag.getNode())
1023 RetOps.push_back(Flag);
1024
Matt Arsenault9babdf42016-06-22 20:15:28 +00001025 unsigned Opc = Info->returnsVoid() ? AMDGPUISD::ENDPGM : AMDGPUISD::RETURN;
1026 return DAG.getNode(Opc, DL, MVT::Other, RetOps);
Marek Olsak8a0f3352016-01-13 17:23:04 +00001027}
1028
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001029unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1030 SelectionDAG &DAG) const {
1031 unsigned Reg = StringSwitch<unsigned>(RegName)
1032 .Case("m0", AMDGPU::M0)
1033 .Case("exec", AMDGPU::EXEC)
1034 .Case("exec_lo", AMDGPU::EXEC_LO)
1035 .Case("exec_hi", AMDGPU::EXEC_HI)
1036 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1037 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1038 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1039 .Default(AMDGPU::NoRegister);
1040
1041 if (Reg == AMDGPU::NoRegister) {
1042 report_fatal_error(Twine("invalid register name \""
1043 + StringRef(RegName) + "\"."));
1044
1045 }
1046
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001047 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001048 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1049 report_fatal_error(Twine("invalid register \""
1050 + StringRef(RegName) + "\" for subtarget."));
1051 }
1052
1053 switch (Reg) {
1054 case AMDGPU::M0:
1055 case AMDGPU::EXEC_LO:
1056 case AMDGPU::EXEC_HI:
1057 case AMDGPU::FLAT_SCR_LO:
1058 case AMDGPU::FLAT_SCR_HI:
1059 if (VT.getSizeInBits() == 32)
1060 return Reg;
1061 break;
1062 case AMDGPU::EXEC:
1063 case AMDGPU::FLAT_SCR:
1064 if (VT.getSizeInBits() == 64)
1065 return Reg;
1066 break;
1067 default:
1068 llvm_unreachable("missing register type checking");
1069 }
1070
1071 report_fatal_error(Twine("invalid type for register \""
1072 + StringRef(RegName) + "\"."));
1073}
1074
Matt Arsenault786724a2016-07-12 21:41:32 +00001075// If kill is not the last instruction, split the block so kill is always a
1076// proper terminator.
1077MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI,
1078 MachineBasicBlock *BB) const {
1079 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1080
1081 MachineBasicBlock::iterator SplitPoint(&MI);
1082 ++SplitPoint;
1083
1084 if (SplitPoint == BB->end()) {
1085 // Don't bother with a new block.
1086 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1087 return BB;
1088 }
1089
1090 MachineFunction *MF = BB->getParent();
1091 MachineBasicBlock *SplitBB
1092 = MF->CreateMachineBasicBlock(BB->getBasicBlock());
1093
Matt Arsenault786724a2016-07-12 21:41:32 +00001094 // Fix the block phi references to point to the new block for the defs in the
1095 // second piece of the block.
1096 for (MachineBasicBlock *Succ : BB->successors()) {
1097 for (MachineInstr &MI : *Succ) {
1098 if (!MI.isPHI())
1099 break;
1100
Matt Arsenault83ab0492016-07-15 00:58:09 +00001101 for (unsigned I = 2, E = MI.getNumOperands(); I != E; I += 2) {
1102 MachineOperand &FromBB = MI.getOperand(I);
Matt Arsenault786724a2016-07-12 21:41:32 +00001103 if (BB == FromBB.getMBB()) {
Matt Arsenault83ab0492016-07-15 00:58:09 +00001104 FromBB.setMBB(SplitBB);
Matt Arsenault786724a2016-07-12 21:41:32 +00001105 break;
1106 }
1107 }
1108 }
1109 }
1110
1111 MF->insert(++MachineFunction::iterator(BB), SplitBB);
1112 SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end());
1113
Matt Arsenault786724a2016-07-12 21:41:32 +00001114 SplitBB->transferSuccessors(BB);
1115 BB->addSuccessor(SplitBB);
1116
1117 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1118 return SplitBB;
1119}
1120
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001121// Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the
1122// wavefront. If the value is uniform and just happens to be in a VGPR, this
1123// will only do one iteration. In the worst case, this will loop 64 times.
1124//
1125// TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value.
1126static void emitLoadM0FromVGPRLoop(const SIInstrInfo *TII,
1127 MachineRegisterInfo &MRI,
1128 MachineBasicBlock &OrigBB,
1129 MachineBasicBlock &LoopBB,
1130 const DebugLoc &DL,
1131 MachineInstr *MovRel,
1132 const MachineOperand &IdxReg,
1133 unsigned InitReg,
1134 unsigned ResultReg,
1135 unsigned PhiReg,
1136 unsigned InitSaveExecReg,
1137 int Offset) {
1138 MachineBasicBlock::iterator I = LoopBB.begin();
1139
1140 unsigned PhiExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1141 unsigned NewExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1142 unsigned CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1143 unsigned CondReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1144
1145 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg)
1146 .addReg(InitReg)
1147 .addMBB(&OrigBB)
1148 .addReg(ResultReg)
1149 .addMBB(&LoopBB);
1150
1151 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec)
1152 .addReg(InitSaveExecReg)
1153 .addMBB(&OrigBB)
1154 .addReg(NewExec)
1155 .addMBB(&LoopBB);
1156
1157 // Read the next variant <- also loop target.
1158 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg)
1159 .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef()));
1160
1161 // Compare the just read M0 value to all possible Idx values.
1162 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg)
1163 .addReg(CurrentIdxReg)
1164 .addOperand(IdxReg);
1165
1166 // Move index from VCC into M0
1167 if (Offset == 0) {
1168 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1169 .addReg(CurrentIdxReg, RegState::Kill);
1170 } else {
1171 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1172 .addReg(CurrentIdxReg, RegState::Kill)
1173 .addImm(Offset);
1174 }
1175
1176 // Update EXEC, save the original EXEC value to VCC.
1177 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), NewExec)
1178 .addReg(CondReg, RegState::Kill);
1179
1180 MRI.setSimpleHint(NewExec, CondReg);
1181
1182 // Do the actual move.
1183 LoopBB.insert(I, MovRel);
1184
1185 // Update EXEC, switch all done bits to 0 and all todo bits to 1.
1186 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
1187 .addReg(AMDGPU::EXEC)
1188 .addReg(NewExec);
1189
1190 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use
1191 // s_cbranch_scc0?
1192
1193 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover.
1194 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
1195 .addMBB(&LoopBB);
1196}
1197
1198// This has slightly sub-optimal regalloc when the source vector is killed by
1199// the read. The register allocator does not understand that the kill is
1200// per-workitem, so is kept alive for the whole loop so we end up not re-using a
1201// subregister from it, using 1 more VGPR than necessary. This was saved when
1202// this was expanded after register allocation.
1203static MachineBasicBlock *loadM0FromVGPR(const SIInstrInfo *TII,
1204 MachineBasicBlock &MBB,
1205 MachineInstr &MI,
1206 MachineInstr *MovRel,
1207 unsigned InitResultReg,
1208 unsigned PhiReg,
1209 int Offset) {
1210 MachineFunction *MF = MBB.getParent();
1211 MachineRegisterInfo &MRI = MF->getRegInfo();
1212 const DebugLoc &DL = MI.getDebugLoc();
1213 MachineBasicBlock::iterator I(&MI);
1214
1215 unsigned DstReg = MI.getOperand(0).getReg();
1216 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1217 unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1218
1219 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec);
1220
1221 // Save the EXEC mask
1222 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec)
1223 .addReg(AMDGPU::EXEC);
1224
1225 // To insert the loop we need to split the block. Move everything after this
1226 // point to a new block, and insert a new empty block between the two.
1227 MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock();
1228 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock();
1229 MachineFunction::iterator MBBI(MBB);
1230 ++MBBI;
1231
1232 MF->insert(MBBI, LoopBB);
1233 MF->insert(MBBI, RemainderBB);
1234
1235 LoopBB->addSuccessor(LoopBB);
1236 LoopBB->addSuccessor(RemainderBB);
1237
1238 // Move the rest of the block into a new block.
1239 RemainderBB->transferSuccessors(&MBB);
1240 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end());
1241
1242 MBB.addSuccessor(LoopBB);
1243
1244 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1245
1246 emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, MovRel, *Idx,
1247 InitResultReg, DstReg, PhiReg, TmpExec, Offset);
1248
1249 MachineBasicBlock::iterator First = RemainderBB->begin();
1250 BuildMI(*RemainderBB, First, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
1251 .addReg(SaveExec);
1252
1253 MI.eraseFromParent();
1254
1255 return RemainderBB;
1256}
1257
1258// Returns subreg index, offset
1259static std::pair<unsigned, int>
1260computeIndirectRegAndOffset(const SIRegisterInfo &TRI,
1261 const TargetRegisterClass *SuperRC,
1262 unsigned VecReg,
1263 int Offset) {
1264 int NumElts = SuperRC->getSize() / 4;
1265
1266 // Skip out of bounds offsets, or else we would end up using an undefined
1267 // register.
1268 if (Offset >= NumElts || Offset < 0)
1269 return std::make_pair(AMDGPU::sub0, Offset);
1270
1271 return std::make_pair(AMDGPU::sub0 + Offset, 0);
1272}
1273
1274// Return true if the index is an SGPR and was set.
1275static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII,
1276 MachineRegisterInfo &MRI,
1277 MachineInstr &MI,
1278 int Offset) {
1279 MachineBasicBlock *MBB = MI.getParent();
1280 const DebugLoc &DL = MI.getDebugLoc();
1281 MachineBasicBlock::iterator I(&MI);
1282
1283 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1284 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
1285
1286 assert(Idx->getReg() != AMDGPU::NoRegister);
1287
1288 if (!TII->getRegisterInfo().isSGPRClass(IdxRC))
1289 return false;
1290
1291 if (Offset == 0) {
1292 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1293 .addOperand(*Idx);
1294 } else {
1295 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1296 .addOperand(*Idx)
1297 .addImm(Offset);
1298 }
1299
1300 return true;
1301}
1302
1303// Control flow needs to be inserted if indexing with a VGPR.
1304static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI,
1305 MachineBasicBlock &MBB,
1306 const SIInstrInfo *TII) {
1307 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1308 MachineFunction *MF = MBB.getParent();
1309 MachineRegisterInfo &MRI = MF->getRegInfo();
1310
1311 unsigned Dst = MI.getOperand(0).getReg();
1312 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
1313 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1314
1315 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
1316
1317 unsigned SubReg;
1318 std::tie(SubReg, Offset)
1319 = computeIndirectRegAndOffset(TRI, VecRC, SrcVec->getReg(), Offset);
1320
1321 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset)) {
1322 MachineBasicBlock::iterator I(&MI);
1323 const DebugLoc &DL = MI.getDebugLoc();
1324
1325 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
1326 .addReg(SrcVec->getReg(), RegState::Undef, SubReg)
1327 .addReg(SrcVec->getReg(), RegState::Implicit);
1328 MI.eraseFromParent();
1329
1330 return &MBB;
1331 }
1332
1333 const DebugLoc &DL = MI.getDebugLoc();
1334 MachineBasicBlock::iterator I(&MI);
1335
1336 unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1337 unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1338
1339 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg);
1340
1341 MachineInstr *MovRel =
1342 BuildMI(*MF, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
1343 .addReg(SrcVec->getReg(), RegState::Undef, SubReg)
1344 .addReg(SrcVec->getReg(), RegState::Implicit);
1345
1346 return loadM0FromVGPR(TII, MBB, MI, MovRel, InitReg, PhiReg, Offset);
1347}
1348
1349static MachineBasicBlock *emitIndirectDst(MachineInstr &MI,
1350 MachineBasicBlock &MBB,
1351 const SIInstrInfo *TII) {
1352 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1353 MachineFunction *MF = MBB.getParent();
1354 MachineRegisterInfo &MRI = MF->getRegInfo();
1355
1356 unsigned Dst = MI.getOperand(0).getReg();
1357 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
1358 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1359 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val);
1360 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1361 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
1362
1363 // This can be an immediate, but will be folded later.
1364 assert(Val->getReg());
1365
1366 unsigned SubReg;
1367 std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC,
1368 SrcVec->getReg(),
1369 Offset);
1370 if (Idx->getReg() == AMDGPU::NoRegister) {
1371 MachineBasicBlock::iterator I(&MI);
1372 const DebugLoc &DL = MI.getDebugLoc();
1373
1374 assert(Offset == 0);
1375
1376 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst)
1377 .addOperand(*SrcVec)
1378 .addOperand(*Val)
1379 .addImm(SubReg);
1380
1381 MI.eraseFromParent();
1382 return &MBB;
1383 }
1384
1385 const MCInstrDesc &MovRelDesc = TII->get(AMDGPU::V_MOVRELD_B32_e32);
1386 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset)) {
1387 MachineBasicBlock::iterator I(&MI);
1388 const DebugLoc &DL = MI.getDebugLoc();
1389
1390 MachineInstr *MovRel =
1391 BuildMI(MBB, I, DL, MovRelDesc)
1392 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst
1393 .addOperand(*Val)
1394 .addReg(Dst, RegState::ImplicitDefine)
1395 .addReg(SrcVec->getReg(), RegState::Implicit);
1396
1397 const int ImpDefIdx = MovRelDesc.getNumOperands() +
1398 MovRelDesc.getNumImplicitUses();
1399 const int ImpUseIdx = ImpDefIdx + 1;
1400
1401 MovRel->tieOperands(ImpDefIdx, ImpUseIdx);
1402 MI.eraseFromParent();
1403 return &MBB;
1404 }
1405
1406 if (Val->isReg())
1407 MRI.clearKillFlags(Val->getReg());
1408
1409 const DebugLoc &DL = MI.getDebugLoc();
1410 unsigned PhiReg = MRI.createVirtualRegister(VecRC);
1411
1412 // vdst is not actually read and just provides the base register index.
1413 MachineInstr *MovRel =
1414 BuildMI(*MF, DL, MovRelDesc)
1415 .addReg(PhiReg, RegState::Undef, SubReg) // vdst
1416 .addOperand(*Val)
1417 .addReg(Dst, RegState::ImplicitDefine)
1418 .addReg(PhiReg, RegState::Implicit);
1419
1420 const int ImpDefIdx = MovRelDesc.getNumOperands() +
1421 MovRelDesc.getNumImplicitUses();
1422 const int ImpUseIdx = ImpDefIdx + 1;
1423
1424 MovRel->tieOperands(ImpDefIdx, ImpUseIdx);
1425
1426 return loadM0FromVGPR(TII, MBB, MI, MovRel,
1427 SrcVec->getReg(), PhiReg, Offset);
1428}
1429
Matt Arsenault786724a2016-07-12 21:41:32 +00001430MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter(
1431 MachineInstr &MI, MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001432 switch (MI.getOpcode()) {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001433 case AMDGPU::SI_INIT_M0: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001434 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001435 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001436 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001437 .addOperand(MI.getOperand(0));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001438 MI.eraseFromParent();
Matt Arsenault20711b72015-02-20 22:10:45 +00001439 return BB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001440 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001441 case AMDGPU::GET_GROUPSTATICSIZE: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001442 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1443
Changpeng Fang01f60622016-03-15 17:28:44 +00001444 MachineFunction *MF = BB->getParent();
1445 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001446 DebugLoc DL = MI.getDebugLoc();
1447 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOVK_I32))
1448 .addOperand(MI.getOperand(0))
1449 .addImm(MFI->LDSSize);
1450 MI.eraseFromParent();
Changpeng Fang01f60622016-03-15 17:28:44 +00001451 return BB;
1452 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001453 case AMDGPU::SI_INDIRECT_SRC_V1:
1454 case AMDGPU::SI_INDIRECT_SRC_V2:
1455 case AMDGPU::SI_INDIRECT_SRC_V4:
1456 case AMDGPU::SI_INDIRECT_SRC_V8:
1457 case AMDGPU::SI_INDIRECT_SRC_V16:
1458 return emitIndirectSrc(MI, *BB, getSubtarget()->getInstrInfo());
1459 case AMDGPU::SI_INDIRECT_DST_V1:
1460 case AMDGPU::SI_INDIRECT_DST_V2:
1461 case AMDGPU::SI_INDIRECT_DST_V4:
1462 case AMDGPU::SI_INDIRECT_DST_V8:
1463 case AMDGPU::SI_INDIRECT_DST_V16:
1464 return emitIndirectDst(MI, *BB, getSubtarget()->getInstrInfo());
Matt Arsenault786724a2016-07-12 21:41:32 +00001465 case AMDGPU::SI_KILL:
1466 return splitKillBlock(MI, BB);
Changpeng Fang01f60622016-03-15 17:28:44 +00001467 default:
1468 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001469 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001470}
1471
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001472bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1473 // This currently forces unfolding various combinations of fsub into fma with
1474 // free fneg'd operands. As long as we have fast FMA (controlled by
1475 // isFMAFasterThanFMulAndFAdd), we should perform these.
1476
1477 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1478 // most of these combines appear to be cycle neutral but save on instruction
1479 // count / code size.
1480 return true;
1481}
1482
Mehdi Amini44ede332015-07-09 02:09:04 +00001483EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1484 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001485 if (!VT.isVector()) {
1486 return MVT::i1;
1487 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001488 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001489}
1490
Mehdi Aminieaabc512015-07-09 15:12:23 +00001491MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001492 return MVT::i32;
1493}
1494
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001495// Answering this is somewhat tricky and depends on the specific device which
1496// have different rates for fma or all f64 operations.
1497//
1498// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1499// regardless of which device (although the number of cycles differs between
1500// devices), so it is always profitable for f64.
1501//
1502// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1503// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1504// which we can always do even without fused FP ops since it returns the same
1505// result as the separate operations and since it is always full
1506// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1507// however does not support denormals, so we do report fma as faster if we have
1508// a fast fma device and require denormals.
1509//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001510bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1511 VT = VT.getScalarType();
1512
1513 if (!VT.isSimple())
1514 return false;
1515
1516 switch (VT.getSimpleVT().SimpleTy) {
1517 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001518 // This is as fast on some subtargets. However, we always have full rate f32
1519 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001520 // which we should prefer over fma. We can't use this if we want to support
1521 // denormals, so only report this in these cases.
1522 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001523 case MVT::f64:
1524 return true;
1525 default:
1526 break;
1527 }
1528
1529 return false;
1530}
1531
Tom Stellard75aadc22012-12-11 21:25:42 +00001532//===----------------------------------------------------------------------===//
1533// Custom DAG Lowering Operations
1534//===----------------------------------------------------------------------===//
1535
1536SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1537 switch (Op.getOpcode()) {
1538 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001539 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001540 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001541 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001542 SDValue Result = LowerLOAD(Op, DAG);
1543 assert((!Result.getNode() ||
1544 Result.getNode()->getNumValues() == 2) &&
1545 "Load should return a value and a chain");
1546 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001547 }
Tom Stellardaf775432013-10-23 00:44:32 +00001548
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001549 case ISD::FSIN:
1550 case ISD::FCOS:
1551 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001552 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001553 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard354a43c2016-04-01 18:27:37 +00001554 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001555 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001556 case ISD::GlobalAddress: {
1557 MachineFunction &MF = DAG.getMachineFunction();
1558 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1559 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001560 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001561 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001562 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001563 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Matt Arsenault99c14522016-04-25 19:27:24 +00001564 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG);
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001565 case ISD::TRAP: return lowerTRAP(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001566 }
1567 return SDValue();
1568}
1569
Tom Stellardf8794352012-12-19 22:10:31 +00001570/// \brief Helper function for LowerBRCOND
1571static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001572
Tom Stellardf8794352012-12-19 22:10:31 +00001573 SDNode *Parent = Value.getNode();
1574 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1575 I != E; ++I) {
1576
1577 if (I.getUse().get() != Value)
1578 continue;
1579
1580 if (I->getOpcode() == Opcode)
1581 return *I;
1582 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001583 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001584}
1585
Tom Stellardb02094e2014-07-21 15:45:01 +00001586SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
1587
Tom Stellardc98ee202015-07-16 19:40:07 +00001588 SDLoc SL(Op);
Tom Stellardb02094e2014-07-21 15:45:01 +00001589 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
1590 unsigned FrameIndex = FINode->getIndex();
1591
Matt Arsenault3a619852016-02-27 20:26:57 +00001592 // A FrameIndex node represents a 32-bit offset into scratch memory. If the
1593 // high bit of a frame index offset were to be set, this would mean that it
1594 // represented an offset of ~2GB * 64 = ~128GB from the start of the scratch
1595 // buffer, with 64 being the number of threads per wave.
Tom Stellardc98ee202015-07-16 19:40:07 +00001596 //
Matt Arsenault3a619852016-02-27 20:26:57 +00001597 // The maximum private allocation for the entire GPU is 4G, and we are
1598 // concerned with the largest the index could ever be for an individual
1599 // workitem. This will occur with the minmum dispatch size. If a program
1600 // requires more, the dispatch size will be reduced.
1601 //
1602 // With this limit, we can mark the high bit of the FrameIndex node as known
1603 // zero, which is important, because it means in most situations we can prove
1604 // that values derived from FrameIndex nodes are non-negative. This enables us
1605 // to take advantage of more addressing modes when accessing scratch buffers,
1606 // since for scratch reads/writes, the register offset must always be
1607 // positive.
1608
1609 uint64_t MaxGPUAlloc = UINT64_C(4) * 1024 * 1024 * 1024;
1610
1611 // XXX - It is unclear if partial dispatch works. Assume it works at half wave
1612 // granularity. It is probably a full wave.
1613 uint64_t MinGranularity = 32;
1614
1615 unsigned KnownBits = Log2_64(MaxGPUAlloc / MinGranularity);
1616 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), KnownBits);
Tom Stellardc98ee202015-07-16 19:40:07 +00001617
1618 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
Tom Stellardc98ee202015-07-16 19:40:07 +00001619 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
Matt Arsenault3a619852016-02-27 20:26:57 +00001620 DAG.getValueType(ExtVT));
Tom Stellardb02094e2014-07-21 15:45:01 +00001621}
1622
Tom Stellardbc4497b2016-02-12 23:45:29 +00001623bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault16f48d72016-02-13 00:36:10 +00001624 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN)
Tom Stellardbc4497b2016-02-12 23:45:29 +00001625 return false;
1626
1627 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1628 default: return false;
1629 case AMDGPUIntrinsic::amdgcn_if:
1630 case AMDGPUIntrinsic::amdgcn_else:
Matt Arsenault48d70cb2016-07-09 17:18:39 +00001631 case AMDGPUIntrinsic::amdgcn_break:
Tom Stellardbc4497b2016-02-12 23:45:29 +00001632 case AMDGPUIntrinsic::amdgcn_if_break:
1633 case AMDGPUIntrinsic::amdgcn_else_break:
1634 case AMDGPUIntrinsic::amdgcn_loop:
1635 case AMDGPUIntrinsic::amdgcn_end_cf:
1636 return true;
1637 }
1638}
1639
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001640void SITargetLowering::createDebuggerPrologueStackObjects(
1641 MachineFunction &MF) const {
1642 // Create stack objects that are used for emitting debugger prologue.
1643 //
1644 // Debugger prologue writes work group IDs and work item IDs to scratch memory
1645 // at fixed location in the following format:
1646 // offset 0: work group ID x
1647 // offset 4: work group ID y
1648 // offset 8: work group ID z
1649 // offset 16: work item ID x
1650 // offset 20: work item ID y
1651 // offset 24: work item ID z
1652 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1653 int ObjectIdx = 0;
1654
1655 // For each dimension:
1656 for (unsigned i = 0; i < 3; ++i) {
1657 // Create fixed stack object for work group ID.
1658 ObjectIdx = MF.getFrameInfo()->CreateFixedObject(4, i * 4, true);
1659 Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx);
1660 // Create fixed stack object for work item ID.
1661 ObjectIdx = MF.getFrameInfo()->CreateFixedObject(4, i * 4 + 16, true);
1662 Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx);
1663 }
1664}
1665
Tom Stellardf8794352012-12-19 22:10:31 +00001666/// This transforms the control flow intrinsics to get the branch destination as
1667/// last parameter, also switches branch target with BR if the need arise
1668SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1669 SelectionDAG &DAG) const {
1670
Andrew Trickef9de2a2013-05-25 02:42:55 +00001671 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001672
1673 SDNode *Intr = BRCOND.getOperand(1).getNode();
1674 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001675 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001676 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001677
1678 if (Intr->getOpcode() == ISD::SETCC) {
1679 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001680 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001681 Intr = SetCC->getOperand(0).getNode();
1682
1683 } else {
1684 // Get the target from BR if we don't negate the condition
1685 BR = findUser(BRCOND, ISD::BR);
1686 Target = BR->getOperand(1);
1687 }
1688
Nicolai Haehnleffbd56a2016-05-05 17:36:36 +00001689 if (!isCFIntrinsic(Intr)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +00001690 // This is a uniform branch so we don't need to legalize.
1691 return BRCOND;
1692 }
1693
1694 assert(!SetCC ||
1695 (SetCC->getConstantOperandVal(1) == 1 &&
Tom Stellardbc4497b2016-02-12 23:45:29 +00001696 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1697 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00001698
1699 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001700 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001701
1702 // operands of the new intrinsic call
1703 SmallVector<SDValue, 4> Ops;
1704 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001705 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001706 Ops.push_back(Target);
1707
1708 // build the new intrinsic call
1709 SDNode *Result = DAG.getNode(
1710 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001711 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001712
1713 if (BR) {
1714 // Give the branch instruction our target
1715 SDValue Ops[] = {
1716 BR->getOperand(0),
1717 BRCOND.getOperand(2)
1718 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001719 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1720 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1721 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001722 }
1723
1724 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1725
1726 // Copy the intrinsic results to registers
1727 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1728 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1729 if (!CopyToReg)
1730 continue;
1731
1732 Chain = DAG.getCopyToReg(
1733 Chain, DL,
1734 CopyToReg->getOperand(1),
1735 SDValue(Result, i - 1),
1736 SDValue());
1737
1738 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1739 }
1740
1741 // Remove the old intrinsic from the chain
1742 DAG.ReplaceAllUsesOfValueWith(
1743 SDValue(Intr, Intr->getNumValues() - 1),
1744 Intr->getOperand(0));
1745
1746 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001747}
1748
Matt Arsenault99c14522016-04-25 19:27:24 +00001749SDValue SITargetLowering::getSegmentAperture(unsigned AS,
1750 SelectionDAG &DAG) const {
1751 SDLoc SL;
1752 MachineFunction &MF = DAG.getMachineFunction();
1753 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00001754 unsigned UserSGPR = Info->getQueuePtrUserSGPR();
1755 assert(UserSGPR != AMDGPU::NoRegister);
1756
Matt Arsenault99c14522016-04-25 19:27:24 +00001757 SDValue QueuePtr = CreateLiveInRegister(
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00001758 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
Matt Arsenault99c14522016-04-25 19:27:24 +00001759
1760 // Offset into amd_queue_t for group_segment_aperture_base_hi /
1761 // private_segment_aperture_base_hi.
1762 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44;
1763
1764 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr,
1765 DAG.getConstant(StructOffset, SL, MVT::i64));
1766
1767 // TODO: Use custom target PseudoSourceValue.
1768 // TODO: We should use the value from the IR intrinsic call, but it might not
1769 // be available and how do we get it?
1770 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()),
1771 AMDGPUAS::CONSTANT_ADDRESS));
1772
1773 MachinePointerInfo PtrInfo(V, StructOffset);
Justin Lebar9c375812016-07-15 18:27:10 +00001774 return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr, PtrInfo,
1775 MinAlign(64, StructOffset),
1776 MachineMemOperand::MOInvariant);
Matt Arsenault99c14522016-04-25 19:27:24 +00001777}
1778
1779SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
1780 SelectionDAG &DAG) const {
1781 SDLoc SL(Op);
1782 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op);
1783
1784 SDValue Src = ASC->getOperand(0);
1785
1786 // FIXME: Really support non-0 null pointers.
1787 SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32);
1788 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
1789
1790 // flat -> local/private
1791 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
1792 if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1793 ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1794 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
1795 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
1796
1797 return DAG.getNode(ISD::SELECT, SL, MVT::i32,
1798 NonNull, Ptr, SegmentNullPtr);
1799 }
1800 }
1801
1802 // local/private -> flat
1803 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
1804 if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1805 ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1806 SDValue NonNull
1807 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
1808
1809 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG);
1810 SDValue CvtPtr
1811 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
1812
1813 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull,
1814 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr),
1815 FlatNullPtr);
1816 }
1817 }
1818
1819 // global <-> flat are no-ops and never emitted.
1820
1821 const MachineFunction &MF = DAG.getMachineFunction();
1822 DiagnosticInfoUnsupported InvalidAddrSpaceCast(
1823 *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc());
1824 DAG.getContext()->diagnose(InvalidAddrSpaceCast);
1825
1826 return DAG.getUNDEF(ASC->getValueType(0));
1827}
1828
Tom Stellard418beb72016-07-13 14:23:33 +00001829static bool shouldEmitGOTReloc(const GlobalValue *GV,
1830 const TargetMachine &TM) {
1831 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
1832 !TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
Tom Stellardb164a982016-06-25 01:59:16 +00001833}
1834
Tom Stellard418beb72016-07-13 14:23:33 +00001835bool
1836SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1837 // We can fold offsets for anything that doesn't require a GOT relocation.
1838 return GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
1839 !shouldEmitGOTReloc(GA->getGlobal(), getTargetMachine());
1840}
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001841
Tom Stellard418beb72016-07-13 14:23:33 +00001842static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV,
1843 SDLoc DL, unsigned Offset, EVT PtrVT,
1844 unsigned GAFlags = SIInstrInfo::MO_NONE) {
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001845 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is
1846 // lowered to the following code sequence:
1847 // s_getpc_b64 s[0:1]
1848 // s_add_u32 s0, s0, $symbol
1849 // s_addc_u32 s1, s1, 0
1850 //
1851 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
1852 // a fixup or relocation is emitted to replace $symbol with a literal
1853 // constant, which is a pc-relative offset from the encoding of the $symbol
1854 // operand to the global variable.
1855 //
1856 // What we want here is an offset from the value returned by s_getpc
1857 // (which is the address of the s_add_u32 instruction) to the global
1858 // variable, but since the encoding of $symbol starts 4 bytes after the start
1859 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too
1860 // small. This requires us to add 4 to the global variable offset in order to
1861 // compute the correct address.
Tom Stellard418beb72016-07-13 14:23:33 +00001862 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
1863 GAFlags);
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001864 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, GA);
1865}
1866
Tom Stellard418beb72016-07-13 14:23:33 +00001867SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1868 SDValue Op,
1869 SelectionDAG &DAG) const {
1870 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1871
1872 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS &&
1873 GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS)
1874 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1875
1876 SDLoc DL(GSD);
1877 const GlobalValue *GV = GSD->getGlobal();
1878 EVT PtrVT = Op.getValueType();
1879
1880 if (!shouldEmitGOTReloc(GV, getTargetMachine()))
1881 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT);
1882
1883 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT,
1884 SIInstrInfo::MO_GOTPCREL);
1885
1886 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext());
1887 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
1888 const DataLayout &DataLayout = DAG.getDataLayout();
1889 unsigned Align = DataLayout.getABITypeAlignment(PtrTy);
1890 // FIXME: Use a PseudoSourceValue once those can be assigned an address space.
1891 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
1892
Justin Lebar9c375812016-07-15 18:27:10 +00001893 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align,
1894 MachineMemOperand::MOInvariant);
Tom Stellard418beb72016-07-13 14:23:33 +00001895}
1896
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001897SDValue SITargetLowering::lowerTRAP(SDValue Op,
1898 SelectionDAG &DAG) const {
1899 const MachineFunction &MF = DAG.getMachineFunction();
1900 DiagnosticInfoUnsupported NoTrap(*MF.getFunction(),
1901 "trap handler not supported",
1902 Op.getDebugLoc(),
1903 DS_Warning);
1904 DAG.getContext()->diagnose(NoTrap);
1905
1906 // Emit s_endpgm.
1907
1908 // FIXME: This should really be selected to s_trap, but that requires
1909 // setting up the trap handler for it o do anything.
Matt Arsenault9babdf42016-06-22 20:15:28 +00001910 return DAG.getNode(AMDGPUISD::ENDPGM, SDLoc(Op), MVT::Other,
1911 Op.getOperand(0));
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001912}
1913
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001914SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain,
1915 const SDLoc &DL, SDValue V) const {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001916 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
1917 // the destination register.
1918 //
Tom Stellardfc92e772015-05-12 14:18:14 +00001919 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1920 // so we will end up with redundant moves to m0.
1921 //
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001922 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
1923
1924 // A Null SDValue creates a glue result.
1925 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
1926 V, Chain);
1927 return SDValue(M0, 0);
Tom Stellardfc92e772015-05-12 14:18:14 +00001928}
1929
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001930SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1931 SDValue Op,
1932 MVT VT,
1933 unsigned Offset) const {
1934 SDLoc SL(Op);
1935 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1936 DAG.getEntryNode(), Offset, false);
1937 // The local size values will have the hi 16-bits as zero.
1938 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1939 DAG.getValueType(VT));
1940}
1941
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001942static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
Matt Arsenaulte0132462016-01-30 05:19:45 +00001943 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001944 "non-hsa intrinsic with hsa target",
1945 DL.getDebugLoc());
1946 DAG.getContext()->diagnose(BadIntrin);
1947 return DAG.getUNDEF(VT);
1948}
1949
1950static SDValue emitRemovedIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
1951 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
1952 "intrinsic not supported on subtarget",
1953 DL.getDebugLoc());
Matt Arsenaulte0132462016-01-30 05:19:45 +00001954 DAG.getContext()->diagnose(BadIntrin);
1955 return DAG.getUNDEF(VT);
1956}
1957
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001958SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1959 SelectionDAG &DAG) const {
1960 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001961 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001962 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001963
1964 EVT VT = Op.getValueType();
1965 SDLoc DL(Op);
1966 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1967
Sanjay Patela2607012015-09-16 16:31:21 +00001968 // TODO: Should this propagate fast-math-flags?
1969
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001970 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001971 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault48ab5262016-04-25 19:27:18 +00001972 case Intrinsic::amdgcn_queue_ptr: {
Matt Arsenault800fecf2016-01-11 21:18:33 +00001973 if (!Subtarget->isAmdHsaOS()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00001974 DiagnosticInfoUnsupported BadIntrin(
1975 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
1976 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00001977 DAG.getContext()->diagnose(BadIntrin);
1978 return DAG.getUNDEF(VT);
1979 }
1980
Matt Arsenault48ab5262016-04-25 19:27:18 +00001981 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ?
1982 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00001983 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
Matt Arsenault48ab5262016-04-25 19:27:18 +00001984 TRI->getPreloadedValue(MF, Reg), VT);
1985 }
Jan Veselyfea814d2016-06-21 20:46:20 +00001986 case Intrinsic::amdgcn_implicitarg_ptr: {
1987 unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
1988 return LowerParameterPtr(DAG, DL, DAG.getEntryNode(), offset);
1989 }
Matt Arsenaultdc4ebad2016-04-29 21:16:52 +00001990 case Intrinsic::amdgcn_kernarg_segment_ptr: {
1991 unsigned Reg
1992 = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
1993 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
1994 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001995 case Intrinsic::amdgcn_rcp:
1996 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
1997 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001998 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001999 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002000 case Intrinsic::amdgcn_rsq_legacy: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002001 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002002 return emitRemovedIntrinsicError(DAG, DL, VT);
2003
2004 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
2005 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +00002006 case Intrinsic::amdgcn_rsq_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002007 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00002008 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00002009
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002010 Type *Type = VT.getTypeForEVT(*DAG.getContext());
2011 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
2012 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
2013
2014 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
2015 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
2016 DAG.getConstantFP(Max, DL, VT));
2017 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
2018 DAG.getConstantFP(Min, DL, VT));
2019 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002020 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002021 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002022 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002023
Tom Stellardec2e43c2014-09-22 15:35:29 +00002024 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2025 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002026 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002027 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002028 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002029
Tom Stellardec2e43c2014-09-22 15:35:29 +00002030 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2031 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002032 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002033 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002034 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002035
Tom Stellardec2e43c2014-09-22 15:35:29 +00002036 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2037 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002038 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002039 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002040 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002041
Tom Stellardec2e43c2014-09-22 15:35:29 +00002042 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2043 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002044 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002045 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002046 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002047
Tom Stellardec2e43c2014-09-22 15:35:29 +00002048 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2049 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002050 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002051 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002052 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002053
Tom Stellardec2e43c2014-09-22 15:35:29 +00002054 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2055 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002056 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002057 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002058 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002059
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002060 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2061 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002062 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002063 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002064 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002065
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002066 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2067 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002068 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002069 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002070 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002071
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002072 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2073 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenaultbef34e22016-01-22 21:30:34 +00002074 case Intrinsic::amdgcn_read_workdim:
2075 case AMDGPUIntrinsic::AMDGPU_read_workdim: // Legacy name.
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002076 // Really only 2 bits.
2077 return lowerImplicitZextParam(DAG, Op, MVT::i8,
2078 getImplicitParameterOffset(MFI, GRID_DIM));
Matt Arsenault43976df2016-01-30 04:25:19 +00002079 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002080 case Intrinsic::r600_read_tgid_x:
2081 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002082 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002083 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002084 case Intrinsic::r600_read_tgid_y:
2085 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002086 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002087 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002088 case Intrinsic::r600_read_tgid_z:
2089 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002090 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002091 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002092 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002093 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002094 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002095 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002096 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002097 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002098 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002099 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002100 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002101 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002102 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002103 case AMDGPUIntrinsic::SI_load_const: {
2104 SDValue Ops[] = {
2105 Op.getOperand(1),
2106 Op.getOperand(2)
2107 };
2108
2109 MachineMemOperand *MMO = MF.getMachineMemOperand(
2110 MachinePointerInfo(),
2111 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
2112 VT.getStoreSize(), 4);
2113 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
2114 Op->getVTList(), Ops, VT, MMO);
2115 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002116 case AMDGPUIntrinsic::SI_vs_load_input:
2117 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
2118 Op.getOperand(1),
2119 Op.getOperand(2),
2120 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00002121
Tom Stellard2a9d9472015-05-12 15:00:46 +00002122 case AMDGPUIntrinsic::SI_fs_constant: {
2123 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2124 SDValue Glue = M0.getValue(1);
2125 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
2126 DAG.getConstant(2, DL, MVT::i32), // P0
2127 Op.getOperand(1), Op.getOperand(2), Glue);
2128 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00002129 case AMDGPUIntrinsic::SI_packf16:
2130 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
2131 return DAG.getUNDEF(MVT::i32);
2132 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00002133 case AMDGPUIntrinsic::SI_fs_interp: {
2134 SDValue IJ = Op.getOperand(4);
2135 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2136 DAG.getConstant(0, DL, MVT::i32));
2137 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2138 DAG.getConstant(1, DL, MVT::i32));
2139 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2140 SDValue Glue = M0.getValue(1);
2141 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
2142 DAG.getVTList(MVT::f32, MVT::Glue),
2143 I, Op.getOperand(1), Op.getOperand(2), Glue);
2144 Glue = SDValue(P1.getNode(), 1);
2145 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
2146 Op.getOperand(1), Op.getOperand(2), Glue);
2147 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00002148 case Intrinsic::amdgcn_interp_p1: {
2149 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
2150 SDValue Glue = M0.getValue(1);
2151 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
2152 Op.getOperand(2), Op.getOperand(3), Glue);
2153 }
2154 case Intrinsic::amdgcn_interp_p2: {
2155 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
2156 SDValue Glue = SDValue(M0.getNode(), 1);
2157 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
2158 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
2159 Glue);
2160 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002161 case Intrinsic::amdgcn_sin:
2162 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
2163
2164 case Intrinsic::amdgcn_cos:
2165 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
2166
2167 case Intrinsic::amdgcn_log_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002168 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002169 return SDValue();
2170
2171 DiagnosticInfoUnsupported BadIntrin(
2172 *MF.getFunction(), "intrinsic not supported on subtarget",
2173 DL.getDebugLoc());
2174 DAG.getContext()->diagnose(BadIntrin);
2175 return DAG.getUNDEF(VT);
2176 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002177 case Intrinsic::amdgcn_ldexp:
2178 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
2179 Op.getOperand(1), Op.getOperand(2));
Matt Arsenault74015162016-05-28 00:19:52 +00002180
2181 case Intrinsic::amdgcn_fract:
2182 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
2183
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002184 case Intrinsic::amdgcn_class:
2185 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
2186 Op.getOperand(1), Op.getOperand(2));
2187 case Intrinsic::amdgcn_div_fmas:
2188 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
2189 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
2190 Op.getOperand(4));
2191
2192 case Intrinsic::amdgcn_div_fixup:
2193 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
2194 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
2195
2196 case Intrinsic::amdgcn_trig_preop:
2197 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
2198 Op.getOperand(1), Op.getOperand(2));
2199 case Intrinsic::amdgcn_div_scale: {
2200 // 3rd parameter required to be a constant.
2201 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2202 if (!Param)
2203 return DAG.getUNDEF(VT);
2204
2205 // Translate to the operands expected by the machine instruction. The
2206 // first parameter must be the same as the first instruction.
2207 SDValue Numerator = Op.getOperand(1);
2208 SDValue Denominator = Op.getOperand(2);
2209
2210 // Note this order is opposite of the machine instruction's operations,
2211 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
2212 // intrinsic has the numerator as the first operand to match a normal
2213 // division operation.
2214
2215 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
2216
2217 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
2218 Denominator, Numerator);
2219 }
Matt Arsenaultc96e1de2016-07-18 18:35:05 +00002220 case Intrinsic::amdgcn_sffbh:
2221 case AMDGPUIntrinsic::AMDGPU_flbit_i32: // Legacy name.
2222 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002223 default:
2224 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
2225 }
2226}
2227
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002228SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
2229 SelectionDAG &DAG) const {
2230 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2231 switch (IntrID) {
2232 case Intrinsic::amdgcn_atomic_inc:
2233 case Intrinsic::amdgcn_atomic_dec: {
2234 MemSDNode *M = cast<MemSDNode>(Op);
2235 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
2236 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
2237 SDValue Ops[] = {
2238 M->getOperand(0), // Chain
2239 M->getOperand(2), // Ptr
2240 M->getOperand(3) // Value
2241 };
2242
2243 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
2244 M->getMemoryVT(), M->getMemOperand());
2245 }
2246 default:
2247 return SDValue();
2248 }
2249}
2250
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002251SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
2252 SelectionDAG &DAG) const {
2253 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00002254 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002255 SDValue Chain = Op.getOperand(0);
2256 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2257
2258 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00002259 case AMDGPUIntrinsic::SI_sendmsg: {
2260 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
2261 SDValue Glue = Chain.getValue(1);
2262 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
2263 Op.getOperand(2), Glue);
2264 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002265 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002266 SDValue Ops[] = {
2267 Chain,
2268 Op.getOperand(2),
2269 Op.getOperand(3),
2270 Op.getOperand(4),
2271 Op.getOperand(5),
2272 Op.getOperand(6),
2273 Op.getOperand(7),
2274 Op.getOperand(8),
2275 Op.getOperand(9),
2276 Op.getOperand(10),
2277 Op.getOperand(11),
2278 Op.getOperand(12),
2279 Op.getOperand(13),
2280 Op.getOperand(14)
2281 };
2282
2283 EVT VT = Op.getOperand(3).getValueType();
2284
2285 MachineMemOperand *MMO = MF.getMachineMemOperand(
2286 MachinePointerInfo(),
2287 MachineMemOperand::MOStore,
2288 VT.getStoreSize(), 4);
2289 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
2290 Op->getVTList(), Ops, VT, MMO);
2291 }
Matt Arsenault00568682016-07-13 06:04:22 +00002292 case AMDGPUIntrinsic::AMDGPU_kill: {
Matt Arsenault03006fd2016-07-19 16:27:56 +00002293 SDValue Src = Op.getOperand(2);
2294 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) {
Matt Arsenault00568682016-07-13 06:04:22 +00002295 if (!K->isNegative())
2296 return Chain;
Matt Arsenault03006fd2016-07-19 16:27:56 +00002297
2298 SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32);
2299 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne);
Matt Arsenault00568682016-07-13 06:04:22 +00002300 }
2301
Matt Arsenault03006fd2016-07-19 16:27:56 +00002302 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src);
2303 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast);
Matt Arsenault00568682016-07-13 06:04:22 +00002304 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002305 default:
2306 return SDValue();
2307 }
2308}
2309
Tom Stellard81d871d2013-11-13 23:36:50 +00002310SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2311 SDLoc DL(Op);
2312 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00002313 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00002314 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00002315
Matt Arsenaulta1436412016-02-10 18:21:45 +00002316 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
2317 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected");
Matt Arsenault6dfda962016-02-10 18:21:39 +00002318 // FIXME: Copied from PPC
2319 // First, load into 32 bits, then truncate to 1 bit.
2320
2321 SDValue Chain = Load->getChain();
2322 SDValue BasePtr = Load->getBasePtr();
2323 MachineMemOperand *MMO = Load->getMemOperand();
2324
2325 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
2326 BasePtr, MVT::i8, MMO);
2327
2328 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002329 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00002330 NewLD.getValue(1)
2331 };
2332
2333 return DAG.getMergeValues(Ops, DL);
2334 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002335
Matt Arsenaulta1436412016-02-10 18:21:45 +00002336 if (!MemVT.isVector())
2337 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002338
Matt Arsenaulta1436412016-02-10 18:21:45 +00002339 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
2340 "Custom lowering for non-i32 vectors hasn't been implemented.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002341
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002342 unsigned AS = Load->getAddressSpace();
2343 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT,
2344 AS, Load->getAlignment())) {
2345 SDValue Ops[2];
2346 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG);
2347 return DAG.getMergeValues(Ops, DL);
2348 }
2349
2350 unsigned NumElements = MemVT.getVectorNumElements();
2351 switch (AS) {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002352 case AMDGPUAS::CONSTANT_ADDRESS:
2353 if (isMemOpUniform(Load))
2354 return SDValue();
2355 // Non-uniform loads will be selected to MUBUF instructions, so they
2356 // have the same legalization requires ments as global and private
2357 // loads.
2358 //
2359 // Fall-through
2360 case AMDGPUAS::GLOBAL_ADDRESS:
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002361 case AMDGPUAS::FLAT_ADDRESS:
2362 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00002363 return SplitVectorLoad(Op, DAG);
2364 // v4 loads are supported for private and global memory.
2365 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002366 case AMDGPUAS::PRIVATE_ADDRESS: {
2367 // Depending on the setting of the private_element_size field in the
2368 // resource descriptor, we can only make private accesses up to a certain
2369 // size.
2370 switch (Subtarget->getMaxPrivateElementSize()) {
2371 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002372 return scalarizeVectorLoad(Load, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002373 case 8:
2374 if (NumElements > 2)
2375 return SplitVectorLoad(Op, DAG);
2376 return SDValue();
2377 case 16:
2378 // Same as global/flat
2379 if (NumElements > 4)
2380 return SplitVectorLoad(Op, DAG);
2381 return SDValue();
2382 default:
2383 llvm_unreachable("unsupported private_element_size");
2384 }
2385 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002386 case AMDGPUAS::LOCAL_ADDRESS: {
2387 if (NumElements > 2)
2388 return SplitVectorLoad(Op, DAG);
2389
2390 if (NumElements == 2)
2391 return SDValue();
2392
Matt Arsenaulta1436412016-02-10 18:21:45 +00002393 // If properly aligned, if we split we might be able to use ds_read_b64.
2394 return SplitVectorLoad(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002395 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00002396 default:
2397 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00002398 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002399}
2400
Tom Stellard0ec134f2014-02-04 17:18:40 +00002401SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2402 if (Op.getValueType() != MVT::i64)
2403 return SDValue();
2404
2405 SDLoc DL(Op);
2406 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002407
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002408 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2409 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002410
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002411 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
2412 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
2413
2414 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
2415 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002416
2417 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
2418
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002419 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
2420 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002421
2422 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
2423
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002424 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002425 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002426}
2427
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002428// Catch division cases where we can use shortcuts with rcp and rsq
2429// instructions.
2430SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002431 SDLoc SL(Op);
2432 SDValue LHS = Op.getOperand(0);
2433 SDValue RHS = Op.getOperand(1);
2434 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002435 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002436
2437 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002438 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
2439 CLHS->isExactlyValue(1.0)) {
2440 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
2441 // the CI documentation has a worst case error of 1 ulp.
2442 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
2443 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002444
2445 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002446 //
2447 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
2448 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002449 if (RHS.getOpcode() == ISD::FSQRT)
2450 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
2451
2452 // 1.0 / x -> rcp(x)
2453 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
2454 }
2455 }
2456
Wei Dinged0f97f2016-06-09 19:17:15 +00002457 const SDNodeFlags *Flags = Op->getFlags();
2458
2459 if (Unsafe || Flags->hasAllowReciprocal()) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002460 // Turn into multiply by the reciprocal.
2461 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00002462 SDNodeFlags Flags;
2463 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002464 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00002465 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002466 }
2467
2468 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002469}
2470
2471SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher538d09d02016-06-07 20:27:12 +00002472 if (SDValue FastLowered = LowerFastFDIV(Op, DAG))
2473 return FastLowered;
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002474
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002475 SDLoc SL(Op);
2476 SDValue LHS = Op.getOperand(0);
2477 SDValue RHS = Op.getOperand(1);
2478
Wei Dinged0f97f2016-06-09 19:17:15 +00002479 // faster 2.5 ulp fdiv when using -amdgpu-fast-fdiv flag
2480 if (EnableAMDGPUFastFDIV) {
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002481 // This does not support denormals.
Wei Dinged0f97f2016-06-09 19:17:15 +00002482 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002483
Wei Dinged0f97f2016-06-09 19:17:15 +00002484 const APFloat K0Val(BitsToFloat(0x6f800000));
2485 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002486
Wei Dinged0f97f2016-06-09 19:17:15 +00002487 const APFloat K1Val(BitsToFloat(0x2f800000));
2488 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002489
Wei Dinged0f97f2016-06-09 19:17:15 +00002490 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
2491
2492 EVT SetCCVT =
2493 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
2494
2495 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
2496
2497 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
2498
2499 // TODO: Should this propagate fast-math-flags?
2500
2501 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
2502
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002503 // rcp does not support denormals.
Wei Dinged0f97f2016-06-09 19:17:15 +00002504 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
2505
2506 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
2507
2508 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002509 }
2510
Wei Dinged0f97f2016-06-09 19:17:15 +00002511 // Generates more precise fpdiv32.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002512 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002513
Wei Dinged0f97f2016-06-09 19:17:15 +00002514 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002515
Wei Dinged0f97f2016-06-09 19:17:15 +00002516 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, RHS, RHS, LHS);
2517 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, LHS, RHS, LHS);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002518
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002519 // Denominator is scaled to not be denormal, so using rcp is ok.
Wei Dinged0f97f2016-06-09 19:17:15 +00002520 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002521
Wei Dinged0f97f2016-06-09 19:17:15 +00002522 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002523
Wei Dinged0f97f2016-06-09 19:17:15 +00002524 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, ApproxRcp, One);
2525 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, ApproxRcp);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002526
Wei Dinged0f97f2016-06-09 19:17:15 +00002527 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, NumeratorScaled, Fma1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002528
Wei Dinged0f97f2016-06-09 19:17:15 +00002529 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, NumeratorScaled);
2530 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul);
2531 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, NumeratorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002532
Wei Dinged0f97f2016-06-09 19:17:15 +00002533 SDValue Scale = NumeratorScaled.getValue(1);
2534 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, Fma4, Fma1, Fma3, Scale);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002535
Wei Dinged0f97f2016-06-09 19:17:15 +00002536 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002537}
2538
2539SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002540 if (DAG.getTarget().Options.UnsafeFPMath)
2541 return LowerFastFDIV(Op, DAG);
2542
2543 SDLoc SL(Op);
2544 SDValue X = Op.getOperand(0);
2545 SDValue Y = Op.getOperand(1);
2546
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002547 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002548
2549 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
2550
2551 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
2552
2553 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
2554
2555 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
2556
2557 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
2558
2559 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
2560
2561 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
2562
2563 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
2564
2565 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
2566 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
2567
2568 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
2569 NegDivScale0, Mul, DivScale1);
2570
2571 SDValue Scale;
2572
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002573 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002574 // Workaround a hardware bug on SI where the condition output from div_scale
2575 // is not usable.
2576
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002577 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002578
2579 // Figure out if the scale to use for div_fmas.
2580 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
2581 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
2582 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
2583 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
2584
2585 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
2586 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
2587
2588 SDValue Scale0Hi
2589 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
2590 SDValue Scale1Hi
2591 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
2592
2593 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
2594 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
2595 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
2596 } else {
2597 Scale = DivScale1.getValue(1);
2598 }
2599
2600 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
2601 Fma4, Fma3, Mul, Scale);
2602
2603 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002604}
2605
2606SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
2607 EVT VT = Op.getValueType();
2608
2609 if (VT == MVT::f32)
2610 return LowerFDIV32(Op, DAG);
2611
2612 if (VT == MVT::f64)
2613 return LowerFDIV64(Op, DAG);
2614
2615 llvm_unreachable("Unexpected type for fdiv");
2616}
2617
Tom Stellard81d871d2013-11-13 23:36:50 +00002618SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2619 SDLoc DL(Op);
2620 StoreSDNode *Store = cast<StoreSDNode>(Op);
2621 EVT VT = Store->getMemoryVT();
2622
Matt Arsenault95245662016-02-11 05:32:46 +00002623 if (VT == MVT::i1) {
2624 return DAG.getTruncStore(Store->getChain(), DL,
2625 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
2626 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00002627 }
2628
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002629 assert(VT.isVector() &&
2630 Store->getValue().getValueType().getScalarType() == MVT::i32);
2631
2632 unsigned AS = Store->getAddressSpace();
2633 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
2634 AS, Store->getAlignment())) {
2635 return expandUnalignedStore(Store, DAG);
2636 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002637
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002638 unsigned NumElements = VT.getVectorNumElements();
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002639 switch (AS) {
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002640 case AMDGPUAS::GLOBAL_ADDRESS:
2641 case AMDGPUAS::FLAT_ADDRESS:
2642 if (NumElements > 4)
2643 return SplitVectorStore(Op, DAG);
2644 return SDValue();
2645 case AMDGPUAS::PRIVATE_ADDRESS: {
2646 switch (Subtarget->getMaxPrivateElementSize()) {
2647 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002648 return scalarizeVectorStore(Store, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002649 case 8:
2650 if (NumElements > 2)
2651 return SplitVectorStore(Op, DAG);
2652 return SDValue();
2653 case 16:
2654 if (NumElements > 4)
2655 return SplitVectorStore(Op, DAG);
2656 return SDValue();
2657 default:
2658 llvm_unreachable("unsupported private_element_size");
2659 }
2660 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002661 case AMDGPUAS::LOCAL_ADDRESS: {
2662 if (NumElements > 2)
2663 return SplitVectorStore(Op, DAG);
2664
2665 if (NumElements == 2)
2666 return Op;
2667
Matt Arsenault95245662016-02-11 05:32:46 +00002668 // If properly aligned, if we split we might be able to use ds_write_b64.
2669 return SplitVectorStore(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002670 }
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002671 default:
2672 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00002673 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002674}
2675
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002676SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002677 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002678 EVT VT = Op.getValueType();
2679 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00002680 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002681 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
2682 DAG.getNode(ISD::FMUL, DL, VT, Arg,
2683 DAG.getConstantFP(0.5/M_PI, DL,
2684 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002685
2686 switch (Op.getOpcode()) {
2687 case ISD::FCOS:
2688 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
2689 case ISD::FSIN:
2690 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
2691 default:
2692 llvm_unreachable("Wrong trig opcode");
2693 }
2694}
2695
Tom Stellard354a43c2016-04-01 18:27:37 +00002696SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
2697 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
2698 assert(AtomicNode->isCompareAndSwap());
2699 unsigned AS = AtomicNode->getAddressSpace();
2700
2701 // No custom lowering required for local address space
2702 if (!isFlatGlobalAddrSpace(AS))
2703 return Op;
2704
2705 // Non-local address space requires custom lowering for atomic compare
2706 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
2707 SDLoc DL(Op);
2708 SDValue ChainIn = Op.getOperand(0);
2709 SDValue Addr = Op.getOperand(1);
2710 SDValue Old = Op.getOperand(2);
2711 SDValue New = Op.getOperand(3);
2712 EVT VT = Op.getValueType();
2713 MVT SimpleVT = VT.getSimpleVT();
2714 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
2715
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002716 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
Tom Stellard354a43c2016-04-01 18:27:37 +00002717 SDValue Ops[] = { ChainIn, Addr, NewOld };
Matt Arsenault88701812016-06-09 23:42:48 +00002718
2719 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(),
2720 Ops, VT, AtomicNode->getMemOperand());
Tom Stellard354a43c2016-04-01 18:27:37 +00002721}
2722
Tom Stellard75aadc22012-12-11 21:25:42 +00002723//===----------------------------------------------------------------------===//
2724// Custom DAG optimizations
2725//===----------------------------------------------------------------------===//
2726
Matt Arsenault364a6742014-06-11 17:50:44 +00002727SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00002728 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00002729 EVT VT = N->getValueType(0);
2730 EVT ScalarVT = VT.getScalarType();
2731 if (ScalarVT != MVT::f32)
2732 return SDValue();
2733
2734 SelectionDAG &DAG = DCI.DAG;
2735 SDLoc DL(N);
2736
2737 SDValue Src = N->getOperand(0);
2738 EVT SrcVT = Src.getValueType();
2739
2740 // TODO: We could try to match extracting the higher bytes, which would be
2741 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
2742 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
2743 // about in practice.
2744 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
2745 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
2746 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
2747 DCI.AddToWorklist(Cvt.getNode());
2748 return Cvt;
2749 }
2750 }
2751
Matt Arsenault364a6742014-06-11 17:50:44 +00002752 return SDValue();
2753}
2754
Eric Christopher6c5b5112015-03-11 18:43:21 +00002755/// \brief Return true if the given offset Size in bytes can be folded into
2756/// the immediate offsets of a memory instruction for the given address space.
2757static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002758 const SISubtarget &STI) {
Eric Christopher6c5b5112015-03-11 18:43:21 +00002759 switch (AS) {
2760 case AMDGPUAS::GLOBAL_ADDRESS: {
2761 // MUBUF instructions a 12-bit offset in bytes.
2762 return isUInt<12>(OffsetSize);
2763 }
2764 case AMDGPUAS::CONSTANT_ADDRESS: {
2765 // SMRD instructions have an 8-bit offset in dwords on SI and
2766 // a 20-bit offset in bytes on VI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002767 if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Eric Christopher6c5b5112015-03-11 18:43:21 +00002768 return isUInt<20>(OffsetSize);
2769 else
2770 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
2771 }
2772 case AMDGPUAS::LOCAL_ADDRESS:
2773 case AMDGPUAS::REGION_ADDRESS: {
2774 // The single offset versions have a 16-bit offset in bytes.
2775 return isUInt<16>(OffsetSize);
2776 }
2777 case AMDGPUAS::PRIVATE_ADDRESS:
2778 // Indirect register addressing does not use any offsets.
2779 default:
2780 return 0;
2781 }
2782}
2783
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002784// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
2785
2786// This is a variant of
2787// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
2788//
2789// The normal DAG combiner will do this, but only if the add has one use since
2790// that would increase the number of instructions.
2791//
2792// This prevents us from seeing a constant offset that can be folded into a
2793// memory instruction's addressing mode. If we know the resulting add offset of
2794// a pointer can be folded into an addressing offset, we can replace the pointer
2795// operand with the add of new constant offset. This eliminates one of the uses,
2796// and may allow the remaining use to also be simplified.
2797//
2798SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
2799 unsigned AddrSpace,
2800 DAGCombinerInfo &DCI) const {
2801 SDValue N0 = N->getOperand(0);
2802 SDValue N1 = N->getOperand(1);
2803
2804 if (N0.getOpcode() != ISD::ADD)
2805 return SDValue();
2806
2807 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
2808 if (!CN1)
2809 return SDValue();
2810
2811 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2812 if (!CAdd)
2813 return SDValue();
2814
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002815 // If the resulting offset is too large, we can't fold it into the addressing
2816 // mode offset.
2817 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002818 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget()))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002819 return SDValue();
2820
2821 SelectionDAG &DAG = DCI.DAG;
2822 SDLoc SL(N);
2823 EVT VT = N->getValueType(0);
2824
2825 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002826 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002827
2828 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
2829}
2830
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002831SDValue SITargetLowering::performAndCombine(SDNode *N,
2832 DAGCombinerInfo &DCI) const {
2833 if (DCI.isBeforeLegalize())
2834 return SDValue();
2835
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002836 if (SDValue Base = AMDGPUTargetLowering::performAndCombine(N, DCI))
2837 return Base;
2838
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002839 SelectionDAG &DAG = DCI.DAG;
2840
2841 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
2842 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
2843 SDValue LHS = N->getOperand(0);
2844 SDValue RHS = N->getOperand(1);
2845
2846 if (LHS.getOpcode() == ISD::SETCC &&
2847 RHS.getOpcode() == ISD::SETCC) {
2848 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
2849 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
2850
2851 SDValue X = LHS.getOperand(0);
2852 SDValue Y = RHS.getOperand(0);
2853 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
2854 return SDValue();
2855
2856 if (LCC == ISD::SETO) {
2857 if (X != LHS.getOperand(1))
2858 return SDValue();
2859
2860 if (RCC == ISD::SETUNE) {
2861 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
2862 if (!C1 || !C1->isInfinity() || C1->isNegative())
2863 return SDValue();
2864
2865 const uint32_t Mask = SIInstrFlags::N_NORMAL |
2866 SIInstrFlags::N_SUBNORMAL |
2867 SIInstrFlags::N_ZERO |
2868 SIInstrFlags::P_ZERO |
2869 SIInstrFlags::P_SUBNORMAL |
2870 SIInstrFlags::P_NORMAL;
2871
2872 static_assert(((~(SIInstrFlags::S_NAN |
2873 SIInstrFlags::Q_NAN |
2874 SIInstrFlags::N_INFINITY |
2875 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
2876 "mask not equal");
2877
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002878 SDLoc DL(N);
2879 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2880 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002881 }
2882 }
2883 }
2884
2885 return SDValue();
2886}
2887
Matt Arsenaultf2290332015-01-06 23:00:39 +00002888SDValue SITargetLowering::performOrCombine(SDNode *N,
2889 DAGCombinerInfo &DCI) const {
2890 SelectionDAG &DAG = DCI.DAG;
2891 SDValue LHS = N->getOperand(0);
2892 SDValue RHS = N->getOperand(1);
2893
Matt Arsenault3b082382016-04-12 18:24:38 +00002894 EVT VT = N->getValueType(0);
2895 if (VT == MVT::i64) {
2896 // TODO: This could be a generic combine with a predicate for extracting the
2897 // high half of an integer being free.
2898
2899 // (or i64:x, (zero_extend i32:y)) ->
2900 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
2901 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
2902 RHS.getOpcode() != ISD::ZERO_EXTEND)
2903 std::swap(LHS, RHS);
2904
2905 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
2906 SDValue ExtSrc = RHS.getOperand(0);
2907 EVT SrcVT = ExtSrc.getValueType();
2908 if (SrcVT == MVT::i32) {
2909 SDLoc SL(N);
2910 SDValue LowLHS, HiBits;
2911 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
2912 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
2913
2914 DCI.AddToWorklist(LowOr.getNode());
2915 DCI.AddToWorklist(HiBits.getNode());
2916
2917 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
2918 LowOr, HiBits);
2919 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2920 }
2921 }
2922 }
2923
Matt Arsenaultf2290332015-01-06 23:00:39 +00002924 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
2925 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
2926 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
2927 SDValue Src = LHS.getOperand(0);
2928 if (Src != RHS.getOperand(0))
2929 return SDValue();
2930
2931 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
2932 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
2933 if (!CLHS || !CRHS)
2934 return SDValue();
2935
2936 // Only 10 bits are used.
2937 static const uint32_t MaxMask = 0x3ff;
2938
2939 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002940 SDLoc DL(N);
2941 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2942 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00002943 }
2944
2945 return SDValue();
2946}
2947
2948SDValue SITargetLowering::performClassCombine(SDNode *N,
2949 DAGCombinerInfo &DCI) const {
2950 SelectionDAG &DAG = DCI.DAG;
2951 SDValue Mask = N->getOperand(1);
2952
2953 // fp_class x, 0 -> false
2954 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
2955 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002956 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002957 }
2958
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002959 if (N->getOperand(0).isUndef())
2960 return DAG.getUNDEF(MVT::i1);
2961
Matt Arsenaultf2290332015-01-06 23:00:39 +00002962 return SDValue();
2963}
2964
Matt Arsenault9cd90712016-04-14 01:42:16 +00002965// Constant fold canonicalize.
2966SDValue SITargetLowering::performFCanonicalizeCombine(
2967 SDNode *N,
2968 DAGCombinerInfo &DCI) const {
2969 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
2970 if (!CFP)
2971 return SDValue();
2972
2973 SelectionDAG &DAG = DCI.DAG;
2974 const APFloat &C = CFP->getValueAPF();
2975
2976 // Flush denormals to 0 if not enabled.
2977 if (C.isDenormal()) {
2978 EVT VT = N->getValueType(0);
2979 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
2980 return DAG.getConstantFP(0.0, SDLoc(N), VT);
2981
2982 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
2983 return DAG.getConstantFP(0.0, SDLoc(N), VT);
2984 }
2985
2986 if (C.isNaN()) {
2987 EVT VT = N->getValueType(0);
2988 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
2989 if (C.isSignaling()) {
2990 // Quiet a signaling NaN.
2991 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
2992 }
2993
2994 // Make sure it is the canonical NaN bitpattern.
2995 //
2996 // TODO: Can we use -1 as the canonical NaN value since it's an inline
2997 // immediate?
2998 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
2999 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3000 }
3001
3002 return SDValue(CFP, 0);
3003}
3004
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003005static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
3006 switch (Opc) {
3007 case ISD::FMAXNUM:
3008 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003009 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003010 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003011 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003012 return AMDGPUISD::UMAX3;
3013 case ISD::FMINNUM:
3014 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003015 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003016 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003017 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003018 return AMDGPUISD::UMIN3;
3019 default:
3020 llvm_unreachable("Not a min/max opcode");
3021 }
3022}
3023
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003024static SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3025 SDValue Op0, SDValue Op1, bool Signed) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003026 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
3027 if (!K1)
3028 return SDValue();
3029
3030 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
3031 if (!K0)
3032 return SDValue();
3033
Matt Arsenaultf639c322016-01-28 20:53:42 +00003034 if (Signed) {
3035 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
3036 return SDValue();
3037 } else {
3038 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
3039 return SDValue();
3040 }
3041
3042 EVT VT = K0->getValueType(0);
3043 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
3044 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
3045}
3046
3047static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
3048 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
3049 return true;
3050
3051 return DAG.isKnownNeverNaN(Op);
3052}
3053
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003054static SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3055 SDValue Op0, SDValue Op1) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003056 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
3057 if (!K1)
3058 return SDValue();
3059
3060 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
3061 if (!K0)
3062 return SDValue();
3063
3064 // Ordered >= (although NaN inputs should have folded away by now).
3065 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
3066 if (Cmp == APFloat::cmpGreaterThan)
3067 return SDValue();
3068
3069 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
3070 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
3071 // give the other result, which is different from med3 with a NaN input.
3072 SDValue Var = Op0.getOperand(0);
3073 if (!isKnownNeverSNan(DAG, Var))
3074 return SDValue();
3075
3076 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
3077 Var, SDValue(K0, 0), SDValue(K1, 0));
3078}
3079
3080SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
3081 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003082 SelectionDAG &DAG = DCI.DAG;
3083
3084 unsigned Opc = N->getOpcode();
3085 SDValue Op0 = N->getOperand(0);
3086 SDValue Op1 = N->getOperand(1);
3087
3088 // Only do this if the inner op has one use since this will just increases
3089 // register pressure for no benefit.
3090
Matt Arsenault5b39b342016-01-28 20:53:48 +00003091 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
3092 // max(max(a, b), c) -> max3(a, b, c)
3093 // min(min(a, b), c) -> min3(a, b, c)
3094 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
3095 SDLoc DL(N);
3096 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3097 DL,
3098 N->getValueType(0),
3099 Op0.getOperand(0),
3100 Op0.getOperand(1),
3101 Op1);
3102 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003103
Matt Arsenault5b39b342016-01-28 20:53:48 +00003104 // Try commuted.
3105 // max(a, max(b, c)) -> max3(a, b, c)
3106 // min(a, min(b, c)) -> min3(a, b, c)
3107 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
3108 SDLoc DL(N);
3109 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3110 DL,
3111 N->getValueType(0),
3112 Op0,
3113 Op1.getOperand(0),
3114 Op1.getOperand(1));
3115 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003116 }
3117
Matt Arsenaultf639c322016-01-28 20:53:42 +00003118 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
3119 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
3120 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
3121 return Med3;
3122 }
3123
3124 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
3125 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
3126 return Med3;
3127 }
3128
3129 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00003130 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
3131 (Opc == AMDGPUISD::FMIN_LEGACY &&
3132 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00003133 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
3134 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
3135 return Res;
3136 }
3137
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003138 return SDValue();
3139}
3140
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003141SDValue SITargetLowering::performSetCCCombine(SDNode *N,
3142 DAGCombinerInfo &DCI) const {
3143 SelectionDAG &DAG = DCI.DAG;
3144 SDLoc SL(N);
3145
3146 SDValue LHS = N->getOperand(0);
3147 SDValue RHS = N->getOperand(1);
3148 EVT VT = LHS.getValueType();
3149
3150 if (VT != MVT::f32 && VT != MVT::f64)
3151 return SDValue();
3152
3153 // Match isinf pattern
3154 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
3155 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
3156 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
3157 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
3158 if (!CRHS)
3159 return SDValue();
3160
3161 const APFloat &APF = CRHS->getValueAPF();
3162 if (APF.isInfinity() && !APF.isNegative()) {
3163 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003164 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
3165 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003166 }
3167 }
3168
3169 return SDValue();
3170}
3171
Tom Stellard75aadc22012-12-11 21:25:42 +00003172SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
3173 DAGCombinerInfo &DCI) const {
3174 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003175 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00003176
3177 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00003178 default:
3179 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003180 case ISD::SETCC:
3181 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00003182 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003183 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003184 case ISD::SMAX:
3185 case ISD::SMIN:
3186 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00003187 case ISD::UMIN:
3188 case AMDGPUISD::FMIN_LEGACY:
3189 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003190 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00003191 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003192 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00003193 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003194 break;
3195 }
Matt Arsenault364a6742014-06-11 17:50:44 +00003196
3197 case AMDGPUISD::CVT_F32_UBYTE0:
3198 case AMDGPUISD::CVT_F32_UBYTE1:
3199 case AMDGPUISD::CVT_F32_UBYTE2:
3200 case AMDGPUISD::CVT_F32_UBYTE3: {
3201 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
Matt Arsenault364a6742014-06-11 17:50:44 +00003202 SDValue Src = N->getOperand(0);
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003203
Matt Arsenault327bb5a2016-07-01 22:47:50 +00003204 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero.
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003205 if (Src.getOpcode() == ISD::SRL) {
3206 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
3207 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
3208 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x
3209
3210 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(1))) {
3211 unsigned SrcOffset = C->getZExtValue() + 8 * Offset;
3212 if (SrcOffset < 32 && SrcOffset % 8 == 0) {
3213 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, DL,
3214 MVT::f32, Src.getOperand(0));
3215 }
3216 }
3217 }
3218
Matt Arsenault364a6742014-06-11 17:50:44 +00003219 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
3220
3221 APInt KnownZero, KnownOne;
3222 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
3223 !DCI.isBeforeLegalizeOps());
3224 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3225 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
3226 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
3227 DCI.CommitTargetLoweringOpt(TLO);
3228 }
3229
3230 break;
3231 }
3232
3233 case ISD::UINT_TO_FP: {
3234 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003235 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003236 case ISD::FADD: {
3237 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3238 break;
3239
3240 EVT VT = N->getValueType(0);
3241 if (VT != MVT::f32)
3242 break;
3243
Matt Arsenault8d630032015-02-20 22:10:41 +00003244 // Only do this if we are not trying to support denormals. v_mad_f32 does
3245 // not support denormals ever.
3246 if (Subtarget->hasFP32Denormals())
3247 break;
3248
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003249 SDValue LHS = N->getOperand(0);
3250 SDValue RHS = N->getOperand(1);
3251
3252 // These should really be instruction patterns, but writing patterns with
3253 // source modiifiers is a pain.
3254
3255 // fadd (fadd (a, a), b) -> mad 2.0, a, b
3256 if (LHS.getOpcode() == ISD::FADD) {
3257 SDValue A = LHS.getOperand(0);
3258 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003259 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003260 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003261 }
3262 }
3263
3264 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
3265 if (RHS.getOpcode() == ISD::FADD) {
3266 SDValue A = RHS.getOperand(0);
3267 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003268 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003269 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003270 }
3271 }
3272
Matt Arsenault8d630032015-02-20 22:10:41 +00003273 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003274 }
Matt Arsenault8675db12014-08-29 16:01:14 +00003275 case ISD::FSUB: {
3276 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3277 break;
3278
3279 EVT VT = N->getValueType(0);
3280
3281 // Try to get the fneg to fold into the source modifier. This undoes generic
3282 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00003283 //
3284 // Only do this if we are not trying to support denormals. v_mad_f32 does
3285 // not support denormals ever.
3286 if (VT == MVT::f32 &&
3287 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00003288 SDValue LHS = N->getOperand(0);
3289 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003290 if (LHS.getOpcode() == ISD::FADD) {
3291 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
3292
3293 SDValue A = LHS.getOperand(0);
3294 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003295 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003296 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
3297
Matt Arsenault8d630032015-02-20 22:10:41 +00003298 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003299 }
3300 }
3301
3302 if (RHS.getOpcode() == ISD::FADD) {
3303 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
3304
3305 SDValue A = RHS.getOperand(0);
3306 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003307 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003308 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003309 }
3310 }
Matt Arsenault8d630032015-02-20 22:10:41 +00003311
3312 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00003313 }
3314
3315 break;
3316 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003317 case ISD::LOAD:
3318 case ISD::STORE:
3319 case ISD::ATOMIC_LOAD:
3320 case ISD::ATOMIC_STORE:
3321 case ISD::ATOMIC_CMP_SWAP:
3322 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
3323 case ISD::ATOMIC_SWAP:
3324 case ISD::ATOMIC_LOAD_ADD:
3325 case ISD::ATOMIC_LOAD_SUB:
3326 case ISD::ATOMIC_LOAD_AND:
3327 case ISD::ATOMIC_LOAD_OR:
3328 case ISD::ATOMIC_LOAD_XOR:
3329 case ISD::ATOMIC_LOAD_NAND:
3330 case ISD::ATOMIC_LOAD_MIN:
3331 case ISD::ATOMIC_LOAD_MAX:
3332 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00003333 case ISD::ATOMIC_LOAD_UMAX:
3334 case AMDGPUISD::ATOMIC_INC:
3335 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003336 if (DCI.isBeforeLegalize())
3337 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003338
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003339 MemSDNode *MemNode = cast<MemSDNode>(N);
3340 SDValue Ptr = MemNode->getBasePtr();
3341
3342 // TODO: We could also do this for multiplies.
3343 unsigned AS = MemNode->getAddressSpace();
3344 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
3345 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
3346 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00003347 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003348
3349 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
3350 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
3351 }
3352 }
3353 break;
3354 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003355 case ISD::AND:
3356 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003357 case ISD::OR:
3358 return performOrCombine(N, DCI);
3359 case AMDGPUISD::FP_CLASS:
3360 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00003361 case ISD::FCANONICALIZE:
3362 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003363 case AMDGPUISD::FRACT:
3364 case AMDGPUISD::RCP:
3365 case AMDGPUISD::RSQ:
3366 case AMDGPUISD::RSQ_LEGACY:
3367 case AMDGPUISD::RSQ_CLAMP:
3368 case AMDGPUISD::LDEXP: {
3369 SDValue Src = N->getOperand(0);
3370 if (Src.isUndef())
3371 return Src;
3372 break;
3373 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003374 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003375 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00003376}
Christian Konigd910b7d2013-02-26 17:52:16 +00003377
Christian Konigf82901a2013-02-26 17:52:23 +00003378/// \brief Analyze the possible immediate value Op
3379///
3380/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
3381/// and the immediate value if it's a literal immediate
3382int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003383 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Christian Konigf82901a2013-02-26 17:52:23 +00003384
Tom Stellardedbf1eb2013-04-05 23:31:20 +00003385 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00003386 if (TII->isInlineConstant(Node->getAPIntValue()))
3387 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00003388
Matt Arsenault11a4d672015-02-13 19:05:03 +00003389 uint64_t Val = Node->getZExtValue();
3390 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00003391 }
3392
3393 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
3394 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
3395 return 0;
3396
3397 if (Node->getValueType(0) == MVT::f32)
3398 return FloatToBits(Node->getValueAPF().convertToFloat());
3399
3400 return -1;
3401 }
3402
3403 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00003404}
3405
Christian Konig8e06e2a2013-04-10 08:39:08 +00003406/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00003407static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00003408 switch (Idx) {
3409 default: return 0;
3410 case AMDGPU::sub0: return 0;
3411 case AMDGPU::sub1: return 1;
3412 case AMDGPU::sub2: return 2;
3413 case AMDGPU::sub3: return 3;
3414 }
3415}
3416
3417/// \brief Adjust the writemask of MIMG instructions
3418void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
3419 SelectionDAG &DAG) const {
3420 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00003421 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003422 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
3423 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00003424 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003425
3426 // Try to figure out the used register components
3427 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
3428 I != E; ++I) {
3429
3430 // Abort if we can't understand the usage
3431 if (!I->isMachineOpcode() ||
3432 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
3433 return;
3434
Tom Stellard54774e52013-10-23 02:53:47 +00003435 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
3436 // Note that subregs are packed, i.e. Lane==0 is the first bit set
3437 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
3438 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00003439 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00003440
Tom Stellard54774e52013-10-23 02:53:47 +00003441 // Set which texture component corresponds to the lane.
3442 unsigned Comp;
3443 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
3444 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00003445 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00003446 Dmask &= ~(1 << Comp);
3447 }
3448
Christian Konig8e06e2a2013-04-10 08:39:08 +00003449 // Abort if we have more than one user per component
3450 if (Users[Lane])
3451 return;
3452
3453 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00003454 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003455 }
3456
Tom Stellard54774e52013-10-23 02:53:47 +00003457 // Abort if there's no change
3458 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00003459 return;
3460
3461 // Adjust the writemask in the node
3462 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003463 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003464 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003465 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00003466 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003467
Christian Konig8b1ed282013-04-10 08:39:16 +00003468 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00003469 // (if NewDmask has only one bit set...)
3470 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003471 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
3472 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00003473 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003474 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00003475 SDValue(Node, 0), RC);
3476 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
3477 return;
3478 }
3479
Christian Konig8e06e2a2013-04-10 08:39:08 +00003480 // Update the users of the node with the new indices
3481 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
3482
3483 SDNode *User = Users[i];
3484 if (!User)
3485 continue;
3486
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003487 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003488 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
3489
3490 switch (Idx) {
3491 default: break;
3492 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
3493 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
3494 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
3495 }
3496 }
3497}
3498
Tom Stellardc98ee202015-07-16 19:40:07 +00003499static bool isFrameIndexOp(SDValue Op) {
3500 if (Op.getOpcode() == ISD::AssertZext)
3501 Op = Op.getOperand(0);
3502
3503 return isa<FrameIndexSDNode>(Op);
3504}
3505
Tom Stellard3457a842014-10-09 19:06:00 +00003506/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
3507/// with frame index operands.
3508/// LLVM assumes that inputs are to these instructions are registers.
3509void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
3510 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003511
3512 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00003513 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00003514 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00003515 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003516 continue;
3517 }
3518
Tom Stellard3457a842014-10-09 19:06:00 +00003519 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003520 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00003521 Node->getOperand(i).getValueType(),
3522 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003523 }
3524
Tom Stellard3457a842014-10-09 19:06:00 +00003525 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003526}
3527
Matt Arsenault08d84942014-06-03 23:06:13 +00003528/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00003529SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
3530 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003531 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003532 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00003533
Nicolai Haehnlec06bfa12016-07-11 21:59:43 +00003534 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() &&
3535 !TII->isGather4(Opcode))
Christian Konig8e06e2a2013-04-10 08:39:08 +00003536 adjustWritemask(Node, DAG);
3537
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003538 if (Opcode == AMDGPU::INSERT_SUBREG ||
3539 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003540 legalizeTargetIndependentNode(Node, DAG);
3541 return Node;
3542 }
Tom Stellard654d6692015-01-08 15:08:17 +00003543 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003544}
Christian Konig8b1ed282013-04-10 08:39:16 +00003545
3546/// \brief Assign the register class depending on the number of
3547/// bits set in the writemask
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003548void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
Christian Konig8b1ed282013-04-10 08:39:16 +00003549 SDNode *Node) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003550 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003551
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003552 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003553
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003554 if (TII->isVOP3(MI.getOpcode())) {
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003555 // Make sure constant bus requirements are respected.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003556 TII->legalizeOperandsVOP3(MRI, MI);
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003557 return;
3558 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00003559
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003560 if (TII->isMIMG(MI)) {
3561 unsigned VReg = MI.getOperand(0).getReg();
3562 unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4;
3563 unsigned Writemask = MI.getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003564 unsigned BitsSet = 0;
3565 for (unsigned i = 0; i < 4; ++i)
3566 BitsSet += Writemask & (1 << i) ? 1 : 0;
3567
3568 const TargetRegisterClass *RC;
3569 switch (BitsSet) {
3570 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00003571 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003572 case 2: RC = &AMDGPU::VReg_64RegClass; break;
3573 case 3: RC = &AMDGPU::VReg_96RegClass; break;
3574 }
3575
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003576 unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet);
3577 MI.setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003578 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00003579 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00003580 }
3581
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003582 // Replace unused atomics with the no return version.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003583 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003584 if (NoRetAtomicOp != -1) {
3585 if (!Node->hasAnyUseOfValue(0)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003586 MI.setDesc(TII->get(NoRetAtomicOp));
3587 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003588 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003589 }
3590
Tom Stellard354a43c2016-04-01 18:27:37 +00003591 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
3592 // instruction, because the return type of these instructions is a vec2 of
3593 // the memory type, so it can be tied to the input operand.
3594 // This means these instructions always have a use, so we need to add a
3595 // special case to check if the atomic has only one extract_subreg use,
3596 // which itself has no uses.
3597 if ((Node->hasNUsesOfValue(1, 0) &&
Nicolai Haehnle750082d2016-04-15 14:42:36 +00003598 Node->use_begin()->isMachineOpcode() &&
Tom Stellard354a43c2016-04-01 18:27:37 +00003599 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
3600 !Node->use_begin()->hasAnyUseOfValue(0))) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003601 unsigned Def = MI.getOperand(0).getReg();
Tom Stellard354a43c2016-04-01 18:27:37 +00003602
3603 // Change this into a noret atomic.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003604 MI.setDesc(TII->get(NoRetAtomicOp));
3605 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003606
3607 // If we only remove the def operand from the atomic instruction, the
3608 // extract_subreg will be left with a use of a vreg without a def.
3609 // So we need to insert an implicit_def to avoid machine verifier
3610 // errors.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003611 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
Tom Stellard354a43c2016-04-01 18:27:37 +00003612 TII->get(AMDGPU::IMPLICIT_DEF), Def);
3613 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003614 return;
3615 }
Christian Konig8b1ed282013-04-10 08:39:16 +00003616}
Tom Stellard0518ff82013-06-03 17:39:58 +00003617
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003618static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL,
3619 uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003620 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00003621 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
3622}
3623
3624MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003625 const SDLoc &DL,
Matt Arsenault485defe2014-11-05 19:01:17 +00003626 SDValue Ptr) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003627 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault485defe2014-11-05 19:01:17 +00003628
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003629 // Build the half of the subregister with the constants before building the
3630 // full 128-bit register. If we are building multiple resource descriptors,
3631 // this will allow CSEing of the 2-component register.
3632 const SDValue Ops0[] = {
3633 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
3634 buildSMovImm32(DAG, DL, 0),
3635 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
3636 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
3637 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
3638 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003639
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003640 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
3641 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00003642
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003643 // Combine the constants and the pointer.
3644 const SDValue Ops1[] = {
3645 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
3646 Ptr,
3647 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
3648 SubRegHi,
3649 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
3650 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003651
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003652 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00003653}
3654
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003655/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003656/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
3657/// of the resource descriptor) to create an offset, which is added to
3658/// the resource pointer.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003659MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL,
3660 SDValue Ptr, uint32_t RsrcDword1,
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003661 uint64_t RsrcDword2And3) const {
3662 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
3663 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
3664 if (RsrcDword1) {
3665 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003666 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
3667 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003668 }
3669
3670 SDValue DataLo = buildSMovImm32(DAG, DL,
3671 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
3672 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
3673
3674 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003675 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003676 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003677 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003678 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003679 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003680 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003681 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003682 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003683 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003684 };
3685
3686 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
3687}
3688
Tom Stellard94593ee2013-06-03 17:40:18 +00003689SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3690 const TargetRegisterClass *RC,
3691 unsigned Reg, EVT VT) const {
3692 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
3693
3694 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
3695 cast<RegisterSDNode>(VReg)->getReg(), VT);
3696}
Tom Stellardd7e6f132015-04-08 01:09:26 +00003697
3698//===----------------------------------------------------------------------===//
3699// SI Inline Assembly Support
3700//===----------------------------------------------------------------------===//
3701
3702std::pair<unsigned, const TargetRegisterClass *>
3703SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003704 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00003705 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003706
3707 if (Constraint.size() == 1) {
3708 switch (Constraint[0]) {
3709 case 's':
3710 case 'r':
3711 switch (VT.getSizeInBits()) {
3712 default:
3713 return std::make_pair(0U, nullptr);
3714 case 32:
Tom Stellardd7e6f132015-04-08 01:09:26 +00003715 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003716 case 64:
3717 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
3718 case 128:
3719 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
3720 case 256:
3721 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
3722 }
3723
3724 case 'v':
3725 switch (VT.getSizeInBits()) {
3726 default:
3727 return std::make_pair(0U, nullptr);
3728 case 32:
3729 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
3730 case 64:
3731 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
3732 case 96:
3733 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
3734 case 128:
3735 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
3736 case 256:
3737 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
3738 case 512:
3739 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
3740 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00003741 }
3742 }
3743
3744 if (Constraint.size() > 1) {
3745 const TargetRegisterClass *RC = nullptr;
3746 if (Constraint[1] == 'v') {
3747 RC = &AMDGPU::VGPR_32RegClass;
3748 } else if (Constraint[1] == 's') {
3749 RC = &AMDGPU::SGPR_32RegClass;
3750 }
3751
3752 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00003753 uint32_t Idx;
3754 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
3755 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00003756 return std::make_pair(RC->getRegister(Idx), RC);
3757 }
3758 }
3759 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
3760}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003761
3762SITargetLowering::ConstraintType
3763SITargetLowering::getConstraintType(StringRef Constraint) const {
3764 if (Constraint.size() == 1) {
3765 switch (Constraint[0]) {
3766 default: break;
3767 case 's':
3768 case 'v':
3769 return C_RegisterClass;
3770 }
3771 }
3772 return TargetLowering::getConstraintType(Constraint);
3773}