blob: acaec9d9a2862f871bd5d09ab31c95079b6a140b [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
Wei Dinged0f97f2016-06-09 19:17:15 +000040// -amdgpu-fast-fdiv - Command line option to enable faster 2.5 ulp fdiv.
41static cl::opt<bool> EnableAMDGPUFastFDIV(
Matt Arsenault37fefd62016-06-10 02:18:02 +000042 "amdgpu-fast-fdiv",
43 cl::desc("Enable faster 2.5 ulp fdiv"),
Wei Dinged0f97f2016-06-09 19:17:15 +000044 cl::init(false));
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);
98 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
99 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
100
101 setOperationAction(ISD::SELECT, MVT::i1, Promote);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000102 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +0000103 setOperationAction(ISD::SELECT, MVT::f64, Promote);
104 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000105
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000106 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
107 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
108 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
109 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Matt Arsenault71e66762016-05-21 02:27:49 +0000110 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000111
Tom Stellardd1efda82016-01-20 21:48:24 +0000112 setOperationAction(ISD::SETCC, MVT::i1, Promote);
Tom Stellard83747202013-07-18 21:43:53 +0000113 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
114 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
115
Matt Arsenault71e66762016-05-21 02:27:49 +0000116 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
117 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Matt Arsenaulte306a322014-10-21 16:25:08 +0000118
Matt Arsenault4e466652014-04-16 01:41:30 +0000119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000125 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
126
Tom Stellard9fa17912013-08-14 23:24:45 +0000127 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000128 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000129 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
130
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000131 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000132 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000133 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
134 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
135 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
136 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000137
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000138 // We only support LOAD/STORE and vector manipulation ops for vectors
139 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000140 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000141 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000142 switch (Op) {
Tom Stellard967bf582014-02-13 23:34:15 +0000143 case ISD::LOAD:
144 case ISD::STORE:
145 case ISD::BUILD_VECTOR:
146 case ISD::BITCAST:
147 case ISD::EXTRACT_VECTOR_ELT:
148 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000149 case ISD::INSERT_SUBVECTOR:
150 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000151 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000152 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000153 case ISD::CONCAT_VECTORS:
154 setOperationAction(Op, VT, Custom);
155 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000156 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000157 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000158 break;
159 }
160 }
161 }
162
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000163 // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that
164 // is expanded to avoid having two separate loops in case the index is a VGPR.
165
Matt Arsenault61001bb2015-11-25 19:58:34 +0000166 // Most operations are naturally 32-bit vector operations. We only support
167 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
168 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
169 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
170 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
171
172 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
173 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
174
175 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
176 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
177
178 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
179 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
180 }
181
Matt Arsenault71e66762016-05-21 02:27:49 +0000182 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
183 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
184 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
185 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000186
Tom Stellard354a43c2016-04-01 18:27:37 +0000187 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling,
188 // and output demarshalling
189 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
190 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
191
192 // We can't return success/failure, only the old value,
193 // let LLVM add the comparison
194 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand);
195 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand);
196
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000197 if (getSubtarget()->hasFlatAddressSpace()) {
Matt Arsenault99c14522016-04-25 19:27:24 +0000198 setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom);
199 setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom);
200 }
201
Matt Arsenault71e66762016-05-21 02:27:49 +0000202 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
203 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
204
205 // On SI this is s_memtime and s_memrealtime on VI.
206 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
Matt Arsenault0bb294b2016-06-17 22:27:03 +0000207 setOperationAction(ISD::TRAP, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000208
209 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
210 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
211
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000212 if (Subtarget->getGeneration() >= SISubtarget::SEA_ISLANDS) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000213 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
214 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
215 setOperationAction(ISD::FRINT, MVT::f64, Legal);
216 }
217
218 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
219
220 setOperationAction(ISD::FSIN, MVT::f32, Custom);
221 setOperationAction(ISD::FCOS, MVT::f32, Custom);
222 setOperationAction(ISD::FDIV, MVT::f32, Custom);
223 setOperationAction(ISD::FDIV, MVT::f64, Custom);
224
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000225 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000226 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000227 setTargetDAGCombine(ISD::FMINNUM);
228 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000229 setTargetDAGCombine(ISD::SMIN);
230 setTargetDAGCombine(ISD::SMAX);
231 setTargetDAGCombine(ISD::UMIN);
232 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000233 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000234 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000235 setTargetDAGCombine(ISD::OR);
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
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000462 if (Subtarget->hasUnalignedBufferAccess()) {
463 // If we have an uniform constant load, it still requires using a slow
464 // buffer instruction if unaligned.
465 if (IsFast) {
466 *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS) ?
467 (Align % 4 == 0) : true;
468 }
469
470 return true;
471 }
472
Tom Stellard33e64c62015-02-04 20:49:52 +0000473 // Smaller than dword value must be aligned.
Tom Stellard33e64c62015-02-04 20:49:52 +0000474 if (VT.bitsLT(MVT::i32))
475 return false;
476
Matt Arsenault1018c892014-04-24 17:08:26 +0000477 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
478 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000479 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000480 if (IsFast)
481 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000482
483 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000484}
485
Matt Arsenault46645fa2014-07-28 17:49:26 +0000486EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
487 unsigned SrcAlign, bool IsMemset,
488 bool ZeroMemset,
489 bool MemcpyStrSrc,
490 MachineFunction &MF) const {
491 // FIXME: Should account for address space here.
492
493 // The default fallback uses the private pointer size as a guess for a type to
494 // use. Make sure we switch these to 64-bit accesses.
495
496 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
497 return MVT::v4i32;
498
499 if (Size >= 8 && DstAlign >= 4)
500 return MVT::v2i32;
501
502 // Use the default.
503 return MVT::Other;
504}
505
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000506static bool isFlatGlobalAddrSpace(unsigned AS) {
507 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
508 AS == AMDGPUAS::FLAT_ADDRESS ||
509 AS == AMDGPUAS::CONSTANT_ADDRESS;
510}
511
512bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
513 unsigned DestAS) const {
Matt Arsenault37fefd62016-06-10 02:18:02 +0000514 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000515}
516
Tom Stellarda6f24c62015-12-15 20:55:55 +0000517bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
518 const MemSDNode *MemNode = cast<MemSDNode>(N);
519 const Value *Ptr = MemNode->getMemOperand()->getValue();
520
521 // UndefValue means this is a load of a kernel input. These are uniform.
Tom Stellard418beb72016-07-13 14:23:33 +0000522 // Sometimes LDS instructions have constant pointers.
523 // If Ptr is null, then that means this mem operand contains a
524 // PseudoSourceValue like GOT.
525 if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) ||
526 isa<Constant>(Ptr) || isa<GlobalValue>(Ptr))
Tom Stellarda6f24c62015-12-15 20:55:55 +0000527 return true;
528
Tom Stellard418beb72016-07-13 14:23:33 +0000529 const Instruction *I = dyn_cast<Instruction>(Ptr);
Tom Stellarda6f24c62015-12-15 20:55:55 +0000530 return I && I->getMetadata("amdgpu.uniform");
531}
532
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000533TargetLoweringBase::LegalizeTypeAction
534SITargetLowering::getPreferredVectorAction(EVT VT) const {
535 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
536 return TypeSplitVector;
537
538 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000539}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000540
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000541bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
542 Type *Ty) const {
Matt Arsenault749035b2016-07-30 01:40:36 +0000543 // FIXME: Could be smarter if called for vector constants.
544 return true;
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000545}
546
Tom Stellard2e045bb2016-01-20 00:13:22 +0000547bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
548
549 // SimplifySetCC uses this function to determine whether or not it should
550 // create setcc with i1 operands. We don't have instructions for i1 setcc.
551 if (VT == MVT::i1 && Op == ISD::SETCC)
552 return false;
553
554 return TargetLowering::isTypeDesirableForOp(Op, VT);
555}
556
Jan Veselyfea814d2016-06-21 20:46:20 +0000557SDValue SITargetLowering::LowerParameterPtr(SelectionDAG &DAG,
558 const SDLoc &SL, SDValue Chain,
559 unsigned Offset) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000560 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000561 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000562 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaultac234b62015-11-30 21:15:57 +0000563 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000564
Matt Arsenault86033ca2014-07-28 17:31:39 +0000565 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000566 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000567 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
568 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
Jan Veselyfea814d2016-06-21 20:46:20 +0000569 return DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
570 DAG.getConstant(Offset, SL, PtrVT));
571}
572SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
573 const SDLoc &SL, SDValue Chain,
574 unsigned Offset, bool Signed) const {
575 const DataLayout &DL = DAG.getDataLayout();
576 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
577 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
578 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Mehdi Amini44ede332015-07-09 02:09:04 +0000579 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000580 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
581
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000582 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000583
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000584 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000585 if (MemVT.isFloatingPoint())
586 ExtTy = ISD::EXTLOAD;
587
Jan Veselyfea814d2016-06-21 20:46:20 +0000588 SDValue Ptr = LowerParameterPtr(DAG, SL, Chain, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +0000589 return DAG.getLoad(ISD::UNINDEXED, ExtTy, VT, SL, Chain, Ptr, PtrOffset,
590 PtrInfo, MemVT, Align, MachineMemOperand::MONonTemporal |
591 MachineMemOperand::MOInvariant);
Tom Stellard94593ee2013-06-03 17:40:18 +0000592}
593
Christian Konig2c8f6d52013-03-07 09:03:52 +0000594SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000595 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000596 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
597 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000598 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000599
600 MachineFunction &MF = DAG.getMachineFunction();
601 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000602 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000603 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000604
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000605 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) {
Matt Arsenaultd48da142015-11-02 23:23:02 +0000606 const Function *Fn = MF.getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000607 DiagnosticInfoUnsupported NoGraphicsHSA(
608 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
Matt Arsenaultd48da142015-11-02 23:23:02 +0000609 DAG.getContext()->diagnose(NoGraphicsHSA);
Diana Picus81bc3172016-05-26 15:24:55 +0000610 return DAG.getEntryNode();
Matt Arsenaultd48da142015-11-02 23:23:02 +0000611 }
612
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000613 // Create stack objects that are used for emitting debugger prologue if
614 // "amdgpu-debugger-emit-prologue" attribute was specified.
615 if (ST.debuggerEmitPrologue())
616 createDebuggerPrologueStackObjects(MF);
617
Christian Konig2c8f6d52013-03-07 09:03:52 +0000618 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000619 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000620
621 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000622 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000623
624 // First check if it's a PS input addr
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000625 if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000626 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000627
Marek Olsakfccabaf2016-01-13 11:45:36 +0000628 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000629 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000630 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000631 ++PSInputNum;
632 continue;
633 }
634
Marek Olsakfccabaf2016-01-13 11:45:36 +0000635 Info->markPSInputAllocated(PSInputNum);
636 if (Arg.Used)
637 Info->PSInputEna |= 1 << PSInputNum;
638
639 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000640 }
641
Matt Arsenault539ca882016-05-05 20:27:02 +0000642 if (AMDGPU::isShader(CallConv)) {
643 // Second split vertices into their elements
644 if (Arg.VT.isVector()) {
645 ISD::InputArg NewArg = Arg;
646 NewArg.Flags.setSplit();
647 NewArg.VT = Arg.VT.getVectorElementType();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000648
Matt Arsenault539ca882016-05-05 20:27:02 +0000649 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
650 // three or five element vertex only needs three or five registers,
651 // NOT four or eight.
652 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
653 unsigned NumElements = ParamType->getVectorNumElements();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000654
Matt Arsenault539ca882016-05-05 20:27:02 +0000655 for (unsigned j = 0; j != NumElements; ++j) {
656 Splits.push_back(NewArg);
657 NewArg.PartOffset += NewArg.VT.getStoreSize();
658 }
659 } else {
660 Splits.push_back(Arg);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000661 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000662 }
663 }
664
665 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000666 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
667 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000668
Christian Konig99ee0f42013-03-07 09:04:14 +0000669 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000670 //
671 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
672 // PSInputAddr, the user wants to enable some bits after the compilation
673 // based on run-time states. Since we can't know what the final PSInputEna
674 // will look like, so we shouldn't do anything here and the user should take
675 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000676 //
677 // Otherwise, the following restrictions apply:
678 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
679 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
680 // enabled too.
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000681 if (CallConv == CallingConv::AMDGPU_PS &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000682 ((Info->getPSInputAddr() & 0x7F) == 0 ||
NAKAMURA Takumife1202c2016-06-20 00:37:41 +0000683 ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000684 CCInfo.AllocateReg(AMDGPU::VGPR0);
685 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000686 Info->markPSInputAllocated(0);
687 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000688 }
689
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000690 if (!AMDGPU::isShader(CallConv)) {
Tom Stellardaf775432013-10-23 00:44:32 +0000691 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
692 Splits);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000693
694 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
695 } else {
696 assert(!Info->hasPrivateSegmentBuffer() && !Info->hasDispatchPtr() &&
697 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() &&
698 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
699 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() &&
700 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() &&
701 !Info->hasWorkItemIDZ());
Tom Stellardaf775432013-10-23 00:44:32 +0000702 }
703
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000704 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
705 if (Info->hasPrivateSegmentBuffer()) {
706 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
707 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
708 CCInfo.AllocateReg(PrivateSegmentBufferReg);
709 }
710
711 if (Info->hasDispatchPtr()) {
712 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
713 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
714 CCInfo.AllocateReg(DispatchPtrReg);
715 }
716
Matt Arsenault48ab5262016-04-25 19:27:18 +0000717 if (Info->hasQueuePtr()) {
718 unsigned QueuePtrReg = Info->addQueuePtr(*TRI);
719 MF.addLiveIn(QueuePtrReg, &AMDGPU::SReg_64RegClass);
720 CCInfo.AllocateReg(QueuePtrReg);
721 }
722
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000723 if (Info->hasKernargSegmentPtr()) {
724 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
725 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
726 CCInfo.AllocateReg(InputPtrReg);
727 }
728
Matt Arsenault8d718dc2016-07-22 17:01:30 +0000729 if (Info->hasDispatchID()) {
730 unsigned DispatchIDReg = Info->addDispatchID(*TRI);
731 MF.addLiveIn(DispatchIDReg, &AMDGPU::SReg_64RegClass);
732 CCInfo.AllocateReg(DispatchIDReg);
733 }
734
Matt Arsenault296b8492016-02-12 06:31:30 +0000735 if (Info->hasFlatScratchInit()) {
736 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
737 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SReg_64RegClass);
738 CCInfo.AllocateReg(FlatScratchInitReg);
739 }
740
Christian Konig2c8f6d52013-03-07 09:03:52 +0000741 AnalyzeFormalArguments(CCInfo, Splits);
742
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000743 SmallVector<SDValue, 16> Chains;
744
Christian Konig2c8f6d52013-03-07 09:03:52 +0000745 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
746
Christian Konigb7be72d2013-05-17 09:46:48 +0000747 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000748 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000749 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000750 continue;
751 }
752
Christian Konig2c8f6d52013-03-07 09:03:52 +0000753 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000754 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000755
756 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000757 VT = Ins[i].VT;
758 EVT MemVT = Splits[i].VT;
Tom Stellardb5798b02015-06-26 21:15:03 +0000759 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
760 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000761 // The first 36 bytes of the input buffer contains information about
762 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000763 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000764 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000765 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000766
Craig Toppere3dcce92015-08-01 22:20:21 +0000767 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000768 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000769 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Tom Stellardca7ecf32014-08-22 18:49:31 +0000770 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
771 // On SI local pointers are just offsets into LDS, so they are always
772 // less than 16-bits. On CI and newer they could potentially be
773 // real pointers, so we can't guarantee their size.
774 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
775 DAG.getValueType(MVT::i16));
776 }
777
Tom Stellarded882c22013-06-03 17:40:11 +0000778 InVals.push_back(Arg);
Matt Arsenault52ef4012016-07-26 16:45:58 +0000779 Info->setABIArgOffset(Offset + MemVT.getStoreSize());
Tom Stellarded882c22013-06-03 17:40:11 +0000780 continue;
781 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000782 assert(VA.isRegLoc() && "Parameter must be in a register!");
783
784 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000785
786 if (VT == MVT::i64) {
787 // For now assume it is a pointer
788 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
789 &AMDGPU::SReg_64RegClass);
790 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000791 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
792 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000793 continue;
794 }
795
796 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
797
798 Reg = MF.addLiveIn(Reg, RC);
799 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
800
Christian Konig2c8f6d52013-03-07 09:03:52 +0000801 if (Arg.VT.isVector()) {
802
803 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000804 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000805 unsigned NumElements = ParamType->getVectorNumElements();
806
807 SmallVector<SDValue, 4> Regs;
808 Regs.push_back(Val);
809 for (unsigned j = 1; j != NumElements; ++j) {
810 Reg = ArgLocs[ArgIdx++].getLocReg();
811 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000812
813 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
814 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000815 }
816
817 // Fill up the missing vector elements
818 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000819 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000820
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000821 InVals.push_back(DAG.getBuildVector(Arg.VT, DL, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000822 continue;
823 }
824
825 InVals.push_back(Val);
826 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000827
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000828 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
829 // these from the dispatch pointer.
830
831 // Start adding system SGPRs.
832 if (Info->hasWorkGroupIDX()) {
833 unsigned Reg = Info->addWorkGroupIDX();
834 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
835 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000836 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000837
838 if (Info->hasWorkGroupIDY()) {
839 unsigned Reg = Info->addWorkGroupIDY();
840 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
841 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000842 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000843
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000844 if (Info->hasWorkGroupIDZ()) {
845 unsigned Reg = Info->addWorkGroupIDZ();
846 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
847 CCInfo.AllocateReg(Reg);
848 }
849
850 if (Info->hasWorkGroupInfo()) {
851 unsigned Reg = Info->addWorkGroupInfo();
852 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
853 CCInfo.AllocateReg(Reg);
854 }
855
856 if (Info->hasPrivateSegmentWaveByteOffset()) {
857 // Scratch wave offset passed in system SGPR.
Tom Stellardf110f8f2016-04-14 16:27:03 +0000858 unsigned PrivateSegmentWaveByteOffsetReg;
859
860 if (AMDGPU::isShader(CallConv)) {
861 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
862 Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
863 } else
864 PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset();
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000865
866 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
867 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
868 }
869
870 // Now that we've figured out where the scratch register inputs are, see if
871 // should reserve the arguments and use them directly.
Matthias Braun941a7052016-07-28 18:40:00 +0000872 bool HasStackObjects = MF.getFrameInfo().hasStackObjects();
Matt Arsenault296b8492016-02-12 06:31:30 +0000873 // Record that we know we have non-spill stack objects so we don't need to
874 // check all stack objects later.
875 if (HasStackObjects)
876 Info->setHasNonSpillStackObjects(true);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000877
878 if (ST.isAmdHsaOS()) {
879 // TODO: Assume we will spill without optimizations.
880 if (HasStackObjects) {
881 // If we have stack objects, we unquestionably need the private buffer
882 // resource. For the HSA ABI, this will be the first 4 user SGPR
883 // inputs. We can reserve those and use them directly.
884
885 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
886 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
887 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
888
889 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
890 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
891 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
892 } else {
893 unsigned ReservedBufferReg
894 = TRI->reservedPrivateSegmentBufferReg(MF);
895 unsigned ReservedOffsetReg
896 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
897
898 // We tentatively reserve the last registers (skipping the last two
899 // which may contain VCC). After register allocation, we'll replace
900 // these with the ones immediately after those which were really
901 // allocated. In the prologue copies will be inserted from the argument
902 // to these reserved registers.
903 Info->setScratchRSrcReg(ReservedBufferReg);
904 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
905 }
906 } else {
907 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
908
909 // Without HSA, relocations are used for the scratch pointer and the
910 // buffer resource setup is always inserted in the prologue. Scratch wave
911 // offset is still in an input SGPR.
912 Info->setScratchRSrcReg(ReservedBufferReg);
913
914 if (HasStackObjects) {
915 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
916 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
917 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
918 } else {
919 unsigned ReservedOffsetReg
920 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
921 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
922 }
923 }
924
925 if (Info->hasWorkItemIDX()) {
926 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
927 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
928 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000929 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000930
931 if (Info->hasWorkItemIDY()) {
932 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
933 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
934 CCInfo.AllocateReg(Reg);
935 }
936
937 if (Info->hasWorkItemIDZ()) {
938 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
939 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
940 CCInfo.AllocateReg(Reg);
941 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000942
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000943 if (Chains.empty())
944 return Chain;
945
946 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000947}
948
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000949SDValue
950SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
951 bool isVarArg,
952 const SmallVectorImpl<ISD::OutputArg> &Outs,
953 const SmallVectorImpl<SDValue> &OutVals,
954 const SDLoc &DL, SelectionDAG &DAG) const {
Marek Olsak8a0f3352016-01-13 17:23:04 +0000955 MachineFunction &MF = DAG.getMachineFunction();
956 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
957
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000958 if (!AMDGPU::isShader(CallConv))
Marek Olsak8a0f3352016-01-13 17:23:04 +0000959 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
960 OutVals, DL, DAG);
961
Marek Olsak8e9cc632016-01-13 17:23:09 +0000962 Info->setIfReturnsVoid(Outs.size() == 0);
963
Marek Olsak8a0f3352016-01-13 17:23:04 +0000964 SmallVector<ISD::OutputArg, 48> Splits;
965 SmallVector<SDValue, 48> SplitVals;
966
967 // Split vectors into their elements.
968 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
969 const ISD::OutputArg &Out = Outs[i];
970
971 if (Out.VT.isVector()) {
972 MVT VT = Out.VT.getVectorElementType();
973 ISD::OutputArg NewOut = Out;
974 NewOut.Flags.setSplit();
975 NewOut.VT = VT;
976
977 // We want the original number of vector elements here, e.g.
978 // three or five, not four or eight.
979 unsigned NumElements = Out.ArgVT.getVectorNumElements();
980
981 for (unsigned j = 0; j != NumElements; ++j) {
982 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
983 DAG.getConstant(j, DL, MVT::i32));
984 SplitVals.push_back(Elem);
985 Splits.push_back(NewOut);
986 NewOut.PartOffset += NewOut.VT.getStoreSize();
987 }
988 } else {
989 SplitVals.push_back(OutVals[i]);
990 Splits.push_back(Out);
991 }
992 }
993
994 // CCValAssign - represent the assignment of the return value to a location.
995 SmallVector<CCValAssign, 48> RVLocs;
996
997 // CCState - Info about the registers and stack slots.
998 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
999 *DAG.getContext());
1000
1001 // Analyze outgoing return values.
1002 AnalyzeReturn(CCInfo, Splits);
1003
1004 SDValue Flag;
1005 SmallVector<SDValue, 48> RetOps;
1006 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1007
1008 // Copy the result values into the output registers.
1009 for (unsigned i = 0, realRVLocIdx = 0;
1010 i != RVLocs.size();
1011 ++i, ++realRVLocIdx) {
1012 CCValAssign &VA = RVLocs[i];
1013 assert(VA.isRegLoc() && "Can only return in registers!");
1014
1015 SDValue Arg = SplitVals[realRVLocIdx];
1016
1017 // Copied from other backends.
1018 switch (VA.getLocInfo()) {
1019 default: llvm_unreachable("Unknown loc info!");
1020 case CCValAssign::Full:
1021 break;
1022 case CCValAssign::BCvt:
1023 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1024 break;
1025 }
1026
1027 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
1028 Flag = Chain.getValue(1);
1029 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1030 }
1031
1032 // Update chain and glue.
1033 RetOps[0] = Chain;
1034 if (Flag.getNode())
1035 RetOps.push_back(Flag);
1036
Matt Arsenault9babdf42016-06-22 20:15:28 +00001037 unsigned Opc = Info->returnsVoid() ? AMDGPUISD::ENDPGM : AMDGPUISD::RETURN;
1038 return DAG.getNode(Opc, DL, MVT::Other, RetOps);
Marek Olsak8a0f3352016-01-13 17:23:04 +00001039}
1040
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001041unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1042 SelectionDAG &DAG) const {
1043 unsigned Reg = StringSwitch<unsigned>(RegName)
1044 .Case("m0", AMDGPU::M0)
1045 .Case("exec", AMDGPU::EXEC)
1046 .Case("exec_lo", AMDGPU::EXEC_LO)
1047 .Case("exec_hi", AMDGPU::EXEC_HI)
1048 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1049 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1050 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1051 .Default(AMDGPU::NoRegister);
1052
1053 if (Reg == AMDGPU::NoRegister) {
1054 report_fatal_error(Twine("invalid register name \""
1055 + StringRef(RegName) + "\"."));
1056
1057 }
1058
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001059 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001060 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1061 report_fatal_error(Twine("invalid register \""
1062 + StringRef(RegName) + "\" for subtarget."));
1063 }
1064
1065 switch (Reg) {
1066 case AMDGPU::M0:
1067 case AMDGPU::EXEC_LO:
1068 case AMDGPU::EXEC_HI:
1069 case AMDGPU::FLAT_SCR_LO:
1070 case AMDGPU::FLAT_SCR_HI:
1071 if (VT.getSizeInBits() == 32)
1072 return Reg;
1073 break;
1074 case AMDGPU::EXEC:
1075 case AMDGPU::FLAT_SCR:
1076 if (VT.getSizeInBits() == 64)
1077 return Reg;
1078 break;
1079 default:
1080 llvm_unreachable("missing register type checking");
1081 }
1082
1083 report_fatal_error(Twine("invalid type for register \""
1084 + StringRef(RegName) + "\"."));
1085}
1086
Matt Arsenault786724a2016-07-12 21:41:32 +00001087// If kill is not the last instruction, split the block so kill is always a
1088// proper terminator.
1089MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI,
1090 MachineBasicBlock *BB) const {
1091 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1092
1093 MachineBasicBlock::iterator SplitPoint(&MI);
1094 ++SplitPoint;
1095
1096 if (SplitPoint == BB->end()) {
1097 // Don't bother with a new block.
1098 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1099 return BB;
1100 }
1101
1102 MachineFunction *MF = BB->getParent();
1103 MachineBasicBlock *SplitBB
1104 = MF->CreateMachineBasicBlock(BB->getBasicBlock());
1105
Matt Arsenault786724a2016-07-12 21:41:32 +00001106 MF->insert(++MachineFunction::iterator(BB), SplitBB);
1107 SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end());
1108
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001109 SplitBB->transferSuccessorsAndUpdatePHIs(BB);
Matt Arsenault786724a2016-07-12 21:41:32 +00001110 BB->addSuccessor(SplitBB);
1111
1112 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1113 return SplitBB;
1114}
1115
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001116// Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the
1117// wavefront. If the value is uniform and just happens to be in a VGPR, this
1118// will only do one iteration. In the worst case, this will loop 64 times.
1119//
1120// TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value.
1121static void emitLoadM0FromVGPRLoop(const SIInstrInfo *TII,
1122 MachineRegisterInfo &MRI,
1123 MachineBasicBlock &OrigBB,
1124 MachineBasicBlock &LoopBB,
1125 const DebugLoc &DL,
1126 MachineInstr *MovRel,
1127 const MachineOperand &IdxReg,
1128 unsigned InitReg,
1129 unsigned ResultReg,
1130 unsigned PhiReg,
1131 unsigned InitSaveExecReg,
1132 int Offset) {
1133 MachineBasicBlock::iterator I = LoopBB.begin();
1134
1135 unsigned PhiExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1136 unsigned NewExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1137 unsigned CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1138 unsigned CondReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1139
1140 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg)
1141 .addReg(InitReg)
1142 .addMBB(&OrigBB)
1143 .addReg(ResultReg)
1144 .addMBB(&LoopBB);
1145
1146 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec)
1147 .addReg(InitSaveExecReg)
1148 .addMBB(&OrigBB)
1149 .addReg(NewExec)
1150 .addMBB(&LoopBB);
1151
1152 // Read the next variant <- also loop target.
1153 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg)
1154 .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef()));
1155
1156 // Compare the just read M0 value to all possible Idx values.
1157 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg)
1158 .addReg(CurrentIdxReg)
Matt Arsenaultf0ba86a2016-07-21 09:40:57 +00001159 .addReg(IdxReg.getReg(), 0, IdxReg.getSubReg());
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001160
1161 // Move index from VCC into M0
1162 if (Offset == 0) {
1163 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1164 .addReg(CurrentIdxReg, RegState::Kill);
1165 } else {
1166 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1167 .addReg(CurrentIdxReg, RegState::Kill)
1168 .addImm(Offset);
1169 }
1170
1171 // Update EXEC, save the original EXEC value to VCC.
1172 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), NewExec)
1173 .addReg(CondReg, RegState::Kill);
1174
1175 MRI.setSimpleHint(NewExec, CondReg);
1176
1177 // Do the actual move.
1178 LoopBB.insert(I, MovRel);
1179
1180 // Update EXEC, switch all done bits to 0 and all todo bits to 1.
1181 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
1182 .addReg(AMDGPU::EXEC)
1183 .addReg(NewExec);
1184
1185 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use
1186 // s_cbranch_scc0?
1187
1188 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover.
1189 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
1190 .addMBB(&LoopBB);
1191}
1192
1193// This has slightly sub-optimal regalloc when the source vector is killed by
1194// the read. The register allocator does not understand that the kill is
1195// per-workitem, so is kept alive for the whole loop so we end up not re-using a
1196// subregister from it, using 1 more VGPR than necessary. This was saved when
1197// this was expanded after register allocation.
1198static MachineBasicBlock *loadM0FromVGPR(const SIInstrInfo *TII,
1199 MachineBasicBlock &MBB,
1200 MachineInstr &MI,
1201 MachineInstr *MovRel,
1202 unsigned InitResultReg,
1203 unsigned PhiReg,
1204 int Offset) {
1205 MachineFunction *MF = MBB.getParent();
1206 MachineRegisterInfo &MRI = MF->getRegInfo();
1207 const DebugLoc &DL = MI.getDebugLoc();
1208 MachineBasicBlock::iterator I(&MI);
1209
1210 unsigned DstReg = MI.getOperand(0).getReg();
1211 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1212 unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1213
1214 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec);
1215
1216 // Save the EXEC mask
1217 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec)
1218 .addReg(AMDGPU::EXEC);
1219
1220 // To insert the loop we need to split the block. Move everything after this
1221 // point to a new block, and insert a new empty block between the two.
1222 MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock();
1223 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock();
1224 MachineFunction::iterator MBBI(MBB);
1225 ++MBBI;
1226
1227 MF->insert(MBBI, LoopBB);
1228 MF->insert(MBBI, RemainderBB);
1229
1230 LoopBB->addSuccessor(LoopBB);
1231 LoopBB->addSuccessor(RemainderBB);
1232
1233 // Move the rest of the block into a new block.
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001234 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001235 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end());
1236
1237 MBB.addSuccessor(LoopBB);
1238
1239 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1240
1241 emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, MovRel, *Idx,
1242 InitResultReg, DstReg, PhiReg, TmpExec, Offset);
1243
1244 MachineBasicBlock::iterator First = RemainderBB->begin();
1245 BuildMI(*RemainderBB, First, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
1246 .addReg(SaveExec);
1247
1248 MI.eraseFromParent();
1249
1250 return RemainderBB;
1251}
1252
1253// Returns subreg index, offset
1254static std::pair<unsigned, int>
1255computeIndirectRegAndOffset(const SIRegisterInfo &TRI,
1256 const TargetRegisterClass *SuperRC,
1257 unsigned VecReg,
1258 int Offset) {
1259 int NumElts = SuperRC->getSize() / 4;
1260
1261 // Skip out of bounds offsets, or else we would end up using an undefined
1262 // register.
1263 if (Offset >= NumElts || Offset < 0)
1264 return std::make_pair(AMDGPU::sub0, Offset);
1265
1266 return std::make_pair(AMDGPU::sub0 + Offset, 0);
1267}
1268
1269// Return true if the index is an SGPR and was set.
1270static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII,
1271 MachineRegisterInfo &MRI,
1272 MachineInstr &MI,
1273 int Offset) {
1274 MachineBasicBlock *MBB = MI.getParent();
1275 const DebugLoc &DL = MI.getDebugLoc();
1276 MachineBasicBlock::iterator I(&MI);
1277
1278 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1279 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
1280
1281 assert(Idx->getReg() != AMDGPU::NoRegister);
1282
1283 if (!TII->getRegisterInfo().isSGPRClass(IdxRC))
1284 return false;
1285
1286 if (Offset == 0) {
1287 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1288 .addOperand(*Idx);
1289 } else {
1290 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1291 .addOperand(*Idx)
1292 .addImm(Offset);
1293 }
1294
1295 return true;
1296}
1297
1298// Control flow needs to be inserted if indexing with a VGPR.
1299static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI,
1300 MachineBasicBlock &MBB,
1301 const SIInstrInfo *TII) {
1302 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1303 MachineFunction *MF = MBB.getParent();
1304 MachineRegisterInfo &MRI = MF->getRegInfo();
1305
1306 unsigned Dst = MI.getOperand(0).getReg();
1307 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
1308 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1309
1310 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
1311
1312 unsigned SubReg;
1313 std::tie(SubReg, Offset)
1314 = computeIndirectRegAndOffset(TRI, VecRC, SrcVec->getReg(), Offset);
1315
1316 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset)) {
1317 MachineBasicBlock::iterator I(&MI);
1318 const DebugLoc &DL = MI.getDebugLoc();
1319
1320 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
1321 .addReg(SrcVec->getReg(), RegState::Undef, SubReg)
1322 .addReg(SrcVec->getReg(), RegState::Implicit);
1323 MI.eraseFromParent();
1324
1325 return &MBB;
1326 }
1327
1328 const DebugLoc &DL = MI.getDebugLoc();
1329 MachineBasicBlock::iterator I(&MI);
1330
1331 unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1332 unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1333
1334 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg);
1335
1336 MachineInstr *MovRel =
1337 BuildMI(*MF, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
1338 .addReg(SrcVec->getReg(), RegState::Undef, SubReg)
1339 .addReg(SrcVec->getReg(), RegState::Implicit);
1340
1341 return loadM0FromVGPR(TII, MBB, MI, MovRel, InitReg, PhiReg, Offset);
1342}
1343
1344static MachineBasicBlock *emitIndirectDst(MachineInstr &MI,
1345 MachineBasicBlock &MBB,
1346 const SIInstrInfo *TII) {
1347 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1348 MachineFunction *MF = MBB.getParent();
1349 MachineRegisterInfo &MRI = MF->getRegInfo();
1350
1351 unsigned Dst = MI.getOperand(0).getReg();
1352 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
1353 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1354 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val);
1355 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1356 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
1357
1358 // This can be an immediate, but will be folded later.
1359 assert(Val->getReg());
1360
1361 unsigned SubReg;
1362 std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC,
1363 SrcVec->getReg(),
1364 Offset);
1365 if (Idx->getReg() == AMDGPU::NoRegister) {
1366 MachineBasicBlock::iterator I(&MI);
1367 const DebugLoc &DL = MI.getDebugLoc();
1368
1369 assert(Offset == 0);
1370
1371 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst)
1372 .addOperand(*SrcVec)
1373 .addOperand(*Val)
1374 .addImm(SubReg);
1375
1376 MI.eraseFromParent();
1377 return &MBB;
1378 }
1379
1380 const MCInstrDesc &MovRelDesc = TII->get(AMDGPU::V_MOVRELD_B32_e32);
1381 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset)) {
1382 MachineBasicBlock::iterator I(&MI);
1383 const DebugLoc &DL = MI.getDebugLoc();
1384
1385 MachineInstr *MovRel =
1386 BuildMI(MBB, I, DL, MovRelDesc)
1387 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst
1388 .addOperand(*Val)
1389 .addReg(Dst, RegState::ImplicitDefine)
1390 .addReg(SrcVec->getReg(), RegState::Implicit);
1391
1392 const int ImpDefIdx = MovRelDesc.getNumOperands() +
1393 MovRelDesc.getNumImplicitUses();
1394 const int ImpUseIdx = ImpDefIdx + 1;
1395
1396 MovRel->tieOperands(ImpDefIdx, ImpUseIdx);
1397 MI.eraseFromParent();
1398 return &MBB;
1399 }
1400
1401 if (Val->isReg())
1402 MRI.clearKillFlags(Val->getReg());
1403
1404 const DebugLoc &DL = MI.getDebugLoc();
1405 unsigned PhiReg = MRI.createVirtualRegister(VecRC);
1406
1407 // vdst is not actually read and just provides the base register index.
1408 MachineInstr *MovRel =
1409 BuildMI(*MF, DL, MovRelDesc)
1410 .addReg(PhiReg, RegState::Undef, SubReg) // vdst
1411 .addOperand(*Val)
1412 .addReg(Dst, RegState::ImplicitDefine)
1413 .addReg(PhiReg, RegState::Implicit);
1414
1415 const int ImpDefIdx = MovRelDesc.getNumOperands() +
1416 MovRelDesc.getNumImplicitUses();
1417 const int ImpUseIdx = ImpDefIdx + 1;
1418
1419 MovRel->tieOperands(ImpDefIdx, ImpUseIdx);
1420
1421 return loadM0FromVGPR(TII, MBB, MI, MovRel,
1422 SrcVec->getReg(), PhiReg, Offset);
1423}
1424
Matt Arsenault786724a2016-07-12 21:41:32 +00001425MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter(
1426 MachineInstr &MI, MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001427 switch (MI.getOpcode()) {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001428 case AMDGPU::SI_INIT_M0: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001429 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001430 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001431 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001432 .addOperand(MI.getOperand(0));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001433 MI.eraseFromParent();
Matt Arsenault20711b72015-02-20 22:10:45 +00001434 return BB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001435 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001436 case AMDGPU::GET_GROUPSTATICSIZE: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001437 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1438
Changpeng Fang01f60622016-03-15 17:28:44 +00001439 MachineFunction *MF = BB->getParent();
1440 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001441 DebugLoc DL = MI.getDebugLoc();
Matt Arsenault3c07c812016-07-22 17:01:33 +00001442 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32))
1443 .addOperand(MI.getOperand(0))
Matt Arsenault52ef4012016-07-26 16:45:58 +00001444 .addImm(MFI->getLDSSize());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001445 MI.eraseFromParent();
Changpeng Fang01f60622016-03-15 17:28:44 +00001446 return BB;
1447 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001448 case AMDGPU::SI_INDIRECT_SRC_V1:
1449 case AMDGPU::SI_INDIRECT_SRC_V2:
1450 case AMDGPU::SI_INDIRECT_SRC_V4:
1451 case AMDGPU::SI_INDIRECT_SRC_V8:
1452 case AMDGPU::SI_INDIRECT_SRC_V16:
1453 return emitIndirectSrc(MI, *BB, getSubtarget()->getInstrInfo());
1454 case AMDGPU::SI_INDIRECT_DST_V1:
1455 case AMDGPU::SI_INDIRECT_DST_V2:
1456 case AMDGPU::SI_INDIRECT_DST_V4:
1457 case AMDGPU::SI_INDIRECT_DST_V8:
1458 case AMDGPU::SI_INDIRECT_DST_V16:
1459 return emitIndirectDst(MI, *BB, getSubtarget()->getInstrInfo());
Matt Arsenault786724a2016-07-12 21:41:32 +00001460 case AMDGPU::SI_KILL:
1461 return splitKillBlock(MI, BB);
Changpeng Fang01f60622016-03-15 17:28:44 +00001462 default:
1463 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001464 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001465}
1466
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001467bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1468 // This currently forces unfolding various combinations of fsub into fma with
1469 // free fneg'd operands. As long as we have fast FMA (controlled by
1470 // isFMAFasterThanFMulAndFAdd), we should perform these.
1471
1472 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1473 // most of these combines appear to be cycle neutral but save on instruction
1474 // count / code size.
1475 return true;
1476}
1477
Mehdi Amini44ede332015-07-09 02:09:04 +00001478EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1479 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001480 if (!VT.isVector()) {
1481 return MVT::i1;
1482 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001483 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001484}
1485
Mehdi Aminieaabc512015-07-09 15:12:23 +00001486MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001487 return MVT::i32;
1488}
1489
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001490// Answering this is somewhat tricky and depends on the specific device which
1491// have different rates for fma or all f64 operations.
1492//
1493// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1494// regardless of which device (although the number of cycles differs between
1495// devices), so it is always profitable for f64.
1496//
1497// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1498// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1499// which we can always do even without fused FP ops since it returns the same
1500// result as the separate operations and since it is always full
1501// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1502// however does not support denormals, so we do report fma as faster if we have
1503// a fast fma device and require denormals.
1504//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001505bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1506 VT = VT.getScalarType();
1507
1508 if (!VT.isSimple())
1509 return false;
1510
1511 switch (VT.getSimpleVT().SimpleTy) {
1512 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001513 // This is as fast on some subtargets. However, we always have full rate f32
1514 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001515 // which we should prefer over fma. We can't use this if we want to support
1516 // denormals, so only report this in these cases.
1517 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001518 case MVT::f64:
1519 return true;
1520 default:
1521 break;
1522 }
1523
1524 return false;
1525}
1526
Tom Stellard75aadc22012-12-11 21:25:42 +00001527//===----------------------------------------------------------------------===//
1528// Custom DAG Lowering Operations
1529//===----------------------------------------------------------------------===//
1530
1531SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1532 switch (Op.getOpcode()) {
1533 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001534 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001535 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001536 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001537 SDValue Result = LowerLOAD(Op, DAG);
1538 assert((!Result.getNode() ||
1539 Result.getNode()->getNumValues() == 2) &&
1540 "Load should return a value and a chain");
1541 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001542 }
Tom Stellardaf775432013-10-23 00:44:32 +00001543
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001544 case ISD::FSIN:
1545 case ISD::FCOS:
1546 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001547 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001548 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard354a43c2016-04-01 18:27:37 +00001549 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001550 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001551 case ISD::GlobalAddress: {
1552 MachineFunction &MF = DAG.getMachineFunction();
1553 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1554 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001555 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001556 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001557 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001558 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Matt Arsenault99c14522016-04-25 19:27:24 +00001559 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG);
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001560 case ISD::TRAP: return lowerTRAP(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001561 }
1562 return SDValue();
1563}
1564
Tom Stellardf8794352012-12-19 22:10:31 +00001565/// \brief Helper function for LowerBRCOND
1566static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001567
Tom Stellardf8794352012-12-19 22:10:31 +00001568 SDNode *Parent = Value.getNode();
1569 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1570 I != E; ++I) {
1571
1572 if (I.getUse().get() != Value)
1573 continue;
1574
1575 if (I->getOpcode() == Opcode)
1576 return *I;
1577 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001578 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001579}
1580
Tom Stellardb02094e2014-07-21 15:45:01 +00001581SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
1582
Tom Stellardc98ee202015-07-16 19:40:07 +00001583 SDLoc SL(Op);
Tom Stellardb02094e2014-07-21 15:45:01 +00001584 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
1585 unsigned FrameIndex = FINode->getIndex();
1586
Matt Arsenault3a619852016-02-27 20:26:57 +00001587 // A FrameIndex node represents a 32-bit offset into scratch memory. If the
1588 // high bit of a frame index offset were to be set, this would mean that it
1589 // represented an offset of ~2GB * 64 = ~128GB from the start of the scratch
1590 // buffer, with 64 being the number of threads per wave.
Tom Stellardc98ee202015-07-16 19:40:07 +00001591 //
Matt Arsenault3a619852016-02-27 20:26:57 +00001592 // The maximum private allocation for the entire GPU is 4G, and we are
1593 // concerned with the largest the index could ever be for an individual
1594 // workitem. This will occur with the minmum dispatch size. If a program
1595 // requires more, the dispatch size will be reduced.
1596 //
1597 // With this limit, we can mark the high bit of the FrameIndex node as known
1598 // zero, which is important, because it means in most situations we can prove
1599 // that values derived from FrameIndex nodes are non-negative. This enables us
1600 // to take advantage of more addressing modes when accessing scratch buffers,
1601 // since for scratch reads/writes, the register offset must always be
1602 // positive.
1603
1604 uint64_t MaxGPUAlloc = UINT64_C(4) * 1024 * 1024 * 1024;
1605
1606 // XXX - It is unclear if partial dispatch works. Assume it works at half wave
1607 // granularity. It is probably a full wave.
1608 uint64_t MinGranularity = 32;
1609
1610 unsigned KnownBits = Log2_64(MaxGPUAlloc / MinGranularity);
1611 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), KnownBits);
Tom Stellardc98ee202015-07-16 19:40:07 +00001612
1613 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
Tom Stellardc98ee202015-07-16 19:40:07 +00001614 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
Matt Arsenault3a619852016-02-27 20:26:57 +00001615 DAG.getValueType(ExtVT));
Tom Stellardb02094e2014-07-21 15:45:01 +00001616}
1617
Tom Stellardbc4497b2016-02-12 23:45:29 +00001618bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault16f48d72016-02-13 00:36:10 +00001619 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN)
Tom Stellardbc4497b2016-02-12 23:45:29 +00001620 return false;
1621
1622 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1623 default: return false;
1624 case AMDGPUIntrinsic::amdgcn_if:
1625 case AMDGPUIntrinsic::amdgcn_else:
Matt Arsenault48d70cb2016-07-09 17:18:39 +00001626 case AMDGPUIntrinsic::amdgcn_break:
Tom Stellardbc4497b2016-02-12 23:45:29 +00001627 case AMDGPUIntrinsic::amdgcn_if_break:
1628 case AMDGPUIntrinsic::amdgcn_else_break:
1629 case AMDGPUIntrinsic::amdgcn_loop:
1630 case AMDGPUIntrinsic::amdgcn_end_cf:
1631 return true;
1632 }
1633}
1634
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001635void SITargetLowering::createDebuggerPrologueStackObjects(
1636 MachineFunction &MF) const {
1637 // Create stack objects that are used for emitting debugger prologue.
1638 //
1639 // Debugger prologue writes work group IDs and work item IDs to scratch memory
1640 // at fixed location in the following format:
1641 // offset 0: work group ID x
1642 // offset 4: work group ID y
1643 // offset 8: work group ID z
1644 // offset 16: work item ID x
1645 // offset 20: work item ID y
1646 // offset 24: work item ID z
1647 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1648 int ObjectIdx = 0;
1649
1650 // For each dimension:
1651 for (unsigned i = 0; i < 3; ++i) {
1652 // Create fixed stack object for work group ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001653 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001654 Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx);
1655 // Create fixed stack object for work item ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001656 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4 + 16, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001657 Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx);
1658 }
1659}
1660
Tom Stellardf8794352012-12-19 22:10:31 +00001661/// This transforms the control flow intrinsics to get the branch destination as
1662/// last parameter, also switches branch target with BR if the need arise
1663SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1664 SelectionDAG &DAG) const {
1665
Andrew Trickef9de2a2013-05-25 02:42:55 +00001666 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001667
1668 SDNode *Intr = BRCOND.getOperand(1).getNode();
1669 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001670 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001671 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001672
1673 if (Intr->getOpcode() == ISD::SETCC) {
1674 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001675 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001676 Intr = SetCC->getOperand(0).getNode();
1677
1678 } else {
1679 // Get the target from BR if we don't negate the condition
1680 BR = findUser(BRCOND, ISD::BR);
1681 Target = BR->getOperand(1);
1682 }
1683
Nicolai Haehnleffbd56a2016-05-05 17:36:36 +00001684 if (!isCFIntrinsic(Intr)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +00001685 // This is a uniform branch so we don't need to legalize.
1686 return BRCOND;
1687 }
1688
1689 assert(!SetCC ||
1690 (SetCC->getConstantOperandVal(1) == 1 &&
Tom Stellardbc4497b2016-02-12 23:45:29 +00001691 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1692 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00001693
1694 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001695 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001696
1697 // operands of the new intrinsic call
1698 SmallVector<SDValue, 4> Ops;
1699 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001700 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001701 Ops.push_back(Target);
1702
1703 // build the new intrinsic call
1704 SDNode *Result = DAG.getNode(
1705 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001706 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001707
1708 if (BR) {
1709 // Give the branch instruction our target
1710 SDValue Ops[] = {
1711 BR->getOperand(0),
1712 BRCOND.getOperand(2)
1713 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001714 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1715 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1716 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001717 }
1718
1719 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1720
1721 // Copy the intrinsic results to registers
1722 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1723 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1724 if (!CopyToReg)
1725 continue;
1726
1727 Chain = DAG.getCopyToReg(
1728 Chain, DL,
1729 CopyToReg->getOperand(1),
1730 SDValue(Result, i - 1),
1731 SDValue());
1732
1733 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1734 }
1735
1736 // Remove the old intrinsic from the chain
1737 DAG.ReplaceAllUsesOfValueWith(
1738 SDValue(Intr, Intr->getNumValues() - 1),
1739 Intr->getOperand(0));
1740
1741 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001742}
1743
Matt Arsenault99c14522016-04-25 19:27:24 +00001744SDValue SITargetLowering::getSegmentAperture(unsigned AS,
1745 SelectionDAG &DAG) const {
1746 SDLoc SL;
1747 MachineFunction &MF = DAG.getMachineFunction();
1748 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00001749 unsigned UserSGPR = Info->getQueuePtrUserSGPR();
1750 assert(UserSGPR != AMDGPU::NoRegister);
1751
Matt Arsenault99c14522016-04-25 19:27:24 +00001752 SDValue QueuePtr = CreateLiveInRegister(
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00001753 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
Matt Arsenault99c14522016-04-25 19:27:24 +00001754
1755 // Offset into amd_queue_t for group_segment_aperture_base_hi /
1756 // private_segment_aperture_base_hi.
1757 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44;
1758
1759 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr,
1760 DAG.getConstant(StructOffset, SL, MVT::i64));
1761
1762 // TODO: Use custom target PseudoSourceValue.
1763 // TODO: We should use the value from the IR intrinsic call, but it might not
1764 // be available and how do we get it?
1765 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()),
1766 AMDGPUAS::CONSTANT_ADDRESS));
1767
1768 MachinePointerInfo PtrInfo(V, StructOffset);
Justin Lebar9c375812016-07-15 18:27:10 +00001769 return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr, PtrInfo,
1770 MinAlign(64, StructOffset),
1771 MachineMemOperand::MOInvariant);
Matt Arsenault99c14522016-04-25 19:27:24 +00001772}
1773
1774SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
1775 SelectionDAG &DAG) const {
1776 SDLoc SL(Op);
1777 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op);
1778
1779 SDValue Src = ASC->getOperand(0);
1780
1781 // FIXME: Really support non-0 null pointers.
1782 SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32);
1783 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
1784
1785 // flat -> local/private
1786 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
1787 if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1788 ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1789 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
1790 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
1791
1792 return DAG.getNode(ISD::SELECT, SL, MVT::i32,
1793 NonNull, Ptr, SegmentNullPtr);
1794 }
1795 }
1796
1797 // local/private -> flat
1798 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
1799 if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1800 ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1801 SDValue NonNull
1802 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
1803
1804 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG);
1805 SDValue CvtPtr
1806 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
1807
1808 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull,
1809 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr),
1810 FlatNullPtr);
1811 }
1812 }
1813
1814 // global <-> flat are no-ops and never emitted.
1815
1816 const MachineFunction &MF = DAG.getMachineFunction();
1817 DiagnosticInfoUnsupported InvalidAddrSpaceCast(
1818 *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc());
1819 DAG.getContext()->diagnose(InvalidAddrSpaceCast);
1820
1821 return DAG.getUNDEF(ASC->getValueType(0));
1822}
1823
Tom Stellard418beb72016-07-13 14:23:33 +00001824static bool shouldEmitGOTReloc(const GlobalValue *GV,
1825 const TargetMachine &TM) {
1826 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
1827 !TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
Tom Stellardb164a982016-06-25 01:59:16 +00001828}
1829
Tom Stellard418beb72016-07-13 14:23:33 +00001830bool
1831SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1832 // We can fold offsets for anything that doesn't require a GOT relocation.
1833 return GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
1834 !shouldEmitGOTReloc(GA->getGlobal(), getTargetMachine());
1835}
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001836
Tom Stellard418beb72016-07-13 14:23:33 +00001837static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV,
1838 SDLoc DL, unsigned Offset, EVT PtrVT,
1839 unsigned GAFlags = SIInstrInfo::MO_NONE) {
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001840 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is
1841 // lowered to the following code sequence:
1842 // s_getpc_b64 s[0:1]
1843 // s_add_u32 s0, s0, $symbol
1844 // s_addc_u32 s1, s1, 0
1845 //
1846 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
1847 // a fixup or relocation is emitted to replace $symbol with a literal
1848 // constant, which is a pc-relative offset from the encoding of the $symbol
1849 // operand to the global variable.
1850 //
1851 // What we want here is an offset from the value returned by s_getpc
1852 // (which is the address of the s_add_u32 instruction) to the global
1853 // variable, but since the encoding of $symbol starts 4 bytes after the start
1854 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too
1855 // small. This requires us to add 4 to the global variable offset in order to
1856 // compute the correct address.
Tom Stellard418beb72016-07-13 14:23:33 +00001857 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
1858 GAFlags);
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001859 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, GA);
1860}
1861
Tom Stellard418beb72016-07-13 14:23:33 +00001862SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1863 SDValue Op,
1864 SelectionDAG &DAG) const {
1865 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1866
1867 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS &&
1868 GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS)
1869 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1870
1871 SDLoc DL(GSD);
1872 const GlobalValue *GV = GSD->getGlobal();
1873 EVT PtrVT = Op.getValueType();
1874
1875 if (!shouldEmitGOTReloc(GV, getTargetMachine()))
1876 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT);
1877
1878 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT,
1879 SIInstrInfo::MO_GOTPCREL);
1880
1881 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext());
1882 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
1883 const DataLayout &DataLayout = DAG.getDataLayout();
1884 unsigned Align = DataLayout.getABITypeAlignment(PtrTy);
1885 // FIXME: Use a PseudoSourceValue once those can be assigned an address space.
1886 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
1887
Justin Lebar9c375812016-07-15 18:27:10 +00001888 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align,
1889 MachineMemOperand::MOInvariant);
Tom Stellard418beb72016-07-13 14:23:33 +00001890}
1891
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001892SDValue SITargetLowering::lowerTRAP(SDValue Op,
1893 SelectionDAG &DAG) const {
1894 const MachineFunction &MF = DAG.getMachineFunction();
1895 DiagnosticInfoUnsupported NoTrap(*MF.getFunction(),
1896 "trap handler not supported",
1897 Op.getDebugLoc(),
1898 DS_Warning);
1899 DAG.getContext()->diagnose(NoTrap);
1900
1901 // Emit s_endpgm.
1902
1903 // FIXME: This should really be selected to s_trap, but that requires
1904 // setting up the trap handler for it o do anything.
Matt Arsenault9babdf42016-06-22 20:15:28 +00001905 return DAG.getNode(AMDGPUISD::ENDPGM, SDLoc(Op), MVT::Other,
1906 Op.getOperand(0));
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001907}
1908
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001909SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain,
1910 const SDLoc &DL, SDValue V) const {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001911 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
1912 // the destination register.
1913 //
Tom Stellardfc92e772015-05-12 14:18:14 +00001914 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1915 // so we will end up with redundant moves to m0.
1916 //
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001917 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
1918
1919 // A Null SDValue creates a glue result.
1920 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
1921 V, Chain);
1922 return SDValue(M0, 0);
Tom Stellardfc92e772015-05-12 14:18:14 +00001923}
1924
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001925SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1926 SDValue Op,
1927 MVT VT,
1928 unsigned Offset) const {
1929 SDLoc SL(Op);
1930 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1931 DAG.getEntryNode(), Offset, false);
1932 // The local size values will have the hi 16-bits as zero.
1933 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1934 DAG.getValueType(VT));
1935}
1936
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001937static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
Matt Arsenaulte0132462016-01-30 05:19:45 +00001938 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001939 "non-hsa intrinsic with hsa target",
1940 DL.getDebugLoc());
1941 DAG.getContext()->diagnose(BadIntrin);
1942 return DAG.getUNDEF(VT);
1943}
1944
1945static SDValue emitRemovedIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
1946 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
1947 "intrinsic not supported on subtarget",
1948 DL.getDebugLoc());
Matt Arsenaulte0132462016-01-30 05:19:45 +00001949 DAG.getContext()->diagnose(BadIntrin);
1950 return DAG.getUNDEF(VT);
1951}
1952
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001953SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1954 SelectionDAG &DAG) const {
1955 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001956 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001957 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001958
1959 EVT VT = Op.getValueType();
1960 SDLoc DL(Op);
1961 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1962
Sanjay Patela2607012015-09-16 16:31:21 +00001963 // TODO: Should this propagate fast-math-flags?
1964
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001965 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001966 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault48ab5262016-04-25 19:27:18 +00001967 case Intrinsic::amdgcn_queue_ptr: {
Matt Arsenault800fecf2016-01-11 21:18:33 +00001968 if (!Subtarget->isAmdHsaOS()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00001969 DiagnosticInfoUnsupported BadIntrin(
1970 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
1971 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00001972 DAG.getContext()->diagnose(BadIntrin);
1973 return DAG.getUNDEF(VT);
1974 }
1975
Matt Arsenault48ab5262016-04-25 19:27:18 +00001976 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ?
1977 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00001978 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
Matt Arsenault48ab5262016-04-25 19:27:18 +00001979 TRI->getPreloadedValue(MF, Reg), VT);
1980 }
Jan Veselyfea814d2016-06-21 20:46:20 +00001981 case Intrinsic::amdgcn_implicitarg_ptr: {
1982 unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
1983 return LowerParameterPtr(DAG, DL, DAG.getEntryNode(), offset);
1984 }
Matt Arsenaultdc4ebad2016-04-29 21:16:52 +00001985 case Intrinsic::amdgcn_kernarg_segment_ptr: {
1986 unsigned Reg
1987 = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
1988 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
1989 }
Matt Arsenault8d718dc2016-07-22 17:01:30 +00001990 case Intrinsic::amdgcn_dispatch_id: {
1991 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_ID);
1992 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
1993 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001994 case Intrinsic::amdgcn_rcp:
1995 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
1996 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001997 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001998 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001999 case Intrinsic::amdgcn_rsq_legacy: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002000 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002001 return emitRemovedIntrinsicError(DAG, DL, VT);
2002
2003 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
2004 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002005 case Intrinsic::amdgcn_rcp_legacy: {
2006 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
2007 return emitRemovedIntrinsicError(DAG, DL, VT);
2008 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1));
2009 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +00002010 case Intrinsic::amdgcn_rsq_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002011 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00002012 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00002013
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002014 Type *Type = VT.getTypeForEVT(*DAG.getContext());
2015 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
2016 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
2017
2018 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
2019 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
2020 DAG.getConstantFP(Max, DL, VT));
2021 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
2022 DAG.getConstantFP(Min, DL, VT));
2023 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002024 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002025 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002026 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002027
Tom Stellardec2e43c2014-09-22 15:35:29 +00002028 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2029 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002030 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002031 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002032 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002033
Tom Stellardec2e43c2014-09-22 15:35:29 +00002034 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2035 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002036 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002037 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002038 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002039
Tom Stellardec2e43c2014-09-22 15:35:29 +00002040 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2041 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002042 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002043 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002044 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002045
Tom Stellardec2e43c2014-09-22 15:35:29 +00002046 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2047 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002048 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002049 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002050 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002051
Tom Stellardec2e43c2014-09-22 15:35:29 +00002052 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2053 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002054 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002055 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002056 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002057
Tom Stellardec2e43c2014-09-22 15:35:29 +00002058 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2059 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002060 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002061 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002062 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002063
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002064 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2065 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002066 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002067 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002068 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002069
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002070 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2071 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002072 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002073 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002074 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002075
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002076 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2077 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenault43976df2016-01-30 04:25:19 +00002078 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002079 case Intrinsic::r600_read_tgid_x:
2080 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002081 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002082 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002083 case Intrinsic::r600_read_tgid_y:
2084 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002085 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002086 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002087 case Intrinsic::r600_read_tgid_z:
2088 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002089 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002090 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002091 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002092 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002093 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002094 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002095 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002096 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002097 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002098 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002099 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002100 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002101 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002102 case AMDGPUIntrinsic::SI_load_const: {
2103 SDValue Ops[] = {
2104 Op.getOperand(1),
2105 Op.getOperand(2)
2106 };
2107
2108 MachineMemOperand *MMO = MF.getMachineMemOperand(
2109 MachinePointerInfo(),
2110 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
2111 VT.getStoreSize(), 4);
2112 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
2113 Op->getVTList(), Ops, VT, MMO);
2114 }
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002115 case AMDGPUIntrinsic::amdgcn_fdiv_fast: {
2116 return lowerFDIV_FAST(Op, DAG);
2117 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002118 case AMDGPUIntrinsic::SI_vs_load_input:
2119 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
2120 Op.getOperand(1),
2121 Op.getOperand(2),
2122 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00002123
Tom Stellard2a9d9472015-05-12 15:00:46 +00002124 case AMDGPUIntrinsic::SI_fs_constant: {
2125 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2126 SDValue Glue = M0.getValue(1);
2127 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
2128 DAG.getConstant(2, DL, MVT::i32), // P0
2129 Op.getOperand(1), Op.getOperand(2), Glue);
2130 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00002131 case AMDGPUIntrinsic::SI_packf16:
2132 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
2133 return DAG.getUNDEF(MVT::i32);
2134 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00002135 case AMDGPUIntrinsic::SI_fs_interp: {
2136 SDValue IJ = Op.getOperand(4);
2137 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2138 DAG.getConstant(0, DL, MVT::i32));
2139 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2140 DAG.getConstant(1, DL, MVT::i32));
2141 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2142 SDValue Glue = M0.getValue(1);
2143 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
2144 DAG.getVTList(MVT::f32, MVT::Glue),
2145 I, Op.getOperand(1), Op.getOperand(2), Glue);
2146 Glue = SDValue(P1.getNode(), 1);
2147 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
2148 Op.getOperand(1), Op.getOperand(2), Glue);
2149 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00002150 case Intrinsic::amdgcn_interp_p1: {
2151 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
2152 SDValue Glue = M0.getValue(1);
2153 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
2154 Op.getOperand(2), Op.getOperand(3), Glue);
2155 }
2156 case Intrinsic::amdgcn_interp_p2: {
2157 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
2158 SDValue Glue = SDValue(M0.getNode(), 1);
2159 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
2160 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
2161 Glue);
2162 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002163 case Intrinsic::amdgcn_sin:
2164 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
2165
2166 case Intrinsic::amdgcn_cos:
2167 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
2168
2169 case Intrinsic::amdgcn_log_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002170 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002171 return SDValue();
2172
2173 DiagnosticInfoUnsupported BadIntrin(
2174 *MF.getFunction(), "intrinsic not supported on subtarget",
2175 DL.getDebugLoc());
2176 DAG.getContext()->diagnose(BadIntrin);
2177 return DAG.getUNDEF(VT);
2178 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002179 case Intrinsic::amdgcn_ldexp:
2180 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
2181 Op.getOperand(1), Op.getOperand(2));
Matt Arsenault74015162016-05-28 00:19:52 +00002182
2183 case Intrinsic::amdgcn_fract:
2184 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
2185
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002186 case Intrinsic::amdgcn_class:
2187 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
2188 Op.getOperand(1), Op.getOperand(2));
2189 case Intrinsic::amdgcn_div_fmas:
2190 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
2191 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
2192 Op.getOperand(4));
2193
2194 case Intrinsic::amdgcn_div_fixup:
2195 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
2196 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
2197
2198 case Intrinsic::amdgcn_trig_preop:
2199 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
2200 Op.getOperand(1), Op.getOperand(2));
2201 case Intrinsic::amdgcn_div_scale: {
2202 // 3rd parameter required to be a constant.
2203 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2204 if (!Param)
2205 return DAG.getUNDEF(VT);
2206
2207 // Translate to the operands expected by the machine instruction. The
2208 // first parameter must be the same as the first instruction.
2209 SDValue Numerator = Op.getOperand(1);
2210 SDValue Denominator = Op.getOperand(2);
2211
2212 // Note this order is opposite of the machine instruction's operations,
2213 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
2214 // intrinsic has the numerator as the first operand to match a normal
2215 // division operation.
2216
2217 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
2218
2219 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
2220 Denominator, Numerator);
2221 }
Wei Ding07e03712016-07-28 16:42:13 +00002222 case Intrinsic::amdgcn_icmp: {
2223 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2224 int CondCode = CD->getSExtValue();
2225
2226 if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE ||
2227 CondCode >= ICmpInst::Predicate::BAD_ICMP_PREDICATE)
2228 return DAG.getUNDEF(VT);
2229
2230 ICmpInst::Predicate IcInput =
2231 static_cast<ICmpInst::Predicate>(CondCode);
2232 ISD::CondCode CCOpcode = getICmpCondCode(IcInput);
2233 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2234 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2235 }
2236 case Intrinsic::amdgcn_fcmp: {
2237 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2238 int CondCode = CD->getSExtValue();
2239
2240 if (CondCode <= FCmpInst::Predicate::FCMP_FALSE ||
2241 CondCode >= FCmpInst::Predicate::FCMP_TRUE)
2242 return DAG.getUNDEF(VT);
2243
2244 FCmpInst::Predicate IcInput =
2245 static_cast<FCmpInst::Predicate>(CondCode);
2246 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput);
2247 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2248 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2249 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002250 case Intrinsic::amdgcn_fmul_legacy:
2251 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT,
2252 Op.getOperand(1), Op.getOperand(2));
Matt Arsenaultc96e1de2016-07-18 18:35:05 +00002253 case Intrinsic::amdgcn_sffbh:
2254 case AMDGPUIntrinsic::AMDGPU_flbit_i32: // Legacy name.
2255 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002256 default:
2257 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
2258 }
2259}
2260
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002261SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
2262 SelectionDAG &DAG) const {
2263 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2264 switch (IntrID) {
2265 case Intrinsic::amdgcn_atomic_inc:
2266 case Intrinsic::amdgcn_atomic_dec: {
2267 MemSDNode *M = cast<MemSDNode>(Op);
2268 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
2269 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
2270 SDValue Ops[] = {
2271 M->getOperand(0), // Chain
2272 M->getOperand(2), // Ptr
2273 M->getOperand(3) // Value
2274 };
2275
2276 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
2277 M->getMemoryVT(), M->getMemOperand());
2278 }
2279 default:
2280 return SDValue();
2281 }
2282}
2283
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002284SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
2285 SelectionDAG &DAG) const {
2286 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00002287 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002288 SDValue Chain = Op.getOperand(0);
2289 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2290
2291 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00002292 case AMDGPUIntrinsic::SI_sendmsg: {
2293 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
2294 SDValue Glue = Chain.getValue(1);
2295 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
2296 Op.getOperand(2), Glue);
2297 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002298 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002299 SDValue Ops[] = {
2300 Chain,
2301 Op.getOperand(2),
2302 Op.getOperand(3),
2303 Op.getOperand(4),
2304 Op.getOperand(5),
2305 Op.getOperand(6),
2306 Op.getOperand(7),
2307 Op.getOperand(8),
2308 Op.getOperand(9),
2309 Op.getOperand(10),
2310 Op.getOperand(11),
2311 Op.getOperand(12),
2312 Op.getOperand(13),
2313 Op.getOperand(14)
2314 };
2315
2316 EVT VT = Op.getOperand(3).getValueType();
2317
2318 MachineMemOperand *MMO = MF.getMachineMemOperand(
2319 MachinePointerInfo(),
2320 MachineMemOperand::MOStore,
2321 VT.getStoreSize(), 4);
2322 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
2323 Op->getVTList(), Ops, VT, MMO);
2324 }
Matt Arsenault00568682016-07-13 06:04:22 +00002325 case AMDGPUIntrinsic::AMDGPU_kill: {
Matt Arsenault03006fd2016-07-19 16:27:56 +00002326 SDValue Src = Op.getOperand(2);
2327 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) {
Matt Arsenault00568682016-07-13 06:04:22 +00002328 if (!K->isNegative())
2329 return Chain;
Matt Arsenault03006fd2016-07-19 16:27:56 +00002330
2331 SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32);
2332 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne);
Matt Arsenault00568682016-07-13 06:04:22 +00002333 }
2334
Matt Arsenault03006fd2016-07-19 16:27:56 +00002335 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src);
2336 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast);
Matt Arsenault00568682016-07-13 06:04:22 +00002337 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002338 default:
2339 return SDValue();
2340 }
2341}
2342
Tom Stellard81d871d2013-11-13 23:36:50 +00002343SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2344 SDLoc DL(Op);
2345 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00002346 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00002347 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00002348
Matt Arsenaulta1436412016-02-10 18:21:45 +00002349 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
2350 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected");
Matt Arsenault6dfda962016-02-10 18:21:39 +00002351 // FIXME: Copied from PPC
2352 // First, load into 32 bits, then truncate to 1 bit.
2353
2354 SDValue Chain = Load->getChain();
2355 SDValue BasePtr = Load->getBasePtr();
2356 MachineMemOperand *MMO = Load->getMemOperand();
2357
2358 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
2359 BasePtr, MVT::i8, MMO);
2360
2361 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002362 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00002363 NewLD.getValue(1)
2364 };
2365
2366 return DAG.getMergeValues(Ops, DL);
2367 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002368
Matt Arsenaulta1436412016-02-10 18:21:45 +00002369 if (!MemVT.isVector())
2370 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002371
Matt Arsenaulta1436412016-02-10 18:21:45 +00002372 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
2373 "Custom lowering for non-i32 vectors hasn't been implemented.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002374
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002375 unsigned AS = Load->getAddressSpace();
2376 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT,
2377 AS, Load->getAlignment())) {
2378 SDValue Ops[2];
2379 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG);
2380 return DAG.getMergeValues(Ops, DL);
2381 }
2382
2383 unsigned NumElements = MemVT.getVectorNumElements();
2384 switch (AS) {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002385 case AMDGPUAS::CONSTANT_ADDRESS:
2386 if (isMemOpUniform(Load))
2387 return SDValue();
2388 // Non-uniform loads will be selected to MUBUF instructions, so they
2389 // have the same legalization requires ments as global and private
2390 // loads.
2391 //
Justin Bognerb03fd122016-08-17 05:10:15 +00002392 LLVM_FALLTHROUGH;
Matt Arsenaulta1436412016-02-10 18:21:45 +00002393 case AMDGPUAS::GLOBAL_ADDRESS:
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002394 case AMDGPUAS::FLAT_ADDRESS:
2395 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00002396 return SplitVectorLoad(Op, DAG);
2397 // v4 loads are supported for private and global memory.
2398 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002399 case AMDGPUAS::PRIVATE_ADDRESS: {
2400 // Depending on the setting of the private_element_size field in the
2401 // resource descriptor, we can only make private accesses up to a certain
2402 // size.
2403 switch (Subtarget->getMaxPrivateElementSize()) {
2404 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002405 return scalarizeVectorLoad(Load, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002406 case 8:
2407 if (NumElements > 2)
2408 return SplitVectorLoad(Op, DAG);
2409 return SDValue();
2410 case 16:
2411 // Same as global/flat
2412 if (NumElements > 4)
2413 return SplitVectorLoad(Op, DAG);
2414 return SDValue();
2415 default:
2416 llvm_unreachable("unsupported private_element_size");
2417 }
2418 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002419 case AMDGPUAS::LOCAL_ADDRESS: {
2420 if (NumElements > 2)
2421 return SplitVectorLoad(Op, DAG);
2422
2423 if (NumElements == 2)
2424 return SDValue();
2425
Matt Arsenaulta1436412016-02-10 18:21:45 +00002426 // If properly aligned, if we split we might be able to use ds_read_b64.
2427 return SplitVectorLoad(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002428 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00002429 default:
2430 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00002431 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002432}
2433
Tom Stellard0ec134f2014-02-04 17:18:40 +00002434SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2435 if (Op.getValueType() != MVT::i64)
2436 return SDValue();
2437
2438 SDLoc DL(Op);
2439 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002440
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002441 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2442 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002443
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002444 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
2445 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
2446
2447 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
2448 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002449
2450 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
2451
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002452 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
2453 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002454
2455 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
2456
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002457 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002458 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002459}
2460
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002461// Catch division cases where we can use shortcuts with rcp and rsq
2462// instructions.
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002463SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op,
2464 SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002465 SDLoc SL(Op);
2466 SDValue LHS = Op.getOperand(0);
2467 SDValue RHS = Op.getOperand(1);
2468 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002469 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002470
2471 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault979902b2016-08-02 22:25:04 +00002472 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals()))) {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002473
Matt Arsenault979902b2016-08-02 22:25:04 +00002474 if (CLHS->isExactlyValue(1.0)) {
2475 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
2476 // the CI documentation has a worst case error of 1 ulp.
2477 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
2478 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002479
Matt Arsenault979902b2016-08-02 22:25:04 +00002480 // 1.0 / sqrt(x) -> rsq(x)
2481 //
2482 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
2483 // error seems really high at 2^29 ULP.
2484 if (RHS.getOpcode() == ISD::FSQRT)
2485 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
2486
2487 // 1.0 / x -> rcp(x)
2488 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
2489 }
2490
2491 // Same as for 1.0, but expand the sign out of the constant.
2492 if (CLHS->isExactlyValue(-1.0)) {
2493 // -1.0 / x -> rcp (fneg x)
2494 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2495 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS);
2496 }
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002497 }
2498 }
2499
Wei Dinged0f97f2016-06-09 19:17:15 +00002500 const SDNodeFlags *Flags = Op->getFlags();
2501
2502 if (Unsafe || Flags->hasAllowReciprocal()) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002503 // Turn into multiply by the reciprocal.
2504 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00002505 SDNodeFlags Flags;
2506 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002507 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00002508 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002509 }
2510
2511 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002512}
2513
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002514// Faster 2.5 ULP division that does not support denormals.
2515SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const {
2516 SDLoc SL(Op);
2517 SDValue LHS = Op.getOperand(1);
2518 SDValue RHS = Op.getOperand(2);
2519
2520 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
2521
2522 const APFloat K0Val(BitsToFloat(0x6f800000));
2523 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
2524
2525 const APFloat K1Val(BitsToFloat(0x2f800000));
2526 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
2527
2528 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
2529
2530 EVT SetCCVT =
2531 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
2532
2533 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
2534
2535 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
2536
2537 // TODO: Should this propagate fast-math-flags?
2538 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
2539
2540 // rcp does not support denormals.
2541 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
2542
2543 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
2544
2545 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
2546}
2547
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002548SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002549 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
Eric Christopher538d09d02016-06-07 20:27:12 +00002550 return FastLowered;
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002551
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002552 SDLoc SL(Op);
2553 SDValue LHS = Op.getOperand(0);
2554 SDValue RHS = Op.getOperand(1);
2555
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002556 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002557
Wei Dinged0f97f2016-06-09 19:17:15 +00002558 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002559
Wei Dinged0f97f2016-06-09 19:17:15 +00002560 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, RHS, RHS, LHS);
2561 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, LHS, RHS, LHS);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002562
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002563 // Denominator is scaled to not be denormal, so using rcp is ok.
Wei Dinged0f97f2016-06-09 19:17:15 +00002564 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002565
Wei Dinged0f97f2016-06-09 19:17:15 +00002566 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002567
Wei Dinged0f97f2016-06-09 19:17:15 +00002568 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, ApproxRcp, One);
2569 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, ApproxRcp);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002570
Wei Dinged0f97f2016-06-09 19:17:15 +00002571 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, NumeratorScaled, Fma1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002572
Wei Dinged0f97f2016-06-09 19:17:15 +00002573 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, NumeratorScaled);
2574 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul);
2575 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, NumeratorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002576
Wei Dinged0f97f2016-06-09 19:17:15 +00002577 SDValue Scale = NumeratorScaled.getValue(1);
2578 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, Fma4, Fma1, Fma3, Scale);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002579
Wei Dinged0f97f2016-06-09 19:17:15 +00002580 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002581}
2582
2583SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002584 if (DAG.getTarget().Options.UnsafeFPMath)
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002585 return lowerFastUnsafeFDIV(Op, DAG);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002586
2587 SDLoc SL(Op);
2588 SDValue X = Op.getOperand(0);
2589 SDValue Y = Op.getOperand(1);
2590
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002591 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002592
2593 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
2594
2595 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
2596
2597 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
2598
2599 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
2600
2601 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
2602
2603 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
2604
2605 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
2606
2607 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
2608
2609 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
2610 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
2611
2612 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
2613 NegDivScale0, Mul, DivScale1);
2614
2615 SDValue Scale;
2616
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002617 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002618 // Workaround a hardware bug on SI where the condition output from div_scale
2619 // is not usable.
2620
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002621 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002622
2623 // Figure out if the scale to use for div_fmas.
2624 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
2625 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
2626 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
2627 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
2628
2629 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
2630 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
2631
2632 SDValue Scale0Hi
2633 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
2634 SDValue Scale1Hi
2635 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
2636
2637 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
2638 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
2639 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
2640 } else {
2641 Scale = DivScale1.getValue(1);
2642 }
2643
2644 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
2645 Fma4, Fma3, Mul, Scale);
2646
2647 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002648}
2649
2650SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
2651 EVT VT = Op.getValueType();
2652
2653 if (VT == MVT::f32)
2654 return LowerFDIV32(Op, DAG);
2655
2656 if (VT == MVT::f64)
2657 return LowerFDIV64(Op, DAG);
2658
2659 llvm_unreachable("Unexpected type for fdiv");
2660}
2661
Tom Stellard81d871d2013-11-13 23:36:50 +00002662SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2663 SDLoc DL(Op);
2664 StoreSDNode *Store = cast<StoreSDNode>(Op);
2665 EVT VT = Store->getMemoryVT();
2666
Matt Arsenault95245662016-02-11 05:32:46 +00002667 if (VT == MVT::i1) {
2668 return DAG.getTruncStore(Store->getChain(), DL,
2669 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
2670 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00002671 }
2672
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002673 assert(VT.isVector() &&
2674 Store->getValue().getValueType().getScalarType() == MVT::i32);
2675
2676 unsigned AS = Store->getAddressSpace();
2677 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
2678 AS, Store->getAlignment())) {
2679 return expandUnalignedStore(Store, DAG);
2680 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002681
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002682 unsigned NumElements = VT.getVectorNumElements();
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002683 switch (AS) {
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002684 case AMDGPUAS::GLOBAL_ADDRESS:
2685 case AMDGPUAS::FLAT_ADDRESS:
2686 if (NumElements > 4)
2687 return SplitVectorStore(Op, DAG);
2688 return SDValue();
2689 case AMDGPUAS::PRIVATE_ADDRESS: {
2690 switch (Subtarget->getMaxPrivateElementSize()) {
2691 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002692 return scalarizeVectorStore(Store, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002693 case 8:
2694 if (NumElements > 2)
2695 return SplitVectorStore(Op, DAG);
2696 return SDValue();
2697 case 16:
2698 if (NumElements > 4)
2699 return SplitVectorStore(Op, DAG);
2700 return SDValue();
2701 default:
2702 llvm_unreachable("unsupported private_element_size");
2703 }
2704 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002705 case AMDGPUAS::LOCAL_ADDRESS: {
2706 if (NumElements > 2)
2707 return SplitVectorStore(Op, DAG);
2708
2709 if (NumElements == 2)
2710 return Op;
2711
Matt Arsenault95245662016-02-11 05:32:46 +00002712 // If properly aligned, if we split we might be able to use ds_write_b64.
2713 return SplitVectorStore(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002714 }
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002715 default:
2716 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00002717 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002718}
2719
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002720SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002721 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002722 EVT VT = Op.getValueType();
2723 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00002724 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002725 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
2726 DAG.getNode(ISD::FMUL, DL, VT, Arg,
2727 DAG.getConstantFP(0.5/M_PI, DL,
2728 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002729
2730 switch (Op.getOpcode()) {
2731 case ISD::FCOS:
2732 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
2733 case ISD::FSIN:
2734 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
2735 default:
2736 llvm_unreachable("Wrong trig opcode");
2737 }
2738}
2739
Tom Stellard354a43c2016-04-01 18:27:37 +00002740SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
2741 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
2742 assert(AtomicNode->isCompareAndSwap());
2743 unsigned AS = AtomicNode->getAddressSpace();
2744
2745 // No custom lowering required for local address space
2746 if (!isFlatGlobalAddrSpace(AS))
2747 return Op;
2748
2749 // Non-local address space requires custom lowering for atomic compare
2750 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
2751 SDLoc DL(Op);
2752 SDValue ChainIn = Op.getOperand(0);
2753 SDValue Addr = Op.getOperand(1);
2754 SDValue Old = Op.getOperand(2);
2755 SDValue New = Op.getOperand(3);
2756 EVT VT = Op.getValueType();
2757 MVT SimpleVT = VT.getSimpleVT();
2758 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
2759
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002760 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
Tom Stellard354a43c2016-04-01 18:27:37 +00002761 SDValue Ops[] = { ChainIn, Addr, NewOld };
Matt Arsenault88701812016-06-09 23:42:48 +00002762
2763 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(),
2764 Ops, VT, AtomicNode->getMemOperand());
Tom Stellard354a43c2016-04-01 18:27:37 +00002765}
2766
Tom Stellard75aadc22012-12-11 21:25:42 +00002767//===----------------------------------------------------------------------===//
2768// Custom DAG optimizations
2769//===----------------------------------------------------------------------===//
2770
Matt Arsenault364a6742014-06-11 17:50:44 +00002771SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00002772 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00002773 EVT VT = N->getValueType(0);
2774 EVT ScalarVT = VT.getScalarType();
2775 if (ScalarVT != MVT::f32)
2776 return SDValue();
2777
2778 SelectionDAG &DAG = DCI.DAG;
2779 SDLoc DL(N);
2780
2781 SDValue Src = N->getOperand(0);
2782 EVT SrcVT = Src.getValueType();
2783
2784 // TODO: We could try to match extracting the higher bytes, which would be
2785 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
2786 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
2787 // about in practice.
2788 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
2789 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
2790 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
2791 DCI.AddToWorklist(Cvt.getNode());
2792 return Cvt;
2793 }
2794 }
2795
Matt Arsenault364a6742014-06-11 17:50:44 +00002796 return SDValue();
2797}
2798
Eric Christopher6c5b5112015-03-11 18:43:21 +00002799/// \brief Return true if the given offset Size in bytes can be folded into
2800/// the immediate offsets of a memory instruction for the given address space.
2801static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002802 const SISubtarget &STI) {
Eric Christopher6c5b5112015-03-11 18:43:21 +00002803 switch (AS) {
2804 case AMDGPUAS::GLOBAL_ADDRESS: {
2805 // MUBUF instructions a 12-bit offset in bytes.
2806 return isUInt<12>(OffsetSize);
2807 }
2808 case AMDGPUAS::CONSTANT_ADDRESS: {
2809 // SMRD instructions have an 8-bit offset in dwords on SI and
2810 // a 20-bit offset in bytes on VI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002811 if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Eric Christopher6c5b5112015-03-11 18:43:21 +00002812 return isUInt<20>(OffsetSize);
2813 else
2814 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
2815 }
2816 case AMDGPUAS::LOCAL_ADDRESS:
2817 case AMDGPUAS::REGION_ADDRESS: {
2818 // The single offset versions have a 16-bit offset in bytes.
2819 return isUInt<16>(OffsetSize);
2820 }
2821 case AMDGPUAS::PRIVATE_ADDRESS:
2822 // Indirect register addressing does not use any offsets.
2823 default:
2824 return 0;
2825 }
2826}
2827
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002828// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
2829
2830// This is a variant of
2831// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
2832//
2833// The normal DAG combiner will do this, but only if the add has one use since
2834// that would increase the number of instructions.
2835//
2836// This prevents us from seeing a constant offset that can be folded into a
2837// memory instruction's addressing mode. If we know the resulting add offset of
2838// a pointer can be folded into an addressing offset, we can replace the pointer
2839// operand with the add of new constant offset. This eliminates one of the uses,
2840// and may allow the remaining use to also be simplified.
2841//
2842SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
2843 unsigned AddrSpace,
2844 DAGCombinerInfo &DCI) const {
2845 SDValue N0 = N->getOperand(0);
2846 SDValue N1 = N->getOperand(1);
2847
2848 if (N0.getOpcode() != ISD::ADD)
2849 return SDValue();
2850
2851 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
2852 if (!CN1)
2853 return SDValue();
2854
2855 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2856 if (!CAdd)
2857 return SDValue();
2858
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002859 // If the resulting offset is too large, we can't fold it into the addressing
2860 // mode offset.
2861 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002862 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget()))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002863 return SDValue();
2864
2865 SelectionDAG &DAG = DCI.DAG;
2866 SDLoc SL(N);
2867 EVT VT = N->getValueType(0);
2868
2869 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002870 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002871
2872 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
2873}
2874
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002875SDValue SITargetLowering::performAndCombine(SDNode *N,
2876 DAGCombinerInfo &DCI) const {
2877 if (DCI.isBeforeLegalize())
2878 return SDValue();
2879
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002880 if (SDValue Base = AMDGPUTargetLowering::performAndCombine(N, DCI))
2881 return Base;
2882
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002883 SelectionDAG &DAG = DCI.DAG;
2884
2885 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
2886 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
2887 SDValue LHS = N->getOperand(0);
2888 SDValue RHS = N->getOperand(1);
2889
2890 if (LHS.getOpcode() == ISD::SETCC &&
2891 RHS.getOpcode() == ISD::SETCC) {
2892 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
2893 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
2894
2895 SDValue X = LHS.getOperand(0);
2896 SDValue Y = RHS.getOperand(0);
2897 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
2898 return SDValue();
2899
2900 if (LCC == ISD::SETO) {
2901 if (X != LHS.getOperand(1))
2902 return SDValue();
2903
2904 if (RCC == ISD::SETUNE) {
2905 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
2906 if (!C1 || !C1->isInfinity() || C1->isNegative())
2907 return SDValue();
2908
2909 const uint32_t Mask = SIInstrFlags::N_NORMAL |
2910 SIInstrFlags::N_SUBNORMAL |
2911 SIInstrFlags::N_ZERO |
2912 SIInstrFlags::P_ZERO |
2913 SIInstrFlags::P_SUBNORMAL |
2914 SIInstrFlags::P_NORMAL;
2915
2916 static_assert(((~(SIInstrFlags::S_NAN |
2917 SIInstrFlags::Q_NAN |
2918 SIInstrFlags::N_INFINITY |
2919 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
2920 "mask not equal");
2921
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002922 SDLoc DL(N);
2923 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2924 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002925 }
2926 }
2927 }
2928
2929 return SDValue();
2930}
2931
Matt Arsenaultf2290332015-01-06 23:00:39 +00002932SDValue SITargetLowering::performOrCombine(SDNode *N,
2933 DAGCombinerInfo &DCI) const {
2934 SelectionDAG &DAG = DCI.DAG;
2935 SDValue LHS = N->getOperand(0);
2936 SDValue RHS = N->getOperand(1);
2937
Matt Arsenault3b082382016-04-12 18:24:38 +00002938 EVT VT = N->getValueType(0);
2939 if (VT == MVT::i64) {
2940 // TODO: This could be a generic combine with a predicate for extracting the
2941 // high half of an integer being free.
2942
2943 // (or i64:x, (zero_extend i32:y)) ->
2944 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
2945 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
2946 RHS.getOpcode() != ISD::ZERO_EXTEND)
2947 std::swap(LHS, RHS);
2948
2949 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
2950 SDValue ExtSrc = RHS.getOperand(0);
2951 EVT SrcVT = ExtSrc.getValueType();
2952 if (SrcVT == MVT::i32) {
2953 SDLoc SL(N);
2954 SDValue LowLHS, HiBits;
2955 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
2956 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
2957
2958 DCI.AddToWorklist(LowOr.getNode());
2959 DCI.AddToWorklist(HiBits.getNode());
2960
2961 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
2962 LowOr, HiBits);
2963 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2964 }
2965 }
2966 }
2967
Matt Arsenaultf2290332015-01-06 23:00:39 +00002968 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
2969 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
2970 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
2971 SDValue Src = LHS.getOperand(0);
2972 if (Src != RHS.getOperand(0))
2973 return SDValue();
2974
2975 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
2976 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
2977 if (!CLHS || !CRHS)
2978 return SDValue();
2979
2980 // Only 10 bits are used.
2981 static const uint32_t MaxMask = 0x3ff;
2982
2983 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002984 SDLoc DL(N);
2985 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2986 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00002987 }
2988
2989 return SDValue();
2990}
2991
2992SDValue SITargetLowering::performClassCombine(SDNode *N,
2993 DAGCombinerInfo &DCI) const {
2994 SelectionDAG &DAG = DCI.DAG;
2995 SDValue Mask = N->getOperand(1);
2996
2997 // fp_class x, 0 -> false
2998 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
2999 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003000 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003001 }
3002
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003003 if (N->getOperand(0).isUndef())
3004 return DAG.getUNDEF(MVT::i1);
3005
Matt Arsenaultf2290332015-01-06 23:00:39 +00003006 return SDValue();
3007}
3008
Matt Arsenault9cd90712016-04-14 01:42:16 +00003009// Constant fold canonicalize.
3010SDValue SITargetLowering::performFCanonicalizeCombine(
3011 SDNode *N,
3012 DAGCombinerInfo &DCI) const {
3013 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
3014 if (!CFP)
3015 return SDValue();
3016
3017 SelectionDAG &DAG = DCI.DAG;
3018 const APFloat &C = CFP->getValueAPF();
3019
3020 // Flush denormals to 0 if not enabled.
3021 if (C.isDenormal()) {
3022 EVT VT = N->getValueType(0);
3023 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
3024 return DAG.getConstantFP(0.0, SDLoc(N), VT);
3025
3026 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
3027 return DAG.getConstantFP(0.0, SDLoc(N), VT);
3028 }
3029
3030 if (C.isNaN()) {
3031 EVT VT = N->getValueType(0);
3032 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
3033 if (C.isSignaling()) {
3034 // Quiet a signaling NaN.
3035 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3036 }
3037
3038 // Make sure it is the canonical NaN bitpattern.
3039 //
3040 // TODO: Can we use -1 as the canonical NaN value since it's an inline
3041 // immediate?
3042 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
3043 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3044 }
3045
3046 return SDValue(CFP, 0);
3047}
3048
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003049static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
3050 switch (Opc) {
3051 case ISD::FMAXNUM:
3052 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003053 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003054 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003055 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003056 return AMDGPUISD::UMAX3;
3057 case ISD::FMINNUM:
3058 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003059 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003060 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003061 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003062 return AMDGPUISD::UMIN3;
3063 default:
3064 llvm_unreachable("Not a min/max opcode");
3065 }
3066}
3067
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003068static SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3069 SDValue Op0, SDValue Op1, bool Signed) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003070 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
3071 if (!K1)
3072 return SDValue();
3073
3074 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
3075 if (!K0)
3076 return SDValue();
3077
Matt Arsenaultf639c322016-01-28 20:53:42 +00003078 if (Signed) {
3079 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
3080 return SDValue();
3081 } else {
3082 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
3083 return SDValue();
3084 }
3085
3086 EVT VT = K0->getValueType(0);
3087 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
3088 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
3089}
3090
3091static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
3092 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
3093 return true;
3094
3095 return DAG.isKnownNeverNaN(Op);
3096}
3097
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003098static SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3099 SDValue Op0, SDValue Op1) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003100 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
3101 if (!K1)
3102 return SDValue();
3103
3104 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
3105 if (!K0)
3106 return SDValue();
3107
3108 // Ordered >= (although NaN inputs should have folded away by now).
3109 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
3110 if (Cmp == APFloat::cmpGreaterThan)
3111 return SDValue();
3112
3113 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
3114 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
3115 // give the other result, which is different from med3 with a NaN input.
3116 SDValue Var = Op0.getOperand(0);
3117 if (!isKnownNeverSNan(DAG, Var))
3118 return SDValue();
3119
3120 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
3121 Var, SDValue(K0, 0), SDValue(K1, 0));
3122}
3123
3124SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
3125 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003126 SelectionDAG &DAG = DCI.DAG;
3127
3128 unsigned Opc = N->getOpcode();
3129 SDValue Op0 = N->getOperand(0);
3130 SDValue Op1 = N->getOperand(1);
3131
3132 // Only do this if the inner op has one use since this will just increases
3133 // register pressure for no benefit.
3134
Matt Arsenault5b39b342016-01-28 20:53:48 +00003135 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
3136 // max(max(a, b), c) -> max3(a, b, c)
3137 // min(min(a, b), c) -> min3(a, b, c)
3138 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
3139 SDLoc DL(N);
3140 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3141 DL,
3142 N->getValueType(0),
3143 Op0.getOperand(0),
3144 Op0.getOperand(1),
3145 Op1);
3146 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003147
Matt Arsenault5b39b342016-01-28 20:53:48 +00003148 // Try commuted.
3149 // max(a, max(b, c)) -> max3(a, b, c)
3150 // min(a, min(b, c)) -> min3(a, b, c)
3151 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
3152 SDLoc DL(N);
3153 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3154 DL,
3155 N->getValueType(0),
3156 Op0,
3157 Op1.getOperand(0),
3158 Op1.getOperand(1));
3159 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003160 }
3161
Matt Arsenaultf639c322016-01-28 20:53:42 +00003162 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
3163 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
3164 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
3165 return Med3;
3166 }
3167
3168 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
3169 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
3170 return Med3;
3171 }
3172
3173 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00003174 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
3175 (Opc == AMDGPUISD::FMIN_LEGACY &&
3176 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00003177 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
3178 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
3179 return Res;
3180 }
3181
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003182 return SDValue();
3183}
3184
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003185SDValue SITargetLowering::performSetCCCombine(SDNode *N,
3186 DAGCombinerInfo &DCI) const {
3187 SelectionDAG &DAG = DCI.DAG;
3188 SDLoc SL(N);
3189
3190 SDValue LHS = N->getOperand(0);
3191 SDValue RHS = N->getOperand(1);
3192 EVT VT = LHS.getValueType();
3193
3194 if (VT != MVT::f32 && VT != MVT::f64)
3195 return SDValue();
3196
3197 // Match isinf pattern
3198 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
3199 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
3200 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
3201 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
3202 if (!CRHS)
3203 return SDValue();
3204
3205 const APFloat &APF = CRHS->getValueAPF();
3206 if (APF.isInfinity() && !APF.isNegative()) {
3207 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003208 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
3209 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003210 }
3211 }
3212
3213 return SDValue();
3214}
3215
Tom Stellard75aadc22012-12-11 21:25:42 +00003216SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
3217 DAGCombinerInfo &DCI) const {
3218 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003219 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00003220
3221 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00003222 default:
3223 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003224 case ISD::SETCC:
3225 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00003226 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003227 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003228 case ISD::SMAX:
3229 case ISD::SMIN:
3230 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00003231 case ISD::UMIN:
3232 case AMDGPUISD::FMIN_LEGACY:
3233 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003234 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00003235 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003236 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00003237 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003238 break;
3239 }
Matt Arsenault364a6742014-06-11 17:50:44 +00003240
3241 case AMDGPUISD::CVT_F32_UBYTE0:
3242 case AMDGPUISD::CVT_F32_UBYTE1:
3243 case AMDGPUISD::CVT_F32_UBYTE2:
3244 case AMDGPUISD::CVT_F32_UBYTE3: {
3245 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
Matt Arsenault364a6742014-06-11 17:50:44 +00003246 SDValue Src = N->getOperand(0);
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003247
Matt Arsenault327bb5a2016-07-01 22:47:50 +00003248 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero.
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003249 if (Src.getOpcode() == ISD::SRL) {
3250 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
3251 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
3252 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x
3253
3254 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(1))) {
3255 unsigned SrcOffset = C->getZExtValue() + 8 * Offset;
3256 if (SrcOffset < 32 && SrcOffset % 8 == 0) {
3257 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, DL,
3258 MVT::f32, Src.getOperand(0));
3259 }
3260 }
3261 }
3262
Matt Arsenault364a6742014-06-11 17:50:44 +00003263 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
3264
3265 APInt KnownZero, KnownOne;
3266 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
3267 !DCI.isBeforeLegalizeOps());
3268 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3269 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
3270 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
3271 DCI.CommitTargetLoweringOpt(TLO);
3272 }
3273
3274 break;
3275 }
3276
3277 case ISD::UINT_TO_FP: {
3278 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003279 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003280 case ISD::FADD: {
3281 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3282 break;
3283
3284 EVT VT = N->getValueType(0);
3285 if (VT != MVT::f32)
3286 break;
3287
Matt Arsenault8d630032015-02-20 22:10:41 +00003288 // Only do this if we are not trying to support denormals. v_mad_f32 does
3289 // not support denormals ever.
3290 if (Subtarget->hasFP32Denormals())
3291 break;
3292
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003293 SDValue LHS = N->getOperand(0);
3294 SDValue RHS = N->getOperand(1);
3295
3296 // These should really be instruction patterns, but writing patterns with
3297 // source modiifiers is a pain.
3298
3299 // fadd (fadd (a, a), b) -> mad 2.0, a, b
3300 if (LHS.getOpcode() == ISD::FADD) {
3301 SDValue A = LHS.getOperand(0);
3302 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003303 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003304 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003305 }
3306 }
3307
3308 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
3309 if (RHS.getOpcode() == ISD::FADD) {
3310 SDValue A = RHS.getOperand(0);
3311 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003312 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003313 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003314 }
3315 }
3316
Matt Arsenault8d630032015-02-20 22:10:41 +00003317 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003318 }
Matt Arsenault8675db12014-08-29 16:01:14 +00003319 case ISD::FSUB: {
3320 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3321 break;
3322
3323 EVT VT = N->getValueType(0);
3324
3325 // Try to get the fneg to fold into the source modifier. This undoes generic
3326 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00003327 //
3328 // Only do this if we are not trying to support denormals. v_mad_f32 does
3329 // not support denormals ever.
3330 if (VT == MVT::f32 &&
3331 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00003332 SDValue LHS = N->getOperand(0);
3333 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003334 if (LHS.getOpcode() == ISD::FADD) {
3335 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
3336
3337 SDValue A = LHS.getOperand(0);
3338 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003339 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003340 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
3341
Matt Arsenault8d630032015-02-20 22:10:41 +00003342 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003343 }
3344 }
3345
3346 if (RHS.getOpcode() == ISD::FADD) {
3347 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
3348
3349 SDValue A = RHS.getOperand(0);
3350 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003351 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003352 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003353 }
3354 }
Matt Arsenault8d630032015-02-20 22:10:41 +00003355
3356 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00003357 }
3358
3359 break;
3360 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003361 case ISD::LOAD:
3362 case ISD::STORE:
3363 case ISD::ATOMIC_LOAD:
3364 case ISD::ATOMIC_STORE:
3365 case ISD::ATOMIC_CMP_SWAP:
3366 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
3367 case ISD::ATOMIC_SWAP:
3368 case ISD::ATOMIC_LOAD_ADD:
3369 case ISD::ATOMIC_LOAD_SUB:
3370 case ISD::ATOMIC_LOAD_AND:
3371 case ISD::ATOMIC_LOAD_OR:
3372 case ISD::ATOMIC_LOAD_XOR:
3373 case ISD::ATOMIC_LOAD_NAND:
3374 case ISD::ATOMIC_LOAD_MIN:
3375 case ISD::ATOMIC_LOAD_MAX:
3376 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00003377 case ISD::ATOMIC_LOAD_UMAX:
3378 case AMDGPUISD::ATOMIC_INC:
3379 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003380 if (DCI.isBeforeLegalize())
3381 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003382
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003383 MemSDNode *MemNode = cast<MemSDNode>(N);
3384 SDValue Ptr = MemNode->getBasePtr();
3385
3386 // TODO: We could also do this for multiplies.
3387 unsigned AS = MemNode->getAddressSpace();
3388 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
3389 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
3390 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00003391 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003392
3393 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
3394 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
3395 }
3396 }
3397 break;
3398 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003399 case ISD::AND:
3400 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003401 case ISD::OR:
3402 return performOrCombine(N, DCI);
3403 case AMDGPUISD::FP_CLASS:
3404 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00003405 case ISD::FCANONICALIZE:
3406 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003407 case AMDGPUISD::FRACT:
3408 case AMDGPUISD::RCP:
3409 case AMDGPUISD::RSQ:
Matt Arsenault32fc5272016-07-26 16:45:45 +00003410 case AMDGPUISD::RCP_LEGACY:
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003411 case AMDGPUISD::RSQ_LEGACY:
3412 case AMDGPUISD::RSQ_CLAMP:
3413 case AMDGPUISD::LDEXP: {
3414 SDValue Src = N->getOperand(0);
3415 if (Src.isUndef())
3416 return Src;
3417 break;
3418 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003419 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003420 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00003421}
Christian Konigd910b7d2013-02-26 17:52:16 +00003422
Christian Konig8e06e2a2013-04-10 08:39:08 +00003423/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00003424static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00003425 switch (Idx) {
3426 default: return 0;
3427 case AMDGPU::sub0: return 0;
3428 case AMDGPU::sub1: return 1;
3429 case AMDGPU::sub2: return 2;
3430 case AMDGPU::sub3: return 3;
3431 }
3432}
3433
3434/// \brief Adjust the writemask of MIMG instructions
3435void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
3436 SelectionDAG &DAG) const {
3437 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00003438 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003439 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
3440 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00003441 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003442
3443 // Try to figure out the used register components
3444 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
3445 I != E; ++I) {
3446
3447 // Abort if we can't understand the usage
3448 if (!I->isMachineOpcode() ||
3449 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
3450 return;
3451
Tom Stellard54774e52013-10-23 02:53:47 +00003452 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
3453 // Note that subregs are packed, i.e. Lane==0 is the first bit set
3454 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
3455 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00003456 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00003457
Tom Stellard54774e52013-10-23 02:53:47 +00003458 // Set which texture component corresponds to the lane.
3459 unsigned Comp;
3460 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
3461 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00003462 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00003463 Dmask &= ~(1 << Comp);
3464 }
3465
Christian Konig8e06e2a2013-04-10 08:39:08 +00003466 // Abort if we have more than one user per component
3467 if (Users[Lane])
3468 return;
3469
3470 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00003471 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003472 }
3473
Tom Stellard54774e52013-10-23 02:53:47 +00003474 // Abort if there's no change
3475 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00003476 return;
3477
3478 // Adjust the writemask in the node
3479 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003480 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003481 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003482 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00003483 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003484
Christian Konig8b1ed282013-04-10 08:39:16 +00003485 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00003486 // (if NewDmask has only one bit set...)
3487 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003488 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
3489 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00003490 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003491 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00003492 SDValue(Node, 0), RC);
3493 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
3494 return;
3495 }
3496
Christian Konig8e06e2a2013-04-10 08:39:08 +00003497 // Update the users of the node with the new indices
3498 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
3499
3500 SDNode *User = Users[i];
3501 if (!User)
3502 continue;
3503
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003504 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003505 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
3506
3507 switch (Idx) {
3508 default: break;
3509 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
3510 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
3511 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
3512 }
3513 }
3514}
3515
Tom Stellardc98ee202015-07-16 19:40:07 +00003516static bool isFrameIndexOp(SDValue Op) {
3517 if (Op.getOpcode() == ISD::AssertZext)
3518 Op = Op.getOperand(0);
3519
3520 return isa<FrameIndexSDNode>(Op);
3521}
3522
Tom Stellard3457a842014-10-09 19:06:00 +00003523/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
3524/// with frame index operands.
3525/// LLVM assumes that inputs are to these instructions are registers.
3526void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
3527 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003528
3529 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00003530 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00003531 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00003532 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003533 continue;
3534 }
3535
Tom Stellard3457a842014-10-09 19:06:00 +00003536 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003537 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00003538 Node->getOperand(i).getValueType(),
3539 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003540 }
3541
Tom Stellard3457a842014-10-09 19:06:00 +00003542 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003543}
3544
Matt Arsenault08d84942014-06-03 23:06:13 +00003545/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00003546SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
3547 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003548 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003549 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00003550
Nicolai Haehnlec06bfa12016-07-11 21:59:43 +00003551 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() &&
3552 !TII->isGather4(Opcode))
Christian Konig8e06e2a2013-04-10 08:39:08 +00003553 adjustWritemask(Node, DAG);
3554
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003555 if (Opcode == AMDGPU::INSERT_SUBREG ||
3556 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003557 legalizeTargetIndependentNode(Node, DAG);
3558 return Node;
3559 }
Tom Stellard654d6692015-01-08 15:08:17 +00003560 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003561}
Christian Konig8b1ed282013-04-10 08:39:16 +00003562
3563/// \brief Assign the register class depending on the number of
3564/// bits set in the writemask
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003565void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
Christian Konig8b1ed282013-04-10 08:39:16 +00003566 SDNode *Node) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003567 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003568
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003569 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003570
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003571 if (TII->isVOP3(MI.getOpcode())) {
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003572 // Make sure constant bus requirements are respected.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003573 TII->legalizeOperandsVOP3(MRI, MI);
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003574 return;
3575 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00003576
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003577 if (TII->isMIMG(MI)) {
3578 unsigned VReg = MI.getOperand(0).getReg();
3579 unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4;
3580 unsigned Writemask = MI.getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003581 unsigned BitsSet = 0;
3582 for (unsigned i = 0; i < 4; ++i)
3583 BitsSet += Writemask & (1 << i) ? 1 : 0;
3584
3585 const TargetRegisterClass *RC;
3586 switch (BitsSet) {
3587 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00003588 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003589 case 2: RC = &AMDGPU::VReg_64RegClass; break;
3590 case 3: RC = &AMDGPU::VReg_96RegClass; break;
3591 }
3592
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003593 unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet);
3594 MI.setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003595 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00003596 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00003597 }
3598
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003599 // Replace unused atomics with the no return version.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003600 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003601 if (NoRetAtomicOp != -1) {
3602 if (!Node->hasAnyUseOfValue(0)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003603 MI.setDesc(TII->get(NoRetAtomicOp));
3604 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003605 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003606 }
3607
Tom Stellard354a43c2016-04-01 18:27:37 +00003608 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
3609 // instruction, because the return type of these instructions is a vec2 of
3610 // the memory type, so it can be tied to the input operand.
3611 // This means these instructions always have a use, so we need to add a
3612 // special case to check if the atomic has only one extract_subreg use,
3613 // which itself has no uses.
3614 if ((Node->hasNUsesOfValue(1, 0) &&
Nicolai Haehnle750082d2016-04-15 14:42:36 +00003615 Node->use_begin()->isMachineOpcode() &&
Tom Stellard354a43c2016-04-01 18:27:37 +00003616 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
3617 !Node->use_begin()->hasAnyUseOfValue(0))) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003618 unsigned Def = MI.getOperand(0).getReg();
Tom Stellard354a43c2016-04-01 18:27:37 +00003619
3620 // Change this into a noret atomic.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003621 MI.setDesc(TII->get(NoRetAtomicOp));
3622 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003623
3624 // If we only remove the def operand from the atomic instruction, the
3625 // extract_subreg will be left with a use of a vreg without a def.
3626 // So we need to insert an implicit_def to avoid machine verifier
3627 // errors.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003628 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
Tom Stellard354a43c2016-04-01 18:27:37 +00003629 TII->get(AMDGPU::IMPLICIT_DEF), Def);
3630 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003631 return;
3632 }
Christian Konig8b1ed282013-04-10 08:39:16 +00003633}
Tom Stellard0518ff82013-06-03 17:39:58 +00003634
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003635static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL,
3636 uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003637 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00003638 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
3639}
3640
3641MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003642 const SDLoc &DL,
Matt Arsenault485defe2014-11-05 19:01:17 +00003643 SDValue Ptr) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003644 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault485defe2014-11-05 19:01:17 +00003645
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003646 // Build the half of the subregister with the constants before building the
3647 // full 128-bit register. If we are building multiple resource descriptors,
3648 // this will allow CSEing of the 2-component register.
3649 const SDValue Ops0[] = {
3650 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
3651 buildSMovImm32(DAG, DL, 0),
3652 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
3653 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
3654 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
3655 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003656
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003657 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
3658 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00003659
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003660 // Combine the constants and the pointer.
3661 const SDValue Ops1[] = {
3662 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
3663 Ptr,
3664 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
3665 SubRegHi,
3666 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
3667 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003668
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003669 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00003670}
3671
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003672/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003673/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
3674/// of the resource descriptor) to create an offset, which is added to
3675/// the resource pointer.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003676MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL,
3677 SDValue Ptr, uint32_t RsrcDword1,
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003678 uint64_t RsrcDword2And3) const {
3679 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
3680 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
3681 if (RsrcDword1) {
3682 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003683 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
3684 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003685 }
3686
3687 SDValue DataLo = buildSMovImm32(DAG, DL,
3688 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
3689 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
3690
3691 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003692 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003693 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003694 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003695 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003696 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003697 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003698 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003699 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003700 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003701 };
3702
3703 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
3704}
3705
Tom Stellard94593ee2013-06-03 17:40:18 +00003706SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3707 const TargetRegisterClass *RC,
3708 unsigned Reg, EVT VT) const {
3709 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
3710
3711 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
3712 cast<RegisterSDNode>(VReg)->getReg(), VT);
3713}
Tom Stellardd7e6f132015-04-08 01:09:26 +00003714
3715//===----------------------------------------------------------------------===//
3716// SI Inline Assembly Support
3717//===----------------------------------------------------------------------===//
3718
3719std::pair<unsigned, const TargetRegisterClass *>
3720SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003721 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00003722 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003723
3724 if (Constraint.size() == 1) {
3725 switch (Constraint[0]) {
3726 case 's':
3727 case 'r':
3728 switch (VT.getSizeInBits()) {
3729 default:
3730 return std::make_pair(0U, nullptr);
3731 case 32:
Tom Stellardd7e6f132015-04-08 01:09:26 +00003732 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003733 case 64:
3734 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
3735 case 128:
3736 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
3737 case 256:
3738 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
3739 }
3740
3741 case 'v':
3742 switch (VT.getSizeInBits()) {
3743 default:
3744 return std::make_pair(0U, nullptr);
3745 case 32:
3746 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
3747 case 64:
3748 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
3749 case 96:
3750 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
3751 case 128:
3752 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
3753 case 256:
3754 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
3755 case 512:
3756 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
3757 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00003758 }
3759 }
3760
3761 if (Constraint.size() > 1) {
3762 const TargetRegisterClass *RC = nullptr;
3763 if (Constraint[1] == 'v') {
3764 RC = &AMDGPU::VGPR_32RegClass;
3765 } else if (Constraint[1] == 's') {
3766 RC = &AMDGPU::SGPR_32RegClass;
3767 }
3768
3769 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00003770 uint32_t Idx;
3771 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
3772 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00003773 return std::make_pair(RC->getRegister(Idx), RC);
3774 }
3775 }
3776 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
3777}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003778
3779SITargetLowering::ConstraintType
3780SITargetLowering::getConstraintType(StringRef Constraint) const {
3781 if (Constraint.size() == 1) {
3782 switch (Constraint[0]) {
3783 default: break;
3784 case 's':
3785 case 'v':
3786 return C_RegisterClass;
3787 }
3788 }
3789 return TargetLowering::getConstraintType(Constraint);
3790}