blob: e6329511f3f672146436ad2856b0b6967b8c2433 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief Custom DAG lowering for SI
12//
13//===----------------------------------------------------------------------===//
14
NAKAMURA Takumi45e0a832014-07-20 11:15:07 +000015#ifdef _MSC_VER
16// Provide M_PI.
17#define _USE_MATH_DEFINES
18#include <cmath>
19#endif
20
Christian Konig99ee0f42013-03-07 09:04:14 +000021#include "AMDGPU.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000022#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000023#include "AMDGPUSubtarget.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000024#include "SIISelLowering.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025#include "SIInstrInfo.h"
26#include "SIMachineFunctionInfo.h"
27#include "SIRegisterInfo.h"
Alexey Samsonova253bf92014-08-27 19:36:53 +000028#include "llvm/ADT/BitVector.h"
Matt Arsenault9a10cea2016-01-26 04:29:24 +000029#include "llvm/ADT/StringSwitch.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000030#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
32#include "llvm/CodeGen/MachineRegisterInfo.h"
33#include "llvm/CodeGen/SelectionDAG.h"
Wei Ding07e03712016-07-28 16:42:13 +000034#include "llvm/CodeGen/Analysis.h"
Oliver Stannard7e7d9832016-02-02 13:52:43 +000035#include "llvm/IR/DiagnosticInfo.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000036#include "llvm/IR/Function.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000037
38using namespace llvm;
39
Matt Arsenaultd486d3f2016-10-12 18:49:05 +000040static cl::opt<bool> EnableVGPRIndexMode(
41 "amdgpu-vgpr-index-mode",
42 cl::desc("Use GPR indexing mode instead of movrel for vector indexing"),
43 cl::init(false));
44
45
Tom Stellardf110f8f2016-04-14 16:27:03 +000046static unsigned findFirstFreeSGPR(CCState &CCInfo) {
47 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs();
48 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) {
49 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) {
50 return AMDGPU::SGPR0 + Reg;
51 }
52 }
53 llvm_unreachable("Cannot allocate sgpr");
54}
55
Matt Arsenault43e92fe2016-06-24 06:30:11 +000056SITargetLowering::SITargetLowering(const TargetMachine &TM,
57 const SISubtarget &STI)
Eric Christopher7792e322015-01-30 23:24:40 +000058 : AMDGPUTargetLowering(TM, STI) {
Tom Stellard1bd80722014-04-30 15:31:33 +000059 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000060 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000061
Tom Stellard334b29c2014-04-17 21:00:09 +000062 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000063 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000064
Tom Stellard436780b2014-05-15 14:41:57 +000065 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
66 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
67 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000068
Matt Arsenault61001bb2015-11-25 19:58:34 +000069 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
70 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
71
Tom Stellard436780b2014-05-15 14:41:57 +000072 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
73 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000074
Tom Stellardf0a21072014-11-18 20:39:39 +000075 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000076 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
77
Tom Stellardf0a21072014-11-18 20:39:39 +000078 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000079 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000080
Eric Christopher23a3a7c2015-02-26 00:00:24 +000081 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000082
Tom Stellard35bb18c2013-08-26 15:06:04 +000083 // We need to custom lower vector stores from local memory
Matt Arsenault71e66762016-05-21 02:27:49 +000084 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000085 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000086 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
87 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000088 setOperationAction(ISD::LOAD, MVT::i1, Custom);
Matt Arsenault2b957b52016-05-02 20:07:26 +000089
Matt Arsenaultbcdfee72016-05-02 20:13:51 +000090 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000091 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
92 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
93 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
94 setOperationAction(ISD::STORE, MVT::i1, Custom);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +000095
Matt Arsenault71e66762016-05-21 02:27:49 +000096 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
97 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000098 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 Arsenaultfa5f7672016-09-14 15:19:03 +0000235 setTargetDAGCombine(ISD::XOR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000236 setTargetDAGCombine(ISD::UINT_TO_FP);
Matt Arsenault9cd90712016-04-14 01:42:16 +0000237 setTargetDAGCombine(ISD::FCANONICALIZE);
Matt Arsenault364a6742014-06-11 17:50:44 +0000238
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000239 // All memory operations. Some folding on the pointer operand is done to help
240 // matching the constant offsets in the addressing modes.
241 setTargetDAGCombine(ISD::LOAD);
242 setTargetDAGCombine(ISD::STORE);
243 setTargetDAGCombine(ISD::ATOMIC_LOAD);
244 setTargetDAGCombine(ISD::ATOMIC_STORE);
245 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
246 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
247 setTargetDAGCombine(ISD::ATOMIC_SWAP);
248 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
249 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
250 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
251 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
252 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
253 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
254 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
255 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
256 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
257 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
258
Christian Konigeecebd02013-03-26 14:04:02 +0000259 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000260}
261
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000262const SISubtarget *SITargetLowering::getSubtarget() const {
263 return static_cast<const SISubtarget *>(Subtarget);
264}
265
Tom Stellard0125f2a2013-06-25 02:39:35 +0000266//===----------------------------------------------------------------------===//
267// TargetLowering queries
268//===----------------------------------------------------------------------===//
269
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000270bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
271 const CallInst &CI,
272 unsigned IntrID) const {
273 switch (IntrID) {
274 case Intrinsic::amdgcn_atomic_inc:
275 case Intrinsic::amdgcn_atomic_dec:
276 Info.opc = ISD::INTRINSIC_W_CHAIN;
277 Info.memVT = MVT::getVT(CI.getType());
278 Info.ptrVal = CI.getOperand(0);
279 Info.align = 0;
280 Info.vol = false;
281 Info.readMem = true;
282 Info.writeMem = true;
283 return true;
284 default:
285 return false;
286 }
287}
288
Matt Arsenaulte306a322014-10-21 16:25:08 +0000289bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
290 EVT) const {
291 // SI has some legal vector types, but no legal vector operations. Say no
292 // shuffles are legal in order to prefer scalarizing some vector operations.
293 return false;
294}
295
Tom Stellard70580f82015-07-20 14:28:41 +0000296bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
297 // Flat instructions do not have offsets, and only have the register
298 // address.
299 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
300}
301
Matt Arsenault711b3902015-08-07 20:18:34 +0000302bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
303 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
304 // additionally can do r + r + i with addr64. 32-bit has more addressing
305 // mode options. Depending on the resource constant, it can also do
306 // (i64 r0) + (i32 r1) * (i14 i).
307 //
308 // Private arrays end up using a scratch buffer most of the time, so also
309 // assume those use MUBUF instructions. Scratch loads / stores are currently
310 // implemented as mubuf instructions with offen bit set, so slightly
311 // different than the normal addr64.
312 if (!isUInt<12>(AM.BaseOffs))
313 return false;
314
315 // FIXME: Since we can split immediate into soffset and immediate offset,
316 // would it make sense to allow any immediate?
317
318 switch (AM.Scale) {
319 case 0: // r + i or just i, depending on HasBaseReg.
320 return true;
321 case 1:
322 return true; // We have r + r or r + i.
323 case 2:
324 if (AM.HasBaseReg) {
325 // Reject 2 * r + r.
326 return false;
327 }
328
329 // Allow 2 * r as r + r
330 // Or 2 * r + i is allowed as r + r + i.
331 return true;
332 default: // Don't allow n * r
333 return false;
334 }
335}
336
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000337bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
338 const AddrMode &AM, Type *Ty,
339 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000340 // No global is ever allowed as a base.
341 if (AM.BaseGV)
342 return false;
343
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000344 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000345 case AMDGPUAS::GLOBAL_ADDRESS: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000346 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) {
Tom Stellard70580f82015-07-20 14:28:41 +0000347 // Assume the we will use FLAT for all global memory accesses
348 // on VI.
349 // FIXME: This assumption is currently wrong. On VI we still use
350 // MUBUF instructions for the r + i addressing mode. As currently
351 // implemented, the MUBUF instructions only work on buffer < 4GB.
352 // It may be possible to support > 4GB buffers with MUBUF instructions,
353 // by setting the stride value in the resource descriptor which would
354 // increase the size limit to (stride * 4GB). However, this is risky,
355 // because it has never been validated.
356 return isLegalFlatAddressingMode(AM);
357 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000358
Matt Arsenault711b3902015-08-07 20:18:34 +0000359 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000360 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000361 case AMDGPUAS::CONSTANT_ADDRESS: {
362 // If the offset isn't a multiple of 4, it probably isn't going to be
363 // correctly aligned.
Matt Arsenault3cc1e002016-08-13 01:43:51 +0000364 // FIXME: Can we get the real alignment here?
Matt Arsenault711b3902015-08-07 20:18:34 +0000365 if (AM.BaseOffs % 4 != 0)
366 return isLegalMUBUFAddressingMode(AM);
367
368 // There are no SMRD extloads, so if we have to do a small type access we
369 // will use a MUBUF load.
370 // FIXME?: We also need to do this if unaligned, but we don't know the
371 // alignment here.
372 if (DL.getTypeStoreSize(Ty) < 4)
373 return isLegalMUBUFAddressingMode(AM);
374
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000375 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000376 // SMRD instructions have an 8-bit, dword offset on SI.
377 if (!isUInt<8>(AM.BaseOffs / 4))
378 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000379 } else if (Subtarget->getGeneration() == SISubtarget::SEA_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000380 // On CI+, this can also be a 32-bit literal constant offset. If it fits
381 // in 8-bits, it can use a smaller encoding.
382 if (!isUInt<32>(AM.BaseOffs / 4))
383 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000384 } else if (Subtarget->getGeneration() == SISubtarget::VOLCANIC_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000385 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
386 if (!isUInt<20>(AM.BaseOffs))
387 return false;
388 } else
389 llvm_unreachable("unhandled generation");
390
391 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
392 return true;
393
394 if (AM.Scale == 1 && AM.HasBaseReg)
395 return true;
396
397 return false;
398 }
399
400 case AMDGPUAS::PRIVATE_ADDRESS:
Matt Arsenault711b3902015-08-07 20:18:34 +0000401 return isLegalMUBUFAddressingMode(AM);
402
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000403 case AMDGPUAS::LOCAL_ADDRESS:
404 case AMDGPUAS::REGION_ADDRESS: {
405 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
406 // field.
407 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
408 // an 8-bit dword offset but we don't know the alignment here.
409 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000410 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000411
412 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
413 return true;
414
415 if (AM.Scale == 1 && AM.HasBaseReg)
416 return true;
417
Matt Arsenault5015a892014-08-15 17:17:07 +0000418 return false;
419 }
Tom Stellard70580f82015-07-20 14:28:41 +0000420 case AMDGPUAS::FLAT_ADDRESS:
Matt Arsenault7d1b6c82016-04-29 06:25:10 +0000421 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
422 // For an unknown address space, this usually means that this is for some
423 // reason being used for pure arithmetic, and not based on some addressing
424 // computation. We don't have instructions that compute pointers with any
425 // addressing modes, so treat them as having no offset like flat
426 // instructions.
Tom Stellard70580f82015-07-20 14:28:41 +0000427 return isLegalFlatAddressingMode(AM);
428
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000429 default:
430 llvm_unreachable("unhandled address space");
431 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000432}
433
Matt Arsenaulte6986632015-01-14 01:35:22 +0000434bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000435 unsigned AddrSpace,
436 unsigned Align,
437 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000438 if (IsFast)
439 *IsFast = false;
440
Matt Arsenault1018c892014-04-24 17:08:26 +0000441 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
442 // which isn't a simple VT.
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000443 // Until MVT is extended to handle this, simply check for the size and
444 // rely on the condition below: allow accesses if the size is a multiple of 4.
445 if (VT == MVT::Other || (VT != MVT::Other && VT.getSizeInBits() > 1024 &&
446 VT.getStoreSize() > 16)) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000447 return false;
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000448 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000449
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000450 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
451 AddrSpace == AMDGPUAS::REGION_ADDRESS) {
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000452 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
453 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
454 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000455 bool AlignedBy4 = (Align % 4 == 0);
456 if (IsFast)
457 *IsFast = AlignedBy4;
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000458
Sanjay Patelce74db92015-09-03 15:03:19 +0000459 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000460 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000461
Tom Stellard64a9d082016-10-14 18:10:39 +0000462 // FIXME: We have to be conservative here and assume that flat operations
463 // will access scratch. If we had access to the IR function, then we
464 // could determine if any private memory was used in the function.
465 if (!Subtarget->hasUnalignedScratchAccess() &&
466 (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS ||
467 AddrSpace == AMDGPUAS::FLAT_ADDRESS)) {
468 return false;
469 }
470
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000471 if (Subtarget->hasUnalignedBufferAccess()) {
472 // If we have an uniform constant load, it still requires using a slow
473 // buffer instruction if unaligned.
474 if (IsFast) {
475 *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS) ?
476 (Align % 4 == 0) : true;
477 }
478
479 return true;
480 }
481
Tom Stellard33e64c62015-02-04 20:49:52 +0000482 // Smaller than dword value must be aligned.
Tom Stellard33e64c62015-02-04 20:49:52 +0000483 if (VT.bitsLT(MVT::i32))
484 return false;
485
Matt Arsenault1018c892014-04-24 17:08:26 +0000486 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
487 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000488 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000489 if (IsFast)
490 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000491
492 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000493}
494
Matt Arsenault46645fa2014-07-28 17:49:26 +0000495EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
496 unsigned SrcAlign, bool IsMemset,
497 bool ZeroMemset,
498 bool MemcpyStrSrc,
499 MachineFunction &MF) const {
500 // FIXME: Should account for address space here.
501
502 // The default fallback uses the private pointer size as a guess for a type to
503 // use. Make sure we switch these to 64-bit accesses.
504
505 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
506 return MVT::v4i32;
507
508 if (Size >= 8 && DstAlign >= 4)
509 return MVT::v2i32;
510
511 // Use the default.
512 return MVT::Other;
513}
514
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000515static bool isFlatGlobalAddrSpace(unsigned AS) {
516 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
517 AS == AMDGPUAS::FLAT_ADDRESS ||
518 AS == AMDGPUAS::CONSTANT_ADDRESS;
519}
520
521bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
522 unsigned DestAS) const {
Matt Arsenault37fefd62016-06-10 02:18:02 +0000523 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000524}
525
Tom Stellarda6f24c62015-12-15 20:55:55 +0000526bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
527 const MemSDNode *MemNode = cast<MemSDNode>(N);
528 const Value *Ptr = MemNode->getMemOperand()->getValue();
529
530 // UndefValue means this is a load of a kernel input. These are uniform.
Tom Stellard418beb72016-07-13 14:23:33 +0000531 // Sometimes LDS instructions have constant pointers.
532 // If Ptr is null, then that means this mem operand contains a
533 // PseudoSourceValue like GOT.
534 if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) ||
535 isa<Constant>(Ptr) || isa<GlobalValue>(Ptr))
Tom Stellarda6f24c62015-12-15 20:55:55 +0000536 return true;
537
Tom Stellard418beb72016-07-13 14:23:33 +0000538 const Instruction *I = dyn_cast<Instruction>(Ptr);
Tom Stellarda6f24c62015-12-15 20:55:55 +0000539 return I && I->getMetadata("amdgpu.uniform");
540}
541
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000542TargetLoweringBase::LegalizeTypeAction
543SITargetLowering::getPreferredVectorAction(EVT VT) const {
544 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
545 return TypeSplitVector;
546
547 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000548}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000549
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000550bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
551 Type *Ty) const {
Matt Arsenault749035b2016-07-30 01:40:36 +0000552 // FIXME: Could be smarter if called for vector constants.
553 return true;
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000554}
555
Tom Stellard2e045bb2016-01-20 00:13:22 +0000556bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
557
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000558 // i16 is not desirable unless it is a load or a store.
559 if (VT == MVT::i16 && Op != ISD::LOAD && Op != ISD::STORE)
560 return false;
561
Tom Stellard2e045bb2016-01-20 00:13:22 +0000562 // SimplifySetCC uses this function to determine whether or not it should
563 // create setcc with i1 operands. We don't have instructions for i1 setcc.
564 if (VT == MVT::i1 && Op == ISD::SETCC)
565 return false;
566
567 return TargetLowering::isTypeDesirableForOp(Op, VT);
568}
569
Jan Veselyfea814d2016-06-21 20:46:20 +0000570SDValue SITargetLowering::LowerParameterPtr(SelectionDAG &DAG,
571 const SDLoc &SL, SDValue Chain,
572 unsigned Offset) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000573 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000574 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000575 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaultac234b62015-11-30 21:15:57 +0000576 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000577
Matt Arsenault86033ca2014-07-28 17:31:39 +0000578 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000579 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000580 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
581 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
Jan Veselyfea814d2016-06-21 20:46:20 +0000582 return DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
583 DAG.getConstant(Offset, SL, PtrVT));
584}
585SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
586 const SDLoc &SL, SDValue Chain,
587 unsigned Offset, bool Signed) const {
588 const DataLayout &DL = DAG.getDataLayout();
589 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
590 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
591 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Mehdi Amini44ede332015-07-09 02:09:04 +0000592 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000593 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
594
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000595 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000596
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000597 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000598 if (MemVT.isFloatingPoint())
599 ExtTy = ISD::EXTLOAD;
600
Jan Veselyfea814d2016-06-21 20:46:20 +0000601 SDValue Ptr = LowerParameterPtr(DAG, SL, Chain, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +0000602 return DAG.getLoad(ISD::UNINDEXED, ExtTy, VT, SL, Chain, Ptr, PtrOffset,
Justin Lebaradbf09e2016-09-11 01:38:58 +0000603 PtrInfo, MemVT, Align,
604 MachineMemOperand::MONonTemporal |
605 MachineMemOperand::MODereferenceable |
606 MachineMemOperand::MOInvariant);
Tom Stellard94593ee2013-06-03 17:40:18 +0000607}
608
Christian Konig2c8f6d52013-03-07 09:03:52 +0000609SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000610 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000611 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
612 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000613 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000614
615 MachineFunction &MF = DAG.getMachineFunction();
616 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000617 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000618 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000619
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000620 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) {
Matt Arsenaultd48da142015-11-02 23:23:02 +0000621 const Function *Fn = MF.getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000622 DiagnosticInfoUnsupported NoGraphicsHSA(
623 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
Matt Arsenaultd48da142015-11-02 23:23:02 +0000624 DAG.getContext()->diagnose(NoGraphicsHSA);
Diana Picus81bc3172016-05-26 15:24:55 +0000625 return DAG.getEntryNode();
Matt Arsenaultd48da142015-11-02 23:23:02 +0000626 }
627
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000628 // Create stack objects that are used for emitting debugger prologue if
629 // "amdgpu-debugger-emit-prologue" attribute was specified.
630 if (ST.debuggerEmitPrologue())
631 createDebuggerPrologueStackObjects(MF);
632
Christian Konig2c8f6d52013-03-07 09:03:52 +0000633 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000634 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000635
636 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000637 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000638
639 // First check if it's a PS input addr
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000640 if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000641 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000642
Marek Olsakfccabaf2016-01-13 11:45:36 +0000643 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000644 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000645 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000646 ++PSInputNum;
647 continue;
648 }
649
Marek Olsakfccabaf2016-01-13 11:45:36 +0000650 Info->markPSInputAllocated(PSInputNum);
651 if (Arg.Used)
652 Info->PSInputEna |= 1 << PSInputNum;
653
654 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000655 }
656
Matt Arsenault539ca882016-05-05 20:27:02 +0000657 if (AMDGPU::isShader(CallConv)) {
658 // Second split vertices into their elements
659 if (Arg.VT.isVector()) {
660 ISD::InputArg NewArg = Arg;
661 NewArg.Flags.setSplit();
662 NewArg.VT = Arg.VT.getVectorElementType();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000663
Matt Arsenault539ca882016-05-05 20:27:02 +0000664 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
665 // three or five element vertex only needs three or five registers,
666 // NOT four or eight.
667 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
668 unsigned NumElements = ParamType->getVectorNumElements();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000669
Matt Arsenault539ca882016-05-05 20:27:02 +0000670 for (unsigned j = 0; j != NumElements; ++j) {
671 Splits.push_back(NewArg);
672 NewArg.PartOffset += NewArg.VT.getStoreSize();
673 }
674 } else {
675 Splits.push_back(Arg);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000676 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000677 }
678 }
679
680 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000681 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
682 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000683
Christian Konig99ee0f42013-03-07 09:04:14 +0000684 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000685 //
686 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
687 // PSInputAddr, the user wants to enable some bits after the compilation
688 // based on run-time states. Since we can't know what the final PSInputEna
689 // will look like, so we shouldn't do anything here and the user should take
690 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000691 //
692 // Otherwise, the following restrictions apply:
693 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
694 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
695 // enabled too.
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000696 if (CallConv == CallingConv::AMDGPU_PS &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000697 ((Info->getPSInputAddr() & 0x7F) == 0 ||
NAKAMURA Takumife1202c2016-06-20 00:37:41 +0000698 ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000699 CCInfo.AllocateReg(AMDGPU::VGPR0);
700 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000701 Info->markPSInputAllocated(0);
702 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000703 }
704
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000705 if (!AMDGPU::isShader(CallConv)) {
Tom Stellardf110f8f2016-04-14 16:27:03 +0000706 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
707 } else {
708 assert(!Info->hasPrivateSegmentBuffer() && !Info->hasDispatchPtr() &&
709 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() &&
710 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
711 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() &&
712 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() &&
713 !Info->hasWorkItemIDZ());
Tom Stellardaf775432013-10-23 00:44:32 +0000714 }
715
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000716 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
717 if (Info->hasPrivateSegmentBuffer()) {
718 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
719 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
720 CCInfo.AllocateReg(PrivateSegmentBufferReg);
721 }
722
723 if (Info->hasDispatchPtr()) {
724 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
725 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
726 CCInfo.AllocateReg(DispatchPtrReg);
727 }
728
Matt Arsenault48ab5262016-04-25 19:27:18 +0000729 if (Info->hasQueuePtr()) {
730 unsigned QueuePtrReg = Info->addQueuePtr(*TRI);
731 MF.addLiveIn(QueuePtrReg, &AMDGPU::SReg_64RegClass);
732 CCInfo.AllocateReg(QueuePtrReg);
733 }
734
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000735 if (Info->hasKernargSegmentPtr()) {
736 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
737 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
738 CCInfo.AllocateReg(InputPtrReg);
739 }
740
Matt Arsenault8d718dc2016-07-22 17:01:30 +0000741 if (Info->hasDispatchID()) {
742 unsigned DispatchIDReg = Info->addDispatchID(*TRI);
743 MF.addLiveIn(DispatchIDReg, &AMDGPU::SReg_64RegClass);
744 CCInfo.AllocateReg(DispatchIDReg);
745 }
746
Matt Arsenault296b8492016-02-12 06:31:30 +0000747 if (Info->hasFlatScratchInit()) {
748 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
749 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SReg_64RegClass);
750 CCInfo.AllocateReg(FlatScratchInitReg);
751 }
752
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000753 if (!AMDGPU::isShader(CallConv))
754 analyzeFormalArgumentsCompute(CCInfo, Ins);
755 else
756 AnalyzeFormalArguments(CCInfo, Splits);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000757
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000758 SmallVector<SDValue, 16> Chains;
759
Christian Konig2c8f6d52013-03-07 09:03:52 +0000760 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
761
Christian Konigb7be72d2013-05-17 09:46:48 +0000762 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000763 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000764 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000765 continue;
766 }
767
Christian Konig2c8f6d52013-03-07 09:03:52 +0000768 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000769 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000770
771 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000772 VT = Ins[i].VT;
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000773 EVT MemVT = VA.getLocVT();
Tom Stellardb5798b02015-06-26 21:15:03 +0000774 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
775 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000776 // The first 36 bytes of the input buffer contains information about
777 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000778 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000779 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000780 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000781
Craig Toppere3dcce92015-08-01 22:20:21 +0000782 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000783 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000784 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Tom Stellardca7ecf32014-08-22 18:49:31 +0000785 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
786 // On SI local pointers are just offsets into LDS, so they are always
787 // less than 16-bits. On CI and newer they could potentially be
788 // real pointers, so we can't guarantee their size.
789 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
790 DAG.getValueType(MVT::i16));
791 }
792
Tom Stellarded882c22013-06-03 17:40:11 +0000793 InVals.push_back(Arg);
Matt Arsenault52ef4012016-07-26 16:45:58 +0000794 Info->setABIArgOffset(Offset + MemVT.getStoreSize());
Tom Stellarded882c22013-06-03 17:40:11 +0000795 continue;
796 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000797 assert(VA.isRegLoc() && "Parameter must be in a register!");
798
799 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000800
801 if (VT == MVT::i64) {
802 // For now assume it is a pointer
803 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
804 &AMDGPU::SReg_64RegClass);
805 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000806 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
807 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000808 continue;
809 }
810
811 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
812
813 Reg = MF.addLiveIn(Reg, RC);
814 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
815
Christian Konig2c8f6d52013-03-07 09:03:52 +0000816 if (Arg.VT.isVector()) {
817
818 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000819 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000820 unsigned NumElements = ParamType->getVectorNumElements();
821
822 SmallVector<SDValue, 4> Regs;
823 Regs.push_back(Val);
824 for (unsigned j = 1; j != NumElements; ++j) {
825 Reg = ArgLocs[ArgIdx++].getLocReg();
826 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000827
828 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
829 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000830 }
831
832 // Fill up the missing vector elements
833 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000834 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000835
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000836 InVals.push_back(DAG.getBuildVector(Arg.VT, DL, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000837 continue;
838 }
839
840 InVals.push_back(Val);
841 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000842
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000843 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
844 // these from the dispatch pointer.
845
846 // Start adding system SGPRs.
847 if (Info->hasWorkGroupIDX()) {
848 unsigned Reg = Info->addWorkGroupIDX();
849 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
850 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000851 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000852
853 if (Info->hasWorkGroupIDY()) {
854 unsigned Reg = Info->addWorkGroupIDY();
855 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
856 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000857 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000858
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000859 if (Info->hasWorkGroupIDZ()) {
860 unsigned Reg = Info->addWorkGroupIDZ();
861 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
862 CCInfo.AllocateReg(Reg);
863 }
864
865 if (Info->hasWorkGroupInfo()) {
866 unsigned Reg = Info->addWorkGroupInfo();
867 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
868 CCInfo.AllocateReg(Reg);
869 }
870
871 if (Info->hasPrivateSegmentWaveByteOffset()) {
872 // Scratch wave offset passed in system SGPR.
Tom Stellardf110f8f2016-04-14 16:27:03 +0000873 unsigned PrivateSegmentWaveByteOffsetReg;
874
875 if (AMDGPU::isShader(CallConv)) {
876 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
877 Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
878 } else
879 PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset();
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000880
881 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
882 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
883 }
884
885 // Now that we've figured out where the scratch register inputs are, see if
886 // should reserve the arguments and use them directly.
Matthias Braun941a7052016-07-28 18:40:00 +0000887 bool HasStackObjects = MF.getFrameInfo().hasStackObjects();
Matt Arsenault296b8492016-02-12 06:31:30 +0000888 // Record that we know we have non-spill stack objects so we don't need to
889 // check all stack objects later.
890 if (HasStackObjects)
891 Info->setHasNonSpillStackObjects(true);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000892
Matt Arsenault253640e2016-10-13 13:10:00 +0000893 // Everything live out of a block is spilled with fast regalloc, so it's
894 // almost certain that spilling will be required.
895 if (getTargetMachine().getOptLevel() == CodeGenOpt::None)
896 HasStackObjects = true;
897
Tom Stellard0b76fc4c2016-09-16 21:34:26 +0000898 if (ST.isAmdCodeObjectV2()) {
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000899 if (HasStackObjects) {
900 // If we have stack objects, we unquestionably need the private buffer
Tom Stellard0b76fc4c2016-09-16 21:34:26 +0000901 // resource. For the Code Object V2 ABI, this will be the first 4 user
902 // SGPR inputs. We can reserve those and use them directly.
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000903
904 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
905 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
906 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
907
908 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
909 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
910 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
911 } else {
912 unsigned ReservedBufferReg
913 = TRI->reservedPrivateSegmentBufferReg(MF);
914 unsigned ReservedOffsetReg
915 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
916
917 // We tentatively reserve the last registers (skipping the last two
918 // which may contain VCC). After register allocation, we'll replace
919 // these with the ones immediately after those which were really
920 // allocated. In the prologue copies will be inserted from the argument
921 // to these reserved registers.
922 Info->setScratchRSrcReg(ReservedBufferReg);
923 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
924 }
925 } else {
926 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
927
928 // Without HSA, relocations are used for the scratch pointer and the
929 // buffer resource setup is always inserted in the prologue. Scratch wave
930 // offset is still in an input SGPR.
931 Info->setScratchRSrcReg(ReservedBufferReg);
932
933 if (HasStackObjects) {
934 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
935 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
936 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
937 } else {
938 unsigned ReservedOffsetReg
939 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
940 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
941 }
942 }
943
944 if (Info->hasWorkItemIDX()) {
945 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
946 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
947 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000948 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000949
950 if (Info->hasWorkItemIDY()) {
951 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
952 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
953 CCInfo.AllocateReg(Reg);
954 }
955
956 if (Info->hasWorkItemIDZ()) {
957 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
958 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
959 CCInfo.AllocateReg(Reg);
960 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000961
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000962 if (Chains.empty())
963 return Chain;
964
965 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000966}
967
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000968SDValue
969SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
970 bool isVarArg,
971 const SmallVectorImpl<ISD::OutputArg> &Outs,
972 const SmallVectorImpl<SDValue> &OutVals,
973 const SDLoc &DL, SelectionDAG &DAG) const {
Marek Olsak8a0f3352016-01-13 17:23:04 +0000974 MachineFunction &MF = DAG.getMachineFunction();
975 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
976
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000977 if (!AMDGPU::isShader(CallConv))
Marek Olsak8a0f3352016-01-13 17:23:04 +0000978 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
979 OutVals, DL, DAG);
980
Marek Olsak8e9cc632016-01-13 17:23:09 +0000981 Info->setIfReturnsVoid(Outs.size() == 0);
982
Marek Olsak8a0f3352016-01-13 17:23:04 +0000983 SmallVector<ISD::OutputArg, 48> Splits;
984 SmallVector<SDValue, 48> SplitVals;
985
986 // Split vectors into their elements.
987 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
988 const ISD::OutputArg &Out = Outs[i];
989
990 if (Out.VT.isVector()) {
991 MVT VT = Out.VT.getVectorElementType();
992 ISD::OutputArg NewOut = Out;
993 NewOut.Flags.setSplit();
994 NewOut.VT = VT;
995
996 // We want the original number of vector elements here, e.g.
997 // three or five, not four or eight.
998 unsigned NumElements = Out.ArgVT.getVectorNumElements();
999
1000 for (unsigned j = 0; j != NumElements; ++j) {
1001 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
1002 DAG.getConstant(j, DL, MVT::i32));
1003 SplitVals.push_back(Elem);
1004 Splits.push_back(NewOut);
1005 NewOut.PartOffset += NewOut.VT.getStoreSize();
1006 }
1007 } else {
1008 SplitVals.push_back(OutVals[i]);
1009 Splits.push_back(Out);
1010 }
1011 }
1012
1013 // CCValAssign - represent the assignment of the return value to a location.
1014 SmallVector<CCValAssign, 48> RVLocs;
1015
1016 // CCState - Info about the registers and stack slots.
1017 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1018 *DAG.getContext());
1019
1020 // Analyze outgoing return values.
1021 AnalyzeReturn(CCInfo, Splits);
1022
1023 SDValue Flag;
1024 SmallVector<SDValue, 48> RetOps;
1025 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1026
1027 // Copy the result values into the output registers.
1028 for (unsigned i = 0, realRVLocIdx = 0;
1029 i != RVLocs.size();
1030 ++i, ++realRVLocIdx) {
1031 CCValAssign &VA = RVLocs[i];
1032 assert(VA.isRegLoc() && "Can only return in registers!");
1033
1034 SDValue Arg = SplitVals[realRVLocIdx];
1035
1036 // Copied from other backends.
1037 switch (VA.getLocInfo()) {
1038 default: llvm_unreachable("Unknown loc info!");
1039 case CCValAssign::Full:
1040 break;
1041 case CCValAssign::BCvt:
1042 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1043 break;
1044 }
1045
1046 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
1047 Flag = Chain.getValue(1);
1048 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1049 }
1050
1051 // Update chain and glue.
1052 RetOps[0] = Chain;
1053 if (Flag.getNode())
1054 RetOps.push_back(Flag);
1055
Matt Arsenault9babdf42016-06-22 20:15:28 +00001056 unsigned Opc = Info->returnsVoid() ? AMDGPUISD::ENDPGM : AMDGPUISD::RETURN;
1057 return DAG.getNode(Opc, DL, MVT::Other, RetOps);
Marek Olsak8a0f3352016-01-13 17:23:04 +00001058}
1059
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001060unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1061 SelectionDAG &DAG) const {
1062 unsigned Reg = StringSwitch<unsigned>(RegName)
1063 .Case("m0", AMDGPU::M0)
1064 .Case("exec", AMDGPU::EXEC)
1065 .Case("exec_lo", AMDGPU::EXEC_LO)
1066 .Case("exec_hi", AMDGPU::EXEC_HI)
1067 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1068 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1069 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1070 .Default(AMDGPU::NoRegister);
1071
1072 if (Reg == AMDGPU::NoRegister) {
1073 report_fatal_error(Twine("invalid register name \""
1074 + StringRef(RegName) + "\"."));
1075
1076 }
1077
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001078 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001079 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1080 report_fatal_error(Twine("invalid register \""
1081 + StringRef(RegName) + "\" for subtarget."));
1082 }
1083
1084 switch (Reg) {
1085 case AMDGPU::M0:
1086 case AMDGPU::EXEC_LO:
1087 case AMDGPU::EXEC_HI:
1088 case AMDGPU::FLAT_SCR_LO:
1089 case AMDGPU::FLAT_SCR_HI:
1090 if (VT.getSizeInBits() == 32)
1091 return Reg;
1092 break;
1093 case AMDGPU::EXEC:
1094 case AMDGPU::FLAT_SCR:
1095 if (VT.getSizeInBits() == 64)
1096 return Reg;
1097 break;
1098 default:
1099 llvm_unreachable("missing register type checking");
1100 }
1101
1102 report_fatal_error(Twine("invalid type for register \""
1103 + StringRef(RegName) + "\"."));
1104}
1105
Matt Arsenault786724a2016-07-12 21:41:32 +00001106// If kill is not the last instruction, split the block so kill is always a
1107// proper terminator.
1108MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI,
1109 MachineBasicBlock *BB) const {
1110 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1111
1112 MachineBasicBlock::iterator SplitPoint(&MI);
1113 ++SplitPoint;
1114
1115 if (SplitPoint == BB->end()) {
1116 // Don't bother with a new block.
1117 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1118 return BB;
1119 }
1120
1121 MachineFunction *MF = BB->getParent();
1122 MachineBasicBlock *SplitBB
1123 = MF->CreateMachineBasicBlock(BB->getBasicBlock());
1124
Matt Arsenault786724a2016-07-12 21:41:32 +00001125 MF->insert(++MachineFunction::iterator(BB), SplitBB);
1126 SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end());
1127
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001128 SplitBB->transferSuccessorsAndUpdatePHIs(BB);
Matt Arsenault786724a2016-07-12 21:41:32 +00001129 BB->addSuccessor(SplitBB);
1130
1131 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1132 return SplitBB;
1133}
1134
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001135// Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the
1136// wavefront. If the value is uniform and just happens to be in a VGPR, this
1137// will only do one iteration. In the worst case, this will loop 64 times.
1138//
1139// TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001140static MachineBasicBlock::iterator emitLoadM0FromVGPRLoop(
1141 const SIInstrInfo *TII,
1142 MachineRegisterInfo &MRI,
1143 MachineBasicBlock &OrigBB,
1144 MachineBasicBlock &LoopBB,
1145 const DebugLoc &DL,
1146 const MachineOperand &IdxReg,
1147 unsigned InitReg,
1148 unsigned ResultReg,
1149 unsigned PhiReg,
1150 unsigned InitSaveExecReg,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001151 int Offset,
1152 bool UseGPRIdxMode) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001153 MachineBasicBlock::iterator I = LoopBB.begin();
1154
1155 unsigned PhiExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1156 unsigned NewExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1157 unsigned CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1158 unsigned CondReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1159
1160 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg)
1161 .addReg(InitReg)
1162 .addMBB(&OrigBB)
1163 .addReg(ResultReg)
1164 .addMBB(&LoopBB);
1165
1166 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec)
1167 .addReg(InitSaveExecReg)
1168 .addMBB(&OrigBB)
1169 .addReg(NewExec)
1170 .addMBB(&LoopBB);
1171
1172 // Read the next variant <- also loop target.
1173 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg)
1174 .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef()));
1175
1176 // Compare the just read M0 value to all possible Idx values.
1177 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg)
1178 .addReg(CurrentIdxReg)
Matt Arsenaultf0ba86a2016-07-21 09:40:57 +00001179 .addReg(IdxReg.getReg(), 0, IdxReg.getSubReg());
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001180
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001181 if (UseGPRIdxMode) {
1182 unsigned IdxReg;
1183 if (Offset == 0) {
1184 IdxReg = CurrentIdxReg;
1185 } else {
1186 IdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1187 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), IdxReg)
1188 .addReg(CurrentIdxReg, RegState::Kill)
1189 .addImm(Offset);
1190 }
1191
1192 MachineInstr *SetIdx =
1193 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_IDX))
1194 .addReg(IdxReg, RegState::Kill);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001195 SetIdx->getOperand(2).setIsUndef();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001196 } else {
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001197 // Move index from VCC into M0
1198 if (Offset == 0) {
1199 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1200 .addReg(CurrentIdxReg, RegState::Kill);
1201 } else {
1202 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1203 .addReg(CurrentIdxReg, RegState::Kill)
1204 .addImm(Offset);
1205 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001206 }
1207
1208 // Update EXEC, save the original EXEC value to VCC.
1209 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), NewExec)
1210 .addReg(CondReg, RegState::Kill);
1211
1212 MRI.setSimpleHint(NewExec, CondReg);
1213
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001214 // Update EXEC, switch all done bits to 0 and all todo bits to 1.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001215 MachineInstr *InsertPt =
1216 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001217 .addReg(AMDGPU::EXEC)
1218 .addReg(NewExec);
1219
1220 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use
1221 // s_cbranch_scc0?
1222
1223 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover.
1224 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
1225 .addMBB(&LoopBB);
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001226
1227 return InsertPt->getIterator();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001228}
1229
1230// This has slightly sub-optimal regalloc when the source vector is killed by
1231// the read. The register allocator does not understand that the kill is
1232// per-workitem, so is kept alive for the whole loop so we end up not re-using a
1233// subregister from it, using 1 more VGPR than necessary. This was saved when
1234// this was expanded after register allocation.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001235static MachineBasicBlock::iterator loadM0FromVGPR(const SIInstrInfo *TII,
1236 MachineBasicBlock &MBB,
1237 MachineInstr &MI,
1238 unsigned InitResultReg,
1239 unsigned PhiReg,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001240 int Offset,
1241 bool UseGPRIdxMode) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001242 MachineFunction *MF = MBB.getParent();
1243 MachineRegisterInfo &MRI = MF->getRegInfo();
1244 const DebugLoc &DL = MI.getDebugLoc();
1245 MachineBasicBlock::iterator I(&MI);
1246
1247 unsigned DstReg = MI.getOperand(0).getReg();
1248 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1249 unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1250
1251 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec);
1252
1253 // Save the EXEC mask
1254 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec)
1255 .addReg(AMDGPU::EXEC);
1256
1257 // To insert the loop we need to split the block. Move everything after this
1258 // point to a new block, and insert a new empty block between the two.
1259 MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock();
1260 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock();
1261 MachineFunction::iterator MBBI(MBB);
1262 ++MBBI;
1263
1264 MF->insert(MBBI, LoopBB);
1265 MF->insert(MBBI, RemainderBB);
1266
1267 LoopBB->addSuccessor(LoopBB);
1268 LoopBB->addSuccessor(RemainderBB);
1269
1270 // Move the rest of the block into a new block.
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001271 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001272 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end());
1273
1274 MBB.addSuccessor(LoopBB);
1275
1276 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1277
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001278 auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx,
1279 InitResultReg, DstReg, PhiReg, TmpExec,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001280 Offset, UseGPRIdxMode);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001281
1282 MachineBasicBlock::iterator First = RemainderBB->begin();
1283 BuildMI(*RemainderBB, First, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
1284 .addReg(SaveExec);
1285
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001286 return InsPt;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001287}
1288
1289// Returns subreg index, offset
1290static std::pair<unsigned, int>
1291computeIndirectRegAndOffset(const SIRegisterInfo &TRI,
1292 const TargetRegisterClass *SuperRC,
1293 unsigned VecReg,
1294 int Offset) {
1295 int NumElts = SuperRC->getSize() / 4;
1296
1297 // Skip out of bounds offsets, or else we would end up using an undefined
1298 // register.
1299 if (Offset >= NumElts || Offset < 0)
1300 return std::make_pair(AMDGPU::sub0, Offset);
1301
1302 return std::make_pair(AMDGPU::sub0 + Offset, 0);
1303}
1304
1305// Return true if the index is an SGPR and was set.
1306static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII,
1307 MachineRegisterInfo &MRI,
1308 MachineInstr &MI,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001309 int Offset,
1310 bool UseGPRIdxMode,
1311 bool IsIndirectSrc) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001312 MachineBasicBlock *MBB = MI.getParent();
1313 const DebugLoc &DL = MI.getDebugLoc();
1314 MachineBasicBlock::iterator I(&MI);
1315
1316 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1317 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
1318
1319 assert(Idx->getReg() != AMDGPU::NoRegister);
1320
1321 if (!TII->getRegisterInfo().isSGPRClass(IdxRC))
1322 return false;
1323
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001324 if (UseGPRIdxMode) {
1325 unsigned IdxMode = IsIndirectSrc ?
1326 VGPRIndexMode::SRC0_ENABLE : VGPRIndexMode::DST_ENABLE;
1327 if (Offset == 0) {
1328 MachineInstr *SetOn =
1329 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1330 .addOperand(*Idx)
1331 .addImm(IdxMode);
1332
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001333 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001334 } else {
1335 unsigned Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
1336 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp)
1337 .addOperand(*Idx)
1338 .addImm(Offset);
1339 MachineInstr *SetOn =
1340 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1341 .addReg(Tmp, RegState::Kill)
1342 .addImm(IdxMode);
1343
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001344 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001345 }
1346
1347 return true;
1348 }
1349
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001350 if (Offset == 0) {
1351 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1352 .addOperand(*Idx);
1353 } else {
1354 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1355 .addOperand(*Idx)
1356 .addImm(Offset);
1357 }
1358
1359 return true;
1360}
1361
1362// Control flow needs to be inserted if indexing with a VGPR.
1363static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI,
1364 MachineBasicBlock &MBB,
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001365 const SISubtarget &ST) {
1366 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001367 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1368 MachineFunction *MF = MBB.getParent();
1369 MachineRegisterInfo &MRI = MF->getRegInfo();
1370
1371 unsigned Dst = MI.getOperand(0).getReg();
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001372 unsigned SrcReg = TII->getNamedOperand(MI, AMDGPU::OpName::src)->getReg();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001373 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1374
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001375 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcReg);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001376
1377 unsigned SubReg;
1378 std::tie(SubReg, Offset)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001379 = computeIndirectRegAndOffset(TRI, VecRC, SrcReg, Offset);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001380
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001381 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode;
1382
1383 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, true)) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001384 MachineBasicBlock::iterator I(&MI);
1385 const DebugLoc &DL = MI.getDebugLoc();
1386
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001387 if (UseGPRIdxMode) {
1388 // TODO: Look at the uses to avoid the copy. This may require rescheduling
1389 // to avoid interfering with other uses, so probably requires a new
1390 // optimization pass.
1391 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001392 .addReg(SrcReg, RegState::Undef, SubReg)
1393 .addReg(SrcReg, RegState::Implicit)
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001394 .addReg(AMDGPU::M0, RegState::Implicit);
1395 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1396 } else {
1397 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001398 .addReg(SrcReg, RegState::Undef, SubReg)
1399 .addReg(SrcReg, RegState::Implicit);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001400 }
1401
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001402 MI.eraseFromParent();
1403
1404 return &MBB;
1405 }
1406
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001407
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001408 const DebugLoc &DL = MI.getDebugLoc();
1409 MachineBasicBlock::iterator I(&MI);
1410
1411 unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1412 unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1413
1414 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg);
1415
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001416 if (UseGPRIdxMode) {
1417 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1418 .addImm(0) // Reset inside loop.
1419 .addImm(VGPRIndexMode::SRC0_ENABLE);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001420 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001421
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001422 // Disable again after the loop.
1423 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1424 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001425
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001426 auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, Offset, UseGPRIdxMode);
1427 MachineBasicBlock *LoopBB = InsPt->getParent();
1428
1429 if (UseGPRIdxMode) {
1430 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001431 .addReg(SrcReg, RegState::Undef, SubReg)
1432 .addReg(SrcReg, RegState::Implicit)
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001433 .addReg(AMDGPU::M0, RegState::Implicit);
1434 } else {
1435 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001436 .addReg(SrcReg, RegState::Undef, SubReg)
1437 .addReg(SrcReg, RegState::Implicit);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001438 }
1439
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001440 MI.eraseFromParent();
1441
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001442 return LoopBB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001443}
1444
1445static MachineBasicBlock *emitIndirectDst(MachineInstr &MI,
1446 MachineBasicBlock &MBB,
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001447 const SISubtarget &ST) {
1448 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001449 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1450 MachineFunction *MF = MBB.getParent();
1451 MachineRegisterInfo &MRI = MF->getRegInfo();
1452
1453 unsigned Dst = MI.getOperand(0).getReg();
1454 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
1455 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1456 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val);
1457 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1458 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
1459
1460 // This can be an immediate, but will be folded later.
1461 assert(Val->getReg());
1462
1463 unsigned SubReg;
1464 std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC,
1465 SrcVec->getReg(),
1466 Offset);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001467 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode;
1468
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001469 if (Idx->getReg() == AMDGPU::NoRegister) {
1470 MachineBasicBlock::iterator I(&MI);
1471 const DebugLoc &DL = MI.getDebugLoc();
1472
1473 assert(Offset == 0);
1474
1475 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst)
1476 .addOperand(*SrcVec)
1477 .addOperand(*Val)
1478 .addImm(SubReg);
1479
1480 MI.eraseFromParent();
1481 return &MBB;
1482 }
1483
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001484 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, false)) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001485 MachineBasicBlock::iterator I(&MI);
1486 const DebugLoc &DL = MI.getDebugLoc();
1487
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001488 if (UseGPRIdxMode) {
1489 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_indirect))
1490 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst
1491 .addOperand(*Val)
1492 .addReg(Dst, RegState::ImplicitDefine)
1493 .addReg(SrcVec->getReg(), RegState::Implicit)
1494 .addReg(AMDGPU::M0, RegState::Implicit);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001495
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001496 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1497 } else {
1498 const MCInstrDesc &MovRelDesc = TII->get(AMDGPU::V_MOVRELD_B32_e32);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001499
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001500 MachineInstr *MovRel =
1501 BuildMI(MBB, I, DL, MovRelDesc)
1502 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst
1503 .addOperand(*Val)
1504 .addReg(Dst, RegState::ImplicitDefine)
1505 .addReg(SrcVec->getReg(), RegState::Implicit);
1506
1507 const int ImpDefIdx = MovRelDesc.getNumOperands() +
1508 MovRelDesc.getNumImplicitUses();
1509 const int ImpUseIdx = ImpDefIdx + 1;
1510
1511 MovRel->tieOperands(ImpDefIdx, ImpUseIdx);
1512 }
1513
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001514 MI.eraseFromParent();
1515 return &MBB;
1516 }
1517
1518 if (Val->isReg())
1519 MRI.clearKillFlags(Val->getReg());
1520
1521 const DebugLoc &DL = MI.getDebugLoc();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001522
1523 if (UseGPRIdxMode) {
1524 MachineBasicBlock::iterator I(&MI);
1525
1526 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1527 .addImm(0) // Reset inside loop.
1528 .addImm(VGPRIndexMode::DST_ENABLE);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001529 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001530
1531 // Disable again after the loop.
1532 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1533 }
1534
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001535 unsigned PhiReg = MRI.createVirtualRegister(VecRC);
1536
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001537 auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg,
1538 Offset, UseGPRIdxMode);
1539 MachineBasicBlock *LoopBB = InsPt->getParent();
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001540
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001541 if (UseGPRIdxMode) {
1542 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_indirect))
1543 .addReg(PhiReg, RegState::Undef, SubReg) // vdst
1544 .addOperand(*Val) // src0
1545 .addReg(Dst, RegState::ImplicitDefine)
1546 .addReg(PhiReg, RegState::Implicit)
1547 .addReg(AMDGPU::M0, RegState::Implicit);
1548 } else {
1549 const MCInstrDesc &MovRelDesc = TII->get(AMDGPU::V_MOVRELD_B32_e32);
1550 // vdst is not actually read and just provides the base register index.
1551 MachineInstr *MovRel =
1552 BuildMI(*LoopBB, InsPt, DL, MovRelDesc)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001553 .addReg(PhiReg, RegState::Undef, SubReg) // vdst
1554 .addOperand(*Val)
1555 .addReg(Dst, RegState::ImplicitDefine)
1556 .addReg(PhiReg, RegState::Implicit);
1557
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001558 const int ImpDefIdx = MovRelDesc.getNumOperands() +
1559 MovRelDesc.getNumImplicitUses();
1560 const int ImpUseIdx = ImpDefIdx + 1;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001561
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001562 MovRel->tieOperands(ImpDefIdx, ImpUseIdx);
1563 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001564
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001565 MI.eraseFromParent();
1566
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001567 return LoopBB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001568}
1569
Matt Arsenault786724a2016-07-12 21:41:32 +00001570MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter(
1571 MachineInstr &MI, MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001572 switch (MI.getOpcode()) {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001573 case AMDGPU::SI_INIT_M0: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001574 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001575 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001576 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001577 .addOperand(MI.getOperand(0));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001578 MI.eraseFromParent();
Matt Arsenault20711b72015-02-20 22:10:45 +00001579 return BB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001580 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001581 case AMDGPU::GET_GROUPSTATICSIZE: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001582 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1583
Changpeng Fang01f60622016-03-15 17:28:44 +00001584 MachineFunction *MF = BB->getParent();
1585 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001586 DebugLoc DL = MI.getDebugLoc();
Matt Arsenault3c07c812016-07-22 17:01:33 +00001587 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32))
1588 .addOperand(MI.getOperand(0))
Matt Arsenault52ef4012016-07-26 16:45:58 +00001589 .addImm(MFI->getLDSSize());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001590 MI.eraseFromParent();
Changpeng Fang01f60622016-03-15 17:28:44 +00001591 return BB;
1592 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001593 case AMDGPU::SI_INDIRECT_SRC_V1:
1594 case AMDGPU::SI_INDIRECT_SRC_V2:
1595 case AMDGPU::SI_INDIRECT_SRC_V4:
1596 case AMDGPU::SI_INDIRECT_SRC_V8:
1597 case AMDGPU::SI_INDIRECT_SRC_V16:
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001598 return emitIndirectSrc(MI, *BB, *getSubtarget());
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001599 case AMDGPU::SI_INDIRECT_DST_V1:
1600 case AMDGPU::SI_INDIRECT_DST_V2:
1601 case AMDGPU::SI_INDIRECT_DST_V4:
1602 case AMDGPU::SI_INDIRECT_DST_V8:
1603 case AMDGPU::SI_INDIRECT_DST_V16:
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001604 return emitIndirectDst(MI, *BB, *getSubtarget());
Matt Arsenault786724a2016-07-12 21:41:32 +00001605 case AMDGPU::SI_KILL:
1606 return splitKillBlock(MI, BB);
Matt Arsenault22e41792016-08-27 01:00:37 +00001607 case AMDGPU::V_CNDMASK_B64_PSEUDO: {
1608 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
1609 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1610
1611 unsigned Dst = MI.getOperand(0).getReg();
1612 unsigned Src0 = MI.getOperand(1).getReg();
1613 unsigned Src1 = MI.getOperand(2).getReg();
1614 const DebugLoc &DL = MI.getDebugLoc();
1615 unsigned SrcCond = MI.getOperand(3).getReg();
1616
1617 unsigned DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1618 unsigned DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1619
1620 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo)
1621 .addReg(Src0, 0, AMDGPU::sub0)
1622 .addReg(Src1, 0, AMDGPU::sub0)
1623 .addReg(SrcCond);
1624 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi)
1625 .addReg(Src0, 0, AMDGPU::sub1)
1626 .addReg(Src1, 0, AMDGPU::sub1)
1627 .addReg(SrcCond);
1628
1629 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst)
1630 .addReg(DstLo)
1631 .addImm(AMDGPU::sub0)
1632 .addReg(DstHi)
1633 .addImm(AMDGPU::sub1);
1634 MI.eraseFromParent();
1635 return BB;
1636 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001637 default:
1638 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001639 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001640}
1641
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001642bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1643 // This currently forces unfolding various combinations of fsub into fma with
1644 // free fneg'd operands. As long as we have fast FMA (controlled by
1645 // isFMAFasterThanFMulAndFAdd), we should perform these.
1646
1647 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1648 // most of these combines appear to be cycle neutral but save on instruction
1649 // count / code size.
1650 return true;
1651}
1652
Mehdi Amini44ede332015-07-09 02:09:04 +00001653EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1654 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001655 if (!VT.isVector()) {
1656 return MVT::i1;
1657 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001658 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001659}
1660
Mehdi Aminieaabc512015-07-09 15:12:23 +00001661MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001662 return MVT::i32;
1663}
1664
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001665// Answering this is somewhat tricky and depends on the specific device which
1666// have different rates for fma or all f64 operations.
1667//
1668// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1669// regardless of which device (although the number of cycles differs between
1670// devices), so it is always profitable for f64.
1671//
1672// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1673// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1674// which we can always do even without fused FP ops since it returns the same
1675// result as the separate operations and since it is always full
1676// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1677// however does not support denormals, so we do report fma as faster if we have
1678// a fast fma device and require denormals.
1679//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001680bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1681 VT = VT.getScalarType();
1682
1683 if (!VT.isSimple())
1684 return false;
1685
1686 switch (VT.getSimpleVT().SimpleTy) {
1687 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001688 // This is as fast on some subtargets. However, we always have full rate f32
1689 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001690 // which we should prefer over fma. We can't use this if we want to support
1691 // denormals, so only report this in these cases.
1692 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001693 case MVT::f64:
1694 return true;
1695 default:
1696 break;
1697 }
1698
1699 return false;
1700}
1701
Tom Stellard75aadc22012-12-11 21:25:42 +00001702//===----------------------------------------------------------------------===//
1703// Custom DAG Lowering Operations
1704//===----------------------------------------------------------------------===//
1705
1706SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1707 switch (Op.getOpcode()) {
1708 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001709 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001710 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001711 SDValue Result = LowerLOAD(Op, DAG);
1712 assert((!Result.getNode() ||
1713 Result.getNode()->getNumValues() == 2) &&
1714 "Load should return a value and a chain");
1715 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001716 }
Tom Stellardaf775432013-10-23 00:44:32 +00001717
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001718 case ISD::FSIN:
1719 case ISD::FCOS:
1720 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001721 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001722 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard354a43c2016-04-01 18:27:37 +00001723 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001724 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001725 case ISD::GlobalAddress: {
1726 MachineFunction &MF = DAG.getMachineFunction();
1727 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1728 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001729 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001730 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001731 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001732 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Matt Arsenault99c14522016-04-25 19:27:24 +00001733 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG);
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001734 case ISD::TRAP: return lowerTRAP(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001735 }
1736 return SDValue();
1737}
1738
Tom Stellardf8794352012-12-19 22:10:31 +00001739/// \brief Helper function for LowerBRCOND
1740static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001741
Tom Stellardf8794352012-12-19 22:10:31 +00001742 SDNode *Parent = Value.getNode();
1743 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1744 I != E; ++I) {
1745
1746 if (I.getUse().get() != Value)
1747 continue;
1748
1749 if (I->getOpcode() == Opcode)
1750 return *I;
1751 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001752 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001753}
1754
Tom Stellardbc4497b2016-02-12 23:45:29 +00001755bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault6408c912016-09-16 22:11:18 +00001756 if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
1757 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1758 case AMDGPUIntrinsic::amdgcn_if:
1759 case AMDGPUIntrinsic::amdgcn_else:
1760 case AMDGPUIntrinsic::amdgcn_end_cf:
1761 case AMDGPUIntrinsic::amdgcn_loop:
1762 return true;
1763 default:
1764 return false;
1765 }
Tom Stellardbc4497b2016-02-12 23:45:29 +00001766 }
Matt Arsenault6408c912016-09-16 22:11:18 +00001767
1768 if (Intr->getOpcode() == ISD::INTRINSIC_WO_CHAIN) {
1769 switch (cast<ConstantSDNode>(Intr->getOperand(0))->getZExtValue()) {
1770 case AMDGPUIntrinsic::amdgcn_break:
1771 case AMDGPUIntrinsic::amdgcn_if_break:
1772 case AMDGPUIntrinsic::amdgcn_else_break:
1773 return true;
1774 default:
1775 return false;
1776 }
1777 }
1778
1779 return false;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001780}
1781
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001782void SITargetLowering::createDebuggerPrologueStackObjects(
1783 MachineFunction &MF) const {
1784 // Create stack objects that are used for emitting debugger prologue.
1785 //
1786 // Debugger prologue writes work group IDs and work item IDs to scratch memory
1787 // at fixed location in the following format:
1788 // offset 0: work group ID x
1789 // offset 4: work group ID y
1790 // offset 8: work group ID z
1791 // offset 16: work item ID x
1792 // offset 20: work item ID y
1793 // offset 24: work item ID z
1794 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1795 int ObjectIdx = 0;
1796
1797 // For each dimension:
1798 for (unsigned i = 0; i < 3; ++i) {
1799 // Create fixed stack object for work group ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001800 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001801 Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx);
1802 // Create fixed stack object for work item ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001803 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4 + 16, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001804 Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx);
1805 }
1806}
1807
Tom Stellardf8794352012-12-19 22:10:31 +00001808/// This transforms the control flow intrinsics to get the branch destination as
1809/// last parameter, also switches branch target with BR if the need arise
1810SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1811 SelectionDAG &DAG) const {
1812
Andrew Trickef9de2a2013-05-25 02:42:55 +00001813 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001814
1815 SDNode *Intr = BRCOND.getOperand(1).getNode();
1816 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001817 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001818 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001819
1820 if (Intr->getOpcode() == ISD::SETCC) {
1821 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001822 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001823 Intr = SetCC->getOperand(0).getNode();
1824
1825 } else {
1826 // Get the target from BR if we don't negate the condition
1827 BR = findUser(BRCOND, ISD::BR);
1828 Target = BR->getOperand(1);
1829 }
1830
Matt Arsenault6408c912016-09-16 22:11:18 +00001831 // FIXME: This changes the types of the intrinsics instead of introducing new
1832 // nodes with the correct types.
1833 // e.g. llvm.amdgcn.loop
1834
1835 // eg: i1,ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3
1836 // => t9: ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3, BasicBlock:ch<bb1 0x7fee5286d088>
1837
Nicolai Haehnleffbd56a2016-05-05 17:36:36 +00001838 if (!isCFIntrinsic(Intr)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +00001839 // This is a uniform branch so we don't need to legalize.
1840 return BRCOND;
1841 }
1842
Matt Arsenault6408c912016-09-16 22:11:18 +00001843 bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID ||
1844 Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN;
1845
Tom Stellardbc4497b2016-02-12 23:45:29 +00001846 assert(!SetCC ||
1847 (SetCC->getConstantOperandVal(1) == 1 &&
Tom Stellardbc4497b2016-02-12 23:45:29 +00001848 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1849 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00001850
Tom Stellardf8794352012-12-19 22:10:31 +00001851 // operands of the new intrinsic call
1852 SmallVector<SDValue, 4> Ops;
Matt Arsenault6408c912016-09-16 22:11:18 +00001853 if (HaveChain)
1854 Ops.push_back(BRCOND.getOperand(0));
1855
1856 Ops.append(Intr->op_begin() + (HaveChain ? 1 : 0), Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001857 Ops.push_back(Target);
1858
Matt Arsenault6408c912016-09-16 22:11:18 +00001859 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
1860
Tom Stellardf8794352012-12-19 22:10:31 +00001861 // build the new intrinsic call
1862 SDNode *Result = DAG.getNode(
1863 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001864 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001865
Matt Arsenault6408c912016-09-16 22:11:18 +00001866 if (!HaveChain) {
1867 SDValue Ops[] = {
1868 SDValue(Result, 0),
1869 BRCOND.getOperand(0)
1870 };
1871
1872 Result = DAG.getMergeValues(Ops, DL).getNode();
1873 }
1874
Tom Stellardf8794352012-12-19 22:10:31 +00001875 if (BR) {
1876 // Give the branch instruction our target
1877 SDValue Ops[] = {
1878 BR->getOperand(0),
1879 BRCOND.getOperand(2)
1880 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001881 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1882 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1883 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001884 }
1885
1886 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1887
1888 // Copy the intrinsic results to registers
1889 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1890 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1891 if (!CopyToReg)
1892 continue;
1893
1894 Chain = DAG.getCopyToReg(
1895 Chain, DL,
1896 CopyToReg->getOperand(1),
1897 SDValue(Result, i - 1),
1898 SDValue());
1899
1900 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1901 }
1902
1903 // Remove the old intrinsic from the chain
1904 DAG.ReplaceAllUsesOfValueWith(
1905 SDValue(Intr, Intr->getNumValues() - 1),
1906 Intr->getOperand(0));
1907
1908 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001909}
1910
Matt Arsenault99c14522016-04-25 19:27:24 +00001911SDValue SITargetLowering::getSegmentAperture(unsigned AS,
1912 SelectionDAG &DAG) const {
1913 SDLoc SL;
1914 MachineFunction &MF = DAG.getMachineFunction();
1915 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00001916 unsigned UserSGPR = Info->getQueuePtrUserSGPR();
1917 assert(UserSGPR != AMDGPU::NoRegister);
1918
Matt Arsenault99c14522016-04-25 19:27:24 +00001919 SDValue QueuePtr = CreateLiveInRegister(
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00001920 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
Matt Arsenault99c14522016-04-25 19:27:24 +00001921
1922 // Offset into amd_queue_t for group_segment_aperture_base_hi /
1923 // private_segment_aperture_base_hi.
1924 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44;
1925
1926 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr,
1927 DAG.getConstant(StructOffset, SL, MVT::i64));
1928
1929 // TODO: Use custom target PseudoSourceValue.
1930 // TODO: We should use the value from the IR intrinsic call, but it might not
1931 // be available and how do we get it?
1932 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()),
1933 AMDGPUAS::CONSTANT_ADDRESS));
1934
1935 MachinePointerInfo PtrInfo(V, StructOffset);
Justin Lebar9c375812016-07-15 18:27:10 +00001936 return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr, PtrInfo,
1937 MinAlign(64, StructOffset),
Justin Lebaradbf09e2016-09-11 01:38:58 +00001938 MachineMemOperand::MODereferenceable |
1939 MachineMemOperand::MOInvariant);
Matt Arsenault99c14522016-04-25 19:27:24 +00001940}
1941
1942SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
1943 SelectionDAG &DAG) const {
1944 SDLoc SL(Op);
1945 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op);
1946
1947 SDValue Src = ASC->getOperand(0);
1948
1949 // FIXME: Really support non-0 null pointers.
1950 SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32);
1951 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
1952
1953 // flat -> local/private
1954 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
1955 if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1956 ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1957 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
1958 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
1959
1960 return DAG.getNode(ISD::SELECT, SL, MVT::i32,
1961 NonNull, Ptr, SegmentNullPtr);
1962 }
1963 }
1964
1965 // local/private -> flat
1966 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
1967 if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1968 ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1969 SDValue NonNull
1970 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
1971
1972 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG);
1973 SDValue CvtPtr
1974 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
1975
1976 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull,
1977 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr),
1978 FlatNullPtr);
1979 }
1980 }
1981
1982 // global <-> flat are no-ops and never emitted.
1983
1984 const MachineFunction &MF = DAG.getMachineFunction();
1985 DiagnosticInfoUnsupported InvalidAddrSpaceCast(
1986 *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc());
1987 DAG.getContext()->diagnose(InvalidAddrSpaceCast);
1988
1989 return DAG.getUNDEF(ASC->getValueType(0));
1990}
1991
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00001992static bool shouldEmitFixup(const GlobalValue *GV,
1993 const TargetMachine &TM) {
1994 // FIXME: We need to emit global variables in constant address space in a
1995 // separate section, and use relocations.
1996 return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS;
1997}
1998
Tom Stellard418beb72016-07-13 14:23:33 +00001999static bool shouldEmitGOTReloc(const GlobalValue *GV,
2000 const TargetMachine &TM) {
2001 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
2002 !TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
Tom Stellardb164a982016-06-25 01:59:16 +00002003}
2004
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002005static bool shouldEmitPCReloc(const GlobalValue *GV,
2006 const TargetMachine &TM) {
2007 return !shouldEmitFixup(GV, TM) && !shouldEmitGOTReloc(GV, TM);
2008}
2009
Tom Stellard418beb72016-07-13 14:23:33 +00002010bool
2011SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2012 // We can fold offsets for anything that doesn't require a GOT relocation.
2013 return GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
2014 !shouldEmitGOTReloc(GA->getGlobal(), getTargetMachine());
2015}
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002016
Tom Stellard418beb72016-07-13 14:23:33 +00002017static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV,
2018 SDLoc DL, unsigned Offset, EVT PtrVT,
2019 unsigned GAFlags = SIInstrInfo::MO_NONE) {
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002020 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is
2021 // lowered to the following code sequence:
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002022 //
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002023 // For constant address space:
2024 // s_getpc_b64 s[0:1]
2025 // s_add_u32 s0, s0, $symbol
2026 // s_addc_u32 s1, s1, 0
2027 //
2028 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
2029 // a fixup or relocation is emitted to replace $symbol with a literal
2030 // constant, which is a pc-relative offset from the encoding of the $symbol
2031 // operand to the global variable.
2032 //
2033 // For global address space:
2034 // s_getpc_b64 s[0:1]
2035 // s_add_u32 s0, s0, $symbol@{gotpc}rel32@lo
2036 // s_addc_u32 s1, s1, $symbol@{gotpc}rel32@hi
2037 //
2038 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
2039 // fixups or relocations are emitted to replace $symbol@*@lo and
2040 // $symbol@*@hi with lower 32 bits and higher 32 bits of a literal constant,
2041 // which is a 64-bit pc-relative offset from the encoding of the $symbol
2042 // operand to the global variable.
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002043 //
2044 // What we want here is an offset from the value returned by s_getpc
2045 // (which is the address of the s_add_u32 instruction) to the global
2046 // variable, but since the encoding of $symbol starts 4 bytes after the start
2047 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too
2048 // small. This requires us to add 4 to the global variable offset in order to
2049 // compute the correct address.
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002050 SDValue PtrLo = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
2051 GAFlags);
2052 SDValue PtrHi = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
2053 GAFlags == SIInstrInfo::MO_NONE ?
2054 GAFlags : GAFlags + 1);
2055 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, PtrLo, PtrHi);
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002056}
2057
Tom Stellard418beb72016-07-13 14:23:33 +00002058SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
2059 SDValue Op,
2060 SelectionDAG &DAG) const {
2061 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
2062
2063 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS &&
2064 GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS)
2065 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
2066
2067 SDLoc DL(GSD);
2068 const GlobalValue *GV = GSD->getGlobal();
2069 EVT PtrVT = Op.getValueType();
2070
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002071 if (shouldEmitFixup(GV, getTargetMachine()))
Tom Stellard418beb72016-07-13 14:23:33 +00002072 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT);
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002073 else if (shouldEmitPCReloc(GV, getTargetMachine()))
2074 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT,
2075 SIInstrInfo::MO_REL32);
Tom Stellard418beb72016-07-13 14:23:33 +00002076
2077 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT,
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002078 SIInstrInfo::MO_GOTPCREL32);
Tom Stellard418beb72016-07-13 14:23:33 +00002079
2080 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext());
2081 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
2082 const DataLayout &DataLayout = DAG.getDataLayout();
2083 unsigned Align = DataLayout.getABITypeAlignment(PtrTy);
2084 // FIXME: Use a PseudoSourceValue once those can be assigned an address space.
2085 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
2086
Justin Lebar9c375812016-07-15 18:27:10 +00002087 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align,
Justin Lebaradbf09e2016-09-11 01:38:58 +00002088 MachineMemOperand::MODereferenceable |
2089 MachineMemOperand::MOInvariant);
Tom Stellard418beb72016-07-13 14:23:33 +00002090}
2091
Matt Arsenault0bb294b2016-06-17 22:27:03 +00002092SDValue SITargetLowering::lowerTRAP(SDValue Op,
2093 SelectionDAG &DAG) const {
2094 const MachineFunction &MF = DAG.getMachineFunction();
2095 DiagnosticInfoUnsupported NoTrap(*MF.getFunction(),
2096 "trap handler not supported",
2097 Op.getDebugLoc(),
2098 DS_Warning);
2099 DAG.getContext()->diagnose(NoTrap);
2100
2101 // Emit s_endpgm.
2102
2103 // FIXME: This should really be selected to s_trap, but that requires
2104 // setting up the trap handler for it o do anything.
Matt Arsenault9babdf42016-06-22 20:15:28 +00002105 return DAG.getNode(AMDGPUISD::ENDPGM, SDLoc(Op), MVT::Other,
2106 Op.getOperand(0));
Matt Arsenault0bb294b2016-06-17 22:27:03 +00002107}
2108
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002109SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain,
2110 const SDLoc &DL, SDValue V) const {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00002111 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
2112 // the destination register.
2113 //
Tom Stellardfc92e772015-05-12 14:18:14 +00002114 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
2115 // so we will end up with redundant moves to m0.
2116 //
Matt Arsenault4ac341c2016-04-14 21:58:15 +00002117 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
2118
2119 // A Null SDValue creates a glue result.
2120 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
2121 V, Chain);
2122 return SDValue(M0, 0);
Tom Stellardfc92e772015-05-12 14:18:14 +00002123}
2124
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002125SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
2126 SDValue Op,
2127 MVT VT,
2128 unsigned Offset) const {
2129 SDLoc SL(Op);
2130 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
2131 DAG.getEntryNode(), Offset, false);
2132 // The local size values will have the hi 16-bits as zero.
2133 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
2134 DAG.getValueType(VT));
2135}
2136
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002137static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
Matt Arsenaulte0132462016-01-30 05:19:45 +00002138 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002139 "non-hsa intrinsic with hsa target",
2140 DL.getDebugLoc());
2141 DAG.getContext()->diagnose(BadIntrin);
2142 return DAG.getUNDEF(VT);
2143}
2144
2145static SDValue emitRemovedIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
2146 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
2147 "intrinsic not supported on subtarget",
2148 DL.getDebugLoc());
Matt Arsenaulte0132462016-01-30 05:19:45 +00002149 DAG.getContext()->diagnose(BadIntrin);
2150 return DAG.getUNDEF(VT);
2151}
2152
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002153SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
2154 SelectionDAG &DAG) const {
2155 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00002156 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002157 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002158
2159 EVT VT = Op.getValueType();
2160 SDLoc DL(Op);
2161 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2162
Sanjay Patela2607012015-09-16 16:31:21 +00002163 // TODO: Should this propagate fast-math-flags?
2164
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002165 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00002166 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault48ab5262016-04-25 19:27:18 +00002167 case Intrinsic::amdgcn_queue_ptr: {
Tom Stellard0b76fc4c2016-09-16 21:34:26 +00002168 if (!Subtarget->isAmdCodeObjectV2()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00002169 DiagnosticInfoUnsupported BadIntrin(
2170 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
2171 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00002172 DAG.getContext()->diagnose(BadIntrin);
2173 return DAG.getUNDEF(VT);
2174 }
2175
Matt Arsenault48ab5262016-04-25 19:27:18 +00002176 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ?
2177 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00002178 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
Matt Arsenault48ab5262016-04-25 19:27:18 +00002179 TRI->getPreloadedValue(MF, Reg), VT);
2180 }
Jan Veselyfea814d2016-06-21 20:46:20 +00002181 case Intrinsic::amdgcn_implicitarg_ptr: {
2182 unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
2183 return LowerParameterPtr(DAG, DL, DAG.getEntryNode(), offset);
2184 }
Matt Arsenaultdc4ebad2016-04-29 21:16:52 +00002185 case Intrinsic::amdgcn_kernarg_segment_ptr: {
2186 unsigned Reg
2187 = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
2188 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
2189 }
Matt Arsenault8d718dc2016-07-22 17:01:30 +00002190 case Intrinsic::amdgcn_dispatch_id: {
2191 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_ID);
2192 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
2193 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002194 case Intrinsic::amdgcn_rcp:
2195 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
2196 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00002197 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002198 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002199 case Intrinsic::amdgcn_rsq_legacy: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002200 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002201 return emitRemovedIntrinsicError(DAG, DL, VT);
2202
2203 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
2204 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002205 case Intrinsic::amdgcn_rcp_legacy: {
2206 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
2207 return emitRemovedIntrinsicError(DAG, DL, VT);
2208 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1));
2209 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +00002210 case Intrinsic::amdgcn_rsq_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002211 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00002212 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00002213
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002214 Type *Type = VT.getTypeForEVT(*DAG.getContext());
2215 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
2216 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
2217
2218 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
2219 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
2220 DAG.getConstantFP(Max, DL, VT));
2221 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
2222 DAG.getConstantFP(Min, DL, VT));
2223 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002224 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002225 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002226 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002227
Tom Stellardec2e43c2014-09-22 15:35:29 +00002228 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2229 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002230 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002231 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002232 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002233
Tom Stellardec2e43c2014-09-22 15:35:29 +00002234 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2235 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002236 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002237 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002238 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002239
Tom Stellardec2e43c2014-09-22 15:35:29 +00002240 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2241 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002242 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002243 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002244 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002245
Tom Stellardec2e43c2014-09-22 15:35:29 +00002246 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2247 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002248 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002249 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002250 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002251
Tom Stellardec2e43c2014-09-22 15:35:29 +00002252 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2253 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002254 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002255 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002256 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002257
Tom Stellardec2e43c2014-09-22 15:35:29 +00002258 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2259 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002260 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002261 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002262 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002263
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002264 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2265 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002266 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002267 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002268 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002269
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002270 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2271 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002272 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002273 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002274 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002275
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002276 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2277 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenault43976df2016-01-30 04:25:19 +00002278 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002279 case Intrinsic::r600_read_tgid_x:
2280 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002281 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002282 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002283 case Intrinsic::r600_read_tgid_y:
2284 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002285 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002286 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002287 case Intrinsic::r600_read_tgid_z:
2288 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002289 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002290 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002291 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002292 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002293 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002294 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002295 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002296 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002297 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002298 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002299 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002300 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002301 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002302 case AMDGPUIntrinsic::SI_load_const: {
2303 SDValue Ops[] = {
2304 Op.getOperand(1),
2305 Op.getOperand(2)
2306 };
2307
2308 MachineMemOperand *MMO = MF.getMachineMemOperand(
Justin Lebaradbf09e2016-09-11 01:38:58 +00002309 MachinePointerInfo(),
2310 MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable |
2311 MachineMemOperand::MOInvariant,
2312 VT.getStoreSize(), 4);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002313 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
2314 Op->getVTList(), Ops, VT, MMO);
2315 }
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002316 case AMDGPUIntrinsic::amdgcn_fdiv_fast: {
2317 return lowerFDIV_FAST(Op, DAG);
2318 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002319 case AMDGPUIntrinsic::SI_vs_load_input:
2320 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
2321 Op.getOperand(1),
2322 Op.getOperand(2),
2323 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00002324
Tom Stellard2a9d9472015-05-12 15:00:46 +00002325 case AMDGPUIntrinsic::SI_fs_constant: {
2326 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2327 SDValue Glue = M0.getValue(1);
2328 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
2329 DAG.getConstant(2, DL, MVT::i32), // P0
2330 Op.getOperand(1), Op.getOperand(2), Glue);
2331 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00002332 case AMDGPUIntrinsic::SI_packf16:
2333 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
2334 return DAG.getUNDEF(MVT::i32);
2335 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00002336 case AMDGPUIntrinsic::SI_fs_interp: {
2337 SDValue IJ = Op.getOperand(4);
2338 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2339 DAG.getConstant(0, DL, MVT::i32));
2340 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2341 DAG.getConstant(1, DL, MVT::i32));
2342 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2343 SDValue Glue = M0.getValue(1);
2344 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
2345 DAG.getVTList(MVT::f32, MVT::Glue),
2346 I, Op.getOperand(1), Op.getOperand(2), Glue);
2347 Glue = SDValue(P1.getNode(), 1);
2348 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
2349 Op.getOperand(1), Op.getOperand(2), Glue);
2350 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00002351 case Intrinsic::amdgcn_interp_p1: {
2352 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
2353 SDValue Glue = M0.getValue(1);
2354 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
2355 Op.getOperand(2), Op.getOperand(3), Glue);
2356 }
2357 case Intrinsic::amdgcn_interp_p2: {
2358 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
2359 SDValue Glue = SDValue(M0.getNode(), 1);
2360 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
2361 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
2362 Glue);
2363 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002364 case Intrinsic::amdgcn_sin:
2365 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
2366
2367 case Intrinsic::amdgcn_cos:
2368 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
2369
2370 case Intrinsic::amdgcn_log_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002371 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002372 return SDValue();
2373
2374 DiagnosticInfoUnsupported BadIntrin(
2375 *MF.getFunction(), "intrinsic not supported on subtarget",
2376 DL.getDebugLoc());
2377 DAG.getContext()->diagnose(BadIntrin);
2378 return DAG.getUNDEF(VT);
2379 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002380 case Intrinsic::amdgcn_ldexp:
2381 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
2382 Op.getOperand(1), Op.getOperand(2));
Matt Arsenault74015162016-05-28 00:19:52 +00002383
2384 case Intrinsic::amdgcn_fract:
2385 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
2386
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002387 case Intrinsic::amdgcn_class:
2388 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
2389 Op.getOperand(1), Op.getOperand(2));
2390 case Intrinsic::amdgcn_div_fmas:
2391 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
2392 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
2393 Op.getOperand(4));
2394
2395 case Intrinsic::amdgcn_div_fixup:
2396 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
2397 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
2398
2399 case Intrinsic::amdgcn_trig_preop:
2400 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
2401 Op.getOperand(1), Op.getOperand(2));
2402 case Intrinsic::amdgcn_div_scale: {
2403 // 3rd parameter required to be a constant.
2404 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2405 if (!Param)
2406 return DAG.getUNDEF(VT);
2407
2408 // Translate to the operands expected by the machine instruction. The
2409 // first parameter must be the same as the first instruction.
2410 SDValue Numerator = Op.getOperand(1);
2411 SDValue Denominator = Op.getOperand(2);
2412
2413 // Note this order is opposite of the machine instruction's operations,
2414 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
2415 // intrinsic has the numerator as the first operand to match a normal
2416 // division operation.
2417
2418 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
2419
2420 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
2421 Denominator, Numerator);
2422 }
Wei Ding07e03712016-07-28 16:42:13 +00002423 case Intrinsic::amdgcn_icmp: {
2424 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2425 int CondCode = CD->getSExtValue();
2426
2427 if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE ||
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002428 CondCode >= ICmpInst::Predicate::BAD_ICMP_PREDICATE)
Wei Ding07e03712016-07-28 16:42:13 +00002429 return DAG.getUNDEF(VT);
2430
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002431 ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode);
Wei Ding07e03712016-07-28 16:42:13 +00002432 ISD::CondCode CCOpcode = getICmpCondCode(IcInput);
2433 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2434 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2435 }
2436 case Intrinsic::amdgcn_fcmp: {
2437 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2438 int CondCode = CD->getSExtValue();
2439
2440 if (CondCode <= FCmpInst::Predicate::FCMP_FALSE ||
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002441 CondCode >= FCmpInst::Predicate::FCMP_TRUE)
Wei Ding07e03712016-07-28 16:42:13 +00002442 return DAG.getUNDEF(VT);
2443
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002444 FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode);
Wei Ding07e03712016-07-28 16:42:13 +00002445 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput);
2446 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2447 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2448 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002449 case Intrinsic::amdgcn_fmul_legacy:
2450 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT,
2451 Op.getOperand(1), Op.getOperand(2));
Matt Arsenaultc96e1de2016-07-18 18:35:05 +00002452 case Intrinsic::amdgcn_sffbh:
2453 case AMDGPUIntrinsic::AMDGPU_flbit_i32: // Legacy name.
2454 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002455 default:
2456 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
2457 }
2458}
2459
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002460SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
2461 SelectionDAG &DAG) const {
2462 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2463 switch (IntrID) {
2464 case Intrinsic::amdgcn_atomic_inc:
2465 case Intrinsic::amdgcn_atomic_dec: {
2466 MemSDNode *M = cast<MemSDNode>(Op);
2467 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
2468 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
2469 SDValue Ops[] = {
2470 M->getOperand(0), // Chain
2471 M->getOperand(2), // Ptr
2472 M->getOperand(3) // Value
2473 };
2474
2475 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
2476 M->getMemoryVT(), M->getMemOperand());
2477 }
2478 default:
2479 return SDValue();
2480 }
2481}
2482
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002483SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
2484 SelectionDAG &DAG) const {
2485 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00002486 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002487 SDValue Chain = Op.getOperand(0);
2488 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2489
2490 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00002491 case AMDGPUIntrinsic::SI_sendmsg: {
2492 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
2493 SDValue Glue = Chain.getValue(1);
2494 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
2495 Op.getOperand(2), Glue);
2496 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002497 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002498 SDValue Ops[] = {
2499 Chain,
2500 Op.getOperand(2),
2501 Op.getOperand(3),
2502 Op.getOperand(4),
2503 Op.getOperand(5),
2504 Op.getOperand(6),
2505 Op.getOperand(7),
2506 Op.getOperand(8),
2507 Op.getOperand(9),
2508 Op.getOperand(10),
2509 Op.getOperand(11),
2510 Op.getOperand(12),
2511 Op.getOperand(13),
2512 Op.getOperand(14)
2513 };
2514
2515 EVT VT = Op.getOperand(3).getValueType();
2516
2517 MachineMemOperand *MMO = MF.getMachineMemOperand(
2518 MachinePointerInfo(),
2519 MachineMemOperand::MOStore,
2520 VT.getStoreSize(), 4);
2521 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
2522 Op->getVTList(), Ops, VT, MMO);
2523 }
Matt Arsenault00568682016-07-13 06:04:22 +00002524 case AMDGPUIntrinsic::AMDGPU_kill: {
Matt Arsenault03006fd2016-07-19 16:27:56 +00002525 SDValue Src = Op.getOperand(2);
2526 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) {
Matt Arsenault00568682016-07-13 06:04:22 +00002527 if (!K->isNegative())
2528 return Chain;
Matt Arsenault03006fd2016-07-19 16:27:56 +00002529
2530 SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32);
2531 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne);
Matt Arsenault00568682016-07-13 06:04:22 +00002532 }
2533
Matt Arsenault03006fd2016-07-19 16:27:56 +00002534 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src);
2535 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast);
Matt Arsenault00568682016-07-13 06:04:22 +00002536 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002537 default:
2538 return SDValue();
2539 }
2540}
2541
Tom Stellard81d871d2013-11-13 23:36:50 +00002542SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2543 SDLoc DL(Op);
2544 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00002545 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00002546 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00002547
Matt Arsenaulta1436412016-02-10 18:21:45 +00002548 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
2549 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected");
Matt Arsenault6dfda962016-02-10 18:21:39 +00002550 // FIXME: Copied from PPC
2551 // First, load into 32 bits, then truncate to 1 bit.
2552
2553 SDValue Chain = Load->getChain();
2554 SDValue BasePtr = Load->getBasePtr();
2555 MachineMemOperand *MMO = Load->getMemOperand();
2556
2557 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
2558 BasePtr, MVT::i8, MMO);
2559
2560 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002561 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00002562 NewLD.getValue(1)
2563 };
2564
2565 return DAG.getMergeValues(Ops, DL);
2566 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002567
Matt Arsenaulta1436412016-02-10 18:21:45 +00002568 if (!MemVT.isVector())
2569 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002570
Matt Arsenaulta1436412016-02-10 18:21:45 +00002571 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
2572 "Custom lowering for non-i32 vectors hasn't been implemented.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002573
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002574 unsigned AS = Load->getAddressSpace();
2575 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT,
2576 AS, Load->getAlignment())) {
2577 SDValue Ops[2];
2578 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG);
2579 return DAG.getMergeValues(Ops, DL);
2580 }
2581
2582 unsigned NumElements = MemVT.getVectorNumElements();
2583 switch (AS) {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002584 case AMDGPUAS::CONSTANT_ADDRESS:
2585 if (isMemOpUniform(Load))
2586 return SDValue();
2587 // Non-uniform loads will be selected to MUBUF instructions, so they
2588 // have the same legalization requires ments as global and private
2589 // loads.
2590 //
Justin Bognerb03fd122016-08-17 05:10:15 +00002591 LLVM_FALLTHROUGH;
Matt Arsenaulta1436412016-02-10 18:21:45 +00002592 case AMDGPUAS::GLOBAL_ADDRESS:
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002593 case AMDGPUAS::FLAT_ADDRESS:
2594 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00002595 return SplitVectorLoad(Op, DAG);
2596 // v4 loads are supported for private and global memory.
2597 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002598 case AMDGPUAS::PRIVATE_ADDRESS: {
2599 // Depending on the setting of the private_element_size field in the
2600 // resource descriptor, we can only make private accesses up to a certain
2601 // size.
2602 switch (Subtarget->getMaxPrivateElementSize()) {
2603 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002604 return scalarizeVectorLoad(Load, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002605 case 8:
2606 if (NumElements > 2)
2607 return SplitVectorLoad(Op, DAG);
2608 return SDValue();
2609 case 16:
2610 // Same as global/flat
2611 if (NumElements > 4)
2612 return SplitVectorLoad(Op, DAG);
2613 return SDValue();
2614 default:
2615 llvm_unreachable("unsupported private_element_size");
2616 }
2617 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002618 case AMDGPUAS::LOCAL_ADDRESS: {
2619 if (NumElements > 2)
2620 return SplitVectorLoad(Op, DAG);
2621
2622 if (NumElements == 2)
2623 return SDValue();
2624
Matt Arsenaulta1436412016-02-10 18:21:45 +00002625 // If properly aligned, if we split we might be able to use ds_read_b64.
2626 return SplitVectorLoad(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002627 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00002628 default:
2629 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00002630 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002631}
2632
Tom Stellard0ec134f2014-02-04 17:18:40 +00002633SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2634 if (Op.getValueType() != MVT::i64)
2635 return SDValue();
2636
2637 SDLoc DL(Op);
2638 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002639
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002640 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2641 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002642
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002643 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
2644 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
2645
2646 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
2647 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002648
2649 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
2650
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002651 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
2652 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002653
2654 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
2655
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002656 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002657 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002658}
2659
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002660// Catch division cases where we can use shortcuts with rcp and rsq
2661// instructions.
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002662SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op,
2663 SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002664 SDLoc SL(Op);
2665 SDValue LHS = Op.getOperand(0);
2666 SDValue RHS = Op.getOperand(1);
2667 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002668 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002669
2670 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault979902b2016-08-02 22:25:04 +00002671 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals()))) {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002672
Matt Arsenault979902b2016-08-02 22:25:04 +00002673 if (CLHS->isExactlyValue(1.0)) {
2674 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
2675 // the CI documentation has a worst case error of 1 ulp.
2676 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
2677 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002678
Matt Arsenault979902b2016-08-02 22:25:04 +00002679 // 1.0 / sqrt(x) -> rsq(x)
2680 //
2681 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
2682 // error seems really high at 2^29 ULP.
2683 if (RHS.getOpcode() == ISD::FSQRT)
2684 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
2685
2686 // 1.0 / x -> rcp(x)
2687 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
2688 }
2689
2690 // Same as for 1.0, but expand the sign out of the constant.
2691 if (CLHS->isExactlyValue(-1.0)) {
2692 // -1.0 / x -> rcp (fneg x)
2693 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2694 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS);
2695 }
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002696 }
2697 }
2698
Wei Dinged0f97f2016-06-09 19:17:15 +00002699 const SDNodeFlags *Flags = Op->getFlags();
2700
2701 if (Unsafe || Flags->hasAllowReciprocal()) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002702 // Turn into multiply by the reciprocal.
2703 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00002704 SDNodeFlags Flags;
2705 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002706 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00002707 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002708 }
2709
2710 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002711}
2712
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002713// Faster 2.5 ULP division that does not support denormals.
2714SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const {
2715 SDLoc SL(Op);
2716 SDValue LHS = Op.getOperand(1);
2717 SDValue RHS = Op.getOperand(2);
2718
2719 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
2720
2721 const APFloat K0Val(BitsToFloat(0x6f800000));
2722 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
2723
2724 const APFloat K1Val(BitsToFloat(0x2f800000));
2725 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
2726
2727 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
2728
2729 EVT SetCCVT =
2730 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
2731
2732 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
2733
2734 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
2735
2736 // TODO: Should this propagate fast-math-flags?
2737 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
2738
2739 // rcp does not support denormals.
2740 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
2741
2742 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
2743
2744 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
2745}
2746
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002747SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002748 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
Eric Christopher538d09d02016-06-07 20:27:12 +00002749 return FastLowered;
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002750
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002751 SDLoc SL(Op);
2752 SDValue LHS = Op.getOperand(0);
2753 SDValue RHS = Op.getOperand(1);
2754
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002755 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002756
Wei Dinged0f97f2016-06-09 19:17:15 +00002757 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002758
Wei Dinged0f97f2016-06-09 19:17:15 +00002759 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, RHS, RHS, LHS);
2760 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, LHS, RHS, LHS);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002761
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002762 // Denominator is scaled to not be denormal, so using rcp is ok.
Wei Dinged0f97f2016-06-09 19:17:15 +00002763 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002764
Wei Dinged0f97f2016-06-09 19:17:15 +00002765 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002766
Wei Dinged0f97f2016-06-09 19:17:15 +00002767 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, ApproxRcp, One);
2768 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, ApproxRcp);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002769
Wei Dinged0f97f2016-06-09 19:17:15 +00002770 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, NumeratorScaled, Fma1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002771
Wei Dinged0f97f2016-06-09 19:17:15 +00002772 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, NumeratorScaled);
2773 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul);
2774 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, NumeratorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002775
Wei Dinged0f97f2016-06-09 19:17:15 +00002776 SDValue Scale = NumeratorScaled.getValue(1);
2777 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, Fma4, Fma1, Fma3, Scale);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002778
Wei Dinged0f97f2016-06-09 19:17:15 +00002779 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002780}
2781
2782SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002783 if (DAG.getTarget().Options.UnsafeFPMath)
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002784 return lowerFastUnsafeFDIV(Op, DAG);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002785
2786 SDLoc SL(Op);
2787 SDValue X = Op.getOperand(0);
2788 SDValue Y = Op.getOperand(1);
2789
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002790 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002791
2792 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
2793
2794 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
2795
2796 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
2797
2798 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
2799
2800 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
2801
2802 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
2803
2804 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
2805
2806 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
2807
2808 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
2809 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
2810
2811 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
2812 NegDivScale0, Mul, DivScale1);
2813
2814 SDValue Scale;
2815
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002816 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002817 // Workaround a hardware bug on SI where the condition output from div_scale
2818 // is not usable.
2819
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002820 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002821
2822 // Figure out if the scale to use for div_fmas.
2823 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
2824 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
2825 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
2826 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
2827
2828 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
2829 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
2830
2831 SDValue Scale0Hi
2832 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
2833 SDValue Scale1Hi
2834 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
2835
2836 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
2837 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
2838 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
2839 } else {
2840 Scale = DivScale1.getValue(1);
2841 }
2842
2843 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
2844 Fma4, Fma3, Mul, Scale);
2845
2846 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002847}
2848
2849SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
2850 EVT VT = Op.getValueType();
2851
2852 if (VT == MVT::f32)
2853 return LowerFDIV32(Op, DAG);
2854
2855 if (VT == MVT::f64)
2856 return LowerFDIV64(Op, DAG);
2857
2858 llvm_unreachable("Unexpected type for fdiv");
2859}
2860
Tom Stellard81d871d2013-11-13 23:36:50 +00002861SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2862 SDLoc DL(Op);
2863 StoreSDNode *Store = cast<StoreSDNode>(Op);
2864 EVT VT = Store->getMemoryVT();
2865
Matt Arsenault95245662016-02-11 05:32:46 +00002866 if (VT == MVT::i1) {
2867 return DAG.getTruncStore(Store->getChain(), DL,
2868 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
2869 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00002870 }
2871
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002872 assert(VT.isVector() &&
2873 Store->getValue().getValueType().getScalarType() == MVT::i32);
2874
2875 unsigned AS = Store->getAddressSpace();
2876 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
2877 AS, Store->getAlignment())) {
2878 return expandUnalignedStore(Store, DAG);
2879 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002880
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002881 unsigned NumElements = VT.getVectorNumElements();
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002882 switch (AS) {
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002883 case AMDGPUAS::GLOBAL_ADDRESS:
2884 case AMDGPUAS::FLAT_ADDRESS:
2885 if (NumElements > 4)
2886 return SplitVectorStore(Op, DAG);
2887 return SDValue();
2888 case AMDGPUAS::PRIVATE_ADDRESS: {
2889 switch (Subtarget->getMaxPrivateElementSize()) {
2890 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002891 return scalarizeVectorStore(Store, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002892 case 8:
2893 if (NumElements > 2)
2894 return SplitVectorStore(Op, DAG);
2895 return SDValue();
2896 case 16:
2897 if (NumElements > 4)
2898 return SplitVectorStore(Op, DAG);
2899 return SDValue();
2900 default:
2901 llvm_unreachable("unsupported private_element_size");
2902 }
2903 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002904 case AMDGPUAS::LOCAL_ADDRESS: {
2905 if (NumElements > 2)
2906 return SplitVectorStore(Op, DAG);
2907
2908 if (NumElements == 2)
2909 return Op;
2910
Matt Arsenault95245662016-02-11 05:32:46 +00002911 // If properly aligned, if we split we might be able to use ds_write_b64.
2912 return SplitVectorStore(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002913 }
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002914 default:
2915 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00002916 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002917}
2918
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002919SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002920 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002921 EVT VT = Op.getValueType();
2922 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00002923 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002924 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
2925 DAG.getNode(ISD::FMUL, DL, VT, Arg,
2926 DAG.getConstantFP(0.5/M_PI, DL,
2927 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002928
2929 switch (Op.getOpcode()) {
2930 case ISD::FCOS:
2931 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
2932 case ISD::FSIN:
2933 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
2934 default:
2935 llvm_unreachable("Wrong trig opcode");
2936 }
2937}
2938
Tom Stellard354a43c2016-04-01 18:27:37 +00002939SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
2940 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
2941 assert(AtomicNode->isCompareAndSwap());
2942 unsigned AS = AtomicNode->getAddressSpace();
2943
2944 // No custom lowering required for local address space
2945 if (!isFlatGlobalAddrSpace(AS))
2946 return Op;
2947
2948 // Non-local address space requires custom lowering for atomic compare
2949 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
2950 SDLoc DL(Op);
2951 SDValue ChainIn = Op.getOperand(0);
2952 SDValue Addr = Op.getOperand(1);
2953 SDValue Old = Op.getOperand(2);
2954 SDValue New = Op.getOperand(3);
2955 EVT VT = Op.getValueType();
2956 MVT SimpleVT = VT.getSimpleVT();
2957 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
2958
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002959 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
Tom Stellard354a43c2016-04-01 18:27:37 +00002960 SDValue Ops[] = { ChainIn, Addr, NewOld };
Matt Arsenault88701812016-06-09 23:42:48 +00002961
2962 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(),
2963 Ops, VT, AtomicNode->getMemOperand());
Tom Stellard354a43c2016-04-01 18:27:37 +00002964}
2965
Tom Stellard75aadc22012-12-11 21:25:42 +00002966//===----------------------------------------------------------------------===//
2967// Custom DAG optimizations
2968//===----------------------------------------------------------------------===//
2969
Matt Arsenault364a6742014-06-11 17:50:44 +00002970SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00002971 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00002972 EVT VT = N->getValueType(0);
2973 EVT ScalarVT = VT.getScalarType();
2974 if (ScalarVT != MVT::f32)
2975 return SDValue();
2976
2977 SelectionDAG &DAG = DCI.DAG;
2978 SDLoc DL(N);
2979
2980 SDValue Src = N->getOperand(0);
2981 EVT SrcVT = Src.getValueType();
2982
2983 // TODO: We could try to match extracting the higher bytes, which would be
2984 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
2985 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
2986 // about in practice.
2987 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
2988 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
2989 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
2990 DCI.AddToWorklist(Cvt.getNode());
2991 return Cvt;
2992 }
2993 }
2994
Matt Arsenault364a6742014-06-11 17:50:44 +00002995 return SDValue();
2996}
2997
Eric Christopher6c5b5112015-03-11 18:43:21 +00002998/// \brief Return true if the given offset Size in bytes can be folded into
2999/// the immediate offsets of a memory instruction for the given address space.
3000static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003001 const SISubtarget &STI) {
Eric Christopher6c5b5112015-03-11 18:43:21 +00003002 switch (AS) {
3003 case AMDGPUAS::GLOBAL_ADDRESS: {
3004 // MUBUF instructions a 12-bit offset in bytes.
3005 return isUInt<12>(OffsetSize);
3006 }
3007 case AMDGPUAS::CONSTANT_ADDRESS: {
3008 // SMRD instructions have an 8-bit offset in dwords on SI and
3009 // a 20-bit offset in bytes on VI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003010 if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Eric Christopher6c5b5112015-03-11 18:43:21 +00003011 return isUInt<20>(OffsetSize);
3012 else
3013 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
3014 }
3015 case AMDGPUAS::LOCAL_ADDRESS:
3016 case AMDGPUAS::REGION_ADDRESS: {
3017 // The single offset versions have a 16-bit offset in bytes.
3018 return isUInt<16>(OffsetSize);
3019 }
3020 case AMDGPUAS::PRIVATE_ADDRESS:
3021 // Indirect register addressing does not use any offsets.
3022 default:
3023 return 0;
3024 }
3025}
3026
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003027// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
3028
3029// This is a variant of
3030// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
3031//
3032// The normal DAG combiner will do this, but only if the add has one use since
3033// that would increase the number of instructions.
3034//
3035// This prevents us from seeing a constant offset that can be folded into a
3036// memory instruction's addressing mode. If we know the resulting add offset of
3037// a pointer can be folded into an addressing offset, we can replace the pointer
3038// operand with the add of new constant offset. This eliminates one of the uses,
3039// and may allow the remaining use to also be simplified.
3040//
3041SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
3042 unsigned AddrSpace,
3043 DAGCombinerInfo &DCI) const {
3044 SDValue N0 = N->getOperand(0);
3045 SDValue N1 = N->getOperand(1);
3046
3047 if (N0.getOpcode() != ISD::ADD)
3048 return SDValue();
3049
3050 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
3051 if (!CN1)
3052 return SDValue();
3053
3054 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3055 if (!CAdd)
3056 return SDValue();
3057
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003058 // If the resulting offset is too large, we can't fold it into the addressing
3059 // mode offset.
3060 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003061 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget()))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003062 return SDValue();
3063
3064 SelectionDAG &DAG = DCI.DAG;
3065 SDLoc SL(N);
3066 EVT VT = N->getValueType(0);
3067
3068 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003069 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003070
3071 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
3072}
3073
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003074static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) {
3075 return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) ||
3076 (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) ||
3077 (Opc == ISD::XOR && Val == 0);
3078}
3079
3080// Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This
3081// will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit
3082// integer combine opportunities since most 64-bit operations are decomposed
3083// this way. TODO: We won't want this for SALU especially if it is an inline
3084// immediate.
3085SDValue SITargetLowering::splitBinaryBitConstantOp(
3086 DAGCombinerInfo &DCI,
3087 const SDLoc &SL,
3088 unsigned Opc, SDValue LHS,
3089 const ConstantSDNode *CRHS) const {
3090 uint64_t Val = CRHS->getZExtValue();
3091 uint32_t ValLo = Lo_32(Val);
3092 uint32_t ValHi = Hi_32(Val);
3093 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
3094
3095 if ((bitOpWithConstantIsReducible(Opc, ValLo) ||
3096 bitOpWithConstantIsReducible(Opc, ValHi)) ||
3097 (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) {
3098 // If we need to materialize a 64-bit immediate, it will be split up later
3099 // anyway. Avoid creating the harder to understand 64-bit immediate
3100 // materialization.
3101 return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi);
3102 }
3103
3104 return SDValue();
3105}
3106
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003107SDValue SITargetLowering::performAndCombine(SDNode *N,
3108 DAGCombinerInfo &DCI) const {
3109 if (DCI.isBeforeLegalize())
3110 return SDValue();
3111
3112 SelectionDAG &DAG = DCI.DAG;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003113 EVT VT = N->getValueType(0);
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003114 SDValue LHS = N->getOperand(0);
3115 SDValue RHS = N->getOperand(1);
3116
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003117
3118 if (VT == MVT::i64) {
3119 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
3120 if (CRHS) {
3121 if (SDValue Split
3122 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS))
3123 return Split;
3124 }
3125 }
3126
3127 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
3128 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
3129 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) {
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003130 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
3131 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
3132
3133 SDValue X = LHS.getOperand(0);
3134 SDValue Y = RHS.getOperand(0);
3135 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
3136 return SDValue();
3137
3138 if (LCC == ISD::SETO) {
3139 if (X != LHS.getOperand(1))
3140 return SDValue();
3141
3142 if (RCC == ISD::SETUNE) {
3143 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
3144 if (!C1 || !C1->isInfinity() || C1->isNegative())
3145 return SDValue();
3146
3147 const uint32_t Mask = SIInstrFlags::N_NORMAL |
3148 SIInstrFlags::N_SUBNORMAL |
3149 SIInstrFlags::N_ZERO |
3150 SIInstrFlags::P_ZERO |
3151 SIInstrFlags::P_SUBNORMAL |
3152 SIInstrFlags::P_NORMAL;
3153
3154 static_assert(((~(SIInstrFlags::S_NAN |
3155 SIInstrFlags::Q_NAN |
3156 SIInstrFlags::N_INFINITY |
3157 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
3158 "mask not equal");
3159
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003160 SDLoc DL(N);
3161 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
3162 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003163 }
3164 }
3165 }
3166
3167 return SDValue();
3168}
3169
Matt Arsenaultf2290332015-01-06 23:00:39 +00003170SDValue SITargetLowering::performOrCombine(SDNode *N,
3171 DAGCombinerInfo &DCI) const {
3172 SelectionDAG &DAG = DCI.DAG;
3173 SDValue LHS = N->getOperand(0);
3174 SDValue RHS = N->getOperand(1);
3175
Matt Arsenault3b082382016-04-12 18:24:38 +00003176 EVT VT = N->getValueType(0);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003177 if (VT == MVT::i1) {
3178 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
3179 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
3180 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
3181 SDValue Src = LHS.getOperand(0);
3182 if (Src != RHS.getOperand(0))
3183 return SDValue();
Matt Arsenault3b082382016-04-12 18:24:38 +00003184
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003185 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
3186 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
3187 if (!CLHS || !CRHS)
3188 return SDValue();
Matt Arsenault3b082382016-04-12 18:24:38 +00003189
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003190 // Only 10 bits are used.
3191 static const uint32_t MaxMask = 0x3ff;
Matt Arsenault3b082382016-04-12 18:24:38 +00003192
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003193 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
3194 SDLoc DL(N);
3195 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
3196 Src, DAG.getConstant(NewMask, DL, MVT::i32));
3197 }
Matt Arsenault3b082382016-04-12 18:24:38 +00003198
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003199 return SDValue();
3200 }
3201
3202 if (VT != MVT::i64)
3203 return SDValue();
3204
3205 // TODO: This could be a generic combine with a predicate for extracting the
3206 // high half of an integer being free.
3207
3208 // (or i64:x, (zero_extend i32:y)) ->
3209 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
3210 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
3211 RHS.getOpcode() != ISD::ZERO_EXTEND)
3212 std::swap(LHS, RHS);
3213
3214 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
3215 SDValue ExtSrc = RHS.getOperand(0);
3216 EVT SrcVT = ExtSrc.getValueType();
3217 if (SrcVT == MVT::i32) {
3218 SDLoc SL(N);
3219 SDValue LowLHS, HiBits;
3220 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
3221 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
3222
3223 DCI.AddToWorklist(LowOr.getNode());
3224 DCI.AddToWorklist(HiBits.getNode());
3225
3226 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
3227 LowOr, HiBits);
3228 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
Matt Arsenault3b082382016-04-12 18:24:38 +00003229 }
3230 }
3231
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003232 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
3233 if (CRHS) {
3234 if (SDValue Split
3235 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS))
3236 return Split;
3237 }
Matt Arsenaultf2290332015-01-06 23:00:39 +00003238
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003239 return SDValue();
3240}
Matt Arsenaultf2290332015-01-06 23:00:39 +00003241
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003242SDValue SITargetLowering::performXorCombine(SDNode *N,
3243 DAGCombinerInfo &DCI) const {
3244 EVT VT = N->getValueType(0);
3245 if (VT != MVT::i64)
3246 return SDValue();
Matt Arsenaultf2290332015-01-06 23:00:39 +00003247
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003248 SDValue LHS = N->getOperand(0);
3249 SDValue RHS = N->getOperand(1);
3250
3251 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
3252 if (CRHS) {
3253 if (SDValue Split
3254 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS))
3255 return Split;
Matt Arsenaultf2290332015-01-06 23:00:39 +00003256 }
3257
3258 return SDValue();
3259}
3260
3261SDValue SITargetLowering::performClassCombine(SDNode *N,
3262 DAGCombinerInfo &DCI) const {
3263 SelectionDAG &DAG = DCI.DAG;
3264 SDValue Mask = N->getOperand(1);
3265
3266 // fp_class x, 0 -> false
3267 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
3268 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003269 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003270 }
3271
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003272 if (N->getOperand(0).isUndef())
3273 return DAG.getUNDEF(MVT::i1);
3274
Matt Arsenaultf2290332015-01-06 23:00:39 +00003275 return SDValue();
3276}
3277
Matt Arsenault9cd90712016-04-14 01:42:16 +00003278// Constant fold canonicalize.
3279SDValue SITargetLowering::performFCanonicalizeCombine(
3280 SDNode *N,
3281 DAGCombinerInfo &DCI) const {
3282 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
3283 if (!CFP)
3284 return SDValue();
3285
3286 SelectionDAG &DAG = DCI.DAG;
3287 const APFloat &C = CFP->getValueAPF();
3288
3289 // Flush denormals to 0 if not enabled.
3290 if (C.isDenormal()) {
3291 EVT VT = N->getValueType(0);
3292 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
3293 return DAG.getConstantFP(0.0, SDLoc(N), VT);
3294
3295 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
3296 return DAG.getConstantFP(0.0, SDLoc(N), VT);
3297 }
3298
3299 if (C.isNaN()) {
3300 EVT VT = N->getValueType(0);
3301 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
3302 if (C.isSignaling()) {
3303 // Quiet a signaling NaN.
3304 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3305 }
3306
3307 // Make sure it is the canonical NaN bitpattern.
3308 //
3309 // TODO: Can we use -1 as the canonical NaN value since it's an inline
3310 // immediate?
3311 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
3312 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3313 }
3314
3315 return SDValue(CFP, 0);
3316}
3317
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003318static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
3319 switch (Opc) {
3320 case ISD::FMAXNUM:
3321 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003322 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003323 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003324 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003325 return AMDGPUISD::UMAX3;
3326 case ISD::FMINNUM:
3327 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003328 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003329 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003330 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003331 return AMDGPUISD::UMIN3;
3332 default:
3333 llvm_unreachable("Not a min/max opcode");
3334 }
3335}
3336
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003337static SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3338 SDValue Op0, SDValue Op1, bool Signed) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003339 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
3340 if (!K1)
3341 return SDValue();
3342
3343 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
3344 if (!K0)
3345 return SDValue();
3346
Matt Arsenaultf639c322016-01-28 20:53:42 +00003347 if (Signed) {
3348 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
3349 return SDValue();
3350 } else {
3351 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
3352 return SDValue();
3353 }
3354
3355 EVT VT = K0->getValueType(0);
3356 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
3357 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
3358}
3359
3360static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
3361 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
3362 return true;
3363
3364 return DAG.isKnownNeverNaN(Op);
3365}
3366
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003367static SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3368 SDValue Op0, SDValue Op1) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003369 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
3370 if (!K1)
3371 return SDValue();
3372
3373 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
3374 if (!K0)
3375 return SDValue();
3376
3377 // Ordered >= (although NaN inputs should have folded away by now).
3378 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
3379 if (Cmp == APFloat::cmpGreaterThan)
3380 return SDValue();
3381
3382 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
3383 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
3384 // give the other result, which is different from med3 with a NaN input.
3385 SDValue Var = Op0.getOperand(0);
3386 if (!isKnownNeverSNan(DAG, Var))
3387 return SDValue();
3388
3389 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
3390 Var, SDValue(K0, 0), SDValue(K1, 0));
3391}
3392
3393SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
3394 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003395 SelectionDAG &DAG = DCI.DAG;
3396
3397 unsigned Opc = N->getOpcode();
3398 SDValue Op0 = N->getOperand(0);
3399 SDValue Op1 = N->getOperand(1);
3400
3401 // Only do this if the inner op has one use since this will just increases
3402 // register pressure for no benefit.
3403
Matt Arsenault5b39b342016-01-28 20:53:48 +00003404 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
3405 // max(max(a, b), c) -> max3(a, b, c)
3406 // min(min(a, b), c) -> min3(a, b, c)
3407 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
3408 SDLoc DL(N);
3409 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3410 DL,
3411 N->getValueType(0),
3412 Op0.getOperand(0),
3413 Op0.getOperand(1),
3414 Op1);
3415 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003416
Matt Arsenault5b39b342016-01-28 20:53:48 +00003417 // Try commuted.
3418 // max(a, max(b, c)) -> max3(a, b, c)
3419 // min(a, min(b, c)) -> min3(a, b, c)
3420 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
3421 SDLoc DL(N);
3422 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3423 DL,
3424 N->getValueType(0),
3425 Op0,
3426 Op1.getOperand(0),
3427 Op1.getOperand(1));
3428 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003429 }
3430
Matt Arsenaultf639c322016-01-28 20:53:42 +00003431 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
3432 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
3433 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
3434 return Med3;
3435 }
3436
3437 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
3438 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
3439 return Med3;
3440 }
3441
3442 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00003443 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
3444 (Opc == AMDGPUISD::FMIN_LEGACY &&
3445 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00003446 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
3447 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
3448 return Res;
3449 }
3450
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003451 return SDValue();
3452}
3453
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003454SDValue SITargetLowering::performSetCCCombine(SDNode *N,
3455 DAGCombinerInfo &DCI) const {
3456 SelectionDAG &DAG = DCI.DAG;
3457 SDLoc SL(N);
3458
3459 SDValue LHS = N->getOperand(0);
3460 SDValue RHS = N->getOperand(1);
3461 EVT VT = LHS.getValueType();
3462
3463 if (VT != MVT::f32 && VT != MVT::f64)
3464 return SDValue();
3465
3466 // Match isinf pattern
3467 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
3468 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
3469 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
3470 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
3471 if (!CRHS)
3472 return SDValue();
3473
3474 const APFloat &APF = CRHS->getValueAPF();
3475 if (APF.isInfinity() && !APF.isNegative()) {
3476 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003477 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
3478 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003479 }
3480 }
3481
3482 return SDValue();
3483}
3484
Tom Stellard75aadc22012-12-11 21:25:42 +00003485SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
3486 DAGCombinerInfo &DCI) const {
3487 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003488 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00003489
3490 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00003491 default:
3492 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003493 case ISD::SETCC:
3494 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00003495 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003496 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003497 case ISD::SMAX:
3498 case ISD::SMIN:
3499 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00003500 case ISD::UMIN:
3501 case AMDGPUISD::FMIN_LEGACY:
3502 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003503 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00003504 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003505 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00003506 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003507 break;
3508 }
Matt Arsenault364a6742014-06-11 17:50:44 +00003509
3510 case AMDGPUISD::CVT_F32_UBYTE0:
3511 case AMDGPUISD::CVT_F32_UBYTE1:
3512 case AMDGPUISD::CVT_F32_UBYTE2:
3513 case AMDGPUISD::CVT_F32_UBYTE3: {
3514 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
Matt Arsenault364a6742014-06-11 17:50:44 +00003515 SDValue Src = N->getOperand(0);
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003516
Matt Arsenault327bb5a2016-07-01 22:47:50 +00003517 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero.
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003518 if (Src.getOpcode() == ISD::SRL) {
3519 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
3520 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
3521 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x
3522
3523 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(1))) {
3524 unsigned SrcOffset = C->getZExtValue() + 8 * Offset;
3525 if (SrcOffset < 32 && SrcOffset % 8 == 0) {
3526 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, DL,
3527 MVT::f32, Src.getOperand(0));
3528 }
3529 }
3530 }
3531
Matt Arsenault364a6742014-06-11 17:50:44 +00003532 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
3533
3534 APInt KnownZero, KnownOne;
3535 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
3536 !DCI.isBeforeLegalizeOps());
3537 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3538 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
3539 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
3540 DCI.CommitTargetLoweringOpt(TLO);
3541 }
3542
3543 break;
3544 }
3545
3546 case ISD::UINT_TO_FP: {
3547 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003548 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003549 case ISD::FADD: {
3550 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3551 break;
3552
3553 EVT VT = N->getValueType(0);
3554 if (VT != MVT::f32)
3555 break;
3556
Matt Arsenault8d630032015-02-20 22:10:41 +00003557 // Only do this if we are not trying to support denormals. v_mad_f32 does
3558 // not support denormals ever.
3559 if (Subtarget->hasFP32Denormals())
3560 break;
3561
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003562 SDValue LHS = N->getOperand(0);
3563 SDValue RHS = N->getOperand(1);
3564
3565 // These should really be instruction patterns, but writing patterns with
3566 // source modiifiers is a pain.
3567
3568 // fadd (fadd (a, a), b) -> mad 2.0, a, b
3569 if (LHS.getOpcode() == ISD::FADD) {
3570 SDValue A = LHS.getOperand(0);
3571 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003572 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003573 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003574 }
3575 }
3576
3577 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
3578 if (RHS.getOpcode() == ISD::FADD) {
3579 SDValue A = RHS.getOperand(0);
3580 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003581 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003582 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003583 }
3584 }
3585
Matt Arsenault8d630032015-02-20 22:10:41 +00003586 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003587 }
Matt Arsenault8675db12014-08-29 16:01:14 +00003588 case ISD::FSUB: {
3589 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3590 break;
3591
3592 EVT VT = N->getValueType(0);
3593
3594 // Try to get the fneg to fold into the source modifier. This undoes generic
3595 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00003596 //
3597 // Only do this if we are not trying to support denormals. v_mad_f32 does
3598 // not support denormals ever.
3599 if (VT == MVT::f32 &&
3600 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00003601 SDValue LHS = N->getOperand(0);
3602 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003603 if (LHS.getOpcode() == ISD::FADD) {
3604 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
3605
3606 SDValue A = LHS.getOperand(0);
3607 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003608 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003609 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
3610
Matt Arsenault8d630032015-02-20 22:10:41 +00003611 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003612 }
3613 }
3614
3615 if (RHS.getOpcode() == ISD::FADD) {
3616 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
3617
3618 SDValue A = RHS.getOperand(0);
3619 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003620 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003621 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003622 }
3623 }
Matt Arsenault8d630032015-02-20 22:10:41 +00003624
3625 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00003626 }
3627
3628 break;
3629 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003630 case ISD::LOAD:
3631 case ISD::STORE:
3632 case ISD::ATOMIC_LOAD:
3633 case ISD::ATOMIC_STORE:
3634 case ISD::ATOMIC_CMP_SWAP:
3635 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
3636 case ISD::ATOMIC_SWAP:
3637 case ISD::ATOMIC_LOAD_ADD:
3638 case ISD::ATOMIC_LOAD_SUB:
3639 case ISD::ATOMIC_LOAD_AND:
3640 case ISD::ATOMIC_LOAD_OR:
3641 case ISD::ATOMIC_LOAD_XOR:
3642 case ISD::ATOMIC_LOAD_NAND:
3643 case ISD::ATOMIC_LOAD_MIN:
3644 case ISD::ATOMIC_LOAD_MAX:
3645 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00003646 case ISD::ATOMIC_LOAD_UMAX:
3647 case AMDGPUISD::ATOMIC_INC:
3648 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003649 if (DCI.isBeforeLegalize())
3650 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003651
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003652 MemSDNode *MemNode = cast<MemSDNode>(N);
3653 SDValue Ptr = MemNode->getBasePtr();
3654
3655 // TODO: We could also do this for multiplies.
3656 unsigned AS = MemNode->getAddressSpace();
3657 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
3658 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
3659 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00003660 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003661
3662 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
3663 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
3664 }
3665 }
3666 break;
3667 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003668 case ISD::AND:
3669 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003670 case ISD::OR:
3671 return performOrCombine(N, DCI);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003672 case ISD::XOR:
3673 return performXorCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003674 case AMDGPUISD::FP_CLASS:
3675 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00003676 case ISD::FCANONICALIZE:
3677 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003678 case AMDGPUISD::FRACT:
3679 case AMDGPUISD::RCP:
3680 case AMDGPUISD::RSQ:
Matt Arsenault32fc5272016-07-26 16:45:45 +00003681 case AMDGPUISD::RCP_LEGACY:
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003682 case AMDGPUISD::RSQ_LEGACY:
3683 case AMDGPUISD::RSQ_CLAMP:
3684 case AMDGPUISD::LDEXP: {
3685 SDValue Src = N->getOperand(0);
3686 if (Src.isUndef())
3687 return Src;
3688 break;
3689 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003690 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003691 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00003692}
Christian Konigd910b7d2013-02-26 17:52:16 +00003693
Christian Konig8e06e2a2013-04-10 08:39:08 +00003694/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00003695static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00003696 switch (Idx) {
3697 default: return 0;
3698 case AMDGPU::sub0: return 0;
3699 case AMDGPU::sub1: return 1;
3700 case AMDGPU::sub2: return 2;
3701 case AMDGPU::sub3: return 3;
3702 }
3703}
3704
3705/// \brief Adjust the writemask of MIMG instructions
3706void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
3707 SelectionDAG &DAG) const {
3708 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00003709 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003710 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
3711 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00003712 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003713
3714 // Try to figure out the used register components
3715 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
3716 I != E; ++I) {
3717
3718 // Abort if we can't understand the usage
3719 if (!I->isMachineOpcode() ||
3720 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
3721 return;
3722
Tom Stellard54774e52013-10-23 02:53:47 +00003723 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
3724 // Note that subregs are packed, i.e. Lane==0 is the first bit set
3725 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
3726 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00003727 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00003728
Tom Stellard54774e52013-10-23 02:53:47 +00003729 // Set which texture component corresponds to the lane.
3730 unsigned Comp;
3731 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
3732 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00003733 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00003734 Dmask &= ~(1 << Comp);
3735 }
3736
Christian Konig8e06e2a2013-04-10 08:39:08 +00003737 // Abort if we have more than one user per component
3738 if (Users[Lane])
3739 return;
3740
3741 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00003742 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003743 }
3744
Tom Stellard54774e52013-10-23 02:53:47 +00003745 // Abort if there's no change
3746 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00003747 return;
3748
3749 // Adjust the writemask in the node
3750 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003751 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003752 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003753 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00003754 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003755
Christian Konig8b1ed282013-04-10 08:39:16 +00003756 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00003757 // (if NewDmask has only one bit set...)
3758 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003759 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
3760 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00003761 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003762 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00003763 SDValue(Node, 0), RC);
3764 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
3765 return;
3766 }
3767
Christian Konig8e06e2a2013-04-10 08:39:08 +00003768 // Update the users of the node with the new indices
3769 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
3770
3771 SDNode *User = Users[i];
3772 if (!User)
3773 continue;
3774
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003775 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003776 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
3777
3778 switch (Idx) {
3779 default: break;
3780 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
3781 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
3782 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
3783 }
3784 }
3785}
3786
Tom Stellardc98ee202015-07-16 19:40:07 +00003787static bool isFrameIndexOp(SDValue Op) {
3788 if (Op.getOpcode() == ISD::AssertZext)
3789 Op = Op.getOperand(0);
3790
3791 return isa<FrameIndexSDNode>(Op);
3792}
3793
Tom Stellard3457a842014-10-09 19:06:00 +00003794/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
3795/// with frame index operands.
3796/// LLVM assumes that inputs are to these instructions are registers.
3797void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
3798 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003799
3800 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00003801 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00003802 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00003803 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003804 continue;
3805 }
3806
Tom Stellard3457a842014-10-09 19:06:00 +00003807 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003808 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00003809 Node->getOperand(i).getValueType(),
3810 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003811 }
3812
Tom Stellard3457a842014-10-09 19:06:00 +00003813 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003814}
3815
Matt Arsenault08d84942014-06-03 23:06:13 +00003816/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00003817SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
3818 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003819 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003820 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00003821
Nicolai Haehnlec06bfa12016-07-11 21:59:43 +00003822 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() &&
3823 !TII->isGather4(Opcode))
Christian Konig8e06e2a2013-04-10 08:39:08 +00003824 adjustWritemask(Node, DAG);
3825
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003826 if (Opcode == AMDGPU::INSERT_SUBREG ||
3827 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003828 legalizeTargetIndependentNode(Node, DAG);
3829 return Node;
3830 }
Tom Stellard654d6692015-01-08 15:08:17 +00003831 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003832}
Christian Konig8b1ed282013-04-10 08:39:16 +00003833
3834/// \brief Assign the register class depending on the number of
3835/// bits set in the writemask
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003836void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
Christian Konig8b1ed282013-04-10 08:39:16 +00003837 SDNode *Node) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003838 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003839
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003840 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003841
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003842 if (TII->isVOP3(MI.getOpcode())) {
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003843 // Make sure constant bus requirements are respected.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003844 TII->legalizeOperandsVOP3(MRI, MI);
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003845 return;
3846 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00003847
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003848 if (TII->isMIMG(MI)) {
3849 unsigned VReg = MI.getOperand(0).getReg();
3850 unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4;
3851 unsigned Writemask = MI.getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003852 unsigned BitsSet = 0;
3853 for (unsigned i = 0; i < 4; ++i)
3854 BitsSet += Writemask & (1 << i) ? 1 : 0;
3855
3856 const TargetRegisterClass *RC;
3857 switch (BitsSet) {
3858 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00003859 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003860 case 2: RC = &AMDGPU::VReg_64RegClass; break;
3861 case 3: RC = &AMDGPU::VReg_96RegClass; break;
3862 }
3863
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003864 unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet);
3865 MI.setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003866 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00003867 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00003868 }
3869
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003870 // Replace unused atomics with the no return version.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003871 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003872 if (NoRetAtomicOp != -1) {
3873 if (!Node->hasAnyUseOfValue(0)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003874 MI.setDesc(TII->get(NoRetAtomicOp));
3875 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003876 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003877 }
3878
Tom Stellard354a43c2016-04-01 18:27:37 +00003879 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
3880 // instruction, because the return type of these instructions is a vec2 of
3881 // the memory type, so it can be tied to the input operand.
3882 // This means these instructions always have a use, so we need to add a
3883 // special case to check if the atomic has only one extract_subreg use,
3884 // which itself has no uses.
3885 if ((Node->hasNUsesOfValue(1, 0) &&
Nicolai Haehnle750082d2016-04-15 14:42:36 +00003886 Node->use_begin()->isMachineOpcode() &&
Tom Stellard354a43c2016-04-01 18:27:37 +00003887 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
3888 !Node->use_begin()->hasAnyUseOfValue(0))) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003889 unsigned Def = MI.getOperand(0).getReg();
Tom Stellard354a43c2016-04-01 18:27:37 +00003890
3891 // Change this into a noret atomic.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003892 MI.setDesc(TII->get(NoRetAtomicOp));
3893 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003894
3895 // If we only remove the def operand from the atomic instruction, the
3896 // extract_subreg will be left with a use of a vreg without a def.
3897 // So we need to insert an implicit_def to avoid machine verifier
3898 // errors.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003899 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
Tom Stellard354a43c2016-04-01 18:27:37 +00003900 TII->get(AMDGPU::IMPLICIT_DEF), Def);
3901 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003902 return;
3903 }
Christian Konig8b1ed282013-04-10 08:39:16 +00003904}
Tom Stellard0518ff82013-06-03 17:39:58 +00003905
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003906static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL,
3907 uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003908 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00003909 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
3910}
3911
3912MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003913 const SDLoc &DL,
Matt Arsenault485defe2014-11-05 19:01:17 +00003914 SDValue Ptr) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003915 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault485defe2014-11-05 19:01:17 +00003916
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003917 // Build the half of the subregister with the constants before building the
3918 // full 128-bit register. If we are building multiple resource descriptors,
3919 // this will allow CSEing of the 2-component register.
3920 const SDValue Ops0[] = {
3921 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
3922 buildSMovImm32(DAG, DL, 0),
3923 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
3924 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
3925 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
3926 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003927
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003928 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
3929 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00003930
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003931 // Combine the constants and the pointer.
3932 const SDValue Ops1[] = {
3933 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
3934 Ptr,
3935 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
3936 SubRegHi,
3937 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
3938 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003939
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003940 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00003941}
3942
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003943/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003944/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
3945/// of the resource descriptor) to create an offset, which is added to
3946/// the resource pointer.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003947MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL,
3948 SDValue Ptr, uint32_t RsrcDword1,
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003949 uint64_t RsrcDword2And3) const {
3950 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
3951 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
3952 if (RsrcDword1) {
3953 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003954 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
3955 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003956 }
3957
3958 SDValue DataLo = buildSMovImm32(DAG, DL,
3959 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
3960 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
3961
3962 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003963 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003964 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003965 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003966 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003967 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003968 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003969 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003970 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003971 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003972 };
3973
3974 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
3975}
3976
Tom Stellard94593ee2013-06-03 17:40:18 +00003977SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3978 const TargetRegisterClass *RC,
3979 unsigned Reg, EVT VT) const {
3980 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
3981
3982 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
3983 cast<RegisterSDNode>(VReg)->getReg(), VT);
3984}
Tom Stellardd7e6f132015-04-08 01:09:26 +00003985
3986//===----------------------------------------------------------------------===//
3987// SI Inline Assembly Support
3988//===----------------------------------------------------------------------===//
3989
3990std::pair<unsigned, const TargetRegisterClass *>
3991SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003992 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00003993 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003994
3995 if (Constraint.size() == 1) {
3996 switch (Constraint[0]) {
3997 case 's':
3998 case 'r':
3999 switch (VT.getSizeInBits()) {
4000 default:
4001 return std::make_pair(0U, nullptr);
4002 case 32:
Matt Arsenaulta609e2d2016-08-30 20:50:08 +00004003 return std::make_pair(0U, &AMDGPU::SReg_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004004 case 64:
4005 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
4006 case 128:
4007 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
4008 case 256:
4009 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
4010 }
4011
4012 case 'v':
4013 switch (VT.getSizeInBits()) {
4014 default:
4015 return std::make_pair(0U, nullptr);
4016 case 32:
4017 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
4018 case 64:
4019 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
4020 case 96:
4021 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
4022 case 128:
4023 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
4024 case 256:
4025 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
4026 case 512:
4027 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
4028 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00004029 }
4030 }
4031
4032 if (Constraint.size() > 1) {
4033 const TargetRegisterClass *RC = nullptr;
4034 if (Constraint[1] == 'v') {
4035 RC = &AMDGPU::VGPR_32RegClass;
4036 } else if (Constraint[1] == 's') {
4037 RC = &AMDGPU::SGPR_32RegClass;
4038 }
4039
4040 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00004041 uint32_t Idx;
4042 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
4043 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00004044 return std::make_pair(RC->getRegister(Idx), RC);
4045 }
4046 }
4047 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
4048}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004049
4050SITargetLowering::ConstraintType
4051SITargetLowering::getConstraintType(StringRef Constraint) const {
4052 if (Constraint.size() == 1) {
4053 switch (Constraint[0]) {
4054 default: break;
4055 case 's':
4056 case 'v':
4057 return C_RegisterClass;
4058 }
4059 }
4060 return TargetLowering::getConstraintType(Constraint);
4061}